cosmic-applets/applets/cosmic-applet-network/src/ui/current_networks.rs

154 lines
5.5 KiB
Rust
Raw Normal View History

2022-02-02 12:31:30 -05:00
// SPDX-License-Identifier: LGPL-3.0-or-later
2022-02-04 13:44:37 -05:00
use cosmic_dbus_networkmanager::{
access_point::AccessPoint,
device::{wired::WiredDevice, SpecificDevice},
interface::enums::{ApFlags, ApSecurityFlags},
nm::NetworkManager,
};
use gtk4::{
glib::{self, clone, source::PRIORITY_DEFAULT, MainContext, Sender},
prelude::*,
Image, ListBox, ListBoxRow, Orientation,
2022-02-04 13:44:37 -05:00
};
2022-02-09 13:22:43 -05:00
use std::{cell::RefCell, net::IpAddr, rc::Rc};
2022-02-04 13:44:37 -05:00
use zbus::Connection;
2022-02-02 12:31:30 -05:00
2022-02-07 11:13:34 -05:00
pub fn add_current_networks(target: &gtk4::Box) {
let networks_list = ListBox::new();
let entries = Rc::<RefCell<Vec<ListBoxRow>>>::default();
let (tx, rx) = MainContext::channel::<Vec<ActiveConnectionInfo>>(PRIORITY_DEFAULT);
crate::task::spawn(handle_devices(tx));
2022-02-07 12:51:16 -05:00
rx.attach(
None,
clone!(@weak networks_list, @strong entries => @default-return Continue(true), move |connections| {
2022-02-07 12:51:16 -05:00
let mut entries = entries.borrow_mut();
display_active_connections(connections, &networks_list, &mut *entries);
2022-02-07 12:51:16 -05:00
Continue(true)
}),
);
target.append(&networks_list);
2022-02-07 11:13:34 -05:00
}
2022-02-04 13:44:37 -05:00
2022-02-07 12:51:16 -05:00
fn display_active_connections(
connections: Vec<ActiveConnectionInfo>,
target: &ListBox,
entries: &mut Vec<ListBoxRow>,
2022-02-07 12:51:16 -05:00
) {
for old_entry in entries.drain(..) {
target.remove(&old_entry);
}
for connection in connections {
let entry = match connection {
ActiveConnectionInfo::Wired {
name,
hw_address,
speed,
2022-02-09 13:22:43 -05:00
ip_addresses,
} => render_wired_connection(name, speed, ip_addresses),
2022-02-07 12:51:16 -05:00
ActiveConnectionInfo::WiFi {
name,
hw_address,
flags,
rsn_flags,
wpa_flags,
} => todo!(),
};
let entry = ListBoxRow::builder().child(&entry).build();
2022-02-07 12:51:16 -05:00
target.append(&entry);
entries.push(entry);
}
}
2022-02-09 13:22:43 -05:00
fn render_wired_connection(name: String, speed: u32, ip_addresses: Vec<IpAddr>) -> gtk4::Box {
2022-02-07 12:51:16 -05:00
view! {
entry = gtk4::Box {
set_orientation: Orientation::Horizontal,
set_spacing: 8,
append: wired_icon = &Image {
set_icon_name: Some("network-wired-symbolic"),
},
append: wired_label_box = &gtk4::Box {
set_orientation: Orientation::Vertical,
append: wired_label = &gtk4::Label {
set_label: &name,
}
},
append: wired_speed = &gtk4::Label {
set_label: &format!("Connected - {} Mbps", speed),
set_valign: gtk4::Align::Center,
},
}
}
2022-02-09 13:22:43 -05:00
for address in ip_addresses {
view! {
wired_ip = gtk4::Label {
set_label: &format!("IP Address: {}", address),
set_halign: gtk4::Align::Start,
}
}
wired_label_box.append(&wired_ip);
}
2022-02-07 12:51:16 -05:00
entry
}
async fn handle_devices(tx: Sender<Vec<ActiveConnectionInfo>>) -> zbus::Result<()> {
2022-02-04 13:44:37 -05:00
let conn = Connection::system().await?;
let network_manager = NetworkManager::new(&conn).await?;
loop {
2022-02-07 11:13:34 -05:00
let active_connections = network_manager.active_connections().await?;
let mut info = Vec::<ActiveConnectionInfo>::with_capacity(active_connections.len());
2022-02-07 11:13:34 -05:00
for connection in active_connections {
for device in connection.devices().await? {
match device.downcast_to_device().await? {
Some(SpecificDevice::Wired(wired_device)) => {
2022-02-09 13:22:43 -05:00
let mut ip_addresses = Vec::new();
for address_data in device.ip4_config().await?.address_data().await? {
ip_addresses.push(IpAddr::V4(address_data.address));
}
for address_data in device.ip6_config().await?.address_data().await? {
ip_addresses.push(IpAddr::V6(address_data.address));
}
info.push(ActiveConnectionInfo::Wired {
name: connection.id().await?,
hw_address: wired_device.hw_address().await?,
speed: wired_device.speed().await?,
2022-02-09 13:22:43 -05:00
ip_addresses,
});
}
Some(SpecificDevice::Wireless(wireless_device)) => {
let access_point = wireless_device.active_access_point().await?;
info.push(ActiveConnectionInfo::WiFi {
name: String::from_utf8_lossy(&access_point.ssid().await?).into_owned(),
hw_address: wireless_device.hw_address().await?,
flags: access_point.flags().await?,
rsn_flags: access_point.rsn_flags().await?,
wpa_flags: access_point.wpa_flags().await?,
});
}
_ => {}
}
}
2022-02-07 11:13:34 -05:00
}
2022-02-07 12:51:16 -05:00
tx.send(info)
.expect("failed to send active connections back to main thread");
2022-02-04 13:44:37 -05:00
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
}
}
enum ActiveConnectionInfo {
Wired {
name: String,
hw_address: String,
speed: u32,
2022-02-09 13:22:43 -05:00
ip_addresses: Vec<IpAddr>,
},
WiFi {
name: String,
hw_address: String,
flags: ApFlags,
rsn_flags: ApSecurityFlags,
wpa_flags: ApSecurityFlags,
},
}