玉兔远程控制 0.0.31
载入中...
搜索中...
未找到
ManagerPlugins.cpp
1#include "ManagerPlugins.h"
2#include "RabbitCommonDir.h"
3#include <QPluginLoader>
4#include <QLoggingCategory>
5
6Q_DECLARE_LOGGING_CATEGORY(Service)
7CManagePlugins::CManagePlugins(QObject *parent)
8 : QObject{parent}
9{
10 LoadPlugins();
11}
12
13CManagePlugins::~CManagePlugins()
14{
15 qDebug(Service) << "CManagerPlugins::~CManagerPlugins";
16}
17
18int CManagePlugins::LoadPlugins()
19{
20 foreach (QObject *plugin, QPluginLoader::staticInstances())
21 {
22 CPluginService* p = qobject_cast<CPluginService*>(plugin);
23 if(p)
24 m_Plugins.insert(p->Id(), p);
25 }
26
27 QString szPath = RabbitCommon::CDir::Instance()->GetDirPlugins()
28 + QDir::separator() + "Service";
29 QStringList filters;
30#if defined (Q_OS_WINDOWS) || defined(Q_OS_WIN)
31 filters << "*PluginService*.dll";
32#else
33 filters << "*PluginService*.so";
34#endif
35 return FindPlugins(szPath, filters);
36}
37
38int CManagePlugins::FindPlugins(QDir dir, QStringList filters)
39{
40 QString fileName;
41 if(filters.isEmpty())
42 {
43#if defined (Q_OS_WINDOWS) || defined(Q_OS_WIN)
44 filters << "*PluginService*.dll";
45#else
46 filters << "*PluginService*.so";
47#endif
48 }
49
50 QString szCurrentPath = QDir::currentPath();
51 QStringList files = dir.entryList(filters, QDir::Files);
52 if(!files.isEmpty())
53 {
54 //This method is invalid
55 //QCoreApplication::addLibraryPath(QDir::cleanPath(dir.absolutePath()));
56
57 QDir::setCurrent(QDir::cleanPath(QDir::cleanPath(dir.absolutePath())));
58
59 // This method is valid
60//#if defined(Q_OS_WINDOWS)
61// QString szPath = QString::fromLocal8Bit(qgetenv("PATH"));
62// szPath += ";";
63// szPath += QDir::cleanPath(QDir::cleanPath(dir.absolutePath()));
64// qputenv("PATH", szPath.toLatin1());
65//#endif
66 }
67
68 foreach (fileName, files) {
69 QString szPlugin = dir.absoluteFilePath(fileName);
70 QPluginLoader loader(szPlugin);
71 QObject *plugin = loader.instance();
72 if (plugin) {
73 CPluginService* p = qobject_cast<CPluginService*>(plugin);
74 if(p)
75 {
76 m_Plugins.insert(p->Id(), p);
77 p->InitTranslator();
78 qInfo(Service) << "Load plugin id:" << p->Id()
79 << ";Description:" << p->Description()
80 << ";File:" << szPlugin;
81 }
82 }else{
83 qCritical(Service) << "load plugin error:" << loader.errorString();;
84 }
85
86 foreach (fileName, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
87 QDir pluginDir = dir;
88 if(pluginDir.cd(fileName))
89 FindPlugins(pluginDir, filters);
90 }
91 }
92
93 QDir::setCurrent(szCurrentPath);
94
95 return 0;
96}
服务插件接口。此接口仅由插件实现。
virtual const QString Description() const =0
插件描述
virtual const QString Id() const
ID. Default: Protocol() + "_" + Name()