Browse Source

Remove empty folders on torrent soft deletion

Code clean up
adaptive-webui-19844
Christophe Dumez 14 years ago
parent
commit
cc4e1c5bbe
  1. 19
      src/misc.cpp
  2. 2
      src/misc.h
  3. 3
      src/properties/propertieswidget.cpp
  4. 11
      src/qtlibtorrent/qbtsession.cpp

19
src/misc.cpp

@ -557,19 +557,6 @@ bool misc::isPreviewable(QString extension){ @@ -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) { @@ -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('/'));
}

2
src/misc.h

@ -179,7 +179,7 @@ public: @@ -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);

3
src/properties/propertieswidget.cpp

@ -640,7 +640,8 @@ void PropertiesWidget::renameSelectedFile() { @@ -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;
}

11
src/qtlibtorrent/qbtsession.cpp

@ -281,7 +281,7 @@ void QBtSession::configureSession() { @@ -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) { @@ -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() { @@ -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() { @@ -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));

Loading…
Cancel
Save