1 /***************************************************************************
2 * Copyright (C) 2004 by Kita Developers *
3 * ikemo@users.sourceforge.jp *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
11 /* DatManager manages all information about thread */
12 #include "datmanager.h"
14 #include <QtCore/QFile>
15 #include <QtCore/QObject>
16 #include <QtCore/QRegExp>
17 #include <QtCore/QStringList>
19 #include "boarddatabase.h"
22 #include "kita_misc.h"
24 #include "threadindex.h"
25 #include "threadinfo.h"
29 static const int DMANAGER_MAXQUEUE = 16;
31 DatInfoList DatManager::m_datInfoList;
34 /*-----------------------------------------------------------------------*/
35 DatManager::DatManager(const KUrl& url) : m_url(url)
37 m_datUrl = getDatUrl(url);
38 m_searchDatInfo = searchDatInfo();
39 m_datInfo = getDatInfo();
43 /* create DatInfo explicitly. */
44 /* Usually, DatInfo is NOT created
45 if cache does not exist(i.e. ReadNum == 0). */ /* public */
46 bool DatManager::createDatInfo() const
48 return getDatInfo(false /* don't check the existence of cache */) != 0;
52 /* get pointer of DatInfo */
55 /* It is very dangerous to access to DatInfo directly. */
56 /* Usually, access to it through DatManager. */ /* public */
57 DatInfo * DatManager::getDatInfoPointer() const
63 /*------------------------------------------------------------------------*/
66 /* This function searches instance of DatInfo in m_datInfoList.
68 If cache exists, create DatInfo and return its pointer.
70 If checkCached == true and cache does not exist, return 0.
72 If checkCached == false and cache does not exist,
73 create DatInfo and return its pointer anyway.
75 see also DatManager::searchDatInfo() and DatManager::createDatInfo() */ /* private */
77 DatInfo* DatManager::getDatInfo(bool checkCached) const
80 /* create and enroll instance */
81 return (m_searchDatInfo != 0)
82 ? m_searchDatInfo : enrollDatInfo(checkCached);
86 /* This function just searches instance of DatInfo specified by datURL
87 without creating instance. */ /* private */
88 DatInfo* DatManager::searchDatInfo() const
90 if (m_datUrl.isEmpty())
91 return 0; /* This url is not enrolled in BoardManager. */
92 if (m_datInfoList.isEmpty())
96 DatInfoList::Iterator it;
99 for (it = m_datInfoList.begin(); it != m_datInfoList.end(); ++it, i++) {
103 if (m_datUrl == datInfo->url()) {
107 m_datInfoList.erase(it);
108 m_datInfoList.prepend(datInfo);
119 /* create and enroll the instance of DatInfo and delete old instances.
120 Note that DatInfo::DatInfo() opens cached data and reads it. */
122 DatInfo* DatManager::enrollDatInfo(bool checkCached) const
124 if (m_datUrl.isEmpty())
125 return 0; /* This url is not enrolled in BoardManager. */
127 /* create DatInfo & read cached data */
128 DatInfo* datInfo = new DatInfo(m_datUrl);
130 /* Does cache exist ? */
131 /* If cache does not exist, delete DatInfo here. */
132 if (checkCached && datInfo->getReadNum() == 0) {
137 m_datInfoList.prepend(datInfo);
139 /* delete the all old instances (LRU algorithm)*/
140 if (m_datInfoList.count() > DMANAGER_MAXQUEUE) {
141 for (int i = DMANAGER_MAXQUEUE; i < m_datInfoList.count(); i++) {
142 DatInfo* deleteInfo = m_datInfoList.at(i);
145 m_datInfoList.removeAt(i);
156 void DatManager::deleteAllDatInfo()
158 while (!m_datInfoList.isEmpty())
159 delete m_datInfoList.takeFirst();
164 /*-------------------------------------------------------------*/
168 /* update cache */ /* public */
169 bool DatManager::updateCache(const QObject* parent) const
171 return (m_datInfo == 0) ? false : m_datInfo->updateCache(parent);
176 int DatManager::getResponseCode() const
178 return (m_datInfo == 0) ? 0 : m_datInfo->getResponseCode();
182 int DatManager::getServerTime() const
184 return (m_datInfo == 0) ? 0 : m_datInfo->getServerTime();
189 bool DatManager::deleteCache() const
191 Thread* thread = Thread::getByUrlNew(m_datUrl);
194 if (thread->readNum() == 0)
198 if (m_searchDatInfo && !m_searchDatInfo->deleteCache()) {
202 /* reset readNum & veiwPos */
203 thread->setReadNum(0);
204 thread->setViewPos(0);
207 Cache cache(m_datUrl);
208 QString cachePath = cache.getPath();
209 QString indexPath = cache.getIndexPath();
210 QFile::remove(indexPath);
211 QFile::remove(cachePath);
213 /* delete log from "cache" */
214 ThreadInfo::removeThreadInfo(m_datUrl.prettyUrl());
220 bool DatManager::isLoadingNow() const
222 return (m_searchDatInfo == 0) ? false : m_searchDatInfo->isLoadingNow();
227 void DatManager::stopLoading() const
229 if (m_searchDatInfo == 0)
231 m_searchDatInfo->stopLoading();
234 /*--------------------------------------*/
239 QString DatManager::getDat(int num) const
241 return (m_datInfo == 0) ? QString() : m_datInfo->getDat(num);
247 QString DatManager::getId(int num) const
249 return (m_datInfo == 0) ? QString() : m_datInfo->getId(num);
254 QString DatManager::getPlainName(int num) const
256 return (m_datInfo == 0) ? QString() : m_datInfo->getPlainName(num);
261 QString DatManager::getPlainBody(int num) const
263 return (m_datInfo == 0) ? QString() : m_datInfo->getPlainBody(num);
268 QString DatManager::getPlainTitle(int num) const
270 return (m_datInfo == 0) ? QString() : m_datInfo->getPlainTitle(num);
274 /* get name (i.e. subject) of thread from URL of dat file. */ /* public */
275 QString DatManager::threadName() const
277 Thread* thread = Thread::getByUrlNew(m_datUrl);
278 return (thread != 0) ? thread->threadName() : QString();
283 QString DatManager::threadId() const
285 return m_datUrl.fileName().section('.', 0, 0);
288 /*---------------------------------------*/
292 QString DatManager::getHtml(int startnum, int endnum, bool checkAbone) const
294 return (m_datInfo == 0)
295 ? QString() : m_datInfo->getHTMLString(startnum, endnum, checkAbone);
301 QString DatManager::getHtmlById(const QString& strid, int &count) const
303 return (m_datInfo == 0) ? QString() : m_datInfo->getHtmlById(strid, count);
308 /* Get HTML document of res tree.*/ /* public */
309 QString DatManager::getTreeByRes(int rootnum, int &count) const
311 return (m_datInfo == 0)
312 ? QString() : m_datInfo->getTreeByRes(rootnum, count);
315 /* Get HTML document of reverse res tree.*/ /* public */
316 QString DatManager::getTreeByResReverse(int rootnum, int &count) const
318 return (m_datInfo == 0)
319 ? QString() : m_datInfo->getTreeByResReverse(rootnum, count);
324 int DatManager::getResNum() const
326 Thread* thread = Thread::getByUrlNew(m_datUrl);
327 return (thread != 0) ? thread->resNum() : 0;
332 int DatManager::getReadNum() const
334 Thread* thread = Thread::getByUrlNew(m_datUrl);
335 return (thread != 0) ? thread->readNum() : 0;
340 int DatManager::getViewPos() const
342 Thread* thread = Thread::getByUrlNew(m_datUrl);
343 return (thread != 0) ? thread->viewPos() : 0;
348 void DatManager::setViewPos(int num) const
350 Thread* thread = Thread::getByUrlNew(m_datUrl);
352 thread->setViewPos(num);
355 ThreadIndex threadIndex(m_url);
356 threadIndex.setViewPos(num);
359 ThreadInfo::setReadNum(m_datUrl.prettyUrl(), num);
364 int DatManager::getDatSize() const
366 return (m_datInfo == 0) ? 0 : m_datInfo->getDatSize();
369 /* get number of responses which have same ID. */ /* public */
370 int DatManager::getNumById(const QString& strid) const
372 return (m_datInfo == 0) ? 0 : m_datInfo->getNumById(strid);
377 bool DatManager::isThreadEnrolled() const
379 return !m_datUrl.isEmpty();
384 bool DatManager::is2chThread() const
386 BoardDatabase db(m_url);
387 if (db.type() != Board_2ch)
389 if (m_datUrl.isEmpty())
392 QRegExp url_2ch(".*\\.2ch\\.net");
393 QRegExp url_bbspink(".*\\.bbspink\\.com");
395 if (url_2ch.indexIn(m_url.host()) != -1
396 || url_bbspink.indexIn(m_url.host()) != -1)
404 bool DatManager::isResValid(int num) const
406 return (m_datInfo == 0) ? false : m_datInfo->isResValid(num);
411 bool DatManager::isBroken() const
413 return (m_datInfo == 0) ? false : m_datInfo->isBroken();
417 bool DatManager::isResBroken(int num) const
419 return (m_datInfo == 0) ? false : m_datInfo->isResBroken(num);
424 /* check if ID == strid */ /* public */
425 bool DatManager::checkId(const QString& strid, int num) const
427 return (m_datInfo == 0) ? false : m_datInfo->checkId(strid, num);
431 /* check if keywords are included */ /* public */
432 bool DatManager::checkWord(const QStringList& strlist, int num,
433 bool checkOr /* AND or OR search */) const
435 return (m_datInfo == 0)
436 ? false : m_datInfo->checkWord(strlist, num, checkOr);
441 bool DatManager::isMarked(int num) const
443 Thread* thread = Thread::getByUrlNew(m_datUrl);
444 return (thread == 0) ? false : thread->isMarked(num);
449 void DatManager::setMark(int num, bool mark) const
451 Thread* thread = Thread::getByUrlNew(m_datUrl);
455 if (thread->setMark(num, mark)) {
456 ThreadIndex threadIndex(m_url);
457 threadIndex.setMarkList(thread->markList());
463 bool DatManager::checkAbone(int num) const
465 return (m_datInfo == 0) ? false : m_datInfo->checkAbone(num);
470 void DatManager::resetAbone() const
474 m_datInfo->resetAbone();
478 /* check if the thread is shown on the main thread tab. */ /* public */
479 bool DatManager::isMainThreadOpened() const
481 return (m_datInfo == 0) ? false : m_datInfo->isOpened();
484 void DatManager::setMainThreadOpened(bool isOpened) const
488 m_datInfo->setOpened(isOpened);
492 /*--------------------------*/
496 QString DatManager::threadUrl() const
498 return getThreadUrl(m_url);