]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface/Plugins.cxx
Windows compilation: 3/3
[cpPlugins.git] / lib / cpPlugins / Interface / Plugins.cxx
index 30378c21ae5f80363c2a90b24742646c231b6bae..42ab2cc77ded0fd88249a1c6a8bf9f6b8b1b70d5 100644 (file)
 #include <cpPlugins/Interface/Plugins.h>
-#include <cpPlugins/Interface/Config.h>
-
-#include <fstream>
-#include <sstream>
-
-#ifdef cpPlugins_Interface_QT4
-
-#include <QApplication>
-#include <QFileDialog>
-#include <QMenu>
-#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/Interface/Dirent.h>
+#include <cpExtensions/Utility.h>
 
 // -------------------------------------------------------------------------
 cpPlugins::Interface::Plugins::
-Plugins( QWidget* widget )
-  : m_Widget( widget ),
-    m_LastLoadedPlugin( "." )
+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
 {
-  this->m_Interface.UnloadAll( );
+  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( ) )
-        );
+    std::stringstream dir;
+    dir << cpExtensions::CanonicalPath( *i );
+    if( dir.str( ) != "" )
+      this->m_Paths.insert( dir.str( ) );
 
-  } // fi
-#endif // cpPlugins_Interface_QT4
+  } // rof
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Plugins::
-AssociatePluginsToMenu( QMenu* menu, QObject* obj, const char* slot )
+LoadEnvironments( )
 {
-#ifdef cpPlugins_Interface_QT4
-  std::map< std::string, std::set< std::string > >::const_iterator i;
-  std::set< std::string >::const_iterator j;
-
-  menu->clear( );
-  for( i = this->m_Filters.begin( ); i != this->m_Filters.end( ); i++ )
+  std::stringstream all_errors;
+  for( auto d = this->m_Paths.begin( ); d != this->m_Paths.end( ); ++d )
   {
-    QMenu* newMenu = menu->addMenu( i->first.c_str( ) );
-    for( j = i->second.begin( ); j != i->second.end( ); ++j )
+    std::stringstream name;
+    name << *d << cpPlugins_CONFIG;
+    std::string buffer;
+    if( cpExtensions::Read( buffer, name.str( ) ) )
     {
-      QAction* a = newMenu->addAction( j->c_str( ) );
-      QObject::connect( a, SIGNAL( triggered( ) ), obj, slot );
+      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
 
-    } // rof
+    } // fi
 
   } // rof
-#endif // cpPlugins_Interface_QT4
+
+  // Throw errors
+  if( all_errors.str( ) != "" )
+    throw std::runtime_error(
+      std::string( "Loading environment libraries errors: " ) +
+      all_errors.str( )
+      );
 }
 
 // -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-LoadPlugins( const std::string& fname )
+void cpPlugins::Interface::Plugins::
+SaveEnvironments( const std::string& dir ) const
 {
-  this->BlockWidget( );
+  std::stringstream buffer;
+  for( auto i = this->m_Paths.begin( ); i != this->m_Paths.end( ); ++i )
+    buffer << *i << std::endl;
+
+  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." );
+}
 
-  // Is it already loaded?
-  bool ret = true;
-  if( this->m_LoadedPlugins.find( fname ) == this->m_LoadedPlugins.end( ) )
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::Plugins::
+OpenEnvironments( const std::string& dir )
+{
+  std::vector< std::string > tokens;
+  cpExtensions::Tokenize( tokens, dir, cpPlugins_ENV_SEPARATOR );
+  for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt )
   {
-    // Was it succesfully loaded?
-    ret = this->m_Interface.Load( fname );
-
-    // Update a simple track
-    if( ret )
+    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
     {
-      this->m_LoadedPlugins.insert( fname );
-      this->m_LastLoadedPlugin = fname;
-      this->_Update( );
+      bool success = true;
+      try
+      {
+        this->LoadDirectory( dir );
+      }
+      catch( ... )
+      {
+        success = false;
 
-    } // fi
+      } // yrt
+      if( success )
+        this->AddEnvironments( dir );
 
-  } // fi
+    } // fi
 
-  this->UnblockWidget( );
-  return( ret );
+  } // rof
 }
 
 // -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-LoadPluginsConfigurationFile( const std::string& fname )
