MythTV  master
mythfrontend/exitprompt.cpp
Go to the documentation of this file.
1 // Qt
2 #include <QCoreApplication>
3 
4 // MythTV
5 #include "config.h"
6 #include "exitprompt.h"
7 #include "mythcontext.h"
8 #include "mythdialogbox.h"
9 #include "mythmainwindow.h"
10 #include "mythscreenstack.h"
11 #include "mythsystemlegacy.h"
12 #include "mythlogging.h"
13 #include "exitcodes.h"
14 
16  : m_power(MythPower::AcquireRelease(this, true))
17 {
18  m_haltCommand = gCoreContext->GetSetting("HaltCommand", "");
19  m_rebootCommand = gCoreContext->GetSetting("RebootCommand", "");
20  m_suspendCommand = gCoreContext->GetSetting("SuspendCommand", "");
21 }
22 
24 {
25  if (m_power)
26  MythPower::AcquireRelease(this, false);
27 }
28 
30 {
31  qApp->exit();
32 }
33 
35 {
37 }
38 
39 void ExitPrompter::DoHalt(bool Confirmed)
40 {
41  if (!Confirmed)
42  return;
43 
44  // Use user specified command if it exists
45  if (!m_haltCommand.isEmpty())
46  {
48  if (ret == GENERIC_EXIT_OK)
49  return;
50 
51  LOG(VB_GENERAL, LOG_ERR,
52  "User defined HaltCommand failed, falling back to "
53  "alternative methods.");
54  }
55 
56  // Otherwise use MythPower
59  return;
60 
61  // halt of last resort
62  myth_system("sudo /sbin/halt -p");
63 }
64 
66 {
68 }
69 
70 void ExitPrompter::DoReboot(bool Confirmed)
71 {
72  if (!Confirmed)
73  return;
74 
75  if (!m_rebootCommand.isEmpty())
76  {
78  if (ret == GENERIC_EXIT_OK)
79  return;
80 
81  LOG(VB_GENERAL, LOG_ERR,
82  "User defined RebootCommand failed, falling back to "
83  "alternative methods.");
84  }
85 
86  // Otherwise use MythPower
89  return;
90 
91  // reboot of last resort
92  myth_system("sudo /sbin/reboot");
93 }
94 
96 {
98 }
99 
100 void ExitPrompter::DoSuspend(bool Confirmed)
101 {
102  if (!Confirmed)
103  return;
104 
105  // Use user specified command if it exists
106  if (!m_suspendCommand.isEmpty())
107  {
109  if (ret == GENERIC_EXIT_OK)
110  return;
111 
112  LOG(VB_GENERAL, LOG_ERR,
113  "User defined SuspendCommand failed, falling back to "
114  "alternative methods.");
115  }
116 
119 }
120 
122 {
124 }
125 
127 {
128  // HACK IsFrontendOnly() triggers a popup if there is no BE connection.
129  // We really don't need that right now. This hack prevents it.
131 
132  // first of all find out, if this is a frontend only host...
133  bool frontendOnly = gCoreContext->IsFrontendOnly();
134 
135  // HACK Undo the hack, just in case we _don't_ quit:
137 
138  // how do you want to quit today?
139  bool allowExit = false;
140  bool allowReboot = false;
141  bool allowShutdown = false;
142  bool allowStandby = false;
143  bool allowSuspend = false;
144 
145  bool haveshutdown = !m_haltCommand.isEmpty();
146  bool havereboot = !m_rebootCommand.isEmpty();
147  bool havesuspend = !m_suspendCommand.isEmpty();
148 
149 #ifdef Q_OS_ANDROID
150  haveshutdown = false;
151  havereboot = false;
152  havesuspend = false;
153 #endif
154 
155  if (m_power)
156  {
160  }
161 
162  switch (gCoreContext->GetNumSetting("OverrideExitMenu", 0))
163  {
164  case 0:
165  allowExit = true;
166  if (frontendOnly)
167  allowShutdown = haveshutdown;
168  break;
169  case 1:
170  allowExit = true;
171  break;
172  case 2:
173  allowExit = true;
174  allowShutdown = haveshutdown;
175  break;
176  case 3:
177  allowExit = true;
178  allowReboot = havereboot;
179  allowShutdown = haveshutdown;
180  break;
181  case 4:
182  allowShutdown = haveshutdown;
183  break;
184  case 5:
185  allowReboot = havereboot;
186  break;
187  case 6:
188  allowReboot = havereboot;
189  allowShutdown = haveshutdown;
190  break;
191  case 7:
192  allowStandby = true;
193  break;
194  case 8:
195  allowSuspend = havesuspend;
196  break;
197  case 9:
198  allowExit = true;
199  allowSuspend = havesuspend;
200  break;
201  }
202 
203  MythScreenStack *ss = GetMythMainWindow()->GetStack("popup stack");
204  auto *dlg = new MythDialogBox(tr("Do you really want to exit MythTV?"), ss,
205  "exit prompt");
206 
207  if (!dlg->Create())
208  {
209  LOG(VB_GENERAL, LOG_ERR, "Can't create Exit Prompt dialog?");
210  delete dlg;
211  DoQuit();
212  return;
213  }
214 
215  dlg->AddButton(QCoreApplication::translate("(Common)", "No"));
216  if (allowExit)
217  dlg->AddButton(tr("Yes, Exit now"), SLOT(DoQuit()));
218  if (allowReboot)
219  dlg->AddButton(tr("Yes, Exit and Reboot"), SLOT(ConfirmReboot()));
220  if (allowShutdown)
221  dlg->AddButton(tr("Yes, Exit and Shutdown"), SLOT(ConfirmHalt()));
222  if (allowStandby)
223  dlg->AddButton(tr("Yes, Enter Standby Mode"), SLOT(DoStandby()));
224  if (allowSuspend)
225  dlg->AddButton(tr("Yes, Suspend"), SLOT(ConfirmSuspend()));
226 
227  // This is a hack so that the button clicks target the correct slot:
228  dlg->SetReturnEvent(this, QString());
229 
230  ss->AddScreen(dlg);
231 }
232 
234 {
235  MythScreenStack *ss = GetMythMainWindow()->GetStack("popup stack");
236 
237  QString msg;
238  switch (Action)
239  {
240  case MythPower::FeatureShutdown: msg = tr("Are you sure you want to shutdown?"); break;
241  case MythPower::FeatureRestart: msg = tr("Are you sure you want to reboot?"); break;
242  case MythPower::FeatureSuspend: msg = tr("Are you sure you want to suspend?"); break;
243  default: break;
244  }
245 
248  msg.prepend(tr("Mythbackend is running on this system. "));
250 
251  auto *dlg = new MythConfirmationDialog(ss, msg);
252 
253  if (!dlg->Create())
254  {
255  delete dlg;
256  DoQuit();
257  return;
258  }
259 
262  else if (Action == MythPower::FeatureRestart)
264  else if (Action == MythPower::FeatureSuspend)
266 
267  ss->AddScreen(dlg);
268 }
virtual bool RequestFeature(Feature Request, bool Delay=true)
Definition: mythpower.cpp:178
void DoReboot(bool Confirmed=true)
VERBOSE_PREAMBLE Most true
Definition: verbosedefs.h:91
Dialog asking for user confirmation.
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
Basic menu dialog, message and a list of options.
MythScreenStack * GetStack(const QString &stackname)
void SetDisableEventPopup(bool check)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:62
void ConfirmSuspend(void) const
An action (for this plugin) consists of a description, and a set of key sequences.
Definition: action.h:40
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
static void DoQuit(void)
QString GetSetting(const QString &key, const QString &defaultval="")
unsigned int uint
Definition: compat.h:140
uint myth_system(const QString &command, uint flags, uint timeout)
static void DoStandby(void)
MythMainWindow * GetMythMainWindow(void)
int GetNumSetting(const QString &key, int defaultval=0)
void ConfirmReboot(void) const
void ConfirmHalt(void) const
void DoSuspend(bool Confirmed=true)
bool IsFrontendOnly(void)
is there a frontend, but no backend, running on this host
static MythPower * AcquireRelease(void *Reference, bool Acquire, uint MinimumDelay=0)
Definition: mythpower.cpp:70
bool IsFeatureSupported(Feature Supported)
Definition: mythpower.cpp:160
void Confirm(MythPower::Feature Action) const
void DoHalt(bool Confirmed=true)
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23