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