| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "core/messagesmodel.h" |
| 4 | |
| 5 | #include "3rd-party/boolinq/boolinq.h" |
| 6 | #include "core/messagesmodelcache.h" |
| 7 | #include "database/databasefactory.h" |
| 8 | #include "database/databasequeries.h" |
| 9 | #include "definitions/definitions.h" |
| 10 | #include "definitions/globals.h" |
| 11 | #include "gui/messagesview.h" |
| 12 | #include "miscellaneous/application.h" |
| 13 | #include "miscellaneous/iconfactory.h" |
| 14 | #include "miscellaneous/settings.h" |
| 15 | #include "miscellaneous/skinfactory.h" |
| 16 | #include "miscellaneous/textfactory.h" |
| 17 | #include "services/abstract/recyclebin.h" |
| 18 | #include "services/abstract/serviceroot.h" |
| 19 | |
| 20 | #include <cmath> |
| 21 | |
| 22 | #include <QPainter> |
| 23 | #include <QPainterPath> |
| 24 | #include <QSqlError> |
| 25 | #include <QSqlField> |
| 26 | |
| 27 | #define RAD_COLOR 0, 180, 0 |
| 28 | |
| 29 | MessagesModel::MessagesModel(QObject* parent) |
| 30 | : QSqlQueryModel(parent), m_view(nullptr), m_cache(new MessagesModelCache(this)), |
| 31 | m_messageHighlighter(MessageHighlighter::NoHighlighting), m_customDateFormat(QString()), |
| 32 | m_customTimeFormat(QString()), m_customFormatForDatesOnly(QString()), m_newerArticlesRelativeTime(-1), |
| 33 | m_selectedItem(nullptr), m_unreadIconType(MessageUnreadIcon::Dot), |
| 34 | m_multilineListItems(qApp->settings()->value(GROUP(Messages), SETTING(Messages::MultilineArticleList)).toBool()) { |
| 35 | updateFeedIconsDisplay(); |
| 36 | updateDateFormat(); |
| 37 | |
| 38 | setupFonts(); |
| 39 | setupIcons(); |
| 40 | setupHeaderData(); |
| 41 | loadMessages(nullptr); |
| 42 | } |
| 43 | |
| 44 | MessagesModel::~MessagesModel() { |
| 45 | qDebugNN << LOGSEC_MESSAGEMODEL << "Destroying MessagesModel instance."; |
| 46 | } |
| 47 | |
| 48 | void MessagesModel::setupIcons() { |
| 49 | m_favoriteIcon = qApp->icons()->fromTheme(QSL("mail-mark-important")); |
| 50 | m_readIcon = qApp->icons()->fromTheme(QSL("mail-mark-read")); |
| 51 | m_unreadIcon = m_unreadIconType == MessageUnreadIcon::Dot ? generateUnreadIcon() |
| 52 | : qApp->icons()->fromTheme(QSL("mail-mark-unread")); |
| 53 | m_enclosuresIcon = qApp->icons()->fromTheme(QSL("mail-attachment")); |
| 54 | |
| 55 | for (int i = int(MSG_SCORE_MIN); i <= int(MSG_SCORE_MAX); i += 10) { |
| 56 | m_scoreIcons.append(generateIconForScore(double(i))); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | QIcon MessagesModel::generateIconForScore(double score) { |
| 61 | QPixmap pix(64, 64); |
| 62 | QPainter paint(&pix); |
| 63 | |
| 64 | paint.setRenderHint(QPainter::RenderHint::Antialiasing); |
| 65 | |
| 66 | int level = std::min(MSG_SCORE_MAX, std::max(MSG_SCORE_MIN, std::floor(score / 10.0))); |
| 67 | QPainterPath path; |
| 68 | |
| 69 | path.addRoundedRect(QRectF(2, 2, 60, 60), 5, 5); |
| 70 | |
| 71 | QPen pen(Qt::GlobalColor::black, 2); |
| 72 | |
| 73 | paint.setPen(pen); |
| 74 | paint.fillPath(path, Qt::GlobalColor::white); |
| 75 | paint.drawPath(path); |
| 76 | |
| 77 | #if QT_VERSION >= 0x050D00 // Qt >= 5.13.0 |
| 78 | path.clear(); |
| 79 | #else |
| 80 | path = QPainterPath(); |
| 81 | #endif |
| 82 | |
| 83 | paint.setPen(Qt::GlobalColor::transparent); |
| 84 | |
| 85 | int bar_height = 6 * level; |
| 86 | |
| 87 | path.addRoundedRect(QRectF(2, 64 - bar_height - 2, 60, bar_height), 5, 5); |
| 88 | paint.fillPath(path, QColor::fromHsv(int(score), 200, 230)); |
| 89 | |
| 90 | return pix; |
| 91 | } |
| 92 | |
| 93 | QIcon MessagesModel::generateUnreadIcon() { |
| 94 | QPointF center(64, 64); |
| 95 | qreal radius = 32; |
| 96 | |
| 97 | QColor custom_clr = qApp->skins()->colorForModel(SkinEnums::PaletteColors::FgInteresting).value<QColor>(); |
| 98 | |
| 99 | if (!custom_clr.isValid()) { |
| 100 | custom_clr = QColor(RAD_COLOR); |
| 101 | } |
| 102 | |
| 103 | QRadialGradient grad(center, radius); |
| 104 | grad.setColorAt(0.0, QColor(custom_clr.red(), custom_clr.green(), custom_clr.blue(), 255)); |
| 105 | grad.setColorAt(0.8, QColor(custom_clr.red(), custom_clr.green(), custom_clr.blue(), 0.8 * 255)); |
| 106 | grad.setColorAt(1.0, QColor(custom_clr.red(), custom_clr.green(), custom_clr.blue(), 0.0)); |
| 107 | |
| 108 | QPen pen; |
| 109 | pen.setWidth(96); |
| 110 | pen.setBrush(grad); |
| 111 | |
| 112 | QPixmap pix(128, 128); |
| 113 | pix.fill(Qt::GlobalColor::transparent); |
| 114 | |
| 115 | QPainter painter(&pix); |
| 116 | painter.setRenderHint(QPainter::RenderHint::Antialiasing); |
| 117 | painter.setPen(pen); |
| 118 | painter.drawPoint(center); |
| 119 | |
| 120 | return QIcon(pix); |
| 121 | } |
| 122 | |
| 123 | QString MessagesModel::descriptionOfUnreadIcon(MessageUnreadIcon type) { |
| 124 | switch (type) { |
| 125 | case MessagesModel::MessageUnreadIcon::Dot: |
| 126 | return tr("dot"); |
| 127 | |
| 128 | case MessagesModel::MessageUnreadIcon::Envelope: |
| 129 | return tr("envelope"); |
| 130 | |
| 131 | case MessagesModel::MessageUnreadIcon::FeedIcon: |
| 132 | return tr("feed icon"); |
| 133 | |
| 134 | default: |
| 135 | return QString(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | MessagesView* MessagesModel::view() const { |
| 140 | return m_view; |
| 141 | } |
| 142 | |
| 143 | void MessagesModel::setView(MessagesView* new_view) { |
| 144 | m_view = new_view; |
| 145 | } |
| 146 | |
| 147 | MessagesModelCache* MessagesModel::cache() const { |
| 148 | return m_cache; |
| 149 | } |
| 150 | |
| 151 | void MessagesModel::repopulate(int additional_article_id) { |
| 152 | m_cache->clear(); |
| 153 | |
| 154 | QString statemnt = selectStatement(additional_article_id); |
| 155 | |
| 156 | setQuery(statemnt, m_db); |
| 157 | |
| 158 | if (lastError().isValid()) { |
| 159 | qCriticalNN << LOGSEC_MESSAGEMODEL |
| 160 | << "Error when setting new msg view query:" << QUOTE_W_SPACE_DOT(lastError().text()); |
| 161 | qCriticalNN << LOGSEC_MESSAGEMODEL << "Used SQL select statement:" << QUOTE_W_SPACE_DOT(statemnt); |
| 162 | } |
| 163 | |
| 164 | while (canFetchMore()) { |
| 165 | fetchMore(); |
| 166 | } |
| 167 | |
| 168 | qDebugNN << LOGSEC_MESSAGEMODEL << "Repopulated model, SQL statement is now:\n" << QUOTE_W_SPACE_DOT(statemnt); |
| 169 | } |
| 170 | |
| 171 | bool MessagesModel::setData(const QModelIndex& idx, const QVariant& value, int role) { |
| 172 | Q_UNUSED(role) |
| 173 | m_cache->setData(idx, value); |
| 174 | |
| 175 | emit dataChanged(index(idx.row(), 0), index(idx.row(), MSG_DB_LABELS_IDS)); |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | void MessagesModel::setupFonts() { |
| 180 | QFont fon; |
| 181 | |
| 182 | if (qApp->settings()->value(GROUP(Messages), SETTING(Messages::CustomizeListFont)).toBool()) { |
| 183 | fon.fromString(qApp->settings() |
| 184 | ->value(GROUP(Messages), Messages::ListFont, Application::font("MessagesView").toString()) |
| 185 | .toString()); |
| 186 | } |
| 187 | else { |
| 188 | fon = Application::font("MessagesView"); |
| 189 | } |
| 190 | |
| 191 | m_normalFont = fon; |
| 192 | m_boldFont = m_normalFont; |
| 193 | m_boldFont.setBold(true); |
| 194 | m_normalStrikedFont = m_normalFont; |
| 195 | m_boldStrikedFont = m_boldFont; |
| 196 | m_normalStrikedFont.setStrikeOut(true); |
| 197 | m_boldStrikedFont.setStrikeOut(true); |
| 198 | } |
| 199 | |
| 200 | void MessagesModel::loadMessages(RootItem* item) { |
| 201 | m_selectedItem = item; |
| 202 | |
| 203 | if (item == nullptr) { |
| 204 | setFilter(QSL(DEFAULT_SQL_MESSAGES_FILTER)); |
| 205 | } |
| 206 | else { |
| 207 | if (!item->getParentServiceRoot()->loadMessagesForItem(item, this)) { |
| 208 | setFilter(QSL(DEFAULT_SQL_MESSAGES_FILTER)); |
| 209 | qCriticalNN << LOGSEC_MESSAGEMODEL << "Loading of messages from item '" << item->title() << "' failed."; |
| 210 | qApp->showGuiMessage(Notification::Event::GeneralEvent, |
| 211 | {tr("Loading of articles from item '%1' failed").arg(item->title()), |
| 212 | tr("Loading of articles failed, maybe messages could not be downloaded."), |
| 213 | QSystemTrayIcon::MessageIcon::Critical}); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | repopulate(); |
| 218 | } |
| 219 | |
| 220 | bool MessagesModel::setMessageImportantById(int id, RootItem::Importance important) { |
| 221 | for (int i = 0; i < rowCount(); i++) { |
| 222 | int found_id = data(i, MSG_DB_ID_INDEX, Qt::EditRole).toInt(); |
| 223 | |
| 224 | if (found_id == id) { |
| 225 | bool set = setData(index(i, MSG_DB_IMPORTANT_INDEX), int(important)); |
| 226 | |
| 227 | if (set) { |
| 228 | emit dataChanged(index(i, 0), index(i, MSG_DB_LABELS_IDS)); |
| 229 | } |
| 230 | |
| 231 | return set; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | void MessagesModel::highlightMessages(MessagesModel::MessageHighlighter highlighter) { |
| 239 | m_messageHighlighter = highlighter; |
| 240 | emit layoutAboutToBeChanged(); |
| 241 | emit layoutChanged(); |
| 242 | } |
| 243 | |
| 244 | int MessagesModel::messageId(int row_index) const { |
| 245 | return data(row_index, MSG_DB_ID_INDEX, Qt::EditRole).toInt(); |
| 246 | } |
| 247 | |
| 248 | RootItem::Importance MessagesModel::messageImportance(int row_index) const { |
| 249 | return RootItem::Importance(data(row_index, MSG_DB_IMPORTANT_INDEX, Qt::EditRole).toInt()); |
| 250 | } |
| 251 | |
| 252 | RootItem* MessagesModel::loadedItem() const { |
| 253 | return m_selectedItem; |
| 254 | } |
| 255 | |
| 256 | void MessagesModel::updateDateFormat() { |
| 257 | if (qApp->settings()->value(GROUP(Messages), SETTING(Messages::UseCustomDate)).toBool()) { |
| 258 | m_customDateFormat = qApp->settings()->value(GROUP(Messages), SETTING(Messages::CustomDateFormat)).toString(); |
| 259 | } |
| 260 | else { |
| 261 | m_customDateFormat = QString(); |
| 262 | } |
| 263 | |
| 264 | if (qApp->settings()->value(GROUP(Messages), SETTING(Messages::UseCustomTime)).toBool()) { |
| 265 | m_customTimeFormat = qApp->settings()->value(GROUP(Messages), SETTING(Messages::CustomTimeFormat)).toString(); |
| 266 | } |
| 267 | else { |
| 268 | m_customTimeFormat = QString(); |
| 269 | } |
| 270 | |
| 271 | if (qApp->settings()->value(GROUP(Messages), SETTING(Messages::UseCustomFormatForDatesOnly)).toBool()) { |
| 272 | m_customFormatForDatesOnly = |
| 273 | qApp->settings()->value(GROUP(Messages), SETTING(Messages::CustomFormatForDatesOnly)).toString(); |
| 274 | } |
| 275 | else { |
| 276 | m_customFormatForDatesOnly = QString(); |
| 277 | } |
| 278 | |
| 279 | m_newerArticlesRelativeTime = |
| 280 | qApp->settings()->value(GROUP(Messages), SETTING(Messages::RelativeTimeForNewerArticles)).toInt(); |
| 281 | } |
| 282 | |
| 283 | void MessagesModel::updateFeedIconsDisplay() { |
| 284 | m_unreadIconType = |
| 285 | MessageUnreadIcon(qApp->settings()->value(GROUP(Messages), SETTING(Messages::UnreadIconType)).toInt()); |
| 286 | } |
| 287 | |
| 288 | void MessagesModel::reloadWholeLayout() { |
| 289 | emit layoutAboutToBeChanged(); |
| 290 | emit layoutChanged(); |
| 291 | } |
| 292 | |
| 293 | Message MessagesModel::messageAt(int row_index) const { |
| 294 | return Message::fromSqlRecord(m_cache->containsData(row_index) ? m_cache->record(row_index) : record(row_index)); |
| 295 | } |
| 296 | |
| 297 | void MessagesModel::setupHeaderData() { |
| 298 | m_headerData << |
| 299 | |
| 300 | /*: Tooltip for ID of message.*/ tr("Id") << |
| 301 | /*: Tooltip for "read" column in msg list.*/ tr("Read") << |
| 302 | /*: Tooltip for "important" column in msg list.*/ tr("Important") << |
| 303 | /*: Tooltip for "deleted" column in msg list.*/ tr("Deleted") << |
| 304 | /*: Tooltip for "pdeleted" column in msg list.*/ tr("Permanently deleted") << |
| 305 | /*: Tooltip for custom ID of feed of message.*/ tr("Feed ID") << |
| 306 | /*: Tooltip for title of message.*/ tr("Title") << |
| 307 | /*: Tooltip for url of message.*/ tr("URL") << |
| 308 | /*: Tooltip for author of message.*/ tr("Author") << |
| 309 | /*: Tooltip for creation date of message.*/ tr("Date") << |
| 310 | /*: Tooltip for contents of message.*/ tr("Contents") << |
| 311 | /*: Tooltip for attachments of message.*/ tr("Attachments") << |
| 312 | /*: Tooltip for score of message.*/ tr("Score") << |
| 313 | /*: Tooltip for account ID of message.*/ tr("Account ID") << |
| 314 | /*: Tooltip for custom ID of message.*/ tr("Custom ID") << |
| 315 | /*: Tooltip for custom hash string of message.*/ tr("Custom hash") << |
| 316 | /*: Tooltip for name of feed for message.*/ tr("Feed") << |
| 317 | /*: Tooltip for indication whether article is RTL or not.*/ tr("RTL") << |
| 318 | /*: Tooltip for indication of presence of enclosures.*/ tr("Has enclosures") << |
| 319 | /*: Tooltip for indication of labels of message.*/ tr("Assigned labels") << |
| 320 | /*: Tooltip for indication of label IDs of message.*/ tr("Assigned label IDs"); |
| 321 | |
| 322 | m_tooltipData << tr("ID of the article.") << tr("Is article read?") << tr("Is article important?") |
| 323 | << tr("Is article deleted?") << tr("Is article permanently deleted from recycle bin?") |
| 324 | << tr("ID of feed which this article belongs to.") << tr("Title of the article.") |
| 325 | << tr("Url of the article.") << tr("Author of the article.") << tr("Creation date of the article.") |
| 326 | << tr("Contents of the article.") << tr("List of attachments.") << tr("Score of the article.") |
| 327 | << tr("Account ID of the article.") << tr("Custom ID of the article.") |
| 328 | << tr("Custom hash of the article.") << tr("Name of feed of the article.") |
| 329 | << tr("Layout direction of the article") << tr("Indication of enclosures presence within the article.") |
| 330 | << tr("Labels assigned to the article.") << tr("Label IDs assigned to the article."); |
| 331 | } |
| 332 | |
| 333 | Qt::ItemFlags MessagesModel::flags(const QModelIndex& index) const { |
| 334 | Q_UNUSED(index) |
| 335 | return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemNeverHasChildren; |
| 336 | } |
| 337 | |
| 338 | QList<Message> MessagesModel::messagesAt(const QList<int>& row_indices) const { |
| 339 | QList<Message> msgs; |
| 340 | msgs.reserve(row_indices.size()); |
| 341 | |
| 342 | for (int idx : row_indices) { |
| 343 | msgs << messageAt(idx); |
| 344 | } |
| 345 | |
| 346 | return msgs; |
| 347 | } |
| 348 | |
| 349 | QVariant MessagesModel::data(int row, int column, int role) const { |
| 350 | return data(index(row, column), role); |
| 351 | } |
| 352 | |
| 353 | QVariant MessagesModel::data(const QModelIndex& idx, int role) const { |
| 354 | // This message is not in cache, return real data from live query. |
| 355 | switch (role) { |
| 356 | // Human readable data for viewing. |
| 357 | case Qt::ItemDataRole::DisplayRole: { |
| 358 | int index_column = idx.column(); |
| 359 | |
| 360 | if (index_column == MSG_DB_DCREATED_INDEX) { |
| 361 | QDateTime utc_dt = |
| 362 | TextFactory::parseDateTime(QSqlQueryModel::data(idx, Qt::ItemDataRole::EditRole).value<qint64>()); |
| 363 | QDateTime dt = utc_dt.toLocalTime(); |
| 364 | |
| 365 | if (dt.date() == QDate::currentDate() && !m_customTimeFormat.isEmpty()) { |
| 366 | return dt.toString(m_customTimeFormat); |
| 367 | } |
| 368 | else if (!m_customFormatForDatesOnly.isEmpty() && utc_dt.time().hour() == 0 && utc_dt.time().minute() == 0 && |
| 369 | utc_dt.time().second() == 0) { |
| 370 | return dt.toString(m_customFormatForDatesOnly); |
| 371 | } |
| 372 | else if (m_newerArticlesRelativeTime > 0 && |
| 373 | dt.daysTo(QDateTime::currentDateTime()) <= m_newerArticlesRelativeTime) { |
| 374 | auto secs_difference = dt.secsTo(QDateTime::currentDateTime()); |
| 375 | |
| 376 | if (secs_difference >= 2419200) { |
| 377 | // More than 1 week. |
| 378 | return tr("%n months ago", nullptr, secs_difference / 2419200); |
| 379 | } |
| 380 | else if (secs_difference >= 604800) { |
| 381 | // More than 1 week. |
| 382 | return tr("%n weeks ago", nullptr, secs_difference / 604800); |
| 383 | } |
| 384 | else if (secs_difference >= 172800) { |
| 385 | // At least 2 days. |
| 386 | return tr("%n days ago", nullptr, secs_difference / 86400); |
| 387 | } |
| 388 | else if (secs_difference >= 86400) { |
| 389 | // 1 day. |
| 390 | return tr("yesterday"); |
| 391 | } |
| 392 | else if (secs_difference >= 3600) { |
| 393 | // Less than a day. |
| 394 | return tr("%n hours ago", nullptr, secs_difference / 3600); |
| 395 | } |
| 396 | else if (secs_difference >= 120) { |
| 397 | // Less then 1 hour ago. |
| 398 | return tr("%n minutes ago", nullptr, secs_difference / 60); |
| 399 | } |
| 400 | else { |
| 401 | return tr("just now"); |
| 402 | } |
| 403 | } |
| 404 | else if (m_customDateFormat.isEmpty()) { |
| 405 | return QLocale().toString(dt, QLocale::FormatType::ShortFormat); |
| 406 | } |
| 407 | else { |
| 408 | return dt.toString(m_customDateFormat); |
| 409 | } |
| 410 | } |
| 411 | else if (index_column == MSG_DB_FEED_TITLE_INDEX) { |
| 412 | // Trim feed title. |
| 413 | return data(idx, Qt::ItemDataRole::EditRole).toString().simplified(); |
| 414 | } |
| 415 | else if (index_column == MSG_DB_CONTENTS_INDEX) { |
| 416 | // Do not display full contents here. |
| 417 | QString contents = data(idx, Qt::ItemDataRole::EditRole).toString().mid(0, 64).simplified() + QL1S("..."); |
| 418 | |
| 419 | return contents; |
| 420 | } |
| 421 | else if (index_column == MSG_DB_LABELS_IDS) { |
| 422 | return m_cache->containsData(idx.row()) ? m_cache->data(idx) : QSqlQueryModel::data(idx, role); |
| 423 | } |
| 424 | else if (index_column == MSG_DB_AUTHOR_INDEX) { |
| 425 | const QString author_name = QSqlQueryModel::data(idx, role).toString(); |
| 426 | |
| 427 | return author_name.isEmpty() ? QSL("-") : author_name; |
| 428 | } |
| 429 | else if (index_column != MSG_DB_IMPORTANT_INDEX && index_column != MSG_DB_READ_INDEX && |
| 430 | index_column != MSG_DB_HAS_ENCLOSURES && index_column != MSG_DB_SCORE_INDEX) { |
| 431 | return QSqlQueryModel::data(idx, role); |
| 432 | } |
| 433 | else { |
| 434 | return QVariant(); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | case TEXT_DIRECTION_ROLE: { |
| 439 | int index_column = idx.column(); |
| 440 | |
| 441 | if (index_column != MSG_DB_TITLE_INDEX && index_column != MSG_DB_FEED_TITLE_INDEX && |
| 442 | index_column != MSG_DB_AUTHOR_INDEX) { |
| 443 | return Qt::LayoutDirection::LayoutDirectionAuto; |
| 444 | } |
| 445 | else { |
| 446 | RtlBehavior rtl_mode = |
| 447 | (m_cache->containsData(idx.row()) |
| 448 | ? m_cache->data(index(idx.row(), MSG_DB_FEED_IS_RTL_INDEX)) |
| 449 | : QSqlQueryModel::data(index(idx.row(), MSG_DB_FEED_IS_RTL_INDEX), Qt::ItemDataRole::EditRole)) |
| 450 | .value<RtlBehavior>(); |
| 451 | |
| 452 | return (rtl_mode == RtlBehavior::Everywhere || rtl_mode == RtlBehavior::EverywhereExceptFeedList) |
| 453 | ? Qt::LayoutDirection::RightToLeft |
| 454 | : Qt::LayoutDirection::LayoutDirectionAuto; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | case LOWER_TITLE_ROLE: |
| 459 | return m_cache->containsData(idx.row()) |
| 460 | ? m_cache->data(idx).toString().toLower() |
| 461 | : QSqlQueryModel::data(idx, Qt::ItemDataRole::EditRole).toString().toLower(); |
| 462 | |
| 463 | case Qt::ItemDataRole::EditRole: |
| 464 | return m_cache->containsData(idx.row()) ? m_cache->data(idx) : QSqlQueryModel::data(idx, role); |
| 465 | |
| 466 | case Qt::ItemDataRole::ToolTipRole: { |
| 467 | if (!qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::EnableTooltipsFeedsMessages)).toBool()) { |
| 468 | return QVariant(); |
| 469 | } |
| 470 | else { |
| 471 | if (idx.column() == MSG_DB_SCORE_INDEX) { |
| 472 | return data(idx, Qt::ItemDataRole::EditRole); |
| 473 | } |
| 474 | else if (idx.column() == MSG_DB_URL_INDEX) { |
| 475 | return TextFactory::shorten(data(idx, Qt::ItemDataRole::DisplayRole).toString(), TEXT_TOOLTIP_LIMIT); |
| 476 | } |
| 477 | else if (idx.column() == MSG_DB_DCREATED_INDEX) { |
| 478 | return qApp->localization() |
| 479 | ->loadedLocale() |
| 480 | .toString(QDateTime::fromMSecsSinceEpoch(data(idx, Qt::ItemDataRole::EditRole).value<qint64>()) |
| 481 | .toLocalTime(), |
| 482 | QLocale::FormatType::LongFormat); |
| 483 | } |
| 484 | else if (idx.column() == MSG_DB_READ_INDEX && m_unreadIconType == MessageUnreadIcon::FeedIcon) { |
| 485 | return data(idx.row(), MSG_DB_FEED_TITLE_INDEX, Qt::ItemDataRole::EditRole); |
| 486 | } |
| 487 | else { |
| 488 | return data(idx, Qt::ItemDataRole::DisplayRole); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | case Qt::ItemDataRole::FontRole: { |
| 494 | QModelIndex idx_read = index(idx.row(), MSG_DB_READ_INDEX); |
| 495 | QVariant data_read = data(idx_read, Qt::ItemDataRole::EditRole); |
| 496 | const bool is_bin = qobject_cast<RecycleBin*>(loadedItem()) != nullptr; |
| 497 | bool is_deleted; |
| 498 | |
| 499 | if (is_bin) { |
| 500 | QModelIndex idx_del = index(idx.row(), MSG_DB_PDELETED_INDEX); |
| 501 | |
| 502 | is_deleted = data(idx_del, Qt::ItemDataRole::EditRole).toBool(); |
| 503 | } |
| 504 | else { |
| 505 | QModelIndex idx_del = index(idx.row(), MSG_DB_DELETED_INDEX); |
| 506 | |
| 507 | is_deleted = data(idx_del, Qt::ItemDataRole::EditRole).toBool(); |
| 508 | } |
| 509 | |
| 510 | const bool striked = is_deleted; |
| 511 | |
| 512 | if (data_read.toBool()) { |
| 513 | return striked ? m_normalStrikedFont : m_normalFont; |
| 514 | } |
| 515 | else { |
| 516 | return striked ? m_boldStrikedFont : m_boldFont; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | case Qt::ItemDataRole::ForegroundRole: |
| 521 | case HIGHLIGHTED_FOREGROUND_TITLE_ROLE: { |
| 522 | if (Globals::hasFlag(m_messageHighlighter, MessageHighlighter::HighlightImportant)) { |
| 523 | QModelIndex idx_important = index(idx.row(), MSG_DB_IMPORTANT_INDEX); |
| 524 | QVariant dta = m_cache->containsData(idx_important.row()) ? m_cache->data(idx_important) |
| 525 | : QSqlQueryModel::data(idx_important); |
| 526 | |
| 527 | if (dta.toInt() == 1) { |
| 528 | return qApp->skins()->colorForModel(role == Qt::ItemDataRole::ForegroundRole |
| 529 | ? SkinEnums::PaletteColors::FgInteresting |
| 530 | : SkinEnums::PaletteColors::FgSelectedInteresting); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | if (Globals::hasFlag(m_messageHighlighter, MessageHighlighter::HighlightUnread)) { |
| 535 | QModelIndex idx_read = index(idx.row(), MSG_DB_READ_INDEX); |
| 536 | QVariant dta = m_cache->containsData(idx_read.row()) ? m_cache->data(idx_read) : QSqlQueryModel::data(idx_read); |
| 537 | |
| 538 | if (dta.toInt() == 0) { |
| 539 | return qApp->skins()->colorForModel(role == Qt::ItemDataRole::ForegroundRole |
| 540 | ? SkinEnums::PaletteColors::FgInteresting |
| 541 | : SkinEnums::PaletteColors::FgSelectedInteresting); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | return QVariant(); |
| 546 | } |
| 547 | |
| 548 | case Qt::ItemDataRole::SizeHintRole: { |
| 549 | if (!m_multilineListItems || m_view == nullptr || m_view->isColumnHidden(idx.column()) || |
| 550 | idx.column() != MSG_DB_TITLE_INDEX) { |
| 551 | return {}; |
| 552 | } |
| 553 | else { |
| 554 | auto wd = m_view->columnWidth(idx.column()); |
| 555 | QString str = data(idx, Qt::ItemDataRole::DisplayRole).toString(); |
| 556 | |
| 557 | if (str.simplified().isEmpty()) { |
| 558 | return {}; |
| 559 | } |
| 560 | |
| 561 | QFontMetrics fm(data(idx, Qt::ItemDataRole::FontRole).value<QFont>()); |
| 562 | auto rct = |
| 563 | fm.boundingRect(QRect(QPoint(0, 0), QPoint(wd - 5, 100000)), |
| 564 | Qt::TextFlag::TextWordWrap | Qt::AlignmentFlag::AlignLeft | Qt::AlignmentFlag::AlignVCenter, |
| 565 | str) |
| 566 | .size(); |
| 567 | |
| 568 | return rct; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | case Qt::ItemDataRole::DecorationRole: { |
| 573 | const int index_column = idx.column(); |
| 574 | |
| 575 | if (index_column == MSG_DB_READ_INDEX) { |
| 576 | if (m_unreadIconType == MessageUnreadIcon::FeedIcon && m_selectedItem != nullptr) { |
| 577 | QModelIndex idx_feedid = index(idx.row(), MSG_DB_FEED_CUSTOM_ID_INDEX); |
| 578 | QVariant dta = |
| 579 | m_cache->containsData(idx_feedid.row()) ? m_cache->data(idx_feedid) : QSqlQueryModel::data(idx_feedid); |
| 580 | QString feed_custom_id = dta.toString(); |
| 581 | |
| 582 | // TODO: Very slow and repeats itself. |
| 583 | auto acc = m_selectedItem->getParentServiceRoot()->feedIconForMessage(feed_custom_id); |
| 584 | |
| 585 | if (acc.isNull()) { |
| 586 | return qApp->icons()->fromTheme(QSL("application-rss+xml")); |
| 587 | } |
| 588 | else { |
| 589 | return acc; |
| 590 | } |
| 591 | } |
| 592 | else { |
| 593 | QModelIndex idx_read = index(idx.row(), MSG_DB_READ_INDEX); |
| 594 | QVariant dta = |
| 595 | m_cache->containsData(idx_read.row()) ? m_cache->data(idx_read) : QSqlQueryModel::data(idx_read); |
| 596 | |
| 597 | if (m_unreadIconType == MessageUnreadIcon::Dot) { |
| 598 | return dta.toInt() == 1 ? QVariant() : m_unreadIcon; |
| 599 | } |
| 600 | else { |
| 601 | return dta.toInt() == 1 ? m_readIcon : m_unreadIcon; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | else if (index_column == MSG_DB_IMPORTANT_INDEX) { |
| 606 | QModelIndex idx_important = index(idx.row(), MSG_DB_IMPORTANT_INDEX); |
| 607 | QVariant dta = m_cache->containsData(idx_important.row()) ? m_cache->data(idx_important) |
| 608 | : QSqlQueryModel::data(idx_important); |
| 609 | |
| 610 | return dta.toInt() == 1 ? m_favoriteIcon : QVariant(); |
| 611 | } |
| 612 | else if (index_column == MSG_DB_HAS_ENCLOSURES) { |
| 613 | QModelIndex idx_enc = index(idx.row(), MSG_DB_HAS_ENCLOSURES); |
| 614 | QVariant dta = QSqlQueryModel::data(idx_enc); |
| 615 | |
| 616 | return dta.toBool() ? m_enclosuresIcon : QVariant(); |
| 617 | } |
| 618 | else if (index_column == MSG_DB_SCORE_INDEX) { |
| 619 | QVariant dta = QSqlQueryModel::data(idx); |
| 620 | int level = std::min(MSG_SCORE_MAX, std::max(MSG_SCORE_MIN, std::floor(dta.toDouble() / 10.0))); |
| 621 | |
| 622 | return m_scoreIcons.at(level); |
| 623 | } |
| 624 | else { |
| 625 | return QVariant(); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | default: |
| 630 | return QVariant(); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | bool MessagesModel::switchMessageReadUnread(int row_index) { |
| 635 | RootItem::ReadStatus current_read = |
| 636 | RootItem::ReadStatus(data(row_index, MSG_DB_READ_INDEX, Qt::ItemDataRole::EditRole).toInt()); |
| 637 | |
| 638 | return setMessageRead(row_index, |
| 639 | current_read == RootItem::ReadStatus::Read ? RootItem::ReadStatus::Unread |
| 640 | : RootItem::ReadStatus::Read); |
| 641 | } |
| 642 | |
| 643 | bool MessagesModel::setMessageRead(int row_index, RootItem::ReadStatus read) { |
| 644 | if (data(row_index, MSG_DB_READ_INDEX, Qt::ItemDataRole::EditRole).toInt() == int(read)) { |
| 645 | // Read status is the same is the one currently set. |
| 646 | // In that case, no extra work is needed. |
| 647 | return true; |
| 648 | } |
| 649 | |
| 650 | Message message = messageAt(row_index); |
| 651 | |
| 652 | if (!m_selectedItem->getParentServiceRoot()->onBeforeSetMessagesRead(m_selectedItem, {message}, read)) { |
| 653 | // Cannot change read status of the item. Abort. |
| 654 | return false; |
| 655 | } |
| 656 | |
| 657 | // Rewrite "visible" data in the model. |
| 658 | bool working_change = setData(index(row_index, MSG_DB_READ_INDEX), int(read)); |
| 659 | |
| 660 | if (!working_change) { |
| 661 | // If rewriting in the model failed, then cancel all actions. |
| 662 | qDebugNN << "Setting of new data to the model failed for message read change."; |
| 663 | return false; |
| 664 | } |
| 665 | |
| 666 | if (DatabaseQueries::markMessagesReadUnread(m_db, QStringList() << QString::number(message.m_id), read)) { |
| 667 | return m_selectedItem->getParentServiceRoot()->onAfterSetMessagesRead(m_selectedItem, |
| 668 | QList<Message>() << message, |
| 669 | read); |
| 670 | } |
| 671 | else { |
| 672 | return false; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | bool MessagesModel::setMessageReadById(int id, RootItem::ReadStatus read) { |
| 677 | for (int i = 0; i < rowCount(); i++) { |
| 678 | int found_id = data(i, MSG_DB_ID_INDEX, Qt::ItemDataRole::EditRole).toInt(); |
| 679 | |
| 680 | if (found_id == id) { |
| 681 | bool set = setData(index(i, MSG_DB_READ_INDEX), int(read)); |
| 682 | |
| 683 | if (set) { |
| 684 | emit dataChanged(index(i, 0), index(i, MSG_DB_LABELS_IDS)); |
| 685 | } |
| 686 | |
| 687 | return set; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | return false; |
| 692 | } |
| 693 | |
| 694 | bool MessagesModel::setMessageLabelsById(int id, const QStringList& label_ids) { |
| 695 | for (int i = 0; i < rowCount(); i++) { |
| 696 | int found_id = data(i, MSG_DB_ID_INDEX, Qt::ItemDataRole::EditRole).toInt(); |
| 697 | |
| 698 | if (found_id == id) { |
| 699 | QString enc_ids = label_ids.isEmpty() ? QSL(".") : QSL(".") + label_ids.join('.') + QSL("."); |
| 700 | bool set = setData(index(i, MSG_DB_LABELS_IDS), enc_ids); |
| 701 | |
| 702 | if (set) { |
| 703 | emit dataChanged(index(i, 0), index(i, MSG_DB_LABELS_IDS)); |
| 704 | } |
| 705 | |
| 706 | return set; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | return false; |
| 711 | } |
| 712 | |
| 713 | bool MessagesModel::switchMessageImportance(int row_index) { |
| 714 | const QModelIndex target_index = index(row_index, MSG_DB_IMPORTANT_INDEX); |
| 715 | const RootItem::Importance current_importance = (RootItem::Importance)data(target_index, Qt::EditRole).toInt(); |
| 716 | const RootItem::Importance next_importance = current_importance == RootItem::Importance::Important |
| 717 | ? RootItem::Importance::NotImportant |
| 718 | : RootItem::Importance::Important; |
| 719 | const Message message = messageAt(row_index); |
| 720 | const QPair<Message, RootItem::Importance> pair(message, next_importance); |
| 721 | |
| 722 | if (!m_selectedItem->getParentServiceRoot() |
| 723 | ->onBeforeSwitchMessageImportance(m_selectedItem, QList<QPair<Message, RootItem::Importance>>() << pair)) { |
| 724 | return false; |
| 725 | } |
| 726 | |
| 727 | // Rewrite "visible" data in the model. |
| 728 | const bool working_change = setData(target_index, int(next_importance)); |
| 729 | |
| 730 | if (!working_change) { |
| 731 | // If rewriting in the model failed, then cancel all actions. |
| 732 | qDebugNN << LOGSEC_MESSAGEMODEL << "Setting of new data to the model failed for message importance change."; |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | // Commit changes. |
| 737 | if (DatabaseQueries::markMessageImportant(m_db, message.m_id, next_importance)) { |
| 738 | emit dataChanged(index(row_index, 0), |
| 739 | index(row_index, MSG_DB_FEED_CUSTOM_ID_INDEX), |
| 740 | QVector<int>() << Qt::FontRole); |
| 741 | |
| 742 | return m_selectedItem->getParentServiceRoot() |
| 743 | ->onAfterSwitchMessageImportance(m_selectedItem, QList<QPair<Message, RootItem::Importance>>() << pair); |
| 744 | } |
| 745 | else { |
| 746 | return false; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | bool MessagesModel::switchBatchMessageImportance(const QModelIndexList& messages) { |
| 751 | QStringList message_ids; |
| 752 | message_ids.reserve(messages.size()); |
| 753 | QList<QPair<Message, RootItem::Importance>> message_states; |
| 754 | message_states.reserve(messages.size()); |
| 755 | |
| 756 | // Obtain IDs of all desired messages. |
| 757 | for (const QModelIndex& message : messages) { |
| 758 | const Message msg = messageAt(message.row()); |
| 759 | |
| 760 | RootItem::Importance message_importance = messageImportance((message.row())); |
| 761 | |
| 762 | message_states.append(QPair<Message, RootItem::Importance>(msg, |
| 763 | message_importance == RootItem::Importance::Important |
| 764 | ? RootItem::Importance::NotImportant |
| 765 | : RootItem::Importance::Important)); |
| 766 | message_ids.append(QString::number(msg.m_id)); |
| 767 | QModelIndex idx_msg_imp = index(message.row(), MSG_DB_IMPORTANT_INDEX); |
| 768 | |
| 769 | setData(idx_msg_imp, |
| 770 | message_importance == RootItem::Importance::Important ? int(RootItem::Importance::NotImportant) |
| 771 | : int(RootItem::Importance::Important)); |
| 772 | } |
| 773 | |
| 774 | reloadWholeLayout(); |
| 775 | |
| 776 | if (!m_selectedItem->getParentServiceRoot()->onBeforeSwitchMessageImportance(m_selectedItem, message_states)) { |
| 777 | return false; |
| 778 | } |
| 779 | |
| 780 | if (DatabaseQueries::switchMessagesImportance(m_db, message_ids)) { |
| 781 | return m_selectedItem->getParentServiceRoot()->onAfterSwitchMessageImportance(m_selectedItem, message_states); |
| 782 | } |
| 783 | else { |
| 784 | return false; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | bool MessagesModel::setBatchMessagesDeleted(const QModelIndexList& messages) { |
| 789 | QStringList message_ids; |
| 790 | message_ids.reserve(messages.size()); |
| 791 | QList<Message> msgs; |
| 792 | msgs.reserve(messages.size()); |
| 793 | |
| 794 | // Obtain IDs of all desired messages. |
| 795 | for (const QModelIndex& message : messages) { |
| 796 | const Message msg = messageAt(message.row()); |
| 797 | |
| 798 | msgs.append(msg); |
| 799 | message_ids.append(QString::number(msg.m_id)); |
| 800 | |
| 801 | if (m_selectedItem->kind() == RootItem::Kind::Bin) { |
| 802 | setData(index(message.row(), MSG_DB_PDELETED_INDEX), 1); |
| 803 | } |
| 804 | else { |
| 805 | setData(index(message.row(), MSG_DB_DELETED_INDEX), 1); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | reloadWholeLayout(); |
| 810 | |
| 811 | if (!m_selectedItem->getParentServiceRoot()->onBeforeMessagesDelete(m_selectedItem, msgs)) { |
| 812 | return false; |
| 813 | } |
| 814 | |
| 815 | bool deleted; |
| 816 | |
| 817 | if (m_selectedItem->kind() != RootItem::Kind::Bin) { |
| 818 | deleted = DatabaseQueries::deleteOrRestoreMessagesToFromBin(m_db, message_ids, true); |
| 819 | } |
| 820 | else { |
| 821 | deleted = DatabaseQueries::permanentlyDeleteMessages(m_db, message_ids); |
| 822 | } |
| 823 | |
| 824 | if (deleted) { |
| 825 | return m_selectedItem->getParentServiceRoot()->onAfterMessagesDelete(m_selectedItem, msgs); |
| 826 | } |
| 827 | else { |
| 828 | return false; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | bool MessagesModel::setBatchMessagesRead(const QModelIndexList& messages, RootItem::ReadStatus read) { |
| 833 | QStringList message_ids; |
| 834 | message_ids.reserve(messages.size()); |
| 835 | QList<Message> msgs; |
| 836 | msgs.reserve(messages.size()); |
| 837 | |
| 838 | // Obtain IDs of all desired messages. |
| 839 | for (const QModelIndex& message : messages) { |
| 840 | Message msg = messageAt(message.row()); |
| 841 | |
| 842 | msgs.append(msg); |
| 843 | message_ids.append(QString::number(msg.m_id)); |
| 844 | setData(index(message.row(), MSG_DB_READ_INDEX), int(read)); |
| 845 | } |
| 846 | |
| 847 | reloadWholeLayout(); |
| 848 | |
| 849 | if (!m_selectedItem->getParentServiceRoot()->onBeforeSetMessagesRead(m_selectedItem, msgs, read)) { |
| 850 | return false; |
| 851 | } |
| 852 | |
| 853 | if (DatabaseQueries::markMessagesReadUnread(m_db, message_ids, read)) { |
| 854 | return m_selectedItem->getParentServiceRoot()->onAfterSetMessagesRead(m_selectedItem, msgs, read); |
| 855 | } |
| 856 | else { |
| 857 | return false; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | bool MessagesModel::setBatchMessagesRestored(const QModelIndexList& messages) { |
| 862 | QStringList message_ids; |
| 863 | message_ids.reserve(messages.size()); |
| 864 | QList<Message> msgs; |
| 865 | msgs.reserve(messages.size()); |
| 866 | |
| 867 | // Obtain IDs of all desired messages. |
| 868 | for (const QModelIndex& message : messages) { |
| 869 | const Message msg = messageAt(message.row()); |
| 870 | |
| 871 | msgs.append(msg); |
| 872 | message_ids.append(QString::number(msg.m_id)); |
| 873 | setData(index(message.row(), MSG_DB_PDELETED_INDEX), 0); |
| 874 | setData(index(message.row(), MSG_DB_DELETED_INDEX), 0); |
| 875 | } |
| 876 | |
| 877 | reloadWholeLayout(); |
| 878 | |
| 879 | if (!m_selectedItem->getParentServiceRoot()->onBeforeMessagesRestoredFromBin(m_selectedItem, msgs)) { |
| 880 | return false; |
| 881 | } |
| 882 | |
| 883 | if (DatabaseQueries::deleteOrRestoreMessagesToFromBin(m_db, message_ids, false)) { |
| 884 | return m_selectedItem->getParentServiceRoot()->onAfterMessagesRestoredFromBin(m_selectedItem, msgs); |
| 885 | } |
| 886 | else { |
| 887 | return false; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | QVariant MessagesModel::headerData(int section, Qt::Orientation orientation, int role) const { |
| 892 | Q_UNUSED(orientation) |
| 893 | |
| 894 | switch (role) { |
| 895 | case Qt::DisplayRole: |
| 896 | // Display textual headers for all columns except "read" and |
| 897 | // "important" and "has enclosures" columns. |
| 898 | if (section != MSG_DB_READ_INDEX && section != MSG_DB_IMPORTANT_INDEX && section != MSG_DB_SCORE_INDEX && |
| 899 | section != MSG_DB_HAS_ENCLOSURES) { |
| 900 | return m_headerData.at(section); |
| 901 | } |
| 902 | else { |
| 903 | return QVariant(); |
| 904 | } |
| 905 | |
| 906 | case Qt::ToolTipRole: |
| 907 | return m_tooltipData.at(section); |
| 908 | |
| 909 | case Qt::EditRole: |
| 910 | return m_headerData.at(section); |
| 911 | |
| 912 | // Display icons for "read" and "important" columns. |
| 913 | case Qt::DecorationRole: { |
| 914 | switch (section) { |
| 915 | case MSG_DB_HAS_ENCLOSURES: |
| 916 | return m_enclosuresIcon; |
| 917 | |
| 918 | case MSG_DB_READ_INDEX: |
| 919 | return m_readIcon; |
| 920 | |
| 921 | case MSG_DB_IMPORTANT_INDEX: |
| 922 | return m_favoriteIcon; |
| 923 | |
| 924 | case MSG_DB_SCORE_INDEX: |
| 925 | return m_scoreIcons.at(5); |
| 926 | |
| 927 | default: |
| 928 | return QVariant(); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | default: |
| 933 | return QVariant(); |
| 934 | } |
| 935 | } |