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/imap/imapsettings.cpp | |
parent | 824ddd0fd4e3333c7c2afec83929c5b4795c16b7 (diff) | |
download | kube-80859db2fb6746441668efc851c500695aaf4d58.tar.gz kube-80859db2fb6746441668efc851c500695aaf4d58.zip |
Share the settings implementation
Diffstat (limited to 'accounts/imap/imapsettings.cpp')
-rw-r--r-- | accounts/imap/imapsettings.cpp | 237 |
1 files changed, 14 insertions, 223 deletions
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 | ||