#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/OS/DirContents.h>
+#include <cpPlugins/Utility.h>
// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::
-Plugins( QWidget* widget )
- : m_Widget( widget ),
- m_LastLoadedPlugin( "." )
-{
-}
+cpPlugins::Interface::Plugins::Pointer
+cpPlugins::Interface::Plugins::m_Singleton = NULL;
// -------------------------------------------------------------------------
cpPlugins::Interface::Plugins::
-~Plugins( )
+Pointer cpPlugins::Interface::Plugins::
+New( )
{
- // TODO: this causes a segfault? this->m_Interface.UnloadAll( );
+ if( Self::m_Singleton.IsNull( ) )
+ Self::m_Singleton = new Self( );
+ return( Self::m_Singleton );
}
// -------------------------------------------------------------------------
-QWidget* cpPlugins::Interface::Plugins::
-GetWidget( )
+itk::LightObject::Pointer cpPlugins::Interface::Plugins::
+CreateAnother( ) const
{
- return( this->m_Widget );
+ itk::LightObject::Pointer smartPtr;
+ smartPtr = Self::m_Singleton;
+ return( smartPtr );
}
// -------------------------------------------------------------------------
-const QWidget* cpPlugins::Interface::Plugins::
-GetWidget( ) const
-{
- return( this->m_Widget );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-SetWidget( QWidget* widget )
+cpPlugins::Interface::Plugins::
+Pointer cpPlugins::Interface::Plugins::
+Clone( ) const
{
- this->m_Widget = widget;
+ return( Self::m_Singleton );
}
// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-BlockWidget( )
+const cpPlugins::Interface::Plugins::
+TFilters& cpPlugins::Interface::Plugins::
+GetFilters( ) const
{
-#ifdef cpPlugins_Interface_QT4
- if( this->m_Widget != NULL )
- {
- QApplication::setOverrideCursor( Qt::WaitCursor );
- this->m_Widget->setEnabled( false );
-
- } // fi
-#endif // cpPlugins_Interface_QT4
+ return( this->m_Filters );
}
// -------------------------------------------------------------------------
void cpPlugins::Interface::Plugins::
-UnblockWidget( )
+LoadPluginsFile( const std::string& libname )
{
-#ifdef cpPlugins_Interface_QT4
- if( this->m_Widget != NULL )
- {
- QApplication::restoreOverrideCursor( );
- this->m_Widget->setEnabled( true );
-
- } // fi
-#endif // cpPlugins_Interface_QT4
+ std::map< std::string, std::set< std::string > > filters;
+ cpPlugins::OS::DLLManager::GetPluginsLibraryContents( filters, libname );
+ THandlers zero( NULL, NULL );
+ for( auto i : filters )
+ for( auto j : i.second )
+ this->m_Filters[ i.first ][ j ] = TLibData( libname, zero );
}
// -------------------------------------------------------------------------
void cpPlugins::Interface::Plugins::
-DialogLoadPlugins( )
+LoadPluginsDirectory( const std::string& dir )
{
-#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( ) )
- );
+ // Create a globbing pattern
+ std::stringstream glob;
+ glob << cpPlugins_LIB_PREFIX << "*" << cpPlugins_LIB_EXT;
- } // fi
-#endif // cpPlugins_Interface_QT4
+ // Get possible shared libraries
+ std::set< std::string > files =
+ cpPlugins::OS::LoadDirContents( dir, false, glob.str( ) );
+ this->m_PluginsPaths.insert( dir );
+ for( auto f : files )
+ try { this->LoadPluginsFile( f ); } catch( ... ) { }
}
// -------------------------------------------------------------------------
void cpPlugins::Interface::Plugins::
-AssociatePluginsToMenu( QMenu* menu, QObject* obj, const char* slot )
+GuessPlugins( )
{
-#ifdef cpPlugins_Interface_QT4
- std::map< std::string, std::set< std::string > >::const_iterator i;
- std::set< std::string >::const_iterator j;
+ // Create a globbing pattern
+ std::stringstream glob;
+ glob << cpPlugins_LIB_PREFIX << "*" << cpPlugins_LIB_EXT;
- menu->clear( );
- for( i = this->m_Filters.begin( ); i != this->m_Filters.end( ); i++ )
+ // Update paths and get possible shared libraries
+ this->_ReadPluginsPathsVariable( );
+ for( auto dir : this->m_PluginsPaths )
{
- QMenu* newMenu = menu->addMenu( i->first.c_str( ) );
- for( j = i->second.begin( ); j != i->second.end( ); ++j )
- {
- QAction* a = newMenu->addAction( j->c_str( ) );
- QObject::connect( a, SIGNAL( triggered( ) ), obj, slot );
-
- } // rof
+ std::set< std::string > files =
+ cpPlugins::OS::LoadDirContents( dir, false, glob.str( ) );
+ for( auto f : files )
+ try { this->LoadPluginsFile( f ); } catch( ... ) { }
} // rof
-#endif // cpPlugins_Interface_QT4
}
// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-LoadPlugins( const std::string& fname )
+void cpPlugins::Interface::Plugins::
+GuessEnvironment( const std::string& dir )
{
- this->BlockWidget( );
-
- // Is it already loaded?
- bool ret = true;
- if( this->m_LoadedPlugins.find( fname ) == this->m_LoadedPlugins.end( ) )
+ std::stringstream fname;
+ fname << dir << cpPlugins_PATH_SEPARATOR << cpPlugins_PATHS;
+ std::string buffer;
+ if( cpPlugins::Read( buffer, fname.str( ) ) )
{
- // 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->_Update( );
-
- } // fi
+ std::istringstream input( buffer );
+ for( std::string line; std::getline( input, line ); )
+ this->m_PluginsPaths.insert( cpPlugins::CanonicalPath( line ) );
} // fi
-
- this->UnblockWidget( );
- return( ret );
}
// -------------------------------------------------------------------------
bool cpPlugins::Interface::Plugins::
-LoadPluginsConfigurationFile( const std::string& fname )
+SaveEnvironment( const std::string& dir )
{
- // 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 );
-}
-
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetImageReaderParameters( )
-{
- return( this->m_ImageReader->GetParameters( ) );
+ std::stringstream buffer;
+ for( auto p : this->m_PluginsPaths )
+ buffer << p << std::endl;
+ std::stringstream fname;
+ fname << dir << cpPlugins_PATH_SEPARATOR << cpPlugins_PATHS;
+ return( cpPlugins::Write( buffer.str( ), fname.str( ) ) );
}
// -------------------------------------------------------------------------
+cpPlugins::BaseObjects::ProcessObject::Pointer
cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetMeshReaderParameters( )
-{
- return( this->m_MeshReader->GetParameters( ) );
-}
-
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetImageWriterParameters( )
-{
- return( this->m_ImageWriter->GetParameters( ) );
-}
-
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetMeshWriterParameters( )
-{
- return( this->m_MeshWriter->GetParameters( ) );
-}
-
-// -------------------------------------------------------------------------
-const cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetImageReaderParameters( ) const
-{
- return( this->m_ImageReader->GetParameters( ) );
-}
-
-// -------------------------------------------------------------------------
-const cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetMeshReaderParameters( ) const
-{
- return( this->m_MeshReader->GetParameters( ) );
-}
-
-// -------------------------------------------------------------------------
-const cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetImageWriterParameters( ) const
+CreateFilter( const std::string& category, const std::string& name )
{
- return( this->m_ImageWriter->GetParameters( ) );
-}
-
-// -------------------------------------------------------------------------
-const cpPlugins::Interface::Plugins::
-TParameters* cpPlugins::Interface::Plugins::
-GetMeshWriterParameters( ) const
-{
- return( this->m_MeshWriter->GetParameters( ) );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-ReadImage( TImage::Pointer& image, bool exec_qt )
-{
- std::string ret = "";
- if( this->m_ImageReader.IsNull( ) )
- ret = "Plugins: No valid image reader. Please load a valid plugin.";
-
- if( ret == "" )
+ typedef void* ( *_TCreator )( );
+ typedef cpPlugins::BaseObjects::ProcessObject::Pointer _TPtr;
+ _TPtr o = NULL;
+ auto cat = this->m_Filters.find( category );
+ if( cat != this->m_Filters.end( ) )
{
- TProcessObject::DialogResult dret = TProcessObject::DialogResult_NoModal;
- if( exec_qt )
- dret = this->m_ImageReader->ExecConfigurationDialog( this->m_Widget );
- if( dret != TProcessObject::DialogResult_Cancel )
+ auto nam = cat->second.find( name );
+ if( nam != cat->second.end( ) )
{
- this->BlockWidget( );
- ret = this->m_ImageReader->Update( );
- this->UnblockWidget( );
- if( ret == "" )
+ void* l_hnd = nam->second.second.first;
+ void* f_hnd = nam->second.second.second;
+ if( l_hnd == NULL )
{
- image = this->m_ImageReader->GetOutput< TImage >( "Output" );
- this->m_ImageReader->DisconnectOutputs( );
- this->m_ImageReader->GetParameters( )->ClearStringList( "FileNames" );
- }
- else
- image = NULL;
-
- } // 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 );
-}
+ l_hnd = cpPlugins::OS::DLLManager::LoadPlugins( nam->second.first );
+ nam->second.second.first = l_hnd;
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-ReadDicomSeries( TImage::Pointer& image )
-{
- std::string ret = "";
- if( this->m_DicomSeriesReader.IsNull( ) )
- ret = "Plugins: No valid dicom series reader. Please load a valid plugin.";
-
- if( ret == "" )
- {
- TProcessObject::DialogResult dret =
- this->m_DicomSeriesReader->ExecConfigurationDialog( this->m_Widget );
- if( dret != TProcessObject::DialogResult_Cancel )
- {
- this->BlockWidget( );
- ret = this->m_DicomSeriesReader->Update( );
- this->UnblockWidget( );
- if( ret == "" )
+ } // fi
+ if( f_hnd == NULL )
{
- image = this->m_DicomSeriesReader->GetOutput< TImage >( "Output" );
- this->m_DicomSeriesReader->DisconnectOutputs( );
- this->m_DicomSeriesReader->GetParameters( )->
- ClearStringList( "FileNames" );
- }
- else
- image = NULL;
-
- } // fi
-
- } // fi
+ f_hnd =
+ cpPlugins::OS::DLLManager::LoadCreator( l_hnd, category, name );
+ nam->second.second.second = f_hnd;
- // 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 );
- }
- else
- return( true );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-ReadMesh( TMesh::Pointer& mesh, bool exec_qt )
-{
- std::string ret = "";
- if( this->m_MeshReader.IsNull( ) )
- ret = "Plugins: No valid mesh reader. Please load a valid plugin.";
-
- if( ret == "" )
- {
- TProcessObject::DialogResult dret = TProcessObject::DialogResult_NoModal;
- if( exec_qt )
- dret = this->m_MeshReader->ExecConfigurationDialog( this->m_Widget );
- if( dret != TProcessObject::DialogResult_Cancel )
- {
- this->BlockWidget( );
- ret = this->m_MeshReader->Update( );
- this->UnblockWidget( );
- if( ret == "" )
+ } // fi
+ _TCreator creator = reinterpret_cast< _TCreator >( f_hnd );
+ if( creator != NULL )
{
- mesh = this->m_MeshReader->GetOutput< TMesh >( "Output" );
- this->m_MeshReader->DisconnectOutputs( );
- }
- else
- mesh = NULL;
-
- } // 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 );
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-WriteImage( TImage* image, bool exec_qt )
-{
- std::string ret = "";
- if( this->m_ImageWriter.IsNull( ) )
- ret = "Plugins: No valid image writer. Please load a valid plugin.";
+ void* a = creator( );
+ o = reinterpret_cast< _TPtr* >( a )->GetPointer( );
+ o->SetName( name );
+ o->SetPluginName( nam->second.first );
- if( ret == "" )
- {
- TProcessObject::DialogResult dret = TProcessObject::DialogResult_NoModal;
- if( exec_qt )
- dret = this->m_ImageWriter->ExecConfigurationDialog( this->m_Widget );
- if( dret != TProcessObject::DialogResult_Cancel )
- {
- this->m_ImageWriter->SetInput( "Input", image );
- this->BlockWidget( );
- ret = this->m_ImageWriter->Update( );
- this->UnblockWidget( );
+ } // fi
} // 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 );
+ if( o.IsNull( ) )
+ throw std::runtime_error(
+ std::string( "Could not create a valid ProcessObject of type \"" ) +
+ category + std::string( ":" ) +
+ name + std::string( "\"" )
+ );
+ 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 == "" )
- {
- TProcessObject::DialogResult dret = TProcessObject::DialogResult_NoModal;
- if( exec_qt )
- dret = this->m_MeshWriter->ExecConfigurationDialog( this->m_Widget );
- if( dret != TProcessObject::DialogResult_Cancel )
- {
- this->m_MeshWriter->SetInput( "Input", 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 );
+ cpPlugins::OS::DLLManager::TeaseLoadedLibraries( );
}
// -------------------------------------------------------------------------
-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 )
+ for( auto i : this->m_Filters )
{
- 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 << "+ " << i.first << std::endl;
+ for( auto j : i.second )
+ os << indent << "|----> " << j.first << std::endl;
} // rof
}
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::Plugins::
+_ReadPluginsPathsVariable( )
+{
+#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 << ".";
+ std::vector< std::string > tokens;
+ cpPlugins::Tokenize( tokens, str.str( ), cpPlugins_ENV_SEPARATOR );
+ for( auto dir : tokens )
+ this->m_PluginsPaths.insert( cpPlugins::CanonicalPath( dir ) );
+}
+
// eof - $RCSfile$