From be5fbd89a3bb0611e117c71325a34f417c19287a Mon Sep 17 00:00:00 2001
From: tangjie02 <tangjie02@kylinsec.com.cn>
Date: Tue, 9 May 2023 16:59:23 +0800
Subject: [PATCH] feature(power): Delete LockScreenWhenHibernate and
 LockScreenWhenSuspend functions and replace suspend/hibernate/shutdown api
 with SessionManager.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- 删除当计算机休眠/待机时是否锁屏功能，将该功能移动到会话管理实现

Relates #68459

Signed-off-by: tangjie02 <tangjie02@kylinsec.com.cn>
---
 .../com.kylinsec.kiran.power.gschema.xml.in   |  10 --
 include/power-i.h                             |   4 -
 ...com.kylinsec.Kiran.SessionDaemon.Power.xml |  24 ----
 plugins/power/power-manager.cpp               |  34 ------
 plugins/power/power-manager.h                 |   8 --
 plugins/power/save/power-save-computer.cpp    |  40 +------
 plugins/power/save/power-save-computer.h      |   4 +-
 plugins/power/wrapper/power-session.cpp       | 111 ++++++++++++++++++
 plugins/power/wrapper/power-session.h         |  10 ++
 9 files changed, 126 insertions(+), 119 deletions(-)

diff --git a/data/schemas/com.kylinsec.kiran.power.gschema.xml.in b/data/schemas/com.kylinsec.kiran.power.gschema.xml.in
index ab51e40..094ea2f 100644
--- a/data/schemas/com.kylinsec.kiran.power.gschema.xml.in
+++ b/data/schemas/com.kylinsec.kiran.power.gschema.xml.in
@@ -97,16 +97,6 @@
             <description>Whether does the cpu save energy when the ups or battery power is low.</description>
         </key>
 
-        <key name="screen-locked-when-suspend" type="b">
-            <default>true</default>
-            <description>Whether lock screen when the computer is suspend.</description>
-        </key>
-
-        <key name="screen-locked-when-hibernate" type="b">
-            <default>true</default>
-            <description>Whether lock screen when the computer is hibernate.</description>
-        </key>
-
         <key name="button-suspend-action" enum="com.kylinsec.kiran.power.event-action">
             <default>'suspend'</default>
             <description>The action to take when the system suspend button is pressed.</description>
diff --git a/include/power-i.h b/include/power-i.h
index 2aba2fc..8d33a91 100644
--- a/include/power-i.h
+++ b/include/power-i.h
@@ -141,10 +141,6 @@ extern "C"
 #define POWER_SCHEMA_ENABLE_CHARGE_LOW_DIMMED "enable-charge-low-dimmed"
 // 电量过低时计算机是否进入节能模式
 #define POWER_SCHEMA_ENABLE_CHARGE_LOW_SAVER "enable-charge-low-saver"
-// 待机时是否锁定屏幕
-#define POWER_SCHEMA_SCREEN_LOCKED_WHEN_SUSPEND "screen-locked-when-suspend"
-// 休眠时是否锁定屏幕
-#define POWER_SCHEMA_SCREEN_LOCKED_WHEN_HIBERNATE "screen-locked-when-hibernate"
 // 按下挂起键触发的节能行为
 #define POWER_SCHEMA_BUTTON_SUSPEND_ACTION "button-suspend-action"
 // 按下休眠键触发的节能行为
diff --git a/plugins/power/com.kylinsec.Kiran.SessionDaemon.Power.xml b/plugins/power/com.kylinsec.Kiran.SessionDaemon.Power.xml
index 29444f4..fe2f35e 100644
--- a/plugins/power/com.kylinsec.Kiran.SessionDaemon.Power.xml
+++ b/plugins/power/com.kylinsec.Kiran.SessionDaemon.Power.xml
@@ -81,20 +81,6 @@
             <description>Enable cpu save energy when the ups or battery power is low.</description>
         </method>
 
-        <method name="LockScreenWhenSuspend">
-            <arg type="b" name="enabled" direction="in">
-                <description>Whether lock screen when the computer is suspend.</description>
-            </arg>
-            <description>Lock screen when the computer is suspend.</description>
-        </method>
-
-        <method name="LockScreenWhenHibernate">
-            <arg type="b" name="enabled" direction="in">
-                <description>Whether lock screen when the computer is hibernate.</description>
-            </arg>
-            <description>Lock screen when the computer is hibernate.</description>
-        </method>
-
         <property name="OnBattery" type="b" access="read">
             <annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="true"/>
             <description>Indicates whether the system is running on battery power.</description>
