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