-
Notifications
You must be signed in to change notification settings - Fork 343
/
exitprompt.cpp
217 lines (189 loc) · 6.55 KB
/
exitprompt.cpp
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "config.h"
#include <QCoreApplication>
#if CONFIG_QTDBUS
#include <QtDBus>
#include <QDBusInterface>
#endif
#include "exitprompt.h"
#include "mythcontext.h"
#include "mythdialogbox.h"
#include "mythmainwindow.h"
#include "mythscreenstack.h"
#include "mythsystem.h"
#include "mythlogging.h"
#include "exitcodes.h"
void ExitPrompter::quit()
{
qApp->exit();
}
static bool DBusHalt(void)
{
#if CONFIG_QTDBUS
QDBusInterface kde("org.kde.ksmserver",
"/KSMServer",
"org.kde.KSMServerInterface");
QDBusInterface gnome("org.gnome.SessionManager",
"/org/gnome/SessionManager",
"org.gnome.SessionManager");
QDBusInterface consolekit("org.freedesktop.ConsoleKit",
"/org/freedesktop/ConsoleKit/Manager",
"org.freedesktop.ConsoleKit.Manager",
QDBusConnection::systemBus());
QDBusInterface hal("org.freedesktop.Hal",
"/org/freedesktop/Hal/devices/computer",
"org.freedesktop.Hal.Device.SystemPowerManagement",
QDBusConnection::systemBus());
QDBusReply<void> void_reply = kde.call("logout", 0, 2, 2);
QDBusReply<bool> bool_reply;
QDBusReply<int> int_reply;
if (!void_reply.isValid())
{
bool_reply = gnome.call("CanShutdown");
if (bool_reply.isValid() && bool_reply.value() == 1)
void_reply = gnome.call("RequestShutdown");
}
if (!void_reply.isValid())
{
bool_reply = consolekit.call("CanStop");
if (bool_reply.isValid() && bool_reply.value() == 1)
void_reply = consolekit.call("Stop");
}
if (!void_reply.isValid())
int_reply = hal.call("Shutdown");
return void_reply.isValid() || int_reply.isValid();
#endif
return false;
}
void ExitPrompter::halt()
{
QString halt_cmd = gCoreContext->GetSetting("HaltCommand","");
int ret = -1;
if (!halt_cmd.isEmpty()) /* Use user specified command if it exists */
{
ret = myth_system(halt_cmd);
if (ret != GENERIC_EXIT_OK)
LOG(VB_GENERAL, LOG_ERR,
"User defined HaltCommand failed, falling back to "
"alternative methods.");
}
/* If supported, use DBus to shutdown */
if (ret != GENERIC_EXIT_OK && !DBusHalt())
myth_system("sudo /sbin/halt -p");
}
static bool DBusReboot(void)
{
#if CONFIG_QTDBUS
QDBusInterface kde("org.kde.ksmserver",
"/KSMServer",
"org.kde.KSMServerInterface");
QDBusInterface gnome("org.gnome.SessionManager",
"/org/gnome/SessionManager",
"org.gnome.SessionManager");
QDBusInterface consolekit("org.freedesktop.ConsoleKit",
"/org/freedesktop/ConsoleKit/Manager",
"org.freedesktop.ConsoleKit.Manager",
QDBusConnection::systemBus());
QDBusInterface hal("org.freedesktop.Hal",
"/org/freedesktop/Hal/devices/computer",
"org.freedesktop.Hal.Device.SystemPowerManagement",
QDBusConnection::systemBus());
QDBusReply<void> void_reply = kde.call("logout", 0, 1, 2);
QDBusReply<bool> bool_reply;
QDBusReply<int> int_reply;
if (!void_reply.isValid())
{
bool_reply = gnome.call("CanShutdown");
if (bool_reply.isValid() && bool_reply.value() == 1)
void_reply=gnome.call("RequestReboot");
}
if (!void_reply.isValid())
{
bool_reply = consolekit.call("CanRestart");
if (bool_reply.isValid() && bool_reply.value() == 1)
void_reply = consolekit.call("Restart");
}
if (!void_reply.isValid())
int_reply = hal.call("Reboot");
return void_reply.isValid() || int_reply.isValid();
#endif
return false;
}
void ExitPrompter::reboot()
{
QString reboot_cmd = gCoreContext->GetSetting("RebootCommand","");
int ret = -1;
if (!reboot_cmd.isEmpty()) /* Use user specified command if it exists */
{
ret = myth_system(reboot_cmd);
if (ret != GENERIC_EXIT_OK)
LOG(VB_GENERAL, LOG_ERR,
"User defined RebootCommand failed, falling back to "
"alternative methods.");
}
/* If supported, use DBus to reboot */
if (ret != GENERIC_EXIT_OK && !DBusReboot())
myth_system("sudo /sbin/reboot");
}
void ExitPrompter::handleExit()
{
// IsFrontendOnly() triggers a popup if there is no BE connection.
// We really don't need that right now. This hack prevents it.
gContext->SetDisableEventPopup(true);
// first of all find out, if this is a frontend only host...
bool frontendOnly = gCoreContext->IsFrontendOnly();
// Undo the hack, just in case we _don't_ quit:
gContext->SetDisableEventPopup(false);
// how do you want to quit today?
bool allowExit = false;
bool allowReboot = false;
bool allowShutdown = false;
switch (gCoreContext->GetNumSetting("OverrideExitMenu", 0))
{
case 0:
allowExit = true;
if (frontendOnly)
allowShutdown = true;
break;
case 1:
allowExit = true;
break;
case 2:
allowExit = true;
allowShutdown = true;
break;
case 3:
allowExit = true;
allowReboot = true;
allowShutdown = true;
break;
case 4:
allowShutdown = true;
break;
case 5:
allowReboot = true;
break;
case 6:
allowReboot = true;
allowShutdown = true;
break;
}
MythScreenStack *ss = GetMythMainWindow()->GetStack("popup stack");
MythDialogBox *dlg = new MythDialogBox(
tr("Do you really want to exit MythTV?"), ss, "exit prompt");
if (!dlg->Create())
{
LOG(VB_GENERAL, LOG_ERR, "Can't create Exit Prompt dialog?");
delete dlg;
quit();
}
dlg->AddButton(tr("No"));
if (allowExit)
dlg->AddButton(QObject::tr("Yes, Exit now"), SLOT(quit()));
if (allowReboot)
dlg->AddButton(QObject::tr("Yes, Exit and Reboot"), SLOT(reboot()));
if (allowShutdown)
dlg->AddButton(QObject::tr("Yes, Exit and Shutdown"), SLOT(halt()));
// This is a hack so that the button clicks target the correct slot:
dlg->SetReturnEvent(this, QString());
ss->AddScreen(dlg);
}