+void cpPlugins::Interface::Plugins::
+LoadFile( 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
+  // 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;
+
+  // 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
+      );
+
+  // Get plugins name
+  typedef const char* ( *_TFunction )( );
+  _TFunction plugins_name_function = ( _TFunction )(
+    cpPlugins::OS::DLLManager::GetFunctionHandle( hnd, "cpPlugins_Name" )
     );
-  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 )
+  if( plugins_name_function == NULL )
   {
-    in_stream.getline( line, 4096 );
-    this->LoadPlugins( line );
-
-  } // elihw
-  delete buffer;
+    cpPlugins::OS::DLLManager::UnLoad( hnd );
+    throw std::runtime_error(
+      std::string( "Library \"" ) +
+      can_name +
+      std::string( "\" not recognized as a cpPlugins library" )
+      );
 
-  return( true );
-}
+  } // fi
+  std::string plugins_name = plugins_name_function( );
 
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetImageReaderParameters( )
-{
-  return( this->m_ImageReader->GetParameters( ) );
-}
+  // Get loaded filters
+  _TFunction function = ( _TFunction )(
+    cpPlugins::OS::DLLManager::GetFunctionHandle( hnd, "cpPlugins_LoadedFilters" )
+    );
+  if( function == NULL )
+  {
+    cpPlugins::OS::DLLManager::UnLoad( hnd );
+    throw std::runtime_error(
+      std::string( "Library \"" ) +
+      can_name +
+      std::string( "\" not recognized as a cpPlugins library" )
+      );
 
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetMeshReaderParameters( )
-{
-  return( this->m_MeshReader->GetParameters( ) );
-}
+  } // fi
+  std::string descriptors = function( );
+  std::replace( descriptors.begin( ), descriptors.end( ), ';', ' ' );
+  std::istringstream str( descriptors );
+  TFilters filters;
+  while( str )
+  {
+    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." )
+        );
 
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetImageWriterParameters( )
-{
-  return( this->m_ImageWriter->GetParameters( ) );
-}
+    } // fi
 
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetMeshWriterParameters( )
-{
-  return( this->m_MeshWriter->GetParameters( ) );
-}
+    // 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." )
+        );
 
-// -------------------------------------------------------------------------
-const cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetImageReaderParameters( ) const
-{
-  return( this->m_ImageReader->GetParameters( ) );
-}
+    } // fi
 
-// -------------------------------------------------------------------------
-const cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetMeshReaderParameters( ) const
-{
-  return( this->m_MeshReader->GetParameters( ) );
-}
+    TCreatorData data;
+    data.PluginName = plugins_name;
+    data.LibraryHandle = hnd;
+    data.Creator = creator;
+    filters[ category ][ name ] = data;
 
-// -------------------------------------------------------------------------
-const cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetImageWriterParameters( ) const
-{
-  return( this->m_ImageWriter->GetParameters( ) );
-}
+  } // elihw
 
-// -------------------------------------------------------------------------
-const cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetMeshWriterParameters( ) const
-{
-  return( this->m_MeshWriter->GetParameters( ) );
+  // 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;
 }
 
 // -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-ReadImage( TImage::Pointer& image, bool exec_qt )
+void cpPlugins::Interface::Plugins::
+LoadPlugin( const std::string& pname )
 {
-  std::string ret = "";
-  if( this->m_ImageReader.IsNull( ) )
-    ret = "Plugins: No valid image reader. Please load a valid plugin.";
-
-  if( ret == "" )
+  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 )
   {
-    bool execute = true;
-    if( exec_qt )
-      execute = this->m_ImageReader->ExecConfigurationDialog( this->m_Widget );
-    if( execute )
+    std::stringstream dir;
+    dir << *i;
+    if( !cpExtensions::IsPathSeparator( i->back( ) ) )
+      dir << cpExtensions_PATH_SEPARATOR;
+    dir << fname.str( );
+    try
     {
-      this->BlockWidget( );
-      ret = this->m_ImageReader->Update( );
-      this->UnblockWidget( );
-      if( ret == "" )
-      {
-        image = this->m_ImageReader->GetOutput< TImage >( "Output" );
-        this->m_ImageReader->DisconnectOutputs( );
-        this->m_ImageReader->GetParameters( )->ClearStringList( "FileNames" );
-      }
-      else
-        image = NULL;
+      this->LoadFile( dir.str( ) );
+    }
+    catch( ... )
+    {
+      count++;
 
-    } // fi
+    } // yrt
 
