Iced network applet (#31)

* builds and shows basic applet

* feat: connect up subscriptions

* feat: network applet mostly working
This commit is contained in:
Ashley Wulber 2022-12-09 15:31:19 -05:00 committed by GitHub
parent 3be995a1d2
commit 9ef8098498
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 1277 additions and 1172 deletions

View file

@ -0,0 +1,344 @@
use cosmic::{
applet::CosmicAppletHelper,
iced::{
executor,
widget::{column, container, row, scrollable, text},
Alignment, Application, Color, Command, Length, Subscription,
},
iced_native::window,
iced_style::{application, svg},
theme::{Button, Svg},
widget::{button, horizontal_rule, icon, list_column, toggler},
Element, Theme,
};
use futures::channel::mpsc::UnboundedSender;
use iced_sctk::{
application::SurfaceIdWrapper,
commands::popup::{destroy_popup, get_popup},
};
use crate::{
config, fl,
network_manager::{
available_wifi::AccessPoint, current_networks::ActiveConnectionInfo,
network_manager_subscription, NetworkManagerEvent, NetworkManagerRequest,
},
};
pub fn run() -> cosmic::iced::Result {
let helper = CosmicAppletHelper::default();
CosmicNetworkApplet::run(helper.window_settings())
}
#[derive(Clone, Default)]
struct CosmicNetworkApplet {
icon_name: String,
theme: Theme,
popup: Option<window::Id>,
id_ctr: u32,
applet_helper: CosmicAppletHelper,
// STATE
airplane_mode: bool,
wifi: bool,
wireless_access_points: Vec<AccessPoint>,
active_conns: Vec<ActiveConnectionInfo>,
nm_sender: Option<UnboundedSender<NetworkManagerRequest>>,
}
impl CosmicNetworkApplet {
fn update_icon_name(&mut self) {
self.icon_name = self
.active_conns
.iter()
.fold("network-offline-symbolic", |icon_name, conn| {
match (icon_name, conn) {
("network-offline-symbolic", ActiveConnectionInfo::WiFi { .. }) => {
"network-wireless-symbolic"
}
(
"network-offline-symbolic",
ActiveConnectionInfo::Wired { .. },
)
| (
"network-wireless-symbolic",
ActiveConnectionInfo::Wired { .. },
) => "network-wired-symbolic",
(_, ActiveConnectionInfo::Vpn { .. }) => "network-vpn-symbolic",
_ => icon_name,
}
})
.to_string()
}
}
#[derive(Debug, Clone)]
enum Message {
TogglePopup,
ToggleAirplaneMode(bool),
ToggleWiFi(bool),
Errored(String),
Ignore,
NetworkManagerEvent(NetworkManagerEvent),
SelectWirelessAccessPoint(String),
}
impl Application for CosmicNetworkApplet {
type Message = Message;
type Theme = Theme;
type Executor = executor::Default;
type Flags = ();
fn new(_flags: ()) -> (Self, Command<Message>) {
(
CosmicNetworkApplet {
icon_name: "network-offline-symbolic".to_string(),
..Default::default()
},
Command::none(),
)
}
fn title(&self) -> String {
config::APP_ID.to_string()
}
fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::TogglePopup => {
if let Some(p) = self.popup.take() {
return destroy_popup(p);
} else {
// TODO request update of state maybe
self.id_ctr += 1;
let new_id = window::Id::new(self.id_ctr);
self.popup.replace(new_id);
let popup_settings = self.applet_helper.get_popup_settings(
window::Id::new(0),
new_id,
(420, 600),
None,
None,
);
return get_popup(popup_settings);
}
}
Message::Errored(_) => todo!(),
Message::Ignore => {}
Message::ToggleAirplaneMode(enabled) => {
self.airplane_mode = enabled;
// TODO apply changes
}
Message::ToggleWiFi(enabled) => {
self.wifi = enabled;
if let Some(tx) = self.nm_sender.as_mut() {
let _ = tx.unbounded_send(NetworkManagerRequest::SetWiFi(enabled));
}
}
Message::NetworkManagerEvent(event) => match event {
NetworkManagerEvent::Init {
sender,
wireless_access_points,
active_conns,
wifi_enabled,
airplane_mode,
} => {
self.nm_sender.replace(sender);
self.wireless_access_points = wireless_access_points;
self.active_conns = active_conns;
self.wifi = wifi_enabled;
self.airplane_mode = airplane_mode;
self.update_icon_name();
}
NetworkManagerEvent::WiFiEnabled(enabled) => {
self.wifi = enabled;
}
NetworkManagerEvent::WirelessAccessPoints(access_points) => {
self.wireless_access_points = access_points;
}
NetworkManagerEvent::ActiveConns(conns) => {
self.active_conns = conns;
self.update_icon_name();
}
NetworkManagerEvent::RequestResponse { wireless_access_points, active_conns, wifi_enabled, success, ..} => {
if success {
self.wireless_access_points = wireless_access_points;
self.active_conns = active_conns;
self.wifi = wifi_enabled;
self.update_icon_name();
}
},
},
Message::SelectWirelessAccessPoint(ssid) => {
if let Some(tx) = self.nm_sender.as_ref() {
let _ = tx.unbounded_send(NetworkManagerRequest::SelectAccessPoint(ssid));
}
}
}
Command::none()
}
fn view(&self, id: SurfaceIdWrapper) -> Element<Message> {
match id {
SurfaceIdWrapper::LayerSurface(_) => unimplemented!(),
SurfaceIdWrapper::Window(_) => self
.applet_helper
.icon_button(&self.icon_name)
.on_press(Message::TogglePopup)
.into(),
SurfaceIdWrapper::Popup(_) => {
let name = text(fl!("network")).size(18);
let icon = icon(&self.icon_name, 24)
.style(Svg::Custom(|theme| svg::Appearance {
fill: Some(theme.palette().text),
}))
.width(Length::Units(24))
.height(Length::Units(24));
let mut list_col = list_column();
for conn in &self.active_conns {
let el = match conn {
ActiveConnectionInfo::Vpn { name, ip_addresses } => {
let mut ipv4 = column![];
let mut ipv6 = column![];
for addr in ip_addresses {
match addr {
std::net::IpAddr::V4(a) => {
ipv4 = ipv4.push(text(format!(
"{}: {}",
fl!("ipv4"),
a.to_string()
)));
}
std::net::IpAddr::V6(a) => {
ipv6 = ipv6.push(text(format!(
"{}: {}",
fl!("ipv6"),
a.to_string()
)));
}
}
}
column![text(name), ipv4, ipv6].spacing(4)
}
ActiveConnectionInfo::Wired {
name,
hw_address,
speed,
ip_addresses,
} => {
let mut ipv4 = column![];
let mut ipv6 = column![];
for addr in ip_addresses {
match addr {
std::net::IpAddr::V4(a) => {
ipv4 = ipv4.push(text(format!(
"{}: {}",
fl!("ipv4"),
a.to_string()
)));
}
std::net::IpAddr::V6(a) => {
ipv6 = ipv6.push(text(format!(
"{}: {}",
fl!("ipv6"),
a.to_string()
)));
}
}
}
column![
row![
text(name),
text(format!("{speed} {}", fl!("megabits-per-second")))
]
.spacing(16),
ipv4,
ipv6,
text(format!("{}: {hw_address}", fl!("mac"))),
]
.spacing(4)
}
ActiveConnectionInfo::WiFi {
name, hw_address, ..
} => column![row![
text(name),
text(format!("{}: {hw_address}", fl!("mac")))
]
.spacing(12)]
.spacing(4),
};
list_col = list_col.add(el);
}
let mut content = column![
row![icon, name].spacing(8).width(Length::Fill),
list_col,
horizontal_rule(1),
container(
toggler(fl!("airplane-mode"), self.airplane_mode, |m| {
Message::ToggleAirplaneMode(m)
})
.width(Length::Fill)
)
.padding([0, 12]),
horizontal_rule(1),
container(
toggler(fl!("wifi"), self.wifi, |m| { Message::ToggleWiFi(m) })
.width(Length::Fill)
)
.padding([0, 12]),
]
.align_items(Alignment::Center)
.spacing(8)
.padding(8);
if self.wifi {
let mut list_col = list_column();
for ap in &self.wireless_access_points {
let button = self
.active_conns
.iter()
.find_map(|conn| match conn {
ActiveConnectionInfo::WiFi { name, .. } if name == &ap.ssid => {
Some(
button(Button::Primary)
.text(&ap.ssid)
.on_press(Message::Ignore)
.width(Length::Fill),
)
}
_ => None,
})
.unwrap_or_else(|| {
button(Button::Text)
.text(&ap.ssid)
.on_press(Message::SelectWirelessAccessPoint(ap.ssid.clone()))
.width(Length::Fill)
});
list_col = list_col.add(button);
}
content = content.push(scrollable(list_col).height(Length::Fill));
}
self.applet_helper.popup_container(content).into()
}
}
}
fn subscription(&self) -> Subscription<Message> {
network_manager_subscription(0).map(|(_, event)| Message::NetworkManagerEvent(event))
}
fn theme(&self) -> Theme {
self.theme
}
fn close_requested(&self, _id: iced_sctk::application::SurfaceIdWrapper) -> Self::Message {
Message::Ignore
}
fn style(&self) -> <Self::Theme as application::StyleSheet>::Style {
<Self::Theme as application::StyleSheet>::Style::Custom(|theme| application::Appearance {
background_color: Color::from_rgba(0.0, 0.0, 0.0, 0.0),
text_color: theme.cosmic().on_bg_color().into(),
})
}
}

