]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface/Plugins.cxx
...
[cpPlugins.git] / lib / cpPlugins / Interface / Plugins.cxx
index a5092ccaeee07f2a014d3fe02029f41dd1b645bb..eabd53ef905e45048dd4530530b360b014a2bb43 100644 (file)
 #include <cpPlugins/Interface/Plugins.h>
-
-#include <stdexcept>
-
-#include <cpPlugins/Interface/Image.h>
-#include <cpPlugins/Interface/Mesh.h>
-
-#ifdef cpPlugins_Interface_QT4
-
-#include <QApplication>
-#include <QFileDialog>
-#include <QMessageBox>
-#include <QWidget>
-
-#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 <cpPlugins/OS/DLLManager.h>
+#include <cpPlugins/OS/DirContents.h>
+#include <cpExtensions/Utility.h>
 
 // -------------------------------------------------------------------------
 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( )
-{
-#ifdef cpPlugins_Interface_QT4
-  if( this->m_Widget != NULL )
-  {
-    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
-}
-
-// -------------------------------------------------------------------------
-cpPlugins::Interface::
-BaseApplication* cpPlugins::Interface::Plugins::
-GetApplication( )
+AddEnvironments( const std::string& env )
 {
-  return( this->m_Application );
-}
-
-// -------------------------------------------------------------------------
-const cpPlugins::Interface::
-BaseApplication* cpPlugins::Interface::Plugins::
-GetApplication( ) const
-{
-  return( this->m_Application );
+  std::vector< std::string > directories;
+  cpExtensions::Tokenize( directories, env, cpPlugins_ENV_SEPARATOR );
+  for( auto dir = directories.begin( ); dir != directories.end( ); ++dir )
+    this->m_Paths.insert( cpExtensions::CanonicalPath( *dir ) );
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Plugins::
-SetApplication( BaseApplication* a )
-{
-  this->m_Application = a;
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-LoadPluginsPath( const std::string& path, bool r )
+LoadEnvironments( )
 {
-  this->BlockWidget( );
-
-  // Load all plugins from given folder
-  std::list< std::string > files =
-    this->m_Interface.LoadFromFolder( path, r );
-
-  // Update a simple track
-  bool ret = false;
-  if( files.size( ) > 0 )
+  std::set< std::string > libs;
+  for( auto d = this->m_Paths.begin( ); d != this->m_Paths.end( ); ++d )
   {
-    for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
+    std::stringstream fname;
+    fname << *d << cpPlugins_PATH_SEPARATOR << cpPlugins_CONFIG;
+    std::string buffer;
+    if( cpExtensions::Read( buffer, fname.str( ) ) )
     {
-      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 )
-{
-  this->BlockWidget( );
-
-  // Is it already loaded?
-  bool ret = true;
-  if( this->m_LoadedPlugins.find( fname ) == this->m_LoadedPlugins.end( ) )
-  {
-    // Was it succesfully loaded?
-    ret = this->m_Interface.Load( fname );
-
-    // Update a simple track
-    if( ret )
-    {
-      this->m_LoadedPlugins.insert( fname );
-      this->m_LastLoadedPlugin = fname;
-      this->_UpdateLoadedPluginsInformation( );
+      std::istringstream input( buffer );
+      for( std::string line; std::getline( input, line ); )
+      {
+        auto pos = line.find( "*" );
+        if( pos != std::string::npos )
+        {
+          std::string dname, fname;
+          cpPlugins::OS::SplitPath( dname, fname, line );
+          auto files = cpPlugins::OS::LoadDirContents( dname, false, fname );
+          for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
+            libs.insert( *fIt );
+        }
+        else
+          libs.insert( line );
+
+      } // rof
 
     } // fi
 
-  } // fi
-
-  this->UnblockWidget( );
-  return( ret );
-}
+  } // rof
 
-// -------------------------------------------------------------------------
-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 )
+  for( auto l = libs.begin( ); l != libs.end( ); ++l )
   {
-    in_stream.getline( line, 4096 );
-    this->LoadPlugins( line );
+    std::string lib = cpExtensions::CanonicalPath( *l );
+    if( lib != "" )
+    {
+      if( this->m_Libraries.find( lib ) == this->m_Libraries.end( ) )
+      {
+        std::string error = "";
+        void* hnd = cpPlugins::OS::DLLManager::Load( lib, error );
+        if( hnd != NULL )
+          this->m_Libraries[ lib ] = hnd;
 
-  } // elihw
-  delete buffer;
+      } // fi
 
-  return( true );
-}
-
-// -------------------------------------------------------------------------
-const cpPlugins::Interface::Plugins::
-TStringContainer& cpPlugins::Interface::Plugins::
-GetLoadedPlugins( ) const
-{
-  return( this->m_LoadedPlugins );
-}
+    } // fi
 
-// -------------------------------------------------------------------------
-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 );
+  } // rof
 }
 
 // -------------------------------------------------------------------------
 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 );
-}
+LoadPaths( const std::string& dir )
+{
+  std::stringstream fname, envs;
+  fname << dir;
+  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 );
+    for( std::string line; std::getline( input, line ); )
+      envs << line << cpPlugins_ENV_SEPARATOR;
 
