2
* Copyright (C) 2013 ~ 2015 National University of Defense Technology(NUDT) & Kylin Ltd.
5
* Kobe Lee xiangli@ubuntukylin.com/kobe24_lixiang@126.com
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; version 3.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#include "pluginmanager.h"
21
#include "../component/plugininterface.h"
25
PluginManager::PluginManager(void)
30
PluginManager::~PluginManager(void)
34
PluginManager* PluginManager::Instance()
36
static PluginManager PluginMgr;
40
bool PluginManager::loadPlugin(QString plugin_path)
42
// qDebug() << "plugin_path="<<plugin_path;
43
QDir pluginsDir(plugin_path);
44
foreach (QString fileName, pluginsDir.entryList(QStringList("*.so"),QDir::Files)) {
45
QPluginLoader *pluginLoader = new QPluginLoader(pluginsDir.absoluteFilePath(fileName));
46
QObject *plugin = pluginLoader->instance();
47
if (plugin) {//测试插件是否有效:使用 qobject_cast()测试插件是否给出了相应接口并进行类型转换,转换成接口对象指针.
48
PluginInterface* interface = qobject_cast<PluginInterface*>(plugin);
50
QString guid = interface->getGuid();
51
plugin_map.insert(guid, pluginLoader);
52
qDebug() << "The plugin interface is: " << interface;
55
qWarning() << pluginLoader->errorString();
56
pluginLoader->unload();
57
pluginLoader->deleteLater();
61
qDebug() << "The plugin is invalid===" << pluginLoader->errorString();
68
bool PluginManager::unloadPlugin(QString plugin_guid)
70
QMap<QString, QPluginLoader*>::iterator iter = plugin_map.find(plugin_guid);
71
if (iter == plugin_map.end())
75
iter.value()->unload();
76
plugin_map.erase(iter);