1 /**************************************************************************
2 * Copyright (C) 2003 by Hideki Ikemoto , (c)2004 by 421 *
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 ***************************************************************************/
13 #include <qstringlist.h>
16 #include "threadinfo.h"
17 #include "parsemisc.h"
18 #include "datmanager.h"
22 #include "kita_misc.h"
23 #include "kita-utf8.h"
24 #include "kita-utf16.h"
28 #define DMANAGER_MAXQUEUE 16
30 /*-------------------------------------------------*/
31 /* DatManager manages all information about *.dat. */
33 DatInfoList DatManager::m_datInfo;
34 QMutex DatManager::m_mutex;
36 /* This function searches instance of DatInfo specified by url.
37 If instance does not exist, create instance. */ /* private */
38 DatInfo* DatManager::getDatInfo( const KURL& url )
40 if ( url.isEmpty() ) {
45 DatInfoList::Iterator it;
48 KURL inurl = Kita::ParseMisc::parseURLonly( url );
51 if ( m_datInfo.count() ) {
52 for ( it = m_datInfo.begin(); it != m_datInfo.end(); ++it, i++ ) {
56 if ( inurl == datInfo->url() ) {
60 m_datInfo.remove( it );
61 m_datInfo.prepend( datInfo );
71 /*create new DatInfo and insert it into list. */
72 KURL daturl = url.protocol() + "://" + url.host() + url.path();
74 datInfo = new DatInfo( daturl );
75 if ( datInfo->getRawDat() == QString::null ) { /* cache does not exist */
81 m_datInfo.prepend( datInfo );
83 /* delete the last items of list */
84 if ( m_datInfo.count() > DMANAGER_MAXQUEUE ) {
86 /* collect unlocked datInfos */
87 typedef QValueList<KURL> DELETELIST;
88 DELETELIST deleteList;
90 for ( it = m_datInfo.at( DMANAGER_MAXQUEUE ); it != m_datInfo.end(); ++it )
91 if ( ! ( *it ) ->isLocked() ) deleteList += ( *it ) ->url();
93 /* delete unlocked datInfos */
94 for ( DELETELIST::Iterator itdel = deleteList.begin(); itdel != deleteList.end(); ++itdel )
95 deleteDatInfoPrivate( ( *itdel ) );
102 /* get pointer of DatInfo */
105 /* It is very dangerous to access to DatInfo directly. */
106 /* Usually, access to it through DatManager. */ /* public */
107 DatInfo * DatManager::getDatInfoPointer( const KURL& url )
109 QMutexLocker locker( &m_mutex );
111 return getDatInfo( url );
117 void DatManager::deleteDatInfo( const KURL& url )
119 QMutexLocker locker( &m_mutex );
121 deleteDatInfoPrivate( url );
126 void DatManager::deleteDatInfoPrivate( const KURL& url )
129 DatInfoList::Iterator it;
131 for ( it = m_datInfo.begin(); it != m_datInfo.end(); ++it ) {
132 if ( url == ( *it ) ->url() ) {
134 if ( ! ( *it ) ->isLocked() ) {
135 m_datInfo.remove( it );
148 /*-------------------------------------------------------------*/
152 /*--------------------------------------*/
153 /* update cache, then return new lines */
155 /* If cache does not exist, then create
156 the new instance. */ /* public */
157 bool DatManager::updateCache( const KURL& url , const QObject* parent )
159 QMutexLocker locker( &m_mutex );
161 DatInfo * datInfo = getDatInfo( url );
163 /* create new DatInfo */
164 if ( datInfo == NULL ) {
165 datInfo = new DatInfo( url );
166 m_datInfo.prepend( datInfo );
169 return datInfo->updateCache( parent );
174 int DatManager::getResponseCode( const KURL& url )
176 QMutexLocker locker( &m_mutex );
178 DatInfo * datInfo = getDatInfo( url );
179 if ( datInfo == NULL ) return 0;
181 return datInfo->getResponseCode();
185 int DatManager::getServerTime( const KURL& url )
187 QMutexLocker locker( &m_mutex );
189 DatInfo * datInfo = getDatInfo( url );
190 if ( datInfo == NULL ) return 0;
192 return datInfo->getServerTime();
197 bool DatManager::deleteCache( const KURL& url )
199 QMutexLocker locker( &m_mutex );
201 DatInfo * datInfo = getDatInfo( url );
202 if ( datInfo == NULL ) return FALSE;
204 return datInfo->deleteCache();
209 bool DatManager::isLoadingNow( const KURL& url )
211 QMutexLocker locker( &m_mutex );
213 DatInfo * datInfo = getDatInfo( url );
214 if ( datInfo == NULL ) return FALSE;
216 return datInfo->isLoadingNow();
221 void DatManager::stopLoading( const KURL& url )
224 /* Don't use QMutexLocker here !!
225 It will cause deadlock , because
226 Kita::Access::stopJob() calls KitaThreadView::slotFinishLoad() back. */
228 DatInfo * datInfo = getDatInfo( url );
230 if ( datInfo == NULL ) return ;
232 return datInfo->stopLoading();
237 /*----------------------*/
238 /* lock, unlock DatInfo */
240 /* If cache does not exist, then create
241 the new instance and lock it.
243 Don't forget to unlock it. */ /* public */
245 void DatManager::lock ( const KURL& url )
247 QMutexLocker locker( &m_mutex );
249 DatInfo * datInfo = getDatInfo( url );
251 /* create new DatInfo */
252 if ( datInfo == NULL ) {
253 datInfo = new DatInfo( url );
254 m_datInfo.prepend( datInfo );
260 void DatManager::unlock( const KURL& url )
262 QMutexLocker locker( &m_mutex );
264 DatInfo * datInfo = getDatInfo( url );
265 if ( datInfo == NULL ) return ;
271 /*--------------------------------------*/
275 const QString& DatManager::getRawDat( const KURL& url )
277 QMutexLocker locker( &m_mutex );
279 DatInfo * datInfo = getDatInfo( url );
280 if ( datInfo == NULL ) return QString::null;
282 return datInfo->getRawDat();
287 const QString& DatManager::getDat( const KURL& url, int num )
289 QMutexLocker locker( &m_mutex );
291 DatInfo * datInfo = getDatInfo( url );
292 if ( datInfo == NULL ) return QString::null;
294 return datInfo->getDat( num );
300 const QString& DatManager::getId( const KURL& url, int num )
302 QMutexLocker locker( &m_mutex );
304 DatInfo * datInfo = getDatInfo( url );
305 if ( datInfo == NULL ) return QString::null;
307 return datInfo->getId( num );
312 const QString& DatManager::getName( const KURL& url, int num )
314 QMutexLocker locker( &m_mutex );
316 DatInfo * datInfo = getDatInfo( url );
317 if ( datInfo == NULL ) return QString::null;
319 return datInfo->getName( num );
324 const QString& DatManager::getBody( const KURL& url, int num )
326 QMutexLocker locker( &m_mutex );
328 DatInfo * datInfo = getDatInfo( url );
329 if ( datInfo == NULL ) return QString::null;
331 return datInfo->getBody( num );
336 QString DatManager::getPlainName( const KURL& url, int num )
338 QMutexLocker locker( &m_mutex );
340 DatInfo * datInfo = getDatInfo( url );
341 if ( datInfo == NULL ) return QString::null;
343 return datInfo->getPlainName( num );
348 QString DatManager::getPlainBody( const KURL& url, int num )
350 QMutexLocker locker( &m_mutex );
352 DatInfo * datInfo = getDatInfo( url );
353 if ( datInfo == NULL ) return QString::null;
355 return datInfo->getPlainBody( num );
360 QString DatManager::getPlainTitle( const KURL& url, int num )
362 QMutexLocker locker( &m_mutex );
364 DatInfo * datInfo = getDatInfo( url );
365 if ( datInfo == NULL ) return QString::null;
367 return datInfo->getPlainTitle( num );
371 /* get URL of thread from URL of dat file. */ /* public */
372 const QString DatManager::threadURL( const KURL& url )
374 QMutexLocker locker( &m_mutex );
376 KURL datURL = Kita::ParseMisc::parseURLonly( url );
377 return Kita::datToThread( datURL.prettyURL() );
381 /* get name (i.e. subject ) of thread from URL of dat file. */ /* public */
382 const QString& DatManager::threadName( const KURL& url )
384 QMutexLocker locker( &m_mutex );
386 KURL datURL = Kita::ParseMisc::parseURLonly( url );
387 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
389 if ( thread == NULL ) {
391 /* get subject from DatInfo */
392 DatInfo * datInfo = getDatInfo( url );
393 if ( datInfo == NULL ) return QString::null;
394 return datInfo->getSubject();
397 return Kita::Thread::getName( datURL );
402 const QString DatManager::threadID( const KURL& url )
404 QMutexLocker locker( &m_mutex );
406 KURL datURL = Kita::ParseMisc::parseURLonly( url );
407 return datURL.filename().section( ".", 0, 0 );
411 /* get URL of board from URL of dat file. */ /* public */
412 const QString DatManager::boardURL( const KURL& url )
414 QMutexLocker locker( &m_mutex );
416 KURL datURL = Kita::ParseMisc::parseURLonly( url );
417 return Kita::datToBoard( datURL.prettyURL() );
421 /* get name of board from URL of dat file. */ /* public */
422 const QString& DatManager::boardName( const KURL& url )
424 QMutexLocker locker( &m_mutex );
426 KURL datURL = Kita::ParseMisc::parseURLonly( url );
427 QString bdURL = Kita::datToBoard( datURL.prettyURL() );
428 return Kita::Board::getName( bdURL );
433 const QString DatManager::boardID( const KURL& url )
435 QMutexLocker locker( &m_mutex );
437 KURL datURL = Kita::ParseMisc::parseURLonly( url );
438 return KURL( Kita::datToBoard( datURL.prettyURL() ) ).fileName();
441 const QString DatManager::getCachePath( const KURL& url )
443 QMutexLocker locker( &m_mutex );
445 KURL datURL = Kita::ParseMisc::parseURLonly( url );
446 return Kita::Cache::getPath( datURL );
449 const QString DatManager::getCacheIndexPath( const KURL& url )
451 QMutexLocker locker( &m_mutex );
453 KURL datURL = Kita::ParseMisc::parseURLonly( url );
454 return Kita::Cache::getIndexPath( datURL );
457 /*---------------------------------------*/
461 QString DatManager::getHtml( const KURL& url, int startnum, int endnum )
463 QMutexLocker locker( &m_mutex );
465 DatInfo * datInfo = getDatInfo( url );
466 if ( datInfo == NULL ) return QString::null;
468 return datInfo->getHtml( startnum, endnum );
474 QString DatManager::getHtmlByID( const KURL& url, const QString& strid, int &count )
476 QMutexLocker locker( &m_mutex );
478 DatInfo* datInfo = getDatInfo( url );
479 if ( datInfo == NULL ) return QString::null;
481 return datInfo->getHtmlByID( strid, count );
486 /* Get HTML document of res tree.*/ /* public */
487 QString DatManager::getTreeByRes( const KURL& url, const int rootnum, int &count )
489 QMutexLocker locker( &m_mutex );
491 DatInfo* datInfo = getDatInfo( url );
492 if ( datInfo == NULL ) return QString::null;
494 return datInfo->getTreeByRes( rootnum, count );
497 /* Get HTML document of reverse res tree.*/ /* public */
498 QString DatManager::getTreeByResReverse( const KURL& url, const int rootnum, int &count )
500 QMutexLocker locker( &m_mutex );
502 DatInfo* datInfo = getDatInfo( url );
503 if ( datInfo == NULL ) return QString::null;
505 return datInfo->getTreeByResReverse( rootnum, count );
508 /* Get DOM element */ /* public */
509 bool DatManager::getDomElement( const KURL& url, int num, DOM::HTMLDocument& hdoc, DOM::Element& retelm )
511 QMutexLocker locker( &m_mutex );
513 DatInfo* datInfo = getDatInfo( url );
514 if ( datInfo == NULL ) return FALSE;
516 return datInfo->getDomElement( num, hdoc, retelm );
522 int DatManager::getResNum( const KURL& url )
524 QMutexLocker locker( &m_mutex );
526 KURL datURL = Kita::ParseMisc::parseURLonly( url );
527 return KitaThreadInfo::resNum( datURL.prettyURL() );
532 int DatManager::getReadNum( const KURL& url )
534 QMutexLocker locker( &m_mutex );
536 DatInfo * datInfo = getDatInfo( url );
537 if ( datInfo == NULL ) return 0;
539 return datInfo->getReadNum();
544 int DatManager::getKokoyonNum( const KURL& url )
546 QMutexLocker locker( &m_mutex );
548 DatInfo * datInfo = getDatInfo( url );
549 if ( datInfo == NULL ) return 0;
551 return datInfo->getKokoyonNum();
556 void DatManager::setKokoyonNum( const KURL& url , int num )
558 QMutexLocker locker( &m_mutex );
560 DatInfo * datInfo = getDatInfo( url );
561 if ( datInfo == NULL ) return ;
563 return datInfo->setKokoyonNum( num );
568 int DatManager::getDatSize( const KURL& url )
570 QMutexLocker locker( &m_mutex );
572 DatInfo * datInfo = getDatInfo( url );
573 if ( datInfo == NULL ) return 0;
575 return datInfo->getDatSize();
579 int DatManager::getNumByID( const KURL& url, const QString& strid )
581 QMutexLocker locker( &m_mutex );
583 DatInfo * datInfo = getDatInfo( url );
584 if ( datInfo == NULL ) return 0;
586 return datInfo->getNumByID( strid );
591 bool DatManager::isResValid( const KURL& url, int num )
593 QMutexLocker locker( &m_mutex );
595 DatInfo * datInfo = getDatInfo( url );
596 if ( datInfo == NULL ) return FALSE;
598 return datInfo->isResValid( num );
603 bool DatManager::isBroken( const KURL& url )
605 QMutexLocker locker( &m_mutex );
607 DatInfo * datInfo = getDatInfo( url );
608 if ( datInfo == NULL ) return FALSE;
610 return datInfo->isBroken();
614 bool DatManager::isResBroken( const KURL& url, int num )
616 QMutexLocker locker( &m_mutex );
618 DatInfo * datInfo = getDatInfo( url );
619 if ( datInfo == NULL ) return FALSE;
621 return datInfo->isResBroken( num );
627 bool DatManager::checkID( const KURL& url, const QString& strid, int num )
629 QMutexLocker locker( &m_mutex );
631 DatInfo* datInfo = getDatInfo( url );
632 if ( datInfo == NULL ) return FALSE;
634 return datInfo->checkID( strid, num );
638 /* check keywords */ /* public */
639 bool DatManager::checkWord( const KURL& url,
640 QStringList& strlist, int num,
641 bool checkOR /* AND or OR search */
644 QMutexLocker locker( &m_mutex );
646 DatInfo* datInfo = getDatInfo( url );
647 if ( datInfo == NULL ) return FALSE;
649 return datInfo->checkWord( strlist, num, checkOR );
654 bool DatManager::isMarked( const KURL& url, int num )
656 QMutexLocker locker( &m_mutex );
658 DatInfo * datInfo = getDatInfo( url );
659 if ( datInfo == NULL ) return FALSE;
661 return datInfo->isMarked( num );
666 void DatManager::setMark( const KURL& url, int num, bool mark )
668 QMutexLocker locker( &m_mutex );
670 DatInfo * datInfo = getDatInfo( url );
671 if ( datInfo == NULL ) return ;
673 datInfo->setMark( num, mark );
678 bool DatManager::checkAbone( const KURL& url, int num )
680 QMutexLocker locker( &m_mutex );
682 DatInfo* datInfo = getDatInfo( url );
683 if ( datInfo == NULL ) return FALSE;
685 return datInfo->checkAbone( num );
690 void DatManager::resetAbone( const KURL& url )
692 QMutexLocker locker( &m_mutex );
694 DatInfo* datInfo = getDatInfo( url );
695 if ( datInfo == NULL ) return ;
697 datInfo->resetAbone();