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 ){
127 DatInfoList::Iterator it;
129 for ( it = m_datInfo.begin(); it != m_datInfo.end(); ++it ) {
130 if ( url == ( *it )->url() ){
132 if(! (*it)->isLocked() ){
133 m_datInfo.remove( it );
146 /*-------------------------------------------------------------*/
150 /*--------------------------------------*/
151 /* update cache, then return new lines */
153 /* If cache does not exist, then create
154 the new instance. */ /* public */
155 bool DatManager::updateCache( const KURL& url , const QObject* parent )
157 QMutexLocker locker( &m_mutex );
159 DatInfo * datInfo = getDatInfo( url );
161 /* create new DatInfo */
162 if ( datInfo == NULL ){
163 datInfo = new DatInfo( url );
164 m_datInfo.prepend( datInfo );
167 return datInfo->updateCache(parent);
172 int DatManager::getResponseCode( const KURL& url )
174 QMutexLocker locker( &m_mutex );
176 DatInfo * datInfo = getDatInfo( url );
177 if ( datInfo == NULL ) return 0;
179 return datInfo->getResponseCode();
183 int DatManager::getServerTime( const KURL& url )
185 QMutexLocker locker( &m_mutex );
187 DatInfo * datInfo = getDatInfo( url );
188 if ( datInfo == NULL ) return 0;
190 return datInfo->getServerTime();
195 bool DatManager::deleteCache( const KURL& url, QWidget* parent )
197 QMutexLocker locker( &m_mutex );
199 DatInfo * datInfo = getDatInfo( url );
200 if ( datInfo == NULL ) return FALSE;
202 return datInfo->deleteCache(parent);
207 bool DatManager::isLoadingNow( const KURL& url )
209 QMutexLocker locker( &m_mutex );
211 DatInfo * datInfo = getDatInfo( url );
212 if ( datInfo == NULL ) return FALSE;
214 return datInfo->isLoadingNow();
219 void DatManager::stopLoading( const KURL& url )
222 /* Don't use QMutexLocker here !!
223 It will cause deadlock , because
224 Kita::Access::stopJob() calls KitaThreadView::slotFinishLoad() back. */
226 DatInfo * datInfo = getDatInfo( url );
228 if ( datInfo == NULL ) return;
230 return datInfo->stopLoading();
235 /*----------------------*/
236 /* lock, unlock DatInfo */
238 /* If cache does not exist, then create
239 the new instance and lock it.
241 Don't forget to unlock it. */ /* public */
243 void DatManager::lock( const KURL& url )
245 QMutexLocker locker( &m_mutex );
247 DatInfo * datInfo = getDatInfo( url );
249 /* create new DatInfo */
250 if ( datInfo == NULL ){
251 datInfo = new DatInfo( url );
252 m_datInfo.prepend( datInfo );
258 void DatManager::unlock( const KURL& url )
260 QMutexLocker locker( &m_mutex );
262 DatInfo * datInfo = getDatInfo( url );
263 if ( datInfo == NULL ) return;
269 /*--------------------------------------*/
273 const QString& DatManager::getRawDat( const KURL& url )
275 QMutexLocker locker( &m_mutex );
277 DatInfo * datInfo = getDatInfo( url );
278 if ( datInfo == NULL ) return QString::null;
280 return datInfo->getRawDat();
285 const QString& DatManager::getDat( const KURL& url, int num )
287 QMutexLocker locker( &m_mutex );
289 DatInfo * datInfo = getDatInfo( url );
290 if ( datInfo == NULL ) return QString::null;
292 return datInfo->getDat( num );
298 const QString& DatManager::getId( const KURL& url, int num )
300 QMutexLocker locker( &m_mutex );
302 DatInfo * datInfo = getDatInfo( url );
303 if ( datInfo == NULL ) return QString::null;
305 return datInfo->getId( num );
310 const QString& DatManager::getName( const KURL& url, int num )
312 QMutexLocker locker( &m_mutex );
314 DatInfo * datInfo = getDatInfo( url );
315 if ( datInfo == NULL ) return QString::null;
317 return datInfo->getName( num );
322 const QString& DatManager::getBody( const KURL& url, int num )
324 QMutexLocker locker( &m_mutex );
326 DatInfo * datInfo = getDatInfo( url );
327 if ( datInfo == NULL ) return QString::null;
329 return datInfo->getBody( num );
334 QString DatManager::getPlainName( const KURL& url, int num )
336 QMutexLocker locker( &m_mutex );
338 DatInfo * datInfo = getDatInfo( url );
339 if ( datInfo == NULL ) return QString::null;
341 return datInfo->getPlainName(num);
346 QString DatManager::getPlainBody( const KURL& url, int num )
348 QMutexLocker locker( &m_mutex );
350 DatInfo * datInfo = getDatInfo( url );
351 if ( datInfo == NULL ) return QString::null;
353 return datInfo->getPlainBody(num);
358 QString DatManager::getPlainTitle( const KURL& url, int num )
360 QMutexLocker locker( &m_mutex );
362 DatInfo * datInfo = getDatInfo( url );
363 if ( datInfo == NULL ) return QString::null;
365 return datInfo->getPlainTitle(num);
369 /* get URL of thread from URL of dat file. */ /* public */
370 const QString DatManager::threadURL( const KURL& url )
372 QMutexLocker locker( &m_mutex );
374 KURL datURL = Kita::ParseMisc::parseURLonly( url );
375 return Kita::datToThread( datURL.prettyURL() );
379 /* get name (i.e. subject ) of thread from URL of dat file. */ /* public */
380 const QString& DatManager::threadName( const KURL& url )
382 QMutexLocker locker( &m_mutex );
384 KURL datURL = Kita::ParseMisc::parseURLonly( url );
385 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
387 if( thread == NULL ){
389 /* get subject from DatInfo */
390 DatInfo * datInfo = getDatInfo( url );
391 if ( datInfo == NULL ) return QString::null;
392 return datInfo->getSubject();
395 return thread->name();
400 const QString DatManager::threadID( const KURL& url )
402 QMutexLocker locker( &m_mutex );
404 KURL datURL = Kita::ParseMisc::parseURLonly( url );
405 return datURL.filename().section( ".", 0, 0 );
409 /* get URL of board from URL of dat file. */ /* public */
410 const QString DatManager::boardURL( const KURL& url )
412 QMutexLocker locker( &m_mutex );
414 KURL datURL = Kita::ParseMisc::parseURLonly( url );
415 return Kita::datToBoard( datURL.prettyURL() );
419 /* get name of board from URL of dat file. */ /* public */
420 const QString& DatManager::boardName( const KURL& url )
422 QMutexLocker locker( &m_mutex );
424 KURL datURL = Kita::ParseMisc::parseURLonly( url );
425 QString bdURL = Kita::datToBoard( datURL.prettyURL() );
426 return Kita::Board::getName( bdURL );
431 const QString DatManager::boardID( const KURL& url )
433 QMutexLocker locker( &m_mutex );
435 KURL datURL = Kita::ParseMisc::parseURLonly( url );
436 return KURL( Kita::datToBoard( datURL.prettyURL() ) ).fileName();
440 /*---------------------------------------*/
444 QString DatManager::getHtml( const KURL& url, int startnum, int endnum )
446 QMutexLocker locker( &m_mutex );
448 DatInfo * datInfo = getDatInfo( url );
449 if ( datInfo == NULL ) return QString::null;
451 return datInfo->getHtml(startnum,endnum);
457 QString DatManager::getHtmlByID(const KURL& url, const QString& strid, int &count )
459 QMutexLocker locker( &m_mutex );
461 DatInfo* datInfo = getDatInfo(url);
462 if(datInfo == NULL) return QString::null;
464 return datInfo->getHtmlByID(strid,count);
469 /* Get HTML document of res tree.*/ /* public */
470 QString DatManager::getTreeByRes(const KURL& url, const int rootnum, int &count)
472 QMutexLocker locker( &m_mutex );
474 DatInfo* datInfo = getDatInfo(url);
475 if(datInfo == NULL) return QString::null;
477 return datInfo->getTreeByRes(rootnum,count);
480 /* Get HTML document of reverse res tree.*/ /* public */
481 QString DatManager::getTreeByResReverse(const KURL& url, const int rootnum, int &count)
483 QMutexLocker locker( &m_mutex );
485 DatInfo* datInfo = getDatInfo(url);
486 if(datInfo == NULL) return QString::null;
488 return datInfo->getTreeByResReverse(rootnum,count);
491 /* Get DOM element */ /* public */
492 bool DatManager::getDomElement(const KURL& url, int num, DOM::HTMLDocument& hdoc, DOM::Element& retelm)
494 QMutexLocker locker( &m_mutex );
496 DatInfo* datInfo = getDatInfo(url);
497 if(datInfo == NULL) return FALSE;
499 return datInfo->getDomElement(num,hdoc,retelm);
505 int DatManager::getResNum( const KURL& url )
507 QMutexLocker locker( &m_mutex );
509 KURL datURL = Kita::ParseMisc::parseURLonly( url );
510 return KitaThreadInfo::resNum( datURL.prettyURL() );
515 int DatManager::getReadNum( const KURL& url )
517 QMutexLocker locker( &m_mutex );
519 DatInfo * datInfo = getDatInfo( url );
520 if ( datInfo == NULL ) return 0;
522 return datInfo->getReadNum();
527 int DatManager::getKokoyonNum( const KURL& url )
529 QMutexLocker locker( &m_mutex );
531 DatInfo * datInfo = getDatInfo( url );
532 if ( datInfo == NULL ) return 0;
534 return datInfo->getKokoyonNum();
539 void DatManager::setKokoyonNum( const KURL& url , int num )
541 QMutexLocker locker( &m_mutex );
543 DatInfo * datInfo = getDatInfo( url );
544 if ( datInfo == NULL ) return;
546 return datInfo->setKokoyonNum(num);
551 int DatManager::getDatSize( const KURL& url )
553 QMutexLocker locker( &m_mutex );
555 DatInfo * datInfo = getDatInfo( url );
556 if ( datInfo == NULL ) return 0;
558 return datInfo->getDatSize();
562 int DatManager::getNumByID( const KURL& url, const QString& strid )
564 QMutexLocker locker( &m_mutex );
566 DatInfo * datInfo = getDatInfo( url );
567 if ( datInfo == NULL ) return 0;
569 return datInfo->getNumByID(strid);
574 bool DatManager::isResValid( const KURL& url,int num )
576 QMutexLocker locker( &m_mutex );
578 DatInfo * datInfo = getDatInfo( url );
579 if ( datInfo == NULL ) return FALSE;
581 return datInfo->isResValid(num);
586 bool DatManager::isBroken( const KURL& url )
588 QMutexLocker locker( &m_mutex );
590 DatInfo * datInfo = getDatInfo( url );
591 if ( datInfo == NULL ) return FALSE;
593 return datInfo->isBroken();
597 bool DatManager::isResBroken( const KURL& url,int num )
599 QMutexLocker locker( &m_mutex );
601 DatInfo * datInfo = getDatInfo( url );
602 if ( datInfo == NULL ) return FALSE;
604 return datInfo->isResBroken(num);
610 bool DatManager::checkID(const KURL& url, const QString& strid, int num )
612 QMutexLocker locker( &m_mutex );
614 DatInfo* datInfo = getDatInfo(url);
615 if(datInfo == NULL) return FALSE;
617 return datInfo->checkID(strid,num);
621 /* check keywords */ /* public */
622 bool DatManager::checkWord(const KURL& url,
623 QStringList& strlist, int num,
624 bool checkOR /* AND or OR search */
627 QMutexLocker locker( &m_mutex );
629 DatInfo* datInfo = getDatInfo(url);
630 if(datInfo == NULL) return FALSE;
632 return datInfo->checkWord(strlist,num,checkOR);
637 bool DatManager::isMarked( const KURL& url, int num )
639 QMutexLocker locker( &m_mutex );
641 DatInfo * datInfo = getDatInfo( url );
642 if ( datInfo == NULL ) return FALSE;
644 return datInfo->isMarked( num );
649 void DatManager::setMark( const KURL& url, int num, bool mark )
651 QMutexLocker locker( &m_mutex );
653 DatInfo * datInfo = getDatInfo( url );
654 if ( datInfo == NULL ) return;
656 datInfo->setMark( num, mark );
661 bool DatManager::checkAbone(const KURL& url, int num)
663 QMutexLocker locker( &m_mutex );
665 DatInfo* datInfo = getDatInfo(url);
666 if(datInfo == NULL) return FALSE;
668 return datInfo->checkAbone(num);
673 void DatManager::resetAbone(const KURL& url)
675 QMutexLocker locker( &m_mutex );
677 DatInfo* datInfo = getDatInfo(url);
678 if(datInfo == NULL) return;
680 datInfo->resetAbone();