View file

@ -0,0 +1,3 @@
pub const APP_ID: &str = "com.system76.CosmicAppletNetwork";
pub const PROFILE: &str = "";
pub const VERSION: &str = "0.1.0";

View file

@ -0,0 +1,47 @@
// SPDX-License-Identifier: MPL-2.0-only
use i18n_embed::{
fluent::{fluent_language_loader, FluentLanguageLoader},
DefaultLocalizer, LanguageLoader, Localizer,
};
use once_cell::sync::Lazy;
use rust_embed::RustEmbed;
#[derive(RustEmbed)]
#[folder = "i18n/"]
struct Localizations;
pub static LANGUAGE_LOADER: Lazy<FluentLanguageLoader> = Lazy::new(|| {
let loader: FluentLanguageLoader = fluent_language_loader!();
loader
.load_fallback_language(&Localizations)
.expect("Error while loading fallback language");
loader
});
#[macro_export]
macro_rules! fl {
($message_id:literal) => {{
i18n_embed_fl::fl!($crate::localize::LANGUAGE_LOADER, $message_id)
}};
($message_id:literal, $($args:expr),*) => {{
i18n_embed_fl::fl!($crate::localize::LANGUAGE_LOADER, $message_id, $($args), *)
}};
}
// Get the `Localizer` to be used for localizing this library.
pub fn localizer() -> Box<dyn Localizer> {
Box::from(DefaultLocalizer::new(&*LANGUAGE_LOADER, &Localizations))
}
pub fn localize() {
let localizer = localizer();
let requested_languages = i18n_embed::DesktopLanguageRequester::requested_languages();
if let Err(error) = localizer.select(&requested_languages) {
eprintln!("Error while loading language for App List {}", error);
}
}

