on a7fc953 help sign in
unity8/qml/Components/Dialogs.qml
304 lines 10 kB history raw
1/*
2 * Copyright (C) 2014-2017 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 */
17import QtQuick 2.4
18import QtQuick.Window 2.2
19import Unity.Application 0.1
20import Unity.Session 0.1
21import GlobalShortcut 1.0
22import Ubuntu.Components 1.3
23import Unity.Platform 1.0
24import Utils 0.1
25import "../Greeter"
27MouseArea {
28 id: root
29 acceptedButtons: Qt.AllButtons
30 hoverEnabled: true
31 onWheel: wheel.accepted = true
33 readonly property bool hasActiveDialog: dialogLoader.active || d.modeSwitchWarningPopup
35 // to be set from outside, useful mostly for testing purposes
36 property var unitySessionService: DBusUnitySessionService
37 property var closeAllApps: function() {
38 while (true) {
39 var app = ApplicationManager.get(0);
40 if (app === null) {
41 break;
42 }
43 ApplicationManager.stopApplication(app.appId);
44 }
45 }
46 property string usageScenario
47 property size screenSize: Qt.size(Screen.width, Screen.height)
48 property bool hasKeyboard: false
50 signal powerOffClicked();
52 function showPowerDialog() {
53 d.showPowerDialog();
54 }
56 onUsageScenarioChanged: {
57 // if we let the user switch manually to desktop mode, don't display the warning dialog
58 // see MenuItemFactory.qml, for the Desktop Mode switch logic
59 var isTabletSize = Math.min(screenSize.width, screenSize.height) > units.gu(60);
61 if (usageScenario != "desktop" && legacyAppsModel.count > 0 && !d.modeSwitchWarningPopup && !isTabletSize) {
62 var comp = Qt.createComponent(Qt.resolvedUrl("ModeSwitchWarningDialog.qml"))
63 d.modeSwitchWarningPopup = comp.createObject(root, {model: legacyAppsModel});
64 d.modeSwitchWarningPopup.forceClose.connect(function() {
65 for (var i = legacyAppsModel.count - 1; i >= 0; i--) {
66 ApplicationManager.stopApplication(legacyAppsModel.get(i).appId);
67 }
68 d.modeSwitchWarningPopup.hide();
69 d.modeSwitchWarningPopup.destroy();
70 d.modeSwitchWarningPopup = null;
71 })
72 } else if (usageScenario == "desktop" && d.modeSwitchWarningPopup) {
73 d.modeSwitchWarningPopup.hide();
74 d.modeSwitchWarningPopup.destroy();
75 d.modeSwitchWarningPopup = null;
76 }
77 }
79 ApplicationsFilterModel {
80 id: legacyAppsModel
81 applicationsModel: ApplicationManager
82 filterTouchApps: true
83 }
85 GlobalShortcut { // reboot/shutdown dialog
86 shortcut: Qt.Key_PowerDown
87 active: Platform.isPC
88 onTriggered: root.unitySessionService.RequestShutdown()
89 }
91 GlobalShortcut { // reboot/shutdown dialog
92 shortcut: Qt.Key_PowerOff
93 active: Platform.isPC
94 onTriggered: root.unitySessionService.RequestShutdown()
95 }
97 GlobalShortcut { // sleep
98 shortcut: Qt.Key_Sleep
99 onTriggered: root.unitySessionService.Suspend()
100 }
102 GlobalShortcut { // hibernate
103 shortcut: Qt.Key_Hibernate
104 onTriggered: root.unitySessionService.Hibernate()
105 }
107 GlobalShortcut { // logout/lock dialog
108 shortcut: Qt.Key_LogOff
109 onTriggered: root.unitySessionService.RequestLogout()
110 }
112 GlobalShortcut { // logout/lock dialog
113 shortcut: Qt.ControlModifier|Qt.AltModifier|Qt.Key_Delete
114 onTriggered: root.unitySessionService.RequestLogout()
115 }
117 GlobalShortcut { // lock screen
118 shortcut: Qt.Key_ScreenSaver
119 onTriggered: LightDMService.greeter.showGreeter()
120 }
122 GlobalShortcut { // lock screen
123 shortcut: Qt.ControlModifier|Qt.AltModifier|Qt.Key_L
124 onTriggered: LightDMService.greeter.showGreeter()
125 }
127 GlobalShortcut { // lock screen
128 shortcut: Qt.MetaModifier|Qt.Key_L
129 onTriggered: LightDMService.greeter.showGreeter()
130 }
132 QtObject {
133 id: d // private stuff
134 objectName: "dialogsPrivate"
136 property var modeSwitchWarningPopup: null
138 function showPowerDialog() {
139 if (!dialogLoader.active) {
140 dialogLoader.sourceComponent = powerDialogComponent;
141 dialogLoader.focus = true;
142 dialogLoader.active = true;
143 }
144 }
145 }
147 Loader {
148 id: dialogLoader
149 objectName: "dialogLoader"
150 anchors.fill: parent
151 active: false
152 onActiveChanged: {
153 if (!active) {
154 if (previousFocusedItem) {
155 previousFocusedItem.forceActiveFocus(Qt.OtherFocusReason);
156 previousFocusedItem = undefined;
157 }
158 previousSourceComponent = undefined;
159 sourceComponent = undefined;
160 }
161 }
162 onSourceComponentChanged: {
163 if (previousSourceComponent !== sourceComponent) {
164 previousSourceComponent = sourceComponent;
165 previousFocusedItem = window.activeFocusItem;
166 }
167 }
169 property var previousSourceComponent: undefined
170 property var previousFocusedItem: undefined
171 }
173 Component {
174 id: logoutDialogComponent
175 ShellDialog {
176 id: logoutDialog
177 title: i18n.ctr("Title: Lock/Log out dialog", "Log out")
178 text: i18n.tr("Are you sure you want to log out?")
179 Button {
180 width: parent.width
181 text: i18n.ctr("Button: Lock the system", "Lock")
182 onClicked: {
183 LightDMService.greeter.showGreeter()
184 logoutDialog.hide();
185 }
186 Component.onCompleted: if (root.hasKeyboard) forceActiveFocus(Qt.TabFocusReason)
187 }
188 Button {
189 width: parent.width
190 focus: true
191 text: i18n.ctr("Button: Log out from the system", "Log Out")
192 onClicked: {
193 unitySessionService.logout();
194 logoutDialog.hide();
195 }
196 }
197 Button {
198 width: parent.width
199 text: i18n.tr("Cancel")
200 onClicked: {
201 logoutDialog.hide();
202 }
203 }
204 }
205 }
207 Component {
208 id: rebootDialogComponent
209 ShellDialog {
210 id: rebootDialog
211 title: i18n.ctr("Title: Reboot dialog", "Reboot")
212 text: i18n.tr("Are you sure you want to reboot?")
213 Button {
214 width: parent.width
215 text: i18n.tr("No")
216 onClicked: {
217 rebootDialog.hide();
218 }
219 }
220 Button {
221 width: parent.width
222 focus: true
223 text: i18n.tr("Yes")
224 onClicked: {
225 root.closeAllApps();
226 unitySessionService.reboot();
227 rebootDialog.hide();
228 }
229 color: theme.palette.normal.negative
230 Component.onCompleted: if (root.hasKeyboard) forceActiveFocus(Qt.TabFocusReason)
231 }
232 }
233 }
235 Component {
236 id: powerDialogComponent
237 ShellDialog {
238 id: powerDialog
239 title: i18n.ctr("Title: Power off/Restart dialog", "Power")
240 text: i18n.tr("Are you sure you would like\nto power off?")
241 Button {
242 width: parent.width
243 focus: true
244 text: i18n.ctr("Button: Power off the system", "Power off")
245 onClicked: {
246 root.closeAllApps();
247 powerDialog.hide();
248 root.powerOffClicked();
249 }
250 color: theme.palette.normal.negative
251 Component.onCompleted: if (root.hasKeyboard) forceActiveFocus(Qt.TabFocusReason)
252 }
253 Button {
254 width: parent.width
255 text: i18n.ctr("Button: Restart the system", "Restart")
256 onClicked: {
257 root.closeAllApps();
258 unitySessionService.reboot();
259 powerDialog.hide();
260 }
261 }
262 Button {
263 width: parent.width
264 text: i18n.tr("Cancel")
265 onClicked: {
266 powerDialog.hide();
267 }
268 }
269 }
270 }
272 Connections {
273 target: root.unitySessionService
275 onLogoutRequested: {
276 // Display a dialog to ask the user to confirm.
277 if (!dialogLoader.active) {
278 dialogLoader.sourceComponent = logoutDialogComponent;
279 dialogLoader.focus = true;
280 dialogLoader.active = true;
281 }
282 }
284 onShutdownRequested: {
285 // Display a dialog to ask the user to confirm.
286 showPowerDialog();
287 }
289 onRebootRequested: {
290 // Display a dialog to ask the user to confirm.
292 // display a combined reboot/shutdown dialog, sadly the session indicator calls rather the "Reboot()" method
293 // than shutdown when clicking on the "Shutdown..." menu item
294 // FIXME: when/if session indicator is fixed, put the rebootDialogComponent here
295 showPowerDialog();
296 }
298 onLogoutReady: {
299 root.closeAllApps();
300 Qt.quit();
301 unitySessionService.endSession();
302 }
303 }