]> 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> 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     QDBusReply<bool> reply = d->powerManagementInterface->call("SuspendAllowed");
55     if (reply.isValid()) {
56         return reply.value();
57     }
58     else {
59         return false;
60     }
61 }
62
63 bool PowerInterface::suspend()
64 {
65     QDBusReply<void> reply = d->powerManagementInterface->call("Suspend");
66     return reply.isValid ();
67 }
68
69 bool PowerInterface::canHibernate()
70 {
71     QDBusReply<bool> reply = d->powerManagementInterface->call("HibernateAllowed");
72     if (reply.isValid()) {
73         return reply.value();
74     }
75     else {
76         return false;
77     }
78 }
79
80 bool PowerInterface::hibernate()
81 {
82     QDBusReply<void> reply = d->powerManagementInterface->call("Hibernate");
83     return reply.isValid ();
84 }
85
86 bool PowerInterface::canShutdown()
87 {
88     if (d->login1Interface->isValid()) {
89         QDBusReply<QString> reply1 = d->login1Interface->call("CanPowerOff");
90         if (reply1.isValid()) {
91             return reply1.value() == "yes";
92         }
93     }
94     qWarning() << d->login1Interface->lastError();
95
96     QDBusReply<bool> reply = d->consoleKitInterface->call("CanStop");
97     if (reply.isValid()) {
98         return reply.value();
99     }
100
101     return false;
102 }
103
104 bool PowerInterface::shutdown()
105 {
106     QDBusReply<void> reply;
107     if (d->login1Interface->isValid())
108         reply = d->login1Interface->call("PowerOff", false);
109     else
110         reply = d->consoleKitInterface->call("Stop");
111     return reply.isValid();
112 }
113
114 bool PowerInterface::canRestart()
115 {
116     if (d->login1Interface->isValid()) {
117         QDBusReply<QString> reply1 = d->login1Interface->call("CanReboot");
118         if (reply1.isValid()) {
119             return reply1.value() == "yes";
120         }
121     }
122     qWarning() << d->login1Interface->lastError();
123   
124     QDBusReply<bool> reply = d->consoleKitInterface->call("CanRestart");
125     if (reply.isValid()) {
126         return reply.value();
127     }
128
129     return false;
130 }
131
132 bool PowerInterface::restart()
133 {
134     QDBusReply<void> reply;
135     if (d->login1Interface->isValid())
136         reply = d->login1Interface->call("Reboot", false);
137     else
138         reply = d->consoleKitInterface->call("Restart");
139     return reply.isValid();
140 }
141
142 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
143 #include "power_moc5.cpp"
144 #else
145 #include "power_moc4.cpp"
146 #endif