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 /accounts | |
parent | 824ddd0fd4e3333c7c2afec83929c5b4795c16b7 (diff) | |
download | kube-80859db2fb6746441668efc851c500695aaf4d58.tar.gz kube-80859db2fb6746441668efc851c500695aaf4d58.zip |
Share the settings implementation
Diffstat (limited to 'accounts')
-rw-r--r-- | accounts/imap/CMakeLists.txt | 4 | ||||
-rw-r--r-- | accounts/imap/imapsettings.cpp | 237 | ||||
-rw-r--r-- | accounts/imap/imapsettings.h | 51 | ||||
-rw-r--r-- | accounts/maildir/CMakeLists.txt | 4 | ||||
-rw-r--r-- | accounts/maildir/maildirsettings.cpp | 247 | ||||
-rw-r--r-- | accounts/maildir/maildirsettings.h | 51 |
6 files changed, 42 insertions, 552 deletions
diff --git a/accounts/imap/CMakeLists.txt b/accounts/imap/CMakeLists.txt index 07c53701..42b50899 100644 --- a/accounts/imap/CMakeLists.txt +++ b/accounts/imap/CMakeLists.txt | |||
@@ -35,11 +35,11 @@ set(SRCS | |||
35 | 35 | ||
36 | add_library(imapaccountplugin SHARED ${SRCS}) | 36 | add_library(imapaccountplugin SHARED ${SRCS}) |
37 | qt5_use_modules(imapaccountplugin Core Quick Qml) | 37 | qt5_use_modules(imapaccountplugin Core Quick Qml) |
38 | target_link_libraries(imapaccountplugin sink settingsplugin) | 38 | target_link_libraries(imapaccountplugin sink settingsplugin mailplugin) |
39 | 39 | ||
40 | add_library(imapaccount_static STATIC ${SRCS}) | 40 | add_library(imapaccount_static STATIC ${SRCS}) |
41 | qt5_use_modules(imapaccount_static Core Quick Qml) | 41 | qt5_use_modules(imapaccount_static Core Quick Qml) |
42 | target_link_libraries(imapaccount_static sink settingsplugin) | 42 | target_link_libraries(imapaccount_static sink settingsplugin mailplugin) |
43 | add_subdirectory(tests) | 43 | add_subdirectory(tests) |
44 | 44 | ||
45 | kpackage_install_package(package org.kube.accounts.imap "genericqml") | 45 | kpackage_install_package(package org.kube.accounts.imap "genericqml") |
diff --git a/accounts/imap/imapsettings.cpp b/accounts/imap/imapsettings.cpp index fcef8971..1f338e83 100644 --- a/accounts/imap/imapsettings.cpp +++ b/accounts/imap/imapsettings.cpp | |||
@@ -18,241 +18,32 @@ | |||
18 | */ | 18 | */ |
19 | #include "imapsettings.h" | 19 | #include "imapsettings.h" |
20 | 20 | ||
21 | #include <settings/settings.h> | ||
22 | |||
23 | #include <sink/store.h> | ||
24 | #include <QDebug> | ||
25 | #include <QDir> | ||
26 | #include <QUrl> | ||
27 | |||
28 | ImapSettings::ImapSettings(QObject *parent) | 21 | ImapSettings::ImapSettings(QObject *parent) |
29 | : QObject(parent) | 22 | : AccountSettings(parent) |
30 | { | ||
31 | } | ||
32 | |||
33 | |||
34 | void 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(Sink::ApplicationDomain::ResourceCapabilities::Mail::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(Sink::ApplicationDomain::ResourceCapabilities::Mail::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 | |||
99 | QByteArray ImapSettings::accountIdentifier() const | ||
100 | { | ||
101 | return mAccountIdentifier; | ||
102 | } | ||
103 | |||
104 | QValidator *ImapSettings::imapServerValidator() const | ||
105 | { | 23 | { |
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 | } | 24 | } |
122 | 25 | ||
123 | QValidator *ImapSettings::smtpServerValidator() const | 26 | void ImapSettings::load() |
124 | { | 27 | { |
125 | class SmtpServerValidator : public QValidator { | 28 | loadAccount(); |
126 | State validate(QString &input, int &pos) const { | 29 | loadImapResource(); |
127 | Q_UNUSED(pos); | 30 | loadMailtransportResource(); |
128 | // smtps://mainserver.example.net:475 | 31 | loadIdentity(); |
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 | } | 32 | } |
141 | 33 | ||
142 | void ImapSettings::save() | 34 | void ImapSettings::save() |
143 | { | 35 | { |
144 | qDebug() << "Saving account " << mAccountIdentifier << mIdentifier << mMailtransportIdentifier; | 36 | saveAccount(); |
145 | Q_ASSERT(!mAccountIdentifier.isEmpty()); | 37 | saveImapResource(); |
146 | Sink::ApplicationDomain::SinkAccount account(mAccountIdentifier); | 38 | saveMailtransportResource(); |
147 | account.setProperty("type", "imap"); | 39 | saveIdentity(); |
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 | } | 40 | } |
222 | 41 | ||
223 | void ImapSettings::remove() | 42 | void ImapSettings::remove() |
224 | { | 43 | { |
225 | if (mMailtransportIdentifier.isEmpty()) { | 44 | removeResource(mMailtransportIdentifier); |
226 | qWarning() << "We're missing an identifier"; | 45 | removeResource(mImapIdentifier); |
227 | } else { | 46 | removeIdentity(); |
228 | Sink::ApplicationDomain::SinkResource mailTransportResource("", mMailtransportIdentifier, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); | 47 | removeAccount(); |
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 | |||
236 | if (mIdentifier.isEmpty()) { | ||
237 | qWarning() << "We're missing an identifier"; | ||
238 | } else { | ||
239 | Sink::ApplicationDomain::SinkResource resource("", mIdentifier, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); | ||
240 | Sink::Store::remove(resource).then<void>([]() {}, | ||
241 | [](int errorCode, const QString &errorMessage) { | ||
242 | qWarning() << "Error while removing resource: " << errorMessage; | ||
243 | }) | ||
244 | .exec(); | ||
245 | } | ||
246 | |||
247 | if (mAccountIdentifier.isEmpty()) { | ||
248 | qWarning() << "We're missing an identifier"; | ||
249 | } else { | ||
250 | Sink::ApplicationDomain::SinkAccount account("", mAccountIdentifier, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); | ||
251 | Sink::Store::remove(account).then<void>([]() {}, | ||
252 | [](int errorCode, const QString &errorMessage) { | ||
253 | qWarning() << "Error while removing account: " << errorMessage; | ||
254 | }) | ||
255 | .exec(); | ||
256 | } | ||
257 | } | 48 | } |
258 | 49 | ||
diff --git a/accounts/imap/imapsettings.h b/accounts/imap/imapsettings.h index 30bd67d1..ca2fd1ca 100644 --- a/accounts/imap/imapsettings.h +++ b/accounts/imap/imapsettings.h | |||
@@ -18,57 +18,16 @@ | |||
18 | */ | 18 | */ |
19 | #pragma once | 19 | #pragma once |
20 | 20 | ||
21 | #include <QObject> | 21 | #include <domain/settings/accountsettings.h> |
22 | #include <QValidator> | ||
23 | 22 | ||
24 | class ImapSettings : public QObject | 23 | class ImapSettings : public AccountSettings |
25 | { | 24 | { |
26 | Q_OBJECT | 25 | 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 | Q_PROPERTY(QString userName MEMBER mUsername NOTIFY identityChanged) | ||
31 | Q_PROPERTY(QString emailAddress MEMBER mEmailAddress NOTIFY identityChanged) | ||
32 | Q_PROPERTY(QString imapServer MEMBER mImapServer NOTIFY imapResourceChanged) | ||
33 | Q_PROPERTY(QValidator* imapServerValidator READ imapServerValidator CONSTANT) | ||
34 | Q_PROPERTY(QString imapUsername MEMBER mImapUsername NOTIFY imapResourceChanged) | ||
35 | Q_PROPERTY(QString imapPassword MEMBER mImapPassword NOTIFY imapResourceChanged) | ||
36 | Q_PROPERTY(QString smtpServer MEMBER mSmtpServer NOTIFY smtpResourceChanged) | ||
37 | Q_PROPERTY(QValidator* smtpServerValidator READ smtpServerValidator CONSTANT) | ||
38 | Q_PROPERTY(QString smtpUsername MEMBER mSmtpUsername NOTIFY smtpResourceChanged) | ||
39 | Q_PROPERTY(QString smtpPassword MEMBER mSmtpPassword NOTIFY smtpResourceChanged) | ||
40 | 26 | ||
41 | public: | 27 | public: |
42 | ImapSettings(QObject *parent = 0); | 28 | ImapSettings(QObject *parent = 0); |
43 | 29 | ||
44 | void setAccountIdentifier(const QByteArray &); | 30 | Q_INVOKABLE virtual void load() Q_DECL_OVERRIDE; |
45 | QByteArray accountIdentifier() const; | 31 | Q_INVOKABLE virtual void save() Q_DECL_OVERRIDE; |
46 | 32 | Q_INVOKABLE virtual void remove() Q_DECL_OVERRIDE; | |
47 | QValidator *imapServerValidator() const; | ||
48 | QValidator *smtpServerValidator() const; | ||
49 | |||
50 | Q_INVOKABLE void save(); | ||
51 | Q_INVOKABLE void remove(); | ||
52 | |||
53 | signals: | ||
54 | void imapResourceChanged(); | ||
55 | void smtpResourceChanged(); | ||
56 | void identityChanged(); | ||
57 | void changed(); | ||
58 | |||
59 | private: | ||
60 | QByteArray mIdentifier; | ||
61 | QByteArray mAccountIdentifier; | ||
62 | QByteArray mMailtransportIdentifier; | ||
63 | QByteArray mIdentityIdentifier; | ||
64 | QString mIcon; | ||
65 | QString mName; | ||
66 | QString mUsername; | ||
67 | QString mEmailAddress; | ||
68 | QString mImapServer; | ||
69 | QString mImapUsername; | ||
70 | QString mImapPassword; | ||
71 | QString mSmtpServer; | ||
72 | QString mSmtpUsername; | ||
73 | QString mSmtpPassword; | ||
74 | }; | 33 | }; |
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 | ||
36 | add_library(maildiraccountplugin SHARED ${SRCS}) | 36 | add_library(maildiraccountplugin SHARED ${SRCS}) |
37 | qt5_use_modules(maildiraccountplugin Core Quick Qml) | 37 | qt5_use_modules(maildiraccountplugin Core Quick Qml) |
38 | target_link_libraries(maildiraccountplugin sink settingsplugin) | 38 | target_link_libraries(maildiraccountplugin sink settingsplugin mailplugin) |
39 | 39 | ||
40 | add_library(maildiraccount_static STATIC ${SRCS}) | 40 | add_library(maildiraccount_static STATIC ${SRCS}) |
41 | qt5_use_modules(maildiraccount_static Core Quick Qml) | 41 | qt5_use_modules(maildiraccount_static Core Quick Qml) |
42 | target_link_libraries(maildiraccount_static sink settingsplugin) | 42 | target_link_libraries(maildiraccount_static sink settingsplugin mailplugin) |
43 | add_subdirectory(tests) | 43 | add_subdirectory(tests) |
44 | 44 | ||
45 | kpackage_install_package(package org.kube.accounts.maildir "genericqml") | 45 | kpackage_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 | |||
28 | MaildirSettings::MaildirSettings(QObject *parent) | 21 | MaildirSettings::MaildirSettings(QObject *parent) |
29 | : QObject(parent) | 22 | : AccountSettings(parent) |
30 | { | ||
31 | } | ||
32 | |||
33 | |||
34 | void 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 | |||
98 | QByteArray MaildirSettings::accountIdentifier() const | ||
99 | { | 23 | { |
100 | return mAccountIdentifier; | ||
101 | } | 24 | } |
102 | 25 | ||
103 | void MaildirSettings::setPath(const QUrl &path) | 26 | void 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 | |||
112 | QUrl MaildirSettings::path() const | ||
113 | { | ||
114 | return QUrl(mPath); | ||
115 | } | ||
116 | |||
117 | QValidator *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 | |||
133 | QValidator *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 | ||
152 | void MaildirSettings::save() | 34 | void 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 | ||
233 | void MaildirSettings::remove() | 42 | void 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 | ||
24 | class MaildirSettings : public QObject | 23 | class 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 | |||
39 | public: | 26 | public: |
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 | |||
54 | signals: | ||
55 | void pathChanged(); | ||
56 | void smtpResourceChanged(); | ||
57 | void identityChanged(); | ||
58 | void changed(); | ||
59 | |||
60 | private: | ||
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 | }; |