From a4294c28b2aa884f9c3caa8622d4fb888a281407 Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Fri, 27 Oct 2023 16:24:14 +0200 Subject: [PATCH] Move files to trash instead of deleting them Add an option to have the client not delete files when they are removed on the remote side, but to move them into the trash bin. Fixes: #9001 --- src/common/filesystembase.cpp | 75 ---------------------------------- src/common/filesystembase.h | 5 --- src/gui/folder.cpp | 5 +++ src/gui/folder.h | 2 + src/gui/folderman.cpp | 9 ++++ src/gui/folderman.h | 3 ++ src/gui/generalsettings.cpp | 6 +++ src/gui/generalsettings.h | 1 + src/gui/generalsettings.ui | 7 ++++ src/gui/settingsdialog.cpp | 1 + src/libsync/propagatorjobs.cpp | 5 ++- 11 files changed, 38 insertions(+), 81 deletions(-) diff --git a/src/common/filesystembase.cpp b/src/common/filesystembase.cpp index c817799822c..bdf48f34d1f 100644 --- a/src/common/filesystembase.cpp +++ b/src/common/filesystembase.cpp @@ -418,81 +418,6 @@ bool FileSystem::remove(const QString &fileName, QString *errorString) return true; } -bool FileSystem::moveToTrash(const QString &fileName, QString *errorString) -{ - // TODO: Qt 5.15 bool QFile::moveToTrash() -#if defined Q_OS_UNIX && !defined Q_OS_MAC - QString trashPath, trashFilePath, trashInfoPath; - QString xdgDataHome = QFile::decodeName(qgetenv("XDG_DATA_HOME")); - if (xdgDataHome.isEmpty()) { - trashPath = QDir::homePath() + QStringLiteral("/.local/share/Trash/"); // trash path that should exist - } else { - trashPath = xdgDataHome + QStringLiteral("/Trash/"); - } - - trashFilePath = trashPath + QStringLiteral("files/"); // trash file path contain delete files - trashInfoPath = trashPath + QStringLiteral("info/"); // trash info path contain delete files information - - if (!(QDir().mkpath(trashFilePath) && QDir().mkpath(trashInfoPath))) { - *errorString = QCoreApplication::translate("FileSystem", "Could not make directories in trash"); - return false; //mkpath will return true if path exists - } - - QFileInfo f(fileName); - - QDir file; - int suffix_number = 1; - if (file.exists(trashFilePath + f.fileName())) { //file in trash already exists, move to "filename.1" - QString path = trashFilePath + f.fileName() + QLatin1Char('.'); - while (file.exists(path + QString::number(suffix_number))) { //or to "filename.2" if "filename.1" exists, etc - suffix_number++; - } - if (!file.rename(f.absoluteFilePath(), path + QString::number(suffix_number))) { // rename(file old path, file trash path) - *errorString = QCoreApplication::translate("FileSystem", "Could not move '%1' to '%2'") - .arg(f.absoluteFilePath(), path + QString::number(suffix_number)); - return false; - } - } else { - if (!file.rename(f.absoluteFilePath(), trashFilePath + f.fileName())) { // rename(file old path, file trash path) - *errorString = QCoreApplication::translate("FileSystem", "Could not move '%1' to '%2'") - .arg(f.absoluteFilePath(), trashFilePath + f.fileName()); - return false; - } - } - - // create file format for trash info file----- START - QFile infoFile; - if (file.exists(trashInfoPath + f.fileName() + QStringLiteral(".trashinfo"))) { //TrashInfo file already exists, create "filename.1.trashinfo" - QString filename = trashInfoPath + f.fileName() + QLatin1Char('.') + QString::number(suffix_number) + QStringLiteral(".trashinfo"); - infoFile.setFileName(filename); //filename+.trashinfo // create file information file in /.local/share/Trash/info/ folder - } else { - QString filename = trashInfoPath + f.fileName() + QStringLiteral(".trashinfo"); - infoFile.setFileName(filename); //filename+.trashinfo // create file information file in /.local/share/Trash/info/ folder - } - - infoFile.open(QIODevice::ReadWrite); - - QTextStream stream(&infoFile); // for write data on open file - - stream << "[Trash Info]\n" - << "Path=" - << QUrl::toPercentEncoding(f.absoluteFilePath(), "~_-./") - << "\n" - << "DeletionDate=" - << QDateTime::currentDateTime().toString(Qt::ISODate) - << '\n'; - infoFile.close(); - - // create info file format of trash file----- END - - return true; -#else - Q_UNUSED(fileName) - *errorString = QCoreApplication::translate("FileSystem", "Moving to the trash is not implemented on this platform"); - return false; -#endif -} - #ifdef Q_OS_WIN namespace { diff --git a/src/common/filesystembase.h b/src/common/filesystembase.h index 0c45108840a..dd90d26b605 100644 --- a/src/common/filesystembase.h +++ b/src/common/filesystembase.h @@ -130,11 +130,6 @@ namespace FileSystem { */ bool OCSYNC_EXPORT remove(const QString &fileName, QString *errorString = nullptr); - /** - * Move the specified file or folder to the trash. (Only implemented on linux) - */ - bool OCSYNC_EXPORT moveToTrash(const QString &filename, QString *errorString); - /** * Replacement for QFile::open(ReadOnly) followed by a seek(). * This version sets a more permissive sharing mode on Windows. diff --git a/src/gui/folder.cpp b/src/gui/folder.cpp index 0cf1201ccc8..58f34c56fdc 100644 --- a/src/gui/folder.cpp +++ b/src/gui/folder.cpp @@ -1015,6 +1015,11 @@ void Folder::setDirtyNetworkLimits() _engine->setNetworkLimits(uploadLimit, downloadLimit); } +void Folder::reloadSyncOptions() +{ + _engine->setSyncOptions(loadSyncOptions()); +} + void Folder::slotSyncError(const QString &message, ErrorCategory category) { _syncResult.appendErrorString(message); diff --git a/src/gui/folder.h b/src/gui/folder.h index 556a115443e..d9039edf73f 100644 --- a/src/gui/folder.h +++ b/src/gui/folder.h @@ -258,6 +258,8 @@ class Folder : public QObject void setDirtyNetworkLimits(); + void reloadSyncOptions(); + /** * Ignore syncing of hidden files or not. This is defined in the * folder definition diff --git a/src/gui/folderman.cpp b/src/gui/folderman.cpp index 796f4ea1631..3f007b35773 100644 --- a/src/gui/folderman.cpp +++ b/src/gui/folderman.cpp @@ -859,6 +859,15 @@ Result FolderMan::unsupportedConfiguration(const QString &path) c return *it; } +void FolderMan::slotReloadSyncOptions() +{ + for (auto *f : qAsConst(_folders)) { + if (f) { + f->reloadSyncOptions(); + } + } +} + bool FolderMan::checkVfsAvailability(const QString &path, Vfs::Mode mode) const { return unsupportedConfiguration(path) && Vfs::checkAvailability(path, mode); diff --git a/src/gui/folderman.h b/src/gui/folderman.h index 2155dd4c4db..7070c02610b 100644 --- a/src/gui/folderman.h +++ b/src/gui/folderman.h @@ -261,6 +261,9 @@ public slots: */ void slotSyncOnceFileUnlocks(const QString &path, FileSystem::LockMode mode); + /// This slot will tell all sync engines to reload the sync options. + void slotReloadSyncOptions(); + private slots: void slotFolderSyncPaused(Folder *, bool paused); void slotFolderCanSyncChanged(); diff --git a/src/gui/generalsettings.cpp b/src/gui/generalsettings.cpp index 3cc7c923cdf..84fb594d379 100644 --- a/src/gui/generalsettings.cpp +++ b/src/gui/generalsettings.cpp @@ -80,6 +80,11 @@ GeneralSettings::GeneralSettings(QWidget *parent) _ui->crashreporterCheckBox->setVisible(Theme::instance()->withCrashReporter()); + connect(_ui->moveToTrashCheckBox, &QCheckBox::toggled, this, [this](bool checked) { + ConfigFile().setMoveToTrash(checked); + Q_EMIT syncOptionsChanged(); + }); + /* Set the left contents margin of the layout to zero to make the checkboxes * align properly vertically , fixes bug #3758 */ @@ -287,6 +292,7 @@ void GeneralSettings::slotIgnoreFilesEditor() void GeneralSettings::reloadConfig() { _ui->syncHiddenFilesCheckBox->setChecked(!FolderMan::instance()->ignoreHiddenFiles()); + _ui->moveToTrashCheckBox->setChecked(ConfigFile().moveToTrash()); if (Utility::hasSystemLaunchOnStartup(Theme::instance()->appName())) { _ui->autostartCheckBox->setChecked(true); _ui->autostartCheckBox->setDisabled(true); diff --git a/src/gui/generalsettings.h b/src/gui/generalsettings.h index 95d26e9b1a1..4edcf2dc0b6 100644 --- a/src/gui/generalsettings.h +++ b/src/gui/generalsettings.h @@ -41,6 +41,7 @@ class GeneralSettings : public QWidget signals: void showAbout(); + void syncOptionsChanged(); private slots: void saveMiscSettings(); diff --git a/src/gui/generalsettings.ui b/src/gui/generalsettings.ui index 232c250fb9e..22fc5c29f8f 100644 --- a/src/gui/generalsettings.ui +++ b/src/gui/generalsettings.ui @@ -120,6 +120,13 @@ + + + + Move remotely deleted files to the local trash bin instead of deleting them + + + diff --git a/src/gui/settingsdialog.cpp b/src/gui/settingsdialog.cpp index 562bcb94297..621a2c5625e 100644 --- a/src/gui/settingsdialog.cpp +++ b/src/gui/settingsdialog.cpp @@ -221,6 +221,7 @@ SettingsDialog::SettingsDialog(ownCloudGui *gui, QWidget *parent) GeneralSettings *generalSettings = new GeneralSettings; _ui->stack->addWidget(generalSettings); QObject::connect(generalSettings, &GeneralSettings::showAbout, gui, &ownCloudGui::slotAbout); + QObject::connect(generalSettings, &GeneralSettings::syncOptionsChanged, FolderMan::instance(), &FolderMan::slotReloadSyncOptions); QWidget *spacer = new QWidget(); spacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum); diff --git a/src/libsync/propagatorjobs.cpp b/src/libsync/propagatorjobs.cpp index 72f0a3603db..4fd886385ac 100644 --- a/src/libsync/propagatorjobs.cpp +++ b/src/libsync/propagatorjobs.cpp @@ -120,7 +120,10 @@ void PropagateLocalRemove::start() bool ok = false; QString removeError; if (_moveToTrash) { - ok = FileSystem::moveToTrash(filename, &removeError); + ok = QFile(filename).moveToTrash(); + if (!ok) { + removeError = tr("Could not move '%1' to the trash bin").arg(filename); + } } else { if (_item->isDirectory()) { // removeRecursively will call done on error