From d3eefee25a5b964cfc3ceea8f0300544a102a9f1 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Tue, 16 May 2023 13:28:48 +0800 Subject: [PATCH 1/2] Replace custom moveToTrash solution with Qt QFile::moveToTrash Signed-off-by: Claudio Cambra --- src/common/filesystembase.cpp | 73 +++-------------------------------- 1 file changed, 5 insertions(+), 68 deletions(-) diff --git a/src/common/filesystembase.cpp b/src/common/filesystembase.cpp index 26d6b9f3121a..89c642692193 100644 --- a/src/common/filesystembase.cpp +++ b/src/common/filesystembase.cpp @@ -367,77 +367,14 @@ bool FileSystem::remove(const QString &fileName, QString *errorString) 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", R"(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", R"(Could not move "%1" to "%2")") - .arg(f.absoluteFilePath(), trashFilePath + f.fileName()); - return false; + QFile f(fileName); + if (!f.moveToTrash()) { + if (errorString) { + *errorString = f.errorString(); } + 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 } bool FileSystem::isFileLocked(const QString &fileName) From e54d78ad69e7f81f9e0eeb9279da6384d8707927 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Tue, 16 May 2023 13:50:10 +0800 Subject: [PATCH 2/2] Add checkbox to general settings to enabling file removal meaning moving files to trash, rather than immediate deletion Signed-off-by: Claudio Cambra --- src/gui/generalsettings.cpp | 25 ++++++++++++++++--------- src/gui/generalsettings.ui | 13 ++++++++++++- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/gui/generalsettings.cpp b/src/gui/generalsettings.cpp index b01a742d1f64..eb468939629f 100644 --- a/src/gui/generalsettings.cpp +++ b/src/gui/generalsettings.cpp @@ -187,6 +187,7 @@ GeneralSettings::GeneralSettings(QWidget *parent) connect(_ui->newFolderLimitCheckBox, &QAbstractButton::toggled, this, &GeneralSettings::saveMiscSettings); connect(_ui->newFolderLimitSpinBox, static_cast(&QSpinBox::valueChanged), this, &GeneralSettings::saveMiscSettings); connect(_ui->newExternalStorage, &QAbstractButton::toggled, this, &GeneralSettings::saveMiscSettings); + connect(_ui->moveFilesToTrashCheckBox, &QAbstractButton::toggled, this, &GeneralSettings::saveMiscSettings); #ifndef WITH_CRASHREPORTER _ui->crashreporterCheckBox->setVisible(false); @@ -246,17 +247,20 @@ void GeneralSettings::loadMiscSettings() { QScopedValueRollback scope(_currentlyLoading, true); ConfigFile cfgFile; + _ui->monoIconsCheckBox->setChecked(cfgFile.monoIcons()); _ui->serverNotificationsCheckBox->setChecked(cfgFile.optionalServerNotifications()); _ui->callNotificationsCheckBox->setEnabled(_ui->serverNotificationsCheckBox->isEnabled()); _ui->callNotificationsCheckBox->setChecked(cfgFile.showCallNotifications()); _ui->showInExplorerNavigationPaneCheckBox->setChecked(cfgFile.showInExplorerNavigationPane()); _ui->crashreporterCheckBox->setChecked(cfgFile.crashReporter()); + _ui->newExternalStorage->setChecked(cfgFile.confirmExternalStorage()); + _ui->monoIconsCheckBox->setChecked(cfgFile.monoIcons()); + _ui->moveFilesToTrashCheckBox->setChecked(cfgFile.moveToTrash()); + auto newFolderLimit = cfgFile.newBigFolderSizeLimit(); _ui->newFolderLimitCheckBox->setChecked(newFolderLimit.first); _ui->newFolderLimitSpinBox->setValue(newFolderLimit.second); - _ui->newExternalStorage->setChecked(cfgFile.confirmExternalStorage()); - _ui->monoIconsCheckBox->setChecked(cfgFile.monoIcons()); } #if defined(BUILD_UPDATER) @@ -406,17 +410,20 @@ void GeneralSettings::slotToggleAutoUpdateCheck() void GeneralSettings::saveMiscSettings() { - if (_currentlyLoading) + if (_currentlyLoading) { return; + } + ConfigFile cfgFile; - bool isChecked = _ui->monoIconsCheckBox->isChecked(); - cfgFile.setMonoIcons(isChecked); - Theme::instance()->setSystrayUseMonoIcons(isChecked); - cfgFile.setCrashReporter(_ui->crashreporterCheckBox->isChecked()); - cfgFile.setNewBigFolderSizeLimit(_ui->newFolderLimitCheckBox->isChecked(), - _ui->newFolderLimitSpinBox->value()); + const auto monoIconsChecked = _ui->monoIconsCheckBox->isChecked(); + cfgFile.setMonoIcons(monoIconsChecked); + Theme::instance()->setSystrayUseMonoIcons(monoIconsChecked); + + cfgFile.setCrashReporter(_ui->crashreporterCheckBox->isChecked()); + cfgFile.setNewBigFolderSizeLimit(_ui->newFolderLimitCheckBox->isChecked(), _ui->newFolderLimitSpinBox->value()); cfgFile.setConfirmExternalStorage(_ui->newExternalStorage->isChecked()); + cfgFile.setMoveToTrash(_ui->moveFilesToTrashCheckBox->isChecked()); } void GeneralSettings::slotToggleLaunchOnStartup(bool enable) diff --git a/src/gui/generalsettings.ui b/src/gui/generalsettings.ui index 00cbeb14953b..ee82031c911e 100644 --- a/src/gui/generalsettings.ui +++ b/src/gui/generalsettings.ui @@ -7,7 +7,7 @@ 0 0 556 - 563 + 613 @@ -276,6 +276,17 @@ + + + + + + Move removed files to trash + + + + +