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