Salome HOME
refs #207 - "Help" button doesn't work
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_RunBrowser.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <HYDROGUI_RunBrowser.h>
24
25 #include <QFileInfo>
26 #include <QProcess>
27 #include <QApplication>
28 #include <QDir>
29
30 #include <LightApp_Application.h>
31
32 #include <QtxWebBrowser.h>
33 #include <SUIT_MessageBox.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_Desktop.h>
36
37 #include <SALOME_Event.h>
38
39 HYDROGUI_RunBrowser::HYDROGUI_RunBrowser( LightApp_Application* app,
40                                           const QString&        theApp,
41                                           const QString&        theParams,
42                                           const QString&        theHelpFile,
43                                           const QString&        theContext )
44 : myApp( theApp ),
45   myParams( theParams ),
46   myContext( theContext ),
47   myStatus(0),
48   myLApp( app )
49 {
50   //For the external browser always specify 'file://' protocol,
51   //because some WEB browsers (for example Mozilla Firefox) can't open local file without protocol.
52   myHelpFile = QString("file://%1").arg( QFileInfo( theHelpFile ).canonicalFilePath() );
53 }
54
55 void HYDROGUI_RunBrowser::run()
56 {
57   if ( !myApp.isEmpty() && !myHelpFile.isEmpty()) {
58     QString aCommand = QString( "%1 %2 \"%3%4\"" ).arg( myApp, myParams, myHelpFile, myContext.isEmpty() ? QString("") : QString( "#%1" ).arg( myContext ) );
59
60     QProcess* proc = new QProcess();
61
62     //proc->start( aCommand );
63     if ( true || !proc->waitForStarted() ) {
64       SALOME_CustomEvent* ce2000 = new SALOME_CustomEvent( 2000 );
65       QString* msg = new QString( QObject::tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).arg( myApp, myHelpFile ) );
66       ce2000->setData( msg );
67       QApplication::postEvent( myLApp, ce2000 );
68     }
69   }
70 }
71
72 void HYDROGUI_RunBrowser::showPage( LightApp_Application* theApp,
73                                     const QString& thePage )
74 {
75   SUIT_ResourceMgr* resMgr = theApp->resourceMgr();
76   QString platform;
77 #ifdef WIN32
78   platform = "winapplication";
79 #else
80   platform = "application";
81 #endif
82   QString anApp = resMgr->stringValue( "ExternalBrowser", platform );
83 #ifdef WIN32
84   QString quote( "\"" );
85   anApp.prepend( quote );
86   anApp.append( quote );
87 #endif
88   QString aParams = resMgr->stringValue( "ExternalBrowser", "parameters" );
89   bool useExtBrowser = resMgr->booleanValue( "ExternalBrowser", "use_external_browser", false );
90
91   if( useExtBrowser ) {
92     if ( !anApp.isEmpty() ) {
93       HYDROGUI_RunBrowser* rs = new HYDROGUI_RunBrowser( theApp, anApp, aParams, thePage );
94       rs->start();
95     }
96     else
97       SUIT_MessageBox::warning(theApp->desktop(), tr( "WRN_WARNING" ), tr( "WRN_DEFINE_EXTERNAL_BROWSER" ));
98   }
99   else {
100 #ifdef WIN32
101     // On Win32 platform QWebKit of the Qt 4.6.3 hang up in case 'file://' protocol 
102     // is defined. On Linux platform QWebKit doesn't work correctly without 'file://' protocol.
103     QtxWebBrowser::loadUrl( thePage );
104 #else
105     QtxWebBrowser::loadUrl( QString( "file://%1" ).arg( helpFile ) );
106 #endif
107   }
108 }
109
110 QString HYDROGUI_RunBrowser::getPagePath( const QString& theModuleName )
111 {
112   QString aPagePath;
113
114   QString rootDir = QString( "%1_ROOT_DIR" ).arg( theModuleName ); // module root dir variable
115   QString modDir  = getenv( rootDir.toLatin1().constData() );      // module root dir
116   if ( !modDir.isEmpty() ) {
117     QStringList idxLst = QStringList() << modDir << "share" << "doc" << "salome"
118                                        << "gui" << theModuleName;
119     QString indexFile = idxLst.join( QDir::separator() );          // index file
120     if ( QFile::exists( indexFile ) )
121       aPagePath = indexFile;
122   }
123   return aPagePath;
124 }