Move Account::settings to AccountState::settings

It is only used by the GUI. Also return a unique_ptr to make the ownership clearer
This commit is contained in:
Olivier Goffart 2015-06-15 15:04:39 +02:00
parent 758a820b0c
commit 6e337ad242
9 changed files with 37 additions and 32 deletions

View file

@ -41,7 +41,7 @@ AccountManager *AccountManager::instance()
bool AccountManager::restore()
{
QScopedPointer<QSettings> settings(Account::settingsWithGroup(QLatin1String(accountsC)));
auto settings = Account::settingsWithGroup(QLatin1String(accountsC));
// If there are no accounts, check the old format.
if (settings->childGroups().isEmpty()) {
@ -63,7 +63,7 @@ bool AccountManager::restore()
bool AccountManager::restoreFromLegacySettings()
{
// try to open the correctly themed settings
QScopedPointer<QSettings> settings(Account::settingsWithGroup(Theme::instance()->appName()));
auto settings = Account::settingsWithGroup(Theme::instance()->appName());
bool migratedCreds = false;
@ -120,7 +120,7 @@ bool AccountManager::restoreFromLegacySettings()
void AccountManager::save()
{
QScopedPointer<QSettings> settings(Account::settingsWithGroup(QLatin1String(accountsC)));
auto settings = Account::settingsWithGroup(QLatin1String(accountsC));
foreach (const auto &acc, _accounts) {
settings->beginGroup(acc->account()->id());
save(acc->account(), *settings);
@ -213,7 +213,7 @@ void AccountManager::deleteAccount(AccountState* account)
auto copy = *it; // keep a reference to the shared pointer so it does not delete it just yet
_accounts.erase(it);
QScopedPointer<QSettings> settings(Account::settingsWithGroup(QLatin1String(accountsC)));
auto settings = Account::settingsWithGroup(QLatin1String(accountsC));
settings->remove(account->account()->id());
accountRemoved(account);

View file

@ -18,6 +18,7 @@
#include "creds/abstractcredentials.h"
#include <QDebug>
#include <QSettings>
namespace OCC {
@ -250,5 +251,12 @@ void AccountState::slotCredentialsFetched(AbstractCredentials* credentials)
checkConnectivity();
}
std::unique_ptr<QSettings> AccountState::settings()
{
auto s = _account->settingsWithGroup(QLatin1String("Accounts"));
s->beginGroup(_account->id());
return s;
}
} // namespace OCC

View file

@ -19,7 +19,9 @@
#include <QPointer>
#include "utility.h"
#include "connectionvalidator.h"
#include <memory>
class QSettings;
namespace OCC {
@ -86,6 +88,11 @@ public:
/// connection status and errors.
void checkConnectivity();
/** Returns a new settings object for this account, already in the right groups. */
std::unique_ptr<QSettings> settings();
private:
void setState(State state);

View file

@ -634,14 +634,14 @@ bool Folder::estimateState(QString fn, csync_ftw_type_e t, SyncFileStatus* s)
void Folder::saveToSettings() const
{
QScopedPointer<QSettings> settings(_accountState->account()->settings());
auto settings = _accountState->settings();
settings->beginGroup(QLatin1String("Folders"));
FolderDefinition::save(*settings, _definition);
}
void Folder::removeFromSettings() const
{
QScopedPointer<QSettings> settings(_accountState->account()->settings());
auto settings = _accountState->settings();
settings->beginGroup(QLatin1String("Folders"));
settings->remove(_definition.alias);
}

View file

@ -184,7 +184,7 @@ int FolderMan::setupFolders()
{
unloadAndDeleteAllFolders();
QScopedPointer<QSettings> settings(Account::settingsWithGroup(QLatin1String("Accounts")));
auto settings = Account::settingsWithGroup(QLatin1String("Accounts"));
const auto accountsWithSettings = settings->childGroups();
if (accountsWithSettings.isEmpty()) {
return setupFoldersMigration();

View file

@ -326,25 +326,18 @@ QUrl Account::concatUrlPath(const QUrl &url, const QString &concatPath,
QString Account::_configFileName;
QSettings *Account::settingsWithGroup(const QString& group, QObject *parent)
std::unique_ptr<QSettings> Account::settingsWithGroup(const QString& group, QObject *parent)
{
if (_configFileName.isEmpty()) {
// cache file name
ConfigFile cfg;
_configFileName = cfg.configFile();
}
QSettings *settings = new QSettings(_configFileName, QSettings::IniFormat, parent);
std::unique_ptr<QSettings> settings(new QSettings(_configFileName, QSettings::IniFormat, parent));
settings->beginGroup(group);
return settings;
}
QSettings* Account::settings()
{
auto s = settingsWithGroup(QLatin1String("Accounts"));
s->beginGroup(id());
return s;
}
QVariant Account::credentialSetting(const QString &key) const
{
if (_credentials) {

View file

@ -25,6 +25,7 @@
#include <QSslError>
#include <QSharedPointer>
#include "utility.h"
#include <memory>
class QSettings;
class QNetworkReply;
@ -124,10 +125,7 @@ public:
/** Returns a new settings pre-set in a specific group. The Settings will be created
with the given parent. If no parents is specified, the caller must destroy the settings */
static QSettings* settingsWithGroup(const QString &group, QObject *parent = 0);
/** Returns a new settings object for this account, already in the right groups. */
QSettings* settings();
static std::unique_ptr<QSettings> settingsWithGroup(const QString& group, QObject* parent = 0);
// to be called by credentials only
QVariant credentialSetting(const QString& key) const;

View file

@ -211,7 +211,7 @@ void HttpCredentials::fetch()
_certificatePath = _account->credentialSetting(QLatin1String(certifPathC)).toString();
_certificatePasswd = _account->credentialSetting(QLatin1String(certifPasswdC)).toString();
QSettings *settings = _account->settingsWithGroup(Theme::instance()->appName());
auto settings = _account->settingsWithGroup(Theme::instance()->appName());
const QString kck = keychainKey(_account->url().toString(), _user );
QString key = QString::fromLatin1( "%1/data" ).arg( kck );
@ -225,12 +225,11 @@ void HttpCredentials::fetch()
}
if (_ready) {
settings->deleteLater();
Q_EMIT fetched();
} else {
ReadPasswordJob *job = new ReadPasswordJob(Theme::instance()->appName());
settings->setParent(job); // make the job parent to make setting deleted properly
job->setSettings(settings);
job->setSettings(settings.release());
job->setInsecureFallback(false);
job->setKey(kck);
@ -319,9 +318,9 @@ void HttpCredentials::invalidateToken()
}
DeletePasswordJob *job = new DeletePasswordJob(Theme::instance()->appName());
QSettings *settings = _account->settingsWithGroup(Theme::instance()->appName());
auto settings = _account->settingsWithGroup(Theme::instance()->appName());
settings->setParent(job); // make the job parent to make setting deleted properly
job->setSettings(settings);
job->setSettings(settings.release());
job->setInsecureFallback(true);
job->setKey(kck);
job->start();
@ -347,9 +346,9 @@ void HttpCredentials::persist()
_account->setCredentialSetting(QLatin1String(certifPathC), _certificatePath);
_account->setCredentialSetting(QLatin1String(certifPasswdC), _certificatePasswd);
WritePasswordJob *job = new WritePasswordJob(Theme::instance()->appName());
QSettings *settings = _account->settingsWithGroup(Theme::instance()->appName());
auto settings = _account->settingsWithGroup(Theme::instance()->appName());
settings->setParent(job); // make the job parent to make setting deleted properly
job->setSettings(settings);
job->setSettings(settings.release());
job->setInsecureFallback(false);
connect(job, SIGNAL(finished(QKeychain::Job*)), SLOT(slotWriteJobDone(QKeychain::Job*)));

View file

@ -206,7 +206,7 @@ void ShibbolethCredentials::fetch()
} else {
_url = _account->url();
ReadPasswordJob *job = new ReadPasswordJob(Theme::instance()->appName());
job->setSettings(_account->settingsWithGroup(Theme::instance()->appName(), job));
job->setSettings(_account->settingsWithGroup(Theme::instance()->appName(), job).release());
job->setInsecureFallback(false);
job->setKey(keychainKey(_account->url().toString(), "shibAssertion"));
connect(job, SIGNAL(finished(QKeychain::Job*)), SLOT(slotReadJobDone(QKeychain::Job*)));
@ -315,7 +315,7 @@ void ShibbolethCredentials::invalidateAndFetch()
// delete the credentials, then in the slot fetch them again (which will trigger browser)
DeletePasswordJob *job = new DeletePasswordJob(Theme::instance()->appName());
job->setSettings(_account->settingsWithGroup(Theme::instance()->appName(), job));
job->setSettings(_account->settingsWithGroup(Theme::instance()->appName(), job).release());
connect(job, SIGNAL(finished(QKeychain::Job*)), SLOT(slotInvalidateAndFetchInvalidateDone(QKeychain::Job*)));
job->setKey(keychainKey(_account->url().toString(), "shibAssertion"));
job->start();
@ -351,7 +351,7 @@ void ShibbolethCredentials::slotReadJobDone(QKeychain::Job *job)
addToCookieJar(_shibCookie);
}
// access
job->setSettings(_account->settingsWithGroup(Theme::instance()->appName(), job));
job->setSettings(_account->settingsWithGroup(Theme::instance()->appName(), job).release());
_ready = true;
_stillValid = true;
@ -412,7 +412,7 @@ QByteArray ShibbolethCredentials::shibCookieName()
void ShibbolethCredentials::storeShibCookie(const QNetworkCookie &cookie)
{
WritePasswordJob *job = new WritePasswordJob(Theme::instance()->appName());
job->setSettings(_account->settingsWithGroup(Theme::instance()->appName(), job));
job->setSettings(_account->settingsWithGroup(Theme::instance()->appName(), job).release());
// we don't really care if it works...
//connect(job, SIGNAL(finished(QKeychain::Job*)), SLOT(slotWriteJobDone(QKeychain::Job*)));
job->setKey(keychainKey(_account->url().toString(), "shibAssertion"));
@ -423,7 +423,7 @@ void ShibbolethCredentials::storeShibCookie(const QNetworkCookie &cookie)
void ShibbolethCredentials::removeShibCookie()
{
DeletePasswordJob *job = new DeletePasswordJob(Theme::instance()->appName());
job->setSettings(_account->settingsWithGroup(Theme::instance()->appName(), job));
job->setSettings(_account->settingsWithGroup(Theme::instance()->appName(), job).release());
job->setKey(keychainKey(_account->url().toString(), "shibAssertion"));
job->start();
}