Salome HOME
Merge commit '4bda6c883bff2af152b1de8d7f6d4820780c2cd2'
[modules/gui.git] / src / SalomeApp / SalomeApp_Engine_i.cxx
index b26a8424cb7b8c9a0b736e6684cfd298b0159663..c9d9f9d1c45b2ba974997ff2bc653f09977ef151 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 
 #include "SalomeApp_Engine_i.h"
 #include "SalomeApp_Application.h"
+#include "SalomeApp_Study.h"
+#include "SUIT_Session.h"
+#include "CAM_Module.h"
+#include "LightApp_DataModel.h"
 
 #include <SALOME_NamingService.hxx>
 #include <SALOMEDS_Tool.hxx>
-#include <Utils_ORB_INIT.hxx>
-#include <Utils_SINGLETON.hxx>
+#include <ArgvKeeper.hxx>
+#include <OpUtil.hxx>
 #include <Utils_SALOME_Exception.hxx>
 #include <utilities.h>
 
   Constructor
 */
 SalomeApp_Engine_i::SalomeApp_Engine_i( const char* theComponentName )
+  : myKeepFiles( false ), myComponentName( theComponentName )
 {
-  myComponentName = theComponentName;
   MESSAGE("SalomeApp_Engine_i::SalomeApp_Engine_i(): myComponentName = " <<
-         myComponentName << ", this = " << this);
+         qPrintable( myComponentName ) << ", this = " << this);
 }
 
 /*!
@@ -56,53 +60,105 @@ SalomeApp_Engine_i::SalomeApp_Engine_i( const char* theComponentName )
 SalomeApp_Engine_i::~SalomeApp_Engine_i()
 {
   MESSAGE("SalomeApp_Engine_i::~SalomeApp_Engine_i(): myComponentName = " << 
-         myComponentName << ", this = " << this);
+         qPrintable( myComponentName ) << ", this = " << this);
 }
 
 SALOMEDS::TMPFile* SalomeApp_Engine_i::Save (SALOMEDS::SComponent_ptr theComponent,
-                                             const char* theURL,
+                                             const char* /*theURL*/,
                                              bool isMultiFile)
 {
   SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
 
-  if (CORBA::is_nil(theComponent) || CORBA::is_nil(theComponent->GetStudy()))
-    return aStreamFile._retn();
 
-  const int studyId = theComponent->GetStudy()->StudyId();
+  if (CORBA::is_nil(theComponent))
+    return aStreamFile._retn();
+  
+  // Component type
+  QString componentName (theComponent->ComponentDataType());
 
+  // Error somewhere outside - Save() called with wrong SComponent instance
+  if ( myComponentName != componentName )
+    return aStreamFile._retn();
+    
   // Get a temporary directory to store a file
   //std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
 
-  if (myMap.count(studyId)) {
-    std::string componentName (theComponent->ComponentDataType());
+  bool manuallySaved = false;
 
-    // Error somewhere outside - Save() called with
-    // wrong SComponent instance
-    if ( myComponentName != componentName )
-      return aStreamFile._retn();
+  if ( GetListOfFiles(0).empty() ) // 0 means persistence file
+  { 
 
-    const ListOfFiles& listOfFiles = myMap[studyId];
+    // Save was probably called from outside GUI, so SetListOfFiles was not called!
+    // Try to get list of files from directly from data model
 
-    // listOfFiles must contain temporary directory name in its first item
-    // and names of files (relatively the temporary directory) in the others
-    const int n = listOfFiles.size() - 1;
+    MESSAGE("SalomeApp_Engine_i::Save(): myComponentName = " <<
+            qPrintable( myComponentName ) <<
+            "it seems Save() was called from outside GUI" );
 
-    if (n > 0) { // there are some files, containing persistent data of the component
-      std::string aTmpDir = listOfFiles[0];
+    // - Get app
+    SalomeApp_Application* app = 
+      dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
+    if ( !app )
+      return aStreamFile._retn();
 
-      // Create a list to store names of created files
-      SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
-      aSeq->length(n);
-      for (int i = 0; i < n; i++)
-        aSeq[i] = CORBA::string_dup(listOfFiles[i + 1].c_str());
+    // - Get study
+    SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
 
-      // Convert a file to the byte stream
-      aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq.in(), isMultiFile);
+    if ( !study )
+      return aStreamFile._retn();
+    QString url = QString::fromStdString(study->studyDS()->URL());
 
-      // Remove the files and tmp directory, created by the component storage procedure
-      if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
+    // - Get module
+    CAM_Module* module = app->module( SalomeApp_Application::moduleTitle( componentName ) );
+    if ( !module ) // load module???
+      return aStreamFile._retn();
+    // - Get data model
+    LightApp_DataModel* dataModel = dynamic_cast<LightApp_DataModel*>( module->dataModel() );
+    if ( !dataModel )
+      return aStreamFile._retn();
+    // - Save data files
+    QStringList dataFiles;
+    // we use 'url' instead of 'theURL' as latter normally contains path to the tmp dir,
+    // but not actual study's URL
+    dataModel->saveAs( url, study, dataFiles );
+    std::vector<std::string> names;
+    foreach ( QString name, dataFiles ) {
+      if ( !name.isEmpty() )
+        names.push_back(name.toUtf8().data());
     }
+    SetListOfFiles( 0, names ); // 0 means persistence file
+    manuallySaved = true;
+  }
+
+  // Get a temporary directory to store a file
+  //std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
+
+  // listOfFiles must contain temporary directory name in its first item
+  // and names of files (relatively the temporary directory) in the others
+  ListOfFiles listOfFiles = GetListOfFiles( 0 ); // 0 means persistence file
+  const int n = (int)listOfFiles.size() - 1; //!< TODO: conversion from size_t to int
+  
+  if (n > 0) { // there are some files, containing persistent data of the component
+    std::string aTmpDir = listOfFiles[0];
+    
+    // Create a list to store names of created files
+    ListOfFiles aSeq;
+    aSeq.reserve(n);
+    for (int i = 0; i < n; i++)
+      aSeq.push_back(CORBA::string_dup(listOfFiles[i + 1].c_str()));
+    
+    // Convert a file to the byte stream
+    aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq, isMultiFile);
+    
+    // Remove the files and tmp directory, created by the component storage procedure
+    SalomeApp_Application* app = 
+      dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
+    SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
+    study->RemoveTemporaryFiles( myComponentName.toStdString().c_str(), isMultiFile );
   }
+  
+  if ( manuallySaved )
+    SetListOfFiles(0, ListOfFiles()); // 0 means persistence file
 
   return aStreamFile._retn();
 }
