From a4a8fa10d53f35cb9061764e15658a558c500efc Mon Sep 17 00:00:00 2001 From: Juan Carlos Cornejo Date: Sun, 18 Dec 2011 15:08:31 -0500 Subject: [PATCH] Reorganized the password manager in order to implement other support for Gnome Keyring, OS X key chain and something for windows. --- owncloud_sync_qt/OwnCloudSync.cpp | 68 ++------------- owncloud_sync_qt/OwnCloudSync.h | 21 ++--- owncloud_sync_qt/OwnPasswordManager.cpp | 107 ++++++++++++++++++++++++ owncloud_sync_qt/OwnPasswordManager.h | 62 ++++++++++++++ owncloud_sync_qt/SyncGlobal.h | 1 + owncloud_sync_qt/SyncWindow.cpp | 15 +++- owncloud_sync_qt/SyncWindow.h | 4 + owncloud_sync_qt/owncloud_sync.pro | 10 ++- 8 files changed, 209 insertions(+), 79 deletions(-) create mode 100644 owncloud_sync_qt/OwnPasswordManager.cpp create mode 100644 owncloud_sync_qt/OwnPasswordManager.h diff --git a/owncloud_sync_qt/OwnCloudSync.cpp b/owncloud_sync_qt/OwnCloudSync.cpp index d8ada39f3..e26a8f14c 100644 --- a/owncloud_sync_qt/OwnCloudSync.cpp +++ b/owncloud_sync_qt/OwnCloudSync.cpp @@ -20,6 +20,7 @@ #include "OwnCloudSync.h" #include "sqlite3_util.h" #include "QWebDAV.h" +#include "OwnPasswordManager.h" #include #include @@ -35,12 +36,10 @@ #include #include -#ifdef Q_OS_LINUX - #include -#endif - -OwnCloudSync::OwnCloudSync(QString name, WId id,QSet *globalFilters) - : mAccountName(name),mWinId(id),mGlobalFilters(globalFilters) +OwnCloudSync::OwnCloudSync(QString name, OwnPasswordManager *passwordManager, + QSet *globalFilters) + : mAccountName(name),mPasswordManager(passwordManager), + mGlobalFilters(globalFilters) { mBusy = false; mIsPaused = false; @@ -48,7 +47,6 @@ OwnCloudSync::OwnCloudSync(QString name, WId id,QSet *globalFilters) // Set the pointers so we can delete them without worrying :) mSyncTimer = 0; mFileWatcher = 0; - mWallet = 0; mHardStop = false; mIsFirstRun = true; @@ -114,11 +112,8 @@ OwnCloudSync::OwnCloudSync(QString name, WId id,QSet *globalFilters) readConfigFromDB(); //ui->buttonSave->setDisabled(true); syncDebug() << "Checking configuration!"; -#ifdef Q_OS_LINUX - // Wait until the password is set -#else + mPassword = mPasswordManager->getPassword(mAccountName); initialize(); -#endif } } else { createDataBase(); // Create the database in memory @@ -128,15 +123,6 @@ OwnCloudSync::OwnCloudSync(QString name, WId id,QSet *globalFilters) connect(mSaveDBTimer, SIGNAL(timeout()), this, SLOT(saveDBToFile())); mSaveDBTimer->start(370000); updateStatus(); - - // Now the password management -#ifdef Q_OS_LINUX - mWallet= KWallet::Wallet::openWallet( - KWallet::Wallet::NetworkWallet(), - mWinId,KWallet::Wallet::Asynchronous); - connect(mWallet, SIGNAL(walletOpened(bool)), SLOT(walletOpened(bool))); -#else -#endif } void OwnCloudSync::setSaveDBTime(qint64 seconds) @@ -1260,9 +1246,7 @@ void OwnCloudSync::saveDBToFile() } else { syncDebug() << "Failed to save DB to file!"; } -#ifdef Q_OS_LINUX - saveWalletPassword(); -#endif + } void OwnCloudSync::loadDBFromFile() @@ -1619,44 +1603,6 @@ bool OwnCloudSync::needsSync() return mNeedsSync; } -#ifdef Q_OS_LINUX - -void OwnCloudSync::requestPassword() -{ - QMap map; - mWallet->readMap(mAccountName,map); - if(map.size()) { - mPassword = map[mAccountName]; - initialize(); - } -} - -void OwnCloudSync::walletOpened(bool ok) -{ - if( (ok && (mWallet->hasFolder("owncloud_sync")) - || mWallet->createFolder("owncloud_sync")) - && mWallet->setFolder("owncloud_sync")) { - //emit toLog("Wallet opened!"); - //syncDebug() << "Wallet opened!" << - // KWallet::Wallet::FormDataFolder() ; - if (mReadPassword ) { - requestPassword(); - } - } else { - syncDebug() << "Error opening wallet"; - } -} - -void OwnCloudSync::saveWalletPassword() -{ - QMap map; - map[mAccountName] = mPassword; - if( mWallet ) - mWallet->writeMap(mAccountName,map); -} - -#endif - QString OwnCloudSync::stringRemoveBasePath(QString path, QString base) { if( base != "/" ) { diff --git a/owncloud_sync_qt/OwnCloudSync.h b/owncloud_sync_qt/OwnCloudSync.h index ed5f292e3..a56f27e36 100644 --- a/owncloud_sync_qt/OwnCloudSync.h +++ b/owncloud_sync_qt/OwnCloudSync.h @@ -19,6 +19,8 @@ #ifndef OWNCLOUDSYNC_H #define OWNCLOUDSYNC_H +#include "SyncGlobal.h" + #include #include "QWebDAV.h" #include @@ -31,17 +33,15 @@ class QTimer; class QFileSystemWatcher; class QNetworkReply; - -#ifdef Q_OS_LINUX - #include -#endif +class OwnPasswordManager; class OwnCloudSync : public QObject { Q_OBJECT public: - explicit OwnCloudSync(QString name, WId id, QSet *globalFilters); + explicit OwnCloudSync(QString name, OwnPasswordManager *passwordManager, + QSet *globalFilters); ~OwnCloudSync(); void initialize(QString host, QString user, QString pass, QString remote, QString local, qint64 time); @@ -100,7 +100,7 @@ public: } private: - WId mWinId; + OwnPasswordManager *mPasswordManager; QWebDAV *mWebdav; bool mIsEnabled; bool mAllowedToSync; @@ -152,11 +152,6 @@ private: QString mAccountName; SyncPosition mLastSyncAborted; SyncPosition mSyncPosition; -#ifdef Q_OS_LINUX - KWallet::Wallet *mWallet; - void saveWalletPassword(); - void requestPassword(); -#endif bool mReadPassword; bool mIsPaused; @@ -221,10 +216,6 @@ public slots: void loadDBFromFile(); void requestTimedout(); void serverDirectoryCreated(QString name); - -//#ifdef Q_OS_LINUX - void walletOpened(bool); -//#endif }; #endif // OWNCLOUDSYNC_H diff --git a/owncloud_sync_qt/OwnPasswordManager.cpp b/owncloud_sync_qt/OwnPasswordManager.cpp new file mode 100644 index 000000000..b36a12112 --- /dev/null +++ b/owncloud_sync_qt/OwnPasswordManager.cpp @@ -0,0 +1,107 @@ +#include "OwnPasswordManager.h" + +OwnPasswordManager::OwnPasswordManager(QObject *parent, WId winID ) + : QObject(parent) +{ +#ifdef Q_OS_LINUX +#if defined(OCS_USE_KWALLET) + mKWallet= KWallet::Wallet::openWallet( + KWallet::Wallet::NetworkWallet(), + winID,KWallet::Wallet::Asynchronous); + connect(mKWallet, SIGNAL(walletOpened(bool)), + this,SLOT(slotKWalletOpened(bool))); +#endif // OCS_USE_KWALLET + +#endif // Q_OS_LINUX +} + +void OwnPasswordManager::savePassword(QString name, QString pass) +{ +#ifdef Q_OS_LINUX + +#if defined(OCS_USE_KWALLET) + QMap map; + map[name] = pass; + if( mKWallet ) + mKWallet->writeMap(name,map); +#endif + +#if defined(OCS_USE_GNOME_KEYRING) +#endif + +#endif // Q_OS_LINUX + +#ifdef Q_OS_MAC_OS_X +#endif // Q_OS_MAC_OS_X + +#ifdef Q_OS_WIN +#endif // Q_OS_WIN +} + +QString OwnPasswordManager::getPassword(QString name) +{ +#ifdef Q_OS_LINUX + +#if defined(OCS_USE_KWALLET) + QMap map; + mKWallet->readMap(name,map); + if(map.size()) { + return map[name]; + } +#endif + +#if defined(OCS_USE_GNOME_KEYRING) +#endif + +#endif // Q_OS_LINUX + +#ifdef Q_OS_MAC_OS_X +#endif // Q_OS_MAC_OS_X + +#ifdef Q_OS_WIN +#endif // Q_OS_WIN + return QString(""); +} + +void OwnPasswordManager::slotKWalletOpened(bool ok) +{ +#ifdef Q_OS_LINUX +#if defined(OCS_USE_KWALLET) + kwalletOpened(ok); +#endif +#endif +} + + +#ifdef Q_OS_LINUX + +#if defined(OCS_USE_KWALLET) +void OwnPasswordManager::kwalletOpened(bool ok) +{ + if( ok && (mKWallet->hasFolder("owncloud_sync") + || mKWallet->createFolder("owncloud_sync")) + && mKWallet->setFolder("owncloud_sync") ){ + emit managerReady(); + //emit toLog("Wallet opened!"); + //syncDebug() << "Wallet opened!" << + // KWallet::Wallet::FormDataFolder() ; + //if (mReadPassword ) { + // requestPassword(); + //} + } else { + syncDebug() << "Error opening wallet"; + } +} + +#endif // OCS_USE_KWALLET + +#if defined(OCS_USE_GNOME_KEYRING) +#endif // OCS_USE_GNOME_KEYRING + +#endif // Q_OS_LINUX + +#ifdef Q_OS_MAC_OS_X +#endif // Q_OS_MAC_OS_X + +#ifdef Q_OS_WIN +#endif // Q_OS_WIN diff --git a/owncloud_sync_qt/OwnPasswordManager.h b/owncloud_sync_qt/OwnPasswordManager.h new file mode 100644 index 000000000..a71b5efbf --- /dev/null +++ b/owncloud_sync_qt/OwnPasswordManager.h @@ -0,0 +1,62 @@ +#ifndef OWNPASSWORDMANAGER_H +#define OWNPASSWORDMANAGER_H + +#include "SyncGlobal.h" + +#include +#include + +#ifdef Q_OS_LINUX +// We have two choices for linux (kwallet,gnome keyring or I suppose, none) + +#if defined(OCS_USE_KWALLET) +#include +#endif + +#if defined(OCS_USE_GNOME_KEYRING) +#endif + +#endif // Q_OS_LINUX + +#ifdef Q_OS_MAC_OS_X +#endif // Q_OS_MAC_OS_X + +#ifdef Q_OS_WIN +#endif // Q_OS_WIN + +class OwnPasswordManager : public QObject +{ + Q_OBJECT +public: + OwnPasswordManager(QObject *parent = 0, WId winId = 0); + void savePassword(QString name, QString pass); + QString getPassword(QString name); + +private: +#ifdef Q_OS_LINUX + +#if defined(OCS_USE_KWALLET) + KWallet::Wallet *mKWallet; + void kwalletOpened(bool); +#endif + +#if defined(OCS_USE_GNOME_KEYRING) +#endif + +#endif // Q_OS_LINUX + +#ifdef Q_OS_MAC_OS_X +#endif // Q_OS_MAC_OS_X + +#ifdef Q_OS_WIN +#endif // Q_OS_WIN + +public slots: + void slotKWalletOpened(bool); + +signals: + void managerReady(); + +}; + +#endif // OWNPASSWORDMANAGER_H diff --git a/owncloud_sync_qt/SyncGlobal.h b/owncloud_sync_qt/SyncGlobal.h index 40c20920f..6e3e7bf70 100644 --- a/owncloud_sync_qt/SyncGlobal.h +++ b/owncloud_sync_qt/SyncGlobal.h @@ -44,6 +44,7 @@ public: qint64 writeData(const char* data, qint64 length){ qDebug() << data; emit debugMessage(QString(data)); + return 0; } signals: diff --git a/owncloud_sync_qt/SyncWindow.cpp b/owncloud_sync_qt/SyncWindow.cpp index 785e0968d..9a0562ae9 100644 --- a/owncloud_sync_qt/SyncWindow.cpp +++ b/owncloud_sync_qt/SyncWindow.cpp @@ -23,6 +23,7 @@ #include "sqlite3_util.h" #include "QWebDAV.h" #include "OwnCloudSync.h" +#include "OwnPasswordManager.h" #include #include @@ -51,6 +52,7 @@ SyncWindow::SyncWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::SyncWindow) { + hide(); mSharedFilters = new QSet(); mIncludedFilters = g_GetIncludedFilterList(); mQuitAction = false; @@ -110,7 +112,17 @@ SyncWindow::SyncWindow(QWidget *parent) : importGlobalFilters(true); updateSharedFilterList(); + // Get the password manager + mPasswordManager = new OwnPasswordManager(this,winId()); + connect(mPasswordManager,SIGNAL(managerReady()), + this,SLOT(passwordManagerReady())); +} + +void SyncWindow::passwordManagerReady() +{ // Look for accounts that already exist + QDir configDir(mConfigDirectory); + configDir.mkpath(mConfigDirectory); QStringList filters; filters << "*.db"; QStringList files = configDir.entryList(filters); @@ -223,7 +235,8 @@ void SyncWindow::systemTrayActivated(QSystemTrayIcon::ActivationReason reason) OwnCloudSync* SyncWindow::addAccount(QString name) { - OwnCloudSync *account = new OwnCloudSync(name,winId(),mSharedFilters); + OwnCloudSync *account = new OwnCloudSync(name,mPasswordManager, + mSharedFilters); mAccounts.append(account); mAccountNames.append(name); diff --git a/owncloud_sync_qt/SyncWindow.h b/owncloud_sync_qt/SyncWindow.h index 96d5038ca..ff767235c 100644 --- a/owncloud_sync_qt/SyncWindow.h +++ b/owncloud_sync_qt/SyncWindow.h @@ -29,6 +29,7 @@ #include #include +class OwnPasswordManager; class QTimer; class OwnCloudSync; class QSignalMapper; @@ -103,6 +104,8 @@ private: QIcon mDefaultConflictIcon; QIcon mSyncConflictIcon; + OwnPasswordManager *mPasswordManager; + void processNextStep(); void saveLogs(); void rebuildAccountsTable(); @@ -134,6 +137,7 @@ public slots: void slotAccountsSignalMapper(int row); void processDebugMessage(const QString msg); void includedFilterListItemChanged(QListWidgetItem* item); + void passwordManagerReady(); // GUI related slots void on_buttonSave_clicked(); diff --git a/owncloud_sync_qt/owncloud_sync.pro b/owncloud_sync_qt/owncloud_sync.pro index 9c3daa1a1..c07a157f6 100644 --- a/owncloud_sync_qt/owncloud_sync.pro +++ b/owncloud_sync_qt/owncloud_sync.pro @@ -21,13 +21,15 @@ SOURCES += main.cpp\ sqlite3_util.cpp \ SyncWindow.cpp \ qwebdav/QWebDAV.cpp \ - OwnCloudSync.cpp + OwnCloudSync.cpp \ + OwnPasswordManager.cpp HEADERS += sqlite3_util.h \ SyncWindow.h \ qwebdav/QWebDAV.h \ OwnCloudSync.h \ - SyncGlobal.h + SyncGlobal.h \ + OwnPasswordManager.h FORMS += SyncWindow.ui INCLUDEPATH += qwebdav/ @@ -88,3 +90,7 @@ OTHER_FILES += \ + + + +