From 28b395cfd770785641c49f98cc9491210271db46 Mon Sep 17 00:00:00 2001
From: David Li 
Date: Sun, 25 Oct 2015 10:12:35 -0400
Subject: Use notify-rust for notifications
---
 src/main.rs    | 57 +++++++++++++++++++++++++++++----------------------------
 src/message.rs |  9 +++++++++
 2 files changed, 38 insertions(+), 28 deletions(-)
(limited to 'src')
diff --git a/src/main.rs b/src/main.rs
index ddf4feb..d904f6a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,12 +1,11 @@
-use std::process::Command;
 use std::fs::File;
 use std::io::prelude::*;
 
+extern crate notify_rust;
 extern crate toml;
 extern crate websocket;
 extern crate rustc_serialize;
-use rustc_serialize::{Decodable};
-use rustc_serialize::json::{Json, Decoder};
+use notify_rust::Notification;
 
 mod message;
 use message::{Message, Push};
@@ -16,11 +15,18 @@ fn main() {
     use websocket::client::request::Url;
     use websocket::Client;
 
-    let mut cfg_file = File::open("config.toml").unwrap();
+    let mut cfg_file = File::open("config.toml").expect("Could not find config.toml.");
     let mut s = String::new();
-    cfg_file.read_to_string(&mut s);
+    cfg_file.read_to_string(&mut s).expect("Could not read config.toml");
     let cfg = toml::Parser::new(s.as_ref()).parse().unwrap();
-    let token = cfg.get("pushbullet").unwrap().as_table().unwrap().get("token").expect("Could not find 'token' in config.toml").as_str().unwrap();
+    let cfg_pb = cfg.get("pushbullet")
+        .expect("Could not find [pushbullet] section in config.")
+        .as_table()
+        .expect("Config should contain a [pushbullet] section.");
+    let token = cfg_pb.get("token")
+        .expect("[pushbullet] section should contain 'token'")
+        .as_str()
+        .expect("'token' should be a string");
 
     let wss_url = format!("wss://stream.pushbullet.com/websocket/{}", token);
     let url = Url::parse(wss_url.as_ref()).unwrap();
@@ -44,28 +50,23 @@ fn main() {
                 return;
             }
             websocket::Message::Text(message) => {
-                let json = Json::from_str(message.as_ref()).unwrap();
-                let mut decoder = Decoder::new(json);
-                let msg = Decodable::decode(&mut decoder).unwrap();
-                println!("Got {:?}", msg);
-                match msg {
-                    Message::Nop => {}
-                    Message::Push(Push::Mirror {
-                        notification_id,
-                        title,
-                        body,
-                        application_name,
-                        ..
-                    }) => {
-                        let _ = Command::new("notify-send")
-                            .arg(title)
-                            .arg(body)
-                            .arg("-t")
-                            .arg("8000")
-                            .status().unwrap();
-                    }
-                    Message::Push(Push::Dismissal { notification_id }) => {
-                        
+                let msg = Message::parse(message.as_ref());
+                if let Some(msg) = msg {
+                    match msg {
+                        Message::Push(Push::Mirror {
+                            title,
+                            body,
+                            application_name,
+                            ..
+                        }) => {
+                            let title = format!("{}: {}", application_name, title);
+                            Notification::new()
+                                .body(body.as_ref())
+                                .summary(title.as_ref())
+                                .show()
+                                .unwrap();
+                        }
+                        _ => {}
                     }
                 }
             }
diff --git a/src/message.rs b/src/message.rs
index c0a766c..0617c4d 100644
--- a/src/message.rs
+++ b/src/message.rs
@@ -23,6 +23,15 @@ pub enum Message {
     Push(Push)
 }
 
+impl Message {
+    pub fn parse(message: &str) -> Option {
+        let json = json::Json::from_str(message).unwrap();
+        let mut decoder = json::Decoder::new(json);
+        let result : Result = Decodable::decode(&mut decoder);
+        result.ok()
+    }
+}
+
 impl Decodable for Push {
     fn decode(d: &mut D) -> Result {
         d.read_struct("root", 0, |d| {
-- 
cgit v1.2.3