@@ -94,91 +94,87 @@ AccountManager *AccountManager::create(QQmlEngine *qmlEngine, QJSEngine *)
9494
9595bool AccountManager::restore ()
9696{
97- auto settings = ConfigFile::settingsWithGroup (accountsC ());
98- if (settings->status () != QSettings::NoError) {
99- qCWarning (lcAccountManager) << " Could not read settings from" << settings->fileName ()
100- << settings->status ();
97+ auto settings = ConfigFile::makeQSettings ();
98+ if (settings.status () != QSettings::NoError) {
99+ qCWarning (lcAccountManager) << " Could not read settings from" << settings.fileName () << settings.status ();
101100 return false ;
102101 }
103102
104- // If there are no accounts, check the old format.
105- const auto &childGroups = settings->childGroups ();
106-
107- for (const auto &accountId : childGroups) {
108- settings->beginGroup (accountId);
109- if (auto acc = loadAccountHelper (*settings)) {
110- acc->_id = accountId;
111- if (auto accState = AccountState::loadFromSettings (acc, *settings)) {
103+ const auto size = settings.beginReadArray (accountsC ());
104+ for (auto i = 0 ; i < size; ++i) {
105+ settings.setArrayIndex (i);
106+ if (auto acc = loadAccountHelper (settings)) {
107+ if (auto accState = AccountState::loadFromSettings (acc, settings)) {
112108 addAccountState (std::move (accState));
113109 }
114110 }
115- settings->endGroup ();
116111 }
112+ settings.endArray ();
117113
118114 return true ;
119115}
120116
121117void AccountManager::save (bool saveCredentials)
122118{
123- for ( const auto &acc : std::as_const (_accounts)) {
124- saveAccount (acc-> account (). data (), saveCredentials );
125- }
119+ auto settings = ConfigFile::makeQSettings ();
120+ settings. remove ( accountsC () );
121+ settings. beginWriteArray ( accountsC (), _accounts. size ());
126122
127- qCInfo (lcAccountManager) << " Saved all account settings" ;
128- }
123+ int i = 0 ;
124+ for (const auto &accountState : std::as_const (_accounts)) {
125+ settings.setArrayIndex (i++);
126+ auto account = accountState->account ();
127+ qCDebug (lcAccountManager) << " Saving account" << account->url ().toString ();
128+ settings.setValue (urlC (), account->_url .toString ());
129+ settings.setValue (davUserDisplyNameC (), account->_displayName );
130+ settings.setValue (userUUIDC (), account->uuid ());
131+ if (account->hasCapabilities ()) {
132+ settings.setValue (capabilitesC (), account->capabilities ().raw ());
133+ }
134+ if (account->hasDefaultSyncRoot ()) {
135+ settings.setValue (defaultSyncRootC (), account->defaultSyncRoot ());
136+ }
137+ if (account->_credentials ) {
138+ if (saveCredentials) {
139+ // Only persist the credentials if the parameter is set, on migration from 1.8.x
140+ // we want to save the accounts but not overwrite the credentials
141+ // (This is easier than asynchronously fetching the credentials from keychain and then
142+ // re-persisting them)
143+ account->_credentials ->persist ();
144+ }
129145
130- void AccountManager::saveAccount (Account *account, bool saveCredentials)
131- {
132- qCDebug (lcAccountManager) << " Saving account" << account->url ().toString ();
133- auto settings = ConfigFile::settingsWithGroup (accountsC ());
134- settings->beginGroup (account->id ());
135-
136- settings->setValue (urlC (), account->_url .toString ());
137- settings->setValue (davUserDisplyNameC (), account->_displayName );
138- settings->setValue (userUUIDC (), account->uuid ());
139- if (account->hasCapabilities ()) {
140- settings->setValue (capabilitesC (), account->capabilities ().raw ());
141- }
142- if (account->hasDefaultSyncRoot ()) {
143- settings->setValue (defaultSyncRootC (), account->defaultSyncRoot ());
144- }
145- if (account->_credentials ) {
146- if (saveCredentials) {
147- // Only persist the credentials if the parameter is set, on migration from 1.8.x
148- // we want to save the accounts but not overwrite the credentials
149- // (This is easier than asynchronously fetching the credentials from keychain and then
150- // re-persisting them)
151- account->_credentials ->persist ();
146+ for (auto it = account->_settingsMap .constBegin (); it != account->_settingsMap .constEnd (); ++it) {
147+ settings.setValue (it.key (), it.value ());
148+ }
149+
150+ // HACK: Save http_user also as user
151+ if (account->_settingsMap .contains (httpUserC ()))
152+ settings.setValue (userC (), account->_settingsMap .value (httpUserC ()));
152153 }
153154
154- for (auto it = account->_settingsMap .constBegin (); it != account->_settingsMap .constEnd (); ++it) {
155- settings->setValue (it.key (), it.value ());
155+ // Save accepted certificates.
156+ settings.beginGroup (" General" );
157+ qCInfo (lcAccountManager) << " Saving " << account->approvedCerts ().count () << " unknown certs." ;
158+ const auto approvedCerts = account->approvedCerts ();
159+ QByteArray certs;
160+ for (const auto &cert : approvedCerts) {
161+ certs += cert.toPem () + ' \n ' ;
156162 }
163+ if (!certs.isEmpty ()) {
164+ settings.setValue (caCertsKeyC (), certs);
165+ }
166+ settings.endGroup ();
157167
158- // HACK: Save http_user also as user
159- if (account->_settingsMap .contains (httpUserC ()))
160- settings->setValue (userC (), account->_settingsMap .value (httpUserC ()));
161- }
168+ // save the account state
169+ this ->account (account->uuid ())->writeToSettings (settings);
170+ settings.endGroup ();
162171
163- // Save accepted certificates.
164- settings->beginGroup (QStringLiteral (" General" ));
165- qCInfo (lcAccountManager) << " Saving " << account->approvedCerts ().count () << " unknown certs." ;
166- const auto approvedCerts = account->approvedCerts ();
167- QByteArray certs;
168- for (const auto &cert : approvedCerts) {
169- certs += cert.toPem () + ' \n ' ;
172+ settings.sync ();
173+ qCDebug (lcAccountManager) << " Saved account settings, status:" << settings.status ();
170174 }
171- if (!certs.isEmpty ()) {
172- settings->setValue (caCertsKeyC (), certs);
173- }
174- settings->endGroup ();
175-
176- // save the account state
177- this ->account (account->uuid ())->writeToSettings (*settings);
178- settings->endGroup ();
175+ settings.endArray ();
179176
180- settings->sync ();
181- qCDebug (lcAccountManager) << " Saved account settings, status:" << settings->status ();
177+ qCInfo (lcAccountManager) << " Saved all account settings" ;
182178}
183179
184180QStringList AccountManager::accountNames () const
@@ -240,28 +236,13 @@ AccountPtr AccountManager::loadAccountHelper(QSettings &settings)
240236 return acc;
241237}
242238
243- AccountStatePtr AccountManager::account (const QString &name )
239+ AccountStatePtr AccountManager::account (const QUuid uuid )
244240{
245- for (const auto &acc : std::as_const (_accounts)) {
246- if (acc->account ()->displayNameWithHost () == name) {
247- return acc;
248- }
249- }
250- return AccountStatePtr ();
251- }
252-
253- AccountStatePtr AccountManager::account (const QUuid uuid) {
254241 return _accounts.value (uuid);
255242}
256243
257244AccountStatePtr AccountManager::addAccount (const AccountPtr &newAccount)
258245{
259- auto id = newAccount->id ();
260- if (id.isEmpty () || !isAccountIdAvailable (id)) {
261- id = generateFreeAccountId ();
262- }
263- newAccount->_id = id;
264-
265246 return addAccountState (AccountState::fromNewAccount (newAccount));
266247}
267248
@@ -283,12 +264,10 @@ void AccountManager::deleteAccount(AccountStatePtr account)
283264 account->account ()->credentials ()->forgetSensitiveData ();
284265 account->account ()->credentialManager ()->clear ();
285266
286- auto settings = ConfigFile::settingsWithGroup (accountsC ());
287- settings->remove (account->account ()->id ());
288-
289267 Q_EMIT accountRemoved (account);
290268 Q_EMIT accountsChanged ();
291269 account->deleteLater ();
270+ save (false );
292271}
293272
294273AccountPtr AccountManager::createAccount (const QUuid &uuid)
@@ -305,36 +284,12 @@ void AccountManager::shutdown()
305284 }
306285}
307286
308- bool AccountManager::isAccountIdAvailable (const QString &id) const
309- {
310- for (const auto &acc : _accounts) {
311- if (acc->account ()->id () == id) {
312- return false ;
313- }
314- }
315- if (_additionalBlockedAccountIds.contains (id))
316- return false ;
317- return true ;
318- }
319-
320- QString AccountManager::generateFreeAccountId () const
321- {
322- int i = 0 ;
323- while (true ) {
324- QString id = QString::number (i);
325- if (isAccountIdAvailable (id)) {
326- return id;
327- }
328- ++i;
329- }
330- }
331-
332287AccountStatePtr AccountManager::addAccountState (std::unique_ptr<AccountState> &&accountState)
333288{
334289 auto *rawAccount = accountState->account ().data ();
335- connect (rawAccount, &Account::wantsAccountSaved, this , [rawAccount, this ] {
290+ connect (rawAccount, &Account::wantsAccountSaved, this , [this ] {
336291 // persis the account, not the credentials, we don't know whether they are ready yet
337- saveAccount (rawAccount, false );
292+ save ( false );
338293 });
339294
340295 AccountStatePtr statePtr = accountState.release ();
0 commit comments