@@ -113,65 +169,55 @@ CORBA::Boolean SalomeApp_Engine_i::Load (SALOMEDS::SComponent_ptr theComponent,
                                          bool isMultiFile)
 {
   std::cout << "SalomeApp_Engine_i::Load() isMultiFile = " << isMultiFile << std::endl;
-  if (CORBA::is_nil(theComponent) || CORBA::is_nil(theComponent->GetStudy()))
+  if (CORBA::is_nil(theComponent))
     return false;
 
   // Error somewhere outside - Load() called with
   // wrong SComponent instance
-  std::string componentName (theComponent->ComponentDataType());
+  QString componentName = theComponent->ComponentDataType();
   if ( myComponentName != componentName )
     return false;
 
-  const int studyId = theComponent->GetStudy()->StudyId();
-
   // Create a temporary directory for the component's data files
   std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
 
   // Convert the byte stream theStream to a files and place them in the tmp directory.
   // The files and temporary directory must be deleted by the component loading procedure.
-  SALOMEDS::ListOfFileNames_var aSeq =
+  ListOfFiles aSeq =
     SALOMEDS_Tool::PutStreamToFiles(theFile, aTmpDir.c_str(), isMultiFile);
 
   // Store list of file names to be used by the component loading procedure
-  const int n = aSeq->length() + 1;
+  const int n = (int)aSeq.size() + 1; //!< TODO: conversion from size_t to int
   ListOfFiles listOfFiles (n);
   listOfFiles[0] = aTmpDir;
   for (int i = 1; i < n; i++)
     listOfFiles[i] = std::string(aSeq[i - 1]);
 
-  SetListOfFiles(listOfFiles, studyId);
+  SetListOfFiles(0, listOfFiles); // 0 means persistence file
+  keepFiles( true );
 
   return true;
 }
 
-SalomeApp_Engine_i::ListOfFiles SalomeApp_Engine_i::GetListOfFiles (const int theStudyId)
+SalomeApp_Engine_i::ListOfFiles SalomeApp_Engine_i::GetListOfFiles(int type)
 {
-  ListOfFiles aListOfFiles;
-
-  if (myMap.find(theStudyId) != myMap.end())
-  {
-    aListOfFiles = myMap[theStudyId];
-  }
-
-  return aListOfFiles;
+  return myListOfFiles.count(type) ? myListOfFiles[type] : ListOfFiles();
 }
 
-void SalomeApp_Engine_i::SetListOfFiles (const ListOfFiles& theListOfFiles,
-                                         const int          theStudyId)
+void SalomeApp_Engine_i::SetListOfFiles (int type, const ListOfFiles& theListOfFiles)
 {
-  myMap[theStudyId] = theListOfFiles;
+  myListOfFiles[type] = theListOfFiles;
 }
 
 /*! 
  *  DumpPython implementation for light modules
  */
