akregator/src

notificationmanager.cpp
1 /*
2  This file is part of Akregator.
3 
4  Copyright (C) 2005 Frank Osterfeld <frank.osterfeld at kdemail.net>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 #include <tdelocale.h>
26 #include <knotifyclient.h>
27 #include <kstaticdeleter.h>
28 #include <kurl.h>
29 
30 #include <tqlabel.h>
31 #include <tqtimer.h>
32 
33 #include "feed.h"
34 #include "notificationmanager.h"
35 
36 namespace Akregator {
37 
38 NotificationManager::NotificationManager() : TQObject()
39 {
40  m_intervalsLapsed = 0;
41  m_checkInterval = 2000;
42  m_maxIntervals = 10;
43  m_running = false;
44  m_addedInLastInterval = false;
45  m_maxArticles = 20;
46  m_widget = NULL;
47  m_instance = NULL;
48 }
49 
50 NotificationManager::~NotificationManager()
51 {
52  m_self = 0;
53 }
54 
55 void NotificationManager::setWidget(TQWidget* widget, TDEInstance* inst)
56 {
57  m_widget = widget;
58  m_instance = inst != NULL ? inst : TDEGlobal::instance();
59 }
60 
61 void NotificationManager::slotNotifyArticle(const Article& article)
62 {
63  m_articles.append(article);
64  m_addedInLastInterval = true;
65  if (m_articles.count() >= m_maxArticles)
66  doNotify();
67  else if (!m_running)
68  {
69  m_running = true;
70  TQTimer::singleShot(m_checkInterval, this, TQ_SLOT(slotIntervalCheck()));
71  }
72 }
73 
74 void NotificationManager::slotNotifyFeeds(const TQStringList& feeds)
75 {
76  if (feeds.count() == 1)
77  {
78  KNotifyClient::Instance inst(m_instance);
79  KNotifyClient::event(m_widget->winId(), "feed_added", i18n("Feed added:\n %1").arg(feeds[0]));
80  }
81  else if (feeds.count() > 1)
82  {
83  TQString message;
84  for (TQStringList::ConstIterator it = feeds.begin(); it != feeds.end(); ++it)
85  message += *it + "\n";
86  KNotifyClient::Instance inst(m_instance);
87  KNotifyClient::event(m_widget->winId(), "feed_added", i18n("Feeds added:\n %1").arg(message));
88  }
89 }
90 
91 void NotificationManager::doNotify()
92 {
93  TQString message = "<html><body>";
94  TQString feedTitle;
95  TQValueList<Article>::ConstIterator it = m_articles.begin();
96  TQValueList<Article>::ConstIterator en = m_articles.end();
97  for (; it != en; ++it)
98  {
99  if (feedTitle != (*it).feed()->title())
100  {
101  feedTitle = (*it).feed()->title();
102  message += TQString("<p><b>%1:</b></p>").arg(feedTitle);
103  }
104  message += (*it).title() + "<br>";
105  }
106  message += "</body></html>";
107  KNotifyClient::Instance inst(m_instance);
108  KNotifyClient::event(m_widget->winId(), "new_articles", message);
109 
110  m_articles.clear();
111  m_running = false;
112  m_intervalsLapsed = 0;
113  m_addedInLastInterval = false;
114 }
115 
116 void NotificationManager::slotIntervalCheck()
117 {
118  if (!m_running)
119  return;
120  m_intervalsLapsed++;
121  if (!m_addedInLastInterval || m_articles.count() >= m_maxArticles || m_intervalsLapsed >= m_maxIntervals)
122  doNotify();
123  else
124  {
125  m_addedInLastInterval = false;
126  TQTimer::singleShot(m_checkInterval, this, TQ_SLOT(slotIntervalCheck()));
127  }
128 
129 }
130 
131 NotificationManager* NotificationManager::m_self;
132 static KStaticDeleter<NotificationManager> notificationmanagersd;
133 
134 NotificationManager* NotificationManager::self()
135 {
136  if (!m_self)
137  m_self = notificationmanagersd.setObject(m_self, new NotificationManager);
138  return m_self;
139 }
140 
141 } // namespace Akregator
142 
143 #include "notificationmanager.moc"
A proxy class for RSS::Article with some additional methods to assist sorting.
Definition: article.h:58
this class collects notification requests (new articles etc.) and processes them using KNotify.