1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-03 03:31:03 +02:00

refactor: networkmanager rewritten to utilise fallible_client

This commit is contained in:
Reinout Meliesie 2024-04-22 15:50:31 +02:00
parent 59a5166eff
commit 6478dd62fb
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
3 changed files with 63 additions and 90 deletions

View file

@ -101,10 +101,15 @@ impl Clients {
} }
#[cfg(feature = "networkmanager")] #[cfg(feature = "networkmanager")]
pub fn networkmanager(&mut self) -> Arc<networkmanager::Client> { pub fn networkmanager(&mut self) -> ClientResult<networkmanager::Client> {
self.networkmanager match &self.networkmanager {
.get_or_insert_with(networkmanager::create_client) Some(client) => Ok(client.clone()),
.clone() None => {
let client = networkmanager::create_client()?;
self.networkmanager = Some(client.clone());
Ok(client)
}
}
} }
#[cfg(feature = "notifications")] #[cfg(feature = "notifications")]

View file

@ -1,14 +1,16 @@
use std::sync::Arc; use std::sync::Arc;
use color_eyre::Result;
use futures_signals::signal::{Mutable, MutableSignalCloned}; use futures_signals::signal::{Mutable, MutableSignalCloned};
use tracing::error; use tracing::error;
use zbus::{ use zbus::{
blocking::{fdo::PropertiesProxy, Connection}, blocking::{fdo::PropertiesProxy, Connection},
names::InterfaceName, names::InterfaceName,
zvariant::{ObjectPath, Str}, zvariant::{Error as ZVariantError, ObjectPath, Str},
Error as ZBusError,
}; };
use crate::{register_client, spawn_blocking}; use crate::{register_fallible_client, spawn_blocking};
static DBUS_BUS: &str = "org.freedesktop.NetworkManager"; static DBUS_BUS: &str = "org.freedesktop.NetworkManager";
static DBUS_PATH: &str = "/org/freedesktop/NetworkManager"; static DBUS_PATH: &str = "/org/freedesktop/NetworkManager";
@ -17,6 +19,8 @@ static DBUS_INTERFACE: &str = "org.freedesktop.NetworkManager";
#[derive(Debug)] #[derive(Debug)]
pub struct Client { pub struct Client {
client_state: Mutable<ClientState>, client_state: Mutable<ClientState>,
interface_name: InterfaceName<'static>,
props_proxy: PropertiesProxy<'static>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -31,104 +35,64 @@ pub enum ClientState {
} }
impl Client { impl Client {
fn new() -> Self { fn new() -> Result<Self> {
let client_state = Mutable::new(ClientState::Unknown); let client_state = Mutable::new(ClientState::Unknown);
Self { client_state } let dbus_connection = Connection::system()?;
let props_proxy = PropertiesProxy::builder(&dbus_connection)
.destination(DBUS_BUS)?
.path(DBUS_PATH)?
.build()?;
let interface_name = InterfaceName::from_static_str(DBUS_INTERFACE)?;
Ok(Self {
client_state,
interface_name,
props_proxy,
})
} }
fn run(&self) { fn run(&self) -> Result<()> {
let Ok(dbus_connection) = Connection::system() else { let props = self.props_proxy.get_all(self.interface_name.clone())?;
error!("Failed to create D-Bus system connection"); let mut primary_connection = props["PrimaryConnection"]
return; .downcast_ref::<ObjectPath>()
}; .ok_or(ZBusError::Variant(ZVariantError::IncorrectType))?
let builder = PropertiesProxy::builder(&dbus_connection); .to_string();
let Ok(builder) = builder.destination(DBUS_BUS) else { let mut primary_connection_type = props["PrimaryConnectionType"]
error!("Failed to connect to NetworkManager D-Bus bus"); .downcast_ref::<Str>()
return; .ok_or(ZBusError::Variant(ZVariantError::IncorrectType))?
}; .to_string();
let Ok(builder) = builder.path(DBUS_PATH) else { let mut wireless_enabled = *props["WirelessEnabled"]
error!("Failed to set to NetworkManager D-Bus path"); .downcast_ref::<bool>()
return; .ok_or(ZBusError::Variant(ZVariantError::IncorrectType))?;
};
let Ok(props_proxy) = builder.build() else {
error!("Failed to create NetworkManager D-Bus properties proxy");
return;
};
let Ok(interface_name) = InterfaceName::from_static_str(DBUS_INTERFACE) else {
error!("Failed to create NetworkManager D-Bus interface name");
return;
};
let Ok(changed_props_stream) = props_proxy.receive_properties_changed() else {
error!("Failed to create NetworkManager D-Bus changed properties stream");
return;
};
let Ok(props) = props_proxy.get_all(interface_name.clone()) else {
error!("Failed to get NetworkManager D-Bus properties");
return;
};
let mut primary_connection = {
let Some(primary_connection) = props["PrimaryConnection"].downcast_ref::<ObjectPath>()
else {
error!("PrimaryConnection D-Bus property is not a path");
return;
};
primary_connection.to_string()
};
let mut primary_connection_type = {
let Some(primary_connection_type) =
props["PrimaryConnectionType"].downcast_ref::<Str>()
else {
error!("PrimaryConnectionType D-Bus property is not a string");
return;
};
primary_connection_type.to_string()
};
let mut wireless_enabled = {
let Some(wireless_enabled) = props["WirelessEnabled"].downcast_ref::<bool>() else {
error!("WirelessEnabled D-Bus property is not a boolean");
return;
};
*wireless_enabled
};
self.client_state.set(determine_state( self.client_state.set(determine_state(
&primary_connection, &primary_connection,
&primary_connection_type, &primary_connection_type,
wireless_enabled, wireless_enabled,
)); ));
let changed_props_stream = self.props_proxy.receive_properties_changed()?;
for signal in changed_props_stream { for signal in changed_props_stream {
let Ok(args) = signal.args() else { let args = signal.args()?;
error!("Failed to obtain NetworkManager D-Bus changed properties signal arguments"); if args.interface_name != self.interface_name {
return;
};
if args.interface_name != interface_name {
continue; continue;
} }
let changed_props = args.changed_properties; let changed_props = args.changed_properties;
if let Some(new_primary_connection) = changed_props.get("PrimaryConnection") { if let Some(new_primary_connection) = changed_props.get("PrimaryConnection") {
let Some(new_primary_connection) = let new_primary_connection = new_primary_connection
new_primary_connection.downcast_ref::<ObjectPath>() .downcast_ref::<ObjectPath>()
else { .ok_or(ZBusError::Variant(ZVariantError::IncorrectType))?;
error!("PrimaryConnection D-Bus property is not a path");
return;
};
primary_connection = new_primary_connection.to_string(); primary_connection = new_primary_connection.to_string();
} }
if let Some(new_primary_connection_type) = changed_props.get("PrimaryConnectionType") { if let Some(new_primary_connection_type) = changed_props.get("PrimaryConnectionType") {
let Some(new_primary_connection_type) = let new_primary_connection_type = new_primary_connection_type
new_primary_connection_type.downcast_ref::<Str>() .downcast_ref::<Str>()
else { .ok_or(ZBusError::Variant(ZVariantError::IncorrectType))?;
error!("PrimaryConnectionType D-Bus property is not a string");
return;
};
primary_connection_type = new_primary_connection_type.to_string(); primary_connection_type = new_primary_connection_type.to_string();
} }
if let Some(new_wireless_enabled) = changed_props.get("WirelessEnabled") { if let Some(new_wireless_enabled) = changed_props.get("WirelessEnabled") {
let Some(new_wireless_enabled) = new_wireless_enabled.downcast_ref::<bool>() else { let new_wireless_enabled = new_wireless_enabled
error!("WirelessEnabled D-Bus property is not a string"); .downcast_ref::<bool>()
return; .ok_or(ZBusError::Variant(ZVariantError::IncorrectType))?;
};
wireless_enabled = *new_wireless_enabled; wireless_enabled = *new_wireless_enabled;
} }
self.client_state.set(determine_state( self.client_state.set(determine_state(
@ -137,6 +101,8 @@ impl Client {
wireless_enabled, wireless_enabled,
)); ));
} }
Ok(())
} }
pub fn subscribe(&self) -> MutableSignalCloned<ClientState> { pub fn subscribe(&self) -> MutableSignalCloned<ClientState> {
@ -144,15 +110,17 @@ impl Client {
} }
} }
pub fn create_client() -> Arc<Client> { pub fn create_client() -> Result<Arc<Client>> {
let client = Arc::new(Client::new()); let client = Arc::new(Client::new()?);
{ {
let client = client.clone(); let client = client.clone();
spawn_blocking(move || { spawn_blocking(move || {
client.run(); if let Err(error) = client.run() {
error!("{}", error)
};
}); });
} }
client Ok(client)
} }
fn determine_state( fn determine_state(
@ -184,4 +152,4 @@ fn determine_state(
} }
} }
register_client!(Client, networkmanager); register_fallible_client!(Client, networkmanager);

View file

@ -36,7 +36,7 @@ impl Module<gtk::Box> for NetworkManagerModule {
context: &WidgetContext<ClientState, ()>, context: &WidgetContext<ClientState, ()>,
_: Receiver<()>, _: Receiver<()>,
) -> Result<()> { ) -> Result<()> {
let client = context.client::<Client>(); let client = context.try_client::<Client>()?;
let mut client_signal = client.subscribe().to_stream(); let mut client_signal = client.subscribe().to_stream();
let widget_transmitter = context.tx.clone(); let widget_transmitter = context.tx.clone();