| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "services/abstract/search.h" |
| 4 | |
| 5 | #include "database/databasefactory.h" |
| 6 | #include "database/databasequeries.h" |
| 7 | #include "miscellaneous/application.h" |
| 8 | #include "services/abstract/cacheforserviceroot.h" |
| 9 | #include "services/abstract/gui/formaddeditprobe.h" |
| 10 | #include "services/abstract/serviceroot.h" |
| 11 | |
| 12 | #include <QPainter> |
| 13 | #include <QPainterPath> |
| 14 | |
| 15 | Search::Search(const QString& name, const QString& filter, const QColor& color, RootItem* parent_item) |
| 16 | : Search(parent_item) { |
| 17 | setColor(color); |
| 18 | setTitle(name); |
| 19 | setFilter(filter); |
| 20 | } |
| 21 | |
| 22 | Search::Search(RootItem* parent_item) : RootItem(parent_item), m_totalCount(-1), m_unreadCount(-1) { |
| 23 | setKind(RootItem::Kind::Probe); |
| 24 | } |
| 25 | |
| 26 | QColor Search::color() const { |
| 27 | return m_color; |
| 28 | } |
| 29 | |
| 30 | void Search::setColor(const QColor& color) { |
| 31 | setIcon(IconFactory::generateIcon(color)); |
| 32 | m_color = color; |
| 33 | } |
| 34 | |
| 35 | int Search::countOfUnreadMessages() const { |
| 36 | return m_unreadCount; |
| 37 | } |
| 38 | |
| 39 | int Search::countOfAllMessages() const { |
| 40 | return m_totalCount; |
| 41 | } |
| 42 | |
| 43 | bool Search::canBeEdited() const { |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | bool Search::canBeDeleted() const { |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | bool Search::deleteItem() { |
| 52 | try { |
| 53 | QSqlDatabase db = qApp->database()->driver()->connection(metaObject()->className()); |
| 54 | DatabaseQueries::deleteProbe(db, this); |
| 55 | getParentServiceRoot()->requestItemRemoval(this); |
| 56 | return true; |
| 57 | } |
| 58 | catch (const ApplicationException& ex) { |
| 59 | qCriticalNN << LOGSEC_CORE << "Failed to remove probe:" << QUOTE_W_SPACE_DOT(ex.message()); |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void Search::updateCounts(bool including_total_count) { |
| 65 | Q_UNUSED(including_total_count) |
| 66 | |
| 67 | setCountOfAllMessages(-1); |
| 68 | setCountOfUnreadMessages(-1); |
| 69 | } |
| 70 | |
| 71 | QList<Message> Search::undeletedMessages() const { |
| 72 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 73 | |
| 74 | return DatabaseQueries::getUndeletedMessagesForProbe(database, this); |
| 75 | } |
| 76 | |
| 77 | QString Search::filter() const { |
| 78 | return m_filter; |
| 79 | } |
| 80 | |
| 81 | void Search::setFilter(const QString& new_filter) { |
| 82 | m_filter = new_filter; |
| 83 | } |
| 84 | |
| 85 | void Search::setCountOfAllMessages(int totalCount) { |
| 86 | m_totalCount = totalCount; |
| 87 | } |
| 88 | |
| 89 | void Search::setCountOfUnreadMessages(int unreadCount) { |
| 90 | m_unreadCount = unreadCount; |
| 91 | } |
| 92 | |
| 93 | bool Search::cleanMessages(bool clear_only_read) { |
| 94 | ServiceRoot* service = getParentServiceRoot(); |
| 95 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 96 | |
| 97 | try { |
| 98 | DatabaseQueries::cleanProbedMessages(database, clear_only_read, this); |
| 99 | service->updateCounts(true); |
| 100 | service->itemChanged(service->getSubTree<RootItem>()); |
| 101 | service->requestReloadMessageList(true); |
| 102 | return true; |
| 103 | } |
| 104 | catch (const ApplicationException& ex) { |
| 105 | qCriticalNN << LOGSEC_CORE << "Failed to clean messages of probe:" << QUOTE_W_SPACE_DOT(ex.message()); |
| 106 | return false; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | QString Search::additionalTooltip() const { |
| 111 | return tr("Regular expression: %1").arg(QSL("<code>%1</code>").arg(filter())); |
| 112 | } |
| 113 | |
| 114 | bool Search::markAsReadUnread(RootItem::ReadStatus status) { |
| 115 | ServiceRoot* service = getParentServiceRoot(); |
| 116 | auto* cache = dynamic_cast<CacheForServiceRoot*>(service); |
| 117 | |
| 118 | if (cache != nullptr) { |
| 119 | try { |
| 120 | cache->addMessageStatesToCache(service->customIDSOfMessagesForItem(this, status), status); |
| 121 | } |
| 122 | catch (const ApplicationException& ex) { |
| 123 | qCriticalNN << LOGSEC_DB << "Cannot add some IDs to state cache:" << QUOTE_W_SPACE_DOT(ex.message()); |
| 124 | return false; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 129 | |
| 130 | try { |
| 131 | DatabaseQueries::markProbeReadUnread(database, this, status); |
| 132 | service->updateCounts(false); |
| 133 | service->itemChanged(service->getSubTree<RootItem>()); |
| 134 | service->requestReloadMessageList(status == RootItem::ReadStatus::Read); |
| 135 | return true; |
| 136 | } |
| 137 | catch (const ApplicationException& ex) { |
| 138 | qCriticalNN << LOGSEC_DB << "Cannot mark probe as read/unread:" << QUOTE_W_SPACE_DOT(ex.message()); |
| 139 | return false; |
| 140 | } |
| 141 | } |