-// -------------------------------------------------------------------------
-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 );
+  } // fi
+  if( envs.str( ).size( ) > 0 )
+    this->AddEnvironments( envs.str( ) );
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Plugins::
-AddInteractor( vtkRenderWindowInteractor* interactor )
+SavePaths( 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( )
-{
-  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 )
+LoadFile( const std::string& fname )
 {
-  // Load source
-  this->_ActivateIOFilter( "ImageReader" );
-
-  // Try to configure source
-  if( this->ConfigureActiveFilter( ) == TProcessObject::DialogResult_Cancel )
-  {
-    this->DeactivateFilter( );
-    return( "" );
-
-  } // fi
-
-  // 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::
-ReadDicomSeries( const std::string& parent )
-{
-  // Load source
-  this->_ActivateIOFilter( "DicomSeriesReader" );
+  // 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 ) );
-}
-
-// -------------------------------------------------------------------------
-std::string cpPlugins::Interface::Plugins::
-ReadMesh( const std::string& fname, const std::string& parent )
-{
-  // Load source
-  this->_ActivateIOFilter( "MeshReader" );
-
-  // Configure reader
-  TParameters* params = this->GetActiveFilterParameters( );
-  params->SetString( "FileName", fname );
-
-  // Execute filter
-  return( this->_ReadData( parent ) );
-}
-
-// -------------------------------------------------------------------------
-std::string cpPlugins::Interface::Plugins::
-ReadMesh( const std::string& parent )
-{
-  // Load source
-  this->_ActivateIOFilter( "MeshReader" );
-
-  // Try to configure source
-  if( this->ConfigureActiveFilter( ) == TProcessObject::DialogResult_Cancel )
+  // Get loaded filters
+  _TFunction function = ( _TFunction )(
+    cpPlugins::OS::DLLManager::GetFunctionHandle( hnd, "cpPlugins_LoadedFilters" )
+    );
+  if( 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
-
-  // 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
+  std::string descriptors = function( );
+  std::replace( descriptors.begin( ), descriptors.end( ), ';', ' ' );
+  std::istringstream str( descriptors );
+  TFilters filters;
+  while( str )
   {
-    this->DeactivateFilter( );
-    return( false );
-
-  } // fi
-
-  // Configure writer
-  TParameters* params = this->GetActiveFilterParameters( );
-  params->SetString( "FileName", fname );
+    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." )
+        );
 
-  // Execute filter
-  return( this->_WriteData( name ) );
-}
+    } // fi
 
-// -------------------------------------------------------------------------
-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
-  {
-    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
 
-  // Try to configure source
-  if( this->ConfigureActiveFilter( ) == TProcessObject::DialogResult_Cancel )
-  {
-    this->DeactivateFilter( );
-    return( false );
+    TCreatorData data;
+    data.PluginName = plugins_name;
+    data.LibraryHandle = hnd;
+    data.Creator = creator;
+    filters[ category ][ name ] = data;
 
-  } // fi
+  } // elihw
 
-  // Execute filter
-  return( this->_WriteData( name ) );
+  // 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::
-ClearDataObjects( )
+LoadPlugin( const std::string& pname )
 {
-  this->m_DataObjects.clear( );
-}
-
-// -------------------------------------------------------------------------
-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 );
-}
+  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++;
 
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-GetDataObjects( TStringContainer& names )
-{
-  names.clear( );
-  auto oIt = this->m_DataObjects.begin( );
-  for( ; oIt != this->m_DataObjects.end( ); ++oIt )
-    names.insert( oIt->first );
-}
+    } // yrt
 
-// -------------------------------------------------------------------------
-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( "" );
-}
+  } // rof
 
-// -------------------------------------------------------------------------
-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 );
+  // 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." )
+      );
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Plugins::
-GetRoots( TStringContainer& names ) const
-{
-  this->GetChildren( names, "" );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-ActivateFilter( const std::string& name )
+LoadDirectory( const std::string& dirname )
 {
-  this->m_ActiveFilter = this->m_Interface.CreateObject( name );
-  if( this->m_ActiveFilter.IsNotNull( ) )
+  std::stringstream pat;
+  pat << "*" << cpPlugins_LIB_EXT;
+  auto libs = cpPlugins::OS::LoadDirContents( dirname, false, pat.str( ) );
+  for( auto lIt = libs.begin( ); lIt != libs.end( ); ++lIt )
   {
-    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 );
-  }
-  else
-    return( false );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-DeactivateFilter( )
-{
-  this->m_ActiveFilter = NULL;
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-HasActiveFilter( ) const
-{
-  return( this->m_ActiveFilter.IsNotNull( ) );
-}
+    try
+    {
+      this->LoadFile( *lIt );
+    }
+    catch( ... ) { }
 
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-IsActiveFilterInteractive( ) const
-{
-  if( this->m_ActiveFilter.IsNotNull( ) )
-    return( this->m_ActiveFilter->GetInteractive( ) );
-  else
-    return( false );
+  } // rof
 }
 
 // -------------------------------------------------------------------------
 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
+Plugins( )
+  : Superclass( )
 {
-  if( this->m_ActiveFilter.IsNotNull( ) )
-    return( this->m_ActiveFilter->GetParameters( ) );
-  else
-    return( NULL );
+#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->AddEnvironments( str.str( ) );
 }
 
 // -------------------------------------------------------------------------
 cpPlugins::Interface::Plugins::
-TProcessObject::DialogResult cpPlugins::Interface::Plugins::
-ConfigureActiveFilter( )
-{
-  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 )
+~Plugins( )
 {
-  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;
-    else
-      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$