File: powerdevilfdoconnector.cpp

package info (click to toggle)
powerdevil 4%3A6.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,680 kB
  • sloc: cpp: 13,284; xml: 1,911; python: 1,204; sh: 19; makefile: 10
file content (128 lines) | stat: -rw-r--r-- 3,678 bytes parent folder | download
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
/*
 *   SPDX-FileCopyrightText: 2008 Kevin Ottens <ervin@kde.org>
 *   SPDX-FileCopyrightText: 2008-2010 Dario Freddi <drf@kde.org>
 *
 *   SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "powerdevilfdoconnector.h"

#include "powerdevilaction.h"
#include "powerdevilcore.h"

#include "powermanagementfdoadaptor.h"
#include "powermanagementinhibitadaptor.h"

#include <KConfigGroup>

using namespace Qt::StringLiterals;

namespace PowerDevil
{
FdoConnector::FdoConnector(PowerDevil::Core *parent)
    : QObject(parent)
    , m_core(parent)
{
    new PowerManagementFdoAdaptor(this);
    new PowerManagementInhibitAdaptor(this);

    QDBusConnection c = QDBusConnection::sessionBus();

    c.registerObject(u"/org/freedesktop/PowerManagement"_s, this);
    c.registerService(u"org.freedesktop.PowerManagement"_s);

    c.registerObject(u"/org/freedesktop/PowerManagement/Inhibit"_s, this);
    c.registerService(u"org.freedesktop.PowerManagement.Inhibit"_s);

    connect(m_core->batteryController(), &BatteryController::acAdapterStateChanged, this, &FdoConnector::onAcAdapterStateChanged);
    connect(PolicyAgent::instance(),
            SIGNAL(unavailablePoliciesChanged(PowerDevil::PolicyAgent::RequiredPolicies)),
            this,
            SLOT(onUnavailablePoliciesChanged(PowerDevil::PolicyAgent::RequiredPolicies)));
}

bool FdoConnector::CanHibernate()
{
    return m_core->suspendController()->canHibernate();
}

bool FdoConnector::CanSuspend()
{
    return m_core->suspendController()->canSuspend();
}

bool FdoConnector::CanHybridSuspend()
{
    return m_core->suspendController()->canHybridSuspend();
}

bool FdoConnector::CanSuspendThenHibernate()
{
    return m_core->suspendController()->canSuspendThenHibernate();
}

bool FdoConnector::GetPowerSaveStatus()
{
    return m_core->batteryController()->acAdapterState() == BatteryController::Unplugged;
}

void FdoConnector::Suspend()
{
    triggerSuspendSession(PowerButtonAction::Sleep);
}

void FdoConnector::Hibernate()
{
    triggerSuspendSession(PowerButtonAction::Hibernate);
}

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(BatteryController::AcAdapterState newstate)
{
    Q_EMIT PowerSaveStatusChanged(newstate == BatteryController::Unplugged);
}

void FdoConnector::onUnavailablePoliciesChanged(PowerDevil::PolicyAgent::RequiredPolicies newpolicies)
{
    Q_EMIT HasInhibitChanged(newpolicies & PowerDevil::PolicyAgent::InterruptSession);
}

void FdoConnector::triggerSuspendSession(PowerButtonAction action)
{
    PowerDevil::Action *helperAction = m_core->action(u"SuspendSession"_s);
    if (helperAction) {
        QVariantMap args;
        args[u"Type"_s] = qToUnderlying(action);
        args[u"Explicit"_s] = true;
        helperAction->trigger(args);
    }
}

}

#include "moc_powerdevilfdoconnector.cpp"