From 034bd83915c51a89db670418f5ac8589b972e10d Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Fri, 1 Jan 2016 16:28:40 +0300 Subject: [PATCH] Manage save path in one place --- src/base/bittorrent/session.cpp | 112 +++++++++++--------------- src/base/bittorrent/session.h | 12 +-- src/base/bittorrent/torrenthandle.cpp | 40 ++++++--- src/base/bittorrent/torrenthandle.h | 8 +- 4 files changed, 82 insertions(+), 90 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 712e044e5..3ee7322d4 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -100,16 +100,6 @@ static void torrentQueuePositionDown(const libt::torrent_handle &handle); static void torrentQueuePositionTop(const libt::torrent_handle &handle); static void torrentQueuePositionBottom(const libt::torrent_handle &handle); -// AddTorrentParams - -AddTorrentParams::AddTorrentParams() - : disableTempPath(false) - , sequential(false) - , ignoreShareRatio(false) - , skipChecking(false) -{ -} - // Session Session *Session::m_instance = 0; @@ -713,7 +703,7 @@ void Session::handleDownloadFailed(const QString &url, const QString &reason) void Session::handleRedirectedToMagnet(const QString &url, const QString &magnetUri) { - addTorrent_impl(addDataFromParams(m_downloadedTorrents.take(url)), MagnetUri(magnetUri)); + addTorrent_impl(m_downloadedTorrents.take(url), MagnetUri(magnetUri)); } void Session::switchToAlternativeMode(bool alternative) @@ -725,7 +715,7 @@ void Session::switchToAlternativeMode(bool alternative) void Session::handleDownloadFinished(const QString &url, const QString &filePath) { emit downloadFromUrlFinished(url); - addTorrent_impl(addDataFromParams(m_downloadedTorrents.take(url)), MagnetUri(), TorrentInfo::loadFromFile(filePath)); + addTorrent_impl(m_downloadedTorrents.take(url), MagnetUri(), TorrentInfo::loadFromFile(filePath)); Utils::Fs::forceRemove(filePath); // remove temporary file } @@ -939,30 +929,9 @@ TorrentStatusReport Session::torrentStatusReport() const return m_torrentStatusReport; } -// source - .torrent file path/url or magnet uri (hash for preloaded torrent) +// source - .torrent file path/url or magnet uri bool Session::addTorrent(QString source, const AddTorrentParams ¶ms) { - InfoHash hash = source; - if (hash.isValid() && m_loadedMetadata.contains(hash)) { - // Adding preloaded torrent - m_loadedMetadata.remove(hash); - libt::torrent_handle handle = m_nativeSession->find_torrent(hash); - --m_extraLimit; - - try { - handle.auto_managed(false); - handle.pause(); - } - catch (std::exception &) {} - - adjustLimits(); - - // use common 2nd step of torrent addition - m_addingTorrents.insert(hash, addDataFromParams(params)); - createTorrentHandle(handle); - return true; - } - MagnetUri magnetUri(source); if (magnetUri.isValid()) { return addTorrent_impl(params, magnetUri); @@ -977,7 +946,7 @@ bool Session::addTorrent(QString source, const AddTorrentParams ¶ms) m_downloadedTorrents[handler->url()] = params; } else { - return addTorrent_impl(addDataFromParams(params), MagnetUri(), TorrentInfo::loadFromFile(source)); + return addTorrent_impl(params, MagnetUri(), TorrentInfo::loadFromFile(source)); } return false; @@ -987,13 +956,26 @@ bool Session::addTorrent(const TorrentInfo &torrentInfo, const AddTorrentParams { if (!torrentInfo.isValid()) return false; - return addTorrent_impl(addDataFromParams(params), MagnetUri(), torrentInfo); + return addTorrent_impl(params, MagnetUri(), torrentInfo); } // Add a torrent to the BitTorrent session bool Session::addTorrent_impl(AddTorrentData addData, const MagnetUri &magnetUri, const TorrentInfo &torrentInfo, const QByteArray &fastresumeData) { + if (!addData.resumed) { + // manage save path + QString defaultSavePath = this->defaultSavePath(); + if (addData.savePath.isEmpty()) + addData.savePath = defaultSavePath; + if (!addData.savePath.endsWith("/")) + addData.savePath += "/"; + if (useAppendLabelToSavePath()) { + if ((addData.savePath == defaultSavePath) && !addData.label.isEmpty()) + addData.savePath += QString("%1/").arg(addData.label); + } + } + libt::add_torrent_params p; InfoHash hash; std::vector buf(fastresumeData.constData(), fastresumeData.constData() + fastresumeData.size()); @@ -1001,8 +983,29 @@ bool Session::addTorrent_impl(AddTorrentData addData, const MagnetUri &magnetUri bool fromMagnetUri = magnetUri.isValid(); if (fromMagnetUri) { - p = magnetUri.addTorrentParams(); hash = magnetUri.hash(); + + if (m_loadedMetadata.contains(hash)) { + // Adding preloaded torrent + m_loadedMetadata.remove(hash); + libt::torrent_handle handle = m_nativeSession->find_torrent(hash); + --m_extraLimit; + + try { + handle.auto_managed(false); + handle.pause(); + } + catch (std::exception &) {} + + adjustLimits(); + + // use common 2nd step of torrent addition + m_addingTorrents.insert(hash, addData); + createTorrentHandle(handle); + return true; + } + + p = magnetUri.addTorrentParams(); } else if (torrentInfo.isValid()) { // Metadata @@ -2347,6 +2350,11 @@ bool loadTorrentResumeData(const QByteArray &data, AddTorrentData &out, MagnetUr if (ec || (fast.type() != libt::lazy_entry::dict_t)) return false; out.savePath = Utils::Fs::fromNativePath(Utils::String::fromStdString(fast.dict_find_string_value("qBt-savePath"))); + if (out.savePath.isEmpty()) { + Logger::instance()->addMessage("Empty torrent save path was loaded from .fastresume file! Using default one...", Log::WARNING); + out.savePath = Preferences::instance()->getSavePath(); + } + out.ratioLimit = Utils::String::fromStdString(fast.dict_find_string_value("qBt-ratioLimit")).toDouble(); out.label = Utils::String::fromStdString(fast.dict_find_string_value("qBt-label")); out.name = Utils::String::fromStdString(fast.dict_find_string_value("qBt-name")); @@ -2421,33 +2429,3 @@ void torrentQueuePositionBottom(const libt::torrent_handle &handle) qDebug() << Q_FUNC_INFO << " fails: " << exc.what(); } } - -AddTorrentData Session::addDataFromParams(const AddTorrentParams ¶ms) -{ - AddTorrentData data; - - data.resumed = false; - data.name = params.name; - data.label = params.label; - data.savePath = params.savePath; - data.disableTempPath = params.disableTempPath; - data.sequential = params.sequential; - data.hasSeedStatus = params.skipChecking; // do not react on 'torrent_finished_alert' when skipping - data.skipChecking = params.skipChecking; - data.addForced = params.addForced; - data.addPaused = params.addPaused; - data.filePriorities = params.filePriorities; - data.ratioLimit = params.ignoreShareRatio ? TorrentHandle::NO_RATIO_LIMIT : TorrentHandle::USE_GLOBAL_RATIO; - - // normalize save path - if (data.savePath.isEmpty()) { - data.savePath = m_defaultSavePath; - if (m_appendLabelToSavePath && !data.label.isEmpty()) - data.savePath += QString("%1/").arg(data.label); - } - else if (!data.savePath.endsWith("/")) { - data.savePath += "/"; - } - - return data; -} diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index e86ab8c59..0289d335b 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -127,15 +127,13 @@ namespace BitTorrent QString name; QString label; QString savePath; - bool disableTempPath; // e.g. for imported torrents - bool sequential; + bool disableTempPath = false; // e.g. for imported torrents + bool sequential = false; TriStateBool addForced; TriStateBool addPaused; QVector filePriorities; // used if TorrentInfo is set - bool ignoreShareRatio; - bool skipChecking; - - AddTorrentParams(); + bool ignoreShareRatio = false; + bool skipChecking = false; }; struct TorrentStatusReport @@ -334,8 +332,6 @@ namespace BitTorrent void dispatchAlerts(std::auto_ptr alertPtr); void getPendingAlerts(QVector &out, ulong time = 0); - AddTorrentData addDataFromParams(const AddTorrentParams ¶ms); - // BitTorrent libtorrent::session *m_nativeSession; diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index 4707b5f77..caac99d20 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -65,10 +65,31 @@ static const char QB_EXT[] = ".!qB"; namespace libt = libtorrent; using namespace BitTorrent; -// TrackerInfo +// AddTorrentData -TrackerInfo::TrackerInfo() - : numPeers(0) +AddTorrentData::AddTorrentData() + : resumed(false) + , disableTempPath(false) + , sequential(false) + , hasSeedStatus(false) + , skipChecking(false) + , ratioLimit(TorrentHandle::USE_GLOBAL_RATIO) +{ +} + +AddTorrentData::AddTorrentData(const AddTorrentParams ¶ms) + : resumed(false) + , name(params.name) + , label(params.label) + , savePath(params.savePath) + , disableTempPath(params.disableTempPath) + , sequential(params.sequential) + , hasSeedStatus(params.skipChecking) // do not react on 'torrent_finished_alert' when skipping + , skipChecking(params.skipChecking) + , addForced(params.addForced) + , addPaused(params.addPaused) + , filePriorities(params.filePriorities) + , ratioLimit(params.ignoreShareRatio ? TorrentHandle::NO_RATIO_LIMIT : TorrentHandle::USE_GLOBAL_RATIO) { } @@ -188,7 +209,11 @@ TorrentHandle::TorrentHandle(Session *session, const libtorrent::torrent_handle , m_pauseAfterRecheck(false) , m_needSaveResumeData(false) { - initialize(); + Q_ASSERT(!m_savePath.isEmpty()); + + updateStatus(); + m_hash = InfoHash(m_nativeStatus.info_hash); + adjustActualSavePath(); if (!data.resumed) { setSequentialDownload(data.sequential); @@ -1704,13 +1729,6 @@ void TorrentHandle::updateTorrentInfo() #endif } -void TorrentHandle::initialize() -{ - updateStatus(); - m_hash = InfoHash(m_nativeStatus.info_hash); - adjustActualSavePath(); -} - bool TorrentHandle::isMoveInProgress() const { return !m_newPath.isEmpty(); diff --git a/src/base/bittorrent/torrenthandle.h b/src/base/bittorrent/torrenthandle.h index 882cdb029..0f1ee3fce 100644 --- a/src/base/bittorrent/torrenthandle.h +++ b/src/base/bittorrent/torrenthandle.h @@ -102,14 +102,15 @@ namespace BitTorrent QVector filePriorities; // for resumed torrents qreal ratioLimit; + + AddTorrentData(); + AddTorrentData(const AddTorrentParams ¶ms); }; struct TrackerInfo { QString lastMessage; - quint32 numPeers; - - TrackerInfo(); + quint32 numPeers = 0; }; class TorrentState @@ -349,7 +350,6 @@ namespace BitTorrent private: typedef boost::function EventTrigger; - void initialize(); void updateStatus(); void updateStatus(const libtorrent::torrent_status &nativeStatus); void updateState();