| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "services/abstract/category.h" |
| 4 | |
| 5 | #include "database/databasequeries.h" |
| 6 | #include "miscellaneous/application.h" |
| 7 | #include "services/abstract/cacheforserviceroot.h" |
| 8 | #include "services/abstract/feed.h" |
| 9 | #include "services/abstract/serviceroot.h" |
| 10 | |
| 11 | Category::Category(RootItem* parent) : RootItem(parent) { |
| 12 | setKind(RootItem::Kind::Category); |
| 13 | } |
| 14 | |
| 15 | Category::Category(const Category& other) : RootItem(other) { |
| 16 | setKind(RootItem::Kind::Category); |
| 17 | } |
| 18 | |
| 19 | void Category::updateCounts(bool including_total_count) { |
| 20 | QList<Feed*> feeds; |
| 21 | auto str = childItems(); |
| 22 | |
| 23 | for (RootItem* child : std::as_const(str)) { |
| 24 | if (child->kind() == RootItem::Kind::Feed) { |
| 25 | feeds.append(child->toFeed()); |
| 26 | } |
| 27 | else if (child->kind() == RootItem::Kind::Category) { |
| 28 | child->updateCounts(including_total_count); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | if (feeds.isEmpty()) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 37 | bool ok; |
| 38 | auto counts = DatabaseQueries::getMessageCountsForCategory(database, |
| 39 | customId(), |
| 40 | getParentServiceRoot()->accountId(), |
| 41 | including_total_count, |
| 42 | &ok); |
| 43 | |
| 44 | if (ok) { |
| 45 | for (Feed* feed : feeds) { |
| 46 | if (counts.contains(feed->customId())) { |
| 47 | feed->setCountOfUnreadMessages(counts.value(feed->customId()).m_unread); |
| 48 | |
| 49 | if (including_total_count) { |
| 50 | feed->setCountOfAllMessages(counts.value(feed->customId()).m_total); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | bool Category::cleanMessages(bool clean_read_only) { |
| 58 | return getParentServiceRoot()->cleanFeeds(getSubTreeFeeds(), clean_read_only); |
| 59 | } |
| 60 | |
| 61 | bool Category::markAsReadUnread(RootItem::ReadStatus status) { |
| 62 | ServiceRoot* service = getParentServiceRoot(); |
| 63 | auto* cache = dynamic_cast<CacheForServiceRoot*>(service); |
| 64 | |
| 65 | if (cache != nullptr) { |
| 66 | cache->addMessageStatesToCache(service->customIDSOfMessagesForItem(this, status), status); |
| 67 | } |
| 68 | |
| 69 | return service->markFeedsReadUnread(getSubTreeFeeds(), status); |
| 70 | } |
| 71 | |
| 72 | QString Category::additionalTooltip() const { |
| 73 | return tr("Number of feeds: %1\n" |
| 74 | "Number of categories: %2\n" |
| 75 | "Number of disabled feeds: %3") |
| 76 | .arg(QString::number(getSubTreeFeeds().size()), |
| 77 | QString::number(getSubTreeCategories().size() - 1), |
| 78 | QString::number(getSubTree<RootItem>([](const RootItem* ri) { |
| 79 | return ri->kind() == RootItem::Kind::Feed && ri->toFeed()->isSwitchedOff(); |
| 80 | }).size())); |
| 81 | } |