summaryrefslogtreecommitdiff
path: root/src/sync/syncmanager.cpp
blob: e926e18179516a1ce70ae65133ce6bc898f47118 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* ============================================================
*
* This file is a part of the rekonq project
*
* Copyright (C) 2011 by Andrea Diamantini <adjam7 at gmail dot com>
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
* ============================================================ */


// Self Includes
#include "syncmanager.h"
#include "syncmanager.moc"

// Auto Includes
#include "rekonq.h"

// Local Includes
#include "application.h"
#include "bookmarkmanager.h"
#include "historymanager.h"
#include "syncwidget.h"

// KDE Includes
#include <KStandardDirs>
#include <KMessageBox>
#include <klocalizedstring.h>

#include <KIO/Job>
#include <KIO/JobUiDelegate>


SyncManager::SyncManager(QObject *parent)
    : QObject(parent)
    , _firstTimeSynced(false)
{
    loadSettings();
}


void SyncManager::loadSettings()
{
    if (ReKonfig::syncEnabled())
    {
        firstTimeSync();

        // bookmarks
        ReKonfig::syncBookmarks()
        ? connect(rApp->bookmarkManager(), SIGNAL(bookmarksUpdated()), this, SLOT(syncBookmarks()))
        : disconnect(rApp->bookmarkManager(), SIGNAL(bookmarksUpdated()), this, SLOT(syncBookmarks()))
        ;

        // history
        ReKonfig::syncHistory()
        ? connect(rApp->historyManager(), SIGNAL(historySaved()), this, SLOT(syncHistory()))
        : disconnect(rApp->historyManager(), SIGNAL(historySaved()), this, SLOT(syncHistory()))
        ;
    }
    else
    {
        // bookmarks
        disconnect(rApp->bookmarkManager(), SIGNAL(bookmarksUpdated()), this, SLOT(syncBookmarks()));

        // history
        disconnect(rApp->historyManager(), SIGNAL(historySaved()), this, SLOT(syncHistory()));
    }
}


void SyncManager::showSettings()
{
    QPointer<KDialog> dialog = new KDialog();
    dialog->setCaption(i18nc("@title:window", "Sync Settings"));
    dialog->setButtons(KDialog::Ok | KDialog::Cancel);

    SyncWidget widget;
    dialog->setMainWidget(&widget);
    connect(dialog, SIGNAL(okClicked()), &widget, SLOT(save()));
    connect(dialog, SIGNAL(okClicked()), this, SLOT(loadSettings()));
    dialog->exec();

    dialog->deleteLater();
}


