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