Skip to content

Commit 1f15adb

Browse files
committed
Refactor loading/saving settings
1 parent 0ec13d1 commit 1f15adb

File tree

17 files changed

+150
-331
lines changed

17 files changed

+150
-331
lines changed

src/gui/accountmanager.cpp

Lines changed: 63 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -94,91 +94,87 @@ AccountManager *AccountManager::create(QQmlEngine *qmlEngine, QJSEngine *)
9494

9595
bool 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

121117
void 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

184180
QStringList 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

257244
AccountStatePtr 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

294273
AccountPtr 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-
332287
AccountStatePtr 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();

src/gui/accountmanager.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ class OPENCLOUD_GUI_EXPORT AccountManager : public QObject
7171
*/
7272
const QList<AccountStatePtr> accounts() { return _accounts.values(); }
7373

74-
/**
75-
* Return the account state pointer for an account identified by its display name
76-
*/
77-
Q_DECL_DEPRECATED_X("Please use the uuid to specify the account") AccountStatePtr account(const QString &name);
78-
7974
/**
8075
* Return the account state pointer for an account identified by its display name
8176
*/
@@ -106,23 +101,17 @@ class OPENCLOUD_GUI_EXPORT AccountManager : public QObject
106101
void saveAccountHelper(Account *account, QSettings &settings, bool saveCredentials = true);
107102
AccountPtr loadAccountHelper(QSettings &settings);
108103

109-
bool isAccountIdAvailable(const QString &id) const;
110-
QString generateFreeAccountId() const;
111-
112104
// Adds an account to the tracked list, emitting accountAdded()
113105
AccountStatePtr addAccountState(std::unique_ptr<AccountState> &&accountState);
114106

115-
public Q_SLOTS:
116-
/// Saves account data, not including the credentials
117-
void saveAccount(Account *account, bool saveCredentials);
118-
119107
Q_SIGNALS:
120108
void accountAdded(AccountStatePtr account);
121109
void accountRemoved(AccountStatePtr account);
122110
void accountsChanged();
123111

124112
private:
125113
AccountManager() {}
114+
126115
QMap<QUuid, AccountStatePtr> _accounts;
127116
/// Account ids from settings that weren't read
128117
QSet<QString> _additionalBlockedAccountIds;

src/gui/accountstate.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -583,13 +583,6 @@ Account *AccountState::accountForQml() const
583583
return _account.data();
584584
}
585585

586-
std::unique_ptr<QSettings> AccountState::settings()
587-
{
588-
auto s = ConfigFile::settingsWithGroup(QStringLiteral("Accounts"));
589-
s->beginGroup(_account->id());
590-
return s;
591-
}
592-
593586
bool AccountState::isSettingUp() const
594587
{
595588
return _settingUp;

src/gui/accountstate.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,6 @@ class OPENCLOUD_GUI_EXPORT AccountState : public QObject
129129

130130
bool isConnected() const;
131131

132-
/** Returns a new settings object for this account, already in the right groups. */
133-
std::unique_ptr<QSettings> settings();
134-
135132
/** Mark the timestamp when the last successful ETag check happened for
136133
* this account.
137134
* The checkConnectivity() method uses the timestamp to save a call to

0 commit comments

Comments
 (0)