From 9ba3dec3e263b6d8a039092a976ae0cc4f625c87 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Fri, 11 Nov 2011 01:17:14 +0100 Subject: Sync feature, first bits --- src/CMakeLists.txt | 5 + src/application.cpp | 18 +++ src/application.h | 3 + src/rekonq.kcfg | 28 +++++ src/sync/settings_sync.ui | 132 ++++++++++++++++++++++ src/sync/syncmanager.cpp | 278 ++++++++++++++++++++++++++++++++++++++++++++++ src/sync/syncmanager.h | 85 ++++++++++++++ src/sync/syncwidget.cpp | 101 +++++++++++++++++ src/sync/syncwidget.h | 65 +++++++++++ 9 files changed, 715 insertions(+) create mode 100644 src/sync/settings_sync.ui create mode 100644 src/sync/syncmanager.cpp create mode 100644 src/sync/syncmanager.h create mode 100644 src/sync/syncwidget.cpp create mode 100644 src/sync/syncwidget.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ea33df82..f8a6124a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -92,6 +92,9 @@ SET( rekonq_KDEINIT_SRCS useragent/useragentinfo.cpp useragent/useragentmanager.cpp useragent/useragentwidget.cpp + #---------------------------------------- + sync/syncmanager.cpp + sync/syncwidget.cpp ) @@ -105,6 +108,7 @@ KDE4_ADD_UI_FILES( rekonq_KDEINIT_SRCS sslinfo.ui webappcreation.ui useragent/useragentsettings.ui + sync/settings_sync.ui ) KDE4_ADD_KCFG_FILES( rekonq_KDEINIT_SRCS rekonq.kcfgc ) @@ -119,6 +123,7 @@ INCLUDE_DIRECTORIES ( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/history ${CMAKE_CURRENT_SOURCE_DIR}/opensearch ${CMAKE_CURRENT_SOURCE_DIR}/settings + ${CMAKE_CURRENT_SOURCE_DIR}/sync ${CMAKE_CURRENT_SOURCE_DIR}/urlbar ${CMAKE_CURRENT_SOURCE_DIR}/useragent ${CMAKE_CURRENT_BINARY_DIR} diff --git a/src/application.cpp b/src/application.cpp index 9dd98a49..768f25a2 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -47,6 +47,7 @@ #include "mainwindow.h" #include "opensearchmanager.h" #include "sessionmanager.h" +#include "syncmanager.h" #include "stackedurlbar.h" #include "tabbar.h" #include "urlbar.h" @@ -150,6 +151,13 @@ Application::~Application() m_userAgentManager.clear(); } + if (!m_syncManager.isNull()) + { + kDebug() << "deleting sync manager"; + delete m_syncManager.data(); + m_syncManager.clear(); + } + // TODO: // add a check to NOT close rekonq // until last download is finished @@ -403,6 +411,16 @@ UserAgentManager *Application::userAgentManager() } +SyncManager *Application::syncManager() +{ + if (m_syncManager.isNull()) + { + m_syncManager = new SyncManager(instance()); + } + return m_syncManager.data(); +} + + void Application::loadUrl(const KUrl& url, const Rekonq::OpenType& type) { if (url.isEmpty()) diff --git a/src/application.h b/src/application.h index 27b46e92..6a67ca61 100644 --- a/src/application.h +++ b/src/application.h @@ -53,6 +53,7 @@ class MainWindow; class OpenSearchManager; class SessionManager; class UserAgentManager; +class SyncManager; class KAction; @@ -96,6 +97,7 @@ public: IconManager *iconManager(); DownloadManager *downloadManager(); UserAgentManager *userAgentManager(); + SyncManager *syncManager(); KAction *privateBrowsingAction() { @@ -142,6 +144,7 @@ private: QWeakPointer m_iconManager; QWeakPointer m_downloadManager; QWeakPointer m_userAgentManager; + QWeakPointer m_syncManager; MainWindowList m_mainWindows; diff --git a/src/rekonq.kcfg b/src/rekonq.kcfg index 73a82efc..c84265e0 100644 --- a/src/rekonq.kcfg +++ b/src/rekonq.kcfg @@ -256,4 +256,32 @@ + + + + false + + + false + + + false + + + false + + + + + + + + + + + + + + + diff --git a/src/sync/settings_sync.ui b/src/sync/settings_sync.ui new file mode 100644 index 00000000..35415f53 --- /dev/null +++ b/src/sync/settings_sync.ui @@ -0,0 +1,132 @@ + + + Sync + + + + 0 + 0 + 633 + 551 + + + + + + + Activate sync + + + + + + + sync + + + + + + bookmarks + + + + + + + history + + + + + + + passwords + + + + + + + + + + ownCloud settings + + + + + + host: + + + + + + + + + + username: + + + + + + + + + + password: + + + + + + + + + + + + + + + Last sync: NEVER + + + + + + + Sync now! + + + + + + + + + Qt::Vertical + + + + 20 + 289 + + + + + + + + + KLineEdit + QLineEdit +
klineedit.h
+
+
+ + +
diff --git a/src/sync/syncmanager.cpp b/src/sync/syncmanager.cpp new file mode 100644 index 00000000..cf3f2df7 --- /dev/null +++ b/src/sync/syncmanager.cpp @@ -0,0 +1,278 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2011 by Andrea Diamantini +* +* +* 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 . +* +* ============================================================ */ + + +// Self Includes +#include "syncmanager.h" +#include "syncmanager.moc" + +// Auto Includes +#include "rekonq.h" + +// KDE Includes +#include +#include +#include + +#include +#include + + +SyncManager::SyncManager(QObject *parent) + : QObject(parent) + , _firstTimeSynced(false) +{ + if (ReKonfig::syncEnabled()) + { + // sync every minute + QTimer *timer = new QTimer(this); + connect(timer, SIGNAL(timeout()), this, SLOT(sync())); + timer->start(60 * 1000); + } +} + + +SyncManager::~SyncManager() +{ + sync(); +} + + +void SyncManager::firstTimeSync() +{ + // Bookmarks + if (ReKonfig::syncBookmarks()) + { + _remoteBookmarksUrl = QUrl(ReKonfig::syncHost()); + _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(ReKonfig::syncHost()); + _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(ReKonfig::syncHost()); + _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; + } +} + + +void SyncManager::sync() +{ + kDebug() << "syncing now..."; + + if (!ReKonfig::syncEnabled()) + return; + + if (!_firstTimeSynced) + { + kDebug() << "need to sync for the first time..."; + firstTimeSync(); + return; + } + + // Bookmarks + if (ReKonfig::syncBookmarks()) + { + KIO::Job *job = KIO::file_copy(_localBookmarksUrl, _remoteBookmarksUrl, -1, KIO::HideProgressInfo | KIO::Overwrite); + connect(job, SIGNAL(finished(KJob *)), this, SLOT(onBookmarksSyncFinished(KJob *))); + } + + // History + if (ReKonfig::syncHistory()) + { + KIO::Job *job = KIO::file_copy(_localHistoryUrl, _remoteHistoryUrl, -1, KIO::HideProgressInfo | KIO::Overwrite); + connect(job, SIGNAL(finished(KJob *)), this, SLOT(onHistorySyncFinished(KJob *))); + } + + // Passwords + if (ReKonfig::syncPasswords()) + { + KIO::Job *job = KIO::file_copy(_localPasswordsUrl, _remotePasswordsUrl, -1, KIO::HideProgressInfo | KIO::Overwrite); + connect(job, SIGNAL(finished(KJob *)), this, SLOT(onPasswordsSyncFinished(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::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::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); +} diff --git a/src/sync/syncmanager.h b/src/sync/syncmanager.h new file mode 100644 index 00000000..3a78191f --- /dev/null +++ b/src/sync/syncmanager.h @@ -0,0 +1,85 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2011 by Andrea Diamantini +* +* +* 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 . +* +* ============================================================ */ + + +#ifndef SYNC_MANAGER_H +#define SYNC_MANAGER_H + + +// Rekonq Includes +#include "rekonq_defines.h" + +// Qt Includes +#include + +// KDE Includes +#include + +// Forward Declarations +class KJob; + + +class REKONQ_TESTS_EXPORT SyncManager : public QObject +{ + Q_OBJECT + +public: + SyncManager(QObject *parent = 0); + ~SyncManager(); + + void firstTimeSync(); + +public Q_SLOTS: + void sync(); + +private Q_SLOTS: + void onBookmarksSyncFinished(KJob *); + void onBookmarksStatFinished(KJob *); + + void onHistorySyncFinished(KJob *); + void onHistoryStatFinished(KJob *); + + void onPasswordsSyncFinished(KJob *); + void onPasswordsStatFinished(KJob *); + +Q_SIGNALS: + void syncBookmarksFinished(bool); + void syncHistoryFinished(bool); + void syncPasswordsFinished(bool); + +private: + bool _firstTimeSynced; + + QUrl _remoteBookmarksUrl; + KUrl _localBookmarksUrl; + + QUrl _remoteHistoryUrl; + KUrl _localHistoryUrl; + + QUrl _remotePasswordsUrl; + KUrl _localPasswordsUrl; +}; + +#endif // SYNC_MANAGER_H diff --git a/src/sync/syncwidget.cpp b/src/sync/syncwidget.cpp new file mode 100644 index 00000000..c5295c93 --- /dev/null +++ b/src/sync/syncwidget.cpp @@ -0,0 +1,101 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2011 by Andrea Diamantini +* +* +* 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 . +* +* ============================================================ */ + + +// Self Includes +#include "syncwidget.h" +#include "syncwidget.moc" + +// Auto Includes +#include "rekonq.h" + +// Local Includes +#include "syncmanager.h" +#include "application.h" + + +SyncWidget::SyncWidget(QWidget *parent) + : QWidget(parent) + , _changed(false) +{ + setupUi(this); + + bool isSyncEnabled = ReKonfig::syncEnabled(); + enablewidgets(isSyncEnabled); + + kcfg_syncPass->setPasswordMode(true); + + connect(kcfg_syncEnabled, SIGNAL(clicked()), this, SLOT(hasChanged())); + connect(syncNowButton, SIGNAL(clicked()), this, SLOT(syncNow())); + + setSyncLabel(ReKonfig::lastSyncDateTime()); +} + + +void SyncWidget::save() +{ + rApp->syncManager()->firstTimeSync(); +} + + +bool SyncWidget::changed() +{ + return _changed; +} + + +void SyncWidget::hasChanged() +{ + enablewidgets(kcfg_syncEnabled->isChecked()); + + _changed = true; + emit changed(true); +} + + +void SyncWidget::enablewidgets(bool b) +{ + syncGroupBox->setEnabled(b); + ownCloudGroupBox->setEnabled(b); + syncNowButton->setEnabled(b); +} + + +void SyncWidget::setSyncLabel(const QDateTime &dt) +{ + if (dt.isNull()) + lastSyncTimeLabel->setText(i18n("Last Sync: NEVER!")); + else + lastSyncTimeLabel->setText(i18n("Last Sync: %1", dt.toString(Qt::DefaultLocaleShortDate))); +} + + +void SyncWidget::syncNow() +{ + rApp->syncManager()->firstTimeSync(); + + // TODO do something in the sync UI... +} + diff --git a/src/sync/syncwidget.h b/src/sync/syncwidget.h new file mode 100644 index 00000000..0e1d57ff --- /dev/null +++ b/src/sync/syncwidget.h @@ -0,0 +1,65 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2011 by Andrea Diamantini +* +* +* 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 . +* +* ============================================================ */ + + +#ifndef SYNC_WIDGET_H +#define SYNC_WIDGET_H + + +// Rekonq Includes +#include "rekonq_defines.h" + +// Ui Includes +#include "ui_settings_sync.h" + +// Qt Includes +#include + + +class SyncWidget : public QWidget, private Ui::Sync +{ + Q_OBJECT + +public: + SyncWidget(QWidget *parent = 0); + + void save(); + bool changed(); + +Q_SIGNALS: + void changed(bool); + +private Q_SLOTS: + void hasChanged(); + void syncNow(); + +private: + void enablewidgets(bool); + void setSyncLabel(const QDateTime &); + + bool _changed; +}; + +#endif // SYNC_WIDGET_H -- cgit v1.2.1