Salome HOME
updated copyright message
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Driver_i.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, 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 = (long)aTMPFile->Size(); //!< TODO: conversion from size_t to long
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 = (long)aTMPFile->Size(); //!< TODO: conversion from size_t to long
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   std::string ver;
192   if ( !CORBA::is_nil( _engine )) {
193     CORBA::String_var v = _engine->getVersion();
194     ver = v;
195   }
196   return ver;
197 }
198
199 std::string SALOMEDS_Driver_i::IORToLocalPersistentID(const SALOMEDSImpl_SObject& theSObject,
200                                                       const std::string& IORString,
201                                                       bool isMultiFile,
202                                                       bool isASCII)
203 {
204   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theSObject, _orb);
205   SALOMEDS::unlock();
206
207   std::string pstr = "";
208
209   if ( !CORBA::is_nil(_driver) ) {
210     CORBA::String_var pers_string =_driver->IORToLocalPersistentID(so.in(), IORString.c_str(),
211                                                                    isMultiFile, isASCII);
212     if ( pers_string.in() )
213       pstr = pers_string;
214   }
215
216   so->UnRegister();
217   SALOMEDS::lock();
218
219   return pstr;
220 }
221
222
223 std::string SALOMEDS_Driver_i::LocalPersistentIDToIOR(const SALOMEDSImpl_SObject& theObject,
224                                                       const std::string& aLocalPersistentID,
225                                                       bool isMultiFile,
226                                                       bool isASCII)
227 {
228   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
229   CORBA::String_var pers_string = CORBA::string_dup(aLocalPersistentID.c_str());
230   SALOMEDS::unlock();
231
232   std::string ior = "";
233
234   if ( !CORBA::is_nil(_driver) ) {
235     CORBA::String_var IOR = _driver->LocalPersistentIDToIOR(so.in(), pers_string.in(), isMultiFile, isASCII);
236     if ( IOR.in() )
237       ior = IOR;
238   }
239   so->UnRegister();
240   SALOMEDS::lock();
241   return ior;
242 }
243
244 bool SALOMEDS_Driver_i::CanCopy(const SALOMEDSImpl_SObject& theObject)
245 {
246   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
247
248   SALOMEDS::unlock();
249   bool isOk = false;
250
251   if ( !CORBA::is_nil(_driver) )
252     isOk = _driver->CanCopy(so.in());
253
254   so->UnRegister();
255   SALOMEDS::lock();
256
257   return isOk;
258 }
259
260
261 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::CopyFrom(const SALOMEDSImpl_SObject& theObject,
262                                                   int& theObjectID,
263                                                   long& theStreamLength)
264 {
265   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
266
267   SALOMEDS::unlock();
268   CORBA::Long anObjectID;
269   SALOMEDS::TMPFile_var aStream;
270
271   if ( !CORBA::is_nil(_driver) )
272     aStream = _driver->CopyFrom(so.in(), anObjectID);
273
274   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
275   theStreamLength = (long)aTMPFile->Size(); //!< TODO: conversion from size_t to long
276   theObjectID = anObjectID;
277
278   so->UnRegister();
279   SALOMEDS::lock();
280
281   return aTMPFile;
282 }
283
284 bool SALOMEDS_Driver_i::CanPaste(const std::string& theComponentName, int theObjectID)
285 {
286   SALOMEDS::unlock();
287   bool canPaste = false;
288
289   if ( !CORBA::is_nil(_driver) )
290     canPaste = _driver->CanPaste(theComponentName.c_str(), theObjectID);
291
292   SALOMEDS::lock();
293   return canPaste;
294 }
295
296 std::string SALOMEDS_Driver_i::PasteInto(const unsigned char* theStream,
297                                          const long theStreamLength,
298                                          int theObjectID,
299                                          const SALOMEDSImpl_SObject& theObject)
300 {
301   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
302   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)theStream;
303
304   SALOMEDS::TMPFile_var aStream;
305   if(theStreamLength > 0)
306     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
307   else
308     aStream = new SALOMEDS::TMPFile(0);
309
310   SALOMEDS::unlock();
311
312   std::string entry = "";
313
314   if ( !CORBA::is_nil(_driver) ) {
315     SALOMEDS::SObject_var ret_so = _driver->PasteInto(aStream.in(), theObjectID, so.in());
316     entry = ret_so->GetID();
317   }
318
319   so->UnRegister();
320   SALOMEDS::lock();
321
322   return entry;
323 }
324
325 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::DumpPython(bool isPublished,
326                                                     bool isMultiFile,
327                                                     bool& isValidScript,
328                                                     long& theStreamLength)
329 {
330   SALOMEDS::unlock();
331
332   Engines::TMPFile_var aStream;
333   CORBA::Boolean aValidScript = true; // VSR: maybe should be false by default ???
334
335   if ( !CORBA::is_nil( _engine ) )
336     aStream = _engine->DumpPython(isPublished, isMultiFile, aValidScript);
337
338   SALOMEDSImpl_TMPFile* aTMPFile = new Engines_TMPFile_i(aStream._retn());
339   theStreamLength = (long)aTMPFile->Size(); //!< TODO: conversion from size_t to long
340   isValidScript = aValidScript;
341
342   SALOMEDS::lock();
343
344   return aTMPFile;
345 }
346
347 //###############################################################################################################
348 //                                          SALOMEDS_DriverFactory
349 //###############################################################################################################
350
351 SALOMEDS_DriverFactory_i::SALOMEDS_DriverFactory_i(CORBA::ORB_ptr theORB, bool isNSAvail):_orb( CORBA::ORB::_duplicate(theORB) )
352 {
353   if(isNSAvail)
354     _name_service = new SALOME_NamingService(_orb);
355 }
356
357 SALOMEDS_DriverFactory_i::SALOMEDS_DriverFactory_i(CORBA::ORB_ptr theORB, SALOME_NamingService_Abstract *ns):_orb( CORBA::ORB::_duplicate(theORB) )
358 {
359   _name_service = ns->cloneCoVar();
360 }
361
362 SALOMEDS_DriverFactory_i::~SALOMEDS_DriverFactory_i()
363 {
364   delete _name_service;
365 }
366
367 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByType(const std::string& theComponentType)
368 {
369
370   if ( theComponentType == SALOMEDSImpl_IParameters::getDefaultVisualComponent() )
371     return NULL; // skip the "Interface Applicative" component
372
373   SALOMEDSImpl_Driver* driver = 0;
374
375   CORBA::Object_var obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component("FactoryServer", theComponentType.c_str());
376
377   if (!CORBA::is_nil(obj)) {
378     Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
379     driver = new SALOMEDS_Driver_i(anEngine, _orb);
380   }
381   else {
382     // It can be "light" module
383     obj = _name_service->Resolve("/Kernel/Session");
384     if (!CORBA::is_nil(obj)) {
385       SALOME::Session_var session = SALOME::Session::_narrow(obj);
386       if (!CORBA::is_nil(session)) {
387         Engines::EngineComponent_var anEngine = session->GetComponent(theComponentType.c_str());
388         if (!CORBA::is_nil(anEngine))
389           driver = new SALOMEDS_Driver_i(anEngine, _orb);
390       }
391     }
392   }
393
394   return driver;
395 }
396
397 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByIOR(const std::string& theIOR)
398 {
399   CORBA::Object_var obj;
400   obj = _orb->string_to_object(theIOR.c_str());
401
402   if (!CORBA::is_nil(obj)) {
403     Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
404     return new SALOMEDS_Driver_i(anEngine, _orb);
405   }
406
407   return NULL;
408 }