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>
17 #include "threadinfo.h"
18 #include "parsemisc.h"
19 #include "datmanager.h"
23 #include "kita_misc.h"
24 #include "kita-utf8.h"
25 #include "kita-utf16.h"
29 #define DMANAGER_MAXQUEUE 16
31 /*-------------------------------------------------*/
32 /* DatManager manages all information about *.dat. */
34 DatInfoList DatManager::m_datInfo;
35 QMutex DatManager::m_mutex;
37 /* This function searches instance of DatInfo specified by url.
38 If instance does not exist, create instance. */ /* private */
39 DatInfo* DatManager::getDatInfo( const KURL& url )
41 if ( url.isEmpty() ) {
46 DatInfoList::Iterator it;
49 KURL inurl = Kita::ParseMisc::parseURLonly( url );
51 /* Is this url registered thread ? */
52 // if( !Board::boardExists( inurl ) ) return NULL;
53 QRegExp threadcheck( "\\d+.dat" );
54 if( inurl.filename().find( threadcheck ) == -1 ) return NULL;
57 if ( m_datInfo.count() ) {
58 for ( it = m_datInfo.begin(); it != m_datInfo.end(); ++it, i++ ) {
62 if ( inurl == datInfo->url() ) {
66 m_datInfo.remove( it );
67 m_datInfo.prepend( datInfo );
77 /*create new DatInfo and insert it into list. */
78 KURL daturl = url.protocol() + "://" + url.host() + url.path();
80 datInfo = new DatInfo( daturl );
81 if ( datInfo->getRawDat() == QString::null ) { /* cache does not exist */
87 m_datInfo.prepend( datInfo );
89 /* delete the last items of list */
90 if ( m_datInfo.count() > DMANAGER_MAXQUEUE ) {
92 /* collect unlocked datInfos */
93 typedef QValueList<KURL> DELETELIST;
94 DELETELIST deleteList;
96 for ( it = m_datInfo.at( DMANAGER_MAXQUEUE ); it != m_datInfo.end(); ++it )
97 if ( ! ( *it ) ->isLocked() ) deleteList += ( *it ) ->url();
99 /* delete unlocked datInfos */
100 for ( DELETELIST::Iterator itdel = deleteList.begin(); itdel != deleteList.end(); ++itdel )
101 deleteDatInfoPrivate( ( *itdel ) );
108 /* get pointer of DatInfo */
111 /* It is very dangerous to access to DatInfo directly. */
112 /* Usually, access to it through DatManager. */ /* public */
113 DatInfo * DatManager::getDatInfoPointer( const KURL& url )
115 QMutexLocker locker( &m_mutex );
117 return getDatInfo( url );
123 void DatManager::deleteDatInfo( const KURL& url )
125 QMutexLocker locker( &m_mutex );
127 deleteDatInfoPrivate( url );
132 void DatManager::deleteDatInfoPrivate( const KURL& url )
135 DatInfoList::Iterator it;
137 for ( it = m_datInfo.begin(); it != m_datInfo.end(); ++it ) {
138 if ( url == ( *it ) ->url() ) {
140 if ( ! ( *it ) ->isLocked() ) {
141 m_datInfo.remove( it );
154 /*-------------------------------------------------------------*/
158 /*--------------------------------------*/
159 /* update cache, then return new lines */
161 /* If cache does not exist, then create
162 the new instance. */ /* public */
163 bool DatManager::updateCache( const KURL& url , const QObject* parent )
165 QMutexLocker locker( &m_mutex );
167 DatInfo * datInfo = getDatInfo( url );
169 /* create new DatInfo */
170 if ( datInfo == NULL ) {
171 datInfo = new DatInfo( url );
172 m_datInfo.prepend( datInfo );
175 return datInfo->updateCache( parent );
180 int DatManager::getResponseCode( const KURL& url )
182 QMutexLocker locker( &m_mutex );
184 DatInfo * datInfo = getDatInfo( url );
185 if ( datInfo == NULL ) return 0;
187 return datInfo->getResponseCode();
191 int DatManager::getServerTime( const KURL& url )
193 QMutexLocker locker( &m_mutex );
195 DatInfo * datInfo = getDatInfo( url );
196 if ( datInfo == NULL ) return 0;
198 return datInfo->getServerTime();
203 bool DatManager::deleteCache( const KURL& url )
205 QMutexLocker locker( &m_mutex );
207 DatInfo * datInfo = getDatInfo( url );
208 if ( datInfo == NULL ) return FALSE;
210 return datInfo->deleteCache();
215 bool DatManager::isLoadingNow( const KURL& url )
217 QMutexLocker locker( &m_mutex );
219 DatInfo * datInfo = getDatInfo( url );
220 if ( datInfo == NULL ) return FALSE;
222 return datInfo->isLoadingNow();
227 void DatManager::stopLoading( const KURL& url )
230 /* Don't use QMutexLocker here !!
231 It will cause deadlock , because
232 Kita::Access::stopJob() calls KitaThreadView::slotFinishLoad() back. */
234 DatInfo * datInfo = getDatInfo( url );
236 if ( datInfo == NULL ) return ;
238 return datInfo->stopLoading();
243 /*----------------------*/
244 /* lock, unlock DatInfo */
246 /* If cache does not exist, then create
247 the new instance and lock it.
249 Don't forget to unlock it. */ /* public */
251 void DatManager::lock ( const KURL& url )
253 QMutexLocker locker( &m_mutex );
255 DatInfo * datInfo = getDatInfo( url );
257 /* create new DatInfo */
258 if ( datInfo == NULL ) {
259 datInfo = new DatInfo( url );
260 m_datInfo.prepend( datInfo );
266 void DatManager::unlock( const KURL& url )
268 QMutexLocker locker( &m_mutex );
270 DatInfo * datInfo = getDatInfo( url );
271 if ( datInfo == NULL ) return ;
277 /*--------------------------------------*/
281 const QString& DatManager::getRawDat( const KURL& url )
283 QMutexLocker locker( &m_mutex );
285 DatInfo * datInfo = getDatInfo( url );
286 if ( datInfo == NULL ) return QString::null;
288 return datInfo->getRawDat();
293 const QString& DatManager::getDat( const KURL& url, int num )
295 QMutexLocker locker( &m_mutex );
297 DatInfo * datInfo = getDatInfo( url );
298 if ( datInfo == NULL ) return QString::null;
300 return datInfo->getDat( num );
306 const QString& DatManager::getId( const KURL& url, int num )
308 QMutexLocker locker( &m_mutex );
310 DatInfo * datInfo = getDatInfo( url );
311 if ( datInfo == NULL ) return QString::null;
313 return datInfo->getId( num );
318 const QString& DatManager::getName( const KURL& url, int num )
320 QMutexLocker locker( &m_mutex );
322 DatInfo * datInfo = getDatInfo( url );
323 if ( datInfo == NULL ) return QString::null;
325 return datInfo->getName( num );
330 const QString& DatManager::getBody( const KURL& url, int num )
332 QMutexLocker locker( &m_mutex );
334 DatInfo * datInfo = getDatInfo( url );
335 if ( datInfo == NULL ) return QString::null;
337 return datInfo->getBody( num );
342 QString DatManager::getPlainName( const KURL& url, int num )
344 QMutexLocker locker( &m_mutex );
346 DatInfo * datInfo = getDatInfo( url );
347 if ( datInfo == NULL ) return QString::null;
349 return datInfo->getPlainName( num );
354 QString DatManager::getPlainBody( const KURL& url, int num )
356 QMutexLocker locker( &m_mutex );
358 DatInfo * datInfo = getDatInfo( url );
359 if ( datInfo == NULL ) return QString::null;
361 return datInfo->getPlainBody( num );
366 QString DatManager::getPlainTitle( const KURL& url, int num )
368 QMutexLocker locker( &m_mutex );
370 DatInfo * datInfo = getDatInfo( url );
371 if ( datInfo == NULL ) return QString::null;
373 return datInfo->getPlainTitle( num );
377 /* get URL of thread from URL of dat file. */ /* public */
378 const QString DatManager::threadURL( const KURL& url )
380 QMutexLocker locker( &m_mutex );
382 KURL datURL = Kita::ParseMisc::parseURLonly( url );
383 return Kita::datToThread( datURL.prettyURL() );
387 /* get name (i.e. subject ) of thread from URL of dat file. */ /* public */
388 const QString& DatManager::threadName( const KURL& url )
390 QMutexLocker locker( &m_mutex );
392 KURL datURL = Kita::ParseMisc::parseURLonly( url );
393 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
395 if ( thread == NULL ) {
397 /* get subject from DatInfo */
398 DatInfo * datInfo = getDatInfo( url );
399 if ( datInfo == NULL ) return QString::null;
400 return datInfo->getSubject();
403 return Kita::Thread::getName( datURL );
408 const QString DatManager::threadID( const KURL& url )
410 QMutexLocker locker( &m_mutex );
412 KURL datURL = Kita::ParseMisc::parseURLonly( url );
413 return datURL.filename().section( ".", 0, 0 );
417 /* get URL of board from URL of dat file. */ /* public */
418 const QString DatManager::boardURL( const KURL& url )
420 QMutexLocker locker( &m_mutex );
422 KURL datURL = Kita::ParseMisc::parseURLonly( url );
423 return Kita::datToBoard( datURL.prettyURL() );
427 /* get root URL of board from URL of dat file. */
428 /* ex.) If datURL = "http://pc5.2ch.net/linux/dat/1089905503/",
429 then "http://pc5.2ch.net/" is returned. */ /* public */
430 const QString DatManager::boardRoot( const KURL& url )
432 QMutexLocker locker( &m_mutex );
434 KURL datURL = Kita::ParseMisc::parseURLonly( url );
435 datURL.cd( "../../../" );
436 return datURL.prettyURL();
440 /* get name of board from URL of dat file. */ /* public */
441 const QString& DatManager::boardName( const KURL& url )
443 QMutexLocker locker( &m_mutex );
445 KURL datURL = Kita::ParseMisc::parseURLonly( url );
446 QString bdURL = Kita::datToBoard( datURL.prettyURL() );
447 return Kita::Board::getName( bdURL );
452 const QString DatManager::boardID( const KURL& url )
454 QMutexLocker locker( &m_mutex );
456 KURL datURL = Kita::ParseMisc::parseURLonly( url );
457 return KURL( Kita::datToBoard( datURL.prettyURL() ) ).fileName();
460 const QString DatManager::getCachePath( const KURL& url )
462 QMutexLocker locker( &m_mutex );
464 KURL datURL = Kita::ParseMisc::parseURLonly( url );
465 return Kita::Cache::getPath( datURL );
468 const QString DatManager::getCacheIndexPath( const KURL& url )
470 QMutexLocker locker( &m_mutex );
472 KURL datURL = Kita::ParseMisc::parseURLonly( url );
473 return Kita::Cache::getIndexPath( datURL );
476 /*---------------------------------------*/
480 QString DatManager::getHtml( const KURL& url, int startnum, int endnum )
482 QMutexLocker locker( &m_mutex );
484 DatInfo * datInfo = getDatInfo( url );
485 if ( datInfo == NULL ) return QString::null;
487 return datInfo->getHtml( startnum, endnum );
493 QString DatManager::getHtmlByID( const KURL& url, const QString& strid, int &count )
495 QMutexLocker locker( &m_mutex );
497 DatInfo* datInfo = getDatInfo( url );
498 if ( datInfo == NULL ) return QString::null;
500 return datInfo->getHtmlByID( strid, count );
505 /* Get HTML document of res tree.*/ /* public */
506 QString DatManager::getTreeByRes( const KURL& url, const int rootnum, int &count )
508 QMutexLocker locker( &m_mutex );
510 DatInfo* datInfo = getDatInfo( url );
511 if ( datInfo == NULL ) return QString::null;
513 return datInfo->getTreeByRes( rootnum, count );
516 /* Get HTML document of reverse res tree.*/ /* public */
517 QString DatManager::getTreeByResReverse( const KURL& url, const int rootnum, int &count )
519 QMutexLocker locker( &m_mutex );
521 DatInfo* datInfo = getDatInfo( url );
522 if ( datInfo == NULL ) return QString::null;
524 return datInfo->getTreeByResReverse( rootnum, count );
527 /* Get DOM element */ /* public */
528 bool DatManager::getDomElement( const KURL& url, int num, DOM::HTMLDocument& hdoc, DOM::Element& retelm )
530 QMutexLocker locker( &m_mutex );
532 DatInfo* datInfo = getDatInfo( url );
533 if ( datInfo == NULL ) return FALSE;
535 return datInfo->getDomElement( num, hdoc, retelm );
541 int DatManager::getResNum( const KURL& url )
543 QMutexLocker locker( &m_mutex );
545 KURL datURL = Kita::ParseMisc::parseURLonly( url );
546 return KitaThreadInfo::resNum( datURL.prettyURL() );
551 int DatManager::getReadNum( const KURL& url )
553 QMutexLocker locker( &m_mutex );
555 DatInfo * datInfo = getDatInfo( url );
556 if ( datInfo == NULL ) return 0;
558 return datInfo->getReadNum();
563 int DatManager::getKokoyonNum( const KURL& url )
565 QMutexLocker locker( &m_mutex );
567 DatInfo * datInfo = getDatInfo( url );
568 if ( datInfo == NULL ) return 0;
570 return datInfo->getKokoyonNum();
575 void DatManager::setKokoyonNum( const KURL& url , int num )
577 QMutexLocker locker( &m_mutex );
579 DatInfo * datInfo = getDatInfo( url );
580 if ( datInfo == NULL ) return ;
582 return datInfo->setKokoyonNum( num );
587 int DatManager::getDatSize( const KURL& url )
589 QMutexLocker locker( &m_mutex );
591 DatInfo * datInfo = getDatInfo( url );
592 if ( datInfo == NULL ) return 0;
594 return datInfo->getDatSize();
598 int DatManager::getNumByID( const KURL& url, const QString& strid )
600 QMutexLocker locker( &m_mutex );
602 DatInfo * datInfo = getDatInfo( url );
603 if ( datInfo == NULL ) return 0;
605 return datInfo->getNumByID( strid );
610 bool DatManager::is2chThread( const KURL& url )
612 QMutexLocker locker( &m_mutex );
614 KURL datURL = Kita::ParseMisc::parseURLonly( url );
616 QRegExp threadcheck( "\\d+.dat" );
617 if( datURL.filename().find( threadcheck ) == -1 ) return FALSE;
619 return Kita::is2ch( datURL );
624 bool DatManager::isResValid( const KURL& url, int num )
626 QMutexLocker locker( &m_mutex );
628 DatInfo * datInfo = getDatInfo( url );
629 if ( datInfo == NULL ) return FALSE;
631 return datInfo->isResValid( num );
636 bool DatManager::isBroken( const KURL& url )
638 QMutexLocker locker( &m_mutex );
640 DatInfo * datInfo = getDatInfo( url );
641 if ( datInfo == NULL ) return FALSE;
643 return datInfo->isBroken();
647 bool DatManager::isResBroken( const KURL& url, int num )
649 QMutexLocker locker( &m_mutex );
651 DatInfo * datInfo = getDatInfo( url );
652 if ( datInfo == NULL ) return FALSE;
654 return datInfo->isResBroken( num );
660 bool DatManager::checkID( const KURL& url, const QString& strid, int num )
662 QMutexLocker locker( &m_mutex );
664 DatInfo* datInfo = getDatInfo( url );
665 if ( datInfo == NULL ) return FALSE;
667 return datInfo->checkID( strid, num );
671 /* check keywords */ /* public */
672 bool DatManager::checkWord( const KURL& url,
673 QStringList& strlist, int num,
674 bool checkOR /* AND or OR search */
677 QMutexLocker locker( &m_mutex );
679 DatInfo* datInfo = getDatInfo( url );
680 if ( datInfo == NULL ) return FALSE;
682 return datInfo->checkWord( strlist, num, checkOR );
687 bool DatManager::isMarked( const KURL& url, int num )
689 QMutexLocker locker( &m_mutex );
691 DatInfo * datInfo = getDatInfo( url );
692 if ( datInfo == NULL ) return FALSE;
694 return datInfo->isMarked( num );
699 void DatManager::setMark( const KURL& url, int num, bool mark )
701 QMutexLocker locker( &m_mutex );
703 DatInfo * datInfo = getDatInfo( url );
704 if ( datInfo == NULL ) return ;
706 datInfo->setMark( num, mark );
711 bool DatManager::checkAbone( const KURL& url, int num )
713 QMutexLocker locker( &m_mutex );
715 DatInfo* datInfo = getDatInfo( url );
716 if ( datInfo == NULL ) return FALSE;
718 return datInfo->checkAbone( num );
723 void DatManager::resetAbone( const KURL& url )
725 QMutexLocker locker( &m_mutex );
727 DatInfo* datInfo = getDatInfo( url );
728 if ( datInfo == NULL ) return ;
730 datInfo->resetAbone();