]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - liblightdm-qt/power.cpp
Merge with trunk
[sojka/lightdm.git] / liblightdm-qt / power.cpp
1 /*
2  * Copyright (C) 2010-2011 David Edmundson
3  * Copyright (C) 2010-2011 Robert Ancell
4  * Author: David Edmundson <kde@davidedmundson.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by the Free
8  * Software Foundation; either version 2 or version 3 of the License.
9  * See http://www.gnu.org/copyleft/lgpl.html the full text of the license.
10  */
11
12
13 #include "QLightDM/power.h"
14
15 #include <QtCore/QVariant>
16 #include <QtDBus/QDBusInterface>
17 #include <QtDBus/QDBusReply>
18 #include <QDebug>
19
20 #include "config.h"
21
22 using namespace QLightDM;
23
24 class PowerInterface::PowerInterfacePrivate
25 {
26 public:
27     PowerInterfacePrivate();
28     QScopedPointer<QDBusInterface> powerManagementInterface;
29     QScopedPointer<QDBusInterface> login1Interface;
30 };
31
32 PowerInterface::PowerInterfacePrivate::PowerInterfacePrivate() :
33     powerManagementInterface(new QDBusInterface("org.freedesktop.UPower","/org/freedesktop/UPower", "org.freedesktop.UPower", QDBusConnection::systemBus())),
34     login1Interface(new QDBusInterface("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", QDBusConnection::systemBus()))
35 {
36 }
37
38
39 PowerInterface::PowerInterface(QObject *parent)
40     : QObject(parent),
41       d(new PowerInterfacePrivate)
42 {
43 }
44
45 PowerInterface::~PowerInterface()
46 {
47     delete d;
48 }
49
50 bool PowerInterface::canSuspend()
51 {
52     if (d->login1Interface->isValid())
53     {
54         QDBusReply<QString> reply = d->login1Interface->call("CanSuspend");
55         if (reply.isValid())
56         {
57             return reply.value() == "yes";
58         }
59     }
60
61     qWarning() << d->login1Interface->lastError();
62
63     QDBusReply<bool> reply = d->powerManagementInterface->call("SuspendAllowed");
64     if (reply.isValid()) {
65         return reply.value();
66     }
67     else {
68         return false;
69     }
70 }
71
72 bool PowerInterface::suspend()
73 {
74     QDBusReply<void> reply;
75     if (d->login1Interface->isValid())
76         reply = d->login1Interface->call("Suspend", false);
77     else
78         reply = d->powerManagementInterface->call("Suspend");
79
80     return reply.isValid ();
81 }
82
83 bool PowerInterface::canHibernate()
84 {
85     if (d->login1Interface->isValid())
86     {
87         QDBusReply<QString> reply = d->login1Interface->call("CanHibernate");
88         if (reply.isValid())
89         {
90             return reply.value() == "yes";
91         }
92     }
93
94     qWarning() << d->login1Interface->lastError();
95
96     QDBusReply<bool> reply = d->powerManagementInterface->call("HibernateAllowed");
97     if (reply.isValid()) {
98         return reply.value();
99     }
100     else {
101         return false;
102     }
103 }
104
105 bool PowerInterface::hibernate()
106 {
107     QDBusReply<void> reply;
108     if (d->login1Interface->isValid())
109         reply = d->login1Interface->call("Hibernate", false);
110     else
111         reply = d->powerManagementInterface->call("Hibernate");
112
113     return reply.isValid ();
114 }
115
116 bool PowerInterface::canShutdown()
117 {
118     QDBusReply<QString> reply1 = d->login1Interface->call("CanPowerOff");
119     if (reply1.isValid()) {
120         return reply1.value() == "yes";
121     }
122
123     return false;
124 }
125
126 bool PowerInterface::shutdown()
127 {
128     QDBusReply<void> reply;
129     reply = d->login1Interface->call("PowerOff", false);
130     return reply.isValid();
131 }
132
133 bool PowerInterface::canRestart()
134 {
135     QDBusReply<QString> reply1 = d->login1Interface->call("CanReboot");
136     if (reply1.isValid()) {
137         return reply1.value() == "yes";
138     }
139
140     return false;
141 }
142
143 bool PowerInterface::restart()
144 {
145     QDBusReply<void> reply;
146     reply = d->login1Interface->call("Reboot", false);
147     return reply.isValid();
148 }
149
150 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
151 #include "power_moc5.cpp"
152 #else
153 #include "power_moc4.cpp"
154 #endif