Salome HOME
Fix problem with the python initialization on windows.
[modules/gui.git] / src / SUITApp / SUITApp.cxx
1 // Copyright (C) 2007-2014  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 #include "Style_Salome.h"
50 #include "QtxSplash.h"
51
52 #include <QDir>
53 #include <QFile>
54 #include <QRegExp>
55 #include <QString>
56 #include <QStringList>
57
58 #include <stdlib.h>
59
60 #ifdef WIN32
61 #include <UserEnv.h>
62 #endif
63
64 static QString salomeVersion()
65 {
66   return GUI_VERSION_STR;
67 }
68
69 static QString getAppName( const QString& libName )
70 {
71   QString appName = QFileInfo( libName ).baseName();
72   if ( appName.startsWith( "lib" ) ) appName = appName.mid( 3 );
73   return appName;
74 }
75
76 // static void MessageOutput( QtMsgType type, const char* msg )
77 // {
78 //   switch ( type )
79 //   {
80 //   case QtDebugMsg:
81 // #ifdef _DEBUG_
82 //     printf( "Debug: %s\n", msg );
83 // #endif
84 //     break;
85 //   case QtWarningMsg:
86 // #ifdef _DEBUG_
87 //     printf( "Warning: %s\n", msg );
88 // #endif
89 //     break;
90 //   case QtFatalMsg:
91 // #ifdef _DEBUG_
92 //     printf( "Fatal: %s\n", msg );
93 // #endif
94 //     break;
95 //   default:
96 //     break;
97 //   }
98 // }
99
100 class SUITApp_Session: public SUIT_Session
101 {
102 public:
103   SUITApp_Session( bool theIniFormat ) : SUIT_Session(), myIniFormat ( theIniFormat ) {}
104   virtual ~SUITApp_Session() {}
105
106   virtual SUIT_ResourceMgr* createResourceMgr( const QString& appName ) const
107   {
108     SUIT_ResourceMgr* resMgr = 0;
109     if ( myIniFormat )
110     {
111       resMgr = new SUIT_ResourceMgr( appName, QString( "%1Config" ) );
112       resMgr->setCurrentFormat( "ini" );
113     }
114     else
115     {
116       resMgr = new SUIT_ResourceMgr( appName, QString( "%1Config" ) );
117       resMgr->setVersion( salomeVersion() );
118       resMgr->setCurrentFormat( "xml" );
119     }
120
121     if ( resMgr )
122     {
123       resMgr->setOption( "translators", QString( "%P_msg_%L.qm|%P_icons.qm|%P_images.qm" ) );
124     }
125     return resMgr;
126   }
127
128 private:
129   bool  myIniFormat;
130 };
131
132 int main( int argc, char* argv[] )
133 {
134   //#ifndef DISABLE_PYCONSOLE
135   //  // First of all initialize Python, as in complex multi-component applications
136   //  // someone else might initialize it some way unsuitable for light SALOME!
137   //  Py_SetProgramName( argv[0] );
138   //  Py_Initialize(); // Initialize the interpreter
139   //  PySys_SetArgv( argc,  argv );
140   //  PyEval_InitThreads(); // Create (and acquire) the interpreter lock
141   //  PyEval_ReleaseLock(); // Let the others use Python API until we need it again
142   //#endif
143
144   //qInstallMsgHandler( MessageOutput );
145
146   QStringList argList;
147   bool noExceptHandling = false;
148   bool iniFormat        = false;
149   bool noSplash         = false;
150   bool useLicense       = false;
151   for ( int i = 1; i < argc; i++ )
152   {
153     if ( !strcmp( argv[i], "--noexcepthandling" ) )
154       noExceptHandling = true;
155     else if ( !strcmp( argv[i], "--format=ini") )
156       iniFormat = true;
157     else if ( !strcmp( argv[i], "--nosplash") )
158       noSplash = true;
159     else if ( !strcmp( argv[i], "--uselicense" ) )
160       useLicense = true;
161     else
162       argList.append( QString( argv[i] ) );
163   }
164
165   // add $QTDIR/plugins to the pluins search path for image plugins
166   QString qtdir( ::getenv( "QTDIR" ) );
167   if ( !qtdir.isEmpty() )
168     QApplication::addLibraryPath( QDir( qtdir ).absoluteFilePath( "plugins" ) );
169
170   //Set a "native" graphic system in case if application runs on the remote host
171   QString remote(::getenv("REMOTEHOST"));
172   QString client(::getenv("SSH_CLIENT"));
173   if(remote.length() > 0 || client.length() > 0 ) {
174     QApplication::setGraphicsSystem(QLatin1String("native"));
175   }
176   
177   SUITApp_Application app( argc, argv );
178   QString cfgAppName = getAppName( argList.isEmpty() ? QString() : argList.first() );
179   // hard-coding for LightApp :( no other way to this for the moment
180   if ( cfgAppName == "LightApp" ) {
181     app.setOrganizationName( "salome" );
182     app.setApplicationName( "salome" );
183     app.setApplicationVersion( salomeVersion() );
184   }
185
186   int result = -1;
187
188   if ( useLicense ) {
189     QString env;
190
191 #ifdef WIN32
192     DWORD aLen=1024;
193     char aStr[1024];
194     HANDLE aToken=0;
195     HANDLE hProcess = GetCurrentProcess();
196     OpenProcessToken(hProcess,TOKEN_QUERY,&aToken);
197     if( GetUserProfileDirectory( aToken, aStr, &aLen ) )
198       env = aStr;
199
200 #else
201     if ( ::getenv( "HOME" ) )
202       env = ::getenv( "HOME" );
203 #endif
204  
205     QFile file( env + "/ReadLicense.log" ); // Read the text from a file    
206     if( !file.exists() ) {
207       SUIT_LicenseDlg aLicense;
208       if ( aLicense.exec() != QDialog::Accepted ) 
209         return result;
210     }
211   }
212
213   if ( !argList.isEmpty() )
214   {
215     SUITApp_Session* aSession = new SUITApp_Session( iniFormat );
216     QtxSplash* splash = 0;
217     SUIT_ResourceMgr* resMgr = aSession->createResourceMgr( argList.first() );
218     if ( !noSplash ) 
219     {
220       if ( resMgr )
221       {
222         resMgr->loadLanguage();
223
224         splash = QtxSplash::splash( QPixmap() );
225         splash->readSettings( resMgr );
226         if ( splash->pixmap().isNull() ) {
227           delete splash;
228           splash = 0;
229         }
230         else {
231           QString appName    = QObject::tr( "APP_NAME" ).trimmed();
232           QString appVersion = QObject::tr( "APP_VERSION" ).trimmed();
233           if ( appVersion == "APP_VERSION" )
234           {
235             if ( appName == "APP_NAME" || appName.toLower() == "salome" )
236               appVersion = salomeVersion();
237             else
238               appVersion = "";
239           }
240           splash->setOption( "%A", appName );
241           splash->setOption( "%V", QObject::tr( "ABOUT_VERSION" ).arg( appVersion ) );
242           splash->setOption( "%L", QObject::tr( "ABOUT_LICENSE" ) );
243           splash->setOption( "%C", QObject::tr( "ABOUT_COPYRIGHT" ) );
244           splash->show();
245           QApplication::instance()->processEvents();
246         }
247       }
248     }
249
250 #ifndef DISABLE_PYCONSOLE
251     //...Initialize python 
252     int   _argc   = 1;
253     char* _argv[] = {(char*)""};
254     SUIT_PYTHON::init_python(_argc,_argv);
255 #endif
256
257     SUIT_Application* theApp = aSession->startApplication( argList.first() );
258     if ( theApp )
259     {
260       Style_Salome::initialize( theApp->resourceMgr() );
261       if ( theApp->resourceMgr()->booleanValue( "Style", "use_salome_style", true ) )
262         Style_Salome::apply();
263
264       if ( !noExceptHandling )
265         app.setHandler( aSession->handler() );
266
267       if ( splash )
268         splash->finish( theApp->desktop() );
269
270       result = app.exec();
271     }
272     delete aSession;
273   }
274
275   return result;
276 }