View file

@ -1,49 +1,23 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#[macro_use]
extern crate relm4_macros;
mod app;
mod config;
mod localize;
mod network_manager;
pub mod task;
pub mod ui;
pub mod widgets;
use log::info;
use gtk4::{glib, prelude::*, Orientation, Separator};
use once_cell::sync::Lazy;
use tokio::runtime::Runtime;
use crate::config::{APP_ID, PROFILE, VERSION};
use crate::localize::localize;
static RT: Lazy<Runtime> = Lazy::new(|| Runtime::new().expect("failed to build tokio runtime"));
fn main() -> cosmic::iced::Result {
// Initialize logger
pretty_env_logger::init();
info!("Iced Workspaces Applet ({})", APP_ID);
info!("Version: {} ({})", VERSION, PROFILE);
fn main() {
let _monitors = libcosmic::init();
// Prepare i18n
localize();
view! {
window = libcosmic_applet::AppletWindow {
set_title: Some("COSMIC Network Applet"),
#[wrap(Some)]
set_child: button = &libcosmic_applet::AppletButton {
set_button_icon_name: "network-workgroup-symbolic",
#[wrap(Some)]
set_popover_child: main_box = &gtk4::Box {
set_orientation: Orientation::Vertical,
set_spacing: 10,
set_margin_top: 20,
set_margin_bottom: 20,
set_margin_start: 24,
set_margin_end: 24
}
}
}
}
ui::current_networks::add_current_networks(&main_box, &button);
main_box.append(&Separator::new(Orientation::Horizontal));
ui::toggles::add_toggles(&main_box);
let available_wifi_separator = Separator::new(Orientation::Horizontal);
main_box.append(&available_wifi_separator);
available_wifi_separator.hide();
ui::available_wifi::add_available_wifi(&main_box, available_wifi_separator);
window.show();
let main_loop = glib::MainLoop::new(None, false);
main_loop.run();
app::run()
}

View file

@ -0,0 +1,43 @@
// SPDX-License-Identifier: GPL-3.0-or-later
use cosmic_dbus_networkmanager::device::wireless::WirelessDevice;
use futures_util::StreamExt;
use itertools::Itertools;
use std::collections::HashMap;
pub async fn handle_wireless_device(device: WirelessDevice<'_>) -> zbus::Result<Vec<AccessPoint>> {
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 {
eprintln!("scan errored");
return Ok(Default::default());
}
}
let access_points = device.get_access_points().await?;
// Sort by strength and remove duplicates
let mut aps = HashMap::<String, AccessPoint>::new();
for ap in access_points {
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)
.sorted_by(|a, b| b.strength.cmp(&a.strength))
.collect();
Ok(aps)
}
#[derive(Debug, Clone)]
pub struct AccessPoint {
pub ssid: String,
pub strength: u8,
}

View file

@ -0,0 +1,108 @@
// SPDX-License-Identifier: GPL-3.0-or-later
use cosmic_dbus_networkmanager::{
active_connection::ActiveConnection,
device::SpecificDevice,
interface::enums::{ApFlags, ApSecurityFlags},
};
use std::net::IpAddr;
pub async fn active_connections(
active_connections: Vec<ActiveConnection<'_>>,
) -> zbus::Result<Vec<ActiveConnectionInfo>> {
let mut info = Vec::<ActiveConnectionInfo>::with_capacity(active_connections.len());
for connection in active_connections {
if connection.vpn().await.unwrap_or_default() {
let mut ip_addresses = Vec::new();
for address_data in connection.ip4_config().await?.address_data().await.unwrap_or_default() {
ip_addresses.push(IpAddr::V4(address_data.address));
}
for address_data in connection.ip6_config().await?.address_data().await.unwrap_or_default() {
ip_addresses.push(IpAddr::V6(address_data.address));
}
info.push(ActiveConnectionInfo::Vpn {
name: connection.id().await?,
ip_addresses,
});
continue;
}
for device in connection.devices().await.unwrap_or_default() {
match device.downcast_to_device().await.ok().and_then(|inner| inner) {
Some(SpecificDevice::Wired(wired_device)) => {
let mut ip_addresses = Vec::new();
for address_data in device.ip4_config().await?.address_data().await.unwrap_or_default() {
ip_addresses.push(IpAddr::V4(address_data.address));
}
for address_data in device.ip6_config().await?.address_data().await.unwrap_or_default() {
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?,
ip_addresses,
});
}
Some(SpecificDevice::Wireless(wireless_device)) => {
if let Ok(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?,
});
}
}
Some(SpecificDevice::WireGuard(_)) => {
let mut ip_addresses = Vec::new();
for address_data in connection.ip4_config().await?.address_data().await.unwrap_or_default() {
ip_addresses.push(IpAddr::V4(address_data.address));
}
for address_data in connection.ip6_config().await?.address_data().await.unwrap_or_default() {
ip_addresses.push(IpAddr::V6(address_data.address));
}
info.push(ActiveConnectionInfo::Vpn {
name: connection.id().await?,
ip_addresses,
});
}
_ => {}
}
}
}
info.sort_by(|a, b| {
let helper = |conn: &ActiveConnectionInfo| {
match conn {
ActiveConnectionInfo::Vpn { name, .. } => format!("0{name}"),
ActiveConnectionInfo::Wired { name, .. } => format!("1{name}"),
ActiveConnectionInfo::WiFi { name, .. } => format!("2{name}"),
}
};
helper(a).cmp(&helper(b))
});
Ok(info)
}
#[derive(Debug, Clone)]
pub enum ActiveConnectionInfo {
Wired {
name: String,
hw_address: String,
speed: u32,
ip_addresses: Vec<IpAddr>,
},
WiFi {
name: String,
hw_address: String,
flags: ApFlags,
rsn_flags: ApSecurityFlags,
wpa_flags: ApSecurityFlags,
},
Vpn {
name: String,
ip_addresses: Vec<IpAddr>,
},
}

