Salome HOME
e31a4f5202d68b144cbcb22d4269c1ea29d15d28
[modules/paravis.git] / src / PVGUI / PARAVIS_Gen_i.cc
1 // Copyright (C) 2010-2012  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 //  File   : PARAVIS_Gen_i.cc
20 //  Author : Vitaly Smetannikov
21 //  Module : PARAVIS
22 //
23
24
25 #include "PARAVIS_Gen_i.hh"
26
27 // IDL Headers
28 #include <omnithread.h>
29 #include CORBA_SERVER_HEADER(SALOME_Session)
30 #include CORBA_SERVER_HEADER(SALOME_ModuleCatalog)
31
32 #include <CAM_Module.h>
33 #include "PVGUI_Module.h"
34 #include <SalomeApp_Application.h>
35 #include <SUIT_ResourceMgr.h>
36
37 #include <SALOMEDS_Tool.hxx>
38 #include <QFileInfo>
39
40 #include "PV_Events.h"
41 #include "PV_Tools.h"
42
43 #include "QDomDocument"
44 #include "QDomNode"
45 #include "QDomElement"
46 #include "QFile"
47 #include "QDir"
48 #include "QTextStream"
49
50 #include "vtkWrapIDL.h"
51
52 #include <pqServer.h>
53 #include <pqServerResource.h>
54
55
56 #ifdef _DEBUG_
57 static int MYDEBUG = 1;
58 #else
59 static int MYDEBUG = 0;
60 #endif
61
62 using namespace std;
63
64 extern PARAVIS::PARAVIS_Base_i* CreateInstance(::vtkObjectBase* Inst, const QString&);
65
66 extern "C" 
67 PARAVIS_I_EXPORT PARAVIS::PARAVIS_Gen_ptr GetImpl(CORBA::ORB_ptr theORB, 
68                                                   PortableServer::POA_ptr thePOA,
69                                                   SALOME_NamingService* theNamingService,
70                                                   QMutex* theMutex)
71 {
72   if(MYDEBUG) MESSAGE("extern 'C' GetImpl");
73   PARAVIS::PARAVIS_Gen_i *aPARAVIS_Gen = new PARAVIS::PARAVIS_Gen_i(theORB, 
74                                                                     thePOA, 
75                                                                     theNamingService, 
76                                                                     theMutex);
77   return aPARAVIS_Gen->_this();
78 }
79
80
81
82 namespace PARAVIS
83 {
84
85   const char* checkNullStr(const char* theStr)
86   {
87     if (strcmp(theStr, "NULL") == 0)
88       return NULL;
89     else
90       return theStr;
91   }
92
93   std::string tabify( const std::string& source, bool isTabify )
94   {
95     std::string result = source;
96     if ( isTabify && !result.empty() ) {
97       std::string caret = "\n";
98       int idx = result.rfind( caret );
99       while ( idx != std::string::npos ) {
100         result.replace( idx, caret.size(), "\n\t" );
101         idx = result.rfind( caret, idx-1 );
102       }
103       result.insert(0, "\t" );
104     }
105     return result;
106   }
107
108   PARAVIS_Base_i::~PARAVIS_Base_i() {
109     if(mySmartPointer != NULL) mySmartPointer->Delete();
110   }
111
112   void PARAVIS_Base_i::Init(::vtkObjectBase* base) {
113     if (mySmartPointer != NULL) mySmartPointer->Delete();
114     mySmartPointer = base;
115   }
116
117   ::vtkObjectBase* PARAVIS_Base_i::getVTKObject(PARAVIS_Base_ptr theBase) {
118     PARAVIS_Base_i* aBase = GET_SERVANT(theBase);
119     return (aBase != NULL)? aBase->getVTKObject() : NULL;
120   }
121
122   CORBA::Boolean PARAVIS_Base_i::IsSame(PARAVIS_Base_ptr theOther)
123   {
124     PARAVIS_Base_i* aBase = GET_SERVANT(theOther);
125     if (aBase == NULL) 
126       return false;
127     return mySmartPointer.GetPointer() == aBase->getVTKObject();
128   }
129
130
131
132
133   PARAVIS_Gen_i*          PARAVIS_Gen_i::myParavisGenImpl;
134   CORBA::ORB_var          PARAVIS_Gen_i::myOrb;
135   PortableServer::POA_var PARAVIS_Gen_i::myPoa;
136   SALOME_LifeCycleCORBA*  PARAVIS_Gen_i::myEnginesLifeCycle;
137   SALOME_NamingService*   PARAVIS_Gen_i::myNamingService;
138   QMutex*                 PARAVIS_Gen_i::myMutex;
139   SalomeApp_Application*  PARAVIS_Gen_i::mySalomeApp = 0;
140
141
142   //----------------------------------------------------------------------------
143   PARAVIS_Gen_i::PARAVIS_Gen_i(CORBA::ORB_ptr theORB, PortableServer::POA_ptr thePOA,
144                                SALOME_NamingService* theNamingService, QMutex* theMutex) :
145     Engines_Component_i()
146   {
147     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PARAVIS_Gen_i");
148     if (mySalomeApp) return;
149
150     myMutex = theMutex;
151     myOrb = CORBA::ORB::_duplicate(theORB);
152     myPoa = PortableServer::POA::_duplicate(thePOA);
153     myParavisGenImpl = this;
154     myNamingService = theNamingService;
155
156     static SALOME_LifeCycleCORBA aEnginesLifeCycle(theNamingService);
157     myEnginesLifeCycle = &aEnginesLifeCycle;
158
159     CORBA::Object_var anObj = myNamingService->Resolve("/myStudyManager");
160     if (!CORBA::is_nil(anObj)) {
161       SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(anObj);
162       SALOMEDS::ListOfOpenStudies_var aListOfOpenStudies = aStudyManager->GetOpenStudies();
163       if(aListOfOpenStudies->length() > 0) {
164         CORBA::String_var aStudyName = aListOfOpenStudies[0];
165         myStudyDocument = aStudyManager->GetStudyByName(aStudyName);
166         if (!myStudyDocument->_is_nil()) {
167           mySalomeApp = ProcessEvent(new TGetGUIApplication(myStudyDocument->StudyId()));
168           if(!myStudyDocument->GetProperties()->IsLocked())
169             FindOrCreateParaVisComponent(myStudyDocument);
170         }
171       } else
172         if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PARAVIS_Gen_i : there is no opened study in StudyManager !!!");
173     } else
174       if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PARAVIS_Gen_i : Can't find StudyManager !!!");
175
176   }
177
178   //----------------------------------------------------------------------------
179   PARAVIS_Gen_i::~PARAVIS_Gen_i()
180   {
181     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::~PARAVIS_Gen_i");
182   }
183
184   //----------------------------------------------------------------------------
185   char* PARAVIS_Gen_i::GetIOR()
186   {
187     if(myIOR == ""){
188       CORBA::Object_var anObject = _this();
189       CORBA::String_var anIOR = myOrb->object_to_string(anObject);
190       myIOR = anIOR.in();
191     }
192     return CORBA::string_dup(myIOR.c_str());
193   }
194
195   //----------------------------------------------------------------------------
196   void PARAVIS_Gen_i::ImportFile(const char* theFileName)
197   {
198     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::ImportFile: " <<theFileName);
199     ProcessVoidEvent(new TImportFile(mySalomeApp, theFileName));
200   }
201
202   void PARAVIS_Gen_i::ExecuteScript(const char* script)
203   {
204     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::ExecuteScript: " <<script);
205     ProcessVoidEvent(new TExecuteScript(mySalomeApp, script));
206   }
207
208   //----------------------------------------------------------------------------
209   char* PARAVIS_Gen_i::GetTrace()
210   {
211     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PrintTrace: ");
212     return CORBA::string_dup(ProcessEvent(new TGetTrace(mySalomeApp)).c_str());
213   }
214
215   //----------------------------------------------------------------------------
216   void PARAVIS_Gen_i::SaveTrace(const char* theFileName)
217   {
218     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::SaveTrace: " <<theFileName);
219     ProcessVoidEvent(new TSaveTrace(mySalomeApp, theFileName));
220   }
221
222   //----------------------------------------------------------------------------
223   void processElements(QDomNode& thePropertyNode, QStringList& theFileNames, const char* theNewPath, bool theRestore)
224   {
225     QDomNode aElementNode = thePropertyNode.firstChild();
226     while (aElementNode.isElement()) {
227       QDomElement aElement = aElementNode.toElement();
228       if (aElement.tagName() == "Element") {
229         QString aIndex = aElement.attribute("index");
230         if (aIndex == "0") {
231           QString aValue = aElement.attribute("value");
232           if (!aValue.isNull()) {
233             if (!theNewPath) {
234               QFileInfo aFInfo(aValue);
235               if (aFInfo.exists()) {
236                 theFileNames<<aValue;
237                 aElement.setAttribute("value", aFInfo.fileName());
238               }
239               break;
240             } else {
241               if (theRestore)
242                 aElement.setAttribute("value", QString(theNewPath) + aValue);
243             }
244           }
245         }
246       }
247       aElementNode = aElementNode.nextSibling();
248     }
249   }
250
251
252   //----------------------------------------------------------------------------
253   void processProperties(QDomNode& theProxyNode, QStringList& theFileNames, const char* theNewPath, bool theRestore)
254   {
255     QDomNode aPropertyNode = theProxyNode.firstChild();
256     while (aPropertyNode.isElement()) {
257       QDomElement aProperty = aPropertyNode.toElement();
258       QString aName = aProperty.attribute("name");
259       if ((aName == "FileName") || (aName == "FileNameInfo") || (aName == "FileNames")) {
260         processElements(aPropertyNode, theFileNames, theNewPath, theRestore);
261       }
262       aPropertyNode = aPropertyNode.nextSibling();
263     }
264   }  
265
266
267   //----------------------------------------------------------------------------
268   void processProxies(QDomNode& theNode, QStringList& theFileNames, const char* theNewPath, bool theRestore)
269   {
270     QDomNode aProxyNode = theNode.firstChild();
271     while (aProxyNode.isElement()) {
272       QDomElement aProxy = aProxyNode.toElement();
273       if (aProxy.tagName() == "Proxy") {
274         QString aGroup = aProxy.attribute("group");
275         if (aGroup == "sources") {
276           processProperties(aProxyNode, theFileNames, theNewPath, theRestore);
277         }
278       }
279       aProxyNode = aProxyNode.nextSibling();
280     }
281   }
282
283   //----------------------------------------------------------------------------
284   bool processAllFilesInState(const char* aFileName, QStringList& theFileNames,
285                               const char* theNewPath, bool theRestore = false)
286   {
287     QFile aFile(aFileName);
288     if (!aFile.open(QFile::ReadOnly)) {
289       MESSAGE("Can't open state file "<<aFileName);
290       return false;
291     }
292     QDomDocument aDoc;
293     bool aRes = aDoc.setContent(&aFile);
294     aFile.close();
295     
296     if (!aRes) {
297       MESSAGE("File "<<aFileName<<" is not XML document");
298       return false;
299     }
300     
301     QDomElement aRoot = aDoc.documentElement();
302     if ( aRoot.isNull() ) {
303       MESSAGE( "Invalid XML root" );
304       return false;
305     }
306
307     QDomNode aNode = aRoot.firstChild();
308     while (aRes  && !aNode.isNull() ) {
309       aRes = aNode.isElement();
310       if ( aRes ) {
311         QDomElement aSection = aNode.toElement();
312         if (aSection.tagName() == "ServerManagerState") {
313           processProxies(aNode, theFileNames, theNewPath, theRestore);
314         }
315       }
316       aNode = aNode.nextSibling();
317     }
318     if (!aFile.open(QFile::WriteOnly | QFile::Truncate)) {
319       MESSAGE("Can't open state file "<<aFileName<<" for writing");
320       return false;
321     }
322     QTextStream out(&aFile);
323     aDoc.save(out, 2);
324     aFile.close();
325     
326     return true;
327   }
328
329   //----------------------------------------------------------------------------
330   QStringList getAllSavedStates(SALOMEDS::SComponent_ptr theComponent, 
331                                 QString theNewPath = QString())
332   {
333     QStringList aStateFiles;
334
335     if (!CORBA::is_nil(theComponent)) {
336       SALOMEDS::Study_var aStudy = theComponent->GetStudy();
337       SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(theComponent);
338       for (; anIter->More(); anIter->Next()) {
339         SALOMEDS::SObject_var aSObj = anIter->Value();
340         SALOMEDS::GenericAttribute_var anAttr;
341         if (!aSObj->FindAttribute(anAttr, "AttributeLocalID")) {
342           continue;
343         }
344         SALOMEDS::AttributeLocalID_var anID = SALOMEDS::AttributeLocalID::_narrow(anAttr);
345         if (!anID->Value() == PVSTATEID) {
346           continue;
347         }
348         if (aSObj->FindAttribute(anAttr, "AttributeString")) {
349           SALOMEDS::AttributeString_var aStringAttr = SALOMEDS::AttributeString::_narrow(anAttr);
350           QString aStateFile(aStringAttr->Value());
351           printf("getAllSavedStates, aStateFile = %s\n", aStateFile.toLatin1().constData());
352           // Replace the old path with the new one
353           if (!theNewPath.isEmpty()) {
354             QFileInfo aFileInfo(aStateFile);
355             QString aPath = aFileInfo.path();
356             aStateFile.replace(aPath, theNewPath);
357             aStringAttr->SetValue(aStateFile.toLatin1().constData());
358             
359             printf("getAllSavedStates, aStateFile NEW = %s\n", aStateFile.toLatin1().constData());
360           }
361           aStateFiles<<aStateFile;
362         }
363       }
364     }
365
366     return aStateFiles;
367   }
368
369   SALOMEDS::TMPFile* SaveState(long thePID, SalomeApp_Application* theApp, SALOMEDS::SComponent_ptr theComponent, 
370                                const char* theURL, bool isMultiFile)
371   {
372     std::string aTmpDir = SALOMEDS_Tool::GetTmpDir();
373
374     std::ostringstream aStream;
375     aStream<<"paravisstate:"<<thePID;
376     std::string aFileName = "_" + aStream.str();
377     if(MYDEBUG) MESSAGE("aFileName = '"<<aFileName);
378
379     SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
380
381     std::string aFile = aTmpDir + aFileName;
382     ProcessVoidEvent(new TSaveStateFile(theApp, aFile.c_str()));
383
384     // Get saved states
385     QStringList aSavedStates = getAllSavedStates(theComponent);
386
387     // Collect all files from state
388     SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
389     int aSavingType = aResourceMgr->integerValue( "PARAVIS", "savestate_type", 0 );
390
391     QStringList aFileNames;
392     QStringList aNames;
393    
394     switch (aSavingType) {
395     case 0: // Save referenced files only for builin server
396       {
397         MESSAGE("Save as built in;")
398         bool aIsBuiltIn = true;
399         pqServer* aServer = ProcessEvent(new TGetActiveServer(theApp));
400         if (aServer) 
401           aIsBuiltIn != aServer->isRemote();
402       
403         if (aIsBuiltIn)
404         {
405           // Find referenced files and collect their paths nullyfying references:
406           
407           // for saved states
408           foreach (QString aState, aSavedStates) {
409             processAllFilesInState(aState.toLatin1().constData(), aFileNames, 0);
410           }
411
412           // for main state
413           processAllFilesInState(aFile.c_str(), aFileNames, 0); 
414
415           SetRestoreParam(theComponent, true);
416         } else {
417           SetRestoreParam(theComponent, false);
418        }
419       }
420       break;
421     case 1: //Save referenced files when they are accessible
422       {
423         // Find referenced files and collect their paths nullyfying references
424
425         // for saved states
426         foreach (QString aState, aSavedStates) {
427           processAllFilesInState(aState.toLatin1().constData(), aFileNames, 0);
428         }
429         
430         // for main state
431         processAllFilesInState(aFile.c_str(), aFileNames, 0);
432         SetRestoreParam(theComponent, true);
433       }
434       break;
435     case 2: //Never save referenced files
436       SetRestoreParam(theComponent, false);
437       break;
438     }
439
440     // Add saved states
441     foreach (QString aSavedState, aSavedStates) {
442       aFileNames<<aSavedState;
443     }
444
445     // Add main state to the end of the list
446     aFileNames<<QString(aFile.c_str());
447
448     // File names for main state and its data files
449     foreach(QString aFile, aFileNames) {
450       QFileInfo aInfo(aFile);
451       aNames<<aInfo.fileName();
452     }
453     SALOMEDS::ListOfFileNames_var aListOfFileNames = GetListOfFileNames(aFileNames);
454     SALOMEDS::ListOfFileNames_var aListOfNames = GetListOfFileNames(aNames);
455
456     aStreamFile = SALOMEDS_Tool::PutFilesToStream(aListOfFileNames.in(), aListOfNames.in());
457     
458     return aStreamFile._retn();
459   }
460
461   //----------------------------------------------------------------------------
462   SALOMEDS::TMPFile* PARAVIS_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent, 
463                                          const char* theURL, bool isMultiFile)
464   {
465     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Save - theURL = '"<<theURL<<"'");
466     return SaveState((long) this, mySalomeApp, theComponent, theURL, isMultiFile);
467   }
468     
469   //----------------------------------------------------------------------------
470   SALOMEDS::TMPFile* PARAVIS_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
471                                               const char* theURL, bool isMultiFile)
472   {
473     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Save - theURL = '"<<theURL<<"'");
474     return SaveState((long) this, mySalomeApp, theComponent, theURL, isMultiFile);
475   }
476     
477
478   //----------------------------------------------------------------------------
479   bool LoadState(SalomeApp_Application* theApp, SALOMEDS::SComponent_ptr theComponent,
480                  const SALOMEDS::TMPFile& theStream, const char* theURL, bool isMultiFile)
481   {
482     std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
483     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Load - "<<aTmpDir);
484
485     SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
486     if (aSeq->length() == 0)
487       return false;
488
489     bool aRestore = GetRestoreParam(theComponent);
490     MESSAGE("PARAVIS_Gen_i::Restore path - "<<aRestore);
491
492     // Process main state
493     std::string aFile = aTmpDir + std::string(aSeq[aSeq->length() - 1]);
494     QStringList aEmptyList;
495     processAllFilesInState(aFile.c_str(), aEmptyList, aTmpDir.c_str(), aRestore);
496     ProcessVoidEvent(new TLoadStateFile(theApp, aFile.c_str()));
497
498     // Process saved states
499     QStringList aSavedStates = getAllSavedStates(theComponent, QString(aTmpDir.c_str()));
500     foreach(QString aState, aSavedStates) {
501       processAllFilesInState(aState.toLatin1().constData(), 
502                              aEmptyList, aTmpDir.c_str(), aRestore);
503     }
504     
505     return true;
506   }
507
508   //----------------------------------------------------------------------------
509   bool PARAVIS_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent, const SALOMEDS::TMPFile& theStream,
510                            const char* theURL, bool isMultiFile)
511   {
512     return LoadState(mySalomeApp, theComponent, theStream, theURL, isMultiFile);
513   }
514
515   //----------------------------------------------------------------------------
516   bool PARAVIS_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent, 
517                                 const SALOMEDS::TMPFile& theStream,
518                                 const char* theURL, bool isMultiFile)
519   {
520     return LoadState(mySalomeApp, theComponent, theStream, theURL, isMultiFile);
521   }
522     
523   //----------------------------------------------------------------------------
524   void PARAVIS_Gen_i::Close(SALOMEDS::SComponent_ptr IORSComponent)
525   {
526   }
527   
528   //----------------------------------------------------------------------------
529   char* PARAVIS_Gen_i::ComponentDataType()
530   {
531     return CORBA::string_dup("PARAVIS");
532   }
533
534   //----------------------------------------------------------------------------
535   char* PARAVIS_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject, const char* IORString,
536                                               CORBA::Boolean isMultiFile, CORBA::Boolean isASCII)
537   {
538     CORBA::String_var aString("");
539     return aString._retn();
540   }
541
542   //----------------------------------------------------------------------------
543   char* PARAVIS_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject, 
544                                               const char* aLocalPersistentID,
545                                               CORBA::Boolean isMultiFile, 
546                                               CORBA::Boolean isASCII)
547   {
548     CORBA::String_var aString("");
549     return aString._retn();
550   }
551
552   //----------------------------------------------------------------------------
553   bool PARAVIS_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
554   {
555     return false;
556   }
557
558   //----------------------------------------------------------------------------
559   SALOMEDS::SObject_ptr PARAVIS_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
560                                                       SALOMEDS::SObject_ptr theSObject, 
561                                                       CORBA::Object_ptr theObject,
562                                                       const char* theName) 
563     throw (SALOME::SALOME_Exception)
564   {
565     SALOMEDS::SObject_var aResultSO;
566     return aResultSO._retn();
567   }
568
569   //----------------------------------------------------------------------------
570   CORBA::Boolean PARAVIS_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject)
571   {
572     return false;
573   }
574
575   //----------------------------------------------------------------------------
576   SALOMEDS::TMPFile* PARAVIS_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID)
577   {
578     SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
579     return aStreamFile._retn();
580   }
581
582   //----------------------------------------------------------------------------
583   CORBA::Boolean PARAVIS_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID)
584   {
585     return false;
586   }
587
588   //----------------------------------------------------------------------------
589   SALOMEDS::SObject_ptr PARAVIS_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
590                                                  CORBA::Long theObjectID, SALOMEDS::SObject_ptr theObject)
591   {
592     return SALOMEDS::SObject::_nil();
593   }
594
595   //----------------------------------------------------------------------------
596   PARAVIS::string_array* PARAVIS_Gen_i::GetClassesList()
597   {
598     uint k;
599     for (k = 0; strcmp(wrapped_classes[k], "") != 0; k++);
600     PARAVIS::string_array_var aNames = new PARAVIS::string_array();
601     aNames->length(k);
602     for (CORBA::ULong i = 0; i < k; i++)
603       aNames[i] = CORBA::string_dup(wrapped_classes[i]);
604     return aNames._retn();
605   }
606
607   //----------------------------------------------------------------------------
608   PARAVIS_Base_ptr PARAVIS_Gen_i::CreateClass(const char* theClassName)
609   {
610     PARAVIS::PARAVIS_Base_i* aClass = CreateInstance(NULL, QString(theClassName));
611     return aClass->_this();
612   }
613
614   //----------------------------------------------------------------------------
615   void PARAVIS_Gen_i::GetConnectionParameters(CORBA::Long& theId, 
616                                               CORBA::String_out theDHost, CORBA::Long& theDPort,
617                                               CORBA::String_out theRHost, CORBA::Long& theRPort,
618                                               CORBA::Long& theReversed)
619   {
620     pqServer* aServer = ProcessEvent(new TGetActiveServer(mySalomeApp));
621     if (aServer) {
622       theId = static_cast<int>(aServer->GetConnectionID());
623
624       pqServerResource serverRes = aServer->getResource();
625       theReversed = (serverRes.scheme() == "csrc" || serverRes.scheme() == "cdsrsrc")? 1 : 0;
626
627       theDPort = serverRes.dataServerPort() < 0? serverRes.port() : serverRes.dataServerPort();
628       theRPort = serverRes.renderServerPort() < 0 ? 0 : serverRes.renderServerPort();
629
630       QString dsHost = serverRes.dataServerHost().isEmpty()? serverRes.host() : serverRes.dataServerHost();
631       QString rsHost = serverRes.renderServerHost();
632
633       theDHost = CORBA::string_dup(qPrintable(dsHost));
634       theRHost = CORBA::string_dup(qPrintable(rsHost));
635     }
636   }
637
638
639   //----------------------------------------------------------------------------
640   Engines::TMPFile* PARAVIS_Gen_i::DumpPython(CORBA::Object_ptr theStudy,
641                                               CORBA::Boolean theIsPublished,
642                                               CORBA::Boolean theIsMultiFile,
643                                               CORBA::Boolean& theIsValidScript)
644   {
645     theIsValidScript = true;
646     std::string aResult;
647     if ( theIsMultiFile )
648       aResult += "\ndef RebuildData(theStudy):\n";
649     aResult += tabify(ProcessEvent(new TGetTrace(mySalomeApp)), theIsMultiFile );
650     if ( theIsMultiFile )
651       aResult += "\n\tpass\n";
652     CORBA::ULong aSize = aResult.size() + 1;
653     char* aBuffer = new char[aSize];
654     memset(aBuffer, 0, aSize);
655     strcpy(aBuffer, &aResult[0]);
656     Engines::TMPFile_var aDump = new Engines::TMPFile(aSize,aSize,(CORBA::Octet*)aBuffer,1);
657     return aDump._retn();
658   }
659
660   //----------------------------------------------------------------------------
661   void PARAVIS_Gen_i::ActivateModule()
662   {
663     ProcessVoidEvent(new TActivateModule(mySalomeApp));
664   }
665
666   //----------------------------------------------------------------------------
667   struct TSetStudyEvent: public SALOME_Event {
668     std::string myStudyName;
669     typedef SalomeApp_Application* TResult;
670     TResult myResult;
671     
672     TSetStudyEvent(const std::string theStudyName):myStudyName(theStudyName), myResult(0) {}
673     virtual void Execute()
674     {
675       bool isActive = false;
676       SUIT_Session* aSession = SUIT_Session::session();
677         QList<SUIT_Application*> anApplications = aSession->applications();
678         QList<SUIT_Application*>::Iterator anIter = anApplications.begin();
679         SUIT_Application* aFirstApp = *anIter;
680         while (anIter != anApplications.end()) {
681           SUIT_Application* anApp = *anIter;
682           if (SUIT_Study* aSStudy = anApp->activeStudy()) {
683             if (SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(aSStudy)) {
684               if (_PTR(Study) aCStudy = aStudy->studyDS()) {
685                 if(MYDEBUG) MESSAGE("There is an application with active study : StudyId = "
686                                     << aCStudy->StudyId() << "; Name = '" << aCStudy->Name() << "'");
687                 if (myStudyName == aCStudy->Name()) {
688                   isActive = true;
689                   break;
690                 }
691               }
692             }
693           }
694           anIter++;
695         }
696         SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(aFirstApp);
697         if (!isActive) {
698           MESSAGE("!!! anApp->onLoadDoc(myStudyName) !!!");
699           // Has to be loaded in an empty or in a new application
700           anApp->onLoadDoc(myStudyName.c_str());
701         }
702         myResult = anApp;
703     }
704   };
705
706   void PARAVIS_Gen_i::SetCurrentStudy(SALOMEDS::Study_ptr theStudy)
707   {
708     if (!CORBA::is_nil(theStudy)) {
709       CORBA::String_var aName = theStudy->Name();
710       std::string aStudyName (aName.in());
711
712       myStudyDocument = SALOMEDS::Study::_duplicate(theStudy);
713       SalomeApp_Application*  anApp = ProcessEvent(new TSetStudyEvent(aStudyName));
714       if (!mySalomeApp)
715         mySalomeApp = anApp;
716     } else {
717       INFOS("CORBA::is_nil(theStudy)");
718     }
719   }
720   
721   
722   //----------------------------------------------------------------------------
723   SALOMEDS::Study_ptr PARAVIS_Gen_i::GetCurrentStudy()
724   {
725     return SALOMEDS::Study::_duplicate(myStudyDocument);
726   }
727
728 }