~lightdm-team/lightdm/1.4

« back to all changes in this revision

Viewing changes to liblightdm-qt/power.cpp

  • Committer: David Edmundson
  • Date: 2011-11-15 19:51:42 UTC
  • mto: (1297.1.18 qt-fixes)
  • mto: This revision was merged to the branch mainline in revision 1322.
  • Revision ID: david@davidedmundson.co.uk-20111115195142-ktno6ezyt17slvij
Turn power interface into a proper class.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
using namespace QLightDM;
23
23
 
24
 
static QDBusInterface* powerManagementInterface = NULL;
25
 
static QDBusInterface* consoleKitInterface = NULL;
26
 
 
27
 
static bool setupPowerManagementInterface ()
28
 
{
29
 
    if (!powerManagementInterface)
30
 
        powerManagementInterface = new QDBusInterface("org.freedesktop.UPower","/org/freedesktop/UPower", "org.freedesktop.UPower", QDBusConnection::systemBus());
31
 
    return powerManagementInterface != NULL;
32
 
}
33
 
 
34
 
static bool setupConsoleKitInterface ()
35
 
{
36
 
    if (!consoleKitInterface)
37
 
        consoleKitInterface = new QDBusInterface("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", QDBusConnection::systemBus());
38
 
    return consoleKitInterface != NULL;
39
 
}
40
 
 
41
 
bool QLightDM::canSuspend()
42
 
{
43
 
    if (!setupPowerManagementInterface())
44
 
        return false;
45
 
 
46
 
    QDBusReply<bool> reply = powerManagementInterface->call("SuspendAllowed");
47
 
    if (reply.isValid())
48
 
        return reply.value();
49
 
    else
50
 
        return false;
51
 
}
52
 
 
53
 
void QLightDM::suspend()
54
 
{
55
 
    if (setupPowerManagementInterface())
56
 
        powerManagementInterface->call("Suspend");
57
 
}
58
 
 
59
 
bool QLightDM::canHibernate()
60
 
{
61
 
    if (!setupPowerManagementInterface())
62
 
        return false;
63
 
 
64
 
    QDBusReply<bool> reply = powerManagementInterface->call("HibernateAllowed");
65
 
    if (reply.isValid())
66
 
        return reply.value();
67
 
    else
68
 
        return false;
69
 
}
70
 
 
71
 
void QLightDM::hibernate()
72
 
{
73
 
    if (setupConsoleKitInterface())
74
 
        powerManagementInterface->call("Hibernate");
75
 
}
76
 
 
77
 
bool QLightDM::canShutdown()
78
 
{
79
 
    if (!setupConsoleKitInterface())
80
 
        return false;
81
 
 
82
 
    QDBusReply<bool> reply = consoleKitInterface->call("CanStop");
83
 
    if (reply.isValid())
84
 
        return reply.value();
85
 
    else
86
 
        return false;
87
 
}
88
 
 
89
 
void QLightDM::shutdown()
90
 
{
91
 
    if (setupConsoleKitInterface())
92
 
        consoleKitInterface->call("Stop");
93
 
}
94
 
 
95
 
bool QLightDM::canRestart()
96
 
{
97
 
    if (!setupConsoleKitInterface())
98
 
        return false;
99
 
 
100
 
    QDBusReply<bool> reply = consoleKitInterface->call("CanRestart");
101
 
    if (reply.isValid())
102
 
        return reply.value();
103
 
    else
104
 
        return false;
105
 
}
106
 
 
107
 
void QLightDM::restart()
108
 
{
109
 
    if (setupConsoleKitInterface())
110
 
        consoleKitInterface->call("Restart");
111
 
}
 
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"