OSDN Git Service

move Thread -> DatInfo
[kita/kita.git] / kita / src / libkita / datmanager.cpp
1 /***************************************************************************
2 *   Copyright (C) 2004 by Kita Developers                                 *
3 *   ikemo@users.sourceforge.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 /* DatManager manages all information about thread */
12
13 #include <qobject.h>
14 #include <qmutex.h>
15 #include <qstringlist.h>
16 #include <qregexp.h>
17 #include <qfile.h>
18
19 #include "thread.h"
20 #include "threadinfo.h"
21 #include "datmanager.h"
22 #include "datinfo.h"
23 #include "cache.h"
24 #include "kita_misc.h"
25 #include "kita-utf8.h"
26 #include "kita-utf16.h"
27 #include "threadindex.h"
28 #include "boardmanager.h"
29
30 using namespace Kita;
31
32 #define DMANAGER_MAXQUEUE 16
33
34 DatInfoList DatManager::m_datInfoList;
35
36
37 /*-----------------------------------------------------------------------*/
38
39
40 /* create DatInfo explicitly.                    */
41 /* Usually, DatInfo is NOT created
42    if cache does not exist( i.e. ReadNum == 0 ). */ /* public */
43 bool DatManager::createDatInfo( const KURL& url )
44 {
45     if ( getDatInfo( url,
46                      FALSE /* don't check the existence of cache */
47                    ) == NULL ) return FALSE;
48     return TRUE;
49 }
50
51
52 /* get pointer of DatInfo */
53
54 /*    !!!  NOTICE  !!!   */
55 /* It is very dangerous to access to DatInfo directly. */
56 /* Usually, access to it through DatManager.           */ /* public */
57 DatInfo * DatManager::getDatInfoPointer( const KURL& url )
58 {
59     return getDatInfo( url );
60 }
61
62
63 /*------------------------------------------------------------------------*/
64
65
66 /* This function searches instance of DatInfo in m_datInfoList.
67  
68    If cache exists, create DatInfo and return its pointer.
69  
70    If checkCached == TRUE and cache does not exist, return NULL.
71  
72    If checkCached == FALSE and cache does not exist,
73    create DatInfo and return its pointer anyway.
74  
75    see also DatManager::searchDatInfo() and DatManager::createDatInfo() */ /* private */
76
77 DatInfo* DatManager::getDatInfo( const KURL& url, bool checkCached )
78 {
79     /* search */
80     DatInfo * datInfo = searchDatInfo( url );
81     if ( datInfo != NULL ) return datInfo;
82
83     /* create and enroll instance */
84     return enrollDatInfo( url, checkCached );
85 }
86
87
88 /* This function just searches instance of DatInfo specified by datURL
89    without creating instance.  */ /* private */
90 DatInfo* DatManager::searchDatInfo( const KURL& url )
91 {
92     KURL datURL = Kita::getDatURL( url );
93     if ( datURL.isEmpty() ) return NULL; /* This url is not enrolled in BoardManager. */
94     if ( m_datInfoList.count() == 0 ) return NULL;
95
96     int i = 0;
97     DatInfoList::Iterator it;
98     DatInfo* datInfo;
99
100     for ( it = m_datInfoList.begin(); it != m_datInfoList.end(); ++it, i++ ) {
101
102         datInfo = ( *it );
103
104         if ( datURL == datInfo->url() ) {
105
106             /* LRU */
107             if ( i ) {
108                 m_datInfoList.remove( it );
109                 m_datInfoList.prepend( datInfo );
110             }
111
112             return datInfo;
113         }
114     }
115
116     return NULL;
117 }
118
119
120 /* create and enroll the instance of DatInfo and delete old instances.
121    Note that DatInfo::DatInfo() opens cached data and reads it. */  /* private */
122 DatInfo* DatManager::enrollDatInfo( const KURL& url, bool checkCached )
123 {
124     KURL datURL = Kita::getDatURL( url );
125     if ( datURL.isEmpty() ) return NULL; /* This url is not enrolled in BoardManager. */
126
127     /* create DatInfo & read cached data */
128     DatInfo* datInfo = new DatInfo( datURL );
129
130     /* Does cache exist ? */
131     /* If cache does not exist, delete DatInfo here. */
132     if ( checkCached && datInfo->getReadNum() == 0 ) {
133         delete datInfo;
134         return NULL;
135     }
136
137     m_datInfoList.prepend( datInfo );
138
139     /* delete the all old instances ( LRU algorithm )*/
140     if ( m_datInfoList.count() > DMANAGER_MAXQUEUE ) {
141
142         DatInfoList::Iterator it;
143         for ( it = m_datInfoList.at( DMANAGER_MAXQUEUE ); it != m_datInfoList.end(); ++it ) {
144
145             if ( ( *it ) == NULL ) continue;
146             DatInfo* deleteInfo = ( *it );
147         }
148     }
149
150     return datInfo;
151 }
152
153
154 /* public */
155 void DatManager::deleteAllDatInfo()
156 {
157     DatInfoList::Iterator it;
158     for ( it = m_datInfoList.begin(); it != m_datInfoList.end(); ++it ) {
159
160         if ( ( *it ) == NULL ) continue;
161         delete ( *it );
162     }
163 }
164
165
166
167 /*-------------------------------------------------------------*/
168
169
170
171 /* update cache */   /* public */
172 bool DatManager::updateCache( const KURL& url , const QObject* parent )
173 {
174     DatInfo * datInfo = getDatInfo( url );
175     if ( datInfo == NULL ) return FALSE;
176
177     return datInfo->updateCache( parent );
178 }
179
180
181 /* public */
182 int DatManager::getResponseCode( const KURL& url )
183 {
184     DatInfo * datInfo = getDatInfo( url );
185     if ( datInfo == NULL ) return 0;
186
187     return datInfo->getResponseCode();
188 }
189
190 /* public */
191 int DatManager::getServerTime( const KURL& url )
192 {
193     DatInfo * datInfo = getDatInfo( url );
194     if ( datInfo == NULL ) return 0;
195
196     return datInfo->getServerTime();
197 }
198
199
200 /* public */
201 bool DatManager::deleteCache( const KURL& url )
202 {
203     KURL datURL = Kita::getDatURL( url );
204     Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
205     if ( thread == NULL ) return FALSE;
206     if ( thread->readNum() == 0 ) return FALSE;
207
208     /* init DatInfo */
209     DatInfo * datInfo = searchDatInfo( datURL );
210     if ( datInfo ) {
211         if ( !datInfo->deleteCache() ) return FALSE;
212     }
213
214     /* reset readNum & veiwPos */
215     thread->setReadNum( 0 );
216     thread->setViewPos( 0 );
217
218     /* delete cache */
219     QString cachePath = Kita::Cache::getPath( datURL );
220     QString indexPath = Kita::Cache::getIndexPath( datURL );
221     QFile::remove( indexPath );
222     QFile::remove( cachePath );
223
224     /* delete log from "cache" */
225     KitaThreadInfo::removeThreadInfo( datURL.prettyURL() );
226     return TRUE;
227 }
228
229
230 /* public */
231 bool DatManager::isLoadingNow( const KURL& url )
232 {
233     DatInfo * datInfo = searchDatInfo( url );
234     if ( datInfo == NULL ) return FALSE;
235
236     return datInfo->isLoadingNow();
237 }
238
239
240 /* public */
241 void DatManager::stopLoading( const KURL& url )
242 {
243     DatInfo * datInfo = searchDatInfo( url );
244     if ( datInfo == NULL ) return ;
245
246     return datInfo->stopLoading();
247 }
248
249 /*--------------------------------------*/
250 /* string data */
251
252
253 /* public */
254 const QString& DatManager::getDat( const KURL& url, int num )
255 {
256     DatInfo * datInfo = getDatInfo( url );
257     if ( datInfo == NULL ) return QString::null;
258
259     return datInfo->getDat( num );
260 }
261
262
263
264 /* public */
265 const QString& DatManager::getId( const KURL& url, int num )
266 {
267     DatInfo * datInfo = getDatInfo( url );
268     if ( datInfo == NULL ) return QString::null;
269
270     return datInfo->getId( num );
271 }
272
273
274 /* public */
275 QString DatManager::getPlainName( const KURL& url, int num )
276 {
277     DatInfo * datInfo = getDatInfo( url );
278     if ( datInfo == NULL ) return QString::null;
279
280     return datInfo->getPlainName( num );
281 }
282
283
284 /* public */
285 QString DatManager::getPlainBody( const KURL& url, int num )
286 {
287     DatInfo * datInfo = getDatInfo( url );
288     if ( datInfo == NULL ) return QString::null;
289
290     return datInfo->getPlainBody( num );
291 }
292
293
294 /* public */
295 QString DatManager::getPlainTitle( const KURL& url, int num )
296 {
297     DatInfo * datInfo = getDatInfo( url );
298     if ( datInfo == NULL ) return QString::null;
299
300     return datInfo->getPlainTitle( num );
301 }
302
303
304 /* get name (i.e. subject ) of thread from URL of dat file. */ /* public */
305 const QString DatManager::threadName( const KURL& url )
306 {
307     KURL datURL = Kita::getDatURL( url );
308     Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
309     if ( thread != NULL ) return thread->threadName();
310
311     return QString::null;
312 }
313
314
315 /* public */
316 const QString DatManager::threadID( const KURL& url )
317 {
318     KURL datURL = Kita::getDatURL( url );
319     return datURL.filename().section( ".", 0, 0 );
320 }
321
322
323 const QString DatManager::getCachePath( const KURL& url )
324 {
325     return Kita::Cache::getPath( url );
326 }
327
328 const QString DatManager::getCacheIndexPath( const KURL& url )
329 {
330     return Kita::Cache::getIndexPath( url );
331 }
332
333 /*---------------------------------------*/
334 /* HTML data */
335
336 /* public */
337 QString DatManager::getHtml( const KURL& url, int startnum, int endnum, bool checkAbone )
338 {
339     DatInfo * datInfo = getDatInfo( url );
340     if ( datInfo == NULL ) return QString::null;
341
342     return datInfo->getHTMLString( startnum, endnum, checkAbone );
343 }
344
345
346
347 /* public */
348 QString DatManager::getHtmlByID( const KURL& url, const QString& strid, int &count )
349 {
350     DatInfo * datInfo = getDatInfo( url );
351     if ( datInfo == NULL ) return QString::null;
352
353     return datInfo->getHtmlByID( strid, count );
354 }
355
356
357
358 /* Get HTML document of res tree.*/ /* public */
359 QString DatManager::getTreeByRes( const KURL& url, const int rootnum, int &count )
360 {
361     DatInfo * datInfo = getDatInfo( url );
362     if ( datInfo == NULL ) return QString::null;
363
364     return datInfo->getTreeByRes( rootnum, count );
365 }
366
367 /* Get HTML document of reverse res tree.*/ /* public */
368 QString DatManager::getTreeByResReverse( const KURL& url, const int rootnum, int &count )
369 {
370     DatInfo * datInfo = getDatInfo( url );
371     if ( datInfo == NULL ) return QString::null;
372
373     return datInfo->getTreeByResReverse( rootnum, count );
374 }
375
376
377 /* public */
378 int DatManager::getResNum( const KURL& url )
379 {
380     KURL datURL = Kita::getDatURL( url );
381     Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
382     if ( thread != NULL ) return thread->resNum();
383
384     return 0;
385 }
386
387
388 /* public */
389 int DatManager::getReadNum( const KURL& url )
390 {
391     KURL datURL = Kita::getDatURL( url );
392     Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
393     if ( thread != NULL ) return thread->readNum();
394
395     return 0;
396 }
397
398
399 /* public */
400 int DatManager::getViewPos( const KURL& url )
401 {
402     KURL datURL = Kita::getDatURL( url );
403     Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
404     if ( thread != NULL ) return thread->viewPos();
405
406     return 0;
407 }
408
409
410 /* public */
411 void DatManager::setViewPos( const KURL& url , int num )
412 {
413     KURL datURL = Kita::getDatURL( url );
414     Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
415     if ( thread != NULL ) thread->setViewPos( num );
416
417     /* save idx */
418     Kita::ThreadIndex::setViewPos( url, num );
419
420     /* save "cache" */
421     KitaThreadInfo::setReadNum( datURL.prettyURL(), num );
422 }
423
424
425 /* public */
426 int DatManager::getDatSize( const KURL& url )
427 {
428     DatInfo * datInfo = getDatInfo( url );
429     if ( datInfo == NULL ) return 0;
430
431     return datInfo->getDatSize();
432 }
433
434 /* get number of responses which have same ID. */ /* public */
435 int DatManager::getNumByID( const KURL& url, const QString& strid )
436 {
437     DatInfo * datInfo = getDatInfo( url );
438     if ( datInfo == NULL ) return 0;
439
440     return datInfo->getNumByID( strid );
441 }
442
443
444 /* public */
445 bool DatManager::isThreadEnrolled( const KURL& url )
446 {
447     if ( Kita::getDatURL( url ).isEmpty() ) return FALSE;
448
449     return TRUE;
450 }
451
452
453 /* public */
454 bool DatManager::is2chThread( const KURL& url )
455 {
456     if ( BoardManager::type( url ) != Board_2ch ) return FALSE;
457     if ( Kita::getDatURL( url ).isEmpty() ) return FALSE;
458
459     QRegExp url_2ch( ".*\\.2ch\\.net" );
460     QRegExp url_bbspink( ".*\\.bbspink\\.com" );
461
462     if ( url_2ch.search( url.host() ) != -1
463             || url_bbspink.search( url.host() ) != -1 ) return TRUE;
464
465     return FALSE;
466 }
467
468
469 /* public */
470 bool DatManager::isResValid( const KURL& url, int num )
471 {
472     DatInfo * datInfo = getDatInfo( url );
473     if ( datInfo == NULL ) return FALSE;
474
475     return datInfo->isResValid( num );
476 }
477
478
479 /* public */
480 bool DatManager::isBroken( const KURL& url )
481 {
482     DatInfo * datInfo = getDatInfo( url );
483     if ( datInfo == NULL ) return FALSE;
484
485     return datInfo->isBroken();
486 }
487
488 /* public */
489 bool DatManager::isResBroken( const KURL& url, int num )
490 {
491     DatInfo * datInfo = getDatInfo( url );
492     if ( datInfo == NULL ) return FALSE;
493
494     return datInfo->isResBroken( num );
495 }
496
497
498
499 /* check if ID == strid  */ /* public */
500 bool DatManager::checkID( const KURL& url, const QString& strid, int num )
501 {
502     DatInfo * datInfo = getDatInfo( url );
503     if ( datInfo == NULL ) return FALSE;
504
505     return datInfo->checkID( strid, num );
506 }
507
508
509 /* check if keywords are included */ /* public */
510 bool DatManager::checkWord( const KURL& url,
511                             QStringList& strlist, int num,
512                             bool checkOR /* AND or OR search */
513                           )
514 {
515     DatInfo * datInfo = getDatInfo( url );
516     if ( datInfo == NULL ) return FALSE;
517
518     return datInfo->checkWord( strlist, num, checkOR );
519 }
520
521
522 /* public */
523 bool DatManager::isMarked( const KURL& url, int num )
524 {
525     KURL datURL = Kita::getDatURL( url );
526     Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
527     if ( thread == NULL ) return FALSE;
528
529     return thread->isMarked( num );
530 }
531
532
533 /* public */
534 void DatManager::setMark( const KURL& url, int num, bool mark )
535 {
536     KURL datURL = Kita::getDatURL( url );
537     Kita::Thread* thread = Kita::Thread::getByURLNew( datURL );
538     if ( thread == NULL ) return ;
539
540     if ( thread->setMark( num, mark ) ) Kita::ThreadIndex::setMarkList( url, thread->markList() );
541 }
542
543
544 /* public */
545 bool DatManager::checkAbone( const KURL& url, int num )
546 {
547     DatInfo * datInfo = getDatInfo( url );
548     if ( datInfo == NULL ) return FALSE;
549
550     return datInfo->checkAbone( num );
551 }
552
553
554 /* public */
555 void DatManager::resetAbone( const KURL& url )
556 {
557     DatInfo * datInfo = getDatInfo( url );
558     if ( datInfo == NULL ) return ;
559
560     datInfo->resetAbone();
561 }
562
563
564 /* check if the thread is shown on the main thread tab. */ /* public */
565 bool DatManager::isMainThreadOpened( const KURL& url )
566 {
567     DatInfo * datInfo = getDatInfo( url );
568     if ( datInfo == NULL ) return FALSE;
569
570     return datInfo->isOpened();
571 }
572
573 void DatManager::setMainThreadOpened( const KURL& url, bool isOpened )
574 {
575     DatInfo * datInfo = getDatInfo( url );
576     if ( datInfo == NULL ) return;
577
578     datInfo->setIsOpened( isOpened );
579 }
580
581
582 /*--------------------------*/
583 /* obsolete */
584
585 /* public */
586 const QString DatManager::threadURL( const KURL& url )
587 {
588     return Kita::getThreadURL( url );
589 }
590