Debian Codegraph @

Dekko/backend/mail/Folder.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 "Folder.h"
19 #include <QDebug>
20 #include <qmailstore.h>
21 #include <QDBusPendingReply>
22 #include <QDBusPendingCallWatcher>
23 #include "MailServiceClient.h"
24 #include "serviceutils.h"
25
26 Folder::Folder(QObject *parent) : QObject(parent),
27 m_type(StandardFolder), m_unreadCount(0)
28 {
29 connect(QMailStore::instance(), &QMailStore::folderContentsModified, this, &Folder::handleContentsModified);
30 connect(this, &Folder::countChanged, this, &Folder::updateUnreadCount);
31 }
32 Folder::Folder(QObject *parent, const QMailFolderId &id, const QMailMessageKey &key, const int &type):
33 QObject(parent),
34 m_folder(id), m_key(key), m_type((FolderType)type), m_unreadCount(0)
35 {
36 connect(QMailStore::instance(), &QMailStore::folderContentsModified, this, &Folder::handleContentsModified);
37 connect(this, &Folder::countChanged, this, &Folder::updateUnreadCount);
38 if (m_folder.id().isValid()) {
39 updateUnreadCount();
40 }
41 }
42
43 Folder::Folder(QObject *parent, const QMailAccountId &accountId, const QMailFolderId &id, const QMailMessageKey &key, const int &type):
44 QObject(parent),
45 m_account(accountId), m_folder(id), m_key(key), m_type((FolderType)type), m_unreadCount(0)
46 {
47 connect(QMailStore::instance(), &QMailStore::folderContentsModified, this, &Folder::handleContentsModified);
48 connect(this, &Folder::countChanged, this, &Folder::updateUnreadCount);
49 if (m_folder.id().isValid()) {
50 updateUnreadCount();
51 }
52 }
53
54 int Folder::folderId() const
55 {
56 return m_folder.id().toULongLong();
57 }
58
59 QVariant Folder::messageKey() const
60 {
61 return m_key;
62 }
63
64 void Folder::setMessageKey(const QMailMessageKey &key)
65 {
66 if (key == m_key) {
67 return;
68 }
69 m_key = key;
70 if (m_folder.id().isValid()) {
71 emit folderChanged();
72 }
73 }
74
75 Folder::FolderType Folder::type() const
76 {
77 return m_type;
78 }
79
80 void Folder::setFolderType(const Folder::FolderType type)
81 {
82 if (type == m_type) {
83 return;
84 }
85 m_type = type;
86 if (m_folder.id().isValid()) {
87 emit folderChanged();
88 }
89 }
90
91 QString Folder::name()
92 {
93 if (!m_folder.id().isValid()) {
94 return QString();
95 }
96 if (m_folder.id() == QMailFolder::LocalStorageFolderId
97 || m_type == SpecialUseInboxFolder /* Just to get the lowercased string */
98 ) {
99 switch (m_type) {
100 case StandardFolder:
101 return m_folder.displayName();
102 case SpecialUseDraftsFolder:
103 return tr("Drafts");
104 case SpecialUseInboxFolder:
105 return tr("Inbox");
106 case SpecialUseJunkFolder:
107 return tr("Spam");
108 case SpecialUseOutboxFolder:
109 return tr("Outbox");
110 case SpecialUseSentFolder:
111 return tr("Sent");
112 case SpecialUseTrashFolder:
113 return tr("Trash");
114 default:
115 return m_folder.displayName();
116 }
117 } else {
118 return m_folder.displayName();
119 }
120 }
121
122 QString Folder::path()
123 {
124 return m_folder.path();
125 }
126
127 int Folder::unreadCount()
128 {
129 return m_unreadCount;
130 }
131
132 int Folder::totalCount() const
133 {
134 return m_folder.serverCount();
135 }
136
137 bool Folder::syncEnabled() const
138 {
139 return (m_folder.status() & QMailFolder::SynchronizationEnabled);
140 }
141
142 bool Folder::synced() const
143 {
144 return (m_folder.status() & QMailFolder::Synchronized);
145 }
146
147 bool Folder::partialContent() const
148 {
149 return (m_folder.status() & QMailFolder::PartialContent);
150 }
151
152 bool Folder::canCreateChildren() const
153 {
154 return (m_folder.status() & QMailFolder::ChildCreationPermitted);
155 }
156
157 bool Folder::canRename() const
158 {
159 return (m_folder.status() & QMailFolder::RenamePermitted);
160 }
161
162 bool Folder::canDelete() const
163 {
164 return (m_folder.status() &QMailFolder::DeletionPermitted);
165 }
166
167 bool Folder::readOnly() const
168 {
169 return (m_folder.status() & QMailFolder::ReadOnly);
170 }
171
172 bool Folder::isFavourite() const
173 {
174 return (m_folder.status() & QMailFolder::Favourite);
175 }
176
177 int Folder::nestingDepth()
178 {
179 if (m_type != StandardFolder) {
180 // All special use folders are in the top level
181 return 0;
182 } else {
183 QMailFolder f = m_folder;
184 int depth = 0;
185 while (f.parentFolderId().isValid()) {
186 f = QMailFolder(f.parentFolderId());
187 ++depth;
188 }
189 return depth;
190 }
191 }
192
193 Folder::FolderType Folder::folderTypeFromId(const QMailFolderId &id)
194 {
195 QMailFolder folder(id);
196 if (!folder.parentAccountId().isValid() || id == QMailFolder::LocalStorageFolderId) {
197 // Local folder
198 return StandardFolder;
199 }
200 QMailAccount account(folder.parentAccountId());
201
202 if (account.standardFolders().values().contains(id)) {
203 QMailFolder::StandardFolder standardFolder = account.standardFolders().key(id);
204 switch (standardFolder) {
205 case QMailFolder::InboxFolder:
206 return SpecialUseInboxFolder;
207 case QMailFolder::OutboxFolder:
208 return SpecialUseOutboxFolder;
209 case QMailFolder::DraftsFolder:
210 return SpecialUseDraftsFolder;
211 case QMailFolder::SentFolder:
212 return SpecialUseSentFolder;
213 case QMailFolder::TrashFolder:
214 return SpecialUseTrashFolder;
215 case QMailFolder::JunkFolder:
216 return SpecialUseJunkFolder;
217 default:
218 return StandardFolder;
219 }
220 }
221 return StandardFolder;
222 }
223
224 QMailFolder::StandardFolder Folder::folderFromType(const Folder::FolderType &type)
225 {
226 QMailFolder::StandardFolder sf = QMailFolder::InboxFolder;
227 switch (type) {
228 case Folder::StandardFolder:
229 break;
230 case Folder::SpecialUseInboxFolder:
231 sf = QMailFolder::InboxFolder;
232 break;
233 case Folder::SpecialUseOutboxFolder:
234 sf = QMailFolder::OutboxFolder;
235 break;
236 case Folder::SpecialUseDraftsFolder:
237 sf = QMailFolder::DraftsFolder;
238 break;
239 case Folder::SpecialUseSentFolder:
240 sf = QMailFolder::SentFolder;
241 break;
242 case Folder::SpecialUseTrashFolder:
243 sf = QMailFolder::TrashFolder;
244 break;
245 case Folder::SpecialUseJunkFolder:
246 sf = QMailFolder::JunkFolder;
247 break;
248 }
249 return sf;
250 }
251
252 void Folder::setFolderId(const int &id)
253 {
254 QMailFolderId folderId(id);
255 if (folderId.isValid()) {
256 m_folder.setId(folderId);
257 updateUnreadCount();
258 emit folderChanged();
259 }
260 }
261
262 void Folder::setIsFavourite(const bool &favourite)
263 {
264 m_folder.setStatus(QMailFolder::Favourite, favourite);
265 QMailStore::instance()->updateFolder(&m_folder);
266 emit folderChanged();
267 }
268
269 void Folder::handleContentsModified(const QMailFolderIdList &idList)
270 {
271 if (idList.contains(m_folder.id())) {
272 emit countChanged();
273 }
274 }
275
276 void Folder::handleUnreadCount(QDBusPendingCallWatcher *call)
277 {
278 QDBusPendingReply<int> reply = *call;
279 if (reply.isError()) {
280 qDebug() << "[Folder::handleUnreadCount] >> Reply error";
281 return;
282 }
283 m_unreadCount = (int)reply.value();
284 qDebug() << "{Folder::handleUnreadCount} >> GOT UNREAD COUNT";
285 call->deleteLater();
286 emit unreadCountChanged();
287 }
288
289 void Folder::updateUnreadCount()
290 {
291 QMailMessageKey unreadKey;
292 switch(m_type) {
293 case StandardFolder:
294 case SpecialUseInboxFolder:
295 {
296 // These actually live on the server so we don't need to keep this local
297 // Now we could just call m_folder.serverUnreadCount() here but we
298 // could be offline and not exported any local changes in a while so our
299 // unread count might actually be less than server until the next sync happens.
300 // So let's just lean on the mailstore for now.
301 unreadKey = QMailMessageKey::parentFolderId(m_folder.id()) & QMailMessageKey::status(QMailMessage::Read, QMailDataComparator::Excludes);
302 break;
303 }
304 case SpecialUseOutboxFolder:
305 case SpecialUseDraftsFolder:
306 {
307 // Typically for these two the total folder count is always shown
308 // Mail waiting to be sent or is still a draft doesn't have a concept of unread vs read
309 QMailMessageKey tmpKey;
310 // If this Folder belongs to an account then we want to filter out any messages
311 //belonging to other accounts. Since these local folders are shared.
312 if (m_account.isValid() && m_folder.id() == QMailFolder::LocalStorageFolderId) {
313 tmpKey = QMailMessageKey::parentAccountId(m_account);
314 }
315 unreadKey = QMailMessageKey::parentFolderId(m_folder.id()) & m_key;
316 break;
317 }
318 case SpecialUseJunkFolder:
319 case SpecialUseTrashFolder:
320 {
321 // Typically for these two the total folder count is always shown
322 // Mail waiting to be sent or is still a draft doesn't have a concept of unread vs read
323 QMailMessageKey tmpKey;
324 // If this Folder belongs to an account then we want to filter out any messages
325 //belonging to other accounts. Since these local folders are shared.
326 if (m_account.isValid() && m_folder.id() == QMailFolder::LocalStorageFolderId) {
327 tmpKey = QMailMessageKey::parentAccountId(m_account);
328 }
329 unreadKey = tmpKey & // limit to this account
330 QMailMessageKey::parentFolderId(m_folder.id()) & // for folder
331 m_key & // with key
332 QMailMessageKey::status(QMailMessage::Read, QMailDataComparator::Excludes);
333 break;
334 }
335 case SpecialUseSentFolder:
336 {
337 // There shouldn't be an unread count for this.
338 return;
339 }
340 }
341
342 QDBusPendingReply<int> reply = Client::instance()->bus()->totalCount(msg_key_bytes(unreadKey));
343
344 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
345
346 connect(watcher, &QDBusPendingCallWatcher::finished, this, &Folder::handleUnreadCount);
347 }
348