Salome HOME
Kernel evolution for salome gui without neither NS nor other servers.
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Driver_i.cxx
index ae2db2077fcfb73511880303cbb5026d424f4459..a5597708d55360c57223c485f13dfa8c23156dbf 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2020  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
 #include "SALOMEDS_SComponent_i.hxx"
 #include "SALOMEDS_Study_i.hxx"
 #include "SALOMEDS.hxx"
+#include <SALOMEDSImpl_IParameters.hxx>
+#include <SALOME_KernelServices.hxx>
 #include <stdlib.h>
 
-SALOMEDS_Driver_i::SALOMEDS_Driver_i(Engines::EngineComponent_ptr theEngine, CORBA::ORB_ptr theORB) 
+#include CORBA_CLIENT_HEADER(SALOME_Session)
+
+SALOMEDS_Driver_i::SALOMEDS_Driver_i(Engines::EngineComponent_ptr theEngine, CORBA::ORB_ptr theORB)
 {
   // engine should not be null - component is supposed to be inherited from Engines::EngineComponent
   _engine = Engines::EngineComponent::_duplicate(theEngine);
   // driver can be null - if component interface does not inherit SALOMEDS::Driver
   _driver = SALOMEDS::Driver::_narrow(theEngine);
-  _orb = CORBA::ORB::_duplicate(theORB);        
+  _orb = CORBA::ORB::_duplicate(theORB);
 }
 
 SALOMEDS_Driver_i::SALOMEDS_Driver_i(SALOMEDS::Driver_ptr theDriver, CORBA::ORB_ptr theORB)
@@ -44,14 +48,14 @@ SALOMEDS_Driver_i::SALOMEDS_Driver_i(SALOMEDS::Driver_ptr theDriver, CORBA::ORB_
   _driver = SALOMEDS::Driver::_duplicate(theDriver);
   // engine can be null - since it is narrowed from SALOMEDS::Driver ptr which can be null
   _engine = Engines::EngineComponent::_narrow(theDriver);
-  _orb = CORBA::ORB::_duplicate(theORB);        
+  _orb = CORBA::ORB::_duplicate(theORB);
 }
 
 SALOMEDS_Driver_i::~SALOMEDS_Driver_i()
 {
 }
 
