OSDN Git Service

private constructor
[kita/kita.git] / kita / src / libkita / board.cpp
1 /***************************************************************************
2  *   Copyright (C) 2003 by Hideki Ikemoto                                  *
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 #include "board.h"
12
13 #include <config.h>
14
15 #include <kio/slaveconfig.h>
16 #include <kio/netaccess.h>
17 #include <kdebug.h>
18
19 #include <kfilterdev.h>
20
21 #include <qregexp.h>
22 #include <qdict.h>
23
24 #include "kita_misc.h"
25 #include "threadinfo.h"
26
27 #include "qcp932codec.h"
28
29 using namespace Kita;
30
31 QDict<Board>* Board::m_boardDict = 0;
32
33 Board::Board()
34 {
35 }
36
37 Board::Board(const QString& boardURL, const QString boardName) : m_boardURL(boardURL), m_boardName(boardName)
38 {
39 }
40
41 Board::~Board()
42 {
43 }
44
45 const KURL Board::subjectTxtURL() const
46 {
47   if( m_boardURL.isEmpty() ) {
48     return KURL();
49   } else {
50     return KURL(m_boardURL, "./subject.txt");
51   }
52 }
53
54 const QString& Board::url() const
55 {
56   return m_boardURL;
57 }
58
59 const QString& Board::name() const
60 {
61   return m_boardName;
62 }
63
64 const QString Board::id() const
65 {
66   return KURL( m_boardURL ).fileName();
67 }
68
69 const QPtrList<Thread> Board::getThreadList() const
70 {
71   if( m_boardURL.isEmpty() ) {
72     kdWarning() << "Waring: board url is empty!" << endl;
73     return QPtrList<Thread>();
74   }
75   QPtrList<Thread> threadList;
76
77   KIO::SlaveConfig::self()->setConfigData("http",
78     KURL( m_boardURL ).host() ,
79     "UserAgent",
80     QString( "Monazilla/1.00 (Kita/%1)" ).arg( VERSION ) );
81   QString tmpFile;
82   if(KIO::NetAccess::download(subjectTxtURL(), tmpFile)) {
83     QIODevice* tmpDevice = KFilterDev::deviceForFile(tmpFile, "application/x-gzip");
84     tmpDevice->open(IO_ReadOnly);
85
86     QCp932Codec cp932Codec;
87     QTextStream stream(tmpDevice);
88     stream.setCodec(&cp932Codec);
89
90     // parse subject.txt(only one format...)
91     QRegExp regexp("(\\d+\\.dat)<>(.*)\\((\\d+)\\)");
92     QString line;
93
94     while((line = stream.readLine()) != QString::null) {
95       int pos = regexp.search(line);
96       if(pos != -1) {
97         QString fname = regexp.cap(1);
98         QString subject = regexp.cap(2);
99         QString num = regexp.cap(3);
100
101         KURL datURL = m_boardURL;
102         datURL.addPath("/dat/" + fname);
103         Kita::Board::setName( datToBoard( datURL.url() ), name() );
104         Kita::Thread::setName( datURL.url(), subject );
105         Kita::Thread* thread = Kita::Thread::getByURL( datURL.url() );
106         KitaThreadInfo::setResNum( datURL.url(), num.toInt() );
107         threadList.append(thread);
108       }
109     }
110
111     KIO::NetAccess::removeTempFile(tmpFile);
112   }
113   return threadList;
114 }
115
116 const QString Board::toXmlFragment() const
117 {
118   QString ret;
119
120   // FIXME: Thread¥¯¥é¥¹¤Ø¥³¥Ô¡¼
121   ret += "<board xmlns=\"http://kita.sourceforge.jp/ns/board\">\n";
122   ret += QString("<url>%1</url>\n").arg( m_boardURL );
123   ret += QString("<name>%1</name>\n").arg( m_boardName );
124   ret += "</board>\n";
125
126   return ret;
127 }
128
129 Board* Board::getByURL( const QString& boardURL )
130 {
131     // FIXME: null¥ª¥Ö¥¸¥§¥¯¥È¤òÍ×µá¤Î¤¿¤Ó¤Ëºî¤Ã¤Æ¤¤¤ë¡£
132     if ( boardURL.isNull() ) {
133         return new NullBoard();
134     }
135
136     if ( m_boardDict == 0 ) {
137         // FIXME: setAutoDelete¤ò²Ã¤¨¤ÆÆ°¤¯¤³¤È¤ò³Î¤«¤á¤ë¤³¤È
138         m_boardDict = new QDict<Board>();
139     }
140
141     if( m_boardDict->find( boardURL ) ) {
142         return m_boardDict->find( boardURL );
143     }
144
145     Board* newBoard = new Board( boardURL );
146     m_boardDict->insert( boardURL, newBoard );
147
148     return newBoard;
149 }
150
151 void Board::setName( const QString& boardURL, const QString& boardName )
152 {
153     if ( boardURL.isNull() ) {
154         return;
155     }
156
157     if ( m_boardDict == 0 ) {
158         // FIXME: setAutoDelete¤ò²Ã¤¨¤ÆÆ°¤¯¤³¤È¤ò³Î¤«¤á¤ë¤³¤È
159         m_boardDict = new QDict<Board>();
160     }
161
162     Board* newBoard = new Board( boardURL, boardName );
163     m_boardDict->replace( boardURL, newBoard );
164
165     return;
166 }
167
168 NullBoard::NullBoard()
169 {
170 }
171
172 NullBoard::~ NullBoard()
173 {
174 }
175
176 bool BoardXmlParser::startElement( const QString&, const QString& localName, const QString&, const QXmlAttributes& )
177 {
178   if( m_inBoard == true ) {
179     if( localName == "url" ) {
180     } else if( localName == "name" ) {
181     } else {
182       // error
183       return false;
184     }
185   } else {
186     if( localName == "board" ) {
187       m_inBoard = true;
188     } else {
189       // error
190       return false;
191     }
192   }
193   return true;
194 }
195
196 bool BoardXmlParser::endElement( const QString&, const QString& localName, const QString& )
197 {
198   if( localName == "board" ) {
199     m_inBoard = false;
200     Kita::Board::setName( m_urlStr, m_nameStr );
201     m_board = Kita::Board::getByURL( m_urlStr );
202     m_isValid = true;
203     // create board;
204   } else if( localName == "url" ) {
205     m_urlStr = m_characters;
206   } else if( localName == "name" ) {
207     m_nameStr = m_characters;
208   } else {
209     // error
210     return false;
211   }
212   return true;
213 }
214
215 bool BoardXmlParser::characters( const QString& ch )
216 {
217   m_characters = ch;
218   return true;
219 }