-  } // fi
+  } // rof
 
-  // Show an error message and return
-  if( ret != "" )
-  {
-#ifdef cpPlugins_Interface_QT4
-    if( this->m_Widget != NULL )
-      QMessageBox::critical(
-        this->m_Widget,
-        QMessageBox::tr( "Error reading image." ),
-        QMessageBox::tr( ret.c_str( ) )
-        );
-#endif // cpPlugins_Interface_QT4
-    return( false );
-  }
-  else
-    return( true );
+  // 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::
-ReadDicomSeries( TImage::Pointer& image )
+void cpPlugins::Interface::Plugins::
+LoadDirectory( const std::string& dirname )
 {
-  std::string ret = "";
-  if( this->m_DicomSeriesReader.IsNull( ) )
-    ret = "Plugins: No valid dicom series reader. Please load a valid plugin.";
-
-  if( ret == "" )
+  DIR* dir;
+  struct dirent* ent;
+  if( ( dir = opendir( dirname.c_str( ) ) ) != NULL )
   {
-    if( this->m_DicomSeriesReader->ExecConfigurationDialog( this->m_Widget ) )
+    while( ( ent = readdir( dir ) ) != NULL )
     {
-      this->BlockWidget( );
-      ret = this->m_DicomSeriesReader->Update( );
-      this->UnblockWidget( );
-      if( ret == "" )
+      try
       {
-        image = this->m_DicomSeriesReader->GetOutput< TImage >( "Output" );
-        this->m_DicomSeriesReader->DisconnectOutputs( );
-        this->m_DicomSeriesReader->GetParameters( )->
-          ClearStringList( "FileNames" );
+        std::stringstream fname;
+        fname << dirname << cpExtensions_PATH_SEPARATOR << ent->d_name;
+        this->LoadFile( fname.str( ) );
       }
-      else
-        image = NULL;
+      catch( ... ) { }
 
-    } // fi
-
-  } // fi
-
-  // Show an error message and return
-  if( ret != "" )
-  {
-#ifdef cpPlugins_Interface_QT4
-    if( this->m_Widget != NULL )
-      QMessageBox::critical(
-        this->m_Widget,
-        QMessageBox::tr( "Error reading dicom series." ),
-        QMessageBox::tr( ret.c_str( ) )
-        );
-#endif // cpPlugins_Interface_QT4
-    return( false );
+    } // elihw
+    closedir( dir );
   }
   else
-    return( true );
+    throw std::runtime_error(
+      std::string( "Could not load directory " ) +
+      std::string( "\"" ) +  dirname + std::string( "\"" )
+      );
 }
 
 // -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-ReadMesh( TMesh::Pointer& mesh, bool exec_qt )
+void cpPlugins::Interface::Plugins::
+GuessPlugins( )
 {
-  std::string ret = "";
-  if( this->m_MeshReader.IsNull( ) )
-    ret = "Plugins: No valid mesh reader. Please load a valid plugin.";
-
-  if( ret == "" )
+  for( auto i = this->m_Paths.begin( ); i != this->m_Paths.end( ); ++i )
   {
-    bool execute = true;
-    if( exec_qt )
-      execute = this->m_MeshReader->ExecConfigurationDialog( this->m_Widget );
-    if( execute )
-    {
-      this->BlockWidget( );
-      ret = this->m_MeshReader->Update( );
-      this->UnblockWidget( );
-      if( ret == "" )
-      {
-        mesh = this->m_MeshReader->GetOutput< TMesh >( "Output" );
-        this->m_MeshReader->DisconnectOutputs( );
-      }
-      else
-        mesh = NULL;
+    try { this->LoadDirectory( *i ); }
+    catch( ... ) { }
 
-    } // fi
-
-  } // fi
-
-  // Show an error message and return
-  if( ret != "" )
-  {
-#ifdef cpPlugins_Interface_QT4
-    if( this->m_Widget != NULL )
-      QMessageBox::critical(
-        this->m_Widget,
-        QMessageBox::tr( "Error reading image." ),
-        QMessageBox::tr( ret.c_str( ) )
-        );
-#endif // cpPlugins_Interface_QT4
-    return( false );
-  }
-  else
-    return( true );
+  } // rof
 }
 
 // -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-WriteImage( TImage* image, bool exec_qt )
