6 #include <dom/html_document.h>
7 #include <dom/html_misc.h>
9 #include <kactioncollection.h>
10 #include <kapplication.h>
12 #include <khtml_settings.h>
13 #include <khtmlview.h>
16 #include <kstandarddirs.h>
17 #include <ktoolbarpopupaction.h>
22 #include <QTextStream>
29 View::View( QWidget
*parentWidget
, QObject
*parent
, KHTMLPart::GUIProfile prof
, KActionCollection
*col
)
30 : KHTMLPart( parentWidget
, parent
, prof
), mState( Docu
), mActionCollection(col
)
32 setJScriptEnabled(false);
33 setJavaEnabled(false);
34 setPluginsEnabled(false);
36 mFormatter
= new Formatter
;
37 if ( !mFormatter
->readTemplates() ) {
38 kDebug() << "Unable to read Formatter templates.";
43 connect( this, SIGNAL( setWindowCaption( const QString
& ) ),
44 this, SLOT( setTitle( const QString
& ) ) );
45 connect( this, SIGNAL( popupMenu( const QString
&, const QPoint
& ) ),
46 this, SLOT( showMenu( const QString
&, const QPoint
& ) ) );
48 QString css
= langLookup("common/kde-default.css");
52 if (css_file
.open(QIODevice::ReadOnly
))
54 QTextStream
s(&css_file
);
55 QString stylesheet
= s
.readAll();
56 preloadStyleSheet("help:/common/kde-default.css", stylesheet
);
60 view()->installEventFilter( this );
68 void View::copySelectedText()
70 kapp
->clipboard()->setText( selectedText() );
73 bool View::openUrl( const KUrl
&url
)
75 if ( url
.protocol().toLower() == "about" )
81 return KHTMLPart::openUrl( url
);
84 void View::saveState( QDataStream
&stream
)
88 KHTMLPart::saveState( stream
);
91 void View::restoreState( QDataStream
&stream
)
95 KHTMLPart::restoreState( stream
);
96 else if ( mState
== About
)
100 void View::showAboutPage()
102 QString file
= KStandardDirs::locate( "data", "khelpcenter/intro.html.in" );
103 if ( file
.isEmpty() )
108 if ( !f
.open( QIODevice::ReadOnly
) )
117 QString res
= t
.readAll();
119 res
= res
.arg( i18n("Conquer your Desktop!") )
120 .arg( langLookup( "khelpcenter/konq.css" ) )
121 .arg( langLookup( "khelpcenter/pointers.png" ) )
122 .arg( langLookup( "khelpcenter/khelpcenter.png" ) )
123 .arg( i18n("Help Center") )
124 .arg( langLookup( "khelpcenter/lines.png" ) )
125 .arg( i18n( "Welcome to the K Desktop Environment" ) )
126 .arg( i18n( "The KDE team welcomes you to user-friendly UNIX computing" ) )
127 .arg( i18n( "KDE is a powerful graphical desktop environment for UNIX workstations. A\n"
128 "KDE desktop combines ease of use, contemporary functionality and outstanding\n"
129 "graphical design with the technological superiority of the UNIX operating\n"
131 .arg( i18n( "What is the K Desktop Environment?" ) )
132 .arg( i18n( "Contacting the KDE Project" ) )
133 .arg( i18n( "Supporting the KDE Project" ) )
134 .arg( i18n( "Useful links" ) )
135 .arg( i18n( "Getting the most out of KDE" ) )
136 .arg( i18n( "General Documentation" ) )
137 .arg( i18n( "A Quick Start Guide to the Desktop" ) )
138 .arg( i18n( "KDE Users' guide" ) )
139 .arg( i18n( "Frequently asked questions" ) )
140 .arg( i18n( "Basic Applications" ) )
141 .arg( i18n( "The Kicker Desktop Panel" ) )
142 .arg( i18n( "The KDE Control Center" ) )
143 .arg( i18n( "The Konqueror File manager and Web Browser" ) )
144 .arg( langLookup( "khelpcenter/kdelogo2.png" ) );
145 begin( KUrl( "about:khelpcenter" ) );
151 QString
View::langLookup( const QString
&fname
)
155 // assemble the local search paths
156 const QStringList localDoc
= KGlobal::dirs()->resourceDirs("html");
158 // look up the different languages
159 for (int id
=localDoc
.count()-1; id
>= 0; --id
)
161 QStringList langs
= KGlobal::locale()->languageList();
162 langs
.append( "en" );
163 langs
.removeAll( "C" );
164 QStringList::ConstIterator lang
;
165 for (lang
= langs
.begin(); lang
!= langs
.end(); ++lang
)
166 search
.append(QString("%1%2/%3").arg(localDoc
[id
]).arg(*lang
).arg(fname
));
169 // try to locate the file
170 QStringList::Iterator it
;
171 for (it
= search
.begin(); it
!= search
.end(); ++it
)
174 if (info
.exists() && info
.isFile() && info
.isReadable())
177 QString file
= (*it
).left((*it
).lastIndexOf('/')) + "/index.docbook";
179 if (info
.exists() && info
.isFile() && info
.isReadable())
186 void View::setTitle( const QString
&title
)
191 void View::beginSearchResult()
199 void View::writeSearchResult( const QString
&str
)
202 mSearchResult
+= str
;
205 void View::endSearchResult()
208 if ( !mSearchResult
.isEmpty() ) emit
searchResultCacheAvailable();
211 void View::beginInternal( const KUrl
&url
)
217 KUrl
View::internalUrl() const
222 void View::lastSearch()
224 if ( mSearchResult
.isEmpty() ) return;
229 write( mSearchResult
);
233 void View::slotIncFontSizes()
235 setZoomFactor( zoomFactor() + m_zoomStepping
);
238 void View::slotDecFontSizes()
240 setZoomFactor( zoomFactor() - m_zoomStepping
);
243 void View::showMenu( const QString
& url
, const QPoint
& pos
)
250 action
= mActionCollection
->action("go_home");
251 if (action
) pop
.addAction( action
);
255 action
= mActionCollection
->action("prevPage");
256 if (action
) pop
.addAction( action
);
257 action
= mActionCollection
->action("nextPage");
258 if (action
) pop
.addAction( action
);
262 pop
.addAction( History::self().m_backAction
);
263 pop
.addAction( History::self().m_forwardAction
);
267 QAction
*action
= pop
.addAction(i18n("Copy Link Address"));
268 connect( action
, SIGNAL( triggered() ), this, SLOT( slotCopyLink() ) );
270 mCopyURL
= completeURL(url
).url();
276 void View::slotCopyLink()
278 QApplication::clipboard()->setText(mCopyURL
);
281 bool View::prevPage(bool checkOnly
)
283 const DOM::HTMLCollection links
= htmlDocument().links();
285 // The first link on a page (top-left corner) would be the Prev link.
286 const DOM::Node prevLinkNode
= links
.item( 0 );
287 KUrl prevURL
= urlFromLinkNode( prevLinkNode
);
288 if (!prevURL
.isValid())
296 bool View::nextPage(bool checkOnly
)
298 const DOM::HTMLCollection links
= htmlDocument().links();
302 // If we're on the first page, the "Next" link is the last link
303 if ( baseURL().path().endsWith( "/index.html" ) )
304 nextURL
= urlFromLinkNode( links
.item( links
.length() - 1 ) );
306 nextURL
= urlFromLinkNode( links
.item( links
.length() - 2 ) );
308 if (!nextURL
.isValid())
311 // If we get a mail link instead of a http URL, or the next link points
312 // to an index.html page (a index.html page is always the first page
313 // there can't be a Next link pointing to it!) there's probably nowhere
314 // to go. Next link at all.
315 if ( nextURL
.protocol() == "mailto" ||
316 nextURL
.path().endsWith( "/index.html" ) )
324 bool View::eventFilter( QObject
*o
, QEvent
*e
)
326 if ( e
->type() != QEvent::KeyPress
||
327 htmlDocument().links().length() == 0 )
328 return KHTMLPart::eventFilter( o
, e
);
330 QKeyEvent
*ke
= static_cast<QKeyEvent
*>( e
);
331 if ( ke
->modifiers() & Qt::ShiftModifier
&& ke
->key() == Qt::Key_Space
) {
332 // If we're on the first page, it does not make sense to go back.
333 if ( baseURL().path().endsWith( "/index.html" ) )
334 return KHTMLPart::eventFilter( o
, e
);
336 const QScrollBar
* const scrollBar
= view()->verticalScrollBar();
337 if ( scrollBar
->value() == scrollBar
->minimum() ) {
341 } else if ( ke
->key() == Qt::Key_Space
) {
342 const QScrollBar
* const scrollBar
= view()->verticalScrollBar();
343 if ( scrollBar
->value() == scrollBar
->maximum() ) {
348 return KHTMLPart::eventFilter( o
, e
);
351 KUrl
View::urlFromLinkNode( const DOM::Node
&n
) const
353 if ( n
.isNull() || n
.nodeType() != DOM::Node::ELEMENT_NODE
)
356 DOM::Element elem
= static_cast<DOM::Element
>( n
);
358 KUrl
href ( elem
.getAttribute( "href" ).string() );
359 if ( !href
.protocol().isNull() )
362 QString path
= baseURL().path();
363 path
.truncate( path
.lastIndexOf( '/' ) + 1 );
366 KUrl url
= baseURL();
367 url
.setRef( QString() );
368 url
.setEncodedPathAndQuery( path
);
373 void View::slotReload( const KUrl
&url
)
375 const_cast<KHTMLSettings
*>( settings() )->init( KGlobal::config().data() );
376 KParts::OpenUrlArguments args
= arguments();
377 args
.setReload( true );
378 setArguments( args
);
380 openUrl( baseURL() );