| 1 | use crate::{error::*, notification::Notification, xdg}; |
| 2 | use futures_lite::stream::StreamExt; |
| 3 | use zbus::MatchRule; |
| 4 | |
| 5 | use super::bus::NotificationBus; |
| 6 | use crate::response::{CloseReason, NotificationResponse, ResponseHandler}; |
| 7 | |
| 8 | pub mod bus { |
| 9 | |
| 10 | use crate::xdg::NOTIFICATION_DEFAULT_BUS; |
| 11 | |
| 12 | fn skip_first_slash(s: &str) -> &str { |
| 13 | if let Some('/') = s.chars().next() { |
| 14 | &s[1..] |
| 15 | } else { |
| 16 | s |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | use std::path::PathBuf; |
| 21 | |
| 22 | type BusNameType = zbus::names::WellKnownName<'static>; |
| 23 | |
| 24 | #[derive(Clone, Debug)] |
| 25 | pub struct NotificationBus(BusNameType); |
| 26 | |
| 27 | impl Default for NotificationBus { |
| 28 | #[cfg(feature = "zbus")] |
| 29 | fn default() -> Self { |
| 30 | Self(zbus::names::WellKnownName::from_static_str(NOTIFICATION_DEFAULT_BUS).unwrap()) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | impl NotificationBus { |
| 35 | fn namespaced_custom(custom_path: &str) -> Option<String> { |
| 36 | // abusing path for semantic join |
| 37 | skip_first_slash( |
| 38 | PathBuf::from("/de/hoodie/Notification") |
| 39 | .join(custom_path) |
| 40 | .to_str()?, |
| 41 | ) |
| 42 | .replace('/', ".") |
| 43 | .into() |
| 44 | } |
| 45 | |
| 46 | pub fn custom(custom_path: &str) -> Option<Self> { |
| 47 | let name = |
| 48 | zbus::names::WellKnownName::try_from(Self::namespaced_custom(custom_path)?).ok()?; |
| 49 | Some(Self(name)) |
| 50 | } |
| 51 | |
| 52 | pub fn into_name(self) -> BusNameType { |
| 53 | self.0 |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /// A handle to a shown notification. |
| 59 | /// |
| 60 | /// This keeps a connection alive to ensure actions work on certain desktops. |
| 61 | #[derive(Debug)] |
| 62 | pub struct ZbusNotificationHandle { |
| 63 | pub(crate) id: u32, |
| 64 | pub(crate) connection: zbus::Connection, |
| 65 | pub(crate) notification: Notification, |
| 66 | } |
| 67 | |
| 68 | impl ZbusNotificationHandle { |
| 69 | pub(crate) fn new( |
| 70 | id: u32, |
| 71 | connection: zbus::Connection, |
| 72 | notification: Notification, |
| 73 | ) -> ZbusNotificationHandle { |
| 74 | ZbusNotificationHandle { |
| 75 | id, |
| 76 | connection, |
| 77 | notification, |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | pub async fn wait_for_action(&self, invocation_closure: impl ResponseHandler) { |
| 82 | wait_for_action_signal(&self.connection, self.id, invocation_closure).await; |
| 83 | } |
| 84 | |
| 85 | pub async fn close_fallible(&self) -> Result<()> { |
| 86 | self.connection |
| 87 | .call_method( |
| 88 | Some(self.notification.bus.clone().into_name()), |
| 89 | xdg::NOTIFICATION_OBJECTPATH, |
| 90 | Some(xdg::NOTIFICATION_INTERFACE), |
| 91 | "CloseNotification", |
| 92 | &(self.id), |
| 93 | ) |
| 94 | .await?; |
| 95 | Ok(()) |
| 96 | } |
| 97 | |
| 98 | pub async fn close(&self) { |
| 99 | let _ = self.close_fallible().await; |
| 100 | } |
| 101 | |
| 102 | pub fn on_close<F>(self, closure: F) |
| 103 | where |
| 104 | F: FnOnce(CloseReason), |
| 105 | { |
| 106 | zbus::block_on(self.wait_for_action(|action: &NotificationResponse| { |
| 107 | if let NotificationResponse::Closed(reason) = action { |
| 108 | closure(*reason); |
| 109 | } |
| 110 | })); |
| 111 | } |
| 112 | |
| 113 | pub fn update_fallible(&mut self) -> Result<()> { |
| 114 | self.id = zbus::block_on(send_notification_via_connection( |
| 115 | &self.notification, |
| 116 | self.id, |
| 117 | &self.connection, |
| 118 | ))?; |
| 119 | Ok(()) |
| 120 | } |
| 121 | |
| 122 | pub fn update(&mut self) -> Result<()> { |
| 123 | self.update_fallible() |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | async fn send_notification_via_connection( |
| 128 | notification: &Notification, |
| 129 | id: u32, |
| 130 | connection: &zbus::Connection, |
| 131 | ) -> Result<u32> { |
| 132 | send_notification_via_connection_at_bus(notification, id, connection, Default::default()).await |
| 133 | } |
| 134 | |
| 135 | async fn send_notification_via_connection_at_bus( |
| 136 | notification: &Notification, |
| 137 | id: u32, |
| 138 | connection: &zbus::Connection, |
| 139 | bus: NotificationBus, |
| 140 | ) -> Result<u32> { |
| 141 | let reply: u32 = connection |
| 142 | .call_method( |
| 143 | Some(bus.into_name()), |
| 144 | xdg::NOTIFICATION_OBJECTPATH, |
| 145 | Some(xdg::NOTIFICATION_INTERFACE), |
| 146 | "Notify", |
| 147 | &( |
| 148 | ¬ification.appname, |
| 149 | id, |
| 150 | ¬ification.icon, |
| 151 | ¬ification.summary, |
| 152 | ¬ification.body, |
| 153 | ¬ification.actions, |
| 154 | crate::hints::hints_to_map(notification), |
| 155 | i32::from(notification.timeout), |
| 156 | ), |
| 157 | ) |
| 158 | .await? |
| 159 | .body() |
| 160 | .deserialize()?; |
| 161 | Ok(reply) |
| 162 | } |
| 163 | |
| 164 | pub async fn connect_and_send_notification( |
| 165 | notification: &Notification, |
| 166 | ) -> Result<ZbusNotificationHandle> { |
| 167 | let bus = notification.bus.clone(); |
| 168 | connect_and_send_notification_at_bus(notification, bus).await |
| 169 | } |
| 170 | |
| 171 | pub(crate) async fn connect_and_send_notification_at_bus( |
| 172 | notification: &Notification, |
| 173 | bus: NotificationBus, |
| 174 | ) -> Result<ZbusNotificationHandle> { |
| 175 | let connection = zbus::Connection::session().await?; |
| 176 | let inner_id = notification.id.unwrap_or(0); |
| 177 | let id = |
| 178 | send_notification_via_connection_at_bus(notification, inner_id, &connection, bus).await?; |
| 179 | |
| 180 | Ok(ZbusNotificationHandle::new( |
| 181 | id, |
| 182 | connection, |
| 183 | notification.clone(), |
| 184 | )) |
| 185 | } |
| 186 | |
| 187 | pub async fn get_capabilities_at_bus(bus: NotificationBus) -> Result<Vec<String>> { |
| 188 | let connection = zbus::Connection::session().await?; |
| 189 | let info: Vec<String> = connection |
| 190 | .call_method( |
| 191 | Some(bus.into_name()), |
| 192 | xdg::NOTIFICATION_OBJECTPATH, |
| 193 | Some(xdg::NOTIFICATION_INTERFACE), |
| 194 | "GetCapabilities", |
| 195 | &(), |
| 196 | ) |
| 197 | .await? |
| 198 | .body() |
| 199 | .deserialize()?; |
| 200 | Ok(info) |
| 201 | } |
| 202 | |
| 203 | pub async fn get_capabilities() -> Result<Vec<String>> { |
| 204 | get_capabilities_at_bus(Default::default()).await |
| 205 | } |
| 206 | |
| 207 | pub async fn get_server_information_at_bus(bus: NotificationBus) -> Result<xdg::ServerInformation> { |
| 208 | let connection = zbus::Connection::session().await?; |
| 209 | let info: xdg::ServerInformation = connection |
| 210 | .call_method( |
| 211 | Some(bus.into_name()), |
| 212 | xdg::NOTIFICATION_OBJECTPATH, |
| 213 | Some(xdg::NOTIFICATION_INTERFACE), |
| 214 | "GetServerInformation", |
| 215 | &(), |
| 216 | ) |
| 217 | .await? |
| 218 | .body() |
| 219 | .deserialize()?; |
| 220 | |
| 221 | Ok(info) |
| 222 | } |
| 223 | |
| 224 | pub async fn get_server_information() -> Result<xdg::ServerInformation> { |
| 225 | get_server_information_at_bus(Default::default()).await |
| 226 | } |
| 227 | |
| 228 | /// Listens for the `ActionInvoked(UInt32, String)` Signal. |
| 229 | /// |
| 230 | /// No need to use this, check out `Notification::show_and_wait_for_action(FnOnce(action:&str))` |
| 231 | pub async fn handle_action(id: u32, func: impl ResponseHandler) { |
| 232 | let connection = zbus::Connection::session().await.unwrap(); |
| 233 | wait_for_action_signal(&connection, id, func).await; |
| 234 | } |
| 235 | |
| 236 | async fn wait_for_action_signal( |
| 237 | connection: &zbus::Connection, |
| 238 | id: u32, |
| 239 | handler: impl ResponseHandler, |
| 240 | ) { |
| 241 | let action_signal_rule = MatchRule::builder() |
| 242 | .msg_type(zbus::message::Type::Signal) |
| 243 | .interface(xdg::NOTIFICATION_INTERFACE) |
| 244 | .unwrap() |
| 245 | .member("ActionInvoked") |
| 246 | .unwrap() |
| 247 | .build(); |
| 248 | |
| 249 | let proxy = zbus::fdo::DBusProxy::new(connection).await.unwrap(); |
| 250 | proxy.add_match_rule(action_signal_rule).await.unwrap(); |
| 251 | |
| 252 | let close_signal_rule = MatchRule::builder() |
| 253 | .msg_type(zbus::message::Type::Signal) |
| 254 | .interface(xdg::NOTIFICATION_INTERFACE) |
| 255 | .unwrap() |
| 256 | .member("NotificationClosed") |
| 257 | .unwrap() |
| 258 | .build(); |
| 259 | proxy.add_match_rule(close_signal_rule).await.unwrap(); |
| 260 | |
| 261 | while let Ok(Some(msg)) = zbus::MessageStream::from(connection).try_next().await { |
| 262 | let header = msg.header(); |
| 263 | if let zbus::message::Type::Signal = header.message_type() { |
| 264 | match header.member() { |
| 265 | Some(name) if name == "ActionInvoked" => { |
| 266 | match msg.body().deserialize::<(u32, String)>() { |
| 267 | Ok((nid, action)) if nid == id => { |
| 268 | let response = if action == "default" { |
| 269 | NotificationResponse::Default |
| 270 | } else { |
| 271 | NotificationResponse::Action(action) |
| 272 | }; |
| 273 | handler.call(&response); |
| 274 | break; |
| 275 | } |
| 276 | _ => {} |
| 277 | } |
| 278 | } |
| 279 | Some(name) if name == "NotificationClosed" => { |
| 280 | match msg.body().deserialize::<(u32, u32)>() { |
| 281 | Ok((nid, reason)) if nid == id => { |
| 282 | handler.call(&NotificationResponse::Closed(reason.into())); |
| 283 | break; |
| 284 | } |
| 285 | _ => {} |
| 286 | } |
| 287 | } |
| 288 | _ => {} |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |