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