Debian Codegraph @

src/hints/message.rs

raw symbols SCIP
1 //! `Hints` allow you to pass extra information to the server.
2 //!
3 //! Many of these are standardized by either:
4 //!
5 //! [galago-project spec](http://www.galago-project.org/specs/notification/0.9/x344.html) or
6 //! [gnome notification-spec](https://developer.gnome.org/notification-spec/#hints)
7 //!
8 //! Which of these are actually implemented depends strongly on the Notification server you talk to.
9 //! Usually the `get_capabilities()` gives some clues, but the standards usually mention much more
10 //! than is actually available.
11 #![cfg_attr(rustfmt, rustfmt_skip)]
12 #![allow(dead_code, unused_imports)]
13
14
15 use super::{Hint, constants::*};
16 use crate ::Urgency;
17
18 #[cfg(all(feature = "images_no_default_features", unix, not(target_os = "macos")))]
19 use crate::image::*;
20
21 use std::collections::{HashMap, HashSet};
22 #[cfg(feature = "dbus")]
23 use dbus::arg::{messageitem::MessageItem, RefArg};
24
25 /// All currently implemented `Hints` that can be sent.
26 ///
27 /// as found on <https://developer.gnome.org/notification-spec/>
28 #[derive(Eq, PartialEq, Hash, Clone, Debug)]
29 pub(crate) struct HintMessage(Hint);
30
31 #[cfg(feature = "dbus")]
32 impl HintMessage {
33 pub fn wrap_hint(hint: Hint) -> (MessageItem, MessageItem) {
34 Self::from(hint).into()
35 }
36 }
37
38 impl From<Hint> for HintMessage {
39 fn from(hint: Hint) -> Self {
40 HintMessage(hint)
41 }
42 }
43
44 impl std::ops::Deref for HintMessage {
45 type Target = Hint;
46
47 fn deref(&self) -> &Self::Target {
48 &self.0
49 }
50 }
51
52 #[cfg(feature = "dbus")]
53 impl<'a, A: RefArg> From<(&'a String, &'a A)> for HintMessage {
54 fn from(pair: (&String, &A)) -> Self {
55
56 let (key, variant) = pair;
57 match (key.as_ref(), variant.as_u64(), variant.as_i64(), variant.as_str().map(String::from)) {
58
59 (ACTION_ICONS, Some(1), _, _ ) => Hint::ActionIcons(true),
60 (ACTION_ICONS, _, _, _ ) => Hint::ActionIcons(false),
61 (URGENCY, level, _, _ ) => Hint::Urgency(level.into()),
62 (CATEGORY, _, _, Some(name) ) => Hint::Category(name),
63
64 (DESKTOP_ENTRY, _, _, Some(entry)) => Hint::DesktopEntry(entry),
65 (IMAGE_PATH, _, _, Some(path) ) => Hint::ImagePath(path),
66 (RESIDENT, Some(1), _, _ ) => Hint::Resident(true),
67 (RESIDENT, _, _, _ ) => Hint::Resident(false),
68
69 (SOUND_FILE, _, _, Some(path) ) => Hint::SoundFile(path),
70 (SOUND_NAME, _, _, Some(name) ) => Hint::SoundName(name),
71 (SUPPRESS_SOUND, Some(1), _, _ ) => Hint::SuppressSound(true),
72 (SUPPRESS_SOUND, _, _, _ ) => Hint::SuppressSound(false),
73 (TRANSIENT, Some(1), _, _ ) => Hint::Transient(true),
74 (TRANSIENT, _, _, _ ) => Hint::Transient(false),
75 (X, _, Some(x), _ ) => Hint::X(x as i32),
76 (Y, _, Some(y), _ ) => Hint::Y(y as i32),
77
78 other => {
79 eprintln!("Invalid Hint{:#?} ", other);
80 Hint::Invalid
81 }
82 }.into()
83 }
84 }
85
86 #[cfg(feature = "dbus")]
87 impl From<HintMessage> for (MessageItem, MessageItem) {
88 fn from(hint: HintMessage) -> Self {
89
90 let (key, value): (String, MessageItem) = match hint.0 {
91 Hint::ActionIcons(value) => (ACTION_ICONS .to_owned(), MessageItem::Bool(value)), // bool
92 Hint::Category(ref value) => (CATEGORY .to_owned(), MessageItem::Str(value.clone())),
93 Hint::DesktopEntry(ref value) => (DESKTOP_ENTRY .to_owned(), MessageItem::Str(value.clone())),
94 #[cfg(all(feature = "images_no_default_features", unix, not(target_os ="macos")))]
95 Hint::ImageData(image) => (image_spec(*crate::SPEC_VERSION), ImageMessage::from(image).into()),
96 Hint::ImagePath(ref value) => (IMAGE_PATH .to_owned(), MessageItem::Str(value.clone())),
97 Hint::Resident(value) => (RESIDENT .to_owned(), MessageItem::Bool(value)), // bool
98 Hint::SoundFile(ref value) => (SOUND_FILE .to_owned(), MessageItem::Str(value.clone())),
99 Hint::SoundName(ref value) => (SOUND_NAME .to_owned(), MessageItem::Str(value.clone())),
100 Hint::SuppressSound(value) => (SUPPRESS_SOUND .to_owned(), MessageItem::Bool(value)),
101 Hint::Transient(value) => (TRANSIENT .to_owned(), MessageItem::Bool(value)),
102 Hint::X(value) => (X .to_owned(), MessageItem::Int32(value)),
103 Hint::Y(value) => (Y .to_owned(), MessageItem::Int32(value)),
104 Hint::Urgency(value) => (URGENCY .to_owned(), MessageItem::Byte(value as u8)),
105 Hint::Custom(ref key, ref val) => (key .to_owned(), MessageItem::Str(val.to_owned ())),
106 Hint::CustomInt(ref key, val) => (key .to_owned(), MessageItem::Int32(val)),
107 Hint::Invalid => ("invalid" .to_owned(), MessageItem::Str("Invalid".to_owned()))
108 };
109
110 (MessageItem::Str(key), MessageItem::Variant(Box::new(value)))
111 }
112 }
113
114
115 // TODO: deprecated, Prefer the DBus Arg and RefArg APIs
116 #[cfg(feature = "dbus")]
117 impl From<(&MessageItem, &MessageItem)> for HintMessage {
118 fn from ((key, mut value): (&MessageItem, &MessageItem)) -> Self {
119 use Hint as Hint;
120
121 // If this is a variant, consider the thing inside it
122 // If it's a nested variant, keep drilling down until we get a real value
123 while let MessageItem::Variant(inner) = value {
124 value = inner;
125 }
126
127 let is_stringy = value.inner::<&str>().is_ok();
128
129 match key.inner::<&str>() {
130 Ok(CATEGORY) => value.inner::<&str>().map(String::from).map(Hint::Category),
131 Ok(ACTION_ICONS) => value.inner().map(Hint::ActionIcons),
132 Ok(DESKTOP_ENTRY) => value.inner::<&str>().map(String::from).map(Hint::DesktopEntry),
133 Ok(IMAGE_PATH) => value.inner::<&str>().map(String::from).map(Hint::ImagePath),
134 Ok(RESIDENT) => value.inner().map(Hint::Resident),
135 Ok(SOUND_FILE) => value.inner::<&str>().map(String::from).map(Hint::SoundFile),
136 Ok(SOUND_NAME) => value.inner::<&str>().map(String::from).map(Hint::SoundName),
137 Ok(SUPPRESS_SOUND) => value.inner().map(Hint::SuppressSound),
138 Ok(TRANSIENT) => value.inner().map(Hint::Transient),
139 Ok(X) => value.inner().map(Hint::X),
140 Ok(Y) => value.inner().map(Hint::Y),
141 Ok(URGENCY) => value.inner().map(|i| match i {
142 0 => Urgency::Low,
143 2 => Urgency::Critical,
144 _ => Urgency::Normal
145 }).map(Hint::Urgency),
146 Ok(k) if is_stringy => value.inner::<&str>().map(|v| Hint::Custom(k.to_string(), v.to_string())),
147 Ok(k) => value.inner().map(|v| Hint::CustomInt(k.to_string(), v)),
148 _ => Err(()),
149 }.unwrap_or(Hint::Invalid)
150 .into()
151 }
152 }
153
154
155 #[allow(missing_docs)]
156 #[cfg(feature = "dbus")]
157 pub(crate) fn hints_from_variants<A: RefArg>(hints: &HashMap<String, A>) -> HashSet<HintMessage> {
158 hints.iter().map(Into::into).collect()
159 }