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