Files
FWS-PacManage/plugins/flatpak-provider/flatpak-manager.h
T
BlackAngelTV eb39905bf5 feat(flatpak): let the user choose which installation to act on
The same application can be present twice — in the user installation
and system-wide, or from two remotes or two branches — and flatpak then
refuses to act on the application id alone: "Multiple installed refs
match 'com.hoptodesk.HopToDesk'". Removing either copy was impossible
from the app.

A row now stands for the application and carries the copies it is made
of, so the details panel lists them and Remove opens one checkbox per
copy; the provider gets back the exact target it listed, and naming a
ref plus its installation is what makes the call unambiguous. Update
gains the same resolution, since it hit the very same refusal, and
removing one copy of two no longer takes the other one's launcher entry
away with it.

The listing that feeds it now asks flatpak for the ref, the branch and
the installation, and reads those columns by position: a branch
installed from a subdirectory has no version, and dropping that empty
column shifted every field after it.
2026-09-27 21:33:41 +02:00

109 lines
5.3 KiB
C++

#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;
}
// Last lines of the failed command output, shown to the user on failure.
std::string lastError() const override { return m_lastError; }
// 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<PackageInfo> parseSearchOutput(const std::string& output);
// Parse `flatpak list --columns=application,version,origin` output
// (tab-separated fields per installed application).
static std::vector<PackageInfo> 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<PackageInfo> parseUpdatesOutput(const std::string& output);
// The Flatpak file formats the app accepts from the window (drag & drop) and
// from the file manager ("Ouvrir avec PacManager").
enum class FileKind { None, Ref, Repo, Bundle };
// Classify by extension, ignoring case and any URL query/fragment, so a
// local path and a flathub URL are both recognised. Exposed as static so the
// routing can be unit-tested without flatpak.
static FileKind classifyFile(const std::string& path);
// Command that installs a ref or bundle file, or registers a repository
// (.flatpakrepo). Exposed so the exact command line is testable without
// running flatpak.
static std::string installFileCommand(FileKind kind, const std::string& path,
bool userScope = false);
// Commands that act on exactly one installation of an application. Both take
// either an application id — flatpak then refuses it when several copies
// exist — or a target ("user:com.example.App/x86_64/stable") naming one copy,
// as found in PackageInfo::instances. Exposed so the exact command line is
// testable without running flatpak.
static std::string uninstallCommand(const std::string& packageName);
static std::string updateCommand(const std::string& packageName);
// Name a `.flatpakrepo` is registered under: its base name without the
// extension (flathub.flatpakrepo -> flathub), which is what the user then
// types in `flatpak install <name> <app>`.
static std::string repoNamespace(const std::string& path);
std::vector<PackageInfo> search(const std::string& query) override;
bool install(const std::string& packageName,
ProgressCallback progressCallback) override;
bool remove(const std::string& packageName,
ProgressCallback progressCallback) override;
bool update(const std::string& packageName = "",
ProgressCallback progressCallback = nullptr) override;
std::vector<PackageInfo> listInstalled() override;
std::vector<PackageInfo> listUpdates() override;
bool refresh(ProgressCallback progressCallback = nullptr) override;
// Cleanup: flatpak knows which runtimes no installed application uses any
// more, so it can reclaim them from the maintenance page.
bool canCleanCache() const override { return true; }
bool cleanCache(ProgressCallback progressCallback = nullptr) override;
// Install from a Flatpak file: a `.flatpakref` (an application reference
// served by a remote), a `.flatpakrepo` (a remote definition) or a `.flatpak`
// bundle. Unlike a name-based install these come from the user's disk or a
// URL, so flatpak resolves the remote itself.
bool canInstallFile(const std::string& path) const override;
bool installFile(const std::string& path, ProgressCallback progressCallback) override;
// Make an app appear in the desktop's application launcher (wofi, GNOME,
// KDE, ...) regardless of XDG_DATA_DIRS, like a pacman install would: link
// its .desktop entries and icons from flatpak's exports into the user's
// standard XDG directories (~/.local/share/{applications,icons}). Exposed
// as static so the linking can be unit/integration tested without flatpak.
static void integrateDesktopEntries(const std::string& appId);
static void uninstallDesktopEntries(const std::string& appId);
// Re-link every installed app so launchers pick up apps whose integration
// is missing (e.g. installed before this feature existed).
void integrateAllInstalled();
private:
bool isInstalled(const std::string& appId);
std::string m_lastError;
};