OSDN Git Service

1592d29fc2c4ee4289b4ca81330c81108cb059f0
[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 "datmanager.h"
16 #include "datinfo.h"
17 #include "kita-utf8.h"
18 #include "kita-utf16.h"
19
20 using namespace Kita;
21
22 #define DMANAGER_MAXQUEUE 16
23
24 /*-------------------------------------------------*/
25 /* DatManager manages all information about *.dat. */
26
27 DatInfoList DatManager::m_datInfo;
28 QMutex DatManager::m_mutex;
29
30 /* This function searches instance of DatInfo specified by url.
31    If instance does not exist, create instance.  */  /* private */
32 DatInfo* DatManager::getDatInfo( const KURL& url )
33 {
34     if ( url.isEmpty() ) {
35         return NULL;
36     }
37
38     int i = 0;
39     DatInfoList::Iterator it;
40     DatInfo* datInfo;
41     
42     KURL inurl = url.protocol() + "://" + url.host() + url.path();
43     
44     /* search */
45     if ( m_datInfo.count() ) {
46         for ( it = m_datInfo.begin(); it != m_datInfo.end(); ++it, i++ ) {
47
48             datInfo = ( *it );
49
50             if ( inurl == datInfo->url() ){
51
52                 /* LRU */
53                 if ( i ) {
54                     m_datInfo.remove( it );
55                     m_datInfo.prepend( datInfo );
56                 }
57
58                 return datInfo;
59             }
60         }
61     }
62
63     /* not found */
64
65     /*create new DatInfo and insert it into list. */
66     KURL daturl = url.protocol() + "://" + url.host() + url.path();
67
68     datInfo = new DatInfo( daturl );
69     if ( datInfo->getRawDat() == QString::null ) { /* cache does not exist */
70         delete( datInfo );
71
72         return NULL;
73     }
74
75     m_datInfo.prepend( datInfo );
76
77     /* delete the last items of list */
78     if ( m_datInfo.count() > DMANAGER_MAXQUEUE ) {
79
80         /* collect unlocked datInfos */
81         typedef QValueList<KURL> DELETELIST;
82         DELETELIST deleteList;
83
84         for ( it = m_datInfo.at(DMANAGER_MAXQUEUE); it != m_datInfo.end(); ++it ) 
85             if(! (*it)->isLocked() ) deleteList += ( *it ) ->url();
86
87         /* delete unlocked datInfos */
88         for (DELETELIST::Iterator itdel = deleteList.begin(); itdel != deleteList.end(); ++itdel )
89             deleteDatInfoPrivate( (*itdel) );
90     }
91
92     return datInfo;
93 }
94
95
96 /* get pointer of DatInfo */
97
98 /*    !!!  NOTICE  !!!   */
99 /* It is very dangerous to access to DatInfo directly. */
100 /* Usually, access to it through DatManager.           */ /* public */
101 DatInfo * DatManager::getDatInfoPointer( const KURL& url )
102 {
103     QMutexLocker locker( &m_mutex );
104     
105     return getDatInfo( url );
106 }
107
108
109
110 /* public */
111 void DatManager::deleteDatInfo( const KURL& url )
112 {
113     QMutexLocker locker( &m_mutex );
114
115     deleteDatInfoPrivate(url);
116 }
117
118
119 /* private */
120 void DatManager::deleteDatInfoPrivate( const KURL& url ){
121
122     DatInfoList::Iterator it;
123
124     for ( it = m_datInfo.begin(); it != m_datInfo.end(); ++it ) {
125         if ( url == ( *it )->url() ){
126
127             if(! (*it)->isLocked() ){
128                 m_datInfo.remove( it );
129                 delete( *it );
130             }
131
132             return ;
133         }
134     }
135
136 }
137
138
139
140
141 /*-------------------------------------------------------------*/
142
143
144
145 /*--------------------------------------*/
146 /* update cache, then return new lines  */
147
148 /* If cache does not exist, then create
149    the new instance.                    */ /* public */
150 bool DatManager::updateCache( const KURL& url , const QObject* parent )
151 {
152     QMutexLocker locker( &m_mutex );
153     
154     DatInfo * datInfo = getDatInfo( url );
155
156     /* create new DatInfo */
157     if ( datInfo == NULL ){
158         datInfo = new DatInfo( url );
159         m_datInfo.prepend( datInfo );
160     }
161     
162     return datInfo->updateCache(parent);
163 }
164
165
166 /* public */
167 int DatManager::getResponseCode( const KURL& url )
168 {
169     QMutexLocker locker( &m_mutex );
170
171     DatInfo * datInfo = getDatInfo( url );
172     if ( datInfo == NULL ) return 0;
173
174     return datInfo->getResponseCode();
175 }
176
177 /* public */
178 int DatManager::getServerTime( const KURL& url )
179 {
180     QMutexLocker locker( &m_mutex );
181
182     DatInfo * datInfo = getDatInfo( url );
183     if ( datInfo == NULL ) return 0;
184
185     return datInfo->getServerTime();
186 }
187
188
189 /* public */
190 bool DatManager::deleteCache( const KURL& url, QWidget* parent )
191 {
192     QMutexLocker locker( &m_mutex );
193
194     DatInfo * datInfo = getDatInfo( url );
195     if ( datInfo == NULL ) return FALSE;
196
197     return datInfo->deleteCache(parent);
198 }
199
200
201 /* public */
202 bool DatManager::isLoadingNow( const KURL& url )
203 {
204     QMutexLocker locker( &m_mutex );
205
206     DatInfo * datInfo = getDatInfo( url );
207     if ( datInfo == NULL ) return FALSE;
208
209     return datInfo->isLoadingNow();
210 }
211
212
213 /* public */
214 void DatManager::stopLoading( const KURL& url )
215 {
216
217     /* Don't use QMutexLocker here !!
218        It will cause deadlock , because
219        Kita::Access::stopJob() calls KitaThreadView::slotFinishLoad() back.  */
220     m_mutex.lock();
221     DatInfo * datInfo = getDatInfo( url );
222     m_mutex.unlock();
223     if ( datInfo == NULL ) return;
224
225     return datInfo->stopLoading();
226 }
227
228
229
230 /*----------------------*/
231 /* lock, unlock DatInfo */
232
233 /* If cache does not exist, then create
234    the new instance and lock it. 
235
236    Don't forget to unlock it. */  /* public */
237
238 void DatManager::lock( const KURL& url )
239 {
240     QMutexLocker locker( &m_mutex );
241     
242     DatInfo * datInfo = getDatInfo( url );
243
244     /* create new DatInfo */
245     if ( datInfo == NULL ){
246         datInfo = new DatInfo( url );
247         m_datInfo.prepend( datInfo );
248     }
249     
250     datInfo->lock();
251 }
252
253 void DatManager::unlock( const KURL& url )
254 {
255     QMutexLocker locker( &m_mutex );
256     
257     DatInfo * datInfo = getDatInfo( url );
258     if ( datInfo == NULL ) return;
259     
260     datInfo->unlock();
261 }
262
263
264
265 /* public */
266 const QString& DatManager::getSubject( const KURL& url )
267 {
268     QMutexLocker locker( &m_mutex );
269
270     DatInfo * datInfo = getDatInfo( url );
271     if ( datInfo == NULL ) return QString::null;
272
273     return datInfo->getSubject();
274 }
275
276
277 /* public */
278 const QString& DatManager::getRawDat( const KURL& url )
279 {
280     QMutexLocker locker( &m_mutex );
281     
282     DatInfo * datInfo = getDatInfo( url );
283     if ( datInfo == NULL ) return QString::null;
284
285     return datInfo->getRawDat();
286 }
287
288
289 /* public */
290 const QString& DatManager::getDat( const KURL& url, int num )
291 {
292     QMutexLocker locker( &m_mutex );
293     
294     DatInfo * datInfo = getDatInfo( url );
295     if ( datInfo == NULL ) return QString::null;
296
297     return datInfo->getDat( num );
298 }
299
300
301
302 /* public */
303 QString DatManager::getPlainBody( const KURL& url, int num )
304 {
305     QMutexLocker locker( &m_mutex );
306     
307     DatInfo * datInfo = getDatInfo( url );
308     if ( datInfo == NULL ) return QString::null;
309
310     return datInfo->getPlainBody(num);
311 }
312
313
314
315 /* public */
316 QString DatManager::getHtml( const KURL& url, int startnum, int endnum )
317 {
318     QMutexLocker locker( &m_mutex );
319     
320     DatInfo * datInfo = getDatInfo( url );
321     if ( datInfo == NULL ) return QString::null;
322
323     return datInfo->getHtml(startnum,endnum);
324 }
325
326
327
328 /* public */
329 QString DatManager::getHtmlByID(const KURL& url, const QString& strid, int &count )
330 {
331     QMutexLocker locker( &m_mutex );
332     
333     DatInfo* datInfo = getDatInfo(url);
334     if(datInfo == NULL) return QString::null;
335
336     return datInfo->getHtmlByID(strid,count);
337 }
338
339
340
341 /* Get HTML document of res tree.*/ /* public */
342 QString DatManager::getTreeByRes(const KURL& url, const int rootnum, int &count)
343 {
344     QMutexLocker locker( &m_mutex );
345     
346     DatInfo* datInfo = getDatInfo(url);
347     if(datInfo == NULL) return QString::null;
348
349     return datInfo->getTreeByRes(rootnum,count);
350 }
351
352
353
354 /* public */
355 int DatManager::getMaxResNumber( const KURL& url )
356 {
357     QMutexLocker locker( &m_mutex );
358     
359     DatInfo * datInfo = getDatInfo( url );
360     if ( datInfo == NULL ) return 0;
361
362     return datInfo->getMaxResNumber();
363 }
364
365
366
367 /* public */
368 int DatManager::getNumByID( const KURL& url, const QString& strid )
369 {
370     QMutexLocker locker( &m_mutex );
371     
372     DatInfo * datInfo = getDatInfo( url );
373     if ( datInfo == NULL ) return 0;
374
375     return datInfo->getNumByID(strid);
376 }
377
378
379
380 /* public */
381 int DatManager::getKokoyonNum( const KURL& url )
382 {
383     QMutexLocker locker( &m_mutex );
384     
385     DatInfo * datInfo = getDatInfo( url );
386     if ( datInfo == NULL ) return 0;
387
388     return datInfo->getKokoyonNum();
389 }
390
391
392 /* public */
393 void DatManager::setKokoyonNum( const KURL& url , int num )
394 {
395     QMutexLocker locker( &m_mutex );
396     
397     DatInfo * datInfo = getDatInfo( url );
398     if ( datInfo == NULL ) return;
399
400     return datInfo->setKokoyonNum(num);
401 }
402
403
404 /* public */
405 int DatManager::getDatSize( const KURL& url )
406 {
407     QMutexLocker locker( &m_mutex );
408     
409     DatInfo * datInfo = getDatInfo( url );
410     if ( datInfo == NULL ) return 0;
411
412     return datInfo->getDatSize();
413 }
414
415
416
417
418 /* public */
419 bool DatManager::isResValid( const KURL& url,int num )
420 {
421     QMutexLocker locker( &m_mutex );
422     
423     DatInfo * datInfo = getDatInfo( url );
424     if ( datInfo == NULL ) return FALSE;
425
426     return datInfo->isResValid(num);
427 }
428
429
430 /* public */
431 bool DatManager::isBroken( const KURL& url )
432 {
433     QMutexLocker locker( &m_mutex );
434     
435     DatInfo * datInfo = getDatInfo( url );
436     if ( datInfo == NULL ) return FALSE;
437
438     return datInfo->isBroken();
439 }
440
441 /* public */
442 bool DatManager::isResBroken( const KURL& url,int num )
443 {
444     QMutexLocker locker( &m_mutex );
445     
446     DatInfo * datInfo = getDatInfo( url );
447     if ( datInfo == NULL ) return FALSE;
448
449     return datInfo->isResBroken(num);
450 }
451
452
453
454 /* public */
455 bool DatManager::checkID(const KURL& url, const QString& strid, int num )
456 {
457     QMutexLocker locker( &m_mutex );
458     
459     DatInfo* datInfo = getDatInfo(url);
460     if(datInfo == NULL) return FALSE;
461     
462     return datInfo->checkID(strid,num);
463 }
464
465
466 /* check keywords */ /* public */
467 bool DatManager::checkWord(const KURL& url,
468                             QStringList& strlist, int num,
469                             bool checkOR /* AND or OR search */
470     )
471 {
472     QMutexLocker locker( &m_mutex );
473     
474     DatInfo* datInfo = getDatInfo(url);
475     if(datInfo == NULL) return FALSE;
476
477     return datInfo->checkWord(strlist,num,checkOR);
478 }
479
480
481 /* public */
482 bool DatManager::checkAbone(const KURL& url, int num)
483 {
484     QMutexLocker locker( &m_mutex );
485     
486     DatInfo* datInfo = getDatInfo(url);
487     if(datInfo == NULL) return FALSE;
488
489     return datInfo->checkAbone(num);
490 }
491
492
493
494 /*-----------------------------------*/
495 /* Wrapper functions for Thread class*/
496
497 /* They ara public */
498
499 const QString DatManager::thread_datID( const KURL& url )
500 {
501     QMutexLocker locker( &m_mutex );
502     
503     DatInfo* datInfo = getDatInfo(url);
504     if(datInfo == NULL) return QString::null;
505
506     return datInfo->thread_datID();
507 }
508
509 const QString& DatManager::thread_name( const KURL& url )
510 {
511     QMutexLocker locker( &m_mutex );
512     
513     DatInfo* datInfo = getDatInfo(url);
514     if(datInfo == NULL) return QString::null;
515
516     return datInfo->thread_name();
517 }
518
519 const QString& DatManager::thread_boardName( const KURL& url )
520 {
521     QMutexLocker locker( &m_mutex );
522     
523     DatInfo* datInfo = getDatInfo(url);
524     if(datInfo == NULL) return QString::null;
525
526     return datInfo->thread_boardName();
527 }
528
529 const QString DatManager::thread_url( const KURL& url )
530 {
531     QMutexLocker locker( &m_mutex );
532     
533     DatInfo* datInfo = getDatInfo(url);
534     if(datInfo == NULL) return QString::null;
535
536     return datInfo->thread_url();
537 }
538
539 const QString DatManager::thread_boardURL( const KURL& url )
540 {
541     QMutexLocker locker( &m_mutex );
542     
543     DatInfo* datInfo = getDatInfo(url);
544     if(datInfo == NULL) return QString::null;
545
546     return datInfo->thread_boardURL();
547 }
548
549 const QString DatManager::thread_boardID( const KURL& url )
550 {
551     QMutexLocker locker( &m_mutex );
552     
553     DatInfo* datInfo = getDatInfo(url);
554     if(datInfo == NULL) return QString::null;
555
556     return datInfo->thread_boardID();
557 }
558
559
560
561