File indexing completed on 2024-04-28 16:55:14

0001 /***************************************************************************
0002  *   Copyright (C) 2008 by Kevin Ottens <ervin@kde.org>                    *
0003  *   Copyright (C) 2008-2010 by Dario Freddi <drf@kde.org>                 *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0019  **************************************************************************/
0020 
0021 #include "powerdevilfdoconnector.h"
0022 
0023 #include "powerdevilaction.h"
0024 #include "powerdevilactionpool.h"
0025 #include "powerdevilcore.h"
0026 
0027 #include "powermanagementfdoadaptor.h"
0028 #include "powermanagementinhibitadaptor.h"
0029 
0030 #include "actions/bundled/suspendsession.h"
0031 
0032 #include <KConfigGroup>
0033 
0034 namespace PowerDevil {
0035 
0036 FdoConnector::FdoConnector(PowerDevil::Core *parent)
0037         : QObject(parent), m_core(parent)
0038 {
0039     new PowerManagementFdoAdaptor(this);
0040     new PowerManagementInhibitAdaptor(this);
0041 
0042     QDBusConnection c = QDBusConnection::sessionBus();
0043 
0044     c.registerService("org.freedesktop.PowerManagement");
0045     c.registerObject("/org/freedesktop/PowerManagement", this);
0046 
0047     c.registerService("org.freedesktop.PowerManagement.Inhibit");
0048     c.registerObject("/org/freedesktop/PowerManagement/Inhibit", this);
0049 
0050     connect(m_core->backend(), &BackendInterface::acAdapterStateChanged,
0051             this, &FdoConnector::onAcAdapterStateChanged);
0052     connect(PolicyAgent::instance(), SIGNAL(unavailablePoliciesChanged(PowerDevil::PolicyAgent::RequiredPolicies)),
0053             this, SLOT(onUnavailablePoliciesChanged(PowerDevil::PolicyAgent::RequiredPolicies)));
0054 }
0055 
0056 bool FdoConnector::CanHibernate()
0057 {
0058     return m_core->backend()->supportedSuspendMethods() & PowerDevil::BackendInterface::ToDisk;
0059 }
0060 
0061 bool FdoConnector::CanSuspend()
0062 {
0063     return m_core->backend()->supportedSuspendMethods() & PowerDevil::BackendInterface::ToRam;
0064 }
0065 
0066 bool FdoConnector::CanHybridSuspend()
0067 {
0068     return m_core->backend()->supportedSuspendMethods() & PowerDevil::BackendInterface::HybridSuspend;
0069 }
0070 
0071 bool FdoConnector::CanSuspendThenHibernate()
0072 {
0073     return m_core->backend()->supportedSuspendMethods() & PowerDevil::BackendInterface::SuspendThenHibernate;
0074 }
0075 
0076 bool FdoConnector::GetPowerSaveStatus()
0077 {
0078     return m_core->backend()->acAdapterState() == PowerDevil::BackendInterface::Unplugged;
0079 }
0080 
0081 void FdoConnector::Suspend()
0082 {
0083     triggerSuspendSession(BundledActions::SuspendSession::ToRamMode);
0084 }
0085 
0086 void FdoConnector::Hibernate()
0087 {
0088     triggerSuspendSession(BundledActions::SuspendSession::ToDiskMode);
0089 }
0090 
0091 void FdoConnector::HybridSuspend()
0092 {
0093     triggerSuspendSession(BundledActions::SuspendSession::SuspendHybridMode);
0094 }
0095 
0096 bool FdoConnector::HasInhibit()
0097 {
0098     return PolicyAgent::instance()->requirePolicyCheck(PolicyAgent::InterruptSession) != PolicyAgent::None;
0099 }
0100 
0101 int FdoConnector::Inhibit(const QString &application, const QString &reason)
0102 {
0103     // Inhibit here means we cannot interrupt the session.
0104     // If we've been called from DBus, use PolicyAgent's service watching system
0105     if (calledFromDBus()) {
0106         return PolicyAgent::instance()->addInhibitionWithExplicitDBusService((uint)PolicyAgent::InterruptSession,
0107                                                                              application, reason, message().service());
0108     } else {
0109         return PolicyAgent::instance()->AddInhibition((uint)PolicyAgent::InterruptSession, application, reason);
0110     }
0111 }
0112 
0113 void FdoConnector::UnInhibit(int cookie)
0114 {
0115     PolicyAgent::instance()->ReleaseInhibition(cookie);
0116 }
0117 
0118 void FdoConnector::ForceUnInhibitAll()
0119 {
0120     PolicyAgent::instance()->releaseAllInhibitions();
0121 }
0122 
0123 void FdoConnector::onAcAdapterStateChanged(PowerDevil::BackendInterface::AcAdapterState newstate)
0124 {
0125     Q_EMIT PowerSaveStatusChanged(newstate == PowerDevil::BackendInterface::Unplugged);
0126 }
0127 
0128 void FdoConnector::onUnavailablePoliciesChanged(PowerDevil::PolicyAgent::RequiredPolicies newpolicies)
0129 {
0130     Q_EMIT HasInhibitChanged(newpolicies & PowerDevil::PolicyAgent::InterruptSession);
0131 }
0132 
0133 void FdoConnector::triggerSuspendSession(uint action)
0134 {
0135     PowerDevil::Action *helperAction = ActionPool::instance()->loadAction("SuspendSession", KConfigGroup(), m_core);
0136     if (helperAction) {
0137         QVariantMap args;
0138         args["Type"] = action;
0139         args["Explicit"] = true;
0140         helperAction->trigger(args);
0141     }
0142 }
0143 
0144 }