@@ -1,29 +1,82 | |||||
1 | #include <Plugin/PluginManager.h> |
|
1 | #include <Plugin/PluginManager.h> | |
2 |
|
2 | |||
3 | #include <Plugin/IPlugin.h> |
|
3 | #include <Plugin/IPlugin.h> | |
4 |
|
4 | |||
5 | #include <QDir> |
|
5 | #include <QDir> | |
|
6 | #include <QLibrary> | |||
|
7 | #include <QPluginLoader> | |||
6 |
|
8 | |||
7 | Q_LOGGING_CATEGORY(LOG_PluginManager, "PluginManager") |
|
9 | Q_LOGGING_CATEGORY(LOG_PluginManager, "PluginManager") | |
8 |
|
10 | |||
|
11 | namespace { | |||
|
12 | ||||
|
13 | /// Key for retrieving metadata of the plugin | |||
|
14 | const auto PLUGIN_METADATA_KEY = QStringLiteral("MetaData"); | |||
|
15 | ||||
|
16 | /// Key for retrieving the name of the plugin in its metadata | |||
|
17 | const auto PLUGIN_NAME_KEY = QStringLiteral("name"); | |||
|
18 | ||||
|
19 | } // namespace | |||
|
20 | ||||
9 | struct PluginManager::PluginManagerPrivate { |
|
21 | struct PluginManager::PluginManagerPrivate { | |
|
22 | /** | |||
|
23 | * Loads a single plugin into SciQlop. The method has no effect if the plugin is malformed (e.g. | |||
|
24 | * wrong library type, missing metadata, etc.) | |||
|
25 | * @param pluginPath the path to the plugin library. | |||
|
26 | */ | |||
|
27 | void loadPlugin(const QString &pluginPath) | |||
|
28 | { | |||
|
29 | qCDebug(LOG_PluginManager()) | |||
|
30 | << QObject::tr("Attempting to load file '%1' as a plugin").arg(pluginPath); | |||
|
31 | ||||
|
32 | if (QLibrary::isLibrary(pluginPath)) { | |||
|
33 | QPluginLoader pluginLoader{pluginPath}; | |||
|
34 | ||||
|
35 | // Retrieving the plugin name to check if it can be loaded (i.e. no plugin with the same | |||
|
36 | // name has been registered yet) | |||
|
37 | auto metadata = pluginLoader.metaData().value(PLUGIN_METADATA_KEY).toObject(); | |||
|
38 | auto pluginName = metadata.value(PLUGIN_NAME_KEY).toString(); | |||
|
39 | ||||
|
40 | if (pluginName.isEmpty()) { | |||
|
41 | /// @todo ALX : log error | |||
|
42 | } | |||
|
43 | else if (m_RegisteredPlugins.contains(pluginName)) { | |||
|
44 | /// @todo ALX : log error | |||
|
45 | } | |||
|
46 | else { | |||
|
47 | if (auto pluginInstance = qobject_cast<IPlugin *>(pluginLoader.instance())) { | |||
|
48 | pluginInstance->initialize(); | |||
|
49 | m_RegisteredPlugins.insert(pluginName, pluginPath); | |||
|
50 | } | |||
|
51 | else { | |||
|
52 | /// @todo ALX : log error | |||
|
53 | } | |||
|
54 | } | |||
|
55 | } | |||
|
56 | else { | |||
|
57 | /// @todo ALX : log error | |||
|
58 | } | |||
|
59 | } | |||
|
60 | ||||
|
61 | /// Registered plugins (key: plugin name, value: plugin path) | |||
|
62 | QHash<QString, QString> m_RegisteredPlugins; | |||
10 | }; |
|
63 | }; | |
11 |
|
64 | |||
12 | PluginManager::PluginManager() : impl{spimpl::make_unique_impl<PluginManagerPrivate>()} |
|
65 | PluginManager::PluginManager() : impl{spimpl::make_unique_impl<PluginManagerPrivate>()} | |
13 | { |
|
66 | { | |
14 | } |
|
67 | } | |
15 |
|
68 | |||
16 | void PluginManager::loadPlugins(const QDir &pluginDir) |
|
69 | void PluginManager::loadPlugins(const QDir &pluginDir) | |
17 | { |
|
70 | { | |
18 | // Load plugins |
|
71 | // Load plugins | |
19 | auto pluginInfoList = pluginDir.entryInfoList(QDir::Files, QDir::Name); |
|
72 | auto pluginInfoList = pluginDir.entryInfoList(QDir::Files, QDir::Name); | |
20 | for (auto pluginInfo : qAsConst(pluginInfoList)) { |
|
73 | for (auto pluginInfo : qAsConst(pluginInfoList)) { | |
21 | /// @todo ALX |
|
74 | impl->loadPlugin(pluginInfo.absoluteFilePath()); | |
22 | } |
|
75 | } | |
23 | } |
|
76 | } | |
24 |
|
77 | |||
25 | int PluginManager::nbPluginsLoaded() const noexcept |
|
78 | int PluginManager::nbPluginsLoaded() const noexcept | |
26 | { |
|
79 | { | |
27 | /// @todo ALX |
|
80 | /// @todo ALX | |
28 | return 0; |
|
81 | return 0; | |
29 | } |
|
82 | } |
General Comments 0
You need to be logged in to leave comments.
Login now