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