/* * 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