~feng-kylin/youker-assistant/youker-assistant

« back to all changes in this revision

Viewing changes to src/pluginmanager.cpp

  • Committer: lixiang
  • Date: 2018-03-06 03:13:06 UTC
  • Revision ID: lixiang@kylinos.cn-20180306031306-fd7qnru3vm4a1xjd
Rewrite with Qt5, and add system monitor

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 ~ 2015 National University of Defense Technology(NUDT) & Kylin Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *  Kobe Lee    xiangli@ubuntukylin.com/kobe24_lixiang@126.com
 
6
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
#include "pluginmanager.h"
 
21
#include "../component/plugininterface.h"
 
22
#include <QDir>
 
23
#include <QDebug>
 
24
 
 
25
PluginManager::PluginManager(void)
 
26
{
 
27
 
 
28
}
 
29
 
 
30
PluginManager::~PluginManager(void)
 
31
{
 
32
}
 
33
 
 
34
PluginManager* PluginManager::Instance()
 
35
{
 
36
    static PluginManager PluginMgr;
 
37
    return &PluginMgr;
 
38
}
 
39
 
 
40
bool PluginManager::loadPlugin(QString plugin_path)
 
41
{
 
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);
 
49
            if (interface) {
 
50
                QString guid = interface->getGuid();
 
51
                plugin_map.insert(guid, pluginLoader);
 
52
                qDebug() << "The plugin interface is: " << interface;
 
53
            }
 
54
            else {
 
55
                qWarning() << pluginLoader->errorString();
 
56
                pluginLoader->unload();
 
57
                pluginLoader->deleteLater();
 
58
            }
 
59
        }
 
60
        else {
 
61
            qDebug() << "The plugin is invalid===" << pluginLoader->errorString();
 
62
            delete pluginLoader;
 
63
        }
 
64
    }
 
65
    return true;
 
66
}
 
67
 
 
68
bool PluginManager::unloadPlugin(QString plugin_guid)
 
69
{
 
70
    QMap<QString, QPluginLoader*>::iterator iter = plugin_map.find(plugin_guid);
 
71
    if (iter == plugin_map.end())
 
72
    {
 
73
        return false;
 
74
    }
 
75
    iter.value()->unload();
 
76
    plugin_map.erase(iter);
 
77
    return true;
 
78
}