Salome HOME
PR: mergefrom_BR_CCRT_11Nov04
[modules/kernel.git] / src / Session / SALOME_Session_i.cxx
index 414c850e9364c4e1353d39405fb5449a5a90fa99..7e6c34c8699a52601fef7e4bcf2fc05330a1b7c4 100644 (file)
 //  Module : SALOME
 //  $Header$
 
-using namespace std;
 #include "utilities.h"
 
 #include "SALOME_Session_i.hxx"
 #include "SALOME_NamingService.hxx"
-#include "SALOME_Session_QThread.hxx"
+#include "SALOME_Event.hxx"
 
 #include "QAD_Application.h"
 #include "QAD_Desktop.h"
@@ -41,6 +40,7 @@ using namespace std;
 #include <OSD_SharedLibrary.hxx>
 #include <OSD_LoadMode.hxx>
 #include <OSD_Function.hxx>
+using namespace std;
 
 //=============================================================================
 /*! SALOME_Session_i
@@ -48,26 +48,40 @@ using namespace std;
  */ 
 //=============================================================================
 
-SALOME_Session_i::SALOME_Session_i(int argc, char ** argv, CORBA::ORB_ptr orb, PortableServer::POA_ptr poa)
+SALOME_Session_i::SALOME_Session_i(int argc, 
+                                  char ** argv, 
+                                  CORBA::ORB_ptr orb, 
+                                  PortableServer::POA_ptr poa, 
+                                  QMutex* GUIMutex,
+                                  QWaitCondition* GUILauncher)
 {
   _argc = argc ;
   _argv = argv ;
-  _IAPPThread = new SALOME_Session_QThread(_argc, _argv) ;
   _isGUI = FALSE ;
   _runningStudies= 0 ;
   _orb = CORBA::ORB::_duplicate(orb) ;
   _poa = PortableServer::POA::_duplicate(poa) ;
-  MESSAGE("constructor end");
+  _GUIMutex = GUIMutex;
+  _GUILauncher = GUILauncher;
+  //MESSAGE("constructor end");
 }
