1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SalomeApp_Engine_i : implementation of SalomeApp_Engine.idl
24 // File : SalomeApp_Engine_i.cxx
25 // Author : Alexander SLADKOV
27 #include "SalomeApp_Engine_i.h"
28 #include "SalomeApp_Application.h"
29 #include "SalomeApp_Study.h"
30 #include "SUIT_Session.h"
31 #include "CAM_Module.h"
32 #include "LightApp_DataModel.h"
34 #include <SALOME_NamingService.hxx>
35 #include <SALOMEDS_Tool.hxx>
36 #include <Utils_ORB_INIT.hxx>
37 #include <Utils_SINGLETON.hxx>
38 #include <Utils_SALOME_Exception.hxx>
39 #include <utilities.h>
41 #include <QApplication>
50 SalomeApp_Engine_i::SalomeApp_Engine_i( const char* theComponentName )
51 : myComponentName( theComponentName )
53 MESSAGE("SalomeApp_Engine_i::SalomeApp_Engine_i(): myComponentName = " <<
54 qPrintable( myComponentName ) << ", this = " << this);
60 SalomeApp_Engine_i::~SalomeApp_Engine_i()
62 MESSAGE("SalomeApp_Engine_i::~SalomeApp_Engine_i(): myComponentName = " <<
63 qPrintable( myComponentName ) << ", this = " << this);
66 SALOMEDS::TMPFile* SalomeApp_Engine_i::Save (SALOMEDS::SComponent_ptr theComponent,
70 SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
73 if (CORBA::is_nil(theComponent))
74 return aStreamFile._retn();
77 QString componentName (theComponent->ComponentDataType());
79 // Error somewhere outside - Save() called with wrong SComponent instance
80 if ( myComponentName != componentName )
81 return aStreamFile._retn();
83 // Get a temporary directory to store a file
84 //std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
86 bool manuallySaved = false;
88 if ( GetListOfFiles().empty() ) {
90 // Save was probably called from outside GUI, so SetListOfFiles was not called!
91 // Try to get list of files from directly from data model
93 MESSAGE("SalomeApp_Engine_i::Save(): myComponentName = " <<
94 qPrintable( myComponentName ) <<
95 "it seems Save() was called from outside GUI" );
98 SalomeApp_Application* app =
99 dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
101 return aStreamFile._retn();
104 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
107 return aStreamFile._retn();
108 QString url = QString::fromStdString(study->studyDS()->URL());
111 CAM_Module* module = app->module( SalomeApp_Application::moduleTitle( componentName ) );
112 if ( !module ) // load module???
113 return aStreamFile._retn();
115 LightApp_DataModel* dataModel = dynamic_cast<LightApp_DataModel*>( module->dataModel() );
117 return aStreamFile._retn();
119 QStringList dataFiles;
120 // we use 'url' instead of 'theURL' as latter normally contains path to the tmp dir,
121 // but not actual study's URL
122 dataModel->saveAs( url, study, dataFiles );
123 std::vector<std::string> names;
124 foreach ( QString name, dataFiles ) {
125 if ( !name.isEmpty() )
126 names.push_back(name.toUtf8().data());
128 SetListOfFiles( names );
129 manuallySaved = true;
132 // Get a temporary directory to store a file
133 //std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
135 // listOfFiles must contain temporary directory name in its first item
136 // and names of files (relatively the temporary directory) in the others
137 const int n = myListOfFiles.size() - 1;
139 if (n > 0) { // there are some files, containing persistent data of the component
140 std::string aTmpDir = myListOfFiles[0];
142 // Create a list to store names of created files
145 for (int i = 0; i < n; i++)
146 aSeq.push_back(CORBA::string_dup(myListOfFiles[i + 1].c_str()));
148 // Convert a file to the byte stream
149 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq, isMultiFile);
151 // Remove the files and tmp directory, created by the component storage procedure
152 if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq, true);
156 SetListOfFiles( ListOfFiles());
158 return aStreamFile._retn();
161 CORBA::Boolean SalomeApp_Engine_i::Load (SALOMEDS::SComponent_ptr theComponent,
162 const SALOMEDS::TMPFile& theFile,
166 std::cout << "SalomeApp_Engine_i::Load() isMultiFile = " << isMultiFile << std::endl;
167 if (CORBA::is_nil(theComponent))
170 // Error somewhere outside - Load() called with
171 // wrong SComponent instance
172 QString componentName = theComponent->ComponentDataType();
173 if ( myComponentName != componentName )
176 // Create a temporary directory for the component's data files
177 std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
179 // Convert the byte stream theStream to a files and place them in the tmp directory.
180 // The files and temporary directory must be deleted by the component loading procedure.
182 SALOMEDS_Tool::PutStreamToFiles(theFile, aTmpDir.c_str(), isMultiFile);
184 // Store list of file names to be used by the component loading procedure
185 const int n = aSeq.size() + 1;
186 ListOfFiles listOfFiles (n);
187 listOfFiles[0] = aTmpDir;
188 for (int i = 1; i < n; i++)
189 listOfFiles[i] = std::string(aSeq[i - 1]);
191 SetListOfFiles(listOfFiles);
196 SalomeApp_Engine_i::ListOfFiles SalomeApp_Engine_i::GetListOfFiles()
198 return myListOfFiles;
201 void SalomeApp_Engine_i::SetListOfFiles (const ListOfFiles& theListOfFiles)
203 myListOfFiles = theListOfFiles;
207 * DumpPython implementation for light modules
209 Engines::TMPFile* SalomeApp_Engine_i::DumpPython(CORBA::Boolean isPublished,
210 CORBA::Boolean isMultiFile,
211 CORBA::Boolean& isValidScript)
213 MESSAGE("SalomeApp_Engine_i::DumpPython(): myComponentName = "<<
214 qPrintable( myComponentName ) << ", this = " << this);
216 // Temporary solution: returning a non-empty sequence
217 // even if there's nothing to dump, to avoid crashes in SALOMEDS
218 // TODO: Improve SALOMEDSImpl_Study::DumpStudy() by skipping the components
219 // with isValidScript == false, and initialize isValidScript by false below.
220 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(1);
221 aStreamFile->length( 1 );
222 aStreamFile[0] = '\0';
223 isValidScript = true;
225 // listOfFiles must contain temporary directory name in its first item
226 // and names of files (relatively the temporary directory) in the others
227 if ( myListOfFiles.size() < 2 )
228 return aStreamFile._retn();
230 // there are some files, containing persistent data of the component
231 QString aTmpPath( myListOfFiles.front().c_str() );
232 QDir aTmpDir( aTmpPath );
233 if ( !aTmpDir.exists() )
234 return aStreamFile._retn();
236 // Calculate file sizes
237 QStringList aFilePaths;
238 QList<qint64> aFileSizes;
239 qint64 aBuffSize = 0;
240 ListOfFiles::const_iterator aFIt = myListOfFiles.begin();
241 ListOfFiles::const_iterator aFEnd = myListOfFiles.end();
243 for (; aFIt != aFEnd; aFIt++){
244 QString aFileName( (*aFIt).c_str() );
245 if ( !aTmpDir.exists( aFileName ) ){
249 QFile aFile( aTmpDir.filePath( aFileName ) );
250 if ( !aFile.open( QIODevice::ReadOnly ) ){
254 aFilePaths.push_back( aTmpDir.filePath( aFileName ) );
255 aFileSizes.push_back( aFile.size() );
256 aBuffSize += aFileSizes.back();
261 if ( !aFilePaths.size() || !aBuffSize )
262 return aStreamFile._retn();
264 char* aBuffer = new char[aBuffSize + 1];
266 return aStreamFile._retn();
268 // Convert the file(s) to the byte stream, multiple files are simply
270 // TODO: imporve multi-script support if necessary...
272 QStringList::const_iterator aFileIt = aFilePaths.begin();
273 QStringList::const_iterator aFileEnd = aFilePaths.end();
274 QList<qint64>::const_iterator aSIt = aFileSizes.begin();
275 for ( ; aFileIt != aFileEnd; aFileIt++, aSIt++ ){
276 QFile aFile( aTmpDir.filePath( *aFileIt ) );
277 if ( !aFile.open( QIODevice::ReadOnly ) ){
281 // Incorrect size of file
282 // Do not remove the bad file to have some diagnostic means
283 if ( aFile.read( aBuffer + aCurrPos, *aSIt ) != *aSIt ){
285 return aStreamFile._retn();
292 // Here we should end up with empty aTmpDir
293 // TODO: Handle QDir::rmdir() error status somehow...
294 aTmpDir.rmdir( aTmpPath );
296 aBuffer[aBuffSize] = '\0';
297 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
298 aStreamFile = new Engines::TMPFile(aBuffSize + 1, aBuffSize + 1, anOctetBuf, 1);
300 return aStreamFile._retn();
304 \return Component data type string for this instance of the engine
306 char* SalomeApp_Engine_i::ComponentDataType()
308 return CORBA::string_dup( myComponentName.toLatin1().constData() );
312 \return Component version
314 char* SalomeApp_Engine_i::getVersion()
316 SalomeApp_Application::ModuleShortInfoList versions = SalomeApp_Application::getVersionInfo();
318 SalomeApp_Application::ModuleShortInfo version_info;
319 foreach ( version_info, versions ) {
320 if ( SalomeApp_Application::moduleName( version_info.name ) == myComponentName ) {
321 version = version_info.version;
326 return CORBA::string_dup( version.toLatin1().constData() );
332 CORBA::ORB_var SalomeApp_Engine_i::orb()
334 static CORBA::ORB_var _orb;
336 if ( CORBA::is_nil( _orb ) ) {
337 Qtx::CmdLineArgs args;
338 ORB_INIT& init = *SINGLETON_<ORB_INIT>::Instance();
339 _orb = init( args.argc(), args.argv() );
348 PortableServer::POA_var SalomeApp_Engine_i::poa()
350 static PortableServer::POA_var _poa;
351 if ( CORBA::is_nil( _poa ) ){
352 CORBA::Object_var obj = orb()->resolve_initial_references( "RootPOA" );
353 _poa = PortableServer::POA::_narrow( obj );
361 SALOME_NamingService* SalomeApp_Engine_i::namingService()
363 static SALOME_NamingService _ns(orb());
368 Internal method, creates a CORBA engine for a light SALOME module
369 with the given "component data type" string,
370 activates it and registers in SALOME naming service with
371 /SalomeAppEngine/comp_data_type path. If the engine is already in the
372 naming service, simply returns and object reference to it.
373 \param theComponentName - synthetic "component data type" used to identify a given light module
374 \return Object reference to the CORBA engine
376 CORBA::Object_ptr SalomeApp_Engine_i::EngineForComponent( const char* theComponentName,
379 CORBA::Object_var anEngine;
380 if ( !theComponentName || !strlen( theComponentName ) )
381 return anEngine._retn();
383 if ( SalomeApp_Application::moduleTitle( theComponentName ).isEmpty() )
384 return anEngine._retn();
386 std::string aPath( "/SalomeAppEngine/" );
387 aPath += theComponentName;
388 anEngine = namingService()->Resolve( aPath.c_str() );
390 // Activating a new instance of the servant
391 if ( toCreate && CORBA::is_nil( anEngine ) ){
393 SalomeApp_Engine_i* aServant = new SalomeApp_Engine_i( theComponentName );
394 PortableServer::ObjectId_var id = poa()->activate_object( aServant );
395 anEngine = aServant->_this();
396 aServant->_remove_ref();
397 namingService()->Register( anEngine.in(), aPath.c_str() );
399 catch (CORBA::SystemException&) {
400 INFOS("Caught CORBA::SystemException.");
402 catch (CORBA::Exception&) {
403 INFOS("Caught CORBA::Exception.");
406 INFOS("Caught unknown exception.");
410 return anEngine._retn();
414 \param theComponentName - synthetic "component data type" used to identify a given light module
415 \return IOR string for the CORBA engine for a light SALOME module
416 with the given "component data type" string
417 \sa GetInstance( const char* theComponentName )
419 std::string SalomeApp_Engine_i::EngineIORForComponent( const char* theComponentName,
422 std::string anIOR( "" );
423 CORBA::Object_var anEngine = EngineForComponent( theComponentName, toCreate );
424 if ( !CORBA::is_nil( anEngine ) )
426 CORBA::String_var objStr = orb()->object_to_string( anEngine.in() );
427 anIOR = std::string( objStr.in() );
433 \param theComponentName - synthetic "component data type" used to identify a given light module
434 \return A pointer to corresponding C++ engine instance, null means some internal problems.
435 \sa EngineIORForComponent( const char* theComponentName )
437 SalomeApp_Engine_i* SalomeApp_Engine_i::GetInstance( const char* theComponentName,
440 SalomeApp_Engine_i* aServant = 0;
441 CORBA::Object_var anEngine = EngineForComponent( theComponentName, toCreate );
442 if ( !CORBA::is_nil( anEngine ) )
444 PortableServer::Servant aServantBase = poa()->reference_to_servant( anEngine.in() );
445 aServant = dynamic_cast<SalomeApp_Engine_i*>( aServantBase );
447 MESSAGE("SalomeApp_Engine_i::GetInstance(): theComponentName = " <<
448 theComponentName << ", aServant = " << aServant);