Use flatpak trash portal instad of g_file_trash

This commit is contained in:
2024-08-26 11:43:57 +02:00
parent 2c7a7d473f
commit d6e257e201
3 changed files with 28 additions and 18 deletions
+21 -11
View File
@@ -1,20 +1,30 @@
#ifdef __linux__
#ifdef FLATPAK
#define QT_NO_KEYWORDS
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusUnixFileDescriptor>
#include <QString>
#include <iostream>
#include <gio/gio.h>
#include <fcntl.h>
#include <unistd.h>
//flatpak bug prevent to use QFile::moveToTrash
bool moveToTrash(const QString &path)
{
GFile *gfile = g_file_new_for_path(path.toLocal8Bit().data());
GError *error = nullptr;
g_file_trash(gfile, nullptr, &error);
if(error)std::cerr << "failed to trash file " << error->code << " " << error->message << std::endl;
g_clear_error(&error);
g_object_unref(gfile);
return true;
QDBusConnection con = QDBusConnection::sessionBus();
QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop", "org.freedesktop.portal.Trash", "TrashFile");
int fd = ::open(path.toLocal8Bit().data(), O_RDWR);
if(fd >= 0)
{
QList<QVariant> args = {QVariant::fromValue(QDBusUnixFileDescriptor(fd))};
message.setArguments(args);
QDBusMessage reply = con.call(message);
::close(fd);
if(reply.type() == QDBusMessage::ReplyMessage && reply.arguments().size() && reply.arguments().first().toInt())
return true;
else
return false;
}
return false;
}
#else