akregator/src

feedstoragedummyimpl.cpp
1 /*
2  This file is part of Akregator.
3 
4  Copyright (C) 2005 Frank Osterfeld <frank.osterfeld@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 "feedstoragedummyimpl.h"
26 #include "storagedummyimpl.h"
27 
28 #include <feed.h>
29 
30 #include <tqmap.h>
31 #include <tqstring.h>
32 #include <tqstringlist.h>
33 #include <tqvaluelist.h>
34 
35 //typedef unsigned int uint;
36 namespace Akregator {
37 namespace Backend {
38 
39 class FeedStorageDummyImpl::FeedStorageDummyImplPrivate
40 {
41  public:
42  class Entry
43  {
44  public:
45  Entry() : guidIsHash(false), guidIsPermaLink(false), status(0), pubDate(0), hash(0) {}
46  StorageDummyImpl* mainStorage;
47  TQValueList<Category> categories;
48  TQString title;
49  TQString description;
50  TQString link;
51  TQString author;
52  TQString commentsLink;
53  bool guidIsHash;
54  bool guidIsPermaLink;
55  int comments;
56  int status;
57  uint pubDate;
58  uint hash;
59  TQStringList tags;
60  bool hasEnclosure;
61  TQString enclosureUrl;
62  TQString enclosureType;
63  int enclosureLength;
64  };
65  TQMap<TQString, Entry> entries;
66 
67  // all tags occurring in the feed
68  TQStringList tags;
69 
70  // tag -> articles index
71  TQMap<TQString, TQStringList > taggedArticles;
72 
73  TQValueList<Category> categories;
74  TQMap<Category, TQStringList> categorizedArticles;
75 
76  Storage* mainStorage;
77  TQString url;
78 };
79 
80 
81 void FeedStorageDummyImpl::convertOldArchive()
82 {
83 }
84 
85 FeedStorageDummyImpl::FeedStorageDummyImpl(const TQString& url, StorageDummyImpl* main) : d(new FeedStorageDummyImplPrivate)
86 {
87  d->url = url;
88  d->mainStorage = main;
89 }
90 
91 FeedStorageDummyImpl::~FeedStorageDummyImpl()
92 {
93  delete d; d = 0;
94 }
95 
96 void FeedStorageDummyImpl::commit()
97 {
98 }
99 
100 void FeedStorageDummyImpl::rollback()
101 {
102 }
103 
104 void FeedStorageDummyImpl::close()
105 {
106 }
107 
108 int FeedStorageDummyImpl::unread()
109 {
110  return d->mainStorage->unreadFor(d->url);
111 }
112 
113 void FeedStorageDummyImpl::setUnread(int unread)
114 {
115  d->mainStorage->setUnreadFor(d->url, unread);
116 }
117 
118 int FeedStorageDummyImpl::totalCount()
119 {
120  return d->mainStorage->totalCountFor(d->url);
121 }
122 
123 void FeedStorageDummyImpl::setTotalCount(int total)
124 {
125  d->mainStorage->setTotalCountFor(d->url, total);
126 }
127 
128 int FeedStorageDummyImpl::lastFetch()
129 {
130  return d->mainStorage->lastFetchFor(d->url);
131 }
132 
133 void FeedStorageDummyImpl::setLastFetch(int lastFetch)
134 {
135  d->mainStorage->setLastFetchFor(d->url, lastFetch);
136 }
137 
138 TQStringList FeedStorageDummyImpl::articles(const TQString& tag)
139 {
140  return tag.isNull() ? TQStringList(d->entries.keys()) : d->taggedArticles[tag];
141 }
142 
143 TQStringList FeedStorageDummyImpl::articles(const Category& cat)
144 {
145  return d->categorizedArticles[cat];
146 }
147 
148 void FeedStorageDummyImpl::addEntry(const TQString& guid)
149 {
150  if (!d->entries.contains(guid))
151  {
152  d->entries[guid] = FeedStorageDummyImplPrivate::Entry();
153  setTotalCount(totalCount()+1);
154  }
155 }
156 
157 bool FeedStorageDummyImpl::contains(const TQString& guid)
158 {
159  return d->entries.contains(guid);
160 }
161 
162 void FeedStorageDummyImpl::deleteArticle(const TQString& guid)
163 {
164  if (!d->entries.contains(guid))
165  return;
166 
167  setDeleted(guid);
168 
169  d->entries.remove(guid);
170 }
171 
172 int FeedStorageDummyImpl::comments(const TQString& guid)
173 {
174 
175  return contains(guid) ? d->entries[guid].comments : 0;
176 }
177 
178 TQString FeedStorageDummyImpl::commentsLink(const TQString& guid)
179 {
180  return contains(guid) ? d->entries[guid].commentsLink : "";
181 }
182 
183 bool FeedStorageDummyImpl::guidIsHash(const TQString& guid)
184 {
185  return contains(guid) ? d->entries[guid].guidIsHash : false;
186 }
187 
188 bool FeedStorageDummyImpl::guidIsPermaLink(const TQString& guid)
189 {
190  return contains(guid) ? d->entries[guid].guidIsPermaLink : false;
191 }
192 
193 uint FeedStorageDummyImpl::hash(const TQString& guid)
194 {
195  return contains(guid) ? d->entries[guid].hash : 0;
196 }
197 
198 
199 void FeedStorageDummyImpl::setDeleted(const TQString& guid)
200 {
201  if (!contains(guid))
202  return;
203 
204  FeedStorageDummyImplPrivate::Entry entry = d->entries[guid];
205 
206  // remove article from tag->article index
207  TQStringList::ConstIterator it = entry.tags.begin();
208  TQStringList::ConstIterator end = entry.tags.end();
209 
210  for ( ; it != end; ++it)
211  {
212  d->taggedArticles[*it].remove(guid);
213  if (d->taggedArticles[*it].count() == 0)
214  d->tags.remove(*it);
215  }
216 
217  // remove article from tag->category index
218  TQValueList<Category>::ConstIterator it2 = entry.categories.begin();
219  TQValueList<Category>::ConstIterator end2 = entry.categories.end();
220 
221  for ( ; it2 != end2; ++it2)
222  {
223  d->categorizedArticles[*it2].remove(guid);
224  if (d->categorizedArticles[*it2].count() == 0)
225  d->categories.remove(*it2);
226  }
227 
228  entry.description = "";
229  entry.title = "";
230  entry.link = "";
231  entry.commentsLink = "";
232 }
233 
234 TQString FeedStorageDummyImpl::link(const TQString& guid)
235 {
236  return contains(guid) ? d->entries[guid].link : "";
237 }
238 
239 uint FeedStorageDummyImpl::pubDate(const TQString& guid)
240 {
241  return contains(guid) ? d->entries[guid].pubDate : 0;
242 }
243 
244 int FeedStorageDummyImpl::status(const TQString& guid)
245 {
246  return contains(guid) ? d->entries[guid].status : 0;
247 }
248 
249 void FeedStorageDummyImpl::setStatus(const TQString& guid, int status)
250 {
251  if (contains(guid))
252  d->entries[guid].status = status;
253 }
254 
255 TQString FeedStorageDummyImpl::title(const TQString& guid)
256 {
257  return contains(guid) ? d->entries[guid].title : "";
258 }
259 
260 TQString FeedStorageDummyImpl::description(const TQString& guid)
261 {
262  return contains(guid) ? d->entries[guid].description : "";
263 }
264 
265 
266 void FeedStorageDummyImpl::setPubDate(const TQString& guid, uint pubdate)
267 {
268  if (contains(guid))
269  d->entries[guid].pubDate = pubdate;
270 }
271 
272 void FeedStorageDummyImpl::setGuidIsHash(const TQString& guid, bool isHash)
273 {
274  if (contains(guid))
275  d->entries[guid].guidIsHash = isHash;
276 }
277 
278 void FeedStorageDummyImpl::setLink(const TQString& guid, const TQString& link)
279 {
280  if (contains(guid))
281  d->entries[guid].link = link;
282 }
283 
284 void FeedStorageDummyImpl::setHash(const TQString& guid, uint hash)
285 {
286  if (contains(guid))
287  d->entries[guid].hash = hash;
288 }
289 
290 void FeedStorageDummyImpl::setTitle(const TQString& guid, const TQString& title)
291 {
292  if (contains(guid))
293  d->entries[guid].title = title;
294 }
295 
296 void FeedStorageDummyImpl::setDescription(const TQString& guid, const TQString& description)
297 {
298  if (contains(guid))
299  d->entries[guid].description = description;
300 }
301 
302 void FeedStorageDummyImpl::setCommentsLink(const TQString& guid, const TQString& commentsLink)
303 {
304  if (contains(guid))
305  d->entries[guid].commentsLink = commentsLink;
306 }
307 
308 void FeedStorageDummyImpl::setComments(const TQString& guid, int comments)
309 {
310  if (contains(guid))
311  d->entries[guid].comments = comments;
312 }
313 
314 
315 void FeedStorageDummyImpl::setGuidIsPermaLink(const TQString& guid, bool isPermaLink)
316 {
317  if (contains(guid))
318  d->entries[guid].guidIsPermaLink = isPermaLink;
319 }
320 
321 void FeedStorageDummyImpl::addTag(const TQString& guid, const TQString& tag)
322 {
323  if (contains(guid))
324  {
325  d->entries[guid].tags.append(tag);
326  if (!d->taggedArticles[tag].contains(guid))
327  d->taggedArticles[tag].append(guid);
328  if (!d->tags.contains(tag))
329  d->tags.append(tag);
330  }
331 
332 }
333 
334 void FeedStorageDummyImpl::addCategory(const TQString& guid, const Category& cat)
335 {
336  if (!contains(guid))
337  return;
338 
339  d->entries[guid].categories.append(cat);
340 
341  if (d->categorizedArticles[cat].count() == 0)
342  d->categories.append(cat);
343  d->categorizedArticles[cat].append(guid);
344 }
345 
346 void FeedStorageDummyImpl::setAuthor(const TQString& guid, const TQString& author)
347 {
348  if (contains(guid))
349  d->entries[guid].author = author;
350 }
351 
352 TQString FeedStorageDummyImpl::author(const TQString& guid)
353 {
354  return contains(guid) ? d->entries[guid].author : TQString();
355 }
356 
357 TQValueList<Category> FeedStorageDummyImpl::categories(const TQString& guid)
358 {
359  if (!guid.isNull())
360  return contains(guid) ? d->entries[guid].categories : TQValueList<Category>();
361  else
362  return d->categories;
363 }
364 
365 
366 void FeedStorageDummyImpl::removeTag(const TQString& guid, const TQString& tag)
367 {
368  if (contains(guid))
369  {
370  d->entries[guid].tags.remove(tag);
371  d->taggedArticles[tag].remove(guid);
372  if (d->taggedArticles[tag].count() == 0)
373  d->tags.remove(tag);
374  }
375 }
376 
377 TQStringList FeedStorageDummyImpl::tags(const TQString& guid)
378 {
379  if (!guid.isNull())
380  return contains(guid) ? d->entries[guid].tags : TQStringList();
381  else
382  {
383  return d->tags;
384  }
385 }
386 
387 void FeedStorageDummyImpl::add(FeedStorage* source)
388 {
389  TQStringList articles = source->articles();
390  for (TQStringList::ConstIterator it = articles.begin(); it != articles.end(); ++it)
391  copyArticle(*it, source);
392  setUnread(source->unread());
393  setLastFetch(source->lastFetch());
394  setTotalCount(source->totalCount());
395 }
396 
397 void FeedStorageDummyImpl::copyArticle(const TQString& guid, FeedStorage* source)
398 {
399  if (!contains(guid))
400  addEntry(guid);
401 
402  setComments(guid, source->comments(guid));
403  setCommentsLink(guid, source->commentsLink(guid));
404  setDescription(guid, source->description(guid));
405  setGuidIsHash(guid, source->guidIsHash(guid));
406  setGuidIsPermaLink(guid, source->guidIsPermaLink(guid));
407  setHash(guid, source->hash(guid));
408  setLink(guid, source->link(guid));
409  setPubDate(guid, source->pubDate(guid));
410  setStatus(guid, source->status(guid));
411  setTitle(guid, source->title(guid));
412  TQStringList tags = source->tags(guid);
413 
414  for (TQStringList::ConstIterator it = tags.begin(); it != tags.end(); ++it)
415  addTag(guid, *it);
416 }
417 
418 void FeedStorageDummyImpl::clear()
419 {
420  d->entries.clear();
421  setUnread(0);
422  setTotalCount(0);
423 }
424 
425 void FeedStorageDummyImpl::setEnclosure(const TQString& guid, const TQString& url, const TQString& type, int length)
426 {
427  if (contains(guid))
428  {
429  FeedStorageDummyImplPrivate::Entry entry = d->entries[guid];
430  entry.hasEnclosure = true;
431  entry.enclosureUrl = url;
432  entry.enclosureType = type;
433  entry.enclosureLength = length;
434  }
435 }
436 
437 void FeedStorageDummyImpl::removeEnclosure(const TQString& guid)
438 {
439  if (contains(guid))
440  {
441  FeedStorageDummyImplPrivate::Entry entry = d->entries[guid];
442  entry.hasEnclosure = false;
443  entry.enclosureUrl = TQString();
444  entry.enclosureType = TQString();
445  entry.enclosureLength = -1;
446  }
447 }
448 
449 void FeedStorageDummyImpl::enclosure(const TQString& guid, bool& hasEnclosure, TQString& url, TQString& type, int& length)
450 {
451  if (contains(guid))
452  {
453  FeedStorageDummyImplPrivate::Entry entry = d->entries[guid];
454  hasEnclosure = entry.hasEnclosure;
455  url = entry.enclosureUrl;
456  type = entry.enclosureType;
457  length = entry.enclosureLength;
458  }
459  else
460  {
461  hasEnclosure = false;
462  url = TQString();
463  type = TQString();
464  length = -1;
465  }
466 }
467 
468 } // namespace Backend
469 } // namespace Akregator