Debian Codegraph @

src/librssguard/services/abstract/feed.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "services/abstract/feed.h"
4
5 #include "3rd-party/boolinq/boolinq.h"
6 #include "database/databasequeries.h"
7 #include "definitions/definitions.h"
8 #include "miscellaneous/application.h"
9 #include "miscellaneous/feedreader.h"
10 #include "miscellaneous/settings.h"
11 #include "miscellaneous/textfactory.h"
12 #include "services/abstract/cacheforserviceroot.h"
13 #include "services/abstract/gui/formfeeddetails.h"
14 #include "services/abstract/serviceroot.h"
15
16 Feed::Feed(RootItem* parent)
17 : RootItem(parent), m_source(QString()), m_status(Status::Normal), m_statusString(QString()),
18 m_autoUpdateType(AutoUpdateType::DefaultAutoUpdate), m_autoUpdateInterval(DEFAULT_AUTO_UPDATE_INTERVAL),
19 m_lastUpdated(QDateTime::currentDateTimeUtc()), m_isSwitchedOff(false), m_isQuiet(false),
20 m_openArticlesDirectly(false), m_rtlBehavior(RtlBehavior::NoRtl),
21 m_messageFilters(QList<QPointer<MessageFilter>>()) {
22 setKind(RootItem::Kind::Feed);
23 }
24
25 Feed::Feed(const QString& title, const QString& custom_id, const QIcon& icon, RootItem* parent) : Feed(parent) {
26 setTitle(title);
27 setCustomId(custom_id);
28 setIcon(icon);
29 }
30
31 Feed::Feed(const Feed& other) : RootItem(other) {
32 setKind(RootItem::Kind::Feed);
33
34 setCountOfAllMessages(other.countOfAllMessages());
35 setCountOfUnreadMessages(other.countOfUnreadMessages());
36 setSource(other.source());
37 setStatus(other.status(), other.statusString());
38 setAutoUpdateType(other.autoUpdateType());
39 setAutoUpdateInterval(other.autoUpdateInterval());
40 setLastUpdated(other.lastUpdated());
41 setMessageFilters(other.messageFilters());
42 setOpenArticlesDirectly(other.openArticlesDirectly());
43 setArticleIgnoreLimit(Feed::ArticleIgnoreLimit(other.articleIgnoreLimit()));
44 setRtlBehavior(other.rtlBehavior());
45 setIsSwitchedOff(other.isSwitchedOff());
46 setIsQuiet(other.isQuiet());
47 }
48
49 QList<Message> Feed::undeletedMessages() const {
50 QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className());
51
52 return DatabaseQueries::getUndeletedMessagesForFeed(database, customId(), getParentServiceRoot()->accountId());
53 }
54
55 QVariant Feed::data(int column, int role) const {
56 switch (role) {
57 case HIGHLIGHTED_FOREGROUND_TITLE_ROLE:
58 switch (status()) {
59 case Status::NewMessages:
60 return qApp->skins()->colorForModel(SkinEnums::PaletteColors::FgSelectedNewMessages);
61
62 case Status::Normal:
63 if (countOfUnreadMessages() > 0) {
64 return qApp->skins()->colorForModel(SkinEnums::PaletteColors::FgSelectedInteresting);
65 }
66 else {
67 return QVariant();
68 }
69
70 case Status::NetworkError:
71 case Status::ParsingError:
72 case Status::AuthError:
73 case Status::OtherError:
74 return qApp->skins()->colorForModel(SkinEnums::PaletteColors::FgSelectedError);
75
76 default:
77 return QVariant();
78 }
79
80 case TEXT_DIRECTION_ROLE: {
81 if (column == FDS_MODEL_TITLE_INDEX) {
82 return rtlBehavior() == RtlBehavior::Everywhere ? Qt::LayoutDirection::RightToLeft
83 : Qt::LayoutDirection::LayoutDirectionAuto;
84 }
85 else {
86 return Qt::LayoutDirection::LayoutDirectionAuto;
87 }
88 }
89
90 case Qt::ItemDataRole::ForegroundRole: {
91 if (isSwitchedOff()) {
92 return qApp->skins()->colorForModel(SkinEnums::PaletteColors::FgDisabledFeed);
93 }
94
95 switch (status()) {
96 case Status::NewMessages:
97 return qApp->skins()->colorForModel(SkinEnums::PaletteColors::FgNewMessages);
98
99 case Status::Normal:
100 if (countOfUnreadMessages() > 0) {
101 return qApp->skins()->colorForModel(SkinEnums::PaletteColors::FgInteresting);
102 }
103 else {
104 return QVariant();
105 }
106
107 case Status::NetworkError:
108 case Status::ParsingError:
109 case Status::AuthError:
110 case Status::OtherError:
111 return qApp->skins()->colorForModel(SkinEnums::PaletteColors::FgError);
112
113 default:
114 return QVariant();
115 }
116 }
117
118 default:
119 return RootItem::data(column, role);
120 }
121 }
122
123 int Feed::autoUpdateInterval() const {
124 return m_autoUpdateInterval;
125 }
126
127 int Feed::countOfAllMessages() const {
128 return m_totalCount;
129 }
130
131 int Feed::countOfUnreadMessages() const {
132 return m_unreadCount;
133 }
134
135 QVariantHash Feed::customDatabaseData() const {
136 return {};
137 }
138
139 void Feed::setCustomDatabaseData(const QVariantHash& data) {
140 Q_UNUSED(data)
141 }
142
143 void Feed::setCountOfAllMessages(int count_all_messages) {
144 m_totalCount = count_all_messages;
145 }
146
147 void Feed::setCountOfUnreadMessages(int count_unread_messages) {
148 if (status() == Status::NewMessages && count_unread_messages < m_unreadCount) {
149 setStatus(Status::Normal);
150 }
151
152 m_unreadCount = count_unread_messages;
153 }
154
155 bool Feed::canBeEdited() const {
156 return true;
157 }
158
159 bool Feed::isFetching() const {
160 return m_status == Status::Fetching;
161 }
162
163 void Feed::setAutoUpdateInterval(int auto_update_interval) {
164 // If new initial auto-update interval is set, then
165 // we should reset time that remains to the next auto-update.
166 m_autoUpdateInterval = auto_update_interval;
167 m_lastUpdated = QDateTime::currentDateTimeUtc();
168 }
169
170 Feed::AutoUpdateType Feed::autoUpdateType() const {
171 return m_autoUpdateType;
172 }
173
174 void Feed::setAutoUpdateType(Feed::AutoUpdateType auto_update_type) {
175 m_autoUpdateType = auto_update_type;
176 }
177
178 Feed::Status Feed::status() const {
179 return m_status;
180 }
181
182 void Feed::setStatus(Feed::Status status, const QString& status_text) {
183 m_status = status;
184 m_statusString = status_text;
185 }
186
187 QString Feed::source() const {
188 return m_source;
189 }
190
191 void Feed::setSource(const QString& source) {
192 m_source = source;
193 }
194
195 bool Feed::openArticlesDirectly() const {
196 return m_openArticlesDirectly;
197 }
198
199 void Feed::setOpenArticlesDirectly(bool opn) {
200 m_openArticlesDirectly = opn;
201 }
202
203 RtlBehavior Feed::rtlBehavior() const {
204 return m_rtlBehavior;
205 }
206
207 void Feed::setRtlBehavior(RtlBehavior rtl) {
208 m_rtlBehavior = rtl;
209 }
210
211 bool Feed::removeUnwantedArticles(QSqlDatabase& db) {
212 Feed::ArticleIgnoreLimit feed_setup = articleIgnoreLimit();
213 Feed::ArticleIgnoreLimit app_setup = Feed::ArticleIgnoreLimit::fromSettings();
214
215 return DatabaseQueries::removeUnwantedArticlesFromFeed(db, this, feed_setup, app_setup);
216 }
217
218 void Feed::appendMessageFilter(MessageFilter* filter) {
219 removeMessageFilter(filter);
220 m_messageFilters.append(QPointer<MessageFilter>(filter));
221 }
222
223 void Feed::updateCounts(bool including_total_count) {
224 QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className());
225 int account_id = getParentServiceRoot()->accountId();
226 auto fc = DatabaseQueries::getMessageCountsForFeed(database, customId(), account_id);
227
228 if (including_total_count) {
229 setCountOfAllMessages(fc.m_total);
230 }
231
232 setCountOfUnreadMessages(fc.m_unread);
233 }
234
235 bool Feed::cleanMessages(bool clean_read_only) {
236 return getParentServiceRoot()->cleanFeeds(QList<Feed*>() << this, clean_read_only);
237 }
238
239 bool Feed::markAsReadUnread(RootItem::ReadStatus status) {
240 ServiceRoot* service = getParentServiceRoot();
241 auto* cache = dynamic_cast<CacheForServiceRoot*>(service);
242
243 if (cache != nullptr) {
244 cache->addMessageStatesToCache(service->customIDSOfMessagesForItem(this, status), status);
245 }
246
247 return service->markFeedsReadUnread({this}, status);
248 }
249
250 QString Feed::getAutoUpdateStatusDescription() const {
251 QString auto_update_string;
252
253 switch (autoUpdateType()) {
254 case AutoUpdateType::DontAutoUpdate:
255 //: Describes feed auto-update status.
256 auto_update_string = tr("does not use auto-fetching of articles");
257 break;
258
259 case AutoUpdateType::DefaultAutoUpdate:
260 //: Describes feed auto-update status.
261 if (qApp->feedReader()->autoUpdateEnabled()) {
262 int secs_to_next =
263 QDateTime::currentDateTimeUtc()
264 .secsTo(qApp->feedReader()->lastAutoUpdate().addSecs(qApp->feedReader()->autoUpdateInterval()));
265
266 auto_update_string =
267 tr("uses global settings (%n minute(s) to next auto-fetch of articles)", nullptr, int(secs_to_next / 60.0));
268 }
269 else {
270 auto_update_string = tr("uses global settings, but global auto-fetching "
271 "of articles is disabled");
272 }
273
274 break;
275
276 case AutoUpdateType::SpecificAutoUpdate:
277 default:
278 int secs_to_next = QDateTime::currentDateTimeUtc().secsTo(lastUpdated().addSecs(autoUpdateInterval()));
279
280 //: Describes feed auto-update status.
281 auto_update_string = tr("uses specific settings (%n minute(s) to next "
282 "auto-fetching of new articles)",
283 nullptr,
284 int(secs_to_next / 60.0));
285 break;
286 }
287
288 return auto_update_string;
289 }
290
291 QString Feed::getStatusDescription() const {
292 switch (m_status) {
293 case Status::Normal:
294 return tr("no errors");
295
296 case Status::NewMessages:
297 return tr("has new articles");
298
299 case Status::AuthError:
300 return tr("authentication error");
301
302 case Status::NetworkError:
303 return tr("network error");
304
305 case Status::ParsingError:
306 return tr("parsing error");
307
308 default:
309 return tr("error");
310 }
311 }
312
313 Feed::ArticleIgnoreLimit& Feed::articleIgnoreLimit() {
314 return m_articleIgnoreLimit;
315 }
316
317 const Feed::ArticleIgnoreLimit& Feed::articleIgnoreLimit() const {
318 return m_articleIgnoreLimit;
319 }
320
321 void Feed::setArticleIgnoreLimit(const ArticleIgnoreLimit& ignore_limit) {
322 m_articleIgnoreLimit = ignore_limit;
323 }
324
325 bool Feed::isQuiet() const {
326 return m_isQuiet;
327 }
328
329 void Feed::setIsQuiet(bool quiet) {
330 m_isQuiet = quiet;
331 }
332
333 QDateTime Feed::lastUpdated() const {
334 return m_lastUpdated;
335 }
336
337 void Feed::setLastUpdated(const QDateTime& last_updated) {
338 m_lastUpdated = last_updated;
339 }
340
341 bool Feed::isSwitchedOff() const {
342 return m_isSwitchedOff;
343 }
344
345 void Feed::setIsSwitchedOff(bool switched_off) {
346 m_isSwitchedOff = switched_off;
347 }
348
349 QString Feed::statusString() const {
350 return m_statusString;
351 }
352
353 QList<QPointer<MessageFilter>> Feed::messageFilters() const {
354 return m_messageFilters;
355 }
356
357 void Feed::setMessageFilters(const QList<QPointer<MessageFilter>>& filters) {
358 m_messageFilters = filters;
359 }
360
361 void Feed::removeMessageFilter(MessageFilter* filter) {
362 m_messageFilters.removeAll(filter);
363 }
364
365 QString Feed::additionalTooltip() const {
366 QString stat = getStatusDescription();
367
368 if (!m_statusString.simplified().isEmpty()) {
369 stat += QSL(" (%1)").arg(m_statusString);
370 }
371
372 auto std_fltrs = boolinq::from(m_messageFilters)
373 .select([](const QPointer<MessageFilter>& pn) {
374 return pn->name();
375 })
376 .toStdList();
377 QStringList fltrs = FROM_STD_LIST(QStringList, std_fltrs);
378
379 return tr("Auto-update status: %1\n"
380 "Active message filters: %2\n"
381 "Status: %3\n"
382 "Source: <a href=\"%4\">%4</a>\n"
383 "Item ID: %5")
384 .arg(getAutoUpdateStatusDescription(),
385 m_messageFilters.size() > 0
386 ? QSL("%1 (%2)").arg(QString::number(m_messageFilters.size()), fltrs.join(QSL(", ")))
387 : QString::number(m_messageFilters.size()),
388 stat,
389 m_source,
390 customId());
391 }
392
393 Qt::ItemFlags Feed::additionalFlags() const {
394 return Qt::ItemFlag::ItemNeverHasChildren;
395 }
396
397 Feed::ArticleIgnoreLimit Feed::ArticleIgnoreLimit::fromSettings() {
398 Feed::ArticleIgnoreLimit art_limit;
399
400 art_limit.m_avoidOldArticles = qApp->settings()->value(GROUP(Messages), SETTING(Messages::AvoidOldArticles)).toBool();
401 art_limit.m_dtToAvoid =
402 qApp->settings()->value(GROUP(Messages), SETTING(Messages::DateTimeToAvoidArticle)).toDateTime();
403 art_limit.m_hoursToAvoid = qApp->settings()->value(GROUP(Messages), SETTING(Messages::HoursToAvoidArticle)).toInt();
404
405 art_limit.m_doNotRemoveStarred =
406 qApp->settings()->value(GROUP(Messages), SETTING(Messages::LimitDoNotRemoveStarred)).toBool();
407 art_limit.m_doNotRemoveUnread =
408 qApp->settings()->value(GROUP(Messages), SETTING(Messages::LimitDoNotRemoveUnread)).toBool();
409 art_limit.m_keepCountOfArticles =
410 qApp->settings()->value(GROUP(Messages), SETTING(Messages::LimitCountOfArticles)).toInt();
411 art_limit.m_moveToBinDontPurge =
412 qApp->settings()->value(GROUP(Messages), SETTING(Messages::LimitRecycleInsteadOfPurging)).toBool();
413
414 return art_limit;
415 }