2 use dbus::blocking::stdintf::org_freedesktop_dbus::PropertiesPropertiesChanged;
3 use dbus::blocking::Connection;
4 use dbus::message::Message;
5 use std::collections::HashMap;
6 use std::sync::{Arc, Mutex};
7 use std::time::Duration;
9 // Custom signal type impl
10 // Mostly copied from library examples
12 pub struct ComJacobCasperMailUnreadCount {
16 impl arg::AppendAll for ComJacobCasperMailUnreadCount {
17 fn append(&self, iter: &mut arg::IterAppend) {
18 arg::RefArg::append(&self.count, iter);
22 impl arg::ReadAll for ComJacobCasperMailUnreadCount {
23 fn read(iter: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> {
24 Ok(ComJacobCasperMailUnreadCount {
30 impl dbus::message::SignalArgs for ComJacobCasperMailUnreadCount {
31 const NAME: &'static str = "UnreadCount";
32 const INTERFACE: &'static str = "com.jacobcasper.Mail";
35 fn update_map(arc_map: Arc<Mutex<HashMap<String, String>>>, key: &str, val: &str) {
36 let clone_arc = arc_map.clone();
37 let mut map = clone_arc.lock().unwrap();
38 map.insert(String::from(key), String::from(val));
40 println! {"{}: {}", k.clone(), v.clone()};
44 fn main() -> Result<(), Box<dyn std::error::Error>> {
45 let arc_locked_xset_map = Arc::new(Mutex::new(HashMap::new()));
46 let conn = Connection::new_session()?;
48 let mail_proxy = conn.with_proxy(
49 "com.jacobcasper.Mail",
50 "/com/jacobcasper/Mail/Unread",
51 Duration::from_millis(5000),
54 let mail_match_map = arc_locked_xset_map.clone();
55 let _ = mail_proxy.match_signal(
56 move |m: ComJacobCasperMailUnreadCount, _: &Connection, _: &Message| {
58 mail_match_map.clone(),
60 m.count.to_string().as_str(),
66 let spotify_proxy = conn.with_proxy(
67 "org.mpris.MediaPlayer2.spotify",
68 "/org/mpris/MediaPlayer2",
69 Duration::from_millis(5000),
72 let spotify_match_map = arc_locked_xset_map.clone();
73 let _ = spotify_proxy.match_signal(
74 move |pc: PropertiesPropertiesChanged, _: &Connection, _: &Message| {
75 pc.changed_properties["Metadata"]
78 .and_then(|mut iter| {
79 // Iterating over a RefArg goes key, value, key, value... Insane honestly.
80 while let Some(key) = iter.next() {
81 let key_str = key.as_str()?;
82 let value = iter.next();
86 // Variant holding a variant that should just be a Vec<&str> I
87 // believe. This is _the recommended_ way to do this by the author.
88 let inner_value = value?.as_iter()?.next()?.as_iter()?.next()?;
89 let artist = inner_value.as_str()?;
90 update_map(spotify_match_map.clone(), "artist", artist);
93 let title = value?.as_iter()?.next()?.as_str()?;
94 update_map(spotify_match_map.clone(), "title", title);
106 conn.process(Duration::from_millis(1000))?;