00001 00023 #ifndef PLUGINLOADER_H 00024 #define PLUGINLOADER_H 00025 00026 #include <QDir> 00027 #include <QFile> 00028 #include <QDebug> 00029 #include <QFileInfo> 00030 #include <QPluginLoader> 00031 #include <QString> 00032 #include <QStringList> 00033 00034 #include <vector> 00035 00036 #include "../../Support/plugininterface.h" 00037 00038 template<class T> 00040 class PluginLoader 00041 { 00042 public: 00043 static void loadPlugins(QDir pluginDir, std::vector<T> &retVector, QStringList filter = (QStringList("*"))); 00044 static QObject* loadPlugin(QFileInfo filePath); 00045 }; 00046 00047 template<class T> 00053 void PluginLoader<T>::loadPlugins(QDir pluginDir, std::vector<T> &retVector, QStringList filter) 00054 { 00055 qDebug() << "PluginLoader<T>::loadPlugins()"; 00056 pluginDir.setFilter(QDir::Files); 00057 pluginDir.setNameFilters(filter); 00058 00059 std::vector<T> pluginsFriction; 00060 00061 QFileInfoList fileList = pluginDir.entryInfoList(); 00062 QObject *plugin; 00063 for (int i = 0; i < fileList.size(); ++i) { 00064 QFileInfo fileInfo = fileList.at(i); 00065 plugin = loadPlugin(fileInfo); 00066 if (plugin != NULL) { 00067 pluginsFriction.push_back(qobject_cast<T>(plugin)); 00068 } 00069 } 00070 retVector.swap(pluginsFriction); 00071 00072 } 00073 00074 template<class T> 00075 QObject * PluginLoader<T>::loadPlugin(QFileInfo fileInfo) 00076 { 00077 QObject *plugin; 00078 QPluginLoader pluginLoader(fileInfo.absoluteFilePath()); 00079 pluginLoader.load(); 00080 if (pluginLoader.isLoaded()) { 00081 plugin = pluginLoader.instance(); 00082 if (plugin) { 00083 qDebug() << "Plugin " << fileInfo.fileName() << " successfully loaded"; 00084 return plugin; 00085 } else { 00086 qDebug() << "- Loading of plugin " << fileInfo.fileName() << " failed, no instance"; 00087 } 00088 } else { 00089 qDebug() << "- Loading of plugin " << fileInfo.fileName() << " failed, not successfully loaded"; 00090 qDebug() << "Reason: " << pluginLoader.errorString(); 00091 } 00092 return NULL; 00093 } 00094 00095 #endif // PLUGINLOADER_H