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 */
15 #include <qstringlist.h>
20 #include "threadinfo.h"
21 #include "datmanager.h"
24 #include "kita_misc.h"
25 #include "kita-utf8.h"
26 #include "kita-utf16.h"
27 #include "threadindex.h"
28 #include "boardmanager.h"
32 #define DMANAGER_MAXQUEUE 16
34 DatInfoList DatManager::m_datInfoList;
37 /*-----------------------------------------------------------------------*/
40 /* create DatInfo explicitly. */
41 /* Usually, DatInfo is NOT created
42 if cache does not exist( i.e. ReadNum == 0 ). */ /* public */
43 bool DatManager::createDatInfo( const KURL& url )
46 FALSE /* don't check the existence of cache */
47 ) == NULL ) return FALSE;
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 KURL& url )
59 return getDatInfo( url );
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 NULL.
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( const KURL& url, bool checkCached )
80 DatInfo * datInfo = searchDatInfo( url );
81 if ( datInfo != NULL ) return datInfo;
83 /* create and enroll instance */
84 return enrollDatInfo( url, checkCached );
88 /* This function just searches instance of DatInfo specified by datURL
89 without creating instance. */ /* private */
90 DatInfo* DatManager::searchDatInfo( const KURL& url )
92 KURL datURL = Kita::getDatURL( url );
93 if ( datURL.isEmpty() ) return NULL; /* This url is not enrolled in BoardManager. */
94 if ( m_datInfoList.count() == 0 ) return NULL;
97 DatInfoList::Iterator it;
100 for ( it = m_datInfoList.begin(); it != m_datInfoList.end(); ++it, i++ ) {
104 if ( datURL == datInfo->url() ) {
108 m_datInfoList.remove( it );
109 m_datInfoList.prepend( datInfo );
120 /* create and enroll the instance of DatInfo and delete old instances.
121 Note that DatInfo::DatInfo() opens cached data and reads it. */ /* private */
122 DatInfo* DatManager::enrollDatInfo( const KURL& url, bool checkCached )
124 KURL datURL = Kita::getDatURL( url );
125 if ( datURL.isEmpty() ) return NULL; /* This url is not enrolled in BoardManager. */
127 /* create DatInfo & read cached data */
128 DatInfo* datInfo = new DatInfo( 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 ) {
142 DatInfoList::Iterator it;
143 for ( it = m_datInfoList.at( DMANAGER_MAXQUEUE ); it != m_datInfoList.end(); ++it ) {
145 if ( ( *it ) == NULL ) continue;
146 DatInfo* deleteInfo = ( *it );
155 void DatManager::deleteAllDatInfo()
157 DatInfoList::Iterator it;
158 for ( it = m_datInfoList.begin(); it != m_datInfoList.end(); ++it ) {
160 if ( ( *it ) == NULL ) continue;
167 /*-------------------------------------------------------------*/
171 /* update cache */ /* public */
172 bool DatManager::updateCache( const KURL& url , const QObject* parent )
174 DatInfo * datInfo = getDatInfo( url );
175 if ( datInfo == NULL ) return FALSE;
177 return datInfo->updateCache( parent );
182 int DatManager::getResponseCode( const KURL& url )
184 DatInfo * datInfo = getDatInfo( url );
185 if ( datInfo == NULL ) return 0;
187 return datInfo->getResponseCode();
191 int DatManager::getServerTime( const KURL& url )
193 DatInfo * datInfo = getDatInfo( url );
194 if ( datInfo == NULL ) return 0;
196 return datInfo->getServerTime();
201 bool DatManager::deleteCache( const KURL& url )
203 KURL datURL = Kita::getDatURL( url );
204 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
205 if ( thread == NULL ) return FALSE;
206 if ( thread->readNum() == 0 ) return FALSE;
209 DatInfo * datInfo = searchDatInfo( datURL );
211 if ( !datInfo->deleteCache() ) return FALSE;
214 /* reset readNum & veiwPos */
215 thread->setReadNum( 0 );
216 thread->setViewPos( 0 );
219 QString cachePath = Kita::Cache::getPath( datURL );
220 QString indexPath = Kita::Cache::getIndexPath( datURL );
221 QFile::remove( indexPath );
222 QFile::remove( cachePath );
224 /* delete log from "cache" */
225 KitaThreadInfo::removeThreadInfo( datURL.prettyURL() );
231 bool DatManager::isLoadingNow( const KURL& url )
233 DatInfo * datInfo = searchDatInfo( url );
234 if ( datInfo == NULL ) return FALSE;
236 return datInfo->isLoadingNow();
241 void DatManager::stopLoading( const KURL& url )
243 DatInfo * datInfo = searchDatInfo( url );
244 if ( datInfo == NULL ) return ;
246 return datInfo->stopLoading();
249 /*--------------------------------------*/
254 const QString& DatManager::getDat( const KURL& url, int num )
256 DatInfo * datInfo = getDatInfo( url );
257 if ( datInfo == NULL ) return QString::null;
259 return datInfo->getDat( num );
265 const QString& DatManager::getId( const KURL& url, int num )
267 DatInfo * datInfo = getDatInfo( url );
268 if ( datInfo == NULL ) return QString::null;
270 return datInfo->getId( num );
275 QString DatManager::getPlainName( const KURL& url, int num )
277 DatInfo * datInfo = getDatInfo( url );
278 if ( datInfo == NULL ) return QString::null;
280 return datInfo->getPlainName( num );
285 QString DatManager::getPlainBody( const KURL& url, int num )
287 DatInfo * datInfo = getDatInfo( url );
288 if ( datInfo == NULL ) return QString::null;
290 return datInfo->getPlainBody( num );
295 QString DatManager::getPlainTitle( const KURL& url, int num )
297 DatInfo * datInfo = getDatInfo( url );
298 if ( datInfo == NULL ) return QString::null;
300 return datInfo->getPlainTitle( num );
304 /* get name (i.e. subject ) of thread from URL of dat file. */ /* public */
305 const QString DatManager::threadName( const KURL& url )
307 KURL datURL = Kita::getDatURL( url );
308 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
309 if ( thread != NULL ) return thread->threadName();
311 return QString::null;
316 const QString DatManager::threadID( const KURL& url )
318 KURL datURL = Kita::getDatURL( url );
319 return datURL.filename().section( ".", 0, 0 );
323 const QString DatManager::getCachePath( const KURL& url )
325 return Kita::Cache::getPath( url );
328 const QString DatManager::getCacheIndexPath( const KURL& url )
330 return Kita::Cache::getIndexPath( url );
333 /*---------------------------------------*/
337 QString DatManager::getHtml( const KURL& url, int startnum, int endnum, bool checkAbone )
339 DatInfo * datInfo = getDatInfo( url );
340 if ( datInfo == NULL ) return QString::null;
342 return datInfo->getHTMLString( startnum, endnum, checkAbone );
348 QString DatManager::getHtmlByID( const KURL& url, const QString& strid, int &count )
350 DatInfo * datInfo = getDatInfo( url );
351 if ( datInfo == NULL ) return QString::null;
353 return datInfo->getHtmlByID( strid, count );
358 /* Get HTML document of res tree.*/ /* public */
359 QString DatManager::getTreeByRes( const KURL& url, const int rootnum, int &count )
361 DatInfo * datInfo = getDatInfo( url );
362 if ( datInfo == NULL ) return QString::null;
364 return datInfo->getTreeByRes( rootnum, count );
367 /* Get HTML document of reverse res tree.*/ /* public */
368 QString DatManager::getTreeByResReverse( const KURL& url, const int rootnum, int &count )
370 DatInfo * datInfo = getDatInfo( url );
371 if ( datInfo == NULL ) return QString::null;
373 return datInfo->getTreeByResReverse( rootnum, count );
378 int DatManager::getResNum( const KURL& url )
380 KURL datURL = Kita::getDatURL( url );
381 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
382 if ( thread != NULL ) return thread->resNum();
389 int DatManager::getReadNum( const KURL& url )
391 KURL datURL = Kita::getDatURL( url );
392 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
393 if ( thread != NULL ) return thread->readNum();
400 int DatManager::getViewPos( const KURL& url )
402 KURL datURL = Kita::getDatURL( url );
403 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
404 if ( thread != NULL ) return thread->viewPos();
411 void DatManager::setViewPos( const KURL& url , int num )
413 KURL datURL = Kita::getDatURL( url );
414 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
415 if ( thread != NULL ) thread->setViewPos( num );
418 Kita::ThreadIndex::setViewPos( url, num );
421 KitaThreadInfo::setReadNum( datURL.prettyURL(), num );
426 int DatManager::getDatSize( const KURL& url )
428 DatInfo * datInfo = getDatInfo( url );
429 if ( datInfo == NULL ) return 0;
431 return datInfo->getDatSize();
434 /* get number of responses which have same ID. */ /* public */
435 int DatManager::getNumByID( const KURL& url, const QString& strid )
437 DatInfo * datInfo = getDatInfo( url );
438 if ( datInfo == NULL ) return 0;
440 return datInfo->getNumByID( strid );
445 bool DatManager::isThreadEnrolled( const KURL& url )
447 if ( Kita::getDatURL( url ).isEmpty() ) return FALSE;
454 bool DatManager::is2chThread( const KURL& url )
456 if ( BoardManager::type( url ) != Board_2ch ) return FALSE;
457 if ( Kita::getDatURL( url ).isEmpty() ) return FALSE;
459 QRegExp url_2ch( ".*\\.2ch\\.net" );
460 QRegExp url_bbspink( ".*\\.bbspink\\.com" );
462 if ( url_2ch.search( url.host() ) != -1
463 || url_bbspink.search( url.host() ) != -1 ) return TRUE;
470 bool DatManager::isResValid( const KURL& url, int num )
472 DatInfo * datInfo = getDatInfo( url );
473 if ( datInfo == NULL ) return FALSE;
475 return datInfo->isResValid( num );
480 bool DatManager::isBroken( const KURL& url )
482 DatInfo * datInfo = getDatInfo( url );
483 if ( datInfo == NULL ) return FALSE;
485 return datInfo->isBroken();
489 bool DatManager::isResBroken( const KURL& url, int num )
491 DatInfo * datInfo = getDatInfo( url );
492 if ( datInfo == NULL ) return FALSE;
494 return datInfo->isResBroken( num );
499 /* check if ID == strid */ /* public */
500 bool DatManager::checkID( const KURL& url, const QString& strid, int num )
502 DatInfo * datInfo = getDatInfo( url );
503 if ( datInfo == NULL ) return FALSE;
505 return datInfo->checkID( strid, num );
509 /* check if keywords are included */ /* public */
510 bool DatManager::checkWord( const KURL& url,
511 QStringList& strlist, int num,
512 bool checkOR /* AND or OR search */
515 DatInfo * datInfo = getDatInfo( url );
516 if ( datInfo == NULL ) return FALSE;
518 return datInfo->checkWord( strlist, num, checkOR );
523 bool DatManager::isMarked( const KURL& url, int num )
525 KURL datURL = Kita::getDatURL( url );
526 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
527 if ( thread == NULL ) return FALSE;
529 return thread->isMarked( num );
534 void DatManager::setMark( const KURL& url, int num, bool mark )
536 KURL datURL = Kita::getDatURL( url );
537 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
538 if ( thread == NULL ) return ;
540 if ( thread->setMark( num, mark ) ) Kita::ThreadIndex::setMarkList( url, thread->markList() );
545 bool DatManager::checkAbone( const KURL& url, int num )
547 DatInfo * datInfo = getDatInfo( url );
548 if ( datInfo == NULL ) return FALSE;
550 return datInfo->checkAbone( num );
555 void DatManager::resetAbone( const KURL& url )
557 DatInfo * datInfo = getDatInfo( url );
558 if ( datInfo == NULL ) return ;
560 datInfo->resetAbone();
564 /* check if the thread is shown on the main thread tab. */ /* public */
565 bool DatManager::isMainThreadOpened( const KURL& url )
567 DatInfo * datInfo = getDatInfo( url );
568 if ( datInfo == NULL ) return FALSE;
570 return datInfo->isOpened();
573 void DatManager::setMainThreadOpened( const KURL& url, bool isOpened )
575 DatInfo * datInfo = getDatInfo( url );
576 if ( datInfo == NULL ) return;
578 datInfo->setIsOpened( isOpened );
582 /*--------------------------*/
586 const QString DatManager::threadURL( const KURL& url )
588 return Kita::getThreadURL( url );