From 418164bee4dfde403345556910639c575273451a Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 18 Sep 2019 15:28:43 +0800 Subject: [PATCH] Move "torrent status report" functionality out of Session class --- src/base/bittorrent/session.cpp | 25 ------------- src/base/bittorrent/session.h | 14 ------- src/gui/transferlistfilterswidget.cpp | 53 +++++++++++++++++++++------ 3 files changed, 42 insertions(+), 50 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 44d4e5ef2..0c63979e6 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -1796,11 +1796,6 @@ QHash Session::torrents() const return m_torrents; } -TorrentStatusReport Session::torrentStatusReport() const -{ - return m_torrentStatusReport; -} - bool Session::addTorrent(const QString &source, const AddTorrentParams ¶ms) { // `source`: .torrent file path/url or magnet uri @@ -4404,26 +4399,6 @@ void Session::handleStateUpdateAlert(const lt::state_update_alert *p) updatedTorrents.push_back(torrent); } - m_torrentStatusReport = TorrentStatusReport(); - for (const TorrentHandle *torrent : asConst(m_torrents)) { - if (torrent->isDownloading()) - ++m_torrentStatusReport.nbDownloading; - if (torrent->isUploading()) - ++m_torrentStatusReport.nbSeeding; - if (torrent->isCompleted()) - ++m_torrentStatusReport.nbCompleted; - if (torrent->isPaused()) - ++m_torrentStatusReport.nbPaused; - if (torrent->isResumed()) - ++m_torrentStatusReport.nbResumed; - if (torrent->isActive()) - ++m_torrentStatusReport.nbActive; - if (torrent->isInactive()) - ++m_torrentStatusReport.nbInactive; - if (torrent->isErrored()) - ++m_torrentStatusReport.nbErrored; - } - emit torrentsUpdated(updatedTorrents); } diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index 043e22de5..52c702fcc 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -86,18 +86,6 @@ namespace BitTorrent class TrackerEntry; struct CreateTorrentParams; - struct TorrentStatusReport - { - uint nbDownloading = 0; - uint nbSeeding = 0; - uint nbCompleted = 0; - uint nbActive = 0; - uint nbInactive = 0; - uint nbPaused = 0; - uint nbResumed = 0; - uint nbErrored = 0; - }; - class SessionSettingsEnums { Q_GADGET @@ -395,7 +383,6 @@ namespace BitTorrent void startUpTorrents(); TorrentHandle *findTorrent(const InfoHash &hash) const; QHash torrents() const; - TorrentStatusReport torrentStatusReport() const; bool hasActiveTorrents() const; bool hasUnfinishedTorrents() const; bool hasRunningSeed() const; @@ -694,7 +681,6 @@ namespace BitTorrent QHash m_addingTorrents; QHash m_downloadedTorrents; QHash m_removingTorrents; - TorrentStatusReport m_torrentStatusReport; QStringMap m_categories; QSet m_tags; diff --git a/src/gui/transferlistfilterswidget.cpp b/src/gui/transferlistfilterswidget.cpp index 3d5bff3f4..9ff3d4097 100644 --- a/src/gui/transferlistfilterswidget.cpp +++ b/src/gui/transferlistfilterswidget.cpp @@ -146,8 +146,12 @@ void BaseFilterWidget::toggleFilter(bool checked) StatusFilterWidget::StatusFilterWidget(QWidget *parent, TransferListWidget *transferList) : BaseFilterWidget(parent, transferList) { + connect(BitTorrent::Session::instance(), &BitTorrent::Session::torrentAdded + , this, &StatusFilterWidget::updateTorrentNumbers); connect(BitTorrent::Session::instance(), &BitTorrent::Session::torrentsUpdated , this, &StatusFilterWidget::updateTorrentNumbers); + connect(BitTorrent::Session::instance(), &BitTorrent::Session::torrentAboutToBeRemoved + , this, &StatusFilterWidget::updateTorrentNumbers); // Add status filters auto *all = new QListWidgetItem(this); @@ -190,17 +194,44 @@ StatusFilterWidget::~StatusFilterWidget() void StatusFilterWidget::updateTorrentNumbers() { - const BitTorrent::TorrentStatusReport report = BitTorrent::Session::instance()->torrentStatusReport(); - - item(TorrentFilter::All)->setData(Qt::DisplayRole, tr("All (%1)").arg(report.nbActive + report.nbInactive)); - item(TorrentFilter::Downloading)->setData(Qt::DisplayRole, tr("Downloading (%1)").arg(report.nbDownloading)); - item(TorrentFilter::Seeding)->setData(Qt::DisplayRole, tr("Seeding (%1)").arg(report.nbSeeding)); - item(TorrentFilter::Completed)->setData(Qt::DisplayRole, tr("Completed (%1)").arg(report.nbCompleted)); - item(TorrentFilter::Paused)->setData(Qt::DisplayRole, tr("Paused (%1)").arg(report.nbPaused)); - item(TorrentFilter::Resumed)->setData(Qt::DisplayRole, tr("Resumed (%1)").arg(report.nbResumed)); - item(TorrentFilter::Active)->setData(Qt::DisplayRole, tr("Active (%1)").arg(report.nbActive)); - item(TorrentFilter::Inactive)->setData(Qt::DisplayRole, tr("Inactive (%1)").arg(report.nbInactive)); - item(TorrentFilter::Errored)->setData(Qt::DisplayRole, tr("Errored (%1)").arg(report.nbErrored)); + int nbDownloading = 0; + int nbSeeding = 0; + int nbCompleted = 0; + int nbResumed = 0; + int nbPaused = 0; + int nbActive = 0; + int nbInactive = 0; + int nbErrored = 0; + + const QHash torrents = BitTorrent::Session::instance()->torrents(); + for (const BitTorrent::TorrentHandle *torrent : torrents) { + if (torrent->isDownloading()) + ++nbDownloading; + if (torrent->isUploading()) + ++nbSeeding; + if (torrent->isCompleted()) + ++nbCompleted; + if (torrent->isResumed()) + ++nbResumed; + if (torrent->isPaused()) + ++nbPaused; + if (torrent->isActive()) + ++nbActive; + if (torrent->isInactive()) + ++nbInactive; + if (torrent->isErrored()) + ++nbErrored; + } + + item(TorrentFilter::All)->setData(Qt::DisplayRole, tr("All (%1)").arg(torrents.count())); + item(TorrentFilter::Downloading)->setData(Qt::DisplayRole, tr("Downloading (%1)").arg(nbDownloading)); + item(TorrentFilter::Seeding)->setData(Qt::DisplayRole, tr("Seeding (%1)").arg(nbSeeding)); + item(TorrentFilter::Completed)->setData(Qt::DisplayRole, tr("Completed (%1)").arg(nbCompleted)); + item(TorrentFilter::Resumed)->setData(Qt::DisplayRole, tr("Resumed (%1)").arg(nbResumed)); + item(TorrentFilter::Paused)->setData(Qt::DisplayRole, tr("Paused (%1)").arg(nbPaused)); + item(TorrentFilter::Active)->setData(Qt::DisplayRole, tr("Active (%1)").arg(nbActive)); + item(TorrentFilter::Inactive)->setData(Qt::DisplayRole, tr("Inactive (%1)").arg(nbInactive)); + item(TorrentFilter::Errored)->setData(Qt::DisplayRole, tr("Errored (%1)").arg(nbErrored)); } void StatusFilterWidget::showMenu(const QPoint &) {}