void SyncManager::firstTimeSync()
{
    // Bookmarks
    if (ReKonfig::syncBookmarks())
    {
        _remoteBookmarksUrl = QUrl::fromUserInput(ReKonfig::syncUrl());
        _remoteBookmarksUrl.setUserName(ReKonfig::syncUser());
        _remoteBookmarksUrl.setPassword(ReKonfig::syncPass());
        _remoteBookmarksUrl.setPath(QL1S("/data/") + ReKonfig::syncUser() + QL1S("/files/bookmarks.xml"));
        kDebug() << "REMOTE BK URL: " << _remoteBookmarksUrl;

        const QString bookmarksFilePath = KStandardDirs::locateLocal("data", QL1S("konqueror/bookmarks.xml"));
        _localBookmarksUrl = KUrl(bookmarksFilePath);
        kDebug() << "LOCAL BK URL: " << _localBookmarksUrl;

        KIO::StatJob *job = KIO::stat(_remoteBookmarksUrl, KIO::StatJob::DestinationSide, 0, KIO::HideProgressInfo);
        connect(job, SIGNAL(finished(KJob *)), this, SLOT(onBookmarksStatFinished(KJob *)));

        _firstTimeSynced = true;
    }

    // History
    if (ReKonfig::syncHistory())
    {
        _remoteHistoryUrl = QUrl::fromUserInput(ReKonfig::syncUrl());
        _remoteHistoryUrl.setUserName(ReKonfig::syncUser());
        _remoteHistoryUrl.setPassword(ReKonfig::syncPass());
        _remoteHistoryUrl.setPath(QL1S("/data/") + ReKonfig::syncUser() + QL1S("/files/history"));
        kDebug() << "REMOTE HISTORY URL: " << _remoteHistoryUrl;

        const QString historyFilePath = KStandardDirs::locateLocal("appdata", "history");
        _localHistoryUrl = KUrl(historyFilePath);
        kDebug() << "LOCAL HISTORY URL: " << _localHistoryUrl;

        KIO::StatJob *job = KIO::stat(_remoteHistoryUrl, KIO::StatJob::DestinationSide, 0, KIO::HideProgressInfo);
        connect(job, SIGNAL(finished(KJob *)), this, SLOT(onHistoryStatFinished(KJob *)));

        _firstTimeSynced = true;
    }

    // Passwords
    if (ReKonfig::syncPasswords())
    {
        _remotePasswordsUrl = QUrl::fromUserInput(ReKonfig::syncUrl());
        _remotePasswordsUrl.setUserName(ReKonfig::syncUser());
        _remotePasswordsUrl.setPassword(ReKonfig::syncPass());
        _remotePasswordsUrl.setPath(QL1S("/data/") + ReKonfig::syncUser() + QL1S("/files/kdewallet.kwl"));
        kDebug() << "REMOTE PSWD URL: " << _remotePasswordsUrl;

        const QString passwordsFilePath = KStandardDirs::locateLocal("data", QL1S("kwallet/kdewallet.kwl"));
        _localPasswordsUrl = KUrl(passwordsFilePath);
        kDebug() << "LOCAL PSWD URL: " << _localPasswordsUrl;

        KIO::StatJob *job = KIO::stat(_remotePasswordsUrl, KIO::StatJob::DestinationSide, 0, KIO::HideProgressInfo);
        connect(job, SIGNAL(finished(KJob *)), this, SLOT(onPasswordsStatFinished(KJob *)));

        _firstTimeSynced = true;
    }
}


bool SyncManager::syncRelativeEnabled(bool check)
{
    if (!ReKonfig::syncEnabled())
        return false;

    if (!_firstTimeSynced)
    {
        kDebug() << "need to sync for the first time...";
        firstTimeSync();
        return false;
    }

    return check;
}


// ---------------------------------------------------------------------------------------


void SyncManager::syncBookmarks()
{
    kDebug() << "syncing now...";

    if (!syncRelativeEnabled(ReKonfig::syncBookmarks()))
        return;

    KIO::Job *job = KIO::file_copy(_localBookmarksUrl, _remoteBookmarksUrl, -1, KIO::HideProgressInfo | KIO::Overwrite);
    connect(job, SIGNAL(finished(KJob *)), this, SLOT(onBookmarksSyncFinished(KJob *)));
}


void SyncManager::onBookmarksStatFinished(KJob *job)
{
    if (job->error() ||
            KMessageBox::questionYesNo(0,
                                       i18n("A remote bookmarks file has just present in your remote server."),
                                       i18n("Server notification"),
                                       KGuiItem(i18n("Overwrite it")),
                                       KGuiItem(i18n("Copy it locally"))
                                      )
       )
    {
        KIO::Job *job = KIO::file_copy(_localBookmarksUrl, _remoteBookmarksUrl, -1, KIO::HideProgressInfo | KIO::Overwrite);
        connect(job, SIGNAL(finished(KJob *)), this, SLOT(onBookmarksSyncFinished(KJob *)));
    }
    else
    {
        KIO::Job *job = KIO::file_copy(_remoteBookmarksUrl, _localBookmarksUrl, -1, KIO::HideProgressInfo | KIO::Overwrite);
        connect(job, SIGNAL(finished(KJob *)), this, SLOT(onBookmarksSyncFinished(KJob *)));
    }
}


