| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "services/abstract/unreadnode.h" |
| 4 | |
| 5 | #include "database/databasequeries.h" |
| 6 | #include "miscellaneous/application.h" |
| 7 | #include "miscellaneous/iconfactory.h" |
| 8 | |
| 9 | UnreadNode::UnreadNode(RootItem* parent_item) : RootItem(parent_item) { |
| 10 | setKind(RootItem::Kind::Unread); |
| 11 | setId(ID_UNREAD); |
| 12 | setIcon(qApp->icons()->fromTheme(QSL("mail-mark-unread"))); |
| 13 | |
| 14 | setTitle(tr("Unread articles")); |
| 15 | setDescription(tr("You can find all unread articles here.")); |
| 16 | } |
| 17 | |
| 18 | QList<Message> UnreadNode::undeletedMessages() const { |
| 19 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 20 | |
| 21 | return DatabaseQueries::getUndeletedUnreadMessages(database, getParentServiceRoot()->accountId()); |
| 22 | } |
| 23 | |
| 24 | void UnreadNode::updateCounts(bool including_total_count) { |
| 25 | Q_UNUSED(including_total_count) |
| 26 | |
| 27 | QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className()); |
| 28 | int account_id = getParentServiceRoot()->accountId(); |
| 29 | |
| 30 | m_totalCount = m_unreadCount = DatabaseQueries::getUnreadMessageCounts(database, account_id); |
| 31 | } |
| 32 | |
| 33 | bool UnreadNode::cleanMessages(bool clean_read_only) { |
| 34 | if (clean_read_only) { |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | ServiceRoot* service = getParentServiceRoot(); |
| 39 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 40 | |
| 41 | if (DatabaseQueries::cleanUnreadMessages(database, service->accountId())) { |
| 42 | service->updateCounts(true); |
| 43 | service->itemChanged(service->getSubTree<RootItem>()); |
| 44 | service->requestReloadMessageList(true); |
| 45 | return true; |
| 46 | } |
| 47 | else { |
| 48 | return false; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | bool UnreadNode::markAsReadUnread(RootItem::ReadStatus status) { |
| 53 | if (status == RootItem::ReadStatus::Unread) { |
| 54 | // NOTE: We do not need to mark already unread messages as unread. |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | ServiceRoot* service = getParentServiceRoot(); |
| 59 | auto* cache = dynamic_cast<CacheForServiceRoot*>(service); |
| 60 | |
| 61 | if (cache != nullptr) { |
| 62 | cache->addMessageStatesToCache(service->customIDSOfMessagesForItem(this, status), status); |
| 63 | } |
| 64 | |
| 65 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 66 | |
| 67 | if (DatabaseQueries::markUnreadMessagesRead(database, service->accountId())) { |
| 68 | service->updateCounts(false); |
| 69 | service->itemChanged(service->getSubTree<RootItem>()); |
| 70 | service->requestReloadMessageList(status == RootItem::ReadStatus::Read); |
| 71 | return true; |
| 72 | } |
| 73 | else { |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | int UnreadNode::countOfUnreadMessages() const { |
| 79 | return m_unreadCount; |
| 80 | } |
| 81 | |
| 82 | int UnreadNode::countOfAllMessages() const { |
| 83 | return m_totalCount; |
| 84 | } |