Skip to content

Commit 751d549

Browse files
committed
Expose DeviceName property to DBus API
1 parent f533e5c commit 751d549

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

src/dbus.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ use crate::config::{DBUS_API_NAME, DBUS_API_PATH};
1010

1111
#[derive(Debug)]
1212
pub struct Packet {
13+
pub device_name: String,
1314
pub visibility: bool,
1415
visibility_tx: Arc<Mutex<Sender<bool>>>,
1516
pub visibility_rx: Arc<Mutex<Receiver<bool>>>,
1617
}
1718

1819
#[zbus::interface(name = "io.github.nozwock.Packet1")]
1920
impl Packet {
21+
#[zbus(property)]
22+
pub async fn device_name(&self) -> &str {
23+
&self.device_name
24+
}
25+
2026
#[zbus(property)]
2127
pub async fn device_visibility(&self) -> bool {
2228
self.visibility
@@ -42,7 +48,10 @@ pub async fn get_connection() -> Option<Connection> {
4248
CONNECTION.lock().await.as_ref().cloned()
4349
}
4450

45-
pub async fn create_connection(visibility: bool) -> anyhow::Result<Connection> {
51+
pub async fn create_connection(
52+
device_name: String,
53+
visibility: bool,
54+
) -> anyhow::Result<Connection> {
4655
let mut conn_guard = CONNECTION.lock().await;
4756

4857
if let Some(conn) = conn_guard.as_ref() {
@@ -54,7 +63,8 @@ pub async fn create_connection(visibility: bool) -> anyhow::Result<Connection> {
5463
.serve_at(
5564
DBUS_API_PATH,
5665
Packet {
57-
visibility: visibility,
66+
device_name,
67+
visibility,
5868
visibility_tx: Arc::new(Mutex::new(tx)),
5969
visibility_rx: Arc::new(Mutex::new(rx)),
6070
},

src/window.rs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,7 +1875,11 @@ impl PacketApplicationWindow {
18751875
let inner = async move || -> anyhow::Result<()> {
18761876
let imp = obj.imp();
18771877

1878-
_ = dbus::create_connection(imp.settings.boolean("device-visibility")).await?;
1878+
_ = dbus::create_connection(
1879+
obj.get_device_name_state().as_str().to_string(),
1880+
imp.settings.boolean("device-visibility"),
1881+
)
1882+
.await?;
18791883

18801884
let handle = glib::spawn_future_local(clone!(
18811885
#[weak]
@@ -1906,21 +1910,37 @@ impl PacketApplicationWindow {
19061910
.push(LoopingTaskHandle::Glib(handle));
19071911

19081912
imp.settings
1909-
.connect_changed(Some("device-visibility"), move |settings, key| {
1910-
let key = key.to_string();
1911-
glib::spawn_future_local(clone!(
1912-
#[weak]
1913-
settings,
1914-
async move {
1915-
let iface_ref = dbus::packet_iface().await;
1916-
let mut iface = iface_ref.get_mut().await;
1917-
iface.visibility = settings.boolean(&key);
1918-
_ = iface
1919-
.device_visibility_changed(iface_ref.signal_emitter())
1920-
.await
1921-
.inspect_err(|err| tracing::warn!(%err));
1922-
}
1923-
));
1913+
.connect_changed(None, move |settings, key| match key {
1914+
"device-visibility" | "device-name" => {
1915+
let key = key.to_string();
1916+
glib::spawn_future_local(clone!(
1917+
#[weak]
1918+
settings,
1919+
async move {
1920+
let iface_ref = dbus::packet_iface().await;
1921+
let mut iface = iface_ref.get_mut().await;
1922+
match key.as_str() {
1923+
"device-name" => {
1924+
iface.device_name =
1925+
settings.string(&key).as_str().to_string();
1926+
_ = iface
1927+
.device_name_changed(iface_ref.signal_emitter())
1928+
.await
1929+
.inspect_err(|err| tracing::warn!(%err));
1930+
}
1931+
"device-visibility" => {
1932+
iface.visibility = settings.boolean(&key);
1933+
_ = iface
1934+
.device_visibility_changed(iface_ref.signal_emitter())
1935+
.await
1936+
.inspect_err(|err| tracing::warn!(%err));
1937+
}
1938+
_ => {}
1939+
}
1940+
}
1941+
));
1942+
}
1943+
_ => {}
19241944
});
19251945

19261946
Ok(())

0 commit comments

Comments
 (0)