| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "services/abstract/serviceroot.h" |
| 4 | |
| 5 | #include "3rd-party/boolinq/boolinq.h" |
| 6 | #include "core/messagesmodel.h" |
| 7 | #include "database/databasequeries.h" |
| 8 | #include "definitions/globals.h" |
| 9 | #include "exceptions/applicationexception.h" |
| 10 | #include "miscellaneous/application.h" |
| 11 | #include "miscellaneous/iconfactory.h" |
| 12 | #include "miscellaneous/textfactory.h" |
| 13 | #include "services/abstract/cacheforserviceroot.h" |
| 14 | #include "services/abstract/category.h" |
| 15 | #include "services/abstract/feed.h" |
| 16 | #include "services/abstract/gui/custommessagepreviewer.h" |
| 17 | #include "services/abstract/gui/formaccountdetails.h" |
| 18 | #include "services/abstract/gui/formaddeditlabel.h" |
| 19 | #include "services/abstract/gui/formaddeditprobe.h" |
| 20 | #include "services/abstract/gui/formcategorydetails.h" |
| 21 | #include "services/abstract/gui/formfeeddetails.h" |
| 22 | #include "services/abstract/importantnode.h" |
| 23 | #include "services/abstract/labelsnode.h" |
| 24 | #include "services/abstract/recyclebin.h" |
| 25 | #include "services/abstract/search.h" |
| 26 | #include "services/abstract/searchsnode.h" |
| 27 | #include "services/abstract/unreadnode.h" |
| 28 | |
| 29 | ServiceRoot::ServiceRoot(RootItem* parent) |
| 30 | : RootItem(parent), m_recycleBin(new RecycleBin(this)), m_importantNode(new ImportantNode(this)), |
| 31 | m_labelsNode(new LabelsNode(this)), m_probesNode(new SearchsNode(this)), m_unreadNode(new UnreadNode(this)), |
| 32 | m_accountId(NO_PARENT_CATEGORY), m_networkProxy(QNetworkProxy()) { |
| 33 | setKind(RootItem::Kind::ServiceRoot); |
| 34 | appendCommonNodes(); |
| 35 | } |
| 36 | |
| 37 | ServiceRoot::~ServiceRoot() {} |
| 38 | |
| 39 | bool ServiceRoot::deleteItem() { |
| 40 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 41 | |
| 42 | if (DatabaseQueries::deleteAccount(database, this)) { |
| 43 | stop(); |
| 44 | requestItemRemoval(this); |
| 45 | return true; |
| 46 | } |
| 47 | else { |
| 48 | return false; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void ServiceRoot::editItems(const QList<RootItem*>& items) { |
| 53 | // Feed editing. |
| 54 | auto std_feeds = boolinq::from(items) |
| 55 | .select([](RootItem* it) { |
| 56 | return qobject_cast<Feed*>(it); |
| 57 | }) |
| 58 | .where([](Feed* fd) { |
| 59 | return fd != nullptr; |
| 60 | }) |
| 61 | .toStdList(); |
| 62 | |
| 63 | if (!std_feeds.empty()) { |
| 64 | QScopedPointer<FormFeedDetails> form_pointer(new FormFeedDetails(this, qApp->mainFormWidget())); |
| 65 | |
| 66 | form_pointer->addEditFeed<Feed>(FROM_STD_LIST(QList<Feed*>, std_feeds)); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | // Category editing. |
| 71 | auto std_categories = boolinq::from(items) |
| 72 | .select([](RootItem* it) { |
| 73 | return qobject_cast<Category*>(it); |
| 74 | }) |
| 75 | .where([](Category* fd) { |
| 76 | return fd != nullptr; |
| 77 | }) |
| 78 | .toStdList(); |
| 79 | |
| 80 | if (!std_categories.empty()) { |
| 81 | QScopedPointer<FormCategoryDetails> form_pointer(new FormCategoryDetails(this, nullptr, qApp->mainFormWidget())); |
| 82 | |
| 83 | form_pointer->addEditCategory<Category>(FROM_STD_LIST(QList<Category*>, std_categories)); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | // Label editing. |
| 88 | auto std_labels = boolinq::from(items) |
| 89 | .select([](RootItem* it) { |
| 90 | return qobject_cast<Label*>(it); |
| 91 | }) |
| 92 | .where([](Label* fd) { |
| 93 | return fd != nullptr; |
| 94 | }) |
| 95 | .toStdList(); |
| 96 | |
| 97 | if (std_labels.size() == 1) { |
| 98 | // Support editing labels one by one. |
| 99 | FormAddEditLabel form(qApp->mainFormWidget()); |
| 100 | Label* lbl = std_labels.front(); |
| 101 | |
| 102 | if (form.execForEdit(lbl)) { |
| 103 | QSqlDatabase db = qApp->database()->driver()->connection(metaObject()->className()); |
| 104 | |
| 105 | DatabaseQueries::updateLabel(db, lbl); |
| 106 | } |
| 107 | |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | // Probe editing. |
| 112 | auto std_probes = boolinq::from(items) |
| 113 | .select([](RootItem* it) { |
| 114 | return qobject_cast<Search*>(it); |
| 115 | }) |
| 116 | .where([](Search* fd) { |
| 117 | return fd != nullptr; |
| 118 | }) |
| 119 | .toStdList(); |
| 120 | |
| 121 | if (std_probes.size() == 1) { |
| 122 | // Support editing probes one by one. |
| 123 | FormAddEditProbe form(qApp->mainFormWidget()); |
| 124 | Search* probe = std_probes.front(); |
| 125 | |
| 126 | if (form.execForEdit(probe)) { |
| 127 | QSqlDatabase db = qApp->database()->driver()->connection(metaObject()->className()); |
| 128 | |
| 129 | DatabaseQueries::updateProbe(db, probe); |
| 130 | updateCounts(probe); |
| 131 | itemChanged({probe}); |
| 132 | } |
| 133 | |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | qApp->showGuiMessage(Notification::Event::GeneralEvent, |
| 138 | {tr("Unsupported"), tr("This is not suppported (yet)."), QSystemTrayIcon::MessageIcon::Warning}); |
| 139 | } |
| 140 | |
| 141 | bool ServiceRoot::markAsReadUnread(RootItem::ReadStatus status) { |
| 142 | auto* cache = dynamic_cast<CacheForServiceRoot*>(this); |
| 143 | |
| 144 | if (cache != nullptr) { |
| 145 | cache->addMessageStatesToCache(customIDSOfMessagesForItem(this, status), status); |
| 146 | } |
| 147 | |
| 148 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 149 | |
| 150 | if (DatabaseQueries::markAccountReadUnread(database, accountId(), status)) { |
| 151 | updateCounts(false); |
| 152 | itemChanged(getSubTree<RootItem>()); |
| 153 | requestReloadMessageList(status == RootItem::ReadStatus::Read); |
| 154 | return true; |
| 155 | } |
| 156 | else { |
| 157 | return false; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | QList<QAction*> ServiceRoot::addItemMenu() { |
| 162 | return QList<QAction*>(); |
| 163 | } |
| 164 | |
| 165 | RecycleBin* ServiceRoot::recycleBin() const { |
| 166 | return m_recycleBin; |
| 167 | } |
| 168 | |
| 169 | QList<QAction*> ServiceRoot::contextMenuFeedsList() { |
| 170 | auto specific = serviceMenu(); |
| 171 | auto base = RootItem::contextMenuFeedsList(); |
| 172 | |
| 173 | if (!specific.isEmpty()) { |
| 174 | auto* act_sep = new QAction(this); |
| 175 | |
| 176 | act_sep->setSeparator(true); |
| 177 | base.append(act_sep); |
| 178 | base.append(specific); |
| 179 | } |
| 180 | |
| 181 | return base; |
| 182 | } |
| 183 | |
| 184 | QList<QAction*> ServiceRoot::contextMenuMessagesList(const QList<Message>& messages) { |
| 185 | Q_UNUSED(messages) |
| 186 | return {}; |
| 187 | } |
| 188 | |
| 189 | QList<QAction*> ServiceRoot::serviceMenu() { |
| 190 | if (m_serviceMenu.isEmpty()) { |
| 191 | if (isSyncable()) { |
| 192 | auto* act_sync_tree = |
| 193 | new QAction(qApp->icons()->fromTheme(QSL("view-refresh")), tr("Synchronize folders && other items"), this); |
| 194 | |
| 195 | connect(act_sync_tree, &QAction::triggered, this, &ServiceRoot::syncIn); |
| 196 | m_serviceMenu.append(act_sync_tree); |
| 197 | |
| 198 | auto* cache = toCache(); |
| 199 | |
| 200 | if (cache != nullptr) { |
| 201 | auto* act_sync_cache = |
| 202 | new QAction(qApp->icons()->fromTheme(QSL("view-refresh")), tr("Synchronize article cache"), this); |
| 203 | |
| 204 | connect(act_sync_cache, &QAction::triggered, this, [cache]() { |
| 205 | cache->saveAllCachedData(false); |
| 206 | }); |
| 207 | |
| 208 | m_serviceMenu.append(act_sync_cache); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return m_serviceMenu; |
| 214 | } |
| 215 | |
| 216 | bool ServiceRoot::isSyncable() const { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | void ServiceRoot::start(bool freshly_activated) { |
| 221 | Q_UNUSED(freshly_activated) |
| 222 | } |
| 223 | |
| 224 | void ServiceRoot::stop() {} |
| 225 | |
| 226 | CustomMessagePreviewer* ServiceRoot::customMessagePreviewer() { |
| 227 | return nullptr; |
| 228 | } |
| 229 | |
| 230 | void ServiceRoot::updateCounts(bool including_total_count) { |
| 231 | QList<Feed*> feeds; |
| 232 | auto str = getSubTree<RootItem>(); |
| 233 | |
| 234 | for (RootItem* child : std::as_const(str)) { |
| 235 | if (child->kind() == RootItem::Kind::Feed) { |
| 236 | feeds.append(child->toFeed()); |
| 237 | } |
| 238 | else if (child->kind() != RootItem::Kind::Label && child->kind() != RootItem::Kind::Category && |
| 239 | child->kind() != RootItem::Kind::ServiceRoot && child->kind() != RootItem::Kind::Probe) { |
| 240 | child->updateCounts(including_total_count); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (feeds.isEmpty()) { |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 249 | bool ok; |
| 250 | QMap<QString, ArticleCounts> counts = |
| 251 | DatabaseQueries::getMessageCountsForAccount(database, accountId(), including_total_count, &ok); |
| 252 | |
| 253 | if (ok) { |
| 254 | for (Feed* feed : feeds) { |
| 255 | if (counts.contains(feed->customId())) { |
| 256 | feed->setCountOfUnreadMessages(counts.value(feed->customId()).m_unread); |
| 257 | |
| 258 | if (including_total_count) { |
| 259 | feed->setCountOfAllMessages(counts.value(feed->customId()).m_total); |
| 260 | } |
| 261 | } |
| 262 | else { |
| 263 | feed->setCountOfUnreadMessages(0); |
| 264 | |
| 265 | if (including_total_count) { |
| 266 | feed->setCountOfAllMessages(0); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | bool ServiceRoot::canBeDeleted() const { |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | void ServiceRoot::completelyRemoveAllData() { |
| 278 | // Purge old data from SQL and clean all model items. |
| 279 | cleanAllItemsFromModel(true); |
| 280 | removeOldAccountFromDatabase(true, true); |
| 281 | updateCounts(true); |
| 282 | itemChanged({this}); |
| 283 | requestReloadMessageList(true); |
| 284 | } |
| 285 | |
| 286 | QIcon ServiceRoot::feedIconForMessage(const QString& feed_custom_id) const { |
| 287 | QString low_id = feed_custom_id.toLower(); |
| 288 | RootItem* found_item = getItemFromSubTree([low_id](const RootItem* it) { |
| 289 | return it->kind() == RootItem::Kind::Feed && it->customId().toLower() == low_id; |
| 290 | }); |
| 291 | |
| 292 | if (found_item != nullptr) { |
| 293 | return found_item->icon(); |
| 294 | } |
| 295 | else { |
| 296 | return QIcon(); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | void ServiceRoot::removeOldAccountFromDatabase(bool delete_messages_too, bool delete_labels_too) { |
| 301 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 302 | |
| 303 | DatabaseQueries::deleteAccountData(database, accountId(), delete_messages_too, delete_labels_too); |
| 304 | } |
| 305 | |
| 306 | void ServiceRoot::cleanAllItemsFromModel(bool clean_labels_too) { |
| 307 | auto chi = childItems(); |
| 308 | |
| 309 | for (RootItem* top_level_item : std::as_const(chi)) { |
| 310 | if (top_level_item->kind() != RootItem::Kind::Bin && top_level_item->kind() != RootItem::Kind::Important && |
| 311 | top_level_item->kind() != RootItem::Kind::Unread && top_level_item->kind() != RootItem::Kind::Probes && |
| 312 | top_level_item->kind() != RootItem::Kind::Labels) { |
| 313 | requestItemRemoval(top_level_item); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if (labelsNode() != nullptr && clean_labels_too) { |
| 318 | auto lbl_chi = labelsNode()->childItems(); |
| 319 | |
| 320 | for (RootItem* lbl : std::as_const(lbl_chi)) { |
| 321 | requestItemRemoval(lbl); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | void ServiceRoot::appendCommonNodes() { |
| 327 | if (recycleBin() != nullptr && !childItems().contains(recycleBin())) { |
| 328 | appendChild(recycleBin()); |
| 329 | } |
| 330 | |
| 331 | if (importantNode() != nullptr && !childItems().contains(importantNode())) { |
| 332 | appendChild(importantNode()); |
| 333 | } |
| 334 | |
| 335 | if (unreadNode() != nullptr && !childItems().contains(unreadNode())) { |
| 336 | appendChild(unreadNode()); |
| 337 | } |
| 338 | |
| 339 | if (labelsNode() != nullptr && !childItems().contains(labelsNode())) { |
| 340 | appendChild(labelsNode()); |
| 341 | } |
| 342 | |
| 343 | if (probesNode() != nullptr && !childItems().contains(probesNode())) { |
| 344 | appendChild(probesNode()); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | bool ServiceRoot::cleanFeeds(const QList<Feed*>& items, bool clean_read_only) { |
| 349 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 350 | |
| 351 | if (DatabaseQueries::cleanFeeds(database, textualFeedIds(items), clean_read_only, accountId())) { |
| 352 | getParentServiceRoot()->updateCounts(true); |
| 353 | getParentServiceRoot()->itemChanged(getParentServiceRoot()->getSubTree<RootItem>()); |
| 354 | getParentServiceRoot()->requestReloadMessageList(true); |
| 355 | return true; |
| 356 | } |
| 357 | else { |
| 358 | return false; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void ServiceRoot::removeLeftOverMessages() { |
| 363 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 364 | |
| 365 | DatabaseQueries::purgeLeftoverMessages(database, accountId()); |
| 366 | } |
| 367 | |
| 368 | void ServiceRoot::removeLeftOverMessageFilterAssignments() { |
| 369 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 370 | |
| 371 | DatabaseQueries::purgeLeftoverMessageFilterAssignments(database, accountId()); |
| 372 | } |
| 373 | |
| 374 | QList<Message> ServiceRoot::undeletedMessages() const { |
| 375 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 376 | |
| 377 | return DatabaseQueries::getUndeletedMessagesForAccount(database, accountId()); |
| 378 | } |
| 379 | |
| 380 | bool ServiceRoot::supportsFeedAdding() const { |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | bool ServiceRoot::supportsCategoryAdding() const { |
| 385 | return false; |
| 386 | } |
| 387 | |
| 388 | ServiceRoot::LabelOperation ServiceRoot::supportedLabelOperations() const { |
| 389 | return LabelOperation::Adding | LabelOperation::Editing | LabelOperation::Deleting; |
| 390 | } |
| 391 | |
| 392 | QString ServiceRoot::additionalTooltip() const { |
| 393 | return tr("Number of feeds: %1\n" |
| 394 | "Number of categories: %2\n" |
| 395 | "Number of disabled feeds: %3") |
| 396 | .arg(QString::number(getSubTreeFeeds().size()), |
| 397 | QString::number(getSubTreeCategories().size()), |
| 398 | QString::number(getSubTree<RootItem>([](const RootItem* ri) { |
| 399 | return ri->kind() == RootItem::Kind::Feed && ri->toFeed()->isSwitchedOff(); |
| 400 | }).size())); |
| 401 | } |
| 402 | |
| 403 | void ServiceRoot::saveAccountDataToDatabase() { |
| 404 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 405 | |
| 406 | try { |
| 407 | DatabaseQueries::createOverwriteAccount(database, this); |
| 408 | } |
| 409 | catch (const ApplicationException& ex) { |
| 410 | qFatal("Account was not saved into database: '%s'.", qPrintable(ex.message())); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | QVariantHash ServiceRoot::customDatabaseData() const { |
| 415 | return {{QSL("show_node_unread"), m_nodeShowUnread}, |
| 416 | {QSL("show_node_important"), m_nodeShowImportant}, |
| 417 | {QSL("show_node_labels"), m_nodeShowLabels}, |
| 418 | {QSL("show_node_probes"), m_nodeShowProbes}}; |
| 419 | } |
| 420 | |
| 421 | void ServiceRoot::setCustomDatabaseData(const QVariantHash& data) { |
| 422 | m_nodeShowUnread = data.value(QSL("show_node_unread"), true).toBool(); |
| 423 | m_nodeShowImportant = data.value(QSL("show_node_important"), true).toBool(); |
| 424 | m_nodeShowLabels = data.value(QSL("show_node_labels"), true).toBool(); |
| 425 | m_nodeShowProbes = data.value(QSL("show_node_probes"), true).toBool(); |
| 426 | } |
| 427 | |
| 428 | bool ServiceRoot::wantsBaggedIdsOfExistingMessages() const { |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | bool ServiceRoot::displaysEnclosures() const { |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | void ServiceRoot::aboutToBeginFeedFetching(const QList<Feed*>& feeds, |
| 437 | const QHash<QString, QHash<BagOfMessages, QStringList>>& stated_messages, |
| 438 | const QHash<QString, QStringList>& tagged_messages) { |
| 439 | Q_UNUSED(feeds) |
| 440 | Q_UNUSED(stated_messages) |
| 441 | Q_UNUSED(tagged_messages) |
| 442 | } |
| 443 | |
| 444 | void ServiceRoot::itemChanged(const QList<RootItem*>& items) { |
| 445 | emit dataChanged(items); |
| 446 | } |
| 447 | |
| 448 | void ServiceRoot::requestReloadMessageList(bool mark_selected_messages_read) { |
| 449 | emit reloadMessageListRequested(mark_selected_messages_read); |
| 450 | } |
| 451 | |
| 452 | void ServiceRoot::requestItemExpand(const QList<RootItem*>& items, bool expand) { |
| 453 | emit itemExpandRequested(items, expand); |
| 454 | } |
| 455 | |
| 456 | void ServiceRoot::requestItemExpandStateSave(RootItem* subtree_root) { |
| 457 | emit itemExpandStateSaveRequested(subtree_root); |
| 458 | } |
| 459 | |
| 460 | void ServiceRoot::requestItemReassignment(RootItem* item, RootItem* new_parent) { |
| 461 | emit itemReassignmentRequested(item, new_parent); |
| 462 | } |
| 463 | |
| 464 | void ServiceRoot::requestItemsReassignment(const QList<RootItem*>& items, RootItem* new_parent) { |
| 465 | for (RootItem* it : items) { |
| 466 | requestItemReassignment(it, new_parent); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | void ServiceRoot::requestItemRemoval(RootItem* item) { |
| 471 | emit itemRemovalRequested(item); |
| 472 | } |
| 473 | |
| 474 | void ServiceRoot::addNewFeed(RootItem* selected_item, const QString& url) { |
| 475 | Q_UNUSED(selected_item) |
| 476 | Q_UNUSED(url) |
| 477 | } |
| 478 | |
| 479 | void ServiceRoot::addNewCategory(RootItem* selected_item) { |
| 480 | Q_UNUSED(selected_item) |
| 481 | } |
| 482 | |
| 483 | QMap<QString, QVariantMap> ServiceRoot::storeCustomFeedsData() { |
| 484 | QMap<QString, QVariantMap> custom_data; |
| 485 | auto str = getSubTreeFeeds(); |
| 486 | |
| 487 | for (const Feed* feed : std::as_const(str)) { |
| 488 | QVariantMap feed_custom_data; |
| 489 | |
| 490 | // TODO: This could potentially call Feed::customDatabaseData() and append it |
| 491 | // to this map and also subsequently restore, but the method is at this point |
| 492 | // not really used by any syncable plugin. |
| 493 | feed_custom_data.insert(QSL("auto_update_interval"), feed->autoUpdateInterval()); |
| 494 | feed_custom_data.insert(QSL("auto_update_type"), int(feed->autoUpdateType())); |
| 495 | feed_custom_data.insert(QSL("msg_filters"), QVariant::fromValue(feed->messageFilters())); |
| 496 | feed_custom_data.insert(QSL("is_off"), feed->isSwitchedOff()); |
| 497 | feed_custom_data.insert(QSL("is_quiet"), feed->isQuiet()); |
| 498 | feed_custom_data.insert(QSL("open_articles_directly"), feed->openArticlesDirectly()); |
| 499 | feed_custom_data.insert(QSL("is_rtl"), QVariant::fromValue(feed->rtlBehavior())); |
| 500 | |
| 501 | feed_custom_data.insert(QSL("article_limit_ignore"), QVariant::fromValue(feed->articleIgnoreLimit())); |
| 502 | |
| 503 | /* |
| 504 | feed_custom_data.insert(QSL("datetime_to_avoid"), |
| 505 | (feed->datetimeToAvoid().isValid() && feed->datetimeToAvoid().toMSecsSinceEpoch() > 0) |
| 506 | ? feed->datetimeToAvoid().toMSecsSinceEpoch() |
| 507 | : feed->hoursToAvoid()); |
| 508 | */ |
| 509 | |
| 510 | // NOTE: This is here specifically to be able to restore custom sort order. |
| 511 | // Otherwise the information is lost when list of feeds/folders is refreshed from remote |
| 512 | // service. |
| 513 | feed_custom_data.insert(QSL("sort_order"), feed->sortOrder()); |
| 514 | |
| 515 | custom_data.insert(feed->customId(), feed_custom_data); |
| 516 | } |
| 517 | |
| 518 | return custom_data; |
| 519 | } |
| 520 | |
| 521 | QMap<QString, QVariantMap> ServiceRoot::storeCustomCategoriesData() { |
| 522 | QMap<QString, QVariantMap> custom_data; |
| 523 | auto str = getSubTreeCategories(); |
| 524 | |
| 525 | for (const Category* cat : std::as_const(str)) { |
| 526 | QVariantMap cat_custom_data; |
| 527 | |
| 528 | // NOTE: This is here specifically to be able to restore custom sort order. |
| 529 | // Otherwise the information is lost when list of feeds/folders is refreshed from remote |
| 530 | // service. |
| 531 | cat_custom_data.insert(QSL("sort_order"), cat->sortOrder()); |
| 532 | |
| 533 | custom_data.insert(cat->customId(), cat_custom_data); |
| 534 | } |
| 535 | |
| 536 | return custom_data; |
| 537 | } |
| 538 | |
| 539 | void ServiceRoot::restoreCustomFeedsData(const QMap<QString, QVariantMap>& data, const QHash<QString, Feed*>& feeds) { |
| 540 | QMapIterator<QString, QVariantMap> i(data); |
| 541 | |
| 542 | while (i.hasNext()) { |
| 543 | i.next(); |
| 544 | const QString custom_id = i.key(); |
| 545 | |
| 546 | if (feeds.contains(custom_id)) { |
| 547 | Feed* feed = feeds.value(custom_id); |
| 548 | QVariantMap feed_custom_data = i.value(); |
| 549 | |
| 550 | feed->setAutoUpdateInterval(feed_custom_data.value(QSL("auto_update_interval")).toInt()); |
| 551 | feed |
| 552 | ->setAutoUpdateType(static_cast<Feed::AutoUpdateType>(feed_custom_data.value(QSL("auto_update_type")).toInt())); |
| 553 | feed->setMessageFilters(feed_custom_data.value(QSL("msg_filters")).value<QList<QPointer<MessageFilter>>>()); |
| 554 | |
| 555 | feed->setIsSwitchedOff(feed_custom_data.value(QSL("is_off")).toBool()); |
| 556 | feed->setIsQuiet(feed_custom_data.value(QSL("is_quiet")).toBool()); |
| 557 | feed->setOpenArticlesDirectly(feed_custom_data.value(QSL("open_articles_directly")).toBool()); |
| 558 | feed->setRtlBehavior(feed_custom_data.value(QSL("is_rtl")).value<RtlBehavior>()); |
| 559 | |
| 560 | feed |
| 561 | ->setArticleIgnoreLimit(feed_custom_data.value(QSL("article_limit_ignore")).value<Feed::ArticleIgnoreLimit>()); |
| 562 | |
| 563 | /* |
| 564 | feed->setAddAnyDatetimeArticles(feed_custom_data.value(QSL("add_any_datetime_articles")).toBool()); |
| 565 | |
| 566 | qint64 time_to_avoid = feed_custom_data.value(QSL("datetime_to_avoid")).value<qint64>(); |
| 567 | |
| 568 | if (time_to_avoid > 10000) { |
| 569 | feed->setDatetimeToAvoid(TextFactory::parseDateTime(time_to_avoid)); |
| 570 | } |
| 571 | else { |
| 572 | feed->setHoursToAvoid(time_to_avoid); |
| 573 | } |
| 574 | */ |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | void ServiceRoot::restoreCustomCategoriesData(const QMap<QString, QVariantMap>& data, |
| 580 | const QHash<QString, Category*>& cats) { |
| 581 | Q_UNUSED(data) |
| 582 | Q_UNUSED(cats) |
| 583 | } |
| 584 | |
| 585 | bool ServiceRoot::nodeShowProbes() const { |
| 586 | return m_nodeShowProbes; |
| 587 | } |
| 588 | |
| 589 | void ServiceRoot::setNodeShowProbes(bool enabled) { |
| 590 | m_nodeShowProbes = enabled; |
| 591 | } |
| 592 | |
| 593 | bool ServiceRoot::nodeShowLabels() const { |
| 594 | return m_nodeShowLabels; |
| 595 | } |
| 596 | |
| 597 | void ServiceRoot::setNodeShowLabels(bool enabled) { |
| 598 | m_nodeShowLabels = enabled; |
| 599 | } |
| 600 | |
| 601 | bool ServiceRoot::nodeShowImportant() const { |
| 602 | return m_nodeShowImportant; |
| 603 | } |
| 604 | |
| 605 | void ServiceRoot::setNodeShowImportant(bool enabled) { |
| 606 | m_nodeShowImportant = enabled; |
| 607 | } |
| 608 | |
| 609 | bool ServiceRoot::nodeShowUnread() const { |
| 610 | return m_nodeShowUnread; |
| 611 | } |
| 612 | |
| 613 | void ServiceRoot::setNodeShowUnread(bool enabled) { |
| 614 | m_nodeShowUnread = enabled; |
| 615 | } |
| 616 | |
| 617 | QNetworkProxy ServiceRoot::networkProxy() const { |
| 618 | return m_networkProxy; |
| 619 | } |
| 620 | |
| 621 | void ServiceRoot::setNetworkProxy(const QNetworkProxy& network_proxy) { |
| 622 | m_networkProxy = network_proxy; |
| 623 | |
| 624 | emit proxyChanged(network_proxy); |
| 625 | } |
| 626 | |
| 627 | ImportantNode* ServiceRoot::importantNode() const { |
| 628 | return m_importantNode; |
| 629 | } |
| 630 | |
| 631 | LabelsNode* ServiceRoot::labelsNode() const { |
| 632 | return m_labelsNode; |
| 633 | } |
| 634 | |
| 635 | SearchsNode* ServiceRoot::probesNode() const { |
| 636 | return m_probesNode; |
| 637 | } |
| 638 | |
| 639 | UnreadNode* ServiceRoot::unreadNode() const { |
| 640 | return m_unreadNode; |
| 641 | } |
| 642 | |
| 643 | FormAccountDetails* ServiceRoot::accountSetupDialog() const { |
| 644 | return nullptr; |
| 645 | } |
| 646 | |
| 647 | void ServiceRoot::onDatabaseCleanup() {} |
| 648 | |
| 649 | void ServiceRoot::syncIn() { |
| 650 | QIcon original_icon = icon(); |
| 651 | |
| 652 | setIcon(qApp->icons()->fromTheme(QSL("view-refresh"))); |
| 653 | itemChanged({this}); |
| 654 | |
| 655 | try { |
| 656 | qDebugNN << LOGSEC_CORE << "Starting sync-in process."; |
| 657 | |
| 658 | RootItem* new_tree = obtainNewTreeForSyncIn(); |
| 659 | |
| 660 | qDebugNN << LOGSEC_CORE << "New feed tree for sync-in obtained."; |
| 661 | |
| 662 | auto feed_custom_data = storeCustomFeedsData(); |
| 663 | auto categories_custom_data = storeCustomCategoriesData(); |
| 664 | |
| 665 | // Remove from feeds model, then from SQL but leave messages intact. |
| 666 | bool uses_remote_labels = Globals::hasFlag(supportedLabelOperations(), LabelOperation::Synchronised); |
| 667 | |
| 668 | // Remove stuff. |
| 669 | cleanAllItemsFromModel(uses_remote_labels); |
| 670 | removeOldAccountFromDatabase(false, uses_remote_labels); |
| 671 | |
| 672 | // Re-sort items to accomodate current sort order. |
| 673 | resortAccountTree(new_tree, categories_custom_data, feed_custom_data); |
| 674 | |
| 675 | // Restore some local settings to feeds etc. |
| 676 | restoreCustomCategoriesData(categories_custom_data, new_tree->getHashedSubTreeCategories()); |
| 677 | restoreCustomFeedsData(feed_custom_data, new_tree->getHashedSubTreeFeeds()); |
| 678 | |
| 679 | // Model is clean, now store new tree into DB and |
| 680 | // set primary IDs of the items. |
| 681 | DatabaseQueries::storeAccountTree(qApp->database()->driver()->connection(metaObject()->className()), |
| 682 | new_tree, |
| 683 | accountId()); |
| 684 | |
| 685 | // We have new feed, some feeds were maybe removed, |
| 686 | // so remove left over messages and filter assignments. |
| 687 | removeLeftOverMessages(); |
| 688 | removeLeftOverMessageFilterAssignments(); |
| 689 | |
| 690 | auto chi = new_tree->childItems(); |
| 691 | |
| 692 | for (RootItem* top_level_item : std::as_const(chi)) { |
| 693 | if (top_level_item->kind() != Kind::Labels) { |
| 694 | top_level_item->setParent(nullptr); |
| 695 | requestItemReassignment(top_level_item, this); |
| 696 | } |
| 697 | else { |
| 698 | // It seems that some labels got synced-in. |
| 699 | if (labelsNode() != nullptr) { |
| 700 | auto lbl_chi = top_level_item->childItems(); |
| 701 | |
| 702 | for (RootItem* new_lbl : std::as_const(lbl_chi)) { |
| 703 | new_lbl->setParent(nullptr); |
| 704 | requestItemReassignment(new_lbl, labelsNode()); |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | new_tree->clearChildren(); |
| 711 | new_tree->deleteLater(); |
| 712 | |
| 713 | updateCounts(true); |
| 714 | requestReloadMessageList(true); |
| 715 | } |
| 716 | catch (const ApplicationException& ex) { |
| 717 | qCriticalNN << LOGSEC_CORE << "New feed tree for sync-in NOT obtained:" << QUOTE_W_SPACE_DOT(ex.message()); |
| 718 | |
| 719 | qApp->showGuiMessage(Notification::Event::GeneralEvent, |
| 720 | GuiMessage(tr("Error when fetching list of feeds"), |
| 721 | tr("Feeds & categories for account '%1' were not fetched, error: %2") |
| 722 | .arg(title(), ex.message()), |
| 723 | QSystemTrayIcon::MessageIcon::Critical), |
| 724 | GuiMessageDestination(true, true)); |
| 725 | } |
| 726 | |
| 727 | setIcon(original_icon); |
| 728 | itemChanged(getSubTree<RootItem>()); |
| 729 | requestItemExpand(getSubTree<RootItem>(), true); |
| 730 | } |
| 731 | |
| 732 | void ServiceRoot::performInitialAssembly(const Assignment& categories, |
| 733 | const Assignment& feeds, |
| 734 | const QList<Label*>& labels, |
| 735 | const QList<Search*>& probes) { |
| 736 | assembleCategories(categories); |
| 737 | assembleFeeds(feeds); |
| 738 | labelsNode()->loadLabels(labels); |
| 739 | probesNode()->loadProbes(probes); |
| 740 | |
| 741 | updateCounts(true); |
| 742 | } |
| 743 | |
| 744 | RootItem* ServiceRoot::obtainNewTreeForSyncIn() const { |
| 745 | return nullptr; |
| 746 | } |
| 747 | |
| 748 | QStringList ServiceRoot::customIDSOfMessagesForItem(RootItem* item, ReadStatus target_read) { |
| 749 | if (item->getParentServiceRoot() != this) { |
| 750 | // Not item from this account. |
| 751 | return {}; |
| 752 | } |
| 753 | else { |
| 754 | QStringList list; |
| 755 | |
| 756 | switch (item->kind()) { |
| 757 | case RootItem::Kind::Labels: |
| 758 | case RootItem::Kind::Probes: |
| 759 | case RootItem::Kind::Category: { |
| 760 | auto chi = item->childItems(); |
| 761 | |
| 762 | for (RootItem* child : std::as_const(chi)) { |
| 763 | list.append(customIDSOfMessagesForItem(child, target_read)); |
| 764 | } |
| 765 | |
| 766 | return list; |
| 767 | } |
| 768 | |
| 769 | case RootItem::Kind::Label: { |
| 770 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 771 | |
| 772 | list = DatabaseQueries::customIdsOfMessagesFromLabel(database, item->toLabel(), target_read); |
| 773 | break; |
| 774 | } |
| 775 | |
| 776 | case RootItem::Kind::Probe: { |
| 777 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 778 | |
| 779 | list = DatabaseQueries::customIdsOfMessagesFromProbe(database, item->toProbe(), target_read); |
| 780 | break; |
| 781 | } |
| 782 | |
| 783 | case RootItem::Kind::ServiceRoot: { |
| 784 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 785 | |
| 786 | list = DatabaseQueries::customIdsOfMessagesFromAccount(database, target_read, accountId()); |
| 787 | break; |
| 788 | } |
| 789 | |
| 790 | case RootItem::Kind::Bin: { |
| 791 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 792 | |
| 793 | list = DatabaseQueries::customIdsOfMessagesFromBin(database, target_read, accountId()); |
| 794 | break; |
| 795 | } |
| 796 | |
| 797 | case RootItem::Kind::Feed: { |
| 798 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 799 | |
| 800 | list = DatabaseQueries::customIdsOfMessagesFromFeed(database, item->customId(), target_read, accountId()); |
| 801 | break; |
| 802 | } |
| 803 | |
| 804 | case RootItem::Kind::Important: { |
| 805 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 806 | |
| 807 | list = DatabaseQueries::customIdsOfImportantMessages(database, target_read, accountId()); |
| 808 | break; |
| 809 | } |
| 810 | |
| 811 | case RootItem::Kind::Unread: { |
| 812 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 813 | |
| 814 | list = DatabaseQueries::customIdsOfUnreadMessages(database, accountId()); |
| 815 | break; |
| 816 | } |
| 817 | |
| 818 | default: |
| 819 | break; |
| 820 | } |
| 821 | |
| 822 | qDebugNN << LOGSEC_CORE << "Custom IDs of messages for some operation are:" << QUOTE_W_SPACE_DOT(list); |
| 823 | return list; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | bool ServiceRoot::markFeedsReadUnread(const QList<Feed*>& items, RootItem::ReadStatus read) { |
| 828 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 829 | |
| 830 | if (DatabaseQueries::markFeedsReadUnread(database, textualFeedIds(items), accountId(), read)) { |
| 831 | getParentServiceRoot()->updateCounts(false); |
| 832 | getParentServiceRoot()->itemChanged(getParentServiceRoot()->getSubTree<RootItem>()); |
| 833 | getParentServiceRoot()->requestReloadMessageList(read == RootItem::ReadStatus::Read); |
| 834 | return true; |
| 835 | } |
| 836 | else { |
| 837 | return false; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | QStringList ServiceRoot::textualFeedUrls(const QList<Feed*>& feeds) const { |
| 842 | QStringList stringy_urls; |
| 843 | |
| 844 | stringy_urls.reserve(feeds.size()); |
| 845 | |
| 846 | for (const Feed* feed : feeds) { |
| 847 | stringy_urls.append(!feed->source().isEmpty() ? feed->source() : QSL("no-url")); |
| 848 | } |
| 849 | |
| 850 | return stringy_urls; |
| 851 | } |
| 852 | |
| 853 | QStringList ServiceRoot::textualFeedIds(const QList<Feed*>& feeds) const { |
| 854 | QStringList stringy_ids; |
| 855 | stringy_ids.reserve(feeds.size()); |
| 856 | |
| 857 | for (const Feed* feed : feeds) { |
| 858 | stringy_ids.append(QSL("'%1'").arg(feed->customId())); |
| 859 | } |
| 860 | |
| 861 | return stringy_ids; |
| 862 | } |
| 863 | |
| 864 | QStringList ServiceRoot::customIDsOfMessages(const QList<ImportanceChange>& changes) { |
| 865 | QSet<QString> list; |
| 866 | list.reserve(changes.size()); |
| 867 | |
| 868 | for (const auto& change : changes) { |
| 869 | list.insert(change.first.m_customId); |
| 870 | } |
| 871 | |
| 872 | return list.values(); |
| 873 | } |
| 874 | |
| 875 | QStringList ServiceRoot::customIDsOfMessages(const QList<Message>& messages) { |
| 876 | QSet<QString> list; |
| 877 | list.reserve(messages.size()); |
| 878 | |
| 879 | for (const Message& message : messages) { |
| 880 | list.insert(message.m_customId); |
| 881 | } |
| 882 | |
| 883 | return list.values(); |
| 884 | } |
| 885 | |
| 886 | int ServiceRoot::accountId() const { |
| 887 | return m_accountId; |
| 888 | } |
| 889 | |
| 890 | void ServiceRoot::setAccountId(int account_id) { |
| 891 | m_accountId = account_id; |
| 892 | |
| 893 | auto* cache = dynamic_cast<CacheForServiceRoot*>(this); |
| 894 | |
| 895 | if (cache != nullptr) { |
| 896 | cache->setUniqueId(account_id); |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | bool ServiceRoot::loadMessagesForItem(RootItem* item, MessagesModel* model) { |
| 901 | if (item->kind() == RootItem::Kind::Bin) { |
| 902 | model->setFilter(QSL("Messages.is_deleted = 1 AND Messages.is_pdeleted = 0 AND Messages.account_id = %1") |
| 903 | .arg(QString::number(accountId()))); |
| 904 | } |
| 905 | else if (item->kind() == RootItem::Kind::Important) { |
| 906 | model->setFilter(QSL("Messages.is_important = 1 AND Messages.is_deleted = 0 AND Messages.is_pdeleted = 0 AND " |
| 907 | "Messages.account_id = %1") |
| 908 | .arg(QString::number(accountId()))); |
| 909 | } |
| 910 | else if (item->kind() == RootItem::Kind::Unread) { |
| 911 | model->setFilter(QSL("Messages.is_read = 0 AND Messages.is_deleted = 0 AND Messages.is_pdeleted = 0 AND " |
| 912 | "Messages.account_id = %1") |
| 913 | .arg(QString::number(accountId()))); |
| 914 | } |
| 915 | else if (item->kind() == RootItem::Kind::Probe) { |
| 916 | item->updateCounts(true); |
| 917 | itemChanged({item}); |
| 918 | |
| 919 | model->setFilter(QSL("Messages.is_deleted = 0 AND Messages.is_pdeleted = 0 AND Messages.account_id = %1 AND " |
| 920 | "(Messages.title REGEXP '%2' OR Messages.contents REGEXP '%2')") |
| 921 | .arg(QString::number(accountId()), item->toProbe()->filter())); |
| 922 | } |
| 923 | else if (item->kind() == RootItem::Kind::Label) { |
| 924 | // Show messages with particular label. |
| 925 | model->setFilter(QSL("Messages.is_deleted = 0 AND Messages.is_pdeleted = 0 AND " |
| 926 | "Messages.labels LIKE '%.%2.%' AND Messages.account_id = %1") |
| 927 | .arg(QString::number(accountId()), item->customId())); |
| 928 | } |
| 929 | else if (item->kind() == RootItem::Kind::Labels) { |
| 930 | // Show messages with any label. |
| 931 | model->setFilter(QSL("Messages.is_deleted = 0 AND Messages.is_pdeleted = 0 AND " |
| 932 | "LENGTH(Messages.labels) > 2 AND Messages.account_id = %1") |
| 933 | .arg(QString::number(accountId()))); |
| 934 | } |
| 935 | else if (item->kind() == RootItem::Kind::ServiceRoot) { |
| 936 | model->setFilter(QSL("Messages.is_deleted = 0 AND Messages.is_pdeleted = 0 AND Messages.account_id = %1") |
| 937 | .arg(QString::number(accountId()))); |
| 938 | |
| 939 | qDebugNN << LOGSEC_CORE << "Displaying messages from account:" << QUOTE_W_SPACE_DOT(accountId()); |
| 940 | } |
| 941 | else if (item->kind() == RootItem::Kind::Probes) { |
| 942 | model->setFilter(QSL(DEFAULT_SQL_MESSAGES_FILTER)); |
| 943 | |
| 944 | qWarningNN << LOGSEC_CORE << "Showing of all regex queries combined is not supported."; |
| 945 | } |
| 946 | else { |
| 947 | QList<Feed*> children = item->getSubTreeFeeds(); |
| 948 | QString filter_clause = textualFeedIds(children).join(QSL(", ")); |
| 949 | |
| 950 | if (filter_clause.isEmpty()) { |
| 951 | filter_clause = QSL("null"); |
| 952 | } |
| 953 | |
| 954 | model->setFilter(QSL("Feeds.custom_id IN (%1) AND Messages.is_deleted = 0 AND Messages.is_pdeleted = 0 AND " |
| 955 | "Messages.account_id = %2") |
| 956 | .arg(filter_clause, QString::number(accountId()))); |
| 957 | |
| 958 | QString urls = textualFeedUrls(children).join(QSL(", ")); |
| 959 | |
| 960 | qDebugNN << LOGSEC_CORE << "Displaying messages from feeds IDs:" << QUOTE_W_SPACE(filter_clause) |
| 961 | << "and URLs:" << QUOTE_W_SPACE_DOT(urls); |
| 962 | } |
| 963 | |
| 964 | return true; |
| 965 | } |
| 966 | |
| 967 | bool ServiceRoot::onBeforeSetMessagesRead(RootItem* selected_item, |
| 968 | const QList<Message>& messages, |
| 969 | RootItem::ReadStatus read) { |
| 970 | Q_UNUSED(selected_item) |
| 971 | |
| 972 | auto cache = dynamic_cast<CacheForServiceRoot*>(this); |
| 973 | |
| 974 | if (cache != nullptr) { |
| 975 | cache->addMessageStatesToCache(customIDsOfMessages(messages), read); |
| 976 | } |
| 977 | |
| 978 | return true; |
| 979 | } |
| 980 | |
| 981 | bool ServiceRoot::onAfterSetMessagesRead(RootItem* selected_item, |
| 982 | const QList<Message>& messages, |
| 983 | RootItem::ReadStatus read) { |
| 984 | Q_UNUSED(messages) |
| 985 | Q_UNUSED(read) |
| 986 | |
| 987 | // We know that some messages were marked as read or unread, therefore we do not need to recount |
| 988 | // all items, but only some: |
| 989 | // - recycle bin (if recycle bin IS selected) |
| 990 | // - feeds of those messages (if recycle bin is NOT selected) |
| 991 | // - important articles (if some messages IS important AND recycle bin is NOT selected) |
| 992 | // - unread articles (if some messages IS unread AND recycle bin is NOT selected) |
| 993 | // - labels assigned to articles (if recycle bin is NOT selected) |
| 994 | // - probes (if recycle bin is NOT selected) |
| 995 | QList<RootItem*> to_update; |
| 996 | |
| 997 | if (selected_item->kind() == RootItem::Kind::Bin) { |
| 998 | selected_item->updateCounts(false); |
| 999 | to_update << selected_item; |
| 1000 | } |
| 1001 | else { |
| 1002 | auto linq = boolinq::from(messages); |
| 1003 | |
| 1004 | // 1. Feeds of messages. |
| 1005 | auto feed_ids = linq |
| 1006 | .select([](const Message& msg) { |
| 1007 | return msg.m_feedId; |
| 1008 | }) |
| 1009 | .distinct() |
| 1010 | .toStdList(); |
| 1011 | |
| 1012 | for (const QString& feed_id : feed_ids) { |
| 1013 | auto* feed = getItemFromSubTree([feed_id](const RootItem* it) { |
| 1014 | return it->kind() == RootItem::Kind::Feed && it->customId() == feed_id; |
| 1015 | }); |
| 1016 | |
| 1017 | if (feed != nullptr) { |
| 1018 | feed->updateCounts(false); |
| 1019 | to_update << feed; |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | // 2. Important. |
| 1024 | if (importantNode() != nullptr) { |
| 1025 | if (linq.any([](const Message& msg) { |
| 1026 | return msg.m_isImportant; |
| 1027 | })) { |
| 1028 | importantNode()->updateCounts(false); |
| 1029 | to_update << importantNode(); |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | // 3. Unread. |
| 1034 | if (unreadNode() != nullptr) { |
| 1035 | unreadNode()->updateCounts(false); |
| 1036 | to_update << unreadNode(); |
| 1037 | } |
| 1038 | |
| 1039 | // 4. Labels assigned. |
| 1040 | if (labelsNode() != nullptr) { |
| 1041 | // auto db = qApp->database()->driver()->connection(metaObject()->className()); |
| 1042 | QStringList lbls; // = DatabaseQueries::getCountOfAssignedLabelsToMessages(db, messages, accountId()); |
| 1043 | |
| 1044 | for (const Message& msg : messages) { |
| 1045 | for (const QString& lbl : msg.m_assignedLabelsIds) { |
| 1046 | if (!lbls.contains(lbl)) { |
| 1047 | lbls.append(lbl); |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | for (const QString& lbl : lbls) { |
| 1053 | Label* l = labelsNode()->labelById(lbl); |
| 1054 | |
| 1055 | if (l != nullptr) { |
| 1056 | l->updateCounts(false); |
| 1057 | to_update << l; |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | // 5. Probes. |
| 1063 | m_probesNode->updateCounts(false); |
| 1064 | to_update << m_probesNode->childItems(); |
| 1065 | } |
| 1066 | |
| 1067 | itemChanged(to_update); |
| 1068 | return true; |
| 1069 | } |
| 1070 | |
| 1071 | bool ServiceRoot::onBeforeSwitchMessageImportance(RootItem* selected_item, const QList<ImportanceChange>& changes) { |
| 1072 | Q_UNUSED(selected_item) |
| 1073 | |
| 1074 | auto cache = dynamic_cast<CacheForServiceRoot*>(this); |
| 1075 | |
| 1076 | if (cache != nullptr) { |
| 1077 | // Now, we need to separate the changes because of Nextcloud API limitations. |
| 1078 | QList<Message> mark_starred_msgs; |
| 1079 | QList<Message> mark_unstarred_msgs; |
| 1080 | |
| 1081 | for (const ImportanceChange& pair : changes) { |
| 1082 | if (pair.second == RootItem::Importance::Important) { |
| 1083 | mark_starred_msgs.append(pair.first); |
| 1084 | } |
| 1085 | else { |
| 1086 | mark_unstarred_msgs.append(pair.first); |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | if (!mark_starred_msgs.isEmpty()) { |
| 1091 | cache->addMessageStatesToCache(mark_starred_msgs, RootItem::Importance::Important); |
| 1092 | } |
| 1093 | |
| 1094 | if (!mark_unstarred_msgs.isEmpty()) { |
| 1095 | cache->addMessageStatesToCache(mark_unstarred_msgs, RootItem::Importance::NotImportant); |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | return true; |
| 1100 | } |
| 1101 | |
| 1102 | bool ServiceRoot::onAfterSwitchMessageImportance(RootItem* selected_item, const QList<ImportanceChange>& changes) { |
| 1103 | Q_UNUSED(selected_item) |
| 1104 | Q_UNUSED(changes) |
| 1105 | |
| 1106 | // NOTE: We know that some messages were marked as starred or unstarred. Starred count |
| 1107 | // is not displayed anywhere in feed list except "Important articles" item. |
| 1108 | auto in = importantNode(); |
| 1109 | |
| 1110 | if (in != nullptr) { |
| 1111 | |
| 1112 | in->updateCounts(true); |
| 1113 | itemChanged({in}); |
| 1114 | } |
| 1115 | |
| 1116 | return true; |
| 1117 | } |
| 1118 | |
| 1119 | bool ServiceRoot::onBeforeMessagesDelete(RootItem* selected_item, const QList<Message>& messages) { |
| 1120 | Q_UNUSED(selected_item) |
| 1121 | Q_UNUSED(messages) |
| 1122 | return true; |
| 1123 | } |
| 1124 | |
| 1125 | bool ServiceRoot::onAfterMessagesDelete(RootItem* selected_item, const QList<Message>& messages) { |
| 1126 | Q_UNUSED(selected_item) |
| 1127 | Q_UNUSED(messages) |
| 1128 | |
| 1129 | // TODO: We know that some messages were deleted, therefore we do not need to recount |
| 1130 | // all items, but only some: |
| 1131 | // - feeds of those messages (if recycle bin is NOT selected) |
| 1132 | // - recycle bin (if recycle bin IS selected) |
| 1133 | // - important articles (if some message IS important AND recycle bin is NOT selected) |
| 1134 | // - unread articles (if some messages IS unread AND if recycle bin is NOT selected) |
| 1135 | // - labels assigned to articles (if recycle bin is NOT selected) |
| 1136 | |
| 1137 | updateCounts(true); |
| 1138 | itemChanged(getSubTree<RootItem>()); |
| 1139 | return true; |
| 1140 | } |
| 1141 | |
| 1142 | bool ServiceRoot::onBeforeLabelMessageAssignmentChanged(const QList<Label*>& labels, |
| 1143 | const QList<Message>& messages, |
| 1144 | bool assign) { |
| 1145 | auto cache = dynamic_cast<CacheForServiceRoot*>(this); |
| 1146 | |
| 1147 | if (cache != nullptr) { |
| 1148 | boolinq::from(labels).for_each([cache, messages, assign](Label* lbl) { |
| 1149 | cache->addLabelsAssignmentsToCache(messages, lbl, assign); |
| 1150 | }); |
| 1151 | } |
| 1152 | |
| 1153 | return true; |
| 1154 | } |
| 1155 | |
| 1156 | bool ServiceRoot::onAfterLabelMessageAssignmentChanged(const QList<Label*>& labels, |
| 1157 | const QList<Message>& messages, |
| 1158 | bool assign) { |
| 1159 | Q_UNUSED(messages) |
| 1160 | Q_UNUSED(assign) |
| 1161 | |
| 1162 | for (Label* lbl : labels) { |
| 1163 | lbl->updateCounts(true); |
| 1164 | }; |
| 1165 | |
| 1166 | auto list = boolinq::from(labels) |
| 1167 | .select([](Label* lbl) { |
| 1168 | return static_cast<RootItem*>(lbl); |
| 1169 | }) |
| 1170 | .toStdList(); |
| 1171 | |
| 1172 | getParentServiceRoot()->itemChanged(FROM_STD_LIST(QList<RootItem*>, list)); |
| 1173 | return true; |
| 1174 | } |
| 1175 | |
| 1176 | bool ServiceRoot::onBeforeMessagesRestoredFromBin(RootItem* selected_item, const QList<Message>& messages) { |
| 1177 | Q_UNUSED(selected_item) |
| 1178 | Q_UNUSED(messages) |
| 1179 | |
| 1180 | return true; |
| 1181 | } |
| 1182 | |
| 1183 | bool ServiceRoot::onAfterMessagesRestoredFromBin(RootItem* selected_item, const QList<Message>& messages) { |
| 1184 | Q_UNUSED(selected_item) |
| 1185 | Q_UNUSED(messages) |
| 1186 | |
| 1187 | updateCounts(true); |
| 1188 | itemChanged(getSubTree<RootItem>()); |
| 1189 | return true; |
| 1190 | } |
| 1191 | |
| 1192 | void ServiceRoot::onAfterFeedsPurged(const QList<Feed*>& feeds) { |
| 1193 | Q_UNUSED(feeds) |
| 1194 | } |
| 1195 | |
| 1196 | CacheForServiceRoot* ServiceRoot::toCache() const { |
| 1197 | return dynamic_cast<CacheForServiceRoot*>(const_cast<ServiceRoot*>(this)); |
| 1198 | } |
| 1199 | |
| 1200 | void ServiceRoot::assembleFeeds(const Assignment& feeds) { |
| 1201 | QHash<int, Category*> categories = getSubTreeCategoriesForAssemble(); |
| 1202 | |
| 1203 | for (const AssignmentItem& feed : feeds) { |
| 1204 | if (feed.first == NO_PARENT_CATEGORY) { |
| 1205 | // This is top-level feed, add it to the root item. |
| 1206 | appendChild(feed.second); |
| 1207 | } |
| 1208 | else if (categories.contains(feed.first)) { |
| 1209 | // This feed belongs to this category. |
| 1210 | categories.value(feed.first)->appendChild(feed.second); |
| 1211 | } |
| 1212 | else { |
| 1213 | qWarningNN << LOGSEC_CORE << "Feed" << QUOTE_W_SPACE(feed.second->title()) << "is loose, skipping it."; |
| 1214 | } |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | void ServiceRoot::resortAccountTree(RootItem* tree, |
| 1219 | const QMap<QString, QVariantMap>& custom_category_data, |
| 1220 | const QMap<QString, QVariantMap>& custom_feed_data) const { |
| 1221 | // Iterate tree and rearrange children. |
| 1222 | QList<RootItem*> traversable_items; |
| 1223 | |
| 1224 | traversable_items.append(tree); |
| 1225 | |
| 1226 | while (!traversable_items.isEmpty()) { |
| 1227 | auto* root = traversable_items.takeFirst(); |
| 1228 | auto& chldr = root->childItems(); |
| 1229 | |
| 1230 | // Sort children so that we are sure that feeds are sorted with sort order |
| 1231 | // other item types do not matter. |
| 1232 | std::sort(chldr.begin(), chldr.end(), [&](const RootItem* lhs, const RootItem* rhs) { |
| 1233 | if (lhs->kind() == RootItem::Kind::Feed && rhs->kind() == RootItem::Kind::Feed) { |
| 1234 | auto lhs_order = custom_feed_data[lhs->customId()].value(QSL("sort_order")).toInt(); |
| 1235 | auto rhs_order = custom_feed_data[rhs->customId()].value(QSL("sort_order")).toInt(); |
| 1236 | |
| 1237 | return lhs_order < rhs_order; |
| 1238 | } |
| 1239 | else if (lhs->kind() == RootItem::Kind::Category && rhs->kind() == RootItem::Kind::Category) { |
| 1240 | auto lhs_order = custom_category_data[lhs->customId()].value(QSL("sort_order")).toInt(); |
| 1241 | auto rhs_order = custom_category_data[rhs->customId()].value(QSL("sort_order")).toInt(); |
| 1242 | |
| 1243 | return lhs_order < rhs_order; |
| 1244 | } |
| 1245 | else { |
| 1246 | return lhs->kind() < rhs->kind(); |
| 1247 | } |
| 1248 | }); |
| 1249 | |
| 1250 | traversable_items.append(root->childItems()); |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | void ServiceRoot::assembleCategories(const Assignment& categories) { |
| 1255 | Assignment editable_categories = categories; |
| 1256 | QHash<int, RootItem*> assignments; |
| 1257 | |
| 1258 | assignments.insert(NO_PARENT_CATEGORY, this); |
| 1259 | |
| 1260 | // Add top-level categories. |
| 1261 | while (!editable_categories.isEmpty()) { |
| 1262 | for (int i = 0; i < editable_categories.size(); i++) { |
| 1263 | if (assignments.contains(editable_categories.at(i).first)) { |
| 1264 | // Parent category of this category is already added. |
| 1265 | assignments.value(editable_categories.at(i).first)->appendChild(editable_categories.at(i).second); |
| 1266 | |
| 1267 | // Now, added category can be parent for another categories, add it. |
| 1268 | assignments.insert(editable_categories.at(i).second->id(), editable_categories.at(i).second); |
| 1269 | |
| 1270 | // Remove the category from the list, because it was |
| 1271 | // added to the final collection. |
| 1272 | editable_categories.removeAt(i); |
| 1273 | i--; |
| 1274 | } |
| 1275 | } |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | ServiceRoot::LabelOperation operator|(ServiceRoot::LabelOperation lhs, ServiceRoot::LabelOperation rhs) { |
| 1280 | return static_cast<ServiceRoot::LabelOperation>(static_cast<char>(lhs) | static_cast<char>(rhs)); |
| 1281 | } |
| 1282 | |
| 1283 | ServiceRoot::LabelOperation operator&(ServiceRoot::LabelOperation lhs, ServiceRoot::LabelOperation rhs) { |
| 1284 | return static_cast<ServiceRoot::LabelOperation>(static_cast<char>(lhs) & static_cast<char>(rhs)); |
| 1285 | } |
| 1286 | |
| 1287 | UpdatedArticles ServiceRoot::updateMessages(QList<Message>& messages, Feed* feed, bool force_update, QMutex* db_mutex) { |
| 1288 | UpdatedArticles updated_messages; |
| 1289 | QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className()); |
| 1290 | |
| 1291 | if (!messages.isEmpty()) { |
| 1292 | bool ok = false; |
| 1293 | |
| 1294 | qDebugNN << LOGSEC_CORE << "Updating messages in DB."; |
| 1295 | |
| 1296 | updated_messages = DatabaseQueries::updateMessages(database, messages, feed, force_update, db_mutex, &ok); |
| 1297 | } |
| 1298 | else { |
| 1299 | qDebugNN << "No messages to be updated/added in DB for feed" << QUOTE_W_SPACE_DOT(feed->customId()); |
| 1300 | } |
| 1301 | |
| 1302 | bool anything_removed = feed->removeUnwantedArticles(database); |
| 1303 | |
| 1304 | if (anything_removed || !updated_messages.m_unread.isEmpty() || !updated_messages.m_all.isEmpty()) { |
| 1305 | QMutexLocker lck(db_mutex); |
| 1306 | |
| 1307 | // Something was added or updated in the DB, update numbers. |
| 1308 | feed->updateCounts(true); |
| 1309 | |
| 1310 | if (recycleBin() != nullptr) { |
| 1311 | recycleBin()->updateCounts(true); |
| 1312 | } |
| 1313 | |
| 1314 | if (importantNode() != nullptr) { |
| 1315 | importantNode()->updateCounts(true); |
| 1316 | } |
| 1317 | |
| 1318 | if (unreadNode() != nullptr) { |
| 1319 | unreadNode()->updateCounts(true); |
| 1320 | } |
| 1321 | |
| 1322 | if (labelsNode() != nullptr) { |
| 1323 | labelsNode()->updateCounts(true); |
| 1324 | } |
| 1325 | |
| 1326 | if (probesNode() != nullptr) { |
| 1327 | probesNode()->updateCounts(true); |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | // NOTE: Do not update model items here. We update only once when all feeds are fetched |
| 1332 | // or separately in downloader, if user has this enabled. |
| 1333 | return updated_messages; |
| 1334 | } |