From bd31eddb941424b09a4323bb5bbce586524ee1f6 Mon Sep 17 00:00:00 2001 From: Sentox6 <71313530+Sentox6@users.noreply.github.com> Date: Mon, 17 Apr 2023 03:09:34 +1200 Subject: [PATCH] Inhibit system sleep while torrents are moving PR #18783. --- src/gui/mainwindow.cpp | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index f2e793c8f..3a554e08b 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -311,9 +311,11 @@ MainWindow::MainWindow(IGUIApplication *app, WindowState initialState) connect(m_ui->actionManageCookies, &QAction::triggered, this, &MainWindow::manageCookies); + // Initialise system sleep inhibition timer m_pwr = new PowerManagement(this); m_preventTimer = new QTimer(this); connect(m_preventTimer, &QTimer::timeout, this, &MainWindow::updatePowerManagementState); + m_preventTimer->start(PREVENT_SUSPEND_INTERVAL); // Configure BT session according to options loadPreferences(); @@ -1448,19 +1450,7 @@ void MainWindow::loadPreferences() showStatusBar(pref->isStatusbarDisplayed()); - if (pref->preventFromSuspendWhenDownloading() || pref->preventFromSuspendWhenSeeding()) - { - if (!m_preventTimer->isActive()) - { - updatePowerManagementState(); - m_preventTimer->start(PREVENT_SUSPEND_INTERVAL); - } - } - else - { - m_preventTimer->stop(); - m_pwr->setActivityState(false); - } + updatePowerManagementState(); m_transferListWidget->setAlternatingRowColors(pref->useAlternatingRowColors()); m_propertiesWidget->getFilesList()->setAlternatingRowColors(pref->useAlternatingRowColors()); @@ -1924,17 +1914,20 @@ void MainWindow::on_actionAutoShutdown_toggled(bool enabled) void MainWindow::updatePowerManagementState() { + const bool preventFromSuspendWhenDownloading = Preferences::instance()->preventFromSuspendWhenDownloading(); + const bool preventFromSuspendWhenSeeding = Preferences::instance()->preventFromSuspendWhenSeeding(); + const QVector allTorrents = BitTorrent::Session::instance()->torrents(); - const bool hasUnfinishedTorrents = std::any_of(allTorrents.cbegin(), allTorrents.cend(), [](const BitTorrent::Torrent *torrent) - { - return (!torrent->isFinished() && !torrent->isPaused() && !torrent->isErrored() && torrent->hasMetadata()); - }); - const bool hasRunningSeed = std::any_of(allTorrents.cbegin(), allTorrents.cend(), [](const BitTorrent::Torrent *torrent) + const bool inhibitSuspend = std::any_of(allTorrents.cbegin(), allTorrents.cend(), [&](const BitTorrent::Torrent *torrent) { - return (torrent->isFinished() && !torrent->isPaused()); + if (preventFromSuspendWhenDownloading && (!torrent->isFinished() && !torrent->isPaused() && !torrent->isErrored() && torrent->hasMetadata())) + return true; + + if (preventFromSuspendWhenSeeding && (torrent->isFinished() && !torrent->isPaused())) + return true; + + return torrent->isMoving(); }); - const bool inhibitSuspend = (Preferences::instance()->preventFromSuspendWhenDownloading() && hasUnfinishedTorrents) - || (Preferences::instance()->preventFromSuspendWhenSeeding() && hasRunningSeed); m_pwr->setActivityState(inhibitSuspend); }