View file

@ -0,0 +1,242 @@
pub mod available_wifi;
pub mod current_networks;
use std::{fmt::Debug, hash::Hash, time::Duration};
use cosmic::iced::{self, subscription};
use cosmic_dbus_networkmanager::{
device::SpecificDevice, interface::enums::DeviceType, nm::NetworkManager,
};
use futures::{
channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender},
FutureExt, StreamExt,
};
use zbus::Connection;
use self::{
available_wifi::{handle_wireless_device, AccessPoint},
current_networks::{active_connections, ActiveConnectionInfo},
};
// TODO subscription for wifi list & selection of wifi
// TODO subscription & channel for enabling / disabling wifi
// TODO subscription for displaying active connections & devices
pub fn network_manager_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
id: I,
) -> iced::Subscription<(I, NetworkManagerEvent)> {
subscription::unfold(id, State::Ready, move |state| start_listening(id, state))
}
#[derive(Debug)]
pub enum State {
Ready,
Waiting(Connection, UnboundedReceiver<NetworkManagerRequest>),
Finished,
}
async fn start_listening<I: Copy>(
id: I,
state: State,
) -> (Option<(I, NetworkManagerEvent)>, State) {
match state {
State::Ready => {
let conn = match Connection::system().await {
Ok(c) => c,
Err(_) => return (None, State::Finished),
};
let network_manager = match NetworkManager::new(&conn).await {
Ok(n) => n,
Err(_) => return (None, State::Finished),
};
let (tx, rx) = unbounded();
let mut active_conns = active_connections(
network_manager
.active_connections()
.await
.unwrap_or_default(),
)
.await
.unwrap_or_default();
active_conns.sort_by(|a, b| {
let helper = |conn: &ActiveConnectionInfo| match conn {
ActiveConnectionInfo::Vpn { name, .. } => format!("0{name}"),
ActiveConnectionInfo::Wired { name, .. } => format!("1{name}"),
ActiveConnectionInfo::WiFi { name, .. } => format!("2{name}"),
};
helper(a).cmp(&helper(b))
});
let wifi_enabled = network_manager.wireless_enabled().await.unwrap_or_default();
let devices = network_manager.devices().await.ok().unwrap_or_default();
let wireless_access_point_futures: Vec<_> = devices
.into_iter()
.map(|device| async move {
if let Ok(Some(SpecificDevice::Wireless(wireless_device))) =
device.downcast_to_device().await
{
handle_wireless_device(wireless_device)
.await
.unwrap_or_default()
} else {
Vec::new()
}
})
.collect();
let mut wireless_access_points =
Vec::with_capacity(wireless_access_point_futures.len());
for f in wireless_access_point_futures {
wireless_access_points.append(&mut f.await);
}
wireless_access_points.sort_by(|a, b| b.strength.cmp(&a.strength));
drop(network_manager);
return (
Some((
id,
NetworkManagerEvent::Init {
sender: tx,
wireless_access_points,
wifi_enabled,
airplane_mode: false,
active_conns,
},
)),
State::Waiting(conn, rx),
);
}
State::Waiting(conn, mut rx) => {
let network_manager = match NetworkManager::new(&conn).await {
Ok(n) => n,
Err(_) => return (None, State::Finished),
};
let mut active_conns_changed = tokio::time::sleep(Duration::from_secs(5))
.then(|_| async { network_manager.receive_active_connections_changed().await })
.await;
let mut devices_changed = network_manager.receive_devices_changed().await;
let mut wireless_enabled_changed =
network_manager.receive_wireless_enabled_changed().await;
let mut req = rx.next().boxed().fuse();
let (update, should_exit) = futures::select! {
req = req => {
match req {
Some(NetworkManagerRequest::SetAirplaneMode(state)) => {
// TODO set airplane mode
let _ = network_manager.set_wireless_enabled(state).await;
(None, false)
}
Some(NetworkManagerRequest::SetWiFi(enabled)) => {
let success = network_manager.set_wireless_enabled(enabled).await.is_ok();
let active_conns = active_connections(network_manager.active_connections().await.unwrap_or_default()).await.unwrap_or_default();
let devices = network_manager.devices().await.ok().unwrap_or_default();
let wireless_access_point_futures: Vec<_> = devices.into_iter().map(|device| async move {
if let Ok(Some(SpecificDevice::Wireless(wireless_device))) =
device.downcast_to_device().await
{
handle_wireless_device(wireless_device).await.unwrap_or_default()
} else {
Vec::new()
}
}).collect();
let mut wireless_access_points = Vec::with_capacity(wireless_access_point_futures.len());
for f in wireless_access_point_futures {
wireless_access_points.append(&mut f.await);
}
(Some((id, NetworkManagerEvent::RequestResponse {
req: NetworkManagerRequest::SetWiFi(enabled),
success,
active_conns,
wireless_access_points,
wifi_enabled: enabled,
airplane_mode: false,
})), false)
}
Some(NetworkManagerRequest::SelectAccessPoint(ssid)) => {
'device_loop: for device in network_manager.devices().await.ok().unwrap_or_default() {
if matches!(device.device_type().await.unwrap_or(DeviceType::Other), DeviceType::Wifi) {
for conn in device.available_connections().await.unwrap_or_default() {
// dbg!(&conn.path());
// TODO activate connection
}
}
}
(None, false)
}
None => {
(None, true)
}
}}
_ = active_conns_changed.next().boxed().fuse() => {
let active_conns = active_connections(network_manager.active_connections().await.unwrap_or_default()).await.unwrap_or_default();
(Some((id, NetworkManagerEvent::ActiveConns(active_conns))), false)
}
_ = devices_changed.next().boxed().fuse() => {
let devices = network_manager.devices().await.ok().unwrap_or_default();
let wireless_access_point_futures: Vec<_> = devices.into_iter().map(|device| async move {
if let Ok(Some(SpecificDevice::Wireless(wireless_device))) =
device.downcast_to_device().await
{
handle_wireless_device(wireless_device).await.unwrap_or_default()
} else {
Vec::new()
}
}).collect();
let mut wireless_access_points = Vec::with_capacity(wireless_access_point_futures.len());
for f in wireless_access_point_futures {
wireless_access_points.append(&mut f.await);
}
(Some((id, NetworkManagerEvent::WirelessAccessPoints(wireless_access_points))), false)
}
enabled = wireless_enabled_changed.next().boxed().fuse() => {
let update = if let Some(update) = enabled {
update.get().await.ok().map(|update| (id, NetworkManagerEvent::WiFiEnabled(update)))
} else {
None
};
(update, false)
}
};
drop(active_conns_changed);
drop(wireless_enabled_changed);
drop(req);
(
update,
if should_exit {
State::Finished
} else {
State::Waiting(conn, rx)
},
)
}
State::Finished => iced::futures::future::pending().await,
}
}
#[derive(Debug, Clone)]
pub enum NetworkManagerRequest {
SetAirplaneMode(bool),
SetWiFi(bool),
SelectAccessPoint(String),
}
#[derive(Debug, Clone)]
pub enum NetworkManagerEvent {
Init {
sender: UnboundedSender<NetworkManagerRequest>,
wireless_access_points: Vec<AccessPoint>,
active_conns: Vec<ActiveConnectionInfo>,
wifi_enabled: bool,
airplane_mode: bool,
},
RequestResponse {
req: NetworkManagerRequest,
wireless_access_points: Vec<AccessPoint>,
active_conns: Vec<ActiveConnectionInfo>,
wifi_enabled: bool,
airplane_mode: bool,
success: bool,
},
WiFiEnabled(bool),
WirelessAccessPoints(Vec<AccessPoint>),
ActiveConns(Vec<ActiveConnectionInfo>),
}

