stray/notifier_host/
mod.rs

1use crate::dbus::notifier_watcher_proxy::StatusNotifierWatcherProxy;
2use crate::error::{Result, StatusNotifierWatcherError};
3use crate::{NotifierItemMessage, StatusNotifierWatcher};
4use tokio::sync::broadcast;
5use zbus::{Connection, ConnectionBuilder};
6
7pub struct NotifierHost {
8    wellknown_name: String,
9    rx: broadcast::Receiver<NotifierItemMessage>,
10    conn: Connection,
11}
12
13impl StatusNotifierWatcher {
14    pub async fn create_notifier_host(&self, unique_id: &str) -> Result<NotifierHost> {
15        let pid = std::process::id();
16        let id = &unique_id;
17        let wellknown_name = format!("org.freedesktop.StatusNotifierHost-{pid}-{id}");
18
19        let conn = ConnectionBuilder::session()?
20            .name(wellknown_name.as_str())?
21            .build()
22            .await?;
23
24        let status_notifier_proxy = StatusNotifierWatcherProxy::new(&conn).await?;
25
26        status_notifier_proxy
27            .register_status_notifier_host(&wellknown_name)
28            .await?;
29
30        Ok(NotifierHost {
31            wellknown_name,
32            rx: self.tx.subscribe(),
33            conn,
34        })
35    }
36}
37
38impl NotifierHost {
39    pub async fn recv(&mut self) -> Result<NotifierItemMessage> {
40        self.rx
41            .recv()
42            .await
43            .map_err(StatusNotifierWatcherError::from)
44    }
45
46    /// This is used to drop the StatusNotifierHost and tell Dbus to release the name
47    pub async fn destroy(self) -> Result<()> {
48        let _ = self.conn.release_name(self.wellknown_name.as_str()).await?;
49        Ok(())
50    }
51}