From cc4e1c5bbe184d7776efc28c20a63ab56b125bb4 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Wed, 29 Dec 2010 20:29:55 +0000 Subject: [PATCH] Remove empty folders on torrent soft deletion Code clean up --- src/misc.cpp | 19 ++++++------------- src/misc.h | 2 +- src/properties/propertieswidget.cpp | 3 ++- src/qtlibtorrent/qbtsession.cpp | 11 +++++++---- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/misc.cpp b/src/misc.cpp index ff0b5825e..bf997d501 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -557,19 +557,6 @@ bool misc::isPreviewable(QString extension){ return false; } -bool misc::removeEmptyTree(QString path) { - QDir dir(path); - foreach(const QString &child, dir.entryList(QDir::AllDirs)) { - if(child == "." || child == "..") continue; - return removeEmptyTree(dir.absoluteFilePath(child)); - } - const QString dir_name = dir.dirName(); - if(dir.cdUp()) { - return dir.rmdir(dir_name); - } - return false; -} - QString misc::bcLinkToMagnet(QString bc_link) { QByteArray raw_bc = bc_link.toUtf8(); raw_bc = raw_bc.mid(8); // skip bc://bt/ @@ -759,3 +746,9 @@ bool misc::isValidTorrentFile(const QString &torrent_path) { } return true; } + +QString misc::branchPath(QString file_path) +{ + file_path.replace("\\", "/"); + return file_path.left(file_path.lastIndexOf('/')); +} diff --git a/src/misc.h b/src/misc.h index b62e91f05..25c60695c 100644 --- a/src/misc.h +++ b/src/misc.h @@ -179,7 +179,7 @@ public: // value must be given in bytes static QString friendlyUnit(double val); static bool isPreviewable(QString extension); - static bool removeEmptyTree(QString path); + static QString branchPath(QString file_path); static QString magnetUriToName(QString magnet_uri); static QString magnetUriToHash(QString magnet_uri); static QString bcLinkToMagnet(QString bc_link); diff --git a/src/properties/propertieswidget.cpp b/src/properties/propertieswidget.cpp index c55441432..748c1526f 100644 --- a/src/properties/propertieswidget.cpp +++ b/src/properties/propertieswidget.cpp @@ -640,7 +640,8 @@ void PropertiesWidget::renameSelectedFile() { // Remove old folder const QDir old_folder(h.save_path()+"/"+old_path); int timeout = 10; - while(!misc::removeEmptyTree(old_folder.absolutePath()) && timeout > 0) { + while(!QDir().rmpath(old_folder.absolutePath()) && timeout > 0) { + // XXX: We should not sleep here (freezes the UI for 1 second) SleeperThread::msleep(100); --timeout; } diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index f8a7046e6..130550d54 100644 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/src/qtlibtorrent/qbtsession.cpp @@ -281,7 +281,7 @@ void QBtSession::configureSession() { int i = 0; foreach (const QString &dir, scan_dirs) { qDebug() << "Adding scan dir" << dir << downloadInDirList.at(i); - ScanFoldersModel::PathStatus ret = m_scanFolders->addPath(dir, downloadInDirList.at(i)); + m_scanFolders->addPath(dir, downloadInDirList.at(i)); ++i; } // * Export Dir @@ -672,6 +672,9 @@ void QBtSession::deleteTorrent(QString hash, bool delete_local_files) { foreach(const QString &uneeded_file, uneeded_files) { qDebug("Removing uneeded file: %s", qPrintable(uneeded_file)); misc::safeRemove(uneeded_file); + const QString parent_folder = misc::branchPath(uneeded_file); + qDebug("Attempt to remove parent folder (if empty): %s", qPrintable(parent_folder)); + QDir().rmpath(parent_folder); } } // Remove it from torrent backup directory @@ -2037,14 +2040,14 @@ void QBtSession::readAlerts() { #endif if(!hash.isEmpty()) { if(savePathsToRemove.contains(hash)) { - misc::removeEmptyTree(savePathsToRemove.take(hash)); + QDir().rmpath(savePathsToRemove.take(hash)); } } else { // XXX: Fallback QStringList hashes_deleted; foreach(const QString& key, savePathsToRemove.keys()) { // Attempt to delete - misc::removeEmptyTree(savePathsToRemove[key]); + QDir().rmpath(savePathsToRemove[key]); if(!QDir(savePathsToRemove[key]).exists()) { hashes_deleted << key; } @@ -2065,7 +2068,7 @@ void QBtSession::readAlerts() { QDir old_save_dir(old_save_path); if(old_save_dir != QDir(defaultSavePath) && old_save_dir != QDir(defaultTempPath)) { qDebug("Attempting to remove %s", qPrintable(old_save_path)); - misc::removeEmptyTree(old_save_path); + QDir().rmpath(old_save_path); } if(defaultTempPath.isEmpty() || !new_save_path.startsWith(defaultTempPath)) { qDebug("Storage has been moved, updating save path to %s", qPrintable(new_save_path));