| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "services/abstract/importantnode.h" |
| 4 | |
| 5 | #include "database/databasequeries.h" |
| 6 | #include "miscellaneous/application.h" |
| 7 | #include "miscellaneous/iconfactory.h" |
| 8 | #include "services/abstract/cacheforserviceroot.h" |
| 9 | #include "services/abstract/serviceroot.h" |
| 10 | |
| 11 | ImportantNode::ImportantNode(RootItem* parent_item) : RootItem(parent_item) { |
| 12 | setKind(RootItem::Kind::Important); |
| 13 | setId(ID_IMPORTANT); |
| 14 | setIcon(qApp->icons()->fromTheme(QSL("mail-mark-important"))); |
| 15 | setTitle(tr("Important articles")); |
| 16 | setDescription(tr("You can find all important articles here.")); |
| 17 | } |
| 18 | |
| 19 | QList<Message> ImportantNode::undeletedMessages() const { |
| 20 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 21 | |
| 22 | return DatabaseQueries::getUndeletedImportantMessages(database, getParentServiceRoot()->accountId()); |
| 23 | } |
| 24 | |
| 25 | void ImportantNode::updateCounts(bool including_total_count) { |
| 26 | QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className()); |
| 27 | int account_id = getParentServiceRoot()->accountId(); |
| 28 | auto ac = DatabaseQueries::getImportantMessageCounts(database, account_id); |
| 29 | |
| 30 | if (including_total_count) { |
| 31 | m_totalCount = ac.m_total; |
| 32 | } |
| 33 | |
| 34 | m_unreadCount = ac.m_unread; |
| 35 | } |
| 36 | |
| 37 | bool ImportantNode::cleanMessages(bool clean_read_only) { |
| 38 | ServiceRoot* service = getParentServiceRoot(); |
| 39 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 40 | |
| 41 | if (DatabaseQueries::cleanImportantMessages(database, clean_read_only, 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 ImportantNode::markAsReadUnread(RootItem::ReadStatus status) { |
| 53 | ServiceRoot* service = getParentServiceRoot(); |
| 54 | auto* cache = dynamic_cast<CacheForServiceRoot*>(service); |
| 55 | |
| 56 | if (cache != nullptr) { |
| 57 | cache->addMessageStatesToCache(service->customIDSOfMessagesForItem(this, status), status); |
| 58 | } |
| 59 | |
| 60 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 61 | |
| 62 | if (DatabaseQueries::markImportantMessagesReadUnread(database, service->accountId(), status)) { |
| 63 | service->updateCounts(false); |
| 64 | service->itemChanged(service->getSubTree<RootItem>()); |
| 65 | service->requestReloadMessageList(status == RootItem::ReadStatus::Read); |
| 66 | return true; |
| 67 | } |
| 68 | else { |
| 69 | return false; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | int ImportantNode::countOfUnreadMessages() const { |
| 74 | return m_unreadCount; |
| 75 | } |
| 76 | |
| 77 | int ImportantNode::countOfAllMessages() const { |
| 78 | return m_totalCount; |
| 79 | } |