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