Back to index

salome-gui  6.5.0
SUITApp.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //#if defined WIN32
00024 //#ifdef SUIT_ENABLE_PYTHON
00025 //#undef SUIT_ENABLE_PYTHON
00026 //#endif
00027 //#else //#if defined WIN32
00028 //#ifndef SUIT_ENABLE_PYTHON
00029 // NOTE: DO NOT DELETE THIS DEFINITION ON LINUX
00030 // or make sure Python is initialized in main() in any case
00031 // Otherwise, application based on light SALOME and using Python 
00032 // are unlikely to work properly.
00033 //#define SUIT_ENABLE_PYTHON
00034 //#include <Python.h>
00035 //#endif
00036 //
00037 #ifdef SUIT_ENABLE_PYTHON
00038 #include "SUITApp_init_python.hxx"
00039 #endif
00040 
00041 //#endif //#if defined WIN32
00042 
00043 #include "GUI_version.h"
00044 #include "SUITApp_Application.h"
00045 #include "SUIT_Desktop.h"
00046 #include "SUIT_LicenseDlg.h"
00047 #include "SUIT_ResourceMgr.h"
00048 #include "SUIT_Session.h"
00049 #include "Style_Salome.h"
00050 #include "QtxSplash.h"
00051 
00052 #include <QDir>
00053 #include <QFile>
00054 #include <QRegExp>
00055 #include <QString>
00056 #include <QStringList>
00057 
00058 #include <stdlib.h>
00059 
00060 #ifdef WIN32
00061 #include <UserEnv.h>
00062 #endif
00063 
00064 static QString salomeVersion()
00065 {
00066   return GUI_VERSION_STR;
00067 }
00068 
00069 static QString getAppName( const QString& libName )
00070 {
00071   QString appName = QFileInfo( libName ).baseName();
00072   if ( appName.startsWith( "lib" ) ) appName = appName.mid( 3 );
00073   return appName;
00074 }
00075 
00076 // static void MessageOutput( QtMsgType type, const char* msg )
00077 // {
00078 //   switch ( type )
00079 //   {
00080 //   case QtDebugMsg:
00081 // #ifdef _DEBUG_
00082 //     printf( "Debug: %s\n", msg );
00083 // #endif
00084 //     break;
00085 //   case QtWarningMsg:
00086 // #ifdef _DEBUG_
00087 //     printf( "Warning: %s\n", msg );
00088 // #endif
00089 //     break;
00090 //   case QtFatalMsg:
00091 // #ifdef _DEBUG_
00092 //     printf( "Fatal: %s\n", msg );
00093 // #endif
00094 //     break;
00095 //   default:
00096 //     break;
00097 //   }
00098 // }
00099 
00100 /* XPM */
00101 static const char* pixmap_not_found_xpm[] = {
00102 "16 16 3 1",
00103 "       c None",
00104 ".      c #000000",
00105 "+      c #A80000",
00106 "                ",
00107 "                ",
00108 "    .     .     ",
00109 "   .+.   .+.    ",
00110 "  .+++. .+++.   ",
00111 "   .+++.+++.    ",
00112 "    .+++++.     ",
00113 "     .+++.      ",
00114 "    .+++++.     ",
00115 "   .+++.+++.    ",
00116 "  .+++. .+++.   ",
00117 "   .+.   .+.    ",
00118 "    .     .     ",
00119 "                ",
00120 "                ",
00121 "                "};
00122 
00123 class SUITApp_Session: public SUIT_Session
00124 {
00125 public:
00126   SUITApp_Session( bool theIniFormat ) : SUIT_Session(), myIniFormat ( theIniFormat ) {}
00127   virtual ~SUITApp_Session() {}
00128 
00129   virtual SUIT_ResourceMgr* createResourceMgr( const QString& appName ) const
00130   {
00131     SUIT_ResourceMgr* resMgr = 0;
00132     if ( myIniFormat )
00133     {
00134       resMgr = new SUIT_ResourceMgr( appName, QString( "%1Config" ) );
00135       resMgr->setCurrentFormat( "ini" );
00136     }
00137     else
00138     {
00139       resMgr = new SUIT_ResourceMgr( appName, QString( "%1Config" ) );
00140       resMgr->setVersion( salomeVersion() );
00141       resMgr->setCurrentFormat( "xml" );
00142     }
00143 
00144     if ( resMgr )
00145     {
00146       static QPixmap defaultPixmap( pixmap_not_found_xpm );
00147       resMgr->setDefaultPixmap( defaultPixmap );
00148       resMgr->setOption( "translators", QString( "%P_msg_%L.qm|%P_icons.qm|%P_images.qm" ) );
00149     }
00150     return resMgr;
00151   }
00152 
00153 private:
00154   bool  myIniFormat;
00155 };
00156 
00157 int main( int argc, char* argv[] )
00158 {
00159   //#ifdef SUIT_ENABLE_PYTHON
00160   //  // First of all initialize Python, as in complex multi-component applications
00161   //  // someone else might initialize it some way unsuitable for light SALOME!
00162   //  Py_SetProgramName( argv[0] );
00163   //  Py_Initialize(); // Initialize the interpreter
00164   //  PySys_SetArgv( argc,  argv );
00165   //  PyEval_InitThreads(); // Create (and acquire) the interpreter lock
00166   //  PyEval_ReleaseLock(); // Let the others use Python API until we need it again
00167   //#endif
00168 
00169   //qInstallMsgHandler( MessageOutput );
00170 
00171   QStringList argList;
00172   bool noExceptHandling = false;
00173   bool iniFormat        = false;
00174   bool noSplash         = false;
00175   bool useLicense       = false;
00176   for ( int i = 1; i < argc; i++ )
00177   {
00178     if ( !strcmp( argv[i], "--noexcepthandling" ) )
00179       noExceptHandling = true;
00180     else if ( !strcmp( argv[i], "--format=ini") )
00181       iniFormat = true;
00182     else if ( !strcmp( argv[i], "--nosplash") )
00183       noSplash = true;
00184     else if ( !strcmp( argv[i], "--uselicense" ) )
00185       useLicense = true;
00186     else
00187       argList.append( QString( argv[i] ) );
00188   }
00189 
00190   // add $QTDIR/plugins to the pluins search path for image plugins
00191   QString qtdir( ::getenv( "QTDIR" ) );
00192   if ( !qtdir.isEmpty() )
00193     QApplication::addLibraryPath( QDir( qtdir ).absoluteFilePath( "plugins" ) );
00194   
00195   SUITApp_Application app( argc, argv );
00196   QString cfgAppName = getAppName( argList.isEmpty() ? QString() : argList.first() );
00197   // hard-coding for LightApp :( no other way to this for the moment
00198   if ( cfgAppName == "LightApp" ) {
00199     app.setOrganizationName( "salome" );
00200     app.setApplicationName( "salome" );
00201     app.setApplicationVersion( salomeVersion() );
00202   }
00203 
00204   int result = -1;
00205 
00206   if ( useLicense ) {
00207     QString env;
00208 
00209 #ifdef WIN32
00210     DWORD aLen=1024;
00211     char aStr[1024];
00212     HANDLE aToken=0;
00213     HANDLE hProcess = GetCurrentProcess();
00214     OpenProcessToken(hProcess,TOKEN_QUERY,&aToken);
00215     if( GetUserProfileDirectory( aToken, aStr, &aLen ) )
00216       env = aStr;
00217 
00218 #else
00219     if ( ::getenv( "HOME" ) )
00220       env = ::getenv( "HOME" );
00221 #endif
00222  
00223     QFile file( env + "/ReadLicense.log" ); // Read the text from a file    
00224     if( !file.exists() ) {
00225       SUIT_LicenseDlg aLicense;
00226       if ( aLicense.exec() != QDialog::Accepted ) 
00227         return result;
00228     }
00229   }
00230 
00231   if ( !argList.isEmpty() )
00232   {
00233     SUITApp_Session* aSession = new SUITApp_Session( iniFormat );
00234     QtxSplash* splash = 0;
00235     SUIT_ResourceMgr* resMgr = aSession->createResourceMgr( argList.first() );
00236     if ( !noSplash ) 
00237     {
00238       if ( resMgr )
00239       {
00240         resMgr->loadLanguage();
00241 
00242         splash = QtxSplash::splash( QPixmap() );
00243         splash->readSettings( resMgr );
00244         if ( splash->pixmap().isNull() ) {
00245           delete splash;
00246           splash = 0;
00247         }
00248         else {
00249           QString appName    = QObject::tr( "APP_NAME" ).trimmed();
00250           QString appVersion = QObject::tr( "APP_VERSION" ).trimmed();
00251           if ( appVersion == "APP_VERSION" )
00252           {
00253             if ( appName == "APP_NAME" || appName.toLower() == "salome" )
00254               appVersion = salomeVersion();
00255             else
00256               appVersion = "";
00257           }
00258           splash->setOption( "%A", appName );
00259           splash->setOption( "%V", QObject::tr( "ABOUT_VERSION" ).arg( appVersion ) );
00260           splash->setOption( "%L", QObject::tr( "ABOUT_LICENSE" ) );
00261           splash->setOption( "%C", QObject::tr( "ABOUT_COPYRIGHT" ) );
00262           splash->show();
00263           QApplication::instance()->processEvents();
00264         }
00265       }
00266     }
00267 
00268 #ifdef SUIT_ENABLE_PYTHON
00269     //...Initialize python 
00270     int   _argc   = 1;
00271     char* _argv[] = {(char*)""};
00272     SUIT_PYTHON::init_python(_argc,_argv);
00273     
00274     PyEval_RestoreThread( SUIT_PYTHON::_gtstate );
00275     
00276     if ( !SUIT_PYTHON::salome_shared_modules_module ) // import only once
00277       SUIT_PYTHON::salome_shared_modules_module = PyImport_ImportModule( (char*)"salome_shared_modules" );
00278     if ( !SUIT_PYTHON::salome_shared_modules_module ) 
00279       PyErr_Print();
00280     
00281     PyEval_ReleaseThread( SUIT_PYTHON::_gtstate );
00282 
00283 #endif
00284 
00285     SUIT_Application* theApp = aSession->startApplication( argList.first() );
00286     if ( theApp )
00287     {
00288       Style_Salome::initialize( theApp->resourceMgr() );
00289       if ( theApp->resourceMgr()->booleanValue( "Style", "use_salome_style", true ) )
00290         Style_Salome::apply();
00291 
00292       if ( !noExceptHandling )
00293         app.setHandler( aSession->handler() );
00294 
00295       if ( splash )
00296         splash->finish( theApp->desktop() );
00297 
00298       result = app.exec();
00299     }
00300     delete aSession;
00301   }
00302 
00303   return result;
00304 }