Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/gui.git] / src / Session / SalomeApp_Engine_i.cxx
1 //  SalomeApp_Engine_i : implementation of SalomeApp_Engine.idl
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : SalomeApp_Engine_i.cxx
25 //  Author : Alexander SLADKOV
26 //  Module : SALOME
27 //  $Header$
28
29 #include "SalomeApp_Engine_i.hxx"
30
31 #include "SALOMEDS_Tool.hxx"
32
33 #include "utilities.h"
34
35 #include <iostream>
36
37 using namespace std;
38
39 SalomeApp_Engine_i* SalomeApp_Engine_i::myInstance = NULL;
40
41 /*!
42   Constructor
43 */
44 SalomeApp_Engine_i::SalomeApp_Engine_i()
45 {
46   myInstance = this;
47 }
48
49 /*!
50   Destructor
51 */
52 SalomeApp_Engine_i::~SalomeApp_Engine_i()
53 {
54 }
55
56 SALOMEDS::TMPFile* SalomeApp_Engine_i::Save (SALOMEDS::SComponent_ptr theComponent,
57                                              const char* theURL,
58                                              bool isMultiFile)
59 {
60   SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
61
62   cout << "SalomeApp_Engine_i::Save() isMultiFile = " << isMultiFile << endl;
63   if (CORBA::is_nil(theComponent) || CORBA::is_nil(theComponent->GetStudy()))
64     return aStreamFile._retn();
65
66   const int studyId = theComponent->GetStudy()->StudyId();
67   cout << "SalomeApp_Engine_i::Save() - studyId = " << studyId << endl;
68
69   // Get a temporary directory to store a file
70   //std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
71
72   if (myMap.count(studyId)) {
73     cout << "SalomeApp_Engine_i::Save() - myMap.count(studyId)" << endl;
74     MapOfListOfFiles mapOfListOfFiles = myMap[studyId];
75     std::string componentName (theComponent->ComponentDataType());
76     cout << "SalomeApp_Engine_i::Save() - componentName = " << componentName << endl;
77     ListOfFiles listOfFiles = mapOfListOfFiles[componentName];
78
79     // listOfFiles must contain temporary directory name in its first item
80     // and names of files (relatively the temporary directory) in the others
81     const int n = listOfFiles.size() - 1;
82
83     if (n > 0) { // there are some files, containing persistent data of the component
84       std::string aTmpDir = listOfFiles[0];
85       cout << "SalomeApp_Engine_i::Save() - aTmpDir = " << aTmpDir << endl;
86
87       // Create a list to store names of created files
88       SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
89       aSeq->length(n);
90       for (int i = 0; i < n; i++)
91         aSeq[i] = CORBA::string_dup(listOfFiles[i + 1].c_str());
92
93       // Convert a file to the byte stream
94       aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq.in(), isMultiFile);
95
96       // Remove the files and tmp directory, created by the component storage procedure
97       if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
98     }
99   }
100
101   return aStreamFile._retn();
102 }
103
104 CORBA::Boolean SalomeApp_Engine_i::Load (SALOMEDS::SComponent_ptr theComponent,
105                                          const SALOMEDS::TMPFile& theFile,
106                                          const char* theURL,
107                                          bool isMultiFile)
108 {
109   cout << "SalomeApp_Engine_i::Load() isMultiFile = " << isMultiFile << endl;
110   if (CORBA::is_nil(theComponent) || CORBA::is_nil(theComponent->GetStudy()))
111     return false;
112
113   const int studyId = theComponent->GetStudy()->StudyId();
114
115   // Create a temporary directory for the component's data files
116   std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
117
118   // Convert the byte stream theStream to a files and place them in the tmp directory.
119   // The files and temporary directory must be deleted by the component loading procedure.
120   SALOMEDS::ListOfFileNames_var aSeq =
121     SALOMEDS_Tool::PutStreamToFiles(theFile, aTmpDir.c_str(), isMultiFile);
122
123   // Store list of file names to be used by the component loading procedure
124   const int n = aSeq->length() + 1;
125   ListOfFiles listOfFiles (n);
126   listOfFiles[0] = aTmpDir;
127   for (int i = 1; i < n; i++)
128     listOfFiles[i] = std::string(aSeq[i - 1]);
129
130   //MapOfListOfFiles mapOfListOfFiles;
131   //if (myMap.count(studyId))
132   //  mapOfListOfFiles = myMap[studyId];
133   //std::string componentName (theComponent->ComponentDataType());
134   //mapOfListOfFiles[componentName] = listOfFiles;
135   //myMap[studyId] = mapOfListOfFiles;
136
137   SetListOfFiles(listOfFiles, studyId, theComponent->ComponentDataType());
138
139   return true;
140 }
141
142 SalomeApp_Engine_i::ListOfFiles SalomeApp_Engine_i::GetListOfFiles (const int theStudyId,
143                                                                     const char* theComponentName)
144 {
145   ListOfFiles aListOfFiles;
146
147   if (myMap.count(theStudyId))
148   {
149     MapOfListOfFiles mapOfListOfFiles = myMap[theStudyId];
150     std::string componentName (theComponentName);
151     if (mapOfListOfFiles.count(componentName))
152       aListOfFiles = mapOfListOfFiles[componentName];
153   }
154
155   return aListOfFiles;
156 }
157
158 void SalomeApp_Engine_i::SetListOfFiles (const ListOfFiles theListOfFiles,
159                                          const int   theStudyId,
160                                          const char* theComponentName)
161 {
162   //if (!myMap.count(theStudyId)) {
163   //  MapOfListOfFiles mapOfListOfFiles;
164   //  myMap[theStudyId] = mapOfListOfFiles;
165   //}
166
167   MapOfListOfFiles& mapOfListOfFiles = myMap[theStudyId];
168   std::string componentName (theComponentName);
169   mapOfListOfFiles[componentName] = theListOfFiles;
170 }
171
172 /*!
173   \return shared instance of engine
174 */
175 SalomeApp_Engine_i* SalomeApp_Engine_i::GetInstance()
176 {
177   return myInstance;
178 }