@@ -1,82 +1,119 | |||||
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> |
|
6 | #include <QLibrary> | |
7 | #include <QPluginLoader> |
|
7 | #include <QPluginLoader> | |
8 |
|
8 | |||
9 | Q_LOGGING_CATEGORY(LOG_PluginManager, "PluginManager") |
|
9 | Q_LOGGING_CATEGORY(LOG_PluginManager, "PluginManager") | |
10 |
|
10 | |||
11 | namespace { |
|
11 | namespace { | |
12 |
|
12 | |||
13 | /// Key for retrieving metadata of the plugin |
|
13 | /// Key for retrieving metadata of the plugin | |
14 | const auto PLUGIN_METADATA_KEY = QStringLiteral("MetaData"); |
|
14 | const auto PLUGIN_METADATA_KEY = QStringLiteral("MetaData"); | |
15 |
|
15 | |||
16 | /// Key for retrieving the name of the plugin in its metadata |
|
16 | /// Key for retrieving the name of the plugin in its metadata | |
17 | const auto PLUGIN_NAME_KEY = QStringLiteral("name"); |
|
17 | const auto PLUGIN_NAME_KEY = QStringLiteral("name"); | |
18 |
|
18 | |||
|
19 | /// Helper to state the plugin loading operation | |||
|
20 | struct LoadPluginState { | |||
|
21 | explicit LoadPluginState(const QString &pluginPath) | |||
|
22 | : m_PluginPath{pluginPath}, m_Valid{true}, m_ErrorMessage{} | |||
|
23 | { | |||
|
24 | } | |||
|
25 | ||||
|
26 | void log() const | |||
|
27 | { | |||
|
28 | if (m_Valid) { | |||
|
29 | qCDebug(LOG_PluginManager()) | |||
|
30 | << QObject::tr("File '%1' has been loaded as a plugin").arg(m_PluginPath); | |||
|
31 | } | |||
|
32 | else { | |||
|
33 | qCWarning(LOG_PluginManager()) | |||
|
34 | << QObject::tr("File '%1' can't be loaded as a plugin: %2") | |||
|
35 | .arg(m_PluginPath) | |||
|
36 | .arg(m_ErrorMessage); | |||
|
37 | } | |||
|
38 | } | |||
|
39 | ||||
|
40 | void setError(const QString &errorMessage) | |||
|
41 | { | |||
|
42 | m_Valid = false; | |||
|
43 | m_ErrorMessage = errorMessage; | |||
|
44 | } | |||
|
45 | ||||
|
46 | QString m_PluginPath; | |||
|
47 | bool m_Valid; | |||
|
48 | QString m_ErrorMessage; | |||
|
49 | }; | |||
|
50 | ||||
19 | } // namespace |
|
51 | } // namespace | |
20 |
|
52 | |||
21 | struct PluginManager::PluginManagerPrivate { |
|
53 | struct PluginManager::PluginManagerPrivate { | |
22 | /** |
|
54 | /** | |
23 | * Loads a single plugin into SciQlop. The method has no effect if the plugin is malformed (e.g. |
|
55 | * 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.) |
|
56 | * wrong library type, missing metadata, etc.) | |
25 | * @param pluginPath the path to the plugin library. |
|
57 | * @param pluginPath the path to the plugin library. | |
26 | */ |
|
58 | */ | |
27 | void loadPlugin(const QString &pluginPath) |
|
59 | void loadPlugin(const QString &pluginPath) | |
28 | { |
|
60 | { | |
29 | qCDebug(LOG_PluginManager()) |
|
61 | qCDebug(LOG_PluginManager()) | |
30 | << QObject::tr("Attempting to load file '%1' as a plugin").arg(pluginPath); |
|
62 | << QObject::tr("Attempting to load file '%1' as a plugin").arg(pluginPath); | |
31 |
|
63 | |||
|
64 | LoadPluginState loadState{pluginPath}; | |||
|
65 | ||||
32 | if (QLibrary::isLibrary(pluginPath)) { |
|
66 | if (QLibrary::isLibrary(pluginPath)) { | |
33 | QPluginLoader pluginLoader{pluginPath}; |
|
67 | QPluginLoader pluginLoader{pluginPath}; | |
34 |
|
68 | |||
35 | // Retrieving the plugin name to check if it can be loaded (i.e. no plugin with the same |
|
69 | // Retrieving the plugin name to check if it can be loaded (i.e. no plugin with the same | |
36 | // name has been registered yet) |
|
70 | // name has been registered yet) | |
37 | auto metadata = pluginLoader.metaData().value(PLUGIN_METADATA_KEY).toObject(); |
|
71 | auto metadata = pluginLoader.metaData().value(PLUGIN_METADATA_KEY).toObject(); | |
38 | auto pluginName = metadata.value(PLUGIN_NAME_KEY).toString(); |
|
72 | auto pluginName = metadata.value(PLUGIN_NAME_KEY).toString(); | |
39 |
|
73 | |||
40 | if (pluginName.isEmpty()) { |
|
74 | if (pluginName.isEmpty()) { | |
41 | /// @todo ALX : log error |
|
75 | loadState.setError(QObject::tr("empty file name")); | |
42 | } |
|
76 | } | |
43 | else if (m_RegisteredPlugins.contains(pluginName)) { |
|
77 | else if (m_RegisteredPlugins.contains(pluginName)) { | |
44 | /// @todo ALX : log error |
|
78 | loadState.setError(QObject::tr("name '%1' already registered").arg(pluginName)); | |
45 | } |
|
79 | } | |
46 | else { |
|
80 | else { | |
47 | if (auto pluginInstance = qobject_cast<IPlugin *>(pluginLoader.instance())) { |
|
81 | if (auto pluginInstance = qobject_cast<IPlugin *>(pluginLoader.instance())) { | |
48 | pluginInstance->initialize(); |
|
82 | pluginInstance->initialize(); | |
49 | m_RegisteredPlugins.insert(pluginName, pluginPath); |
|
83 | m_RegisteredPlugins.insert(pluginName, pluginPath); | |
50 | } |
|
84 | } | |
51 | else { |
|
85 | else { | |
52 | /// @todo ALX : log error |
|
86 | loadState.setError(QObject::tr("the file is not a Sciqlop plugin")); | |
53 | } |
|
87 | } | |
54 | } |
|
88 | } | |
55 | } |
|
89 | } | |
56 | else { |
|
90 | else { | |
57 | /// @todo ALX : log error |
|
91 | loadState.setError(QObject::tr("the file is not a library")); | |
58 | } |
|
92 | } | |
|
93 | ||||
|
94 | // Log loading result | |||
|
95 | loadState.log(); | |||
59 | } |
|
96 | } | |
60 |
|
97 | |||
61 | /// Registered plugins (key: plugin name, value: plugin path) |
|
98 | /// Registered plugins (key: plugin name, value: plugin path) | |
62 | QHash<QString, QString> m_RegisteredPlugins; |
|
99 | QHash<QString, QString> m_RegisteredPlugins; | |
63 | }; |
|
100 | }; | |
64 |
|
101 | |||
65 | PluginManager::PluginManager() : impl{spimpl::make_unique_impl<PluginManagerPrivate>()} |
|
102 | PluginManager::PluginManager() : impl{spimpl::make_unique_impl<PluginManagerPrivate>()} | |
66 | { |
|
103 | { | |
67 | } |
|
104 | } | |
68 |
|
105 | |||
69 | void PluginManager::loadPlugins(const QDir &pluginDir) |
|
106 | void PluginManager::loadPlugins(const QDir &pluginDir) | |
70 | { |
|
107 | { | |
71 | // Load plugins |
|
108 | // Load plugins | |
72 | auto pluginInfoList = pluginDir.entryInfoList(QDir::Files, QDir::Name); |
|
109 | auto pluginInfoList = pluginDir.entryInfoList(QDir::Files, QDir::Name); | |
73 | for (auto pluginInfo : qAsConst(pluginInfoList)) { |
|
110 | for (auto pluginInfo : qAsConst(pluginInfoList)) { | |
74 | impl->loadPlugin(pluginInfo.absoluteFilePath()); |
|
111 | impl->loadPlugin(pluginInfo.absoluteFilePath()); | |
75 | } |
|
112 | } | |
76 | } |
|
113 | } | |
77 |
|
114 | |||
78 | int PluginManager::nbPluginsLoaded() const noexcept |
|
115 | int PluginManager::nbPluginsLoaded() const noexcept | |
79 | { |
|
116 | { | |
80 | /// @todo ALX |
|
117 | /// @todo ALX | |
81 | return 0; |
|
118 | return 0; | |
82 | } |
|
119 | } |
General Comments 0
You need to be logged in to leave comments.
Login now