Salome HOME
0022712: [CEA 1300] LightApp_PreferencesDlg - invalid layout / resize policy
[modules/gui.git] / src / Session / SALOME_Session_Server.cxx
index 3dfee002dfe8f73efd19c00ebafc819970c21f3d..4d34b5e5822f4c08160cc49bb2b8ca8d13929900 100755 (executable)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -6,7 +6,7 @@
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -94,8 +94,6 @@
  * - get session state
  */
 
-PyObject* salome_shared_modules_module = 0;
-
 void MessageOutput( QtMsgType type, const char* msg )
 {
   switch ( type )
@@ -112,29 +110,6 @@ void MessageOutput( QtMsgType type, const char* msg )
   }
 }
 
-/* XPM */
-static const char* pixmap_not_found_xpm[] = {
-"16 16 3 1",
-"       c None",
-".      c #000000",
-"+      c #A80000",
-"                ",
-"                ",
-"    .     .     ",
-"   .+.   .+.    ",
-"  .+++. .+++.   ",
-"   .+++.+++.    ",
-"    .+++++.     ",
-"     .+++.      ",
-"    .+++++.     ",
-"   .+++.+++.    ",
-"  .+++. .+++.   ",
-"   .+.   .+.    ",
-"    .     .     ",
-"                ",
-"                ",
-"                "};
-
 QString salomeVersion()
 {
   return GUI_VERSION_STR;
@@ -147,7 +122,6 @@ public:
   {
     setCurrentFormat( "xml" );
     setOption( "translators", QString( "%P_msg_%L.qm|%P_icons.qm|%P_images.qm" ) );
-    setDefaultPixmap( QPixmap( pixmap_not_found_xpm ) );
   }
   static void initResourceMgr()
   {
@@ -178,36 +152,62 @@ protected:
 
   virtual long userFileId( const QString& _fname ) const
   {
+    //////////////////////////////////////////////////////////////////////////////////////////////
+    // In SALOME and SALOME-based applications the user preferences file is named as
+    // - <AppName>.xml.<AppVersion> on Windows
+    // - <AppName>rc.<AppVersion> on Linux
+    // where
+    //   * AppName is application name, default SalomeApp (can be customized in SALOME-based
+    //     applications
+    //   * AppVersion is application version
+    //
+    // Since version 6.5.0 of SALOME, user file is situated in the ~/.config/salome
+    // directory. For backward compatibility, when user preferences from nearest
+    // version of application is searched, user home directory is also looked through,
+    // with lower priority.
+    // 
+    // Since version 6.6.0 of SALOME, user file name on Linux is no more prefixed by dot
+    // symbol since it is situated in hidden ~/.config/salome directory. Files with dot
+    // prefix also though taken into account (with lower priority) for backward compatibility.
+    //
+    // Notes:
+    // - Currently the following format of version number is supported:
+    //   <major>[.<minor>[.<release>[<type><dev>]]]
+    //   Parts in square brackets are considered optional. Here:
+    //   * major   - major version id
+    //   * minor   - minor version id
+    //   * release - maintenance version id
+    //   * type    - dev or patch marker; it can be either one alphabetical symbol (from 'a' to 'z')
+    //               or 'rc' to point release candidate (case-insensitive)
+    //   * dev     - dev version or patch number
+    //   All numerical values must be of range [1-99].
+    //   Examples: 1.0, 6.5.0, 1.2.0a1, 3.3.3rc3 (release candidate 3), 11.0.0p1 (patch 1)
+    //
+    // - Versioning approach can be customized by implementing and using own resource manager class,
+    //   see QtxResurceMgr, SUIT_ResourceMgr classes.
+    //////////////////////////////////////////////////////////////////////////////////////////////
     long id = -1;
     if ( !myExtAppName.isEmpty() ) {
 #ifdef WIN32
-      QRegExp exp( QString( "%1\\.%2\\.([a-zA-Z0-9.]+)$" ).arg( myExtAppName ).arg( currentFormat() ) );
+      // On Windows, user file name is something like SalomeApp.xml.6.5.0 where
+      // - SalomeApp is an application name (can be customized)
+      // - xml is a file format (xml or ini)
+      // - 6.5.0 is an application version, can include alfa/beta/rc marks, e.g. 6.5.0a3, 6.5.0rc1
+      QRegExp exp( QString( "%1\\.%2\\.([a-zA-Z0-9.]+)" ).arg( myExtAppName ).arg( currentFormat() ) );
 #else
-      QRegExp exp( QString( "\\.%1rc\\.([a-zA-Z0-9.]+)$" ).arg( myExtAppName ) );
+      // On Linux, user file name is something like SalomeApprc.6.5.0 where
+      // - SalomeApp is an application name (can be customized)
+      // - 6.5.0 is an application version, can include alfa/beta/rc marks, e.g. 6.5.0a3, 6.5.0rc1
+
+      // VSR 24/09/2012: issue 0021781: since version 6.6.0 user filename is not prepended with "."
+      // when it is stored in the ~/.config/<appname> directory;
+      // for backward compatibility we also check files prepended with "." with lower priority
+      QRegExp exp( QString( "\\.?%1rc\\.([a-zA-Z0-9.]+)" ).arg( myExtAppName ) );
 #endif
-      QRegExp vers_exp( "^([0-9]+)([A-Za-z]?)([0-9]*)$" );
-      
       QString fname = QFileInfo( _fname ).fileName();
-      if( exp.exactMatch( fname ) ) {
-        QStringList vers = exp.cap( 1 ).split( ".", QString::SkipEmptyParts );
-        int major=0, minor=0;
-        int release = 0, dev1 = 0, dev2 = 0;
-       if ( vers.count() > 0 ) major = vers[0].toInt();
-       if ( vers.count() > 1 ) minor = vers[1].toInt();
-       if ( vers.count() > 2 ) {
-         if( vers_exp.indexIn( vers[2] ) != -1 ) {
-           release = vers_exp.cap( 1 ).toInt();
-           dev1 = vers_exp.cap( 2 )[ 0 ].toLatin1();
-           dev2 = vers_exp.cap( 3 ).toInt();
-         }
-       }
-        
-        int dev = dev1*100+dev2;
-       id = major;
-        id*=100; id+=minor;
-        id*=100; id+=release;
-        id*=10000;
-        if ( dev > 0 ) id+=dev-10000;
+      if ( exp.exactMatch( fname ) ) {
+       long fid = Qtx::versionToId( exp.cap( 1 ) );
+       if ( fid > 0 ) id = fid;
       }
     }
     return id;
@@ -227,7 +227,7 @@ public:
   SALOME_Session() : SUIT_Session() {}
   virtual ~SALOME_Session() {}
 
-protected:
+public:
   virtual SUIT_ResourceMgr* createResourceMgr( const QString& appName ) const
   {
     SALOME_ResourceMgr::initResourceMgr();
@@ -259,19 +259,6 @@ public:
 
   virtual bool notify( QObject* receiver, QEvent* e )
   {
-#if OCC_VERSION_LARGE < 0x06010100
-    // Disable GUI user actions while python command is executed
-    if (SUIT_Session::IsPythonExecuted()) {
-      // Disable mouse and keyboard events
-      QEvent::Type aType = e->type();
-      if (aType == QEvent::MouseButtonPress || aType == QEvent::MouseButtonRelease ||
-          aType == QEvent::MouseButtonDblClick || aType == QEvent::MouseMove ||
-          aType == QEvent::Wheel || aType == QEvent::ContextMenu ||
-          aType == QEvent::KeyPress || aType == QEvent::KeyRelease ||
-          aType == QEvent::Accel || aType == QEvent::AccelOverride)
-        return false;
-    }
-#endif
 
 #ifdef ENABLE_TESTRECORDER
     return myHandler ? myHandler->handle( receiver, e ) :
@@ -351,12 +338,32 @@ int main( int argc, char **argv )
 {
   // Install Qt debug messages handler
   qInstallMsgHandler( MessageOutput );
+
+  //Set a "native" graphic system in case if application runs on the remote host
+  QString remote(getenv("REMOTEHOST"));
+  QString client(getenv("SSH_CLIENT"));
+  if(remote.length() > 0 || client.length() > 0 ) {
+    QApplication::setGraphicsSystem(QLatin1String("native"));
+  }
   
   // add $QTDIR/plugins to the pluins search path for image plugins
   QString qtdir( ::getenv( "QTDIR" ) );
   if ( !qtdir.isEmpty() )
     QApplication::addLibraryPath( QDir( qtdir ).absoluteFilePath( "plugins" ) );
 
+  {
+    SALOME_Session s;
+    QApplication::setApplicationName( "salome" );
+    SUIT_ResourceMgr* resMgr = s.createResourceMgr( "SalomeApp" );
+    bool isCloc = resMgr->booleanValue( "language", "locale", true );
+    if ( isCloc ) { 
+      QLocale::setDefault( QLocale::c() );
+    }
+    else {
+      QLocale::setDefault( QLocale::system() );
+    }
+  }
+
   // Create Qt application instance;
   // this should be done the very first!
   SALOME_QApplication _qappl( argc, argv );
@@ -416,14 +423,6 @@ int main( int argc, char **argv )
     int   _argc   = 1;
     char* _argv[] = {(char*)""};
     KERNEL_PYTHON::init_python( _argc,_argv );
-    PyEval_RestoreThread( KERNEL_PYTHON::_gtstate );
-    if ( !KERNEL_PYTHON::salome_shared_modules_module ) // import only once
-      KERNEL_PYTHON::salome_shared_modules_module = PyImport_ImportModule( "salome_shared_modules" );
-    if ( !KERNEL_PYTHON::salome_shared_modules_module ) {
-      INFOS( "salome_shared_modules_module == NULL" );
-      PyErr_Print();
-    }
-    PyEval_ReleaseThread( KERNEL_PYTHON::_gtstate );
 
     // ...create ORB, get RootPOA object, NamingService, etc.
     ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
@@ -572,28 +571,28 @@ int main( int argc, char **argv )
 
         if ( splash )
           splash->finish( aGUIApp->desktop() );
-       
+
         result = _qappl.exec();
         
         splash = 0;
-       
+
         if ( result == SUIT_Session::NORMAL ) {
-         // desktop is explicitly closed by user from GUI
-         // exit flags says if it's necessary to shutdown all servers
-         // all session server only
+        // desktop is explicitly closed by user from GUI
+        // exit flags says if it's necessary to shutdown all servers
+        // all session server only
           shutdownAll = aGUISession->exitFlags();
-       }
-       else {
-         // desktop might be closed from:
-         // - StopSesion() /temporarily/ or
-         // - Shutdown() /permanently/
-         stat = session->GetStatSession();
-         shutdownSession = stat.state == SALOME::shutdown;
-       }
-       if ( shutdownAll || shutdownSession ) {
-         _SessionMutex.lock(); // lock mutex before leaving loop - it will be unlocked later
-         break;
-       }
+        }
+        else {
+          // desktop might be closed from:
+          // - StopSesion() /temporarily/ or
+          // - Shutdown() /permanently/
+          stat = session->GetStatSession();
+          shutdownSession = stat.state == SALOME::shutdown;
+        }
+        if ( shutdownAll || shutdownSession ) {
+          _SessionMutex.lock(); // lock mutex before leaving loop - it will be unlocked later
+          break;
+        }
       }
 
       delete aGUISession;
@@ -607,11 +606,14 @@ int main( int argc, char **argv )
   // unlock Session mutex
   _SessionMutex.unlock();
   
-  if ( shutdownAll )
+  if ( myServerLauncher )
+    myServerLauncher->ShutdownAll(); // shutdown embedded servers
+
+  if ( shutdownAll )                 // shutdown standalone servers
     shutdownServers( _NS );
 
   if ( myServerLauncher )
-    myServerLauncher->KillAll(); // kill embedded servers
+    myServerLauncher->KillAll();     // kill embedded servers
 
   // Unregister session server
   SALOME_Session_i* sessionServant = dynamic_cast<SALOME_Session_i*>( poa->reference_to_servant( session.in() ) );
@@ -639,12 +641,23 @@ int main( int argc, char **argv )
     abort(); //abort program to avoid deadlock in destructors or atexit when shutdown has been interrupted
   }
 
-  PyGILState_Ensure();
-  //Destroy orb from python (for chasing memory leaks)
+    //Destroy orb from python (for chasing memory leaks)
   //PyRun_SimpleString("from omniORB import CORBA");
   //PyRun_SimpleString("orb=CORBA.ORB_init([''], CORBA.ORB_ID)");
   //PyRun_SimpleString("orb.destroy()");
-  Py_Finalize();
+
+  // Destroy the ORB:
+  MESSAGE("Explicitely destroying the ORB (hoping to kill omniORB threads ...)");
+  ORB_INIT * init = SINGLETON_<ORB_INIT>::Instance();
+  if (init)
+    init->explicit_destroy();
+
+  // After ORB destruction
+  if(Py_IsInitialized())
+    {
+      PyGILState_Ensure();
+      Py_Finalize();
+    }
 
   if ( shutdownAll )
     killOmniNames();