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