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