SpikeStream Application Library  0.2
PluginManager.cpp
Go to the documentation of this file.
00001 //SpikeStream includes
00002 #include "PluginManager.h"
00003 #include "SpikeStreamException.h"
00004 using namespace spikestream;
00005 
00006 //Qt includes
00007 #include <QDir>
00008 #include <QtDebug>
00009 #include <QLibrary>
00010 
00011 
00013 PluginManager::PluginManager(QString& pluginFolder) throw(SpikeStreamException){
00014         //Store widget and plugin folder
00015         this->pluginFolder = pluginFolder;
00016 }
00017 
00018 
00020 PluginManager::~PluginManager(){
00021 
00022 }
00023 
00024 
00025 /*-------------------------------------------------------------------*/
00026 /*--------                  PUBLIC METHODS                    -------*/
00027 /*-------------------------------------------------------------------*/
00028 
00030 QStringList PluginManager::getPluginNames() throw(SpikeStreamException){
00031         return pluginFunctionMap.keys();
00032 }
00033 
00034 
00036 QWidget* PluginManager::getPlugin(QString pluginName) throw(SpikeStreamException){
00037         if(pluginFunctionMap.contains(pluginName)){
00038                 QWidget* tempWidget = pluginFunctionMap[pluginName]();
00039                 qDebug()<<"Getting new plugin with name "<<pluginName<<endl;
00040                 return tempWidget;
00041         }
00042         throw SpikeStreamException("ClassLoader: CANNOT FIND OR RESOLVE CLASS: ");
00043 }
00044 
00045 
00047 void PluginManager::loadPlugins(){
00048         //Get a list of files in the plugins directory
00049         QDir pluginDirectory(this->pluginFolder);
00050 
00051         //Filter plugin appropriately depending on the operating system
00052         QStringList filters;
00053         #ifdef LINUX32_SPIKESTREAM
00054                 filters << "*.so";
00055         #endif
00056         #ifdef WIN32_SPIKESTREAM
00057                 filters << "*.dll";
00058         #endif
00059         #ifdef MAC32_SPIKESTREAM
00060                 filters << "*.dylib";
00061         #endif
00062         pluginDirectory.setNameFilters(filters);
00063 
00064         //Get list of plugin files
00065         QStringList fileList = pluginDirectory.entryList();
00066         qDebug()<<"Path: "<<pluginFolder<<". Available plugins: "<<fileList;
00067 
00068         //Load functions pointing to each plugin
00069         QString failureMsg;
00070         for(QList<QString>::iterator fileIter = fileList.begin(); fileIter != fileList.end(); ++fileIter){
00071                 QString filePath = pluginDirectory.absolutePath() + "/" + *fileIter;
00072                 qDebug()<<"Loading: "<<filePath;
00073 
00074                 //Open the library
00075                 QLibrary tmpLib(filePath);
00076 
00077                 //Get functions to create the widget and to get the widget's name
00078                 CreatePluginFunctionType createPluginFunction = (CreatePluginFunctionType) tmpLib.resolve("getClass");
00079                 GetPluginNameFunctionType  getPluginNameFunction = (GetPluginNameFunctionType) tmpLib.resolve("getName");
00080 
00081                 //Store the function to create the widget
00082                 if (createPluginFunction  && getPluginNameFunction) {
00083 
00084                         //Get the name of the plugin
00085                         QString tmpPluginName = getPluginNameFunction();
00086 
00087                         qDebug()<<"Loading plugin with name "<<tmpPluginName;
00088 
00089                         //Store function in map
00090                         pluginFunctionMap[tmpPluginName] = createPluginFunction;
00091                 }
00092                 else{
00093                         failureMsg += *fileIter + " ";
00094                 }
00095         }
00096 
00097         //Throw exception if we have failed to load some or all of the plugins.
00098         if(!failureMsg.isEmpty()){
00099                 throw SpikeStreamException("Failed to obtain required functions from librarie(s): " + failureMsg);
00100         }
00101 }
00102 
00103 
00104 
 All Classes Files Functions Variables Typedefs Friends Defines