From bd5ef6e3d9d4105be2f4eea16c774c01c8a84379 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Tue, 6 Sep 2016 12:11:19 +0900 Subject: Settings, SettingsGroup, AccountSettings Settings is a trivial wrapper around QSettings that makes value() and setValue() available from QML (Tensor has the same class atm). SettingsGroup is a cleaner way to reach for sections inside QSettings. It doesn't refer to another QSettings class, rather derives from it and overrides functions that deal with groups. AccountSettings, contrary to the two above, is by no means generic: it serves the specific purpose of storing settings of a single account in a uniform way. Rationale of it is that key literals like "keep_logged_in" cannot be enforced, while function names can; and the same goes for QVariants stored inside - the class enforces specific types while allowing further extension by clients. Note that functions in QSettings are not virtual, so all these classes are not polymorphic. --- settings.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 settings.cpp (limited to 'settings.cpp') diff --git a/settings.cpp b/settings.cpp new file mode 100644 index 00000000..45af33e3 --- /dev/null +++ b/settings.cpp @@ -0,0 +1,102 @@ +#include "settings.h" + +#include +#include + +using namespace QMatrixClient; + +Settings::~Settings() +{ } + +void Settings::setValue(const QString& key, const QVariant& value) +{ +// qDebug() << "Setting" << key << "to" << value; + QSettings::setValue(key, value); +} + +QVariant Settings::value(const QString& key, const QVariant& defaultValue) const +{ + return QSettings::value(key, defaultValue); +} + +SettingsGroup::~SettingsGroup() +{ } + +void SettingsGroup::setValue(const QString& key, const QVariant& value) +{ + Settings::setValue(groupPath + "/" + key, value); +} + +bool SettingsGroup::contains(const QString& key) const +{ + return Settings::contains(groupPath + "/" + key); +} + +QVariant SettingsGroup::value(const QString& key, const QVariant& defaultValue) const +{ + return Settings::value(groupPath + "/" + key, defaultValue); +} + +QString SettingsGroup::group() const +{ + return groupPath; +} + +QStringList SettingsGroup::childGroups() const +{ + const_cast(this)->beginGroup(groupPath); + QStringList l { Settings::childGroups() }; + const_cast(this)->endGroup(); + return l; +} + +void SettingsGroup::remove(const QString& key) +{ + QString fullKey { groupPath }; + if (!key.isEmpty()) + fullKey += "/" + key; + Settings::remove(fullKey); +} + +AccountSettings::~AccountSettings() +{ } + +bool AccountSettings::keepLoggedIn() const +{ + return value("keep_logged_in", false).toBool(); +} + +void AccountSettings::setKeepLoggedIn(bool newSetting) +{ + setValue("keep_logged_in", newSetting); +} + +QUrl AccountSettings::homeserver() const +{ + return QUrl::fromUserInput(value("homeserver").toString()); +} + +void AccountSettings::setHomeserver(const QUrl& url) +{ + setValue("homeserver", url.toString()); +} + +QString AccountSettings::userId() const +{ + return group().section('/', -1); +} + +QString AccountSettings::accessToken() const +{ + return value("access_token").toString(); +} + +void AccountSettings::setAccessToken(const QString& accessToken) +{ + setValue("access_token", accessToken); +} + +void AccountSettings::clearAccessToken() +{ + remove("access_token"); +} -- cgit v1.2.3