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 QDBusReply<bool> reply = d->powerManagementInterface->call("SuspendAllowed");
55 if (reply.isValid()) {
63 bool PowerInterface::suspend()
65 QDBusReply<void> reply = d->powerManagementInterface->call("Suspend");
66 return reply.isValid ();
69 bool PowerInterface::canHibernate()
71 QDBusReply<bool> reply = d->powerManagementInterface->call("HibernateAllowed");
72 if (reply.isValid()) {
80 bool PowerInterface::hibernate()
82 QDBusReply<void> reply = d->powerManagementInterface->call("Hibernate");
83 return reply.isValid ();
86 bool PowerInterface::canShutdown()
88 if (d->login1Interface->isValid()) {
89 QDBusReply<QString> reply1 = d->login1Interface->call("CanPowerOff");
90 if (reply1.isValid()) {
91 return reply1.value() == "yes";
94 qWarning() << d->login1Interface->lastError();
96 QDBusReply<bool> reply = d->consoleKitInterface->call("CanStop");
97 if (reply.isValid()) {
104 bool PowerInterface::shutdown()
106 QDBusReply<void> reply;
107 if (d->login1Interface->isValid())
108 reply = d->login1Interface->call("PowerOff", false);
110 reply = d->consoleKitInterface->call("Stop");
111 return reply.isValid();
114 bool PowerInterface::canRestart()
116 if (d->login1Interface->isValid()) {
117 QDBusReply<QString> reply1 = d->login1Interface->call("CanReboot");
118 if (reply1.isValid()) {
119 return reply1.value() == "yes";
122 qWarning() << d->login1Interface->lastError();
124 QDBusReply<bool> reply = d->consoleKitInterface->call("CanRestart");
125 if (reply.isValid()) {
126 return reply.value();
132 bool PowerInterface::restart()
134 QDBusReply<void> reply;
135 if (d->login1Interface->isValid())
136 reply = d->login1Interface->call("Reboot", false);
138 reply = d->consoleKitInterface->call("Restart");
139 return reply.isValid();
142 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
143 #include "power_moc5.cpp"
145 #include "power_moc4.cpp"