summaryrefslogtreecommitdiffstats
path: root/accounts/maildir
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 /accounts/maildir
parent824ddd0fd4e3333c7c2afec83929c5b4795c16b7 (diff)
downloadkube-80859db2fb6746441668efc851c500695aaf4d58.tar.gz
kube-80859db2fb6746441668efc851c500695aaf4d58.zip
Share the settings implementation
Diffstat (limited to 'accounts/maildir')
-rw-r--r--accounts/maildir/CMakeLists.txt4
-rw-r--r--accounts/maildir/maildirsettings.cpp247
-rw-r--r--accounts/maildir/maildirsettings.h51
3 files changed, 21 insertions, 281 deletions
diff --git a/accounts/maildir/CMakeLists.txt b/accounts/maildir/CMakeLists.txt
index a48295d5..e50d4179 100644
--- a/accounts/maildir/CMakeLists.txt
+++ b/accounts/maildir/CMakeLists.txt
@@ -35,11 +35,11 @@ set(SRCS
35 35
36add_library(maildiraccountplugin SHARED ${SRCS}) 36add_library(maildiraccountplugin SHARED ${SRCS})
37qt5_use_modules(maildiraccountplugin Core Quick Qml) 37qt5_use_modules(maildiraccountplugin Core Quick Qml)
38target_link_libraries(maildiraccountplugin sink settingsplugin) 38target_link_libraries(maildiraccountplugin sink settingsplugin mailplugin)
39 39
40add_library(maildiraccount_static STATIC ${SRCS}) 40add_library(maildiraccount_static STATIC ${SRCS})
41qt5_use_modules(maildiraccount_static Core Quick Qml) 41qt5_use_modules(maildiraccount_static Core Quick Qml)
42target_link_libraries(maildiraccount_static sink settingsplugin) 42target_link_libraries(maildiraccount_static sink settingsplugin mailplugin)
43add_subdirectory(tests) 43add_subdirectory(tests)
44 44
45kpackage_install_package(package org.kube.accounts.maildir "genericqml") 45kpackage_install_package(package org.kube.accounts.maildir "genericqml")
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
diff --git a/accounts/maildir/maildirsettings.h b/accounts/maildir/maildirsettings.h
index a02944d9..42336535 100644
--- a/accounts/maildir/maildirsettings.h
+++ b/accounts/maildir/maildirsettings.h
@@ -18,56 +18,15 @@
18*/ 18*/
19#pragma once 19#pragma once
20 20
21#include <QObject> 21#include <domain/settings/accountsettings.h>
22#include <QValidator>
23 22
24class MaildirSettings : public QObject 23class MaildirSettings : public AccountSettings
25{ 24{
26 Q_OBJECT 25 Q_OBJECT
27 Q_PROPERTY(QByteArray accountIdentifier READ accountIdentifier WRITE setAccountIdentifier)
28 Q_PROPERTY(QUrl path READ path WRITE setPath NOTIFY pathChanged)
29 Q_PROPERTY(QValidator* pathValidator READ pathValidator CONSTANT)
30 Q_PROPERTY(QString icon MEMBER mIcon NOTIFY changed)
31 Q_PROPERTY(QString accountName MEMBER mName NOTIFY changed)
32 Q_PROPERTY(QString userName MEMBER mUsername NOTIFY identityChanged)
33 Q_PROPERTY(QString emailAddress MEMBER mEmailAddress NOTIFY identityChanged)
34 Q_PROPERTY(QString smtpServer MEMBER mSmtpServer NOTIFY smtpResourceChanged)
35 Q_PROPERTY(QValidator* smtpServerValidator READ smtpServerValidator CONSTANT)
36 Q_PROPERTY(QString smtpUsername MEMBER mSmtpUsername NOTIFY smtpResourceChanged)
37 Q_PROPERTY(QString smtpPassword MEMBER mSmtpPassword NOTIFY smtpResourceChanged)
38
39public: 26public:
40 MaildirSettings(QObject *parent = 0); 27 MaildirSettings(QObject *parent = 0);
41 28
42 void setAccountIdentifier(const QByteArray &); 29 Q_INVOKABLE virtual void load() Q_DECL_OVERRIDE;
43 QByteArray accountIdentifier() const; 30 Q_INVOKABLE virtual void save() Q_DECL_OVERRIDE;
44 31 Q_INVOKABLE virtual void remove() Q_DECL_OVERRIDE;
45 void setPath(const QUrl &);
46 QUrl path() const;
47 QValidator *pathValidator() const;
48
49 QValidator *smtpServerValidator() const;
50
51 Q_INVOKABLE void save();
52 Q_INVOKABLE void remove();
53
54signals:
55 void pathChanged();
56 void smtpResourceChanged();
57 void identityChanged();
58 void changed();
59
60private:
61 QByteArray mIdentifier;
62 QByteArray mAccountIdentifier;
63 QByteArray mMailtransportIdentifier;
64 QByteArray mIdentityIdentifier;
65 QString mPath;
66 QString mIcon;
67 QString mName;
68 QString mUsername;
69 QString mEmailAddress;
70 QString mSmtpServer;
71 QString mSmtpUsername;
72 QString mSmtpPassword;
73}; 32};