• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • sources
  • kde-4.12
  • kdelibs
  • nepomuk-core
  • services
  • storage
  • backup
backupmanager.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Nepomuk KDE project.
3  Copyright (C) 2010 Vishesh Handa <handa.vish@gmail.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) version 3, or any
9  later version accepted by the membership of KDE e.V. (or its
10  successor approved by the membership of KDE e.V.), which shall
11  act as a proxy defined in Section 6 of version 3 of the license.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 
23 #include "backupmanager.h"
24 #include "backupmanageradaptor.h"
25 #include "storage.h"
26 
27 #include "backupgenerationjob.h"
28 #include "backuprestorationjob.h"
29 
30 #include <QtCore/QListIterator>
31 #include <QtCore/QTimer>
32 #include <QtCore/QDir>
33 #include <QtCore/QThread>
34 
35 #include <KDebug>
36 #include <KStandardDirs>
37 #include <KConfig>
38 #include <KConfigGroup>
39 #include <KDirWatch>
40 #include <KGlobal>
41 #include <KLocale>
42 #include <KCalendarSystem>
43 #include <kdbusconnectionpool.h>
44 
45 
46 Nepomuk2::BackupManager::BackupManager(Nepomuk2::Storage* storageService)
47  : QObject( storageService ),
48  m_config( "nepomukbackuprc" ),
49  m_model( storageService->model() ),
50  m_storageService( storageService )
51 {
52  new BackupManagerAdaptor( this );
53  // Register via DBUs
54  QDBusConnection con = KDBusConnectionPool::threadConnection();
55  con.registerObject( QLatin1String("/backupmanager"), this );
56 
57  m_backupLocation = KStandardDirs::locateLocal( "data", "nepomuk/backupsync/backups/" );
58  m_daysBetweenBackups = 0;
59 
60  KDirWatch* dirWatch = KDirWatch::self();
61  connect( dirWatch, SIGNAL( dirty( const QString& ) ),
62  this, SLOT( slotConfigDirty() ) );
63  connect( dirWatch, SIGNAL( created( const QString& ) ),
64  this, SLOT( slotConfigDirty() ) );
65 
66  dirWatch->addFile( KStandardDirs::locateLocal( "config", m_config.name() ) );
67 
68  connect( &m_timer, SIGNAL(timeout()), this, SLOT(automatedBackup()) );
69  slotConfigDirty();
70 }
71 
72 
73 Nepomuk2::BackupManager::~BackupManager()
74 {
75 }
76 
77 
78 void Nepomuk2::BackupManager::backup(const QString& oldUrl)
79 {
80  QString url = oldUrl;
81  if( url.isEmpty() )
82  url = KStandardDirs::locateLocal( "data", "nepomuk/backupsync/backup" ); // default location
83 
84  kDebug() << url;
85 
86  QFile::remove( url );
87 
88  KJob* job = new BackupGenerationJob( m_model, url );
89 
90  QThread* backupThread = new QThread( this );
91  job->moveToThread( backupThread );
92  job->setAutoDelete(false);
93  backupThread->start();
94 
95  connect( job, SIGNAL(finished(KJob*)), backupThread, SLOT(quit()), Qt::QueuedConnection );
96  connect( backupThread, SIGNAL(finished()), backupThread, SLOT(deleteLater()) );
97  connect( job, SIGNAL(finished(KJob*)), this, SLOT(slotBackupDone(KJob*)), Qt::QueuedConnection );
98  connect( job, SIGNAL(percent(KJob*,ulong)), this, SLOT(slotBackupPercent(KJob*,ulong)), Qt::QueuedConnection );
99  job->start();
100 
101  emit backupStarted();
102 }
103 
104 void Nepomuk2::BackupManager::backupTagsAndRatings(const QString& oldUrl)
105 {
106  QString url = oldUrl;
107  if( url.isEmpty() )
108  url = KStandardDirs::locateLocal( "data", "nepomuk/backupsync/backup" ); // default location
109 
110  kDebug() << url;
111 
112  QFile::remove( url );
113 
114  BackupGenerationJob* job = new BackupGenerationJob( m_model, url );
115  job->setFilter( BackupGenerationJob::Filter_TagsAndRatings );
116 
117  QThread* backupThread = new QThread( this );
118  job->moveToThread( backupThread );
119  job->setAutoDelete(false);
120  backupThread->start();
121 
122  connect( job, SIGNAL(finished(KJob*)), backupThread, SLOT(quit()), Qt::QueuedConnection );
123  connect( backupThread, SIGNAL(finished()), backupThread, SLOT(deleteLater()) );
124  connect( job, SIGNAL(finished(KJob*)), this, SLOT(slotBackupDone(KJob*)), Qt::QueuedConnection );
125  connect( job, SIGNAL(percent(KJob*,ulong)), this, SLOT(slotBackupPercent(KJob*,ulong)), Qt::QueuedConnection );
126  job->start();
127 
128  emit backupStarted();
129 }
130 
131 
132 
133 
134 void Nepomuk2::BackupManager::automatedBackup()
135 {
136  QDate today = QDate::currentDate();
137  backupTagsAndRatings( m_backupLocation + today.toString(Qt::ISODate) );
138 
139  resetTimer();
140  removeOldBackups();
141 }
142 
143 void Nepomuk2::BackupManager::slotConfigDirty()
144 {
145  //kDebug();
146  m_config.reparseConfiguration();
147 
148  QString freq = m_config.group("Backup").readEntry( "backup frequency", QString("disabled") );
149  //kDebug() << "Frequency : " << freq;
150 
151  if( freq == QLatin1String("disabled") ) {
152  //kDebug() << "Auto Backups Disabled";
153  m_timer.stop();
154  return;
155  }
156 
157  QString timeString = m_config.group("Backup").readEntry( "backup time", QTime().toString( Qt::ISODate ) );
158  m_backupTime = QTime::fromString( timeString, Qt::ISODate );
159 
160  if( freq == QLatin1String("daily") ) {
161  m_daysBetweenBackups = 0;
162  }
163 
164  else if( freq == QLatin1String("weekly") ) {
165 
166  const KCalendarSystem* cal = KGlobal::locale()->calendar();
167 
168  int backupDay = m_config.group("Backup").readEntry( "backup day", 0 );
169  int dayOfWeek = cal->dayOfWeek( QDate::currentDate() );
170 
171  //kDebug() << "DayOfWeek: " << dayOfWeek;
172  //kDebug() << "BackupDay: " << backupDay;
173  if( dayOfWeek < backupDay ) {
174  m_daysBetweenBackups = backupDay - dayOfWeek;
175  }
176  else if( dayOfWeek > backupDay ) {
177  m_daysBetweenBackups = cal->daysInWeek( QDate::currentDate() ) - dayOfWeek + backupDay;
178  }
179  else {
180  if( QTime::currentTime() <= m_backupTime )
181  m_daysBetweenBackups = 0;
182  else
183  m_daysBetweenBackups = cal->daysInWeek( QDate::currentDate() );
184  }
185 
186  kDebug() << "Days between backups : " << m_daysBetweenBackups;
187  }
188 
189  else if( freq == QLatin1String("monthly") ) {
190  //TODO: Implement me!
191  }
192 
193  m_maxBackups = m_config.group("Backup").readEntry<int>("max backups", 1);
194 
195  // Remove old timers and start new
196  resetTimer();
197  removeOldBackups();
198 }
199 
200 void Nepomuk2::BackupManager::resetTimer()
201 {
202  if( m_backupTime.isNull() && m_daysBetweenBackups == 0 ) {
203  // Never perform automated backups
204  return;
205  }
206 
207  QDateTime current = QDateTime::currentDateTime();
208  QDateTime dateTime = current.addDays( m_daysBetweenBackups );
209  dateTime.setTime( m_backupTime );
210 
211  if( dateTime < current ) {
212  dateTime = dateTime.addDays( 1 );
213  }
214 
215  int msecs = current.msecsTo( dateTime );
216 
217  m_timer.stop();
218  m_timer.start( msecs );
219  kDebug() << "Setting timer for " << msecs/1000.0/60/60 << " hours";
220 }
221 
222 void Nepomuk2::BackupManager::removeOldBackups()
223 {
224  QDir dir( m_backupLocation );
225  QStringList infoList = dir.entryList( QDir::Files | QDir::NoDotAndDotDot, QDir::Name );
226 
227  while( infoList.size() > m_maxBackups ) {
228  const QString backupPath = m_backupLocation + infoList.first();
229  kDebug() << "Removing : " << backupPath;
230  QFile::remove( backupPath );
231  infoList.pop_back();
232  }
233 }
234 
235 void Nepomuk2::BackupManager::slotBackupDone(KJob* job)
236 {
237  if( !job->error() ) {
238  emit backupDone();
239  }
240  else {
241  emit backupError(job->errorString());
242  }
243 
244 // FIXME: This causes a reliable crash! Why!
245 // delete job;
246 }
247 
248 void Nepomuk2::BackupManager::slotBackupPercent(KJob*, ulong percent)
249 {
250  kDebug() << "WEEEEEE" << percent;
251  emit backupPercent( percent );
252 }
253 
254 void Nepomuk2::BackupManager::restore(const QString& url)
255 {
256  // FIXME: Output an error?
257  if( url.isEmpty() )
258  return;
259 
260  KJob* job = new BackupRestorationJob( m_storageService, QUrl::fromLocalFile(url) );
261  job->start();
262 
263  connect( job, SIGNAL(finished(KJob*)), this, SLOT(slotRestorationDone(KJob*)) );
264  connect( job, SIGNAL(percent(KJob*,ulong)), this, SLOT(slotRestorationPercent(KJob*,ulong)), Qt::QueuedConnection );
265 }
266 
267 void Nepomuk2::BackupManager::slotRestorationPercent(KJob*, ulong percent)
268 {
269  kDebug() << percent;
270  emit restorePercent( percent );
271 }
272 
273 namespace {
274  bool removeDir(const QString & dirName) {
275  bool result = false;
276  QDir dir(dirName);
277 
278  if (dir.exists(dirName)) {
279  QFileInfoList list = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden |
280  QDir::AllDirs | QDir::Files, QDir::DirsFirst);
281  foreach(QFileInfo info, list) {
282  if( info.isDir() )
283  result = removeDir(info.absoluteFilePath());
284  else
285  result = QFile::remove(info.absoluteFilePath());
286 
287  if( !result )
288  return result;
289  }
290  result = dir.rmdir(dirName);
291  }
292  return result;
293  }
294 }
295 void Nepomuk2::BackupManager::slotRestorationDone(KJob* job)
296 {
297  if( job->error() ) {
298  emit restoreError(job->errorString());
299  }
300  else {
301  // Restoration was successful we can safely remove the old repository
302  BackupRestorationJob* brjob = qobject_cast<Nepomuk2::BackupRestorationJob*>(job);
303  if( brjob ) {
304  removeDir( brjob->oldRepositoryPath() );
305  }
306  emit restoreDone();
307  }
308 }
309 
310 
311 #include "backupmanager.moc"
Nepomuk2::BackupManager::backup
void backup(const QString &url=QString())
Definition: backupmanager.cpp:78
Nepomuk2::Storage
Definition: storage.h:35
Nepomuk2::BackupGenerationJob::Filter_TagsAndRatings
Definition: backupgenerationjob.h:39
backupmanager.h
QObject
Nepomuk2::BackupManager::~BackupManager
virtual ~BackupManager()
Definition: backupmanager.cpp:73
Nepomuk2::BackupManager::restore
void restore(const QString &url)
Definition: backupmanager.cpp:254
backuprestorationjob.h
Nepomuk2::BackupRestorationJob
Definition: backuprestorationjob.h:32
backupgenerationjob.h
storage.h
Nepomuk2::BackupManager::backupTagsAndRatings
void backupTagsAndRatings(const QString &url=QString())
Definition: backupmanager.cpp:104
Nepomuk2::BackupGenerationJob
Definition: backupgenerationjob.h:30
Nepomuk2::BackupGenerationJob::start
virtual void start()
Definition: backupgenerationjob.cpp:49
Nepomuk2::BackupGenerationJob::setFilter
void setFilter(Filter filter)
Definition: backupgenerationjob.cpp:54
Nepomuk2::BackupManager::BackupManager
BackupManager(Storage *storageService)
Definition: backupmanager.cpp:46
KJob
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk-Core

Skip menu "Nepomuk-Core"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal