akregator/src

fetchqueue.cpp
1 /*
2  This file is part of Akregator.
3 
4  Copyright (C) 2004 Sashmit Bhaduri <smt@vfemail.net>
5  2005 Frank Osterfeld <frank.osterfeld at kdemail.net>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, permission is given to link this program
22  with any edition of TQt, and distribute the resulting executable,
23  without including the source code for TQt in the source distribution.
24 */
25 
26 #include <tqvaluelist.h>
27 
28 #include "akregatorconfig.h"
29 #include "fetchqueue.h"
30 #include "feed.h"
31 #include "treenode.h"
32 
33 
34 namespace Akregator {
35 
36 class FetchQueue::FetchQueuePrivate
37 {
38  public:
39 
40  TQValueList<Feed*> queuedFeeds;
41  TQValueList<Feed*> fetchingFeeds;
42 };
43 
44 
45 FetchQueue::FetchQueue(TQObject* parent, const char* name): TQObject(parent, name), d(new FetchQueuePrivate) {}
46 
47 
48 FetchQueue::~FetchQueue()
49 {
50  slotAbort();
51  delete d;
52  d = 0;
53 }
54 
55 void FetchQueue::slotAbort()
56 {
57  for (TQValueList<Feed*>::Iterator it = d->fetchingFeeds.begin(); it != d->fetchingFeeds.end(); ++it)
58  {
59  disconnectFromFeed(*it);
60  (*it)->slotAbortFetch();
61  }
62  d->fetchingFeeds.clear();
63 
64  for (TQValueList<Feed*>::Iterator it = d->queuedFeeds.begin(); it != d->queuedFeeds.end(); ++it)
65  {
66  disconnectFromFeed(*it);
67  }
68  d->queuedFeeds.clear();
69 
70  emit signalStopped();
71 }
72 
73 void FetchQueue::addFeed(Feed *f)
74 {
75  if (!d->queuedFeeds.contains(f) && !d->fetchingFeeds.contains(f))
76  {
77  connectToFeed(f);
78  d->queuedFeeds.append(f);
79  fetchNextFeed();
80  }
81 }
82 
83 void FetchQueue::fetchNextFeed()
84 {
85  if (!d->queuedFeeds.isEmpty() && d->fetchingFeeds.count() < Settings::concurrentFetches())
86  {
87  if (d->fetchingFeeds.isEmpty() && d->queuedFeeds.count() == 1)
88  emit signalStarted();
89  Feed* f = *(d->queuedFeeds.begin());
90  d->queuedFeeds.pop_front();
91  d->fetchingFeeds.append(f);
92  f->fetch(false);
93 
94  }
95 }
96 
97 void FetchQueue::slotFeedFetched(Feed *f)
98 {
99  emit fetched(f);
100  feedDone(f);
101 }
102 
103 void FetchQueue::slotFetchError(Feed *f)
104 {
105  emit fetchError(f);
106  feedDone(f);
107 }
108 
109 void FetchQueue::slotFetchAborted(Feed *f)
110 {
111  emit fetched(f); // FIXME: better use a signal like signalAborted(Feed*)
112  feedDone(f);
113 }
114 
115 bool FetchQueue::isEmpty() const
116 {
117  return d->queuedFeeds.isEmpty() && d->fetchingFeeds.isEmpty();
118 }
119 
120 void FetchQueue::feedDone(Feed *f)
121 {
122  disconnectFromFeed(f);
123  d->fetchingFeeds.remove(f);
124  if (isEmpty())
125  emit signalStopped();
126  else
127  fetchNextFeed();
128 }
129 
130 void FetchQueue::connectToFeed(Feed* feed)
131 {
132  connect (feed, TQ_SIGNAL(fetched(Feed*)), this, TQ_SLOT(slotFeedFetched(Feed*)));
133  connect (feed, TQ_SIGNAL(fetchError(Feed*)), this, TQ_SLOT(slotFetchError(Feed*)));
134  connect (feed, TQ_SIGNAL(fetchAborted(Feed*)), this, TQ_SLOT(slotFetchAborted(Feed*)));
135  connect (feed, TQ_SIGNAL(signalDestroyed(TreeNode*)), this, TQ_SLOT(slotNodeDestroyed(TreeNode*)));
136 }
137 
138 void FetchQueue::disconnectFromFeed(Feed* feed)
139 {
140  disconnect (feed, TQ_SIGNAL(fetched(Feed*)), this, TQ_SLOT(slotFeedFetched(Feed*)));
141  disconnect (feed, TQ_SIGNAL(fetchError(Feed*)), this, TQ_SLOT(slotFetchError(Feed*)));
142  disconnect (feed, TQ_SIGNAL(fetchAborted(Feed*)), this, TQ_SLOT(slotFetchAborted(Feed*)));
143  disconnect (feed, TQ_SIGNAL(signalDestroyed(TreeNode*)), this, TQ_SLOT(slotNodeDestroyed(TreeNode*)));
144 }
145 
146 
147 void FetchQueue::slotNodeDestroyed(TreeNode* node)
148 {
149  Feed* feed = dynamic_cast<Feed*> (node);
150 
151  if (feed)
152  {
153  d->fetchingFeeds.remove(feed);
154  d->queuedFeeds.remove(feed);
155  }
156 }
157 
158 } // namespace Akregator
159 
160 #include "fetchqueue.moc"