OSDN Git Service

>>968 more fixes.
[kita/kita.git] / kita / src / libkita / datmanager.cpp
1 /**************************************************************************
2 *   Copyright (C) 2003 by Hideki Ikemoto , (c)2004 by 421                 *
3 *   ikemo@wakaba.jp                                                       *
4 *                                                                         *
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 ***************************************************************************/
10
11 #include <qobject.h>
12 #include <qmutex.h>
13 #include <qstringlist.h>
14
15 #include "thread.h"
16 #include "threadinfo.h"
17 #include "parsemisc.h"
18 #include "datmanager.h"
19 #include "datinfo.h"
20 #include "board.h"
21 #include "kita_misc.h"
22 #include "kita-utf8.h"
23 #include "kita-utf16.h"
24
25 using namespace Kita;
26
27 #define DMANAGER_MAXQUEUE 16
28
29 /*-------------------------------------------------*/
30 /* DatManager manages all information about *.dat. */
31
32 DatInfoList DatManager::m_datInfo;
33 QMutex DatManager::m_mutex;
34
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 )
38 {
39     if ( url.isEmpty() ) {
40         return NULL;
41     }
42
43     int i = 0;
44     DatInfoList::Iterator it;
45     DatInfo* datInfo;
46     
47     KURL inurl = Kita::ParseMisc::parseURLonly( url );
48     
49     /* search */
50     if ( m_datInfo.count() ) {
51         for ( it = m_datInfo.begin(); it != m_datInfo.end(); ++it, i++ ) {
52
53             datInfo = ( *it );
54
55             if ( inurl == datInfo->url() ){
56
57                 /* LRU */
58                 if ( i ) {
59                     m_datInfo.remove( it );
60                     m_datInfo.prepend( datInfo );
61                 }
62
63                 return datInfo;
64             }
65         }
66     }
67
68     /* not found */
69
70     /*create new DatInfo and insert it into list. */
71     KURL daturl = url.protocol() + "://" + url.host() + url.path();
72
73     datInfo = new DatInfo( daturl );
74     if ( datInfo->getRawDat() == QString::null ) { /* cache does not exist */
75         delete( datInfo );
76
77         return NULL;
78     }
79
80     m_datInfo.prepend( datInfo );
81
82     /* delete the last items of list */
83     if ( m_datInfo.count() > DMANAGER_MAXQUEUE ) {
84
85         /* collect unlocked datInfos */
86         typedef QValueList<KURL> DELETELIST;
87         DELETELIST deleteList;
88
89         for ( it = m_datInfo.at(DMANAGER_MAXQUEUE); it != m_datInfo.end(); ++it ) 
90             if(! (*it)->isLocked() ) deleteList += ( *it ) ->url();
91
92         /* delete unlocked datInfos */
93         for (DELETELIST::Iterator itdel = deleteList.begin(); itdel != deleteList.end(); ++itdel )
94             deleteDatInfoPrivate( (*itdel) );
95     }
96
97     return datInfo;
98 }
99
100
101 /* get pointer of DatInfo */
102
103 /*    !!!  NOTICE  !!!   */
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 )
107 {
108     QMutexLocker locker( &m_mutex );
109     
110     return getDatInfo( url );
111 }
112
113
114
115 /* public */
116 void DatManager::deleteDatInfo( const KURL& url )
117 {
118     QMutexLocker locker( &m_mutex );
119
120     deleteDatInfoPrivate(url);
121 }
122
123
124 /* private */
125 void DatManager::deleteDatInfoPrivate( const KURL& url ){
126
127     DatInfoList::Iterator it;
128
129     for ( it = m_datInfo.begin(); it != m_datInfo.end(); ++it ) {
130         if ( url == ( *it )->url() ){
131
132             if(! (*it)->isLocked() ){
133                 m_datInfo.remove( it );
134                 delete( *it );
135             }
136
137             return ;
138         }
139     }
140
141 }
142
143
144
145
146 /*-------------------------------------------------------------*/
147
148
149
150 /*--------------------------------------*/
151 /* update cache, then return new lines  */
152
153 /* If cache does not exist, then create
154    the new instance.                    */ /* public */
155 bool DatManager::updateCache( const KURL& url , const QObject* parent )
156 {
157     QMutexLocker locker( &m_mutex );
158     
159     DatInfo * datInfo = getDatInfo( url );
160
161     /* create new DatInfo */
162     if ( datInfo == NULL ){
163         datInfo = new DatInfo( url );
164         m_datInfo.prepend( datInfo );
165     }
166     
167     return datInfo->updateCache(parent);
168 }
169
170
171 /* public */
172 int DatManager::getResponseCode( const KURL& url )
173 {
174     QMutexLocker locker( &m_mutex );
175
176     DatInfo * datInfo = getDatInfo( url );
177     if ( datInfo == NULL ) return 0;
178
179     return datInfo->getResponseCode();
180 }
181
182 /* public */
183 int DatManager::getServerTime( const KURL& url )
184 {
185     QMutexLocker locker( &m_mutex );
186
187     DatInfo * datInfo = getDatInfo( url );
188     if ( datInfo == NULL ) return 0;
189
190     return datInfo->getServerTime();
191 }
192
193
194 /* public */
195 bool DatManager::deleteCache( const KURL& url, QWidget* parent )
196 {
197     QMutexLocker locker( &m_mutex );
198
199     DatInfo * datInfo = getDatInfo( url );
200     if ( datInfo == NULL ) return FALSE;
201
202     return datInfo->deleteCache(parent);
203 }
204
205
206 /* public */
207 bool DatManager::isLoadingNow( const KURL& url )
208 {
209     QMutexLocker locker( &m_mutex );
210
211     DatInfo * datInfo = getDatInfo( url );
212     if ( datInfo == NULL ) return FALSE;
213
214     return datInfo->isLoadingNow();
215 }
216
217
218 /* public */
219 void DatManager::stopLoading( const KURL& url )
220 {
221
222     /* Don't use QMutexLocker here !!
223        It will cause deadlock , because
224        Kita::Access::stopJob() calls KitaThreadView::slotFinishLoad() back.  */
225     m_mutex.lock();
226     DatInfo * datInfo = getDatInfo( url );
227     m_mutex.unlock();
228     if ( datInfo == NULL ) return;
229
230     return datInfo->stopLoading();
231 }
232
233
234
235 /*----------------------*/
236 /* lock, unlock DatInfo */
237
238 /* If cache does not exist, then create
239    the new instance and lock it. 
240
241    Don't forget to unlock it. */  /* public */
242
243 void DatManager::lock( const KURL& url )
244 {
245     QMutexLocker locker( &m_mutex );
246     
247     DatInfo * datInfo = getDatInfo( url );
248
249     /* create new DatInfo */
250     if ( datInfo == NULL ){
251         datInfo = new DatInfo( url );
252         m_datInfo.prepend( datInfo );
253     }
254     
255     datInfo->lock();
256 }
257
258 void DatManager::unlock( const KURL& url )
259 {
260     QMutexLocker locker( &m_mutex );
261     
262     DatInfo * datInfo = getDatInfo( url );
263     if ( datInfo == NULL ) return;
264     
265     datInfo->unlock();
266 }
267
268
269 /*--------------------------------------*/
270 /* string data */
271
272 /* public */
273 const QString& DatManager::getRawDat( const KURL& url )
274 {
275     QMutexLocker locker( &m_mutex );
276     
277     DatInfo * datInfo = getDatInfo( url );
278     if ( datInfo == NULL ) return QString::null;
279
280     return datInfo->getRawDat();
281 }
282
283
284 /* public */
285 const QString& DatManager::getDat( const KURL& url, int num )
286 {
287     QMutexLocker locker( &m_mutex );
288     
289     DatInfo * datInfo = getDatInfo( url );
290     if ( datInfo == NULL ) return QString::null;
291
292     return datInfo->getDat( num );
293 }
294
295
296
297 /* public */
298 const QString& DatManager::getId( const KURL& url, int num )
299 {
300     QMutexLocker locker( &m_mutex );
301     
302     DatInfo * datInfo = getDatInfo( url );
303     if ( datInfo == NULL ) return QString::null;
304
305     return datInfo->getId( num );
306 }
307
308
309 /* public */
310 const QString& DatManager::getName( const KURL& url, int num )
311 {
312     QMutexLocker locker( &m_mutex );
313     
314     DatInfo * datInfo = getDatInfo( url );
315     if ( datInfo == NULL ) return QString::null;
316
317     return datInfo->getName( num );
318 }
319
320
321 /* public */
322 const QString& DatManager::getBody( const KURL& url, int num )
323 {
324     QMutexLocker locker( &m_mutex );
325     
326     DatInfo * datInfo = getDatInfo( url );
327     if ( datInfo == NULL ) return QString::null;
328
329     return datInfo->getBody( num );
330 }
331
332
333 /* public */
334 QString DatManager::getPlainName( const KURL& url, int num )
335 {
336     QMutexLocker locker( &m_mutex );
337     
338     DatInfo * datInfo = getDatInfo( url );
339     if ( datInfo == NULL ) return QString::null;
340
341     return datInfo->getPlainName(num);
342 }
343
344
345 /* public */
346 QString DatManager::getPlainBody( const KURL& url, int num )
347 {
348     QMutexLocker locker( &m_mutex );
349     
350     DatInfo * datInfo = getDatInfo( url );
351     if ( datInfo == NULL ) return QString::null;
352
353     return datInfo->getPlainBody(num);
354 }
355
356
357 /* public */
358 QString DatManager::getPlainTitle( const KURL& url, int num )
359 {
360     QMutexLocker locker( &m_mutex );
361     
362     DatInfo * datInfo = getDatInfo( url );
363     if ( datInfo == NULL ) return QString::null;
364
365     return datInfo->getPlainTitle(num);
366 }
367
368
369 /* get URL of thread from URL of dat file. */ /* public */
370 const QString DatManager::threadURL( const KURL& url )
371 {
372     QMutexLocker locker( &m_mutex );
373
374     KURL datURL =  Kita::ParseMisc::parseURLonly( url );
375     return Kita::datToThread( datURL.prettyURL() );
376 }
377
378
379 /* get name (i.e. subject ) of thread from URL of dat file. */ /* public */
380 const QString& DatManager::threadName( const KURL& url )
381 {
382     QMutexLocker locker( &m_mutex );
383
384     KURL datURL =  Kita::ParseMisc::parseURLonly( url );    
385     Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
386
387     if( thread == NULL ){
388         
389         /* get subject from DatInfo */
390         DatInfo * datInfo = getDatInfo( url );
391         if ( datInfo == NULL ) return QString::null;
392         return datInfo->getSubject();
393     }
394
395     return thread->name();
396 }
397
398
399 /* public */
400 const QString DatManager::threadID( const KURL& url )
401 {
402     QMutexLocker locker( &m_mutex );
403
404     KURL datURL =  Kita::ParseMisc::parseURLonly( url );
405     return datURL.filename().section( ".", 0, 0 );
406 }
407
408
409 /* get URL of board from URL of dat file. */ /* public */
410 const QString DatManager::boardURL( const KURL& url )
411 {
412     QMutexLocker locker( &m_mutex );
413     
414     KURL datURL =  Kita::ParseMisc::parseURLonly( url );
415     return Kita::datToBoard( datURL.prettyURL() );
416 }
417
418
419 /* get name of board from URL of dat file. */ /* public */
420 const QString& DatManager::boardName( const KURL& url )
421 {
422     QMutexLocker locker( &m_mutex );
423
424     KURL datURL =  Kita::ParseMisc::parseURLonly( url );
425     QString bdURL = Kita::datToBoard( datURL.prettyURL() );
426     return Kita::Board::getName( bdURL );
427 }
428
429
430 /* public */
431 const QString DatManager::boardID( const KURL& url )
432 {
433     QMutexLocker locker( &m_mutex );
434     
435     KURL datURL =  Kita::ParseMisc::parseURLonly( url );
436     return KURL( Kita::datToBoard( datURL.prettyURL() ) ).fileName();
437 }
438
439
440 /*---------------------------------------*/
441 /* HTML data */
442
443 /* public */
444 QString DatManager::getHtml( const KURL& url, int startnum, int endnum )
445 {
446     QMutexLocker locker( &m_mutex );
447     
448     DatInfo * datInfo = getDatInfo( url );
449     if ( datInfo == NULL ) return QString::null;
450
451     return datInfo->getHtml(startnum,endnum);
452 }
453
454
455
456 /* public */
457 QString DatManager::getHtmlByID(const KURL& url, const QString& strid, int &count )
458 {
459     QMutexLocker locker( &m_mutex );
460     
461     DatInfo* datInfo = getDatInfo(url);
462     if(datInfo == NULL) return QString::null;
463
464     return datInfo->getHtmlByID(strid,count);
465 }
466
467
468
469 /* Get HTML document of res tree.*/ /* public */
470 QString DatManager::getTreeByRes(const KURL& url, const int rootnum, int &count)
471 {
472     QMutexLocker locker( &m_mutex );
473     
474     DatInfo* datInfo = getDatInfo(url);
475     if(datInfo == NULL) return QString::null;
476
477     return datInfo->getTreeByRes(rootnum,count);
478 }
479
480 /* Get HTML document of reverse res tree.*/ /* public */
481 QString DatManager::getTreeByResReverse(const KURL& url, const int rootnum, int &count)
482 {
483     QMutexLocker locker( &m_mutex );
484     
485     DatInfo* datInfo = getDatInfo(url);
486     if(datInfo == NULL) return QString::null;
487
488     return datInfo->getTreeByResReverse(rootnum,count);
489 }
490
491 /* Get DOM element */ /* public */
492 bool DatManager::getDomElement(const KURL& url, int num, DOM::HTMLDocument& hdoc, DOM::Element& retelm)
493 {
494     QMutexLocker locker( &m_mutex );
495     
496     DatInfo* datInfo = getDatInfo(url);
497     if(datInfo == NULL) return FALSE;
498
499     return datInfo->getDomElement(num,hdoc,retelm);
500 }
501
502
503
504 /* public */
505 int DatManager::getResNum( const KURL& url )
506 {
507     QMutexLocker locker( &m_mutex );
508
509     KURL datURL =  Kita::ParseMisc::parseURLonly( url );
510     return KitaThreadInfo::resNum( datURL.prettyURL() );
511 }
512
513
514 /* public */
515 int DatManager::getReadNum( const KURL& url )
516 {
517     QMutexLocker locker( &m_mutex );
518     
519     DatInfo * datInfo = getDatInfo( url );
520     if ( datInfo == NULL ) return 0;
521
522     return datInfo->getReadNum();
523 }
524
525
526 /* public */
527 int DatManager::getKokoyonNum( const KURL& url )
528 {
529     QMutexLocker locker( &m_mutex );
530     
531     DatInfo * datInfo = getDatInfo( url );
532     if ( datInfo == NULL ) return 0;
533
534     return datInfo->getKokoyonNum();
535 }
536
537
538 /* public */
539 void DatManager::setKokoyonNum( const KURL& url , int num )
540 {
541     QMutexLocker locker( &m_mutex );
542     
543     DatInfo * datInfo = getDatInfo( url );
544     if ( datInfo == NULL ) return;
545
546     return datInfo->setKokoyonNum(num);
547 }
548
549
550 /* public */
551 int DatManager::getDatSize( const KURL& url )
552 {
553     QMutexLocker locker( &m_mutex );
554     
555     DatInfo * datInfo = getDatInfo( url );
556     if ( datInfo == NULL ) return 0;
557
558     return datInfo->getDatSize();
559 }
560
561 /* public */
562 int DatManager::getNumByID( const KURL& url, const QString& strid )
563 {
564     QMutexLocker locker( &m_mutex );
565     
566     DatInfo * datInfo = getDatInfo( url );
567     if ( datInfo == NULL ) return 0;
568
569     return datInfo->getNumByID(strid);
570 }
571
572
573 /* public */
574 bool DatManager::isResValid( const KURL& url,int num )
575 {
576     QMutexLocker locker( &m_mutex );
577     
578     DatInfo * datInfo = getDatInfo( url );
579     if ( datInfo == NULL ) return FALSE;
580
581     return datInfo->isResValid(num);
582 }
583
584
585 /* public */
586 bool DatManager::isBroken( const KURL& url )
587 {
588     QMutexLocker locker( &m_mutex );
589     
590     DatInfo * datInfo = getDatInfo( url );
591     if ( datInfo == NULL ) return FALSE;
592
593     return datInfo->isBroken();
594 }
595
596 /* public */
597 bool DatManager::isResBroken( const KURL& url,int num )
598 {
599     QMutexLocker locker( &m_mutex );
600     
601     DatInfo * datInfo = getDatInfo( url );
602     if ( datInfo == NULL ) return FALSE;
603
604     return datInfo->isResBroken(num);
605 }
606
607
608
609 /* public */
610 bool DatManager::checkID(const KURL& url, const QString& strid, int num )
611 {
612     QMutexLocker locker( &m_mutex );
613     
614     DatInfo* datInfo = getDatInfo(url);
615     if(datInfo == NULL) return FALSE;
616     
617     return datInfo->checkID(strid,num);
618 }
619
620
621 /* check keywords */ /* public */
622 bool DatManager::checkWord(const KURL& url,
623                             QStringList& strlist, int num,
624                             bool checkOR /* AND or OR search */
625     )
626 {
627     QMutexLocker locker( &m_mutex );
628     
629     DatInfo* datInfo = getDatInfo(url);
630     if(datInfo == NULL) return FALSE;
631
632     return datInfo->checkWord(strlist,num,checkOR);
633 }
634
635
636 /* public */
637 bool DatManager::isMarked( const KURL& url, int num )
638 {
639     QMutexLocker locker( &m_mutex );
640     
641     DatInfo * datInfo = getDatInfo( url );
642     if ( datInfo == NULL ) return FALSE;
643
644     return datInfo->isMarked( num );
645 }
646
647
648 /* public */
649 void DatManager::setMark( const KURL& url, int num, bool mark )
650 {
651     QMutexLocker locker( &m_mutex );
652     
653     DatInfo * datInfo = getDatInfo( url );
654     if ( datInfo == NULL ) return;
655
656     datInfo->setMark( num, mark );
657 }
658
659
660 /* public */
661 bool DatManager::checkAbone(const KURL& url, int num)
662 {
663     QMutexLocker locker( &m_mutex );
664     
665     DatInfo* datInfo = getDatInfo(url);
666     if(datInfo == NULL) return FALSE;
667
668     return datInfo->checkAbone(num);
669 }
670
671
672 /* public */
673 void DatManager::resetAbone(const KURL& url)
674 {
675     QMutexLocker locker( &m_mutex );
676     
677     DatInfo* datInfo = getDatInfo(url);
678     if(datInfo == NULL) return;
679
680     datInfo->resetAbone();
681 }
682
683
684
685
686
687