Salome HOME
a9237a271beb7f0cb13f4d3c1051e735ffe05d0d
[modules/gui.git] / src / HelpBrowser / HelpBrowser.cxx
1 // Copyright (C) 2007-2023  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         if( ! url.startsWith("--remove") ) {
111           QtxWebBrowser::load( url );
112       RaiseWindowHelper* helper = new RaiseWindowHelper( this );
113         }
114   }
115 };
116
117 int main( int argc, char **argv )
118 {
119   // Set application name (for preferences)
120   HelpBrowser_Application::setApplicationName("salome");
121
122   // Specify application identifier via its name
123   QFileInfo fi( argv[0] );
124
125   // Create application instance
126   HelpBrowser_Application instance( fi.fileName(), argc, argv );
127
128   // Parse command line arguments
129   bool showHelp  = false;
130   bool removeId  = false;
131
132   QString language;
133   QString helpfile;
134   QString anAppID = QString();
135
136   QRegExp rl( "--(.+)=(.+)" );
137   rl.setMinimal( false );
138
139   for ( int i = 1; i < argc; i++ )
140   {
141     QString param = argv[i];
142     if ( param == "--help" || param == "-h" ) {
143       showHelp = true;
144     }
145     else if ( rl.exactMatch( param ) ) {
146       QString opt = rl.cap( 1 );
147       QString val = rl.cap( 2 );
148       if ( opt == "language" )
149         language = val;
150       else if ( opt == "add" )
151         anAppID = val;
152       else if ( opt == "remove" ) {
153         anAppID = val;
154         removeId = true;
155       }
156     }
157     else {
158       helpfile = param;
159     }
160   }
161
162 #if defined(WIN32) && defined(UNICODE)                   
163   LPWSTR *szArglist = NULL;
164   int nArgs;
165   szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);          
166   helpfile = QString::fromWCharArray(szArglist[nArgs-1]);
167   // Free memory allocated for CommandLineToArgvW arguments.
168   LocalFree(szArglist);
169 #endif
170
171   // Show help and exit if '--help' or '-h' option has been specified via command line
172   if ( showHelp )
173   {
174     printHelp();
175     exit( 0 );
176   }
177
178   // Create a file with an application PIDs. File will be managed by only current application
179   QStringList dirs;
180   dirs << QDir::homePath();
181   dirs << QString( ".config" );
182   dirs << HelpBrowser_Application::applicationName();
183   dirs << QString( "HelpBrowser" );
184   QString aWatchedFile = dirs.join( QDir::separator() );
185
186   QFile aFile( aWatchedFile );
187   if ( instance.sendMessage( helpfile ) )
188   {
189     // Client application.
190     if ( aFile.exists() && !anAppID.isEmpty() )
191     {
192       // Update the content of monitoring file
193       if ( aFile.open( QIODevice::ReadWrite | QIODevice::Text ) )
194       {
195         QTextStream anInStream( &aFile );
196         QString aContent( anInStream.readAll() );
197
198         QRegExp rx("(\\d+)+");
199         QStringList anAppIDs;
200         int pos = 0;
201         while ( pos >= 0 )
202         {
203           pos = rx.indexIn( aContent, pos );
204           if ( pos >= 0 )
205           {
206             anAppIDs += rx.cap( 1 );
207             pos += rx.matchedLength();
208           }
209         }
210
211         if ( removeId )
212         {
213           if ( anAppIDs.contains( anAppID ) )
214             anAppIDs.removeAt( anAppIDs.indexOf( anAppID ) );
215         }
216         else
217         {
218           if ( !anAppIDs.contains( anAppID ) )
219             anAppIDs.append( anAppID );
220         }
221
222         aFile.resize( 0 );
223
224         QTextStream anOutStream( &aFile );
225         
226         foreach (QString anAppId, anAppIDs )
227           anOutStream << anAppId << endl;
228         aFile.close();
229       }
230     }
231     return 0;
232   }
233   else
234   {
235     if ( !anAppID.isEmpty() )
236     {
237       // Clear file system watcher if one has have path
238       instance.clearWatcher();
239
240       QFileInfo wfi( aFile.fileName() );
241       if ( QDir().mkpath( wfi.absolutePath() ) && aFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
242       {
243         // Write date and time when the file was created
244         QTextStream aOutStream( &aFile );
245         aOutStream << anAppID << endl;
246         aFile.close();
247       }
248
249       // Add file path to file system watcher
250       instance.addWatchPath( aWatchedFile );
251     }
252   }
253
254   if ( removeId )
255     return 0;
256
257   // Load translations
258   QtxTranslator tqt, tsal;
259   if ( !language.isEmpty() ) {
260     if ( tqt.load( QString( "qt_%1" ).arg( language ), QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
261       instance.installTranslator( &tqt );
262
263     QDir appDir = HelpBrowser_Application::applicationDirPath();
264     appDir.cdUp(); appDir.cdUp();
265
266     if ( tsal.load( QString( "Qtx_msg_%1" ).arg( language ), appDir.filePath( "share/salome/resources/gui" ) ) )
267       instance.installTranslator( &tsal );
268   }
269
270   // Initialize resource manager (for preferences)
271   QtxResourceMgr* resMgr = new QtxResourceMgr( "HelpBrowser", "%1Config" );
272   resMgr->setCurrentFormat( "xml" );
273   QtxWebBrowser::setResourceMgr( resMgr );
274
275   // Show main window
276   HelpBrowser browser;
277   browser.show();
278
279   // Load file specified via command line
280   if ( helpfile.isEmpty() ) {
281     QString docdir = Qtx::getenv( "DOCUMENTATION_ROOT_DIR" );
282     if ( !docdir.isEmpty() )
283       helpfile = QDir::toNativeSeparators( QString( "%1/index.html" ).arg( docdir ) );
284   }
285
286   if ( !helpfile.isEmpty() ) {
287     browser.load( helpfile );
288   }
289
290   // Finalize main window activation
291   instance.setActivationWindow( &browser );
292
293   QObject::connect( &instance, SIGNAL( messageReceived( QString ) ),
294                     &browser,  SLOT( load ( QString ) ) );
295
296   QObject::connect( instance.fileSysWatcher(), SIGNAL(fileChanged(const QString&)),
297                     &instance, SLOT(updateWatchStatement(const QString&)));
298
299   return instance.exec();
300 }