-Engines::TMPFile* SalomeApp_Engine_i::DumpPython(CORBA::Object_ptr theStudy, 
-                                                CORBA::Boolean isPublished, 
-                                                CORBA::Boolean isMultiFile, 
-                                                CORBA::Boolean& isValidScript)
+Engines::TMPFile* SalomeApp_Engine_i::DumpPython(CORBA::Boolean /*isPublished*/,
+                                                 CORBA::Boolean /*isMultiFile*/,
+                                                 CORBA::Boolean& isValidScript)
 {
   MESSAGE("SalomeApp_Engine_i::DumpPython(): myComponentName = "<<
-         myComponentName << ", this = " << this);
+         qPrintable( myComponentName ) << ", this = " << this);
   
   // Temporary solution: returning a non-empty sequence
   // even if there's nothing to dump, to avoid crashes in SALOMEDS
@@ -182,20 +228,11 @@ Engines::TMPFile* SalomeApp_Engine_i::DumpPython(CORBA::Object_ptr theStudy,
   aStreamFile[0] = '\0';
   isValidScript = true;
 
-  if (CORBA::is_nil(theStudy))
-    return aStreamFile._retn();
-
-  SALOMEDS::Study_var studyDS = SALOMEDS::Study::_narrow( theStudy );
-  const int studyId = studyDS->StudyId();
-
-  if (!myMap.count(studyId))
-    return aStreamFile._retn();
-
-  ListOfFiles listOfFiles = myMap[studyId];
+  ListOfFiles listOfFiles = GetListOfFiles( 1 ); // 1  means dump file
 
   // listOfFiles must contain temporary directory name in its first item
   // and names of files (relatively the temporary directory) in the others
-  if ( listOfFiles.size() < 2 ) 
+  if ( listOfFiles.size() < 2 )
     return aStreamFile._retn();
 
   // there are some files, containing persistent data of the component
@@ -276,7 +313,7 @@ Engines::TMPFile* SalomeApp_Engine_i::DumpPython(CORBA::Object_ptr theStudy,
 */
 char* SalomeApp_Engine_i::ComponentDataType()
 {
-  return const_cast<char*>( myComponentName.c_str() );
+  return CORBA::string_dup( myComponentName.toLatin1().constData() );
 }
 
 /*!
@@ -288,7 +325,7 @@ char* SalomeApp_Engine_i::getVersion()
   QString version;
   SalomeApp_Application::ModuleShortInfo version_info;
   foreach ( version_info, versions ) {
-    if ( SalomeApp_Application::moduleName( version_info.name ) == myComponentName.c_str() ) {
+    if ( SalomeApp_Application::moduleName( version_info.name ) == myComponentName ) {
       version = version_info.version;
       break;
     }
@@ -305,19 +342,9 @@ CORBA::ORB_var SalomeApp_Engine_i::orb()
   static CORBA::ORB_var _orb;
 
   if ( CORBA::is_nil( _orb ) ) {
-    QStringList args = QApplication::arguments();
-    int argc = args.size();
-    std::vector<std::string> args1(argc);
-    char** argv = new char*[argc];
-    for ( int i = 0; i < argc; ++i ) {
-      args1[i] = args[i].toStdString();
-      argv[i]  = const_cast<char*>( args1[i].c_str() );
-    }
-
-    ORB_INIT& init = *SINGLETON_<ORB_INIT>::Instance();
-    _orb = init( argc, argv );
-
-    delete [] argv;
+    Qtx::CmdLineArgs args;
+    SetArgcArgv( args.argc(), args.argv() );
+    _orb = KERNEL::GetRefToORB();
   }
 
   return _orb;
@@ -339,10 +366,9 @@ PortableServer::POA_var SalomeApp_Engine_i::poa()
 /*!
   \return 
 */
-SALOME_NamingService* SalomeApp_Engine_i::namingService()
+SALOME_NamingService_Abstract* SalomeApp_Engine_i::namingService()
 {
-  static SALOME_NamingService _ns(orb());
-  return &_ns;
+  return SalomeApp_Application::namingService();
 }
 
 /*!
@@ -361,6 +387,9 @@ CORBA::Object_ptr SalomeApp_Engine_i::EngineForComponent( const char* theCompone
   if ( !theComponentName || !strlen( theComponentName ) )
     return anEngine._retn();
 
+  if ( SalomeApp_Application::moduleTitle( theComponentName ).isEmpty() )
+    return anEngine._retn();
+
   std::string aPath( "/SalomeAppEngine/" );
   aPath += theComponentName;
   anEngine = namingService()->Resolve( aPath.c_str() );