Salome HOME
Python 3: PoC to correctly manage unicode strings from Python
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_StudyManager.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 //  File   : SALOMEDSClient_StudyManager.cxx
24 //  Author : Sergey RUIN
25 //  Module : SALOME
26 //
27 #include "SALOMEDS_StudyManager.hxx"
28
29 #include "SALOMEDS.hxx"
30 #include "SALOMEDS_Study.hxx"
31 #include "SALOMEDS_SObject.hxx"
32 #include "SALOMEDS_Driver_i.hxx"
33
34 #include "SALOMEDSImpl_Study.hxx"
35
36 #include "Utils_ORB_INIT.hxx"
37 #include "Utils_SINGLETON.hxx"
38 #include "utilities.h"
39
40 #include "Basics_Utils.hxx"
41
42 #ifdef WIN32
43 #include <process.h>
44 #else
45 #include <sys/types.h>
46 #include <unistd.h>
47 #endif
48
49 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb);
50
51 SALOMEDS_StudyManager::SALOMEDS_StudyManager(SALOMEDS::StudyManager_ptr theManager)
52 {
53
54 #ifdef WIN32
55   long pid =  (long)_getpid();
56 #else
57   long pid =  (long)getpid();
58 #endif
59
60   CORBA::LongLong addr = theManager->GetLocalImpl(Kernel_Utils::GetHostname().c_str(), pid, _isLocal);
61   if(_isLocal) {
62     _local_impl = reinterpret_cast<SALOMEDSImpl_StudyManager*>(addr);
63     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
64   }
65   else {
66     _local_impl = NULL;
67     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
68   }
69
70   init_orb();
71 }
72
73 SALOMEDS_StudyManager::SALOMEDS_StudyManager()
74 {
75   init_orb();
76
77   SALOME_NamingService namingService(_orb);
78   CORBA::Object_var obj = namingService.Resolve( "/myStudyManager" );
79   SALOMEDS::StudyManager_var theManager = SALOMEDS::StudyManager::_narrow( obj );
80   ASSERT( !CORBA::is_nil(theManager) );
81
82 #ifdef WIN32
83   long pid =  (long)_getpid();
84 #else
85   long pid =  (long)getpid();
86 #endif
87
88   CORBA::LongLong addr = theManager->GetLocalImpl(Kernel_Utils::GetHostname().c_str(), pid, _isLocal);
89   if(_isLocal) {
90     _local_impl = reinterpret_cast<SALOMEDSImpl_StudyManager*>(addr);
91     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
92   }
93   else {
94     _local_impl = NULL;
95     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
96   }
97 }
98
99 SALOMEDS_StudyManager::~SALOMEDS_StudyManager()
100 {
101 }
102
103 _PTR(Study) SALOMEDS_StudyManager::NewStudy(const std::string& study_name)
104 {
105   //SRN: Pure CORBA NewStudy as it does more initialization than the local one
106   SALOMEDSClient_Study* aStudy = NULL;
107
108   SALOMEDS::Study_var aStudy_impl = _corba_impl->NewStudy((char*)study_name.c_str());
109   if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
110   aStudy = new SALOMEDS_Study(aStudy_impl);
111
112   return _PTR(Study)(aStudy);
113 }
114
115 _PTR(Study) SALOMEDS_StudyManager::Open(const std::string& theStudyUrl)
116 {
117   //SRN: Pure CORBA Open as it does more initialization than the local one
118   SALOMEDSClient_Study* aStudy = NULL;
119
120   std::wstring wtheStudyUrl = std::wstring(theStudyUrl.begin(), theStudyUrl.end());
121   SALOMEDS::Study_var aStudy_impl = _corba_impl->Open((wchar_t*)theStudyUrl.c_str());
122   if(CORBA::is_nil(aStudy_impl)) return  _PTR(Study)(aStudy);
123
124   aStudy = new SALOMEDS_Study(aStudy_impl.in());
125
126   return _PTR(Study)(aStudy);
127 }
128
129 void SALOMEDS_StudyManager::Close(const _PTR(Study)& theStudy)
130 {
131   //SRN: Pure CORBA close as it does more cleaning than the local one
132   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
133   _corba_impl->Close(aStudy);
134 }
135
136 bool SALOMEDS_StudyManager::Save(const _PTR(Study)& theStudy, bool theMultiFile)
137 {
138   //SRN: Pure CORBA save as the save operation require CORBA in any case
139   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
140   return _corba_impl->Save(aStudy, theMultiFile);
141 }
142
143 bool SALOMEDS_StudyManager::SaveASCII(const _PTR(Study)& theStudy, bool theMultiFile)
144 {
145   //SRN: Pure CORBA save as the save operation require CORBA in any case
146   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
147   return _corba_impl->SaveASCII(aStudy, theMultiFile);
148 }
149
150 bool SALOMEDS_StudyManager::SaveAs(const std::string& theUrl,  const _PTR(Study)& theStudy, bool theMultiFile)
151 {
152   //SRN: Pure CORBA save as the save operation require CORBA in any case
153   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
154   return _corba_impl->SaveAs((char*)theUrl.c_str(), aStudy, theMultiFile);
155 }
156
157 bool SALOMEDS_StudyManager::SaveAsASCII(const std::string& theUrl,  const _PTR(Study)& theStudy, bool theMultiFile)
158 {
159   //SRN: Pure CORBA save as the save operation require CORBA in any case
160   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
161   return _corba_impl->SaveAsASCII((char*)theUrl.c_str(), aStudy, theMultiFile);
162 }
163
164 std::vector<std::string> SALOMEDS_StudyManager::GetOpenStudies()
165 {
166   std::vector<std::string> aVector;
167   int aLength, i;
168
169   if (_isLocal) {
170     SALOMEDS::Locker lock;
171
172     std::vector<SALOMEDSImpl_Study*> aSeq = _local_impl->GetOpenStudies();
173     aLength = aSeq.size();
174     for(i = 0; i < aLength; i++)
175       aVector.push_back(aSeq[i]->Name());
176   }
177   else {
178     SALOMEDS::ListOfOpenStudies_var aSeq = _corba_impl->GetOpenStudies();
179     aLength = aSeq->length();
180     for(i = 0; i < aLength; i++)
181       aVector.push_back(aSeq[i].in());
182   }
183   return aVector;
184 }
185
186 _PTR(Study) SALOMEDS_StudyManager::GetStudyByName(const std::string& theStudyName)
187 {
188   SALOMEDSClient_Study* aStudy = NULL;
189   if (_isLocal) {
190     SALOMEDS::Locker lock;
191
192     SALOMEDSImpl_Study* aStudy_impl = _local_impl->GetStudyByName(theStudyName);
193     if(!aStudy_impl) return _PTR(Study)(aStudy);
194     aStudy = new SALOMEDS_Study(aStudy_impl);
195   }
196   else  {
197     SALOMEDS::Study_var aStudy_impl = _corba_impl->GetStudyByName((char*)theStudyName.c_str());
198     if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
199     aStudy = new SALOMEDS_Study(aStudy_impl);
200   }
201   return _PTR(Study)(aStudy);
202 }
203
204 _PTR(Study) SALOMEDS_StudyManager::GetStudyByID(int theStudyID)
205 {
206   SALOMEDSClient_Study* aStudy = NULL;
207   if (_isLocal) {
208     SALOMEDS::Locker lock;
209
210     SALOMEDSImpl_Study* aStudy_impl = _local_impl->GetStudyByID(theStudyID);
211     if(!aStudy_impl) return _PTR(Study)(aStudy);
212     aStudy = new SALOMEDS_Study(aStudy_impl);
213   }
214   else {
215     SALOMEDS::Study_var aStudy_impl = _corba_impl->GetStudyByID(theStudyID);
216     if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
217     aStudy = new SALOMEDS_Study(aStudy_impl);
218   }
219   return _PTR(Study)(aStudy);
220 }
221
222 bool SALOMEDS_StudyManager::CanCopy(const _PTR(SObject)& theSO)
223 {
224   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
225   bool ret;
226
227   if (_isLocal) {
228     SALOMEDS::Locker lock;
229
230     SALOMEDSImpl_SObject aSO_impl = *(aSO->GetLocalImpl());
231     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
232     ret = _local_impl->CanCopy(aSO_impl, aDriver);
233     delete aDriver;
234   }
235   else {
236     ret = _corba_impl->CanCopy(aSO->GetCORBAImpl());
237   }
238
239   return ret;
240 }
241
242 bool SALOMEDS_StudyManager::Copy(const _PTR(SObject)& theSO)
243 {
244   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
245   bool ret;
246   if (_isLocal) {
247     SALOMEDS::Locker lock;
248
249     SALOMEDSImpl_SObject aSO_impl = *(aSO->GetLocalImpl());
250     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
251     ret = _local_impl->Copy(aSO_impl, aDriver);
252     delete aDriver;
253   }
254   else {
255     ret = _corba_impl->Copy(aSO->GetCORBAImpl());
256   }
257   return ret;
258 }
259
260 bool SALOMEDS_StudyManager::CanPaste(const _PTR(SObject)& theSO)
261 {
262   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
263   bool ret;
264
265   if (_isLocal) {
266     SALOMEDS::Locker lock;
267
268     SALOMEDSImpl_SObject aSO_impl = *(aSO->GetLocalImpl());
269     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
270     ret = _local_impl->CanPaste(aSO_impl, aDriver);
271     delete aDriver;
272   }
273   else {
274     ret = _corba_impl->CanPaste(aSO->GetCORBAImpl());
275   }
276
277   return ret;
278 }
279
280 _PTR(SObject) SALOMEDS_StudyManager::Paste(const _PTR(SObject)& theSO)
281 {
282   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
283   SALOMEDSClient_SObject* aResult = NULL;
284
285   if (_isLocal) {
286     SALOMEDS::Locker lock;
287
288     SALOMEDSImpl_SObject aSO_impl = *(aSO->GetLocalImpl());
289     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
290     SALOMEDSImpl_SObject aNewSO = _local_impl->Paste(aSO_impl, aDriver);
291     delete aDriver;
292     if(aNewSO.IsNull()) return _PTR(SObject)(aResult);
293     aResult = new SALOMEDS_SObject(aNewSO);
294   }
295   else {
296     SALOMEDS::SObject_ptr aNewSO = _corba_impl->Paste(aSO->GetCORBAImpl());
297     if(CORBA::is_nil(aNewSO)) return _PTR(SObject)(aResult);
298     aResult = new SALOMEDS_SObject(aNewSO);
299   }
300
301   return _PTR(SObject)(aResult);
302 }
303
304
305 void SALOMEDS_StudyManager::init_orb()
306 {
307   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
308   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
309   _orb = init(0 , 0 );
310 }
311
312 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
313 {
314   SALOMEDS_Driver_i* driver = NULL;
315
316   SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
317   if(!aSCO.IsNull()) {
318     std::string IOREngine = aSCO.GetIOR();
319     if(!IOREngine.empty()) {
320       CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
321       Engines::EngineComponent_var Engine = Engines::EngineComponent::_narrow(obj) ;
322       driver = new SALOMEDS_Driver_i(Engine, orb);
323     }
324   }
325
326   return driver;
327 }