Unity 8
30-wifi.qml
1 /*
2  * Copyright (C) 2013-2016 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.4
18 import QtQuick.Layouts 1.1
19 import QMenuModel 0.1 as QMenuModel
20 import Ubuntu.Components 1.3
21 import Wizard 0.1
22 import Ubuntu.Connectivity 1.0
23 import Ubuntu.SystemImage 0.1
24 import ".." as LocalComponents
25 import "../../Components"
26 
27 LocalComponents.Page {
28  id: wifiPage
29  objectName: "wifiPage"
30 
31  title: i18n.tr("Connect to Wi‑Fi")
32  forwardButtonSourceComponent: forwardButton
33 
34  readonly property bool connected: Connectivity.online
35 
36  function getExtendedProperty(object, propertyName, defaultValue) {
37  if (object && object.hasOwnProperty(propertyName)) {
38  return object[propertyName];
39  }
40  return defaultValue;
41  }
42 
43  function getAPIcon(adHoc, signalStrength, secure) {
44  var imageName = "nm-no-connection";
45 
46  if (adHoc) {
47  imageName = "nm-adhoc";
48  } else if (signalStrength == 0) {
49  imageName = "nm-signal-00";
50  } else if (signalStrength <= 25) {
51  imageName = "nm-signal-25";
52  } else if (signalStrength <= 50) {
53  imageName = "nm-signal-50";
54  } else if (signalStrength <= 75) {
55  imageName = "nm-signal-75";
56  } else if (signalStrength <= 100) {
57  imageName = "nm-signal-100";
58  }
59 
60  if (secure) {
61  imageName += "-secure";
62  }
63  return imageName;
64  }
65 
66  QMenuModel.UnityMenuModel {
67  id: menuModel
68  busName: "com.canonical.indicator.network"
69  actions: { "indicator": "/com/canonical/indicator/network" }
70  menuObjectPath: "/com/canonical/indicator/network/phone_wifi_settings"
71  }
72 
73  Component {
74  id: accessPointComponent
75  ListItem {
76  id: accessPoint
77  objectName: "accessPoint_" + apName.text.toLowerCase().replace(/\s+/g, '_')
78  highlightColor: backgroundColor
79  enabled: menuData && menuData.sensitive || false
80  divider.colorFrom: dividerColor
81  divider.colorTo: backgroundColor
82 
83  property QtObject menuData: null
84  property var unityMenuModel: menuModel
85  property var extendedData: menuData && menuData.ext || undefined
86  property var strengthAction: QMenuModel.UnityMenuAction {
87  model: unityMenuModel
88  index: menuIndex
89  name: getExtendedProperty(extendedData, "xCanonicalWifiApStrengthAction", "")
90  }
91  readonly property bool secure: getExtendedProperty(extendedData, "xCanonicalWifiApIsSecure", false)
92  readonly property bool adHoc: getExtendedProperty(extendedData, "xCanonicalWifiApIsAdhoc", false)
93  readonly property bool isConnected: menuData && menuData.actionState
94  readonly property bool isEnterprise: getExtendedProperty(extendedData, "xCanonicalWifiApIsEnterprise", false)
95  readonly property int signalStrength: strengthAction.valid ? strengthAction.state : 0
96  property int menuIndex: -1
97 
98  function loadAttributes() {
99  if (!unityMenuModel || menuIndex == -1) return;
100  unityMenuModel.loadExtendedAttributes(menuIndex, {'x-canonical-wifi-ap-is-adhoc': 'bool',
101  'x-canonical-wifi-ap-is-secure': 'bool',
102  'x-canonical-wifi-ap-is-enterprise': 'bool',
103  'x-canonical-wifi-ap-strength-action': 'string'});
104  }
105 
106  Icon {
107  id: apIcon
108  anchors {
109  left: parent.left
110  verticalCenter: parent.verticalCenter
111  leftMargin: column.anchors.leftMargin == 0 ? staticMargin : 0
112  }
113  height: units.gu(2.5)
114  width: height
115  name: getAPIcon(accessPoint.adHoc, accessPoint.signalStrength, accessPoint.secure)
116  color: textColor
117  }
118 
119  Column {
120  anchors.verticalCenter: parent.verticalCenter
121  anchors.left: apIcon.right
122  anchors.leftMargin: units.gu(2)
123  Label {
124  id: apName
125  text: menuData && menuData.label || ""
126  font.weight: accessPoint.isConnected ? Font.Normal : Font.Light
127  fontSize: "medium"
128  color: textColor
129  }
130  Label {
131  id: connectedLabel
132  text: i18n.tr("Connected")
133  font.weight: Font.Light
134  fontSize: "small"
135  color: okColor
136  visible: accessPoint.isConnected
137  }
138  }
139 
140  onClicked: {
141  unityMenuModel.activate(menuIndex);
142  listview.positionViewAtBeginning();
143  }
144  }
145  }
146 
147  ColumnLayout {
148  id: column
149  spacing: units.gu(2)
150  anchors {
151  fill: content
152  topMargin: customMargin
153  leftMargin: wideMode ? parent.leftMargin : 0
154  rightMargin: wideMode ? parent.rightMargin : 0
155  }
156 
157  Label {
158  id: label
159  anchors.left: parent.left
160  anchors.right: parent.right
161  anchors.leftMargin: column.anchors.leftMargin == 0 ? staticMargin : 0
162  font.weight: Font.Light
163  color: textColor
164  wrapMode: Text.Wrap
165  text: listview.count > 0 ? i18n.tr("Available Wi-Fi networks")
166  : i18n.tr("No available Wi-Fi networks")
167  }
168 
169  ListView {
170  id: listview
171  objectName: "accessPointsListView"
172  anchors.left: parent.left
173  anchors.right: parent.right
174  clip: true
175  model: menuModel
176  Layout.fillHeight: true
177 
178  delegate: Loader {
179  readonly property bool isAccessPoint: model.type === "unity.widgets.systemsettings.tablet.accesspoint"
180  readonly property bool isConnected: item && item.menuData && item.menuData.actionState
181  readonly property bool isEnterprise: item && item.isEnterprise
182 
183  height: !!sourceComponent ? (isConnected ? units.gu(9) : units.gu(7)) : 0
184  anchors.left: parent.left
185  anchors.right: parent.right
186 
187  asynchronous: true
188  sourceComponent: {
189  if (isAccessPoint && !isEnterprise) {
190  return accessPointComponent;
191  }
192  return null;
193  }
194 
195  onLoaded: {
196  item.menuData = Qt.binding(function() { return model; });
197  item.menuIndex = Qt.binding(function() { return index; });
198  item.loadAttributes();
199  }
200  }
201  }
202  }
203 
204  Component {
205  id: forwardButton
206  LocalComponents.StackButton {
207  text: (connected || listview.count === 0) ? i18n.tr("Next") : i18n.tr("Skip")
208  onClicked: {
209  if (connected) {
210  SystemImage.checkForUpdate(); // initiate the background check for System Update
211  }
212  pageStack.next();
213  }
214  }
215  }
216 }