+/* ThreadList */
+
+
+/* get list of pointers of Thread classes. */
+/*
+ Input:
+
+ url: URL of board.
+ oldLogs: If true, search cache and get list of pointer of old threads.
+ online: online or offline mode.
+
+ Output:
+
+ threadList: list of pointers of Thread classes.
+ oldLogList: list of pointers of old threads.
+
+ */ /* public */ /* static */
+void BoardManager::getThreadList(
+
+ /* input */
+ const KUrl& url,
+ bool oldLogs,
+ bool online,
+
+ /* output */
+ QList<Thread*>& threadList,
+ QList<Thread*>& oldLogList)
+{
+ threadList.clear();
+ oldLogList.clear();
+
+ /* get all obtained threads list from cache */
+ if (url.prettyUrl() == "http://virtual/obtained/") {
+
+ QStringList bbslist = allBoardUrlList();
+
+ /* search all cache dirs */
+ for (QStringList::iterator it = bbslist.begin() ; it != bbslist.end(); ++it) {
+
+ getCachedThreadList((*it), threadList);
+ }
+
+ return ;
+ }
+
+ /*-------------------------*/
+
+ BoardData* bdata = getBoardData(url);
+ if (bdata == 0) return ;
+
+ /* download subject.txt */
+ if (online) {
+
+ /* make directory */
+ Cache cache(url);
+ QString cacheDir = cache.getDirPath();
+ if (!QDir::root().mkpath(cacheDir)) return;
+
+ KIO::SlaveConfig::self() ->setConfigData("http",
+ url.host() ,
+ "UserAgent",
+ QString("Monazilla/1.00 (Kita/%1)").arg(VERSION));
+ QString subjectPath = cache.getSubjectPath();
+ KIO::NetAccess::download(subjectUrl(url), subjectPath, 0);
+ }
+
+ /* open and read subject.txt */
+ readSubjectTxt(bdata, url, threadList);
+
+ /* get old logs */
+ if (oldLogs) {
+
+ QList<Thread*> tmpList;
+ tmpList.clear();
+ getCachedThreadList(url, tmpList);
+
+ for (int i = 0; i < tmpList.count(); i++) {
+ if (threadList.contains(tmpList.at(i)) == 0)
+ oldLogList.append(tmpList.at(i));
+ }
+ }
+}
+
+
+/* read the cache dir & get the list of all threads. */ /* private */ /* static */
+void BoardManager::getCachedThreadList(const KUrl& url, QList<Thread*>& threadList)
+{
+ Cache cache(url);
+ QString cacheDir = cache.getDirPath();
+ QDir d(cacheDir);
+ if (d.exists()) {
+
+ /* get all file names */
+ QString ext = BoardManager::getBoardData(url) ->ext();
+ QString boardUrl = BoardManager::getBoardData(url) ->basePath();
+ QStringList filter('*' + ext);
+ QStringList flist = d.entryList(filter);
+
+ for (QStringList::iterator it = flist.begin(); it != flist.end(); ++it) {
+ if ((*it).isEmpty()) continue;
+
+ QString datUrl = boardUrl + "dat/" + (*it);
+
+ /* read idx file */
+ Thread* thread = Thread::getByUrlNew(datUrl);
+ if (thread == 0) {
+
+ thread = Thread::getByUrl(datUrl);
+ if (thread == 0)
+ continue;
+ ThreadIndex threadIndex(datUrl);
+ threadIndex.loadIndex(thread, false);
+ }
+
+ if (thread != 0) threadList.append(thread);
+ }
+ }
+}
+
+
+
+/* open subject.txt and get list of Thread classes */ /* private */ /* static */
+bool BoardManager::readSubjectTxt(BoardData* bdata, const KUrl& url, QList<Thread*>& threadList)
+{
+ /* get all names of cached files to read idx. */
+ QStringList cacheList;
+ if (!bdata->readIdx()) {
+ Cache cache(url);
+ QString cacheDir = cache.getDirPath();
+ QDir d(cacheDir);
+ if (d.exists()) {
+ QString ext = BoardManager::getBoardData(url) ->ext();
+ QStringList filter('*' + ext);
+ cacheList = d.entryList(filter);
+ }
+ }
+
+ /* open subject.txt */
+ Cache cache(url);
+ QString subjectPath = cache.getSubjectPath();
+ QIODevice * device = KFilterDev::deviceForFile(subjectPath, "application/x-gzip");
+ if (!device->open(QIODevice::ReadOnly)) return false;
+
+ QTextStream stream(device);
+
+ if (BoardManager::type(url) == Board_JBBS) {
+ if (!m_eucJpCodec) m_eucJpCodec = QTextCodec::codecForName("eucJP");
+ stream.setCodec(m_eucJpCodec);
+ } else {
+ if (!m_cp932Codec) m_cp932Codec = QTextCodec::codecForName("Shift-JIS");
+ stream.setCodec(m_cp932Codec);
+ }
+
+ QRegExp regexp;
+ switch (BoardManager::type(url)) {
+
+ case Board_MachiBBS:
+ case Board_JBBS:
+ regexp.setPattern("(\\d+\\.cgi),(.*)\\((\\d+)\\)");
+ break;
+
+ default:
+ regexp.setPattern("(\\d+\\.dat)<>(.*)\\((\\d+)\\)");
+ break;
+ }
+ QString line;
+
+ while (!(line = stream.readLine()).isEmpty()) {
+ int pos = regexp.indexIn(line);
+ if (pos != -1) {
+ QString fname = regexp.cap(1);
+ QString subject = regexp.cap(2);
+ QString num = regexp.cap(3);
+
+ /* get pointer of Thread class */
+ QString datUrl = boardUrl(url) + "dat/" + fname;
+ Thread* thread = Thread::getByUrl(datUrl);
+ ThreadIndex threadIndex(datUrl);
+ if (threadList.indexOf(thread) == -1) {
+ threadList.append(thread);
+ }
+
+ /* set thread name */
+ thread->setThreadName(subject);
+
+ /* load index file */
+ if (!bdata->readIdx()) {
+
+ if (cacheList.contains(fname)) {
+ threadIndex.loadIndex(thread, false);
+ }
+ }
+
+ /* update res num */
+ int newNum = num.toInt();
+ if (thread->readNum()) { /* cache exists */
+ int oldNum = thread->resNum();
+
+ if (newNum > oldNum) {
+ threadIndex.setResNum(newNum);
+ }
+ }
+ thread->setResNum(newNum);
+ }
+ }
+
+ device->close();
+ bdata->setReadIdx(true); /* never read idx files again */
+
+ return true;
+}
+
+/*---------------------------*/