summaryrefslogtreecommitdiffstats
path: root/accounts/imap/imapsettings.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'accounts/imap/imapsettings.cpp')
-rw-r--r--accounts/imap/imapsettings.cpp250
1 files changed, 250 insertions, 0 deletions
diff --git a/accounts/imap/imapsettings.cpp b/accounts/imap/imapsettings.cpp
new file mode 100644
index 00000000..7d93606d
--- /dev/null
+++ b/accounts/imap/imapsettings.cpp
@@ -0,0 +1,250 @@
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 "imapsettings.h"
20
21#include <settings/settings.h>
22
23#include <sink/store.h>
24#include <QDebug>
25#include <QDir>
26#include <QUrl>
27
28ImapSettings::ImapSettings(QObject *parent)
29 : QObject(parent)
30{
31}
32
33
34void ImapSettings::setAccountIdentifier(const QByteArray &id)
35{
36 if (id.isEmpty()) {
37 return;
38 }
39 mAccountIdentifier = id;
40
41 //Clear
42 mIcon = QString();
43 mName = QString();
44 mImapServer = QString();
45 mImapUsername = QString();
46 mImapPassword = QString();
47 mSmtpServer = QString();
48 mSmtpUsername = QString();
49 mSmtpPassword = QString();
50 emit changed();
51 emit imapResourceChanged();
52 emit smtpResourceChanged();
53
54 Q_ASSERT(!id.isEmpty());
55 Sink::Store::fetchOne<Sink::ApplicationDomain::SinkAccount>(Sink::Query::IdentityFilter(id))
56 .then<void, Sink::ApplicationDomain::SinkAccount>([this](const Sink::ApplicationDomain::SinkAccount &account) {
57 mIcon = account.getProperty("icon").toString();
58 mName = account.getProperty("name").toString();
59 emit changed();
60 }).exec();
61
62 Sink::Store::fetchOne<Sink::ApplicationDomain::SinkResource>(Sink::Query::AccountFilter(id) + Sink::Query::CapabilityFilter("storage"))
63 .then<void, Sink::ApplicationDomain::SinkResource>([this](const Sink::ApplicationDomain::SinkResource &resource) {
64 mIdentifier = resource.identifier();
65 mImapServer = resource.getProperty("server").toString();
66 mImapUsername = resource.getProperty("username").toString();
67 mImapPassword = resource.getProperty("password").toString();
68 emit imapResourceChanged();
69 },
70 [](int errorCode, const QString &errorMessage) {
71 qWarning() << "Failed to find the imap resource: " << errorMessage;
72 }).exec();
73
74 Sink::Store::fetchOne<Sink::ApplicationDomain::SinkResource>(Sink::Query::AccountFilter(id) + Sink::Query::CapabilityFilter("transport"))
75 .then<void, Sink::ApplicationDomain::SinkResource>([this](const Sink::ApplicationDomain::SinkResource &resource) {
76 mMailtransportIdentifier = resource.identifier();
77 mSmtpServer = resource.getProperty("server").toString();
78 mSmtpUsername = resource.getProperty("username").toString();
79 mSmtpPassword = resource.getProperty("password").toString();
80 emit smtpResourceChanged();
81 },
82 [](int errorCode, const QString &errorMessage) {
83 qWarning() << "Failed to find the smtp resource: " << errorMessage;
84 }).exec();
85
86 //FIXME this assumes that we only ever have one identity per account
87 Sink::Store::fetchOne<Sink::ApplicationDomain::Identity>(Sink::Query::AccountFilter(id))
88 .then<void, Sink::ApplicationDomain::Identity>([this](const Sink::ApplicationDomain::Identity &identity) {
89 mIdentityIdentifier = identity.identifier();
90 mUsername = identity.getProperty("username").toString();
91 mEmailAddress = identity.getProperty("address").toString();
92 emit identityChanged();
93 },
94 [](int errorCode, const QString &errorMessage) {
95 qWarning() << "Failed to find the identity resource: " << errorMessage;
96 }).exec();
97}
98
99QByteArray ImapSettings::accountIdentifier() const
100{
101 return mAccountIdentifier;
102}
103
104QValidator *ImapSettings::imapServerValidator() const
105{
106 class ImapServerValidator : public QValidator {
107 State validate(QString &input, int &pos) const {
108 Q_UNUSED(pos);
109 // imaps://mainserver.example.net:475
110 const QUrl url(input);
111 static QSet<QString> validProtocols = QSet<QString>() << "imap" << "imaps";
112 if (url.isValid() && validProtocols.contains(url.scheme().toLower())) {
113 return Acceptable;
114 } else {
115 return Intermediate;
116 }
117 }
118 };
119 static ImapServerValidator *validator = new ImapServerValidator;
120 return validator;
121}
122
123QValidator *ImapSettings::smtpServerValidator() const
124{
125 class SmtpServerValidator : public QValidator {
126 State validate(QString &input, int &pos) const {
127 Q_UNUSED(pos);
128 // smtps://mainserver.example.net:475
129 const QUrl url(input);
130 static QSet<QString> validProtocols = QSet<QString>() << "smtp" << "smtps";
131 if (url.isValid() && validProtocols.contains(url.scheme().toLower())) {
132 return Acceptable;
133 } else {
134 return Intermediate;
135 }
136 }
137 };
138 static SmtpServerValidator *validator = new SmtpServerValidator;
139 return validator;
140}
141
142void ImapSettings::save()
143{
144 qDebug() << "Saving account " << mAccountIdentifier << mIdentifier << mMailtransportIdentifier;
145 Q_ASSERT(!mAccountIdentifier.isEmpty());
146 Sink::ApplicationDomain::SinkAccount account(mAccountIdentifier);
147 account.setProperty("type", "imap");
148 account.setProperty("name", mName);
149 account.setProperty("icon", mIcon);
150 Q_ASSERT(!account.identifier().isEmpty());
151 Sink::Store::modify(account).then<void>([]() {},
152 [](int errorCode, const QString &errorMessage) {
153 qWarning() << "Error while creating account: " << errorMessage;
154 })
155 .exec();
156
157 if (!mIdentifier.isEmpty()) {
158 Sink::ApplicationDomain::SinkResource resource(mIdentifier);
159 resource.setProperty("server", mImapServer);
160 resource.setProperty("username", mImapUsername);
161 resource.setProperty("password", mImapPassword);
162 Sink::Store::modify(resource).then<void>([](){}, [](int errorCode, const QString &errorMessage) {
163 qWarning() << "Error while modifying resource: " << errorMessage;
164 })
165 .exec();
166 } else {
167 auto resource = Sink::ApplicationDomain::ImapResource::create(mAccountIdentifier);
168 mIdentifier = resource.identifier();
169 resource.setProperty("server", mImapServer);
170 resource.setProperty("username", mImapUsername);
171 resource.setProperty("password", mImapPassword);
172 Sink::Store::create(resource).then<void>([]() {},
173 [](int errorCode, const QString &errorMessage) {
174 qWarning() << "Error while creating resource: " << errorMessage;
175 })
176 .exec();
177 }
178
179 if (!mMailtransportIdentifier.isEmpty()) {
180 Sink::ApplicationDomain::SinkResource resource(mMailtransportIdentifier);
181 resource.setProperty("server", mSmtpServer);
182 resource.setProperty("username", mSmtpUsername);
183 resource.setProperty("password", mSmtpPassword);
184 Sink::Store::modify(resource).then<void>([](){}, [](int errorCode, const QString &errorMessage) {
185 qWarning() << "Error while modifying resource: " << errorMessage;
186 })
187 .exec();
188 } else {
189 auto resource = Sink::ApplicationDomain::MailtransportResource::create(mAccountIdentifier);
190 mMailtransportIdentifier = resource.identifier();
191 resource.setProperty("server", mSmtpServer);
192 resource.setProperty("username", mSmtpUsername);
193 resource.setProperty("password", mSmtpPassword);
194 Sink::Store::create(resource).then<void>([]() {},
195 [](int errorCode, const QString &errorMessage) {
196 qWarning() << "Error while creating resource: " << errorMessage;
197 })
198 .exec();
199 }
200
201 if (!mIdentityIdentifier.isEmpty()) {
202 Sink::ApplicationDomain::Identity identity(mMailtransportIdentifier);
203 identity.setProperty("username", mUsername);
204 identity.setProperty("address", mEmailAddress);
205 Sink::Store::modify(identity).then<void>([](){}, [](int errorCode, const QString &errorMessage) {
206 qWarning() << "Error while modifying identity: " << errorMessage;
207 })
208 .exec();
209 } else {
210 auto identity = Sink::ApplicationDomain::ApplicationDomainType::createEntity<Sink::ApplicationDomain::Identity>();
211 mIdentityIdentifier = identity.identifier();
212 identity.setProperty("account", mAccountIdentifier);
213 identity.setProperty("username", mUsername);
214 identity.setProperty("address", mEmailAddress);
215 Sink::Store::create(identity).then<void>([]() {},
216 [](int errorCode, const QString &errorMessage) {
217 qWarning() << "Error while creating identity: " << errorMessage;
218 })
219 .exec();
220 }
221}
222
223void ImapSettings::remove()
224{
225 if (mIdentifier.isEmpty()) {
226 qWarning() << "We're missing an identifier";
227 } else {
228 Sink::ApplicationDomain::SinkResource mailTransportResource("", mMailtransportIdentifier, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create());
229 Sink::Store::remove(mailTransportResource).then<void>([]() {},
230 [](int errorCode, const QString &errorMessage) {
231 qWarning() << "Error while removing resource: " << errorMessage;
232 })
233 .exec();
234
235 Sink::ApplicationDomain::SinkResource resource("", mIdentifier, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create());
236 Sink::Store::remove(resource).then<void>([]() {},
237 [](int errorCode, const QString &errorMessage) {
238 qWarning() << "Error while removing resource: " << errorMessage;
239 })
240 .exec();
241
242 Sink::ApplicationDomain::SinkAccount account("", mAccountIdentifier, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create());
243 Sink::Store::remove(account).then<void>([]() {},
244 [](int errorCode, const QString &errorMessage) {
245 qWarning() << "Error while removing account: " << errorMessage;
246 })
247 .exec();
248 }
249}
250