From 1052cd019bcd9afee72c842b510dfda0311d9cfa Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sat, 10 Apr 2010 14:17:26 +0000 Subject: [PATCH] Better fix for FS changes on torrent labeling --- src/bittorrent.cpp | 47 +++++++++++++--------------------------------- src/misc.cpp | 24 +++++++++++++++++++++++ src/misc.h | 2 ++ 3 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index a749d37f6..06432d317 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -1637,33 +1637,16 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { void Bittorrent::changeLabelInTorrentSavePath(QTorrentHandle h, QString old_label, QString new_label) { if(!h.is_valid()) return; if(!appendLabelToSavePath) return; - if(old_label == new_label) return; QString old_save_path = TorrentPersistentData::getSavePath(h.hash()); if(!old_save_path.startsWith(defaultSavePath)) return; - QString new_save_path = old_save_path.replace(defaultSavePath, ""); - QStringList path_parts = new_save_path.split(QDir::separator(), QString::SkipEmptyParts); - if(path_parts.empty()) { - if(!new_label.isEmpty()) - path_parts << new_label; - } else { - if(old_label.isEmpty() || path_parts.first() != old_label) { - path_parts.prepend(new_label); - } else { - if(new_label.isEmpty()) - path_parts.removeAt(0); - else - path_parts.replace(0, new_label); - } + QString new_save_path = misc::updateLabelInSavePath(defaultSavePath, old_save_path, old_label, new_label); + if(new_save_path != old_save_path) { + // Move storage + qDebug("Moving storage to %s", qPrintable(new_save_path)); + QDir().mkpath(new_save_path); + h.move_storage(new_save_path); + emit savePathChanged(h); } - new_save_path = defaultSavePath; - if(!new_save_path.endsWith(QDir::separator())) new_save_path += QDir::separator(); - new_save_path += path_parts.join(QDir::separator()); - TorrentPersistentData::saveSavePath(h.hash(), new_save_path); - // Move storage - qDebug("Moving storage to %s", qPrintable(new_save_path)); - QDir().mkpath(new_save_path); - h.move_storage(new_save_path); - emit savePathChanged(h); } void Bittorrent::appendLabelToTorrentSavePath(QTorrentHandle h) { @@ -1671,15 +1654,11 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { const QString &label = TorrentPersistentData::getLabel(h.hash()); if(label.isEmpty()) return; // Current save path - const QString &old_save_path = TorrentPersistentData::getSavePath(h.hash()); - const QDir old_dir(old_save_path); - if(old_dir.dirName() != label) { - const QString &new_save_path = old_dir.absoluteFilePath(label); - TorrentPersistentData::saveSavePath(h.hash(), new_save_path); - if(old_dir == QDir(h.save_path())) { - // Move storage - h.move_storage(new_save_path); - } + QString old_save_path = TorrentPersistentData::getSavePath(h.hash()); + QString new_save_path = misc::updateLabelInSavePath(defaultSavePath, old_save_path, "", label); + if(old_save_path != new_save_path) { + // Move storage + h.move_storage(new_save_path); emit savePathChanged(h); } } @@ -2198,7 +2177,7 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { if(savePath.isEmpty()) { savePath = defaultSavePath; } - if(appendLabelToSavePath) { + if(appendLabelToSavePath && savePath.startsWith(defaultSavePath)) { qDebug("appendLabelToSavePath is true"); const QString &label = TorrentTempData::getLabel(hash); if(!label.isEmpty()) { diff --git a/src/misc.cpp b/src/misc.cpp index 89bad3a74..c9d5fedfa 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -248,6 +248,30 @@ void misc::copyDir(QString src_path, QString dst_path) { } } +QString misc::updateLabelInSavePath(const QString& defaultSavePath, QString save_path, const QString old_label, const QString new_label) { + if(old_label == new_label) return save_path; + if(!save_path.startsWith(defaultSavePath)) return save_path; + QString new_save_path = save_path.replace(defaultSavePath, ""); + QStringList path_parts = new_save_path.split(QDir::separator(), QString::SkipEmptyParts); + if(path_parts.empty()) { + if(!new_label.isEmpty()) + path_parts << new_label; + } else { + if(old_label.isEmpty() || path_parts.first() != old_label) { + path_parts.prepend(new_label); + } else { + if(new_label.isEmpty()) + path_parts.removeAt(0); + else + path_parts.replace(0, new_label); + } + } + new_save_path = defaultSavePath; + if(!new_save_path.endsWith(QDir::separator())) new_save_path += QDir::separator(); + new_save_path += path_parts.join(QDir::separator()); + return new_save_path; +} + void misc::moveToXDGFolders() { const QString &old_qBtPath = QDir::homePath()+QDir::separator()+QString::fromUtf8(".qbittorrent") + QDir::separator(); if(QDir(old_qBtPath).exists()) { diff --git a/src/misc.h b/src/misc.h index 28c4ea25e..e5b3f8119 100644 --- a/src/misc.h +++ b/src/misc.h @@ -73,6 +73,8 @@ public: static QString truncateRootFolder(boost::intrusive_ptr t); static QString truncateRootFolder(torrent_handle h); + static QString updateLabelInSavePath(const QString& defaultSavePath, QString save_path, const QString old_label, const QString new_label); + static bool sameFiles(QString path1, QString path2); static void copyDir(QString src_path, QString dst_path); // Introduced in v2.1.0 for backward compatibility