Unity 8
Dialogs.qml
1 /*
2  * Copyright (C) 2014-2015 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.4
18 
19 import Unity.Application 0.1
20 import Unity.Session 0.1
21 import GlobalShortcut 1.0
22 import Ubuntu.Components 1.3
23 import Unity.Platform 1.0
24 import Utils 0.1
25 import "../Greeter"
26 
27 Item {
28  id: root
29 
30  // to be set from outside, useful mostly for testing purposes
31  property var unitySessionService: DBusUnitySessionService
32  property var closeAllApps: function() {
33  while (true) {
34  var app = ApplicationManager.get(0);
35  if (app === null) {
36  break;
37  }
38  ApplicationManager.stopApplication(app.appId);
39  }
40  }
41  property string usageScenario
42 
43  signal powerOffClicked();
44 
45  function showPowerDialog() {
46  d.showPowerDialog();
47  }
48 
49  onUsageScenarioChanged: {
50  if (usageScenario != "desktop" && legacyAppsModel.count > 0 && !d.modeSwitchWarningPopup) {
51  var comp = Qt.createComponent(Qt.resolvedUrl("ModeSwitchWarningDialog.qml"))
52  d.modeSwitchWarningPopup = comp.createObject(root, {model: legacyAppsModel});
53  d.modeSwitchWarningPopup.forceClose.connect(function() {
54  while (legacyAppsModel.count > 0) {
55  ApplicationManager.stopApplication(legacyAppsModel.get(0).appId);
56  }
57  d.modeSwitchWarningPopup.hide();
58  d.modeSwitchWarningPopup.destroy();
59  d.modeSwitchWarningPopup = null;
60  })
61  } else if (usageScenario == "desktop" && d.modeSwitchWarningPopup) {
62  d.modeSwitchWarningPopup.hide();
63  d.modeSwitchWarningPopup.destroy();
64  d.modeSwitchWarningPopup = null;
65  }
66  }
67 
68  ApplicationsFilterModel {
69  id: legacyAppsModel
70  applicationsModel: ApplicationManager
71  filterTouchApps: true
72  }
73 
74  GlobalShortcut { // reboot/shutdown dialog
75  shortcut: Qt.Key_PowerDown
76  active: Platform.isPC
77  onTriggered: root.unitySessionService.RequestShutdown()
78  }
79 
80  GlobalShortcut { // reboot/shutdown dialog
81  shortcut: Qt.Key_PowerOff
82  active: Platform.isPC
83  onTriggered: root.unitySessionService.RequestShutdown()
84  }
85 
86  GlobalShortcut { // sleep
87  shortcut: Qt.Key_Sleep
88  onTriggered: root.unitySessionService.Suspend()
89  }
90 
91  GlobalShortcut { // hibernate
92  shortcut: Qt.Key_Hibernate
93  onTriggered: root.unitySessionService.Hibernate()
94  }
95 
96  GlobalShortcut { // logout/lock dialog
97  shortcut: Qt.Key_LogOff
98  onTriggered: root.unitySessionService.RequestLogout()
99  }
100 
101  GlobalShortcut { // logout/lock dialog
102  shortcut: Qt.ControlModifier|Qt.AltModifier|Qt.Key_Delete
103  onTriggered: root.unitySessionService.RequestLogout()
104  }
105 
106  GlobalShortcut { // lock screen
107  shortcut: Qt.Key_ScreenSaver
108  onTriggered: lightDM.greeter.showGreeter()
109  }
110 
111  GlobalShortcut { // lock screen
112  shortcut: Qt.ControlModifier|Qt.AltModifier|Qt.Key_L
113  onTriggered: lightDM.greeter.showGreeter()
114  }
115 
116  QtObject {
117  id: d // private stuff
118  objectName: "dialogsPrivate"
119 
120  property var modeSwitchWarningPopup: null
121 
122  function showPowerDialog() {
123  if (!dialogLoader.active) {
124  dialogLoader.sourceComponent = powerDialogComponent;
125  dialogLoader.focus = true;
126  dialogLoader.active = true;
127  }
128  }
129  }
130 
131  Loader {
132  id: dialogLoader
133  objectName: "dialogLoader"
134  anchors.fill: parent
135  active: false
136  }
137 
138  LightDM {id: lightDM} // Provide backend access
139 
140  Component {
141  id: logoutDialogComponent
142  ShellDialog {
143  id: logoutDialog
144  title: i18n.ctr("Title: Lock/Log out dialog", "Log out")
145  text: i18n.tr("Are you sure you want to log out?")
146  Button {
147  text: i18n.ctr("Button: Lock the system", "Lock")
148  onClicked: {
149  lightDM.greeter.showGreeter()
150  logoutDialog.hide();
151  }
152  }
153  Button {
154  text: i18n.ctr("Button: Log out from the system", "Log Out")
155  onClicked: {
156  unitySessionService.logout();
157  logoutDialog.hide();
158  }
159  }
160  Button {
161  text: i18n.tr("Cancel")
162  onClicked: {
163  logoutDialog.hide();
164  }
165  }
166  }
167  }
168 
169  Component {
170  id: shutdownDialogComponent
171  ShellDialog {
172  id: shutdownDialog
173  title: i18n.ctr("Title: Reboot/Shut down dialog", "Shut down")
174  text: i18n.tr("Are you sure you want to shut down?")
175  Button {
176  text: i18n.ctr("Button: Reboot the system", "Reboot")
177  onClicked: {
178  root.closeAllApps();
179  unitySessionService.reboot();
180  shutdownDialog.hide();
181  }
182  color: UbuntuColors.lightGrey
183  }
184  Button {
185  text: i18n.ctr("Button: Shut down the system", "Shut down")
186  onClicked: {
187  root.closeAllApps();
188  unitySessionService.shutdown();
189  shutdownDialog.hide();
190  }
191  color: UbuntuColors.red
192  }
193  Button {
194  text: i18n.tr("Cancel")
195  onClicked: {
196  shutdownDialog.hide();
197  }
198  color: UbuntuColors.lightGrey
199  }
200  }
201  }
202 
203  Component {
204  id: rebootDialogComponent
205  ShellDialog {
206  id: rebootDialog
207  title: i18n.ctr("Title: Reboot dialog", "Reboot")
208  text: i18n.tr("Are you sure you want to reboot?")
209  Button {
210  text: i18n.tr("No")
211  onClicked: {
212  rebootDialog.hide();
213  }
214  color: UbuntuColors.lightGrey
215  }
216  Button {
217  text: i18n.tr("Yes")
218  onClicked: {
219  root.closeAllApps();
220  unitySessionService.reboot();
221  rebootDialog.hide();
222  }
223  color: UbuntuColors.red
224  }
225  }
226  }
227 
228  Component {
229  id: powerDialogComponent
230  ShellDialog {
231  id: powerDialog
232  title: i18n.ctr("Title: Power off/Restart dialog", "Power")
233  text: i18n.tr("Are you sure you would like\nto power off?")
234  Button {
235  text: i18n.ctr("Button: Power off the system", "Power off")
236  onClicked: {
237  root.closeAllApps();
238  powerDialog.hide();
239  root.powerOffClicked();
240  }
241  color: UbuntuColors.red
242  }
243  Button {
244  text: i18n.ctr("Button: Restart the system", "Restart")
245  onClicked: {
246  root.closeAllApps();
247  unitySessionService.reboot();
248  powerDialog.hide();
249  }
250  color: UbuntuColors.lightGrey
251  }
252  Button {
253  text: i18n.tr("Cancel")
254  onClicked: {
255  powerDialog.hide();
256  }
257  color: UbuntuColors.lightGrey
258  }
259  }
260  }
261 
262  Connections {
263  target: root.unitySessionService
264 
265  onLogoutRequested: {
266  // Display a dialog to ask the user to confirm.
267  if (!dialogLoader.active) {
268  dialogLoader.sourceComponent = logoutDialogComponent;
269  dialogLoader.focus = true;
270  dialogLoader.active = true;
271  }
272  }
273 
274  onShutdownRequested: {
275  // Display a dialog to ask the user to confirm.
276  if (!dialogLoader.active) {
277  dialogLoader.sourceComponent = shutdownDialogComponent;
278  dialogLoader.focus = true;
279  dialogLoader.active = true;
280  }
281  }
282 
283  onRebootRequested: {
284  // Display a dialog to ask the user to confirm.
285  if (!dialogLoader.active) {
286  // display a combined reboot/shutdown dialog, sadly the session indicator calls rather the "Reboot()" method
287  // than shutdown when clicking on the "Shutdown..." menu item
288  // FIXME: when/if session indicator is fixed, put the rebootDialogComponent here
289  dialogLoader.sourceComponent = shutdownDialogComponent;
290  dialogLoader.focus = true;
291  dialogLoader.active = true;
292  }
293  }
294 
295  onLogoutReady: {
296  root.closeAllApps();
297  Qt.quit();
298  unitySessionService.endSession();
299  }
300  }
301 }