VMCPlugin/plugins/PluginManager.cpp

135 lines
4.1 KiB
C++
Raw Normal View History

#include <QPluginLoader>
#include <QSettings>
#include "PluginManager.h"
#include "ATBHMIconfig.h"
#include "plugins/ATBAPPplugin.h"
#include "plugins/CC/CCInterface.h"
#include "plugins/CalculatePrice/CalculatePriceInterface.h"
PluginManager::PluginManager(ATBHMIconfig *config, QObject *parent) :
QObject(parent),
config(config)
{
this->loadPlugins();
}
void PluginManager::loadPlugins()
{
/* NOTE: setting a search path via 'QCoreApplication::addLibraryPath' does not work for
* user specific plugins...
* This seems to work only for QT core plugins which are loaded before QApplication object is
* created.
* => we set the path for our plugins manually here.
*/
// DEBUG
/* qDebug() << "--------------------------- DEBUG -----------------------------------";
* qDebug() << "QCoreApplication::applicationDirPath() = " << QCoreApplication::applicationDirPath();
* qDebug() << " ";
* qDebug() << "QCoreApplication::libraryPaths()";
* foreach (const QString &path, QCoreApplication::libraryPaths()) {
* qDebug() << " " << path;
* }
* qDebug() << "---------------------------------------------------------------------";
*/
/* NOTE: qputenv() to set LD_LIBRARY_PATH does not work either!
* We tried this to set the path were libraries used by the plugin itself could be found
* (e.g. kdsoap-library, which is used by several plugins).
*/
// load all available plugins in config group [PLUGINS]
QSettings * settings = this->config->getSettingsPtr();
if (!settings->childGroups().contains("PLUGINS")) {
qCritical() << "PluginManager: no plugins defined in config";
return;
}
settings->beginGroup("PLUGINS");
QStringList pluginStringList = settings->childKeys();
QStringListIterator PluginsIterator(pluginStringList);
QString pluginKey;
QString pluginName;
while(PluginsIterator.hasNext()) {
pluginKey = PluginsIterator.next();
pluginName = settings->value(pluginKey, "").toString();
if (pluginName == "") {
qCritical() << "PluginManager: plugin " << pluginKey << " is not defined";
}
else if (pluginKey.startsWith('#')) {
qCritical() << "PluginManager: skip comment " << pluginKey;
}
else if (QString::compare(pluginName, "notUsed", Qt::CaseInsensitive) == 0) {
qCritical() << "PluginManager: plugin " << pluginKey << " is not used";
}
else {
if (!QLibrary::isLibrary(pluginName)) pluginName.append(".so");
if (!pluginName.startsWith("lib")) pluginName.prepend("lib");
if (!pluginName.startsWith("plugins")) pluginName.prepend("plugins/");
if (!pluginName.startsWith('/')) {
pluginName.prepend('/').prepend(QApplication::applicationDirPath());
}
qCritical() << "PluginManager::loadPlugins() load plugin: "
<< pluginName;
// TODO: check, if plugin file is available, readable etc...
QPluginLoader* pluginLoader = new QPluginLoader();
pluginLoader->setFileName(pluginName);
plugins.insert(pluginKey, pluginLoader);
}
}
settings->endGroup();
}
QObject * PluginManager::getInstance(const QString & pluginname)
{
QObject * result = nullptr;
QPluginLoader* pluginLoader = plugins[pluginname];
if (pluginLoader != nullptr) {
QObject* plugin = pluginLoader->instance();
if (!pluginLoader->isLoaded()) {
qCritical() << "PluginManager::getInstance("
<< pluginname
<< ") errorString: "
<< pluginLoader->errorString();
}
else {
result = plugin;
}
}
return result;
}
const QList<QString> PluginManager::listAvailablePlugins()
{
return plugins.keys();
}
bool PluginManager::isPluginAvailable(const QString & pluginname)
{
if (plugins[pluginname] != nullptr) {
return true;
}
else {
return false;
}
}