From ad91adf1d96ae77da3bd0245b536cae6904746d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Poizl?= Date: Thu, 15 Dec 2022 17:26:16 +0100 Subject: [PATCH] Implement workaround for flatpak QFile::moveToTrash --- CMakeLists.txt | 6 +++++- delete.cpp | 30 ++++++++++++++++++++++++++++++ mainwindow.cpp | 6 ++++-- 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 delete.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 64bcba3..a661406 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ set(TENMON_SRC about.cpp database.cpp databaseview.cpp + delete.cpp filesystemwidget.cpp imageinfo.cpp imageringlist.cpp @@ -63,6 +64,8 @@ elseif(APPLE) else() add_compile_definitions("__PCL_LINUX") set(tenmon_ICON "") + find_package(PkgConfig REQUIRED) + pkg_search_module(GIO REQUIRED gio-2.0) endif() add_executable(tenmon WIN32 MACOSX_BUNDLE ${tenmon_ICON} ${TENMON_SRC}) @@ -76,13 +79,14 @@ elseif(APPLE) target_link_directories(tenmon PRIVATE 3rdparty/lib/MacOS) else() target_link_directories(tenmon PRIVATE 3rdparty/lib/Linux) + target_include_directories(tenmon PRIVATE ${GIO_INCLUDE_DIRS}) endif() target_link_libraries(tenmon Qt5::Widgets Qt5::Sql ${OpenCV_LIBS} ${GSL_LIB} ${GSLCBLAS_LIB} ${EXIF_LIB} ${FITS_LIB} ${RAW_LIB} ${WCS_LIB}) if(APPLE) target_link_libraries(tenmon PCL-pxi lcms-pxi lz4-pxi RFC6234-pxi zlib-pxi "-framework CoreFoundation") else() - target_link_libraries(tenmon PCL lcms lz4 RFC6234 zlib) + target_link_libraries(tenmon PCL lcms lz4 RFC6234 zlib ${GIO_LDFLAGS}) endif(APPLE) if(LIBRAW_STATIC) diff --git a/delete.cpp b/delete.cpp new file mode 100644 index 0000000..ea5ae5f --- /dev/null +++ b/delete.cpp @@ -0,0 +1,30 @@ +#ifdef __linux__ + +#define QT_NO_KEYWORDS +#include +#include +#include + +//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; +} + +#else + +#include +#include + +bool moveToTrash(const QString &path) +{ + return QFile::moveToTrash(path); +} + +#endif diff --git a/mainwindow.cpp b/mainwindow.cpp index 83966e5..cfda381 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -30,6 +30,8 @@ #include #endif +bool moveToTrash(const QString &path); + int MainWindow::socketPair[2] = {0, 0}; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) @@ -332,7 +334,7 @@ void MainWindow::copyOrMove(bool copy, const QString &dest) QProgressDialog progress(copy ? tr("Copying") : tr("Moving"), tr("Cancel"), 0, files.size(), this); progress.setWindowModality(Qt::WindowModal); progress.show(); - foreach(const QString &file, files) + for(const QString &file : files) { bool result = false; QFileInfo info(file); @@ -571,7 +573,7 @@ void MainWindow::deleteMarked() if(progress.wasCanceled()) return; - if(!QFile::moveToTrash(file)) + if(!moveToTrash(file)) { QMessageBox::warning(this, tr("Failed to move file to trash"), tr("Failed to move file to trash %1").arg(file)); return;