Salome HOME
- Deleted Study as an input parameter and class field.
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Study_i.cxx
index 5c1337f1851906c3194881007a903f585a933227..cd586ec2f80023d78afc04392e2afcadf5bd4b98 100644 (file)
@@ -165,10 +165,10 @@ namespace SALOMEDS
     {
       for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
       {
-       if ( it->first->_is_equivalent(theObs) ) {
-         myObservers.erase( it );
-         break;
-       }
+           if ( it->first->_is_equivalent(theObs) ) {
+             myObservers.erase( it );
+             break;
+           }
       }
     }
 
@@ -235,6 +235,7 @@ SALOMEDS_Study_i::SALOMEDS_Study_i(CORBA::ORB_ptr orb)
   _orb     = CORBA::ORB::_duplicate(orb);
   _impl    = new SALOMEDSImpl_Study();
   _factory = new SALOMEDS_DriverFactory_i(_orb);
+  _closed  = true;
 
   Init();
 }
@@ -258,6 +259,12 @@ SALOMEDS_Study_i::~SALOMEDS_Study_i()
 //============================================================================
 void SALOMEDS_Study_i::Init()
 {
+  if ( !_impl->GetDocument() )
+    _impl->Init();
+
+  if (!_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();
+
   _builder        = new SALOMEDS_StudyBuilder_i(_impl->NewBuilder(), _orb);  
   _notifier       = new SALOMEDS::Notifier(_orb);
   _genObjRegister = new SALOMEDS::GenObjRegister(_orb);
@@ -266,6 +273,9 @@ void SALOMEDS_Study_i::Init()
   _impl->setNotifier(_notifier);
   _impl->setGenObjRegister( _genObjRegister );
 
+  // update desktop title with new study name
+  NameChanged();
+
   // Notify GUI that study was created
   SALOME_NamingService *aNamingService = KERNEL::getNamingService();
   CORBA::Object_var obj = aNamingService->Resolve("/Kernel/Session");
@@ -287,54 +297,49 @@ void SALOMEDS_Study_i::Init()
 //============================================================================
 void SALOMEDS_Study_i::Clear()
 {
+  SALOMEDS::Locker lock;
+  if (_closed)
+    return;
   //delete the builder servant
   PortableServer::POA_var poa=_builder->_default_POA();
   PortableServer::ObjectId_var anObjectId = poa->servant_to_id(_builder);
   poa->deactivate_object(anObjectId.in());
   _builder->_remove_ref();
-  
-  _impl->Clear();
-  _impl->setNotifier(0);
-  delete _notifier;
-  delete _genObjRegister;
-
-  SALOMEDS::Locker lock;
-
-  if (_closed)
-    throw SALOMEDS::Study::StudyInvalidReference();
 
   RemovePostponed(-1);
 
-  SALOMEDS::SComponentIterator_var itcomponent = NewComponentIterator();
-  for (; itcomponent->More(); itcomponent->Next()) {
-    SALOMEDS::SComponent_var sco = itcomponent->Value();
-    CORBA::String_var compodatatype=sco->ComponentDataType();
-    MESSAGE ( "Look for an engine for data type :"<< compodatatype);
-    // if there is an associated Engine call its method for closing
-    CORBA::String_var IOREngine;
-    if (sco->ComponentIOR(IOREngine)) {
-      // we have found the associated engine to write the data
-      MESSAGE ( "We have found an engine for data type :"<< compodatatype);
-      //_narrow can throw a corba exception
-      try {
-           CORBA::Object_var obj = _orb->string_to_object(IOREngine);
-           if (!CORBA::is_nil(obj)) {
-             SALOMEDS::Driver_var anEngine = SALOMEDS::Driver::_narrow(obj) ;
-             if (!anEngine->_is_nil())  {
-               SALOMEDS::unlock();
-               anEngine->Close(sco);
-               SALOMEDS::lock();
+  if (_impl->GetDocument()) {
+    SALOMEDS::SComponentIterator_var itcomponent = NewComponentIterator();
+    for (; itcomponent->More(); itcomponent->Next()) {
+      SALOMEDS::SComponent_var sco = itcomponent->Value();
+      CORBA::String_var compodatatype=sco->ComponentDataType();
+      MESSAGE ( "Look for an engine for data type :"<< compodatatype);
+      // if there is an associated Engine call its method for closing
+      CORBA::String_var IOREngine;
+      if (sco->ComponentIOR(IOREngine)) {
+        // we have found the associated engine to write the data
+        MESSAGE ( "We have found an engine for data type :"<< compodatatype);
+        //_narrow can throw a corba exception
+        try {
+             CORBA::Object_var obj = _orb->string_to_object(IOREngine);
+             if (!CORBA::is_nil(obj)) {
+               SALOMEDS::Driver_var anEngine = SALOMEDS::Driver::_narrow(obj) ;
+               if (!anEngine->_is_nil())  {
+                 SALOMEDS::unlock();
+                 anEngine->Close(sco);
+                 SALOMEDS::lock();
+               }
              }
-           }
-      }
-      catch (CORBA::Exception&) {
+        }
+        catch (CORBA::Exception&) {
+        }
       }
+      sco->UnRegister();
     }
-    sco->UnRegister();
-  }
 
-  //Does not need any more this iterator
-  itcomponent->UnRegister();
+    //Does not need any more this iterator
+    itcomponent->UnRegister();
+  }
 
   // Notify GUI that study is cleared
   SALOME_NamingService *aNamingService = KERNEL::getNamingService();
@@ -349,6 +354,12 @@ void SALOMEDS_Study_i::Clear()
     SALOMEDS::lock();
   }
 
+  _impl->Clear();
+  _impl->setNotifier(0);
+  delete _notifier;
+  delete _genObjRegister;
+  _notifier = NULL;
+
   _closed = true;
 }
 
@@ -360,6 +371,9 @@ void SALOMEDS_Study_i::Clear()
 bool SALOMEDS_Study_i::Open(const char* aUrl)
   throw(SALOME::SALOME_Exception)
 {
+  if (!_closed)
+    Clear();
+  Init();
   SALOMEDS::Locker lock;
 
   Unexpect aCatch(SalomeException);
@@ -367,6 +381,9 @@ bool SALOMEDS_Study_i::Open(const char* aUrl)
 
   bool res = _impl->Open(std::string(aUrl));
 
+  // update desktop title with new study name
+  NameChanged();
+
   if ( !res )
     THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
   return res;
@@ -377,16 +394,12 @@ bool SALOMEDS_Study_i::Open(const char* aUrl)
  *  Purpose  : Save a Study to it's persistent reference
  */
 //============================================================================
-CORBA::Boolean SALOMEDS_Study_i::Save(CORBA::Boolean theMultiFile)
-{
-  SALOMEDS::Locker lock;
-  return _impl->Save(_factory, theMultiFile);
-}
-
-CORBA::Boolean SALOMEDS_Study_i::SaveASCII(CORBA::Boolean theMultiFile)
+CORBA::Boolean SALOMEDS_Study_i::Save(CORBA::Boolean theMultiFile, CORBA::Boolean theASCII)
 {
   SALOMEDS::Locker lock;
-  return _impl->SaveASCII(_factory, theMultiFile);
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();
+  return _impl->Save(_factory, theMultiFile, theASCII);
 }
 
 //=============================================================================
@@ -394,16 +407,12 @@ CORBA::Boolean SALOMEDS_Study_i::SaveASCII(CORBA::Boolean theMultiFile)
  *  Purpose  : Save a study to the persistent reference aUrl
  */
 //============================================================================
-CORBA::Boolean SALOMEDS_Study_i::SaveAs(const char* aUrl, CORBA::Boolean theMultiFile)
-{
-  SALOMEDS::Locker lock;
-  return _impl->SaveAs(std::string(aUrl), _factory, theMultiFile);
-}
-
-CORBA::Boolean SALOMEDS_Study_i::SaveAsASCII(const char* aUrl, CORBA::Boolean theMultiFile)
+CORBA::Boolean SALOMEDS_Study_i::SaveAs(const char* aUrl, CORBA::Boolean theMultiFile, CORBA::Boolean theASCII)
 {
   SALOMEDS::Locker lock;
-  return _impl->SaveAsASCII(std::string(aUrl), _factory, theMultiFile);
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();
+  return _impl->SaveAs(std::string(aUrl), _factory, theMultiFile, theASCII);
 }
 
 //============================================================================
@@ -414,6 +423,8 @@ CORBA::Boolean SALOMEDS_Study_i::SaveAsASCII(const char* aUrl, CORBA::Boolean th
 CORBA::Boolean SALOMEDS_Study_i::CanCopy(SALOMEDS::SObject_ptr theObject)
 {
   SALOMEDS::Locker lock;
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();
 
   CORBA::String_var anID = theObject->GetID();
   SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
@@ -432,6 +443,8 @@ CORBA::Boolean SALOMEDS_Study_i::CanCopy(SALOMEDS::SObject_ptr theObject)
 CORBA::Boolean SALOMEDS_Study_i::Copy(SALOMEDS::SObject_ptr theObject)
 {
   SALOMEDS::Locker lock;
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();
 
   CORBA::String_var anID = theObject->GetID();
   SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
@@ -450,6 +463,8 @@ CORBA::Boolean SALOMEDS_Study_i::Copy(SALOMEDS::SObject_ptr theObject)
 CORBA::Boolean SALOMEDS_Study_i::CanPaste(SALOMEDS::SObject_ptr theObject)
 {
   SALOMEDS::Locker lock;
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();
 
   CORBA::String_var anID = theObject->GetID();
   SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
@@ -518,18 +533,6 @@ char* SALOMEDS_Study_i::GetPersistentReference()
     throw SALOMEDS::Study::StudyInvalidReference();  
   return CORBA::string_dup(_impl->GetPersistentReference().c_str());
 }
-//============================================================================
-/*! Function : GetTransientReference
- *  Purpose  : Get IOR of the Study (registred in OCAF document in doc->Root)
- */
-//============================================================================
-char* SALOMEDS_Study_i::GetTransientReference()
-{
-  SALOMEDS::Locker lock; 
-  if (_closed)
-    throw SALOMEDS::Study::StudyInvalidReference();  
-  return CORBA::string_dup(_impl->GetTransientReference().c_str());
-}
 
 //============================================================================
 /*! Function : IsEmpty
@@ -761,157 +764,6 @@ char* SALOMEDS_Study_i::GetObjectPath(CORBA::Object_ptr theObject)
   return CORBA::string_dup(aPath.c_str());
 }
 
-
-//============================================================================
-/*! Function : SetContext
- *  Purpose  : Sets the current context
- */
-//============================================================================
-void SALOMEDS_Study_i::SetContext(const char* thePath) 
-{
-  SALOMEDS::Locker lock; 
-
-  if (_closed)
-    throw SALOMEDS::Study::StudyInvalidReference();  
-
-  _impl->SetContext(std::string((char*)thePath));
-  if (_impl->IsError() && _impl->GetErrorCode() == "InvalidContext") 
-    throw SALOMEDS::Study::StudyInvalidContext();  
-}
-
-//============================================================================
-/*! Function : GetContext
- *  Purpose  : Gets the current context
- */
-//============================================================================
-char* SALOMEDS_Study_i::GetContext() 
-{
-  SALOMEDS::Locker lock; 
-  
-  if (_closed)
-    throw SALOMEDS::Study::StudyInvalidReference();  
-
-  if (!_impl->HasCurrentContext()) throw SALOMEDS::Study::StudyInvalidContext();
-
-  return CORBA::string_dup(_impl->GetContext().c_str());
-}
-
-//============================================================================
-/*! Function : GetObjectNames
- *  Purpose  : method to get all object names in the given context (or in the current context, if 'theContext' is empty)
- */
-//============================================================================
-SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetObjectNames(const char* theContext) 
-{
-  SALOMEDS::Locker lock; 
-
-  if (_closed)
-    throw SALOMEDS::Study::StudyInvalidReference();  
-
-  SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
-
-  if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
-    throw SALOMEDS::Study::StudyInvalidContext();
-  
-  std::vector<std::string> aSeq = _impl->GetObjectNames(std::string((char*)theContext));
-  if (_impl->GetErrorCode() == "InvalidContext")
-    throw SALOMEDS::Study::StudyInvalidContext();
-
-  int aLength = aSeq.size();
-  aResult->length(aLength);
-  for (int anIndex = 0; anIndex < aLength; anIndex++) {
-    aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
-  }
-
-  return aResult._retn();
-}
-
-//============================================================================
-/*! Function : GetDirectoryNames
- *  Purpose  : method to get all directory names in the given context (or in the current context, if 'theContext' is empty)
- */
-//============================================================================
-SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetDirectoryNames(const char* theContext) 
-{
-  SALOMEDS::Locker lock; 
-
-  if (_closed)
-    throw SALOMEDS::Study::StudyInvalidReference();  
-
-  SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
-
-  if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
-    throw SALOMEDS::Study::StudyInvalidContext();
-  
-  std::vector<std::string> aSeq = _impl->GetDirectoryNames(std::string((char*)theContext));
-  if (_impl->GetErrorCode() == "InvalidContext")
-    throw SALOMEDS::Study::StudyInvalidContext();
-
-  int aLength = aSeq.size();
-  aResult->length(aLength);
-  for (int anIndex = 0; anIndex < aLength; anIndex++) {
-    aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
-  }
-  
-  return aResult._retn();
-}
-
-//============================================================================
-/*! Function : GetFileNames
- *  Purpose  : method to get all file names in the given context (or in the current context, if 'theContext' is empty)
- */
-//============================================================================
-SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetFileNames(const char* theContext) 
-{
-  SALOMEDS::Locker lock; 
-
-  if (_closed)
-    throw SALOMEDS::Study::StudyInvalidReference();  
-
-  SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
-
-  if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
-    throw SALOMEDS::Study::StudyInvalidContext();
-  
-  std::vector<std::string> aSeq = _impl->GetFileNames(std::string((char*)theContext));
-  if (_impl->GetErrorCode() == "InvalidContext")
-    throw SALOMEDS::Study::StudyInvalidContext();
-
-  int aLength = aSeq.size();
-  aResult->length(aLength);
-  for (int anIndex = 0; anIndex < aLength; anIndex++) {
-    aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
-  }
-
-  return aResult._retn();
-}
-
-//============================================================================
-/*! Function : GetComponentNames
- *  Purpose  : method to get all components names
- *  SRN:       Note, theContext can be any, it doesn't matter
- */
-//============================================================================
-SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetComponentNames(const char* theContext) 
-{
-  SALOMEDS::Locker lock; 
-
-  if (_closed)
-    throw SALOMEDS::Study::StudyInvalidReference();  
-
-  SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
-
-  std::vector<std::string> aSeq = _impl->GetComponentNames(std::string((char*)theContext));
-
-  int aLength = aSeq.size();
-  aResult->length(aLength);
-  for(int anIndex = 0; anIndex < aLength; anIndex++) {
-    aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
-  }
-
-  return aResult._retn();
-}
-
 //============================================================================
 /*! Function : NewChildIterator
  *  Purpose  : Create a ChildIterator from an SObject
@@ -1060,6 +912,9 @@ void SALOMEDS_Study_i::URL(const char* url)
   SALOMEDS::Locker lock; 
   // URL is specified as IDL attribute: user exception cannot be raised
   _impl->URL(std::string((char*)url));
+
+  // update desktop title with new study name
+  NameChanged();
 }
 
 void SALOMEDS_Study_i::UpdateIORLabelMap(const char* anIOR, const char* anEntry) 
@@ -1072,23 +927,6 @@ void SALOMEDS_Study_i::UpdateIORLabelMap(const char* anIOR, const char* anEntry)
   _impl->UpdateIORLabelMap(std::string((char*)anIOR), std::string((char*)anEntry));
 }
 
-SALOMEDS::Study_ptr SALOMEDS_Study_i::GetStudy(const DF_Label& theLabel, CORBA::ORB_ptr orb)
-{
-  SALOMEDS::Locker lock;
-
-  SALOMEDSImpl_AttributeIOR* Att = NULL;
-  if ((Att=(SALOMEDSImpl_AttributeIOR*)theLabel.Root().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))){
-    char* IOR = CORBA::string_dup(Att->Value().c_str());
-    CORBA::Object_var obj = orb->string_to_object(IOR);
-    SALOMEDS::Study_ptr aStudy = SALOMEDS::Study::_narrow(obj) ;
-    ASSERT(!CORBA::is_nil(aStudy));
-    return SALOMEDS::Study::_duplicate(aStudy);
-  } else {
-    MESSAGE("GetStudy: Problem to get study");
-  }
-  return SALOMEDS::Study::_nil();
-}
-
 void SALOMEDS_Study_i::IORUpdated(SALOMEDSImpl_AttributeIOR* theAttribute) 
 {
   SALOMEDS::Locker lock; 
@@ -1768,3 +1606,19 @@ CORBA::LongLong SALOMEDS_Study_i::GetLocalImpl(const char* theHostname, CORBA::L
   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
   return reinterpret_cast<CORBA::LongLong>(_impl);
 }
+
+void SALOMEDS_Study_i::NameChanged()
+{
+  // Notify GUI that the name of study was changed
+  SALOME_NamingService *aNamingService = KERNEL::getNamingService();
+  CORBA::Object_var obj = aNamingService->Resolve("/Kernel/Session");
+  SALOME::Session_var aSession = SALOME::Session::_narrow(obj);
+  if ( !CORBA::is_nil(aSession) ) {
+    std::stringstream ss;
+    ss << "studyNameChanged";
+    std::string str = ss.str();
+    SALOMEDS::unlock();
+    aSession->emitMessageOneWay(str.c_str());
+    SALOMEDS::lock();
+  }
+}