Skip to content

Commit

Permalink
WifiPage: update listbox with list model
Browse files Browse the repository at this point in the history
  • Loading branch information
danirabbit committed Dec 10, 2024
1 parent 2ee8bac commit b792489
Showing 1 changed file with 43 additions and 57 deletions.
100 changes: 43 additions & 57 deletions src/Views/WifiPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Network.WifiInterface : Network.Widgets.Page {
public NM.DeviceWifi? wifi_device;
private NM.AccessPoint? active_ap;

private ListStore ap_list_store;
private Gtk.ListBox wifi_list;

private WifiMenuItem? active_wifi_item { get; set; }
Expand Down Expand Up @@ -39,6 +40,8 @@ public class Network.WifiInterface : Network.Widgets.Page {
construct {
icon = new ThemedIcon ("network-wireless");

ap_list_store = new ListStore (typeof (NM.AccessPoint));

placeholder = new Gtk.Stack () {
visible = true
};
Expand All @@ -48,7 +51,7 @@ public class Network.WifiInterface : Network.Widgets.Page {
selection_mode = SINGLE,
visible = true
};
wifi_list.set_sort_func (sort_func);
wifi_list.bind_model (ap_list_store, create_widget_func);
wifi_list.set_placeholder (placeholder);
wifi_list.add_css_class (Granite.STYLE_CLASS_RICH_LIST);

Expand Down Expand Up @@ -159,37 +162,49 @@ public class Network.WifiInterface : Network.Widgets.Page {
}
}

void access_point_added_cb (Object ap_) {
NM.AccessPoint ap = (NM.AccessPoint)ap_;
private void access_point_added_cb (Object object) {
var ap = (NM.AccessPoint) object;

bool found = false;
// Don't show connected AP in list
if (ap == wifi_device.get_active_access_point ()) {
return;
}

if (ap.ssid != null) {
unowned var child = wifi_list.get_first_child ();
while (child != null) {
if (child is WifiMenuItem) {
var menu_item = (WifiMenuItem) child;
if (ap.ssid.compare (menu_item.ssid) == 0) {
found = true;
menu_item.add_ap (ap);
break;
}
}
child = child.get_next_sibling ();
}
// Don't add duplicates
uint pos;
if (ap_list_store.find (ap, out pos) != false) {
return;
}

/* Sometimes network manager sends a (fake?) AP without a valid ssid. */
if (!found && ap.ssid != null) {
var item = new WifiMenuItem (ap) {
visible = true
};
item.user_action.connect (wifi_activate_cb);
// Sometimes network manager sends a (fake?) AP without a valid ssid
if (ap.ssid == null) {
return;
}

wifi_list.append (item);
ap_list_store.insert_sorted (ap, sort_func);
update ();
}

private void access_point_removed_cb (Object object) {
var ap = (NM.AccessPoint) object;

update ();
uint pos;
if (ap_list_store.find (ap, out pos) == false) {
critical ("Couldn't remove an access point which has not been added.");
return;
}

ap_list_store.remove (pos);
update ();
}

private Gtk.Widget create_widget_func (Object object) {
var ap = (NM.AccessPoint) object;

var row = new WifiMenuItem (ap);
row.user_action.connect (wifi_activate_cb);

return row;
}

void update_active_ap () {
Expand Down Expand Up @@ -233,34 +248,6 @@ public class Network.WifiInterface : Network.Widgets.Page {
}
}

void access_point_removed_cb (Object ap_) {
NM.AccessPoint ap = (NM.AccessPoint)ap_;

WifiMenuItem found_item = null;
unowned var child = wifi_list.get_first_child ();
while (child != null && found_item == null) {
if (child is WifiMenuItem) {
var menu_item = (WifiMenuItem) child;

if (ap.ssid.compare (menu_item.ssid) == 0) {
found_item = menu_item;
}
}

child = child.get_next_sibling ();
}

if (found_item == null) {
critical ("Couldn't remove an access point which has not been added.");
} else {
if (!found_item.remove_ap (ap)) {
found_item.destroy ();
}
}

update ();
}

public override void update () {
bool sensitive = (device.get_state () == NM.DeviceState.ACTIVATED);
if (disconnect_btn != null) {
Expand Down Expand Up @@ -687,12 +674,11 @@ public class Network.WifiInterface : Network.Widgets.Page {
}
}

private int sort_func (Gtk.ListBoxRow r1, Gtk.ListBoxRow r2) {
if (r1 == null || r2 == null) {
private int sort_func (Object object1, Object object2) {
if (object1 == null || object1 == null) {
return 0;
}

return ((WifiMenuItem) r2).ap.strength - ((WifiMenuItem) r1).ap.strength;
return ((NM.AccessPoint) object2).strength - ((NM.AccessPoint) object1).strength;
}

}

0 comments on commit b792489

Please sign in to comment.