Debian Codegraph @

Dekko/backend/accounts/AccountConfiguration.cpp

raw symbols SCIP
1 /* Copyright (C) 2016 - 2017 Dan Chapman <[email protected]>
2
3 This file is part of Dekko email client for Ubuntu devices
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License or (at your option) version 3
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "AccountConfiguration.h"
19 #include <QStringList>
20
21 Q_LOGGING_CATEGORY(D_ACCOUNT_CONF, "dekko.accounts.config")
22
23 //TODO: Move to seperate file??
24 const QString AccountKeys::name = QStringLiteral("username");
25 const QString AccountKeys::email = QStringLiteral("address");
26 const QString AccountKeys::password = QStringLiteral("password");
27 const QString AccountKeys::smtppassword = QStringLiteral("smtppassword");
28 const QString AccountKeys::server = QStringLiteral("server");
29 const QString AccountKeys::port = QStringLiteral("port");
30 const QString AccountKeys::encryption = QStringLiteral("encryption");
31 const QString AccountKeys::acceptUntrustedCert = QStringLiteral("acceptUntrustedCertificates");
32 const QString AccountKeys::canDeleteMail = QStringLiteral("canDelete");
33 const QString AccountKeys::downloadAttachments = QStringLiteral("downloadAttachments");
34 const QString AccountKeys::autoDownload = QStringLiteral("autoDownload");
35 const QString AccountKeys::maxMailSize = QStringLiteral("maxSize");
36 const QString AccountKeys::prefferedSubType = QStringLiteral("textSubtype");
37 const QString AccountKeys::idleEnabled = QStringLiteral("pushEnabled");
38 const QString AccountKeys::canIDLE = QStringLiteral("pushCapable");
39 const QString AccountKeys::baseFolder = QStringLiteral("baseFolder");
40 const QString AccountKeys::pushFolders = QStringLiteral("pushFolders");
41 const QString AccountKeys::checkInterval = QStringLiteral("checkInterval");
42 const QString AccountKeys::checkWhenRoaming = QStringLiteral("intervalCheckRoamingEnabled");
43 const QString AccountKeys::capabilities = QStringLiteral("capabilities");
44 const QString AccountKeys::timeTillLogout = QStringLiteral("timeTillLogout");
45 const QString AccountKeys::searchLimit = QStringLiteral("searchLimit");
46 const QString AccountKeys::saslMechanism = QStringLiteral("authentication");
47 const QString AccountKeys::username = QStringLiteral("smtpusername");
48 const QString AccountKeys::authFromCaps = QStringLiteral("authFromCapabilities");
49 const QString AccountKeys::signature = QStringLiteral("signature");
50
51 namespace {
52 QString defaultPort(AccountConfiguration::ServiceType service, AccountConfiguration::EncryptionMethod method) {
53 switch(service) {
54 case AccountConfiguration::IMAP:
55 switch(method) {
56 case AccountConfiguration::None:
57 case AccountConfiguration::STARTTLS:
58 return QStringLiteral("143");
59 case AccountConfiguration::SSL:
60 return QStringLiteral("993");
61 }
62 case AccountConfiguration::POP3:
63 switch(method) {
64 case AccountConfiguration::None:
65 case AccountConfiguration::STARTTLS:
66 return QStringLiteral("110");
67 case AccountConfiguration::SSL:
68 return QStringLiteral("995");
69 }
70 case AccountConfiguration::SMTP:
71 switch(method) {
72 case AccountConfiguration::None:
73 case AccountConfiguration::STARTTLS:
74 return QStringLiteral("587");
75 case AccountConfiguration::SSL:
76 return QStringLiteral("465");
77 }
78 }
79 Q_UNREACHABLE();
80 }
81 }
82
83 AccountConfiguration::AccountConfiguration(QObject *parent) : QObject(parent), m_config(0), m_service(0)
84 {
85
86 }
87
88 AccountConfiguration::AccountConfiguration(
89 QObject *parent,
90 QMailAccountConfiguration *config,
91 const QString &serviceType) : QObject(parent),
92 m_config(0), m_service(0)
93 {
94 init(config, serviceType);
95 }
96
97 AccountConfiguration::~AccountConfiguration()
98 {
99 delete m_service;
100 }
101
102 QString AccountConfiguration::name() const {
103 return m_service->value(AccountKeys::name);
104 }
105
106 QString AccountConfiguration::email() const
107 {
108 return m_service->value(AccountKeys::email);
109 }
110
111 QString AccountConfiguration::password() const
112 {
113 if (serviceType() == SMTP) {
114 return PasswordHelper::decodePassword(m_service->value(AccountKeys::smtppassword));
115 } else {
116 return PasswordHelper::decodePassword(m_service->value(AccountKeys::password));
117 }
118 }
119
120 QString AccountConfiguration::server() const
121 {
122 return m_service->value(AccountKeys::server);
123 }
124
125 QString AccountConfiguration::port() const
126 {
127 return m_service->value(AccountKeys::port, defaultPort(serviceType(), encryption()));
128 }
129
130 AccountConfiguration::EncryptionMethod AccountConfiguration::encryption() const
131 {
132 return (EncryptionMethod)m_service->value(AccountKeys::encryption,
133 QString::number(EncryptionMethod::SSL)).toInt();
134 }
135
136 AccountConfiguration::ServiceType AccountConfiguration::serviceType() const
137 {
138 if (m_serviceType == QStringLiteral("smtp")) {
139 return SMTP;
140 } else if (m_serviceType == QStringLiteral("pop3")) {
141 return POP3;
142 } else {
143 return IMAP;
144 }
145 }
146
147 bool AccountConfiguration::acceptUntrustedCertificates() const
148 {
149 return (m_service->value(AccountKeys::acceptUntrustedCert, "0").toInt() != 0);
150 }
151
152 QString AccountConfiguration::customValue(const QString &ns, const QString &key)
153 {
154 return m_service->value(QString("%1.%2").arg(ns, key));
155 }
156
157 void AccountConfiguration::setCustomValue(const QString &ns, const QString &key, const QString &value)
158 {
159 const QString nskey = QString("%1.%2").arg(ns, key);
160 m_service->setValue(nskey, value);
161 emit customValueChanged(nskey, value);
162 }
163
164 void AccountConfiguration::setName(const QString &name)
165 {
166 m_service->setValue(AccountKeys::name, name);
167 emit nameChanged(name);
168 }
169
170 void AccountConfiguration::setEmail(const QString &email)
171 {
172 m_service->setValue(AccountKeys::email, email);
173 emit emailChanged(email);
174 }
175
176 void AccountConfiguration::setPassword(const QString &pwd)
177 {
178 if (serviceType() == SMTP) {
179 m_service->setValue(AccountKeys::smtppassword, PasswordHelper::encodePassword(pwd));
180 } else {
181 m_service->setValue(AccountKeys::password, PasswordHelper::encodePassword(pwd));
182 }
183 emit passwordChanged();
184 }
185
186 void AccountConfiguration::setServer(const QString &server)
187 {
188 m_service->setValue(AccountKeys::server, server);
189 emit serverChanged(server);
190 }
191
192 void AccountConfiguration::setPort(const QString &port)
193 {
194 m_service->setValue(AccountKeys::port, port);
195 emit portChanged(port);
196 }
197
198 void AccountConfiguration::setEncryption(AccountConfiguration::EncryptionMethod method)
199 {
200 m_service->setValue(AccountKeys::encryption, QString::number(method));
201 emit encryptionChanged();
202 }
203
204 void AccountConfiguration::setAcceptUntrustedCertificates(const bool accept)
205 {
206 m_service->setValue(AccountKeys::acceptUntrustedCert, QString::number(accept ? 1 : 0));
207 emit acceptUntrustedCertChanged();
208 }
209
210 void AccountConfiguration::init(QMailAccountConfiguration *config, const QString &serviceType) {
211 m_config = config;
212 m_serviceType = serviceType;
213 m_service = new QMailServiceConfiguration(m_config, m_serviceType);
214 m_service->setType((m_serviceType == QStringLiteral("smtp")) ?
215 QMailServiceConfiguration::Sink :
216 QMailServiceConfiguration::Source);
217 m_service->setVersion(100);
218 }
219
220 void AccountConfiguration::emitConfigurationChanges(){
221 emit nameChanged(name());
222 emit emailChanged(email());
223 emit serverChanged(server());
224 emit portChanged(port());
225 emit encryptionChanged();
226 emit passwordChanged();
227 emit acceptUntrustedCertChanged();
228 }
229
230 ImapAccountConfiguration::ImapAccountConfiguration(QObject *parent) : AccountConfiguration(parent){}
231
232 AccountConfiguration::SaslMechanism ImapAccountConfiguration::saslMechanism() const
233 {
234 return (AccountConfiguration::SaslMechanism)m_service->value(AccountKeys::saslMechanism, "0").toInt();
235 }
236
237 bool ImapAccountConfiguration::canDeleteMail() const
238 {
239 return (m_service->value(AccountKeys::canDeleteMail, "1").toInt() != 0);
240 }
241
242 bool ImapAccountConfiguration::downloadAttachments() const
243 {
244 return m_service->value(AccountKeys::downloadAttachments, "0").toInt() != 0;
245 }
246
247 bool ImapAccountConfiguration::isAutoDownload() const
248 {
249 return m_service->value(AccountKeys::autoDownload, "0").toInt() != 0;
250 }
251
252 int ImapAccountConfiguration::maxMailSize() const
253 {
254 return m_service->value(AccountKeys::maxMailSize, QStringLiteral("100")).toInt();
255 }
256
257 ImapAccountConfiguration::SubType ImapAccountConfiguration::preferredSubType() const
258 {
259 if (m_service->value(AccountKeys::prefferedSubType, QStringLiteral("html")) == QStringLiteral("html"))
260 return SubType::Html;
261 else
262 return SubType::Plain;
263 }
264
265 bool ImapAccountConfiguration::canIDLE() const
266 {
267 return m_service->value(AccountKeys::canIDLE, QStringLiteral("0")).toInt() != 0;
268 }
269
270 bool ImapAccountConfiguration::idleEnabled() const
271 {
272 return m_service->value(AccountKeys::idleEnabled, QStringLiteral("1")).toInt() != 0;
273 }
274
275 QString ImapAccountConfiguration::baseFolder() const
276 {
277 return m_service->value(AccountKeys::baseFolder, QStringLiteral(""));
278 }
279
280 QStringList ImapAccountConfiguration::pushFolders() const
281 {
282 return m_service->value(AccountKeys::pushFolders).split(QChar('\x0A'), QString::SkipEmptyParts);
283 }
284
285 int ImapAccountConfiguration::checkInterval() const
286 {
287 return m_service->value(AccountKeys::checkInterval, QStringLiteral("5")).toInt();
288 }
289
290 bool ImapAccountConfiguration::checkWhenRoaming() const
291 {
292 return m_service->value(AccountKeys::checkWhenRoaming, QStringLiteral("0")).toInt() != 0;
293 }
294
295 QStringList ImapAccountConfiguration::capabilities() const
296 {
297 return m_service->value(AccountKeys::capabilities).split(QChar(' '), QString::SkipEmptyParts);
298 }
299
300 int ImapAccountConfiguration::timeTillLogout() const
301 {
302 const int tenSeconds(10 * 1000);
303 QString t(m_service->value(AccountKeys::timeTillLogout, QString::number(tenSeconds)));
304
305 bool ok;
306 int val(t.toInt(&ok));
307 if (!ok) {
308 qCWarning(D_ACCOUNT_CONF) << "Could not parse timeTillLogout";
309 return tenSeconds;
310 } else {
311 return val;
312 }
313 }
314
315 int ImapAccountConfiguration::searchLimit() const
316 {
317 const int defaultLimit = 100;
318 QString t(m_service->value(AccountKeys::searchLimit, QString::number(defaultLimit)));
319 bool ok;
320 int val(t.toInt(&ok));
321 if (!ok) {
322 qCWarning(D_ACCOUNT_CONF) << "Could not parse searchLimit";
323 return 0;
324 } else {
325 return val;
326 }
327 }
328
329 // setters
330
331 void ImapAccountConfiguration::setSaslMechanism(AccountConfiguration::SaslMechanism sasl)
332 {
333 m_service->setValue(AccountKeys::saslMechanism, QString::number(sasl));
334 emit saslChanged();
335 }
336
337 void ImapAccountConfiguration::setDeleteMail(const bool canDelete)
338 {
339 m_service->setValue(AccountKeys::canDeleteMail, QString::number(canDelete ? 1 : 0));
340 }
341
342 void ImapAccountConfiguration::setDownloadAttachments(const bool download)
343 {
344 m_service->setValue(AccountKeys::downloadAttachments, QString::number(download ? 1 : 0));
345 emit downloadAttachmentsChanged();
346 }
347
348 void ImapAccountConfiguration::setAutoDownload(const bool autoDownload)
349 {
350 m_service->setValue(AccountKeys::autoDownload, QString::number(autoDownload ? 1 : 0));
351 emit autoDownloadChanged();
352 }
353
354 void ImapAccountConfiguration::setMaxMailSize(const int &size)
355 {
356 m_service->setValue(AccountKeys::maxMailSize, QString::number(size));
357 emit maxMailSizeChanged();
358 }
359
360 void ImapAccountConfiguration::setPreferredSubType(ImapAccountConfiguration::SubType subType)
361 {
362 m_service->setValue(AccountKeys::prefferedSubType, QString((subType == SubType::Html) ? "html" : "plain"));
363 emit preferredSubTypeChanged();
364 }
365
366 void ImapAccountConfiguration::setIdleEnabled(const bool enabled)
367 {
368 m_service->setValue(AccountKeys::idleEnabled, QString::number(enabled ? 1 : 0));
369 emit idleEnabledChanged();
370 }
371
372 void ImapAccountConfiguration::setBaseFolder(const QString &baseFolder)
373 {
374 m_service->setValue(AccountKeys::baseFolder, baseFolder);
375 emit baseFolderChanged();
376 }
377
378 void ImapAccountConfiguration::setPushFolders(const QStringList &pushFolders)
379 {
380 m_service->setValue(AccountKeys::pushFolders, QString("") + pushFolders.join(QChar('\x0A')));
381 emit pushFolderChanged();
382 }
383
384 void ImapAccountConfiguration::setCheckInterval(const int &interval)
385 {
386 m_service->setValue(AccountKeys::checkInterval, QString::number(interval));
387 emit checkIntervalChanged();
388 }
389
390 void ImapAccountConfiguration::setCheckWhenRoaming(const bool check)
391 {
392 m_service->setValue(AccountKeys::checkWhenRoaming, QString::number(check ? 1 : 0));
393 emit checkWhenRoamingChanged();
394 }
395
396 void ImapAccountConfiguration::setSearchLimit(const int &limit)
397 {
398 m_service->setValue(AccountKeys::searchLimit, QString::number(limit));
399 emit searchLimitChanged();
400 }
401
402 void ImapAccountConfiguration::emitConfigurationChanges() {
403 emit saslChanged();
404 emit canDeleteMailChanged();
405 emit downloadAttachmentsChanged();
406 emit autoDownloadChanged();
407 emit maxMailSizeChanged();
408 emit preferredSubTypeChanged();
409 emit idleEnabledChanged();
410 emit baseFolderChanged();
411 emit pushFolderChanged();
412 emit checkIntervalChanged();
413 emit checkWhenRoamingChanged();
414 emit searchLimitChanged();
415 AccountConfiguration::emitConfigurationChanges();
416 }
417
418 SmtpAccountConfiguration::SmtpAccountConfiguration(QObject *parent) : AccountConfiguration(parent){}
419
420 AccountConfiguration::SaslMechanism SmtpAccountConfiguration::saslMechanism() const
421 {
422 return (AccountConfiguration::SaslMechanism)m_service->value(AccountKeys::saslMechanism, "0").toInt();
423 }
424
425 QString SmtpAccountConfiguration::username() const
426 {
427 return m_service->value(AccountKeys::username);
428 }
429
430 bool SmtpAccountConfiguration::authFromCaps() const
431 {
432 return m_service->value(AccountKeys::authFromCaps, QStringLiteral("0")).toInt() != 0;
433 }
434
435 QString SmtpAccountConfiguration::initials()
436 {
437 QString name = this->name();
438 if (name.isEmpty() || !name.at(0).isLetter()) {
439 // Name is empty so return first character of address. :-/
440 return QString(email().at(0).toUpper());
441 }
442 // Intitials string
443 QString initials;
444 // Now break up the name, we have to set the encoding here as QT_NO_CAST_FROM/TO_ASCII is set
445 QStringList parts = name.split(QStringLiteral(" "));
446 if (parts.first().at(0).isLetter()) {
447 initials += parts.first().at(0).toUpper();
448 }
449 if (parts.size() > 1) {
450 if (parts.last().at(0).isLetter()) {
451 initials += parts.last().at(0).toUpper();
452 }
453 }
454 return initials;
455 }
456
457 QString SmtpAccountConfiguration::signature() const
458 {
459 static const QString defaultSig = QStringLiteral("-- \n%1").arg(tr("Sent with Dekko"));
460 return m_service->value(AccountKeys::signature, defaultSig);
461 }
462
463 void SmtpAccountConfiguration::setSaslMechanism(AccountConfiguration::SaslMechanism sasl)
464 {
465 m_service->setValue(AccountKeys::saslMechanism, QString::number(sasl));
466 emit saslChanged();
467 }
468
469 void SmtpAccountConfiguration::setUsername(const QString &username)
470 {
471 m_service->setValue(AccountKeys::username, username);
472 emit usernameChanged();
473 }
474
475 void SmtpAccountConfiguration::setAuthFromCaps(const bool use)
476 {
477 m_service->setValue(AccountKeys::authFromCaps, QString::number(use ? 1 : 0));
478 emit authFromCapsChanged();
479 }
480
481 void SmtpAccountConfiguration::setSignature(const QString &sig)
482 {
483 m_service->setValue(AccountKeys::signature, sig);
484 emit signatureChanged();
485 }
486
487 void SmtpAccountConfiguration::emitConfigurationChanges() {
488 emit saslChanged();
489 emit usernameChanged();
490 emit authFromCapsChanged();
491 emit signatureChanged();
492 AccountConfiguration::emitConfigurationChanges();
493 }
494
495 PopAccountConfiguration::PopAccountConfiguration(QObject *parent) : AccountConfiguration(parent){}
496
497 bool PopAccountConfiguration::canDeleteMail() const
498 {
499 return m_service->value(AccountKeys::canDeleteMail, QStringLiteral("1")).toInt() != 0;
500 }
501
502 bool PopAccountConfiguration::isAutoDownload() const
503 {
504 return m_service->value(AccountKeys::autoDownload, QStringLiteral("0")).toInt() != 0;
505 }
506
507 int PopAccountConfiguration::maxMailSize() const
508 {
509 return m_service->value(AccountKeys::maxMailSize, QStringLiteral("100")).toInt();
510 }
511
512 int PopAccountConfiguration::checkInterval() const
513 {
514 return m_service->value(AccountKeys::checkInterval, QStringLiteral("-1")).toInt();
515 }
516
517 bool PopAccountConfiguration::checkWhenRoaming() const
518 {
519 return m_service->value(AccountKeys::checkWhenRoaming, QStringLiteral("0")).toInt() != 0;
520 }
521
522 void PopAccountConfiguration::setDeleteMail(const bool setDelete)
523 {
524 m_service->setValue(AccountKeys::canDeleteMail, QString::number(setDelete ? 1 : 0));
525 emit canDeleteMailChanged();
526 }
527
528 void PopAccountConfiguration::setAutoDownload(const bool autoDownload)
529 {
530 m_service->setValue(AccountKeys::autoDownload, QString::number(autoDownload ? 1 : 0));
531 emit autoDownloadChanged();
532 }
533
534 void PopAccountConfiguration::setMaxMailSize(const int &maxSize)
535 {
536 m_service->setValue(AccountKeys::maxMailSize, QString::number(maxSize));
537 emit maxMailSizeChanged();
538 }
539
540 void PopAccountConfiguration::setCheckInterval(const int &interval)
541 {
542 m_service->setValue(AccountKeys::checkInterval, QString::number(interval));
543 emit checkIntervalChanged();
544 }
545
546 void PopAccountConfiguration::setCheckWhenRoaming(const bool check)
547 {
548 m_service->setValue(AccountKeys::checkWhenRoaming, QString::number(check ? 1 : 0));
549 emit checkWhenRoamingChanged();
550 }
551
552 void PopAccountConfiguration::emitConfigurationChanges() {
553 emit canDeleteMailChanged();
554 emit autoDownloadChanged();
555 emit maxMailSizeChanged();
556 emit checkIntervalChanged();
557 emit checkWhenRoamingChanged();
558 AccountConfiguration::emitConfigurationChanges();
559 }