Files
BlackAngelTV c63b231778 refactor(tree): split the plugin host from the providers it loads
plugins/ held two unrelated things: the host that discovers and installs
plugins, and the plugins themselves. The host moves to plugins/ with the
built-in pacman provider beside it in providers/, where the external
providers already live — so plugins/ is now one thing, and the name says
which.

The external plugin projects only change folder, not layout: providers/
holds aur-provider, flatpak-provider, example-provider and example-addon.
2026-09-29 21:25:34 +02:00

253 lines
8.5 KiB
C++

#include "plugin-manager.h"
#include "app-logging.h"
#include <QDir>
#include <QFileInfo>
#include <QCoreApplication>
// Plugin directories, checked in order. Bundled plugins ship next to the
// executable (<prefix>/bin/plugins after install); user plugins live under
// the standard data directory so they survive application updates.
static QStringList pluginSearchPaths() {
QStringList paths;
paths << QCoreApplication::applicationDirPath() + "/plugins";
paths << QDir::homePath() + "/.local/share/PacManager/plugins";
// System-wide plugins: <prefix>/lib/PacManager/plugins for installs to
// /usr or /usr/local (covers both lib and lib64 layouts).
const QString appDir = QCoreApplication::applicationDirPath();
QDir prefix(appDir);
if (prefix.dirName() == QLatin1String("bin") && prefix.cdUp()) {
paths << prefix.absoluteFilePath("lib/PacManager/plugins");
paths << prefix.absoluteFilePath("lib64/PacManager/plugins");
}
return paths;
}
// Run a plugin's teardown and free what the host owns: the addon/provider
// instance (after onUnload) and the library it was compiled into.
static void unloadEntry(PluginManager::PluginEntry& entry) {
if (entry.plugin) {
entry.plugin->onUnload();
delete entry.plugin;
entry.plugin = nullptr;
}
if (entry.manager) {
delete entry.manager;
entry.manager = nullptr;
}
delete entry.library;
entry.library = nullptr;
}
PluginManager::PluginManager() = default;
PluginManager::~PluginManager() {
// Unload plugins first (they may run code), then the libraries they were
// compiled into.
for (auto& entry : m_plugins) {
unloadEntry(entry);
}
}
void PluginManager::registerBuiltin(PluginInfo info, IPackageManager* manager) {
info.builtin = true;
info.available = manager->isAvailable();
m_plugins.push_back({std::move(info), nullptr, manager, nullptr});
}
// Load a single shared library as a plugin. Returns false (without erroring)
// when the file is not a plugin, so unrelated libraries in the directory are
// skipped. The plugin's kind decides which create function is resolved:
// "addon" -> IPlugin (see plugin.h), anything else -> IPackageManager.
bool PluginManager::loadPluginFile(const QString& filePath) {
typedef const char* (*MetaFn)();
typedef IPackageManager* (*CreateProviderFn)();
typedef IPlugin* (*CreatePluginFn)();
// Own the library for the lifetime of the plugin: it must stay loaded
// as long as the addon/manager instance created from it exists.
QLibrary* library = new QLibrary(filePath);
if (!library->load()) {
qCWarning(pacmgrPlugin) << "not a loadable plugin, skipping" << filePath
<< library->errorString();
delete library;
return false;
}
MetaFn idFn = reinterpret_cast<MetaFn>(library->resolve("fws_plugin_id"));
MetaFn nameFn = reinterpret_cast<MetaFn>(library->resolve("fws_plugin_name"));
MetaFn versionFn = reinterpret_cast<MetaFn>(library->resolve("fws_plugin_version"));
MetaFn descFn = reinterpret_cast<MetaFn>(library->resolve("fws_plugin_description"));
MetaFn authorFn = reinterpret_cast<MetaFn>(library->resolve("fws_plugin_author"));
MetaFn kindFn = reinterpret_cast<MetaFn>(library->resolve("fws_plugin_kind"));
CreateProviderFn createProviderFn =
reinterpret_cast<CreateProviderFn>(library->resolve("fws_create_provider"));
CreatePluginFn createPluginFn =
reinterpret_cast<CreatePluginFn>(library->resolve("fws_create_plugin"));
// Optional: only present when the plugin declares an install hint.
MetaFn installHintFn = reinterpret_cast<MetaFn>(library->resolve("fws_plugin_install_hint"));
if (!idFn || !nameFn || !versionFn || !descFn || !authorFn) {
library->unload();
delete library;
return false;
}
// Reject plugins whose id collides with an already registered plugin.
const std::string id = idFn();
for (const auto& entry : m_plugins) {
if (entry.info.id == id) {
library->unload();
delete library;
return false;
}
}
// Kind defaults to "provider" so legacy plugins without the symbol keep
// loading; "addon" requires the matching create function.
const std::string kind = kindFn ? kindFn() : "provider";
const bool isAddon = (kind == "addon");
if ((isAddon && !createPluginFn) || (!isAddon && !createProviderFn)) {
library->unload();
delete library;
return false;
}
PluginInfo info;
info.id = id;
info.name = nameFn();
info.version = versionFn();
info.description = descFn();
info.author = authorFn();
info.kind = isAddon ? PluginKind::Addon : PluginKind::Provider;
info.path = filePath.toStdString();
if (installHintFn && installHintFn() && *installHintFn()) {
info.installHint = installHintFn();
}
if (isAddon) {
IPlugin* plugin = createPluginFn();
plugin->onLoad();
info.available = plugin->isAvailable();
m_plugins.push_back({std::move(info), plugin, nullptr, library});
} else {
IPackageManager* manager = createProviderFn();
info.available = manager->isAvailable();
m_plugins.push_back({std::move(info), nullptr, manager, library});
}
qCDebug(pacmgrPlugin) << "loaded" << (isAddon ? "addon" : "provider")
<< filePath << "available" << m_plugins.back().info.available;
return true;
}
void PluginManager::scanDirectory(const QString& dir) {
QDir directory(dir);
if (!directory.exists()) return;
const QStringList filters = {
#ifdef Q_OS_WIN
"*.dll"
#elif defined(Q_OS_MACOS)
"*.dylib"
#else
"*.so"
#endif
};
const QStringList files = directory.entryList(filters, QDir::Files);
for (const QString& file : files) {
loadPluginFile(directory.absoluteFilePath(file));
}
}
void PluginManager::unloadExternal() {
for (auto it = m_plugins.begin(); it != m_plugins.end();) {
if (!it->info.builtin) {
unloadEntry(*it);
it = m_plugins.erase(it);
} else {
++it;
}
}
}
void PluginManager::discoverExternal() {
for (const QString& dir : pluginSearchPaths()) {
scanDirectory(dir);
}
}
void PluginManager::reload() {
unloadExternal();
discoverExternal();
}
void PluginManager::refreshAvailability() {
for (auto& entry : m_plugins) {
if (entry.plugin) {
entry.info.available = entry.plugin->isAvailable();
} else {
entry.info.available = entry.manager->isAvailable();
}
}
}
const std::vector<PluginManager::PluginEntry>& PluginManager::plugins() const {
return m_plugins;
}
std::vector<const PluginManager::PluginEntry*> PluginManager::availableProviders() const {
std::vector<const PluginEntry*> result;
for (const auto& entry : m_plugins) {
if (entry.manager && entry.info.available && isEnabled(entry.info.id)) {
result.push_back(&entry);
}
}
return result;
}
std::vector<IPackageManager*> PluginManager::availableManagers() const {
std::vector<IPackageManager*> result;
for (const auto& entry : m_plugins) {
if (entry.manager && entry.info.available && isEnabled(entry.info.id)) {
result.push_back(entry.manager);
}
}
return result;
}
const PluginManager::PluginEntry* PluginManager::findAvailable(const std::string& id) const {
for (const auto& entry : m_plugins) {
if (entry.info.id == id && entry.manager && entry.info.available
&& isEnabled(entry.info.id)) {
return &entry;
}
}
return nullptr;
}
std::vector<IPlugin*> PluginManager::availableAddons() const {
std::vector<IPlugin*> result;
for (const auto& entry : m_plugins) {
if (entry.plugin && entry.info.available && isEnabled(entry.info.id)) {
result.push_back(entry.plugin);
}
}
return result;
}
bool PluginManager::isEnabled(const std::string& id) const {
return m_settings.value(QString("plugins/%1/enabled").arg(QString::fromStdString(id)),
true).toBool();
}
void PluginManager::setEnabled(const std::string& id, bool enabled) {
m_settings.setValue(QString("plugins/%1/enabled").arg(QString::fromStdString(id)), enabled);
}
IPackageManager* PluginManager::managerFor(const std::string& id) const {
for (const auto& entry : m_plugins) {
if (entry.info.id == id) return entry.manager;
}
return nullptr;
}