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;
35 QMutex DatManager::m_mutex;
38 /*-----------------------------------------------------------------------*/
41 /* create DatInfo explicitly. */
42 /* Usually, DatInfo is NOT created
43 if cache does not exist( i.e. ReadNum == 0 ). */ /* public */
44 bool DatManager::createDatInfo( const KURL& url )
47 FALSE /* don't check the existence of cache */
48 ) == NULL ) return FALSE;
53 /* get pointer of DatInfo */
56 /* It is very dangerous to access to DatInfo directly. */
57 /* Usually, access to it through DatManager. */ /* public */
58 DatInfo * DatManager::getDatInfoPointer( const KURL& url )
60 return getDatInfo( url );
64 /*------------------------------------------------------------------------*/
67 /* This function searches instance of DatInfo in m_datInfoList.
69 If cache exists, create DatInfo and return its pointer.
71 If checkCached == TRUE and cache does not exist, return NULL.
73 If checkCached == FALSE and cache does not exist,
74 create DatInfo and return its pointer anyway.
76 see also DatManager::searchDatInfo() and DatManager::createDatInfo() */ /* private */
78 DatInfo* DatManager::getDatInfo( const KURL& url, bool checkCached )
81 DatInfo* datInfo = searchDatInfo( url );
82 if( datInfo != NULL ) return datInfo;
84 /* create and enroll instance */
85 return enrollDatInfo( url, checkCached );
89 /* This function just searches instance of DatInfo specified by datURL
90 without creating instance. */ /* private */
91 DatInfo* DatManager::searchDatInfo( const KURL& url )
93 QMutexLocker locker( &m_mutex );
95 KURL datURL = Kita::getDatURL( url );
96 if ( datURL.isEmpty() ) return NULL; /* This url is not enrolled in BoardManager. */
97 if ( m_datInfoList.count() == 0 ) return NULL;
100 DatInfoList::Iterator it;
103 for ( it = m_datInfoList.begin(); it != m_datInfoList.end(); ++it, i++ ) {
107 if ( datURL == datInfo->url() ) {
111 m_datInfoList.remove( it );
112 m_datInfoList.prepend( datInfo );
123 /* create and enroll the instance of DatInfo and delete old instances.
124 Note that DatInfo::DatInfo() opens cached data and reads it. */ /* private */
125 DatInfo* DatManager::enrollDatInfo( const KURL& url, bool checkCached )
127 QMutexLocker locker( &m_mutex );
129 KURL datURL = Kita::getDatURL( url );
130 if( datURL.isEmpty() ) return NULL; /* This url is not enrolled in BoardManager. */
132 /* create DatInfo & read cached data */
133 DatInfo* datInfo = new DatInfo( datURL );
135 /* Does cache exist ? */
136 /* If cache does not exist, delete DatInfo here. */
137 if( checkCached && datInfo->getReadNum() == 0 ){
142 m_datInfoList.prepend( datInfo );
144 /* delete the all old instances ( LRU algorithm )*/
145 if ( m_datInfoList.count() > DMANAGER_MAXQUEUE ) {
147 DatInfoList::Iterator it;
148 for ( it = m_datInfoList.at( DMANAGER_MAXQUEUE ); it != m_datInfoList.end(); ++it ){
150 if( ( *it ) == NULL ) continue;
151 DatInfo* deleteInfo = ( *it );
153 if ( ! deleteInfo->isLocked() ){
154 m_datInfoList.remove( it );
156 deleteInfo->wait(); /* wait until DatInfo::m_mutex is released. */
167 void DatManager::deleteAllDatInfo()
169 DatInfoList::Iterator it;
170 for ( it = m_datInfoList.begin(); it != m_datInfoList.end(); ++it ){
172 if( ( *it ) == NULL ) continue;
180 /*-------------------------------------------------------------*/
184 /* update cache */ /* public */
185 bool DatManager::updateCache( const KURL& url , const QObject* parent )
187 DatInfo * datInfo = getDatInfo( url );
188 if ( datInfo == NULL ) return FALSE;
190 return datInfo->updateCache( parent );
195 int DatManager::getResponseCode( const KURL& url )
197 DatInfo * datInfo = getDatInfo( url );
198 if ( datInfo == NULL ) return 0;
200 return datInfo->getResponseCode();
204 int DatManager::getServerTime( const KURL& url )
206 DatInfo * datInfo = getDatInfo( url );
207 if ( datInfo == NULL ) return 0;
209 return datInfo->getServerTime();
214 bool DatManager::deleteCache( const KURL& url )
216 KURL datURL = Kita::getDatURL( url );
217 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
218 if( thread == NULL ) return FALSE;
219 if( thread->readNum() == 0 ) return FALSE;
222 DatInfo * datInfo = searchDatInfo( datURL );
224 if( !datInfo->deleteCache() ) return FALSE;
227 /* reset readNum & veiwPos */
228 thread->setReadNum( 0 );
229 thread->setViewPos( 0 );
232 QString cachePath = Kita::Cache::getPath( datURL );
233 QString indexPath = Kita::Cache::getIndexPath( datURL );
234 QFile::remove( indexPath );
235 QFile::remove( cachePath );
237 /* delete log from "cache" */
238 KitaThreadInfo::removeThreadInfo( datURL.prettyURL() );
244 bool DatManager::isLoadingNow( const KURL& url )
246 DatInfo * datInfo = searchDatInfo( url );
247 if ( datInfo == NULL ) return FALSE;
249 return datInfo->isLoadingNow();
254 void DatManager::stopLoading( const KURL& url )
256 DatInfo * datInfo = searchDatInfo( url );
257 if ( datInfo == NULL ) return ;
259 return datInfo->stopLoading();
264 /*----------------------*/
265 /* lock, unlock DatInfo */
268 void DatManager::lock( const KURL& url )
270 DatInfo * datInfo = getDatInfo( url );
271 if( datInfo == NULL ) return;
277 void DatManager::unlock( const KURL& url )
279 DatInfo * datInfo = searchDatInfo( url );
280 if ( datInfo == NULL ) return ;
286 /*--------------------------------------*/
290 const QString& DatManager::getRawDat( const KURL& url )
292 DatInfo * datInfo = getDatInfo( url );
293 if ( datInfo == NULL ) return QString::null;
295 return datInfo->getRawDat();
300 const QString& DatManager::getDat( const KURL& url, int num )
302 DatInfo * datInfo = getDatInfo( url );
303 if ( datInfo == NULL ) return QString::null;
305 return datInfo->getDat( num );
311 const QString& DatManager::getId( const KURL& url, int num )
313 DatInfo * datInfo = getDatInfo( url );
314 if ( datInfo == NULL ) return QString::null;
316 return datInfo->getId( num );
321 const QString& DatManager::getName( const KURL& url, int num )
323 DatInfo * datInfo = getDatInfo( url );
324 if ( datInfo == NULL ) return QString::null;
326 return datInfo->getName( num );
331 const QString& DatManager::getBody( const KURL& url, int num )
333 DatInfo * datInfo = getDatInfo( url );
334 if ( datInfo == NULL ) return QString::null;
336 return datInfo->getBody( num );
341 QString DatManager::getPlainName( const KURL& url, int num )
343 DatInfo * datInfo = getDatInfo( url );
344 if ( datInfo == NULL ) return QString::null;
346 return datInfo->getPlainName( num );
351 QString DatManager::getPlainBody( const KURL& url, int num )
353 DatInfo * datInfo = getDatInfo( url );
354 if ( datInfo == NULL ) return QString::null;
356 return datInfo->getPlainBody( num );
361 QString DatManager::getPlainTitle( const KURL& url, int num )
363 DatInfo * datInfo = getDatInfo( url );
364 if ( datInfo == NULL ) return QString::null;
366 return datInfo->getPlainTitle( num );
370 /* get name (i.e. subject ) of thread from URL of dat file. */ /* public */
371 const QString DatManager::threadName( const KURL& url )
373 KURL datURL = Kita::getDatURL( url );
374 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
375 if( thread != NULL ) return thread->threadName();
377 return QString::null;
382 const QString DatManager::threadID( const KURL& url )
384 KURL datURL = Kita::getDatURL( url );
385 return datURL.filename().section( ".", 0, 0 );
389 const QString DatManager::getCachePath( const KURL& url )
391 return Kita::Cache::getPath( url );
394 const QString DatManager::getCacheIndexPath( const KURL& url )
396 return Kita::Cache::getIndexPath( url );
399 /*---------------------------------------*/
403 QString DatManager::getHtml( const KURL& url, int startnum, int endnum )
405 DatInfo * datInfo = getDatInfo( url );
406 if ( datInfo == NULL ) return QString::null;
408 return datInfo->getHtml( startnum, endnum );
414 QString DatManager::getHtmlByID( const KURL& url, const QString& strid, int &count )
416 DatInfo* datInfo = getDatInfo( url );
417 if ( datInfo == NULL ) return QString::null;
419 return datInfo->getHtmlByID( strid, count );
424 /* Get HTML document of res tree.*/ /* public */
425 QString DatManager::getTreeByRes( const KURL& url, const int rootnum, int &count )
427 DatInfo* datInfo = getDatInfo( url );
428 if ( datInfo == NULL ) return QString::null;
430 return datInfo->getTreeByRes( rootnum, count );
433 /* Get HTML document of reverse res tree.*/ /* public */
434 QString DatManager::getTreeByResReverse( const KURL& url, const int rootnum, int &count )
436 DatInfo* datInfo = getDatInfo( url );
437 if ( datInfo == NULL ) return QString::null;
439 return datInfo->getTreeByResReverse( rootnum, count );
442 /* Get DOM element */ /* public */
443 bool DatManager::getDomElement( const KURL& url, int num, DOM::HTMLDocument& hdoc, DOM::Element& retelm )
445 DatInfo* datInfo = getDatInfo( url );
446 if ( datInfo == NULL ) return FALSE;
448 return datInfo->getDomElement( num, hdoc, retelm );
453 int DatManager::getResNum( const KURL& url )
455 KURL datURL = Kita::getDatURL( url );
456 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
457 if( thread != NULL ) return thread->resNum();
464 int DatManager::getReadNum( const KURL& url )
466 KURL datURL = Kita::getDatURL( url );
467 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
468 if( thread != NULL ) return thread->readNum();
475 int DatManager::getViewPos( const KURL& url )
477 KURL datURL = Kita::getDatURL( url );
478 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
479 if( thread != NULL ) return thread->viewPos();
486 void DatManager::setViewPos( const KURL& url , int num )
488 KURL datURL = Kita::getDatURL( url );
489 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
490 if( thread != NULL ) thread->setViewPos( num );
493 Kita::ThreadIndex::setViewPos( url, num );
496 KitaThreadInfo::setReadNum( datURL.prettyURL(), num );
501 int DatManager::getDatSize( const KURL& url )
503 DatInfo * datInfo = getDatInfo( url );
504 if ( datInfo == NULL ) return 0;
506 return datInfo->getDatSize();
509 /* get number of responses which have same ID. */ /* public */
510 int DatManager::getNumByID( const KURL& url, const QString& strid )
512 DatInfo * datInfo = getDatInfo( url );
513 if ( datInfo == NULL ) return 0;
515 return datInfo->getNumByID( strid );
520 bool DatManager::isThreadEnrolled( const KURL& url )
522 if( Kita::getDatURL( url ).isEmpty() ) return FALSE;
529 bool DatManager::is2chThread( const KURL& url )
531 if( BoardManager::type( url ) != Board_2ch ) return FALSE;
532 if( Kita::getDatURL( url ).isEmpty() ) return FALSE;
534 QRegExp url_2ch( ".*\\.2ch\\.net" );
535 QRegExp url_bbspink( ".*\\.bbspink\\.com" );
537 if ( url_2ch.search( url.host() ) != -1
538 || url_bbspink.search( url.host() ) != -1 ) return TRUE;
545 bool DatManager::isResValid( const KURL& url, int num )
547 DatInfo * datInfo = getDatInfo( url );
548 if ( datInfo == NULL ) return FALSE;
550 return datInfo->isResValid( num );
555 bool DatManager::isBroken( const KURL& url )
557 DatInfo * datInfo = getDatInfo( url );
558 if ( datInfo == NULL ) return FALSE;
560 return datInfo->isBroken();
564 bool DatManager::isResBroken( const KURL& url, int num )
566 DatInfo * datInfo = getDatInfo( url );
567 if ( datInfo == NULL ) return FALSE;
569 return datInfo->isResBroken( num );
574 /* check if ID == strid */ /* public */
575 bool DatManager::checkID( const KURL& url, const QString& strid, int num )
577 DatInfo* datInfo = getDatInfo( url );
578 if ( datInfo == NULL ) return FALSE;
580 return datInfo->checkID( strid, num );
584 /* check if keywords are included */ /* public */
585 bool DatManager::checkWord( const KURL& url,
586 QStringList& strlist, int num,
587 bool checkOR /* AND or OR search */
590 DatInfo* datInfo = getDatInfo( url );
591 if ( datInfo == NULL ) return FALSE;
593 return datInfo->checkWord( strlist, num, checkOR );
598 bool DatManager::isMarked( const KURL& url, int num )
600 DatInfo * datInfo = getDatInfo( url );
601 if ( datInfo == NULL ) return FALSE;
603 return datInfo->isMarked( num );
608 void DatManager::setMark( const KURL& url, int num, bool mark )
610 DatInfo * datInfo = getDatInfo( url );
611 if ( datInfo == NULL ) return ;
613 datInfo->setMark( num, mark );
618 bool DatManager::checkAbone( const KURL& url, int num )
620 DatInfo* datInfo = getDatInfo( url );
621 if ( datInfo == NULL ) return FALSE;
623 return datInfo->checkAbone( num );
628 void DatManager::resetAbone( const KURL& url )
630 DatInfo* datInfo = getDatInfo( url );
631 if ( datInfo == NULL ) return ;
633 datInfo->resetAbone();
637 /* check if the thread is shown on the main thread tab. */ /* public */
638 bool DatManager::isMainThreadOpened( const KURL& url )
640 KURL datURL = Kita::getDatURL( url ).prettyURL();
641 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
642 if( thread == NULL ) return FALSE;
644 return thread->isOpened();
647 void DatManager::setMainThreadOpened( const KURL& url, bool isOpened )
649 KURL datURL = Kita::getDatURL( url ).prettyURL();
650 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
651 if( thread != NULL ) thread->setIsOpened( isOpened );
655 /*--------------------------*/
659 const QString DatManager::threadURL( const KURL& url )
661 return Kita::getThreadURL( url );