akregator/src
notificationmanager.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <klocale.h>
00026 #include <knotifyclient.h>
00027 #include <kstaticdeleter.h>
00028 #include <kurl.h>
00029
00030 #include <qlabel.h>
00031 #include <qtimer.h>
00032
00033 #include "feed.h"
00034 #include "notificationmanager.h"
00035
00036 namespace Akregator {
00037
00038 NotificationManager::NotificationManager() : QObject()
00039 {
00040 m_intervalsLapsed = 0;
00041 m_checkInterval = 2000;
00042 m_maxIntervals = 10;
00043 m_running = false;
00044 m_addedInLastInterval = false;
00045 m_maxArticles = 20;
00046 m_widget = NULL;
00047 m_instance = NULL;
00048 }
00049
00050 NotificationManager::~NotificationManager()
00051 {
00052 m_self = 0;
00053 }
00054
00055 void NotificationManager::setWidget(QWidget* widget, KInstance* inst)
00056 {
00057 m_widget = widget;
00058 m_instance = inst != NULL ? inst : KGlobal::instance();
00059 }
00060
00061 void NotificationManager::slotNotifyArticle(const Article& article)
00062 {
00063 m_articles.append(article);
00064 m_addedInLastInterval = true;
00065 if (m_articles.count() >= m_maxArticles)
00066 doNotify();
00067 else if (!m_running)
00068 {
00069 m_running = true;
00070 QTimer::singleShot(m_checkInterval, this, SLOT(slotIntervalCheck()));
00071 }
00072 }
00073
00074 void NotificationManager::slotNotifyFeeds(const QStringList& feeds)
00075 {
00076 if (feeds.count() == 1)
00077 {
00078 KNotifyClient::Instance inst(m_instance);
00079 KNotifyClient::event(m_widget->winId(), "feed_added", i18n("Feed added:\n %1").arg(feeds[0]));
00080 }
00081 else if (feeds.count() > 1)
00082 {
00083 QString message;
00084 for (QStringList::ConstIterator it = feeds.begin(); it != feeds.end(); ++it)
00085 message += *it + "\n";
00086 KNotifyClient::Instance inst(m_instance);
00087 KNotifyClient::event(m_widget->winId(), "feed_added", i18n("Feeds added:\n %1").arg(message));
00088 }
00089 }
00090
00091 void NotificationManager::doNotify()
00092 {
00093 QString message = "<html><body>";
00094 QString feedTitle;
00095 QValueList<Article>::ConstIterator it = m_articles.begin();
00096 QValueList<Article>::ConstIterator en = m_articles.end();
00097 for (; it != en; ++it)
00098 {
00099 if (feedTitle != (*it).feed()->title())
00100 {
00101 feedTitle = (*it).feed()->title();
00102 message += QString("<p><b>%1:</b></p>").arg(feedTitle);
00103 }
00104 message += (*it).title() + "<br>";
00105 }
00106 message += "</body></html>";
00107 KNotifyClient::Instance inst(m_instance);
00108 KNotifyClient::event(m_widget->winId(), "new_articles", message);
00109
00110 m_articles.clear();
00111 m_running = false;
00112 m_intervalsLapsed = 0;
00113 m_addedInLastInterval = false;
00114 }
00115
00116 void NotificationManager::slotIntervalCheck()
00117 {
00118 if (!m_running)
00119 return;
00120 m_intervalsLapsed++;
00121 if (!m_addedInLastInterval || m_articles.count() >= m_maxArticles || m_intervalsLapsed >= m_maxIntervals)
00122 doNotify();
00123 else
00124 {
00125 m_addedInLastInterval = false;
00126 QTimer::singleShot(m_checkInterval, this, SLOT(slotIntervalCheck()));
00127 }
00128
00129 }
00130
00131 NotificationManager* NotificationManager::m_self;
00132 static KStaticDeleter<NotificationManager> notificationmanagersd;
00133
00134 NotificationManager* NotificationManager::self()
00135 {
00136 if (!m_self)
00137 m_self = notificationmanagersd.setObject(m_self, new NotificationManager);
00138 return m_self;
00139 }
00140
00141 }
00142
00143 #include "notificationmanager.moc"
|