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> login1Interface;
32 PowerInterface::PowerInterfacePrivate::PowerInterfacePrivate() :
33 powerManagementInterface(new QDBusInterface("org.freedesktop.UPower","/org/freedesktop/UPower", "org.freedesktop.UPower", QDBusConnection::systemBus())),
34 login1Interface(new QDBusInterface("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", QDBusConnection::systemBus()))
39 PowerInterface::PowerInterface(QObject *parent)
41 d(new PowerInterfacePrivate)
45 PowerInterface::~PowerInterface()
50 bool PowerInterface::canSuspend()
52 if (d->login1Interface->isValid())
54 QDBusReply<QString> reply = d->login1Interface->call("CanSuspend");
57 return reply.value() == "yes";
61 qWarning() << d->login1Interface->lastError();
63 QDBusReply<bool> reply = d->powerManagementInterface->call("SuspendAllowed");
64 if (reply.isValid()) {
72 bool PowerInterface::suspend()
74 QDBusReply<void> reply;
75 if (d->login1Interface->isValid())
76 reply = d->login1Interface->call("Suspend", false);
78 reply = d->powerManagementInterface->call("Suspend");
80 return reply.isValid ();
83 bool PowerInterface::canHibernate()
85 if (d->login1Interface->isValid())
87 QDBusReply<QString> reply = d->login1Interface->call("CanHibernate");
90 return reply.value() == "yes";
94 qWarning() << d->login1Interface->lastError();
96 QDBusReply<bool> reply = d->powerManagementInterface->call("HibernateAllowed");
97 if (reply.isValid()) {
105 bool PowerInterface::hibernate()
107 QDBusReply<void> reply;
108 if (d->login1Interface->isValid())
109 reply = d->login1Interface->call("Hibernate", false);
111 reply = d->powerManagementInterface->call("Hibernate");
113 return reply.isValid ();
116 bool PowerInterface::canShutdown()
118 QDBusReply<QString> reply1 = d->login1Interface->call("CanPowerOff");
119 if (reply1.isValid()) {
120 return reply1.value() == "yes";
126 bool PowerInterface::shutdown()
128 QDBusReply<void> reply;
129 reply = d->login1Interface->call("PowerOff", false);
130 return reply.isValid();
133 bool PowerInterface::canRestart()
135 QDBusReply<QString> reply1 = d->login1Interface->call("CanReboot");
136 if (reply1.isValid()) {
137 return reply1.value() == "yes";
143 bool PowerInterface::restart()
145 QDBusReply<void> reply;
146 reply = d->login1Interface->call("Reboot", false);
147 return reply.isValid();
150 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
151 #include "power_moc5.cpp"
153 #include "power_moc4.cpp"