aboutsummaryrefslogtreecommitdiff
path: root/src/plugin.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugin.h')
-rw-r--r--src/plugin.h93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/plugin.h b/src/plugin.h
new file mode 100644
index 0000000..4de3407
--- /dev/null
+++ b/src/plugin.h
@@ -0,0 +1,93 @@
+/*
+ * This file is part of smolbote. It's copyrighted by the contributors recorded
+ * in the version control history of the file, available from its original
+ * location: https://neueland.iserlohn-fortress.net/smolbote.hg
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#ifndef SMOLBOTE_PLUGIN_H
+#define SMOLBOTE_PLUGIN_H
+
+#include <interfaces.h>
+#include <QPluginLoader>
+#include <QFileInfo>
+#include <QDir>
+
+inline Plugin loadPluginFromPath(const QString &path)
+{
+ Plugin p;
+ QPluginLoader loader(path);
+
+ if(loader.load()) {
+#ifdef QT_DEBUG
+ qDebug("Loading plugin: %s [ok]", qUtf8Printable(path));
+#endif
+
+ auto meta = loader.metaData().value("MetaData").toObject();
+ p.name = meta.value("name").toString();
+ p.author = meta.value("author").toString();
+ p.shortcut = QKeySequence::fromString(meta.value("shortcut").toString());
+
+ p.instance = loader.instance();
+
+ } else {
+ qDebug("Loading pluing: %s [failed]", qUtf8Printable(path));
+ qDebug("%s", qUtf8Printable(loader.errorString()));
+ }
+
+ return p;
+}
+
+inline QVector<Plugin> loadPlugins(const QString &path)
+{
+ QVector<Plugin> list;
+
+ // quit if there's nothing to load
+ if(path.isEmpty())
+ return list;
+
+ // plugins can be a semicolon-separated list
+ if(path.contains(';')) {
+ auto pluginList = path.split(';');
+ for(const auto &pluginPath : pluginList) {
+ auto plugin = loadPluginFromPath(pluginPath);
+ if(plugin.instance)
+ list.append(plugin);
+ }
+
+ return list;
+ }
+
+ // check if path is path to a file or a folder
+ QFileInfo location(path);
+
+ if(!location.exists()) {
+ qDebug("Plugin path doesn't exist.");
+ return list;
+ }
+
+ if(location.isFile()) {
+ // only load this one plugin
+ auto p = loadPluginFromPath(location.absoluteFilePath());
+ if(p.instance)
+ list.append(p);
+
+ } else if(location.isDir()) {
+ // load all profiles from this directory
+ const auto entries = QDir(location.absoluteFilePath()).entryInfoList(QDir::Files | QDir::Readable);
+ for(const auto &f : entries) {
+ auto p = loadPluginFromPath(f.absoluteFilePath());
+ if(p.instance)
+ list.append(p);
+ }
+#ifdef QT_DEBUG
+ } else {
+ qDebug("Path is neither file nor folder: %s", qUtf8Printable(path));
+#endif
+ }
+
+ return list;
+}
+
+#endif // SMOLBOTE_PLUGIN_H