Add Notification support

This commit is contained in:
Val Packett 2026-02-06 05:05:03 -03:00
parent 2561342e0c
commit 2626130659
3 changed files with 122 additions and 5 deletions

View file

@ -1,5 +1,6 @@
pub mod documents;
pub mod file_chooser;
pub mod file_transfer;
pub mod notification;
pub mod request;
pub mod settings;

View file

@ -0,0 +1,95 @@
use std::collections::HashMap;
use tokio_stream::StreamExt;
use tracing::warn;
use zbus::{Connection, fdo::Result, names::UniqueName, object_server::SignalEmitter, zvariant};
#[derive(Clone)]
pub struct Notification {
host: NotificationProxy<'static>,
}
#[zbus::interface(
name = "org.freedesktop.portal.Notification",
proxy(
default_service = "org.freedesktop.portal.Desktop",
default_path = "/org/freedesktop/portal/desktop"
)
)]
impl Notification {
async fn add_notification(
&self,
#[zbus(header)] hdr: zbus::message::Header<'_>,
id: &str,
notification: HashMap<&str, zvariant::Value<'_>>,
) -> Result<()> {
let sender = hdr.sender().ok_or_else(|| zbus::Error::MissingField)?;
self.host
.add_notification(
&format!("{sender}\x0C\x0CSIDEBUS\x0C\x0C{id}"),
notification,
)
.await
}
async fn remove_notification(
&self,
#[zbus(header)] hdr: zbus::message::Header<'_>,
id: &str,
) -> Result<()> {
let sender = hdr.sender().ok_or_else(|| zbus::Error::MissingField)?;
self.host
.remove_notification(&format!("{sender}\x0C\x0CSIDEBUS\x0C\x0C{id}"))
.await
}
#[zbus(signal)]
async fn action_invoked(
signal_emitter: &SignalEmitter<'_>,
id: &str,
action: &str,
parameter: Vec<zvariant::Value<'_>>,
) -> zbus::Result<()>;
#[zbus(property)]
async fn supported_options(&self) -> Result<HashMap<String, zvariant::OwnedValue>> {
self.host
.supported_options()
.await
.map_err(|err| err.into())
}
#[zbus(property, name = "version")]
fn version(&self) -> Result<u32> {
Ok(2)
}
}
impl Notification {
pub async fn new(host_session_conn: &Connection) -> Result<Self> {
let host = NotificationProxy::builder(host_session_conn)
.build()
.await?;
Ok(Self { host })
}
pub async fn forward_actions(&self, mut signal_emitter: SignalEmitter<'static>) -> Result<()> {
let mut stream = self.host.receive_action_invoked().await?;
while let Some(x) = stream.next().await {
let args = x.args()?;
let mut split = args.id.split("\x0C\x0CSIDEBUS\x0C\x0C");
let sender = split
.next()
.and_then(|x| UniqueName::try_from(x).ok().map(|x| x.to_owned()))
.ok_or_else(|| zbus::fdo::Error::Failed("bad ID".to_owned()))?;
let id = split
.next()
.ok_or_else(|| zbus::fdo::Error::Failed("bad ID".to_owned()))?;
signal_emitter = signal_emitter.set_destination(sender.into());
Notification::action_invoked(&signal_emitter, id, args.action, args.parameter).await?;
()
}
warn!("actions stream end");
Ok(())
}
}