File indexing completed on 2024-04-28 05:36:16
0001 /* 0002 * SPDX-FileCopyrightText: 2008 Kevin Ottens <ervin@kde.org> 0003 * SPDX-FileCopyrightText: 2008-2010 Dario Freddi <drf@kde.org> 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "powerdevilfdoconnector.h" 0009 0010 #include "powerdevilaction.h" 0011 #include "powerdevilcore.h" 0012 0013 #include "powermanagementfdoadaptor.h" 0014 #include "powermanagementinhibitadaptor.h" 0015 0016 #include <KConfigGroup> 0017 0018 namespace PowerDevil 0019 { 0020 FdoConnector::FdoConnector(PowerDevil::Core *parent) 0021 : QObject(parent) 0022 , m_core(parent) 0023 { 0024 new PowerManagementFdoAdaptor(this); 0025 new PowerManagementInhibitAdaptor(this); 0026 0027 QDBusConnection c = QDBusConnection::sessionBus(); 0028 0029 c.registerService("org.freedesktop.PowerManagement"); 0030 c.registerObject("/org/freedesktop/PowerManagement", this); 0031 0032 c.registerService("org.freedesktop.PowerManagement.Inhibit"); 0033 c.registerObject("/org/freedesktop/PowerManagement/Inhibit", this); 0034 0035 connect(m_core->batteryController(), &BatteryController::acAdapterStateChanged, this, &FdoConnector::onAcAdapterStateChanged); 0036 connect(PolicyAgent::instance(), 0037 SIGNAL(unavailablePoliciesChanged(PowerDevil::PolicyAgent::RequiredPolicies)), 0038 this, 0039 SLOT(onUnavailablePoliciesChanged(PowerDevil::PolicyAgent::RequiredPolicies))); 0040 } 0041 0042 bool FdoConnector::CanHibernate() 0043 { 0044 return m_core->suspendController()->canHibernate(); 0045 } 0046 0047 bool FdoConnector::CanSuspend() 0048 { 0049 return m_core->suspendController()->canSuspend(); 0050 } 0051 0052 bool FdoConnector::CanHybridSuspend() 0053 { 0054 return m_core->suspendController()->canHybridSuspend(); 0055 } 0056 0057 bool FdoConnector::CanSuspendThenHibernate() 0058 { 0059 return m_core->suspendController()->canSuspendThenHibernate(); 0060 } 0061 0062 bool FdoConnector::GetPowerSaveStatus() 0063 { 0064 return m_core->batteryController()->acAdapterState() == BatteryController::Unplugged; 0065 } 0066 0067 void FdoConnector::Suspend() 0068 { 0069 triggerSuspendSession(PowerButtonAction::Sleep); 0070 } 0071 0072 void FdoConnector::Hibernate() 0073 { 0074 triggerSuspendSession(PowerButtonAction::Hibernate); 0075 } 0076 0077 bool FdoConnector::HasInhibit() 0078 { 0079 return PolicyAgent::instance()->requirePolicyCheck(PolicyAgent::InterruptSession) != PolicyAgent::None; 0080 } 0081 0082 int FdoConnector::Inhibit(const QString &application, const QString &reason) 0083 { 0084 // Inhibit here means we cannot interrupt the session. 0085 // If we've been called from DBus, use PolicyAgent's service watching system 0086 if (calledFromDBus()) { 0087 return PolicyAgent::instance()->addInhibitionWithExplicitDBusService((uint)PolicyAgent::InterruptSession, application, reason, message().service()); 0088 } else { 0089 return PolicyAgent::instance()->AddInhibition((uint)PolicyAgent::InterruptSession, application, reason); 0090 } 0091 } 0092 0093 void FdoConnector::UnInhibit(int cookie) 0094 { 0095 PolicyAgent::instance()->ReleaseInhibition(cookie); 0096 } 0097 0098 void FdoConnector::ForceUnInhibitAll() 0099 { 0100 PolicyAgent::instance()->releaseAllInhibitions(); 0101 } 0102 0103 void FdoConnector::onAcAdapterStateChanged(BatteryController::AcAdapterState newstate) 0104 { 0105 Q_EMIT PowerSaveStatusChanged(newstate == BatteryController::Unplugged); 0106 } 0107 0108 void FdoConnector::onUnavailablePoliciesChanged(PowerDevil::PolicyAgent::RequiredPolicies newpolicies) 0109 { 0110 Q_EMIT HasInhibitChanged(newpolicies & PowerDevil::PolicyAgent::InterruptSession); 0111 } 0112 0113 void FdoConnector::triggerSuspendSession(PowerButtonAction action) 0114 { 0115 PowerDevil::Action *helperAction = m_core->action("SuspendSession"); 0116 if (helperAction) { 0117 QVariantMap args; 0118 args["Type"] = qToUnderlying(action); 0119 args["Explicit"] = true; 0120 helperAction->trigger(args); 0121 } 0122 } 0123 0124 } 0125 0126 #include "moc_powerdevilfdoconnector.cpp"