Replace QList by QVector for tracker related operations

This commit is contained in:
Chocobo1 2019-05-21 12:21:17 +08:00
parent f86c5442aa
commit ed6bb0efdc
No known key found for this signature in database
GPG key ID: 210D9C873253A68C
14 changed files with 97 additions and 81 deletions

View file

@ -76,6 +76,7 @@ MagnetUri::MagnetUri(const QString &source)
m_hash = m_addTorrentParams.info_hash;
m_name = QString::fromStdString(m_addTorrentParams.name);
m_trackers.reserve(m_addTorrentParams.trackers.size());
for (const std::string &tracker : m_addTorrentParams.trackers)
m_trackers.append(lt::announce_entry {tracker});
@ -98,7 +99,7 @@ QString MagnetUri::name() const
return m_name;
}
QList<TrackerEntry> MagnetUri::trackers() const
QVector<TrackerEntry> MagnetUri::trackers() const
{
return m_trackers;
}

View file

@ -33,6 +33,7 @@
#include <QList>
#include <QString>
#include <QVector>
#include "infohash.h"
#include "trackerentry.h"
@ -49,7 +50,7 @@ namespace BitTorrent
bool isValid() const;
InfoHash hash() const;
QString name() const;
QList<TrackerEntry> trackers() const;
QVector<TrackerEntry> trackers() const;
QList<QUrl> urlSeeds() const;
QString url() const;
@ -60,7 +61,7 @@ namespace BitTorrent
QString m_url;
InfoHash m_hash;
QString m_name;
QList<TrackerEntry> m_trackers;
QVector<TrackerEntry> m_trackers;
QList<QUrl> m_urlSeeds;
lt::add_torrent_params m_addTorrentParams;
};

View file

