From 69b4f9113a896dfc4ae4dc3dbaa804d0b9288e4e Mon Sep 17 00:00:00 2001 From: vkrishtal Date: Fri, 25 Apr 2014 00:28:22 +0400 Subject: [PATCH] BugFix: clementine cannot delete directories in file tab #858 --- src/core/filesystemmusicstorage.cpp | 41 ++++++++++++++++++++++++++++- src/core/filesystemmusicstorage.h | 3 +++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/core/filesystemmusicstorage.cpp b/src/core/filesystemmusicstorage.cpp index 1c7213f5c..5981cb01c 100644 --- a/src/core/filesystemmusicstorage.cpp +++ b/src/core/filesystemmusicstorage.cpp @@ -50,5 +50,44 @@ bool FilesystemMusicStorage::CopyToStorage(const CopyJob& job) { } bool FilesystemMusicStorage::DeleteFromStorage(const DeleteJob& job) { - return QFile::remove(job.metadata_.url().toLocalFile()); + bool result = false; + + QString path = job.metadata_.url().toLocalFile(); + QFileInfo fileInfo(path); + if (fileInfo.isDir()) + result = RemoveDirectory(path); + else + result = QFile::remove(path); + + return result; +} + +bool FilesystemMusicStorage::RemoveDirectory(const QString& dirName) { + bool result = true; + QDir dir(dirName); + + if (dir.exists(dirName)) { + auto fileInfoList = dir.entryInfoList(QDir::NoDotAndDotDot + | QDir::System + | QDir::Hidden + | QDir::AllDirs + | QDir::Files + , QDir::DirsFirst); + + for (auto info : fileInfoList) { + // remove subdirectories too + if (info.isDir()) { + result = RemoveDirectory(info.absoluteFilePath()); + } + else { + result = QFile::remove(info.absoluteFilePath()); + } + + if (!result) { + return result; + } + } + result = dir.rmdir(dirName); + } + return result; } diff --git a/src/core/filesystemmusicstorage.h b/src/core/filesystemmusicstorage.h index 71cae43dc..7721c47b3 100644 --- a/src/core/filesystemmusicstorage.h +++ b/src/core/filesystemmusicstorage.h @@ -30,6 +30,9 @@ class FilesystemMusicStorage : public virtual MusicStorage { bool CopyToStorage(const CopyJob& job); bool DeleteFromStorage(const DeleteJob& job); +private: + bool RemoveDirectory(const QString &dirName); + private: QString root_; };