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.
This commit is contained in:
14 files changed
+559
-20
No files matched your search
@@ -95,6 +95,75 @@ static unsigned long long stagedBytes() {
|
||||
return total;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Naming and targeting one installation
|
||||
// ---------------------------------------------------------------------------
|
||||
// flatpak installs the same application id several times over — once in the
|
||||
// user installation, once system-wide, or from two remotes or branches — and
|
||||
// then refuses to act on the id alone: "Multiple installed refs match
|
||||
// 'com.example.App', unable to proceed in non-interactive mode". Naming one
|
||||
// copy therefore takes both its installation and its full ref.
|
||||
|
||||
// The string that names exactly one installation, "<installation>:<ref>". The
|
||||
// view never builds it and never reads it: it is what the provider put in
|
||||
// PackageInfo::instances when it listed the app, handed back unchanged when the
|
||||
// user picks the copies to act on.
|
||||
static std::string installationTarget(const std::string& installation,
|
||||
const std::string& ref) {
|
||||
return installation + ":" + ref;
|
||||
}
|
||||
|
||||
// "user:com.example.App/x86_64/stable" -> installation "user", ref the rest.
|
||||
static bool splitInstallationTarget(const std::string& target, std::string* installation,
|
||||
std::string* ref) {
|
||||
const std::size_t colon = target.find(':');
|
||||
if (colon == std::string::npos || colon == 0 || colon + 1 >= target.size()) return false;
|
||||
const std::string head = target.substr(0, colon);
|
||||
// A plain application id has no colon, and anything else is not a target.
|
||||
if (head != "user" && head != "system") return false;
|
||||
*installation = head;
|
||||
*ref = target.substr(colon + 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
// What the user reads for one copy: "utilisateur · flathub · stable". The
|
||||
// remote and the branch are the two things that tell two copies of the same id
|
||||
// apart, so they are what the label carries.
|
||||
static std::string instanceLabel(const std::string& installation, const std::string& origin,
|
||||
const std::string& branch) {
|
||||
std::string label = installation == "user" ? "utilisateur" : "système";
|
||||
if (!origin.empty() && origin != "-" && origin != "flathub") {
|
||||
label += " · " + origin;
|
||||
}
|
||||
if (!branch.empty() && branch != "master" && branch != "stable") {
|
||||
label += " · " + branch;
|
||||
}
|
||||
return label;
|
||||
}
|
||||
|
||||
// flatpak refuses an application id that names several copies, and the wording
|
||||
// depends on what makes it ambiguous: several installed refs, or several branches
|
||||
// of one ref. Both are the same situation here — the copies have to be listed
|
||||
// and named one by one.
|
||||
static bool isAmbiguous(const std::string& output) {
|
||||
return output.find("Multiple installed refs match") != std::string::npos
|
||||
|| output.find("Multiple branches available") != std::string::npos;
|
||||
}
|
||||
|
||||
// Every installation of `appId` in a listing, as targets the caller can hand
|
||||
// straight back. Empty when the id is not installed at all.
|
||||
static std::vector<std::string> targetsFor(const std::vector<PackageInfo>& installed,
|
||||
const std::string& appId) {
|
||||
std::vector<std::string> targets;
|
||||
for (const PackageInfo& pkg : installed) {
|
||||
if (pkg.name != appId) continue;
|
||||
for (const PackageInstance& instance : pkg.instances) {
|
||||
if (!instance.target.empty()) targets.push_back(instance.target);
|
||||
}
|
||||
}
|
||||
return targets;
|
||||
}
|
||||
|
||||
// "218 Mio", "1,4 Gio": the same units a French file manager would use.
|
||||
static std::string humanBytes(unsigned long long bytes) {
|
||||
static const char* units[] = {"o", "Kio", "Mio", "Gio", "To"};
|
||||
@@ -298,7 +367,8 @@ static std::string shellQuote(const std::string& value) {
|
||||
}
|
||||
|
||||
// Split a flatpak --columns line. flatpak emits tab-separated fields with that
|
||||
// flag; fall back to whitespace splitting when tabs are absent.
|
||||
// flag; fall back to whitespace splitting when tabs are absent. Empty fields are
|
||||
// dropped, which is what an aligned search row needs.
|
||||
static std::vector<std::string> splitColumns(const std::string& line) {
|
||||
std::vector<std::string> fields;
|
||||
std::istringstream ss(line);
|
||||
@@ -313,6 +383,20 @@ static std::vector<std::string> splitColumns(const std::string& line) {
|
||||
return fields;
|
||||
}
|
||||
|
||||
// Same split, but an empty column stays a column: a listing read by position
|
||||
// would otherwise shift every field after it. `flatpak list` prints an empty
|
||||
// version for a branch installed from a subdirectory, which is exactly where a
|
||||
// row carries the ref and the installation that tell its copies apart.
|
||||
static std::vector<std::string> splitColumnsByPosition(const std::string& line) {
|
||||
if (line.find('\t') == std::string::npos) return {line};
|
||||
|
||||
std::vector<std::string> fields;
|
||||
std::istringstream ss(line);
|
||||
std::string field;
|
||||
while (std::getline(ss, field, '\t')) 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
|
||||
@@ -371,7 +455,7 @@ std::vector<PackageInfo> FlatpakManager::parseInstalledOutput(const std::string&
|
||||
while (std::getline(iss, line)) {
|
||||
if (line.empty()) continue;
|
||||
|
||||
std::vector<std::string> fields = splitColumns(line);
|
||||
std::vector<std::string> fields = splitColumnsByPosition(line);
|
||||
if (fields.empty()) continue;
|
||||
|
||||
PackageInfo pkg;
|
||||
@@ -379,6 +463,22 @@ std::vector<PackageInfo> FlatpakManager::parseInstalledOutput(const std::string&
|
||||
if (fields.size() > 1) pkg.version = fields[1];
|
||||
if (fields.size() > 2) pkg.repository = fields[2];
|
||||
pkg.installed = true;
|
||||
|
||||
// The ref, branch and installation columns (the last three) say *which*
|
||||
// copy this row is: the same application can be installed in the user
|
||||
// installation and system-wide, or from two remotes or branches, and
|
||||
// flatpak then refuses to act on the application id alone.
|
||||
if (fields.size() >= 6) {
|
||||
const std::string& ref = fields[3];
|
||||
const std::string& branch = fields[4];
|
||||
const std::string& installation = fields[5];
|
||||
if (!ref.empty()) {
|
||||
PackageInstance instance;
|
||||
instance.target = installationTarget(installation, ref);
|
||||
instance.label = instanceLabel(installation, pkg.repository, branch);
|
||||
pkg.instances.push_back(instance);
|
||||
}
|
||||
}
|
||||
results.push_back(pkg);
|
||||
}
|
||||
|
||||
@@ -442,6 +542,30 @@ std::string FlatpakManager::installFileCommand(FileKind kind, const std::string&
|
||||
+ " 2>&1";
|
||||
}
|
||||
|
||||
std::string FlatpakManager::uninstallCommand(const std::string& packageName) {
|
||||
std::string installation;
|
||||
std::string ref;
|
||||
if (splitInstallationTarget(packageName, &installation, &ref)) {
|
||||
// Naming the ref is what makes it unambiguous; the scope is what picks
|
||||
// the copy, since the same ref can sit in both installations.
|
||||
const std::string scope = installation == "user" ? "--user " : "";
|
||||
return "flatpak uninstall --noninteractive --assumeyes " + scope + shellQuote(ref)
|
||||
+ " 2>&1";
|
||||
}
|
||||
return "flatpak uninstall --noninteractive --assumeyes " + packageName + " 2>&1";
|
||||
}
|
||||
|
||||
std::string FlatpakManager::updateCommand(const std::string& packageName) {
|
||||
std::string installation;
|
||||
std::string ref;
|
||||
if (splitInstallationTarget(packageName, &installation, &ref)) {
|
||||
const std::string scope = installation == "user" ? "--user " : "";
|
||||
return "flatpak update --noninteractive --assumeyes " + scope + shellQuote(ref)
|
||||
+ " 2>&1";
|
||||
}
|
||||
return "flatpak update --noninteractive --assumeyes " + packageName + " 2>&1";
|
||||
}
|
||||
|
||||
// Application ids mentioned in a command's output, e.g.
|
||||
// "Installing: org.gnome.Dictionary/x86_64/stable from flathub". A file install
|
||||
// only reveals the app id once flatpak resolved the file, and the launcher
|
||||
@@ -487,8 +611,12 @@ std::vector<PackageInfo> FlatpakManager::search(const std::string& query) {
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> FlatpakManager::listInstalled() {
|
||||
return parseInstalledOutput(
|
||||
exec("flatpak list --app --columns=application,version,origin 2>&1"));
|
||||
// ref, branch and installation are what tell two installations of the same
|
||||
// application id apart, so they are listed: without them the view could
|
||||
// neither show the copies nor choose between them.
|
||||
return parseInstalledOutput(exec("flatpak list --app "
|
||||
"--columns=application,version,origin,ref,branch,installation "
|
||||
"2>&1"));
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> FlatpakManager::listUpdates() {
|
||||
@@ -648,20 +776,56 @@ void FlatpakManager::integrateAllInstalled() {
|
||||
bool FlatpakManager::remove(const std::string& packageName,
|
||||
ProgressCallback progressCallback) {
|
||||
m_lastError.clear();
|
||||
if (progressCallback) progressCallback("Suppression de " + packageName + "...", -1);
|
||||
|
||||
std::string output = runFlatpak(
|
||||
"flatpak uninstall --noninteractive --assumeyes " + packageName + " 2>&1",
|
||||
progressCallback);
|
||||
std::string installation;
|
||||
std::string ref;
|
||||
const bool targeted = splitInstallationTarget(packageName, &installation, &ref);
|
||||
const std::string appId = targeted ? ref.substr(0, ref.find('/')) : packageName;
|
||||
|
||||
if (output.find("error:") != std::string::npos || isInstalled(packageName)) {
|
||||
m_lastError = lastOutputLines(output);
|
||||
return false;
|
||||
if (progressCallback) {
|
||||
progressCallback(
|
||||
targeted ? "Suppression de " + appId + " (" + installation + ")..."
|
||||
: "Suppression de " + packageName + "...",
|
||||
-1);
|
||||
}
|
||||
|
||||
// Clear the launcher links created at install time (see integrateDesktopEntries).
|
||||
uninstallDesktopEntries(packageName);
|
||||
return true;
|
||||
std::vector<std::string> targets;
|
||||
if (targeted) {
|
||||
targets.push_back(packageName);
|
||||
} else {
|
||||
// A row stands for the application, not for one of its copies, so the
|
||||
// application id is tried as it is: flatpak only refuses it when the
|
||||
// copies are several, and the copies are then the ones to remove.
|
||||
const std::string output = runFlatpak(uninstallCommand(packageName), progressCallback);
|
||||
if (!isAmbiguous(output)) {
|
||||
if (output.find("error:") != std::string::npos || isInstalled(appId)) {
|
||||
m_lastError = lastOutputLines(output);
|
||||
return false;
|
||||
}
|
||||
uninstallDesktopEntries(appId);
|
||||
return true;
|
||||
}
|
||||
targets = targetsFor(listInstalled(), appId);
|
||||
if (targets.empty()) {
|
||||
m_lastError = lastOutputLines(output);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool success = true;
|
||||
for (const std::string& target : targets) {
|
||||
const std::string output = runFlatpak(uninstallCommand(target), progressCallback);
|
||||
if (output.find("error:") != std::string::npos) {
|
||||
m_lastError = lastOutputLines(output);
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
|
||||
// The launcher links belong to the application as a whole: they go only once
|
||||
// no copy is left, or removing one of two copies would take the entry away
|
||||
// from the other.
|
||||
if (success && !isInstalled(appId)) uninstallDesktopEntries(appId);
|
||||
return success;
|
||||
}
|
||||
|
||||
bool FlatpakManager::update(const std::string& packageName,
|
||||
@@ -680,6 +844,27 @@ bool FlatpakManager::update(const std::string& packageName,
|
||||
}
|
||||
|
||||
std::string output = tryUpdate("");
|
||||
|
||||
// Several installations of the same application: the id alone is refused, so
|
||||
// every copy is updated with its own scope. No polkit fallback here — it
|
||||
// would quietly update a different copy than the one that failed.
|
||||
if (isAmbiguous(output)) {
|
||||
const std::vector<std::string> targets = targetsFor(listInstalled(), packageName);
|
||||
if (targets.empty()) {
|
||||
m_lastError = lastOutputLines(output);
|
||||
return false;
|
||||
}
|
||||
bool success = true;
|
||||
for (const std::string& target : targets) {
|
||||
const std::string copy = runFlatpak(updateCommand(target), progressCallback);
|
||||
if (copy.find("error:") != std::string::npos) {
|
||||
m_lastError = lastOutputLines(copy);
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
// System-wide updates need polkit: fall back to the per-user installation.
|
||||
if (output.find("error:") != std::string::npos) {
|
||||
if (progressCallback) progressCallback("Nouvel essai en mode utilisateur...", -1);
|
||||
|
||||
@@ -53,6 +53,14 @@ public:
|
||||
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>`.
|
||||
|
||||
Reference in new issue
Block a user