diff --git a/Cargo.toml b/Cargo.toml index bfed3a539..149ead04c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ toml = "^0.4.2" serde_derive = "^1.0.9" serde = "^1.0.9" xdg = "*" +which = "*" [dependencies.gtk] git = "https://github.com/gtk-rs/gtk.git" diff --git a/src/main.rs b/src/main.rs index c3d39b73a..c9812daec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,7 @@ extern crate gobject_sys; extern crate gtk; extern crate gtk_sys; extern crate libc; +extern crate which; extern crate xdg; use std::rc::Rc; @@ -65,10 +66,10 @@ fn main() { let apps = Rc::new(AppS::new()); let mut prefs = prefs::Prefs::new().unwrap(); - println!("Channel: {:?}", prefs.to_str()); + println!("Channel: {}", prefs.to_str()); prefs.store_config(); - // println!("Channel: {:?}", prefs.to_str()); + println!("Control_cmd: {:?}", prefs.get_avail_vol_control_cmd()); ui_entry::init(apps); diff --git a/src/prefs.rs b/src/prefs.rs index 13f25ec58..0638e5b33 100644 --- a/src/prefs.rs +++ b/src/prefs.rs @@ -1,8 +1,12 @@ use errors::*; -use toml; -use xdg; +use std::fmt::Display; +use std::fmt::Formatter; use std::fs::File; use std::io::prelude::*; +use std; +use toml; +use which; +use xdg; @@ -28,17 +32,6 @@ impl Default for MiddleClickAction { } -#[derive(Deserialize, Debug, Serialize, Default)] -#[serde(default)] -pub struct Prefs { - pub device_prefs: DevicePrefs, - pub view_prefs: ViewPrefs, - pub behavior_prefs: BehaviorPrefs, - pub notify_prefs: NotifyPrefs, - // TODO: HotKeys? -} - - #[derive(Deserialize, Debug, Serialize)] #[serde(default)] pub struct DevicePrefs { @@ -101,7 +94,7 @@ impl Default for VolColor { #[derive(Deserialize, Debug, Serialize)] #[serde(default)] pub struct BehaviorPrefs { - vol_control_cmd: Option, + pub vol_control_cmd: Option, pub vol_scroll_step: f64, pub middle_click_action: MiddleClickAction, // TODO: fine scroll step? @@ -142,8 +135,15 @@ impl Default for NotifyPrefs { } - - +#[derive(Deserialize, Debug, Serialize, Default)] +#[serde(default)] +pub struct Prefs { + pub device_prefs: DevicePrefs, + pub view_prefs: ViewPrefs, + pub behavior_prefs: BehaviorPrefs, + pub notify_prefs: NotifyPrefs, + // TODO: HotKeys? +} impl Prefs { pub fn new() -> Result { @@ -201,12 +201,32 @@ impl Prefs { } - // TODO: implement - pub fn vol_control_cmd() -> String { - return String::from(""); + pub fn get_avail_vol_control_cmd(&self) -> Option { + match self.behavior_prefs.vol_control_cmd { + Some(ref c) => return Some(c.clone()), + None => { + for command in VOL_CONTROL_COMMANDS.iter() { + if which::which(command).is_ok() { + return Some(String::from(*command)); + } + } + } + } + + return None; } } +impl Display for Prefs { + fn fmt(&self, + f: &mut Formatter) + -> std::result::Result<(), std::fmt::Error> { + let s = self.to_str(); + return write!(f, "{}", s); + } +} + + fn get_xdg_dirs() -> xdg::BaseDirectories { return xdg::BaseDirectories::with_prefix("pnmixer-rs").unwrap(); }