Listen for custom mail count signal
authorJacob Casper <dev@jacobcasper.com>
Fri, 30 Oct 2020 21:55:33 +0000 (16:55 -0500)
committerJacob Casper <dev@jacobcasper.com>
Fri, 6 Nov 2020 20:24:53 +0000 (14:24 -0600)
src/main.rs

index da31c89..0aaba48 100644 (file)
@@ -1,3 +1,4 @@
+use dbus::arg;
 use dbus::blocking::stdintf::org_freedesktop_dbus::PropertiesPropertiesChanged;
 use dbus::blocking::Connection;
 use dbus::message::Message;
@@ -5,6 +6,32 @@ use std::collections::HashMap;
 use std::sync::{Arc, Mutex};
 use std::time::Duration;
 
+// Custom signal type impl
+// Mostly copied from library examples
+#[derive(Debug)]
+pub struct ComJacobCasperMailUnreadCount {
+    pub count: u32,
+}
+
+impl arg::AppendAll for ComJacobCasperMailUnreadCount {
+    fn append(&self, iter: &mut arg::IterAppend) {
+        arg::RefArg::append(&self.count, iter);
+    }
+}
+
+impl arg::ReadAll for ComJacobCasperMailUnreadCount {
+    fn read(iter: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> {
+        Ok(ComJacobCasperMailUnreadCount {
+            count: iter.read()?,
+        })
+    }
+}
+
+impl dbus::message::SignalArgs for ComJacobCasperMailUnreadCount {
+    const NAME: &'static str = "UnreadCount";
+    const INTERFACE: &'static str = "com.jacobcasper.Mail";
+}
+
 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();
@@ -18,14 +45,32 @@ 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 mail_proxy = conn.with_proxy(
+        "com.jacobcasper.Mail",
+        "/com/jacobcasper/Mail/Unread",
+        Duration::from_millis(5000),
+    );
+
+    let mail_match_map = arc_locked_xset_map.clone();
+    let _ = mail_proxy.match_signal(
+        move |m: ComJacobCasperMailUnreadCount, _: &Connection, _: &Message| {
+            update_map(
+                mail_match_map.clone(),
+                "unread_count",
+                m.count.to_string().as_str(),
+            );
+            true
+        },
+    );
+
+    let spotify_proxy = conn.with_proxy(
         "org.mpris.MediaPlayer2.spotify",
         "/org/mpris/MediaPlayer2",
         Duration::from_millis(5000),
     );
 
     let spotify_match_map = arc_locked_xset_map.clone();
-    let _ = proxy.match_signal(
+    let _ = spotify_proxy.match_signal(
         move |pc: PropertiesPropertiesChanged, _: &Connection, _: &Message| {
             pc.changed_properties["Metadata"]
                 .0