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 --- Cargo.lock | 38 ++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 57 +++++++++++++++++++++++++++++---------------------------- src/message.rs | 9 +++++++++ 4 files changed, 77 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6d0a73f..fb839df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,7 @@ name = "rust-pushbullet" version = "0.1.0" dependencies = [ + "notify-rust 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)", "websocket 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -16,6 +17,11 @@ dependencies = [ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ansi_term" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "bitflags" version = "0.3.2" @@ -26,6 +32,16 @@ name = "byteorder" version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "clap" +version = "1.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ansi_term 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "cookie" version = "0.1.21" @@ -37,6 +53,14 @@ dependencies = [ "url 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "dbus" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "gcc" version = "0.3.18" @@ -133,6 +157,15 @@ dependencies = [ "log 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "notify-rust" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "clap 1.4.5 (registry+https://github.com/rust-lang/crates.io-index)", + "dbus 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "num_cpus" version = "0.2.6" @@ -200,6 +233,11 @@ dependencies = [ "log 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "strsim" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "tempdir" version = "0.3.4" diff --git a/Cargo.toml b/Cargo.toml index c46b3b9..7ecd3f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ authors = ["David Li "] websocket = "~0.12.2" rustc-serialize = "0.3" toml = "0.1" +notify-rust = "3.0" 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