1 // Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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, or (at your option) any later version.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : HelpBrowser.cxx
24 // Author : Vadim SANDLER, OpenCASCADE S.A.S. (vadim.sandler@opencascade.com)
25 // Maxim GLIBIN, OpenCASCADE S.A.S. (maxim.glibin@opencascade.com)
27 #include "HelpBrowser_Application.h"
32 #include <QFileSystemWatcher>
33 #include <QLibraryInfo>
34 #include <QMessageBox>
35 #include <QTextStream>
37 #include <QtxResourceMgr.h>
38 #include <QtxTranslator.h>
39 #include <QtxWebBrowser.h>
47 QFileInfo fi( HelpBrowser_Application::arguments().at(0) );
49 std::cout << std::endl;
50 std::cout << "SALOME Help Browser" << std::endl;
51 std::cout << std::endl;
52 std::cout << "usage: " << qUtf8Printable( fi.fileName() ) << " [options] file" << std::endl;
53 std::cout << " file is a help file to be opened" << std::endl;
54 std::cout << std::endl;
55 std::cout << "Options:" << std::endl;
56 std::cout << "-h, --help Prints this help and quits." << std::endl;
57 std::cout << "--language=LANG Use LANG language in menus." << std::endl;
58 //std::cout << "--add=APP_PID Adds PID of application into the file." << std::endl;
59 //std::cout << "--remove=APP_PID Removes PID of application from the file." << std::endl;
60 std::cout << std::endl;
64 class RaiseWindowHelper: public QDialog
67 RaiseWindowHelper( QWidget* parent ) : QDialog( parent, Qt::FramelessWindowHint )
69 setAttribute( Qt::WA_DeleteOnClose, true );
72 QTimer::singleShot( 100, this, SLOT( close() ) );
73 QTimer::singleShot( 500, parent, SLOT( setFocus() ) );
77 class HelpBrowser: public QtxWebBrowser
80 HelpBrowser() : QtxWebBrowser()
82 setAttribute( Qt::WA_DeleteOnClose, false );
87 resourceMgr()->save();
92 QFile f( ":/COPYING" );
93 f.open( QIODevice::ReadOnly );
96 info << QtxWebBrowser::tr( "%1 has been developed using %2" ).arg( QString( "SALOME %1").arg( tr( "Help Browser" ) ) ).arg( "Qt Solutions Component: Single Application." );
98 info << in.readAll().split( "\n" );
100 QMessageBox::about( this, tr( "About %1" ).arg( tr( "Help Browser" ) ),
103 void load( const QString& url )
105 QtxWebBrowser::load( url );
106 RaiseWindowHelper* helper = new RaiseWindowHelper( this );
110 int main( int argc, char **argv )
112 // Set application name (for preferences)
113 HelpBrowser_Application::setApplicationName( "salome" );
115 // Specify application identifier via its name
116 QFileInfo fi( argv[0] );
118 // Create application instance
119 HelpBrowser_Application instance( fi.fileName(), argc, argv );
121 // Parse command line arguments
122 bool showHelp = false;
123 bool removeId = false;
127 QString anAppID = QString();
129 QRegExp rl( "--(.+)=(.+)" );
130 rl.setMinimal( false );
132 for ( int i = 1; i < argc; i++ )
134 QString param = argv[i];
135 if ( param == "--help" || param == "-h" ) {
138 else if ( rl.exactMatch( param ) ) {
139 QString opt = rl.cap( 1 );
140 QString val = rl.cap( 2 );
141 if ( opt == "language" )
143 else if ( opt == "add" )
145 else if ( opt == "remove" ) {
155 // Show help and exit if '--help' or '-h' option has been specified via command line
162 // Create a file with an application PIDs. File will be managed by only current application
164 dirs << QDir::homePath();
165 dirs << QString( ".config" );
166 dirs << HelpBrowser_Application::applicationName();
167 dirs << QString( "HelpBrowser" );
168 QString aWatchedFile = dirs.join( QDir::separator() );
170 QFile aFile( aWatchedFile );
171 if ( instance.sendMessage( helpfile ) )
173 // Client application.
174 if ( aFile.exists() && !anAppID.isEmpty() )
176 // Update the content of monitoring file
177 if ( aFile.open( QIODevice::ReadWrite | QIODevice::Text ) )
179 QTextStream anInStream( &aFile );
180 QString aContent( anInStream.readAll() );
182 QRegExp rx("(\\d+)+");
183 QStringList anAppIDs;
187 pos = rx.indexIn( aContent, pos );
190 anAppIDs += rx.cap( 1 );
191 pos += rx.matchedLength();
197 if ( anAppIDs.contains( anAppID ) )
198 anAppIDs.removeAt( anAppIDs.indexOf( anAppID ) );
202 if ( !anAppIDs.contains( anAppID ) )
203 anAppIDs.append( anAppID );
208 QTextStream anOutStream( &aFile );
210 foreach (QString anAppId, anAppIDs )
211 anOutStream << anAppId << endl;
219 if ( !anAppID.isEmpty() )
221 // Clear file system watcher if one has have path
222 instance.clearWatcher();
224 QFileInfo wfi( aFile.fileName() );
225 if ( QDir().mkpath( wfi.absolutePath() ) && aFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
227 // Write date and time when the file was created
228 QTextStream aOutStream( &aFile );
229 aOutStream << anAppID << endl;
233 // Add file path to file system watcher
234 instance.addWatchPath( aWatchedFile );
242 QtxTranslator tqt, tsal;
243 if ( !language.isEmpty() ) {
244 if ( tqt.load( QString( "qt_%1" ).arg( language ), QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
245 instance.installTranslator( &tqt );
247 QDir appDir = HelpBrowser_Application::applicationDirPath();
248 appDir.cdUp(); appDir.cdUp();
250 if ( tsal.load( QString( "Qtx_msg_%1" ).arg( language ), appDir.filePath( "share/salome/resources/gui" ) ) )
251 instance.installTranslator( &tsal );
254 // Initialize resource manager (for preferences)
255 QtxResourceMgr* resMgr = new QtxResourceMgr( "HelpBrowser", "%1Config" );
256 resMgr->setCurrentFormat( "xml" );
257 QtxWebBrowser::setResourceMgr( resMgr );
263 // Load file specified via command line
264 if ( helpfile.isEmpty() ) {
265 QString docdir = qgetenv( "DOCUMENTATION_ROOT_DIR" );
266 if ( !docdir.isEmpty() )
267 helpfile = QDir::toNativeSeparators( QString( "%1/index.html" ).arg( docdir ) );
270 if ( !helpfile.isEmpty() ) {
271 browser.load( helpfile );
274 // Finalize main window activation
275 instance.setActivationWindow( &browser );
277 QObject::connect( &instance, SIGNAL( messageReceived( QString ) ),
278 &browser, SLOT( load ( QString ) ) );
280 QObject::connect( instance.fileSysWatcher(), SIGNAL(fileChanged(const QString&)),
281 &instance, SLOT(updateWatchStatement(const QString&)));
283 return instance.exec();