| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "core/messagesproxymodel.h" |
| 4 | |
| 5 | #include "core/messagesmodel.h" |
| 6 | #include "core/messagesmodelcache.h" |
| 7 | #include "definitions/globals.h" |
| 8 | #include "miscellaneous/regexfactory.h" |
| 9 | #include "miscellaneous/textfactory.h" |
| 10 | |
| 11 | #include <QTimer> |
| 12 | |
| 13 | MessagesProxyModel::MessagesProxyModel(MessagesModel* source_model, QObject* parent) |
| 14 | : QSortFilterProxyModel(parent), m_sourceModel(source_model), m_filter(MessageListFilter::NoFiltering), |
| 15 | m_additionalArticleId(0) { |
| 16 | setObjectName(QSL("MessagesProxyModel")); |
| 17 | |
| 18 | initializeFilters(); |
| 19 | |
| 20 | setSortRole(Qt::ItemDataRole::EditRole); |
| 21 | setSortCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive); |
| 22 | |
| 23 | setFilterKeyColumn(-1); |
| 24 | setFilterRole(Qt::ItemDataRole::EditRole); |
| 25 | |
| 26 | setDynamicSortFilter(false); |
| 27 | setSourceModel(m_sourceModel); |
| 28 | } |
| 29 | |
| 30 | MessagesProxyModel::~MessagesProxyModel() { |
| 31 | qDebugNN << LOGSEC_MESSAGEMODEL << "Destroying MessagesProxyModel instance."; |
| 32 | } |
| 33 | |
| 34 | void MessagesProxyModel::initializeFilters() { |
| 35 | m_filters[MessageListFilter::ShowUnread] = [this](int msg_row_index) { |
| 36 | return !m_sourceModel->data(msg_row_index, MSG_DB_READ_INDEX, Qt::ItemDataRole::EditRole).toBool(); |
| 37 | }; |
| 38 | |
| 39 | m_filters[MessageListFilter::ShowRead] = [this](int msg_row_index) { |
| 40 | return m_sourceModel->data(msg_row_index, MSG_DB_READ_INDEX, Qt::ItemDataRole::EditRole).toBool(); |
| 41 | }; |
| 42 | |
| 43 | m_filters[MessageListFilter::ShowImportant] = [this](int msg_row_index) { |
| 44 | return m_sourceModel->data(msg_row_index, MSG_DB_IMPORTANT_INDEX, Qt::ItemDataRole::EditRole).toBool(); |
| 45 | }; |
| 46 | |
| 47 | m_filters[MessageListFilter::ShowToday] = [this](int msg_row_index) { |
| 48 | const QDateTime current_dt = QDateTime::currentDateTime(); |
| 49 | const QDate current_d = current_dt.date(); |
| 50 | const QDateTime msg_created = |
| 51 | TextFactory::parseDateTime(m_sourceModel->data(msg_row_index, MSG_DB_DCREATED_INDEX, Qt::ItemDataRole::EditRole) |
| 52 | .value<qint64>()); |
| 53 | |
| 54 | return current_d.startOfDay() <= msg_created && msg_created <= current_d.endOfDay(); |
| 55 | }; |
| 56 | |
| 57 | m_filters[MessageListFilter::ShowYesterday] = [this](int msg_row_index) { |
| 58 | const QDateTime current_dt = QDateTime::currentDateTime(); |
| 59 | const QDate current_d = current_dt.date(); |
| 60 | const QDateTime msg_created = |
| 61 | TextFactory::parseDateTime(m_sourceModel->data(msg_row_index, MSG_DB_DCREATED_INDEX, Qt::ItemDataRole::EditRole) |
| 62 | .value<qint64>()); |
| 63 | |
| 64 | return current_d.addDays(-1).startOfDay() <= msg_created && msg_created <= current_d.addDays(-1).endOfDay(); |
| 65 | }; |
| 66 | |
| 67 | m_filters[MessageListFilter::ShowLast24Hours] = [this](int msg_row_index) { |
| 68 | const QDateTime current_dt = QDateTime::currentDateTime(); |
| 69 | const QDateTime msg_created = |
| 70 | TextFactory::parseDateTime(m_sourceModel->data(msg_row_index, MSG_DB_DCREATED_INDEX, Qt::ItemDataRole::EditRole) |
| 71 | .value<qint64>()); |
| 72 | |
| 73 | return current_dt.addSecs(-24 * 60 * 60) <= msg_created && msg_created <= current_dt; |
| 74 | }; |
| 75 | |
| 76 | m_filters[MessageListFilter::ShowLast48Hours] = [this](int msg_row_index) { |
| 77 | const QDateTime current_dt = QDateTime::currentDateTime(); |
| 78 | const QDateTime msg_created = |
| 79 | TextFactory::parseDateTime(m_sourceModel->data(msg_row_index, MSG_DB_DCREATED_INDEX, Qt::ItemDataRole::EditRole) |
| 80 | .value<qint64>()); |
| 81 | |
| 82 | return current_dt.addSecs(-48 * 60 * 60) <= msg_created && msg_created <= current_dt; |
| 83 | }; |
| 84 | |
| 85 | m_filters[MessageListFilter::ShowThisWeek] = [this](int msg_row_index) { |
| 86 | const QDateTime current_dt = QDateTime::currentDateTime(); |
| 87 | const QDate current_d = current_dt.date(); |
| 88 | const QDateTime msg_created = |
| 89 | TextFactory::parseDateTime(m_sourceModel->data(msg_row_index, MSG_DB_DCREATED_INDEX, Qt::ItemDataRole::EditRole) |
| 90 | .value<qint64>()); |
| 91 | |
| 92 | return current_d.year() == msg_created.date().year() && current_d.weekNumber() == msg_created.date().weekNumber(); |
| 93 | }; |
| 94 | |
| 95 | m_filters[MessageListFilter::ShowLastWeek] = [this](int msg_row_index) { |
| 96 | const QDateTime current_dt = QDateTime::currentDateTime(); |
| 97 | const QDate current_d = current_dt.date(); |
| 98 | const QDateTime msg_created = |
| 99 | TextFactory::parseDateTime(m_sourceModel->data(msg_row_index, MSG_DB_DCREATED_INDEX, Qt::ItemDataRole::EditRole) |
| 100 | .value<qint64>()); |
| 101 | |
| 102 | return current_d.addDays(-7).year() == msg_created.date().year() && |
| 103 | current_d.addDays(-7).weekNumber() == msg_created.date().weekNumber(); |
| 104 | }; |
| 105 | |
| 106 | m_filters[MessageListFilter::ShowOnlyWithAttachments] = [this](int msg_row_index) { |
| 107 | const bool msg_has_enclosures = |
| 108 | m_sourceModel->data(msg_row_index, MSG_DB_HAS_ENCLOSURES, Qt::ItemDataRole::EditRole).toBool(); |
| 109 | |
| 110 | return msg_has_enclosures; |
| 111 | }; |
| 112 | |
| 113 | m_filters[MessageListFilter::ShowOnlyWithScore] = [this](int msg_row_index) { |
| 114 | const int msg_score = m_sourceModel->data(msg_row_index, MSG_DB_SCORE_INDEX, Qt::ItemDataRole::EditRole).toDouble(); |
| 115 | |
| 116 | return msg_score > MSG_SCORE_MIN; |
| 117 | }; |
| 118 | |
| 119 | m_filterKeys = m_filters.keys(); |
| 120 | } |
| 121 | |
| 122 | bool MessagesProxyModel::filterAcceptsMessage(int msg_row_index) const { |
| 123 | if (m_filter == MessageListFilter::NoFiltering) { |
| 124 | return true; |
| 125 | } |
| 126 | else if (m_additionalArticleId > 0 && |
| 127 | m_sourceModel->data(msg_row_index, MSG_DB_ID_INDEX, Qt::ItemDataRole::EditRole).toInt() == |
| 128 | m_additionalArticleId) { |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | for (MessageListFilter val : m_filterKeys) { |
| 133 | if (Globals::hasFlag(m_filter, val) && m_filters[val](msg_row_index)) { |
| 134 | return true; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | QModelIndex MessagesProxyModel::getNextPreviousImportantItemIndex(int default_row) const { |
| 142 | const bool started_from_zero = default_row == 0; |
| 143 | QModelIndex next_index = getNextImportantItemIndex(default_row, rowCount() - 1); |
| 144 | |
| 145 | // There is no next message, check previous. |
| 146 | if (!next_index.isValid() && !started_from_zero) { |
| 147 | next_index = getNextImportantItemIndex(0, default_row - 1); |
| 148 | } |
| 149 | |
| 150 | return next_index; |
| 151 | } |
| 152 | |
| 153 | QModelIndex MessagesProxyModel::getNextPreviousUnreadItemIndex(int default_row) const { |
| 154 | const bool started_from_zero = default_row == 0; |
| 155 | QModelIndex next_index = getNextUnreadItemIndex(default_row, rowCount() - 1); |
| 156 | |
| 157 | // There is no next message, check previous. |
| 158 | if (!next_index.isValid() && !started_from_zero) { |
| 159 | next_index = getNextUnreadItemIndex(0, default_row - 1); |
| 160 | } |
| 161 | |
| 162 | return next_index; |
| 163 | } |
| 164 | |
| 165 | QModelIndex MessagesProxyModel::indexFromMessage(const Message& msg) const { |
| 166 | for (int i = 0; i < rowCount(); i++) { |
| 167 | auto idx = index(i, 0); |
| 168 | auto id = |
| 169 | m_sourceModel->data(m_sourceModel->index(mapToSource(idx).row(), MSG_DB_ID_INDEX), Qt::ItemDataRole::EditRole) |
| 170 | .toInt(); |
| 171 | |
| 172 | if (id == msg.m_id) { |
| 173 | return idx; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return QModelIndex(); |
| 178 | } |
| 179 | |
| 180 | QModelIndex MessagesProxyModel::getNextImportantItemIndex(int default_row, int max_row) const { |
| 181 | while (default_row <= max_row) { |
| 182 | // Get info if the message is read or not. |
| 183 | const QModelIndex proxy_index = index(default_row, MSG_DB_IMPORTANT_INDEX); |
| 184 | const bool is_important = |
| 185 | m_sourceModel->data(mapToSource(proxy_index).row(), MSG_DB_IMPORTANT_INDEX, Qt::ItemDataRole::EditRole).toInt() == |
| 186 | 1; |
| 187 | |
| 188 | if (!is_important) { |
| 189 | // We found unread message, mark it. |
| 190 | return proxy_index; |
| 191 | } |
| 192 | else { |
| 193 | default_row++; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return QModelIndex(); |
| 198 | } |
| 199 | |
| 200 | QModelIndex MessagesProxyModel::getNextUnreadItemIndex(int default_row, int max_row) const { |
| 201 | while (default_row <= max_row) { |
| 202 | // Get info if the message is read or not. |
| 203 | const QModelIndex proxy_index = index(default_row, MSG_DB_READ_INDEX); |
| 204 | const bool is_read = |
| 205 | m_sourceModel->data(mapToSource(proxy_index).row(), MSG_DB_READ_INDEX, Qt::ItemDataRole::EditRole).toInt() == 1; |
| 206 | |
| 207 | if (!is_read) { |
| 208 | // We found unread message, mark it. |
| 209 | return proxy_index; |
| 210 | } |
| 211 | else { |
| 212 | default_row++; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return QModelIndex(); |
| 217 | } |
| 218 | |
| 219 | bool MessagesProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const { |
| 220 | Q_UNUSED(left) |
| 221 | Q_UNUSED(right) |
| 222 | |
| 223 | // NOTE: Comparisons are done by SQL server itself, not client-side. |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | bool MessagesProxyModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const { |
| 228 | // We want to show only regexped messages when "all" should be visible |
| 229 | // and we want to show only regexped AND unread messages when unread should be visible. |
| 230 | // |
| 231 | // But also, we want to see messages which have their dirty states cached, because |
| 232 | // otherwise they would just disappear from the list for example when batch marked as read |
| 233 | // which is distracting. |
| 234 | return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent) && |
| 235 | (m_sourceModel->cache()->containsData(source_row) || filterAcceptsMessage(source_row)); |
| 236 | } |
| 237 | |
| 238 | int MessagesProxyModel::additionalArticleId() const { |
| 239 | return m_additionalArticleId; |
| 240 | } |
| 241 | |
| 242 | void MessagesProxyModel::setAdditionalArticleId(int additional_article_id) { |
| 243 | m_additionalArticleId = additional_article_id; |
| 244 | } |
| 245 | |
| 246 | void MessagesProxyModel::setMessageListFilter(MessageListFilter filter) { |
| 247 | m_filter = filter; |
| 248 | m_additionalArticleId = 0; |
| 249 | } |
| 250 | |
| 251 | QModelIndexList MessagesProxyModel::mapListFromSource(const QModelIndexList& indexes, bool deep) const { |
| 252 | QModelIndexList mapped_indexes; |
| 253 | |
| 254 | for (const QModelIndex& index : indexes) { |
| 255 | if (deep) { |
| 256 | // Construct new source index. |
| 257 | mapped_indexes << mapFromSource(m_sourceModel->index(index.row(), index.column())); |
| 258 | } |
| 259 | else { |
| 260 | mapped_indexes << mapFromSource(index); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return mapped_indexes; |
| 265 | } |
| 266 | |
| 267 | QModelIndexList MessagesProxyModel::match(const QModelIndex& start, |
| 268 | int role, |
| 269 | const QVariant& entered_value, |
| 270 | int hits, |
| 271 | Qt::MatchFlags flags) const { |
| 272 | QModelIndexList result; |
| 273 | const int match_type = flags & 0x0F; |
| 274 | const Qt::CaseSensitivity case_sensitivity = Qt::CaseSensitivity::CaseInsensitive; |
| 275 | const bool wrap = Globals::hasFlag(flags, Qt::MatchFlag::MatchWrap); |
| 276 | ; |
| 277 | const bool all_hits = (hits == -1); |
| 278 | QString entered_text; |
| 279 | int from = start.row(); |
| 280 | int to = rowCount(); |
| 281 | |
| 282 | for (int i = 0; (wrap && i < 2) || (!wrap && i < 1); i++) { |
| 283 | for (int r = from; (r < to) && (all_hits || result.count() < hits); r++) { |
| 284 | QModelIndex idx = index(r, start.column()); |
| 285 | |
| 286 | if (!idx.isValid()) { |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | QVariant item_value = m_sourceModel->data(mapToSource(idx).row(), MSG_DB_TITLE_INDEX, role); |
| 291 | |
| 292 | // QVariant based matching. |
| 293 | if (match_type == Qt::MatchExactly) { |
| 294 | if (entered_value == item_value) { |
| 295 | result.append(idx); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // QString based matching. |
| 300 | else { |
| 301 | if (entered_text.isEmpty()) { |
| 302 | entered_text = entered_value.toString(); |
| 303 | } |
| 304 | |
| 305 | QString item_text = item_value.toString(); |
| 306 | |
| 307 | switch (match_type) { |
| 308 | #if QT_VERSION >= 0x050F00 // Qt >= 5.15.0 |
| 309 | case Qt::MatchFlag::MatchRegularExpression: |
| 310 | #else |
| 311 | case Qt::MatchFlag::MatchRegExp: |
| 312 | #endif |
| 313 | if (QRegularExpression(entered_text, |
| 314 | QRegularExpression::PatternOption::CaseInsensitiveOption | |
| 315 | QRegularExpression::PatternOption::UseUnicodePropertiesOption) |
| 316 | .match(item_text) |
| 317 | .hasMatch()) { |
| 318 | result.append(idx); |
| 319 | } |
| 320 | |
| 321 | break; |
| 322 | |
| 323 | case Qt::MatchWildcard: |
| 324 | if (QRegularExpression(RegexFactory::wildcardToRegularExpression(entered_text), |
| 325 | QRegularExpression::PatternOption::CaseInsensitiveOption | |
| 326 | QRegularExpression::PatternOption::UseUnicodePropertiesOption) |
| 327 | .match(item_text) |
| 328 | .hasMatch()) { |
| 329 | result.append(idx); |
| 330 | } |
| 331 | |
| 332 | break; |
| 333 | |
| 334 | case Qt::MatchStartsWith: |
| 335 | if (item_text.startsWith(entered_text, case_sensitivity)) { |
| 336 | result.append(idx); |
| 337 | } |
| 338 | |
| 339 | break; |
| 340 | |
| 341 | case Qt::MatchEndsWith: |
| 342 | if (item_text.endsWith(entered_text, case_sensitivity)) { |
| 343 | result.append(idx); |
| 344 | } |
| 345 | |
| 346 | break; |
| 347 | |
| 348 | case Qt::MatchFixedString: |
| 349 | if (item_text.compare(entered_text, case_sensitivity) == 0) { |
| 350 | result.append(idx); |
| 351 | } |
| 352 | |
| 353 | break; |
| 354 | |
| 355 | case Qt::MatchContains: |
| 356 | default: |
| 357 | if (item_text.contains(entered_text, case_sensitivity)) { |
| 358 | result.append(idx); |
| 359 | } |
| 360 | |
| 361 | break; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | // Prepare for the next iteration. |
| 367 | from = 0; |
| 368 | to = start.row(); |
| 369 | } |
| 370 | |
| 371 | return result; |
| 372 | } |
| 373 | |
| 374 | void MessagesProxyModel::sort(int column, Qt::SortOrder order) { |
| 375 | // NOTE: Ignore here, sort is done elsewhere (server-side). |
| 376 | Q_UNUSED(column) |
| 377 | Q_UNUSED(order) |
| 378 | } |
| 379 | |
| 380 | QModelIndexList MessagesProxyModel::mapListToSource(const QModelIndexList& indexes) const { |
| 381 | QModelIndexList source_indexes; |
| 382 | source_indexes.reserve(indexes.size()); |
| 383 | |
| 384 | for (const QModelIndex& index : indexes) { |
| 385 | source_indexes << mapToSource(index); |
| 386 | } |
| 387 | |
| 388 | return source_indexes; |
| 389 | } |