@ -3336,7 +3336,7 @@ void Session::handleTorrentSavingModeChanged(TorrentHandle *const torrent)
emit torrentSavingModeChanged(torrent);
}
void Session::handleTorrentTrackersAdded(TorrentHandle *const torrent, const QList<TrackerEntry> &newTrackers)
void Session::handleTorrentTrackersAdded(TorrentHandle *const torrent, const QVector<TrackerEntry> &newTrackers)
{
saveTorrentResumeData(torrent);
@ -3348,7 +3348,7 @@ void Session::handleTorrentTrackersAdded(TorrentHandle *const torrent, const QLi
emit trackersChanged(torrent);
}
void Session::handleTorrentTrackersRemoved(TorrentHandle *const torrent, const QList<TrackerEntry> &deletedTrackers)
void Session::handleTorrentTrackersRemoved(TorrentHandle *const torrent, const QVector<TrackerEntry> &deletedTrackers)
{
saveTorrentResumeData(torrent);

View file

@ -436,8 +436,8 @@ namespace BitTorrent
void handleTorrentResumed(TorrentHandle *const torrent);
void handleTorrentChecked(TorrentHandle *const torrent);
void handleTorrentFinished(TorrentHandle *const torrent);
void handleTorrentTrackersAdded(TorrentHandle *const torrent, const QList<TrackerEntry> &newTrackers);
void handleTorrentTrackersRemoved(TorrentHandle *const torrent, const QList<TrackerEntry> &deletedTrackers);
void handleTorrentTrackersAdded(TorrentHandle *const torrent, const QVector<TrackerEntry> &newTrackers);
void handleTorrentTrackersRemoved(TorrentHandle *const torrent, const QVector<TrackerEntry> &deletedTrackers);
void handleTorrentTrackersChanged(TorrentHandle *const torrent);
void handleTorrentUrlSeedsAdded(TorrentHandle *const torrent, const QList<QUrl> &newUrlSeeds);
void handleTorrentUrlSeedsRemoved(TorrentHandle *const torrent, const QList<QUrl> &urlSeeds);
@ -474,8 +474,8 @@ namespace BitTorrent
void recursiveTorrentDownloadPossible(BitTorrent::TorrentHandle *const torrent);
void speedLimitModeChanged(bool alternative);
void IPFilterParsed(bool error, int ruleCount);
void trackersAdded(BitTorrent::TorrentHandle *const torrent, const QList<BitTorrent::TrackerEntry> &trackers);
void trackersRemoved(BitTorrent::TorrentHandle *const torrent, const QList<BitTorrent::TrackerEntry> &trackers);
void trackersAdded(BitTorrent::TorrentHandle *const torrent, const QVector<BitTorrent::TrackerEntry> &trackers);
void trackersRemoved(BitTorrent::TorrentHandle *const torrent, const QVector<BitTorrent::TrackerEntry> &trackers);
void trackersChanged(BitTorrent::TorrentHandle *const torrent);
void trackerlessStateChanged(BitTorrent::TorrentHandle *const torrent, bool trackerless);
void downloadFromUrlFailed(const QString &url, const QString &reason);
@ -669,7 +669,7 @@ namespace BitTorrent
int m_numResumeData;
int m_extraLimit;
QList<BitTorrent::TrackerEntry> m_additionalTrackerList;
QVector<BitTorrent::TrackerEntry> m_additionalTrackerList;
QString m_resumeFolderPath;
QFile m_resumeFolderLock;
bool m_useProxy;

View file

@ -386,14 +386,14 @@ void TorrentHandle::setAutoManaged(const bool enable)
#endif
}
QList<TrackerEntry> TorrentHandle::trackers() const
QVector<TrackerEntry> TorrentHandle::trackers() const
{
QList<TrackerEntry> entries;
const std::vector<lt::announce_entry> announces = m_nativeHandle.trackers();
QVector<TrackerEntry> entries;
entries.reserve(announces.size());
for (const lt::announce_entry &tracker : announces)
entries << tracker;
return entries;
}
@ -402,53 +402,55 @@ QHash<QString, TrackerInfo> TorrentHandle::trackerInfos() const
return m_trackerInfos;
}
void TorrentHandle::addTrackers(const QList<TrackerEntry> &trackers)
void TorrentHandle::addTrackers(const QVector<TrackerEntry> &trackers)
{
QList<TrackerEntry> addedTrackers;
const QVector<TrackerEntry> currentTrackers = this->trackers();
QVector<TrackerEntry> newTrackers;
newTrackers.reserve(trackers.size());
for (const TrackerEntry &tracker : trackers) {
if (addTracker(tracker))
addedTrackers << tracker;
if (!currentTrackers.contains(tracker)) {
m_nativeHandle.add_tracker(tracker.nativeEntry());
newTrackers << tracker;
}
}
if (!addedTrackers.isEmpty())
m_session->handleTorrentTrackersAdded(this, addedTrackers);
if (!newTrackers.isEmpty())
m_session->handleTorrentTrackersAdded(this, newTrackers);
}
void TorrentHandle::replaceTrackers(const QList<TrackerEntry> &trackers)
void TorrentHandle::replaceTrackers(const QVector<TrackerEntry> &trackers)
{
QList<TrackerEntry> existingTrackers = this->trackers();
QList<TrackerEntry> addedTrackers;
QVector<TrackerEntry> currentTrackers = this->trackers();
QVector<TrackerEntry> newTrackers;
newTrackers.reserve(trackers.size());
std::vector<lt::announce_entry> announces;
for (const TrackerEntry &tracker : trackers) {
announces.push_back(tracker.nativeEntry());
if (!existingTrackers.contains(tracker))
addedTrackers << tracker;
else
existingTrackers.removeOne(tracker);
announces.emplace_back(tracker.nativeEntry());
if (!currentTrackers.removeOne(tracker))
newTrackers << tracker;
}
m_nativeHandle.replace_trackers(announces);
if (addedTrackers.isEmpty() && existingTrackers.isEmpty()) {
if (newTrackers.isEmpty() && currentTrackers.isEmpty()) {
// when existing tracker reorders
m_session->handleTorrentTrackersChanged(this);
}
else {
if (!existingTrackers.isEmpty())
m_session->handleTorrentTrackersRemoved(this, existingTrackers);
if (!addedTrackers.isEmpty())
m_session->handleTorrentTrackersAdded(this, addedTrackers);
if (!currentTrackers.isEmpty())
m_session->handleTorrentTrackersRemoved(this, currentTrackers);
if (!newTrackers.isEmpty())
m_session->handleTorrentTrackersAdded(this, newTrackers);
}
}
bool TorrentHandle::addTracker(const TrackerEntry &tracker)
{
if (trackers().contains(tracker))
return false;
m_nativeHandle.add_tracker(tracker.nativeEntry());
return true;
}
QList<QUrl> TorrentHandle::urlSeeds() const
{
QList<QUrl> urlSeeds;

View file

@ -37,6 +37,7 @@
#include <libtorrent/torrent_status.hpp>
#include <QHash>
#include <QList>
#include <QObject>
#include <QQueue>
#include <QSet>
@ -260,7 +261,7 @@ namespace BitTorrent
bool hasError() const;
bool hasFilteredPieces() const;
int queuePosition() const;
QList<TrackerEntry> trackers() const;
QVector<TrackerEntry> trackers() const;
QHash<QString, TrackerInfo> trackerInfos() const;
QList<QUrl> urlSeeds() const;
QString error() const;
@ -323,8 +324,8 @@ namespace BitTorrent
void setDownloadLimit(int limit);
void setSuperSeeding(bool enable);
void flushCache();
void addTrackers(const QList<TrackerEntry> &trackers);
void replaceTrackers(const QList<TrackerEntry> &trackers);
void addTrackers(const QVector<TrackerEntry> &trackers);
void replaceTrackers(const QVector<TrackerEntry> &trackers);
void addUrlSeeds(const QList<QUrl> &urlSeeds);
void removeUrlSeeds(const QList<QUrl> &urlSeeds);
bool connectPeer(const PeerAddress &peerAddress);
@ -388,7 +389,6 @@ namespace BitTorrent
void move_impl(QString path, bool overwrite);
void moveStorage(const QString &newPath, bool overwrite);
void manageIncompleteFiles();
bool addTracker(const TrackerEntry &tracker);
bool addUrlSeed(const QUrl &urlSeed);
bool removeUrlSeed(const QUrl &urlSeed);
void setFirstLastPiecePriorityImpl(bool enabled, const QVector<DownloadPriority> &updatedFilePrio = {});

View file

@ -264,15 +264,18 @@ qlonglong TorrentInfo::fileOffset(const int index) const
return m_nativeInfo->files().file_offset(LTFileIndex {index});
}
QList<TrackerEntry> TorrentInfo::trackers() const
QVector<TrackerEntry> TorrentInfo::trackers() const
{
if (!isValid()) return {};
QList<TrackerEntry> trackers;
for (const lt::announce_entry &tracker : m_nativeInfo->trackers())
trackers.append(tracker);
const std::vector<lt::announce_entry> trackers = m_nativeInfo->trackers();
return trackers;
QVector<TrackerEntry> ret;
ret.reserve(trackers.size());
for (const lt::announce_entry &tracker : trackers)
ret.append(tracker);
return ret;
}
QList<QUrl> TorrentInfo::urlSeeds() const

View file

@ -88,7 +88,7 @@ namespace BitTorrent
QString origFilePath(int index) const;
qlonglong fileSize(int index) const;
qlonglong fileOffset(int index) const;
QList<TrackerEntry> trackers() const;
QVector<TrackerEntry> trackers() const;
QList<QUrl> urlSeeds() const;
QByteArray metadata() const;
QStringList filesForPiece(int pieceIndex) const;

View file

@ -47,6 +47,7 @@ namespace BitTorrent
NotWorking = 4
};
TrackerEntry() = default;
TrackerEntry(const QString &url);
TrackerEntry(const lt::announce_entry &nativeEntry);
TrackerEntry(const TrackerEntry &other) = default;