@@ -120,16 +106,6 @@
             <description>Whether does the cpu save energy when the ups or battery power is low.</description>
         </property>
 
-        <property name="ScreenLockedWhenSuspend" type="b" access="read">
-            <annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="true"/>
-            <description>Whether lock screen when the computer is suspend.</description>
-        </property>
-
-        <property name="ScreenLockedWhenHibernate" type="b" access="read">
-            <annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="true"/>
-            <description>Whether lock screen when the computer is hibernate.</description>
-        </property>
-
         <signal name="IdleActionChanged">
             <arg name="device" type="i" />
             <arg name="supply" type="i" />
diff --git a/plugins/power/power-manager.cpp b/plugins/power/power-manager.cpp
index 8438b3f..9282b85 100644
--- a/plugins/power/power-manager.cpp
+++ b/plugins/power/power-manager.cpp
@@ -329,18 +329,6 @@ void PowerManager::EnableChargeLowSaver(bool enabled, MethodInvocation& invocati
     invocation.ret();
 }
 
-void PowerManager::LockScreenWhenSuspend(bool enabled, MethodInvocation& invocation)
-{
-    this->ScreenLockedWhenSuspend_set(enabled);
-    invocation.ret();
-}
-
-void PowerManager::LockScreenWhenHibernate(bool enabled, MethodInvocation& invocation)
-{
-    this->ScreenLockedWhenHibernate_set(enabled);
-    invocation.ret();
-}
-
 bool PowerManager::DisplayIdleDimmedEnabled_setHandler(bool value)
 {
     this->power_settings_->set_boolean(POWER_SCHEMA_ENABLE_DISPLAY_IDLE_DIMMED, value);
@@ -359,18 +347,6 @@ bool PowerManager::ChargeLowSaverEnabled_setHandler(bool value)
     return true;
 }
 
-bool PowerManager::ScreenLockedWhenSuspend_setHandler(bool value)
-{
-    this->power_settings_->set_boolean(POWER_SCHEMA_SCREEN_LOCKED_WHEN_SUSPEND, value);
-    return true;
-}
-
-bool PowerManager::ScreenLockedWhenHibernate_setHandler(bool value)
-{
-    this->power_settings_->set_boolean(POWER_SCHEMA_SCREEN_LOCKED_WHEN_HIBERNATE, value);
-    return true;
-}
-
 bool PowerManager::OnBattery_get()
 {
     return this->upower_client_->get_on_battery();
@@ -396,16 +372,6 @@ bool PowerManager::ChargeLowSaverEnabled_get()
     return this->power_settings_->get_boolean(POWER_SCHEMA_ENABLE_CHARGE_LOW_SAVER);
 }
 
