File indexing completed on 2024-04-28 05:11:02
0001 /* 0002 This file is part of Akregator. 0003 0004 SPDX-FileCopyrightText: 2005 Frank Osterfeld <osterfeld@kde.org> 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0 0007 */ 0008 0009 #include "notificationmanager.h" 0010 #include "feed.h" 0011 0012 #include <KAboutData> 0013 #include <KLocalizedString> 0014 #include <KNotification> 0015 #include <QTimer> 0016 0017 using namespace Akregator; 0018 NotificationManager::NotificationManager(QObject *parent) 0019 : QObject(parent) 0020 { 0021 } 0022 0023 NotificationManager::~NotificationManager() 0024 { 0025 m_self = nullptr; 0026 } 0027 0028 void NotificationManager::setWidget(QWidget *widget, const QString &componentName) 0029 { 0030 m_widget = widget; 0031 m_componantName = componentName.isEmpty() ? KAboutData::applicationData().componentName() : componentName; 0032 } 0033 0034 void NotificationManager::slotNotifyArticle(const Article &article) 0035 { 0036 m_articles.append(article); 0037 m_addedInLastInterval = true; 0038 if (!m_running) { 0039 m_running = true; 0040 QTimer::singleShot(m_checkInterval, this, &NotificationManager::slotIntervalCheck); 0041 } 0042 } 0043 0044 void NotificationManager::slotNotifyFeeds(const QStringList &feeds) 0045 { 0046 const int feedsCount(feeds.count()); 0047 if (feedsCount == 1) { 0048 KNotification::event(QStringLiteral("FeedAdded"), i18n("Feed added:\n %1", feeds[0]), QPixmap(), KNotification::CloseOnTimeout, m_componantName); 0049 } else if (feedsCount > 1) { 0050 QString message; 0051 QStringList::ConstIterator end = feeds.constEnd(); 0052 for (QStringList::ConstIterator it = feeds.constBegin(); it != end; ++it) { 0053 message += *it + QLatin1Char('\n'); 0054 } 0055 KNotification::event(QStringLiteral("FeedAdded"), i18n("Feeds added:\n %1", message), QPixmap(), KNotification::CloseOnTimeout, m_componantName); 0056 } 0057 } 0058 0059 void NotificationManager::doNotify() 0060 { 0061 QString message = QStringLiteral("<html><body>"); 0062 QString feedTitle; 0063 int entriesCount = 1; 0064 const int maxNewArticlesShown = 2; 0065 0066 // adding information about how many new articles 0067 auto feedClosure = [&entriesCount, &message]() { 0068 if ((entriesCount - maxNewArticlesShown) > 1) { 0069 message += i18np("<i>and 1 other</i>", "<i>and %1 others</i>", entriesCount - maxNewArticlesShown - 1) + QLatin1StringView("<br>"); 0070 } 0071 }; 0072 0073 for (const Article &i : std::as_const(m_articles)) { 0074 const QString currentFeedTitle(i.feed()->title()); 0075 if (feedTitle != currentFeedTitle) { 0076 // closing previous feed, if any, and resetting the counter 0077 feedClosure(); 0078 entriesCount = 1; 0079 0080 // starting a new feed 0081 feedTitle = currentFeedTitle; 0082 message += QStringLiteral("<p><b>%1:</b></p>").arg(feedTitle); 0083 } 0084 // check not exceeding maxNewArticlesShown per feed 0085 if (entriesCount <= maxNewArticlesShown) { 0086 message += i.title() + QLatin1StringView("<br>"); 0087 } 0088 entriesCount++; 0089 } 0090 feedClosure(); 0091 message += QLatin1StringView("</body></html>"); 0092 KNotification::event(QStringLiteral("NewArticles"), message, QPixmap(), KNotification::CloseOnTimeout, m_componantName); 0093 0094 m_articles.clear(); 0095 m_running = false; 0096 m_intervalsLapsed = 0; 0097 m_addedInLastInterval = false; 0098 } 0099 0100 void NotificationManager::slotIntervalCheck() 0101 { 0102 if (!m_running) { 0103 return; 0104 } 0105 m_intervalsLapsed++; 0106 if (!m_addedInLastInterval || m_articles.count() >= m_maxArticles || m_intervalsLapsed >= m_maxIntervals) { 0107 doNotify(); 0108 } else { 0109 m_addedInLastInterval = false; 0110 QTimer::singleShot(m_checkInterval, this, &NotificationManager::slotIntervalCheck); 0111 } 0112 } 0113 0114 NotificationManager *NotificationManager::m_self = nullptr; 0115 0116 NotificationManager *NotificationManager::self() 0117 { 0118 static NotificationManager self; 0119 if (!m_self) { 0120 m_self = &self; 0121 } 0122 return m_self; 0123 } 0124 0125 #include "moc_notificationmanager.cpp"