+ QString title = tr( "Help Browser" );
+ if ( !myWebView->title().isEmpty() ) title += QString( " [%1]" ).arg( myWebView->title() );
+ setWindowTitle( title );
+}
+
+/*
+ \brief Called when link is processed by browser
+ \param ok operation status: \c true is URL is correctly processed, \c false otherwise
+*/
+void QtxWebBrowser::finished( bool ok )
+{
+ if ( !ok && !myLastUrl.isEmpty() ) {
+ if ( isLocalFile( myLastUrl ) ) {
+ QString filename = myLastUrl.path();
+ QString extension = QFileInfo( filename ).suffix();
+ if ( extension == "html" || extension == "htm" ) return;
+ openLink( filename );
+ }
+ }
+}
+
+/*
+ \brief Called when link is processed from browser via popup menu actions
+*/
+void QtxWebBrowser::linkAction()
+{
+ QObject* s = sender();
+ if ( s == myWebView->pageAction( QWebPage::DownloadLinkToDisk ) ) {
+ saveLink( myLastUrl.path() );
+ }
+ else if ( s == myWebView->pageAction( QWebPage::OpenLink ) ) {
+ QString fileName = myLastUrl.path();
+ QString extension = QFileInfo( fileName ).suffix();
+ if ( extension != "html" && extension != "htm" ) {
+ openLink( fileName, true );
+ }
+ else {
+ linkClicked( myLastUrl );
+ }
+ }
+}
+
+/*!
+ \brief Open file
+ \param fileName link to the file being opened
+ Opens dialog box to allow the user to choose the program to be used to open the file.
+*/
+void QtxWebBrowser::openLink( const QString& fileName, bool force )
+{
+ QString extension = QFileInfo( fileName ).suffix();
+ int defAction = Downloader::mOpen;
+ bool defRepeat = false;
+ QString defProgram;
+ QString resSection = QString( "%1:%2" ).arg( "web_browser" ).arg( extension );
+ QString actionParam = "action";
+ QString programParam = "program";
+ QString repeatParam = "repeat";
+
+ if ( !extension.isEmpty() && myResourceMgr ) {
+ defAction = myResourceMgr->integerValue( resSection, actionParam, defAction );
+ defRepeat = myResourceMgr->booleanValue( resSection, repeatParam, defRepeat );
+ defProgram = myResourceMgr->stringValue( resSection, programParam, defProgram );
+ }
+
+ if ( force || !defRepeat || ( defAction == Downloader::mOpen && defProgram.isEmpty() ) ) {
+ Downloader downloader( fileName, defAction, defProgram, this );
+ if ( !downloader.exec() ) return;
+ defAction = downloader.action();
+ defRepeat = downloader.isRepeatAction();
+ defProgram = downloader.program();
+ if ( myResourceMgr ) {
+ myResourceMgr->setValue( resSection, actionParam, defAction );
+ myResourceMgr->setValue( resSection, repeatParam, defRepeat );
+ if ( defAction == Downloader::mOpen )
+ myResourceMgr->setValue( resSection, programParam, defProgram );
+ }
+ }
+ switch( defAction ) {
+ case Downloader::mOpen:
+ if ( !defProgram.isEmpty() ) {
+ QStringList parameters;
+#ifdef WIN32
+ QString cmd = defProgram;
+#else
+ // If Salome Qt version is lower than the system one, on KDE an unresolved symbol is raised
+ // In this case, we can try to launch the pdf viewer after unsetting the LD_LIBRARY_PATH environnement variable
+ QString cmd = "env";
+ parameters << "LD_LIBRARY_PATH=/usr/lib:/usr/lib64";
+ parameters << defProgram;
+#endif
+ parameters << QFileInfo( myLastUrl.path() ).absoluteFilePath();
+ QProcess::startDetached( cmd, parameters );
+ }
+ break;
+ case Downloader::mSave:
+ {
+ saveLink( fileName );
+ }
+ break;
+ default:
+ break;
+ }
+}
+
+/*!
+ \brief Load URL
+ \param url path to the file to be opened in the browser
+*/
+void QtxWebBrowser::load( const QString& url )
+{
+ QString u = url;
+ if ( !u.isEmpty() )
+ myWebView->load( QUrl( u.replace('\\', '/') ) );
+}
+
+/*!
+ \brief Save file
+ \param fileName link to the file being saved
+ Shows "Save file" standard dialog box to allow user to choose where to save the file.
+*/
+void QtxWebBrowser::saveLink( const QString& fileName )
+{
+ QString newFileName = QFileDialog::getSaveFileName( this, tr( "Save file" ), fileName,
+ QString( "*.%1" ).arg( QFileInfo( fileName ).suffix() ) );
+ if ( !newFileName.isEmpty() &&
+ QFileInfo( newFileName ).canonicalFilePath() != QFileInfo( fileName ).canonicalFilePath() ) {
+ QFile toFile( newFileName );
+ QFile fromFile( fileName );
+ if ( toFile.exists() && !toFile.remove() || !fromFile.copy( newFileName ) )
+ QMessageBox::warning( this, tr( "Error"), tr( "Can't save file:\n%1" ).arg( newFileName ) );
+ }
+}
+
+/*!
+ \brief Open file
+ Shows "Open file" standard dialog box to allow user to choose file to open.
+*/
+void QtxWebBrowser::open()
+{
+ QString url;
+ if ( isLocalFile( myWebView->url() ) ) url = myWebView->url().path();
+ url = QFileDialog::getOpenFileName( this, tr( "Open file" ), url, "HTML files (*.html *.htm);; All files (*)" );
+ if ( !url.isEmpty() ) load( url );