OSDN Git Service

remove unused code.
[kita/kita.git] / kita / src / domtree.cpp
1 /***************************************************************************
2  *   Copyright (C) 2004, 2007 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 /* This class manages the DOM elements  */
12
13 #include "domtree.h"
14
15 #include <kurl.h>
16
17 #include <dom/dom_text.h>
18
19 #include "libkita/datmanager.h"
20 #include "libkita/datinfo.h"
21 #include "libkita/kita-utf8.h"
22 #include "libkita/kita_misc.h"
23
24 KitaDomTree::KitaDomTree( const DOM::HTMLDocument& hdoc, const KURL& datURL )
25 {
26     m_hdoc = hdoc;
27     m_bufSize = 0;
28     m_bottomNum = 0;
29
30     /* create the nodes of footer, header, etc. */
31     createKokoyon();
32     createFooter();
33     createHeader();
34
35     /* get pointer of DatInfo */
36     /* Note that m_datURL is already locked in the KitaHTMLPart. */
37     m_datInfo = Kita::DatManager::getDatInfoPointer( datURL );
38 }
39
40 KitaDomTree::~KitaDomTree() {}
41
42 /*
43  * This function creates DOM elements of both title and body.
44  * To show the res, call appendRes()
45  */
46 bool KitaDomTree::createResElement( int num )
47 {
48     Q_ASSERT( num > 0 );
49     Q_ASSERT( m_datInfo != NULL );
50
51     if ( num < m_bufSize && m_resStatus[ num ] != KITA_HTML_NOTPARSED ) {
52         /* already parsed */
53         return TRUE;
54     }
55
56     if ( num >= m_bufSize ) {
57         /* resize buffer size */
58         if ( m_bufSize == 0 ) m_bufSize = 100;
59         while ( num >= m_bufSize ) m_bufSize += 500;
60
61         m_titleElm.resize( m_bufSize );
62         m_bodyElm.resize( m_bufSize );
63         m_resStatus.resize( m_bufSize, FALSE );
64         m_coloredNum.resize( m_bufSize, FALSE );
65     }
66
67     /* cleate elements */
68     QString titleHTML, bodyHTML;
69     m_resStatus[ num ] = m_datInfo->getHTML( num, TRUE, titleHTML, bodyHTML );
70
71     if ( m_resStatus[ num ] == KITA_HTML_NOTPARSED ) {
72         return FALSE;
73     }
74
75     m_titleElm[ num ] = m_hdoc.createElement( "DIV" );
76     m_titleElm[ num ].setAttribute( "class", "res_title" );
77     m_titleElm[ num ].setAttribute( "id", QString().setNum( num ) );
78     m_titleElm[ num ].setInnerHTML( titleHTML );
79
80     m_bodyElm[ num ] = m_hdoc.createElement( "DIV" );
81     m_bodyElm[ num ].setAttribute( "class", "res_body" );
82     m_bodyElm[ num ].setAttribute( "id", QString().setNum( num ) );
83     m_bodyElm[ num ].setInnerHTML( bodyHTML );
84
85     return TRUE;
86 }
87
88 /*
89  * append the response
90  */
91 bool KitaDomTree::appendRes( int num )
92 {
93     if ( !createResElement( num ) ) return FALSE;
94
95     m_hdoc.body().appendChild( m_titleElm[ num ] );
96     m_hdoc.body().appendChild( m_bodyElm[ num ] );
97
98     if ( num > m_bottomNum ) m_bottomNum = num;
99
100     return TRUE;
101 }
102
103 /*
104  * redraw all
105  */
106 void KitaDomTree::redraw( bool force )
107 {
108     Q_ASSERT( m_datInfo != NULL );
109
110     int readNum = m_datInfo->getReadNum();
111
112     /* don't forget to reset abone here... */
113     m_datInfo->resetAbone();
114
115     for ( int i = 1; i <= readNum; i++ ) {
116         QString titleHTML, bodyHTML;
117
118         int oldStatus = m_resStatus[ i ];
119         m_resStatus[ i ] = m_datInfo->getHTML( i , TRUE, titleHTML, bodyHTML );
120
121         if ( force || oldStatus != m_resStatus[ i ] ) {
122             m_titleElm[ i ].setInnerHTML( titleHTML );
123             m_bodyElm[ i ].setInnerHTML( bodyHTML );
124         }
125     }
126 }
127
128
129 /*
130  * change color of number of the res which is responsed.
131  *
132  * See also KitaDomTree::changeColorOfNumber() ,
133  * DatInfo::copyOneLineToResDat(),
134  * and DatInfo::collectResponsedRes().
135  */
136 void KitaDomTree::changeColorOfAllResponsedNumber()
137 {
138     for ( int i = 1; i <= m_bottomNum; ++i ) {
139         if ( m_datInfo->isResponsed( i ) ) {
140             changeColorOfNumber( i );
141         }
142     }
143 }
144
145 const int KitaDomTree::getBottomResNumber() const
146 {
147     return m_bottomNum;
148 }
149
150 /*
151  * append footer & header
152  */
153 void KitaDomTree::appendFooterAndHeader()
154 {
155     Q_ASSERT( m_datInfo != NULL );
156
157     int readNum = m_datInfo->getReadNum();
158     if ( !readNum ) return ;
159
160     updateHeader( m_header );
161     updateFooter( m_footer );
162
163     m_hdoc.body().insertBefore( m_header, m_hdoc.body().firstChild() );
164     m_hdoc.body().appendChild( m_footer );
165 }
166
167 /*
168  * append kokomadeyonda
169  */
170 void KitaDomTree::appendKokoyon()
171 {
172     Q_ASSERT( m_datInfo != NULL );
173
174     int readNum = m_datInfo->getReadNum();
175     if ( !readNum ) return ;
176
177     int viewPos = m_datInfo->getViewPos();
178     if ( viewPos == 0 ) return ;
179
180     int i = viewPos + 1;
181
182     if ( i <= readNum ) m_hdoc.body().insertBefore( m_kokoyon, m_titleElm[ i ] );
183     else m_hdoc.body().appendChild( m_kokoyon );
184 }
185
186 /*
187  * private functions
188  */
189
190 /*
191  * append "A" Node to rootnode
192  */
193 void KitaDomTree::appendAnchorNode( DOM::Element rootnode, const QString& href, const QString& linkstr )
194 {
195     DOM::Element element;
196
197     element = rootnode.appendChild( m_hdoc.createElement( "A" ) );
198     {
199         element.setAttribute( "href", href );
200         element.appendChild( m_hdoc.createTextNode( linkstr ) );
201     }
202 }
203
204
205 /*
206  * update header
207  *
208  * example
209  * before: #KokomadeYonda 1- 101- 201- #ToSaigo<br><br>
210  * after : #KokomadeYonda 1- 101- 201- 301- 401- #ToSaigo<br><br>
211  */
212 void KitaDomTree::updateHeader( DOM::Element& headerElement )
213 {
214     if ( ! m_datInfo ) return ;
215
216     DOM::Element backupElement1, backupElement2, backupElement3;
217     int readNum = m_datInfo->getReadNum();
218
219     /* remove <a href="#tosaigo"> and <BR> */
220     backupElement1 = headerElement.removeChild( headerElement.lastChild() ); /* BR */
221     backupElement2 = headerElement.removeChild( headerElement.lastChild() ); /* BR */
222     backupElement3 = headerElement.removeChild( headerElement.lastChild() ); /* "#tosaigo" */
223
224     DOM::Node node = headerElement.firstChild(); /* node is now "#kokomade_yonda" */
225     node = node.nextSibling(); /* " " */
226     node = node.nextSibling();
227
228     /* '1-', '101-' などのリンクを作成 */
229     for ( int num = 1; num < readNum ; num += 100 ) {
230         if ( node == NULL ) {
231             QString href = QString( "#%1" ).arg( num );
232             QString linkStr = QString( "%1-" ).arg( num );
233
234             appendAnchorNode( headerElement, href, linkStr );
235             node = headerElement.appendChild( m_hdoc.createTextNode( " " ) );
236             node = node.nextSibling();
237         } else {
238             // 既にリンクが作られている場合は飛ばす
239             node = node.nextSibling();
240             if ( node != NULL ) node = node.nextSibling();
241         }
242     }
243
244     /* restore <a href="#tosaigo"> and <BR> */
245     headerElement.appendChild( backupElement3 ); /* "#tosaigo" */
246     headerElement.appendChild( backupElement2 ); /* BR */
247     headerElement.appendChild( backupElement1 ); /* BR */
248 }
249
250 /* 
251  * update footer
252  *
253  * example
254  * before: #KokomadeYonda 1- 101- 201- #ToSaigo
255  * after : #KokomadeYonda 1- 101- 201- 301- 401- #ToSaigo
256  */
257 void KitaDomTree::updateFooter( DOM::Element& footerElement )
258 {
259     Q_ASSERT( m_datInfo != NULL );
260
261     DOM::Element backupElement;
262     int readNum = m_datInfo->getReadNum();
263
264     backupElement = footerElement.removeChild( footerElement.lastChild() ); /* "#tosaigo" */
265
266     DOM::Node node = footerElement.firstChild(); /* node is now "#kokomade_yonda" */
267     node = node.nextSibling(); /* " " */
268     node = node.nextSibling();
269
270     /* '1-', '101-' などのリンクを作成 */
271     for ( int num = 1; num < readNum ; num += 100 ) {
272         if ( node == NULL ) {
273             QString href = QString( "#%1" ).arg( num );
274             QString linkStr = QString( "%1-" ).arg( num );
275
276             appendAnchorNode( footerElement, href, linkStr );
277             node = footerElement.appendChild( m_hdoc.createTextNode( " " ) );
278             node = node.nextSibling();
279         } else {
280             // 既にリンクが作られている場合は飛ばす
281             node = node.nextSibling();
282             if ( node != NULL ) node = node.nextSibling();
283         }
284     }
285
286     footerElement.appendChild( backupElement ); /* "#tosaigo" */
287 }
288
289 /*
290  * create header node
291  */
292 void KitaDomTree::createHeader()
293 {
294     QString str;
295     DOM::Element rootnode;
296
297     rootnode = m_hdoc.createElement( "DIV" );
298     {
299         rootnode.setAttribute( "kita_type", "header" );
300         rootnode.setAttribute( "id", "header" );
301
302         str = Kita::utf8ToUnicode( KITAUTF8_KOKOYON );
303         appendAnchorNode( rootnode, "#kokomade_yonda", str );
304         rootnode.appendChild( m_hdoc.createTextNode( " " ) );
305
306         str = Kita::utf8ToUnicode( KITAUTF8_SAIGO );
307         appendAnchorNode( rootnode, "#tosaigo", str );
308
309         rootnode.appendChild( m_hdoc.createElement( "BR" ) );
310         rootnode.appendChild( m_hdoc.createElement( "BR" ) );
311     }
312
313     m_header = rootnode;
314 }
315
316
317 /*
318  * create footer node
319  */
320 void KitaDomTree::createFooter()
321 {
322     QString str;
323     DOM::Element rootnode;
324
325     rootnode = m_hdoc.createElement( "DIV" );
326     {
327         rootnode.setAttribute( "kita_type", "footer" );
328         rootnode.setAttribute( "id", "footer" );
329
330         str = Kita::utf8ToUnicode( KITAUTF8_KOKOYON );
331         appendAnchorNode( rootnode, "#kokomade_yonda", str );
332         rootnode.appendChild( m_hdoc.createTextNode( " " ) );
333
334         str = Kita::utf8ToUnicode( KITAUTF8_SAIGO );
335         appendAnchorNode( rootnode, "#tosaigo", str );
336     }
337
338     m_footer = rootnode;
339 }
340
341 /*
342  * create kokomadeyonda node
343  */
344 void KitaDomTree::createKokoyon()
345 {
346     QString str, style;
347     DOM::Element rootnode;
348
349     str = Kita::utf8ToUnicode( KITAUTF8_KOKOYON2 );
350
351     rootnode = m_hdoc.createElement( "DIV" );
352     {
353         rootnode.setAttribute( "class", "kokoyon" );
354         rootnode.setAttribute( "kita_type", "kokoyon" );
355         rootnode.setAttribute( "id", "kokomade_yonda" );
356         rootnode.appendChild( m_hdoc.createTextNode( str ) );
357     }
358
359     m_kokoyon = rootnode;
360 }
361
362 /*
363  * change color of number
364  * specify color like this:  "a.coloredLink:link{ color: red; }"
365  */
366 void KitaDomTree::changeColorOfNumber( int num )
367 {
368     if ( m_coloredNum[ num ] ) return ;
369
370     m_coloredNum[ num ] = TRUE;
371
372     DOM::Node node = m_titleElm[ num ];
373     node = node.firstChild();
374
375     static_cast<DOM::HTMLElement>( node ).setAttribute( "class", "coloredLink" );
376 }