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 /*--------------------------------------*/
291 const QString& DatManager::getDat( const KURL& url, int num )
293 DatInfo * datInfo = getDatInfo( url );
294 if ( datInfo == NULL ) return QString::null;
296 return datInfo->getDat( num );
302 const QString& DatManager::getId( const KURL& url, int num )
304 DatInfo * datInfo = getDatInfo( url );
305 if ( datInfo == NULL ) return QString::null;
307 return datInfo->getId( num );
312 QString DatManager::getPlainName( const KURL& url, int num )
314 DatInfo * datInfo = getDatInfo( url );
315 if ( datInfo == NULL ) return QString::null;
317 return datInfo->getPlainName( num );
322 QString DatManager::getPlainBody( const KURL& url, int num )
324 DatInfo * datInfo = getDatInfo( url );
325 if ( datInfo == NULL ) return QString::null;
327 return datInfo->getPlainBody( num );
332 QString DatManager::getPlainTitle( const KURL& url, int num )
334 DatInfo * datInfo = getDatInfo( url );
335 if ( datInfo == NULL ) return QString::null;
337 return datInfo->getPlainTitle( num );
341 /* get name (i.e. subject ) of thread from URL of dat file. */ /* public */
342 const QString DatManager::threadName( const KURL& url )
344 KURL datURL = Kita::getDatURL( url );
345 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
346 if( thread != NULL ) return thread->threadName();
348 return QString::null;
353 const QString DatManager::threadID( const KURL& url )
355 KURL datURL = Kita::getDatURL( url );
356 return datURL.filename().section( ".", 0, 0 );
360 const QString DatManager::getCachePath( const KURL& url )
362 return Kita::Cache::getPath( url );
365 const QString DatManager::getCacheIndexPath( const KURL& url )
367 return Kita::Cache::getIndexPath( url );
370 /*---------------------------------------*/
374 QString DatManager::getHtml( const KURL& url, int startnum, int endnum, bool checkAbone )
376 DatInfo * datInfo = getDatInfo( url );
377 if ( datInfo == NULL ) return QString::null;
379 return datInfo->getHTMLString( startnum, endnum, checkAbone );
385 QString DatManager::getHtmlByID( const KURL& url, const QString& strid, int &count )
387 DatInfo* datInfo = getDatInfo( url );
388 if ( datInfo == NULL ) return QString::null;
390 return datInfo->getHtmlByID( strid, count );
395 /* Get HTML document of res tree.*/ /* public */
396 QString DatManager::getTreeByRes( const KURL& url, const int rootnum, int &count )
398 DatInfo* datInfo = getDatInfo( url );
399 if ( datInfo == NULL ) return QString::null;
401 return datInfo->getTreeByRes( rootnum, count );
404 /* Get HTML document of reverse res tree.*/ /* public */
405 QString DatManager::getTreeByResReverse( const KURL& url, const int rootnum, int &count )
407 DatInfo* datInfo = getDatInfo( url );
408 if ( datInfo == NULL ) return QString::null;
410 return datInfo->getTreeByResReverse( rootnum, count );
415 int DatManager::getResNum( const KURL& url )
417 KURL datURL = Kita::getDatURL( url );
418 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
419 if( thread != NULL ) return thread->resNum();
426 int DatManager::getReadNum( const KURL& url )
428 KURL datURL = Kita::getDatURL( url );
429 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
430 if( thread != NULL ) return thread->readNum();
437 int DatManager::getViewPos( const KURL& url )
439 KURL datURL = Kita::getDatURL( url );
440 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
441 if( thread != NULL ) return thread->viewPos();
448 void DatManager::setViewPos( const KURL& url , int num )
450 KURL datURL = Kita::getDatURL( url );
451 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
452 if( thread != NULL ) thread->setViewPos( num );
455 Kita::ThreadIndex::setViewPos( url, num );
458 KitaThreadInfo::setReadNum( datURL.prettyURL(), num );
463 int DatManager::getDatSize( const KURL& url )
465 DatInfo * datInfo = getDatInfo( url );
466 if ( datInfo == NULL ) return 0;
468 return datInfo->getDatSize();
471 /* get number of responses which have same ID. */ /* public */
472 int DatManager::getNumByID( const KURL& url, const QString& strid )
474 DatInfo * datInfo = getDatInfo( url );
475 if ( datInfo == NULL ) return 0;
477 return datInfo->getNumByID( strid );
482 bool DatManager::isThreadEnrolled( const KURL& url )
484 if( Kita::getDatURL( url ).isEmpty() ) return FALSE;
491 bool DatManager::is2chThread( const KURL& url )
493 if( BoardManager::type( url ) != Board_2ch ) return FALSE;
494 if( Kita::getDatURL( url ).isEmpty() ) return FALSE;
496 QRegExp url_2ch( ".*\\.2ch\\.net" );
497 QRegExp url_bbspink( ".*\\.bbspink\\.com" );
499 if ( url_2ch.search( url.host() ) != -1
500 || url_bbspink.search( url.host() ) != -1 ) return TRUE;
507 bool DatManager::isResValid( const KURL& url, int num )
509 DatInfo * datInfo = getDatInfo( url );
510 if ( datInfo == NULL ) return FALSE;
512 return datInfo->isResValid( num );
517 bool DatManager::isBroken( const KURL& url )
519 DatInfo * datInfo = getDatInfo( url );
520 if ( datInfo == NULL ) return FALSE;
522 return datInfo->isBroken();
526 bool DatManager::isResBroken( const KURL& url, int num )
528 DatInfo * datInfo = getDatInfo( url );
529 if ( datInfo == NULL ) return FALSE;
531 return datInfo->isResBroken( num );
536 /* check if ID == strid */ /* public */
537 bool DatManager::checkID( const KURL& url, const QString& strid, int num )
539 DatInfo* datInfo = getDatInfo( url );
540 if ( datInfo == NULL ) return FALSE;
542 return datInfo->checkID( strid, num );
546 /* check if keywords are included */ /* public */
547 bool DatManager::checkWord( const KURL& url,
548 QStringList& strlist, int num,
549 bool checkOR /* AND or OR search */
552 DatInfo* datInfo = getDatInfo( url );
553 if ( datInfo == NULL ) return FALSE;
555 return datInfo->checkWord( strlist, num, checkOR );
560 bool DatManager::isMarked( const KURL& url, int num )
562 KURL datURL = Kita::getDatURL( url );
563 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
564 if( thread == NULL ) return FALSE;
566 return thread->isMarked( num );
571 void DatManager::setMark( const KURL& url, int num, bool mark )
573 KURL datURL = Kita::getDatURL( url );
574 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
575 if( thread == NULL ) return;
577 if( thread->setMark( num, mark ) ) Kita::ThreadIndex::setMarkList( url, thread->markList() );
582 bool DatManager::checkAbone( const KURL& url, int num )
584 DatInfo* datInfo = getDatInfo( url );
585 if ( datInfo == NULL ) return FALSE;
587 return datInfo->checkAbone( num );
592 void DatManager::resetAbone( const KURL& url )
594 DatInfo* datInfo = getDatInfo( url );
595 if ( datInfo == NULL ) return ;
597 datInfo->resetAbone();
601 /* check if the thread is shown on the main thread tab. */ /* public */
602 bool DatManager::isMainThreadOpened( const KURL& url )
604 KURL datURL = Kita::getDatURL( url ).prettyURL();
605 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
606 if( thread == NULL ) return FALSE;
608 return thread->isOpened();
611 void DatManager::setMainThreadOpened( const KURL& url, bool isOpened )
613 KURL datURL = Kita::getDatURL( url ).prettyURL();
614 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
615 if( thread != NULL ) thread->setIsOpened( isOpened );
619 /*--------------------------*/
623 const QString DatManager::threadURL( const KURL& url )
625 return Kita::getThreadURL( url );