OSDN Git Service

remove unused code.
[kita/kita.git] / kita / src / previewpart.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 #include "previewpart.h"
12
13 #include <qcursor.h>
14
15 #include <khtml_events.h>
16
17 #include <dom/html_misc.h>
18
19 #include "respopup.h"
20 #include "htmlpart.h"
21 #include "viewmediator.h"
22
23 #include "kitaui/htmlview.h"
24
25 #include "libkita/kitaconfig.h"
26 #include "libkita/datmanager.h"
27 #include "libkita/kita_misc.h"
28 #include "libkita/signalcollection.h"
29 #include "libkita/config_xt.h"
30
31 /*
32  * Don't forget to call setup() later.
33  */
34 KitaPreviewPart::KitaPreviewPart( QWidget* parent, const char* name )
35         : KHTMLPart( new KitaHTMLView( this, parent, name ) )
36 {
37     m_popup = NULL;
38     m_datURL = QString::null;
39
40     clearPart();
41     createHTMLDocument();
42     connectSignals();
43 }
44
45 KitaPreviewPart::~KitaPreviewPart()
46 {
47     clearPart();
48 }
49
50 void KitaPreviewPart::clearPart()
51 {
52     slotDeletePopup();
53
54     if ( !m_datURL.isEmpty() ) {
55         /* This part is opened. */
56
57         /* don't forget to unlock previous datURL here. */
58         Kita::DatManager::unlock( m_datURL );
59     }
60     m_datURL = QString::null;
61 }
62
63 bool KitaPreviewPart::setup( const KURL& url )
64 {
65     if ( url.isEmpty() ) return FALSE;
66
67     clearPart();
68
69     m_datURL = Kita::getDatURL( url );
70
71     /* Lock datURL. Don't forget to unlock it later ! */
72     Kita::DatManager::lock ( m_datURL );
73
74     /* create HTML Document */
75     createHTMLDocument();
76
77     return TRUE;
78 }
79
80 void KitaPreviewPart::connectSignals()
81 {
82     Kita::SignalCollection * signalCollection = Kita::SignalCollection::getInstance();
83
84     /* popup */
85     connect( this, SIGNAL( onURL( const QString& ) ), SLOT( slotOnURL( const QString& ) ) );
86
87     connect( view(), SIGNAL( leave() ), SLOT( slotLeave() ) );
88     connect( view(), SIGNAL( verticalSliderReleased() ), SLOT( slotVSliderReleased() ) );
89     connect( view(), SIGNAL( horizontalSliderReleased() ), SLOT( slotHSliderReleased() ) );
90
91     connect( signalCollection, SIGNAL( windowDeactivated() ), SLOT( slotHideChildPopup() ) );
92 }
93
94 void KitaPreviewPart::createHTMLDocument()
95 {
96     /* style */
97     QString style = QString( "body { font-size: %1pt; font-family: \"%2\"; color: %3; background-color: %4; }" )
98                     .arg( Kita::Config::threadFont().pointSize() )
99                     .arg( Kita::Config::threadFont().family() )
100                     .arg( Kita::Config::threadColor().name() )
101                     .arg( Kita::Config::threadBackground().name() );
102
103     QString text = "<html><head><style>";
104     text += KitaConfig::defaultStyleSheetText();
105     text += style;
106     if ( Kita::Config::useStyleSheet() ) {
107         text += KitaConfig::styleSheetText();
108     }
109     text += "</style></head><body></body></html>";
110
111     setJScriptEnabled( FALSE );
112     setJavaEnabled( FALSE );
113
114     /* Use dummy URL here, and protocol should be "file:".
115        If protocol is "http:", local image files are not shown
116        (for security reasons ?).
117      */
118     begin( "file:/dummy.htm" );
119     write( text );
120     end();
121 }
122
123 void KitaPreviewPart::setInnerHTML( const QString& innerHTML )
124 {
125     createHTMLDocument();
126     htmlDocument().body().setInnerHTML( innerHTML );
127 }
128
129 void KitaPreviewPart::slotSetFaceOfHTMLPart()
130 {
131     QFont font = Kita::Config::threadFont();
132
133     DOM::CSSStyleDeclaration style = htmlDocument().body().style();
134     style.setProperty( "font-family", font.family(), "" );
135     style.setProperty( "font-size", QString( "%1pt" ).arg( font.pointSize() ), "" );
136     style.setProperty( "color", Kita::Config::threadColor().name(), "" );
137     style.setProperty( "background-color", Kita::Config::threadBackground().name(), "" );
138
139     htmlDocument().applyChanges();
140 }
141
142 /*
143  * @Override
144  */
145 void KitaPreviewPart::customEvent( QCustomEvent * e )
146 {
147     if ( e->type() == EVENT_GotoAnchor ) {
148         KHTMLPart::gotoAnchor( static_cast< GotoAnchorEvent* >( e ) ->getAnc() );
149         return ;
150     }
151
152     KHTMLPart::customEvent( e );
153 }
154
155 /*
156  * @Override
157  */
158 void KitaPreviewPart::khtmlMousePressEvent( khtml::MousePressEvent* e )
159 {
160     emit mousePressed(); /* to KitaThreadView to focus this view. */
161
162     KURL kurl;
163     if ( e->url().string() != QString::null ) {
164         kurl = KURL( Kita::BoardManager::boardURL( m_datURL ), e->url().string() );
165     }
166
167     if ( e->qmouseEvent()->button() & Qt::RightButton ) {
168         m_isPushedRightButton = TRUE;
169     } else {
170         m_isPushedRightButton = FALSE;
171     }
172
173     if ( e->url() != NULL ) {
174
175         if ( e->url().string().at( 0 ) == '#' ) { /* anchor */
176             kurl = m_datURL;
177             kurl.setRef( e->url().string().mid( 1 ) ) ;
178         }
179
180         clickAnchor( kurl );
181         m_isPushedRightButton = FALSE;
182         return;
183     }
184
185     KHTMLPart::khtmlMousePressEvent( e );
186 }
187
188 /*
189  * This function is called when user clicked res anchor
190  */
191 void KitaPreviewPart::clickAnchor( const KURL& urlin )
192 {
193     QString refstr;
194     KURL datURL = Kita::getDatURL( urlin, refstr );
195
196     /*--------------------------------*/
197     /* If this is not anchor, then    */
198     /* emit openURLRequest and return */
199     if ( datURL.host() != m_datURL.host() || datURL.path() != m_datURL.path() ) {
200         ViewMediator::getInstance()->openURL( urlin );
201         return ;
202     }
203
204     if ( refstr == QString::null ) return ;
205
206     /*-------------------------*/
207     /* start multi-popup mdde  */
208     if ( m_isPushedRightButton && startMultiPopup() ) return ;
209
210     int refNum, refNum2;
211
212     int i = refstr.find( "-" );
213     if ( i != -1 ) {
214         refNum = refstr.left( i ).toInt();
215         refNum2 = refstr.mid( i + 1 ).toInt();
216         if ( refNum2 < refNum ) {
217             refNum2 = refNum;
218         }
219     } else {
220         refNum = refNum2 = refstr.toInt();
221     }
222
223     if ( !refNum ) return ;
224
225     ViewMediator::getInstance()->openURL( urlin );
226 }
227
228 void KitaPreviewPart::slotDeletePopup()
229 {
230     if ( m_popup ) delete m_popup;
231     m_popup = NULL;
232     m_multiPopup = FALSE;
233 }
234
235 /*
236  * for convenience
237  */
238 void KitaPreviewPart::showPopup( const KURL& url, const QString& innerHTML )
239 {
240     slotDeletePopup();
241     m_multiPopup = FALSE;
242
243     m_popup = new Kita::ResPopup( view() , url );
244
245     connect( m_popup, SIGNAL( hideChildPopup() ), SLOT( slotHideChildPopup() ) );
246
247     m_popup->setText( innerHTML );
248     m_popup->adjustSize();
249     m_popup->adjustPos( QCursor::pos() );
250     m_popup->show();
251 }
252
253 /*
254  * start multi-popup mode
255  */
256 bool KitaPreviewPart::startMultiPopup()
257 {
258     if ( m_popup && m_popup->isVisible() ) {
259         m_multiPopup = TRUE;
260         m_popup->moveMouseAbove();
261     } else {
262         m_multiPopup = FALSE;
263     }
264
265     return m_multiPopup;
266 }
267
268 /*
269  * Is it multi-popup mode now ?
270  */
271 bool KitaPreviewPart::isMultiPopupMode()
272 {
273     if ( !m_popup ) {
274         m_multiPopup = FALSE;
275     } else if ( m_popup->isHidden() ) {
276         m_multiPopup = FALSE;
277     }
278
279     return m_multiPopup;
280 }
281
282 void KitaPreviewPart::hidePopup()
283 {
284     if ( m_popup ) {
285         m_popup->hide();
286     }
287
288     m_multiPopup = FALSE;
289 }
290
291 void KitaPreviewPart::slotLeave()
292 {
293     if ( isMultiPopupMode() ) return ;
294     if ( view()->isHorizontalSliderPressed() ) return ;
295     if ( view()->isVerticalSliderPressed () ) return ;
296
297     hidePopup();
298 }
299
300 void KitaPreviewPart::slotVSliderReleased()
301 {
302     QScrollBar* bar = view()->verticalScrollBar();
303     QRect rt = bar->sliderRect();
304
305     hidePopup();
306 }
307
308 void KitaPreviewPart::slotHSliderReleased()
309 {
310     QScrollBar* bar = view()->horizontalScrollBar();
311     QRect rt = bar->sliderRect();
312
313     hidePopup();
314 }
315
316 void KitaPreviewPart::slotHideChildPopup()
317 {
318     hidePopup();
319 }
320
321 /*
322  * This slot is called when mouse moves onto the URL
323  */
324 void KitaPreviewPart::slotOnURL( const QString& url )
325 {
326     const int maxpopup = 10;  /* max number of responses shown in the popup window */
327
328     if ( isMultiPopupMode() ) return ;
329
330     slotDeletePopup();
331
332     if ( url.isEmpty() ) return ;
333     if ( url.left( 7 ) == "mailto:" ) return ;
334
335     /* Is Kita active now ? */
336     if ( ViewMediator::getInstance()->isKitaActive() == FALSE ) return;
337
338     /* get reference */
339     QString refstr;
340     KURL datURL = m_datURL;
341     if ( url.at( 0 ) == '#' ) {
342         refstr = url.mid( 1 );
343     } else {
344         datURL = Kita::getDatURL( KURL( m_datURL, url ), refstr );
345     }
346
347     /*-------------------------*/
348     /* popup for anchor        */
349
350     QString innerHTML = QString::null;
351     int refNum;
352     int refNum2;
353
354     int i = refstr.find( "-" );
355     if ( i != -1 ) { /* >>refNum-refNum2 */
356
357         refNum = refstr.left( i ).toInt();
358         refNum2 = refstr.mid( i + 1 ).toInt();
359
360         if ( refNum ) {
361             if ( refNum2 < refNum ) refNum2 = refNum;
362             if ( refNum2 - refNum > maxpopup - 1 ) refNum2 = refNum + maxpopup - 1;
363         }
364
365     } else { /* >>refNum */
366         refNum = refstr.toInt();
367         refNum2 = refNum;
368     }
369
370     /* another thread ? */
371     if ( datURL.host() != m_datURL.host() || datURL.path() != m_datURL.path() ) {
372
373         /* get board name */
374         QString boardName = Kita::BoardManager::boardName( datURL );
375         if ( boardName != QString::null ) {
376             innerHTML += "[" + boardName + "] ";
377         }
378
379         /* If idx file of datURL is not read, thread name cannot be obtained.
380            so, create DatInfo if cache exists, and read idx file in DatInfo::DatInfo(). */
381         Kita::DatManager::getDatInfoPointer( datURL );
382
383         /* get thread Name */
384         QString subName = Kita::DatManager::threadName( datURL );
385         if ( subName != QString::null ) {
386             innerHTML += subName + "<br><br>";
387         }
388
389         if ( !refNum ) refNum = refNum2 = 1;
390     }
391
392     /* get HTML and show it */
393     if ( !refNum ) return ;
394
395     innerHTML += Kita::DatManager::getHtml( datURL, refNum, refNum2 );
396     if ( innerHTML != QString::null ) {
397         showPopup( datURL, innerHTML );
398     }
399 }