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"
21 #include "kita_misc.h"
22 #include "kita-utf8.h"
23 #include "kita-utf16.h"
27 #define DMANAGER_MAXQUEUE 16
29 /*-------------------------------------------------*/
30 /* DatManager manages all information about *.dat. */
32 DatInfoList DatManager::m_datInfo;
33 QMutex DatManager::m_mutex;
35 /* This function searches instance of DatInfo specified by url.
36 If instance does not exist, create instance. */ /* private */
37 DatInfo* DatManager::getDatInfo( const KURL& url )
39 if ( url.isEmpty() ) {
44 DatInfoList::Iterator it;
47 KURL inurl = Kita::ParseMisc::parseURLonly( url );
50 if ( m_datInfo.count() ) {
51 for ( it = m_datInfo.begin(); it != m_datInfo.end(); ++it, i++ ) {
55 if ( inurl == datInfo->url() ) {
59 m_datInfo.remove( it );
60 m_datInfo.prepend( datInfo );
70 /*create new DatInfo and insert it into list. */
71 KURL daturl = url.protocol() + "://" + url.host() + url.path();
73 datInfo = new DatInfo( daturl );
74 if ( datInfo->getRawDat() == QString::null ) { /* cache does not exist */
80 m_datInfo.prepend( datInfo );
82 /* delete the last items of list */
83 if ( m_datInfo.count() > DMANAGER_MAXQUEUE ) {
85 /* collect unlocked datInfos */
86 typedef QValueList<KURL> DELETELIST;
87 DELETELIST deleteList;
89 for ( it = m_datInfo.at( DMANAGER_MAXQUEUE ); it != m_datInfo.end(); ++it )
90 if ( ! ( *it ) ->isLocked() ) deleteList += ( *it ) ->url();
92 /* delete unlocked datInfos */
93 for ( DELETELIST::Iterator itdel = deleteList.begin(); itdel != deleteList.end(); ++itdel )
94 deleteDatInfoPrivate( ( *itdel ) );
101 /* get pointer of DatInfo */
104 /* It is very dangerous to access to DatInfo directly. */
105 /* Usually, access to it through DatManager. */ /* public */
106 DatInfo * DatManager::getDatInfoPointer( const KURL& url )
108 QMutexLocker locker( &m_mutex );
110 return getDatInfo( url );
116 void DatManager::deleteDatInfo( const KURL& url )
118 QMutexLocker locker( &m_mutex );
120 deleteDatInfoPrivate( url );
125 void DatManager::deleteDatInfoPrivate( const KURL& url )
128 DatInfoList::Iterator it;
130 for ( it = m_datInfo.begin(); it != m_datInfo.end(); ++it ) {
131 if ( url == ( *it ) ->url() ) {
133 if ( ! ( *it ) ->isLocked() ) {
134 m_datInfo.remove( it );
147 /*-------------------------------------------------------------*/
151 /*--------------------------------------*/
152 /* update cache, then return new lines */
154 /* If cache does not exist, then create
155 the new instance. */ /* public */
156 bool DatManager::updateCache( const KURL& url , const QObject* parent )
158 QMutexLocker locker( &m_mutex );
160 DatInfo * datInfo = getDatInfo( url );
162 /* create new DatInfo */
163 if ( datInfo == NULL ) {
164 datInfo = new DatInfo( url );
165 m_datInfo.prepend( datInfo );
168 return datInfo->updateCache( parent );
173 int DatManager::getResponseCode( const KURL& url )
175 QMutexLocker locker( &m_mutex );
177 DatInfo * datInfo = getDatInfo( url );
178 if ( datInfo == NULL ) return 0;
180 return datInfo->getResponseCode();
184 int DatManager::getServerTime( const KURL& url )
186 QMutexLocker locker( &m_mutex );
188 DatInfo * datInfo = getDatInfo( url );
189 if ( datInfo == NULL ) return 0;
191 return datInfo->getServerTime();
196 bool DatManager::deleteCache( const KURL& url )
198 QMutexLocker locker( &m_mutex );
200 DatInfo * datInfo = getDatInfo( url );
201 if ( datInfo == NULL ) return FALSE;
203 return datInfo->deleteCache();
208 bool DatManager::isLoadingNow( const KURL& url )
210 QMutexLocker locker( &m_mutex );
212 DatInfo * datInfo = getDatInfo( url );
213 if ( datInfo == NULL ) return FALSE;
215 return datInfo->isLoadingNow();
220 void DatManager::stopLoading( const KURL& url )
223 /* Don't use QMutexLocker here !!
224 It will cause deadlock , because
225 Kita::Access::stopJob() calls KitaThreadView::slotFinishLoad() back. */
227 DatInfo * datInfo = getDatInfo( url );
229 if ( datInfo == NULL ) return ;
231 return datInfo->stopLoading();
236 /*----------------------*/
237 /* lock, unlock DatInfo */
239 /* If cache does not exist, then create
240 the new instance and lock it.
242 Don't forget to unlock it. */ /* public */
244 void DatManager::lock ( const KURL& url )
246 QMutexLocker locker( &m_mutex );
248 DatInfo * datInfo = getDatInfo( url );
250 /* create new DatInfo */
251 if ( datInfo == NULL ) {
252 datInfo = new DatInfo( url );
253 m_datInfo.prepend( datInfo );
259 void DatManager::unlock( const KURL& url )
261 QMutexLocker locker( &m_mutex );
263 DatInfo * datInfo = getDatInfo( url );
264 if ( datInfo == NULL ) return ;
270 /*--------------------------------------*/
274 const QString& DatManager::getRawDat( const KURL& url )
276 QMutexLocker locker( &m_mutex );
278 DatInfo * datInfo = getDatInfo( url );
279 if ( datInfo == NULL ) return QString::null;
281 return datInfo->getRawDat();
286 const QString& DatManager::getDat( const KURL& url, int num )
288 QMutexLocker locker( &m_mutex );
290 DatInfo * datInfo = getDatInfo( url );
291 if ( datInfo == NULL ) return QString::null;
293 return datInfo->getDat( num );
299 const QString& DatManager::getId( const KURL& url, int num )
301 QMutexLocker locker( &m_mutex );
303 DatInfo * datInfo = getDatInfo( url );
304 if ( datInfo == NULL ) return QString::null;
306 return datInfo->getId( num );
311 const QString& DatManager::getName( const KURL& url, int num )
313 QMutexLocker locker( &m_mutex );
315 DatInfo * datInfo = getDatInfo( url );
316 if ( datInfo == NULL ) return QString::null;
318 return datInfo->getName( num );
323 const QString& DatManager::getBody( const KURL& url, int num )
325 QMutexLocker locker( &m_mutex );
327 DatInfo * datInfo = getDatInfo( url );
328 if ( datInfo == NULL ) return QString::null;
330 return datInfo->getBody( num );
335 QString DatManager::getPlainName( const KURL& url, int num )
337 QMutexLocker locker( &m_mutex );
339 DatInfo * datInfo = getDatInfo( url );
340 if ( datInfo == NULL ) return QString::null;
342 return datInfo->getPlainName( num );
347 QString DatManager::getPlainBody( const KURL& url, int num )
349 QMutexLocker locker( &m_mutex );
351 DatInfo * datInfo = getDatInfo( url );
352 if ( datInfo == NULL ) return QString::null;
354 return datInfo->getPlainBody( num );
359 QString DatManager::getPlainTitle( const KURL& url, int num )
361 QMutexLocker locker( &m_mutex );
363 DatInfo * datInfo = getDatInfo( url );
364 if ( datInfo == NULL ) return QString::null;
366 return datInfo->getPlainTitle( num );
370 /* get URL of thread from URL of dat file. */ /* public */
371 const QString DatManager::threadURL( const KURL& url )
373 QMutexLocker locker( &m_mutex );
375 KURL datURL = Kita::ParseMisc::parseURLonly( url );
376 return Kita::datToThread( datURL.prettyURL() );
380 /* get name (i.e. subject ) of thread from URL of dat file. */ /* public */
381 const QString& DatManager::threadName( const KURL& url )
383 QMutexLocker locker( &m_mutex );
385 KURL datURL = Kita::ParseMisc::parseURLonly( url );
386 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
388 if ( thread == NULL ) {
390 /* get subject from DatInfo */
391 DatInfo * datInfo = getDatInfo( url );
392 if ( datInfo == NULL ) return QString::null;
393 return datInfo->getSubject();
396 return thread->name();
401 const QString DatManager::threadID( const KURL& url )
403 QMutexLocker locker( &m_mutex );
405 KURL datURL = Kita::ParseMisc::parseURLonly( url );
406 return datURL.filename().section( ".", 0, 0 );
410 /* get URL of board from URL of dat file. */ /* public */
411 const QString DatManager::boardURL( const KURL& url )
413 QMutexLocker locker( &m_mutex );
415 KURL datURL = Kita::ParseMisc::parseURLonly( url );
416 return Kita::datToBoard( datURL.prettyURL() );
420 /* get name of board from URL of dat file. */ /* public */
421 const QString& DatManager::boardName( const KURL& url )
423 QMutexLocker locker( &m_mutex );
425 KURL datURL = Kita::ParseMisc::parseURLonly( url );
426 QString bdURL = Kita::datToBoard( datURL.prettyURL() );
427 return Kita::Board::getName( bdURL );
432 const QString DatManager::boardID( const KURL& url )
434 QMutexLocker locker( &m_mutex );
436 KURL datURL = Kita::ParseMisc::parseURLonly( url );
437 return KURL( Kita::datToBoard( datURL.prettyURL() ) ).fileName();
441 /*---------------------------------------*/
445 QString DatManager::getHtml( const KURL& url, int startnum, int endnum )
447 QMutexLocker locker( &m_mutex );
449 DatInfo * datInfo = getDatInfo( url );
450 if ( datInfo == NULL ) return QString::null;
452 return datInfo->getHtml( startnum, endnum );
458 QString DatManager::getHtmlByID( const KURL& url, const QString& strid, int &count )
460 QMutexLocker locker( &m_mutex );
462 DatInfo* datInfo = getDatInfo( url );
463 if ( datInfo == NULL ) return QString::null;
465 return datInfo->getHtmlByID( strid, count );
470 /* Get HTML document of res tree.*/ /* public */
471 QString DatManager::getTreeByRes( const KURL& url, const int rootnum, int &count )
473 QMutexLocker locker( &m_mutex );
475 DatInfo* datInfo = getDatInfo( url );
476 if ( datInfo == NULL ) return QString::null;
478 return datInfo->getTreeByRes( rootnum, count );
481 /* Get HTML document of reverse res tree.*/ /* public */
482 QString DatManager::getTreeByResReverse( const KURL& url, const int rootnum, int &count )
484 QMutexLocker locker( &m_mutex );
486 DatInfo* datInfo = getDatInfo( url );
487 if ( datInfo == NULL ) return QString::null;
489 return datInfo->getTreeByResReverse( rootnum, count );
492 /* Get DOM element */ /* public */
493 bool DatManager::getDomElement( const KURL& url, int num, DOM::HTMLDocument& hdoc, DOM::Element& retelm )
495 QMutexLocker locker( &m_mutex );
497 DatInfo* datInfo = getDatInfo( url );
498 if ( datInfo == NULL ) return FALSE;
500 return datInfo->getDomElement( num, hdoc, retelm );
506 int DatManager::getResNum( const KURL& url )
508 QMutexLocker locker( &m_mutex );
510 KURL datURL = Kita::ParseMisc::parseURLonly( url );
511 return KitaThreadInfo::resNum( datURL.prettyURL() );
516 int DatManager::getReadNum( const KURL& url )
518 QMutexLocker locker( &m_mutex );
520 DatInfo * datInfo = getDatInfo( url );
521 if ( datInfo == NULL ) return 0;
523 return datInfo->getReadNum();
528 int DatManager::getKokoyonNum( const KURL& url )
530 QMutexLocker locker( &m_mutex );
532 DatInfo * datInfo = getDatInfo( url );
533 if ( datInfo == NULL ) return 0;
535 return datInfo->getKokoyonNum();
540 void DatManager::setKokoyonNum( const KURL& url , int num )
542 QMutexLocker locker( &m_mutex );
544 DatInfo * datInfo = getDatInfo( url );
545 if ( datInfo == NULL ) return ;
547 return datInfo->setKokoyonNum( num );
552 int DatManager::getDatSize( const KURL& url )
554 QMutexLocker locker( &m_mutex );
556 DatInfo * datInfo = getDatInfo( url );
557 if ( datInfo == NULL ) return 0;
559 return datInfo->getDatSize();
563 int DatManager::getNumByID( const KURL& url, const QString& strid )
565 QMutexLocker locker( &m_mutex );
567 DatInfo * datInfo = getDatInfo( url );
568 if ( datInfo == NULL ) return 0;
570 return datInfo->getNumByID( strid );
575 bool DatManager::isResValid( const KURL& url, int num )
577 QMutexLocker locker( &m_mutex );
579 DatInfo * datInfo = getDatInfo( url );
580 if ( datInfo == NULL ) return FALSE;
582 return datInfo->isResValid( num );
587 bool DatManager::isBroken( const KURL& url )
589 QMutexLocker locker( &m_mutex );
591 DatInfo * datInfo = getDatInfo( url );
592 if ( datInfo == NULL ) return FALSE;
594 return datInfo->isBroken();
598 bool DatManager::isResBroken( const KURL& url, int num )
600 QMutexLocker locker( &m_mutex );
602 DatInfo * datInfo = getDatInfo( url );
603 if ( datInfo == NULL ) return FALSE;
605 return datInfo->isResBroken( num );
611 bool DatManager::checkID( const KURL& url, const QString& strid, int num )
613 QMutexLocker locker( &m_mutex );
615 DatInfo* datInfo = getDatInfo( url );
616 if ( datInfo == NULL ) return FALSE;
618 return datInfo->checkID( strid, num );
622 /* check keywords */ /* public */
623 bool DatManager::checkWord( const KURL& url,
624 QStringList& strlist, int num,
625 bool checkOR /* AND or OR search */
628 QMutexLocker locker( &m_mutex );
630 DatInfo* datInfo = getDatInfo( url );
631 if ( datInfo == NULL ) return FALSE;
633 return datInfo->checkWord( strlist, num, checkOR );
638 bool DatManager::isMarked( const KURL& url, int num )
640 QMutexLocker locker( &m_mutex );
642 DatInfo * datInfo = getDatInfo( url );
643 if ( datInfo == NULL ) return FALSE;
645 return datInfo->isMarked( num );
650 void DatManager::setMark( const KURL& url, int num, bool mark )
652 QMutexLocker locker( &m_mutex );
654 DatInfo * datInfo = getDatInfo( url );
655 if ( datInfo == NULL ) return ;
657 datInfo->setMark( num, mark );
662 bool DatManager::checkAbone( const KURL& url, int num )
664 QMutexLocker locker( &m_mutex );
666 DatInfo* datInfo = getDatInfo( url );
667 if ( datInfo == NULL ) return FALSE;
669 return datInfo->checkAbone( num );
674 void DatManager::resetAbone( const KURL& url )
676 QMutexLocker locker( &m_mutex );
678 DatInfo* datInfo = getDatInfo( url );
679 if ( datInfo == NULL ) return ;
681 datInfo->resetAbone();