diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-07-16 10:02:36 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-07-16 10:19:34 +0200 |
commit | 80859db2fb6746441668efc851c500695aaf4d58 (patch) | |
tree | cb6f1dfbc3c1876d9edb6e3744102fa0029b23ce /framework/domain/settings/accountsettings.cpp | |
parent | 824ddd0fd4e3333c7c2afec83929c5b4795c16b7 (diff) | |
download | kube-80859db2fb6746441668efc851c500695aaf4d58.tar.gz kube-80859db2fb6746441668efc851c500695aaf4d58.zip |
Share the settings implementation
Diffstat (limited to 'framework/domain/settings/accountsettings.cpp')
-rw-r--r-- | framework/domain/settings/accountsettings.cpp | 337 |
1 files changed, 337 insertions, 0 deletions
diff --git a/framework/domain/settings/accountsettings.cpp b/framework/domain/settings/accountsettings.cpp new file mode 100644 index 00000000..cf348f39 --- /dev/null +++ b/framework/domain/settings/accountsettings.cpp | |||
@@ -0,0 +1,337 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | #include "accountsettings.h" | ||
20 | |||
21 | #include <sink/store.h> | ||
22 | #include <QDebug> | ||
23 | #include <QDir> | ||
24 | #include <QUrl> | ||
25 | |||
26 | AccountSettings::AccountSettings(QObject *parent) | ||
27 | : QObject(parent) | ||
28 | { | ||
29 | } | ||
30 | |||
31 | |||
32 | void AccountSettings::setAccountIdentifier(const QByteArray &id) | ||
33 | { | ||
34 | if (id.isEmpty()) { | ||
35 | return; | ||
36 | } | ||
37 | mAccountIdentifier = id; | ||
38 | |||
39 | //Clear | ||
40 | mIcon = QString(); | ||
41 | mName = QString(); | ||
42 | mImapServer = QString(); | ||
43 | mImapUsername = QString(); | ||
44 | mImapPassword = QString(); | ||
45 | mSmtpServer = QString(); | ||
46 | mSmtpUsername = QString(); | ||
47 | mSmtpPassword = QString(); | ||
48 | emit changed(); | ||
49 | emit imapResourceChanged(); | ||
50 | emit smtpResourceChanged(); | ||
51 | |||
52 | load(); | ||
53 | |||
54 | } | ||
55 | |||
56 | QByteArray AccountSettings::accountIdentifier() const | ||
57 | { | ||
58 | return mAccountIdentifier; | ||
59 | } | ||
60 | |||
61 | void AccountSettings::setPath(const QUrl &path) | ||
62 | { | ||
63 | auto normalizedPath = path.path(); | ||
64 | if (mPath != normalizedPath) { | ||
65 | mPath = normalizedPath; | ||
66 | emit pathChanged(); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | QUrl AccountSettings::path() const | ||
71 | { | ||
72 | return QUrl(mPath); | ||
73 | } | ||
74 | |||
75 | QValidator *AccountSettings::pathValidator() const | ||
76 | { | ||
77 | class PathValidator : public QValidator { | ||
78 | State validate(QString &input, int &pos) const { | ||
79 | Q_UNUSED(pos); | ||
80 | if (!input.isEmpty() && QDir(input).exists()) { | ||
81 | return Acceptable; | ||
82 | } else { | ||
83 | return Intermediate; | ||
84 | } | ||
85 | } | ||
86 | }; | ||
87 | static PathValidator *pathValidator = new PathValidator; | ||
88 | return pathValidator; | ||
89 | } | ||
90 | |||
91 | QValidator *AccountSettings::imapServerValidator() const | ||
92 | { | ||
93 | class ImapServerValidator : public QValidator { | ||
94 | State validate(QString &input, int &pos) const { | ||
95 | Q_UNUSED(pos); | ||
96 | // imaps://mainserver.example.net:475 | ||
97 | const QUrl url(input); | ||
98 | static QSet<QString> validProtocols = QSet<QString>() << "imap" << "imaps"; | ||
99 | if (url.isValid() && validProtocols.contains(url.scheme().toLower())) { | ||
100 | return Acceptable; | ||
101 | } else { | ||
102 | return Intermediate; | ||
103 | } | ||
104 | } | ||
105 | }; | ||
106 | static ImapServerValidator *validator = new ImapServerValidator; | ||
107 | return validator; | ||
108 | } | ||
109 | |||
110 | QValidator *AccountSettings::smtpServerValidator() const | ||
111 | { | ||
112 | class SmtpServerValidator : public QValidator { | ||
113 | State validate(QString &input, int &pos) const { | ||
114 | Q_UNUSED(pos); | ||
115 | // smtps://mainserver.example.net:475 | ||
116 | const QUrl url(input); | ||
117 | static QSet<QString> validProtocols = QSet<QString>() << "smtp" << "smtps"; | ||
118 | if (url.isValid() && validProtocols.contains(url.scheme().toLower())) { | ||
119 | return Acceptable; | ||
120 | } else { | ||
121 | return Intermediate; | ||
122 | } | ||
123 | } | ||
124 | }; | ||
125 | static SmtpServerValidator *validator = new SmtpServerValidator; | ||
126 | return validator; | ||
127 | } | ||
128 | |||
129 | void AccountSettings::saveAccount() | ||
130 | { | ||
131 | qDebug() << "Saving account " << mAccountIdentifier << mMailtransportIdentifier; | ||
132 | Q_ASSERT(!mAccountIdentifier.isEmpty()); | ||
133 | Sink::ApplicationDomain::SinkAccount account(mAccountIdentifier); | ||
134 | account.setProperty("type", "imap"); | ||
135 | account.setProperty("name", mName); | ||
136 | account.setProperty("icon", mIcon); | ||
137 | Q_ASSERT(!account.identifier().isEmpty()); | ||
138 | Sink::Store::modify(account).then<void>([]() {}, | ||
139 | [](int errorCode, const QString &errorMessage) { | ||
140 | qWarning() << "Error while creating account: " << errorMessage; | ||
141 | }) | ||
142 | .exec(); | ||
143 | } | ||
144 | |||
145 | void AccountSettings::loadAccount() | ||
146 | { | ||
147 | Q_ASSERT(!mAccountIdentifier.isEmpty()); | ||
148 | Sink::Store::fetchOne<Sink::ApplicationDomain::SinkAccount>(Sink::Query::IdentityFilter(mAccountIdentifier)) | ||
149 | .then<void, Sink::ApplicationDomain::SinkAccount>([this](const Sink::ApplicationDomain::SinkAccount &account) { | ||
150 | mIcon = account.getProperty("icon").toString(); | ||
151 | mName = account.getProperty("name").toString(); | ||
152 | emit changed(); | ||
153 | }).exec(); | ||
154 | } | ||
155 | |||
156 | void AccountSettings::loadImapResource() | ||
157 | { | ||
158 | Sink::Store::fetchOne<Sink::ApplicationDomain::SinkResource>(Sink::Query::AccountFilter(mAccountIdentifier) + Sink::Query::CapabilityFilter(Sink::ApplicationDomain::ResourceCapabilities::Mail::storage)) | ||
159 | .then<void, Sink::ApplicationDomain::SinkResource>([this](const Sink::ApplicationDomain::SinkResource &resource) { | ||
160 | mImapIdentifier = resource.identifier(); | ||
161 | mImapServer = resource.getProperty("server").toString(); | ||
162 | mImapUsername = resource.getProperty("username").toString(); | ||
163 | mImapPassword = resource.getProperty("password").toString(); | ||
164 | emit imapResourceChanged(); | ||
165 | }, | ||
166 | [](int errorCode, const QString &errorMessage) { | ||
167 | qWarning() << "Failed to find the imap resource: " << errorMessage; | ||
168 | }).exec(); | ||
169 | } | ||
170 | |||
171 | void AccountSettings::loadMaildirResource() | ||
172 | { | ||
173 | Sink::Store::fetchOne<Sink::ApplicationDomain::SinkResource>(Sink::Query::AccountFilter(mAccountIdentifier) + Sink::Query::CapabilityFilter(Sink::ApplicationDomain::ResourceCapabilities::Mail::storage)) | ||
174 | .then<void, Sink::ApplicationDomain::SinkResource>([this](const Sink::ApplicationDomain::SinkResource &resource) { | ||
175 | mMaildirIdentifier = resource.identifier(); | ||
176 | auto path = resource.getProperty("path").toString(); | ||
177 | if (mPath != path) { | ||
178 | mPath = path; | ||
179 | emit pathChanged(); | ||
180 | } | ||
181 | }, | ||
182 | [](int errorCode, const QString &errorMessage) { | ||
183 | qWarning() << "Failed to find the maildir resource: " << errorMessage; | ||
184 | }).exec(); | ||
185 | } | ||
186 | |||
187 | void AccountSettings::loadMailtransportResource() | ||
188 | { | ||
189 | Sink::Store::fetchOne<Sink::ApplicationDomain::SinkResource>(Sink::Query::AccountFilter(mAccountIdentifier) + Sink::Query::CapabilityFilter(Sink::ApplicationDomain::ResourceCapabilities::Mail::transport)) | ||
190 | .then<void, Sink::ApplicationDomain::SinkResource>([this](const Sink::ApplicationDomain::SinkResource &resource) { | ||
191 | mMailtransportIdentifier = resource.identifier(); | ||
192 | mSmtpServer = resource.getProperty("server").toString(); | ||
193 | mSmtpUsername = resource.getProperty("username").toString(); | ||
194 | mSmtpPassword = resource.getProperty("password").toString(); | ||
195 | emit smtpResourceChanged(); | ||
196 | }, | ||
197 | [](int errorCode, const QString &errorMessage) { | ||
198 | qWarning() << "Failed to find the smtp resource: " << errorMessage; | ||
199 | }).exec(); | ||
200 | } | ||
201 | |||
202 | void AccountSettings::loadIdentity() | ||
203 | { | ||
204 | //FIXME this assumes that we only ever have one identity per account | ||
205 | Sink::Store::fetchOne<Sink::ApplicationDomain::Identity>(Sink::Query::AccountFilter(mAccountIdentifier)) | ||
206 | .then<void, Sink::ApplicationDomain::Identity>([this](const Sink::ApplicationDomain::Identity &identity) { | ||
207 | mIdentityIdentifier = identity.identifier(); | ||
208 | mUsername = identity.getProperty("username").toString(); | ||
209 | mEmailAddress = identity.getProperty("address").toString(); | ||
210 | emit identityChanged(); | ||
211 | }, | ||
212 | [](int errorCode, const QString &errorMessage) { | ||
213 | qWarning() << "Failed to find the identity resource: " << errorMessage; | ||
214 | }).exec(); | ||
215 | } | ||
216 | |||
217 | |||
218 | |||
219 | template<typename ResourceType> | ||
220 | static QByteArray saveResource(const QByteArray &accountIdentifier, const QByteArray &identifier, const std::map<QByteArray, QVariant> &properties) | ||
221 | { | ||
222 | if (!identifier.isEmpty()) { | ||
223 | Sink::ApplicationDomain::SinkResource resource(identifier); | ||
224 | for (const auto &pair : properties) { | ||
225 | resource.setProperty(pair.first, pair.second); | ||
226 | } | ||
227 | Sink::Store::modify(resource).then<void>([](){}, [](int errorCode, const QString &errorMessage) { | ||
228 | qWarning() << "Error while modifying resource: " << errorMessage; | ||
229 | }) | ||
230 | .exec(); | ||
231 | } else { | ||
232 | auto resource = ResourceType::create(accountIdentifier); | ||
233 | auto newIdentifier = resource.identifier(); | ||
234 | for (const auto &pair : properties) { | ||
235 | resource.setProperty(pair.first, pair.second); | ||
236 | } | ||
237 | Sink::Store::create(resource).template then<void>([]() {}, | ||
238 | [](int errorCode, const QString &errorMessage) { | ||
239 | qWarning() << "Error while creating resource: " << errorMessage; | ||
240 | }) | ||
241 | .exec(); | ||
242 | return newIdentifier; | ||
243 | } | ||
244 | return identifier; | ||
245 | } | ||
246 | |||
247 | void AccountSettings::saveImapResource() | ||
248 | { | ||
249 | mImapIdentifier = saveResource<Sink::ApplicationDomain::ImapResource>(mAccountIdentifier, mImapIdentifier, { | ||
250 | {"server", mImapServer}, | ||
251 | {"username", mImapUsername}, | ||
252 | {"password", mImapPassword}, | ||
253 | }); | ||
254 | } | ||
255 | |||
256 | void AccountSettings::saveMaildirResource() | ||
257 | { | ||
258 | mMaildirIdentifier = saveResource<Sink::ApplicationDomain::MaildirResource>(mAccountIdentifier, mMaildirIdentifier, { | ||
259 | {"path", mPath}, | ||
260 | }); | ||
261 | } | ||
262 | |||
263 | void AccountSettings::saveMailtransportResource() | ||
264 | { | ||
265 | mMailtransportIdentifier = saveResource<Sink::ApplicationDomain::MailtransportResource>(mAccountIdentifier, mMailtransportIdentifier, { | ||
266 | {"server", mSmtpServer}, | ||
267 | {"username", mSmtpUsername}, | ||
268 | {"password", mSmtpPassword}, | ||
269 | }); | ||
270 | } | ||
271 | |||
272 | void AccountSettings::saveIdentity() | ||
273 | { | ||
274 | if (!mIdentityIdentifier.isEmpty()) { | ||
275 | Sink::ApplicationDomain::Identity identity(mMailtransportIdentifier); | ||
276 | identity.setProperty("username", mUsername); | ||
277 | identity.setProperty("address", mEmailAddress); | ||
278 | Sink::Store::modify(identity).then<void>([](){}, [](int errorCode, const QString &errorMessage) { | ||
279 | qWarning() << "Error while modifying identity: " << errorMessage; | ||
280 | }) | ||
281 | .exec(); | ||
282 | } else { | ||
283 | auto identity = Sink::ApplicationDomain::ApplicationDomainType::createEntity<Sink::ApplicationDomain::Identity>(); | ||
284 | mIdentityIdentifier = identity.identifier(); | ||
285 | identity.setProperty("account", mAccountIdentifier); | ||
286 | identity.setProperty("username", mUsername); | ||
287 | identity.setProperty("address", mEmailAddress); | ||
288 | Sink::Store::create(identity).then<void>([]() {}, | ||
289 | [](int errorCode, const QString &errorMessage) { | ||
290 | qWarning() << "Error while creating identity: " << errorMessage; | ||
291 | }) | ||
292 | .exec(); | ||
293 | } | ||
294 | } | ||
295 | |||
296 | void AccountSettings::removeResource(const QByteArray &identifier) | ||
297 | { | ||
298 | if (identifier.isEmpty()) { | ||
299 | qWarning() << "We're missing an identifier"; | ||
300 | } else { | ||
301 | Sink::ApplicationDomain::SinkResource resource("", identifier, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); | ||
302 | Sink::Store::remove(resource).template then<void>([]() {}, | ||
303 | [](int errorCode, const QString &errorMessage) { | ||
304 | qWarning() << "Error while removing resource: " << errorMessage; | ||
305 | }) | ||
306 | .exec(); | ||
307 | } | ||
308 | } | ||
309 | |||
310 | void AccountSettings::removeAccount() | ||
311 | { | ||
312 | if (mAccountIdentifier.isEmpty()) { | ||
313 | qWarning() << "We're missing an identifier"; | ||
314 | } else { | ||
315 | Sink::ApplicationDomain::SinkAccount account("", mAccountIdentifier, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); | ||
316 | Sink::Store::remove(account).then<void>([]() {}, | ||
317 | [](int errorCode, const QString &errorMessage) { | ||
318 | qWarning() << "Error while removing account: " << errorMessage; | ||
319 | }) | ||
320 | .exec(); | ||
321 | } | ||
322 | } | ||
323 | |||
324 | void AccountSettings::removeIdentity() | ||
325 | { | ||
326 | if (mIdentityIdentifier.isEmpty()) { | ||
327 | qWarning() << "We're missing an identifier"; | ||
328 | } else { | ||
329 | Sink::ApplicationDomain::Identity identity("", mIdentityIdentifier, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); | ||
330 | Sink::Store::remove(identity).then<void>([]() {}, | ||
331 | [](int errorCode, const QString &errorMessage) { | ||
332 | qWarning() << "Error while removing identity: " << errorMessage; | ||
333 | }) | ||
334 | .exec(); | ||
335 | } | ||
336 | } | ||
337 | |||