From da12daffee4f778c65458f4cfba6710a6ce74c3c Mon Sep 17 00:00:00 2001 From: Prince Gupta Date: Sat, 5 Mar 2022 16:08:10 +0530 Subject: [PATCH] Cache tracker item in TrackerFilterList --- src/gui/transferlistfilterswidget.cpp | 32 +++++++++++++-------------- src/gui/transferlistfilterswidget.h | 8 ++++++- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/gui/transferlistfilterswidget.cpp b/src/gui/transferlistfilterswidget.cpp index 86bc01a38..da0ab15ef 100644 --- a/src/gui/transferlistfilterswidget.cpp +++ b/src/gui/transferlistfilterswidget.cpp @@ -419,17 +419,18 @@ TrackerFiltersList::~TrackerFiltersList() void TrackerFiltersList::addItem(const QString &tracker, const BitTorrent::TorrentID &id) { const QString host {getHost(tracker)}; - const bool exists {m_trackers.contains(host)}; + const auto existingDataItr = m_trackers.find(host); + const bool exists {existingDataItr != m_trackers.end()}; QListWidgetItem *trackerItem {nullptr}; if (exists) { - if (m_trackers.value(host).contains(id)) + if (existingDataItr->torrents.contains(id)) return; - trackerItem = item((host == NULL_HOST) - ? TRACKERLESS_ROW - : rowFromTracker(host)); + trackerItem = (host == NULL_HOST) + ? item(TRACKERLESS_ROW) + : existingDataItr->item; } else { @@ -441,7 +442,7 @@ void TrackerFiltersList::addItem(const QString &tracker, const BitTorrent::Torre } if (!trackerItem) return; - QSet &torrentIDs {m_trackers[host]}; + QSet &torrentIDs {m_trackers[host].torrents}; torrentIDs.insert(id); if (host == NULL_HOST) @@ -478,13 +479,12 @@ void TrackerFiltersList::addItem(const QString &tracker, const BitTorrent::Torre void TrackerFiltersList::removeItem(const QString &trackerURL, const BitTorrent::TorrentID &id) { const QString host = getHost(trackerURL); - QSet torrentIDs = m_trackers.value(host); + QSet torrentIDs = m_trackers.value(host).torrents; if (torrentIDs.empty()) return; torrentIDs.remove(id); - int row = 0; QListWidgetItem *trackerItem = nullptr; if (!host.isEmpty()) @@ -518,12 +518,11 @@ void TrackerFiltersList::removeItem(const QString &trackerURL, const BitTorrent: } } - row = rowFromTracker(host); - trackerItem = item(row); + trackerItem = m_trackers.value(host).item; if (torrentIDs.empty()) { - if (currentRow() == row) + if (currentItem() == trackerItem) setCurrentRow(0, QItemSelectionModel::SelectCurrent); delete trackerItem; m_trackers.remove(host); @@ -536,15 +535,14 @@ void TrackerFiltersList::removeItem(const QString &trackerURL, const BitTorrent: } else { - row = 1; trackerItem = item(TRACKERLESS_ROW); trackerItem->setText(tr("Trackerless (%1)").arg(torrentIDs.size())); } - m_trackers.insert(host, torrentIDs); + m_trackers.insert(host, {torrentIDs, trackerItem}); - if (currentRow() == row) - applyFilter(row); + if (currentItem() == trackerItem) + applyFilter(currentRow()); } void TrackerFiltersList::changeTrackerless(const bool trackerless, const BitTorrent::TorrentID &id) @@ -758,13 +756,13 @@ QSet TrackerFiltersList::getTorrentIDs(const int row) con switch (row) { case TRACKERLESS_ROW: - return m_trackers.value(NULL_HOST); + return m_trackers.value(NULL_HOST).torrents; case ERROR_ROW: return {m_errors.keyBegin(), m_errors.keyEnd()}; case WARNING_ROW: return {m_warnings.keyBegin(), m_warnings.keyEnd()}; default: - return m_trackers.value(trackerFromRow(row)); + return m_trackers.value(trackerFromRow(row)).torrents; } } diff --git a/src/gui/transferlistfilterswidget.h b/src/gui/transferlistfilterswidget.h index 68c55422d..6f53071ad 100644 --- a/src/gui/transferlistfilterswidget.h +++ b/src/gui/transferlistfilterswidget.h @@ -142,7 +142,13 @@ private: QSet getTorrentIDs(int row) const; void downloadFavicon(const QString &url); - QHash> m_trackers; // + struct TrackerData + { + QSet torrents; + QListWidgetItem *item = nullptr; + }; + + QHash m_trackers; QHash> m_errors; // QHash> m_warnings; // PathList m_iconPaths;