mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-04-19 19:34:24 +02:00
refactor: re-add proxy trait to client
This commit is contained in:
parent
96d0d9e85b
commit
ef7397293a
1 changed files with 37 additions and 57 deletions
|
@ -1,16 +1,19 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::thread;
|
||||||
|
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use futures_signals::signal::{Mutable, MutableSignalCloned};
|
use futures_signals::signal::{Mutable, MutableSignalCloned};
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
|
use zbus::blocking::fdo::PropertiesProxy;
|
||||||
|
use zbus::blocking::Connection;
|
||||||
use zbus::{
|
use zbus::{
|
||||||
blocking::{fdo::PropertiesProxy, Connection},
|
dbus_proxy,
|
||||||
names::InterfaceName,
|
names::InterfaceName,
|
||||||
zvariant::{Error as ZVariantError, ObjectPath, Str},
|
zvariant::{Error as ZVariantError, ObjectPath, Str},
|
||||||
Error as ZBusError,
|
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_BUS: &str = "org.freedesktop.NetworkManager";
|
||||||
const DBUS_PATH: &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 {
|
pub struct Client {
|
||||||
client_state: Mutable<ClientState>,
|
client_state: Mutable<ClientState>,
|
||||||
interface_name: InterfaceName<'static>,
|
interface_name: InterfaceName<'static>,
|
||||||
props_proxy: PropertiesProxy<'static>,
|
dbus_connection: Connection,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
@ -34,75 +37,52 @@ pub enum ClientState {
|
||||||
Unknown,
|
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 {
|
impl Client {
|
||||||
fn new() -> Result<Self> {
|
fn new() -> Result<Self> {
|
||||||
let client_state = Mutable::new(ClientState::Unknown);
|
let client_state = Mutable::new(ClientState::Unknown);
|
||||||
let dbus_connection = Connection::system()?;
|
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)?;
|
let interface_name = InterfaceName::from_static_str(DBUS_INTERFACE)?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
client_state,
|
client_state,
|
||||||
interface_name,
|
interface_name,
|
||||||
props_proxy,
|
dbus_connection,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self) -> Result<()> {
|
fn run(&self) -> Result<()> {
|
||||||
let props = self.props_proxy.get_all(self.interface_name.clone())?;
|
let proxy = NetworkManagerDbusProxyBlocking::new(&self.dbus_connection)?;
|
||||||
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 changed_props_stream = self.props_proxy.receive_properties_changed()?;
|
let mut primary_connection = proxy.primary_connection()?;
|
||||||
for signal in changed_props_stream {
|
let mut primary_connection_type = proxy.primary_connection_type()?;
|
||||||
let args = signal.args()?;
|
let mut wireless_enabled = proxy.wireless_enabled()?;
|
||||||
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,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subscribe(&self) -> MutableSignalCloned<ClientState> {
|
pub fn subscribe(&self) -> MutableSignalCloned<ClientState> {
|
||||||
|
|
Loading…
Add table
Reference in a new issue