From 9d004e3d18312bea2259bc30064999f9f2db14a6 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 22 Jun 2018 16:15:31 +0200 Subject: Plugin commands no longer require --command --- src/plugin.h | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/plugin.h (limited to 'src/plugin.h') 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 +#include +#include +#include + +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 loadPlugins(const QString &path) +{ + QVector 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 -- cgit v1.2.1