Debian Codegraph @

Dekko/backend/mail/MessageList.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 "MessageList.h"
19 #include <algorithm>
20 #include <QDebug>
21 #include <QSet>
22 #include <QDBusPendingReply>
23 #include <QDataStream>
24 #include <qmailstore.h>
25 #include <qmaildisconnected.h>
26 #include <QElapsedTimer>
27 #include <MailServiceClient.h>
28 #include "serviceutils.h"
29
30 Q_LOGGING_CATEGORY(D_MSG_LIST, "dekko.mail.msglist")
31
32 void MessageListWorker::updateMessages(const QMailMessageIdList &idList, const QMailMessageIdList &needsUpdate, const QMailMessageIdList &newIds, const QMap<QMailMessageId, int> &indexMap, const int &limit)
33 {
34 QElapsedTimer timer;
35 qCDebug(D_MSG_LIST) << "[MessageListWorker::updateMessages] >> Starting";
36 timer.start();
37
38 QList<int> insertIndices;
39 QList<int> removeIndices;
40 QList<int> updateIndices;
41 QMap<QMailMessageId, int> newPositions;
42
43 auto indexOf = [=](const QMailMessageId &id) -> int {
44 QMap<QMailMessageId, int>::const_iterator it = indexMap.find(id);
45 if (it != indexMap.end()) {
46 return it.value();
47 }
48 return -1;
49 };
50
51 int index = 0;
52 foreach (const QMailMessageId &id, newIds) {
53 newPositions.insert(id, index);
54 ++index;
55 }
56
57 QMap<int, QMailMessageId> indexId;
58 foreach (const QMailMessageId &id, needsUpdate) {
59 int newIndex = -1;
60 QMap<QMailMessageId, int>::const_iterator it = newPositions.find(id);
61 if (it != newPositions.end()) {
62 newIndex = it.value();
63 }
64
65 int oldIndex(indexOf(id));
66 if (oldIndex == -1) {
67 // This message was not previously in our set - add it
68 if (newIndex != -1) {
69 insertIndices.append(newIndex);
70 indexId.insert(newIndex, id);
71 }
72 } else {
73 // We already had this message
74 if (newIndex == -1) {
75 removeIndices.append(oldIndex);
76 } else {
77 bool reinsert(false);
78
79 // See if this item is still sorted correctly with respect to its neighbours
80 if (newIndex >= idList.count()) {
81 qCWarning(D_MSG_LIST) << "MessageListWorker::updateMessages index out of bounds" << newIndex << idList.count();
82 } else if (newIndex > 0) {
83 if (newIds.indexOf(idList.at(newIndex - 1)) > newIndex) {
84 reinsert = true;
85 }
86 }
87
88 if (newIndex < idList.count() - 1) {
89 if (newIds.indexOf(idList.at(newIndex + 1)) < newIndex) {
90 reinsert = true;
91 }
92 }
93
94 if (reinsert) {
95 removeIndices.append(oldIndex);
96 insertIndices.append(newIndex);
97 indexId.insert(newIndex, id);
98 } else {
99 // This message is updated but has not changed position
100 updateIndices.append(newIndex);
101 }
102 }
103 }
104 }
105 // qCDebug(D_MSG_LIST) << "Remove: " << removeIndices;
106 // Sort the lists to yield ascending order
107 std::sort(removeIndices.begin(), removeIndices.end());
108 for (int i = removeIndices.count(); i > 0; --i) {
109 int index = removeIndices[i - 1];
110 emit removeMessageAt(index);
111 }
112
113 // qCDebug(D_MSG_LIST) << "Insert: " << insertIndices;
114 std::sort(insertIndices.begin(), insertIndices.end());
115 foreach (const int &index, insertIndices) {
116 // Since the list is ordered, if index is bigger than the limit
117 // we stop inserting
118 if(limit && index > limit) {
119 break;
120 }
121 emit insertMessageAt(index, indexId[index]);
122 }
123 // Check if we passed the model limit, if so remove exceeding messages
124 if (limit && idList.count() > limit) {
125 QMailMessageIdList idsToRemove = idList.mid(limit);
126 emit removeMessages(idsToRemove);
127 }
128
129 // qCDebug(D_MSG_LIST) << "Update: " << updateIndices;
130 std::sort(updateIndices.begin(), updateIndices.end());
131 foreach (const int &index, updateIndices) {
132 emit updateMessageAt(index);
133 }
134
135 emit canPossiblyLoadMore();
136 qCDebug(D_MSG_LIST) << "[MessageListWorker::updateMessages] >> Finished in: " << timer.elapsed() << "milliseconds";
137 }
138
139 void MessageListWorker::sortAndAppend(const QMailMessageIdList &idList, const QMailMessageIdList &idsToAppend, const QMailMessageIdList &newIdsList, const QMap<QMailMessageId, int> &indexMap, const int &limit)
140 {
141 QElapsedTimer timer;
142 qCDebug(D_MSG_LIST) << "[MessageListWorker::sortAndAppend] >> Starting";
143 timer.start();
144 QList<int> insertIndices;
145 QMap<QMailMessageId, int> newPositions;
146
147 auto indexOf = [=](const QMailMessageId &id) -> int {
148 QMap<QMailMessageId, int>::const_iterator it = indexMap.find(id);
149 if (it != indexMap.end()) {
150 return it.value();
151 }
152 return -1;
153 };
154
155 int index = 0;
156 foreach (const QMailMessageId &id, newIdsList) {
157 newPositions.insert(id, index);
158 ++index;
159 }
160
161 QMap<int, QMailMessageId> indexId;
162 foreach (const QMailMessageId &id, idsToAppend) {
163 int newIndex = -1;
164 QMap<QMailMessageId, int>::const_iterator it = newPositions.find(id);
165 if (it != newPositions.end()) {
166 newIndex = it.value();
167 }
168
169 int oldIndex(indexOf(id));
170 if (oldIndex == -1) {
171 // This message was not previously in our set - add it
172 if (newIndex != -1) {
173 insertIndices.append(newIndex);
174 indexId.insert(newIndex, id);
175 }
176 }
177 }
178 std::sort(insertIndices.begin(), insertIndices.end());
179 foreach (const int &index, insertIndices) {
180 // Since the list is ordered, if index is bigger than the limit
181 // we stop inserting
182 if(limit && index > limit) {
183 break;
184 }
185 emit insertMessageAt(index, indexId[index]);
186 }
187
188 // Check if we passed the model limit, if so remove exceeding messages
189 if (limit && idList.count() > limit) {
190 QMailMessageIdList idsToRemove = idList.mid(limit);
191 emit removeMessages(idsToRemove);
192 }
193 emit canPossiblyLoadMore();
194 qCDebug(D_MSG_LIST) << "[MessageListWorker::sortAndAppend] >> Finished in: " << timer.elapsed() << "milliseconds";
195
196 }
197
198 MessageList::MessageList(QObject *parent) : QObject(parent),
199 m_model(0), m_initialized(false), m_selectionMode(false), m_currentIndex(-1), m_filter(FilterKey::All), m_disableUpdates(false),
200 m_needsRefresh(false), m_loading(false), m_disableRemovals(false)
201 {
202
203 qRegisterMetaType<QMap<QMailMessageId, int>>("QMap<QMailMessageId, int>");
204
205 MessageListWorker *worker = new MessageListWorker;
206 worker->moveToThread(&m_workerThread);
207 connect(&m_workerThread, &QThread::finished, worker, &QObject::deleteLater);
208 connect(this, &MessageList::sortAndAppendNewMessages, worker, &MessageListWorker::sortAndAppend);
209 connect(this, &MessageList::updateMessages, worker, &MessageListWorker::updateMessages);
210 connect(worker, &MessageListWorker::insertMessageAt, this, &MessageList::insertMessageAt);
211 connect(worker, &MessageListWorker::removeMessageAt, this, &MessageList::removeMessageAt);
212 connect(worker, &MessageListWorker::updateMessageAt, this, &MessageList::updateMessageAt);
213 connect(worker, &MessageListWorker::removeMessages, this, &MessageList::removeMessages);
214 connect(worker, &MessageListWorker::canPossiblyLoadMore, this, &MessageList::canPossiblyLoadMore);
215 m_workerThread.start();
216
217 m_model = new QQmlObjectListModel<MinimalMessage>(this);
218 m_msgKey = QMailMessageKey::nonMatchingKey();
219 m_sortOrder = Qt::DescendingOrder;
220 m_sortKey = QMailMessageSortKey::timeStamp(m_sortOrder);
221 connect(QMailStore::instance(), SIGNAL(messagesAdded(QMailMessageIdList)), this, SLOT(handleNewMessages(QMailMessageIdList)));
222 connect(QMailStore::instance(), SIGNAL(messagesRemoved(QMailMessageIdList)), this, SLOT(handleMessagesRemoved(QMailMessageIdList)));
223 connect(QMailStore::instance(), SIGNAL(messagesUpdated(QMailMessageIdList)), this, SLOT(handleUpdatedMessages(QMailMessageIdList)));
224 }
225
226 MessageList::~MessageList()
227 {
228 m_workerThread.quit();
229 m_workerThread.wait();
230 }
231
232 int MessageList::limit() const
233 {
234 return m_limit;
235 }
236
237 QVariant MessageList::key() const
238 {
239 return m_msgKey;
240 }
241
242 Qt::SortOrder MessageList::sortOrder() const
243 {
244 return m_sortOrder;
245 }
246
247 int MessageList::totalCount()
248 {
249 return QMailStore::instance()->countMessages(messageListKey());
250 }
251
252 bool MessageList::canLoadMore()
253 {
254 if (m_model->isEmpty()) {
255 return false;
256 } else {
257 return m_limit < totalCount();
258 }
259 }
260
261 bool MessageList::canSelectAll()
262 {
263 bool shouldSelectAll = false;
264 foreach (const auto *msg, m_model->toList()) {
265 if (msg->checked() == Qt::Unchecked) {
266 shouldSelectAll = true;
267 break;
268 }
269 }
270 return shouldSelectAll;
271 }
272
273 bool MessageList::canMarkSelectionAsRead()
274 {
275 bool canMarkRead = false;
276 foreach (const auto &id, checkedIds()) {
277 bool isRead = (QMailMessageMetaData(id).status() & QMailMessageMetaData::Read);
278 if (!isRead) {
279 canMarkRead = true;
280 break; // No need to keep looping. one seals the deal
281 }
282 }
283 return canMarkRead;
284 }
285
286 bool MessageList::canMarkSelectionImportant()
287 {
288 bool canMarkFlagged = false;
289 foreach (const auto &id, checkedIds()) {
290 bool isImportant = (QMailMessageMetaData(id).status() & QMailMessageMetaData::Important);
291 if (!isImportant) {
292 canMarkFlagged = true;
293 break; // No need to keep looping. one seals the deal
294 }
295 }
296 return canMarkFlagged;
297 }
298
299 int MessageList::indexOf(const quint64 &id)
300 {
301 return indexOf(QMailMessageId(id));
302 }
303
304 int MessageList::indexOf(const QMailMessageId &id)
305 {
306 QMap<QMailMessageId, int>::const_iterator it = m_indexMap.find(id);
307 if (it != m_indexMap.end()) {
308 return it.value();
309 }
310 return -1;
311 }
312
313 void MessageList::loadMore()
314 {
315 if (canLoadMore()) {
316 setLimit(m_limit + INCREMENT_VALUE);
317 }
318 }
319
320 void MessageList::refresh()
321 {
322 qCDebug(D_MSG_LIST) << "Refreshing Message List";
323 m_loading = true;
324 emit loadingChanged();
325 QDBusPendingReply<QList<quint64> > reply = Client::instance()->bus()->queryMessages(
326 msg_key_bytes(messageListKey()),
327 msg_sort_key_bytes(m_sortKey),
328 m_limit);
329
330 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
331
332 connect(watcher, &QDBusPendingCallWatcher::finished, this, &MessageList::refreshResponse);
333 }
334
335 int MessageList::currentSelectedIndex() const
336 {
337 return m_currentIndex;
338 }
339
340 MessageList::FilterKey MessageList::filterKey() const
341 {
342 return m_filter;
343 }
344
345 bool MessageList::disableUpdates() const
346 {
347 return m_disableUpdates;
348 }
349
350 bool MessageList::disableRemovals() const
351 {
352 return m_disableRemovals;
353 }
354
355 void MessageList::setLimit(int limit)
356 {
357 if (m_limit == limit)
358 return;
359 if (m_limit == 0) {
360 m_limit = limit;
361 reset();
362 } else if (m_limit > limit) {
363 m_limit = limit;
364 removeMessages(m_idList.mid(limit));
365 } else {
366 m_limit = limit;
367 refresh();
368 }
369 emit limitChanged(limit);
370 }
371
372 void MessageList::setKey(const QVariant &key)
373 {
374 if (key.value<QMailMessageKey>() == m_msgKey) {
375 return;
376 }
377 //reset model limit
378 m_limit = INCREMENT_VALUE;
379
380 if (key.canConvert<QMailMessageKey>()) {
381 m_msgKey = key.value<QMailMessageKey>();
382 emit messageKeyChanged();
383 } else {
384 qCDebug(D_MSG_LIST) << "Message key: " << key << " Not of type QMailMessageKey";
385 m_msgKey = QMailMessageKey::nonMatchingKey();
386 }
387 reset();
388 #ifdef EXPERIMENTAL_REMEMBER_SELECTED_MESSAGE
389 QByteArray cacheKey;
390 QDataStream keystream(&cacheKey, QIODevice::WriteOnly);
391 m_msgKey.serialize<QDataStream>(keystream);
392 if (m_cache.contains(cacheKey)) {
393 QMailMessageId lookingFor = *m_cache.object(cacheKey);
394 foreach (auto msg, m_model->toList()) {
395 if (msg->messageId() == lookingFor.toULongLong()) {
396 setCurrentSelectedIndex(m_model->indexOf(msg));
397 return;
398 }
399 setCurrentSelectedIndex(-1);
400 }
401 }
402 #endif
403 }
404
405 void MessageList::setSortOrder(const Qt::SortOrder &order)
406 {
407 if (order == m_sortOrder) {
408 return; // already applied
409 }
410 m_sortOrder = order;
411 m_sortKey = QMailMessageSortKey::timeStamp(m_sortOrder);
412 reset();
413 emit sortOrderChanged();
414 }
415
416 void MessageList::startSelection()
417 {
418 m_selectionMode = true;
419 emit isInSelectionModeChanged();
420 emit selectionStarted();
421 }
422
423 void MessageList::endSelection()
424 {
425 m_selectionMode = false;
426 emit isInSelectionModeChanged();
427 emit selectionEnded();
428 }
429
430 void MessageList::selectAll()
431 {
432 // XXX: iterator?
433 foreach(auto *msg, m_model->toList()) {
434 msg->setProperty("checked", Qt::Checked);
435 }
436 emit selectionIndexesChanged();
437 }
438
439 void MessageList::unselectAll()
440 {
441 foreach(auto *msg, m_model->toList()) {
442 msg->setProperty("checked", Qt::Unchecked);
443 }
444 emit selectionIndexesChanged();
445 }
446
447 void MessageList::setChecked(const int &index, const Qt::CheckState &checkState)
448 {
449 if (index < m_model->count()) {
450 m_model->at(index)->setProperty("checked", checkState);
451 emit selectionIndexesChanged();
452 }
453 }
454
455 void MessageList::markSelectedMessagesImportant()
456 {
457 Client::instance()->markMessagesImportant(checkedIds(), canMarkSelectionImportant());
458 unselectAll();
459 }
460
461 void MessageList::markSelectedMessagesRead()
462 {
463 Client::instance()->markMessagesRead(checkedIds(), canMarkSelectionAsRead());
464 unselectAll();
465 }
466
467 void MessageList::deleteSelectedMessages()
468 {
469 Client::instance()->deleteMessages(checkedIds());
470 unselectAll();
471 }
472
473 void MessageList::setCurrentSelectedIndex(int currentSelectedIndex)
474 {
475 qCDebug(D_MSG_LIST) << "CURRENT INDEX: " << currentSelectedIndex;
476 if (m_currentIndex == currentSelectedIndex)
477 return;
478
479 m_currentIndex = currentSelectedIndex;
480
481 if (currentSelectedIndex == -1) {
482 emit currentSelectedIndexChanged();
483 return;
484 }
485 #ifdef EXPERIMENTAL_REMEMBER_SELECTED_MESSAGE
486 if (!m_msgKey.isEmpty()) {
487 QByteArray cacheKey;
488 QDataStream keystream(&cacheKey, QIODevice::WriteOnly);
489 m_msgKey.serialize<QDataStream>(keystream);
490 if (m_cache.contains(cacheKey)) {
491 m_cache.remove(cacheKey);
492 }
493 m_cache.insert(cacheKey, new QMailMessageId(m_model->at(m_currentIndex)->messageId()));
494 }
495 #endif
496 emit currentSelectedIndexChanged();
497 }
498
499 void MessageList::setFilterKey(MessageList::FilterKey filter)
500 {
501 if (m_filter == filter)
502 return;
503
504 m_filter = filter;
505 if (m_initialized) {
506 reset();
507 }
508 emit filterKeyChanged(filter);
509 }
510
511 void MessageList::setDisableUpdates(bool disableUpdates)
512 {
513 if (m_disableUpdates == disableUpdates)
514 return;
515
516 m_disableUpdates = disableUpdates;
517 emit disableUpdatesChanged(disableUpdates);
518
519 if (!m_disableUpdates && m_needsRefresh) {
520 // QSet<QMailMessageId> set = QSet<QMailMessageId>::fromList(m_refreshList);
521 // handleUpdatedMessages(static_cast<QMailMessageIdList>(set.toList()));
522 refresh();
523 m_needsRefresh = false;
524 }
525 }
526
527 void MessageList::setDisableRemovals(bool disableRemovals)
528 {
529 if (m_disableRemovals == disableRemovals)
530 return;
531
532 m_disableRemovals = disableRemovals;
533 emit disableRemovalsChanged(disableRemovals);
534 }
535
536 void MessageList::handleNewMessages(const QMailMessageIdList &newList)
537 {
538 QElapsedTimer timer;
539 qCDebug(D_MSG_LIST) << "[handleNewMessages] >> Starting";
540
541 if (m_disableUpdates) {
542 m_needsRefresh = true;
543 return;
544 }
545
546 timer.start();
547 if (newList.isEmpty()) {
548 return;
549 }
550
551 if (m_msgKey.isNonMatching()) {
552 return;
553 }
554 if (!m_initialized) {
555 init();
556 }
557 addNewMessages(newList);
558 qCDebug(D_MSG_LIST) << "[handleNewMessages] >> Finished in: " << timer.elapsed() << "milliseconds";
559 }
560
561 void MessageList::handleMessagesRemoved(const QMailMessageIdList &removedList)
562 {
563 QElapsedTimer timer;
564 qCDebug(D_MSG_LIST) << "[handleMessagesRemoved] >> Starting";
565
566 if (m_disableUpdates) {
567 m_needsRefresh = true;
568 // m_refreshList << updatedList;
569 return;
570 }
571
572 if (m_disableRemovals) {
573 return;
574 }
575
576 timer.start();
577 if (removedList.isEmpty()) {
578 return;
579 }
580
581 if (m_msgKey.isNonMatching()) {
582 return;
583 }
584 if (!m_initialized) {
585 init();
586 }
587 removeMessages(removedList);
588 qCDebug(D_MSG_LIST) << "[handleMessagesRemoved] >> Finished in: " << timer.elapsed() << "milliseconds";
589 }
590
591 void MessageList::handleUpdatedMessages(const QMailMessageIdList &updatedList)
592 {
593 QElapsedTimer timer;
594 qCDebug(D_MSG_LIST) << "[handleUpdatedMessages] >> Starting";
595
596 if (m_disableUpdates) {
597 m_needsRefresh = true;
598 // m_refreshList << updatedList;
599 return;
600 }
601 timer.start();
602
603 QMailMessageIdList needsUpdate;
604 foreach(const QMailMessageId &id, updatedList) {
605 if (m_idList.contains(id)) {
606 needsUpdate << id;
607 }
608 }
609
610 if (needsUpdate.isEmpty()) {
611 qCDebug(D_MSG_LIST) << "[handleUpdatedMessages] >> No updates in this list. Finished in: " << timer.elapsed() << "milliseconds";
612 return;
613 }
614
615 // Find the updated positions for our messages
616 QMailMessageKey idKey(QMailMessageKey::id((m_idList.toSet() + needsUpdate.toSet()).toList()));
617 QMailMessageKey msgKey = messageListKey() & idKey;
618 QDBusPendingReply<QList<quint64> > reply = Client::instance()->bus()->queryMessages(msg_key_bytes(msgKey), msg_sort_key_bytes(m_sortKey), m_limit);
619
620 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
621
622 connect(watcher, &QDBusPendingCallWatcher::finished, [=](QDBusPendingCallWatcher *call){
623 QDBusPendingReply<QList<quint64> > reply = *call;
624 if (reply.isError()) {
625 qCDebug(D_MSG_LIST) << "[handleUpdatedMessages] >> Reply error";
626 return;
627 }
628 QMailMessageIdList newIds = from_dbus_msglist(reply.argumentAt<0>());
629
630 emit updateMessages(m_idList, needsUpdate, newIds, m_indexMap, m_limit);
631 call->deleteLater();
632 });
633 qCDebug(D_MSG_LIST) << "[handleUpdatedMessages] >> Finished in: " << timer.elapsed() << "milliseconds";
634 }
635
636 void MessageList::insertMessageAt(const int &index, const QMailMessageId &id)
637 {
638 MinimalMessage *msg = new MinimalMessage();
639 msg->setMessageId(id);
640 connect(this, &MessageList::selectionStarted, msg, &MinimalMessage::selectionStarted);
641 connect(this, &MessageList::selectionEnded, msg, &MinimalMessage::selectionEnded);
642 m_model->insert(index, msg);
643 // update indexes
644 m_idList.insert(index, id);
645 m_indexMap.insert(id, index);
646 // Adjust the indices for the items that have been moved
647 QList<QMailMessageId>::iterator it = m_idList.begin() + (index + 1), end = m_idList.end();
648 for ( ; it != end; ++it) {
649 m_indexMap[*it] += 1;
650 }
651 emit totalCountChanged();
652 }
653
654 void MessageList::removeMessageAt(const int &index)
655 {
656 if (m_disableRemovals) {
657 return;
658 }
659 QElapsedTimer timer;
660 qCDebug(D_MSG_LIST) << "[removeMessageAt] >> Starting";
661 timer.start();
662 QMailMessageId id(m_idList.at(index));
663 m_indexMap.remove(id);
664 m_idList.removeAt(index);
665 qCDebug(D_MSG_LIST) << "Removing Message:" << m_model->at(index)->messageId();
666 m_model->remove(index);
667
668 // Adjust the indices for the items that have been moved
669 QList<QMailMessageId>::iterator it = m_idList.begin() + index, end = m_idList.end();
670 for ( ; it != end; ++it) {
671 m_indexMap[*it] -= 1;
672 }
673 emit totalCountChanged();
674 qCDebug(D_MSG_LIST) << "[removeMessageAt] >> Finished in: " << timer.elapsed() << "milliseconds";
675 }
676
677 void MessageList::addNewMessages(const QMailMessageIdList &idList)
678 {
679 QElapsedTimer timer;
680 qCDebug(D_MSG_LIST) << "[addNewMessages] >> Starting";
681 timer.start();
682 // Are any of these messages members of our display set?
683 // Note - we must only consider messages in the set given by (those we currently know +
684 // those we have now been informed of) because the database content may have changed between
685 // when this event was recorded and when we're processing the signal.
686
687 QMailMessageKey idKey(QMailMessageKey::id(m_idList + idList));
688 QMailMessageKey mIdKey = messageListKey() & idKey;
689
690 QDBusPendingReply<QList<quint64> > reply = Client::instance()->bus()->queryMessages(
691 msg_key_bytes(mIdKey), msg_sort_key_bytes(m_sortKey), m_limit);
692
693 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
694
695 connect(watcher, &QDBusPendingCallWatcher::finished, [=](QDBusPendingCallWatcher *call){
696 QDBusPendingReply<QList<quint64> > reply = *call;
697 if (reply.isError()) {
698 qCDebug(D_MSG_LIST) << "[addNewMessages] >> Reply error";
699 return;
700 }
701 QMailMessageIdList newIdsList = from_dbus_msglist(reply.argumentAt<0>());
702 emit sortAndAppendNewMessages(m_idList, idList, newIdsList, m_indexMap, m_limit);
703 call->deleteLater();
704 });
705 qCDebug(D_MSG_LIST) << "[addNewMessages] >> Finished in: " << timer.elapsed() << "milliseconds";
706 }
707
708 void MessageList::removeMessages(const QMailMessageIdList &idList)
709 {
710 QList<int> removeIndices;
711 foreach (const QMailMessageId &id, idList) {
712 int index(indexOf(id));
713 if (index != -1) {
714 removeIndices.append(index);
715 }
716 }
717
718 // Sort the indices to yield ascending order (they must be deleted in descending order!)
719 std::sort(removeIndices.begin(), removeIndices.end());
720
721 for (int i = removeIndices.count() - 1 ; i > 0; --i) {
722 int index = removeIndices.at(i - 1);
723 removeMessageAt(index);
724 }
725 }
726
727 void MessageList::updateMessageAt(const int &index)
728 {
729 qCDebug(D_MSG_LIST) << "Message Updated: " << m_model->at(index)->messageId();
730 m_model->at(index)->emitMinMessageChanged();
731 }
732
733 void MessageList::refreshResponse(QDBusPendingCallWatcher *call)
734 {
735 qCDebug(D_MSG_LIST) << "[MessageList::refreshResponse] >> Started";
736 QDBusPendingReply<QList<quint64> > reply = *call;
737 if (reply.isError()) {
738 qCDebug(D_MSG_LIST) << "Reply error for refresh response";
739 return;
740 }
741 QList<quint64> ids = reply.argumentAt<0>();
742 QMailMessageIdList newIdsList = from_dbus_msglist(ids);
743
744 QMailMessageIdList idsToAppend;
745 foreach (const QMailMessageId &id, newIdsList) {
746 if (!m_idList.contains(id)) {
747 idsToAppend.append(id);
748 }
749 }
750 emit sortAndAppendNewMessages(m_idList, idsToAppend, newIdsList, m_indexMap, m_limit);
751 call->deleteLater();
752
753 if (m_loading) {
754 m_loading = false;
755 emit loadingChanged();
756 }
757 qCDebug(D_MSG_LIST) << "[MessageList::refreshResponse] >> Finished";
758 }
759
760 void MessageList::queryMessageResponse(QDBusPendingCallWatcher *call)
761 {
762 qCDebug(D_MSG_LIST) << "GOT DBUS QUERY RESPONSE";
763 // DO NOT REMOVE!!!
764 call->deleteLater();
765 }
766
767 QMailMessageIdList MessageList::checkedIds()
768 {
769 if (!m_selectionMode) {
770 return QMailMessageIdList();
771 }
772 QMailMessageIdList checkedMsgs;
773 foreach(const MinimalMessage *m, m_model->toList()) {
774 if (m->checked() == Qt::Checked) {
775 checkedMsgs.append(QMailMessageId(m->messageId()));
776 }
777 }
778 return checkedMsgs;
779 }
780
781 void MessageList::init()
782 {
783 if (!m_initialized) {
784 m_model->clear();
785 m_idList.clear();
786 m_indexMap.clear();
787
788 m_loading = true;
789 emit loadingChanged();
790
791 QDBusPendingReply<QList<quint64> > reply = Client::instance()->bus()->queryMessages(
792 msg_key_bytes(messageListKey()), msg_sort_key_bytes(m_sortKey), m_limit);
793
794 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
795
796 connect(watcher, &QDBusPendingCallWatcher::finished, [=](QDBusPendingCallWatcher *call){
797 QDBusPendingReply<QList<quint64> > reply = *call;
798 if (reply.isError()) {
799 qCDebug(D_MSG_LIST) << "Reply error for init";
800 return;
801 }
802 QMailMessageIdList tmpList = from_dbus_msglist(reply.argumentAt<0>());
803 int index = 0;
804 Q_FOREACH(const auto &id, tmpList) {
805 insertMessageAt(index, id);
806 ++index;
807 }
808 m_initialized = true;
809 emit canPossiblyLoadMore();
810 call->deleteLater();
811
812 if (m_loading) {
813 m_loading = false;
814 emit loadingChanged();
815 }
816 });
817 }
818 }
819
820 void MessageList::reset()
821 {
822 m_initialized = false;
823 m_limit = 50;
824 init();
825 }
826
827 QMailMessageKey MessageList::messageListKey()
828 {
829 QMailMessageKey key;
830 switch (m_filter) {
831 case FilterKey::All:
832 return m_msgKey;
833 case FilterKey::Important:
834 key = QMailMessageKey::status(QMailMessage::Important, QMailDataComparator::Includes);
835 break;
836 case FilterKey::Unread:
837 key = QMailMessageKey::status((QMailMessage::Read | QMailMessage::ReadElsewhere), QMailDataComparator::Excludes);
838 break;
839 case FilterKey::Replied:
840 key = QMailMessageKey::status((QMailMessage::Replied | QMailMessage::RepliedAll), QMailDataComparator::Includes);
841 break;
842 case FilterKey::Forwarded:
843 key = QMailMessageKey::status(QMailMessage::Forwarded, QMailDataComparator::Includes);
844 break;
845 case FilterKey::Attachments:
846 key = QMailMessageKey::status(QMailMessage::HasAttachments, QMailDataComparator::Includes);
847 break;
848 case FilterKey::HighPriority:
849 key = QMailMessageKey::status(QMailMessage::HighPriority, QMailDataComparator::Includes);
850 break;
851 case FilterKey::Calendar:
852 key = QMailMessageKey::status(QMailMessage::CalendarInvitation, QMailDataComparator::Includes);
853 break;
854 case FilterKey::Todo:
855 key = QMailMessageKey::status(QMailMessage::Todo, QMailDataComparator::Includes);
856 break;
857 }
858 return m_msgKey & key;
859 }
860
861
862