refactor: replace plugin system with native pacman manager
Remove the dynamic plugin loader (pacman/apt/pkg/universal) and drive the UI directly through PacmanManager. Rename sources and headers to kebab-case to match the project's file naming convention. Generated with Codebuff 🤖 Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
1 parent
4a773c63ce
commit
5054f04c54
23 files changed
+276
-1735
No files matched your search
@@ -1,134 +0,0 @@
|
||||
#include "PluginManager.h"
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
PluginManager::PluginManager() {
|
||||
// Default search paths
|
||||
#ifdef _WIN32
|
||||
char* appdata = getenv("APPDATA");
|
||||
if (appdata) {
|
||||
m_searchPaths.push_back(std::string(appdata) + "\\PacManager\\plugins");
|
||||
}
|
||||
m_searchPaths.push_back(".\\plugins");
|
||||
#else
|
||||
char* home = getenv("HOME");
|
||||
if (home) {
|
||||
m_searchPaths.push_back(std::string(home) + "/.local/lib/PacManager/plugins");
|
||||
}
|
||||
m_searchPaths.push_back("/usr/lib/PacManager/plugins");
|
||||
m_searchPaths.push_back("/usr/local/lib/PacManager/plugins");
|
||||
#endif
|
||||
m_searchPaths.push_back("./plugins");
|
||||
|
||||
// Load from all search paths
|
||||
for (const auto& path : m_searchPaths) {
|
||||
if (fs::exists(path) && fs::is_directory(path)) {
|
||||
loadPluginsFromDirectory(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PluginManager::~PluginManager() {
|
||||
unloadAll();
|
||||
}
|
||||
|
||||
void PluginManager::loadPluginsFromDirectory(const std::string& directory) {
|
||||
try {
|
||||
for (const auto& entry : fs::directory_iterator(directory)) {
|
||||
if (entry.is_regular_file()) {
|
||||
auto ext = entry.path().extension().string();
|
||||
#ifdef _WIN32
|
||||
if (ext == ".dll") {
|
||||
#else
|
||||
if (ext == ".so" || ext == ".dylib") {
|
||||
#endif
|
||||
loadPlugin(entry.path().string());
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (const fs::filesystem_error& e) {
|
||||
std::cerr << "Error scanning plugin directory: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
bool PluginManager::loadPlugin(const std::string& path) {
|
||||
dynlib_handle handle = dynlib_open(path.c_str());
|
||||
if (!handle) {
|
||||
std::cerr << "Failed to load plugin: " << path << " - " << dynlib_error() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
using CreateFunc = IPackageManager* (*)();
|
||||
using DestroyFunc = void (*)(IPackageManager*);
|
||||
|
||||
auto createFunc = reinterpret_cast<CreateFunc>(dynlib_sym(handle, "createPlugin"));
|
||||
auto destroyFunc = reinterpret_cast<DestroyFunc>(dynlib_sym(handle, "destroyPlugin"));
|
||||
|
||||
if (!createFunc || !destroyFunc) {
|
||||
std::cerr << "Invalid plugin: " << path << " (missing entry points)" << std::endl;
|
||||
dynlib_close(handle);
|
||||
return false;
|
||||
}
|
||||
|
||||
IPackageManager* instance = createFunc();
|
||||
if (!instance) {
|
||||
std::cerr << "Plugin returned null: " << path << std::endl;
|
||||
dynlib_close(handle);
|
||||
return false;
|
||||
}
|
||||
|
||||
PluginInfo info;
|
||||
info.path = path;
|
||||
info.name = instance->getName();
|
||||
info.instance = instance;
|
||||
info.handle = handle;
|
||||
info.loaded = true;
|
||||
|
||||
m_plugins.push_back(info);
|
||||
std::cout << "Loaded plugin: " << info.name << " from " << path << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
void PluginManager::unloadAll() {
|
||||
for (auto& plugin : m_plugins) {
|
||||
if (plugin.instance && plugin.handle) {
|
||||
using DestroyFunc = void (*)(IPackageManager*);
|
||||
auto destroyFunc = reinterpret_cast<DestroyFunc>(dynlib_sym(plugin.handle, "destroyPlugin"));
|
||||
if (destroyFunc) {
|
||||
destroyFunc(plugin.instance);
|
||||
}
|
||||
dynlib_close(plugin.handle);
|
||||
plugin.instance = nullptr;
|
||||
plugin.handle = nullptr;
|
||||
plugin.loaded = false;
|
||||
}
|
||||
}
|
||||
m_plugins.clear();
|
||||
}
|
||||
|
||||
const std::vector<PluginInfo>& PluginManager::getPlugins() const {
|
||||
return m_plugins;
|
||||
}
|
||||
|
||||
IPackageManager* PluginManager::getPlugin(size_t index) const {
|
||||
if (index < m_plugins.size() && m_plugins[index].loaded) {
|
||||
return m_plugins[index].instance;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
size_t PluginManager::getPluginCount() const {
|
||||
return m_plugins.size();
|
||||
}
|
||||
|
||||
IPackageManager* PluginManager::findPlugin(const std::string& name) const {
|
||||
for (const auto& plugin : m_plugins) {
|
||||
if (plugin.name == name && plugin.loaded) {
|
||||
return plugin.instance;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "HistoryManager.h"
|
||||
#include "history-manager.h"
|
||||
#include <filesystem>
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "PackageModel.h"
|
||||
#include "package-model.h"
|
||||
|
||||
PackageModel::PackageModel(QObject* parent) : QAbstractListModel(parent) {}
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
#include "pacman-manager.h"
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
// Helper to run shell commands and capture output
|
||||
static std::string exec(const char* cmd) {
|
||||
std::array<char, 256> buffer;
|
||||
std::string result;
|
||||
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
|
||||
if (!pipe) throw std::runtime_error("popen() failed");
|
||||
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
|
||||
result += buffer.data();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> PacmanManager::search(const std::string& query) {
|
||||
std::vector<PackageInfo> results;
|
||||
|
||||
std::string cmd = "pacman -Ss " + query + " 2>&1";
|
||||
std::string output = exec(cmd.c_str());
|
||||
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
if (line.empty()) continue;
|
||||
|
||||
// Repository line: repo/name version
|
||||
if (line[0] != ' ' && line[0] != '\t') {
|
||||
PackageInfo pkg;
|
||||
size_t slashPos = line.find('/');
|
||||
size_t spacePos = line.find(' ', slashPos);
|
||||
|
||||
if (slashPos != std::string::npos) {
|
||||
pkg.repository = line.substr(0, slashPos);
|
||||
pkg.name = line.substr(slashPos + 1, spacePos - slashPos - 1);
|
||||
|
||||
if (spacePos != std::string::npos) {
|
||||
size_t nextSpace = line.find(' ', spacePos + 1);
|
||||
pkg.version = line.substr(spacePos + 1, nextSpace - spacePos - 1);
|
||||
}
|
||||
}
|
||||
|
||||
pkg.installed = isPackageInstalled(pkg.name);
|
||||
results.push_back(pkg);
|
||||
}
|
||||
// Description line (starts with spaces)
|
||||
else if (!results.empty()) {
|
||||
std::string desc = line;
|
||||
// Trim leading spaces
|
||||
size_t start = desc.find_first_not_of(" \t");
|
||||
if (start != std::string::npos) {
|
||||
desc = desc.substr(start);
|
||||
}
|
||||
results.back().description = desc;
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
bool PacmanManager::install(const std::string& packageName,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
if (progressCallback) progressCallback("Installation de " + packageName + "...");
|
||||
|
||||
std::string cmd = "pkexec pacman -S --noconfirm " + packageName + " 2>&1";
|
||||
std::string output = exec(cmd.c_str());
|
||||
|
||||
if (progressCallback) {
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty()) progressCallback(line);
|
||||
}
|
||||
}
|
||||
|
||||
return isPackageInstalled(packageName);
|
||||
}
|
||||
|
||||
bool PacmanManager::remove(const std::string& packageName,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
if (progressCallback) progressCallback("Suppression de " + packageName + "...");
|
||||
|
||||
std::string cmd = "pkexec pacman -R --noconfirm " + packageName + " 2>&1";
|
||||
std::string output = exec(cmd.c_str());
|
||||
|
||||
if (progressCallback) {
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty()) progressCallback(line);
|
||||
}
|
||||
}
|
||||
|
||||
return !isPackageInstalled(packageName);
|
||||
}
|
||||
|
||||
bool PacmanManager::update(const std::string& packageName,
|
||||
std::function<void(const std::string&)> progressCallback) {
|
||||
if (packageName.empty()) {
|
||||
if (progressCallback) progressCallback("Mise à jour de tous les paquets...");
|
||||
std::string cmd = "pkexec pacman -Syu --noconfirm 2>&1";
|
||||
exec(cmd.c_str());
|
||||
return true;
|
||||
} else {
|
||||
if (progressCallback) progressCallback("Mise à jour de " + packageName + "...");
|
||||
std::string cmd = "pkexec pacman -S --noconfirm " + packageName + " 2>&1";
|
||||
exec(cmd.c_str());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> PacmanManager::listInstalled() {
|
||||
return parseInstalledOutput(exec("pacman -Q 2>&1"));
|
||||
}
|
||||
|
||||
// Parse `pacman -Qu` output (one "name version" line per outdated package).
|
||||
// Exposed as static so the parsing can be unit-tested without invoking pacman.
|
||||
static std::vector<PackageInfo> parseUpdatesOutput(const std::string& output) {
|
||||
std::vector<PackageInfo> results;
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
if (line.empty()) continue;
|
||||
|
||||
PackageInfo pkg;
|
||||
size_t spacePos = line.find(' ');
|
||||
if (spacePos == std::string::npos) continue;
|
||||
|
||||
pkg.name = line.substr(0, spacePos);
|
||||
pkg.version = line.substr(spacePos + 1);
|
||||
pkg.installed = true;
|
||||
pkg.updateAvailable = true;
|
||||
results.push_back(pkg);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> PacmanManager::listUpdates() {
|
||||
return parseUpdatesOutput(exec("pacman -Qu 2>&1"));
|
||||
}
|
||||
|
||||
bool PacmanManager::refresh(std::function<void(const std::string&)> progressCallback) {
|
||||
if (progressCallback) progressCallback("Rafraîchissement des bases de données...");
|
||||
|
||||
std::string cmd = "pkexec pacman -Sy --noconfirm 2>&1";
|
||||
std::string output = exec(cmd.c_str());
|
||||
|
||||
if (progressCallback) {
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty()) progressCallback(line);
|
||||
}
|
||||
}
|
||||
|
||||
// `pacman -Sy` has no easy success code; treat a completed run as success
|
||||
// unless pacman itself reported an explicit error.
|
||||
return output.find("error:") == std::string::npos;
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> PacmanManager::parseInstalledOutput(const std::string& output) {
|
||||
std::vector<PackageInfo> results;
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
if (line.empty()) continue;
|
||||
|
||||
PackageInfo pkg;
|
||||
// `pacman -Q` prints "name version" separated by a single space;
|
||||
// splitting on the first space keeps hyphens in names (e.g. python-pip) intact.
|
||||
size_t spacePos = line.find(' ');
|
||||
if (spacePos == std::string::npos) continue;
|
||||
|
||||
pkg.name = line.substr(0, spacePos);
|
||||
pkg.version = line.substr(spacePos + 1);
|
||||
pkg.installed = true;
|
||||
results.push_back(pkg);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
bool PacmanManager::isPackageInstalled(const std::string& name) {
|
||||
std::string cmd = "pacman -Qi " + name + " > /dev/null 2>&1";
|
||||
return system(cmd.c_str()) == 0;
|
||||
}
|
||||
Reference in new issue
Block a user