Skip to content

Commit 3fac115

Browse files
authored
WifiPage: update listbox with list model (#419)
1 parent 2a67237 commit 3fac115

File tree

1 file changed

+43
-55
lines changed

1 file changed

+43
-55
lines changed

src/Views/WifiPage.vala

Lines changed: 43 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class Network.WifiInterface : Network.Widgets.Page {
99
public NM.DeviceWifi? wifi_device;
1010
private NM.AccessPoint? active_ap;
1111

12+
private ListStore ap_list_store;
1213
private Gtk.ListBox wifi_list;
1314

1415
private WifiMenuItem? active_wifi_item { get; set; }
@@ -37,6 +38,8 @@ public class Network.WifiInterface : Network.Widgets.Page {
3738
construct {
3839
icon = new ThemedIcon ("network-wireless");
3940

41+
ap_list_store = new ListStore (typeof (NM.AccessPoint));
42+
4043
placeholder = new Gtk.Stack () {
4144
visible = true
4245
};
@@ -46,7 +49,7 @@ public class Network.WifiInterface : Network.Widgets.Page {
4649
selection_mode = SINGLE,
4750
visible = true
4851
};
49-
wifi_list.set_sort_func (sort_func);
52+
wifi_list.bind_model (ap_list_store, create_widget_func);
5053
wifi_list.set_placeholder (placeholder);
5154
wifi_list.add_css_class (Granite.STYLE_CLASS_RICH_LIST);
5255

@@ -157,35 +160,49 @@ public class Network.WifiInterface : Network.Widgets.Page {
157160
}
158161
}
159162

160-
void access_point_added_cb (Object ap_) {
161-
NM.AccessPoint ap = (NM.AccessPoint)ap_;
163+
private void access_point_added_cb (Object object) {
164+
var ap = (NM.AccessPoint) object;
162165

163-
bool found = false;
166+
// Don't show connected AP in list
167+
if (ap == wifi_device.get_active_access_point ()) {
168+
return;
169+
}
164170

165-
if (ap.ssid != null) {
166-
unowned var child = wifi_list.get_first_child ();
167-
while (child != null) {
168-
if (child is WifiMenuItem) {
169-
var menu_item = (WifiMenuItem) child;
170-
if (ap.ssid.compare (menu_item.ssid) == 0) {
171-
found = true;
172-
menu_item.add_ap (ap);
173-
break;
174-
}
175-
}
176-
child = child.get_next_sibling ();
177-
}
171+
// Don't add duplicates
172+
uint pos;
173+
if (ap_list_store.find (ap, out pos) != false) {
174+
return;
178175
}
179176

180-
/* Sometimes network manager sends a (fake?) AP without a valid ssid. */
181-
if (!found && ap.ssid != null) {
182-
var item = new WifiMenuItem (ap);
183-
item.user_action.connect (wifi_activate_cb);
177+
// Sometimes network manager sends a (fake?) AP without a valid ssid
178+
if (ap.ssid == null) {
179+
return;
180+
}
184181

185-
wifi_list.append (item);
182+
ap_list_store.insert_sorted (ap, sort_func);
183+
update ();
184+
}
185+
186+
private void access_point_removed_cb (Object object) {
187+
var ap = (NM.AccessPoint) object;
186188

187-
update ();
189+
uint pos;
190+
if (ap_list_store.find (ap, out pos) == false) {
191+
critical ("Couldn't remove an access point which has not been added.");
192+
return;
188193
}
194+
195+
ap_list_store.remove (pos);
196+
update ();
197+
}
198+
199+
private Gtk.Widget create_widget_func (Object object) {
200+
var ap = (NM.AccessPoint) object;
201+
202+
var row = new WifiMenuItem (ap);
203+
row.user_action.connect (wifi_activate_cb);
204+
205+
return row;
189206
}
190207

191208
void update_active_ap () {
@@ -229,34 +246,6 @@ public class Network.WifiInterface : Network.Widgets.Page {
229246
}
230247
}
231248

232-
void access_point_removed_cb (Object ap_) {
233-
NM.AccessPoint ap = (NM.AccessPoint)ap_;
234-
235-
WifiMenuItem found_item = null;
236-
unowned var child = wifi_list.get_first_child ();
237-
while (child != null && found_item == null) {
238-
if (child is WifiMenuItem) {
239-
var menu_item = (WifiMenuItem) child;
240-
241-
if (ap.ssid.compare (menu_item.ssid) == 0) {
242-
found_item = menu_item;
243-
}
244-
}
245-
246-
child = child.get_next_sibling ();
247-
}
248-
249-
if (found_item == null) {
250-
critical ("Couldn't remove an access point which has not been added.");
251-
} else {
252-
if (!found_item.remove_ap (ap)) {
253-
found_item.destroy ();
254-
}
255-
}
256-
257-
update ();
258-
}
259-
260249
public override void update () {
261250
bool sensitive = (device.get_state () == NM.DeviceState.ACTIVATED);
262251
if (hidden_btn != null) {
@@ -651,12 +640,11 @@ public class Network.WifiInterface : Network.Widgets.Page {
651640
}
652641
}
653642

654-
private int sort_func (Gtk.ListBoxRow r1, Gtk.ListBoxRow r2) {
655-
if (r1 == null || r2 == null) {
643+
private int sort_func (Object object1, Object object2) {
644+
if (object1 == null || object1 == null) {
656645
return 0;
657646
}
658647

659-
return ((WifiMenuItem) r2).ap.strength - ((WifiMenuItem) r1).ap.strength;
648+
return ((NM.AccessPoint) object2).strength - ((NM.AccessPoint) object1).strength;
660649
}
661-
662650
}

0 commit comments

Comments
 (0)