on f45a574 help sign in
unity8/qml/Components/Dialogs.qml
227 lines 6.9 kB history raw
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 */
17import QtQuick 2.0
19import Unity.Application 0.1
20import Unity.Session 0.1
21import Ubuntu.Components 1.1
22import Ubuntu.Components.Popups 0.1
24Item {
25 id: root
27 // Explicitly use the right domain for this widget because it might be used
28 // in other applications like the welcome wizard.
29 readonly property string domain: "unity8"
31 function onPowerKeyPressed() {
32 // FIXME: event.isAutoRepeat is always false on Nexus 4.
33 // So we use powerKeyTimer.running to avoid the PowerOff key repeat
34 // https://launchpad.net/bugs/1349416
35 if (!powerKeyTimer.running) {
36 powerKeyTimer.restart();
37 }
38 }
40 function onPowerKeyReleased() {
41 powerKeyTimer.stop();
42 }
44 signal powerOffClicked();
46 QtObject {
47 id: d // private stuff
49 property bool dialogShown: false
51 function showPowerDialog() {
52 if (!d.dialogShown) {
53 d.dialogShown = true;
54 PopupUtils.open(powerDialog, root, {"z": root.z});
55 }
56 }
57 }
59 Timer {
60 id: powerKeyTimer
61 interval: 2000
62 repeat: false
63 triggeredOnStart: false
65 onTriggered: {
66 d.showPowerDialog();
67 }
68 }
70 Component {
71 id: logoutDialog
72 Dialog {
73 id: dialogueLogout
74 title: i18n.dtr(root.domain, "Log out")
75 text: i18n.dtr(root.domain, "Are you sure you want to log out?")
76 Button {
77 text: i18n.dtr(root.domain, "No")
78 onClicked: {
79 PopupUtils.close(dialogueLogout);
80 d.dialogShown = false;
81 }
82 }
83 Button {
84 text: i18n.dtr(root.domain, "Yes")
85 onClicked: {
86 DBusUnitySessionService.Logout();
87 PopupUtils.close(dialogueLogout);
88 d.dialogShown = false;
89 }
90 }
91 }
92 }
94 Component {
95 id: shutdownDialog
96 Dialog {
97 id: dialogueShutdown
98 title: i18n.dtr(root.domain, "Shut down")
99 text: i18n.dtr(root.domain, "Are you sure you want to shut down?")
100 Button {
101 text: i18n.dtr(root.domain, "No")
102 onClicked: {
103 PopupUtils.close(dialogueShutdown);
104 d.dialogShown = false;
105 }
106 }
107 Button {
108 text: i18n.dtr(root.domain, "Yes")
109 onClicked: {
110 dBusUnitySessionServiceConnection.closeAllApps();
111 DBusUnitySessionService.Shutdown();
112 PopupUtils.close(dialogueShutdown);
113 d.dialogShown = false;
114 }
115 }
116 }
117 }
119 Component {
120 id: rebootDialog
121 Dialog {
122 id: dialogueReboot
123 title: i18n.dtr(root.domain, "Reboot")
124 text: i18n.dtr(root.domain, "Are you sure you want to reboot?")
125 Button {
126 text: i18n.dtr(root.domain, "No")
127 onClicked: {
128 PopupUtils.close(dialogueReboot)
129 d.dialogShown = false;
130 }
131 }
132 Button {
133 text: i18n.dtr(root.domain, "Yes")
134 onClicked: {
135 dBusUnitySessionServiceConnection.closeAllApps();
136 DBusUnitySessionService.Reboot();
137 PopupUtils.close(dialogueReboot);
138 d.dialogShown = false;
139 }
140 }
141 }
142 }
144 Component {
145 id: powerDialog
146 Dialog {
147 id: dialoguePower
148 title: i18n.dtr(root.domain, "Power")
149 text: i18n.dtr(root.domain, "Are you sure you would like\nto power off?")
150 Button {
151 text: i18n.dtr(root.domain, "Power off")
152 onClicked: {
153 dBusUnitySessionServiceConnection.closeAllApps();
154 PopupUtils.close(dialoguePower);
155 d.dialogShown = false;
156 root.powerOffClicked();
157 }
158 color: UbuntuColors.red
159 }
160 Button {
161 text: i18n.dtr(root.domain, "Restart")
162 onClicked: {
163 dBusUnitySessionServiceConnection.closeAllApps();
164 DBusUnitySessionService.Reboot();
165 PopupUtils.close(dialoguePower);
166 d.dialogShown = false;
167 }
168 color: UbuntuColors.green
169 }
170 Button {
171 text: i18n.dtr(root.domain, "Cancel")
172 onClicked: {
173 PopupUtils.close(dialoguePower);
174 d.dialogShown = false;
175 }
176 color: UbuntuColors.coolGrey
177 }
178 }
179 }
182 Connections {
183 id: dBusUnitySessionServiceConnection
184 objectName: "dBusUnitySessionServiceConnection"
185 target: DBusUnitySessionService
187 function closeAllApps() {
188 while (true) {
189 var app = ApplicationManager.get(0);
190 if (app === null) {
191 break;
192 }
193 ApplicationManager.stopApplication(app.appId);
194 }
195 }
197 onLogoutRequested: {
198 // Display a dialog to ask the user to confirm.
199 if (!d.dialogShown) {
200 d.dialogShown = true;
201 PopupUtils.open(logoutDialog, root, {"z": root.z});
202 }
203 }
205 onShutdownRequested: {
206 // Display a dialog to ask the user to confirm.
207 if (!d.dialogShown) {
208 d.dialogShown = true;
209 PopupUtils.open(shutdownDialog, root, {"z": root.z});
210 }
211 }
213 onRebootRequested: {
214 // Display a dialog to ask the user to confirm.
215 if (!d.dialogShown) {
216 d.dialogShown = true;
217 PopupUtils.open(rebootDialog, root, {"z": root.z});
218 }
219 }
221 onLogoutReady: {
222 closeAllApps();
223 Qt.quit();
224 }
225 }