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 "parsemisc.h"
17 #include "datmanager.h"
20 #include "kita_misc.h"
21 #include "kita-utf8.h"
22 #include "kita-utf16.h"
26 #define DMANAGER_MAXQUEUE 16
28 /*-------------------------------------------------*/
29 /* DatManager manages all information about *.dat. */
31 DatInfoList DatManager::m_datInfo;
32 QMutex DatManager::m_mutex;
34 /* This function searches instance of DatInfo specified by url.
35 If instance does not exist, create instance. */ /* private */
36 DatInfo* DatManager::getDatInfo( const KURL& url )
38 if ( url.isEmpty() ) {
43 DatInfoList::Iterator it;
46 KURL inurl = Kita::ParseMisc::parseURLonly( url );
49 if ( m_datInfo.count() ) {
50 for ( it = m_datInfo.begin(); it != m_datInfo.end(); ++it, i++ ) {
54 if ( inurl == datInfo->url() ){
58 m_datInfo.remove( it );
59 m_datInfo.prepend( datInfo );
69 /*create new DatInfo and insert it into list. */
70 KURL daturl = url.protocol() + "://" + url.host() + url.path();
72 datInfo = new DatInfo( daturl );
73 if ( datInfo->getRawDat() == QString::null ) { /* cache does not exist */
79 m_datInfo.prepend( datInfo );
81 /* delete the last items of list */
82 if ( m_datInfo.count() > DMANAGER_MAXQUEUE ) {
84 /* collect unlocked datInfos */
85 typedef QValueList<KURL> DELETELIST;
86 DELETELIST deleteList;
88 for ( it = m_datInfo.at(DMANAGER_MAXQUEUE); it != m_datInfo.end(); ++it )
89 if(! (*it)->isLocked() ) deleteList += ( *it ) ->url();
91 /* delete unlocked datInfos */
92 for (DELETELIST::Iterator itdel = deleteList.begin(); itdel != deleteList.end(); ++itdel )
93 deleteDatInfoPrivate( (*itdel) );
100 /* get pointer of DatInfo */
103 /* It is very dangerous to access to DatInfo directly. */
104 /* Usually, access to it through DatManager. */ /* public */
105 DatInfo * DatManager::getDatInfoPointer( const KURL& url )
107 QMutexLocker locker( &m_mutex );
109 return getDatInfo( url );
115 void DatManager::deleteDatInfo( const KURL& url )
117 QMutexLocker locker( &m_mutex );
119 deleteDatInfoPrivate(url);
124 void DatManager::deleteDatInfoPrivate( const KURL& url ){
126 DatInfoList::Iterator it;
128 for ( it = m_datInfo.begin(); it != m_datInfo.end(); ++it ) {
129 if ( url == ( *it )->url() ){
131 if(! (*it)->isLocked() ){
132 m_datInfo.remove( it );
145 /*-------------------------------------------------------------*/
149 /*--------------------------------------*/
150 /* update cache, then return new lines */
152 /* If cache does not exist, then create
153 the new instance. */ /* public */
154 bool DatManager::updateCache( const KURL& url , const QObject* parent )
156 QMutexLocker locker( &m_mutex );
158 DatInfo * datInfo = getDatInfo( url );
160 /* create new DatInfo */
161 if ( datInfo == NULL ){
162 datInfo = new DatInfo( url );
163 m_datInfo.prepend( datInfo );
166 return datInfo->updateCache(parent);
171 int DatManager::getResponseCode( const KURL& url )
173 QMutexLocker locker( &m_mutex );
175 DatInfo * datInfo = getDatInfo( url );
176 if ( datInfo == NULL ) return 0;
178 return datInfo->getResponseCode();
182 int DatManager::getServerTime( const KURL& url )
184 QMutexLocker locker( &m_mutex );
186 DatInfo * datInfo = getDatInfo( url );
187 if ( datInfo == NULL ) return 0;
189 return datInfo->getServerTime();
194 bool DatManager::deleteCache( const KURL& url, QWidget* parent )
196 QMutexLocker locker( &m_mutex );
198 DatInfo * datInfo = getDatInfo( url );
199 if ( datInfo == NULL ) return FALSE;
201 return datInfo->deleteCache(parent);
206 bool DatManager::isLoadingNow( const KURL& url )
208 QMutexLocker locker( &m_mutex );
210 DatInfo * datInfo = getDatInfo( url );
211 if ( datInfo == NULL ) return FALSE;
213 return datInfo->isLoadingNow();
218 void DatManager::stopLoading( const KURL& url )
221 /* Don't use QMutexLocker here !!
222 It will cause deadlock , because
223 Kita::Access::stopJob() calls KitaThreadView::slotFinishLoad() back. */
225 DatInfo * datInfo = getDatInfo( url );
227 if ( datInfo == NULL ) return;
229 return datInfo->stopLoading();
234 /*----------------------*/
235 /* lock, unlock DatInfo */
237 /* If cache does not exist, then create
238 the new instance and lock it.
240 Don't forget to unlock it. */ /* public */
242 void DatManager::lock( const KURL& url )
244 QMutexLocker locker( &m_mutex );
246 DatInfo * datInfo = getDatInfo( url );
248 /* create new DatInfo */
249 if ( datInfo == NULL ){
250 datInfo = new DatInfo( url );
251 m_datInfo.prepend( datInfo );
257 void DatManager::unlock( const KURL& url )
259 QMutexLocker locker( &m_mutex );
261 DatInfo * datInfo = getDatInfo( url );
262 if ( datInfo == NULL ) return;
268 /*--------------------------------------*/
272 const QString& DatManager::getRawDat( const KURL& url )
274 QMutexLocker locker( &m_mutex );
276 DatInfo * datInfo = getDatInfo( url );
277 if ( datInfo == NULL ) return QString::null;
279 return datInfo->getRawDat();
284 const QString& DatManager::getDat( const KURL& url, int num )
286 QMutexLocker locker( &m_mutex );
288 DatInfo * datInfo = getDatInfo( url );
289 if ( datInfo == NULL ) return QString::null;
291 return datInfo->getDat( num );
297 const QString& DatManager::getId( const KURL& url, int num )
299 QMutexLocker locker( &m_mutex );
301 DatInfo * datInfo = getDatInfo( url );
302 if ( datInfo == NULL ) return QString::null;
304 return datInfo->getId( num );
309 const QString& DatManager::getName( const KURL& url, int num )
311 QMutexLocker locker( &m_mutex );
313 DatInfo * datInfo = getDatInfo( url );
314 if ( datInfo == NULL ) return QString::null;
316 return datInfo->getName( num );
321 const QString& DatManager::getBody( const KURL& url, int num )
323 QMutexLocker locker( &m_mutex );
325 DatInfo * datInfo = getDatInfo( url );
326 if ( datInfo == NULL ) return QString::null;
328 return datInfo->getBody( num );
333 QString DatManager::getPlainName( const KURL& url, int num )
335 QMutexLocker locker( &m_mutex );
337 DatInfo * datInfo = getDatInfo( url );
338 if ( datInfo == NULL ) return QString::null;
340 return datInfo->getPlainName(num);
345 QString DatManager::getPlainBody( const KURL& url, int num )
347 QMutexLocker locker( &m_mutex );
349 DatInfo * datInfo = getDatInfo( url );
350 if ( datInfo == NULL ) return QString::null;
352 return datInfo->getPlainBody(num);
357 QString DatManager::getPlainTitle( const KURL& url, int num )
359 QMutexLocker locker( &m_mutex );
361 DatInfo * datInfo = getDatInfo( url );
362 if ( datInfo == NULL ) return QString::null;
364 return datInfo->getPlainTitle(num);
368 /* get URL of thread from URL of dat file. */ /* public */
369 const QString DatManager::threadURL( const KURL& url )
371 QMutexLocker locker( &m_mutex );
373 KURL datURL = Kita::ParseMisc::parseURLonly( url );
374 return Kita::datToThread( datURL.prettyURL() );
378 /* get name (i.e. subject ) of thread from URL of dat file. */ /* public */
379 const QString& DatManager::threadName( const KURL& url )
381 QMutexLocker locker( &m_mutex );
383 KURL datURL = Kita::ParseMisc::parseURLonly( url );
384 Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
386 if( thread == NULL ){
388 /* get subject from DatInfo */
389 DatInfo * datInfo = getDatInfo( url );
390 if ( datInfo == NULL ) return QString::null;
391 return datInfo->getSubject();
394 return thread->name();
399 const QString DatManager::threadID( const KURL& url )
401 QMutexLocker locker( &m_mutex );
403 KURL datURL = Kita::ParseMisc::parseURLonly( url );
404 return datURL.filename().section( ".", 0, 0 );
408 /* get URL of board from URL of dat file. */ /* public */
409 const QString DatManager::boardURL( const KURL& url )
411 QMutexLocker locker( &m_mutex );
413 KURL datURL = Kita::ParseMisc::parseURLonly( url );
414 return Kita::datToBoard( datURL.prettyURL() );
418 /* get name of board from URL of dat file. */ /* public */
419 const QString& DatManager::boardName( const KURL& url )
421 QMutexLocker locker( &m_mutex );
423 KURL datURL = Kita::ParseMisc::parseURLonly( url );
424 QString bdURL = Kita::datToBoard( datURL.prettyURL() );
425 return Kita::Board::getName( bdURL );
430 const QString DatManager::boardID( const KURL& url )
432 QMutexLocker locker( &m_mutex );
434 KURL datURL = Kita::ParseMisc::parseURLonly( url );
435 return KURL( Kita::datToBoard( datURL.prettyURL() ) ).fileName();
439 /*---------------------------------------*/
443 QString DatManager::getHtml( const KURL& url, int startnum, int endnum )
445 QMutexLocker locker( &m_mutex );
447 DatInfo * datInfo = getDatInfo( url );
448 if ( datInfo == NULL ) return QString::null;
450 return datInfo->getHtml(startnum,endnum);
456 QString DatManager::getHtmlByID(const KURL& url, const QString& strid, int &count )
458 QMutexLocker locker( &m_mutex );
460 DatInfo* datInfo = getDatInfo(url);
461 if(datInfo == NULL) return QString::null;
463 return datInfo->getHtmlByID(strid,count);
468 /* Get HTML document of res tree.*/ /* public */
469 QString DatManager::getTreeByRes(const KURL& url, const int rootnum, int &count)
471 QMutexLocker locker( &m_mutex );
473 DatInfo* datInfo = getDatInfo(url);
474 if(datInfo == NULL) return QString::null;
476 return datInfo->getTreeByRes(rootnum,count);
479 /* Get HTML document of reverse res tree.*/ /* public */
480 QString DatManager::getTreeByResReverse(const KURL& url, const int rootnum, int &count)
482 QMutexLocker locker( &m_mutex );
484 DatInfo* datInfo = getDatInfo(url);
485 if(datInfo == NULL) return QString::null;
487 return datInfo->getTreeByResReverse(rootnum,count);
490 /* Get DOM element */ /* public */
491 bool DatManager::getDomElement(const KURL& url, int num, DOM::HTMLDocument& hdoc, DOM::Element& retelm)
493 QMutexLocker locker( &m_mutex );
495 DatInfo* datInfo = getDatInfo(url);
496 if(datInfo == NULL) return FALSE;
498 return datInfo->getDomElement(num,hdoc,retelm);
504 int DatManager::getMaxResNumber( const KURL& url )
506 QMutexLocker locker( &m_mutex );
508 DatInfo * datInfo = getDatInfo( url );
509 if ( datInfo == NULL ) return 0;
511 return datInfo->getMaxResNumber();
517 int DatManager::getNumByID( const KURL& url, const QString& strid )
519 QMutexLocker locker( &m_mutex );
521 DatInfo * datInfo = getDatInfo( url );
522 if ( datInfo == NULL ) return 0;
524 return datInfo->getNumByID(strid);
530 int DatManager::getKokoyonNum( const KURL& url )
532 QMutexLocker locker( &m_mutex );
534 DatInfo * datInfo = getDatInfo( url );
535 if ( datInfo == NULL ) return 0;
537 return datInfo->getKokoyonNum();
542 void DatManager::setKokoyonNum( const KURL& url , int num )
544 QMutexLocker locker( &m_mutex );
546 DatInfo * datInfo = getDatInfo( url );
547 if ( datInfo == NULL ) return;
549 return datInfo->setKokoyonNum(num);
554 int DatManager::getDatSize( const KURL& url )
556 QMutexLocker locker( &m_mutex );
558 DatInfo * datInfo = getDatInfo( url );
559 if ( datInfo == NULL ) return 0;
561 return datInfo->getDatSize();
568 bool DatManager::isResValid( const KURL& url,int num )
570 QMutexLocker locker( &m_mutex );
572 DatInfo * datInfo = getDatInfo( url );
573 if ( datInfo == NULL ) return FALSE;
575 return datInfo->isResValid(num);
580 bool DatManager::isBroken( const KURL& url )
582 QMutexLocker locker( &m_mutex );
584 DatInfo * datInfo = getDatInfo( url );
585 if ( datInfo == NULL ) return FALSE;
587 return datInfo->isBroken();
591 bool DatManager::isResBroken( const KURL& url,int num )
593 QMutexLocker locker( &m_mutex );
595 DatInfo * datInfo = getDatInfo( url );
596 if ( datInfo == NULL ) return FALSE;
598 return datInfo->isResBroken(num);
604 bool DatManager::checkID(const KURL& url, const QString& strid, int num )
606 QMutexLocker locker( &m_mutex );
608 DatInfo* datInfo = getDatInfo(url);
609 if(datInfo == NULL) return FALSE;
611 return datInfo->checkID(strid,num);
615 /* check keywords */ /* public */
616 bool DatManager::checkWord(const KURL& url,
617 QStringList& strlist, int num,
618 bool checkOR /* AND or OR search */
621 QMutexLocker locker( &m_mutex );
623 DatInfo* datInfo = getDatInfo(url);
624 if(datInfo == NULL) return FALSE;
626 return datInfo->checkWord(strlist,num,checkOR);
631 bool DatManager::isMarked( const KURL& url, int num )
633 QMutexLocker locker( &m_mutex );
635 DatInfo * datInfo = getDatInfo( url );
636 if ( datInfo == NULL ) return FALSE;
638 return datInfo->isMarked( num );
643 void DatManager::setMark( const KURL& url, int num, bool mark )
645 QMutexLocker locker( &m_mutex );
647 DatInfo * datInfo = getDatInfo( url );
648 if ( datInfo == NULL ) return;
650 datInfo->setMark( num, mark );
655 bool DatManager::checkAbone(const KURL& url, int num)
657 QMutexLocker locker( &m_mutex );
659 DatInfo* datInfo = getDatInfo(url);
660 if(datInfo == NULL) return FALSE;
662 return datInfo->checkAbone(num);
667 void DatManager::resetAbone(const KURL& url)
669 QMutexLocker locker( &m_mutex );
671 DatInfo* datInfo = getDatInfo(url);
672 if(datInfo == NULL) return;
674 datInfo->resetAbone();