~ubuntu-branches/ubuntu/quantal/kde-runtime/quantal

« back to all changes in this revision

Viewing changes to nepomuk/services/backupsync/service/backupmanager.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2012-06-03 21:50:00 UTC
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: package-import@ubuntu.com-20120603215000-vn7oarsq0ynrydj5
Tags: upstream-4.8.80
Import upstream version 4.8.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 "logstorage.h"
26
 
#include "tools.h"
27
 
 
28
 
#include "changelog.h"
29
 
#include "syncfile.h"
30
 
#include "identificationset.h"
31
 
#include "backupgenerationjob.h"
32
 
 
33
 
#include <QtDBus/QDBusConnection>
34
 
#include <QtCore/QListIterator>
35
 
#include <QtCore/QTimer>
36
 
#include <QtCore/QDir>
37
 
 
38
 
#include <KDebug>
39
 
#include <KStandardDirs>
40
 
#include <KConfig>
41
 
#include <KConfigGroup>
42
 
#include <KDirWatch>
43
 
#include <KGlobal>
44
 
#include <KLocale>
45
 
#include <KCalendarSystem>
46
 
 
47
 
 
48
 
Nepomuk::BackupManager::BackupManager(QObject* parent)
49
 
    : QObject( parent ),
50
 
      m_config( "nepomukbackuprc" )
51
 
{
52
 
    new BackupManagerAdaptor( this );
53
 
    // Register via DBUs
54
 
    QDBusConnection con = QDBusConnection::sessionBus();
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
 
Nepomuk::BackupManager::~BackupManager()
74
 
{
75
 
}
76
 
 
77
 
 
78
 
void Nepomuk::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( url, this );
89
 
 
90
 
    connect( job, SIGNAL(finished(KJob*)), this, SLOT(slotBackupDone(KJob*)) );
91
 
    job->start();
92
 
}
93
 
 
94
 
void Nepomuk::BackupManager::automatedBackup()
95
 
{
96
 
    QDate today = QDate::currentDate();
97
 
    backup( m_backupLocation + today.toString(Qt::ISODate) );
98
 
 
99
 
    resetTimer();
100
 
    removeOldBackups();
101
 
}
102
 
 
103
 
void Nepomuk::BackupManager::slotConfigDirty()
104
 
{
105
 
    kDebug();
106
 
    m_config.reparseConfiguration();
107
 
 
108
 
    QString freq = m_config.group("Backup").readEntry( "backup frequency", QString("disabled") );
109
 
    kDebug() << "Frequency : " << freq;
110
 
 
111
 
    if( freq == QLatin1String("disabled") ) {
112
 
        kDebug() << "Auto Backups Disabled";
113
 
        m_timer.stop();
114
 
        return;
115
 
    }
116
 
 
117
 
    QString timeString = m_config.group("Backup").readEntry( "backup time", QTime().toString( Qt::ISODate ) );
118
 
    m_backupTime = QTime::fromString( timeString, Qt::ISODate );
119
 
 
120
 
    if( freq == QLatin1String("daily") ) {
121
 
        m_daysBetweenBackups = 0;
122
 
    }
123
 
 
124
 
    else if( freq == QLatin1String("weekly") ) {
125
 
 
126
 
        const KCalendarSystem* cal = KGlobal::locale()->calendar();
127
 
 
128
 
        int backupDay = m_config.group("Backup").readEntry( "backup day", 0 );
129
 
        int dayOfWeek = cal->dayOfWeek( QDate::currentDate() );
130
 
 
131
 
        kDebug() << "DayOfWeek: " << dayOfWeek;
132
 
        kDebug() << "BackupDay: " << backupDay;
133
 
        if( dayOfWeek < backupDay ) {
134
 
            m_daysBetweenBackups = backupDay - dayOfWeek;
135
 
        }
136
 
        else if( dayOfWeek > backupDay ) {
137
 
            m_daysBetweenBackups = cal->daysInWeek( QDate::currentDate() ) - dayOfWeek + backupDay;
138
 
        }
139
 
        else {
140
 
            if( QTime::currentTime() <= m_backupTime )
141
 
                m_daysBetweenBackups = 0;
142
 
            else
143
 
                m_daysBetweenBackups = cal->daysInWeek( QDate::currentDate() );
144
 
        }
145
 
 
146
 
        kDebug() << "Days between backups : " << m_daysBetweenBackups;
147
 
    }
148
 
 
149
 
    else if( freq == QLatin1String("monthly") ) {
150
 
        //TODO: Implement me!
151
 
    }
152
 
 
153
 
    m_maxBackups = m_config.group("Backup").readEntry<int>("max backups", 1);
154
 
 
155
 
    // Remove old timers and start new
156
 
    resetTimer();
157
 
    removeOldBackups();
158
 
}
159
 
 
160
 
void Nepomuk::BackupManager::resetTimer()
161
 
{
162
 
    if( m_backupTime.isNull() && m_daysBetweenBackups == 0 ) {
163
 
        // Never perform automated backups
164
 
        return;
165
 
    }
166
 
 
167
 
    QDateTime current = QDateTime::currentDateTime();
168
 
    QDateTime dateTime = current.addDays( m_daysBetweenBackups );
169
 
    dateTime.setTime( m_backupTime );
170
 
 
171
 
    if( dateTime < current ) {
172
 
        dateTime = dateTime.addDays( 1 );
173
 
    }
174
 
 
175
 
    int msecs = current.msecsTo( dateTime );
176
 
 
177
 
    m_timer.stop();
178
 
    m_timer.start( msecs );
179
 
    kDebug() << "Setting timer for " << msecs/1000.0/60/60 << " hours";
180
 
}
181
 
 
182
 
void Nepomuk::BackupManager::removeOldBackups()
183
 
{
184
 
    QDir dir( m_backupLocation );
185
 
    QStringList infoList = dir.entryList( QDir::Files | QDir::NoDotAndDotDot, QDir::Name );
186
 
 
187
 
    while( infoList.size() > m_maxBackups ) {
188
 
        const QString backupPath = m_backupLocation + infoList.last();
189
 
        kDebug() << "Removing : " << backupPath;
190
 
        QFile::remove( backupPath );
191
 
        infoList.pop_back();
192
 
    }
193
 
}
194
 
 
195
 
void Nepomuk::BackupManager::slotBackupDone(KJob* job)
196
 
{
197
 
    if( !job->error() ) {
198
 
        emit backupDone();
199
 
    }
200
 
}
201
 
 
202
 
 
203
 
 
204
 
#include "backupmanager.moc"