feat(providers): add provider plugin system

Introduce a registry (PluginManager) that owns package managers and can
load additional providers at runtime from shared-library plugins dropped
in a plugins directory. Providers declare their metadata and a factory
through a C ABI (provider-plugin.h); requiresPassword() tells the UI
whether a provider needs sudo. Ships a sample external plugin as a
template for adding new backends.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
BlackAngelTV committed 2026-08-29 13:34:29 +02:00
1 parent a4a345984b
commit a2cef3d256
7 files changed
+390

No files matched your search

+188
View File
@@ -0,0 +1,188 @@
#include "plugin-manager.h"
#include <QDir>
#include <QFileInfo>
#include <QCoreApplication>
// Plugin directories, checked in order. Bundled plugins ship next to the
// executable; 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";
return paths;
}
PluginManager::PluginManager() = default;
PluginManager::~PluginManager() {
// Delete managers first (they may run code), then unload the libraries
// they were compiled into.
for (auto& entry : m_providers) {
delete entry.manager;
delete entry.library;
}
}
void PluginManager::registerBuiltin(ProviderInfo info, IPackageManager* manager) {
info.builtin = true;
info.available = manager->isAvailable();
m_providers.push_back({std::move(info), manager, nullptr});
}
// Load a single shared library as a provider plugin. Returns false (without
// erroring) when the file is not a provider plugin, so unrelated libraries in
// the directory are skipped.
bool PluginManager::loadPluginFile(const QString& filePath) {
typedef const char* (*MetaFn)();
typedef IPackageManager* (*CreateFn)();
// Own the library for the lifetime of the provider: it must stay loaded
// as long as the manager instance created from it exists.
QLibrary* library = new QLibrary(filePath);
if (!library->load()) {
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"));
CreateFn createFn = reinterpret_cast<CreateFn>(library->resolve("fws_create_provider"));
// 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 || !createFn) {
library->unload();
delete library;
return false;
}
// Reject plugins whose id collides with an already registered provider.
const std::string id = idFn();
for (const auto& entry : m_providers) {
if (entry.info.id == id) {
library->unload();
delete library;
return false;
}
}
ProviderInfo info;
info.id = id;
info.name = nameFn();
info.version = versionFn();
info.description = descFn();
info.author = authorFn();
info.path = filePath.toStdString();
if (installHintFn && installHintFn() && *installHintFn()) {
info.installHint = installHintFn();
}
IPackageManager* manager = createFn();
info.available = manager->isAvailable();
m_providers.push_back({std::move(info), manager, library});
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_providers.begin(); it != m_providers.end();) {
if (!it->info.builtin) {
delete it->manager;
delete it->library;
it = m_providers.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_providers) {
entry.info.available = entry.manager->isAvailable();
}
}
const std::vector<PluginManager::ProviderEntry>& PluginManager::providers() const {
return m_providers;
}
std::vector<const PluginManager::ProviderEntry*> PluginManager::availableProviders() const {
std::vector<const ProviderEntry*> result;
for (const auto& entry : m_providers) {
if (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_providers) {
if (entry.info.available && isEnabled(entry.info.id)) {
result.push_back(entry.manager);
}
}
return result;
}
const PluginManager::ProviderEntry* PluginManager::findAvailable(const std::string& id) const {
for (const auto& entry : m_providers) {
if (entry.info.id == id && entry.info.available && isEnabled(entry.info.id)) {
return &entry;
}
}
return nullptr;
}
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_providers) {
if (entry.info.id == id) return entry.manager;
}
return nullptr;
}