View file

@ -1,35 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-or-later
use std::future::Future;
use tokio::sync::oneshot;
pub fn spawn<O, F>(future: F) -> tokio::task::JoinHandle<O>
where
F: Future<Output = O> + Send + 'static,
O: Send + 'static,
{
crate::RT.spawn(future)
}
pub fn block_on<O, F>(future: F) -> O
where
F: Future<Output = O> + Send + 'static,
O: Send + 'static,
{
crate::RT.block_on(future)
}
pub fn spawn_local<F: Future<Output = ()> + 'static>(future: F) {
gtk4::glib::MainContext::default().spawn_local(future);
}
pub async fn wait_for_local<O, F>(future: F) -> Option<O>
where
O: Send + 'static,
F: Future<Output = O> + Send + 'static,
{
let (tx, rx) = oneshot::channel::<O>();
gtk4::glib::MainContext::default().spawn_local(async move {
std::mem::drop(tx.send(future.await));
});
rx.await.ok()
}

View file

@ -1,5 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pub mod available_wifi;
pub mod current_networks;
pub mod toggles;

View file

@ -1,128 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-or-later
use crate::task;
use cosmic_dbus_networkmanager::{
device::{wireless::WirelessDevice, SpecificDevice},
nm::NetworkManager,
};
use futures_util::StreamExt;
use gtk4::{
glib::{self, clone, source::PRIORITY_DEFAULT, MainContext, Sender},
prelude::*,
Image, ListBox, ListBoxRow, Separator,
};
use libcosmic::widgets::{relm4::RelmContainerExt, LabeledItem};
use std::{
cell::RefCell,
collections::{BTreeMap, HashMap},
rc::Rc,
};
use zbus::Connection;
pub fn add_available_wifi(target: &gtk4::Box, separator: Separator) {
let ap_entries = Rc::<RefCell<Vec<ListBoxRow>>>::default();
let (tx, rx) = MainContext::channel::<Vec<AccessPoint>>(PRIORITY_DEFAULT);
task::spawn(async move {
if let Err(err) = scan_for_wifi(tx).await {
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);
scrolled_window.set_propagate_natural_height(true);
scrolled_window.set_max_content_height(300);
let wifi_list = ListBox::new();
rx.attach(
None,
clone!(@strong ap_entries, @weak wifi_list, @weak separator, => @default-return Continue(true), move |aps| {
build_aps_list(ap_entries.clone(), &wifi_list, aps);
separator.set_visible(!ap_entries.borrow().is_empty());
Continue(true)
}),
);
scrolled_window.set_child(Some(&wifi_list));
target.append(&scrolled_window);
}
fn build_aps_list(
ap_entries: Rc<RefCell<Vec<ListBoxRow>>>,
target: &ListBox,
aps: Vec<AccessPoint>,
) {
let mut ap_entries = ap_entries.borrow_mut();
for old_ap_box in ap_entries.drain(..) {
target.remove(&old_ap_box);
}
for ap in aps {
view! {
entry = ListBoxRow {
#[wrap(Some)]
set_child: entry_box = &gtk4::Box {
container_add: labeled_item = &LabeledItem {
set_title: &ap.ssid,
set_child: icon = &Image {
set_icon_name: Some("network-wireless-symbolic")
}
}
}
}
}
target.append(&entry);
ap_entries.push(entry);
}
}
async fn scan_for_wifi(tx: Sender<Vec<AccessPoint>>) -> zbus::Result<()> {
let conn = Connection::system().await?;
let network_manager = NetworkManager::new(&conn).await?;
loop {
let devices = network_manager.devices().await?;
for device in devices {
if let Ok(Some(SpecificDevice::Wireless(wireless_device))) =
device.downcast_to_device().await
{
handle_wireless_device(wireless_device, tx.clone()).await?;
}
}
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
}
}
async fn handle_wireless_device(
device: WirelessDevice<'_>,
tx: Sender<Vec<AccessPoint>>,
) -> zbus::Result<()> {
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 {
eprintln!("scan errored");
return Ok(());
}
}
let access_points = device.get_access_points().await?;
// Sort by SSID and remove duplicates
let mut aps = BTreeMap::<String, AccessPoint>::new();
for ap in access_points {
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(())
}
#[derive(Debug)]
struct AccessPoint {
ssid: String,
strength: u8,
}

View file

@ -1,248 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-or-later
use cosmic_dbus_networkmanager::{
active_connection::ActiveConnection,
device::SpecificDevice,
interface::{
active_connection::ActiveConnectionProxy,
enums::{ApFlags, ApSecurityFlags},
},
nm::NetworkManager,
};
use futures_util::StreamExt;
use gtk4::{
glib::{self, clone, source::PRIORITY_DEFAULT, MainContext, Sender},
prelude::*,
IconSize, Image, ListBox, ListBoxRow, Orientation,
};
use std::{cell::RefCell, net::IpAddr, rc::Rc};
use zbus::Connection;
pub fn add_current_networks(target: &gtk4::Box, icon_image: &libcosmic_applet::AppletButton) {
let networks_list = ListBox::builder().show_separators(true).build();
let entries = Rc::<RefCell<Vec<ListBoxRow>>>::default();
let (tx, rx) = MainContext::channel::<Vec<ActiveConnectionInfo>>(PRIORITY_DEFAULT);
crate::task::spawn(handle_devices(tx));
rx.attach(
None,
clone!(@weak networks_list, @weak icon_image, @strong entries => @default-return Continue(true), move |connections| {
let mut entries = entries.borrow_mut();
display_active_connections(connections, &networks_list, &mut *entries, &icon_image);
Continue(true)
}),
);
target.append(&networks_list);
}
fn display_active_connections(
connections: Vec<ActiveConnectionInfo>,
target: &ListBox,
entries: &mut Vec<ListBoxRow>,
icon_image: &libcosmic_applet::AppletButton,
) {
for old_entry in entries.drain(..) {
target.remove(&old_entry);
}
for connection in connections {
let entry = match connection {
ActiveConnectionInfo::Wired {
name,
hw_address,
speed,
ip_addresses,
} => {
icon_image.set_button_icon_name("network-wired-symbolic");
render_wired_connection(name, speed, ip_addresses)
}
ActiveConnectionInfo::WiFi {
name,
hw_address,
flags,
rsn_flags,
wpa_flags,
} => continue,
ActiveConnectionInfo::Vpn { name, ip_addresses } => {
icon_image.set_button_icon_name("network-vpn-symbolic");
render_vpn(name, ip_addresses)
}
};
let entry = ListBoxRow::builder().child(&entry).build();
target.append(&entry);
entries.push(entry);
}
}
fn render_wired_connection(name: String, speed: u32, ip_addresses: Vec<IpAddr>) -> gtk4::Box {
view! {
entry = gtk4::Box {
set_orientation: Orientation::Horizontal,
set_spacing: 8,
append: wired_icon = &Image {
set_icon_name: Some("network-wired-symbolic"),
set_icon_size: IconSize::Large
},
append: wired_label_box = &gtk4::Box {
set_orientation: Orientation::Vertical,
append: wired_label = &gtk4::Label {
set_label: &name,
set_halign: gtk4::Align::Start,
}
},
append: wired_speed = &gtk4::Label {
set_label: &format!("Connected - {} Mbps", speed),
set_valign: gtk4::Align::Center,
},
}
}
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);
}
entry
}
fn render_vpn(name: String, ip_addresses: Vec<IpAddr>) -> gtk4::Box {
view! {
entry = gtk4::Box {
set_orientation: Orientation::Horizontal,
set_spacing: 8,
append: wired_icon = &Image {
set_icon_name: Some("network-vpn-symbolic"),
set_icon_size: IconSize::Large
},
append: wired_label_box = &gtk4::Box {
set_orientation: Orientation::Vertical,
append: wired_label = &gtk4::Label {
set_label: &name,
set_halign: gtk4::Align::Start,
}
}
}
}
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);
}
entry
}
async fn handle_devices(tx: Sender<Vec<ActiveConnectionInfo>>) -> zbus::Result<()> {
let conn = Connection::system().await?;
let network_manager = NetworkManager::new(&conn).await?;
handle_active_connections(tx.clone(), network_manager.active_connections().await?).await?;
let mut active_connections_changed = network_manager.receive_active_connections_changed().await;
while let Some(active_connection_objects) = active_connections_changed.next().await {
let active_connection_objects = active_connection_objects.get().await?;
let mut active_connections = Vec::with_capacity(active_connection_objects.len());
for object in active_connection_objects {
active_connections.push(
ActiveConnectionProxy::builder(&conn)
.path(object)?
.build()
.await
.map(ActiveConnection::from)?,
);
}
handle_active_connections(tx.clone(), active_connections).await?;
}
Ok(())
}
async fn handle_active_connections(
tx: Sender<Vec<ActiveConnectionInfo>>,
active_connections: Vec<ActiveConnection<'_>>,
) -> zbus::Result<()> {
let mut info = Vec::<ActiveConnectionInfo>::with_capacity(active_connections.len());
for connection in active_connections {
if connection.vpn().await? {
let mut ip_addresses = Vec::new();
for address_data in connection.ip4_config().await?.address_data().await? {
ip_addresses.push(IpAddr::V4(address_data.address));
}
for address_data in connection.ip6_config().await?.address_data().await? {
ip_addresses.push(IpAddr::V6(address_data.address));
}
info.push(ActiveConnectionInfo::Vpn {
name: connection.id().await?,
ip_addresses,
});
continue;
}
for device in connection.devices().await? {
match device.downcast_to_device().await? {
Some(SpecificDevice::Wired(wired_device)) => {
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?,
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?,
});
}
Some(SpecificDevice::WireGuard(_)) => {
let mut ip_addresses = Vec::new();
for address_data in connection.ip4_config().await?.address_data().await? {
ip_addresses.push(IpAddr::V4(address_data.address));
}
for address_data in connection.ip6_config().await?.address_data().await? {
ip_addresses.push(IpAddr::V6(address_data.address));
}
info.push(ActiveConnectionInfo::Vpn {
name: connection.id().await?,
ip_addresses,
});
}
_ => {}
}
}
}
tx.send(info)
.expect("failed to send active connections back to main thread");
Ok(())
}
enum ActiveConnectionInfo {
Wired {
name: String,
hw_address: String,
speed: u32,
ip_addresses: Vec<IpAddr>,
},
WiFi {
name: String,
hw_address: String,
flags: ApFlags,
rsn_flags: ApSecurityFlags,
wpa_flags: ApSecurityFlags,
},
Vpn {
name: String,
ip_addresses: Vec<IpAddr>,
},
}

