From 5fb037b003571729e84247a873c6d0c7adfc8086 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danielle=20For=C3=A9?= Date: Tue, 10 Dec 2024 12:48:09 -0800 Subject: [PATCH] Revert "WifiPage: update listbox with list model (#419)" This reverts commit 3fac115aedb480e09eb89d48620b22b3312ba822. --- src/Views/WifiPage.vala | 98 +++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 43 deletions(-) diff --git a/src/Views/WifiPage.vala b/src/Views/WifiPage.vala index aeaa54a3..1c2c5e44 100644 --- a/src/Views/WifiPage.vala +++ b/src/Views/WifiPage.vala @@ -9,7 +9,6 @@ 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; } @@ -38,8 +37,6 @@ 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 }; @@ -49,7 +46,7 @@ public class Network.WifiInterface : Network.Widgets.Page { selection_mode = SINGLE, visible = true }; - wifi_list.bind_model (ap_list_store, create_widget_func); + wifi_list.set_sort_func (sort_func); wifi_list.set_placeholder (placeholder); wifi_list.add_css_class (Granite.STYLE_CLASS_RICH_LIST); @@ -160,49 +157,35 @@ public class Network.WifiInterface : Network.Widgets.Page { } } - private void access_point_added_cb (Object object) { - var ap = (NM.AccessPoint) object; + void access_point_added_cb (Object ap_) { + NM.AccessPoint ap = (NM.AccessPoint)ap_; - // Don't show connected AP in list - if (ap == wifi_device.get_active_access_point ()) { - return; - } - - // Don't add duplicates - uint pos; - if (ap_list_store.find (ap, out pos) != false) { - return; - } + bool found = false; - // Sometimes network manager sends a (fake?) AP without a valid ssid - if (ap.ssid == null) { - 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 (); + } } - ap_list_store.insert_sorted (ap, sort_func); - update (); - } + /* Sometimes network manager sends a (fake?) AP without a valid ssid. */ + if (!found && ap.ssid != null) { + var item = new WifiMenuItem (ap); + item.user_action.connect (wifi_activate_cb); - private void access_point_removed_cb (Object object) { - var ap = (NM.AccessPoint) object; + wifi_list.append (item); - uint pos; - if (ap_list_store.find (ap, out pos) == false) { - critical ("Couldn't remove an access point which has not been added."); - return; + update (); } - - 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 () { @@ -246,6 +229,34 @@ 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 (hidden_btn != null) { @@ -640,11 +651,12 @@ public class Network.WifiInterface : Network.Widgets.Page { } } - private int sort_func (Object object1, Object object2) { - if (object1 == null || object1 == null) { + private int sort_func (Gtk.ListBoxRow r1, Gtk.ListBoxRow r2) { + if (r1 == null || r2 == null) { return 0; } - return ((NM.AccessPoint) object2).strength - ((NM.AccessPoint) object1).strength; + return ((WifiMenuItem) r2).ap.strength - ((WifiMenuItem) r1).ap.strength; } + }