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