From 48cd993c92e182ccd92568e1bde089bf12117f9d Mon Sep 17 00:00:00 2001 From: Lukas Greib Date: Thu, 2 Aug 2018 15:45:21 -0400 Subject: [PATCH] Inhibit sleep regardless of activity "Active torrents" is a somewhat unintuitive concept as a basis for preventing sleep, as torrents can become active or inactive on the network at any time. This brings some predictability to the inhibit sleep option, and will inhibit sleep as long as there are unpaused downloads or uploads, regardless of network activity. Closes #1696, #4592, #4655, #7019, #7159, #7452 --- src/base/bittorrent/session.cpp | 24 +++++++++++++++--------- src/base/bittorrent/session.h | 1 + src/base/preferences.cpp | 31 +++++++++++++++++++++++++------ src/base/preferences.h | 6 ++++-- src/gui/mainwindow.cpp | 10 ++++++---- src/gui/mainwindow.h | 4 ++-- src/gui/optionsdialog.cpp | 17 ++++++++--------- src/gui/optionsdialog.h | 1 - src/gui/optionsdialog.ui | 14 +++++++++++--- 9 files changed, 72 insertions(+), 36 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 8171d321d..2519dba6d 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -1863,20 +1863,26 @@ TorrentHandle *Session::findTorrent(const InfoHash &hash) const bool Session::hasActiveTorrents() const { - foreach (TorrentHandle *const torrent, m_torrents) - if (TorrentFilter::ActiveTorrent.match(torrent)) - return true; - - return false; + return std::any_of(m_torrents.begin(), m_torrents.end(), [](TorrentHandle *torrent) + { + return TorrentFilter::ActiveTorrent.match(torrent); + }); } bool Session::hasUnfinishedTorrents() const { - foreach (TorrentHandle *const torrent, m_torrents) - if (!torrent->isSeed() && !torrent->isPaused()) - return true; + return std::any_of(m_torrents.begin(), m_torrents.end(), [](const TorrentHandle *torrent) + { + return (!torrent->isSeed() && !torrent->isPaused()); + }); +} - return false; +bool Session::hasRunningSeed() const +{ + return std::any_of(m_torrents.begin(), m_torrents.end(), [](const TorrentHandle *torrent) + { + return (torrent->isSeed() && !torrent->isPaused()); + }); } void Session::banIP(const QString &ip) diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index 1d41eec03..03859ae5b 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -454,6 +454,7 @@ namespace BitTorrent TorrentStatusReport torrentStatusReport() const; bool hasActiveTorrents() const; bool hasUnfinishedTorrents() const; + bool hasRunningSeed() const; const SessionStatus &status() const; const CacheStatus &cacheStatus() const; quint64 getAlltimeDL() const; diff --git a/src/base/preferences.cpp b/src/base/preferences.cpp index b02b1e5b5..831140cab 100644 --- a/src/base/preferences.cpp +++ b/src/base/preferences.cpp @@ -235,14 +235,24 @@ void Preferences::setSplashScreenDisabled(bool b) } // Preventing from system suspend while active torrents are presented. -bool Preferences::preventFromSuspend() const +bool Preferences::preventFromSuspendWhenDownloading() const { - return value("Preferences/General/PreventFromSuspend", false).toBool(); + return value("Preferences/General/PreventFromSuspendWhenDownloading", false).toBool(); } -void Preferences::setPreventFromSuspend(bool b) +void Preferences::setPreventFromSuspendWhenDownloading(bool b) { - setValue("Preferences/General/PreventFromSuspend", b); + setValue("Preferences/General/PreventFromSuspendWhenDownloading", b); +} + +bool Preferences::preventFromSuspendWhenSeeding() const +{ + return value("Preferences/General/PreventFromSuspendWhenSeeding", false).toBool(); +} + +void Preferences::setPreventFromSuspendWhenSeeding(bool b) +{ + setValue("Preferences/General/PreventFromSuspendWhenSeeding", b); } #ifdef Q_OS_WIN @@ -1552,6 +1562,8 @@ void Preferences::setSpeedWidgetGraphEnable(int id, const bool enable) void Preferences::upgrade() { + SettingsStorage *settingsStorage = SettingsStorage::instance(); + QStringList labels = value("TransferListFilters/customLabels").toStringList(); if (!labels.isEmpty()) { QVariantMap categories = value("BitTorrent/Session/Categories").toMap(); @@ -1560,10 +1572,17 @@ void Preferences::upgrade() categories[label] = ""; } setValue("BitTorrent/Session/Categories", categories); - SettingsStorage::instance()->removeValue("TransferListFilters/customLabels"); + settingsStorage->removeValue("TransferListFilters/customLabels"); } - SettingsStorage::instance()->removeValue("Preferences/Downloads/AppendLabel"); + settingsStorage->removeValue("Preferences/Downloads/AppendLabel"); + + // Inhibit sleep based on running downloads/available seeds rather than network activity. + if (value("Preferences/General/PreventFromSuspend", false).toBool()) { + setPreventFromSuspendWhenDownloading(true); + setPreventFromSuspendWhenSeeding(true); + } + settingsStorage->removeValue("Preferences/General/PreventFromSuspend"); } void Preferences::apply() diff --git a/src/base/preferences.h b/src/base/preferences.h index 136ad106f..77f85af72 100644 --- a/src/base/preferences.h +++ b/src/base/preferences.h @@ -123,8 +123,10 @@ public: void setStartMinimized(bool b); bool isSplashScreenDisabled() const; void setSplashScreenDisabled(bool b); - bool preventFromSuspend() const; - void setPreventFromSuspend(bool b); + bool preventFromSuspendWhenDownloading() const; + void setPreventFromSuspendWhenDownloading(bool b); + bool preventFromSuspendWhenSeeding() const; + void setPreventFromSuspendWhenSeeding(bool b); #ifdef Q_OS_WIN bool WinStartup() const; void setWinStartup(bool b); diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 396e4a087..5e8e87d6b 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -342,7 +342,7 @@ MainWindow::MainWindow(QWidget *parent) m_pwr = new PowerManagement(this); m_preventTimer = new QTimer(this); - connect(m_preventTimer, &QTimer::timeout, this, &MainWindow::checkForActiveTorrents); + connect(m_preventTimer, &QTimer::timeout, this, &MainWindow::updatePowerManagementState); // Configure BT session according to options loadPreferences(false); @@ -1445,7 +1445,7 @@ void MainWindow::loadPreferences(bool configureSession) showStatusBar(pref->isStatusbarDisplayed()); - if (pref->preventFromSuspend() && !m_preventTimer->isActive()) { + if ((pref->preventFromSuspendWhenDownloading() || pref->preventFromSuspendWhenSeeding()) && !m_preventTimer->isActive()) { m_preventTimer->start(PREVENT_SUSPEND_INTERVAL); } else { @@ -1971,9 +1971,11 @@ void MainWindow::on_actionAutoShutdown_toggled(bool enabled) Preferences::instance()->setShutdownWhenDownloadsComplete(enabled); } -void MainWindow::checkForActiveTorrents() +void MainWindow::updatePowerManagementState() { - m_pwr->setActivityState(BitTorrent::Session::instance()->hasActiveTorrents()); + const bool inhibitSuspend = (Preferences::instance()->preventFromSuspendWhenDownloading() && BitTorrent::Session::instance()->hasUnfinishedTorrents()) + || (Preferences::instance()->preventFromSuspendWhenSeeding() && BitTorrent::Session::instance()->hasRunningSeed()); + m_pwr->setActivityState(inhibitSuspend); } #ifndef Q_OS_MAC diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index d8625779f..07c4440af 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -173,8 +173,8 @@ private slots: void on_actionDownloadFromURL_triggered(); void on_actionExit_triggered(); void on_actionLock_triggered(); - // Check for active torrents and set preventing from suspend state - void checkForActiveTorrents(); + // Check for unpaused downloading or seeding torrents and prevent system suspend/sleep according to preferences + void updatePowerManagementState(); #if defined(Q_OS_WIN) || defined(Q_OS_MAC) void checkProgramUpdate(); #endif diff --git a/src/gui/optionsdialog.cpp b/src/gui/optionsdialog.cpp index e64c97ab0..44209f49a 100644 --- a/src/gui/optionsdialog.cpp +++ b/src/gui/optionsdialog.cpp @@ -225,10 +225,12 @@ OptionsDialog::OptionsDialog(QWidget *parent) connect(m_ui->checkShowSplash, &QAbstractButton::toggled, this, &ThisType::enableApplyButton); connect(m_ui->checkProgramExitConfirm, &QAbstractButton::toggled, this, &ThisType::enableApplyButton); connect(m_ui->checkProgramAutoExitConfirm, &QAbstractButton::toggled, this, &ThisType::enableApplyButton); - connect(m_ui->checkPreventFromSuspend, &QAbstractButton::toggled, this, &ThisType::enableApplyButton); + connect(m_ui->checkPreventFromSuspendWhenDownloading, &QAbstractButton::toggled, this, &ThisType::enableApplyButton); + connect(m_ui->checkPreventFromSuspendWhenSeeding, &QAbstractButton::toggled, this, &ThisType::enableApplyButton); connect(m_ui->comboTrayIcon, qComboBoxCurrentIndexChanged, this, &ThisType::enableApplyButton); #if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC)) && !defined(QT_DBUS_LIB) - m_ui->checkPreventFromSuspend->setDisabled(true); + m_ui->checkPreventFromSuspendWhenDownloading->setDisabled(true); + m_ui->checkPreventFromSuspendWhenSeeding->setDisabled(true); #endif #if defined(Q_OS_WIN) || defined(Q_OS_MAC) connect(m_ui->checkAssociateTorrents, &QAbstractButton::toggled, this, &ThisType::enableApplyButton); @@ -556,7 +558,8 @@ void OptionsDialog::saveOptions() pref->setSplashScreenDisabled(isSplashScreenDisabled()); pref->setConfirmOnExit(m_ui->checkProgramExitConfirm->isChecked()); pref->setDontConfirmAutoExit(!m_ui->checkProgramAutoExitConfirm->isChecked()); - pref->setPreventFromSuspend(preventFromSuspend()); + pref->setPreventFromSuspendWhenDownloading(m_ui->checkPreventFromSuspendWhenDownloading->isChecked()); + pref->setPreventFromSuspendWhenSeeding(m_ui->checkPreventFromSuspendWhenSeeding->isChecked()); #ifdef Q_OS_WIN pref->setWinStartup(WinStartup()); // Windows: file association settings @@ -793,7 +796,8 @@ void OptionsDialog::loadOptions() } #endif - m_ui->checkPreventFromSuspend->setChecked(pref->preventFromSuspend()); + m_ui->checkPreventFromSuspendWhenDownloading->setChecked(pref->preventFromSuspendWhenDownloading()); + m_ui->checkPreventFromSuspendWhenSeeding->setChecked(pref->preventFromSuspendWhenSeeding()); #ifdef Q_OS_WIN m_ui->checkStartup->setChecked(pref->WinStartup()); @@ -1407,11 +1411,6 @@ bool OptionsDialog::WinStartup() const } #endif -bool OptionsDialog::preventFromSuspend() const -{ - return m_ui->checkPreventFromSuspend->isChecked(); -} - bool OptionsDialog::preAllocateAllFiles() const { return m_ui->checkPreallocateAll->isChecked(); diff --git a/src/gui/optionsdialog.h b/src/gui/optionsdialog.h index 36d34513b..bac2945d7 100644 --- a/src/gui/optionsdialog.h +++ b/src/gui/optionsdialog.h @@ -122,7 +122,6 @@ private: #endif bool startMinimized() const; bool isSplashScreenDisabled() const; - bool preventFromSuspend() const; #ifdef Q_OS_WIN bool WinStartup() const; #endif diff --git a/src/gui/optionsdialog.ui b/src/gui/optionsdialog.ui index fa7a0688d..dcfc08e94 100644 --- a/src/gui/optionsdialog.ui +++ b/src/gui/optionsdialog.ui @@ -479,9 +479,16 @@ - + - Inhibit system sleep when torrents are active + Inhibit system sleep when torrents are downloading + + + + + + + Inhibit system sleep when torrents are seeding @@ -3369,7 +3376,8 @@ Use ';' to split multiple entries. Can use wildcard '*'. comboTrayIcon checkAssociateTorrents checkAssociateMagnetLinks - checkPreventFromSuspend + checkPreventFromSuspendWhenDownloading + checkPreventFromSuspendWhenSeeding checkAdditionDialog checkAdditionDialogFront checkPreallocateAll