feat(flatpak): link installed apps into the desktop launcher

When a flatpak app is installed through the app, its .desktop entries and
icons are now symlinked into the user's standard XDG dirs
(~/.local/share/{applications,icons}), which every desktop scans by default.
This makes flatpak apps appear in any launcher (wofi, GNOME, KDE...) the same
way a pacman install would, without relying on XDG_DATA_DIRS or any
desktop-specific configuration. The links are removed on uninstall, and a
refresh re-links every installed app so ones installed before this feature
(or outside the app) self-heal.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
BlackAngelTV committed 2026-08-29 16:54:39 +02:00
1 parent 6224a94df6
commit a1d8c96e4e
2 files changed
+123 -1

No files matched your search

+111 -1
View File
@@ -5,9 +5,30 @@
#include <sstream>
#include <cctype>
#include <algorithm>
#include <cstdlib>
#include <filesystem>
#include <set>
#include <vector>
namespace fs = std::filesystem;
// The per-user standard XDG data dir (~/.local/share).
static fs::path userShareDir() {
const char* home = std::getenv("HOME");
return fs::path(home ? home : "/tmp") / ".local/share";
}
// Link (or fall back to copy) one flatpak export into the user's standard XDG
// dir. Preferring a symlink means it survives flatpak app updates, which
// re-export at the same path.
static void linkFile(const fs::path& src, const fs::path& dest) {
std::error_code ec;
fs::create_directories(dest.parent_path(), ec);
fs::remove(dest, ec); // Replace any stale entry (e.g. from a past run).
fs::create_symlink(fs::absolute(src), dest, ec);
if (ec) fs::copy_file(src, dest, fs::copy_options::overwrite_existing, ec);
}
// Helper to run shell commands and capture output.
static std::string exec(const char* cmd) {
std::array<char, 256> buffer;
@@ -223,9 +244,11 @@ bool FlatpakManager::install(const std::string& packageName,
m_lastError.clear();
// Idempotent: an app already deployed (in either installation) is reported
// as installed without re-running the installer.
// as installed without re-running the installer, then made sure it appears
// in the desktop launcher (covers apps installed before this feature).
if (isInstalled(packageName)) {
if (progressCallback) progressCallback(packageName + " est déjà installé.");
integrateDesktopEntries(packageName);
return true;
}
@@ -277,9 +300,88 @@ bool FlatpakManager::install(const std::string& packageName,
m_lastError = lastOutputLines(output);
return false;
}
// Like a pacman install, make the freshly installed app appear in the
// desktop launcher immediately (see integrateDesktopEntries).
integrateDesktopEntries(packageName);
return true;
}
// Link an app's launcher entries and icons into the user's standard XDG dirs
// (~/.local/share/{applications,icons}), which every desktop scans by default.
// This makes flatpak apps show up in any launcher (wofi, dmenu, GNOME menu,
// KDE...) regardless of XDG_DATA_DIRS, mirroring what a pacman install does
// through /usr/share/applications.
void FlatpakManager::integrateDesktopEntries(const std::string& appId) {
std::error_code ec;
fs::path exports = userShareDir() / "flatpak/exports/share";
if (!fs::is_directory(exports, ec)) return;
// .desktop files are the core of the integration.
fs::path srcApps = exports / "applications";
fs::path destApps = userShareDir() / "applications";
if (fs::is_directory(srcApps, ec)) {
fs::create_directories(destApps, ec);
for (const auto& entry : fs::directory_iterator(srcApps)) {
const std::string name = entry.path().filename().string();
if (name.rfind(appId, 0) == 0
&& entry.path().extension() == ".desktop") {
linkFile(entry.path(), destApps / name);
}
}
}
// Mirror the app's icons so the launcher shows a proper icon.
fs::path srcIcons = exports / "icons";
fs::path destIcons = userShareDir() / "icons";
if (fs::is_directory(srcIcons, ec)) {
for (const auto& entry : fs::recursive_directory_iterator(srcIcons)) {
if (!entry.is_regular_file()) continue;
const std::string name = entry.path().filename().string();
if (name.rfind(appId, 0) != 0) continue;
auto rel = entry.path().lexically_relative(srcIcons);
linkFile(entry.path(), destIcons / rel);
}
}
// Refresh the desktop-entry database so launchers pick up the new entries.
system(("update-desktop-database " + destApps.string() + " >/dev/null 2>&1").c_str());
}
// Remove the launcher links created by integrateDesktopEntries. Only entries
// that are symlinks we created are removed, so a real file of the same name is
// never touched.
void FlatpakManager::uninstallDesktopEntries(const std::string& appId) {
auto removeLinks = [&](const fs::path& dir, bool recursive) {
std::error_code ec;
if (!fs::is_directory(dir, ec)) return;
if (recursive) {
for (const auto& entry : fs::recursive_directory_iterator(dir, ec)) {
if (!entry.is_symlink()) continue;
if (entry.path().filename().string().rfind(appId, 0) != 0) continue;
fs::remove(entry.path(), ec);
}
} else {
for (const auto& entry : fs::directory_iterator(dir, ec)) {
if (!entry.is_symlink()) continue;
if (entry.path().filename().string().rfind(appId, 0) != 0) continue;
fs::remove(entry.path(), ec);
}
}
};
removeLinks(userShareDir() / "applications", /*recursive=*/false);
removeLinks(userShareDir() / "icons", /*recursive=*/true);
system(("update-desktop-database " + (userShareDir() / "applications").string()
+ " >/dev/null 2>&1").c_str());
}
void FlatpakManager::integrateAllInstalled() {
for (const auto& pkg : listInstalled()) {
integrateDesktopEntries(pkg.name);
}
}
bool FlatpakManager::remove(const std::string& packageName,
std::function<void(const std::string&)> progressCallback) {
m_lastError.clear();
@@ -293,6 +395,9 @@ bool FlatpakManager::remove(const std::string& packageName,
m_lastError = lastOutputLines(output);
return false;
}
// Clear the launcher links created at install time (see integrateDesktopEntries).
uninstallDesktopEntries(packageName);
return true;
}
@@ -348,5 +453,10 @@ bool FlatpakManager::refresh(std::function<void(const std::string&)> progressCal
m_lastError = lastOutputLines(output);
return false;
}
// Self-heal: re-link launcher entries of every installed app so any that
// predate desktop integration (or were installed outside the app) still
// show up in the desktop menu.
integrateAllInstalled();
return true;
}