-  
-Engines::Component_ptr SALOME_Session_i::GetVisuGen(){
-  typedef Engines::Component_ptr VisuGen(CORBA::ORB_ptr, PortableServer::POA_ptr,
-                                        SALOME_NamingService*, QMutex*);
-  MESSAGE("SALOME_Session_i::GetVisuGen");
-  OSD_SharedLibrary  visuSharedLibrary("libVisuEngine.so");
-  if(visuSharedLibrary.DlOpen(OSD_RTLD_LAZY))
-    if(OSD_Function osdFun = visuSharedLibrary.DlSymb("GetVisuGen"))
-      return ((VisuGen (*)) osdFun)(_orb,_poa,_NS,&_GUIMutex);
+
+//=============================================================================
+/*! GetVisuComponent
+ *  returns Visu component
+ */ 
+//=============================================================================
+
+Engines::Component_ptr SALOME_Session_i::GetVisuComponent()
+{
+  //MESSAGE("SALOME_Session_i::GetVisuGen");
+  typedef Engines::Component_ptr TGetImpl(CORBA::ORB_ptr,
+                                        PortableServer::POA_ptr,
+                                        SALOME_NamingService*,QMutex*);
+  OSD_SharedLibrary  aSharedLibrary("libVISUEngineImpl.so");
+  if(aSharedLibrary.DlOpen(OSD_RTLD_LAZY))
+    if(OSD_Function anOSDFun = aSharedLibrary.DlSymb("GetImpl"))
+      return ((TGetImpl (*)) anOSDFun)(_orb,_poa,_NS,_GUIMutex);
   return Engines::Component::_nil();
 }
 
@@ -79,7 +93,7 @@ Engines::Component_ptr SALOME_Session_i::GetVisuGen(){
 
 SALOME_Session_i::~SALOME_Session_i()
 {
-  MESSAGE("destructor end"); 
+  //MESSAGE("destructor end"); 
 }
 
 //=============================================================================
@@ -96,7 +110,6 @@ void SALOME_Session_i::NSregister()
     {
       _NS = new SALOME_NamingService(_orb);
       _NS->Register(pSession, "/Kernel/Session");
-      _IAPPThread->setNamingService(_NS);
     }
   catch (ServiceUnreachable&)
     {
@@ -107,7 +120,7 @@ void SALOME_Session_i::NSregister()
     {
       INFOS("Caught unknown exception from Naming Service");
     }
-  MESSAGE("NSregister end"); 
+  //MESSAGE("Session registered in Naming Service"); 
 }
 
 //=============================================================================
@@ -119,13 +132,10 @@ void SALOME_Session_i::NSregister()
 
 void SALOME_Session_i::GetInterface()
 {
-  _GUIMutex.lock() ;       // get access to boolean _isGUI
-  _isGUI = _IAPPThread->running();
-  if(!_isGUI){
-    _isGUI = TRUE ; 
-    _IAPPThread->start() ;
+  if( !QAD_Application::getDesktop() ) {
+    _GUILauncher->wakeAll();
+    MESSAGE("SALOME_Session_i::GetInterface() called, starting GUI...")
   }
-  _GUIMutex.unlock() ; // release access to boolean _isGUI 
 }
 
 //=============================================================================
@@ -133,27 +143,18 @@ void SALOME_Session_i::GetInterface()
  *  Kills the session if there are no active studies nore GUI
  */ 
 //=============================================================================
+class CloseEvent : public SALOME_Event
+{
+public:
+  virtual void Execute() {
+    if ( QAD_Application::getDesktop() )
+      QAD_Application::getDesktop()->closeDesktop( true );
+  }
+};
 
 void SALOME_Session_i::StopSession()
 {
-  qApp->lock();
-  QAD_Application::getDesktop()->closeDesktop( true );
-  qApp->unlock();
-/*
-  _GUIMutex.lock();         // get access to boolean _isGUI
-  if ((! _isGUI) && (! _runningStudies))
-    {
-      MESSAGE("Ask for Session Kill, OK");
-      exit(0);
-    }
-  else
-    {
-      _GUIMutex.unlock() ;  // release access to boolean _isGUI
-      MESSAGE("Ask for Session Kill, NOK");
-      if (_isGUI) throw SALOME::Session::GUIActive();   
-      if (_runningStudies) throw SALOME::Session::RunningStudies();
-    }
-*/
+  ProcessVoidEvent( new CloseEvent() );
 }
  
 //=============================================================================
@@ -163,19 +164,29 @@ void SALOME_Session_i::StopSession()
  */ 
 //=============================================================================
 
+class QtLock
+{
+public:
+  QtLock() { if ( qApp ) qApp->lock(); }
+  ~QtLock() { if ( qApp ) qApp->unlock(); }
+};
+
+
 SALOME::StatSession SALOME_Session_i::GetStatSession()
 {
   // update Session state
-  _GUIMutex.lock();    
-  _isGUI = _IAPPThread->running();
+  _GUIMutex->lock();    
+
   _runningStudies = 0;
-  if (_isGUI) {
-    qApp->lock();
-    if ( QAD_Application::getDesktop() && QAD_Application::getDesktop()->getActiveApp() )
+  {
+    QtLock lock;
+    _isGUI = QAD_Application::getDesktop();
+    if ( _isGUI && QAD_Application::getDesktop()->getActiveApp() )
       _runningStudies = QAD_Application::getDesktop()->getActiveApp()->getStudies().count();
-    qApp->unlock();
   }
-  _GUIMutex.unlock();
+
+  _GUIMutex->unlock();
+
   // getting stat info
   SALOME::StatSession_var myStats = new SALOME::StatSession ;
   if (_runningStudies)
@@ -187,3 +198,11 @@ SALOME::StatSession SALOME_Session_i::GetStatSession()
   return myStats._retn() ;
 }
 
+CORBA::Long SALOME_Session_i::GetActiveStudyId()
+{
+  long aStudyId=-1;
+  if( QAD_Application::getDesktop() && QAD_Application::getDesktop()->getActiveStudy()) {
+    aStudyId = QAD_Application::getDesktop()->getActiveStudy()->getStudyId();
+  }
+  return aStudyId;
+}