View file

@ -1,71 +0,0 @@
use crate::{task, widgets::SettingsEntry};
use cosmic_dbus_networkmanager::nm::NetworkManager;
use futures_util::StreamExt;
use gtk4::{
glib::{self, clone, source::PRIORITY_DEFAULT, MainContext, Sender},
prelude::*,
Inhibit, Orientation, Separator, Switch,
};
use zbus::Connection;
pub fn add_toggles(target: &gtk4::Box) {
view! {
airplane_mode = SettingsEntry {
set_title_markup: "<b>Airplane Mode</b>",
set_child: airplane_mode_switch = &Switch {}
}
}
view! {
wifi = SettingsEntry {
set_title_markup: "<b>WiFi</b>",
set_child: wifi_switch = &Switch {}
}
}
target.append(&airplane_mode);
target.append(&Separator::new(Orientation::Horizontal));
target.append(&wifi);
target.append(&Separator::new(Orientation::Horizontal));
let (wifi_tx, wifi_rx) = MainContext::channel::<bool>(PRIORITY_DEFAULT);
wifi_switch.connect_state_set(
clone!(@strong wifi_tx => @default-return Inhibit(false), move |_switch, state| {
match task::block_on(set_wifi_mode(state)) {
Ok(()) => Inhibit(false),
Err(err) => {
eprintln!("set_wifi_mode failed: {}", err);
Inhibit(true)
}
}
}),
);
wifi_rx.attach(
None,
clone!(@weak wifi_switch => @default-return Continue(true), move |wifi| {
wifi_switch.set_active(wifi);
Continue(true)
}),
);
task::spawn(get_wifi_mode(wifi_tx));
}
async fn get_wifi_mode(tx: Sender<bool>) -> zbus::Result<()> {
let connection = Connection::system().await?;
let network_manager = NetworkManager::new(&connection).await?;
let wireless_enabled = network_manager.wireless_enabled().await?;
tx.send(wireless_enabled)
.expect("Failed to send wifi enablement back to main thread");
let mut stream = network_manager.receive_wireless_enabled_changed().await;
while let Some(wireless_enabled) = stream.next().await {
if let Ok(wireless_enabled) = wireless_enabled.get().await {
tx.send(wireless_enabled)
.expect("Failed to send wifi enablement back to main thread");
}
}
Ok(())
}
async fn set_wifi_mode(state: bool) -> zbus::Result<()> {
let connection = Connection::system().await?;
let network_manager = NetworkManager::new(&connection).await?;
network_manager.set_wireless_enabled(state).await
}

