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