Revise operations in TorrentHandle class

This commit is contained in:
Chocobo1 2019-08-07 18:22:19 +08:00
parent b5b678c58f
commit c420ff29af
No known key found for this signature in database
GPG key ID: 210D9C873253A68C
2 changed files with 48 additions and 47 deletions

View file

@ -104,6 +104,8 @@ namespace
std::vector<LTDownloadPriority> toLTDownloadPriorities(const QVector<DownloadPriority> &priorities) std::vector<LTDownloadPriority> toLTDownloadPriorities(const QVector<DownloadPriority> &priorities)
{ {
std::vector<LTDownloadPriority> out; std::vector<LTDownloadPriority> out;
out.reserve(priorities.size());
std::transform(priorities.cbegin(), priorities.cend() std::transform(priorities.cbegin(), priorities.cend()
, std::back_inserter(out), [](BitTorrent::DownloadPriority priority) , std::back_inserter(out), [](BitTorrent::DownloadPriority priority)
{ {
@ -264,16 +266,17 @@ InfoHash TorrentHandle::hash() const
QString TorrentHandle::name() const QString TorrentHandle::name() const
{ {
QString name = m_name; QString name = m_name;
if (name.isEmpty()) if (!name.isEmpty()) return name;
name = QString::fromStdString(m_nativeStatus.name);
if (name.isEmpty() && hasMetadata()) name = QString::fromStdString(m_nativeStatus.name);
if (!name.isEmpty()) return name;
if (hasMetadata()) {
name = QString::fromStdString(m_torrentInfo.nativeInfo()->orig_files().name()); name = QString::fromStdString(m_torrentInfo.nativeInfo()->orig_files().name());
if (!name.isEmpty()) return name;
}
if (name.isEmpty()) return m_hash;
name = m_hash;
return name;
} }
QDateTime TorrentHandle::creationDate() const QDateTime TorrentHandle::creationDate() const
@ -353,14 +356,15 @@ QString TorrentHandle::rootPath(bool actual) const
return QDir(savePath(actual)).absoluteFilePath(firstFilePath); return QDir(savePath(actual)).absoluteFilePath(firstFilePath);
} }
QString TorrentHandle::contentPath(bool actual) const QString TorrentHandle::contentPath(const bool actual) const
{ {
if (filesCount() == 1) if (filesCount() == 1)
return QDir(savePath(actual)).absoluteFilePath(filePath(0)); return QDir(savePath(actual)).absoluteFilePath(filePath(0));
else if (hasRootFolder())
if (hasRootFolder())
return rootPath(actual); return rootPath(actual);
else
return savePath(actual); return savePath(actual);
} }
bool TorrentHandle::isAutoTMMEnabled() const bool TorrentHandle::isAutoTMMEnabled() const
@ -412,12 +416,14 @@ void TorrentHandle::setAutoManaged(const bool enable)
QVector<TrackerEntry> TorrentHandle::trackers() const QVector<TrackerEntry> TorrentHandle::trackers() const
{ {
const std::vector<lt::announce_entry> announces = m_nativeHandle.trackers(); const std::vector<lt::announce_entry> nativeTrackers = m_nativeHandle.trackers();
QVector<TrackerEntry> entries; QVector<TrackerEntry> entries;
entries.reserve(announces.size()); entries.reserve(nativeTrackers.size());
for (const lt::announce_entry &tracker : announces)
for (const lt::announce_entry &tracker : nativeTrackers)
entries << tracker; entries << tracker;
return entries; return entries;
} }
@ -428,7 +434,9 @@ QHash<QString, TrackerInfo> TorrentHandle::trackerInfos() const
void TorrentHandle::addTrackers(const QVector<TrackerEntry> &trackers) void TorrentHandle::addTrackers(const QVector<TrackerEntry> &trackers)
{ {
const QVector<TrackerEntry> currentTrackers = this->trackers(); QSet<TrackerEntry> currentTrackers;
for (const lt::announce_entry &entry : m_nativeHandle.trackers())
currentTrackers << entry;
QVector<TrackerEntry> newTrackers; QVector<TrackerEntry> newTrackers;
newTrackers.reserve(trackers.size()); newTrackers.reserve(trackers.size());
@ -451,16 +459,17 @@ void TorrentHandle::replaceTrackers(const QVector<TrackerEntry> &trackers)
QVector<TrackerEntry> newTrackers; QVector<TrackerEntry> newTrackers;
newTrackers.reserve(trackers.size()); newTrackers.reserve(trackers.size());
std::vector<lt::announce_entry> announces; std::vector<lt::announce_entry> nativeTrackers;
nativeTrackers.reserve(trackers.size());
for (const TrackerEntry &tracker : trackers) { for (const TrackerEntry &tracker : trackers) {
announces.emplace_back(tracker.nativeEntry()); nativeTrackers.emplace_back(tracker.nativeEntry());
if (!currentTrackers.removeOne(tracker)) if (!currentTrackers.removeOne(tracker))
newTrackers << tracker; newTrackers << tracker;
} }
m_nativeHandle.replace_trackers(announces); m_nativeHandle.replace_trackers(nativeTrackers);
if (newTrackers.isEmpty() && currentTrackers.isEmpty()) { if (newTrackers.isEmpty() && currentTrackers.isEmpty()) {
// when existing tracker reorders // when existing tracker reorders
@ -477,12 +486,12 @@ void TorrentHandle::replaceTrackers(const QVector<TrackerEntry> &trackers)
QVector<QUrl> TorrentHandle::urlSeeds() const QVector<QUrl> TorrentHandle::urlSeeds() const
{ {
const std::set<std::string> seeds = m_nativeHandle.url_seeds(); const std::set<std::string> currentSeeds = m_nativeHandle.url_seeds();
QVector<QUrl> urlSeeds; QVector<QUrl> 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())); urlSeeds.append(QUrl(urlSeed.c_str()));
return urlSeeds; return urlSeeds;
@ -490,11 +499,17 @@ QVector<QUrl> TorrentHandle::urlSeeds() const
void TorrentHandle::addUrlSeeds(const QVector<QUrl> &urlSeeds) void TorrentHandle::addUrlSeeds(const QVector<QUrl> &urlSeeds)
{ {
const std::set<std::string> currentSeeds = m_nativeHandle.url_seeds();
QVector<QUrl> addedUrlSeeds; QVector<QUrl> addedUrlSeeds;
addedUrlSeeds.reserve(urlSeeds.size()); addedUrlSeeds.reserve(urlSeeds.size());
for (const QUrl &urlSeed : urlSeeds) {
if (addUrlSeed(urlSeed)) for (const QUrl &url : urlSeeds) {
addedUrlSeeds << urlSeed; const std::string nativeUrl = url.toString().toStdString();
if (currentSeeds.find(nativeUrl) == currentSeeds.end()) {
m_nativeHandle.add_url_seed(nativeUrl);
addedUrlSeeds << url;
}
} }
if (!addedUrlSeeds.isEmpty()) if (!addedUrlSeeds.isEmpty())
@ -503,35 +518,23 @@ void TorrentHandle::addUrlSeeds(const QVector<QUrl> &urlSeeds)
void TorrentHandle::removeUrlSeeds(const QVector<QUrl> &urlSeeds) void TorrentHandle::removeUrlSeeds(const QVector<QUrl> &urlSeeds)
{ {
const std::set<std::string> currentSeeds = m_nativeHandle.url_seeds();
QVector<QUrl> removedUrlSeeds; QVector<QUrl> removedUrlSeeds;
removedUrlSeeds.reserve(urlSeeds.size()); removedUrlSeeds.reserve(urlSeeds.size());
for (const QUrl &urlSeed : urlSeeds) {
if (removeUrlSeed(urlSeed)) for (const QUrl &url : urlSeeds) {
removedUrlSeeds << urlSeed; const std::string nativeUrl = url.toString().toStdString();
if (currentSeeds.find(nativeUrl) != currentSeeds.end()) {
m_nativeHandle.remove_url_seed(nativeUrl);
removedUrlSeeds << url;
}
} }
if (!removedUrlSeeds.isEmpty()) if (!removedUrlSeeds.isEmpty())
m_session->handleTorrentUrlSeedsRemoved(this, removedUrlSeeds); m_session->handleTorrentUrlSeedsRemoved(this, removedUrlSeeds);
} }
bool TorrentHandle::addUrlSeed(const QUrl &urlSeed)
{
const QVector<QUrl> 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<QUrl> seeds = urlSeeds();
if (!seeds.contains(urlSeed)) return false;
m_nativeHandle.remove_url_seed(urlSeed.toString().toStdString());
return true;
}
bool TorrentHandle::connectPeer(const PeerAddress &peerAddress) bool TorrentHandle::connectPeer(const PeerAddress &peerAddress)
{ {
lt::error_code ec; lt::error_code ec;

View file

@ -394,8 +394,6 @@ namespace BitTorrent
void move_impl(QString path, bool overwrite); void move_impl(QString path, bool overwrite);
void moveStorage(const QString &newPath, bool overwrite); void moveStorage(const QString &newPath, bool overwrite);
void manageIncompleteFiles(); void manageIncompleteFiles();
bool addUrlSeed(const QUrl &urlSeed);
bool removeUrlSeed(const QUrl &urlSeed);
void setFirstLastPiecePriorityImpl(bool enabled, const QVector<DownloadPriority> &updatedFilePrio = {}); void setFirstLastPiecePriorityImpl(bool enabled, const QVector<DownloadPriority> &updatedFilePrio = {});
Session *const m_session; Session *const m_session;