Debian Codegraph @

src/librssguard/services/abstract/rootitem.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "services/abstract/rootitem.h"
4
5 #include "3rd-party/boolinq/boolinq.h"
6 #include "miscellaneous/application.h"
7 #include "miscellaneous/iconfactory.h"
8 #include "miscellaneous/settings.h"
9 #include "services/abstract/category.h"
10 #include "services/abstract/feed.h"
11 #include "services/abstract/label.h"
12 #include "services/abstract/search.h"
13 #include "services/abstract/serviceroot.h"
14
15 #include <QVariant>
16
17 RootItem::RootItem(RootItem* parent_item)
18 : QObject(nullptr), m_kind(RootItem::Kind::Root), m_id(NO_PARENT_CATEGORY), m_customId(QL1S("")), m_title(QString()),
19 m_description(QString()), m_creationDate(QDateTime::currentDateTimeUtc()), m_keepOnTop(false),
20 m_sortOrder(NO_PARENT_CATEGORY), m_childItems(QList<RootItem*>()), m_parentItem(parent_item) {}
21
22 RootItem::RootItem(const RootItem& other) : RootItem(nullptr) {
23 setTitle(other.title());
24 setId(other.id());
25 setCustomId(other.customId());
26 setIcon(other.icon());
27 setKeepOnTop(other.keepOnTop());
28 setSortOrder(other.sortOrder());
29
30 // NOTE: We do not need to clone childs, because that would mean that
31 // either source or target item tree would get corrupted.
32 // setChildItems(other.childItems());
33
34 setParent(other.parent());
35 setCreationDate(other.creationDate());
36 setDescription(other.description());
37 }
38
39 RootItem::~RootItem() {
40 qDeleteAll(m_childItems);
41 }
42
43 QString RootItem::hashCode() const {
44 ServiceRoot* root = getParentServiceRoot();
45 int acc_id = root == nullptr ? 0 : root->accountId();
46
47 return QString::number(acc_id) + QL1S("-") + QString::number(int(kind())) + QL1S("-") + QString::number(id());
48 }
49
50 QString RootItem::additionalTooltip() const {
51 return QString();
52 }
53
54 QList<QAction*> RootItem::contextMenuFeedsList() {
55 return {};
56 }
57
58 bool RootItem::canBeEdited() const {
59 return false;
60 }
61
62 bool RootItem::canBeDeleted() const {
63 return false;
64 }
65
66 bool RootItem::deleteItem() {
67 return false;
68 }
69
70 bool RootItem::isFetching() const {
71 return false;
72 }
73
74 bool RootItem::markAsReadUnread(ReadStatus status) {
75 bool result = true;
76
77 for (RootItem* child : std::as_const(m_childItems)) {
78 result &= child->markAsReadUnread(status);
79 }
80
81 return result;
82 }
83
84 QList<Message> RootItem::undeletedMessages() const {
85 QList<Message> messages;
86
87 for (RootItem* child : std::as_const(m_childItems)) {
88 if (child->kind() != Kind::Bin && child->kind() != Kind::Labels && child->kind() != Kind::Label) {
89 messages.append(child->undeletedMessages());
90 }
91 }
92
93 return messages;
94 }
95
96 bool RootItem::cleanMessages(bool clear_only_read) {
97 bool result = true;
98
99 for (RootItem* child : std::as_const(m_childItems)) {
100 if (child->kind() != RootItem::Kind::Bin) {
101 result &= child->cleanMessages(clear_only_read);
102 }
103 }
104
105 return result;
106 }
107
108 void RootItem::updateCounts(bool including_total_count) {
109 for (RootItem* child : std::as_const(m_childItems)) {
110 child->updateCounts(including_total_count);
111 }
112 }
113
114 int RootItem::row() const {
115 if (m_parentItem != nullptr) {
116 return m_parentItem->m_childItems.indexOf(const_cast<RootItem*>(this));
117 }
118 else {
119 // This item has no parent. Therefore, its row index is 0.
120 return 0;
121 }
122 }
123
124 QVariant RootItem::data(int column, int role) const {
125 switch (role) {
126 case Qt::ItemDataRole::ToolTipRole:
127 if (column == FDS_MODEL_TITLE_INDEX) {
128 QString tool_tip = QSL("<b>%1</b>").arg(m_title);
129
130 if (!m_description.isEmpty()) {
131 tool_tip += QL1S("\n") + m_description;
132 }
133
134 QString extra_tooltip = additionalTooltip();
135
136 if (!extra_tooltip.isEmpty()) {
137 tool_tip += QL1S("\n\n") + extra_tooltip;
138 }
139
140 return tool_tip.replace(QSL("\n"), QSL("<br />"));
141 }
142 else if (column == FDS_MODEL_COUNTS_INDEX) {
143 //: Tooltip for "unread" column of feed list.
144 return tr("%n unread article(s).", nullptr, countOfUnreadMessages());
145 }
146 else {
147 return QVariant();
148 }
149
150 case LOWER_TITLE_ROLE:
151 return m_title.toLower();
152
153 case Qt::ItemDataRole::EditRole:
154 if (column == FDS_MODEL_TITLE_INDEX) {
155 return m_title;
156 }
157 else if (column == FDS_MODEL_COUNTS_INDEX) {
158 return countOfUnreadMessages();
159 }
160 else {
161 return QVariant();
162 }
163
164 case Qt::ItemDataRole::DisplayRole:
165 if (column == FDS_MODEL_TITLE_INDEX) {
166 return sanitizedTitle();
167 }
168 else if (column == FDS_MODEL_COUNTS_INDEX) {
169 int count_unread = countOfUnreadMessages();
170
171 if (count_unread <= 0 && qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::HideCountsIfNoUnread)).toBool()) {
172 return QString();
173 }
174 else {
175 int count_all = countOfAllMessages();
176
177 return qApp->settings()
178 ->value(GROUP(Feeds), SETTING(Feeds::CountFormat))
179 .toString()
180 .replace(QSL(PLACEHOLDER_UNREAD_COUNTS), count_unread < 0 ? QSL("-") : QString::number(count_unread))
181 .replace(QSL(PLACEHOLDER_ALL_COUNTS), count_all < 0 ? QSL("-") : QString::number(count_all));
182 }
183 }
184 else {
185 return QVariant();
186 }
187
188 case Qt::ItemDataRole::DecorationRole:
189 if (column == FDS_MODEL_TITLE_INDEX) {
190 return fullIcon();
191 }
192 else {
193 return QVariant();
194 }
195
196 case Qt::ItemDataRole::TextAlignmentRole:
197 if (column == FDS_MODEL_COUNTS_INDEX) {
198 return Qt::AlignmentFlag::AlignCenter;
199 }
200 else {
201 return QVariant();
202 }
203
204 default:
205 return QVariant();
206 }
207 }
208
209 Qt::ItemFlags RootItem::additionalFlags() const {
210 if (m_kind == RootItem::Kind::Root) {
211 return Qt::ItemFlag::ItemIsDropEnabled;
212 }
213 else {
214 return Qt::ItemFlag::NoItemFlags;
215 }
216 }
217
218 bool RootItem::performDragDropChange(RootItem* target_item) {
219 Q_UNUSED(target_item)
220 return false;
221 }
222
223 int RootItem::countOfUnreadMessages() const {
224 return boolinq::from(m_childItems).sum([](RootItem* it) {
225 return (it->kind() == RootItem::Kind::Bin || it->kind() == RootItem::Kind::Important ||
226 it->kind() == RootItem::Kind::Unread || it->kind() == RootItem::Kind::Labels ||
227 it->kind() == RootItem::Kind::Probes)
228 ? 0
229 : std::max(it->countOfUnreadMessages(), 0);
230 });
231 }
232
233 int RootItem::countOfAllMessages() const {
234 return boolinq::from(m_childItems).sum([](RootItem* it) {
235 return (it->kind() == RootItem::Kind::Bin || it->kind() == RootItem::Kind::Important ||
236 it->kind() == RootItem::Kind::Unread || it->kind() == RootItem::Kind::Labels ||
237 it->kind() == RootItem::Kind::Probes)
238 ? 0
239 : std::max(it->countOfAllMessages(), 0);
240 });
241 }
242
243 bool RootItem::isChildOf(const RootItem* root) const {
244 if (root == nullptr) {
245 return false;
246 }
247
248 const RootItem* this_item = this;
249
250 while (this_item->kind() != RootItem::Kind::Root) {
251 if (root->childItems().contains(const_cast<RootItem* const>(this_item))) {
252 return true;
253 }
254 else {
255 this_item = this_item->parent();
256 }
257 }
258
259 return false;
260 }
261
262 bool RootItem::isParentOf(const RootItem* child) const {
263 if (child == nullptr) {
264 return false;
265 }
266 else {
267 return child->isChildOf(this);
268 }
269 }
270
271 QList<RootItem*> RootItem::getSubTree(RootItem::Kind kind_of_item) const {
272 return getSubTree<RootItem>([kind_of_item](const RootItem* ri) {
273 return int(ri->kind() & kind_of_item) > 0;
274 });
275 }
276
277 QList<Category*> RootItem::getSubTreeCategories() const {
278 return getSubTree<Category>([](const RootItem* ri) {
279 return ri->kind() == RootItem::Kind::Category;
280 });
281 }
282
283 RootItem* RootItem::getItemFromSubTree(std::function<bool(const RootItem*)> tester) const {
284 QList<RootItem*> children;
285 QList<RootItem*> traversable_items;
286
287 traversable_items.append(const_cast<RootItem* const>(this));
288
289 // Iterate all nested items.
290 while (!traversable_items.isEmpty()) {
291 RootItem* active_item = traversable_items.takeFirst();
292
293 if (tester(active_item)) {
294 return active_item;
295 }
296
297 children.append(active_item);
298 traversable_items.append(active_item->childItems());
299 }
300
301 return nullptr;
302 }
303
304 QHash<int, Category*> RootItem::getSubTreeCategoriesForAssemble() const {
305 QHash<int, Category*> children;
306 QList<RootItem*> traversable_items;
307
308 traversable_items.append(const_cast<RootItem* const>(this));
309
310 // Iterate all nested items.
311 while (!traversable_items.isEmpty()) {
312 RootItem* active_item = traversable_items.takeFirst();
313
314 if (active_item->kind() == RootItem::Kind::Category && !children.contains(active_item->id())) {
315 children.insert(active_item->id(), active_item->toCategory());
316 }
317
318 traversable_items.append(active_item->childItems());
319 }
320
321 return children;
322 }
323
324 QHash<QString, Category*> RootItem::getHashedSubTreeCategories() const {
325 QHash<QString, Category*> children;
326 QList<RootItem*> traversable_items;
327
328 traversable_items.append(const_cast<RootItem* const>(this));
329
330 // Iterate all nested items.
331 while (!traversable_items.isEmpty()) {
332 RootItem* active_item = traversable_items.takeFirst();
333
334 if (active_item->kind() == RootItem::Kind::Category && !children.contains(active_item->customId())) {
335 children.insert(active_item->customId(), active_item->toCategory());
336 }
337
338 traversable_items.append(active_item->childItems());
339 }
340
341 return children;
342 }
343
344 QHash<QString, Feed*> RootItem::getHashedSubTreeFeeds() const {
345 QHash<QString, Feed*> children;
346 QList<RootItem*> traversable_items;
347
348 traversable_items.append(const_cast<RootItem* const>(this));
349
350 // Iterate all nested items.
351 while (!traversable_items.isEmpty()) {
352 RootItem* active_item = traversable_items.takeFirst();
353
354 if (active_item->kind() == RootItem::Kind::Feed && !children.contains(active_item->customId())) {
355 children.insert(active_item->customId(), active_item->toFeed());
356 }
357
358 traversable_items.append(active_item->childItems());
359 }
360
361 return children;
362 }
363
364 QList<Feed*> RootItem::getSubTreeFeeds(bool recursive) const {
365 QList<Feed*> children;
366 QList<RootItem*> traversable_items;
367 bool traversed = false;
368
369 traversable_items.append(const_cast<RootItem* const>(this));
370
371 // Iterate all nested items.
372 while (!traversable_items.isEmpty()) {
373 RootItem* active_item = traversable_items.takeFirst();
374
375 if (active_item->kind() == RootItem::Kind::Feed) {
376 children.append(active_item->toFeed());
377 }
378
379 if (recursive || !traversed) {
380 traversed = true;
381 traversable_items.append(active_item->childItems());
382 }
383 }
384
385 return children;
386 }
387
388 QList<Feed*> RootItem::getSubTreeAutoFetchingWithManualIntervalsFeeds() const {
389 QList<Feed*> children;
390 QList<RootItem*> traversable_items;
391
392 traversable_items.append(const_cast<RootItem* const>(this));
393
394 // Iterate all nested items.
395 while (!traversable_items.isEmpty()) {
396 RootItem* active_item = traversable_items.takeFirst();
397
398 if (active_item->kind() == RootItem::Kind::Feed &&
399 active_item->toFeed()->autoUpdateType() == Feed::AutoUpdateType::SpecificAutoUpdate) {
400 children.append(active_item->toFeed());
401 }
402
403 traversable_items.append(active_item->childItems());
404 }
405
406 return children;
407 }
408
409 QList<Feed*> RootItem::getSubAutoFetchingEnabledFeeds() const {
410 QList<Feed*> children;
411 QList<RootItem*> traversable_items;
412
413 traversable_items.append(const_cast<RootItem* const>(this));
414
415 // Iterate all nested items.
416 while (!traversable_items.isEmpty()) {
417 RootItem* active_item = traversable_items.takeFirst();
418
419 if (active_item->kind() == RootItem::Kind::Feed &&
420 active_item->toFeed()->autoUpdateType() != Feed::AutoUpdateType::DontAutoUpdate) {
421 children.append(active_item->toFeed());
422 }
423
424 traversable_items.append(active_item->childItems());
425 }
426
427 return children;
428 }
429
430 ServiceRoot* RootItem::getParentServiceRoot() const {
431 const RootItem* working_parent = this;
432
433 while (working_parent->kind() != RootItem::Kind::Root) {
434 if (working_parent->kind() == RootItem::Kind::ServiceRoot) {
435 return working_parent->toServiceRoot();
436 }
437 else {
438 working_parent = working_parent->parent();
439 }
440 }
441
442 return nullptr;
443 }
444
445 RootItem::Kind RootItem::kind() const {
446 return m_kind;
447 }
448
449 void RootItem::setKind(RootItem::Kind kind) {
450 m_kind = kind;
451 }
452
453 QIcon RootItem::icon() const {
454 return m_icon;
455 }
456
457 void RootItem::setIcon(const QIcon& icon) {
458 m_icon = icon;
459 }
460
461 QIcon RootItem::fullIcon() const {
462 QIcon ico = icon();
463
464 if (ico.isNull()) {
465 if (kind() == RootItem::Kind::Feed) {
466 return qApp->icons()->fromTheme(QSL("application-rss+xml"));
467 }
468 else if (kind() == RootItem::Kind::Category) {
469 return qApp->icons()->fromTheme(QSL("folder"));
470 }
471 }
472
473 return ico;
474 }
475
476 int RootItem::id() const {
477 return m_id;
478 }
479
480 void RootItem::setId(int id) {
481 m_id = id;
482 }
483
484 QString RootItem::title() const {
485 return m_title;
486 }
487
488 QString RootItem::sanitizedTitle() const {
489 return m_title.simplified();
490 }
491
492 void RootItem::setTitle(const QString& title) {
493 m_title = title;
494 }
495
496 QDateTime RootItem::creationDate() const {
497 return m_creationDate;
498 }
499
500 void RootItem::setCreationDate(const QDateTime& creation_date) {
501 m_creationDate = creation_date;
502 }
503
504 QString RootItem::description() const {
505 return m_description;
506 }
507
508 void RootItem::setDescription(const QString& description) {
509 m_description = description;
510 }
511
512 bool RootItem::removeChild(RootItem* child) {
513 return m_childItems.removeOne(child);
514 }
515
516 QString RootItem::customId() const {
517 return m_customId;
518 }
519
520 int RootItem::customNumericId() const {
521 return customId().toInt();
522 }
523
524 void RootItem::setCustomId(const QString& custom_id) {
525 m_customId = custom_id;
526 }
527
528 Category* RootItem::toCategory() const {
529 return qobject_cast<Category*>(const_cast<RootItem*>(this));
530 }
531
532 Feed* RootItem::toFeed() const {
533 return qobject_cast<Feed*>(const_cast<RootItem*>(this));
534 }
535
536 Label* RootItem::toLabel() const {
537 return qobject_cast<Label*>(const_cast<RootItem*>(this));
538 }
539
540 Search* RootItem::toProbe() const {
541 return qobject_cast<Search*>(const_cast<RootItem*>(this));
542 }
543
544 ServiceRoot* RootItem::toServiceRoot() const {
545 return qobject_cast<ServiceRoot*>(const_cast<RootItem*>(this));
546 }
547
548 bool RootItem::keepOnTop() const {
549 return m_keepOnTop;
550 }
551
552 void RootItem::setKeepOnTop(bool keep_on_top) {
553 m_keepOnTop = keep_on_top;
554 }
555
556 int RootItem::sortOrder() const {
557 return m_sortOrder;
558 }
559
560 void RootItem::setSortOrder(int sort_order) {
561 m_sortOrder = sort_order;
562 }
563
564 bool RootItem::removeChild(int index) {
565 if (index >= 0 && index < m_childItems.size()) {
566 m_childItems.removeAt(index);
567 return true;
568 }
569 else {
570 return false;
571 }
572 }
573
574 QDataStream& operator>>(QDataStream& in, RootItem::ReadStatus& myObj) {
575 int obj;
576
577 in >> obj;
578
579 myObj = (RootItem::ReadStatus)obj;
580
581 return in;
582 }
583
584 QDataStream& operator<<(QDataStream& out, const RootItem::ReadStatus& myObj) {
585 out << (int)myObj;
586
587 return out;
588 }
589
590 QDataStream& operator>>(QDataStream& in, RootItem::Importance& myObj) {
591 int obj;
592
593 in >> obj;
594
595 myObj = (RootItem::Importance)obj;
596
597 return in;
598 }
599
600 QDataStream& operator<<(QDataStream& out, const RootItem::Importance& myObj) {
601 out << (int)myObj;
602
603 return out;
604 }
605
606 RootItem::Kind operator|(RootItem::Kind a, RootItem::Kind b) {
607 return static_cast<RootItem::Kind>(static_cast<int>(a) | static_cast<int>(b));
608 }
609
610 RootItem::Kind operator&(RootItem::Kind a, RootItem::Kind b) {
611 return static_cast<RootItem::Kind>(static_cast<int>(a) & static_cast<int>(b));
612 }
613
614 QList<RootItem*> RootItem::childItems(Kind kind) const {
615 auto linq = boolinq::from(m_childItems)
616 .where([=](RootItem* it) {
617 return it->kind() == kind;
618 })
619 .toStdList();
620
621 return FROM_STD_LIST(QList<RootItem*>, linq);
622 }