void SyncManager::onBookmarksSyncFinished(KJob *job)
{
    if (job->error())
    {
        job->uiDelegate()->showErrorMessage();
        emit syncBookmarksFinished(false);
        return;
    }

    QDateTime now = QDateTime::currentDateTime();
    ReKonfig::setLastSyncDateTime(now);
    emit syncBookmarksFinished(true);
}


// ---------------------------------------------------------------------------------------


void SyncManager::syncHistory()
{
    kDebug() << "syncing now...";

    if (!syncRelativeEnabled(ReKonfig::syncHistory()))
        return;

    KIO::Job *job = KIO::file_copy(_localHistoryUrl, _remoteHistoryUrl, -1, KIO::HideProgressInfo | KIO::Overwrite);
    connect(job, SIGNAL(finished(KJob *)), this, SLOT(onHistorySyncFinished(KJob *)));
}


void SyncManager::onHistoryStatFinished(KJob *job)
{
    if (job->error() ||
            KMessageBox::questionYesNo(0,
                                       i18n("A remote history file has just present in your remote server."),
                                       i18n("Server notification"),
                                       KGuiItem(i18n("Overwrite it")),
                                       KGuiItem(i18n("Copy it locally"))
                                      )
       )
    {
        KIO::Job *job = KIO::file_copy(_localHistoryUrl, _remoteHistoryUrl, -1, KIO::HideProgressInfo | KIO::Overwrite);
        connect(job, SIGNAL(finished(KJob *)), this, SLOT(onHistorySyncFinished(KJob *)));
    }
    else
    {
        KIO::Job *job = KIO::file_copy(_remoteHistoryUrl, _localHistoryUrl, -1, KIO::HideProgressInfo | KIO::Overwrite);
        connect(job, SIGNAL(finished(KJob *)), this, SLOT(onHistorySyncFinished(KJob *)));
    }
}


void SyncManager::onHistorySyncFinished(KJob *job)
{
    if (job->error())
    {
        job->uiDelegate()->showErrorMessage();
        emit syncHistoryFinished(false);
        return;
    }

    QDateTime now = QDateTime::currentDateTime();
    ReKonfig::setLastSyncDateTime(now);
    emit syncHistoryFinished(true);
}


// ---------------------------------------------------------------------------------------


void SyncManager::syncPasswords()
{
    kDebug() << "syncing now...";

    if (!syncRelativeEnabled(ReKonfig::syncPasswords()))
        return;

    KIO::Job *job = KIO::file_copy(_localPasswordsUrl, _remotePasswordsUrl, -1, KIO::HideProgressInfo | KIO::Overwrite);
    connect(job, SIGNAL(finished(KJob *)), this, SLOT(onPasswordsSyncFinished(KJob *)));
}


void SyncManager::onPasswordsStatFinished(KJob *job)
{
    if (job->error() ||
            KMessageBox::questionYesNo(0,
                                       i18n("A remote passwords file has just present in your remote server."),
                                       i18n("Server notification"),
                                       KGuiItem(i18n("Overwrite it")),
                                       KGuiItem(i18n("Copy it locally"))
                                      )
       )
    {
        KIO::Job *job = KIO::file_copy(_localPasswordsUrl, _remotePasswordsUrl, -1, KIO::HideProgressInfo | KIO::Overwrite);
        connect(job, SIGNAL(finished(KJob *)), this, SLOT(onPasswordsSyncFinished(KJob *)));
    }
    else
    {
        KIO::Job *job = KIO::file_copy(_remotePasswordsUrl, _localPasswordsUrl, -1, KIO::HideProgressInfo | KIO::Overwrite);
        connect(job, SIGNAL(finished(KJob *)), this, SLOT(onPasswordsSyncFinished(KJob *)));
    }
}


void SyncManager::onPasswordsSyncFinished(KJob *job)
{
    if (job->error())
    {
        job->uiDelegate()->showErrorMessage();
        emit syncPasswordsFinished(false);
        return;
    }

    QDateTime now = QDateTime::currentDateTime();
    ReKonfig::setLastSyncDateTime(now);
    emit syncPasswordsFinished(true);
}