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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/***************************************************************************
* Copyright (C) 2008 by Kevin Ottens <ervin@kde.org> *
* Copyright (C) 2008-2010 by Dario Freddi <drf@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
**************************************************************************/
#include "powerdevilfdoconnector.h"
#include "powerdevilaction.h"
#include "powerdevilactionpool.h"
#include "powerdevilcore.h"
#include "powermanagementfdoadaptor.h"
#include "powermanagementinhibitadaptor.h"
#include "actions/bundled/suspendsession.h"
#include <KConfigGroup>
namespace PowerDevil {
FdoConnector::FdoConnector(PowerDevil::Core *parent)
: QObject(parent), m_core(parent)
{
new PowerManagementFdoAdaptor(this);
new PowerManagementInhibitAdaptor(this);
QDBusConnection c = QDBusConnection::sessionBus();
c.registerService("org.freedesktop.PowerManagement");
c.registerObject("/org/freedesktop/PowerManagement", this);
c.registerService("org.freedesktop.PowerManagement.Inhibit");
c.registerObject("/org/freedesktop/PowerManagement/Inhibit", this);
connect(m_core->backend(), SIGNAL(acAdapterStateChanged(PowerDevil::BackendInterface::AcAdapterState)),
this, SLOT(onAcAdapterStateChanged(PowerDevil::BackendInterface::AcAdapterState)));
connect(PolicyAgent::instance(), SIGNAL(unavailablePoliciesChanged(PowerDevil::PolicyAgent::RequiredPolicies)),
this, SLOT(onUnavailablePoliciesChanged(PowerDevil::PolicyAgent::RequiredPolicies)));
}
bool FdoConnector::CanHibernate()
{
return m_core->backend()->supportedSuspendMethods() & PowerDevil::BackendInterface::ToDisk;
}
bool FdoConnector::CanSuspend()
{
return m_core->backend()->supportedSuspendMethods() & PowerDevil::BackendInterface::ToRam;
}
bool FdoConnector::CanHybridSuspend()
{
return m_core->backend()->supportedSuspendMethods() & PowerDevil::BackendInterface::HybridSuspend;
}
bool FdoConnector::CanSuspendThenHibernate()
{
return m_core->backend()->supportedSuspendMethods() & PowerDevil::BackendInterface::SuspendThenHibernate;
}
bool FdoConnector::GetPowerSaveStatus()
{
return m_core->backend()->acAdapterState() == PowerDevil::BackendInterface::Unplugged;
}
void FdoConnector::Suspend()
{
triggerSuspendSession(BundledActions::SuspendSession::ToRamMode);
}
void FdoConnector::Hibernate()
{
triggerSuspendSession(BundledActions::SuspendSession::ToDiskMode);
}
void FdoConnector::HybridSuspend()
{
triggerSuspendSession(BundledActions::SuspendSession::SuspendHybridMode);
}
bool FdoConnector::HasInhibit()
{
return PolicyAgent::instance()->requirePolicyCheck(PolicyAgent::InterruptSession) != PolicyAgent::None;
}
int FdoConnector::Inhibit(const QString &application, const QString &reason)
{
// Inhibit here means we cannot interrupt the session.
// If we've been called from DBus, use PolicyAgent's service watching system
if (calledFromDBus()) {
return PolicyAgent::instance()->addInhibitionWithExplicitDBusService((uint)PolicyAgent::InterruptSession,
application, reason, message().service());
} else {
return PolicyAgent::instance()->AddInhibition((uint)PolicyAgent::InterruptSession, application, reason);
}
}
void FdoConnector::UnInhibit(int cookie)
{
PolicyAgent::instance()->ReleaseInhibition(cookie);
}
void FdoConnector::ForceUnInhibitAll()
{
PolicyAgent::instance()->releaseAllInhibitions();
}
void FdoConnector::onAcAdapterStateChanged(PowerDevil::BackendInterface::AcAdapterState newstate)
{
Q_EMIT PowerSaveStatusChanged(newstate == PowerDevil::BackendInterface::Unplugged);
}
void FdoConnector::onUnavailablePoliciesChanged(PowerDevil::PolicyAgent::RequiredPolicies newpolicies)
{
Q_EMIT HasInhibitChanged(newpolicies & PowerDevil::PolicyAgent::InterruptSession);
}
void FdoConnector::triggerSuspendSession(uint action)
{
PowerDevil::Action *helperAction = ActionPool::instance()->loadAction("SuspendSession", KConfigGroup(), m_core);
if (helperAction) {
QVariantMap args;
args["Type"] = action;
args["Explicit"] = true;
helperAction->trigger(args);
}
}
}
|