X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=lib%2FcpPlugins%2FInterface%2FPlugins.cxx;h=42ab2cc77ded0fd88249a1c6a8bf9f6b8b1b70d5;hb=77d3666adac324c796ec446fd21179bd8619b39a;hp=e7ebb4b5892a0b7d24f3917d7b4d29f10ef8896a;hpb=ad814fa27df55cd06a071e474e56efc27f9f8fa5;p=cpPlugins.git diff --git a/lib/cpPlugins/Interface/Plugins.cxx b/lib/cpPlugins/Interface/Plugins.cxx index e7ebb4b..42ab2cc 100644 --- a/lib/cpPlugins/Interface/Plugins.cxx +++ b/lib/cpPlugins/Interface/Plugins.cxx @@ -1,800 +1,488 @@ #include - -#include - -#include -#include - -#ifdef cpPlugins_Interface_QT4 - -#include -#include -#include -#include - -#ifdef _WIN32 -# define PLUGIN_PREFIX "" -# define PLUGIN_EXT "dll" -# define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)" -#else // Linux -# define PLUGIN_PREFIX "lib" -# define PLUGIN_EXT "so" -# define PLUGIN_REGEX "Plugins file (*.so);;All files (*)" -#endif // _WIN32 - -#endif // cpPlugins_Interface_QT4 +#include +#include +#include // ------------------------------------------------------------------------- cpPlugins::Interface::Plugins:: -Plugins( ) - : m_Widget( NULL ), - m_Application( NULL ), - m_LastLoadedPlugin( "" ), - m_ActiveFilter( NULL ) +Pointer cpPlugins::Interface::Plugins:: +New( ) { + static Pointer singleton = NULL; + if( singleton.IsNull( ) ) + singleton = new Self( ); + return( singleton ); } // ------------------------------------------------------------------------- -cpPlugins::Interface::Plugins:: -~Plugins( ) +itk::LightObject::Pointer cpPlugins::Interface::Plugins:: +CreateAnother( ) const { + itk::LightObject::Pointer smartPtr; + smartPtr = NULL; + return( smartPtr ); } // ------------------------------------------------------------------------- -QWidget* cpPlugins::Interface::Plugins:: -GetWidget( ) +cpPlugins::Interface::Plugins:: +Pointer cpPlugins::Interface::Plugins:: +Clone( ) const { - return( this->m_Widget ); + Pointer r = NULL; + return( r ); } // ------------------------------------------------------------------------- -const QWidget* cpPlugins::Interface::Plugins:: -GetWidget( ) const +cpPlugins::Interface::Plugins:: +TStrings cpPlugins::Interface::Plugins:: +GetLibraries( ) const { - return( this->m_Widget ); + TStrings res; + for( + auto i = this->m_Libraries.begin( ); i != this->m_Libraries.end( ); ++i + ) + res.insert( i->first ); + return( res ); } // ------------------------------------------------------------------------- -void cpPlugins::Interface::Plugins:: -SetWidget( QWidget* widget ) +cpPlugins::Interface::Plugins:: +TStrings cpPlugins::Interface::Plugins:: +GetPlugins( ) const { - this->m_Widget = widget; + TStrings res; + for( auto i = this->m_Plugins.begin( ); i != this->m_Plugins.end( ); ++i ) + res.insert( i->first ); + return( res ); } // ------------------------------------------------------------------------- -void cpPlugins::Interface::Plugins:: -BlockWidget( ) +cpPlugins::Interface::Plugins:: +TStrings cpPlugins::Interface::Plugins:: +GetCategories( ) const { -#ifdef cpPlugins_Interface_QT4 - if( this->m_Widget != NULL ) - { - QApplication::setOverrideCursor( Qt::WaitCursor ); - this->m_Widget->setEnabled( false ); - - } // fi -#endif // cpPlugins_Interface_QT4 + TStrings res; + for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i ) + res.insert( i->first ); + return( res ); } // ------------------------------------------------------------------------- -void cpPlugins::Interface::Plugins:: -UnblockWidget( ) +cpPlugins::Interface::Plugins:: +TStrings cpPlugins::Interface::Plugins:: +GetFilters( const std::string& category ) const { -#ifdef cpPlugins_Interface_QT4 - if( this->m_Widget != NULL ) - { - QApplication::restoreOverrideCursor( ); - this->m_Widget->setEnabled( true ); - - } // fi -#endif // cpPlugins_Interface_QT4 + TStrings res; + auto cIt = this->m_Filters.find( category ); + if( cIt != this->m_Filters.end( ) ) + for( auto i = cIt->second.begin( ); i != cIt->second.end( ); ++i ) + res.insert( i->first ); + return( res ); } // ------------------------------------------------------------------------- void cpPlugins::Interface::Plugins:: -DialogLoadPlugins( ) +AddEnvironments( const std::string& new_environment ) { -#ifdef cpPlugins_Interface_QT4 - if( this->m_Widget != NULL ) + std::vector< std::string > tokens; + cpExtensions::Tokenize( tokens, new_environment, cpPlugins_ENV_SEPARATOR ); + for( auto i = tokens.begin( ); i != tokens.end( ); ++i ) { - QFileDialog dialog( this->m_Widget ); - dialog.setFileMode( QFileDialog::ExistingFile ); - dialog.setDirectory( this->m_LastLoadedPlugin.c_str( ) ); - dialog.setNameFilter( QFileDialog::tr( PLUGIN_REGEX ) ); - dialog.setDefaultSuffix( QFileDialog::tr( PLUGIN_EXT ) ); - if( !( dialog.exec( ) ) ) - return; - - std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( ); - if( !( this->LoadPlugins( fname ) ) ) - QMessageBox::critical( - this->m_Widget, - QMessageBox::tr( "Ignoring plugin" ), - QMessageBox::tr( fname.c_str( ) ) - ); - - } // fi -#endif // cpPlugins_Interface_QT4 -} + std::stringstream dir; + dir << cpExtensions::CanonicalPath( *i ); + if( dir.str( ) != "" ) + this->m_Paths.insert( dir.str( ) ); -// ------------------------------------------------------------------------- -cpPlugins::Interface:: -BaseApplication* cpPlugins::Interface::Plugins:: -GetApplication( ) -{ - return( this->m_Application ); -} - -// ------------------------------------------------------------------------- -const cpPlugins::Interface:: -BaseApplication* cpPlugins::Interface::Plugins:: -GetApplication( ) const -{ - return( this->m_Application ); + } // rof } // ------------------------------------------------------------------------- void cpPlugins::Interface::Plugins:: -SetApplication( BaseApplication* a ) -{ - this->m_Application = a; -} - -// ------------------------------------------------------------------------- -bool cpPlugins::Interface::Plugins:: -LoadPluginsPath( const std::string& path ) -{ - this->BlockWidget( ); - - // Load all plugins from given folder - std::list< std::string > files = - this->m_Interface.LoadFromFolder( path ); - - // Update a simple track - bool ret = false; - if( files.size( ) > 0 ) - { - for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt ) - { - this->m_LoadedPlugins.insert( *fIt ); - this->m_LastLoadedPlugin = *fIt; - ret = true; - - } // rof - this->_UpdateLoadedPluginsInformation( ); - - } // fi - - this->UnblockWidget( ); - return( ret ); -} - -// ------------------------------------------------------------------------- -bool cpPlugins::Interface::Plugins:: -LoadPlugins( const std::string& fname ) +LoadEnvironments( ) { - this->BlockWidget( ); - - // Is it already loaded? - bool ret = true; - if( this->m_LoadedPlugins.find( fname ) == this->m_LoadedPlugins.end( ) ) + std::stringstream all_errors; + for( auto d = this->m_Paths.begin( ); d != this->m_Paths.end( ); ++d ) { - // Was it succesfully loaded? - ret = this->m_Interface.Load( fname ); - - // Update a simple track - if( ret ) + std::stringstream name; + name << *d << cpPlugins_CONFIG; + std::string buffer; + if( cpExtensions::Read( buffer, name.str( ) ) ) { - this->m_LoadedPlugins.insert( fname ); - this->m_LastLoadedPlugin = fname; - this->_UpdateLoadedPluginsInformation( ); + std::istringstream input( buffer ); + for( std::string line; std::getline( input, line ); ) + { + std::vector< std::string > tokens; + cpExtensions::Tokenize( tokens, line, "@" ); + std::string library_file = ""; + if( tokens[ 0 ] == "local" ) + library_file = + cpExtensions::CanonicalPath( + *d + std::string( cpPlugins_LIB_PREFIX ) + + tokens[ 1 ] + std::string( cpPlugins_LIB_EXT ) + ); + else if( tokens[ 0 ] == "global" ) + library_file = tokens[ 1 ]; + + if( library_file != "" ) + { + std::string error = ""; + void* hnd = cpPlugins::OS::DLLManager::Load( library_file, error ); + if( hnd != NULL ) + this->m_Libraries[ library_file ] = hnd; + else + all_errors << " ; " << error; + + } // fi + + } // rof } // fi - } // fi - - this->UnblockWidget( ); - return( ret ); -} - -// ------------------------------------------------------------------------- -bool cpPlugins::Interface::Plugins:: -LoadPluginsConfigurationFile( const std::string& fname ) -{ - // Load file into a char buffer - std::ifstream in( - fname.c_str( ), std::ios::in | std::ios::binary | std::ios::ate - ); - if( !in.is_open( ) ) - return( false ); - - std::streampos size = in.tellg( ); - char* buffer = new char[ size ]; - in.seekg( 0, std::ios::beg ); - in.read( buffer, size ); - in.close( ); - - // Read stream - std::stringstream in_stream( buffer ); - char line[ 4096 ]; - while( in_stream ) - { - in_stream.getline( line, 4096 ); - this->LoadPlugins( line ); - - } // elihw - delete buffer; - - return( true ); -} - -// ------------------------------------------------------------------------- -const cpPlugins::Interface::Plugins:: -TStringContainer& cpPlugins::Interface::Plugins:: -GetLoadedPlugins( ) const -{ - return( this->m_LoadedPlugins ); -} - -// ------------------------------------------------------------------------- -void cpPlugins::Interface::Plugins:: -GetLoadedCategories( TStringContainer& categories ) const -{ - categories.clear( ); - auto fIt = this->m_LoadedFilters.begin( ); - for( ; fIt != this->m_LoadedFilters.end( ); ++fIt ) - categories.insert( fIt->first ); -} - -// ------------------------------------------------------------------------- -void cpPlugins::Interface::Plugins:: -GetLoadedFilters( TStringContainer& filters ) const -{ - filters.clear( ); - auto pIt = this->m_LoadedFilters.begin( ); - for( ; pIt != this->m_LoadedFilters.end( ); ++pIt ) - for( auto fIt = pIt->second.begin( ); fIt != pIt->second.end( ); ++fIt ) - filters.insert( *fIt ); -} + } // rof -// ------------------------------------------------------------------------- -const cpPlugins::Interface::Plugins:: -TStringContainer& cpPlugins::Interface::Plugins:: -GetLoadedFilters( const std::string& category ) const -{ - static const TStringContainer EMPTY; - auto pIt = this->m_LoadedFilters.find( category ); - if( pIt != this->m_LoadedFilters.end( ) ) - return( pIt->second ); - else - return( EMPTY ); + // Throw errors + if( all_errors.str( ) != "" ) + throw std::runtime_error( + std::string( "Loading environment libraries errors: " ) + + all_errors.str( ) + ); } // ------------------------------------------------------------------------- void cpPlugins::Interface::Plugins:: -AddInteractor( vtkRenderWindowInteractor* interactor ) +SaveEnvironments( const std::string& dir ) const { - this->m_Interactors.insert( interactor ); -} + std::stringstream buffer; + for( auto i = this->m_Paths.begin( ); i != this->m_Paths.end( ); ++i ) + buffer << *i << std::endl; -// ------------------------------------------------------------------------- -void cpPlugins::Interface::Plugins:: -RemoveInteractor( vtkRenderWindowInteractor* interactor ) -{ - this->m_Interactors.erase( interactor ); + std::stringstream fname; + fname << dir; + if( !cpExtensions::IsPathSeparator( dir.back( ) ) ) + fname << cpExtensions_PATH_SEPARATOR; + fname << cpPlugins_PATHS; + if( !cpExtensions::Write( buffer.str( ), fname.str( ) ) ) + throw std::runtime_error( "Error writing environment file." ); } // ------------------------------------------------------------------------- void cpPlugins::Interface::Plugins:: -ClearInteractors( ) +OpenEnvironments( const std::string& dir ) { - this->m_Interactors.clear( ); -} - -// ------------------------------------------------------------------------- -#define cpPlugins_Plugins_HasMacro( F ) \ - bool cpPlugins::Interface::Plugins:: \ - Has##F( ) const \ - { \ - return( this->m_IOFilters.find( #F ) != this->m_IOFilters.end( ) ); \ - } - -cpPlugins_Plugins_HasMacro( ImageReader ); -cpPlugins_Plugins_HasMacro( DicomSeriesReader ); -cpPlugins_Plugins_HasMacro( MeshReader ); -cpPlugins_Plugins_HasMacro( ImageWriter ); -cpPlugins_Plugins_HasMacro( MeshWriter ); - -// ------------------------------------------------------------------------- -std::string cpPlugins::Interface::Plugins:: -ReadImage( const std::string& fname, const std::string& parent ) -{ - std::vector< std::string > fnames( 1, fname ); - return( this->ReadImage( fnames, parent ) ); -} - -// ------------------------------------------------------------------------- -std::string cpPlugins::Interface::Plugins:: -ReadImage( - const std::vector< std::string >& fnames, const std::string& parent - ) -{ - // Load source - this->_ActivateIOFilter( "ImageReader" ); - - // Configure reader - TParameters* params = this->GetActiveFilterParameters( ); - params->ClearStringList( "FileNames" ); - for( auto nIt = fnames.begin( ); nIt != fnames.end( ); ++nIt ) - params->AddToStringList( "FileNames", *nIt ); - - // Execute filter - return( this->_ReadData( parent ) ); -} - -// ------------------------------------------------------------------------- -std::string cpPlugins::Interface::Plugins:: -ReadImage( const std::string& parent ) -{ - // Load source - this->_ActivateIOFilter( "ImageReader" ); - - // Try to configure source - if( this->ConfigureActiveFilter( ) == TProcessObject::DialogResult_Cancel ) + std::vector< std::string > tokens; + cpExtensions::Tokenize( tokens, dir, cpPlugins_ENV_SEPARATOR ); + for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt ) { - this->DeactivateFilter( ); - return( "" ); - - } // fi - - // Execute filter - return( this->_ReadData( parent ) ); -} - -// ------------------------------------------------------------------------- -std::string cpPlugins::Interface::Plugins:: -ReadDicomSeries( const std::string& parent ) -{ - // Load source - this->_ActivateIOFilter( "DicomSeriesReader" ); - - // Try to configure source - if( this->ConfigureActiveFilter( ) == TProcessObject::DialogResult_Cancel ) - { - this->DeactivateFilter( ); - return( "" ); + std::stringstream fname; + fname << *tIt; + if( !cpExtensions::IsPathSeparator( dir.back( ) ) ) + fname << cpExtensions_PATH_SEPARATOR; + fname << cpPlugins_PATHS; + std::string buffer; + if( cpExtensions::Read( buffer, fname.str( ) ) ) + { + std::istringstream input( buffer ); + std::stringstream paths; + for( std::string line; std::getline( input, line ); ) + paths << line << cpPlugins_ENV_SEPARATOR; + this->AddEnvironments( paths.str( ) ); + } + else + { + bool success = true; + try + { + this->LoadDirectory( dir ); + } + catch( ... ) + { + success = false; + + } // yrt + if( success ) + this->AddEnvironments( dir ); - } // fi + } // fi - // Execute filter - return( this->_ReadData( parent ) ); + } // rof } // ------------------------------------------------------------------------- -std::string cpPlugins::Interface::Plugins:: -ReadMesh( const std::string& fname, const std::string& parent ) +void cpPlugins::Interface::Plugins:: +LoadFile( const std::string& fname ) { - // Load source - this->_ActivateIOFilter( "MeshReader" ); - - // Configure reader - TParameters* params = this->GetActiveFilterParameters( ); - params->SetString( "FileName", fname ); - - // Execute filter - return( this->_ReadData( parent ) ); -} + // Resolve canonical filename + std::string can_name = cpExtensions::CanonicalPath( fname ); + if( can_name == "" ) + throw std::runtime_error( + std::string( "Loading file: can't find library \"" ) + + fname + + std::string( "\"" ) + ); + if( this->m_Plugins.find( can_name ) != this->m_Plugins.end( ) ) + return; -// ------------------------------------------------------------------------- -std::string cpPlugins::Interface::Plugins:: -ReadMesh( const std::string& parent ) -{ - // Load source - this->_ActivateIOFilter( "MeshReader" ); + // Load the library + std::string error = ""; + void* hnd = cpPlugins::OS::DLLManager::Load( can_name, error ); + if( hnd == NULL ) + throw std::runtime_error( + std::string( "Loading plugin library: " ) + error + ); - // Try to configure source - if( this->ConfigureActiveFilter( ) == TProcessObject::DialogResult_Cancel ) + // Get plugins name + typedef const char* ( *_TFunction )( ); + _TFunction plugins_name_function = ( _TFunction )( + cpPlugins::OS::DLLManager::GetFunctionHandle( hnd, "cpPlugins_Name" ) + ); + if( plugins_name_function == NULL ) { - this->DeactivateFilter( ); - return( "" ); + cpPlugins::OS::DLLManager::UnLoad( hnd ); + throw std::runtime_error( + std::string( "Library \"" ) + + can_name + + std::string( "\" not recognized as a cpPlugins library" ) + ); } // fi + std::string plugins_name = plugins_name_function( ); - // Execute filter - return( this->_ReadData( parent ) ); -} - -// ------------------------------------------------------------------------- -bool cpPlugins::Interface::Plugins:: -WriteDataObject( const std::string& fname, const std::string& name ) -{ - typedef cpPlugins::Interface::Image _TImage; - typedef cpPlugins::Interface::Mesh _TMesh; - - // Activate sink - TDataObject* obj = this->GetData< TDataObject >( name ); - if( dynamic_cast< cpPlugins::Interface::Image* >( obj ) != NULL ) - this->_ActivateIOFilter( "ImageWriter" ); - else if( dynamic_cast< cpPlugins::Interface::Mesh* >( obj ) != NULL ) - this->_ActivateIOFilter( "MeshWriter" ); - else + // Get loaded filters + _TFunction function = ( _TFunction )( + cpPlugins::OS::DLLManager::GetFunctionHandle( hnd, "cpPlugins_LoadedFilters" ) + ); + if( function == NULL ) { - this->DeactivateFilter( ); - return( false ); + cpPlugins::OS::DLLManager::UnLoad( hnd ); + throw std::runtime_error( + std::string( "Library \"" ) + + can_name + + std::string( "\" not recognized as a cpPlugins library" ) + ); } // fi - - // Configure writer - TParameters* params = this->GetActiveFilterParameters( ); - params->SetString( "FileName", fname ); - - // Execute filter - return( this->_WriteData( name ) ); -} - -// ------------------------------------------------------------------------- -bool cpPlugins::Interface::Plugins:: -WriteDataObject( const std::string& name ) -{ - typedef cpPlugins::Interface::Image _TImage; - typedef cpPlugins::Interface::Mesh _TMesh; - - // Activate sink - TDataObject* obj = this->GetData< TDataObject >( name ); - if( dynamic_cast< cpPlugins::Interface::Image* >( obj ) != NULL ) - this->_ActivateIOFilter( "ImageWriter" ); - else if( dynamic_cast< cpPlugins::Interface::Mesh* >( obj ) != NULL ) - this->_ActivateIOFilter( "MeshWriter" ); - else + std::string descriptors = function( ); + std::replace( descriptors.begin( ), descriptors.end( ), ';', ' ' ); + std::istringstream str( descriptors ); + TFilters filters; + while( str ) { - this->DeactivateFilter( ); - return( false ); + std::string value, category, name; + str >> value; + if( value == "" ) + continue; + std::replace( value.begin( ), value.end( ), ':', ' ' ); + std::istringstream value_str( value ); + value_str >> category >> name; + + // Check if the filter has been already loaded + bool found = false; + auto fIt = this->m_Filters.find( category ); + if( fIt != this->m_Filters.end( ) ) + found = fIt->second.find( name ) != fIt->second.end( ); + if( found ) + { + cpPlugins::OS::DLLManager::UnLoad( hnd ); + throw std::runtime_error( + std::string( "Filter \"" ) + + category + std::string( "::" ) + name + + std::string( "\" already exists." ) + ); - } // fi + } // fi - // Try to configure source - if( this->ConfigureActiveFilter( ) == TProcessObject::DialogResult_Cancel ) - { - this->DeactivateFilter( ); - return( false ); + // Get filter creator + TCreator creator = ( TCreator )( + cpPlugins::OS::DLLManager::GetFunctionHandle( hnd, category + "_" + name ) + ); + if( creator == NULL ) + { + cpPlugins::OS::DLLManager::UnLoad( hnd ); + throw std::runtime_error( + std::string( "Filter \"" ) + + category + std::string( "::" ) + name + + std::string( "\" does not have a valid creator." ) + ); - } // fi + } // fi - // Execute filter - return( this->_WriteData( name ) ); -} + TCreatorData data; + data.PluginName = plugins_name; + data.LibraryHandle = hnd; + data.Creator = creator; + filters[ category ][ name ] = data; -// ------------------------------------------------------------------------- -void cpPlugins::Interface::Plugins:: -ClearDataObjects( ) -{ - this->m_DataObjects.clear( ); -} + } // elihw -// ------------------------------------------------------------------------- -void cpPlugins::Interface::Plugins:: -DeleteDataObject( const std::string& name ) -{ - // Find and delete object - auto oIt = this->m_DataObjects.find( name ); - if( oIt == this->m_DataObjects.end( ) ) - return; - this->m_DataObjects.erase( oIt ); - - // Delete children - TStringContainer children; - this->GetChildren( children, name ); - auto cIt = children.begin( ); - for( ; cIt != children.end( ); ++cIt ) - this->DeleteDataObject( *cIt ); + // Keep track of all loaded handlers + for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt ) + for( auto nIt = cIt->second.begin( ); nIt != cIt->second.end( ); ++nIt ) + this->m_Filters[ cIt->first ][ nIt->first ] = nIt->second; + this->m_Plugins[ can_name ] = hnd; } // ------------------------------------------------------------------------- void cpPlugins::Interface::Plugins:: -GetDataObjects( TStringContainer& names ) +LoadPlugin( const std::string& pname ) { - names.clear( ); - auto oIt = this->m_DataObjects.begin( ); - for( ; oIt != this->m_DataObjects.end( ); ++oIt ) - names.insert( oIt->first ); -} + std::stringstream fname; + fname << cpPlugins_LIB_PREFIX << pname << cpPlugins_LIB_EXT; + unsigned int count = 0; + for( auto i = this->m_Paths.begin( ); i != this->m_Paths.end( ); ++i ) + { + std::stringstream dir; + dir << *i; + if( !cpExtensions::IsPathSeparator( i->back( ) ) ) + dir << cpExtensions_PATH_SEPARATOR; + dir << fname.str( ); + try + { + this->LoadFile( dir.str( ) ); + } + catch( ... ) + { + count++; -// ------------------------------------------------------------------------- -std::string cpPlugins::Interface::Plugins:: -GetParent( const std::string& name ) const -{ - // Find and delete object - auto oIt = this->m_DataObjects.find( name ); - if( oIt != this->m_DataObjects.end( ) ) - return( oIt->second.first ); - else - return( "" ); -} + } // yrt -// ------------------------------------------------------------------------- -void cpPlugins::Interface::Plugins:: -GetChildren( TStringContainer& names, const std::string& name ) const -{ - names.clear( ); - auto oIt = this->m_DataObjects.begin( ); - for( ; oIt != this->m_DataObjects.end( ); ++oIt ) - if( oIt->second.first == name ) - names.insert( oIt->first ); -} + } // rof -// ------------------------------------------------------------------------- -void cpPlugins::Interface::Plugins:: -GetRoots( TStringContainer& names ) const -{ - this->GetChildren( names, "" ); + // Throw error, if any + if( count == this->m_Paths.size( ) ) + throw std::runtime_error( + std::string( "Could not load plugin " ) + + std::string( "\"" ) + pname + + std::string( "\" from any registered path." ) + ); } // ------------------------------------------------------------------------- -bool cpPlugins::Interface::Plugins:: -ActivateFilter( const std::string& name ) +void cpPlugins::Interface::Plugins:: +LoadDirectory( const std::string& dirname ) { - this->m_ActiveFilter = this->m_Interface.CreateObject( name ); - if( this->m_ActiveFilter.IsNotNull( ) ) + DIR* dir; + struct dirent* ent; + if( ( dir = opendir( dirname.c_str( ) ) ) != NULL ) { - this->m_ActiveFilter->SetPlugins( this ); - auto iIt = this->m_Interactors.begin( ); - for( ; iIt != this->m_Interactors.end( ); ++iIt ) - this->m_ActiveFilter->AddInteractor( *iIt ); - return( true ); + while( ( ent = readdir( dir ) ) != NULL ) + { + try + { + std::stringstream fname; + fname << dirname << cpExtensions_PATH_SEPARATOR << ent->d_name; + this->LoadFile( fname.str( ) ); + } + catch( ... ) { } + + } // elihw + closedir( dir ); } else - return( false ); -} - -// ------------------------------------------------------------------------- -void cpPlugins::Interface::Plugins:: -DeactivateFilter( ) -{ - this->m_ActiveFilter = NULL; -} - -// ------------------------------------------------------------------------- -bool cpPlugins::Interface::Plugins:: -HasActiveFilter( ) const -{ - return( this->m_ActiveFilter.IsNotNull( ) ); -} - -// ------------------------------------------------------------------------- -bool cpPlugins::Interface::Plugins:: -IsActiveFilterInteractive( ) const -{ - if( this->m_ActiveFilter.IsNotNull( ) ) - return( this->m_ActiveFilter->GetInteractive( ) ); - else - return( false ); + throw std::runtime_error( + std::string( "Could not load directory " ) + + std::string( "\"" ) + dirname + std::string( "\"" ) + ); } // ------------------------------------------------------------------------- void cpPlugins::Interface::Plugins:: -GetActiveFilterInputsNames( TStringContainer& names ) const +GuessPlugins( ) { - if( this->m_ActiveFilter.IsNotNull( ) ) - this->m_ActiveFilter->GetInputsNames( names ); -} + for( auto i = this->m_Paths.begin( ); i != this->m_Paths.end( ); ++i ) + { + try { this->LoadDirectory( *i ); } + catch( ... ) { } -// ------------------------------------------------------------------------- -void cpPlugins::Interface::Plugins:: -GetActiveFilterOutputsNames( TStringContainer& names ) const -{ - if( this->m_ActiveFilter.IsNotNull( ) ) - this->m_ActiveFilter->GetOutputsNames( names ); + } // rof } // ------------------------------------------------------------------------- -bool cpPlugins::Interface::Plugins:: -ConnectInputInActiveFilter( - const std::string& object_name, const std::string& input_name - ) +cpPlugins::Interface::Plugins::TProcess::Pointer +cpPlugins::Interface::Plugins:: +CreateProcessObject( const std::string& category, const std::string& name ) { - if( this->m_ActiveFilter.IsNotNull( ) ) + typedef cpPlugins::BaseObjects::ProcessObject::Pointer _Ptr; + _Ptr o = NULL; + auto cIt = this->m_Filters.find( category ); + if( cIt != this->m_Filters.end( ) ) { - TDataObject* dobj = this->GetData< TDataObject >( object_name ); - if( dobj != NULL ) + auto nIt = cIt->second.find( name ); + if( nIt != cIt->second.end( ) ) { - this->m_ActiveFilter->SetInput( input_name, dobj ); - return( true ); + o = reinterpret_cast< _Ptr* >( nIt->second.Creator( ) )->GetPointer( ); + o->SetName( name ); + o->SetPluginName( nIt->second.PluginName ); } // fi } // fi - return( false ); -} - -// ------------------------------------------------------------------------- -bool cpPlugins::Interface::Plugins:: -SetOutputNameInActiveFilter( - const std::string& new_object_name, const std::string& output_name - ) -{ - if( this->m_ActiveFilter.IsNotNull( ) ) - return( - this->m_ActiveFilter->SetOutputObjectName( - new_object_name, output_name - ) - ); - else - return( false ); + return( o ); } // ------------------------------------------------------------------------- cpPlugins::Interface::Plugins:: -TParameters* cpPlugins::Interface::Plugins:: -GetActiveFilterParameters( ) -{ - if( this->m_ActiveFilter.IsNotNull( ) ) - return( this->m_ActiveFilter->GetParameters( ) ); - else - return( NULL ); -} - -// ------------------------------------------------------------------------- -const cpPlugins::Interface::Plugins:: -TParameters* cpPlugins::Interface::Plugins:: -GetActiveFilterParameters( ) const -{ - if( this->m_ActiveFilter.IsNotNull( ) ) - return( this->m_ActiveFilter->GetParameters( ) ); - else - return( NULL ); +Plugins( ) + : Superclass( ) +{ +#ifdef cpPlugins_OS_Windows + char* p; + size_t size; + _dupenv_s( &p, &size, cpPlugins_PATHS ); +#else // cpPlugins_OS_Windows + char* p = std::getenv( cpPlugins_PATHS ); +#endif // cpPlugins_OS_Windows + std::stringstream str; + if( p != NULL ) + str << p << cpPlugins_ENV_SEPARATOR; + str << "."; + this->OpenEnvironments( str.str( ) ); + + // Try to read locally defined paths + /* TODO + std::vector< std::string > tokens; + cpExtensions::Tokenize( tokens, str.str( ), cpPlugins_ENV_SEPARATOR ); + for( auto t = tokens.begin( ); t != tokens.end( ); ++t ) + { + try + { + this->OpenEnvironments( *t ); + } + catch( ... ) { } + + } // rof + */ } // ------------------------------------------------------------------------- cpPlugins::Interface::Plugins:: -TProcessObject::DialogResult cpPlugins::Interface::Plugins:: -ConfigureActiveFilter( ) +~Plugins( ) { - if( this->m_ActiveFilter.IsNotNull( ) ) - return( this->m_ActiveFilter->ExecConfigurationDialog( this->m_Widget ) ); - else - return( TProcessObject::DialogResult_Cancel ); -} - -// ------------------------------------------------------------------------- -bool cpPlugins::Interface::Plugins:: -UpdateActiveFilter( TStringContainer& outputs, const std::string& parent ) -{ - if( this->m_ActiveFilter.IsNull( ) ) - return( false ); - - // Execute filter - this->BlockWidget( ); - std::string err = this->m_ActiveFilter->Update( ); - this->UnblockWidget( ); - - // Associate outputs - outputs.clear( ); - if( err == "" ) - { - std::set< std::string > names; - this->m_ActiveFilter->GetOutputsNames( names ); - for( auto oIt = names.begin( ); oIt != names.end( ); ++oIt ) - { - TDataObject* dobj = - this->m_ActiveFilter->GetOutput< TDataObject >( *oIt ); - this->_InsertNewData( dobj, parent ); - outputs.insert( dobj->GetName( ) ); - - } // rof - this->m_ActiveFilter->DisconnectOutputs( ); - return( true ); - } - else - { -#ifdef cpPlugins_Interface_QT4 - if( this->m_Widget != NULL ) - QMessageBox::critical( - this->m_Widget, - QMessageBox::tr( "Error reading image." ), - QMessageBox::tr( err.c_str( ) ) - ); - else - throw std::runtime_error( - std::string( "Error reading image: " ) + err - ); -#else // cpPlugins_Interface_QT4 - throw std::runtime_error( - std::string( "Error reading image: " ) + err - ); -#endif // cpPlugins_Interface_QT4 - return( false ); - - } // fi } // ------------------------------------------------------------------------- void cpPlugins::Interface::Plugins:: -_UpdateLoadedPluginsInformation( ) -{ - this->m_LoadedFilters.clear( ); - const TInterface::TClasses& cls = this->m_Interface.GetClasses( ); - for( auto i = cls.begin( ); i != cls.end( ); ++i ) +PrintSelf( std::ostream& os, itk::Indent indent ) const +{ + // Show data + os << indent << "----- PATHS -----" << std::endl; + auto paths = this->GetPaths( ); + for( auto paIt = paths.begin( ); paIt != paths.end( ); ++paIt ) + os << indent << *paIt << std::endl; + os << indent << std::endl << indent << "----- PLUGINS -----" << std::endl; + auto plugins = this->GetPlugins( ); + for( auto plIt = plugins.begin( ); plIt != plugins.end( ); ++plIt ) + os << indent << *plIt << std::endl; + os << indent << std::endl << indent << "----- FILTERS -----" << std::endl; + auto categories = this->GetCategories( ); + for( auto cIt = categories.begin( ); cIt != categories.end( ); ++cIt ) { - TProcessObject::Pointer o = this->m_Interface.CreateObject( i->first ); - std::string name = o->GetClassName( ); - std::string category = o->GetClassCategory( ); - - if( - category == "ImageReader" || - category == "ImageWriter" || - category == "MeshReader" || - category == "MeshWriter" || - category == "DicomSeriesReader" - ) - this->m_IOFilters[ category ] = o; - this->m_LoadedFilters[ category ].insert( name ); + os << indent << "** Category: " << *cIt << " **" << std::endl; + auto filters = this->GetFilters( *cIt ); + for( auto fIt = filters.begin( ); fIt != filters.end( ); ++fIt ) + os << indent << indent << indent << "Filter: " << *fIt << std::endl; } // rof -} - -// ------------------------------------------------------------------------- -bool cpPlugins::Interface::Plugins:: -_ActivateIOFilter( const std::string& filter ) -{ - // Activate reader - auto fIt = this->m_IOFilters.find( filter ); - if( fIt != this->m_IOFilters.end( ) ) - { - this->m_ActiveFilter = fIt->second; - return( true ); - } - else - return( false ); -} - -// ------------------------------------------------------------------------- -std::string cpPlugins::Interface::Plugins:: -_ReadData( const std::string& parent ) -{ - if( !( this->HasActiveFilter( ) ) ) - return( "" ); - TStringContainer outputs; - if( this->UpdateActiveFilter( outputs, parent ) ) - return( *( outputs.begin( ) ) ); - else - return( "" ); -} - -// ------------------------------------------------------------------------ -bool cpPlugins::Interface::Plugins:: -_WriteData( const std::string& name ) -{ - if( !( this->HasActiveFilter( ) ) ) - return( false ); - - TStringContainer inputs; - this->GetActiveFilterInputsNames( inputs ); - bool r = true; - for( auto iIt = inputs.begin( ); iIt != inputs.end( ); ++iIt ) - r &= this->ConnectInputInActiveFilter( name, *iIt ); - - if( r ) - { - TStringContainer outputs; - return( this->UpdateActiveFilter( outputs, "" ) ); - } - else - return( false ); -} - -// ------------------------------------------------------------------------- -bool cpPlugins::Interface::Plugins:: -_InsertNewData( TDataObject* dobj, const std::string& parent ) -{ - if( parent != "" ) - { - auto oIt = this->m_DataObjects.find( parent ); - if( oIt == this->m_DataObjects.end( ) ) - return( false ); - - } // fi - this->m_DataObjects[ dobj->GetName( ) ] = _TTreeNode( parent, dobj ); - return( true ); } // eof - $RCSfile$