Salome HOME
Porting to VTK 6.
[modules/gui.git] / src / SUITApp / SUITApp.cxx
1 // Copyright (C) 2007-2012  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.
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 //#if defined WIN32
24 //#ifdef SUIT_ENABLE_PYTHON
25 //#undef SUIT_ENABLE_PYTHON
26 //#endif
27 //#else //#if defined WIN32
28 //#ifndef SUIT_ENABLE_PYTHON
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 //#define SUIT_ENABLE_PYTHON
34 //#include <Python.h>
35 //#endif
36 //
37 #ifdef SUIT_ENABLE_PYTHON
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 /* XPM */
101 static const char* pixmap_not_found_xpm[] = {
102 "16 16 3 1",
103 "       c None",
104 ".      c #000000",
105 "+      c #A80000",
106 "                ",
107 "                ",
108 "    .     .     ",
109 "   .+.   .+.    ",
110 "  .+++. .+++.   ",
111 "   .+++.+++.    ",
112 "    .+++++.     ",
113 "     .+++.      ",
114 "    .+++++.     ",
115 "   .+++.+++.    ",
116 "  .+++. .+++.   ",
117 "   .+.   .+.    ",
118 "    .     .     ",
119 "                ",
120 "                ",
121 "                "};
122
123 class SUITApp_Session: public SUIT_Session
124 {
125 public:
126   SUITApp_Session( bool theIniFormat ) : SUIT_Session(), myIniFormat ( theIniFormat ) {}
127   virtual ~SUITApp_Session() {}
128
129   virtual SUIT_ResourceMgr* createResourceMgr( const QString& appName ) const
130   {
131     SUIT_ResourceMgr* resMgr = 0;
132     if ( myIniFormat )
133     {
134       resMgr = new SUIT_ResourceMgr( appName, QString( "%1Config" ) );
135       resMgr->setCurrentFormat( "ini" );
136     }
137     else
138     {
139       resMgr = new SUIT_ResourceMgr( appName, QString( "%1Config" ) );
140       resMgr->setVersion( salomeVersion() );
141       resMgr->setCurrentFormat( "xml" );
142     }
143
144     if ( resMgr )
145     {
146       static QPixmap defaultPixmap( pixmap_not_found_xpm );
147       resMgr->setDefaultPixmap( defaultPixmap );
148       resMgr->setOption( "translators", QString( "%P_msg_%L.qm|%P_icons.qm|%P_images.qm" ) );
149     }
150     return resMgr;
151   }
152
153 private:
154   bool  myIniFormat;
155 };
156
157 int main( int argc, char* argv[] )
158 {
159   //#ifdef SUIT_ENABLE_PYTHON
160   //  // First of all initialize Python, as in complex multi-component applications
161   //  // someone else might initialize it some way unsuitable for light SALOME!
162   //  Py_SetProgramName( argv[0] );
163   //  Py_Initialize(); // Initialize the interpreter
164   //  PySys_SetArgv( argc,  argv );
165   //  PyEval_InitThreads(); // Create (and acquire) the interpreter lock
166   //  PyEval_ReleaseLock(); // Let the others use Python API until we need it again
167   //#endif
168
169   //qInstallMsgHandler( MessageOutput );
170
171   QStringList argList;
172   bool noExceptHandling = false;
173   bool iniFormat        = false;
174   bool noSplash         = false;
175   bool useLicense       = false;
176   for ( int i = 1; i < argc; i++ )
177   {
178     if ( !strcmp( argv[i], "--noexcepthandling" ) )
179       noExceptHandling = true;
180     else if ( !strcmp( argv[i], "--format=ini") )
181       iniFormat = true;
182     else if ( !strcmp( argv[i], "--nosplash") )
183       noSplash = true;
184     else if ( !strcmp( argv[i], "--uselicense" ) )
185       useLicense = true;
186     else
187       argList.append( QString( argv[i] ) );
188   }
189
190   // add $QTDIR/plugins to the pluins search path for image plugins
191   QString qtdir( ::getenv( "QTDIR" ) );
192   if ( !qtdir.isEmpty() )
193     QApplication::addLibraryPath( QDir( qtdir ).absoluteFilePath( "plugins" ) );
194   
195   SUITApp_Application app( argc, argv );
196   QString cfgAppName = getAppName( argList.isEmpty() ? QString() : argList.first() );
197   // hard-coding for LightApp :( no other way to this for the moment
198   if ( cfgAppName == "LightApp" ) {
199     app.setOrganizationName( "salome" );
200     app.setApplicationName( "salome" );
201     app.setApplicationVersion( salomeVersion() );
202   }
203
204   int result = -1;
205
206   if ( useLicense ) {
207     QString env;
208
209 #ifdef WIN32
210     DWORD aLen=1024;
211     char aStr[1024];
212     HANDLE aToken=0;
213     HANDLE hProcess = GetCurrentProcess();
214     OpenProcessToken(hProcess,TOKEN_QUERY,&aToken);
215     if( GetUserProfileDirectory( aToken, aStr, &aLen ) )
216       env = aStr;
217
218 #else
219     if ( ::getenv( "HOME" ) )
220       env = ::getenv( "HOME" );
221 #endif
222  
223     QFile file( env + "/ReadLicense.log" ); // Read the text from a file    
224     if( !file.exists() ) {
225       SUIT_LicenseDlg aLicense;
226       if ( aLicense.exec() != QDialog::Accepted ) 
227         return result;
228     }
229   }
230
231   if ( !argList.isEmpty() )
232   {
233     SUITApp_Session* aSession = new SUITApp_Session( iniFormat );
234     QtxSplash* splash = 0;
235     SUIT_ResourceMgr* resMgr = aSession->createResourceMgr( argList.first() );
236     if ( !noSplash ) 
237     {
238       if ( resMgr )
239       {
240         resMgr->loadLanguage();
241
242         splash = QtxSplash::splash( QPixmap() );
243         splash->readSettings( resMgr );
244         if ( splash->pixmap().isNull() ) {
245           delete splash;
246           splash = 0;
247         }
248         else {
249           QString appName    = QObject::tr( "APP_NAME" ).trimmed();
250           QString appVersion = QObject::tr( "APP_VERSION" ).trimmed();
251           if ( appVersion == "APP_VERSION" )
252           {
253             if ( appName == "APP_NAME" || appName.toLower() == "salome" )
254               appVersion = salomeVersion();
255             else
256               appVersion = "";
257           }
258           splash->setOption( "%A", appName );
259           splash->setOption( "%V", QObject::tr( "ABOUT_VERSION" ).arg( appVersion ) );
260           splash->setOption( "%L", QObject::tr( "ABOUT_LICENSE" ) );
261           splash->setOption( "%C", QObject::tr( "ABOUT_COPYRIGHT" ) );
262           splash->show();
263           QApplication::instance()->processEvents();
264         }
265       }
266     }
267
268 #ifdef SUIT_ENABLE_PYTHON
269     //...Initialize python 
270     int   _argc   = 1;
271     char* _argv[] = {(char*)""};
272     SUIT_PYTHON::init_python(_argc,_argv);
273     
274     PyEval_RestoreThread( SUIT_PYTHON::_gtstate );
275     
276     if ( !SUIT_PYTHON::salome_shared_modules_module ) // import only once
277       SUIT_PYTHON::salome_shared_modules_module = PyImport_ImportModule( (char*)"salome_shared_modules" );
278     if ( !SUIT_PYTHON::salome_shared_modules_module ) 
279       PyErr_Print();
280     
281     PyEval_ReleaseThread( SUIT_PYTHON::_gtstate );
282
283 #endif
284
285     SUIT_Application* theApp = aSession->startApplication( argList.first() );
286     if ( theApp )
287     {
288       Style_Salome::initialize( theApp->resourceMgr() );
289       if ( theApp->resourceMgr()->booleanValue( "Style", "use_salome_style", true ) )
290         Style_Salome::apply();
291
292       if ( !noExceptHandling )
293         app.setHandler( aSession->handler() );
294
295       if ( splash )
296         splash->finish( theApp->desktop() );
297
298       result = app.exec();
299     }
300     delete aSession;
301   }
302
303   return result;
304 }