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