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>
22 #include "kitaui/htmlview.h"
24 #include "libkita/kitaconfig.h"
25 #include "libkita/datmanager.h"
26 #include "libkita/kita_misc.h"
27 #include "libkita/signalcollection.h"
28 #include "libkita/config_xt.h"
31 * Don't forget to call setup() later.
33 KitaPreviewPart::KitaPreviewPart( QWidget* parent, const char* name )
34 : KHTMLPart( new KitaHTMLView( this, parent, name ) )
37 m_datURL = QString::null;
44 KitaPreviewPart::~KitaPreviewPart()
49 void KitaPreviewPart::clearPart()
53 if ( !m_datURL.isEmpty() ) {
54 /* This part is opened. */
56 /* don't forget to unlock previous datURL here. */
57 Kita::DatManager::unlock( m_datURL );
59 m_datURL = QString::null;
62 bool KitaPreviewPart::setup( const KURL& url )
64 if ( url.isEmpty() ) return FALSE;
68 m_datURL = Kita::getDatURL( url );
70 /* Lock datURL. Don't forget to unlock it later ! */
71 Kita::DatManager::lock ( m_datURL );
73 /* create HTML Document */
79 void KitaPreviewPart::connectSignals()
81 Kita::SignalCollection * signalCollection = Kita::SignalCollection::getInstance();
84 connect( signalCollection, SIGNAL( threadFaceChanged() ), SLOT( slotSetFaceOfHTMLPart() ) );
85 connect( signalCollection, SIGNAL( setStyleSheetOfHTMLPart() ), SLOT( slotSetStyleSheetOfHTMLPart() ) );
88 connect( this, SIGNAL( onURL( const QString& ) ), SLOT( slotOnURL( const QString& ) ) );
89 connect( this, SIGNAL( isKitaActive() ), signalCollection, SIGNAL( isKitaActive() ) );
91 connect( view(), SIGNAL( leave() ), SLOT( slotLeave() ) );
92 connect( view(), SIGNAL( verticalSliderReleased() ), SLOT( slotVSliderReleased() ) );
93 connect( view(), SIGNAL( horizontalSliderReleased() ), SLOT( slotHSliderReleased() ) );
95 connect( signalCollection, SIGNAL( kitaIsActive() ), SLOT( slotKitaIsActive() ) );
96 connect( signalCollection, SIGNAL( windowDeactivated() ), SLOT( slotHideChildPopup() ) );
99 connect( this, SIGNAL( openURLRequestExt( const KURL&, const QString ) ),
100 signalCollection, SIGNAL( openURLRequestExt( const KURL&, const QString ) ) );
103 void KitaPreviewPart::createHTMLDocument()
106 QString style = QString( "body { font-size: %1pt; font-family: \"%2\"; color: %3; background-color: %4; }" )
107 .arg( Kita::Config::threadFont().pointSize() )
108 .arg( Kita::Config::threadFont().family() )
109 .arg( Kita::Config::threadColor().name() )
110 .arg( Kita::Config::threadBackground().name() );
112 QString text = "<html><head><style>";
113 text += KitaConfig::defaultStyleSheetText();
115 if ( Kita::Config::useStyleSheet() ) {
116 text += KitaConfig::styleSheetText();
118 text += "</style></head><body></body></html>";
120 setJScriptEnabled( false );
121 setJavaEnabled( false );
123 /* Use dummy URL here, and protocol should be "file:".
124 If protocol is "http:", local image files are not shown
125 (for security reasons ?).
127 begin( "file:/dummy.htm" );
132 void KitaPreviewPart::setInnerHTML( const QString& innerHTML )
134 createHTMLDocument();
135 htmlDocument().body().setInnerHTML( innerHTML );
138 void KitaPreviewPart::slotSetFaceOfHTMLPart()
140 QFont font = Kita::Config::threadFont();
142 DOM::CSSStyleDeclaration style = htmlDocument().body().style();
143 style.setProperty( "font-family", font.family(), "" );
144 style.setProperty( "font-size", QString( "%1pt" ).arg( font.pointSize() ), "" );
145 style.setProperty( "color", Kita::Config::threadColor().name(), "" );
146 style.setProperty( "background-color", Kita::Config::threadBackground().name(), "" );
148 htmlDocument().applyChanges();
151 void KitaPreviewPart::slotSetStyleSheetOfHTMLPart()
153 /* [0]<html> -> [1]<head> -> [2]<style> */
154 DOM::HTMLCollection collection = htmlDocument().all();
155 DOM::HTMLElement element;
157 for ( i = 0 ; i < collection.length() ; i++ ) {
158 element = collection.item( i );
159 if ( element.tagName().upper() == "STYLE" ) {
160 QString style = QString( "body { font-size: %1pt; font-family: %2; color: %3; background-color: %4; }" )
161 .arg( Kita::Config::threadFont().pointSize() )
162 .arg( Kita::Config::threadFont().family() )
163 .arg( Kita::Config::threadColor().name() )
164 .arg( Kita::Config::threadBackground().name() );
166 QString style0 = KitaConfig::defaultStyleSheetText();
168 if ( Kita::Config::useStyleSheet() ) {
169 style0 += KitaConfig::styleSheetText();
172 element.setInnerText( style0 );
173 htmlDocument().applyChanges();
182 void KitaPreviewPart::customEvent( QCustomEvent * e )
184 if ( e->type() == EVENT_GotoAnchor ) {
185 KHTMLPart::gotoAnchor( static_cast< GotoAnchorEvent* >( e ) ->getAnc() );
189 KHTMLPart::customEvent( e );
195 void KitaPreviewPart::khtmlMousePressEvent( khtml::MousePressEvent* e )
197 emit mousePressed(); /* to KitaThreadView to focus this view. */
200 if ( e->url().string() != QString::null ) {
201 kurl = KURL( Kita::BoardManager::boardURL( m_datURL ), e->url().string() );
204 if ( e->qmouseEvent()->button() & Qt::RightButton ) {
205 m_isPushedRightButton = TRUE;
207 m_isPushedRightButton = FALSE;
210 if ( e->url() != NULL ) {
212 if ( e->url().string().at( 0 ) == '#' ) { /* anchor */
214 kurl.setRef( e->url().string().mid( 1 ) ) ;
218 m_isPushedRightButton = FALSE;
222 KHTMLPart::khtmlMousePressEvent( e );
226 * This function is called when user clicked res anchor
228 void KitaPreviewPart::clickAnchor( const KURL& urlin )
231 KURL datURL = Kita::getDatURL( urlin, refstr );
233 /*--------------------------------*/
234 /* If this is not anchor, then */
235 /* emit openURLRequest and return */
236 if ( datURL.host() != m_datURL.host() || datURL.path() != m_datURL.path() ) {
237 emit openURLRequestExt( urlin );
241 if ( refstr == QString::null ) return ;
243 /*-------------------------*/
244 /* start multi-popup mdde */
245 if ( m_isPushedRightButton && startMultiPopup() ) return ;
249 int i = refstr.find( "-" );
251 refNum = refstr.left( i ).toInt();
252 refNum2 = refstr.mid( i + 1 ).toInt();
253 if ( refNum2 < refNum ) {
257 refNum = refNum2 = refstr.toInt();
260 if ( !refNum ) return ;
262 emit openURLRequestExt( urlin );
265 void KitaPreviewPart::slotDeletePopup()
267 if ( m_popup ) delete m_popup;
269 m_multiPopup = FALSE;
275 void KitaPreviewPart::showPopup( const KURL& url, const QString& innerHTML )
278 m_multiPopup = FALSE;
280 m_popup = new Kita::ResPopup( view() , url );
282 connect( m_popup, SIGNAL( hideChildPopup() ), SLOT( slotHideChildPopup() ) );
284 m_popup->setText( innerHTML );
285 m_popup->adjustSize();
286 m_popup->adjustPos( QCursor::pos() );
291 * start multi-popup mode
293 bool KitaPreviewPart::startMultiPopup()
295 if ( m_popup && m_popup->isVisible() ) {
297 m_popup->moveMouseAbove();
299 m_multiPopup = FALSE;
306 * Is it multi-popup mode now ?
308 bool KitaPreviewPart::isMultiPopupMode()
311 m_multiPopup = FALSE;
312 } else if ( m_popup->isHidden() ) {
313 m_multiPopup = FALSE;
319 void KitaPreviewPart::hidePopup()
325 m_multiPopup = FALSE;
328 void KitaPreviewPart::slotLeave()
330 if ( isMultiPopupMode() ) return ;
331 if ( view()->isHorizontalSliderPressed() ) return ;
332 if ( view()->isVerticalSliderPressed () ) return ;
337 void KitaPreviewPart::slotVSliderReleased()
339 QScrollBar* bar = view()->verticalScrollBar();
340 QRect rt = bar->sliderRect();
345 void KitaPreviewPart::slotHSliderReleased()
347 QScrollBar* bar = view()->horizontalScrollBar();
348 QRect rt = bar->sliderRect();
353 void KitaPreviewPart::slotHideChildPopup()
359 * called back when kita is active.
360 * see also an explanation in slotOnURL.
362 void KitaPreviewPart::slotKitaIsActive()
364 m_kitaIsActive = TRUE;
368 * This slot is called when mouse moves onto the URL
370 void KitaPreviewPart::slotOnURL( const QString& url )
372 const int maxpopup = 10; /* max number of responses shown in the popup window */
374 if ( isMultiPopupMode() ) return ;
378 if ( url.isEmpty() ) return ;
379 if ( url.left( 7 ) == "mailto:" ) return ;
381 /* Is Kita active now ?
383 emit SIGNAL( isKitaActive() ) to KitaMainWindow, KitaNavi, etc. ,
384 and if one of them is active, then slotKitaIsActive() is called
385 back, and m_kitaIsActive is set to TRUE. */
386 m_kitaIsActive = FALSE;
388 if ( !m_kitaIsActive ) return ;
392 KURL datURL = m_datURL;
393 if ( url.at( 0 ) == '#' ) {
394 refstr = url.mid( 1 );
396 datURL = Kita::getDatURL( KURL( m_datURL, url ), refstr );
399 /*-------------------------*/
400 /* popup for anchor */
402 QString innerHTML = QString::null;
406 int i = refstr.find( "-" );
407 if ( i != -1 ) { /* >>refNum-refNum2 */
409 refNum = refstr.left( i ).toInt();
410 refNum2 = refstr.mid( i + 1 ).toInt();
413 if ( refNum2 < refNum ) refNum2 = refNum;
414 if ( refNum2 - refNum > maxpopup - 1 ) refNum2 = refNum + maxpopup - 1;
417 } else { /* >>refNum */
418 refNum = refstr.toInt();
422 /* another thread ? */
423 if ( datURL.host() != m_datURL.host() || datURL.path() != m_datURL.path() ) {
426 QString boardName = Kita::BoardManager::boardName( datURL );
427 if ( boardName != QString::null ) {
428 innerHTML += "[" + boardName + "] ";
431 /* If idx file of datURL is not read, thread name cannot be obtained.
432 so, create DatInfo if cache exists, and read idx file in DatInfo::DatInfo(). */
433 Kita::DatManager::getDatInfoPointer( datURL );
435 /* get thread Name */
436 QString subName = Kita::DatManager::threadName( datURL );
437 if ( subName != QString::null ) {
438 innerHTML += subName + "<br><br>";
441 if ( !refNum ) refNum = refNum2 = 1;
444 /* get HTML and show it */
445 if ( !refNum ) return ;
447 innerHTML += Kita::DatManager::getHtml( datURL, refNum, refNum2 );
448 if ( innerHTML != QString::null ) {
449 showPopup( datURL, innerHTML );