Skip to content

Commit

Permalink
Calculate session's download and upload rate from torrent updates
Browse files Browse the repository at this point in the history
Libtorrent stats and torrent updates can't be taken at same time, this causes out of sync speed reporting in different parts of GUI like in status bar, this commits adds two new fields in Session class i.e downloadRate and uploadRate which are calculated from torrent updates, they can be used to show global rates in GUI

Use Session::uploadRate and Session::downloadRate in StatusBar and Main Window for reporting global rates
  • Loading branch information
jagannatharjun committed Dec 7, 2020
1 parent 7016cba commit a25f185
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
17 changes: 17 additions & 0 deletions src/base/bittorrent/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2597,6 +2597,16 @@ void Session::configureListeningInterface()
configureDeferred();
}

int Session::downloadRate() const
{
return m_downloadRate;
}

int Session::uploadRate() const
{
return m_uploadRate;
}

int Session::globalDownloadSpeedLimit() const
{
// Unfortunately the value was saved as KiB instead of B.
Expand Down Expand Up @@ -4956,6 +4966,9 @@ void Session::handleStateUpdateAlert(const lt::state_update_alert *p)
QVector<TorrentHandle *> updatedTorrents;
updatedTorrents.reserve(p->status.size());

m_downloadRate = 0;
m_uploadRate = 0;

for (const lt::torrent_status &status : p->status)
{
TorrentHandleImpl *const torrent = m_torrents.value(status.info_hash);
Expand All @@ -4965,8 +4978,12 @@ void Session::handleStateUpdateAlert(const lt::state_update_alert *p)

torrent->handleStateUpdate(status);
updatedTorrents.push_back(torrent);

m_downloadRate += torrent->downloadPayloadRate();
m_uploadRate += torrent->uploadPayloadRate();
}

emit speedRatesUpdated();
if (!updatedTorrents.isEmpty())
emit torrentsUpdated(updatedTorrents);

Expand Down
5 changes: 5 additions & 0 deletions src/base/bittorrent/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ namespace BitTorrent
QString finishedTorrentExportDirectory() const;
void setFinishedTorrentExportDirectory(QString path);

int downloadRate() const;
int uploadRate() const;
int globalDownloadSpeedLimit() const;
void setGlobalDownloadSpeedLimit(int limit);
int globalUploadSpeedLimit() const;
Expand Down Expand Up @@ -494,6 +496,7 @@ namespace BitTorrent
void findIncompleteFiles(const TorrentInfo &torrentInfo, const QString &savePath) const;

signals:
void speedRatesUpdated();
void allTorrentsFinished();
void categoryAdded(const QString &categoryName);
void categoryRemoved(const QString &categoryName);
Expand Down Expand Up @@ -746,6 +749,8 @@ namespace BitTorrent

int m_numResumeData = 0;
int m_extraLimit = 0;
int m_downloadRate = 0;
int m_uploadRate = 0;
QVector<TrackerEntry> m_additionalTrackerList;
QString m_resumeFolderPath;
QFile *m_resumeFolderLock = nullptr;
Expand Down
21 changes: 11 additions & 10 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ MainWindow::MainWindow(QWidget *parent)
// Configure BT session according to options
loadPreferences(false);

connect(BitTorrent::Session::instance(), &BitTorrent::Session::statsUpdated, this, &MainWindow::reloadSessionStats);
connect(BitTorrent::Session::instance(), &BitTorrent::Session::speedRatesUpdated, this, &MainWindow::reloadSessionSpeeds);
connect(BitTorrent::Session::instance(), &BitTorrent::Session::torrentsUpdated, this, &MainWindow::reloadTorrentStats);

// Accept drag 'n drops
Expand Down Expand Up @@ -1644,16 +1644,17 @@ void MainWindow::loadPreferences(const bool configureSession)
qDebug("GUI settings loaded");
}

void MainWindow::reloadSessionStats()
void MainWindow::reloadSessionSpeeds()
{
const BitTorrent::SessionStatus &status = BitTorrent::Session::instance()->status();
const int downloadRate = BitTorrent::Session::instance()->downloadRate();
const int uploadRate = BitTorrent::Session::instance()->uploadRate();

// update global information
#ifdef Q_OS_MACOS
if (status.payloadDownloadRate > 0)
if (downloadRate > 0)
{
QtMac::setBadgeLabelText(tr("%1/s", "s is a shorthand for seconds")
.arg(Utils::Misc::friendlyUnit(status.payloadDownloadRate)));
.arg(Utils::Misc::friendlyUnit(downloadRate)));
}
else if (!QtMac::badgeLabelText().isEmpty())
{
Expand All @@ -1663,17 +1664,17 @@ void MainWindow::reloadSessionStats()
if (m_systrayIcon)
{
const QString toolTip = QString::fromLatin1("%1\n%2").arg(
tr("DL speed: %1", "e.g: Download speed: 10 KiB/s").arg(Utils::Misc::friendlyUnit(status.payloadDownloadRate, true))
, tr("UP speed: %1", "e.g: Upload speed: 10 KiB/s").arg(Utils::Misc::friendlyUnit(status.payloadUploadRate, true)));
tr("DL speed: %1", "e.g: Download speed: 10 KiB/s").arg(Utils::Misc::friendlyUnit(downloadRate, true))
, tr("UP speed: %1", "e.g: Upload speed: 10 KiB/s").arg(Utils::Misc::friendlyUnit(uploadRate, true)));
m_systrayIcon->setToolTip(toolTip); // tray icon
}
#endif // Q_OS_MACOS

