diff --git a/CMakeLists.txt b/CMakeLists.txt index abc3d33..6fb5ade 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ set(SOURCES src/main.cpp src/main-window.cpp src/pacman-manager.cpp + src/flatpak-manager.cpp src/plugin-manager.cpp src/history-manager.cpp src/package-model.cpp @@ -24,6 +25,7 @@ set(SOURCES set(HEADERS include/package-manager.h include/pacman-manager.h + include/flatpak-manager.h include/plugin-manager.h include/provider-plugin.h include/main-window.h diff --git a/include/flatpak-manager.h b/include/flatpak-manager.h new file mode 100644 index 0000000..8e467d6 --- /dev/null +++ b/include/flatpak-manager.h @@ -0,0 +1,51 @@ +#pragma once + +#include "package-manager.h" + +// Flatpak provider: manages flatpak applications through the `flatpak` CLI. +// Unlike pacman it runs per-user (no sudo), so requiresPassword() is false. +class FlatpakManager : public IPackageManager { +public: + std::string getName() const override { return "flatpak"; } + std::string getVersion() const override { return "1.0.0"; } + std::string getDescription() const override { + return "Gestionnaire d'applications Flatpak"; + } + + 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 flatpak > /dev/null 2>&1") == 0; + } + + // Parse `flatpak search` output (one whitespace-aligned row per app: + // "Name Description Application ID Version Branch Remotes"). + // Exposed as static so the parsing can be unit-tested without flatpak. + static std::vector parseSearchOutput(const std::string& output); + + // Parse `flatpak list --columns=application,version,origin` output + // (tab-separated fields per installed application). + static std::vector parseInstalledOutput(const std::string& output); + + // Parse `flatpak remote-ls --updates --columns=application,version` output + // (tab-separated fields per application with a pending update). + static std::vector parseUpdatesOutput(const std::string& output); + + std::vector search(const std::string& query) override; + bool install(const std::string& packageName, + std::function progressCallback) override; + bool remove(const std::string& packageName, + std::function progressCallback) override; + bool update(const std::string& packageName = "", + std::function progressCallback = nullptr) override; + std::vector listInstalled() override; + std::vector listUpdates() override; + bool refresh(std::function progressCallback = nullptr) override; + +private: + bool isInstalled(const std::string& appId); +}; diff --git a/src/flatpak-manager.cpp b/src/flatpak-manager.cpp new file mode 100644 index 0000000..30f8693 --- /dev/null +++ b/src/flatpak-manager.cpp @@ -0,0 +1,230 @@ +#include "flatpak-manager.h" +#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; +} + +// Stream the lines of a command's output to the progress callback, if any. +static void streamOutput(const std::string& output, + std::function progressCallback) { + if (!progressCallback) return; + std::istringstream iss(output); + std::string line; + while (std::getline(iss, line)) { + if (!line.empty()) progressCallback(line); + } +} + +// A flatpak application ID is a dotted identifier with at least two dots whose +// segments start with an alphanumeric character (e.g. org.mozilla.firefox). +// This deliberately rejects version strings such as "128.0.2". +static bool isAppId(const std::string& token) { + int dots = 0; + bool first = true; + for (size_t i = 0; i < token.size(); ++i) { + char c = token[i]; + if (c == '.') { + ++dots; + first = true; // next segment must start with an alnum again + } else if (std::isalnum(static_cast(c)) || c == '-' || c == '_') { + if (first && !std::isalnum(static_cast(c))) return false; + first = false; + } else { + return false; + } + } + return dots >= 2 && !first; +} + +// Split a flatpak --columns line. flatpak emits tab-separated fields with that +// flag; fall back to whitespace splitting when tabs are absent. +static std::vector splitColumns(const std::string& line) { + std::vector fields; + std::istringstream ss(line); + std::string field; + if (line.find('\t') != std::string::npos) { + while (std::getline(ss, field, '\t')) { + if (!field.empty()) fields.push_back(field); + } + } else { + while (ss >> field) fields.push_back(field); + } + return fields; +} + +// `flatpak search` prints one aligned row per app: +// Name Description Application ID Version Branch Remotes +// The description may contain spaces, so the application ID is located by +// shape (dotted identifier) and everything between Name and it is joined back. +std::vector FlatpakManager::parseSearchOutput(const std::string& output) { + std::vector results; + std::istringstream iss(output); + std::string line; + + bool firstLine = true; + while (std::getline(iss, line)) { + if (firstLine) { + firstLine = false; + continue; // Skip the column header. + } + if (line.empty()) continue; + + std::vector tokens = splitColumns(line); + if (tokens.empty()) continue; + + int appIdx = -1; + for (int i = 1; i < static_cast(tokens.size()); ++i) { + if (isAppId(tokens[i])) { + appIdx = i; + break; + } + } + if (appIdx == -1) continue; // Not an application row. + + PackageInfo pkg; + // Use the application ID as the package name so install/remove/update + // work directly with the CLI; the human-readable name is kept at the + // front of the description (e.g. "Firefox — Fast and privacy-aware"). + pkg.name = tokens[appIdx]; + pkg.version = (appIdx + 1 < static_cast(tokens.size())) ? tokens[appIdx + 1] : ""; + pkg.repository = tokens.back(); + + for (int i = 1; i < appIdx; ++i) { + if (!pkg.description.empty()) pkg.description += " "; + pkg.description += tokens[i]; + } + if (!tokens[0].empty() && tokens[0] != pkg.name) { + pkg.description = tokens[0] + " — " + pkg.description; + } + results.push_back(pkg); + } + + return results; +} + +std::vector FlatpakManager::parseInstalledOutput(const std::string& output) { + std::vector results; + std::istringstream iss(output); + std::string line; + + while (std::getline(iss, line)) { + if (line.empty()) continue; + + std::vector fields = splitColumns(line); + if (fields.empty()) continue; + + PackageInfo pkg; + pkg.name = fields[0]; + if (fields.size() > 1) pkg.version = fields[1]; + if (fields.size() > 2) pkg.repository = fields[2]; + pkg.installed = true; + results.push_back(pkg); + } + + return results; +} + +std::vector FlatpakManager::parseUpdatesOutput(const std::string& output) { + std::vector results; + std::istringstream iss(output); + std::string line; + + while (std::getline(iss, line)) { + if (line.empty()) continue; + + std::vector fields = splitColumns(line); + if (fields.empty()) continue; + + PackageInfo pkg; + pkg.name = fields[0]; + if (fields.size() > 1) pkg.version = fields[1]; + pkg.installed = true; + pkg.updateAvailable = true; + results.push_back(pkg); + } + + return results; +} + +std::vector FlatpakManager::search(const std::string& query) { + std::string cmd = "flatpak search " + query + " 2>&1"; + return parseSearchOutput(exec(cmd.c_str())); +} + +std::vector FlatpakManager::listInstalled() { + return parseInstalledOutput( + exec("flatpak list --app --columns=application,version,origin 2>&1")); +} + +std::vector FlatpakManager::listUpdates() { + return parseUpdatesOutput( + exec("flatpak remote-ls --updates --columns=application,version 2>&1")); +} + +bool FlatpakManager::isInstalled(const std::string& appId) { + std::string cmd = "flatpak info " + appId + " > /dev/null 2>&1"; + return system(cmd.c_str()) == 0; +} + +bool FlatpakManager::install(const std::string& packageName, + std::function progressCallback) { + if (progressCallback) progressCallback("Installation de " + packageName + "..."); + + std::string output = exec( + ("flatpak install --noninteractive --assumeyes " + packageName + " 2>&1").c_str()); + streamOutput(output, progressCallback); + + // Any "error:" line from flatpak means the install failed. + return output.find("error:") == std::string::npos && isInstalled(packageName); +} + +bool FlatpakManager::remove(const std::string& packageName, + std::function progressCallback) { + if (progressCallback) progressCallback("Suppression de " + packageName + "..."); + + std::string output = exec( + ("flatpak uninstall --noninteractive --assumeyes " + packageName + " 2>&1").c_str()); + streamOutput(output, progressCallback); + + return output.find("error:") == std::string::npos && !isInstalled(packageName); +} + +bool FlatpakManager::update(const std::string& packageName, + std::function progressCallback) { + std::string output; + if (packageName.empty()) { + if (progressCallback) progressCallback("Mise à jour de toutes les applications..."); + output = exec("flatpak update --noninteractive --assumeyes 2>&1"); + } else { + if (progressCallback) progressCallback("Mise à jour de " + packageName + "..."); + output = exec( + ("flatpak update --noninteractive --assumeyes " + packageName + " 2>&1").c_str()); + } + streamOutput(output, progressCallback); + + return output.find("error:") == std::string::npos; +} + +bool FlatpakManager::refresh(std::function progressCallback) { + if (progressCallback) progressCallback("Rafraîchissement des métadonnées Flatpak..."); + + // Re-download the appstream metadata of every remote so search and + // update detection reflect the latest published versions. + std::string output = exec("flatpak update --appstream 2>&1"); + streamOutput(output, progressCallback); + + return output.find("error:") == std::string::npos; +}