aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Li <li.davidm96@gmail.com>2015-10-25 10:12:35 -0400
committerDavid Li <li.davidm96@gmail.com>2015-10-25 10:12:35 -0400
commit28b395cfd770785641c49f98cc9491210271db46 (patch)
tree38776725d968491ab148a800b675f3fbd2913bb8 /src
parent31be915ab71dcab004b64b313b20188af85c5d24 (diff)
Use notify-rust for notifications
Diffstat (limited to 'src')
-rw-r--r--src/main.rs57
-rw-r--r--src/message.rs9
2 files changed, 38 insertions, 28 deletions
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<Message> {
+ let json = json::Json::from_str(message).unwrap();
+ let mut decoder = json::Decoder::new(json);
+ let result : Result<Message, _> = Decodable::decode(&mut decoder);
+ result.ok()
+ }
+}
+
impl Decodable for Push {
fn decode<D: Decoder>(d: &mut D) -> Result<Push, D::Error> {
d.read_struct("root", 0, |d| {