24 #include "backupmanageradaptor.h"
30 #include <QtCore/QListIterator>
31 #include <QtCore/QTimer>
32 #include <QtCore/QDir>
33 #include <QtCore/QThread>
36 #include <KStandardDirs>
38 #include <KConfigGroup>
42 #include <KCalendarSystem>
43 #include <kdbusconnectionpool.h>
48 m_config(
"nepomukbackuprc" ),
49 m_model( storageService->model() ),
50 m_storageService( storageService )
52 new BackupManagerAdaptor(
this );
54 QDBusConnection con = KDBusConnectionPool::threadConnection();
55 con.registerObject( QLatin1String(
"/backupmanager"),
this );
57 m_backupLocation = KStandardDirs::locateLocal(
"data",
"nepomuk/backupsync/backups/" );
58 m_daysBetweenBackups = 0;
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() ) );
66 dirWatch->addFile( KStandardDirs::locateLocal(
"config", m_config.name() ) );
68 connect( &m_timer, SIGNAL(timeout()),
this, SLOT(automatedBackup()) );
82 url = KStandardDirs::locateLocal(
"data",
"nepomuk/backupsync/backup" );
90 QThread* backupThread =
new QThread(
this );
91 job->moveToThread( backupThread );
92 job->setAutoDelete(
false);
93 backupThread->start();
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 );
101 emit backupStarted();
106 QString url = oldUrl;
108 url = KStandardDirs::locateLocal(
"data",
"nepomuk/backupsync/backup" );
112 QFile::remove( url );
117 QThread* backupThread =
new QThread(
this );
118 job->moveToThread( backupThread );
119 job->setAutoDelete(
false);
120 backupThread->
start();
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 );
128 emit backupStarted();
134 void Nepomuk2::BackupManager::automatedBackup()
136 QDate today = QDate::currentDate();
137 backupTagsAndRatings( m_backupLocation + today.toString(Qt::ISODate) );
143 void Nepomuk2::BackupManager::slotConfigDirty()
146 m_config.reparseConfiguration();
148 QString freq = m_config.group(
"Backup").readEntry(
"backup frequency", QString(
"disabled") );
151 if( freq == QLatin1String(
"disabled") ) {
157 QString timeString = m_config.group(
"Backup").readEntry(
"backup time", QTime().toString( Qt::ISODate ) );
158 m_backupTime = QTime::fromString( timeString, Qt::ISODate );
160 if( freq == QLatin1String(
"daily") ) {
161 m_daysBetweenBackups = 0;
164 else if( freq == QLatin1String(
"weekly") ) {
166 const KCalendarSystem* cal = KGlobal::locale()->calendar();
168 int backupDay = m_config.group(
"Backup").readEntry(
"backup day", 0 );
169 int dayOfWeek = cal->dayOfWeek( QDate::currentDate() );
173 if( dayOfWeek < backupDay ) {
174 m_daysBetweenBackups = backupDay - dayOfWeek;
176 else if( dayOfWeek > backupDay ) {
177 m_daysBetweenBackups = cal->daysInWeek( QDate::currentDate() ) - dayOfWeek + backupDay;
180 if( QTime::currentTime() <= m_backupTime )
181 m_daysBetweenBackups = 0;
183 m_daysBetweenBackups = cal->daysInWeek( QDate::currentDate() );
186 kDebug() <<
"Days between backups : " << m_daysBetweenBackups;
189 else if( freq == QLatin1String(
"monthly") ) {
193 m_maxBackups = m_config.group(
"Backup").readEntry<
int>(
"max backups", 1);
200 void Nepomuk2::BackupManager::resetTimer()
202 if( m_backupTime.isNull() && m_daysBetweenBackups == 0 ) {
207 QDateTime current = QDateTime::currentDateTime();
208 QDateTime dateTime = current.addDays( m_daysBetweenBackups );
209 dateTime.setTime( m_backupTime );
211 if( dateTime < current ) {
212 dateTime = dateTime.addDays( 1 );
215 int msecs = current.msecsTo( dateTime );
218 m_timer.start( msecs );
219 kDebug() <<
"Setting timer for " << msecs/1000.0/60/60 <<
" hours";
222 void Nepomuk2::BackupManager::removeOldBackups()
224 QDir dir( m_backupLocation );
225 QStringList infoList = dir.entryList( QDir::Files | QDir::NoDotAndDotDot, QDir::Name );
227 while( infoList.size() > m_maxBackups ) {
228 const QString backupPath = m_backupLocation + infoList.first();
229 kDebug() <<
"Removing : " << backupPath;
230 QFile::remove( backupPath );
235 void Nepomuk2::BackupManager::slotBackupDone(
KJob* job)
237 if( !job->error() ) {
241 emit backupError(job->errorString());
248 void Nepomuk2::BackupManager::slotBackupPercent(
KJob*, ulong percent)
250 kDebug() <<
"WEEEEEE" << percent;
251 emit backupPercent( percent );
263 connect( job, SIGNAL(finished(
KJob*)),
this, SLOT(slotRestorationDone(
KJob*)) );
264 connect( job, SIGNAL(percent(
KJob*,ulong)),
this, SLOT(slotRestorationPercent(
KJob*,ulong)), Qt::QueuedConnection );
267 void Nepomuk2::BackupManager::slotRestorationPercent(
KJob*, ulong percent)
270 emit restorePercent( percent );
274 bool removeDir(
const QString & dirName) {
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) {
283 result = removeDir(info.absoluteFilePath());
285 result = QFile::remove(info.absoluteFilePath());
290 result = dir.rmdir(dirName);
295 void Nepomuk2::BackupManager::slotRestorationDone(
KJob* job)
298 emit restoreError(job->errorString());
304 removeDir( brjob->oldRepositoryPath() );
311 #include "backupmanager.moc"
void backup(const QString &url=QString())
void restore(const QString &url)
void backupTagsAndRatings(const QString &url=QString())
void setFilter(Filter filter)
BackupManager(Storage *storageService)