feedstoragedummyimpl.cpp
00001 /* 00002 This file is part of Akregator. 00003 00004 Copyright (C) 2005 Frank Osterfeld <frank.osterfeld@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 TQt, and distribute the resulting executable, 00022 without including the source code for TQt in the source distribution. 00023 */ 00024 00025 #include "feedstoragedummyimpl.h" 00026 #include "storagedummyimpl.h" 00027 00028 #include <feed.h> 00029 00030 #include <tqmap.h> 00031 #include <tqstring.h> 00032 #include <tqstringlist.h> 00033 #include <tqvaluelist.h> 00034 00035 //typedef unsigned int uint; 00036 namespace Akregator { 00037 namespace Backend { 00038 00039 class FeedStorageDummyImpl::FeedStorageDummyImplPrivate 00040 { 00041 public: 00042 class Entry 00043 { 00044 public: 00045 Entry() : guidIsHash(false), guidIsPermaLink(false), status(0), pubDate(0), hash(0) {} 00046 StorageDummyImpl* mainStorage; 00047 TQValueList<Category> categories; 00048 TQString title; 00049 TQString description; 00050 TQString link; 00051 TQString author; 00052 TQString commentsLink; 00053 bool guidIsHash; 00054 bool guidIsPermaLink; 00055 int comments; 00056 int status; 00057 uint pubDate; 00058 uint hash; 00059 TQStringList tags; 00060 bool hasEnclosure; 00061 TQString enclosureUrl; 00062 TQString enclosureType; 00063 int enclosureLength; 00064 }; 00065 TQMap<TQString, Entry> entries; 00066 00067 // all tags occurring in the feed 00068 TQStringList tags; 00069 00070 // tag -> articles index 00071 TQMap<TQString, TQStringList > taggedArticles; 00072 00073 TQValueList<Category> categories; 00074 TQMap<Category, TQStringList> categorizedArticles; 00075 00076 Storage* mainStorage; 00077 TQString url; 00078 }; 00079 00080 00081 void FeedStorageDummyImpl::convertOldArchive() 00082 { 00083 } 00084 00085 FeedStorageDummyImpl::FeedStorageDummyImpl(const TQString& url, StorageDummyImpl* main) : d(new FeedStorageDummyImplPrivate) 00086 { 00087 d->url = url; 00088 d->mainStorage = main; 00089 } 00090 00091 FeedStorageDummyImpl::~FeedStorageDummyImpl() 00092 { 00093 delete d; d = 0; 00094 } 00095 00096 void FeedStorageDummyImpl::commit() 00097 { 00098 } 00099 00100 void FeedStorageDummyImpl::rollback() 00101 { 00102 } 00103 00104 void FeedStorageDummyImpl::close() 00105 { 00106 } 00107 00108 int FeedStorageDummyImpl::unread() 00109 { 00110 return d->mainStorage->unreadFor(d->url); 00111 } 00112 00113 void FeedStorageDummyImpl::setUnread(int unread) 00114 { 00115 d->mainStorage->setUnreadFor(d->url, unread); 00116 } 00117 00118 int FeedStorageDummyImpl::totalCount() 00119 { 00120 return d->mainStorage->totalCountFor(d->url); 00121 } 00122 00123 void FeedStorageDummyImpl::setTotalCount(int total) 00124 { 00125 d->mainStorage->setTotalCountFor(d->url, total); 00126 } 00127 00128 int FeedStorageDummyImpl::lastFetch() 00129 { 00130 return d->mainStorage->lastFetchFor(d->url); 00131 } 00132 00133 void FeedStorageDummyImpl::setLastFetch(int lastFetch) 00134 { 00135 d->mainStorage->setLastFetchFor(d->url, lastFetch); 00136 } 00137 00138 TQStringList FeedStorageDummyImpl::articles(const TQString& tag) 00139 { 00140 return tag.isNull() ? TQStringList(d->entries.keys()) : d->taggedArticles[tag]; 00141 } 00142 00143 TQStringList FeedStorageDummyImpl::articles(const Category& cat) 00144 { 00145 return d->categorizedArticles[cat]; 00146 } 00147 00148 void FeedStorageDummyImpl::addEntry(const TQString& guid) 00149 { 00150 if (!d->entries.contains(guid)) 00151 { 00152 d->entries[guid] = FeedStorageDummyImplPrivate::Entry(); 00153 setTotalCount(totalCount()+1); 00154 } 00155 } 00156 00157 bool FeedStorageDummyImpl::contains(const TQString& guid) 00158 { 00159 return d->entries.contains(guid); 00160 } 00161 00162 void FeedStorageDummyImpl::deleteArticle(const TQString& guid) 00163 { 00164 if (!d->entries.contains(guid)) 00165 return; 00166 00167 setDeleted(guid); 00168 00169 d->entries.remove(guid); 00170 } 00171 00172 int FeedStorageDummyImpl::comments(const TQString& guid) 00173 { 00174 00175 return contains(guid) ? d->entries[guid].comments : 0; 00176 } 00177 00178 TQString FeedStorageDummyImpl::commentsLink(const TQString& guid) 00179 { 00180 return contains(guid) ? d->entries[guid].commentsLink : ""; 00181 } 00182 00183 bool FeedStorageDummyImpl::guidIsHash(const TQString& guid) 00184 { 00185 return contains(guid) ? d->entries[guid].guidIsHash : false; 00186 } 00187 00188 bool FeedStorageDummyImpl::guidIsPermaLink(const TQString& guid) 00189 { 00190 return contains(guid) ? d->entries[guid].guidIsPermaLink : false; 00191 } 00192 00193 uint FeedStorageDummyImpl::hash(const TQString& guid) 00194 { 00195 return contains(guid) ? d->entries[guid].hash : 0; 00196 } 00197 00198 00199 void FeedStorageDummyImpl::setDeleted(const TQString& guid) 00200 { 00201 if (!contains(guid)) 00202 return; 00203 00204 FeedStorageDummyImplPrivate::Entry entry = d->entries[guid]; 00205 00206 // remove article from tag->article index 00207 TQStringList::ConstIterator it = entry.tags.begin(); 00208 TQStringList::ConstIterator end = entry.tags.end(); 00209 00210 for ( ; it != end; ++it) 00211 { 00212 d->taggedArticles[*it].remove(guid); 00213 if (d->taggedArticles[*it].count() == 0) 00214 d->tags.remove(*it); 00215 } 00216 00217 // remove article from tag->category index 00218 TQValueList<Category>::ConstIterator it2 = entry.categories.begin(); 00219 TQValueList<Category>::ConstIterator end2 = entry.categories.end(); 00220 00221 for ( ; it2 != end2; ++it2) 00222 { 00223 d->categorizedArticles[*it2].remove(guid); 00224 if (d->categorizedArticles[*it2].count() == 0) 00225 d->categories.remove(*it2); 00226 } 00227 00228 entry.description = ""; 00229 entry.title = ""; 00230 entry.link = ""; 00231 entry.commentsLink = ""; 00232 } 00233 00234 TQString FeedStorageDummyImpl::link(const TQString& guid) 00235 { 00236 return contains(guid) ? d->entries[guid].link : ""; 00237 } 00238 00239 uint FeedStorageDummyImpl::pubDate(const TQString& guid) 00240 { 00241 return contains(guid) ? d->entries[guid].pubDate : 0; 00242 } 00243 00244 int FeedStorageDummyImpl::status(const TQString& guid) 00245 { 00246 return contains(guid) ? d->entries[guid].status : 0; 00247 } 00248 00249 void FeedStorageDummyImpl::setStatus(const TQString& guid, int status) 00250 { 00251 if (contains(guid)) 00252 d->entries[guid].status = status; 00253 } 00254 00255 TQString FeedStorageDummyImpl::title(const TQString& guid) 00256 { 00257 return contains(guid) ? d->entries[guid].title : ""; 00258 } 00259 00260 TQString FeedStorageDummyImpl::description(const TQString& guid) 00261 { 00262 return contains(guid) ? d->entries[guid].description : ""; 00263 } 00264 00265 00266 void FeedStorageDummyImpl::setPubDate(const TQString& guid, uint pubdate) 00267 { 00268 if (contains(guid)) 00269 d->entries[guid].pubDate = pubdate; 00270 } 00271 00272 void FeedStorageDummyImpl::setGuidIsHash(const TQString& guid, bool isHash) 00273 { 00274 if (contains(guid)) 00275 d->entries[guid].guidIsHash = isHash; 00276 } 00277 00278 void FeedStorageDummyImpl::setLink(const TQString& guid, const TQString& link) 00279 { 00280 if (contains(guid)) 00281 d->entries[guid].link = link; 00282 } 00283 00284 void FeedStorageDummyImpl::setHash(const TQString& guid, uint hash) 00285 { 00286 if (contains(guid)) 00287 d->entries[guid].hash = hash; 00288 } 00289 00290 void FeedStorageDummyImpl::setTitle(const TQString& guid, const TQString& title) 00291 { 00292 if (contains(guid)) 00293 d->entries[guid].title = title; 00294 } 00295 00296 void FeedStorageDummyImpl::setDescription(const TQString& guid, const TQString& description) 00297 { 00298 if (contains(guid)) 00299 d->entries[guid].description = description; 00300 } 00301 00302 void FeedStorageDummyImpl::setCommentsLink(const TQString& guid, const TQString& commentsLink) 00303 { 00304 if (contains(guid)) 00305 d->entries[guid].commentsLink = commentsLink; 00306 } 00307 00308 void FeedStorageDummyImpl::setComments(const TQString& guid, int comments) 00309 { 00310 if (contains(guid)) 00311 d->entries[guid].comments = comments; 00312 } 00313 00314 00315 void FeedStorageDummyImpl::setGuidIsPermaLink(const TQString& guid, bool isPermaLink) 00316 { 00317 if (contains(guid)) 00318 d->entries[guid].guidIsPermaLink = isPermaLink; 00319 } 00320 00321 void FeedStorageDummyImpl::addTag(const TQString& guid, const TQString& tag) 00322 { 00323 if (contains(guid)) 00324 { 00325 d->entries[guid].tags.append(tag); 00326 if (!d->taggedArticles[tag].contains(guid)) 00327 d->taggedArticles[tag].append(guid); 00328 if (!d->tags.contains(tag)) 00329 d->tags.append(tag); 00330 } 00331 00332 } 00333 00334 void FeedStorageDummyImpl::addCategory(const TQString& guid, const Category& cat) 00335 { 00336 if (!contains(guid)) 00337 return; 00338 00339 d->entries[guid].categories.append(cat); 00340 00341 if (d->categorizedArticles[cat].count() == 0) 00342 d->categories.append(cat); 00343 d->categorizedArticles[cat].append(guid); 00344 } 00345 00346 void FeedStorageDummyImpl::setAuthor(const TQString& guid, const TQString& author) 00347 { 00348 if (contains(guid)) 00349 d->entries[guid].author = author; 00350 } 00351 00352 TQString FeedStorageDummyImpl::author(const TQString& guid) 00353 { 00354 return contains(guid) ? d->entries[guid].author : TQString(); 00355 } 00356 00357 TQValueList<Category> FeedStorageDummyImpl::categories(const TQString& guid) 00358 { 00359 if (!guid.isNull()) 00360 return contains(guid) ? d->entries[guid].categories : TQValueList<Category>(); 00361 else 00362 return d->categories; 00363 } 00364 00365 00366 void FeedStorageDummyImpl::removeTag(const TQString& guid, const TQString& tag) 00367 { 00368 if (contains(guid)) 00369 { 00370 d->entries[guid].tags.remove(tag); 00371 d->taggedArticles[tag].remove(guid); 00372 if (d->taggedArticles[tag].count() == 0) 00373 d->tags.remove(tag); 00374 } 00375 } 00376 00377 TQStringList FeedStorageDummyImpl::tags(const TQString& guid) 00378 { 00379 if (!guid.isNull()) 00380 return contains(guid) ? d->entries[guid].tags : TQStringList(); 00381 else 00382 { 00383 return d->tags; 00384 } 00385 } 00386 00387 void FeedStorageDummyImpl::add(FeedStorage* source) 00388 { 00389 TQStringList articles = source->articles(); 00390 for (TQStringList::ConstIterator it = articles.begin(); it != articles.end(); ++it) 00391 copyArticle(*it, source); 00392 setUnread(source->unread()); 00393 setLastFetch(source->lastFetch()); 00394 setTotalCount(source->totalCount()); 00395 } 00396 00397 void FeedStorageDummyImpl::copyArticle(const TQString& guid, FeedStorage* source) 00398 { 00399 if (!contains(guid)) 00400 addEntry(guid); 00401 00402 setComments(guid, source->comments(guid)); 00403 setCommentsLink(guid, source->commentsLink(guid)); 00404 setDescription(guid, source->description(guid)); 00405 setGuidIsHash(guid, source->guidIsHash(guid)); 00406 setGuidIsPermaLink(guid, source->guidIsPermaLink(guid)); 00407 setHash(guid, source->hash(guid)); 00408 setLink(guid, source->link(guid)); 00409 setPubDate(guid, source->pubDate(guid)); 00410 setStatus(guid, source->status(guid)); 00411 setTitle(guid, source->title(guid)); 00412 TQStringList tags = source->tags(guid); 00413 00414 for (TQStringList::ConstIterator it = tags.begin(); it != tags.end(); ++it) 00415 addTag(guid, *it); 00416 } 00417 00418 void FeedStorageDummyImpl::clear() 00419 { 00420 d->entries.clear(); 00421 setUnread(0); 00422 setTotalCount(0); 00423 } 00424 00425 void FeedStorageDummyImpl::setEnclosure(const TQString& guid, const TQString& url, const TQString& type, int length) 00426 { 00427 if (contains(guid)) 00428 { 00429 FeedStorageDummyImplPrivate::Entry entry = d->entries[guid]; 00430 entry.hasEnclosure = true; 00431 entry.enclosureUrl = url; 00432 entry.enclosureType = type; 00433 entry.enclosureLength = length; 00434 } 00435 } 00436 00437 void FeedStorageDummyImpl::removeEnclosure(const TQString& guid) 00438 { 00439 if (contains(guid)) 00440 { 00441 FeedStorageDummyImplPrivate::Entry entry = d->entries[guid]; 00442 entry.hasEnclosure = false; 00443 entry.enclosureUrl = TQString(); 00444 entry.enclosureType = TQString(); 00445 entry.enclosureLength = -1; 00446 } 00447 } 00448 00449 void FeedStorageDummyImpl::enclosure(const TQString& guid, bool& hasEnclosure, TQString& url, TQString& type, int& length) 00450 { 00451 if (contains(guid)) 00452 { 00453 FeedStorageDummyImplPrivate::Entry entry = d->entries[guid]; 00454 hasEnclosure = entry.hasEnclosure; 00455 url = entry.enclosureUrl; 00456 type = entry.enclosureType; 00457 length = entry.enclosureLength; 00458 } 00459 else 00460 { 00461 hasEnclosure = false; 00462 url = TQString(); 00463 type = TQString(); 00464 length = -1; 00465 } 00466 } 00467 00468 } // namespace Backend 00469 } // namespace Akregator