Debian Codegraph @

src/librssguard/services/abstract/labelsnode.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "services/abstract/labelsnode.h"
4
5 #include "3rd-party/boolinq/boolinq.h"
6 #include "database/databasefactory.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 "services/abstract/gui/formaddeditlabel.h"
13 #include "services/abstract/serviceroot.h"
14
15 LabelsNode::LabelsNode(RootItem* parent_item) : RootItem(parent_item), m_actLabelNew(nullptr) {
16 setKind(RootItem::Kind::Labels);
17 setId(ID_LABELS);
18 setIcon(qApp->icons()->fromTheme(QSL("tag-folder"), QSL("emblem-favorite")));
19 setTitle(tr("Labels"));
20 setDescription(tr("You can see all your labels (tags) here."));
21 }
22
23 void LabelsNode::loadLabels(const QList<Label*>& labels) {
24 for (Label* lbl : labels) {
25 appendChild(lbl);
26 }
27 }
28
29 QList<Message> LabelsNode::undeletedMessages() const {
30 QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className());
31
32 return DatabaseQueries::getUndeletedLabelledMessages(database, getParentServiceRoot()->accountId());
33 }
34
35 void LabelsNode::updateCounts(bool including_total_count) {
36 // TODO: This is still rather slow because this is automatically
37 // called when message is marked (un)read or starred.
38 // It would be enough if only labels which are assigned to article
39 // are recounted, not all.
40
41 QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className());
42 int account_id = getParentServiceRoot()->accountId();
43 auto acc = DatabaseQueries::getMessageCountsForAllLabels(database, account_id);
44
45 for (Label* lbl : labels()) {
46 if (!acc.contains(lbl->customId())) {
47 if (including_total_count) {
48 lbl->setCountOfAllMessages(0);
49 }
50
51 lbl->setCountOfUnreadMessages(0);
52 }
53 else {
54 auto ac = acc.value(lbl->customId());
55
56 if (including_total_count) {
57 lbl->setCountOfAllMessages(ac.m_total);
58 }
59
60 lbl->setCountOfUnreadMessages(ac.m_unread);
61 }
62 }
63 }
64
65 Label* LabelsNode::labelById(const QString& custom_id) {
66 auto chi = childItems();
67
68 return qobject_cast<Label*>(boolinq::from(chi).firstOrDefault([custom_id](RootItem* it) {
69 return it->customId() == custom_id;
70 }));
71 }
72
73 QList<Label*> LabelsNode::labels() const {
74 auto list = boolinq::from(childItems())
75 .select([](RootItem* it) {
76 return static_cast<Label*>(it);
77 })
78 .toStdList();
79
80 return FROM_STD_LIST(QList<Label*>, list);
81 }
82
83 QList<QAction*> LabelsNode::contextMenuFeedsList() {
84 if (m_actLabelNew == nullptr) {
85 // Initialize it all.
86 m_actLabelNew = new QAction(qApp->icons()->fromTheme(QSL("tag-new")), tr("New label"), this);
87
88 connect(m_actLabelNew, &QAction::triggered, this, &LabelsNode::createLabel);
89 }
90
91 return QList<QAction*>{m_actLabelNew};
92 }
93
94 void LabelsNode::createLabel() {
95 if (Globals::hasFlag(getParentServiceRoot()->supportedLabelOperations(), ServiceRoot::LabelOperation::Adding)) {
96 FormAddEditLabel frm(qApp->mainFormWidget());
97 Label* new_lbl = frm.execForAdd();
98
99 if (new_lbl != nullptr) {
100 QSqlDatabase db = qApp->database()->driver()->connection(metaObject()->className());
101
102 try {
103 DatabaseQueries::createLabel(db, new_lbl, getParentServiceRoot()->accountId());
104
105 getParentServiceRoot()->requestItemReassignment(new_lbl, this);
106 getParentServiceRoot()->requestItemExpand({this}, true);
107 }
108 catch (const ApplicationException&) {
109 new_lbl->deleteLater();
110 }
111 }
112 }
113 else {
114 qApp->showGuiMessage(Notification::Event::GeneralEvent,
115 {tr("This account does not allow you to create labels."),
116 tr("Not allowed"),
117 QSystemTrayIcon::MessageIcon::Critical});
118 }
119 }