From d357cdd5f9998cc9c3696235d3b06ac4cb249c93 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 21 Jul 2023 00:57:43 +0800 Subject: [PATCH 1/4] Set power state to idle when deconstructing class --- src/gui/powermanagement/powermanagement.cpp | 23 ++++++++++++++------- src/gui/powermanagement/powermanagement.h | 4 ++-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/gui/powermanagement/powermanagement.cpp b/src/gui/powermanagement/powermanagement.cpp index dfdfb6e54..2fbecec45 100644 --- a/src/gui/powermanagement/powermanagement.cpp +++ b/src/gui/powermanagement/powermanagement.cpp @@ -44,10 +44,15 @@ PowerManagement::PowerManagement(QObject *parent) : QObject(parent) -{ #ifdef QBT_USES_DBUS - m_inhibitor = new PowerManagementInhibitor(this); + , m_inhibitor {new PowerManagementInhibitor(this)} #endif +{ +} + +PowerManagement::~PowerManagement() +{ + setIdle(); } void PowerManagement::setActivityState(const bool busy) @@ -60,15 +65,16 @@ void PowerManagement::setActivityState(const bool busy) void PowerManagement::setBusy() { - if (m_busy) return; + if (m_busy) + return; m_busy = true; #ifdef Q_OS_WIN - SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED); + ::SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED); #elif defined(QBT_USES_DBUS) m_inhibitor->requestBusy(); #elif defined(Q_OS_MACOS) - IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn + const IOReturn success = ::IOPMAssertionCreateWithName(kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn , tr("qBittorrent is active").toCFString(), &m_assertionID); if (success != kIOReturnSuccess) m_busy = false; @@ -77,14 +83,15 @@ void PowerManagement::setBusy() void PowerManagement::setIdle() { - if (!m_busy) return; + if (!m_busy) + return; m_busy = false; #ifdef Q_OS_WIN - SetThreadExecutionState(ES_CONTINUOUS); + ::SetThreadExecutionState(ES_CONTINUOUS); #elif defined(QBT_USES_DBUS) m_inhibitor->requestIdle(); #elif defined(Q_OS_MACOS) - IOPMAssertionRelease(m_assertionID); + ::IOPMAssertionRelease(m_assertionID); #endif } diff --git a/src/gui/powermanagement/powermanagement.h b/src/gui/powermanagement/powermanagement.h index 6fc42c679..39bbcf6d0 100644 --- a/src/gui/powermanagement/powermanagement.h +++ b/src/gui/powermanagement/powermanagement.h @@ -40,14 +40,14 @@ class PowerManagementInhibitor; #endif -class PowerManagement : public QObject +class PowerManagement final : public QObject { Q_OBJECT Q_DISABLE_COPY_MOVE(PowerManagement) public: PowerManagement(QObject *parent = nullptr); - virtual ~PowerManagement() = default; + ~PowerManagement() override; void setActivityState(bool busy); From cffcf5783f045b4ce38c9a957a3e1a08443ce06d Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 21 Jul 2023 01:06:20 +0800 Subject: [PATCH 2/4] Avoid excessive power management updates --- src/gui/mainwindow.cpp | 15 +++++++++------ src/gui/mainwindow.h | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index f3548d731..0f6a2d82e 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -323,8 +323,10 @@ MainWindow::MainWindow(IGUIApplication *app, WindowState initialState) // Initialise system sleep inhibition timer m_pwr = new PowerManagement(this); m_preventTimer = new QTimer(this); + m_preventTimer->setSingleShot(true); connect(m_preventTimer, &QTimer::timeout, this, &MainWindow::updatePowerManagementState); - m_preventTimer->start(PREVENT_SUSPEND_INTERVAL); + connect(pref, &Preferences::changed, this, &MainWindow::updatePowerManagementState); + updatePowerManagementState(); // Configure BT session according to options loadPreferences(); @@ -1465,8 +1467,6 @@ void MainWindow::loadPreferences() showStatusBar(pref->isStatusbarDisplayed()); - updatePowerManagementState(); - m_transferListWidget->setAlternatingRowColors(pref->useAlternatingRowColors()); m_propertiesWidget->getFilesList()->setAlternatingRowColors(pref->useAlternatingRowColors()); m_propertiesWidget->getTrackerList()->setAlternatingRowColors(pref->useAlternatingRowColors()); @@ -1927,10 +1927,11 @@ void MainWindow::on_actionAutoShutdown_toggled(bool enabled) Preferences::instance()->setShutdownWhenDownloadsComplete(enabled); } -void MainWindow::updatePowerManagementState() +void MainWindow::updatePowerManagementState() const { - const bool preventFromSuspendWhenDownloading = Preferences::instance()->preventFromSuspendWhenDownloading(); - const bool preventFromSuspendWhenSeeding = Preferences::instance()->preventFromSuspendWhenSeeding(); + const auto *pref = Preferences::instance(); + const bool preventFromSuspendWhenDownloading = pref->preventFromSuspendWhenDownloading(); + const bool preventFromSuspendWhenSeeding = pref->preventFromSuspendWhenSeeding(); const QVector allTorrents = BitTorrent::Session::instance()->torrents(); const bool inhibitSuspend = std::any_of(allTorrents.cbegin(), allTorrents.cend(), [&](const BitTorrent::Torrent *torrent) @@ -1944,6 +1945,8 @@ void MainWindow::updatePowerManagementState() return torrent->isMoving(); }); m_pwr->setActivityState(inhibitSuspend); + + m_preventTimer->start(PREVENT_SUSPEND_INTERVAL); } void MainWindow::applyTransferListFilter() diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index 0566c7050..5b7e389e5 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -164,7 +164,7 @@ private slots: void on_actionExit_triggered(); void on_actionLock_triggered(); // Check for unpaused downloading or seeding torrents and prevent system suspend/sleep according to preferences - void updatePowerManagementState(); + void updatePowerManagementState() const; void toolbarMenuRequested(); void toolbarIconsOnly(); From 0bcc1cf4a06a422aaf733bccae1f64e324ce7e89 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 21 Jul 2023 20:41:00 +0800 Subject: [PATCH 3/4] Fix indentation --- src/gui/powermanagement/powermanagement.h | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/gui/powermanagement/powermanagement.h b/src/gui/powermanagement/powermanagement.h index 39bbcf6d0..f5168c1df 100644 --- a/src/gui/powermanagement/powermanagement.h +++ b/src/gui/powermanagement/powermanagement.h @@ -36,31 +36,30 @@ #endif #ifdef QBT_USES_DBUS -// Require DBus class PowerManagementInhibitor; #endif class PowerManagement final : public QObject { - Q_OBJECT - Q_DISABLE_COPY_MOVE(PowerManagement) + Q_OBJECT + Q_DISABLE_COPY_MOVE(PowerManagement) public: - PowerManagement(QObject *parent = nullptr); - ~PowerManagement() override; + PowerManagement(QObject *parent = nullptr); + ~PowerManagement() override; - void setActivityState(bool busy); + void setActivityState(bool busy); private: - void setBusy(); - void setIdle(); + void setBusy(); + void setIdle(); - bool m_busy = false; + bool m_busy = false; #ifdef QBT_USES_DBUS - PowerManagementInhibitor *m_inhibitor = nullptr; + PowerManagementInhibitor *m_inhibitor = nullptr; #endif #ifdef Q_OS_MACOS - IOPMAssertionID m_assertionID {}; + IOPMAssertionID m_assertionID {}; #endif }; From 1874fd7f9393117950e3ddb2dc4d1a15f52db5cb Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 22 Jul 2023 01:53:39 +0800 Subject: [PATCH 4/4] Fix incorrect state Fix up f3f9cfe44ed49d5bf3de0e318fa9c8ec48645ca4. --- src/gui/powermanagement/powermanagement_x11.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/gui/powermanagement/powermanagement_x11.cpp b/src/gui/powermanagement/powermanagement_x11.cpp index 6ade281fa..ed3b16079 100644 --- a/src/gui/powermanagement/powermanagement_x11.cpp +++ b/src/gui/powermanagement/powermanagement_x11.cpp @@ -80,15 +80,15 @@ void PowerManagementInhibitor::requestIdle() if ((m_state == Error) || (m_state == Idle) || (m_state == RequestIdle) || (m_state == RequestBusy)) return; - m_state = RequestIdle; - if (m_manager == ManagerType::Systemd) { - QDBusUnixFileDescriptor dummy; - m_fd.swap(dummy); + m_fd = {}; + m_state = Idle; return; } + m_state = RequestIdle; + const QString method = (m_manager == ManagerType::Gnome) ? u"Uninhibit"_s : u"UnInhibit"_s;