-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginLoader.cpp
More file actions
73 lines (58 loc) · 2.49 KB
/
PluginLoader.cpp
File metadata and controls
73 lines (58 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "PluginLoader.h"
#include <QQmlComponent>
#include <QLibrary>
#include <QDirIterator>
#include <QResource>
#include <QCoreApplication>
#include <QQmlEngine>
#include <QtGlobal>
#ifdef Q_OS_WINDOWS
#include "libloaderapi.h"
#endif
void PluginLoader::addPlugins(QObject* tabBar, QObject* stackLayout, QObject* msgGen)
{
QDir pluginPath = QCoreApplication::applicationDirPath() + "/plugins";
int apiVersion = msgGen->property("ApiVersion").toInt();
if(not pluginPath.exists())
{
return;
}
QDirIterator pluginsIt{pluginPath};
while(pluginsIt.hasNext())
{
QString plugin = pluginsIt.next();
if(not QFile::exists(plugin + "/resources.rcc"))
{
continue;
}
#ifdef Q_OS_WINDOWS
const auto directoryCookie = AddDllDirectory(plugin.toStdWString().c_str());
SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
#endif
QLibrary lib{plugin + "/lib"};
lib.load();
QResource::registerResource(plugin + "/resources.rcc");
QString name = reinterpret_cast<const char*(*)()>(lib.resolve("getPluginName"))();
int requiredApiVersion = reinterpret_cast<int(*)()>(lib.resolve("getRequiredApiVersion"))();
if(requiredApiVersion > apiVersion)
{
qWarning() << name << " required api version: " << requiredApiVersion << " but simulator has: " << apiVersion << " - ignoring plugin";
continue;
}
auto component = new QQmlComponent(::qmlEngine(stackLayout), QUrl{"qrc:/plugins/" + name + "/TabEntry.qml"}, stackLayout);
QObject* controller = reinterpret_cast<QObject*(*)()>(lib.resolve("getPluginController"))();
QQmlEngine::setObjectOwnership(component, QQmlEngine::JavaScriptOwnership);
QQmlEngine::setObjectOwnership(controller, QQmlEngine::JavaScriptOwnership);
QMetaObject::invokeMethod(stackLayout, "addEntry",
Q_ARG(QVariant, QVariant::fromValue(component)),
Q_ARG(QVariant, QVariant::fromValue(controller)));
QMetaObject::invokeMethod(tabBar, "addEntry", Q_ARG(QVariant, QVariant{name}));
connect(msgGen, SIGNAL(rawMsgReceived(const QString&)),
controller, SLOT(handleMessage(const QString&)));
connect(controller, SIGNAL(sendMessageSignal(const QString&)),
msgGen, SLOT(sendMessage(const QString&)));
#ifdef Q_OS_WINDOWS
RemoveDllDirectory(directoryCookie);
#endif
}
}