| 1 | use crate::xdg::NOTIFICATION_DEFAULT_BUS; |
| 2 | |
| 3 | fn skip_first_slash(s: &str) -> &str { |
| 4 | if let Some('/') = s.chars().next() { |
| 5 | &s[1..] |
| 6 | } else { |
| 7 | s |
| 8 | } |
| 9 | } |
| 10 | |
| 11 | use std::path::PathBuf; |
| 12 | |
| 13 | type BusNameType = std::borrow::Cow<'static, str>; |
| 14 | |
| 15 | #[derive(Clone, Debug)] |
| 16 | pub struct NotificationBus(BusNameType); |
| 17 | |
| 18 | impl Default for NotificationBus { |
| 19 | #[cfg(feature = "zbus")] |
| 20 | fn default() -> Self { |
| 21 | Self( |
| 22 | zbus::names::WellKnownName::from_static_str(NOTIFICATION_DEFAULT_BUS) |
| 23 | .unwrap() |
| 24 | .to_string() |
| 25 | .into(), |
| 26 | ) |
| 27 | } |
| 28 | |
| 29 | #[cfg(all(feature = "dbus", not(feature = "zbus")))] |
| 30 | fn default() -> Self { |
| 31 | Self( |
| 32 | dbus::strings::BusName::from_slice(NOTIFICATION_DEFAULT_BUS) |
| 33 | .unwrap() |
| 34 | .to_string() |
| 35 | .into(), |
| 36 | ) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | impl NotificationBus { |
| 41 | fn namespaced_custom(custom_path: &str) -> Option<String> { |
| 42 | // abusing path for semantic join |
| 43 | skip_first_slash( |
| 44 | PathBuf::from("/de/hoodie/Notification") |
| 45 | .join(custom_path) |
| 46 | .to_str()?, |
| 47 | ) |
| 48 | .replace('/', ".") |
| 49 | .into() |
| 50 | } |
| 51 | |
| 52 | #[cfg(feature = "zbus")] |
| 53 | pub fn custom(custom_path: &str) -> Option<Self> { |
| 54 | let name = |
| 55 | zbus::names::WellKnownName::try_from(Self::namespaced_custom(custom_path)?).ok()?; |
| 56 | Some(Self(name.to_string().into())) |
| 57 | } |
| 58 | |
| 59 | #[cfg(all(feature = "dbus", not(feature = "zbus")))] |
| 60 | pub fn custom(custom_path: &str) -> Option<Self> { |
| 61 | let name = dbus::strings::BusName::new(Self::namespaced_custom(custom_path)?).ok()?; |
| 62 | Some(Self(name.to_string().into())) |
| 63 | } |
| 64 | |
| 65 | pub fn into_name(self) -> BusNameType { |
| 66 | self.0 |
| 67 | } |
| 68 | } |