-bool PowerManager::ScreenLockedWhenSuspend_get()
-{
-    return this->power_settings_->get_boolean(POWER_SCHEMA_SCREEN_LOCKED_WHEN_SUSPEND);
-}
-
-bool PowerManager::ScreenLockedWhenHibernate_get()
-{
-    return this->power_settings_->get_boolean(POWER_SCHEMA_SCREEN_LOCKED_WHEN_HIBERNATE);
-}
-
 void PowerManager::on_battery_changed(bool on_battery)
 {
     this->OnBattery_set(on_battery);
diff --git a/plugins/power/power-manager.h b/plugins/power/power-manager.h
index 0f799ee..674de4d 100644
--- a/plugins/power/power-manager.h
+++ b/plugins/power/power-manager.h
@@ -88,18 +88,12 @@ protected:
     virtual void EnableChargeLowDimmed(bool enabled, MethodInvocation &invocation);
     // 电量过低时是否进入节能模式
     virtual void EnableChargeLowSaver(bool enabled, MethodInvocation &invocation);
-    // 待机时是否锁定屏幕
-    virtual void LockScreenWhenSuspend(bool enabled, MethodInvocation &invocation);
-    // 休眠时是否锁定屏幕
-    virtual void LockScreenWhenHibernate(bool enabled, MethodInvocation &invocation);
 
     virtual bool OnBattery_setHandler(bool value) { return true; }
     virtual bool LidIsPresent_setHandler(bool value) { return true; }
     virtual bool DisplayIdleDimmedEnabled_setHandler(bool value);
     virtual bool ChargeLowDimmedEnabled_setHandler(bool value);
     virtual bool ChargeLowSaverEnabled_setHandler(bool value);
-    virtual bool ScreenLockedWhenSuspend_setHandler(bool value);
-    virtual bool ScreenLockedWhenHibernate_setHandler(bool value);
 
     // 系统是否在使用电池供电
     virtual bool OnBattery_get();
@@ -108,8 +102,6 @@ protected:
     virtual bool DisplayIdleDimmedEnabled_get();
     virtual bool ChargeLowDimmedEnabled_get();
     virtual bool ChargeLowSaverEnabled_get();
-    virtual bool ScreenLockedWhenSuspend_get();
-    virtual bool ScreenLockedWhenHibernate_get();
 
 private:
     void init();
diff --git a/plugins/power/save/power-save-computer.cpp b/plugins/power/save/power-save-computer.cpp
index cc62fbd..fdf7743 100644
--- a/plugins/power/save/power-save-computer.cpp
+++ b/plugins/power/save/power-save-computer.cpp
@@ -18,9 +18,7 @@ namespace Kiran
 {
 PowerSaveComputer::PowerSaveComputer()
 {
-    this->power_settings_ = Gio::Settings::create(POWER_SCHEMA_ID);
-    this->login1_ = PowerWrapperManager::get_instance()->get_default_login1();
-    this->screensaver_ = PowerWrapperManager::get_instance()->get_default_screensaver();
+    this->session_ = PowerWrapperManager::get_instance()->get_default_session();
 }
 
 void PowerSaveComputer::init()
@@ -29,51 +27,21 @@ void PowerSaveComputer::init()
 
 void PowerSaveComputer::suspend()
 {
-    uint32_t throttle = 0;
-
-    auto lockscreen = this->power_settings_->get_boolean(POWER_SCHEMA_SCREEN_LOCKED_WHEN_SUSPEND);
-    // 挂起之前锁定屏幕
-    if (lockscreen)
-    {
-        throttle = this->screensaver_->lock_and_throttle("suspend");
-    }
-
     this->save_changed_.emit(ComputerSaveState::COMPUTER_SAVE_STATE_SLEEP, PowerAction::POWER_ACTION_COMPUTER_SUSPEND);
-    this->login1_->suspend();
+    this->session_->suspend();
     this->save_changed_.emit(ComputerSaveState::COMPUTER_SAVE_STATE_RESUME, PowerAction::POWER_ACTION_COMPUTER_SUSPEND);
-
-    this->screensaver_->poke();
-    if (throttle)
-    {
-        this->screensaver_->remove_throttle(throttle);
-    }
 }
 
 void PowerSaveComputer::hibernate()
 {
-    uint32_t throttle = 0;
-
-    auto lockscreen = this->power_settings_->get_boolean(POWER_SCHEMA_SCREEN_LOCKED_WHEN_HIBERNATE);
-    // 休眠之前锁定屏幕
-    if (lockscreen)
-    {
-        throttle = this->screensaver_->lock_and_throttle("hibernate");
-    }
-
     this->save_changed_.emit(ComputerSaveState::COMPUTER_SAVE_STATE_SLEEP, PowerAction::POWER_ACTION_COMPUTER_HIBERNATE);
-    this->login1_->hibernate();
+    this->session_->hibernate();
     this->save_changed_.emit(ComputerSaveState::COMPUTER_SAVE_STATE_RESUME, PowerAction::POWER_ACTION_COMPUTER_HIBERNATE);
-
-    this->screensaver_->poke();
-    if (throttle)
-    {
-        this->screensaver_->remove_throttle(throttle);
-    }
 }
 
 void PowerSaveComputer::shutdown()
 {
-    this->login1_->shutdown();
+    this->session_->shutdown();
 }
 
 }  // namespace Kiran
diff --git a/plugins/power/save/power-save-computer.h b/plugins/power/save/power-save-computer.h
index eb8f7c3..f516cbf 100644
--- a/plugins/power/save/power-save-computer.h
+++ b/plugins/power/save/power-save-computer.h
@@ -49,9 +49,7 @@ private:
 private:
     static PowerSaveComputer* instance_;
 
-    Glib::RefPtr<Gio::Settings> power_settings_;
-    std::shared_ptr<PowerLogin1> login1_;
-    std::shared_ptr<PowerScreenSaver> screensaver_;
+    std::shared_ptr<PowerSession> session_;
 
     sigc::signal<void, ComputerSaveState, PowerAction> save_changed_;
 };
