feat(flatpak): install packages from a Flatpak file
Dragging a .flatpakref, .flatpakrepo or .flatpak onto the window, or opening it with the file manager, has to reach flatpak without the user going through a search. The provider now owns its formats: it claims the files it handles and builds the matching command, and the core routes the request to whichever provider recognises it, so a new provider can claim its own formats the same way. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
9 files changed
+381
-5
No files matched your search
@@ -84,6 +84,39 @@ static std::string lastOutputLines(const std::string& output, int maxLines = 4)
|
||||
return result;
|
||||
}
|
||||
|
||||
static std::string toLower(std::string text) {
|
||||
std::transform(text.begin(), text.end(), text.begin(),
|
||||
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
||||
return text;
|
||||
}
|
||||
|
||||
// Last component of a path or URL, without any query/fragment: the only part a
|
||||
// file is identified by, whatever the transport it came from.
|
||||
static std::string fileName(const std::string& path) {
|
||||
std::string cleaned = path;
|
||||
const std::size_t cut = cleaned.find_first_of("?#");
|
||||
if (cut != std::string::npos) cleaned.erase(cut);
|
||||
const std::size_t slash = cleaned.find_last_of('/');
|
||||
return slash == std::string::npos ? cleaned : cleaned.substr(slash + 1);
|
||||
}
|
||||
|
||||
static bool endsWith(const std::string& text, const std::string& suffix) {
|
||||
return text.size() >= suffix.size()
|
||||
&& text.compare(text.size() - suffix.size(), suffix.size(), suffix) == 0;
|
||||
}
|
||||
|
||||
// Quote one argument for the shell popen() runs, so a dropped file whose name
|
||||
// contains spaces or quotes still reaches flatpak as a single argument.
|
||||
static std::string shellQuote(const std::string& value) {
|
||||
std::string quoted = "'";
|
||||
for (char c : value) {
|
||||
if (c == '\'') quoted += "'\\''";
|
||||
else quoted += c;
|
||||
}
|
||||
quoted += "'";
|
||||
return quoted;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
@@ -194,6 +227,63 @@ std::vector<PackageInfo> FlatpakManager::parseUpdatesOutput(const std::string& o
|
||||
return results;
|
||||
}
|
||||
|
||||
FlatpakManager::FileKind FlatpakManager::classifyFile(const std::string& path) {
|
||||
const std::string name = toLower(fileName(path));
|
||||
|
||||
// A bundle's extension is a prefix of the ref one's (.flatpak vs
|
||||
// .flatpakref), so the longer names are matched first.
|
||||
if (endsWith(name, ".flatpakref")) return FileKind::Ref;
|
||||
if (endsWith(name, ".flatpakrepo")) return FileKind::Repo;
|
||||
if (endsWith(name, ".flatpak")) return FileKind::Bundle;
|
||||
return FileKind::None;
|
||||
}
|
||||
|
||||
std::string FlatpakManager::repoNamespace(const std::string& path) {
|
||||
std::string name = toLower(fileName(path));
|
||||
const std::size_t dot = name.find_last_of('.');
|
||||
if (dot != std::string::npos) name = name.substr(0, dot);
|
||||
// flatpak requires a name, and a nameless path would otherwise register a
|
||||
// remote that cannot be typed back.
|
||||
return name.empty() ? "flatpak-repo" : name;
|
||||
}
|
||||
|
||||
std::string FlatpakManager::installFileCommand(FileKind kind, const std::string& path,
|
||||
bool userScope) {
|
||||
const std::string scope = userScope ? "--user " : "";
|
||||
if (kind == FileKind::Repo) {
|
||||
// A .flatpakrepo only defines a remote: --if-not-exists makes re-opening
|
||||
// the same file a no-op instead of an error.
|
||||
return "flatpak remote-add --if-not-exists " + scope + repoNamespace(path)
|
||||
+ " " + shellQuote(path) + " 2>&1";
|
||||
}
|
||||
// A ref file names the remote it needs and a bundle embeds its runtime, so
|
||||
// both are installed by handing the file itself to `flatpak install`.
|
||||
return "flatpak install --noninteractive --assumeyes " + scope + shellQuote(path)
|
||||
+ " 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
|
||||
// integration needs it (see integrateDesktopEntries).
|
||||
static std::set<std::string> appIdsInOutput(const std::string& output) {
|
||||
std::set<std::string> ids;
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
while (std::getline(iss, line)) {
|
||||
std::istringstream fields(line);
|
||||
std::string token;
|
||||
while (fields >> token) {
|
||||
const std::size_t slash = token.find('/');
|
||||
if (slash != std::string::npos) token = token.substr(0, slash);
|
||||
const std::size_t colon = token.find(':');
|
||||
if (colon != std::string::npos) token = token.substr(colon + 1);
|
||||
if (isAppId(token)) ids.insert(token);
|
||||
}
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
std::vector<PackageInfo> FlatpakManager::search(const std::string& query) {
|
||||
std::string cmd = "flatpak search " + query + " 2>&1";
|
||||
std::vector<PackageInfo> results = parseSearchOutput(exec(cmd.c_str()));
|
||||
@@ -450,6 +540,83 @@ bool FlatpakManager::refresh(ProgressCallback progressCallback) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FlatpakManager::canInstallFile(const std::string& path) const {
|
||||
return classifyFile(path) != FileKind::None;
|
||||
}
|
||||
|
||||
bool FlatpakManager::installFile(const std::string& path,
|
||||
ProgressCallback progressCallback) {
|
||||
m_lastError.clear();
|
||||
|
||||
const FileKind kind = classifyFile(path);
|
||||
if (kind == FileKind::None) {
|
||||
m_lastError = "Format Flatpak non reconnu : " + path;
|
||||
return false;
|
||||
}
|
||||
|
||||
// A file install accepts a local path (what the user drops or opens) and an
|
||||
// URL (.flatpakref links served over https); only the local case can be
|
||||
// checked before flatpak runs.
|
||||
const bool isUrl = path.rfind("http://", 0) == 0 || path.rfind("https://", 0) == 0;
|
||||
if (!isUrl) {
|
||||
std::error_code ec;
|
||||
if (!fs::is_regular_file(path, ec)) {
|
||||
m_lastError = "Fichier introuvable : " + path;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Registering a repository is the whole job, and the per-user scope avoids
|
||||
// polkit on a machine where the user is not an administrator.
|
||||
if (kind == FileKind::Repo) {
|
||||
if (progressCallback) {
|
||||
progressCallback("Ajout du dépôt " + repoNamespace(path) + "...", -1);
|
||||
}
|
||||
const std::string output =
|
||||
runWithProgress(installFileCommand(kind, path, /*userScope=*/true), progressCallback);
|
||||
if (output.find("error:") != std::string::npos) {
|
||||
m_lastError = lastOutputLines(output);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (progressCallback) {
|
||||
progressCallback(kind == FileKind::Ref
|
||||
? "Installation depuis " + path + "..."
|
||||
: "Installation du paquet " + path + "...",
|
||||
-1);
|
||||
}
|
||||
|
||||
std::string output =
|
||||
runWithProgress(installFileCommand(kind, path, /*userScope=*/false), progressCallback);
|
||||
// A system-wide install needs polkit: fall back to the per-user one, exactly
|
||||
// like a name-based install does.
|
||||
if (output.find("error:") != std::string::npos) {
|
||||
if (progressCallback) progressCallback("Nouvel essai en mode utilisateur...", -1);
|
||||
output = runWithProgress(installFileCommand(kind, path, /*userScope=*/true),
|
||||
progressCallback);
|
||||
}
|
||||
|
||||
if (output.find("error:") != std::string::npos) {
|
||||
m_lastError = lastOutputLines(output);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Same launcher integration as a name-based install (see
|
||||
// integrateDesktopEntries), on the ids flatpak just reported. When the file
|
||||
// installed an app whose id cannot be read from the output, every installed
|
||||
// app is re-linked instead: missing the integration would leave the app
|
||||
// invisible in the desktop menu.
|
||||
const std::set<std::string> appIds = appIdsInOutput(output);
|
||||
if (appIds.empty()) {
|
||||
integrateAllInstalled();
|
||||
} else {
|
||||
for (const auto& appId : appIds) integrateDesktopEntries(appId);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FlatpakManager::cleanCache(ProgressCallback progressCallback) {
|
||||
m_lastError.clear();
|
||||
if (progressCallback) {
|
||||
|
||||
Reference in new issue
Block a user