Unity 8
Dialogs.qml
1 /*
2  * Copyright (C) 2014 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 import QtQuick 2.0
18 
19 import Unity.Application 0.1
20 import Unity.Session 0.1
21 import Ubuntu.Components 1.1
22 import GlobalShortcut 1.0
23 import "../Greeter"
24 
25 Item {
26  id: root
27 
28  // to be set from outside, useful mostly for testing purposes
29  property var unitySessionService: DBusUnitySessionService
30  property var closeAllApps: function() {
31  while (true) {
32  var app = ApplicationManager.get(0);
33  if (app === null) {
34  break;
35  }
36  ApplicationManager.stopApplication(app.appId);
37  }
38  }
39  property string usageScenario
40 
41  signal powerOffClicked();
42 
43  function showPowerDialog() {
44  d.showPowerDialog();
45  }
46 
47  GlobalShortcut { // reboot/shutdown dialog
48  shortcut: Qt.Key_PowerDown
49  active: root.usageScenario === "desktop"
50  onTriggered: root.unitySessionService.RequestShutdown()
51  }
52 
53  GlobalShortcut { // reboot/shutdown dialog
54  shortcut: Qt.Key_PowerOff
55  active: root.usageScenario === "desktop"
56  onTriggered: root.unitySessionService.RequestShutdown()
57  }
58 
59  GlobalShortcut { // sleep
60  shortcut: Qt.Key_Sleep
61  onTriggered: root.unitySessionService.Suspend()
62  }
63 
64  GlobalShortcut { // hibernate
65  shortcut: Qt.Key_Hibernate
66  onTriggered: root.unitySessionService.Hibernate()
67  }
68 
69  GlobalShortcut { // logout/lock dialog
70  shortcut: Qt.Key_LogOff
71  onTriggered: root.unitySessionService.RequestLogout()
72  }
73 
74  GlobalShortcut { // logout/lock dialog
75  shortcut: Qt.ControlModifier|Qt.AltModifier|Qt.Key_Delete
76  onTriggered: root.unitySessionService.RequestLogout()
77  }
78 
79  GlobalShortcut { // lock screen
80  shortcut: Qt.Key_ScreenSaver
81  onTriggered: lightDM.greeter.showGreeter()
82  }
83 
84  GlobalShortcut { // lock screen
85  shortcut: Qt.ControlModifier|Qt.AltModifier|Qt.Key_L
86  onTriggered: lightDM.greeter.showGreeter()
87  }
88 
89  QtObject {
90  id: d // private stuff
91  objectName: "dialogsPrivate"
92 
93  function showPowerDialog() {
94  if (!dialogLoader.active) {
95  dialogLoader.sourceComponent = powerDialogComponent;
96  dialogLoader.focus = true;
97  dialogLoader.active = true;
98  }
99  }
100  }
101 
102  Loader {
103  id: dialogLoader
104  objectName: "dialogLoader"
105  anchors.fill: parent
106  active: false
107  }
108 
109  LightDM {id: lightDM} // Provide backend access
110 
111  Component {
112  id: logoutDialogComponent
113  ShellDialog {
114  id: logoutDialog
115  title: i18n.ctr("Title: Lock/Log out dialog", "Log out")
116  text: i18n.tr("Are you sure you want to log out?")
117  Button {
118  text: i18n.ctr("Button: Lock the system", "Lock")
119  onClicked: {
120  lightDM.greeter.showGreeter()
121  logoutDialog.hide();
122  }
123  }
124  Button {
125  text: i18n.ctr("Button: Log out from the system", "Log Out")
126  onClicked: {
127  unitySessionService.logout();
128  logoutDialog.hide();
129  }
130  }
131  Button {
132  text: i18n.tr("Cancel")
133  onClicked: {
134  logoutDialog.hide();
135  }
136  }
137  }
138  }
139 
140  Component {
141  id: shutdownDialogComponent
142  ShellDialog {
143  id: shutdownDialog
144  title: i18n.ctr("Title: Reboot/Shut down dialog", "Shut down")
145  text: i18n.tr("Are you sure you want to shut down?")
146  Button {
147  text: i18n.ctr("Button: Reboot the system", "Reboot")
148  onClicked: {
149  root.closeAllApps();
150  unitySessionService.reboot();
151  shutdownDialog.hide();
152  }
153  color: UbuntuColors.lightGrey
154  }
155  Button {
156  text: i18n.ctr("Button: Shut down the system", "Shut down")
157  onClicked: {
158  root.closeAllApps();
159  unitySessionService.shutdown();
160  shutdownDialog.hide();
161  }
162  color: UbuntuColors.red
163  }
164  Button {
165  text: i18n.tr("Cancel")
166  onClicked: {
167  shutdownDialog.hide();
168  }
169  color: UbuntuColors.lightGrey
170  }
171  }
172  }
173 
174  Component {
175  id: rebootDialogComponent
176  ShellDialog {
177  id: rebootDialog
178  title: i18n.ctr("Title: Reboot dialog", "Reboot")
179  text: i18n.tr("Are you sure you want to reboot?")
180  Button {
181  text: i18n.tr("No")
182  onClicked: {
183  rebootDialog.hide();
184  }
185  color: UbuntuColors.lightGrey
186  }
187  Button {
188  text: i18n.tr("Yes")
189  onClicked: {
190  root.closeAllApps();
191  unitySessionService.reboot();
192  rebootDialog.hide();
193  }
194  color: UbuntuColors.red
195  }
196  }
197  }
198 
199  Component {
200  id: powerDialogComponent
201  ShellDialog {
202  id: powerDialog
203  title: i18n.ctr("Title: Power off/Restart dialog", "Power")
204  text: i18n.tr("Are you sure you would like\nto power off?")
205  Button {
206  text: i18n.ctr("Button: Power off the system", "Power off")
207  onClicked: {
208  root.closeAllApps();
209  powerDialog.hide();
210  root.powerOffClicked();
211  }
212  color: UbuntuColors.red
213  }
214  Button {
215  text: i18n.ctr("Button: Restart the system", "Restart")
216  onClicked: {
217  root.closeAllApps();
218  unitySessionService.reboot();
219  powerDialog.hide();
220  }
221  color: UbuntuColors.lightGrey
222  }
223  Button {
224  text: i18n.tr("Cancel")
225  onClicked: {
226  powerDialog.hide();
227  }
228  color: UbuntuColors.lightGrey
229  }
230  }
231  }
232 
233  Connections {
234  target: root.unitySessionService
235 
236  onLogoutRequested: {
237  // Display a dialog to ask the user to confirm.
238  if (!dialogLoader.active) {
239  dialogLoader.sourceComponent = logoutDialogComponent;
240  dialogLoader.focus = true;
241  dialogLoader.active = true;
242  }
243  }
244 
245  onShutdownRequested: {
246  // Display a dialog to ask the user to confirm.
247  if (!dialogLoader.active) {
248  dialogLoader.sourceComponent = shutdownDialogComponent;
249  dialogLoader.focus = true;
250  dialogLoader.active = true;
251  }
252  }
253 
254  onRebootRequested: {
255  // Display a dialog to ask the user to confirm.
256  if (!dialogLoader.active) {
257  // display a combined reboot/shutdown dialog, sadly the session indicator calls rather the "Reboot()" method
258  // than shutdown when clicking on the "Shutdown..." menu item
259  // FIXME: when/if session indicator is fixed, put the rebootDialogComponent here
260  dialogLoader.sourceComponent = shutdownDialogComponent;
261  dialogLoader.focus = true;
262  dialogLoader.active = true;
263  }
264  }
265 
266  onLogoutReady: {
267  root.closeAllApps();
268  Qt.quit();
269  unitySessionService.endSession();
270  }
271  }
272 
273 }