network: Don't show SSID multiple times, and make sure it's sorted

This commit is contained in:
Ian Douglas Scott 2022-06-28 14:10:07 -07:00
parent 14101dc7db
commit 3782df1e96
3 changed files with 22 additions and 14 deletions

View file

@ -7,10 +7,10 @@ pub mod task;
pub mod ui;
pub mod widgets;
use gtk4::{glib, gio::ApplicationFlags, prelude::*, Orientation, Separator};
use cosmic_panel_config::config::{CosmicPanelConfig, XdgWrapperConfig};
use gtk4::{gio::ApplicationFlags, glib, prelude::*, Orientation, Separator};
use once_cell::sync::Lazy;
use tokio::runtime::Runtime;
use cosmic_panel_config::config::{CosmicPanelConfig, XdgWrapperConfig};
static RT: Lazy<Runtime> = Lazy::new(|| Runtime::new().expect("failed to build tokio runtime"));

View file

@ -12,7 +12,11 @@ use gtk4::{
Image, ListBox, ListBoxRow, Separator,
};
use libcosmic_widgets::{relm4::RelmContainerExt, LabeledItem};
use std::{cell::RefCell, rc::Rc};
use std::{
cell::RefCell,
collections::{BTreeMap, HashMap},
rc::Rc,
};
use zbus::Connection;
pub fn add_available_wifi(target: &gtk4::Box, separator: Separator) {
@ -23,7 +27,7 @@ pub fn add_available_wifi(target: &gtk4::Box, separator: Separator) {
eprintln!("scan_for_wifi failed: {}", err);
}
});
let scrolled_window = gtk4::ScrolledWindow::new();
scrolled_window.set_hscrollbar_policy(gtk4::PolicyType::Never);
scrolled_window.set_vscrollbar_policy(gtk4::PolicyType::Automatic);
@ -90,9 +94,7 @@ async fn handle_wireless_device(
device: WirelessDevice<'_>,
tx: Sender<Vec<AccessPoint>>,
) -> zbus::Result<()> {
device
.request_scan(std::collections::HashMap::new())
.await?;
device.request_scan(HashMap::new()).await?;
let mut scan_changed = device.receive_last_scan_changed().await;
if let Some(t) = scan_changed.next().await {
if let Ok(-1) = t.get().await {
@ -101,13 +103,19 @@ async fn handle_wireless_device(
}
}
let access_points = device.get_access_points().await?;
let mut aps = Vec::with_capacity(access_points.len());
// Sort by SSID and remove duplicates
let mut aps = BTreeMap::<String, AccessPoint>::new();
for ap in access_points {
aps.push(AccessPoint {
ssid: String::from_utf8_lossy(&ap.ssid().await?.clone()).into_owned(),
strength: ap.strength().await?,
});
let ssid = String::from_utf8_lossy(&ap.ssid().await?.clone()).into_owned();
let strength = ap.strength().await?;
if let Some(access_point) = aps.get(&ssid) {
if access_point.strength > strength {
continue;
}
}
aps.insert(ssid.clone(), AccessPoint { ssid, strength });
}
let aps = aps.into_iter().map(|(_, x)| x).collect();
tx.send(aps).expect("failed to send back to main thread");
Ok(())
}

View file

@ -53,7 +53,7 @@ fn display_active_connections(
} => {
icon_image.set_icon_name(Some("network-wired-symbolic"));
render_wired_connection(name, speed, ip_addresses)
},
}
ActiveConnectionInfo::WiFi {
name,
hw_address,
@ -64,7 +64,7 @@ fn display_active_connections(
ActiveConnectionInfo::Vpn { name, ip_addresses } => {
icon_image.set_icon_name(Some("network-vpn-symbolic"));
render_vpn(name, ip_addresses)
},
}
};
let entry = ListBoxRow::builder().child(&entry).build();
target.append(&entry);