2
This file is part of the Nepomuk KDE project.
3
Copyright (C) 2010 Vishesh Handa <handa.vish@gmail.com>
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.
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.
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/>.
23
#include "backupmanager.h"
24
#include "backupmanageradaptor.h"
25
#include "logstorage.h"
28
#include "changelog.h"
30
#include "identificationset.h"
31
#include "backupgenerationjob.h"
33
#include <QtDBus/QDBusConnection>
34
#include <QtCore/QListIterator>
35
#include <QtCore/QTimer>
36
#include <QtCore/QDir>
39
#include <KStandardDirs>
41
#include <KConfigGroup>
45
#include <KCalendarSystem>
48
Nepomuk::BackupManager::BackupManager(QObject* parent)
50
m_config( "nepomukbackuprc" )
52
new BackupManagerAdaptor( this );
54
QDBusConnection con = QDBusConnection::sessionBus();
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()) );
73
Nepomuk::BackupManager::~BackupManager()
78
void Nepomuk::BackupManager::backup(const QString& oldUrl)
82
url = KStandardDirs::locateLocal( "data", "nepomuk/backupsync/backup" ); // default location
88
KJob * job = new BackupGenerationJob( url, this );
90
connect( job, SIGNAL(finished(KJob*)), this, SLOT(slotBackupDone(KJob*)) );
94
void Nepomuk::BackupManager::automatedBackup()
96
QDate today = QDate::currentDate();
97
backup( m_backupLocation + today.toString(Qt::ISODate) );
103
void Nepomuk::BackupManager::slotConfigDirty()
106
m_config.reparseConfiguration();
108
QString freq = m_config.group("Backup").readEntry( "backup frequency", QString("disabled") );
109
kDebug() << "Frequency : " << freq;
111
if( freq == QLatin1String("disabled") ) {
112
kDebug() << "Auto Backups Disabled";
117
QString timeString = m_config.group("Backup").readEntry( "backup time", QTime().toString( Qt::ISODate ) );
118
m_backupTime = QTime::fromString( timeString, Qt::ISODate );
120
if( freq == QLatin1String("daily") ) {
121
m_daysBetweenBackups = 0;
124
else if( freq == QLatin1String("weekly") ) {
126
const KCalendarSystem* cal = KGlobal::locale()->calendar();
128
int backupDay = m_config.group("Backup").readEntry( "backup day", 0 );
129
int dayOfWeek = cal->dayOfWeek( QDate::currentDate() );
131
kDebug() << "DayOfWeek: " << dayOfWeek;
132
kDebug() << "BackupDay: " << backupDay;
133
if( dayOfWeek < backupDay ) {
134
m_daysBetweenBackups = backupDay - dayOfWeek;
136
else if( dayOfWeek > backupDay ) {
137
m_daysBetweenBackups = cal->daysInWeek( QDate::currentDate() ) - dayOfWeek + backupDay;
140
if( QTime::currentTime() <= m_backupTime )
141
m_daysBetweenBackups = 0;
143
m_daysBetweenBackups = cal->daysInWeek( QDate::currentDate() );
146
kDebug() << "Days between backups : " << m_daysBetweenBackups;
149
else if( freq == QLatin1String("monthly") ) {
150
//TODO: Implement me!
153
m_maxBackups = m_config.group("Backup").readEntry<int>("max backups", 1);
155
// Remove old timers and start new
160
void Nepomuk::BackupManager::resetTimer()
162
if( m_backupTime.isNull() && m_daysBetweenBackups == 0 ) {
163
// Never perform automated backups
167
QDateTime current = QDateTime::currentDateTime();
168
QDateTime dateTime = current.addDays( m_daysBetweenBackups );
169
dateTime.setTime( m_backupTime );
171
if( dateTime < current ) {
172
dateTime = dateTime.addDays( 1 );
175
int msecs = current.msecsTo( dateTime );
178
m_timer.start( msecs );
179
kDebug() << "Setting timer for " << msecs/1000.0/60/60 << " hours";
182
void Nepomuk::BackupManager::removeOldBackups()
184
QDir dir( m_backupLocation );
185
QStringList infoList = dir.entryList( QDir::Files | QDir::NoDotAndDotDot, QDir::Name );
187
while( infoList.size() > m_maxBackups ) {
188
const QString backupPath = m_backupLocation + infoList.last();
189
kDebug() << "Removing : " << backupPath;
190
QFile::remove( backupPath );
195
void Nepomuk::BackupManager::slotBackupDone(KJob* job)
197
if( !job->error() ) {
204
#include "backupmanager.moc"