From 1242bf6430340b1c1eac124ba89d990fd29db5b3 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Mon, 9 Jul 2018 11:00:31 +0800 Subject: [PATCH] fix(DTrashManager): clear trash behavior not correct Change-Id: I65bcee631da8d8db3089370beb28144d20c66ef2 --- src/util/dtrashmanager_linux.cpp | 45 ++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/util/dtrashmanager_linux.cpp b/src/util/dtrashmanager_linux.cpp index b1ac64c66..741b8bac9 100644 --- a/src/util/dtrashmanager_linux.cpp +++ b/src/util/dtrashmanager_linux.cpp @@ -155,6 +155,9 @@ class DTrashManagerPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate DTrashManagerPrivate(DTrashManager *q_ptr) : DObjectPrivate(q_ptr) {} + static bool removeFileOrDir(const QString &path); + static bool removeFromIterator(QDirIterator &iter); + D_DECLARE_PUBLIC(DTrashManager) }; @@ -175,28 +178,14 @@ bool DTrashManager::trashIsEmpty() const bool DTrashManager::cleanTrash() { QDirIterator iterator_info(TRASH_INFO_PATH, -// QStringList() << "*.trashinfo", QDir::Files | QDir::NoDotAndDotDot | QDir::Hidden); - bool ok = true; - - while (iterator_info.hasNext()) { - if (!QFile::remove(iterator_info.next())) { - ok = false; - } - } - QDirIterator iterator_files(TRASH_FILES_PATH, QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden, QDirIterator::Subdirectories); - while (iterator_files.hasNext()) { - if (!QFile::remove(iterator_files.next())) { - ok = false; - } - } - - return ok; + return DTrashManagerPrivate::removeFromIterator(iterator_info) && + DTrashManagerPrivate::removeFromIterator(iterator_files); } bool DTrashManager::moveToTrash(const QString &filePath, bool followSymlink) @@ -245,4 +234,28 @@ DTrashManager::DTrashManager() } +bool DTrashManagerPrivate::removeFileOrDir(const QString &path) +{ + QFileInfo fileInfo(path); + if (fileInfo.isDir()) { + QDir dir(path); + return dir.removeRecursively(); + } else { + return QFile::remove(path); + } +} + +bool DTrashManagerPrivate::removeFromIterator(QDirIterator &iter) +{ + bool ok = true; + while (iter.hasNext()) { + QString nextPath = iter.next(); +// qDebug() << iter.fileName() << iterator_info.filePath(); + if (!DTrashManagerPrivate::removeFileOrDir(nextPath)) { + ok = false; + } + } + return ok; +} + DWIDGET_END_NAMESPACE