]> SALOME platform Git repositories - modules/gui.git/blob - src/Session/SalomeApp_Engine_i.cxx
Salome HOME
e861236f795ef804218052c4a5e54963eb1d2958
[modules/gui.git] / src / Session / SalomeApp_Engine_i.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 //  SalomeApp_Engine_i : implementation of SalomeApp_Engine.idl
23 //  File   : SalomeApp_Engine_i.cxx
24 //  Author : Alexander SLADKOV
25 //  Module : SALOME
26 //  $Header$
27 //
28 #include "SalomeApp_Engine_i.hxx"
29
30 #include "SALOMEDS_Tool.hxx"
31
32 #include <iostream>
33
34 using namespace std;
35
36 SalomeApp_Engine_i* SalomeApp_Engine_i::myInstance = NULL;
37
38 /*!
39   Constructor
40 */
41 SalomeApp_Engine_i::SalomeApp_Engine_i()
42 {
43   myInstance = this;
44 }
45
46 /*!
47   Destructor
48 */
49 SalomeApp_Engine_i::~SalomeApp_Engine_i()
50 {
51 }
52
53 SALOMEDS::TMPFile* SalomeApp_Engine_i::Save (SALOMEDS::SComponent_ptr theComponent,
54                                              const char* theURL,
55                                              bool isMultiFile)
56 {
57   SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
58
59   cout << "SalomeApp_Engine_i::Save() isMultiFile = " << isMultiFile << endl;
60   if (CORBA::is_nil(theComponent) || CORBA::is_nil(theComponent->GetStudy()))
61     return aStreamFile._retn();
62
63   const int studyId = theComponent->GetStudy()->StudyId();
64   cout << "SalomeApp_Engine_i::Save() - studyId = " << studyId << endl;
65
66   // Get a temporary directory to store a file
67   //std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
68
69   if (myMap.count(studyId)) {
70     cout << "SalomeApp_Engine_i::Save() - myMap.count(studyId)" << endl;
71     MapOfListOfFiles mapOfListOfFiles = myMap[studyId];
72     std::string componentName (theComponent->ComponentDataType());
73     cout << "SalomeApp_Engine_i::Save() - componentName = " << componentName << endl;
74     ListOfFiles listOfFiles = mapOfListOfFiles[componentName];
75
76     // listOfFiles must contain temporary directory name in its first item
77     // and names of files (relatively the temporary directory) in the others
78     const int n = listOfFiles.size() - 1;
79
80     if (n > 0) { // there are some files, containing persistent data of the component
81       std::string aTmpDir = listOfFiles[0];
82       cout << "SalomeApp_Engine_i::Save() - aTmpDir = " << aTmpDir << endl;
83
84       // Create a list to store names of created files
85       SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
86       aSeq->length(n);
87       for (int i = 0; i < n; i++)
88         aSeq[i] = CORBA::string_dup(listOfFiles[i + 1].c_str());
89
90       // Convert a file to the byte stream
91       aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq.in(), isMultiFile);
92
93       // Remove the files and tmp directory, created by the component storage procedure
94       if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
95     }
96   }
97
98   return aStreamFile._retn();
99 }
100
101 CORBA::Boolean SalomeApp_Engine_i::Load (SALOMEDS::SComponent_ptr theComponent,
102                                          const SALOMEDS::TMPFile& theFile,
103                                          const char* theURL,
104                                          bool isMultiFile)
105 {
106   cout << "SalomeApp_Engine_i::Load() isMultiFile = " << isMultiFile << endl;
107   if (CORBA::is_nil(theComponent) || CORBA::is_nil(theComponent->GetStudy()))
108     return false;
109
110   const int studyId = theComponent->GetStudy()->StudyId();
111
112   // Create a temporary directory for the component's data files
113   std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
114
115   // Convert the byte stream theStream to a files and place them in the tmp directory.
116   // The files and temporary directory must be deleted by the component loading procedure.
117   SALOMEDS::ListOfFileNames_var aSeq =
118     SALOMEDS_Tool::PutStreamToFiles(theFile, aTmpDir.c_str(), isMultiFile);
119
120   // Store list of file names to be used by the component loading procedure
121   const int n = aSeq->length() + 1;
122   ListOfFiles listOfFiles (n);
123   listOfFiles[0] = aTmpDir;
124   for (int i = 1; i < n; i++)
125     listOfFiles[i] = std::string(aSeq[i - 1]);
126
127   //MapOfListOfFiles mapOfListOfFiles;
128   //if (myMap.count(studyId))
129   //  mapOfListOfFiles = myMap[studyId];
130   //std::string componentName (theComponent->ComponentDataType());
131   //mapOfListOfFiles[componentName] = listOfFiles;
132   //myMap[studyId] = mapOfListOfFiles;
133
134   SetListOfFiles(listOfFiles, studyId, theComponent->ComponentDataType());
135
136   return true;
137 }
138
139 SalomeApp_Engine_i::ListOfFiles SalomeApp_Engine_i::GetListOfFiles (const int theStudyId,
140                                                                     const char* theComponentName)
141 {
142   ListOfFiles aListOfFiles;
143
144   if (myMap.count(theStudyId))
145   {
146     MapOfListOfFiles mapOfListOfFiles = myMap[theStudyId];
147     std::string componentName (theComponentName);
148     if (mapOfListOfFiles.count(componentName))
149       aListOfFiles = mapOfListOfFiles[componentName];
150   }
151
152   return aListOfFiles;
153 }
154
155 void SalomeApp_Engine_i::SetListOfFiles (const ListOfFiles theListOfFiles,
156                                          const int   theStudyId,
157                                          const char* theComponentName)
158 {
159   //if (!myMap.count(theStudyId)) {
160   //  MapOfListOfFiles mapOfListOfFiles;
161   //  myMap[theStudyId] = mapOfListOfFiles;
162   //}
163
164   MapOfListOfFiles& mapOfListOfFiles = myMap[theStudyId];
165   std::string componentName (theComponentName);
166   mapOfListOfFiles[componentName] = theListOfFiles;
167 }
168
169 /*!
170   \return shared instance of engine
171 */
172 SalomeApp_Engine_i* SalomeApp_Engine_i::GetInstance()
173 {
174   return myInstance;
175 }