+cpPlugins::Interface::Plugins::TProcess::Pointer
+cpPlugins::Interface::Plugins::
+CreateProcessObject( const std::string& category, const std::string& name )
 {
-  std::string ret = "";
-  if( this->m_ImageWriter.IsNull( ) )
-    ret = "Plugins: No valid image writer. Please load a valid plugin.";
-
-  if( ret == "" )
+  typedef cpPlugins::BaseObjects::ProcessObject::Pointer _Ptr;
+  _Ptr o = NULL;
+  auto cIt = this->m_Filters.find( category );
+  if( cIt != this->m_Filters.end( ) )
   {
-    bool execute = true;
-    if( exec_qt )
-      execute = this->m_ImageWriter->ExecConfigurationDialog( this->m_Widget );
-    if( execute )
+    auto nIt = cIt->second.find( name );
+    if( nIt != cIt->second.end( ) )
     {
-      this->m_ImageWriter->SetInput( "Input", image );
-      this->BlockWidget( );
-      ret = this->m_ImageWriter->Update( );
-      this->UnblockWidget( );
+      o = reinterpret_cast< _Ptr* >( nIt->second.Creator( ) )->GetPointer( );
+      o->SetName( name );
+      o->SetPluginName( nIt->second.PluginName );
 
     } // fi
 
   } // fi
-
-  // Show an error message and return
-  if( ret != "" )
-  {
-#ifdef cpPlugins_Interface_QT4
-    if( this->m_Widget != NULL )
-      QMessageBox::critical(
-        this->m_Widget,
-        QMessageBox::tr( "Error reading image." ),
-        QMessageBox::tr( ret.c_str( ) )
-        );
-#endif // cpPlugins_Interface_QT4
-    return( false );
-  }
-  else
-    return( true );
+  return( o );
 }
 
 // -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-WriteMesh( TMesh* mesh, bool exec_qt )
+cpPlugins::Interface::Plugins::
+Plugins( )
+  : Superclass( )
 {
-  std::string ret = "";
-  if( this->m_MeshWriter.IsNull( ) )
-    ret = "Plugins: No valid mesh writer. Please load a valid plugin.";
-
-  if( ret == "" )
-  {
-    bool execute = true;
-    if( exec_qt )
-      execute = this->m_MeshReader->ExecConfigurationDialog( this->m_Widget );
-    if( execute )
-    {
-      this->m_MeshWriter->SetInput( 0, mesh );
-      this->BlockWidget( );
-      ret = this->m_MeshWriter->Update( );
-      this->UnblockWidget( );
-
-    } // fi
-
-  } // fi
-
-  // Show an error message and return
-  if( ret != "" )
-  {
-#ifdef cpPlugins_Interface_QT4
-    if( this->m_Widget != NULL )
-      QMessageBox::critical(
-        this->m_Widget,
-        QMessageBox::tr( "Error reading image." ),
-        QMessageBox::tr( ret.c_str( ) )
-        );
-#endif // cpPlugins_Interface_QT4
-    return( false );
-  }
-  else
-    return( true );
+#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
+  */
 }
 
 // -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-CreateFilter( TProcessObject::Pointer& filter, const std::string& name )
+cpPlugins::Interface::Plugins::
+~Plugins( )
 {
-  filter = this->m_Interface.CreateProcessObject( name );
-  return( filter.IsNotNull( ) );
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Plugins::
-_Update( )
+PrintSelf( std::ostream& os, itk::Indent indent ) const
 {
-  typedef TInterface::TClasses _C;
-
-  this->m_Filters.clear( );
-  _C& classes = this->m_Interface.GetClasses( );
-  for( _C::const_iterator i = classes.begin( ); i != classes.end( ); ++i )
+  // 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.CreateProcessObject( i->first );
-    std::string name = o->GetClassName( );
-    std::string category = o->GetClassCategory( );
-    if( category == "ImageReader" )
-      this->m_ImageReader = o;
-    else if( category == "ImageWriter" )
-      this->m_ImageWriter = o;
-    else if( category == "MeshReader" )
-      this->m_MeshReader = o;
-    else if( category == "MeshWriter" )
-      this->m_MeshWriter = o;
-    else if( category == "DicomSeriesReader" )
-      this->m_DicomSeriesReader = o;
-    else
-      this->m_Filters[ 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
+
 }
 
 // eof - $RCSfile$