Salome HOME
Initial version
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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.h>
36
37 SalomeApp_Engine_i* SalomeApp_Engine_i::myInstance = NULL;
38
39 SalomeApp_Engine_i::SalomeApp_Engine_i()
40 {
41   myInstance = this;
42 }
43
44 SalomeApp_Engine_i::~SalomeApp_Engine_i()
45 {
46 }
47
48 SALOMEDS::TMPFile* SalomeApp_Engine_i::Save (SALOMEDS::SComponent_ptr theComponent,
49                                              const char* theURL,
50                                              bool isMultiFile)
51 {
52   SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
53
54   cout << "SalomeApp_Engine_i::Save() isMultiFile = " << isMultiFile << endl;
55   if (CORBA::is_nil(theComponent) || CORBA::is_nil(theComponent->GetStudy()))
56     return aStreamFile._retn();
57
58   const int studyId = theComponent->GetStudy()->StudyId();
59   cout << "SalomeApp_Engine_i::Save() - studyId = " << studyId << endl;
60
61   // Get a temporary directory to store a file
62   //std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
63
64   if (myMap.count(studyId)) {
65     cout << "SalomeApp_Engine_i::Save() - myMap.count(studyId)" << endl;
66     MapOfListOfFiles mapOfListOfFiles = myMap[studyId];
67     std::string componentName (theComponent->ComponentDataType());
68     cout << "SalomeApp_Engine_i::Save() - componentName = " << componentName << endl;
69     ListOfFiles listOfFiles = mapOfListOfFiles[componentName];
70
71     // listOfFiles must contain temporary directory name in its first item
72     // and names of files (relatively the temporary directory) in the others
73     const int n = listOfFiles.size() - 1;
74
75     if (n > 0) { // there are some files, containing persistent data of the component
76       std::string aTmpDir = listOfFiles[0];
77       cout << "SalomeApp_Engine_i::Save() - aTmpDir = " << aTmpDir << endl;
78
79       // Create a list to store names of created files
80       SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
81       aSeq->length(n);
82       for (int i = 0; i < n; i++)
83         aSeq[i] = CORBA::string_dup(listOfFiles[i + 1].c_str());
84
85       // Convert a file to the byte stream
86       aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq.in(), isMultiFile);
87
88       // Remove the files and tmp directory, created by the component storage procedure
89       if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
90     }
91   }
92
93   return aStreamFile._retn();
94 }
95
96 CORBA::Boolean SalomeApp_Engine_i::Load (SALOMEDS::SComponent_ptr theComponent,
97                                          const SALOMEDS::TMPFile& theFile,
98                                          const char* theURL,
99                                          bool isMultiFile)
100 {
101   cout << "SalomeApp_Engine_i::Load() isMultiFile = " << isMultiFile << endl;
102   if (CORBA::is_nil(theComponent) || CORBA::is_nil(theComponent->GetStudy()))
103     return false;
104
105   const int studyId = theComponent->GetStudy()->StudyId();
106
107   // Create a temporary directory for the component's data files
108   std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
109
110   // Convert the byte stream theStream to a files and place them in the tmp directory.
111   // The files and temporary directory must be deleted by the component loading procedure.
112   SALOMEDS::ListOfFileNames_var aSeq =
113     SALOMEDS_Tool::PutStreamToFiles(theFile, aTmpDir.c_str(), isMultiFile);
114
115   // Store list of file names to be used by the component loading procedure
116   const int n = aSeq->length() + 1;
117   ListOfFiles listOfFiles (n);
118   listOfFiles[0] = aTmpDir;
119   for (int i = 1; i < n; i++)
120     listOfFiles[i] = std::string(aSeq[i - 1]);
121
122   //MapOfListOfFiles mapOfListOfFiles;
123   //if (myMap.count(studyId))
124   //  mapOfListOfFiles = myMap[studyId];
125   //std::string componentName (theComponent->ComponentDataType());
126   //mapOfListOfFiles[componentName] = listOfFiles;
127   //myMap[studyId] = mapOfListOfFiles;
128
129   SetListOfFiles(listOfFiles, studyId, theComponent->ComponentDataType());
130
131   return true;
132 }
133
134 SalomeApp_Engine_i::ListOfFiles SalomeApp_Engine_i::GetListOfFiles (const int theStudyId,
135                                                                     const char* theComponentName)
136 {
137   ListOfFiles aListOfFiles;
138
139   if (myMap.count(theStudyId))
140   {
141     MapOfListOfFiles mapOfListOfFiles = myMap[theStudyId];
142     std::string componentName (theComponentName);
143     if (mapOfListOfFiles.count(componentName))
144       aListOfFiles = mapOfListOfFiles[componentName];
145   }
146
147   return aListOfFiles;
148 }
149
150 void SalomeApp_Engine_i::SetListOfFiles (const ListOfFiles theListOfFiles,
151                                          const int   theStudyId,
152                                          const char* theComponentName)
153 {
154   //if (!myMap.count(theStudyId)) {
155   //  MapOfListOfFiles mapOfListOfFiles;
156   //  myMap[theStudyId] = mapOfListOfFiles;
157   //}
158
159   MapOfListOfFiles& mapOfListOfFiles = myMap[theStudyId];
160   std::string componentName (theComponentName);
161   mapOfListOfFiles[componentName] = theListOfFiles;
162 }
163
164 SalomeApp_Engine_i* SalomeApp_Engine_i::GetInstance()
165 {
166   return myInstance;
167 }