summaryrefslogtreecommitdiffstats
path: root/framework/domain
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-07-16 10:02:36 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-07-16 10:19:34 +0200
commit80859db2fb6746441668efc851c500695aaf4d58 (patch)
treecb6f1dfbc3c1876d9edb6e3744102fa0029b23ce /framework/domain
parent824ddd0fd4e3333c7c2afec83929c5b4795c16b7 (diff)
downloadkube-80859db2fb6746441668efc851c500695aaf4d58.tar.gz
kube-80859db2fb6746441668efc851c500695aaf4d58.zip
Share the settings implementation
Diffstat (limited to 'framework/domain')
-rw-r--r--framework/domain/CMakeLists.txt1
-rw-r--r--framework/domain/settings/accountsettings.cpp337
-rw-r--r--framework/domain/settings/accountsettings.h110
3 files changed, 448 insertions, 0 deletions
diff --git a/framework/domain/CMakeLists.txt b/framework/domain/CMakeLists.txt
index 114b054d..39a47111 100644
--- a/framework/domain/CMakeLists.txt
+++ b/framework/domain/CMakeLists.txt
@@ -14,6 +14,7 @@ set(mailplugin_SRCS
14 accountscontroller.cpp 14 accountscontroller.cpp
15 accountsmodel.cpp 15 accountsmodel.cpp
16 identitiesmodel.cpp 16 identitiesmodel.cpp
17 settings/accountsettings.cpp
17) 18)
18add_definitions(-DMAIL_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data") 19add_definitions(-DMAIL_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data")
19 20
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
26AccountSettings::AccountSettings(QObject *parent)
27 : QObject(parent)
28{
29}
30
31
32void 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
56QByteArray AccountSettings::accountIdentifier() const
57{
58 return mAccountIdentifier;
59}
60
61void AccountSettings::setPath(const QUrl &path)
62{
63 auto normalizedPath = path.path();
64 if (mPath != normalizedPath) {
65 mPath = normalizedPath;
66 emit pathChanged();
67 }
68}
69
70QUrl AccountSettings::path() const
71{
72 return QUrl(mPath);
73}
74
75QValidator *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
91QValidator *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
110QValidator *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
129void 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
145void 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
156void 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
171void 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
187void 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
202void 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
219template<typename ResourceType>
220static 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
247void AccountSettings::saveImapResource()
248{
249 mImapIdentifier = saveResource<Sink::ApplicationDomain::ImapResource>(mAccountIdentifier, mImapIdentifier, {
250 {"server", mImapServer},
251 {"username", mImapUsername},
252 {"password", mImapPassword},
253 });
254}
255
256void AccountSettings::saveMaildirResource()
257{
258 mMaildirIdentifier = saveResource<Sink::ApplicationDomain::MaildirResource>(mAccountIdentifier, mMaildirIdentifier, {
259 {"path", mPath},
260 });
261}
262
263void AccountSettings::saveMailtransportResource()
264{
265 mMailtransportIdentifier = saveResource<Sink::ApplicationDomain::MailtransportResource>(mAccountIdentifier, mMailtransportIdentifier, {
266 {"server", mSmtpServer},
267 {"username", mSmtpUsername},
268 {"password", mSmtpPassword},
269 });
270}
271
272void 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
296void 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
310void 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
324void 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
diff --git a/framework/domain/settings/accountsettings.h b/framework/domain/settings/accountsettings.h
new file mode 100644
index 00000000..08215a32
--- /dev/null
+++ b/framework/domain/settings/accountsettings.h
@@ -0,0 +1,110 @@
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#pragma once
20
21#include <QObject>
22#include <QValidator>
23
24class AccountSettings : public QObject
25{
26 Q_OBJECT
27 Q_PROPERTY(QByteArray accountIdentifier READ accountIdentifier WRITE setAccountIdentifier)
28 Q_PROPERTY(QString icon MEMBER mIcon NOTIFY changed)
29 Q_PROPERTY(QString accountName MEMBER mName NOTIFY changed)
30
31 Q_PROPERTY(QString userName MEMBER mUsername NOTIFY identityChanged)
32 Q_PROPERTY(QString emailAddress MEMBER mEmailAddress NOTIFY identityChanged)
33
34 Q_PROPERTY(QString imapServer MEMBER mImapServer NOTIFY imapResourceChanged)
35 Q_PROPERTY(QValidator* imapServerValidator READ imapServerValidator CONSTANT)
36 Q_PROPERTY(QString imapUsername MEMBER mImapUsername NOTIFY imapResourceChanged)
37 Q_PROPERTY(QString imapPassword MEMBER mImapPassword NOTIFY imapResourceChanged)
38
39 Q_PROPERTY(QString smtpServer MEMBER mSmtpServer NOTIFY smtpResourceChanged)
40 Q_PROPERTY(QValidator* smtpServerValidator READ smtpServerValidator CONSTANT)
41 Q_PROPERTY(QString smtpUsername MEMBER mSmtpUsername NOTIFY smtpResourceChanged)
42 Q_PROPERTY(QString smtpPassword MEMBER mSmtpPassword NOTIFY smtpResourceChanged)
43
44 Q_PROPERTY(QUrl path READ path WRITE setPath NOTIFY pathChanged)
45 Q_PROPERTY(QValidator* pathValidator READ pathValidator CONSTANT)
46
47public:
48 AccountSettings(QObject *parent = 0);
49
50 void setAccountIdentifier(const QByteArray &);
51 QByteArray accountIdentifier() const;
52
53 void setPath(const QUrl &);
54 QUrl path() const;
55
56 virtual QValidator *imapServerValidator() const;
57 virtual QValidator *smtpServerValidator() const;
58 virtual QValidator *pathValidator() const;
59
60 Q_INVOKABLE virtual void load() = 0;
61 Q_INVOKABLE virtual void save() = 0;
62 Q_INVOKABLE virtual void remove() = 0;
63
64signals:
65 void imapResourceChanged();
66 void smtpResourceChanged();
67 void identityChanged();
68 void pathChanged();
69 void changed();
70
71protected:
72 void saveAccount();
73 void saveImapResource();
74 void saveMaildirResource();
75 void saveMailtransportResource();
76 void saveIdentity();
77
78 void loadAccount();
79 void loadImapResource();
80 void loadMaildirResource();
81 void loadMailtransportResource();
82 void loadIdentity();
83
84 void removeAccount();
85 void removeResource(const QByteArray &identifier);
86
87 void removeIdentity();
88
89 QByteArray mAccountIdentifier;
90 QString mIcon;
91 QString mName;
92
93 QByteArray mImapIdentifier;
94 QString mImapServer;
95 QString mImapUsername;
96 QString mImapPassword;
97
98 QByteArray mMaildirIdentifier;
99 QString mPath;
100
101 QByteArray mMailtransportIdentifier;
102 QString mSmtpServer;
103 QString mSmtpUsername;
104 QString mSmtpPassword;
105
106 QByteArray mIdentityIdentifier;
107 QString mUsername;
108 QString mEmailAddress;
109};
110