| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "services/abstract/label.h" |
| 4 | |
| 5 | #include "database/databasefactory.h" |
| 6 | #include "database/databasequeries.h" |
| 7 | #include "definitions/globals.h" |
| 8 | #include "miscellaneous/application.h" |
| 9 | #include "services/abstract/cacheforserviceroot.h" |
| 10 | #include "services/abstract/gui/formaddeditlabel.h" |
| 11 | #include "services/abstract/serviceroot.h" |
| 12 | |
| 13 | #include <QPainter> |
| 14 | #include <QPainterPath> |
| 15 | |
| 16 | Label::Label(const QString& name, const QColor& color, RootItem* parent_item) : Label(parent_item) { |
| 17 | setColor(color); |
| 18 | setTitle(name); |
| 19 | } |
| 20 | |
| 21 | Label::Label(RootItem* parent_item) : RootItem(parent_item) { |
| 22 | setKind(RootItem::Kind::Label); |
| 23 | } |
| 24 | |
| 25 | QColor Label::color() const { |
| 26 | return m_color; |
| 27 | } |
| 28 | |
| 29 | void Label::setColor(const QColor& color) { |
| 30 | setIcon(IconFactory::generateIcon(color)); |
| 31 | m_color = color; |
| 32 | } |
| 33 | |
| 34 | int Label::countOfUnreadMessages() const { |
| 35 | return m_unreadCount; |
| 36 | } |
| 37 | |
| 38 | int Label::countOfAllMessages() const { |
| 39 | return m_totalCount; |
| 40 | } |
| 41 | |
| 42 | bool Label::canBeEdited() const { |
| 43 | return Globals::hasFlag(getParentServiceRoot()->supportedLabelOperations(), ServiceRoot::LabelOperation::Editing); |
| 44 | } |
| 45 | |
| 46 | bool Label::canBeDeleted() const { |
| 47 | return Globals::hasFlag(getParentServiceRoot()->supportedLabelOperations(), ServiceRoot::LabelOperation::Deleting); |
| 48 | } |
| 49 | |
| 50 | bool Label::deleteItem() { |
| 51 | QSqlDatabase db = qApp->database()->driver()->connection(metaObject()->className()); |
| 52 | |
| 53 | if (DatabaseQueries::deleteLabel(db, this)) { |
| 54 | getParentServiceRoot()->requestItemRemoval(this); |
| 55 | return true; |
| 56 | } |
| 57 | else { |
| 58 | return false; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void Label::updateCounts(bool including_total_count) { |
| 63 | QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className()); |
| 64 | int account_id = getParentServiceRoot()->accountId(); |
| 65 | |
| 66 | // TODO: slow |
| 67 | auto ac = DatabaseQueries::getMessageCountsForLabel(database, this, account_id); |
| 68 | |
| 69 | if (including_total_count) { |
| 70 | setCountOfAllMessages(ac.m_total); |
| 71 | } |
| 72 | |
| 73 | setCountOfUnreadMessages(ac.m_unread); |
| 74 | } |
| 75 | |
| 76 | QList<Message> Label::undeletedMessages() const { |
| 77 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 78 | |
| 79 | return DatabaseQueries::getUndeletedMessagesWithLabel(database, this); |
| 80 | } |
| 81 | |
| 82 | void Label::assignToMessage(const Message& msg, bool reload_model) { |
| 83 | QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className()); |
| 84 | |
| 85 | if (getParentServiceRoot()->onBeforeLabelMessageAssignmentChanged({this}, {msg}, true)) { |
| 86 | DatabaseQueries::assignLabelToMessage(database, this, msg); |
| 87 | |
| 88 | if (reload_model) { |
| 89 | getParentServiceRoot()->onAfterLabelMessageAssignmentChanged({this}, {msg}, true); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void Label::deassignFromMessage(const Message& msg, bool reload_model) { |
| 95 | QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className()); |
| 96 | |
| 97 | if (getParentServiceRoot()->onBeforeLabelMessageAssignmentChanged({this}, {msg}, false)) { |
| 98 | DatabaseQueries::deassignLabelFromMessage(database, this, msg); |
| 99 | |
| 100 | if (reload_model) { |
| 101 | getParentServiceRoot()->onAfterLabelMessageAssignmentChanged({this}, {msg}, false); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void Label::setCountOfAllMessages(int totalCount) { |
| 107 | m_totalCount = totalCount; |
| 108 | } |
| 109 | |
| 110 | void Label::setCountOfUnreadMessages(int unreadCount) { |
| 111 | m_unreadCount = unreadCount; |
| 112 | } |
| 113 | |
| 114 | bool Label::cleanMessages(bool clear_only_read) { |
| 115 | ServiceRoot* service = getParentServiceRoot(); |
| 116 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 117 | |
| 118 | if (DatabaseQueries::cleanLabelledMessages(database, clear_only_read, this)) { |
| 119 | service->updateCounts(true); |
| 120 | service->itemChanged(service->getSubTree<RootItem>()); |
| 121 | service->requestReloadMessageList(true); |
| 122 | return true; |
| 123 | } |
| 124 | else { |
| 125 | return false; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | bool Label::markAsReadUnread(RootItem::ReadStatus status) { |
| 130 | ServiceRoot* service = getParentServiceRoot(); |
| 131 | auto* cache = dynamic_cast<CacheForServiceRoot*>(service); |
| 132 | |
| 133 | if (cache != nullptr) { |
| 134 | cache->addMessageStatesToCache(service->customIDSOfMessagesForItem(this, status), status); |
| 135 | } |
| 136 | |
| 137 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 138 | |
| 139 | if (DatabaseQueries::markLabelledMessagesReadUnread(database, this, status)) { |
| 140 | service->updateCounts(false); |
| 141 | service->itemChanged(service->getSubTree<RootItem>()); |
| 142 | service->requestReloadMessageList(status == RootItem::ReadStatus::Read); |
| 143 | return true; |
| 144 | } |
| 145 | else { |
| 146 | return false; |
| 147 | } |
| 148 | } |