1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
From ad8d2273b9169564c95acbe58e7a78da37e14a2d Mon Sep 17 00:00:00 2001
From: Vlad Zahorodnii <vlad.zahorodnii@kde.org>
Date: Wed, 23 Apr 2025 11:25:35 +0300
Subject: [PATCH] Use std::expected to clean up PowerInhibitor
---
interface.cpp | 23 +++++++++--------------
interface.h | 5 +++--
2 files changed, 12 insertions(+), 16 deletions(-)
diff --git a/interface.cpp b/interface.cpp
index 2b73b677..e4a83f01 100644
--- a/interface.cpp
+++ b/interface.cpp
@@ -38,10 +38,7 @@ PowerInhibitor::~PowerInhibitor()
void PowerInhibitor::release()
{
m_released = true;
- // TODO: See how to clean up this code.
- if (m_failed) {
- deleteLater();
- } else if (m_cookie) {
+ if (m_cookie.has_value()) {
uninhibit();
}
}
@@ -60,27 +57,25 @@ void PowerInhibitor::inhibit(const QString &applicationName, const QString &reas
if (!reply.isValid()) {
qCWarning(KSCREENLOCKER) << "org.kde.Solid.PowerManagement.PolicyAgent.AddInhibition failed:" << reply.error();
- m_failed = true;
+ m_cookie = std::unexpected(reply.error());
} else {
m_cookie = reply.value();
}
if (m_released) {
- if (m_failed) {
- deleteLater();
- } else {
- uninhibit();
- }
+ uninhibit();
}
});
}
void PowerInhibitor::uninhibit()
{
- OrgKdeSolidPowerManagementPolicyAgentInterface policyAgent(QStringLiteral("org.kde.Solid.PowerManagement.PolicyAgent"),
- QStringLiteral("/org/kde/Solid/PowerManagement/PolicyAgent"),
- QDBusConnection::sessionBus());
- policyAgent.ReleaseInhibition(m_cookie.value());
+ if (const auto &result = m_cookie.value()) {
+ OrgKdeSolidPowerManagementPolicyAgentInterface policyAgent(QStringLiteral("org.kde.Solid.PowerManagement.PolicyAgent"),
+ QStringLiteral("/org/kde/Solid/PowerManagement/PolicyAgent"),
+ QDBusConnection::sessionBus());
+ policyAgent.ReleaseInhibition(result.value());
+ }
deleteLater();
}
diff --git a/interface.h b/interface.h
index fac7842a..21447810 100644
--- a/interface.h
+++ b/interface.h
@@ -10,6 +10,8 @@ SPDX-License-Identifier: GPL-2.0-or-later
#include <QDBusMessage>
#include <QObject>
+#include <expected>
+
class QDBusServiceWatcher;
namespace ScreenLocker
@@ -29,8 +31,7 @@ private:
void inhibit(const QString &applicationName, const QString &reason);
void uninhibit();
- std::optional<uint> m_cookie;
- bool m_failed = false;
+ std::optional<std::expected<uint, QDBusError>> m_cookie;
bool m_released = false;
};
--
GitLab
|