1 /***************************************************************************
2 * Copyright (C) 2004 by Kita Developers *
3 * ikemo@users.sourceforge.jp *
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 ***************************************************************************/
11 #include "previewpart.h"
15 #include <khtml_events.h>
17 #include <dom/html_misc.h>
21 #include "viewmediator.h"
23 #include "kitaui/htmlview.h"
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"
32 * Don't forget to call setup() later.
34 KitaPreviewPart::KitaPreviewPart( QWidget* parent, const char* name )
35 : KHTMLPart( new KitaHTMLView( this, parent, name ) )
38 m_datURL = QString::null;
45 KitaPreviewPart::~KitaPreviewPart()
50 void KitaPreviewPart::clearPart()
54 if ( !m_datURL.isEmpty() ) {
55 /* This part is opened. */
57 /* don't forget to unlock previous datURL here. */
58 Kita::DatManager::unlock( m_datURL );
60 m_datURL = QString::null;
63 bool KitaPreviewPart::setup( const KURL& url )
65 if ( url.isEmpty() ) return FALSE;
69 m_datURL = Kita::getDatURL( url );
71 /* Lock datURL. Don't forget to unlock it later ! */
72 Kita::DatManager::lock ( m_datURL );
74 /* create HTML Document */
80 void KitaPreviewPart::connectSignals()
82 Kita::SignalCollection * signalCollection = Kita::SignalCollection::getInstance();
85 connect( this, SIGNAL( onURL( const QString& ) ), SLOT( slotOnURL( const QString& ) ) );
87 connect( view(), SIGNAL( leave() ), SLOT( slotLeave() ) );
88 connect( view(), SIGNAL( verticalSliderReleased() ), SLOT( slotVSliderReleased() ) );
89 connect( view(), SIGNAL( horizontalSliderReleased() ), SLOT( slotHSliderReleased() ) );
91 connect( signalCollection, SIGNAL( windowDeactivated() ), SLOT( slotHideChildPopup() ) );
94 void KitaPreviewPart::createHTMLDocument()
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() );
103 QString text = "<html><head><style>";
104 text += KitaConfig::defaultStyleSheetText();
106 if ( Kita::Config::useStyleSheet() ) {
107 text += KitaConfig::styleSheetText();
109 text += "</style></head><body></body></html>";
111 setJScriptEnabled( FALSE );
112 setJavaEnabled( FALSE );
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 ?).
118 begin( "file:/dummy.htm" );
123 void KitaPreviewPart::setInnerHTML( const QString& innerHTML )
125 createHTMLDocument();
126 htmlDocument().body().setInnerHTML( innerHTML );
129 void KitaPreviewPart::slotSetFaceOfHTMLPart()
131 QFont font = Kita::Config::threadFont();
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(), "" );
139 htmlDocument().applyChanges();
145 void KitaPreviewPart::customEvent( QCustomEvent * e )
147 if ( e->type() == EVENT_GotoAnchor ) {
148 KHTMLPart::gotoAnchor( static_cast< GotoAnchorEvent* >( e ) ->getAnc() );
152 KHTMLPart::customEvent( e );
158 void KitaPreviewPart::khtmlMousePressEvent( khtml::MousePressEvent* e )
160 emit mousePressed(); /* to KitaThreadView to focus this view. */
163 if ( e->url().string() != QString::null ) {
164 kurl = KURL( Kita::BoardManager::boardURL( m_datURL ), e->url().string() );
167 if ( e->qmouseEvent()->button() & Qt::RightButton ) {
168 m_isPushedRightButton = TRUE;
170 m_isPushedRightButton = FALSE;
173 if ( e->url() != NULL ) {
175 if ( e->url().string().at( 0 ) == '#' ) { /* anchor */
177 kurl.setRef( e->url().string().mid( 1 ) ) ;
181 m_isPushedRightButton = FALSE;
185 KHTMLPart::khtmlMousePressEvent( e );
189 * This function is called when user clicked res anchor
191 void KitaPreviewPart::clickAnchor( const KURL& urlin )
194 KURL datURL = Kita::getDatURL( urlin, refstr );
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 );
204 if ( refstr == QString::null ) return ;
206 /*-------------------------*/
207 /* start multi-popup mdde */
208 if ( m_isPushedRightButton && startMultiPopup() ) return ;
212 int i = refstr.find( "-" );
214 refNum = refstr.left( i ).toInt();
215 refNum2 = refstr.mid( i + 1 ).toInt();
216 if ( refNum2 < refNum ) {
220 refNum = refNum2 = refstr.toInt();
223 if ( !refNum ) return ;
225 ViewMediator::getInstance()->openURL( urlin );
228 void KitaPreviewPart::slotDeletePopup()
230 if ( m_popup ) delete m_popup;
232 m_multiPopup = FALSE;
238 void KitaPreviewPart::showPopup( const KURL& url, const QString& innerHTML )
241 m_multiPopup = FALSE;
243 m_popup = new Kita::ResPopup( view() , url );
245 connect( m_popup, SIGNAL( hideChildPopup() ), SLOT( slotHideChildPopup() ) );
247 m_popup->setText( innerHTML );
248 m_popup->adjustSize();
249 m_popup->adjustPos( QCursor::pos() );
254 * start multi-popup mode
256 bool KitaPreviewPart::startMultiPopup()
258 if ( m_popup && m_popup->isVisible() ) {
260 m_popup->moveMouseAbove();
262 m_multiPopup = FALSE;
269 * Is it multi-popup mode now ?
271 bool KitaPreviewPart::isMultiPopupMode()
274 m_multiPopup = FALSE;
275 } else if ( m_popup->isHidden() ) {
276 m_multiPopup = FALSE;
282 void KitaPreviewPart::hidePopup()
288 m_multiPopup = FALSE;
291 void KitaPreviewPart::slotLeave()
293 if ( isMultiPopupMode() ) return ;
294 if ( view()->isHorizontalSliderPressed() ) return ;
295 if ( view()->isVerticalSliderPressed () ) return ;
300 void KitaPreviewPart::slotVSliderReleased()
302 QScrollBar* bar = view()->verticalScrollBar();
303 QRect rt = bar->sliderRect();
308 void KitaPreviewPart::slotHSliderReleased()
310 QScrollBar* bar = view()->horizontalScrollBar();
311 QRect rt = bar->sliderRect();
316 void KitaPreviewPart::slotHideChildPopup()
322 * This slot is called when mouse moves onto the URL
324 void KitaPreviewPart::slotOnURL( const QString& url )
326 const int maxpopup = 10; /* max number of responses shown in the popup window */
328 if ( isMultiPopupMode() ) return ;
332 if ( url.isEmpty() ) return ;
333 if ( url.left( 7 ) == "mailto:" ) return ;
335 /* Is Kita active now ? */
336 if ( ViewMediator::getInstance()->isKitaActive() == FALSE ) return;
340 KURL datURL = m_datURL;
341 if ( url.at( 0 ) == '#' ) {
342 refstr = url.mid( 1 );
344 datURL = Kita::getDatURL( KURL( m_datURL, url ), refstr );
347 /*-------------------------*/
348 /* popup for anchor */
350 QString innerHTML = QString::null;
354 int i = refstr.find( "-" );
355 if ( i != -1 ) { /* >>refNum-refNum2 */
357 refNum = refstr.left( i ).toInt();
358 refNum2 = refstr.mid( i + 1 ).toInt();
361 if ( refNum2 < refNum ) refNum2 = refNum;
362 if ( refNum2 - refNum > maxpopup - 1 ) refNum2 = refNum + maxpopup - 1;
365 } else { /* >>refNum */
366 refNum = refstr.toInt();
370 /* another thread ? */
371 if ( datURL.host() != m_datURL.host() || datURL.path() != m_datURL.path() ) {
374 QString boardName = Kita::BoardManager::boardName( datURL );
375 if ( boardName != QString::null ) {
376 innerHTML += "[" + boardName + "] ";
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 );
383 /* get thread Name */
384 QString subName = Kita::DatManager::threadName( datURL );
385 if ( subName != QString::null ) {
386 innerHTML += subName + "<br><br>";
389 if ( !refNum ) refNum = refNum2 = 1;
392 /* get HTML and show it */
393 if ( !refNum ) return ;
395 innerHTML += Kita::DatManager::getHtml( datURL, refNum, refNum2 );
396 if ( innerHTML != QString::null ) {
397 showPopup( datURL, innerHTML );