Salome HOME
Run SALOME with UNICODE path.
[modules/gui.git] / src / HelpBrowser / HelpBrowser.cxx
1 // Copyright (C) 2007-2019  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, or (at your option) any later version.
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 // 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)
26
27 #include "HelpBrowser_Application.h"
28
29 #include <QDir>
30 #include <QFile>
31 #include <QFileInfo>
32 #include <QFileSystemWatcher>
33 #include <QLibraryInfo>
34 #include <QMessageBox>
35 #include <QTextStream>
36 #include <QTimer>
37 #include <QtxResourceMgr.h>
38 #include <QtxTranslator.h>
39 #include <QtxWebBrowser.h>
40
41 #include <iostream>
42
43 #ifdef WIN32
44 #include <windows.h>
45 #endif
46
47
48 namespace
49 {
50   void printHelp()
51   {
52     QFileInfo fi( HelpBrowser_Application::arguments().at(0) );
53
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;
66   }
67 }
68
69 class RaiseWindowHelper: public QDialog
70 {
71 public:
72   RaiseWindowHelper( QWidget* parent ) : QDialog( parent, Qt::FramelessWindowHint )
73   {
74     setAttribute( Qt::WA_DeleteOnClose, true );
75     resize( 1, 1 );
76     show();
77     QTimer::singleShot( 100, this, SLOT( close() ) );
78     QTimer::singleShot( 500, parent, SLOT( setFocus() ) );
79   }
80 };
81
82 class HelpBrowser: public QtxWebBrowser
83 {
84 public:
85   HelpBrowser() : QtxWebBrowser()
86   {
87     setAttribute( Qt::WA_DeleteOnClose, false );
88   }
89   ~HelpBrowser()
90   {
91     if ( resourceMgr() )
92       resourceMgr()->save();
93   }
94   void about()
95   {
96     QStringList info;
97     QFile f( ":/COPYING" );
98     f.open( QIODevice::ReadOnly );
99     QTextStream in( &f );
100
101     info << QtxWebBrowser::tr( "%1 has been developed using %2" ).arg( QString( "SALOME %1").arg( tr( "Help Browser" ) ) ).arg( "Qt Solutions Component: Single Application." );
102     info << "";
103     info << in.readAll().split( "\n" );
104
105     QMessageBox::about( this, tr( "About %1" ).arg( tr( "Help Browser" ) ),
106       info.join( "\n" ) );
107   }
108   void load( const QString& url )
109   {
110     QtxWebBrowser::load( url );
111     RaiseWindowHelper* helper = new RaiseWindowHelper( this );
112   }
113 };
114
115 int main( int argc, char **argv )
116 {
117   // Set application name (for preferences)
118   HelpBrowser_Application::setApplicationName( "salome" );
119
120   // Specify application identifier via its name
121   QFileInfo fi( argv[0] );
122
123   // Create application instance
124   HelpBrowser_Application instance( fi.fileName(), argc, argv );
125
126   // Parse command line arguments
127   bool showHelp  = false;
128   bool removeId  = false;
129
130   QString language;
131   QString helpfile;
132   QString anAppID = QString();
133
134   QRegExp rl( "--(.+)=(.+)" );
135   rl.setMinimal( false );
136
137   for ( int i = 1; i < argc; i++ )
138   {
139     QString param = argv[i];
140     if ( param == "--help" || param == "-h" ) {
141       showHelp = true;
142     }
143     else if ( rl.exactMatch( param ) ) {
144       QString opt = rl.cap( 1 );
145       QString val = rl.cap( 2 );
146       if ( opt == "language" )
147         language = val;
148       else if ( opt == "add" )
149         anAppID = val;
150       else if ( opt == "remove" ) {
151         anAppID = val;
152         removeId = true;
153       }
154     }
155     else {
156       helpfile = param;
157     }
158   }
159
160 #if defined(WIN32) && defined(UNICODE)                   
161   LPWSTR *szArglist = NULL;
162   int nArgs;
163   int i;
164   szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);          
165   helpfile = QString::fromWCharArray(szArglist[nArgs-1]);
166   // Free memory allocated for CommandLineToArgvW arguments.
167   LocalFree(szArglist);
168 #endif
169
170   // Show help and exit if '--help' or '-h' option has been specified via command line
171   if ( showHelp )
172   {
173     printHelp();
174     exit( 0 );
175   }
176
177   // Create a file with an application PIDs. File will be managed by only current application
178   QStringList dirs;
179   dirs << QDir::homePath();
180   dirs << QString( ".config" );
181   dirs << HelpBrowser_Application::applicationName();
182   dirs << QString( "HelpBrowser" );
183   QString aWatchedFile = dirs.join( QDir::separator() );
184
185   QFile aFile( aWatchedFile );
186   if ( instance.sendMessage( helpfile ) )
187   {
188     // Client application.
189     if ( aFile.exists() && !anAppID.isEmpty() )
190     {
191       // Update the content of monitoring file
192       if ( aFile.open( QIODevice::ReadWrite | QIODevice::Text ) )
193       {
194         QTextStream anInStream( &aFile );
195         QString aContent( anInStream.readAll() );
196
197         QRegExp rx("(\\d+)+");
198         QStringList anAppIDs;
199         int pos = 0;
200         while ( pos >= 0 )
201         {
202           pos = rx.indexIn( aContent, pos );
203           if ( pos >= 0 )
204           {
205             anAppIDs += rx.cap( 1 );
206             pos += rx.matchedLength();
207           }
208         }
209
210         if ( removeId )
211         {
212           if ( anAppIDs.contains( anAppID ) )
213             anAppIDs.removeAt( anAppIDs.indexOf( anAppID ) );
214         }
215         else
216         {
217           if ( !anAppIDs.contains( anAppID ) )
218             anAppIDs.append( anAppID );
219         }
220
221         aFile.resize( 0 );
222
223         QTextStream anOutStream( &aFile );
224         
225         foreach (QString anAppId, anAppIDs )
226           anOutStream << anAppId << endl;
227         aFile.close();
228       }
229     }
230     return 0;
231   }
232   else
233   {
234     if ( !anAppID.isEmpty() )
235     {
236       // Clear file system watcher if one has have path
237       instance.clearWatcher();
238
239       QFileInfo wfi( aFile.fileName() );
240       if ( QDir().mkpath( wfi.absolutePath() ) && aFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
241       {
242         // Write date and time when the file was created
243         QTextStream aOutStream( &aFile );
244         aOutStream << anAppID << endl;
245         aFile.close();
246       }
247
248       // Add file path to file system watcher
249       instance.addWatchPath( aWatchedFile );
250     }
251   }
252
253   if ( removeId )
254     return 0;
255
256   // Load translations
257   QtxTranslator tqt, tsal;
258   if ( !language.isEmpty() ) {
259     if ( tqt.load( QString( "qt_%1" ).arg( language ), QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
260       instance.installTranslator( &tqt );
261
262     QDir appDir = HelpBrowser_Application::applicationDirPath();
263     appDir.cdUp(); appDir.cdUp();
264
265     if ( tsal.load( QString( "Qtx_msg_%1" ).arg( language ), appDir.filePath( "share/salome/resources/gui" ) ) )
266       instance.installTranslator( &tsal );
267   }
268
269   // Initialize resource manager (for preferences)
270   QtxResourceMgr* resMgr = new QtxResourceMgr( "HelpBrowser", "%1Config" );
271   resMgr->setCurrentFormat( "xml" );
272   QtxWebBrowser::setResourceMgr( resMgr );
273
274   // Show main window
275   HelpBrowser browser;
276   browser.show();
277
278   // Load file specified via command line
279   if ( helpfile.isEmpty() ) {
280     QString docdir = Qtx::getenv( "DOCUMENTATION_ROOT_DIR" );
281     if ( !docdir.isEmpty() )
282       helpfile = QDir::toNativeSeparators( QString( "%1/index.html" ).arg( docdir ) );
283   }
284
285   if ( !helpfile.isEmpty() ) {
286     browser.load( helpfile );
287   }
288
289   // Finalize main window activation
290   instance.setActivationWindow( &browser );
291
292   QObject::connect( &instance, SIGNAL( messageReceived( QString ) ),
293                     &browser,  SLOT( load ( QString ) ) );
294
295   QObject::connect( instance.fileSysWatcher(), SIGNAL(fileChanged(const QString&)),
296                     &instance, SLOT(updateWatchStatement(const QString&)));
297
298   return instance.exec();
299 }