View file

@ -1,5 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-or-later
mod settings_entry;
pub use settings_entry::SettingsEntry;

View file

@ -1,211 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-or-later
use gtk4::{
glib::{self, Object},
prelude::*,
subclass::prelude::*,
Label,
};
use std::cell::RefCell;
glib::wrapper! {
pub struct SettingsEntry(ObjectSubclass<SettingsEntryImp>)
@extends gtk4::Widget,
@implements gtk4::Accessible;
}
impl SettingsEntry {
pub fn new() -> Self {
Self::default()
}
pub fn set_child<'a, Widget, IntoWidget>(&self, child: IntoWidget)
where
Widget: IsA<gtk4::Widget>,
IntoWidget: Into<Option<&'a Widget>>,
{
let imp = self.inner();
let child = child.into().map(AsRef::as_ref);
let child_box_ref = imp.child_box.borrow();
let child_box: &gtk4::Box = child_box_ref.as_ref().expect("child_box not created??");
if let Some(new_child) = child {
new_child.set_halign(gtk4::Align::End);
child_box.append(new_child);
}
if let Some(old_child) = imp.child.replace(child.cloned()) {
child_box.remove(&old_child);
}
}
pub fn set_child_label<A: AsRef<str>>(&self, label: A) {
let label = label.as_ref();
let child = Label::builder()
.label(label)
.css_classes(vec!["settings-entry-text".into()])
.build();
self.set_child(&child);
}
pub fn align_child(&self, alignment: gtk4::Align) {
let imp = self.inner();
let child_box = imp.child_box.borrow();
let child_box = child_box.as_ref().expect("child_box not created??");
let child = imp.child.borrow();
let child = child.as_ref().expect("child not set");
let title_desc_box = imp.title_desc_box.borrow();
let title_desc_box = title_desc_box
.as_ref()
.expect("title_desc_box not created?");
match alignment {
gtk4::Align::Start => {
child_box.reorder_child_after(title_desc_box, Some(child));
}
gtk4::Align::End => {
child_box.reorder_child_after(child, Some(title_desc_box));
}
_ => unimplemented!(),
}
}
pub fn set_title(&self, title: &str) {
let inner = self.inner();
let title_ref = inner.title.borrow_mut();
match &*title_ref {
Some(label) => label.set_label(title),
None => {
let title = gtk4::Label::builder()
.label(title)
.css_classes(vec!["settings-entry-title".into()])
.halign(gtk4::Align::Start)
.build();
let title_desc_box = inner.title_desc_box.borrow();
let title_desc_box = title_desc_box
.as_ref()
.expect("title_desc_box not created?");
if inner.desc.borrow().is_some() {
title_desc_box.prepend(&title);
} else {
title_desc_box.append(&title);
}
}
}
}
pub fn set_title_markup(&self, title: &str) {
let inner = self.inner();
let title_ref = inner.title.borrow_mut();
match &*title_ref {
Some(label) => label.set_markup(title),
None => {
let title = gtk4::Label::builder()
.label(title)
.use_markup(true)
.css_classes(vec!["settings-entry-title".into()])
.halign(gtk4::Align::Start)
.build();
let title_desc_box = inner.title_desc_box.borrow();
let title_desc_box = title_desc_box
.as_ref()
.expect("title_desc_box not created?");
if inner.desc.borrow().is_some() {
title_desc_box.prepend(&title);
} else {
title_desc_box.append(&title);
}
}
}
}
pub fn set_description(&self, description: &str) {
let inner = self.inner();
let desc_ref = inner.desc.borrow_mut();
match &*desc_ref {
Some(label) => label.set_label(description),
None => {
let desc = gtk4::Label::builder()
.label(description)
.css_classes(vec!["settings-entry-desc".into()])
.halign(gtk4::Align::Start)
.build();
let title_desc_box = inner.title_desc_box.borrow();
let title_desc_box = title_desc_box
.as_ref()
.expect("title_desc_box not created?");
title_desc_box.append(&desc);
}
}
}
fn inner(&self) -> &SettingsEntryImp {
SettingsEntryImp::from_instance(self)
}
}
impl Default for SettingsEntry {
fn default() -> Self {
Object::new(&[]).expect("Failed to create `SettingsEntry`.")
}
}
#[derive(Debug, Default)]
pub struct SettingsEntryImp {
title: RefCell<Option<gtk4::Label>>,
desc: RefCell<Option<gtk4::Label>>,
title_desc_box: RefCell<Option<gtk4::Box>>,
child_box: RefCell<Option<gtk4::Box>>,
child: RefCell<Option<gtk4::Widget>>,
}
#[glib::object_subclass]
impl ObjectSubclass for SettingsEntryImp {
const NAME: &'static str = "SettingsEntry";
type Type = SettingsEntry;
type ParentType = gtk4::Widget;
fn class_init(klass: &mut Self::Class) {
// The layout manager determines how child widgets are laid out.
klass.set_layout_manager_type::<gtk4::BinLayout>();
}
}
impl ObjectImpl for SettingsEntryImp {
fn constructed(&self, obj: &Self::Type) {
self.parent_constructed(obj);
let child = gtk4::Box::builder()
.css_classes(vec!["settings-entry".into()])
.orientation(gtk4::Orientation::Horizontal)
.hexpand(true)
.margin_start(24)
.margin_end(24)
.margin_top(8)
.margin_bottom(8)
.spacing(16)
.build();
let title_and_desc = gtk4::Box::builder()
.css_classes(vec!["settings-entry-info".into()])
.orientation(gtk4::Orientation::Vertical)
.spacing(4)
.hexpand(true)
.valign(gtk4::Align::Center)
.build();
child.append(&title_and_desc);
*self.title_desc_box.borrow_mut() = Some(title_and_desc);
if let Some(entry_child) = self.child.borrow().as_ref() {
child.append(entry_child);
}
child.set_parent(obj);
*self.child_box.borrow_mut() = Some(child);
}
fn dispose(&self, _obj: &Self::Type) {
if let Some(child) = self.child.borrow_mut().take() {
child.unparent();
}
if let Some(child_box) = self.child_box.borrow_mut().take() {
child_box.unparent();
}
}
}
impl WidgetImpl for SettingsEntryImp {}