]> SALOME platform Git repositories - modules/gui.git/blob - src/SUITApp/SUITApp.cxx
Salome HOME
9fefe90f0813d8f3328eb86481459a7b9b49311f
[modules/gui.git] / src / SUITApp / SUITApp.cxx
1 //  Copyright (C) 2007-2008  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 #if defined WIN32
23
24 #ifdef SUIT_ENABLE_PYTHON
25 #undef SUIT_ENABLE_PYTHON
26 #endif
27
28 #else //#if defined WIN32
29
30 #ifndef SUIT_ENABLE_PYTHON
31 // NOTE: DO NOT DELETE THIS DEFINITION ON LINUX
32 // or make sure Python is initialized in main() in any case
33 // Otherwise, application based on light SALOME and using Python 
34 // are unlikely to work properly.
35 #define SUIT_ENABLE_PYTHON
36 #include <Python.h>
37 #endif
38
39 #endif //#if defined WIN32
40
41 #include "SUITApp_Application.h"
42
43 #include <SUIT_Session.h>
44 #include <SUIT_Desktop.h>
45 #include <SUIT_ResourceMgr.h>
46 #include <Style_Salome.h>
47 #include <QtxSplash.h>
48
49 #include <SUIT_LicenseDlg.h>
50
51 #include <QDir>
52 #include <QFile>
53 #include <QRegExp>
54 #include <QString>
55 #include <QStringList>
56
57 #include <stdlib.h>
58
59 #ifdef WIN32
60 #include <UserEnv.h>
61 #endif
62
63 static QString salomeVersion()
64 {
65   QString path( ::getenv( "GUI_ROOT_DIR" ) );
66   if ( !path.isEmpty() )
67     path += QDir::separator();
68
69   path += QString( "bin/salome/VERSION" );
70
71   QFile vf( path );
72   if ( !vf.open( QFile::ReadOnly ) )
73     return QString();
74
75   QString line = vf.readLine( 1024 );
76   vf.close();
77
78   if ( line.isEmpty() )
79     return QString();
80
81   while ( !line.isEmpty() && line.at( line.length() - 1 ) == QChar( '\n' ) )
82     line.remove( line.length() - 1, 1 );
83
84   QString ver;
85   int idx = line.lastIndexOf( ":" );
86   if ( idx != -1 )
87     ver = line.mid( idx + 1 ).trimmed();
88
89   return ver;
90 }
91
92 static void MessageOutput( QtMsgType type, const char* msg )
93 {
94   switch ( type )
95   {
96   case QtDebugMsg:
97 #ifdef _DEBUG_
98     printf( "Debug: %s\n", msg );
99 #endif
100     break;
101   case QtWarningMsg:
102 #ifdef _DEBUG_
103     printf( "Warning: %s\n", msg );
104 #endif
105     break;
106   case QtFatalMsg:
107 #ifdef _DEBUG_
108     printf( "Fatal: %s\n", msg );
109 #endif
110     break;
111   default:
112     break;
113   }
114 }
115
116 /* XPM */
117 static const char* pixmap_not_found_xpm[] = {
118 "16 16 3 1",
119 "       c None",
120 ".      c #000000",
121 "+      c #A80000",
122 "                ",
123 "                ",
124 "    .     .     ",
125 "   .+.   .+.    ",
126 "  .+++. .+++.   ",
127 "   .+++.+++.    ",
128 "    .+++++.     ",
129 "     .+++.      ",
130 "    .+++++.     ",
131 "   .+++.+++.    ",
132 "  .+++. .+++.   ",
133 "   .+.   .+.    ",
134 "    .     .     ",
135 "                ",
136 "                ",
137 "                "};
138
139 class SUITApp_Session: public SUIT_Session
140 {
141 public:
142   SUITApp_Session( bool theIniFormat ) : SUIT_Session(), myIniFormat ( theIniFormat ) {}
143   virtual ~SUITApp_Session() {}
144
145   virtual SUIT_ResourceMgr* createResourceMgr( const QString& appName ) const
146   {
147     SUIT_ResourceMgr* resMgr = 0;
148     if ( myIniFormat )
149     {
150       resMgr = new SUIT_ResourceMgr( appName, QString( "%1Config" ) );
151       resMgr->setCurrentFormat( "ini" );
152     }
153     else
154     {
155       resMgr = new SUIT_ResourceMgr( appName, QString( "%1Config" ) );
156       resMgr->setVersion( salomeVersion() );
157       resMgr->setCurrentFormat( "xml" );
158     }
159
160     if ( resMgr )
161     {
162       static QPixmap defaultPixmap( pixmap_not_found_xpm );
163       resMgr->setDefaultPixmap( defaultPixmap );
164       resMgr->setOption( "translators", QString( "%P_msg_%L.qm|%P_icons.qm|%P_images.qm" ) );
165     }
166     return resMgr;
167   }
168
169 private:
170   bool  myIniFormat;
171 };
172
173 int main( int argc, char* argv[] )
174 {
175 #ifdef SUIT_ENABLE_PYTHON
176   // First of all initialize Python, as in complex multi-component applications
177   // someone else might initialize it some way unsuitable for light SALOME!
178   Py_SetProgramName( argv[0] );
179   Py_Initialize(); // Initialize the interpreter
180   PySys_SetArgv( argc,  argv );
181   PyEval_InitThreads(); // Create (and acquire) the interpreter lock
182   PyEval_ReleaseLock(); // Let the others use Python API until we need it again
183 #endif
184
185   //qInstallMsgHandler( MessageOutput );
186
187   QStringList argList;
188   bool noExceptHandling = false;
189   bool iniFormat        = false;
190   bool noSplash         = false;
191   bool useLicense       = false;
192   for ( int i = 1; i < argc /*&& !noExceptHandling*/; i++ )
193   {
194     if ( !strcmp( argv[i], "--noexcepthandling" ) )
195       noExceptHandling = true;
196     else if ( !strcmp( argv[i], "--format=ini") )
197       iniFormat = true;
198     else if ( !strcmp( argv[i], "--nosplash") )
199       noSplash = true;
200         else if ( !strcmp( argv[i], "--uselicense" ) )
201       useLicense = true;
202         else
203       argList.append( QString( argv[i] ) );
204   }
205
206   SUITApp_Application app( argc, argv );
207
208   int result = -1;
209
210   if ( useLicense ) {
211     QString env;
212
213 #ifdef WIN32
214     DWORD aLen=1024;
215     char aStr[1024];
216     HANDLE aToken=0;
217     HANDLE hProcess = GetCurrentProcess();
218     OpenProcessToken(hProcess,TOKEN_QUERY,&aToken);
219     if( GetUserProfileDirectory( aToken, aStr, &aLen ) )
220       env = aStr;
221
222 #else
223     if ( ::getenv( "HOME" ) )
224       env = ::getenv( "HOME" );
225 #endif
226  
227     QFile file( env + "/ReadLicense.log" ); // Read the text from a file    
228     if( !file.exists() ) {
229       SUIT_LicenseDlg aLicense;
230       if ( aLicense.exec() != QDialog::Accepted ) 
231         return result;
232     }
233   }
234
235   if ( !argList.isEmpty() )
236   {
237     SUITApp_Session* aSession = new SUITApp_Session( iniFormat );
238     QtxSplash* splash = 0;
239     SUIT_ResourceMgr* resMgr = aSession->createResourceMgr( argList.first() );
240     if ( !noSplash ) 
241     {
242       if ( resMgr )
243       {
244         resMgr->loadLanguage( false );
245
246         splash = QtxSplash::splash( QPixmap() );
247         splash->readSettings( resMgr );
248         if ( splash->pixmap().isNull() ) {
249           delete splash;
250           splash = 0;
251         }
252         else {
253           QString appName    = QObject::tr( "APP_NAME" ).trimmed();
254           QString appVersion = QObject::tr( "APP_VERSION" ).trimmed();
255           if ( appVersion == "APP_VERSION" )
256           {
257             if ( appName == "APP_NAME" || appName.toLower() == "salome" )
258               appVersion = salomeVersion();
259             else
260               appVersion = "";
261           }
262           splash->setOption( "%A", appName );
263           splash->setOption( "%V", QObject::tr( "ABOUT_VERSION" ).arg( appVersion ) );
264           splash->setOption( "%L", QObject::tr( "ABOUT_LICENSE" ) );
265           splash->setOption( "%C", QObject::tr( "ABOUT_COPYRIGHT" ) );
266           splash->show();
267           QApplication::instance()->processEvents();
268         }
269       }
270     }
271
272     SUIT_Application* theApp = aSession->startApplication( argList.first() );
273     if ( theApp )
274     {
275       Style_Salome::initialize( theApp->resourceMgr() );
276       if ( theApp->resourceMgr()->booleanValue( "Style", "use_salome_style", true ) )
277         Style_Salome::apply();
278
279       if ( !noExceptHandling )
280         app.setHandler( aSession->handler() );
281
282       if ( splash )
283         splash->finish( theApp->desktop() );
284
285       result = app.exec();
286     }
287     delete aSession;
288   }
289
290   return result;
291 }