2 * Copyright (C) 2010-2011 David Edmundson
3 * Copyright (C) 2010-2011 Robert Ancell
4 * Author: David Edmundson <kde@davidedmundson.co.uk>
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.
13 #include "QLightDM/power.h"
15 #include <QtCore/QVariant>
16 #include <QtDBus/QDBusInterface>
17 #include <QtDBus/QDBusReply>
22 using namespace QLightDM;
24 class PowerInterface::PowerInterfacePrivate
27 PowerInterfacePrivate();
28 QScopedPointer<QDBusInterface> powerManagementInterface;
29 QScopedPointer<QDBusInterface> consoleKitInterface;
30 QScopedPointer<QDBusInterface> login1Interface;
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()))
41 PowerInterface::PowerInterface(QObject *parent)
43 d(new PowerInterfacePrivate)
47 PowerInterface::~PowerInterface()
52 bool PowerInterface::canSuspend()
54 if (d->login1Interface->isValid())
56 QDBusReply<QString> reply = d->login1Interface->call("CanSuspend");
59 return reply.value() == "yes";
63 qWarning() << d->login1Interface->lastError();
65 QDBusReply<bool> reply = d->powerManagementInterface->call("SuspendAllowed");
66 if (reply.isValid()) {
74 bool PowerInterface::suspend()
76 QDBusReply<void> reply;
77 if (d->login1Interface->isValid())
78 reply = d->login1Interface->call("Suspend", false);
80 reply = d->powerManagementInterface->call("Suspend");
82 return reply.isValid ();
85 bool PowerInterface::canHibernate()
87 if (d->login1Interface->isValid())
89 QDBusReply<QString> reply = d->login1Interface->call("CanHibernate");
92 return reply.value() == "yes";
96 qWarning() << d->login1Interface->lastError();
98 QDBusReply<bool> reply = d->powerManagementInterface->call("HibernateAllowed");
99 if (reply.isValid()) {
100 return reply.value();
107 bool PowerInterface::hibernate()
109 QDBusReply<void> reply;
110 if (d->login1Interface->isValid())
111 reply = d->login1Interface->call("Hibernate", false);
113 reply = d->powerManagementInterface->call("Hibernate");
115 return reply.isValid ();
118 bool PowerInterface::canShutdown()
120 if (d->login1Interface->isValid()) {
121 QDBusReply<QString> reply1 = d->login1Interface->call("CanPowerOff");
122 if (reply1.isValid()) {
123 return reply1.value() == "yes";
126 qWarning() << d->login1Interface->lastError();
128 QDBusReply<bool> reply = d->consoleKitInterface->call("CanStop");
129 if (reply.isValid()) {
130 return reply.value();
136 bool PowerInterface::shutdown()
138 QDBusReply<void> reply;
139 if (d->login1Interface->isValid())
140 reply = d->login1Interface->call("PowerOff", false);
142 reply = d->consoleKitInterface->call("Stop");
143 return reply.isValid();
146 bool PowerInterface::canRestart()
148 if (d->login1Interface->isValid()) {
149 QDBusReply<QString> reply1 = d->login1Interface->call("CanReboot");
150 if (reply1.isValid()) {
151 return reply1.value() == "yes";
154 qWarning() << d->login1Interface->lastError();
156 QDBusReply<bool> reply = d->consoleKitInterface->call("CanRestart");
157 if (reply.isValid()) {
158 return reply.value();
164 bool PowerInterface::restart()
166 QDBusReply<void> reply;
167 if (d->login1Interface->isValid())
168 reply = d->login1Interface->call("Reboot", false);
170 reply = d->consoleKitInterface->call("Restart");
171 return reply.isValid();
174 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
175 #include "power_moc5.cpp"
177 #include "power_moc4.cpp"