diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2016-09-06 12:11:19 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2016-09-06 12:11:19 +0900 |
commit | bd5ef6e3d9d4105be2f4eea16c774c01c8a84379 (patch) | |
tree | d17bc7d76178bdaa79db4547f054f3163d5471e0 | |
parent | 4e70dd91bb70fa2b9b338686e4d5ab2560947723 (diff) | |
download | libquotient-bd5ef6e3d9d4105be2f4eea16c774c01c8a84379.tar.gz libquotient-bd5ef6e3d9d4105be2f4eea16c774c01c8a84379.zip |
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.
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | settings.cpp | 102 | ||||
-rw-r--r-- | settings.h | 91 |
3 files changed, 194 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 80ffcfd7..e9127752 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,6 +32,7 @@ set(libqmatrixclient_SRCS user.cpp logmessage.cpp state.cpp + settings.cpp events/event.cpp events/roommessageevent.cpp events/roomnameevent.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 <QtCore/QUrl>
+#include <QtCore/QDebug>
+
+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<SettingsGroup*>(this)->beginGroup(groupPath);
+ QStringList l { Settings::childGroups() };
+ const_cast<SettingsGroup*>(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");
+}
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 <kitsune-ral@users.sf.net>
+ *
+ * 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 <QtCore/QSettings>
+#include <QtCore/QVector>
+
+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 <typename... ArgTs>
+ 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 <typename... ArgTs>
+ 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();
+ };
+}
|