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:
7 files changed
+390
No files matched your search
@@ -15,6 +15,7 @@ set(SOURCES
|
||||
src/main.cpp
|
||||
src/main-window.cpp
|
||||
src/pacman-manager.cpp
|
||||
src/plugin-manager.cpp
|
||||
src/history-manager.cpp
|
||||
src/package-model.cpp
|
||||
resources/resources.qrc
|
||||
@@ -23,6 +24,8 @@ set(SOURCES
|
||||
set(HEADERS
|
||||
include/package-manager.h
|
||||
include/pacman-manager.h
|
||||
include/plugin-manager.h
|
||||
include/provider-plugin.h
|
||||
include/main-window.h
|
||||
include/history-manager.h
|
||||
include/package-model.h
|
||||
@@ -42,3 +45,20 @@ enable_testing()
|
||||
add_executable(pacman-manager-test tests/pacman-manager.test.cpp src/pacman-manager.cpp)
|
||||
target_include_directories(pacman-manager-test PRIVATE include)
|
||||
add_test(NAME pacman-manager-test COMMAND pacman-manager-test)
|
||||
|
||||
# Optional example external provider plugin. Demonstrates the plugin mechanism:
|
||||
# the produced library is copied next to the executable and discovered at
|
||||
# runtime by PluginManager (see the Providers page).
|
||||
option(FWS_BUILD_EXAMPLE_PLUGIN "Build the example external provider plugin" OFF)
|
||||
if(FWS_BUILD_EXAMPLE_PLUGIN)
|
||||
add_library(example-provider-plugin MODULE plugins/example-provider/example-provider.cpp)
|
||||
target_include_directories(example-provider-plugin PRIVATE include)
|
||||
set_target_properties(example-provider-plugin PROPERTIES
|
||||
OUTPUT_NAME "sample-provider"
|
||||
CXX_STANDARD 17
|
||||
CXX_STANDARD_REQUIRED ON)
|
||||
add_custom_command(TARGET example-provider-plugin POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:PacManager>/plugins
|
||||
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:example-provider-plugin>
|
||||
$<TARGET_FILE_DIR:PacManager>/plugins/)
|
||||
endif()
|
||||
@@ -12,6 +12,10 @@ struct PackageInfo {
|
||||
std::string repository;
|
||||
std::string size;
|
||||
std::string installedSize;
|
||||
// Id of the provider that owns this package (e.g. "pacman", "flatpak").
|
||||
// Filled by the aggregating workers so operations can be routed to the
|
||||
// right manager without the user picking one.
|
||||
std::string provider;
|
||||
bool installed = false;
|
||||
bool updateAvailable = false;
|
||||
};
|
||||
@@ -47,6 +51,10 @@ public:
|
||||
|
||||
// Check if the package manager is available on the system
|
||||
virtual bool isAvailable() const = 0;
|
||||
|
||||
// Whether privileged operations need a password (e.g. sudo for pacman).
|
||||
// Providers that run per-user (e.g. flatpak) can keep the default.
|
||||
virtual bool requiresPassword() const { return false; }
|
||||
};
|
||||
|
||||
|
||||
@@ -22,6 +22,9 @@ public:
|
||||
return system("which pacman > /dev/null 2>&1") == 0;
|
||||
}
|
||||
|
||||
// pacman operations run through sudo and need the session password.
|
||||
bool requiresPassword() const override { return true; }
|
||||
|
||||
// Parse `pacman -Q` output (one "name version" line per package).
|
||||
// Exposed as static so the parsing can be unit-tested without invoking pacman.
|
||||
static std::vector<PackageInfo> parseInstalledOutput(const std::string& output);
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
|
||||
#include "provider-plugin.h"
|
||||
#include <QSettings>
|
||||
#include <QLibrary>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// Registry of package providers. It owns every IPackageManager (built-in and
|
||||
// external) and can load additional providers at runtime from shared-library
|
||||
// plugins dropped in the plugin directories (see PluginManager::scanDirectory).
|
||||
class PluginManager {
|
||||
public:
|
||||
struct ProviderEntry {
|
||||
ProviderInfo info;
|
||||
IPackageManager* manager = nullptr; // owned by the PluginManager
|
||||
QLibrary* library = nullptr; // owned by the PluginManager; null for built-ins
|
||||
};
|
||||
|
||||
PluginManager();
|
||||
~PluginManager();
|
||||
|
||||
// Register a provider compiled into the application (e.g. pacman, flatpak).
|
||||
// Takes ownership of the manager.
|
||||
void registerBuiltin(ProviderInfo info, IPackageManager* manager);
|
||||
|
||||
// Scan the plugin directories for external provider plugins and load them.
|
||||
void discoverExternal();
|
||||
|
||||
// Drop external plugins and rescan their directories; built-in providers
|
||||
// are kept (their instances stay valid).
|
||||
void reload();
|
||||
|
||||
// Re-run isAvailable() on every known provider (e.g. after the app
|
||||
// installed a missing tool) so the Providers page reflects reality.
|
||||
void refreshAvailability();
|
||||
|
||||
// All known providers, in registration order.
|
||||
const std::vector<ProviderEntry>& providers() const;
|
||||
|
||||
// Providers that are enabled (in settings) and whose tool is on the system.
|
||||
std::vector<const ProviderEntry*> availableProviders() const;
|
||||
|
||||
// Same as availableProviders() but as plain manager pointers, convenient
|
||||
// for running aggregated operations across every provider.
|
||||
std::vector<IPackageManager*> availableManagers() const;
|
||||
|
||||
// A single available+enabled provider matching id, or nullptr.
|
||||
const ProviderEntry* findAvailable(const std::string& id) const;
|
||||
|
||||
bool isEnabled(const std::string& id) const;
|
||||
void setEnabled(const std::string& id, bool enabled);
|
||||
|
||||
// Manager for a known provider id (enabled or not), or nullptr.
|
||||
IPackageManager* managerFor(const std::string& id) const;
|
||||
|
||||
private:
|
||||
bool loadPluginFile(const QString& filePath);
|
||||
void scanDirectory(const QString& dir);
|
||||
void unloadExternal();
|
||||
|
||||
std::vector<ProviderEntry> m_providers;
|
||||
QSettings m_settings;
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include "package-manager.h"
|
||||
#include <string>
|
||||
|
||||
// Metadata describing a provider, whether it is compiled into the application
|
||||
// (pacman, flatpak) or loaded at runtime from an external plugin.
|
||||
struct ProviderInfo {
|
||||
std::string id; // unique identifier, e.g. "pacman", "flatpak"
|
||||
std::string name; // display name, e.g. "Flatpak"
|
||||
std::string version; // plugin version
|
||||
std::string description; // what this provider manages
|
||||
std::string author; // who wrote the provider
|
||||
bool builtin = false; // true when compiled into the application
|
||||
std::string path; // filesystem path (external plugins only)
|
||||
bool available = false; // whether the backing tool is present on the system
|
||||
// OS package name providing the backing tool (e.g. "flatpak"). Empty when
|
||||
// the provider cannot be installed from the application.
|
||||
std::string installHint;
|
||||
};
|
||||
|
||||
// ---- External plugin contract (C ABI) ----
|
||||
//
|
||||
// An external provider plugin is a shared library (lib*.so / *.dll) dropped in
|
||||
// a plugin directory (see PluginManager). It must export the symbols below so
|
||||
// the host can query metadata and instantiate the provider. The convenience
|
||||
// macro FWS_PROVIDER_PLUGIN generates them from a class implementing
|
||||
// IPackageManager:
|
||||
//
|
||||
// #include "provider-plugin.h"
|
||||
// #include "my-manager.h"
|
||||
// FWS_PROVIDER_PLUGIN(MyManager, "myprov", "My Provider", "1.0.0",
|
||||
// "Manages my stuff", "Jane Doe");
|
||||
//
|
||||
// Plugins are compiled with the same toolchain/headers as the host, exactly
|
||||
// like Qt plugins, so the IPackageManager vtable and PackageInfo layout match.
|
||||
extern "C" {
|
||||
const char* fws_plugin_id();
|
||||
const char* fws_plugin_name();
|
||||
const char* fws_plugin_version();
|
||||
const char* fws_plugin_description();
|
||||
const char* fws_plugin_author();
|
||||
IPackageManager* fws_create_provider();
|
||||
// Optional: OS package name that provides the backing tool, so the app can
|
||||
// offer to install it when unavailable. May be omitted by a plugin.
|
||||
const char* fws_plugin_install_hint();
|
||||
}
|
||||
|
||||
#define FWS_PROVIDER_PLUGIN(PluginClass, id_, name_, version_, desc_, author_) \
|
||||
extern "C" const char* fws_plugin_id() { return id_; } \
|
||||
extern "C" const char* fws_plugin_name() { return name_; } \
|
||||
extern "C" const char* fws_plugin_version() { return version_; } \
|
||||
extern "C" const char* fws_plugin_description() { return desc_; } \
|
||||
extern "C" const char* fws_plugin_author() { return author_; } \
|
||||
extern "C" IPackageManager* fws_create_provider() { return new PluginClass(); }
|
||||
@@ -0,0 +1,52 @@
|
||||
// Example external provider plugin — a template for adding new package
|
||||
// providers without touching the application code.
|
||||
//
|
||||
// Build it as a shared library and drop it in a plugin directory
|
||||
// (next to the PacManager binary in ./plugins/ or in
|
||||
// ~/.local/share/PacManager/plugins/); the app discovers it at launch and on
|
||||
// the Providers page via "Recharger".
|
||||
//
|
||||
// A plugin only needs to:
|
||||
// 1. implement IPackageManager (see include/package-manager.h)
|
||||
// 2. export the C ABI symbols via the FWS_PROVIDER_PLUGIN macro
|
||||
//
|
||||
// To build with CMake from the project root:
|
||||
// cmake -B build -DFWS_BUILD_EXAMPLE_PLUGIN=ON && cmake --build build
|
||||
|
||||
#include "provider-plugin.h"
|
||||
|
||||
// A trivial provider that reports itself as always available but manages
|
||||
// nothing — replace the stubs with real commands (apt, dnf, zypper, ...).
|
||||
class SampleProvider : public IPackageManager {
|
||||
public:
|
||||
std::string getName() const override { return "sample"; }
|
||||
std::string getVersion() const override { return "0.1.0"; }
|
||||
std::string getDescription() const override {
|
||||
return "Exemple de plugin fournisseur (modèle à copier)";
|
||||
}
|
||||
|
||||
bool canSearch() const override { return false; }
|
||||
bool canInstall() const override { return false; }
|
||||
bool canRemove() const override { return false; }
|
||||
bool canUpdate() const override { return false; }
|
||||
bool canListInstalled() const override { return false; }
|
||||
|
||||
bool isAvailable() const override { return true; }
|
||||
|
||||
std::vector<PackageInfo> search(const std::string&) override { return {}; }
|
||||
bool install(const std::string&, std::function<void(const std::string&)>) override {
|
||||
return false;
|
||||
}
|
||||
bool remove(const std::string&, std::function<void(const std::string&)>) override {
|
||||
return false;
|
||||
}
|
||||
bool update(const std::string&, std::function<void(const std::string&)>) override {
|
||||
return false;
|
||||
}
|
||||
std::vector<PackageInfo> listInstalled() override { return {}; }
|
||||
std::vector<PackageInfo> listUpdates() override { return {}; }
|
||||
bool refresh(std::function<void(const std::string&)>) override { return false; }
|
||||
};
|
||||
|
||||
FWS_PROVIDER_PLUGIN(SampleProvider, "sample", "Sample Provider", "0.1.0",
|
||||
"Exemple de plugin fournisseur (modèle à copier)", "FWS");
|
||||
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user