[Pkg-owncloud-commits] [owncloud-client] 48/498: Save and restore multiple accounts.

Sandro Knauß hefee-guest at moszumanska.debian.org
Tue Aug 11 14:48:33 UTC 2015


This is an automated email from the git hooks/post-receive script.

hefee-guest pushed a commit to branch master
in repository owncloud-client.

commit f184d66ea23e5177c75c58d199893dce32c1c6a9
Author: Christian Kamm <kamm at incasoftware.de>
Date:   Thu Apr 23 15:42:18 2015 +0200

    Save and restore multiple accounts.
---
 src/gui/accountmanager.cpp | 90 +++++++++++++++++++++++++++++++---------------
 src/gui/accountmanager.h   | 10 +++---
 2 files changed, 68 insertions(+), 32 deletions(-)

diff --git a/src/gui/accountmanager.cpp b/src/gui/accountmanager.cpp
index 518154d..b72af54 100644
--- a/src/gui/accountmanager.cpp
+++ b/src/gui/accountmanager.cpp
@@ -40,13 +40,34 @@ AccountManager *AccountManager::instance()
 
 bool AccountManager::restore()
 {
+    QScopedPointer<QSettings> settings(Account::settingsWithGroup("Accounts"));
+
+    // If there are no accounts, check the old format.
+    if (settings->childGroups().isEmpty()) {
+        return restoreFromLegacySettings();
+    }
+
+    foreach (const auto& accountId, settings->childGroups()) {
+        settings->beginGroup(accountId);
+        if (auto acc = load(settings)) {
+            acc->_id = accountId;
+            addAccount(acc);
+        }
+        settings->endGroup();
+    }
+
+    return true;
+}
+
+bool AccountManager::restoreFromLegacySettings()
+{
     // try to open the correctly themed settings
     QScopedPointer<QSettings> settings(Account::settingsWithGroup(Theme::instance()->appName()));
 
-    AccountPtr acc;
     bool migratedCreds = false;
 
     // if the settings file could not be opened, the childKeys list is empty
+    // then try to load settings from a very old place
     if( settings->childKeys().isEmpty() ) {
         // Now try to open the original ownCloud settings to see if they exist.
         QString oCCfgFile = QDir::fromNativeSeparators( settings->fileName() );
@@ -72,7 +93,7 @@ bool AccountManager::restore()
                 // in case the urls are equal reset the settings object to read from
                 // the ownCloud settings object
                 qDebug() << "Migrate oC config if " << oCUrl << " == " << overrideUrl << ":"
-                << (oCUrl == overrideUrl ? "Yes" : "No");
+                         << (oCUrl == overrideUrl ? "Yes" : "No");
                 if( oCUrl == overrideUrl ) {
                     migratedCreds = true;
                     settings.reset( oCSettings );
@@ -83,44 +104,31 @@ bool AccountManager::restore()
         }
     }
 
+    // Try to load the single account.
     if (!settings->childKeys().isEmpty()) {
-        acc = Account::create();
-
-        acc->setUrl(settings->value(QLatin1String(urlC)).toUrl());
-
-        // We want to only restore settings for that auth type and the user value
-        acc->_settingsMap.insert(QLatin1String(userC), settings->value(userC));
-        QString authTypePrefix = settings->value(authTypeC).toString() + "_";
-        Q_FOREACH(QString key, settings->childKeys()) {
-            if (!key.startsWith(authTypePrefix))
-                continue;
-            acc->_settingsMap.insert(key, settings->value(key));
+        if (auto acc = load(settings)) {
+            if (migratedCreds) {
+                acc->setMigrated(true);
+            }
+            addAccount(acc);
+            return true;
         }
-
-        acc->setCredentials(CredentialsFactory::create(settings->value(QLatin1String(authTypeC)).toString()));
-
-        // now the cert, it is in the general group
-        settings->beginGroup(QLatin1String("General"));
-        acc->setApprovedCerts(QSslCertificate::fromData(settings->value(caCertsKeyC).toByteArray()));
-        acc->setMigrated(migratedCreds);
-        acc->setSslErrorHandler(new SslDialogErrorHandler);
-        addAccount(acc);
-        return true;
     }
     return false;
 }
 
-
 void AccountManager::save()
 {
-    foreach (const auto &acc , _accounts) {
-        save(acc->account());
+    QScopedPointer<QSettings> settings(Account::settingsWithGroup("Accounts"));
+    foreach (const auto &acc, _accounts) {
+        settings->beginGroup(acc->account()->id());
+        save(acc->account(), settings);
+        settings->endGroup();
     }
 }
 
-void AccountManager::save(const AccountPtr& acc)
+void AccountManager::save(const AccountPtr& acc, QScopedPointer<QSettings>& settings)
 {
-    QScopedPointer<QSettings> settings(Account::settingsWithGroup(Theme::instance()->appName()));
     settings->setValue(QLatin1String(urlC), acc->_url.toString());
     if (acc->_credentials) {
         acc->_credentials->persist();
@@ -145,6 +153,7 @@ void AccountManager::save(const AccountPtr& acc)
     if (!certs.isEmpty()) {
         settings->setValue( QLatin1String(caCertsKeyC), certs );
     }
+    settings->endGroup();
 
     // Save cookies.
     if (acc->_am) {
@@ -156,6 +165,31 @@ void AccountManager::save(const AccountPtr& acc)
     }
 }
 
+AccountPtr AccountManager::load(const QScopedPointer<QSettings>& settings)
+{
+    auto acc = Account::create();
+
+    acc->setUrl(settings->value(QLatin1String(urlC)).toUrl());
+
+    // We want to only restore settings for that auth type and the user value
+    acc->_settingsMap.insert(QLatin1String(userC), settings->value(userC));
+    QString authTypePrefix = settings->value(authTypeC).toString() + "_";
+    Q_FOREACH(QString key, settings->childKeys()) {
+        if (!key.startsWith(authTypePrefix))
+            continue;
+        acc->_settingsMap.insert(key, settings->value(key));
+    }
+
+    acc->setCredentials(CredentialsFactory::create(settings->value(QLatin1String(authTypeC)).toString()));
+
+    // now the cert, it is in the general group
+    settings->beginGroup(QLatin1String("General"));
+    acc->setApprovedCerts(QSslCertificate::fromData(settings->value(caCertsKeyC).toByteArray()));
+    acc->setSslErrorHandler(new SslDialogErrorHandler);
+    settings->endGroup();
+    return acc;
+}
+
 void AccountManager::addAccount(const AccountPtr& newAccount)
 {
     AccountStatePtr newAccountState(new AccountState(newAccount));
diff --git a/src/gui/accountmanager.h b/src/gui/accountmanager.h
index 0ea3361..65a9ccc 100644
--- a/src/gui/accountmanager.h
+++ b/src/gui/accountmanager.h
@@ -27,12 +27,12 @@ public:
     ~AccountManager() {}
 
     /**
-     * Saves the account to a given settings file
+     * Saves the accounts to a given settings file
      */
     void save();
 
     /**
-     * Creates an account object from from a given settings file.
+     * Creates account objects from from a given settings file.
      * return true if the account was restored
      */
     bool restore();
@@ -51,7 +51,9 @@ public:
     QList<AccountStatePtr> accounts() { return _accounts; }
 
 private:
-    void save(const AccountPtr &account);
+    void save(const AccountPtr& account, QScopedPointer<QSettings>& settings);
+    AccountPtr load(const QScopedPointer<QSettings>& settings);
+    bool restoreFromLegacySettings();
 
 
 Q_SIGNALS:
@@ -63,4 +65,4 @@ private:
     QList<AccountStatePtr> _accounts;
 };
 
-}
\ No newline at end of file
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud-client.git



More information about the Pkg-owncloud-commits mailing list