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