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