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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/**
* Copyright (C) 2021-2024 Saturneric <eric@bktus.com>
*
* This file is part of GpgFrontend.
*
* GpgFrontend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GpgFrontend 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
*
* The initial version of the source code is inherited from
* the gpg4usb project, which is under GPL-3.0-or-later.
*
* All the source code of GpgFrontend was modified and released by
* Saturneric <eric@bktus.com> starting on May 12, 2021.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#include "EMailAccountStore.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QMutex>
#include <QMutexLocker>
#include <QSettings>
#include <QUuid>
#include "GFModule.h"
#include "GFSDKUI.h"
namespace {
// Relative: the Host places them in this module's own settings group, which
// is where they always were ("email/..."), so no account moves.
constexpr auto kSchemaVersionKey = "schema_version";
constexpr auto kAccountsKey = "accounts";
constexpr auto kDefaultAccountKey = "default_account";
constexpr auto kSchemaVersion = 1;
/// A read-modify-write of the account list is several calls; the mutex keeps
/// this module's own threads from interleaving them. No settings object is
/// shared: every call reaches the Host's settings through the storage
/// capability.
Q_GLOBAL_STATIC(QMutex, settings_mutex)
auto Get(const char* key, const QVariant& fallback = {}) -> QVariant {
return gf::sdk::Setting(GFModuleSdkContext(), GF_SETTING_MODULE, key,
fallback);
}
void Put(const char* key, const QVariant& value) {
gf::sdk::SetSetting(GFModuleSdkContext(), GF_SETTING_MODULE, key, value);
}
} // namespace
namespace EMailAccountStore {
auto Load() -> QList<MailAccountConfig> { return LoadChecked().accounts; }
auto LoadChecked() -> LoadResult {
QMutexLocker locker(settings_mutex());
const auto version = Get(kSchemaVersionKey, 0).toInt();
if (version > kSchemaVersion) {
// Written by a newer build. Refusing to read it is the honest answer:
// silently reinterpreting fields we do not understand could downgrade a
// transport's security, and writing it back would destroy the newer data.
LOG_WARN("mail account schema is newer than this build understands");
return {{}, LoadOutcome::kNEWER};
}
const auto raw = Get(kAccountsKey).toString();
const auto document = QJsonDocument::fromJson(raw.toUtf8());
if (!document.isArray()) {
// An absent value is not a corrupt one: a profile that has never had a
// mail account has no key here, and that must stay writable.
if (raw.trimmed().isEmpty()) return {};
LOG_WARN("mail account list is not readable as this schema");
return {{}, LoadOutcome::kUNREADABLE};
}
QList<MailAccountConfig> accounts;
for (const auto value : document.array()) {
if (!value.isObject()) continue;
auto account = MailAccountConfig::FromJson(value.toObject());
if (account.id.isEmpty()) continue;
accounts.append(account);
}
return {accounts, LoadOutcome::kOK};
}
void Store(const QList<MailAccountConfig>& accounts,
const QString& default_id) {
QMutexLocker locker(settings_mutex());
// Refused here, not only in the settings page. A newer build may have
// written a schema this one cannot represent, and stamping kSchemaVersion
// over it would discard those accounts irreversibly. The page checks too and
// gives the user a reason; this is what makes the rule hold for any future
// caller that forgets to.
if (Get(kSchemaVersionKey, 0).toInt() > kSchemaVersion) {
LOG_WARN("refusing to overwrite mail accounts written by a newer version");
return;
}
QJsonArray array;
QStringList ids;
for (const auto& account : accounts) {
if (account.id.isEmpty()) continue;
array.append(account.ToJson());
ids.append(account.id);
}
// A default naming an account that is no longer listed would leave every
// operation pointing at something the user thought they had removed.
auto resolved = default_id;
if (!ids.contains(resolved))
resolved = ids.isEmpty() ? QString() : ids.first();
Put(kSchemaVersionKey, kSchemaVersion);
Put(kAccountsKey, QString::fromUtf8(
QJsonDocument(array).toJson(QJsonDocument::Compact)));
Put(kDefaultAccountKey, resolved);
}
auto DefaultAccount() -> MailAccountConfig {
const auto accounts = Load();
if (accounts.isEmpty()) return {};
QString default_id;
{
QMutexLocker locker(settings_mutex());
default_id = Get(kDefaultAccountKey).toString();
}
for (const auto& account : accounts) {
if (account.id == default_id) return account;
}
for (const auto& account : accounts) {
if (account.IsUsable()) return account;
}
return accounts.first();
}
auto FindById(const QString& id, MailAccountConfig& out) -> bool {
if (id.isEmpty()) return false;
for (const auto& account : Load()) {
if (account.id != id) continue;
out = account;
return true;
}
return false;
}
auto ImapAccounts() -> QList<MailAccountConfig> {
QList<MailAccountConfig> result;
for (const auto& account : Load()) {
if (account.imap.enabled && account.IsUsable()) result.append(account);
}
return result;
}
auto SmtpAccounts() -> QList<MailAccountConfig> {
QList<MailAccountConfig> result;
for (const auto& account : Load()) {
if (account.smtp.enabled && account.IsUsable()) result.append(account);
}
return result;
}
auto NewAccountId() -> QString {
return QUuid::createUuid().toString(QUuid::WithoutBraces);
}
} // namespace EMailAccountStore
|