From 906e26dc6abffbd8cb1c2ad0b9b2ebcd4b36a280 Mon Sep 17 00:00:00 2001 From: timoreo Date: Thu, 13 Aug 2026 11:30:32 +0200 Subject: [PATCH] feat: Added Trashing for Flatpak Signed-off-by: timoreo --- launcher/CMakeLists.txt | 2 +- launcher/FileSystem.cpp | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index 0704f5676..9e42710ca 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -1298,7 +1298,7 @@ target_link_libraries(Launcher_logic Launcher_rainbow ) if (TARGET ${Launcher_QT_DBUS}) - add_compile_definitions(WITH_QTDBUS) + target_compile_definitions(Launcher_logic PUBLIC WITH_QTDBUS) endif() if(APPLE) diff --git a/launcher/FileSystem.cpp b/launcher/FileSystem.cpp index ef56e3e65..552a03d25 100644 --- a/launcher/FileSystem.cpp +++ b/launcher/FileSystem.cpp @@ -86,6 +86,13 @@ namespace fs = std::filesystem; #include #include #include + +#if defined(WITH_QTDBUS) +#include +#include +#include +#endif + #elif defined(Q_OS_MACOS) #include #include @@ -712,9 +719,36 @@ bool deleteContents(const QString& path) bool trash(QString path, QString* pathInTrash) { - // FIXME: Figure out trash in Flatpak. Qt seemingly doesn't use the Trash portal - if (DesktopServices::isFlatpak()) +#ifdef Q_OS_LINUX + if (DesktopServices::isFlatpak()) { +#if defined(WITH_QTDBUS) + const int file = open(path.toUtf8().data(), O_PATH | O_CLOEXEC, 0); + if (file == -1) { + return false; + } + const QDBusUnixFileDescriptor fileDescriptor(file); + close(file); + + QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop", + "org.freedesktop.portal.Trash", "TrashFile"); + + message << QVariant::fromValue(fileDescriptor); + + const QDBusReply reply = QDBusConnection::sessionBus().call(message); + if (!reply.isValid()) { + qWarning() << "Error while trying to trash the file" << path << reply.error().name() << ":" << reply.error().message(); + return false; + } + if (pathInTrash) { + *pathInTrash = QString(); // No info provided by the API, use the same semantics as QFile::moveToTrash + } + + return reply.value() != 0; +#else return false; +#endif + } +#endif #if defined Q_OS_WIN32 if (IsWindowsServer()) return false;