diff options
Diffstat (limited to 'lib/settings.cpp')
-rw-r--r-- | lib/settings.cpp | 75 |
1 files changed, 57 insertions, 18 deletions
diff --git a/lib/settings.cpp b/lib/settings.cpp index 124d7042..dd086d9c 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -4,7 +4,7 @@ #include <QtCore/QUrl> -using namespace QMatrixClient; +using namespace Quotient; QString Settings::legacyOrganizationName {}; QString Settings::legacyApplicationName {}; @@ -16,14 +16,25 @@ void Settings::setLegacyNames(const QString& organizationName, legacyApplicationName = applicationName; } +Settings::Settings(QObject* parent) : QSettings(parent) +{ + setIniCodec("UTF-8"); +} + 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 +53,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 +71,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,12 +97,17 @@ 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) +QTNT_DEFINE_SETTING(AccountSettings, QString, deviceId, "device_id", {}, + setDeviceId) +QTNT_DEFINE_SETTING(AccountSettings, QString, deviceName, "device_name", {}, + setDeviceName) +QTNT_DEFINE_SETTING(AccountSettings, bool, keepLoggedIn, "keep_logged_in", false, + setKeepLoggedIn) static const auto HomeserverKey = QStringLiteral("homeserver"); static const auto AccessTokenKey = QStringLiteral("access_token"); +static const auto EncryptionAccountPickleKey = + QStringLiteral("encryption_account_pickle"); QUrl AccountSettings::homeserver() const { @@ -101,10 +119,7 @@ void AccountSettings::setHomeserver(const QUrl& url) setValue(HomeserverKey, url.toString()); } -QString AccountSettings::userId() const -{ - return group().section('/', -1); -} +QString AccountSettings::userId() const { return group().section('/', -1); } QString AccountSettings::accessToken() const { @@ -114,13 +129,37 @@ QString AccountSettings::accessToken() const void AccountSettings::setAccessToken(const QString& accessToken) { qCWarning(MAIN) << "Saving access_token to QSettings is insecure." - " Developers, please save access_token separately."; + " Developers, do it manually or contribute to share " + "QtKeychain logic to libQuotient."; setValue(AccessTokenKey, accessToken); } void AccountSettings::clearAccessToken() { legacySettings.remove(AccessTokenKey); - legacySettings.remove(QStringLiteral("device_id")); // Force the server to re-issue it + legacySettings.remove(QStringLiteral("device_id")); // Force the server to + // re-issue it remove(AccessTokenKey); } + +QByteArray AccountSettings::encryptionAccountPickle() +{ + QString passphrase = ""; // FIXME: add QtKeychain + return value("encryption_account_pickle", "").toByteArray(); +} + +void AccountSettings::setEncryptionAccountPickle( + const QByteArray& encryptionAccountPickle) +{ + qCWarning(MAIN) + << "Saving encryption_account_pickle to QSettings is insecure." + " Developers, do it manually or contribute to share QtKeychain " + "logic to libQuotient."; + QString passphrase = ""; // FIXME: add QtKeychain + setValue("encryption_account_pickle", encryptionAccountPickle); +} + +void AccountSettings::clearEncryptionAccountPickle() +{ + remove(EncryptionAccountPickleKey); // TODO: Force to re-issue it? +} |