Debian Codegraph @

Dekko/backend/accounts/Account.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 "Account.h"
19 #include <QDebug>
20 #include <qmailfolder.h>
21 #include <qmailmessage.h>
22 #include <qmailserviceconfiguration.h>
23 #include <qmailstore.h>
24
25 Q_LOGGING_CATEGORY(D_ACCOUNT, "dekko.accounts.account")
26
27 const QString Account::imapServiceType = QStringLiteral("imap4");
28 const QString Account::popServiceType = QStringLiteral("pop3");
29 const QString Account::qmfStorage = QStringLiteral("qmfstoragemanager");
30 const QString Account::smtpServiceType = QStringLiteral("smtp");
31
32 Account::Account(QObject *parent) : QObject(parent),
33 m_account(new QMailAccount()),
34 m_accountConfig(new QMailAccountConfiguration()),
35 m_incoming(Q_NULLPTR),
36 m_outgoing(Q_NULLPTR)
37 {
38 // connect(QMailStore::instance(), SIGNAL(accountsUpdated(QMailAccountIdList)), this, SLOT(reload(QMailAccountIdList)));
39 m_account->setMessageType(QMailMessage::Email);
40 initialize();
41 }
42
43 void Account::setSpecialUseFolder(Account::SpecialUseFolder folder, const quint64 &folderId)
44 {
45 QMailFolderId fId(folderId);
46 if (fId.isValid()) {
47 m_account->setStandardFolder((QMailFolder::StandardFolder)folder, fId);
48 } else {
49 qCDebug(D_ACCOUNT) << __func__ << "Invalid folder id";
50 emit error(Error::InvalidFolderId, id());
51 }
52 }
53
54 void Account::setSpecialUseFolder(Account::SpecialUseFolder folder, const QString &path)
55 {
56 const QMailFolderIdList folders = QMailStore::instance()->queryFolders(
57 QMailFolderKey::path(path) & QMailFolderKey::parentAccountId(m_account->id())
58 );
59 if (path.isEmpty() || folders.count() == 0) {
60 m_account->setStandardFolder((QMailFolder::StandardFolder)folder, QMailFolderId());
61 return;
62 }
63 if (folders.count() != 1) {
64 return;
65 }
66 setSpecialUseFolder(folder, folders.first().toULongLong());
67 }
68
69 QString Account::specialUseFolderPath(Account::SpecialUseFolder folder)
70 {
71 QMailFolderId folderId = m_account->standardFolder((QMailFolder::StandardFolder)folder);
72 return folderId.isValid() ? QMailFolder(folderId).path() : QString();
73 }
74
75 void Account::setId(const int &id)
76 {
77 QMailAccountId aId(id);
78 if (aId.isValid()) {
79 m_account = new QMailAccount(aId);
80 m_accountConfig = new QMailAccountConfiguration(m_account->id());
81 initialize(); // TODO: is this needed?
82 emit accountChanged(aId.toULongLong());
83 } else {
84 qCDebug(D_ACCOUNT) << __func__ << "Account id: '" << aId.toULongLong() << "' is not valid";
85 emit error(Error::InvalidAccountId, id);
86 }
87 }
88
89 void Account::setName(const QString &name)
90 {
91 if (!name.isEmpty()) {
92 m_account->setName(name);
93 } else {
94 qCDebug(D_ACCOUNT) << __func__ << "Empty name";
95 emit error(Error::EmptyName, id());
96 }
97 }
98
99 bool Account::save()
100 {
101 bool result;
102 m_account->setStatus(QMailAccount::UserEditable, true);
103 m_account->setStatus(QMailAccount::UserRemovable, true);
104 m_account->setStatus(QMailAccount::MessageSource, true);
105 m_account->setStatus(QMailAccount::CanRetrieve, true);
106 m_account->setStatus(QMailAccount::MessageSink, true);
107 m_account->setStatus(QMailAccount::CanTransmit, true);
108 m_account->setStatus(QMailAccount::Enabled, true);
109 m_account->setStatus(QMailAccount::CanCreateFolders, false);
110 m_account->setFromAddress(QMailAddress(m_outgoing->name(), m_outgoing->email()));
111 if (m_account->id().isValid()) {
112 qCDebug(D_ACCOUNT) << __func__ << "Updating account settings for: " << m_account->id();
113 result = QMailStore::instance()->updateAccount(m_account, m_accountConfig);
114 } else {
115 qCDebug(D_ACCOUNT) << __func__ << "Creating new account";
116 result = QMailStore::instance()->addAccount(m_account, m_accountConfig);
117 }
118 if (result) {
119 qCDebug(D_ACCOUNT) << __func__ << "Success";
120 } else {
121 qCDebug(D_ACCOUNT) << __func__ << "Oops that didn't work,' something bad happened";
122 }
123 return result;
124 }
125
126 QString Account::customValue(const QString &ns, const QString &key)
127 {
128 return m_account->customField(QString("%1.%2").arg(ns, key));
129 }
130
131 void Account::setCustomValue(const QString &ns, const QString &key, const QString &value)
132 {
133 m_account->setCustomField(QString("%1.%2").arg(ns, key), value);
134 QMailStore::instance()->updateAccount(m_account);
135 }
136
137 void Account::initialize()
138 {
139 QStringList accountServices = m_accountConfig->services();
140 // Need to check the qmfstoragemanager is an available service
141 if (!accountServices.contains(qmfStorage)) {
142 // qCDebug(D_ACCOUNT) << __func__ << "qmfstoragemanager not in services, adding it now";
143 m_accountConfig->addServiceConfiguration(qmfStorage);
144 QMailServiceConfiguration qmfStorageConfig(m_accountConfig, qmfStorage);
145 qmfStorageConfig.setType(QMailServiceConfiguration::Storage);
146 qmfStorageConfig.setVersion(101);
147 qmfStorageConfig.setValue(QStringLiteral("basePath"), QStringLiteral(""));
148 }
149 if (!accountServices.contains(QStringLiteral("smtp"))) {
150 // qCDebug(D_ACCOUNT) << __func__ << "smtp not in services, adding now";
151 m_accountConfig->addServiceConfiguration(smtpServiceType);
152 }
153 QString recvType;
154 if (accountServices.contains(imapServiceType)) {
155 recvType = imapServiceType;
156 } else if (accountServices.contains(popServiceType)) {
157 recvType = popServiceType;
158 } else {
159 // default to imap
160 recvType = imapServiceType;
161 m_accountConfig->addServiceConfiguration(imapServiceType);
162 }
163
164 if (recvType == imapServiceType) {
165 m_incoming = new ImapAccountConfiguration(this, m_accountConfig, recvType);
166 } else {
167 m_incoming = new PopAccountConfiguration(this, m_accountConfig, recvType);
168 }
169 m_outgoing = new SmtpAccountConfiguration(this, m_accountConfig, smtpServiceType);
170 }
171
172 void Account::reload(const QMailAccountIdList &ids)
173 {
174 if (!ids.contains(m_account->id())) {
175 return;
176 }
177 qCDebug(D_ACCOUNT) << __func__ << "Reloading account";
178 delete m_incoming;
179 m_incoming = 0;
180 delete m_outgoing;
181 m_outgoing = 0;
182 delete m_accountConfig;
183 m_accountConfig = 0;
184 m_accountConfig = new QMailAccountConfiguration();
185 QMailAccountId id(m_account->id());
186 delete m_account;
187 m_account = 0;
188 setId(id.toULongLong());
189 emit accountChanged(m_account->id().toULongLong());
190 m_incoming->emitConfigurationChanges();
191 m_outgoing->emitConfigurationChanges();
192 }
193
194 NewAccount::NewAccount(QObject *parent) : Account(parent)
195 {
196 }
197
198 void NewAccount::setSourceType(const int &srcType)
199 {
200 m_type = (AccountConfiguration::ServiceType)srcType;
201 delete m_accountConfig;
202 m_accountConfig = 0;
203 m_accountConfig = new QMailAccountConfiguration();
204 m_account->setStatus(QMailAccount::UserEditable, true);
205 m_account->setStatus(QMailAccount::UserRemovable, true);
206 QStringList accountServices = m_accountConfig->services();
207 if (!accountServices.contains(qmfStorage)) {
208 m_accountConfig->addServiceConfiguration(qmfStorage);
209 QMailServiceConfiguration qmfStorageConfig(m_accountConfig, qmfStorage);
210 qmfStorageConfig.setType(QMailServiceConfiguration::Storage);
211 qmfStorageConfig.setVersion(101);
212 qmfStorageConfig.setValue(QStringLiteral("basePath"), QStringLiteral(""));
213 }
214 if (m_type == AccountConfiguration::IMAP) {
215 qCDebug(D_ACCOUNT) << "Create new imap configuration";
216 if (!accountServices.contains(imapServiceType)) {
217 qCDebug(D_ACCOUNT) << "Adding IMAP service configuration";
218 m_accountConfig->addServiceConfiguration(imapServiceType);
219 }
220 m_incoming = new ImapAccountConfiguration(this, m_accountConfig, imapServiceType);
221 } else if (m_type == AccountConfiguration::POP3) {
222 qCDebug(D_ACCOUNT) << "Create new pop configuration";
223 if (!accountServices.contains(popServiceType)) {
224 qCDebug(D_ACCOUNT) << "Adding pop service configuration";
225 m_accountConfig->addServiceConfiguration(popServiceType);
226 }
227 m_incoming = new PopAccountConfiguration(this, m_accountConfig, popServiceType);
228 }
229 if (!accountServices.contains(QStringLiteral("smtp"))) {
230 qCDebug(D_ACCOUNT) << __func__ << "smtp not in services, adding now";
231 m_accountConfig->addServiceConfiguration(smtpServiceType);
232 }
233 m_outgoing = new SmtpAccountConfiguration(this, m_accountConfig, smtpServiceType);
234 }