on xenial help sign in
Add reboot/shutdown Fixes: 1234062

Approved by: Michael Zanetti

commit7a1fa198e3ccb9acfdc953b7760a25dc5d48da86 authorYing-Chun Liu <paul.liu@canonical.com> dateThu 17 Jul 2014, 09:37 (12 years ago) parentf47831ec4d5467
3 files changed+283−6
plugins/Unity/Session/dbusunitysessionservice.cpp+33 plugins/Unity/Session/dbusunitysessionservice.h+61−4 qml/Shell.qml+189−2
plugins/Unity/Session/dbusunitysessionservice.cpp+33
@@ -19,6 +19,7 @@
1919
2020// Qt
2121#include <QDBusConnection>
22+#include <QDBusInterface>
2223
2324DBusUnitySessionService::DBusUnitySessionService() : QObject()
2425{
@@ -44,3 +45,35 @@ void DBusUnitySessionService::RequestLogout()
4445{
4546 Q_EMIT logoutRequested(false);
4647}
48+
49+void DBusUnitySessionService::Reboot()
50+{
51+ QDBusConnection connection = QDBusConnection::systemBus();
52+ QDBusInterface iface1 ("org.freedesktop.login1",
53+ "/org/freedesktop/login1",
54+ "org.freedesktop.login1.Manager",
55+ connection);
56+
57+ iface1.call("Reboot", false);
58+}
59+
60+void DBusUnitySessionService::RequestReboot()
61+{
62+ Q_EMIT rebootRequested(false);
63+}
64+
65+void DBusUnitySessionService::Shutdown()
66+{
67+ QDBusConnection connection = QDBusConnection::systemBus();
68+ QDBusInterface iface1 ("org.freedesktop.login1",
69+ "/org/freedesktop/login1",
70+ "org.freedesktop.login1.Manager",
71+ connection);
72+
73+ iface1.call("PowerOff", false);
74+}
75+
76+void DBusUnitySessionService::RequestShutdown()
77+{
78+ Q_EMIT shutdownRequested(false);
79+}
plugins/Unity/Session/dbusunitysessionservice.h+61−4
@@ -46,6 +46,27 @@ Q_SIGNALS:
4646 */
4747 void logoutRequested(bool have_inhibitors);
4848
49+ /**
50+ * rebootRequested signal
51+ *
52+ * This signal is emitted when some applications request the system to
53+ * reboot.
54+ * @param have_inhibitors if there are any special running applications
55+ * which inhibit the reboot.
56+ */
57+ void rebootRequested(bool have_inhibitors);
58+
59+ /**
60+ * shutdownRequested signal
61+ *
62+ * This signal is emitted when some applications request the system to
63+ * shutdown.
64+ * @param have_inhibitors if there are any special running applications
65+ * which inhibit the shutdown.
66+ */
67+ void shutdownRequested(bool have_inhibitors);
68+
69+
4970 /**
5071 * logoutReady signal
5172 *
@@ -61,22 +82,58 @@ public Q_SLOTS:
6182 * This method directly logout the system without user's confirmation.
6283 * Ordinary applications should avoid calling this method. Please call
6384 * RequestLogout() to ask the user to decide logout or not.
64− * This method will stop all the running applications and then signal
65− * logoutReady when all the apps stopped.
6685 */
6786 Q_SCRIPTABLE void Logout();
6887
88+ /**
89+ * Reboot the system.
90+ *
91+ * This method directly reboot the system without user's confirmation.
92+ * Ordinary applications should avoid calling this method. Please call
93+ * RequestReboot() to ask the user to decide reboot or not.
94+ */
95+ Q_SCRIPTABLE void Reboot();
96+
97+ /**
98+ * Shutdown the system.
99+ *
100+ * This method directly shutdown the system without user's confirmation.
101+ * Ordinary applications should avoid calling this method. Please call
102+ * RequestShutdown() to ask the user to decide shutdown or not.
103+ */
104+ Q_SCRIPTABLE void Shutdown();
105+
69106 /**
70107 * Issue a logout request.
71108 *
72109 * This method emit the logoutRequested signal to the shell with a boolean
73110 * which indicates if there's any inhibitors. The shell should receive
74111 * this signal and display a dialog to ask the user to confirm the logout
75− * action. If confirmed, shell can call Logout() method to kill the apps
76− * and then logout
112+ * action. If confirmed, shell can call Logout() method to logout.
77113 */
78114 Q_SCRIPTABLE void RequestLogout();
79115
116+ /**
117+ * Issue a reboot request.
118+ *
119+ * This method emit the rebootRequested signal to the shell with a boolean
120+ * which indicates if there's any inhibitors. The shell should receive
121+ * this signal and display a dialog to ask the user to confirm the reboot
122+ * action. If confirmed, shell can call Reboot() method to reboot
123+ */
124+ Q_SCRIPTABLE void RequestReboot();
125+
126+ /**
127+ * Issue a shutdown request.
128+ *
129+ * This method emit the shutdownRequested signal to the shell with a
130+ * boolean which indicates if there's any inhibitors.
131+ * The shell should receive
132+ * this signal and display a dialog to ask the user to confirm the reboot
133+ * action. If confirmed, shell can call Shutdown() method to reboot
134+ */
135+ Q_SCRIPTABLE void RequestShutdown();
136+
80137};
81138
82139#endif // DBUSUNITYSESSIONSERVICE_H
qml/Shell.qml+189−2
@@ -19,6 +19,7 @@ import AccountsService 0.1
1919import GSettings 1.0
2020import Unity.Application 0.1
2121import Ubuntu.Components 0.1
22+import Ubuntu.Components.Popups 0.1
2223import Ubuntu.Gestures 0.1
2324import Unity.Launcher 0.1
2425import LightDM 0.1 as LightDM
@@ -268,7 +269,128 @@ FocusScope {
268269 }
269270 }
270271
272+ property bool dialogShown: false
273+
274+ Component {
275+ id: logoutDialog
276+ Dialog {
277+ id: dialogueLogout
278+ title: "Logout"
279+ text: "Are you sure that you want to logout?"
280+ Button {
281+ text: "Cancel"
282+ onClicked: {
283+ PopupUtils.close(dialogueLogout);
284+ stages.dialogShown = false;
285+ }
286+ }
287+ Button {
288+ text: "Yes"
289+ onClicked: {
290+ DBusUnitySessionService.Logout();
291+ PopupUtils.close(dialogueLogout);
292+ stages.dialogShown = false;
293+ }
294+ }
295+ }
296+ }
297+
298+ Component {
299+ id: shutdownDialog
300+ Dialog {
301+ id: dialogueShutdown
302+ title: "Shutdown"
303+ text: "Are you sure that you want to shutdown?"
304+ Button {
305+ text: "Cancel"
306+ onClicked: {
307+ PopupUtils.close(dialogueShutdown);
308+ stages.dialogShown = false;
309+ }
310+ }
311+ Button {
312+ text: "Yes"
313+ onClicked: {
314+ dBusUnitySessionServiceConnection.closeAllApps();
315+ DBusUnitySessionService.Shutdown();
316+ PopupUtils.close(dialogueShutdown);
317+ stages.dialogShown = false;
318+ }
319+ }
320+ }
321+ }
322+
323+ Component {
324+ id: rebootDialog
325+ Dialog {
326+ id: dialogueReboot
327+ title: "Reboot"
328+ text: "Are you sure that you want to reboot?"
329+ Button {
330+ text: "Cancel"
331+ onClicked: {
332+ PopupUtils.close(dialogueReboot)
333+ stages.dialogShown = false;
334+ }
335+ }
336+ Button {
337+ text: "Yes"
338+ onClicked: {
339+ dBusUnitySessionServiceConnection.closeAllApps();
340+ DBusUnitySessionService.Reboot();
341+ PopupUtils.close(dialogueReboot);
342+ stages.dialogShown = false;
343+ }
344+ }
345+ }
346+ }
347+
348+ Component {
349+ id: powerDialog
350+ Dialog {
351+ id: dialoguePower
352+ title: "Power"
353+ text: i18n.tr("Are you sure you would like to turn power off?")
354+ Button {
355+ text: i18n.tr("Power off")
356+ onClicked: {
357+ dBusUnitySessionServiceConnection.closeAllApps();
358+ PopupUtils.close(dialoguePower);
359+ stages.dialogShown = false;
360+ shutdownFadeOutRectangle.enabled = true;
361+ shutdownFadeOutRectangle.visible = true;
362+ shutdownFadeOut.start();
363+ }
364+ }
365+ Button {
366+ text: i18n.tr("Restart")
367+ onClicked: {
368+ dBusUnitySessionServiceConnection.closeAllApps();
369+ DBusUnitySessionService.Reboot();
370+ PopupUtils.close(dialoguePower);
371+ stages.dialogShown = false;
372+ }
373+ }
374+ Button {
375+ text: i18n.tr("Cancel")
376+ onClicked: {
377+ PopupUtils.close(dialoguePower);
378+ stages.dialogShown = false;
379+ }
380+ }
381+ }
382+ }
383+
384+ function showPowerDialog() {
385+ if (!stages.dialogShown) {
386+ stages.dialogShown = true;
387+ PopupUtils.open(powerDialog);
388+ }
389+ }
390+
271391 Connections {
392+ id: dBusUnitySessionServiceConnection
393+ objectName: "dBusUnitySessionServiceConnection"
272394 target: DBusUnitySessionService
273395
274396 function closeAllApps() {
@@ -282,8 +404,27 @@ FocusScope {
282404 }
283405
284406 onLogoutRequested: {
285− // TODO: Display a dialog to ask the user to confirm.
286− DBusUnitySessionService.Logout();
407+ // Display a dialog to ask the user to confirm.
408+ if (!stages.dialogShown) {
409+ stages.dialogShown = true;
410+ PopupUtils.open(logoutDialog);
411+ }
412+ }
413+
414+ onShutdownRequested: {
415+ // Display a dialog to ask the user to confirm.
416+ if (!stages.dialogShown) {
417+ stages.dialogShown = true;
418+ PopupUtils.open(shutdownDialog);
419+ }
420+ }
421+
422+ onRebootRequested: {
423+ // Display a dialog to ask the user to confirm.
424+ if (!stages.dialogShown) {
425+ stages.dialogShown = true;
426+ PopupUtils.open(rebootDialog);
427+ }
287428 }
288429
289430 onLogoutReady: {
@@ -690,4 +831,50 @@ FocusScope {
690831 target: SessionBroadcast
691832 onShowHome: showHome()
692833 }
834+
835+ Keys.onPressed: {
836+ if (event.key == Qt.Key_PowerOff || event.key == Qt.Key_PowerDown) {
837+ if (!powerKeyTimer.running) {
838+ powerKeyTimer.start();
839+ }
840+ event.accepted = true;
841+ }
842+ }
843+
844+ Keys.onReleased: {
845+ if (event.key == Qt.Key_PowerOff || event.key == Qt.Key_PowerDown) {
846+ powerKeyTimer.stop();
847+ event.accepted = true;
848+ }
849+ }
850+
851+ Timer {
852+ id: powerKeyTimer
853+ interval: 2000
854+ repeat: false
855+ triggeredOnStart: false
856+
857+ onTriggered: {
858+ stages.showPowerDialog();
859+ }
860+ }
861+
862+ Rectangle {
863+ id: shutdownFadeOutRectangle
864+ enabled: false
865+ visible: false
866+ color: "black"
867+ anchors.fill: parent
868+ opacity: 0.0
869+ NumberAnimation on opacity {
870+ id: shutdownFadeOut
871+ from: 0.0
872+ to: 1.0
873+ onStopped: {
874+ if (shutdownFadeOutRectangle.enabled && shutdownFadeOutRectangle.visible) {
875+ DBusUnitySessionService.Shutdown();
876+ }
877+ }
878+ }
879+ }
693880}