Files
FWS-PacManage/providers/flatpak-provider/flatpak-manager.h
T
BlackAngelTV 55718064a6 feat(files): confirm dropped files and refuse the ones already installed
A drop — or "Ouvrir avec PacManager" — answered nothing: a Flatpak
install walked off into the background and a plugin package landed
without a sound, so the gesture read as ignored. Both now open a
dialog saying what was taken and what it starts.

A file the machine already has is refused before anything runs, and
named. The application a file would install is read out of the file
itself (the [Flatpak Ref] section of a .flatpakref, the header of a
.flatpak bundle, the manifest of a .fwspacm) and compared with what
the provider has, so nothing is started to find out: Flatpak would
otherwise skip the install and report a success the user never got,
and a plugin package would silently replace the one in place.
2026-10-09 19:22:24 +02:00

125 lines
6.4 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);
// Application a file would install. A `.flatpakref` spells it out in its
// `[Flatpak Ref]` section; a `.flatpak` bundle carries the exact ref it
// installs in its own header. Both are read without running flatpak — the
// file is already on disk — so the answer is available before anything is
// started. Empty when the file names no application or cannot be read, in
// which case the caller simply installs it.
static std::string appIdFromRefFile(const std::string& path);
static std::string appIdFromBundle(const std::string& path);
// The application id inside a ref ("app/com.example.App/x86_64/stable" ->
// "com.example.App"). Empty when the string is not a ref.
static std::string appIdFromRef(const std::string& ref);
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;
std::string fileTargetName(const std::string& path) const override;
bool isPackageInstalled(const std::string& packageName) const 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:
// Whether that application is deployed, in any installation (flatpak info
// looks them all up). Exposed through isPackageInstalled().
bool isInstalled(const std::string& appId) const;
std::string m_lastError;
};