use dbus::blocking::stdintf::org_freedesktop_dbus::PropertiesPropertiesChanged;
use dbus::blocking::Connection;
use dbus::message::Message;
+use std::collections::HashMap;
+use std::sync::{Arc, Mutex};
use std::time::Duration;
+fn update_map(arc_map: Arc<Mutex<HashMap<String, String>>>, key: &str, val: &str) {
+ let clone_arc = arc_map.clone();
+ let mut map = clone_arc.lock().unwrap();
+ map.insert(String::from(key), String::from(val));
+ for (k, v) in &*map {
+ println! {"{}: {}", k.clone(), v.clone()};
+ }
+}
+
fn main() -> Result<(), Box<dyn std::error::Error>> {
+ let arc_locked_xset_map = Arc::new(Mutex::new(HashMap::new()));
let conn = Connection::new_session()?;
let proxy = conn.with_proxy(
);
let _ = proxy.match_signal(
- |pc: PropertiesPropertiesChanged, _: &Connection, _: &Message| {
- println! {"dumping {:#x?}", pc.changed_properties["Metadata"]};
+ move |pc: PropertiesPropertiesChanged, _: &Connection, _: &Message| {
+ pc.changed_properties["Metadata"]
+ .0
+ .as_iter()
+ .and_then(|mut iter| {
+ // Iterating over a RefArg goes key, value, key, value... Insane honestly.
+ while let Some(key) = iter.next() {
+ let key_str = key.as_str()?;
+ let value = iter.next();
+
+ let arc_locked_xset_map = Arc::clone(&arc_locked_xset_map);
+
+ match key_str {
+ "xesam:artist" => {
+ // Variant holding a variant that should just be a Vec<&str> I
+ // believe. This is _the recommended_ way to do this by the author.
+ let inner_value = value?.as_iter()?.next()?.as_iter()?.next()?;
+ let artist = inner_value.as_str()?;
+ update_map(arc_locked_xset_map, "artist", artist);
+ }
+ "xesam:title" => {
+ let title = value?.as_iter()?.next()?.as_str()?;
+ update_map(arc_locked_xset_map, "title", title);
+ }
+ _ => (),
+ }
+ }
+ Some(())
+ });
true
},
);