View file

@ -184,9 +184,10 @@ void TrackerListWidget::moveSelectionUp()
setSelectionModel(selection);
// Update torrent trackers
QList<BitTorrent::TrackerEntry> trackers;
QVector<BitTorrent::TrackerEntry> trackers;
trackers.reserve(topLevelItemCount());
for (int i = NB_STICKY_ITEM; i < topLevelItemCount(); ++i) {
QString trackerURL = topLevelItem(i)->data(COL_URL, Qt::DisplayRole).toString();
const QString trackerURL = topLevelItem(i)->data(COL_URL, Qt::DisplayRole).toString();
BitTorrent::TrackerEntry e(trackerURL);
e.setTier(i - NB_STICKY_ITEM);
trackers.append(e);
@ -225,9 +226,10 @@ void TrackerListWidget::moveSelectionDown()
setSelectionModel(selection);
// Update torrent trackers
QList<BitTorrent::TrackerEntry> trackers;
QVector<BitTorrent::TrackerEntry> trackers;
trackers.reserve(topLevelItemCount());
for (int i = NB_STICKY_ITEM; i < topLevelItemCount(); ++i) {
QString trackerURL = topLevelItem(i)->data(COL_URL, Qt::DisplayRole).toString();
const QString trackerURL = topLevelItem(i)->data(COL_URL, Qt::DisplayRole).toString();
BitTorrent::TrackerEntry e(trackerURL);
e.setTier(i - NB_STICKY_ITEM);
trackers.append(e);
@ -388,7 +390,7 @@ void TrackerListWidget::askForTrackers()
BitTorrent::TorrentHandle *const torrent = m_properties->getCurrentTorrent();
if (!torrent) return;
QList<BitTorrent::TrackerEntry> trackers;
QVector<BitTorrent::TrackerEntry> trackers;
for (const QString &tracker : asConst(TrackersAdditionDialog::askForTrackers(this, torrent)))
trackers << tracker;
@ -430,14 +432,17 @@ void TrackerListWidget::deleteSelectedTrackers()
}
// Iterate over the trackers and remove the selected ones
QList<BitTorrent::TrackerEntry> remainingTrackers;
const QList<BitTorrent::TrackerEntry> trackers = torrent->trackers();
const QVector<BitTorrent::TrackerEntry> trackers = torrent->trackers();
QVector<BitTorrent::TrackerEntry> remainingTrackers;
remainingTrackers.reserve(trackers.size());
for (const BitTorrent::TrackerEntry &entry : trackers) {
if (!urlsToRemove.contains(entry.url()))
remainingTrackers.push_back(entry);
}
torrent->replaceTrackers(remainingTrackers);
if (!torrent->isPaused())
torrent->forceReannounce();
}
@ -451,10 +456,10 @@ void TrackerListWidget::editSelectedTracker()
if (selectedTrackerItems.isEmpty()) return;
// During multi-select only process item selected last
QUrl trackerURL = selectedTrackerItems.last()->text(COL_URL);
const QUrl trackerURL = selectedTrackerItems.last()->text(COL_URL);
bool ok;
QUrl newTrackerURL = AutoExpandableDialog::getText(this, tr("Tracker editing"), tr("Tracker URL:"),
const QUrl newTrackerURL = AutoExpandableDialog::getText(this, tr("Tracker editing"), tr("Tracker URL:"),
QLineEdit::Normal, trackerURL.toString(), &ok).trimmed();
if (!ok) return;
@ -464,23 +469,24 @@ void TrackerListWidget::editSelectedTracker()
}
if (newTrackerURL == trackerURL) return;
QList<BitTorrent::TrackerEntry> trackers = torrent->trackers();
QVector<BitTorrent::TrackerEntry> trackers = torrent->trackers();
bool match = false;
for (auto &entry : trackers) {
for (BitTorrent::TrackerEntry &entry : trackers) {
if (newTrackerURL == QUrl(entry.url())) {
QMessageBox::warning(this, tr("Tracker editing failed"), tr("The tracker URL already exists."));
return;
}
if (trackerURL == QUrl(entry.url()) && !match) {
if (!match && (trackerURL == QUrl(entry.url()))) {
match = true;
BitTorrent::TrackerEntry newEntry(newTrackerURL.toString());
newEntry.setTier(entry.tier());
match = true;
entry = newEntry;
}
}
torrent->replaceTrackers(trackers);
if (!torrent->isPaused())
torrent->forceReannounce();
}
@ -493,7 +499,7 @@ void TrackerListWidget::reannounceSelected()
BitTorrent::TorrentHandle *const torrent = m_properties->getCurrentTorrent();
if (!torrent) return;
QList<BitTorrent::TrackerEntry> trackers = torrent->trackers();
const QVector<BitTorrent::TrackerEntry> trackers = torrent->trackers();
for (const QTreeWidgetItem *item : selItems) {
// DHT case

View file

@ -89,12 +89,11 @@ void TrackersAdditionDialog::torrentListDownloadFinished(const Net::DownloadResu
return;
}
// Load from torrent handle
QList<BitTorrent::TrackerEntry> existingTrackers = m_torrent->trackers();
// Load from current user list
const QStringList tmp = m_ui->textEditTrackersList->toPlainText().split('\n');
for (const QString &userURL : tmp) {
BitTorrent::TrackerEntry userTracker(userURL);
const QStringList trackersFromUser = m_ui->textEditTrackersList->toPlainText().split('\n');
QVector<BitTorrent::TrackerEntry> existingTrackers = m_torrent->trackers();
existingTrackers.reserve(trackersFromUser.size());
for (const QString &userURL : trackersFromUser) {
const BitTorrent::TrackerEntry userTracker(userURL);
if (!existingTrackers.contains(userTracker))
existingTrackers << userTracker;
}

View file

@ -474,7 +474,7 @@ void TrackerFiltersList::applyFilter(int row)
void TrackerFiltersList::handleNewTorrent(BitTorrent::TorrentHandle *const torrent)
{
QString hash = torrent->hash();
const QList<BitTorrent::TrackerEntry> trackers = torrent->trackers();
const QVector<BitTorrent::TrackerEntry> trackers = torrent->trackers();
for (const BitTorrent::TrackerEntry &tracker : trackers)
addItem(tracker.url(), hash);
@ -488,7 +488,7 @@ void TrackerFiltersList::handleNewTorrent(BitTorrent::TorrentHandle *const torre
void TrackerFiltersList::torrentAboutToBeDeleted(BitTorrent::TorrentHandle *const torrent)
{
QString hash = torrent->hash();
const QList<BitTorrent::TrackerEntry> trackers = torrent->trackers();
const QVector<BitTorrent::TrackerEntry> trackers = torrent->trackers();
for (const BitTorrent::TrackerEntry &tracker : trackers)
removeItem(tracker.url(), hash);
@ -647,13 +647,13 @@ void TransferListFiltersWidget::setDownloadTrackerFavicon(bool value)
m_trackerFilters->setDownloadTrackerFavicon(value);
}
void TransferListFiltersWidget::addTrackers(BitTorrent::TorrentHandle *const torrent, const QList<BitTorrent::TrackerEntry> &trackers)
void TransferListFiltersWidget::addTrackers(BitTorrent::TorrentHandle *const torrent, const QVector<BitTorrent::TrackerEntry> &trackers)
{
for (const BitTorrent::TrackerEntry &tracker : trackers)
m_trackerFilters->addItem(tracker.url(), torrent->hash());
}
void TransferListFiltersWidget::removeTrackers(BitTorrent::TorrentHandle *const torrent, const QList<BitTorrent::TrackerEntry> &trackers)
void TransferListFiltersWidget::removeTrackers(BitTorrent::TorrentHandle *const torrent, const QVector<BitTorrent::TrackerEntry> &trackers)
{
for (const BitTorrent::TrackerEntry &tracker : trackers)
m_trackerFilters->removeItem(tracker.url(), torrent->hash());

View file

@ -148,8 +148,8 @@ public:
void setDownloadTrackerFavicon(bool value);
public slots:
void addTrackers(BitTorrent::TorrentHandle *const torrent, const QList<BitTorrent::TrackerEntry> &trackers);
void removeTrackers(BitTorrent::TorrentHandle *const torrent, const QList<BitTorrent::TrackerEntry> &trackers);
void addTrackers(BitTorrent::TorrentHandle *const torrent, const QVector<BitTorrent::TrackerEntry> &trackers);
void removeTrackers(BitTorrent::TorrentHandle *const torrent, const QVector<BitTorrent::TrackerEntry> &trackers);
void changeTrackerless(BitTorrent::TorrentHandle *const torrent, bool trackerless);
void trackerSuccess(BitTorrent::TorrentHandle *const torrent, const QString &tracker);
void trackerWarning(BitTorrent::TorrentHandle *const torrent, const QString &tracker);

View file

@ -604,7 +604,7 @@ void TorrentsController::addTrackersAction()
if (!torrent)
throw APIError(APIErrorType::NotFound);
QList<BitTorrent::TrackerEntry> trackers;
QVector<BitTorrent::TrackerEntry> trackers;
for (const QString &urlStr : asConst(params()["urls"].split('\n'))) {
const QUrl url {urlStr.trimmed()};
if (url.isValid())
@ -632,7 +632,7 @@ void TorrentsController::editTrackerAction()
if (!newTrackerUrl.isValid())
throw APIError(APIErrorType::BadParams, "New tracker URL is invalid");
QList<BitTorrent::TrackerEntry> trackers = torrent->trackers();
QVector<BitTorrent::TrackerEntry> trackers = torrent->trackers();
bool match = false;
for (BitTorrent::TrackerEntry &tracker : trackers) {
const QUrl trackerUrl(tracker.url());
@ -649,6 +649,7 @@ void TorrentsController::editTrackerAction()
throw APIError(APIErrorType::Conflict, "Tracker not found");
torrent->replaceTrackers(trackers);
if (!torrent->isPaused())
torrent->forceReannounce();
}
@ -664,8 +665,9 @@ void TorrentsController::removeTrackersAction()
if (!torrent)
throw APIError(APIErrorType::NotFound);
QList<BitTorrent::TrackerEntry> remainingTrackers;
const QList<BitTorrent::TrackerEntry> trackers = torrent->trackers();
const QVector<BitTorrent::TrackerEntry> trackers = torrent->trackers();
QVector<BitTorrent::TrackerEntry> remainingTrackers;
remainingTrackers.reserve(trackers.size());
for (const BitTorrent::TrackerEntry &entry : trackers) {
if (!urls.contains(entry.url()))
remainingTrackers.push_back(entry);
@ -675,6 +677,7 @@ void TorrentsController::removeTrackersAction()
throw APIError(APIErrorType::Conflict, "No trackers were removed");
torrent->replaceTrackers(remainingTrackers);
if (!torrent->isPaused())
torrent->forceReannounce();
}