if (m_displaySpeedInTitle)
{
setWindowTitle(tr("[D: %1, U: %2] qBittorrent %3", "D = Download; U = Upload; %3 is qBittorrent version")
.arg(Utils::Misc::friendlyUnit(status.payloadDownloadRate, true)
, Utils::Misc::friendlyUnit(status.payloadUploadRate, true)
.arg(Utils::Misc::friendlyUnit(downloadRate, true)
, Utils::Misc::friendlyUnit(uploadRate, true)
, QBT_VERSION));
}
}
Expand Down Expand Up @@ -1861,7 +1862,7 @@ void MainWindow::on_actionSpeedInTitleBar_triggered()
m_displaySpeedInTitle = static_cast<QAction * >(sender())->isChecked();
Preferences::instance()->showSpeedInTitleBar(m_displaySpeedInTitle);
if (m_displaySpeedInTitle)
reloadSessionStats();
reloadSessionSpeeds();
else
setWindowTitle("qBittorrent " QBT_VERSION);
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private slots:
void displayRSSTab();
void displayExecutionLogTab();
void focusSearchFilter();
void reloadSessionStats();
void reloadSessionSpeeds();
void reloadTorrentStats(const QVector<BitTorrent::TorrentHandle *> &torrents);
void loadPreferences(bool configureSession = true);
void addTorrentFailed(const QString &error) const;
Expand Down
13 changes: 9 additions & 4 deletions src/gui/statusbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ StatusBar::StatusBar(QWidget *parent)
// Is DHT enabled
m_DHTLbl->setVisible(session->isDHTEnabled());
refresh();
updateSpeedLabels();
connect(session, &BitTorrent::Session::statsUpdated, this, &StatusBar::refresh);
connect(session, &BitTorrent::Session::speedRatesUpdated, this, &StatusBar::updateSpeedLabels);
}

StatusBar::~StatusBar()
Expand Down Expand Up @@ -210,16 +212,17 @@ void StatusBar::updateDHTNodesNumber()

void StatusBar::updateSpeedLabels()
{
const BitTorrent::SessionStatus &sessionStatus = BitTorrent::Session::instance()->status();
const BitTorrent::Session *session = BitTorrent::Session::instance();
const BitTorrent::SessionStatus &sessionStatus = session->status();

QString dlSpeedLbl = Utils::Misc::friendlyUnit(sessionStatus.payloadDownloadRate, true);
QString dlSpeedLbl = Utils::Misc::friendlyUnit(session->downloadRate(), true);
const int dlSpeedLimit = BitTorrent::Session::instance()->downloadSpeedLimit();
if (dlSpeedLimit > 0)
dlSpeedLbl += " [" + Utils::Misc::friendlyUnit(dlSpeedLimit, true) + ']';
dlSpeedLbl += " (" + Utils::Misc::friendlyUnit(sessionStatus.totalPayloadDownload) + ')';
m_dlSpeedLbl->setText(dlSpeedLbl);

QString upSpeedLbl = Utils::Misc::friendlyUnit(sessionStatus.payloadUploadRate, true);
QString upSpeedLbl = Utils::Misc::friendlyUnit(session->uploadRate(), true);
const int upSpeedLimit = BitTorrent::Session::instance()->uploadSpeedLimit();
if (upSpeedLimit > 0)
upSpeedLbl += " [" + Utils::Misc::friendlyUnit(upSpeedLimit, true) + ']';
Expand All @@ -231,7 +234,6 @@ void StatusBar::refresh()
{
updateConnectionStatus();
updateDHTNodesNumber();
updateSpeedLabels();
}

void StatusBar::updateAltSpeedsBtn(bool alternative)
Expand All @@ -249,6 +251,7 @@ void StatusBar::updateAltSpeedsBtn(bool alternative)
m_altSpeedsBtn->setDown(false);
}
refresh();
updateSpeedLabels();
}

void StatusBar::capDownloadSpeed()
Expand All @@ -263,6 +266,7 @@ void StatusBar::capDownloadSpeed()
qDebug("Setting global download rate limit to %.1fKb/s", newLimit / 1024.);
session->setDownloadSpeedLimit(newLimit);
refresh();
updateSpeedLabels();
}
}

Expand All @@ -278,5 +282,6 @@ void StatusBar::capUploadSpeed()
qDebug("Setting global upload rate limit to %.1fKb/s", newLimit / 1024.);
session->setUploadSpeedLimit(newLimit);
refresh();
updateSpeedLabels();
}
}

0 comments on commit a25f185

Please sign in to comment.