OSDN Git Service

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