-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.cpp
More file actions
169 lines (150 loc) · 4.5 KB
/
Copy pathmanager.cpp
File metadata and controls
169 lines (150 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "manager.h"
#include <QDebug>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QCoreApplication>
#include <QTimer>
Manager::Manager(QObject *parent) : QObject(parent)
, ckit(0)
, upower(0)
, shutdownStatus(false)
, sleepStatus(false)
{
QDBusConnection system = QDBusConnection::systemBus();
if (!system.isConnected()) {
qWarning() << "D-Bus not available!!!";
QTimer::singleShot(0, qApp, SLOT(quit()));
}
ckit = new QDBusInterface(CONSOLEKIT_SERVICE,
CONSOLEKIT_MANAGER_PATH,
CONSOLEKIT_MANAGER,
system,
this);
upower = new QDBusInterface(UPOWER_SERVICE,
UPOWER_PATH,
UPOWER_MANAGER,
system,
this);
if (!ckit->isValid()) {
qWarning() << "ConsoleKit is not available!!!";
QTimer::singleShot(100, qApp, SLOT(quit()));
}
system.connect(CONSOLEKIT_SERVICE,
CONSOLEKIT_MANAGER_PATH,
CONSOLEKIT_MANAGER,
"PrepareForSleep",
this,
SLOT(handlePrepareForSuspend(bool)));
system.connect(CONSOLEKIT_SERVICE,
CONSOLEKIT_MANAGER_PATH,
CONSOLEKIT_MANAGER,
"PrepareForShutdown",
this,
SLOT(handlePrepareForShutdown(bool)));
}
bool Manager::Docked()
{
if (upower->isValid()) { return upower->property("IsDocked").toBool(); }
return false;
}
bool Manager::PreparingForShutdown()
{
return shutdownStatus;
}
bool Manager::PreparingForSleep()
{
return sleepStatus;
}
bool Manager::canAction(const QString &action)
{
if (!ckit->isValid() || action.isEmpty()) { return false; }
QDBusMessage reply = ckit->call(action);
if (reply.arguments().first().toString() == "yes") { return true; }
bool result = reply.arguments().first().toBool();
if (!reply.errorMessage().isEmpty()) { result = false; }
return result;
}
const QString Manager::doAction(const QString &action)
{
if (!ckit->isValid() || action.isEmpty()) {
return "ConsoleKit is not running or action is empty";
}
QDBusMessage reply = ckit->call(action, true);
return reply.errorMessage();
}
void Manager::handlePrepareForSuspend(bool suspend)
{
sleepStatus = suspend;
emit PrepareForSleep(suspend);
}
void Manager::handlePrepareForShutdown(bool shutdown)
{
shutdownStatus = shutdown;
emit PrepareForShutdown(shutdown);
}
void Manager::PowerOff(bool trigger)
{
if (!trigger || !ckit->isValid()) { return; }
QString result = doAction("PowerOff");
if (!result.isEmpty()) { qWarning() << result; }
}
void Manager::Reboot(bool trigger)
{
if (!trigger || !ckit->isValid()) { return; }
QString result = doAction("Reboot");
if (!result.isEmpty()) { qWarning() << result; }
}
void Manager::Suspend(bool trigger)
{
if (!trigger || !ckit->isValid()) { return; }
QString result = doAction("Suspend");
if (!result.isEmpty()) { qWarning() << result; }
}
void Manager::Hibernate(bool trigger)
{
if (!trigger || !ckit->isValid()) { return; }
QString result = doAction("Hibernate");
if (!result.isEmpty()) { qWarning() << result; }
}
void Manager::HybridSleep(bool trigger)
{
if (!trigger || !ckit->isValid()) { return; }
QString result = doAction("HybridSleep");
if (!result.isEmpty()) { qWarning() << result; }
}
const QString Manager::CanPowerOff()
{
if (canAction("CanPowerOff")) { return "yes"; }
return "no";
}
const QString Manager::CanReboot()
{
if (canAction("CanReboot")) { return "yes"; }
return "no";
}
const QString Manager::CanSuspend()
{
if (canAction("CanSuspend")) { return "yes"; }
return "no";
}
const QString Manager::CanHibernate()
{
if (canAction("CanHibernate")) { return "yes"; }
return "no";
}
const QString Manager::CanHybridSleep()
{
if (canAction("CanHybridSleep")) { return "yes"; }
return "no";
}
const QString Manager::Inhibit(const QString &what,
const QString &who,
const QString &why,
const QString &mode)
{
QString result;
qDebug() << "Inhibit requested" << what << who << why << mode;
// https://github.com/rodlie/nologind/issues/2
result = "/tmp/fofo"; // whatever, we don't care (for now)
return result;
}