From 0afa4d260c9213e4b64501ceca9dff42168a9a26 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Sun, 16 Aug 2015 20:03:32 +0300 Subject: [PATCH] Fix 'Append label to save path'. Closes #3495. --- .../bittorrent/private/torrenthandleprivate.h | 1 - src/core/bittorrent/session.cpp | 40 +++++++++++++----- src/core/bittorrent/torrenthandle.cpp | 42 +------------------ src/core/bittorrent/torrenthandle.h | 3 -- 4 files changed, 31 insertions(+), 55 deletions(-) diff --git a/src/core/bittorrent/private/torrenthandleprivate.h b/src/core/bittorrent/private/torrenthandleprivate.h index db19933be..907a28a23 100644 --- a/src/core/bittorrent/private/torrenthandleprivate.h +++ b/src/core/bittorrent/private/torrenthandleprivate.h @@ -39,7 +39,6 @@ struct TorrentHandlePrivate { virtual void handleAlert(libtorrent::alert *) = 0; virtual void handleStateUpdate(const libtorrent::torrent_status &) = 0; - virtual void handleDefaultSavePathChanged() = 0; virtual void handleTempPathChanged() = 0; virtual void handleAppendExtensionToggled() = 0; diff --git a/src/core/bittorrent/session.cpp b/src/core/bittorrent/session.cpp index ad64966e0..36d0d510a 100644 --- a/src/core/bittorrent/session.cpp +++ b/src/core/bittorrent/session.cpp @@ -1412,14 +1412,9 @@ void Session::setDefaultSavePath(const QString &path) { if (path.isEmpty()) return; - QString defaultSavePath = Utils::Fs::fromNativePath(path); - if (!defaultSavePath.endsWith("/")) - defaultSavePath += "/"; - if (m_defaultSavePath != defaultSavePath) { - m_defaultSavePath = defaultSavePath; - foreach (TorrentHandlePrivate *const torrent, m_torrents) - torrent->handleDefaultSavePathChanged(); - } + m_defaultSavePath = Utils::Fs::fromNativePath(path); + if (!m_defaultSavePath.endsWith("/")) + m_defaultSavePath += "/"; } void Session::setDefaultTempPath(const QString &path) @@ -1443,8 +1438,20 @@ void Session::setAppendLabelToSavePath(bool append) { if (m_appendLabelToSavePath != append) { m_appendLabelToSavePath = append; - foreach (TorrentHandlePrivate *const torrent, m_torrents) - torrent->handleDefaultSavePathChanged(); + foreach (TorrentHandle *const torrent, m_torrents) { + QString label = torrent->label(); + if (label.isEmpty()) continue; + + QString testedOldSavePath = m_defaultSavePath; + QString newSavePath = m_defaultSavePath; + if (!m_appendLabelToSavePath) + testedOldSavePath += QString("%1/").arg(label); + else + newSavePath += QString("%1/").arg(label); + + if (torrent->savePath() == testedOldSavePath) + torrent->move(newSavePath); + } } } @@ -1597,6 +1604,19 @@ void Session::handleTorrentSavePathChanged(TorrentHandle *const torrent) void Session::handleTorrentLabelChanged(TorrentHandle *const torrent, const QString &oldLabel) { + if (m_appendLabelToSavePath) { + QString testedOldSavePath = m_defaultSavePath; + if (!oldLabel.isEmpty()) + testedOldSavePath += QString("%1/").arg(oldLabel); + QString newLabel = torrent->label(); + if (torrent->savePath() == testedOldSavePath) { + QString newSavePath = m_defaultSavePath; + if (!newLabel.isEmpty()) + newSavePath += QString("%1/").arg(newLabel); + torrent->move(newSavePath); + } + } + emit torrentLabelChanged(torrent, oldLabel); } diff --git a/src/core/bittorrent/torrenthandle.cpp b/src/core/bittorrent/torrenthandle.cpp index 4aa12cf5c..a72d33d64 100644 --- a/src/core/bittorrent/torrenthandle.cpp +++ b/src/core/bittorrent/torrenthandle.cpp @@ -199,7 +199,6 @@ TorrentHandle::TorrentHandle(Session *session, const libtorrent::torrent_handle , m_ratioLimit(data.ratioLimit) , m_tempPathDisabled(data.disableTempPath) , m_hasMissingFiles(false) - , m_useDefaultSavePath(false) , m_pauseAfterRecheck(false) , m_needSaveResumeData(false) { @@ -1119,7 +1118,6 @@ void TorrentHandle::setLabel(const QString &label) QString oldLabel = m_label; m_label = label; m_needSaveResumeData = true; - adjustSavePath(); m_session->handleTorrentLabelChanged(this, oldLabel); } } @@ -1131,10 +1129,6 @@ void TorrentHandle::setSequentialDownload(bool b) void TorrentHandle::move(QString path) { - // now we use non-default save path - // even if new path same as default. - m_useDefaultSavePath = false; - path = Utils::Fs::toNativePath(path); if (path == savePath()) return; @@ -1463,7 +1457,7 @@ void TorrentHandle::handleSaveResumeDataAlert(libtorrent::save_resume_data_alert resumeData["qBt-forced"] = isForced(); } resumeData["qBt-addedTime"] = m_addedTime.toTime_t(); - resumeData["qBt-savePath"] = m_useDefaultSavePath ? "" : Utils::String::toStdString(m_savePath); + resumeData["qBt-savePath"] = Utils::String::toStdString(m_savePath); resumeData["qBt-ratioLimit"] = Utils::String::toStdString(QString::number(m_ratioLimit)); resumeData["qBt-label"] = Utils::String::toStdString(m_label); resumeData["qBt-name"] = Utils::String::toStdString(m_name); @@ -1586,32 +1580,6 @@ void TorrentHandle::handleMetadataReceivedAlert(libt::metadata_received_alert *p } } -void TorrentHandle::handleDefaultSavePathChanged() -{ - adjustSavePath(); -} - -void TorrentHandle::adjustSavePath() -{ - // If we use default save path... - if (m_useDefaultSavePath) { - QString defaultSavePath = m_session->defaultSavePath(); - if (m_session->useAppendLabelToSavePath() && !m_label.isEmpty()) - defaultSavePath += QString("%1/").arg(m_label); - defaultSavePath = Utils::Fs::toNativePath(defaultSavePath); - if (m_savePath != defaultSavePath) { - if (!useTempPath()) { - moveStorage(defaultSavePath); - } - else { - m_savePath = defaultSavePath; - m_needSaveResumeData = true; - m_session->handleTorrentSavePathChanged(this); - } - } - } -} - void TorrentHandle::handleTempPathChanged() { adjustActualSavePath(); @@ -1749,15 +1717,7 @@ void TorrentHandle::updateTorrentInfo() void TorrentHandle::initialize() { updateStatus(); - m_hash = InfoHash(m_nativeStatus.info_hash); - if (m_savePath.isEmpty()) { - // we use default save path - m_savePath = nativeActualSavePath(); - m_useDefaultSavePath = true; - } - - adjustSavePath(); adjustActualSavePath(); } diff --git a/src/core/bittorrent/torrenthandle.h b/src/core/bittorrent/torrenthandle.h index 4a2ec204a..b8d6689f6 100644 --- a/src/core/bittorrent/torrenthandle.h +++ b/src/core/bittorrent/torrenthandle.h @@ -310,7 +310,6 @@ namespace BitTorrent void handleAlert(libtorrent::alert *a); void handleStateUpdate(const libtorrent::torrent_status &nativeStatus); - void handleDefaultSavePathChanged(); void handleTempPathChanged(); void handleAppendExtensionToggled(); @@ -336,7 +335,6 @@ namespace BitTorrent bool useTempPath() const; QString nativeActualSavePath() const; - void adjustSavePath(); void adjustActualSavePath(); void adjustActualSavePath_impl(); void moveStorage(const QString &newPath); @@ -375,7 +373,6 @@ namespace BitTorrent bool m_tempPathDisabled; bool m_hasMissingFiles; - bool m_useDefaultSavePath; bool m_pauseAfterRecheck; bool m_needSaveResumeData; QHash m_trackerInfos;