diff --git a/plugins/power/wrapper/power-session.cpp b/plugins/power/wrapper/power-session.cpp
index 1a613c1..d87e38b 100644
--- a/plugins/power/wrapper/power-session.cpp
+++ b/plugins/power/wrapper/power-session.cpp
@@ -73,6 +73,117 @@ void PowerSession::init()
     this->sm_presence_proxy_->signal_signal().connect(sigc::mem_fun(this, &PowerSession::on_sm_presence_signal));
 }
 
+bool PowerSession::can_suspend()
+{
+    try
+    {
+        auto retval = this->sm_proxy_->call_sync("CanSuspend", Glib::VariantContainerBase());
+        auto v1 = retval.get_child(0);
+        return Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(v1).get();
+    }
+    catch (const Glib::Error& e)
+    {
+        KLOG_WARNING("Failed to call CanSuspend: %s", e.what().c_str());
+    }
+    catch (const std::exception& e)
+    {
+        KLOG_WARNING("Failed to get the retval of function CanSuspend: %s", e.what());
+    }
+    return false;
+}
+
+void PowerSession::suspend()
+{
+    if (!this->can_suspend())
+    {
+        KLOG_WARNING("The session manager doesn't allow suspend.");
+        return;
+    }
+
+    try
+    {
+        this->sm_proxy_->call_sync("Suspend", Glib::VariantContainerBase());
+    }
+    catch (const Glib::Error& e)
+    {
+        KLOG_WARNING("Failed to call Suspend: %s", e.what().c_str());
+    }
+}
+
+bool PowerSession::can_hibernate()
+{
+    try
+    {
+        auto retval = this->sm_proxy_->call_sync("CanHibernate", Glib::VariantContainerBase());
+        auto v1 = retval.get_child(0);
+        return Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(v1).get();
+    }
+    catch (const Glib::Error& e)
+    {
+        KLOG_WARNING("Failed to call CanHibernate: %s", e.what().c_str());
+    }
+    catch (const std::exception& e)
+    {
+        KLOG_WARNING("Failed to get the retval of function CanHibernate: %s", e.what());
+    }
+    return false;
+}
+
+void PowerSession::hibernate()
+{
+    if (!this->can_hibernate())
+    {
+        KLOG_WARNING("The session manager doesn't allow hibernate.");
+        return;
+    }
+
+    try
+    {
+        this->sm_proxy_->call_sync("Hibernate", Glib::VariantContainerBase());
+    }
+    catch (const Glib::Error& e)
+    {
+        KLOG_WARNING("Failed to call Hibernate: %s", e.what().c_str());
+    }
+}
+
+bool PowerSession::can_shutdown()
+{
+    try
+    {
+        auto retval = this->sm_proxy_->call_sync("CanShutdown", Glib::VariantContainerBase());
+        auto v1 = retval.get_child(0);
+        return Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(v1).get();
+    }
+    catch (const Glib::Error& e)
+    {
+        KLOG_WARNING("Failed to call CanShutdown: %s", e.what().c_str());
+    }
+    catch (const std::exception& e)
+    {
+        KLOG_WARNING("Failed to get the retval of function CanShutdown: %s", e.what());
+    }
+    return false;
+}
+
+void PowerSession::shutdown()
+{
+    if (!this->can_shutdown())
+    {
+        KLOG_WARNING("The session manager doesn't allow shutdown.");
+        return;
+    }
+
+    try
+    {
+        this->sm_proxy_->call_sync("Shutdown", Glib::VariantContainerBase());
+    }
+    catch (const Glib::Error& e)
+    {
+        KLOG_WARNING("Failed to call shutdown: %s", e.what().c_str());
+    }
+}
+
 uint32_t PowerSession::get_status()
 {
     KLOG_PROFILE("");
diff --git a/plugins/power/wrapper/power-session.h b/plugins/power/wrapper/power-session.h
index a2e26c9..6b0e770 100644
--- a/plugins/power/wrapper/power-session.h
+++ b/plugins/power/wrapper/power-session.h
@@ -39,6 +39,16 @@ public:
     // 挂起状态是否禁用
     bool get_suspend_inhibited() { return this->is_suspend_inhibited_; };
 
+    // 挂机
+    bool can_suspend();
+    void suspend();
+    // 休眠
+    bool can_hibernate();
+    void hibernate();
+    // 关机
+    bool can_shutdown();
+    void shutdown();
+
 private:
     // 获取空闲状态
     uint32_t get_status();
-- 
2.36.1

