1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-04-19 11:24:24 +02:00

refactor: re-add proxy trait to client

This commit is contained in:
Reinout Meliesie 2024-06-05 00:55:02 +02:00
parent 96d0d9e85b
commit ef7397293a
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6

View file

@ -1,16 +1,19 @@
use std::sync::Arc;
use std::thread;
use color_eyre::Result;
use futures_signals::signal::{Mutable, MutableSignalCloned};
use tracing::error;
use zbus::blocking::fdo::PropertiesProxy;
use zbus::blocking::Connection;
use zbus::{
blocking::{fdo::PropertiesProxy, Connection},
dbus_proxy,
names::InterfaceName,
zvariant::{Error as ZVariantError, ObjectPath, Str},
Error as ZBusError,
};
use crate::{register_fallible_client, spawn_blocking};
use crate::{register_fallible_client, spawn, spawn_blocking};
const DBUS_BUS: &str = "org.freedesktop.NetworkManager";
const DBUS_PATH: &str = "/org/freedesktop/NetworkManager";
@ -20,7 +23,7 @@ const DBUS_INTERFACE: &str = "org.freedesktop.NetworkManager";
pub struct Client {
client_state: Mutable<ClientState>,
interface_name: InterfaceName<'static>,
props_proxy: PropertiesProxy<'static>,
dbus_connection: Connection,
}
#[derive(Clone, Debug)]
@ -34,75 +37,52 @@ pub enum ClientState {
Unknown,
}
#[dbus_proxy(
default_service = "org.freedesktop.NetworkManager",
interface = "org.freedesktop.NetworkManager",
default_path = "/org/freedesktop/NetworkManager"
)]
trait NetworkManagerDbus {
#[dbus_proxy(property)]
fn active_connections(&self) -> Result<Vec<ObjectPath>>;
#[dbus_proxy(property)]
fn devices(&self) -> Result<Vec<ObjectPath>>;
#[dbus_proxy(property)]
fn networking_enabled(&self) -> Result<bool>;
#[dbus_proxy(property)]
fn primary_connection(&self) -> Result<ObjectPath>;
#[dbus_proxy(property)]
fn primary_connection_type(&self) -> Result<Str>;
#[dbus_proxy(property)]
fn wireless_enabled(&self) -> Result<bool>;
}
impl Client {
fn new() -> Result<Self> {
let client_state = Mutable::new(ClientState::Unknown);
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,
dbus_connection,
})
}
fn run(&self) -> Result<()> {
let props = self.props_proxy.get_all(self.interface_name.clone())?;
let mut primary_connection = props["PrimaryConnection"]
.downcast_ref::<ObjectPath>()
.ok_or(ZBusError::Variant(ZVariantError::IncorrectType))?
.to_string();
let mut primary_connection_type = props["PrimaryConnectionType"]
.downcast_ref::<Str>()
.ok_or(ZBusError::Variant(ZVariantError::IncorrectType))?
.to_string();
let mut wireless_enabled = *props["WirelessEnabled"]
.downcast_ref::<bool>()
.ok_or(ZBusError::Variant(ZVariantError::IncorrectType))?;
self.client_state.set(determine_state(
&primary_connection,
&primary_connection_type,
wireless_enabled,
));
let proxy = NetworkManagerDbusProxyBlocking::new(&self.dbus_connection)?;
let changed_props_stream = self.props_proxy.receive_properties_changed()?;
for signal in changed_props_stream {
let args = signal.args()?;
if args.interface_name != self.interface_name {
continue;
}
let changed_props = args.changed_properties;
if let Some(new_primary_connection) = changed_props.get("PrimaryConnection") {
let new_primary_connection = new_primary_connection
.downcast_ref::<ObjectPath>()
.ok_or(ZBusError::Variant(ZVariantError::IncorrectType))?;
primary_connection = new_primary_connection.to_string();
}
if let Some(new_primary_connection_type) = changed_props.get("PrimaryConnectionType") {
let new_primary_connection_type = new_primary_connection_type
.downcast_ref::<Str>()
.ok_or(ZBusError::Variant(ZVariantError::IncorrectType))?;
primary_connection_type = new_primary_connection_type.to_string();
}
if let Some(new_wireless_enabled) = changed_props.get("WirelessEnabled") {
let new_wireless_enabled = new_wireless_enabled
.downcast_ref::<bool>()
.ok_or(ZBusError::Variant(ZVariantError::IncorrectType))?;
wireless_enabled = *new_wireless_enabled;
}
self.client_state.set(determine_state(
&primary_connection,
&primary_connection_type,
wireless_enabled,
));
}
let mut primary_connection = proxy.primary_connection()?;
let mut primary_connection_type = proxy.primary_connection_type()?;
let mut wireless_enabled = proxy.wireless_enabled()?;
Ok(())
todo!()
}
pub fn subscribe(&self) -> MutableSignalCloned<ClientState> {