diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 09e3bc22f..fd3a800ed 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -1405,9 +1405,14 @@ bool Session::deleteTorrent(const QString &hash, bool deleteLocalFiles) // Remove it from session if (deleteLocalFiles) { - QString rootPath = torrent->rootPath(true); - if (!rootPath.isEmpty()) - m_savePathsToRemove[torrent->hash()] = rootPath; + if (torrent->savePath(true) == torrentTempPath(torrent->hash())) { + m_savePathsToRemove[torrent->hash()] = torrent->savePath(true); + } + else { + QString rootPath = torrent->rootPath(true); + if (!rootPath.isEmpty()) + m_savePathsToRemove[torrent->hash()] = rootPath; + } m_nativeSession->remove_torrent(torrent->nativeHandle(), libt::session::delete_files); } else { @@ -3441,17 +3446,16 @@ void Session::handleTorrentRemovedAlert(libt::torrent_removed_alert *p) void Session::handleTorrentDeletedAlert(libt::torrent_deleted_alert *p) { - m_savePathsToRemove.remove(p->info_hash); + const QString path = m_savePathsToRemove.take(p->info_hash); + if (path == torrentTempPath(p->info_hash)) + Utils::Fs::smartRemoveEmptyFolderTree(path); } void Session::handleTorrentDeleteFailedAlert(libt::torrent_delete_failed_alert *p) { // libtorrent won't delete the directory if it contains files not listed in the torrent, // so we remove the directory ourselves - if (m_savePathsToRemove.contains(p->info_hash)) { - QString path = m_savePathsToRemove.take(p->info_hash); - Utils::Fs::smartRemoveEmptyFolderTree(path); - } + Utils::Fs::smartRemoveEmptyFolderTree(m_savePathsToRemove.take(p->info_hash)); } void Session::handleMetadataReceivedAlert(libt::metadata_received_alert *p) diff --git a/src/base/utils/fs.cpp b/src/base/utils/fs.cpp index a47ba0fe1..51cfc508d 100644 --- a/src/base/utils/fs.cpp +++ b/src/base/utils/fs.cpp @@ -100,7 +100,7 @@ QString Utils::Fs::folderName(const QString& file_path) */ bool Utils::Fs::smartRemoveEmptyFolderTree(const QString& path) { - if (!QDir(path).exists()) + if (path.isEmpty() || !QDir(path).exists()) return false; static const QStringList deleteFilesList = { @@ -161,9 +161,10 @@ bool Utils::Fs::forceRemove(const QString& file_path) * Removes directory and its content recursively. * */ -void Utils::Fs::removeDirRecursive(const QString& dirName) +void Utils::Fs::removeDirRecursive(const QString &path) { - QDir(dirName).removeRecursively(); + if (!path.isEmpty()) + QDir(path).removeRecursively(); } /** diff --git a/src/base/utils/fs.h b/src/base/utils/fs.h index d3305afb8..fa327cd60 100644 --- a/src/base/utils/fs.h +++ b/src/base/utils/fs.h @@ -59,7 +59,7 @@ namespace Utils bool smartRemoveEmptyFolderTree(const QString& path); bool forceRemove(const QString& file_path); - void removeDirRecursive(const QString& dirName); + void removeDirRecursive(const QString& path); QString tempPath(); }