OSDN Git Service

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