From c420ff29afd82bc815ef6273af36d1368f7d5f0d Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 7 Aug 2019 18:22:19 +0800 Subject: [PATCH] Revise operations in TorrentHandle class --- src/base/bittorrent/torrenthandle.cpp | 93 ++++++++++++++------------- src/base/bittorrent/torrenthandle.h | 2 - 2 files changed, 48 insertions(+), 47 deletions(-) diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index 6d512fa6b..bb75083b6 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -104,6 +104,8 @@ namespace std::vector toLTDownloadPriorities(const QVector &priorities) { std::vector out; + out.reserve(priorities.size()); + std::transform(priorities.cbegin(), priorities.cend() , std::back_inserter(out), [](BitTorrent::DownloadPriority priority) { @@ -264,16 +266,17 @@ InfoHash TorrentHandle::hash() const QString TorrentHandle::name() const { QString name = m_name; - if (name.isEmpty()) - name = QString::fromStdString(m_nativeStatus.name); + if (!name.isEmpty()) return name; - if (name.isEmpty() && hasMetadata()) - name = QString::fromStdString(m_torrentInfo.nativeInfo()->orig_files().name()); + name = QString::fromStdString(m_nativeStatus.name); + if (!name.isEmpty()) return name; - if (name.isEmpty()) - name = m_hash; + if (hasMetadata()) { + name = QString::fromStdString(m_torrentInfo.nativeInfo()->orig_files().name()); + if (!name.isEmpty()) return name; + } - return name; + return m_hash; } QDateTime TorrentHandle::creationDate() const @@ -353,14 +356,15 @@ QString TorrentHandle::rootPath(bool actual) const return QDir(savePath(actual)).absoluteFilePath(firstFilePath); } -QString TorrentHandle::contentPath(bool actual) const +QString TorrentHandle::contentPath(const bool actual) const { if (filesCount() == 1) return QDir(savePath(actual)).absoluteFilePath(filePath(0)); - else if (hasRootFolder()) + + if (hasRootFolder()) return rootPath(actual); - else - return savePath(actual); + + return savePath(actual); } bool TorrentHandle::isAutoTMMEnabled() const @@ -412,12 +416,14 @@ void TorrentHandle::setAutoManaged(const bool enable) QVector TorrentHandle::trackers() const { - const std::vector announces = m_nativeHandle.trackers(); + const std::vector nativeTrackers = m_nativeHandle.trackers(); QVector entries; - entries.reserve(announces.size()); - for (const lt::announce_entry &tracker : announces) + entries.reserve(nativeTrackers.size()); + + for (const lt::announce_entry &tracker : nativeTrackers) entries << tracker; + return entries; } @@ -428,7 +434,9 @@ QHash TorrentHandle::trackerInfos() const void TorrentHandle::addTrackers(const QVector &trackers) { - const QVector currentTrackers = this->trackers(); + QSet currentTrackers; + for (const lt::announce_entry &entry : m_nativeHandle.trackers()) + currentTrackers << entry; QVector newTrackers; newTrackers.reserve(trackers.size()); @@ -451,16 +459,17 @@ void TorrentHandle::replaceTrackers(const QVector &trackers) QVector newTrackers; newTrackers.reserve(trackers.size()); - std::vector announces; + std::vector nativeTrackers; + nativeTrackers.reserve(trackers.size()); for (const TrackerEntry &tracker : trackers) { - announces.emplace_back(tracker.nativeEntry()); + nativeTrackers.emplace_back(tracker.nativeEntry()); if (!currentTrackers.removeOne(tracker)) newTrackers << tracker; } - m_nativeHandle.replace_trackers(announces); + m_nativeHandle.replace_trackers(nativeTrackers); if (newTrackers.isEmpty() && currentTrackers.isEmpty()) { // when existing tracker reorders @@ -477,12 +486,12 @@ void TorrentHandle::replaceTrackers(const QVector &trackers) QVector TorrentHandle::urlSeeds() const { - const std::set seeds = m_nativeHandle.url_seeds(); + const std::set currentSeeds = m_nativeHandle.url_seeds(); QVector urlSeeds; - urlSeeds.reserve(seeds.size()); + urlSeeds.reserve(currentSeeds.size()); - for (const std::string &urlSeed : seeds) + for (const std::string &urlSeed : currentSeeds) urlSeeds.append(QUrl(urlSeed.c_str())); return urlSeeds; @@ -490,11 +499,17 @@ QVector TorrentHandle::urlSeeds() const void TorrentHandle::addUrlSeeds(const QVector &urlSeeds) { + const std::set currentSeeds = m_nativeHandle.url_seeds(); + QVector addedUrlSeeds; addedUrlSeeds.reserve(urlSeeds.size()); - for (const QUrl &urlSeed : urlSeeds) { - if (addUrlSeed(urlSeed)) - addedUrlSeeds << urlSeed; + + for (const QUrl &url : urlSeeds) { + const std::string nativeUrl = url.toString().toStdString(); + if (currentSeeds.find(nativeUrl) == currentSeeds.end()) { + m_nativeHandle.add_url_seed(nativeUrl); + addedUrlSeeds << url; + } } if (!addedUrlSeeds.isEmpty()) @@ -503,35 +518,23 @@ void TorrentHandle::addUrlSeeds(const QVector &urlSeeds) void TorrentHandle::removeUrlSeeds(const QVector &urlSeeds) { + const std::set currentSeeds = m_nativeHandle.url_seeds(); + QVector removedUrlSeeds; removedUrlSeeds.reserve(urlSeeds.size()); - for (const QUrl &urlSeed : urlSeeds) { - if (removeUrlSeed(urlSeed)) - removedUrlSeeds << urlSeed; + + for (const QUrl &url : urlSeeds) { + const std::string nativeUrl = url.toString().toStdString(); + if (currentSeeds.find(nativeUrl) != currentSeeds.end()) { + m_nativeHandle.remove_url_seed(nativeUrl); + removedUrlSeeds << url; + } } if (!removedUrlSeeds.isEmpty()) m_session->handleTorrentUrlSeedsRemoved(this, removedUrlSeeds); } -bool TorrentHandle::addUrlSeed(const QUrl &urlSeed) -{ - const QVector seeds = urlSeeds(); - if (seeds.contains(urlSeed)) return false; - - m_nativeHandle.add_url_seed(urlSeed.toString().toStdString()); - return true; -} - -bool TorrentHandle::removeUrlSeed(const QUrl &urlSeed) -{ - const QVector seeds = urlSeeds(); - if (!seeds.contains(urlSeed)) return false; - - m_nativeHandle.remove_url_seed(urlSeed.toString().toStdString()); - return true; -} - bool TorrentHandle::connectPeer(const PeerAddress &peerAddress) { lt::error_code ec; diff --git a/src/base/bittorrent/torrenthandle.h b/src/base/bittorrent/torrenthandle.h index 77829d559..7443024be 100644 --- a/src/base/bittorrent/torrenthandle.h +++ b/src/base/bittorrent/torrenthandle.h @@ -394,8 +394,6 @@ namespace BitTorrent void move_impl(QString path, bool overwrite); void moveStorage(const QString &newPath, bool overwrite); void manageIncompleteFiles(); - bool addUrlSeed(const QUrl &urlSeed); - bool removeUrlSeed(const QUrl &urlSeed); void setFirstLastPiecePriorityImpl(bool enabled, const QVector &updatedFilePrio = {}); Session *const m_session;