OSDN Git Service

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