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>
52 QFileInfo fi( HelpBrowser_Application::arguments().at(0) );
54 std::cout << std::endl;
55 std::cout << "SALOME Help Browser" << std::endl;
56 std::cout << std::endl;
57 std::cout << "usage: " << qUtf8Printable( fi.fileName() ) << " [options] file" << std::endl;
58 std::cout << " file is a help file to be opened" << std::endl;
59 std::cout << std::endl;
60 std::cout << "Options:" << std::endl;
61 std::cout << "-h, --help Prints this help and quits." << std::endl;
62 std::cout << "--language=LANG Use LANG language in menus." << std::endl;
63 //std::cout << "--add=APP_PID Adds PID of application into the file." << std::endl;
64 //std::cout << "--remove=APP_PID Removes PID of application from the file." << std::endl;
65 std::cout << std::endl;
69 class RaiseWindowHelper: public QDialog
72 RaiseWindowHelper( QWidget* parent ) : QDialog( parent, Qt::FramelessWindowHint )
74 setAttribute( Qt::WA_DeleteOnClose, true );
77 QTimer::singleShot( 100, this, SLOT( close() ) );
78 QTimer::singleShot( 500, parent, SLOT( setFocus() ) );
82 class HelpBrowser: public QtxWebBrowser
85 HelpBrowser() : QtxWebBrowser()
87 setAttribute( Qt::WA_DeleteOnClose, false );
92 resourceMgr()->save();
97 QFile f( ":/COPYING" );
98 f.open( QIODevice::ReadOnly );
101 info << QtxWebBrowser::tr( "%1 has been developed using %2" ).arg( QString( "SALOME %1").arg( tr( "Help Browser" ) ) ).arg( "Qt Solutions Component: Single Application." );
103 info << in.readAll().split( "\n" );
105 QMessageBox::about( this, tr( "About %1" ).arg( tr( "Help Browser" ) ),
108 void load( const QString& url )
110 if( ! url.startsWith("--remove") ) {
111 QtxWebBrowser::load( url );
112 RaiseWindowHelper* helper = new RaiseWindowHelper( this );
117 int main( int argc, char **argv )
119 // Set application name (for preferences)
120 HelpBrowser_Application::setApplicationName("salome");
122 // Specify application identifier via its name
123 QFileInfo fi( argv[0] );
125 // Create application instance
126 HelpBrowser_Application instance( fi.fileName(), argc, argv );
128 // Parse command line arguments
129 bool showHelp = false;
130 bool removeId = false;
134 QString anAppID = QString();
136 QRegExp rl( "--(.+)=(.+)" );
137 rl.setMinimal( false );
139 for ( int i = 1; i < argc; i++ )
141 QString param = argv[i];
142 if ( param == "--help" || param == "-h" ) {
145 else if ( rl.exactMatch( param ) ) {
146 QString opt = rl.cap( 1 );
147 QString val = rl.cap( 2 );
148 if ( opt == "language" )
150 else if ( opt == "add" )
152 else if ( opt == "remove" ) {
162 #if defined(WIN32) && defined(UNICODE)
163 LPWSTR *szArglist = NULL;
166 szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
167 helpfile = QString::fromWCharArray(szArglist[nArgs-1]);
168 // Free memory allocated for CommandLineToArgvW arguments.
169 LocalFree(szArglist);
172 // Show help and exit if '--help' or '-h' option has been specified via command line
179 // Create a file with an application PIDs. File will be managed by only current application
181 dirs << QDir::homePath();
182 dirs << QString( ".config" );
183 dirs << HelpBrowser_Application::applicationName();
184 dirs << QString( "HelpBrowser" );
185 QString aWatchedFile = dirs.join( QDir::separator() );
187 QFile aFile( aWatchedFile );
188 if ( instance.sendMessage( helpfile ) )
190 // Client application.
191 if ( aFile.exists() && !anAppID.isEmpty() )
193 // Update the content of monitoring file
194 if ( aFile.open( QIODevice::ReadWrite | QIODevice::Text ) )
196 QTextStream anInStream( &aFile );
197 QString aContent( anInStream.readAll() );
199 QRegExp rx("(\\d+)+");
200 QStringList anAppIDs;
204 pos = rx.indexIn( aContent, pos );
207 anAppIDs += rx.cap( 1 );
208 pos += rx.matchedLength();
214 if ( anAppIDs.contains( anAppID ) )
215 anAppIDs.removeAt( anAppIDs.indexOf( anAppID ) );
219 if ( !anAppIDs.contains( anAppID ) )
220 anAppIDs.append( anAppID );
225 QTextStream anOutStream( &aFile );
227 foreach (QString anAppId, anAppIDs )
228 anOutStream << anAppId << endl;
236 if ( !anAppID.isEmpty() )
238 // Clear file system watcher if one has have path
239 instance.clearWatcher();
241 QFileInfo wfi( aFile.fileName() );
242 if ( QDir().mkpath( wfi.absolutePath() ) && aFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
244 // Write date and time when the file was created
245 QTextStream aOutStream( &aFile );
246 aOutStream << anAppID << endl;
250 // Add file path to file system watcher
251 instance.addWatchPath( aWatchedFile );
259 QtxTranslator tqt, tsal;
260 if ( !language.isEmpty() ) {
261 if ( tqt.load( QString( "qt_%1" ).arg( language ), QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
262 instance.installTranslator( &tqt );
264 QDir appDir = HelpBrowser_Application::applicationDirPath();
265 appDir.cdUp(); appDir.cdUp();
267 if ( tsal.load( QString( "Qtx_msg_%1" ).arg( language ), appDir.filePath( "share/salome/resources/gui" ) ) )
268 instance.installTranslator( &tsal );
271 // Initialize resource manager (for preferences)
272 QtxResourceMgr* resMgr = new QtxResourceMgr( "HelpBrowser", "%1Config" );
273 resMgr->setCurrentFormat( "xml" );
274 QtxWebBrowser::setResourceMgr( resMgr );
280 // Load file specified via command line
281 if ( helpfile.isEmpty() ) {
282 QString docdir = Qtx::getenv( "DOCUMENTATION_ROOT_DIR" );
283 if ( !docdir.isEmpty() )
284 helpfile = QDir::toNativeSeparators( QString( "%1/index.html" ).arg( docdir ) );
287 if ( !helpfile.isEmpty() ) {
288 browser.load( helpfile );
291 // Finalize main window activation
292 instance.setActivationWindow( &browser );
294 QObject::connect( &instance, SIGNAL( messageReceived( QString ) ),
295 &browser, SLOT( load ( QString ) ) );
297 QObject::connect( instance.fileSysWatcher(), SIGNAL(fileChanged(const QString&)),
298 &instance, SLOT(updateWatchStatement(const QString&)));
300 return instance.exec();