diff options
author | n-peugnet <n.peugnet@free.fr> | 2022-10-06 19:27:24 +0200 |
---|---|---|
committer | n-peugnet <n.peugnet@free.fr> | 2022-10-06 19:27:24 +0200 |
commit | d911b207f49e936b3e992200796110f0749ed150 (patch) | |
tree | 96d20ebb4d074a4c1755e21cb316a52d584daee3 /lib/settings.cpp | |
parent | 8ad8a74152c5701b6ca1f9a00487ba9257a439b4 (diff) | |
parent | 56c2f2e2b809b9077393eb617828f33d144f5634 (diff) | |
download | libquotient-d911b207f49e936b3e992200796110f0749ed150.tar.gz libquotient-d911b207f49e936b3e992200796110f0749ed150.zip |
New upstream version 0.7.0
Diffstat (limited to 'lib/settings.cpp')
-rw-r--r-- | lib/settings.cpp | 82 |
1 files changed, 56 insertions, 26 deletions
diff --git a/lib/settings.cpp b/lib/settings.cpp index 852e19cb..510d253c 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -1,10 +1,14 @@ +// SPDX-FileCopyrightText: 2016 Kitsune Ral <kitsune-ral@users.sf.net> +// SPDX-License-Identifier: LGPL-2.1-or-later + #include "settings.h" +#include "util.h" #include "logging.h" #include <QtCore/QUrl> -using namespace QMatrixClient; +using namespace Quotient; QString Settings::legacyOrganizationName {}; QString Settings::legacyApplicationName {}; @@ -16,14 +20,27 @@ void Settings::setLegacyNames(const QString& organizationName, legacyApplicationName = applicationName; } +Settings::Settings(QObject* parent) : QSettings(parent) +{ +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) + setIniCodec("UTF-8"); +#endif +} + void Settings::setValue(const QString& key, const QVariant& value) { -// qCDebug() << "Setting" << key << "to" << value; QSettings::setValue(key, value); if (legacySettings.contains(key)) legacySettings.remove(key); } +void Settings::remove(const QString& key) +{ + QSettings::remove(key); + if (legacySettings.contains(key)) + legacySettings.remove(key); +} + QVariant Settings::value(const QString& key, const QVariant& defaultValue) const { auto value = QSettings::value(key, legacySettings.value(key, defaultValue)); @@ -42,8 +59,12 @@ bool Settings::contains(const QString& key) const QStringList Settings::childGroups() const { - auto l = QSettings::childGroups(); - return !l.isEmpty() ? l : legacySettings.childGroups(); + auto groups = QSettings::childGroups(); + const auto& legacyGroups = legacySettings.childGroups(); + for (const auto& g: legacyGroups) + if (!groups.contains(g)) + groups.push_back(g); + return groups; } void SettingsGroup::setValue(const QString& key, const QVariant& value) @@ -56,15 +77,13 @@ bool SettingsGroup::contains(const QString& key) const return Settings::contains(groupPath + '/' + key); } -QVariant SettingsGroup::value(const QString& key, const QVariant& defaultValue) const +QVariant SettingsGroup::value(const QString& key, + const QVariant& defaultValue) const { return Settings::value(groupPath + '/' + key, defaultValue); } -QString SettingsGroup::group() const -{ - return groupPath; -} +QString SettingsGroup::group() const { return groupPath; } QStringList SettingsGroup::childGroups() const { @@ -84,40 +103,51 @@ void SettingsGroup::remove(const QString& key) Settings::remove(fullKey); } -QMC_DEFINE_SETTING(AccountSettings, QString, deviceId, "device_id", "", setDeviceId) -QMC_DEFINE_SETTING(AccountSettings, QString, deviceName, "device_name", "", setDeviceName) -QMC_DEFINE_SETTING(AccountSettings, bool, keepLoggedIn, "keep_logged_in", false, setKeepLoggedIn) +QUO_DEFINE_SETTING(AccountSettings, QString, deviceId, "device_id", {}, + setDeviceId) +QUO_DEFINE_SETTING(AccountSettings, QString, deviceName, "device_name", {}, + setDeviceName) +QUO_DEFINE_SETTING(AccountSettings, bool, keepLoggedIn, "keep_logged_in", false, + setKeepLoggedIn) + +namespace { +constexpr auto HomeserverKey = "homeserver"_ls; +constexpr auto AccessTokenKey = "access_token"_ls; +constexpr auto EncryptionAccountPickleKey = "encryption_account_pickle"_ls; +} QUrl AccountSettings::homeserver() const { - return QUrl::fromUserInput(value("homeserver").toString()); + return QUrl::fromUserInput(value(HomeserverKey).toString()); } void AccountSettings::setHomeserver(const QUrl& url) { - setValue("homeserver", url.toString()); + setValue(HomeserverKey, url.toString()); } -QString AccountSettings::userId() const +QString AccountSettings::userId() const { return group().section('/', -1); } + +void AccountSettings::clearAccessToken() { - return group().section('/', -1); + legacySettings.remove(AccessTokenKey); + legacySettings.remove(QStringLiteral("device_id")); // Force the server to + // re-issue it + remove(AccessTokenKey); } -QString AccountSettings::accessToken() const +QByteArray AccountSettings::encryptionAccountPickle() { - return value("access_token").toString(); + return value("encryption_account_pickle", "").toByteArray(); } -void AccountSettings::setAccessToken(const QString& accessToken) +void AccountSettings::setEncryptionAccountPickle( + const QByteArray& encryptionAccountPickle) { - qCWarning(MAIN) << "Saving access_token to QSettings is insecure." - " Developers, please save access_token separately."; - setValue("access_token", accessToken); + setValue("encryption_account_pickle", encryptionAccountPickle); } -void AccountSettings::clearAccessToken() +void AccountSettings::clearEncryptionAccountPickle() { - legacySettings.remove("access_token"); - legacySettings.remove("device_id"); // Force the server to re-issue it - remove("access_token"); + remove(EncryptionAccountPickleKey); // TODO: Force to re-issue it? } |