]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - liblightdm-qt/power.cpp
Load all users only when really needed
[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> consoleKitInterface;
30     QScopedPointer<QDBusInterface> login1Interface;
31 };
32
33 PowerInterface::PowerInterfacePrivate::PowerInterfacePrivate() :
34     powerManagementInterface(new QDBusInterface("org.freedesktop.UPower","/org/freedesktop/UPower", "org.freedesktop.UPower", QDBusConnection::systemBus())),
35     consoleKitInterface(new QDBusInterface("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", QDBusConnection::systemBus())),
36     login1Interface(new QDBusInterface("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", QDBusConnection::systemBus()))
37 {
38 }
39
40
41 PowerInterface::PowerInterface(QObject *parent)
42     : QObject(parent),
43       d(new PowerInterfacePrivate)
44 {
45 }
46
47 PowerInterface::~PowerInterface()
48 {
49     delete d;
50 }
51
52 bool PowerInterface::canSuspend()
53 {
54     if (d->login1Interface->isValid())
55     {
56         QDBusReply<QString> reply = d->login1Interface->call("CanSuspend");
57         if (reply.isValid())
58         {
59             return reply.value() == "yes";
60         }
61     }
62
63     qWarning() << d->login1Interface->lastError();
64
65     QDBusReply<bool> reply = d->powerManagementInterface->call("SuspendAllowed");
66     if (reply.isValid()) {
67         return reply.value();
68     }
69     else {
70         return false;
71     }
72 }
73
74 bool PowerInterface::suspend()
75 {
76     QDBusReply<void> reply;
77     if (d->login1Interface->isValid())
78         reply = d->login1Interface->call("Suspend", false);
79     else
80         reply = d->powerManagementInterface->call("Suspend");
81
82     return reply.isValid ();
83 }
84
85 bool PowerInterface::canHibernate()
86 {
87     if (d->login1Interface->isValid())
88     {
89         QDBusReply<QString> reply = d->login1Interface->call("CanHibernate");
90         if (reply.isValid())
91         {
92             return reply.value() == "yes";
93         }
94     }
95
96     qWarning() << d->login1Interface->lastError();
97
98     QDBusReply<bool> reply = d->powerManagementInterface->call("HibernateAllowed");
99     if (reply.isValid()) {
100         return reply.value();
101     }
102     else {
103         return false;
104     }
105 }
106
107 bool PowerInterface::hibernate()
108 {
109     QDBusReply<void> reply;
110     if (d->login1Interface->isValid())
111         reply = d->login1Interface->call("Hibernate", false);
112     else
113         reply = d->powerManagementInterface->call("Hibernate");
114
115     return reply.isValid ();
116 }
117
118 bool PowerInterface::canShutdown()
119 {
120     if (d->login1Interface->isValid()) {
121         QDBusReply<QString> reply1 = d->login1Interface->call("CanPowerOff");
122         if (reply1.isValid()) {
123             return reply1.value() == "yes";
124         }
125     }
126     qWarning() << d->login1Interface->lastError();
127
128     QDBusReply<bool> reply = d->consoleKitInterface->call("CanStop");
129     if (reply.isValid()) {
130         return reply.value();
131     }
132
133     return false;
134 }
135
136 bool PowerInterface::shutdown()
137 {
138     QDBusReply<void> reply;
139     if (d->login1Interface->isValid())
140         reply = d->login1Interface->call("PowerOff", false);
141     else
142         reply = d->consoleKitInterface->call("Stop");
143     return reply.isValid();
144 }
145
146 bool PowerInterface::canRestart()
147 {
148     if (d->login1Interface->isValid()) {
149         QDBusReply<QString> reply1 = d->login1Interface->call("CanReboot");
150         if (reply1.isValid()) {
151             return reply1.value() == "yes";
152         }
153     }
154     qWarning() << d->login1Interface->lastError();
155
156     QDBusReply<bool> reply = d->consoleKitInterface->call("CanRestart");
157     if (reply.isValid()) {
158         return reply.value();
159     }
160
161     return false;
162 }
163
164 bool PowerInterface::restart()
165 {
166     QDBusReply<void> reply;
167     if (d->login1Interface->isValid())
168         reply = d->login1Interface->call("Reboot", false);
169     else
170         reply = d->consoleKitInterface->call("Restart");
171     return reply.isValid();
172 }
173
174 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
175 #include "power_moc5.cpp"
176 #else
177 #include "power_moc4.cpp"
178 #endif