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.h | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 settings.h (limited to 'settings.h') diff --git a/settings.h b/settings.h new file mode 100644 index 00000000..005b6cef --- /dev/null +++ b/settings.h @@ -0,0 +1,91 @@ +/****************************************************************************** + * Copyright (C) 2016 Kitsune Ral + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#pragma once + +#include +#include + +class QVariant; +class QUrl; + +namespace QMatrixClient +{ + class Settings: public QSettings + { + public: + using QSettings::QSettings; + virtual ~Settings(); + + Q_INVOKABLE void setValue(const QString &key, + const QVariant &value); + Q_INVOKABLE QVariant value(const QString &key, + const QVariant &defaultValue = {}) const; + }; + + class SettingsGroup: public Settings + { + public: + template + explicit SettingsGroup(const QString& path, ArgTs... qsettingsArgs) + : Settings(qsettingsArgs...) + , groupPath(path) + { } + virtual ~SettingsGroup(); + + Q_INVOKABLE bool contains(const QString& key) const; + Q_INVOKABLE QVariant value(const QString &key, + const QVariant &defaultValue = {}) const; + Q_INVOKABLE QString group() const; + Q_INVOKABLE QStringList childGroups() const; + Q_INVOKABLE void setValue(const QString &key, + const QVariant &value); + + Q_INVOKABLE void remove(const QString& key); + + private: + QString groupPath; + }; + + class AccountSettings: public SettingsGroup + { + Q_OBJECT + Q_PROPERTY(QString userId READ userId) + Q_PROPERTY(QUrl homeserver READ homeserver WRITE setHomeserver) + Q_PROPERTY(bool keepLoggedIn READ keepLoggedIn WRITE setKeepLoggedIn) + Q_PROPERTY(QString accessToken READ accessToken WRITE setAccessToken) + public: + template + AccountSettings(const QString& accountId, ArgTs... qsettingsArgs) + : SettingsGroup("Accounts/" + accountId, qsettingsArgs...) + { } + virtual ~AccountSettings(); + + QString userId() const; + + QUrl homeserver() const; + void setHomeserver(const QUrl& url); + + bool keepLoggedIn() const; + void setKeepLoggedIn(bool newSetting); + + QString accessToken() const; + void setAccessToken(const QString& accessToken); + Q_INVOKABLE void clearAccessToken(); + }; +} -- cgit v1.2.3 From e24ebc6dc5382ad05077575d267f66efac39e8dd Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Tue, 6 Sep 2016 18:36:40 +0900 Subject: Fix building with VS2013 VS2013 doesn't like 'using' statements if a base class has private constructors (as in QSettings - Q_DISABLE_COPY makes a copy constructor private and deleted). Hence a workaround. --- settings.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'settings.h') diff --git a/settings.h b/settings.h index 005b6cef..7cc7895b 100644 --- a/settings.h +++ b/settings.h @@ -29,7 +29,13 @@ namespace QMatrixClient class Settings: public QSettings { public: +#if defined(_MSC_VER) && _MSC_VER <= 1200 + // VS 2013 (and probably older) aren't friends with 'using' statements + // that involve private constructors + explicit Settings(QObject* parent = 0) : QSettings(parent) { } +#else using QSettings::QSettings; +#endif virtual ~Settings(); Q_INVOKABLE void setValue(const QString &key, @@ -71,7 +77,7 @@ namespace QMatrixClient Q_PROPERTY(QString accessToken READ accessToken WRITE setAccessToken) public: template - AccountSettings(const QString& accountId, ArgTs... qsettingsArgs) + explicit AccountSettings(const QString& accountId, ArgTs... qsettingsArgs) : SettingsGroup("Accounts/" + accountId, qsettingsArgs...) { } virtual ~AccountSettings(); -- cgit v1.2.3 From ea865bade764c095aff175d10a4c2601a9ee34e0 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Tue, 6 Sep 2016 22:12:19 +0900 Subject: Corrected the faulty VS version --- settings.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'settings.h') diff --git a/settings.h b/settings.h index 7cc7895b..47b3ef85 100644 --- a/settings.h +++ b/settings.h @@ -29,7 +29,7 @@ namespace QMatrixClient class Settings: public QSettings { public: -#if defined(_MSC_VER) && _MSC_VER <= 1200 +#if defined(_MSC_VER) && _MSC_VER <= 1900 // VS 2013 (and probably older) aren't friends with 'using' statements // that involve private constructors explicit Settings(QObject* parent = 0) : QSettings(parent) { } -- cgit v1.2.3 From ae0b65440834a76f3ce6c266ee4b7d683b215842 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Tue, 6 Sep 2016 22:56:51 +0900 Subject: Relaxed VS version a bit. --- settings.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'settings.h') diff --git a/settings.h b/settings.h index 47b3ef85..10b2bb0a 100644 --- a/settings.h +++ b/settings.h @@ -29,7 +29,7 @@ namespace QMatrixClient class Settings: public QSettings { public: -#if defined(_MSC_VER) && _MSC_VER <= 1900 +#if defined(_MSC_VER) && _MSC_VER < 1900 // VS 2013 (and probably older) aren't friends with 'using' statements // that involve private constructors explicit Settings(QObject* parent = 0) : QSettings(parent) { } -- cgit v1.2.3