]> git.sur5r.net Git - minitube/blob - lib/idle/src/idle_linux.cpp
New upstream version 3.1
[minitube] / lib / idle / src / idle_linux.cpp
1 #include "idle.h"
2
3 #include <QDBusConnectionInterface>
4 #include <QDBusInterface>
5 #include <QDBusReply>
6 #include <QtCore>
7
8 namespace {
9
10 const QString fdDisplayService = "org.freedesktop.ScreenSaver";
11 const QString fdDisplayPath = "/org/freedesktop/ScreenSaver";
12 const QString fdDisplayInterface = fdDisplayService;
13
14 const QString gnomeSystemService = "org.gnome.SessionManager";
15 const QString gnomeSystemPath = "/org/gnome/SessionManager";
16 const QString gnomeSystemInterface = gnomeSystemService;
17
18 const QString inhibitMethod = "Inhibit";
19 const QString uninhibitMethod = "UnInhibit";
20
21 quint32 cookie;
22 QString errorMessage;
23
24 bool handleReply(const QDBusReply<quint32> &reply) {
25     if (reply.isValid()) {
26         cookie = reply.value();
27         errorMessage.clear();
28         return true;
29     }
30     errorMessage = reply.error().message();
31     return false;
32 }
33
34 } // namespace
35
36 bool Idle::preventDisplaySleep(const QString &reason) {
37     QDBusInterface dbus(fdDisplayService, fdDisplayPath, fdDisplayInterface);
38     QDBusReply<quint32> reply =
39             dbus.call(inhibitMethod, QCoreApplication::applicationName(), reason);
40     return handleReply(reply);
41 }
42
43 bool Idle::allowDisplaySleep() {
44     QDBusInterface dbus(fdDisplayService, fdDisplayPath, fdDisplayInterface);
45     dbus.call(uninhibitMethod, cookie);
46     return true;
47 }
48
49 QString Idle::displayErrorMessage() {
50     return errorMessage;
51 }
52
53 bool Idle::preventSystemSleep(const QString &reason) {
54     QDBusInterface dbus(gnomeSystemService, gnomeSystemPath, gnomeSystemInterface);
55     QDBusReply<quint32> reply =
56             dbus.call(inhibitMethod, QCoreApplication::applicationName(), reason);
57     return handleReply(reply);
58 }
59
60 bool Idle::allowSystemSleep() {
61     QDBusInterface dbus(gnomeSystemService, gnomeSystemPath, gnomeSystemInterface);
62     dbus.call(uninhibitMethod, cookie);
63     return true;
64 }
65
66 QString Idle::systemErrorMessage() {
67     return errorMessage;
68 }