Salome HOME
48df0f7e1647f60ccc42ef325d44a30450a14e4e
[modules/gui.git] / src / SUITApp / SUITApp.cxx
1 // Copyright (C) 2007-2016  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 //#ifdefined WIN32
24 //#ifndef DISABLE_PYCONSOLE
25 //#define DISABLE_PYCONSOLE
26 //#endif
27 //#else //#if defined WIN32
28 //#ifdef DISABLE_PYCONSOLE
29 // NOTE: DO NOT DELETE THIS DEFINITION ON LINUX
30 // or make sure Python is initialized in main() in any case
31 // Otherwise, application based on light SALOME and using Python 
32 // are unlikely to work properly.
33 //#undef DISABLE_PYCONSOLE
34 //#include <Python.h>
35 //#endif
36 //
37 #ifndef DISABLE_PYCONSOLE
38 #include "SUITApp_init_python.hxx"
39 #endif
40
41 //#endif //#if defined WIN32
42
43 #include "GUI_version.h"
44 #include "SUITApp_Application.h"
45 #include "SUIT_Desktop.h"
46 #include "SUIT_LicenseDlg.h"
47 #include "SUIT_ResourceMgr.h"
48 #include "SUIT_Session.h"
49 #ifdef USE_SALOME_STYLE
50 #include "Style_Salome.h"
51 #endif // USE_SALOME_STYLE
52 #include "QtxSplash.h"
53
54 #include <QDir>
55 #include <QFile>
56 #include <QLocale>
57 #include <QRegExp>
58 #include <QString>
59 #include <QStringList>
60
61 #include <stdlib.h>
62
63 #ifdef WIN32
64 #include <UserEnv.h>
65 #endif
66
67 static QString salomeVersion()
68 {
69   return GUI_VERSION_STR;
70 }
71
72 static QString getAppName( const QString& libName )
73 {
74   QString appName = QFileInfo( libName ).baseName();
75   if ( appName.startsWith( "lib" ) ) appName = appName.mid( 3 );
76   return appName;
77 }
78
79 // static void MessageOutput( QtMsgType type, const char* msg )
80 // {
81 //   switch ( type )
82 //   {
83 //   case QtDebugMsg:
84 // #ifdef _DEBUG_
85 //     printf( "Debug: %s\n", msg );
86 // #endif
87 //     break;
88 //   case QtWarningMsg:
89 // #ifdef _DEBUG_
90 //     printf( "Warning: %s\n", msg );
91 // #endif
92 //     break;
93 //   case QtFatalMsg:
94 // #ifdef _DEBUG_
95 //     printf( "Fatal: %s\n", msg );
96 // #endif
97 //     break;
98 //   default:
99 //     break;
100 //   }
101 // }
102
103 class SUITApp_Session: public SUIT_Session
104 {
105 public:
106   SUITApp_Session( bool theIniFormat, int argc, char** argv ) : SUIT_Session( argc, argv ), myIniFormat ( theIniFormat ) {}
107   virtual ~SUITApp_Session() {}
108
109   virtual SUIT_ResourceMgr* createResourceMgr( const QString& appName ) const
110   {
111     SUIT_ResourceMgr* resMgr = 0;
112     if ( myIniFormat )
113     {
114       resMgr = new SUIT_ResourceMgr( appName, QString( "%1Config" ) );
115       resMgr->setCurrentFormat( "ini" );
116     }
117     else
118     {
119       resMgr = new SUIT_ResourceMgr( appName, QString( "%1Config" ) );
120       resMgr->setVersion( salomeVersion() );
121       resMgr->setCurrentFormat( "xml" );
122     }
123
124     if ( resMgr )
125     {
126       resMgr->setOption( "translators", QString( "%P_msg_%L.qm|%P_icons.qm|%P_images.qm" ) );
127     }
128     return resMgr;
129   }
130
131 private:
132   bool  myIniFormat;
133 };
134
135 int main( int argc, char* argv[] )
136 {
137   //#ifndef DISABLE_PYCONSOLE
138   //  // First of all initialize Python, as in complex multi-component applications
139   //  // someone else might initialize it some way unsuitable for light SALOME!
140   //  Py_SetProgramName( argv[0] );
141   //  Py_Initialize(); // Initialize the interpreter
142   //  PySys_SetArgv( argc,  argv );
143   //  PyEval_InitThreads(); // Create (and acquire) the interpreter lock
144   //  PyEval_ReleaseLock(); // Let the others use Python API until we need it again
145   //#endif
146
147   //qInstallMsgHandler( MessageOutput );
148
149   QStringList argList;
150   bool noExceptHandling = false;
151   bool iniFormat        = false;
152   bool noSplash         = false;
153   bool useLicense       = false;
154   for ( int i = 1; i < argc; i++ )
155   {
156     if ( !strcmp( argv[i], "--noexcepthandling" ) )
157       noExceptHandling = true;
158     else if ( !strcmp( argv[i], "--format=ini") )
159       iniFormat = true;
160     else if ( !strcmp( argv[i], "--nosplash") )
161       noSplash = true;
162     else if ( !strcmp( argv[i], "--uselicense" ) )
163       useLicense = true;
164     else
165       argList.append( QString( argv[i] ) );
166   }
167
168   // set "C" locale if requested via preferences
169   {
170     SUITApp_Session stmp( iniFormat, argc, argv );
171     QApplication::setApplicationName( "salome" );
172     SUIT_ResourceMgr* resMgr = stmp.createResourceMgr( "LightApp" );
173     bool isCloc = resMgr->booleanValue( "language", "locale", true );
174     if ( isCloc ) {
175       QLocale::setDefault( QLocale::c() );
176     }
177     else {
178       QLocale::setDefault( QLocale::system() );
179     }
180   }
181
182 #if QT_VERSION > QT_VERSION_CHECK(5, 0, 0)
183   // RNV: setup the default format:
184   // QSurfaceFormat should be set before creation of QApplication,  
185   // so to avoid conflicts beetween SALOME and ParaView QSurfaceFormats we should merge theirs formats
186   // (see void Qtx::initDefaultSurfaceFormat()) and set the resultant format here.
187   Qtx::initDefaultSurfaceFormat();
188 #endif
189
190   // add <qtdir>/plugins directory to the pluins search path for image plugins
191   QString qtdir = Qtx::qtDir( "plugins" );
192   if ( !qtdir.isEmpty() )
193     QApplication::addLibraryPath( qtdir );
194
195 // TODO (QT5 PORTING) Below is a temporary solution, to allow compiling with Qt 5
196 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
197   //Set a "native" graphic system in case if application runs on the remote host
198   QString remote(::getenv("REMOTEHOST"));
199   QString client(::getenv("SSH_CLIENT"));
200   if(remote.length() > 0 || client.length() > 0 ) {
201     QApplication::setGraphicsSystem(QLatin1String("native"));
202   }
203 #endif
204   
205   SUITApp_Application app( argc, argv );
206   QString cfgAppName = getAppName( argList.isEmpty() ? QString() : argList.first() );
207   // hard-coding for LightApp :( no other way to this for the moment
208   if ( cfgAppName == "LightApp" ) {
209     app.setOrganizationName( "salome" );
210     app.setApplicationName( "salome" );
211     app.setApplicationVersion( salomeVersion() );
212   }
213
214   int result = -1;
215
216   if ( useLicense ) {
217     QString env;
218
219 #ifdef WIN32
220     DWORD aLen=1024;
221     TCHAR aStr[1024];
222     HANDLE aToken=0;
223     HANDLE hProcess = GetCurrentProcess();
224     OpenProcessToken(hProcess,TOKEN_QUERY,&aToken);
225     if( GetUserProfileDirectory( aToken, aStr, &aLen ) ) {
226 #ifdef UNICODE
227                 env = QString::fromWCharArray(aStr);
228 #else 
229                 env = aStr;
230 #endif
231         }
232
233 #else
234     if ( ::getenv( "HOME" ) )
235       env = ::getenv( "HOME" );
236 #endif
237  
238     QFile file( env + "/ReadLicense.log" ); // Read the text from a file    
239     if( !file.exists() ) {
240       SUIT_LicenseDlg aLicense;
241       if ( aLicense.exec() != QDialog::Accepted ) 
242         return result;
243     }
244   }
245
246   if ( !argList.isEmpty() )
247   {
248     SUITApp_Session aSession( iniFormat, argc, argv );
249     QtxSplash* splash = 0;
250     SUIT_ResourceMgr* resMgr = aSession.createResourceMgr( argList.first() );
251     if ( !noSplash ) 
252     {
253       if ( resMgr )
254       {
255         resMgr->loadLanguage();
256
257         splash = QtxSplash::splash( QPixmap() );
258         splash->readSettings( resMgr );
259         if ( splash->pixmap().isNull() ) {
260           delete splash;
261           splash = 0;
262         }
263         else {
264           QString appName    = QObject::tr( "APP_NAME" ).trimmed();
265           QString appVersion = QObject::tr( "APP_VERSION" ).trimmed();
266           if ( appVersion == "APP_VERSION" )
267           {
268             if ( appName == "APP_NAME" || appName.toLower() == "salome" )
269               appVersion = salomeVersion();
270             else
271               appVersion = "";
272           }
273           splash->setOption( "%A", appName );
274           splash->setOption( "%V", QObject::tr( "ABOUT_VERSION" ).arg( appVersion ) );
275           splash->setOption( "%L", QObject::tr( "ABOUT_LICENSE" ) );
276           splash->setOption( "%C", QObject::tr( "ABOUT_COPYRIGHT" ) );
277           splash->show();
278           QApplication::instance()->processEvents();
279         }
280       }
281     }
282
283 #ifndef DISABLE_PYCONSOLE
284     //...Initialize python 
285     int   _argc   = 1;
286     char* _argv[] = {(char*)""};
287     SUIT_PYTHON::init_python(_argc,_argv);
288 #endif
289
290     SUIT_Application* theApp = aSession.startApplication( argList.first() );
291     if ( theApp )
292     {
293 #ifdef USE_SALOME_STYLE
294       Style_Salome::initialize( theApp->resourceMgr() );
295       if ( theApp->resourceMgr()->booleanValue( "Style", "use_salome_style", true ) )
296         Style_Salome::apply();
297 #endif // USE_SALOME_STYLE
298
299       if ( !noExceptHandling )
300         app.setHandler( aSession.handler() );
301
302       if ( splash )
303         splash->finish( theApp->desktop() );
304
305       result = app.exec();
306     }
307   }
308
309   return result;
310 }