Unity 8
 All Classes Functions
Dialogs.qml
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  */
16 
17 import QtQuick 2.0
18 
19 import Unity.Application 0.1
20 import Unity.Session 0.1
21 import Ubuntu.Components 1.1
22 import Ubuntu.Components.Popups 0.1
23 
24 Item {
25  id: root
26 
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"
30 
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  }
39 
40  function onPowerKeyReleased() {
41  powerKeyTimer.stop();
42  }
43 
44  signal powerOffClicked();
45 
46  QtObject {
47  id: d // private stuff
48 
49  property bool dialogShown: false
50 
51  function showPowerDialog() {
52  if (!d.dialogShown) {
53  d.dialogShown = true;
54  PopupUtils.open(powerDialog, root, {"z": root.z});
55  }
56  }
57  }
58 
59  Timer {
60  id: powerKeyTimer
61  interval: 2000
62  repeat: false
63  triggeredOnStart: false
64 
65  onTriggered: {
66  d.showPowerDialog();
67  }
68  }
69 
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  }
93 
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  }
118 
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  }
143 
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  }
180 
181 
182  Connections {
183  id: dBusUnitySessionServiceConnection
184  objectName: "dBusUnitySessionServiceConnection"
185  target: DBusUnitySessionService
186 
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  }
196 
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  }
204 
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  }
212 
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  }
220 
221  onLogoutReady: {
222  closeAllApps();
223  Qt.quit();
224  }
225  }
226 
227 }