-std::string SALOMEDS_Driver_i::GetIOR() 
+std::string SALOMEDS_Driver_i::GetIOR()
 {
   std::string ior = "";
   if ( !CORBA::is_nil(_engine) ) {
@@ -62,10 +66,10 @@ std::string SALOMEDS_Driver_i::GetIOR()
 }
 
 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::Save(const SALOMEDSImpl_SComponent& theComponent,
-                                             const std::string& theURL,
-                                             long& theStreamLength,
-                                             bool isMultiFile)
-{  
+                                              const std::string& theURL,
+                                              long& theStreamLength,
+                                              bool isMultiFile)
+{
   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
 
@@ -76,7 +80,9 @@ SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::Save(const SALOMEDSImpl_SComponent& the
     aStream = _driver->Save(sco.in(), url, isMultiFile);
 
   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
-  theStreamLength = aTMPFile->Size();
+  theStreamLength = (long)aTMPFile->Size(); //!< TODO: conversion from size_t to long
+
+  sco->UnRegister();
   SALOMEDS::lock();
 
   return aTMPFile;
@@ -97,12 +103,14 @@ SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::SaveASCII(const SALOMEDSImpl_SComponent
     aStream = _driver->SaveASCII(sco.in(), url, isMultiFile);
 
   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
-  theStreamLength = aTMPFile->Size();
+  theStreamLength = (long)aTMPFile->Size(); //!< TODO: conversion from size_t to long
+
+  sco->UnRegister();
   SALOMEDS::lock();
 
   return aTMPFile;
 }
-  
+
 bool SALOMEDS_Driver_i::Load(const SALOMEDSImpl_SComponent& theComponent,
                              const unsigned char* theStream,
                              const long theStreamLength,
@@ -114,9 +122,9 @@ bool SALOMEDS_Driver_i::Load(const SALOMEDSImpl_SComponent& theComponent,
   CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
 
   SALOMEDS::TMPFile_var aStream;
-  if (theStreamLength > 0) 
-    aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);  
-  else 
+  if (theStreamLength > 0)
+    aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
+  else
     aStream = new SALOMEDS::TMPFile(0);
 
   SALOMEDS::unlock();
@@ -125,6 +133,7 @@ bool SALOMEDS_Driver_i::Load(const SALOMEDSImpl_SComponent& theComponent,
   if ( !CORBA::is_nil(_driver) )
     isOk = _driver->Load(sco.in(), aStream.in(), url, isMultiFile);
 
+  sco->UnRegister();
   SALOMEDS::lock();
   return isOk;
 }
@@ -140,9 +149,9 @@ bool SALOMEDS_Driver_i::LoadASCII(const SALOMEDSImpl_SComponent& theComponent,
   CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
 
   SALOMEDS::TMPFile_var aStream;
-  if(theStreamLength > 0) 
-    aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);  
-  else 
+  if(theStreamLength > 0)
+    aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
+  else
     aStream = new SALOMEDS::TMPFile(0);
 
   SALOMEDS::unlock();
@@ -151,6 +160,7 @@ bool SALOMEDS_Driver_i::LoadASCII(const SALOMEDSImpl_SComponent& theComponent,
   if ( !CORBA::is_nil(_driver) )
     isOk = _driver->LoadASCII(sco.in(), aStream.in(), url, isMultiFile);
 
+  sco->UnRegister();
   SALOMEDS::lock();
   return isOk;
 }
@@ -162,10 +172,11 @@ void SALOMEDS_Driver_i::Close(const SALOMEDSImpl_SComponent& theComponent)
   SALOMEDS::unlock();
   if ( !CORBA::is_nil(_driver) )
     _driver->Close(sco.in());
+  sco->UnRegister();
   SALOMEDS::lock();
 }
 
-std::string SALOMEDS_Driver_i::ComponentDataType() 
+std::string SALOMEDS_Driver_i::ComponentDataType()
 {
   std::string dtype = "";
   if ( !CORBA::is_nil(_driver) ) {
@@ -177,26 +188,32 @@ std::string SALOMEDS_Driver_i::ComponentDataType()
 
 std::string SALOMEDS_Driver_i::Version()
 {
-  return !CORBA::is_nil( _engine ) ? _engine->getVersion() : std::string("");
+  std::string ver;
+  if ( !CORBA::is_nil( _engine )) {
+    CORBA::String_var v = _engine->getVersion();
+    ver = v;
+  }
+  return ver;
 }
 
 std::string SALOMEDS_Driver_i::IORToLocalPersistentID(const SALOMEDSImpl_SObject& theSObject,
-                                                     const std::string& IORString,
-                                                     bool isMultiFile,
-                                                     bool isASCII)
+                                                      const std::string& IORString,
+                                                      bool isMultiFile,
+                                                      bool isASCII)
 {
   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theSObject, _orb);
-  CORBA::String_var ior = CORBA::string_dup(IORString.c_str());
-
   SALOMEDS::unlock();
 
   std::string pstr = "";
 
   if ( !CORBA::is_nil(_driver) ) {
-    CORBA::String_var pers_string =_driver->IORToLocalPersistentID(so.in(), ior.in(), isMultiFile, isASCII);
-    pstr = pers_string;
+    CORBA::String_var pers_string =_driver->IORToLocalPersistentID(so.in(), IORString.c_str(),
+                                                                   isMultiFile, isASCII);
+    if ( pers_string.in() )
+      pstr = pers_string;
   }
 
+  so->UnRegister();
   SALOMEDS::lock();
 
   return pstr;
@@ -204,9 +221,9 @@ std::string SALOMEDS_Driver_i::IORToLocalPersistentID(const SALOMEDSImpl_SObject
 
 
 std::string SALOMEDS_Driver_i::LocalPersistentIDToIOR(const SALOMEDSImpl_SObject& theObject,
-                                                     const std::string& aLocalPersistentID,
-                                                     bool isMultiFile,
-                                                     bool isASCII)
+                                                      const std::string& aLocalPersistentID,
+                                                      bool isMultiFile,
+                                                      bool isASCII)
 {
   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
   CORBA::String_var pers_string = CORBA::string_dup(aLocalPersistentID.c_str());
@@ -216,9 +233,10 @@ std::string SALOMEDS_Driver_i::LocalPersistentIDToIOR(const SALOMEDSImpl_SObject
 
   if ( !CORBA::is_nil(_driver) ) {
     CORBA::String_var IOR = _driver->LocalPersistentIDToIOR(so.in(), pers_string.in(), isMultiFile, isASCII);
-    ior = IOR;
+    if ( IOR.in() )
+      ior = IOR;
   }
-
+  so->UnRegister();
   SALOMEDS::lock();
   return ior;
 }
@@ -240,7 +258,7 @@ bool SALOMEDS_Driver_i::CanCopy(const SALOMEDSImpl_SObject& theObject)
 }
 
 
-SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::CopyFrom(const SALOMEDSImpl_SObject& theObject, 
+SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::CopyFrom(const SALOMEDSImpl_SObject& theObject,
                                                   int& theObjectID,
                                                   long& theStreamLength)
 {
@@ -254,8 +272,10 @@ SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::CopyFrom(const SALOMEDSImpl_SObject& th
     aStream = _driver->CopyFrom(so.in(), anObjectID);
 
   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
-  theStreamLength = aTMPFile->Size();
+  theStreamLength = (long)aTMPFile->Size(); //!< TODO: conversion from size_t to long
   theObjectID = anObjectID;
+
+  so->UnRegister();
   SALOMEDS::lock();
 
   return aTMPFile;
@@ -274,17 +294,17 @@ bool SALOMEDS_Driver_i::CanPaste(const std::string& theComponentName, int theObj
 }
 
 std::string SALOMEDS_Driver_i::PasteInto(const unsigned char* theStream,
-                                        const long theStreamLength,
-                                        int theObjectID,
-                                        const SALOMEDSImpl_SObject& theObject)
+                                         const long theStreamLength,
+                                         int theObjectID,
+                                         const SALOMEDSImpl_SObject& theObject)
 {
   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)theStream;
 
   SALOMEDS::TMPFile_var aStream;
-  if(theStreamLength > 0) 
-    aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);  
-  else 
+  if(theStreamLength > 0)
+    aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
+  else
     aStream = new SALOMEDS::TMPFile(0);
 
   SALOMEDS::unlock();
@@ -296,30 +316,27 @@ std::string SALOMEDS_Driver_i::PasteInto(const unsigned char* theStream,
     entry = ret_so->GetID();
   }
 
+  so->UnRegister();
   SALOMEDS::lock();
 
   return entry;
 }
 
-SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::DumpPython(SALOMEDSImpl_Study* theStudy, 
-                                                    bool isPublished, 
+SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::DumpPython(bool isPublished,
                                                     bool isMultiFile,
                                                     bool& isValidScript,
                                                     long& theStreamLength)
 {
-  SALOMEDS_Study_i *  st_servant = SALOMEDS_Study_i::GetStudyServant(theStudy, _orb);//new SALOMEDS_Study_i (theStudy, _orb);
-  SALOMEDS::Study_var st  = SALOMEDS::Study::_narrow(st_servant->_this());
-
   SALOMEDS::unlock();
 
   Engines::TMPFile_var aStream;
   CORBA::Boolean aValidScript = true; // VSR: maybe should be false by default ???
 
   if ( !CORBA::is_nil( _engine ) )
-    aStream = _engine->DumpPython(st.in(), isPublished, isMultiFile, aValidScript);
+    aStream = _engine->DumpPython(isPublished, isMultiFile, aValidScript);
 
   SALOMEDSImpl_TMPFile* aTMPFile = new Engines_TMPFile_i(aStream._retn());
-  theStreamLength = aTMPFile->Size();
+  theStreamLength = (long)aTMPFile->Size(); //!< TODO: conversion from size_t to long
   isValidScript = aValidScript;
 
   SALOMEDS::lock();
@@ -331,47 +348,57 @@ SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::DumpPython(SALOMEDSImpl_Study* theStudy
 //                                          SALOMEDS_DriverFactory
 //###############################################################################################################
 
-SALOMEDS_DriverFactory_i::SALOMEDS_DriverFactory_i(CORBA::ORB_ptr theORB
+SALOMEDS_DriverFactory_i::SALOMEDS_DriverFactory_i(CORBA::ORB_ptr theORB, bool isNSAvail):_orb( CORBA::ORB::_duplicate(theORB) )
 {
-  _orb = CORBA::ORB::_duplicate(theORB);
-  _name_service = new SALOME_NamingService(_orb);
+  if(isNSAvail)
+    _name_service = new SALOME_NamingService(_orb);
 }
 
+SALOMEDS_DriverFactory_i::SALOMEDS_DriverFactory_i(CORBA::ORB_ptr theORB, SALOME_NamingService_Abstract *ns):_orb( CORBA::ORB::_duplicate(theORB) )
+{
+  _name_service = ns->clone();
+}
 
-SALOMEDS_DriverFactory_i::~SALOMEDS_DriverFactory_i() 
+SALOMEDS_DriverFactory_i::~SALOMEDS_DriverFactory_i()
 {
   delete _name_service;
 }
 
 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByType(const std::string& theComponentType)
 {
-  CORBA::Object_var obj;
 
-  std::string aFactoryType;
-  if (theComponentType == "SUPERV") aFactoryType = "SuperVisionContainer";
-  else aFactoryType = "FactoryServer";
+  if ( theComponentType == SALOMEDSImpl_IParameters::getDefaultVisualComponent() )
+    return NULL; // skip the "Interface Applicative" component
 
-  SALOMEDS::unlock();
-  obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component(aFactoryType.c_str(), theComponentType.c_str());
-  SALOMEDS::lock();
+  SALOMEDSImpl_Driver* driver = 0;
 
-  if (CORBA::is_nil(obj)) {
-    obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component("FactoryServerPy", theComponentType.c_str());
-  }
+  CORBA::Object_var obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component("FactoryServer", theComponentType.c_str());
 
   if (!CORBA::is_nil(obj)) {
     Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
-    return new SALOMEDS_Driver_i(anEngine, _orb);
+    driver = new SALOMEDS_Driver_i(anEngine, _orb);
+  }
+  else {
+    // It can be "light" module
+    obj = _name_service->Resolve("/Kernel/Session");
+    if (!CORBA::is_nil(obj)) {
+      SALOME::Session_var session = SALOME::Session::_narrow(obj);
+      if (!CORBA::is_nil(session)) {
+        Engines::EngineComponent_var anEngine = session->GetComponent(theComponentType.c_str());
+        if (!CORBA::is_nil(anEngine))
+          driver = new SALOMEDS_Driver_i(anEngine, _orb);
+      }
+    }
   }
 
-  return NULL;
+  return driver;
 }
 
 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByIOR(const std::string& theIOR)
 {
   CORBA::Object_var obj;
   obj = _orb->string_to_object(theIOR.c_str());
+
   if (!CORBA::is_nil(obj)) {
     Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
     return new SALOMEDS_Driver_i(anEngine, _orb);