OSDN Git Service

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