Salome HOME
Merge Python 3 porting: additional chnages.
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Driver_i.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 #include "SALOMEDS_Driver_i.hxx"
24 #include "SALOMEDS_TMPFile_i.hxx"
25 #include "utilities.h"
26 #include "SALOMEDS_SObject_i.hxx"
27 #include "SALOMEDS_SComponent_i.hxx"
28 #include "SALOMEDS_Study_i.hxx"
29 #include "SALOMEDS.hxx"
30 #include <SALOMEDSImpl_IParameters.hxx>
31 #include <SALOME_KernelServices.hxx>
32 #include <stdlib.h>
33
34 #include CORBA_CLIENT_HEADER(SALOME_Session)
35
36 SALOMEDS_Driver_i::SALOMEDS_Driver_i(Engines::EngineComponent_ptr theEngine, CORBA::ORB_ptr theORB)
37 {
38   // engine should not be null - component is supposed to be inherited from Engines::EngineComponent
39   _engine = Engines::EngineComponent::_duplicate(theEngine);
40   // driver can be null - if component interface does not inherit SALOMEDS::Driver
41   _driver = SALOMEDS::Driver::_narrow(theEngine);
42   _orb = CORBA::ORB::_duplicate(theORB);
43 }
44
45 SALOMEDS_Driver_i::SALOMEDS_Driver_i(SALOMEDS::Driver_ptr theDriver, CORBA::ORB_ptr theORB)
46 {
47   // driver can be null - if component interface does not inherit SALOMEDS::Driver
48   _driver = SALOMEDS::Driver::_duplicate(theDriver);
49   // engine can be null - since it is narrowed from SALOMEDS::Driver ptr which can be null
50   _engine = Engines::EngineComponent::_narrow(theDriver);
51   _orb = CORBA::ORB::_duplicate(theORB);
52 }
53
54 SALOMEDS_Driver_i::~SALOMEDS_Driver_i()
55 {
56 }
57
58 std::string SALOMEDS_Driver_i::GetIOR()
59 {
60   std::string ior = "";
61   if ( !CORBA::is_nil(_engine) ) {
62     CORBA::String_var cior = _orb->object_to_string(_engine);
63     ior = cior;
64   }
65   return ior;
66 }
67
68 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::Save(const SALOMEDSImpl_SComponent& theComponent,
69                                               const std::string& theURL,
70                                               long& theStreamLength,
71                                               bool isMultiFile)
72 {
73   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
74   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
75
76   SALOMEDS::unlock();
77   SALOMEDS::TMPFile_var aStream;
78
79   if ( !CORBA::is_nil(_driver) )
80     aStream = _driver->Save(sco.in(), url, isMultiFile);
81
82   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
83   theStreamLength = aTMPFile->Size();
84
85   sco->UnRegister();
86   SALOMEDS::lock();
87
88   return aTMPFile;
89 }
90
91 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::SaveASCII(const SALOMEDSImpl_SComponent& theComponent,
92                                                    const std::string& theURL,
93                                                    long& theStreamLength,
94                                                    bool isMultiFile)
95 {
96   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
97   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
98
99   SALOMEDS::unlock();
100   SALOMEDS::TMPFile_var aStream;
101
102   if ( !CORBA::is_nil(_driver) )
103     aStream = _driver->SaveASCII(sco.in(), url, isMultiFile);
104
105   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
106   theStreamLength = aTMPFile->Size();
107
108   sco->UnRegister();
109   SALOMEDS::lock();
110
111   return aTMPFile;
112 }
113
114 bool SALOMEDS_Driver_i::Load(const SALOMEDSImpl_SComponent& theComponent,
115                              const unsigned char* theStream,
116                              const long theStreamLength,
117                              const std::string& theURL,
118                              bool isMultiFile)
119 {
120   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
121   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
122   CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
123
124   SALOMEDS::TMPFile_var aStream;
125   if (theStreamLength > 0)
126     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
127   else
128     aStream = new SALOMEDS::TMPFile(0);
129
130   SALOMEDS::unlock();
131   bool isOk = false;
132
133   if ( !CORBA::is_nil(_driver) )
134     isOk = _driver->Load(sco.in(), aStream.in(), url, isMultiFile);
135
136   sco->UnRegister();
137   SALOMEDS::lock();
138   return isOk;
139 }
140
141 bool SALOMEDS_Driver_i::LoadASCII(const SALOMEDSImpl_SComponent& theComponent,
142                                   const unsigned char* theStream,
143                                   const long theStreamLength,
144                                   const std::string& theURL,
145                                   bool isMultiFile)
146 {
147   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
148   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
149   CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
150
151   SALOMEDS::TMPFile_var aStream;
152   if(theStreamLength > 0)
153     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
154   else
155     aStream = new SALOMEDS::TMPFile(0);
156
157   SALOMEDS::unlock();
158   bool isOk = false;
159
160   if ( !CORBA::is_nil(_driver) )
161     isOk = _driver->LoadASCII(sco.in(), aStream.in(), url, isMultiFile);
162
163   sco->UnRegister();
164   SALOMEDS::lock();
165   return isOk;
166 }
167
168 void SALOMEDS_Driver_i::Close(const SALOMEDSImpl_SComponent& theComponent)
169 {
170   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
171
172   SALOMEDS::unlock();
173   if ( !CORBA::is_nil(_driver) )
174     _driver->Close(sco.in());
175   sco->UnRegister();
176   SALOMEDS::lock();
177 }
178
179 std::string SALOMEDS_Driver_i::ComponentDataType()
180 {
181   std::string dtype = "";
182   if ( !CORBA::is_nil(_driver) ) {
183     CORBA::String_var type = _driver->ComponentDataType();
184     dtype = type;
185   }
186   return dtype;
187 }
188
189 std::string SALOMEDS_Driver_i::Version()
190 {
191   return !CORBA::is_nil( _engine ) ? _engine->getVersion() : std::string("");
192 }
193
194 std::string SALOMEDS_Driver_i::IORToLocalPersistentID(const SALOMEDSImpl_SObject& theSObject,
195                                                       const std::string& IORString,
196                                                       bool isMultiFile,
197                                                       bool isASCII)
198 {
199   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theSObject, _orb);
200   SALOMEDS::unlock();
201
202   std::string pstr = "";
203
204   if ( !CORBA::is_nil(_driver) ) {
205     CORBA::String_var pers_string =_driver->IORToLocalPersistentID(so.in(), IORString.c_str(),
206                                                                    isMultiFile, isASCII);
207     if ( pers_string.in() )
208       pstr = pers_string;
209   }
210
211   so->UnRegister();
212   SALOMEDS::lock();
213
214   return pstr;
215 }
216
217
218 std::string SALOMEDS_Driver_i::LocalPersistentIDToIOR(const SALOMEDSImpl_SObject& theObject,
219                                                       const std::string& aLocalPersistentID,
220                                                       bool isMultiFile,
221                                                       bool isASCII)
222 {
223   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
224   CORBA::String_var pers_string = CORBA::string_dup(aLocalPersistentID.c_str());
225   SALOMEDS::unlock();
226
227   std::string ior = "";
228
229   if ( !CORBA::is_nil(_driver) ) {
230     CORBA::String_var IOR = _driver->LocalPersistentIDToIOR(so.in(), pers_string.in(), isMultiFile, isASCII);
231     if ( IOR.in() )
232       ior = IOR;
233   }
234   so->UnRegister();
235   SALOMEDS::lock();
236   return ior;
237 }
238
239 bool SALOMEDS_Driver_i::CanCopy(const SALOMEDSImpl_SObject& theObject)
240 {
241   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
242
243   SALOMEDS::unlock();
244   bool isOk = false;
245
246   if ( !CORBA::is_nil(_driver) )
247     isOk = _driver->CanCopy(so.in());
248
249   so->UnRegister();
250   SALOMEDS::lock();
251
252   return isOk;
253 }
254
255
256 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::CopyFrom(const SALOMEDSImpl_SObject& theObject,
257                                                   int& theObjectID,
258                                                   long& theStreamLength)
259 {
260   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
261
262   SALOMEDS::unlock();
263   CORBA::Long anObjectID;
264   SALOMEDS::TMPFile_var aStream;
265
266   if ( !CORBA::is_nil(_driver) )
267     aStream = _driver->CopyFrom(so.in(), anObjectID);
268
269   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
270   theStreamLength = aTMPFile->Size();
271   theObjectID = anObjectID;
272
273   so->UnRegister();
274   SALOMEDS::lock();
275
276   return aTMPFile;
277 }
278
279 bool SALOMEDS_Driver_i::CanPaste(const std::string& theComponentName, int theObjectID)
280 {
281   SALOMEDS::unlock();
282   bool canPaste = false;
283
284   if ( !CORBA::is_nil(_driver) )
285     canPaste = _driver->CanPaste(theComponentName.c_str(), theObjectID);
286
287   SALOMEDS::lock();
288   return canPaste;
289 }
290
291 std::string SALOMEDS_Driver_i::PasteInto(const unsigned char* theStream,
292                                          const long theStreamLength,
293                                          int theObjectID,
294                                          const SALOMEDSImpl_SObject& theObject)
295 {
296   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
297   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)theStream;
298
299   SALOMEDS::TMPFile_var aStream;
300   if(theStreamLength > 0)
301     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
302   else
303     aStream = new SALOMEDS::TMPFile(0);
304
305   SALOMEDS::unlock();
306
307   std::string entry = "";
308
309   if ( !CORBA::is_nil(_driver) ) {
310     SALOMEDS::SObject_var ret_so = _driver->PasteInto(aStream.in(), theObjectID, so.in());
311     entry = ret_so->GetID();
312   }
313
314   so->UnRegister();
315   SALOMEDS::lock();
316
317   return entry;
318 }
319
320 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::DumpPython(bool isPublished,
321                                                     bool isMultiFile,
322                                                     bool& isValidScript,
323                                                     long& theStreamLength)
324 {
325   SALOMEDS::unlock();
326
327   Engines::TMPFile_var aStream;
328   CORBA::Boolean aValidScript = true; // VSR: maybe should be false by default ???
329
330   if ( !CORBA::is_nil( _engine ) )
331     aStream = _engine->DumpPython(isPublished, isMultiFile, aValidScript);
332
333   SALOMEDSImpl_TMPFile* aTMPFile = new Engines_TMPFile_i(aStream._retn());
334   theStreamLength = aTMPFile->Size();
335   isValidScript = aValidScript;
336
337   SALOMEDS::lock();
338
339   return aTMPFile;
340 }
341
342 //###############################################################################################################
343 //                                          SALOMEDS_DriverFactory
344 //###############################################################################################################
345
346 SALOMEDS_DriverFactory_i::SALOMEDS_DriverFactory_i(CORBA::ORB_ptr theORB)
347 {
348   _orb = CORBA::ORB::_duplicate(theORB);
349   _name_service = new SALOME_NamingService(_orb);
350 }
351
352
353 SALOMEDS_DriverFactory_i::~SALOMEDS_DriverFactory_i()
354 {
355   delete _name_service;
356 }
357
358 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByType(const std::string& theComponentType)
359 {
360
361   if ( theComponentType == SALOMEDSImpl_IParameters::getDefaultVisualComponent() )
362     return NULL; // skip the "Interface Applicative" component
363
364   SALOMEDSImpl_Driver* driver = 0;
365
366   CORBA::Object_var obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component("FactoryServer", theComponentType.c_str());
367
368   if (!CORBA::is_nil(obj)) {
369     Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
370     driver = new SALOMEDS_Driver_i(anEngine, _orb);
371   }
372   else {
373     // It can be "light" module
374     obj = _name_service->Resolve("/Kernel/Session");
375     if (!CORBA::is_nil(obj)) {
376       SALOME::Session_var session = SALOME::Session::_narrow(obj);
377       if (!CORBA::is_nil(session)) {
378         Engines::EngineComponent_var anEngine = session->GetComponent(theComponentType.c_str());
379         if (!CORBA::is_nil(anEngine))
380           driver = new SALOMEDS_Driver_i(anEngine, _orb);
381       }
382     }
383   }
384
385   return driver;
386 }
387
388 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByIOR(const std::string& theIOR)
389 {
390   CORBA::Object_var obj;
391   obj = _orb->string_to_object(theIOR.c_str());
392
393   if (!CORBA::is_nil(obj)) {
394     Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
395     return new SALOMEDS_Driver_i(anEngine, _orb);
396   }
397
398   return NULL;
399 }