1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#include "settings.h"
#include "logging.h"
#include <QtCore/QUrl>
using namespace QMatrixClient;
QString Settings::legacyOrganizationName {};
QString Settings::legacyApplicationName {};
void Settings::setLegacyNames(const QString& organizationName,
const QString& applicationName)
{
legacyOrganizationName = organizationName;
legacyApplicationName = applicationName;
}
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);
}
QVariant Settings::value(const QString& key, const QVariant& defaultValue) const
{
auto value = QSettings::value(key, legacySettings.value(key, defaultValue));
// QML's Qt.labs.Settings stores boolean values as strings, which, if loaded
// through the usual QSettings interface, confuses QML
// (QVariant("false") == true in JavaScript). Since we have a mixed
// environment where both QSettings and Qt.labs.Settings may potentially
// work with same settings, better ensure compatibility.
return value.toString() == QStringLiteral("false") ? QVariant(false) : value;
}
bool Settings::contains(const QString& key) const
{
return QSettings::contains(key) || legacySettings.contains(key);
}
QStringList Settings::childGroups() const
{
auto l = QSettings::childGroups();
return !l.isEmpty() ? l : legacySettings.childGroups();
}
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);
const_cast<QSettings&>(legacySettings).beginGroup(groupPath);
QStringList l = Settings::childGroups();
const_cast<SettingsGroup*>(this)->endGroup();
const_cast<QSettings&>(legacySettings).endGroup();
return l;
}
void SettingsGroup::remove(const QString& key)
{
QString fullKey { groupPath };
if (!key.isEmpty())
fullKey += "/" + 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)
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
{
return QUrl::fromUserInput(value(HomeserverKey).toString());
}
void AccountSettings::setHomeserver(const QUrl& url)
{
setValue(HomeserverKey, url.toString());
}
QString AccountSettings::userId() const { return group().section('/', -1); }
QString AccountSettings::accessToken() const
{
return value(AccessTokenKey).toString();
}
void AccountSettings::setAccessToken(const QString& accessToken)
{
qCWarning(MAIN) << "Saving access_token to QSettings is insecure."
" 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
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?
}
|