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( signalCollection, SIGNAL( threadFaceChanged() ), SLOT( slotSetFaceOfHTMLPart() ) );
88 connect( this, SIGNAL( onURL( const QString& ) ), SLOT( slotOnURL( const QString& ) ) );
90 connect( view(), SIGNAL( leave() ), SLOT( slotLeave() ) );
91 connect( view(), SIGNAL( verticalSliderReleased() ), SLOT( slotVSliderReleased() ) );
92 connect( view(), SIGNAL( horizontalSliderReleased() ), SLOT( slotHSliderReleased() ) );
94 connect( signalCollection, SIGNAL( windowDeactivated() ), SLOT( slotHideChildPopup() ) );
97 connect( this, SIGNAL( openURLRequestExt( const KURL&, const QString ) ),
98 signalCollection, SIGNAL( openURLRequestExt( const KURL&, const QString ) ) );
101 void KitaPreviewPart::createHTMLDocument()
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() );
110 QString text = "<html><head><style>";
111 text += KitaConfig::defaultStyleSheetText();
113 if ( Kita::Config::useStyleSheet() ) {
114 text += KitaConfig::styleSheetText();
116 text += "</style></head><body></body></html>";
118 setJScriptEnabled( false );
119 setJavaEnabled( false );
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 ?).
125 begin( "file:/dummy.htm" );
130 void KitaPreviewPart::setInnerHTML( const QString& innerHTML )
132 createHTMLDocument();
133 htmlDocument().body().setInnerHTML( innerHTML );
136 void KitaPreviewPart::slotSetFaceOfHTMLPart()
138 QFont font = Kita::Config::threadFont();
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(), "" );
146 htmlDocument().applyChanges();
152 void KitaPreviewPart::customEvent( QCustomEvent * e )
154 if ( e->type() == EVENT_GotoAnchor ) {
155 KHTMLPart::gotoAnchor( static_cast< GotoAnchorEvent* >( e ) ->getAnc() );
159 KHTMLPart::customEvent( e );
165 void KitaPreviewPart::khtmlMousePressEvent( khtml::MousePressEvent* e )
167 emit mousePressed(); /* to KitaThreadView to focus this view. */
170 if ( e->url().string() != QString::null ) {
171 kurl = KURL( Kita::BoardManager::boardURL( m_datURL ), e->url().string() );
174 if ( e->qmouseEvent()->button() & Qt::RightButton ) {
175 m_isPushedRightButton = TRUE;
177 m_isPushedRightButton = FALSE;
180 if ( e->url() != NULL ) {
182 if ( e->url().string().at( 0 ) == '#' ) { /* anchor */
184 kurl.setRef( e->url().string().mid( 1 ) ) ;
188 m_isPushedRightButton = FALSE;
192 KHTMLPart::khtmlMousePressEvent( e );
196 * This function is called when user clicked res anchor
198 void KitaPreviewPart::clickAnchor( const KURL& urlin )
201 KURL datURL = Kita::getDatURL( urlin, refstr );
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 );
211 if ( refstr == QString::null ) return ;
213 /*-------------------------*/
214 /* start multi-popup mdde */
215 if ( m_isPushedRightButton && startMultiPopup() ) return ;
219 int i = refstr.find( "-" );
221 refNum = refstr.left( i ).toInt();
222 refNum2 = refstr.mid( i + 1 ).toInt();
223 if ( refNum2 < refNum ) {
227 refNum = refNum2 = refstr.toInt();
230 if ( !refNum ) return ;
232 emit openURLRequestExt( urlin );
235 void KitaPreviewPart::slotDeletePopup()
237 if ( m_popup ) delete m_popup;
239 m_multiPopup = FALSE;
245 void KitaPreviewPart::showPopup( const KURL& url, const QString& innerHTML )
248 m_multiPopup = FALSE;
250 m_popup = new Kita::ResPopup( view() , url );
252 connect( m_popup, SIGNAL( hideChildPopup() ), SLOT( slotHideChildPopup() ) );
254 m_popup->setText( innerHTML );
255 m_popup->adjustSize();
256 m_popup->adjustPos( QCursor::pos() );
261 * start multi-popup mode
263 bool KitaPreviewPart::startMultiPopup()
265 if ( m_popup && m_popup->isVisible() ) {
267 m_popup->moveMouseAbove();
269 m_multiPopup = FALSE;
276 * Is it multi-popup mode now ?
278 bool KitaPreviewPart::isMultiPopupMode()
281 m_multiPopup = FALSE;
282 } else if ( m_popup->isHidden() ) {
283 m_multiPopup = FALSE;
289 void KitaPreviewPart::hidePopup()
295 m_multiPopup = FALSE;
298 void KitaPreviewPart::slotLeave()
300 if ( isMultiPopupMode() ) return ;
301 if ( view()->isHorizontalSliderPressed() ) return ;
302 if ( view()->isVerticalSliderPressed () ) return ;
307 void KitaPreviewPart::slotVSliderReleased()
309 QScrollBar* bar = view()->verticalScrollBar();
310 QRect rt = bar->sliderRect();
315 void KitaPreviewPart::slotHSliderReleased()
317 QScrollBar* bar = view()->horizontalScrollBar();
318 QRect rt = bar->sliderRect();
323 void KitaPreviewPart::slotHideChildPopup()
329 * This slot is called when mouse moves onto the URL
331 void KitaPreviewPart::slotOnURL( const QString& url )
333 const int maxpopup = 10; /* max number of responses shown in the popup window */
335 if ( isMultiPopupMode() ) return ;
339 if ( url.isEmpty() ) return ;
340 if ( url.left( 7 ) == "mailto:" ) return ;
342 /* Is Kita active now ? */
343 if ( ViewMediator::getInstance()->isKitaActive() == FALSE ) return;
347 KURL datURL = m_datURL;
348 if ( url.at( 0 ) == '#' ) {
349 refstr = url.mid( 1 );
351 datURL = Kita::getDatURL( KURL( m_datURL, url ), refstr );
354 /*-------------------------*/
355 /* popup for anchor */
357 QString innerHTML = QString::null;
361 int i = refstr.find( "-" );
362 if ( i != -1 ) { /* >>refNum-refNum2 */
364 refNum = refstr.left( i ).toInt();
365 refNum2 = refstr.mid( i + 1 ).toInt();
368 if ( refNum2 < refNum ) refNum2 = refNum;
369 if ( refNum2 - refNum > maxpopup - 1 ) refNum2 = refNum + maxpopup - 1;
372 } else { /* >>refNum */
373 refNum = refstr.toInt();
377 /* another thread ? */
378 if ( datURL.host() != m_datURL.host() || datURL.path() != m_datURL.path() ) {
381 QString boardName = Kita::BoardManager::boardName( datURL );
382 if ( boardName != QString::null ) {
383 innerHTML += "[" + boardName + "] ";
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 );
390 /* get thread Name */
391 QString subName = Kita::DatManager::threadName( datURL );
392 if ( subName != QString::null ) {
393 innerHTML += subName + "<br><br>";
396 if ( !refNum ) refNum = refNum2 = 1;
399 /* get HTML and show it */
400 if ( !refNum ) return ;
402 innerHTML += Kita::DatManager::getHtml( datURL, refNum, refNum2 );
403 if ( innerHTML != QString::null ) {
404 showPopup( datURL, innerHTML );