Debian Codegraph @

src/librssguard/gui/notifications/articlelistnotification.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "gui/notifications/articlelistnotification.h"
4
5 #include "core/articlelistnotificationmodel.h"
6 #include "database/databasequeries.h"
7 #include "miscellaneous/iconfactory.h"
8 #include "network-web/webfactory.h"
9
10 #include <QMouseEvent>
11 #include <QTreeView>
12
13 ArticleListNotification::ArticleListNotification(QWidget* parent)
14 : BaseToastNotification(parent), m_model(new ArticleListNotificationModel(this)) {
15 m_ui.setupUi(this);
16
17 setupHeading(m_ui.m_lblTitle);
18 setupCloseButton(m_ui.m_btnClose);
19
20 m_ui.m_treeArticles->viewport()->installEventFilter(this);
21
22 m_ui.m_btnNextPage->setIcon(qApp->icons()->fromTheme(QSL("arrow-right"), QSL("stock_right")));
23 m_ui.m_btnPreviousPage->setIcon(qApp->icons()->fromTheme(QSL("arrow-left"), QSL("stock_left")));
24 m_ui.m_btnOpenArticleList->setIcon(qApp->icons()->fromTheme(QSL("view-list-details")));
25 m_ui.m_btnOpenWebBrowser->setIcon(qApp->icons()->fromTheme(QSL("document-open")));
26 m_ui.m_btnMarkAllRead->setIcon(qApp->icons()->fromTheme(QSL("mail-mark-read")));
27
28 m_ui.m_treeArticles->setModel(m_model);
29
30 connect(m_model,
31 &ArticleListNotificationModel::nextPagePossibleChanged,
32 m_ui.m_btnNextPage,
33 &PlainToolButton::setEnabled);
34 connect(m_model,
35 &ArticleListNotificationModel::previousPagePossibleChanged,
36 m_ui.m_btnPreviousPage,
37 &PlainToolButton::setEnabled);
38 connect(m_ui.m_btnNextPage, &PlainToolButton::clicked, m_model, &ArticleListNotificationModel::nextPage);
39 connect(m_ui.m_btnPreviousPage, &PlainToolButton::clicked, m_model, &ArticleListNotificationModel::previousPage);
40 connect(m_ui.m_btnMarkAllRead, &PlainToolButton::clicked, this, &ArticleListNotification::markAllRead);
41 connect(m_ui.m_treeArticles,
42 &QAbstractItemView::doubleClicked,
43 this,
44 &ArticleListNotification::openArticleInWebBrowser);
45 connect(m_ui.m_btnOpenWebBrowser, &PlainToolButton::clicked, this, &ArticleListNotification::openArticleInWebBrowser);
46 connect(m_ui.m_btnOpenArticleList,
47 &PlainToolButton::clicked,
48 this,
49 &ArticleListNotification::openArticleInArticleList);
50 connect(m_ui.m_treeArticles->selectionModel(),
51 &QItemSelectionModel::currentChanged,
52 this,
53 &ArticleListNotification::onMessageSelected);
54
55 m_ui.m_treeArticles->setAttribute(Qt::WA_NoSystemBackground, true);
56
57 // Make background transparent.
58 auto pal = m_ui.m_treeArticles->palette();
59 pal.setColor(QPalette::ColorRole::Base, Qt::transparent);
60 m_ui.m_treeArticles->setPalette(pal);
61
62 connect(m_ui.m_cmbFeeds,
63 QOverload<int>::of(&QComboBox::currentIndexChanged),
64 this,
65 &ArticleListNotification::showFeed);
66 }
67
68 void ArticleListNotification::loadResults(const QHash<Feed*, QList<Message>>& new_messages) {
69 qDebugNN << LOGSEC_NOTIFICATIONS << "Setting up RESULTS notification.";
70 setupTimedClosing(false);
71
72 m_newMessages = new_messages;
73
74 m_ui.m_cmbFeeds->model()->sort(0, Qt::SortOrder::AscendingOrder);
75 m_ui.m_cmbFeeds->clear();
76
77 auto ks = new_messages.keys();
78
79 std::sort(ks.begin(), ks.end(), [](Feed* lhs, Feed* rhs) {
80 return QString::compare(lhs->sanitizedTitle(), rhs->sanitizedTitle(), Qt::CaseSensitivity::CaseInsensitive) < 0;
81 });
82
83 for (Feed* fd : ks) {
84 if (fd->isQuiet()) {
85 continue;
86 }
87
88 if (m_newMessages[fd].size() > 0) {
89 m_ui.m_cmbFeeds->addItem(fd->sanitizedTitle(), QVariant::fromValue(fd));
90 }
91 }
92
93 m_ui.m_lblTitle->setText(tr("%n feeds fetched", nullptr, m_ui.m_cmbFeeds->count()));
94 m_ui.m_lblTitle->setToolTip(m_ui.m_lblTitle->text());
95 }
96
97 void ArticleListNotification::openArticleInArticleList() {
98 emit openingArticleInArticleListRequested(m_ui.m_cmbFeeds->currentData().value<Feed*>(), selectedMessage());
99
100 if (m_newMessages.size() == 1 && m_newMessages.value(m_newMessages.keys().at(0)).size() == 1) {
101 // We only have 1 message in 1 feed.
102 emit closeRequested(this);
103 }
104 }
105
106 void ArticleListNotification::onMessageSelected(const QModelIndex& current, const QModelIndex& previous) {
107 Q_UNUSED(previous)
108
109 m_ui.m_btnOpenArticleList->setEnabled(m_ui.m_treeArticles->currentIndex().isValid());
110
111 try {
112 Message msg = selectedMessage();
113
114 m_ui.m_btnOpenWebBrowser->setEnabled(!msg.m_url.isEmpty());
115 }
116 catch (...) {
117 m_ui.m_btnOpenWebBrowser->setEnabled(false);
118 }
119 }
120
121 void ArticleListNotification::showFeed(int index) {
122 Q_UNUSED(index)
123 m_model->setArticles(m_newMessages.value(selectedFeed()));
124 onMessageSelected({}, {});
125 }
126
127 void ArticleListNotification::openArticleInWebBrowser() {
128 Feed* fd = selectedFeed();
129 Message msg = selectedMessage();
130
131 markAsRead(fd, {msg});
132 emit reloadMessageListRequested(false);
133
134 qApp->web()->openUrlInExternalBrowser(msg.m_url);
135
136 if (m_newMessages.size() == 1 && m_newMessages.value(m_newMessages.keys().at(0)).size() == 1) {
137 // We only have 1 message in 1 feed.
138 emit closeRequested(this);
139 }
140 }
141
142 void ArticleListNotification::markAllRead() {
143 for (Feed* fd : m_newMessages.keys()) {
144 markAsRead(fd, m_newMessages.value(fd));
145 }
146
147 emit reloadMessageListRequested(false);
148 }
149
150 void ArticleListNotification::markAsRead(Feed* feed, const QList<Message>& articles) {
151 ServiceRoot* acc = feed->getParentServiceRoot();
152 QStringList message_ids;
153 message_ids.reserve(articles.size());
154
155 // Obtain IDs of all desired messages.
156 for (const Message& message : articles) {
157 message_ids.append(QString::number(message.m_id));
158 }
159
160 if (acc->onBeforeSetMessagesRead(feed, articles, RootItem::ReadStatus::Read)) {
161 auto db = qApp->database()->driver()->connection(metaObject()->className());
162
163 if (DatabaseQueries::markMessagesReadUnread(db, message_ids, RootItem::ReadStatus::Read)) {
164 acc->onAfterSetMessagesRead(feed, articles, RootItem::ReadStatus::Read);
165 }
166 }
167 }
168
169 Feed* ArticleListNotification::selectedFeed(int index) const {
170 if (index < 0) {
171 return m_ui.m_cmbFeeds->currentData().value<Feed*>();
172 }
173 else {
174 return m_ui.m_cmbFeeds->itemData(index).value<Feed*>();
175 }
176 }
177
178 Message ArticleListNotification::selectedMessage() const {
179 if (m_ui.m_treeArticles->currentIndex().isValid()) {
180 return m_model->message(m_ui.m_treeArticles->currentIndex());
181 }
182 else {
183 throw ApplicationException(QSL("message cannot be loaded, wrong index"));
184 }
185 }
186
187 bool ArticleListNotification::eventFilter(QObject* watched, QEvent* event) {
188 if (event->type() == QEvent::Type::MouseButtonRelease) {
189 if (dynamic_cast<QMouseEvent*>(event)->button() == Qt::MouseButton::MiddleButton) {
190 openArticleInArticleList();
191 }
192 }
193
194 return BaseToastNotification::eventFilter(watched, event);
195 }