]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - liblightdm-qt/power.cpp
Turn power interface into a proper class.
[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 3 of the License, or (at your option) any
9  * later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the
10  * license.
11  */
12
13
14 #include "QLightDM/power.h"
15
16 #include <QtCore/QVariant>
17 #include <QtDBus/QDBusInterface>
18 #include <QtDBus/QDBusReply>
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 };
31
32 PowerInterface::PowerInterfacePrivate::PowerInterfacePrivate() :
33     powerManagementInterface(new QDBusInterface("org.freedesktop.UPower","/org/freedesktop/UPower", "org.freedesktop.UPower", QDBusConnection::systemBus())),
34     consoleKitInterface(new QDBusInterface("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.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     QDBusReply<bool> reply = d->powerManagementInterface->call("SuspendAllowed");
53     if (reply.isValid()) {
54         return reply.value();
55     }
56     else {
57         return false;
58     }
59 }
60
61 void PowerInterface::suspend()
62 {
63     d->powerManagementInterface->call("Suspend");
64 }
65
66 bool PowerInterface::canHibernate()
67 {
68     QDBusReply<bool> reply = d->powerManagementInterface->call("HibernateAllowed");
69     if (reply.isValid()) {
70         return reply.value();
71     }
72     else {
73         return false;
74     }
75 }
76
77 void PowerInterface::hibernate()
78 {
79     d->powerManagementInterface->call("Hibernate");
80 }
81
82 bool PowerInterface::canShutdown()
83 {
84     QDBusReply<bool> reply = d->consoleKitInterface->call("CanStop");
85     if (reply.isValid()) {
86         return reply.value();
87     }
88     else {
89         return false;
90     }
91 }
92
93 void PowerInterface::shutdown()
94 {
95     d->consoleKitInterface->call("Stop");
96 }
97
98 bool PowerInterface::canRestart()
99 {
100     QDBusReply<bool> reply = d->consoleKitInterface->call("CanRestart");
101     if (reply.isValid()) {
102         return reply.value();
103     }
104     else {
105         return false;
106     }
107 }
108
109 void PowerInterface::restart()
110 {
111     d->consoleKitInterface->call("Restart");
112 }
113
114 #include "power_moc.cpp"