fix(flatpak): per-user install fallback and real error reporting

Installing through a system-wide remote fails under polkit and, when a
remote exists in both installations, flatpak refuses non-interactive
installs ("Multiple remotes have refs matching"). Retry with an explicit
`--user flathub` target, refresh appstream metadata before giving up on
stale refs, and let update/refresh fall back to the user installation as
well. Expose the last command output through IPackageManager::lastError
and show it in a dialog on failure instead of failing silently.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
BlackAngelTV committed 2026-08-29 13:57:03 +02:00
1 parent 5cc85cac4a
commit 6aef549eb0
7 files changed
+144 -15

No files matched your search

+1 -1
View File
@@ -136,7 +136,7 @@ Search queries **all active providers at once**: every result shows its origin i
### Adding a provider (plugins)
Pacman and Flatpak are **built-in**. To add another manager (apt, dnf, zypper, or another distro's repos) without touching the application:
Pacman and Flatpak are **built-in**. Flatpak installs auto-add the flathub remote when missing and fall back to a **per-user** install (no password) when the system-wide install fails (polkit, homonymous remotes) — the real error is shown on failure. To add another manager (apt, dnf, zypper, or another distro's repos) without touching the application:
1. Implement `IPackageManager` in a `.cpp` file (copy `plugins/example-provider/example-provider.cpp` as a template).
2. Export the C ABI contract with the `FWS_PROVIDER_PLUGIN(MyManager, "id", "Name", "1.0.0", "Description", "Author")` macro.
+1 -1
View File
@@ -136,7 +136,7 @@ La recherche interroge **tous les fournisseurs actifs à la fois** : chaque rés
### Ajouter un fournisseur (plugins)
Pacman et Flatpak sont **intégrés**. Pour ajouter un autre gestionnaire (apt, dnf, zypper, ou les dépôts d'une autre distro) sans toucher à l'application :
Pacman et Flatpak sont **intégrés**. L'installation Flatpak ajoute automatiquement le dépôt flathub si besoin et retombe en installation **utilisateur** (sans mot de passe) quand l'installation système échoue (polkit, plusieurs dépôts homonymes) — l'erreur réelle est affichée en cas d'échec. Pour ajouter un autre gestionnaire (apt, dnf, zypper, ou les dépôts d'une autre distro) sans toucher à l'application :
1. Implémentez `IPackageManager` dans un fichier `.cpp` (copiez `plugins/example-provider/example-provider.cpp` comme modèle).
2. Exportez le contrat C ABI avec la macro `FWS_PROVIDER_PLUGIN(MyManager, "id", "Nom", "1.0.0", "Description", "Auteur")`.
+5
View File
@@ -22,6 +22,9 @@ public:
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.
@@ -48,4 +51,6 @@ public:
private:
bool isInstalled(const std::string& appId);
std::string m_lastError;
};
+4
View File
@@ -119,6 +119,10 @@ private:
// first available provider when the tag is unknown or empty.
IPackageManager* providerFor(const std::string& id) const;
// Show the provider's last error in a dialog when one is available, so a
// failed operation explains itself instead of failing silently.
void showProviderError(const QString& title, IPackageManager* provider);
void setupUI();
void setupSidebar();
void setupHeader();
+5
View File
@@ -143,6 +143,11 @@ public:
// Whether privileged operations need a password (e.g. sudo for pacman).
// Providers that run per-user (e.g. flatpak) can keep the default.
virtual bool requiresPassword() const { return false; }
// Last error reported by the most recent failed operation, if any. Used
// to surface the real command output to the user instead of a generic
// failure message.
virtual std::string lastError() const { return {}; }
};
+101 -11
View File
@@ -4,6 +4,8 @@
#include <stdexcept>
#include <sstream>
#include <cctype>
#include <algorithm>
#include <vector>
// Helper to run shell commands and capture output.
static std::string exec(const char* cmd) {
@@ -49,6 +51,25 @@ static bool isAppId(const std::string& token) {
return dots >= 2 && !first;
}
// Keep the tail of a command's output (the last few non-empty lines) so the
// user sees the real reason an operation failed.
static std::string lastOutputLines(const std::string& output, int maxLines = 4) {
std::istringstream iss(output);
std::vector<std::string> lines;
std::string line;
while (std::getline(iss, line)) {
if (!line.empty()) lines.push_back(line);
}
std::string result;
int start = std::max(0, static_cast<int>(lines.size()) - maxLines);
for (int i = start; i < static_cast<int>(lines.size()); ++i) {
result += lines[i];
result += "\n";
}
return result;
}
// 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<std::string> splitColumns(const std::string& line) {
@@ -181,50 +202,119 @@ bool FlatpakManager::isInstalled(const std::string& appId) {
bool FlatpakManager::install(const std::string& packageName,
std::function<void(const std::string&)> progressCallback) {
m_lastError.clear();
if (progressCallback) progressCallback("Installation de " + packageName + "...");
std::string output = exec(
("flatpak install --noninteractive --assumeyes " + packageName + " 2>&1").c_str());
// A fresh flatpak setup has no remote configured: add the default flathub
// one (idempotent, per-user) so the install can resolve the app.
streamOutput(
exec("flatpak remote-add --if-not-exists --user flathub "
"https://flathub.org/repo/flathub.flatpakrepo 2>&1"),
progressCallback);
auto tryInstall = [&](const std::string& extra) {
return exec(("flatpak install --noninteractive --assumeyes " + extra + " "
+ packageName + " 2>&1").c_str());
};
std::string output = tryInstall("");
// Stale remote metadata makes flatpak report "No refs found": refresh the
// appstream cache and retry once before giving up.
if (output.find("error:") != std::string::npos && !isInstalled(packageName)
&& (output.find("No refs found") != std::string::npos
|| output.find("No remote refs") != std::string::npos)) {
if (progressCallback) progressCallback("Mise à jour des métadonnées des dépôts...");
streamOutput(exec("flatpak update --appstream 2>&1"), progressCallback);
output = tryInstall("");
}
// Failures worth a second attempt in the per-user installation (no polkit,
// no ambiguity): "Multiple remotes have refs matching", system-wide
// remotes requiring authentication, or missing user metadata.
if (output.find("error:") != std::string::npos && !isInstalled(packageName)) {
if (progressCallback) progressCallback("Nouvel essai en mode utilisateur...");
output = tryInstall("--user flathub");
}
streamOutput(output, progressCallback);
// Any "error:" line from flatpak means the install failed.
return output.find("error:") == std::string::npos && isInstalled(packageName);
// The authoritative success check is `flatpak info`; the output may carry
// noise such as "already installed" that must not count as a failure.
if (!isInstalled(packageName)) {
m_lastError = lastOutputLines(output);
return false;
}
return true;
}
bool FlatpakManager::remove(const std::string& packageName,
std::function<void(const std::string&)> progressCallback) {
m_lastError.clear();
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);
if (output.find("error:") != std::string::npos || isInstalled(packageName)) {
m_lastError = lastOutputLines(output);
return false;
}
return true;
}
bool FlatpakManager::update(const std::string& packageName,
std::function<void(const std::string&)> progressCallback) {
std::string output;
m_lastError.clear();
auto tryUpdate = [&](const std::string& extra) {
if (packageName.empty()) {
return exec(("flatpak update --noninteractive --assumeyes " + extra + " 2>&1").c_str());
}
return exec(("flatpak update --noninteractive --assumeyes " + extra + " "
+ packageName + " 2>&1").c_str());
};
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());
}
std::string output = tryUpdate("");
// 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...");
output = tryUpdate("--user");
}
streamOutput(output, progressCallback);
return output.find("error:") == std::string::npos;
if (output.find("error:") != std::string::npos) {
m_lastError = lastOutputLines(output);
return false;
}
return true;
}
bool FlatpakManager::refresh(std::function<void(const std::string&)> progressCallback) {
m_lastError.clear();
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");
// System remotes need polkit for that: fall back to the user installation.
if (output.find("error:") != std::string::npos) {
exec("flatpak remote-add --if-not-exists --user flathub "
"https://flathub.org/repo/flathub.flatpakrepo 2>&1");
output = exec("flatpak update --appstream --user 2>&1");
}
streamOutput(output, progressCallback);
return output.find("error:") == std::string::npos;
if (output.find("error:") != std::string::npos) {
m_lastError = lastOutputLines(output);
return false;
}
return true;
}
+27 -2
View File
@@ -1099,6 +1099,14 @@ IPackageManager* MainWindow::providerFor(const std::string& id) const {
return available.empty() ? nullptr : available.front();
}
void MainWindow::showProviderError(const QString& title, IPackageManager* provider) {
if (!provider) return;
std::string err = provider->lastError();
if (!err.empty()) {
QMessageBox::warning(this, title, QString::fromStdString(err));
}
}
void MainWindow::clearCredentialsAfterUse() {
// In per-operation mode the password is single-use: drop it once the
// command ran so the next operation prompts again.
@@ -1489,6 +1497,7 @@ void MainWindow::onUpdate() {
statusBar()->showMessage("Mise à jour terminée");
} else {
statusBar()->showMessage("Échec de la mise à jour");
showProviderError("Échec de la mise à jour", provider);
}
m_historyManager->record({"", "update", m_selectedPackage.name,
provider->getName(), success});
@@ -1514,12 +1523,19 @@ void MainWindow::onUpdateAll() {
connect(worker, &InstallWorker::progress, this, [this](const QString& msg) {
statusBar()->showMessage(msg);
});
connect(worker, &InstallWorker::finished, this, [this](bool success) {
connect(worker, &InstallWorker::finished, this, [this, providers](bool success) {
setBusy(false);
if (success) {
statusBar()->showMessage("Tous les paquets sont à jour");
} else {
statusBar()->showMessage("Erreur lors de la mise à jour globale");
for (IPackageManager* pm : providers) {
if (!pm->lastError().empty()) {
QMessageBox::warning(this, "Erreur lors de la mise à jour globale",
QString::fromStdString(pm->lastError()));
break;
}
}
}
clearCredentialsAfterUse();
reloadCurrentView();
@@ -1544,12 +1560,19 @@ void MainWindow::onRefresh() {
connect(worker, &InstallWorker::progress, this, [this](const QString& msg) {
statusBar()->showMessage(msg);
});
connect(worker, &InstallWorker::finished, this, [this](bool success) {
connect(worker, &InstallWorker::finished, this, [this, providers](bool success) {
setBusy(false);
if (success) {
statusBar()->showMessage("Sources rafraîchies");
} else {
statusBar()->showMessage("Erreur lors du rafraîchissement");
for (IPackageManager* pm : providers) {
if (!pm->lastError().empty()) {
QMessageBox::warning(this, "Erreur lors du rafraîchissement",
QString::fromStdString(pm->lastError()));
break;
}
}
}
clearCredentialsAfterUse();
refreshDashboard();
@@ -1581,6 +1604,7 @@ void MainWindow::performInstall(const std::string& package) {
statusBar()->showMessage(
QString("Échec de l'installation de %1").arg(
QString::fromStdString(m_selectedPackage.name)));
showProviderError("Échec de l'installation", provider);
}
m_historyManager->record({"", "install", m_selectedPackage.name,
provider->getName(), success});
@@ -1615,6 +1639,7 @@ void MainWindow::performRemove(const std::string& package) {
statusBar()->showMessage(
QString("Échec de la suppression de %1").arg(
QString::fromStdString(m_selectedPackage.name)));
showProviderError("Échec de la suppression", provider);
}
m_historyManager->record({"", "remove", m_selectedPackage.name,
provider->getName(), success});