akregator/src

notificationmanager.cpp

00001 /*
00002     This file is part of Akregator.
00003 
00004     Copyright (C) 2005 Frank Osterfeld <frank.osterfeld at kdemail.net>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
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 } // namespace Akregator
00142 
00143 #include "notificationmanager.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys