#include "IPackageManager.h" #include #include #include #include #include #include #include // Helper to run shell commands and capture output static std::string exec(const char* cmd) { std::array buffer; std::string result; std::unique_ptr 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; } class PacmanPlugin : public IPackageManager { public: std::string getName() const override { return "pacman"; } std::string getVersion() const override { return "1.0.0"; } std::string getDescription() const override { return "Gestionnaire de paquets natif d'Arch Linux"; } bool canSearch() const override { return true; } bool canInstall() const override { return true; } bool canRemove() const override { return true; } bool canUpdate() const override { return true; } bool canListInstalled() const override { return true; } bool isAvailable() const override { return system("which pacman > /dev/null 2>&1") == 0; } std::vector search(const std::string& query) override { std::vector results; std::string cmd = "pacman -Ss " + query + " 2>&1"; std::string output = exec(cmd.c_str()); std::istringstream iss(output); std::string line; std::string currentRepo; 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); currentRepo = pkg.name; } // 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 install(const std::string& packageName, std::function progressCallback) override { 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 remove(const std::string& packageName, std::function progressCallback) override { 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 update(const std::string& packageName = "", std::function progressCallback = nullptr) override { 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 listInstalled() override { std::vector results; std::string output = exec("pacman -Q 2>&1"); std::istringstream iss(output); std::string line; while (std::getline(iss, line)) { if (line.empty()) continue; PackageInfo pkg; size_t spacePos = line.find(' '); size_t dashPos = line.find('-'); // Handle package names with hyphens (version starts after last pair of hyphens) size_t lastDash = line.rfind('-'); size_t secondLastDash = line.rfind('-', lastDash - 1); if (secondLastDash != std::string::npos && lastDash != std::string::npos) { pkg.name = line.substr(0, secondLastDash); pkg.version = line.substr(secondLastDash + 1); } else if (spacePos != std::string::npos) { pkg.name = line.substr(0, spacePos); pkg.version = line.substr(spacePos + 1); } pkg.installed = true; results.push_back(pkg); } return results; } private: bool isPackageInstalled(const std::string& name) { std::string cmd = "pacman -Qi " + name + " > /dev/null 2>&1"; return system(cmd.c_str()) == 0; } }; extern "C" { IPackageManager* createPlugin() { return new PacmanPlugin(); } void destroyPlugin(IPackageManager* plugin) { delete plugin; } }