OSDN Git Service

Use `record' instead of `enroll'
[kita/kita.git] / 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 #include "datmanager.h"
13
14 #include <QtCore/QFile>
15 #include <QtCore/QObject>
16 #include <QtCore/QRegExp>
17 #include <QtCore/QStringList>
18
19 #include "boarddatabase.h"
20 #include "cache.h"
21 #include "datinfo.h"
22 #include "kita_misc.h"
23 #include "thread.h"
24 #include "threadindex.h"
25 #include "threadinfo.h"
26
27 using namespace Kita;
28
29 static const int DMANAGER_MAXQUEUE = 16;
30
31 DatInfoList DatManager::m_datInfoList;
32
33
34 /*-----------------------------------------------------------------------*/
35 DatManager::DatManager(const KUrl& url) : m_url(url)
36 {
37     m_datUrl = getDatUrl(url);
38     m_searchDatInfo = searchDatInfo();
39     m_datInfo = getDatInfo();
40 }
41
42
43 /* create DatInfo explicitly.                    */
44 /* Usually, DatInfo is NOT created
45    if cache does not exist(i.e. ReadNum == 0). */ /* public */
46 bool DatManager::createDatInfo() const
47 {
48     return getDatInfo(false /* don't check the existence of cache */) != 0;
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
58 {
59     return m_datInfo;
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 0.
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(bool checkCached) const
78 {
79     /* search */
80     /* create and record instance */
81     return (m_searchDatInfo != 0)
82         ? m_searchDatInfo : recordDatInfo(checkCached);
83 }
84
85
86 /* This function just searches instance of DatInfo specified by datURL
87    without creating instance.  */ /* private */
88 DatInfo* DatManager::searchDatInfo() const
89 {
90     if (m_datUrl.isEmpty())
91         return 0; /* This url is not recorded in BoardManager. */
92     if (m_datInfoList.isEmpty())
93         return 0;
94
95     int i = 0;
96     DatInfoList::Iterator it;
97     DatInfo* datInfo;
98
99     for (it = m_datInfoList.begin(); it != m_datInfoList.end(); ++it, i++) {
100
101         datInfo = (*it);
102
103         if (m_datUrl == datInfo->url()) {
104
105             /* LRU */
106             if (i) {
107                 m_datInfoList.erase(it);
108                 m_datInfoList.prepend(datInfo);
109             }
110
111             return datInfo;
112         }
113     }
114
115     return 0;
116 }
117
118
119 /* create and record the instance of DatInfo and delete old instances.
120    Note that DatInfo::DatInfo() opens cached data and reads it. */
121 /* private */
122 DatInfo* DatManager::recordDatInfo(bool checkCached) const
123 {
124     if (m_datUrl.isEmpty())
125         return 0; /* This url is not recorded in BoardManager. */
126
127     /* create DatInfo & read cached data */
128     DatInfo* datInfo = new DatInfo(m_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 0;
135     }
136
137     m_datInfoList.prepend(datInfo);
138
139     /* delete the all old instances (LRU algorithm)*/
140     if (m_datInfoList.count() > DMANAGER_MAXQUEUE) {
141         for (int i = DMANAGER_MAXQUEUE; i < m_datInfoList.count(); i++) {
142             DatInfo* deleteInfo = m_datInfoList.at(i);
143             if (deleteInfo == 0)
144                 continue;
145             m_datInfoList.removeAt(i);
146             i--;
147             delete datInfo;
148         }
149     }
150
151     return datInfo;
152 }
153
154
155 /* public */
156 void DatManager::deleteAllDatInfo()
157 {
158     while (!m_datInfoList.isEmpty())
159         delete m_datInfoList.takeFirst();
160 }
161
162
163
164 /*-------------------------------------------------------------*/
165
166
167
168 /* update cache */   /* public */
169 bool DatManager::updateCache(const QObject* parent) const
170 {
171     return (m_datInfo == 0) ? false : m_datInfo->updateCache(parent);
172 }
173
174
175 /* public */
176 int DatManager::getResponseCode() const
177 {
178     return (m_datInfo == 0) ? 0 : m_datInfo->getResponseCode();
179 }
180
181 /* public */
182 int DatManager::getServerTime() const
183 {
184     return (m_datInfo == 0) ? 0 : m_datInfo->getServerTime();
185 }
186
187
188 /* public */
189 bool DatManager::deleteCache() const
190 {
191     Thread* thread = Thread::getByUrlNew(m_datUrl);
192     if (thread == 0)
193         return false;
194     if (thread->readNum() == 0)
195         return false;
196
197     /* init DatInfo */
198     if (m_searchDatInfo && !m_searchDatInfo->deleteCache()) {
199         return false;
200     }
201
202     /* reset readNum & veiwPos */
203     thread->setReadNum(0);
204     thread->setViewPos(0);
205
206     /* delete cache */
207     Cache cache(m_datUrl);
208     QString cachePath = cache.getPath();
209     QString indexPath = cache.getIndexPath();
210     QFile::remove(indexPath);
211     QFile::remove(cachePath);
212
213     /* delete log from "cache" */
214     ThreadInfo::removeThreadInfo(m_datUrl.prettyUrl());
215     return true;
216 }
217
218
219 /* public */
220 bool DatManager::isLoadingNow() const
221 {
222     return (m_searchDatInfo == 0) ? false : m_searchDatInfo->isLoadingNow();
223 }
224
225
226 /* public */
227 void DatManager::stopLoading() const
228 {
229     if (m_searchDatInfo == 0)
230         return;
231     m_searchDatInfo->stopLoading();
232 }
233
234 /*--------------------------------------*/
235 /* string data */
236
237
238 /* public */
239 QString DatManager::getDat(int num) const
240 {
241     return (m_datInfo == 0) ? QString() : m_datInfo->getDat(num);
242 }
243
244
245
246 /* public */
247 QString DatManager::getId(int num) const
248 {
249     return (m_datInfo == 0) ? QString() : m_datInfo->getId(num);
250 }
251
252
253 /* public */
254 QString DatManager::getPlainName(int num) const
255 {
256     return (m_datInfo == 0) ? QString() : m_datInfo->getPlainName(num);
257 }
258
259
260 /* public */
261 QString DatManager::getPlainBody(int num) const
262 {
263     return (m_datInfo == 0) ? QString() : m_datInfo->getPlainBody(num);
264 }
265
266
267 /* public */
268 QString DatManager::getPlainTitle(int num) const
269 {
270     return (m_datInfo == 0) ? QString() : m_datInfo->getPlainTitle(num);
271 }
272
273
274 /* get name (i.e. subject) of thread from URL of dat file. */ /* public */
275 QString DatManager::threadName() const
276 {
277     Thread* thread = Thread::getByUrlNew(m_datUrl);
278     return (thread != 0) ? thread->threadName() : QString();
279 }
280
281
282 /* public */
283 QString DatManager::threadId() const
284 {
285     return m_datUrl.fileName().section('.', 0, 0);
286 }
287
288 /*---------------------------------------*/
289 /* HTML data */
290
291 /* public */
292 QString DatManager::getHtml(int startnum, int endnum, bool checkAbone) const
293 {
294     return (m_datInfo == 0)
295         ? QString() : m_datInfo->getHTMLString(startnum, endnum, checkAbone);
296 }
297
298
299
300 /* public */
301 QString DatManager::getHtmlById(const QString& strid, int &count) const
302 {
303     return (m_datInfo == 0) ? QString() : m_datInfo->getHtmlById(strid, count);
304 }
305
306
307
308 /* Get HTML document of res tree.*/ /* public */
309 QString DatManager::getTreeByRes(int rootnum, int &count) const
310 {
311     return (m_datInfo == 0)
312         ? QString() : m_datInfo->getTreeByRes(rootnum, count);
313 }
314
315 /* Get HTML document of reverse res tree.*/ /* public */
316 QString DatManager::getTreeByResReverse(int rootnum, int &count) const
317 {
318     return (m_datInfo == 0)
319         ? QString() : m_datInfo->getTreeByResReverse(rootnum, count);
320 }
321
322
323 /* public */
324 int DatManager::getResNum() const
325 {
326     Thread* thread = Thread::getByUrlNew(m_datUrl);
327     return (thread != 0) ? thread->resNum() : 0;
328 }
329
330
331 /* public */
332 int DatManager::getReadNum() const
333 {
334     Thread* thread = Thread::getByUrlNew(m_datUrl);
335     return (thread != 0) ? thread->readNum() : 0;
336 }
337
338
339 /* public */
340 int DatManager::getViewPos() const
341 {
342     Thread* thread = Thread::getByUrlNew(m_datUrl);
343     return (thread != 0) ? thread->viewPos() : 0;
344 }
345
346
347 /* public */
348 void DatManager::setViewPos(int num) const
349 {
350     Thread* thread = Thread::getByUrlNew(m_datUrl);
351     if (thread != 0)
352         thread->setViewPos(num);
353
354     /* save idx */
355     ThreadIndex threadIndex(m_url);
356     threadIndex.setViewPos(num);
357
358     /* save "cache" */
359     ThreadInfo::setReadNum(m_datUrl.prettyUrl(), num);
360 }
361
362
363 /* public */
364 int DatManager::getDatSize() const
365 {
366     return (m_datInfo == 0) ? 0 : m_datInfo->getDatSize();
367 }
368
369 /* get number of responses which have same ID. */ /* public */
370 int DatManager::getNumById(const QString& strid) const
371 {
372     return (m_datInfo == 0) ? 0 : m_datInfo->getNumById(strid);
373 }
374
375
376 /* public */
377 bool DatManager::isThreadRecorded() const
378 {
379     return !m_datUrl.isEmpty();
380 }
381
382
383 /* public */
384 bool DatManager::is2chThread() const
385 {
386     BoardDatabase db(m_url);
387     if (db.type() != Board_2ch)
388         return false;
389     if (m_datUrl.isEmpty())
390         return false;
391
392     QRegExp url_2ch(".*\\.2ch\\.net");
393     QRegExp url_bbspink(".*\\.bbspink\\.com");
394
395     if (url_2ch.indexIn(m_url.host()) != -1
396             || url_bbspink.indexIn(m_url.host()) != -1)
397         return true;
398
399     return false;
400 }
401
402
403 /* public */
404 bool DatManager::isResValid(int num) const
405 {
406     return (m_datInfo == 0) ? false : m_datInfo->isResValid(num);
407 }
408
409
410 /* public */
411 bool DatManager::isBroken() const
412 {
413     return (m_datInfo == 0) ? false : m_datInfo->isBroken();
414 }
415
416 /* public */
417 bool DatManager::isResBroken(int num) const
418 {
419     return (m_datInfo == 0) ? false : m_datInfo->isResBroken(num);
420 }
421
422
423
424 /* check if ID == strid  */ /* public */
425 bool DatManager::checkId(const QString& strid, int num) const
426 {
427     return (m_datInfo == 0) ? false : m_datInfo->checkId(strid, num);
428 }
429
430
431 /* check if keywords are included */ /* public */
432 bool DatManager::checkWord(const QStringList& strlist, int num,
433         bool checkOr /* AND or OR search */) const
434 {
435     return (m_datInfo == 0)
436         ? false : m_datInfo->checkWord(strlist, num, checkOr);
437 }
438
439
440 /* public */
441 bool DatManager::isMarked(int num) const
442 {
443     Thread* thread = Thread::getByUrlNew(m_datUrl);
444     return (thread == 0) ? false : thread->isMarked(num);
445 }
446
447
448 /* public */
449 void DatManager::setMark(int num, bool mark) const
450 {
451     Thread* thread = Thread::getByUrlNew(m_datUrl);
452     if (thread == 0)
453         return;
454
455     if (thread->setMark(num, mark)) {
456         ThreadIndex threadIndex(m_url);
457         threadIndex.setMarkList(thread->markList());
458     }
459 }
460
461
462 /* public */
463 bool DatManager::checkAbone(int num) const
464 {
465     return (m_datInfo == 0) ? false : m_datInfo->checkAbone(num);
466 }
467
468
469 /* public */
470 void DatManager::resetAbone() const
471 {
472     if (m_datInfo == 0)
473         return;
474     m_datInfo->resetAbone();
475 }
476
477
478 /* check if the thread is shown on the main thread tab. */ /* public */
479 bool DatManager::isMainThreadOpened() const
480 {
481     return (m_datInfo == 0) ? false : m_datInfo->isOpened();
482 }
483
484 void DatManager::setMainThreadOpened(bool isOpened) const
485 {
486     if (m_datInfo == 0)
487         return;
488     m_datInfo->setOpened(isOpened);
489 }
490
491
492 /*--------------------------*/
493 /* obsolete */
494
495 /* public */
496 QString DatManager::threadUrl() const
497 {
498     return getThreadUrl(m_url);
499 }