Unity 8
40-wifi.qml
1 /*
2  * Copyright (C) 2013 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 QMenuModel 0.1 as QMenuModel
19 import QtSystemInfo 5.0
20 import Ubuntu.Components 1.3
21 import Ubuntu.Components.ListItems 1.3 as ListItem
22 import Ubuntu.Settings.Menus 0.1 as Menus
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: networkInfo.accessPointName
33 
34  function getExtendedProperty(object, propertyName, defaultValue) {
35  if (object && object.hasOwnProperty(propertyName)) {
36  return object[propertyName];
37  }
38  return defaultValue;
39  }
40 
41  QMenuModel.UnityMenuModel {
42  id: menuModel
43  busName: "com.canonical.indicator.network"
44  actions: { "indicator": "/com/canonical/indicator/network" }
45  menuObjectPath: "/com/canonical/indicator/network/phone_wifi_settings"
46  }
47 
48  NetworkInfo {
49  id: networkInfo
50 
51  property string accessPointName
52 
53  monitorCurrentNetworkMode: true
54  monitorNetworkName: true
55  monitorNetworkStatus: true
56 
57  onCurrentNetworkModeChanged: getAccessPointName()
58  onNetworkNameChanged: getAccessPointName()
59  onNetworkStatusChanged: if (status !== NetworkInfo.HomeNetwork) accessPointName = ""
60 
61  Component.onCompleted: getAccessPointName()
62 
63  function getAccessPointName() {
64  // 0 is the interface
65  if (currentNetworkMode === NetworkInfo.WlanMode &&
66  networkStatus(NetworkInfo.WlanMode, 0) === NetworkInfo.HomeNetwork)
67  accessPointName = networkName(NetworkInfo.WlanMode, 0);
68  else
69  accessPointName = "";
70  }
71  }
72 
73  Component {
74  id: accessPointComponent
75  ListItem.Standard {
76  id: accessPoint
77  objectName: "accessPoint"
78 
79  property QtObject menuData: null
80  property var unityMenuModel: menuModel
81  property var extendedData: menuData && menuData.ext || undefined
82  property var strengthAction: QMenuModel.UnityMenuAction {
83  model: unityMenuModel
84  index: menuIndex
85  name: getExtendedProperty(extendedData, "xCanonicalWifiApStrengthAction", "")
86  }
87  property bool checked: menuData && menuData.isToggled || false
88  property bool secure: getExtendedProperty(extendedData, "xCanonicalWifiApIsSecure", false)
89  property bool adHoc: getExtendedProperty(extendedData, "xCanonicalWifiApIsAdhoc", false)
90  property int signalStrength: strengthAction.valid ? strengthAction.state : 0
91  property int menuIndex: -1
92 
93  function loadAttributes() {
94  if (!unityMenuModel || menuIndex == -1) return;
95  unityMenuModel.loadExtendedAttributes(menuIndex, {'x-canonical-wifi-ap-is-adhoc': 'bool',
96  'x-canonical-wifi-ap-is-secure': 'bool',
97  'x-canonical-wifi-ap-strength-action': 'string'});
98  }
99 
100  signal activate()
101 
102  text: menuData && menuData.label || ""
103  enabled: menuData && menuData.sensitive || false
104  iconName: {
105  var imageName = "nm-signal-100";
106 
107  if (adHoc) {
108  imageName = "nm-adhoc";
109  } else if (signalStrength == 0) {
110  imageName = "nm-signal-00";
111  } else if (signalStrength <= 25) {
112  imageName = "nm-signal-25";
113  } else if (signalStrength <= 50) {
114  imageName = "nm-signal-50";
115  } else if (signalStrength <= 75) {
116  imageName = "nm-signal-75";
117  }
118 
119  if (secure) {
120  imageName += "-secure";
121  }
122  return imageName;
123  }
124  iconFrame: false
125  control: CheckBox {
126  id: checkBoxActive
127 
128  onClicked: {
129  accessPoint.activate();
130  }
131  }
132  style: Rectangle {
133  color: "#4c000000"
134  }
135 
136  Component.onCompleted: {
137  loadAttributes();
138  }
139  onUnityMenuModelChanged: {
140  loadAttributes();
141  }
142  onMenuIndexChanged: {
143  loadAttributes();
144  }
145  onCheckedChanged: {
146  // Can't rely on binding. Checked is assigned on click.
147  checkBoxActive.checked = checked;
148  }
149  onActivate: unityMenuModel.activate(menuIndex);
150  }
151  }
152 
153  Column {
154  id: column
155  spacing: units.gu(2)
156  anchors.top: content.top
157  anchors.bottom: content.bottom
158  anchors.left: wifiPage.left
159  anchors.right: wifiPage.right
160 
161  Label {
162  id: label
163  anchors.left: parent.left
164  anchors.leftMargin: leftMargin
165  anchors.right: parent.right
166  anchors.rightMargin: rightMargin
167  fontSize: "small"
168  text: mainMenu.count > 0 ? i18n.tr("Available networks…")
169  : i18n.tr("No available networks.")
170  }
171 
172  Flickable {
173  anchors.left: parent.left
174  anchors.right: parent.right
175  height: column.height - label.height - column.spacing
176  contentHeight: contentItem.childrenRect.height
177  clip: true
178  flickDeceleration: 1500 * units.gridUnit / 8
179  maximumFlickVelocity: 2500 * units.gridUnit / 8
180  boundsBehavior: (contentHeight > height) ? Flickable.DragAndOvershootBounds : Flickable.StopAtBounds
181 
182  Column {
183  anchors.left: parent.left
184  anchors.right: parent.right
185 
186  Repeater {
187  id: mainMenu
188 
189  model: menuModel
190 
191  delegate: Loader {
192  id: loader
193 
194  readonly property bool isAccessPoint: model.type === "unity.widgets.systemsettings.tablet.accesspoint"
195 
196  anchors.left: parent.left
197  anchors.right: parent.right
198  height: isAccessPoint ? units.gu(6) : 0
199  asynchronous: true
200  sourceComponent: isAccessPoint ? accessPointComponent : null
201 
202  onLoaded: {
203  item.menuData = Qt.binding(function() { return model; });
204  item.menuIndex = Qt.binding(function() { return index; });
205  }
206  }
207  }
208  }
209  }
210  }
211 
212  Component {
213  id: forwardButton
214  LocalComponents.StackButton {
215  text: (connected || mainMenu.count === 0) ? i18n.tr("Continue") : i18n.tr("Skip")
216  onClicked: pageStack.next()
217  }
218  }
219 }