2017-07-14 15:23:42 +00:00
|
|
|
#![allow(missing_docs)]
|
|
|
|
|
|
|
|
//! The preferences subsystem.
|
|
|
|
//!
|
|
|
|
//! These are the global application preferences, which can be set
|
|
|
|
//! by the user. They read from a file in TOML format, presented
|
|
|
|
//! in the preferences dialog and saved back to the file on request.
|
|
|
|
|
|
|
|
|
2017-06-26 07:08:37 +00:00
|
|
|
use errors::*;
|
|
|
|
use std::fmt::Display;
|
|
|
|
use std::fmt::Formatter;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std;
|
|
|
|
use toml;
|
|
|
|
use which;
|
|
|
|
use xdg;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const VOL_CONTROL_COMMANDS: [&str; 3] =
|
|
|
|
["gnome-alsamixer", "xfce4-mixer", "alsamixergui"];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug, Serialize, Clone, Copy)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
2017-07-14 15:23:42 +00:00
|
|
|
/// When the tray icon is middle-clicked.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub enum MiddleClickAction {
|
|
|
|
ToggleMute,
|
|
|
|
ShowPreferences,
|
|
|
|
VolumeControl,
|
|
|
|
CustomCommand,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for MiddleClickAction {
|
|
|
|
fn default() -> MiddleClickAction {
|
|
|
|
return MiddleClickAction::ToggleMute;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<i32> for MiddleClickAction {
|
|
|
|
fn from(i: i32) -> Self {
|
|
|
|
match i {
|
|
|
|
0 => MiddleClickAction::ToggleMute,
|
|
|
|
1 => MiddleClickAction::ShowPreferences,
|
|
|
|
2 => MiddleClickAction::VolumeControl,
|
|
|
|
3 => MiddleClickAction::CustomCommand,
|
|
|
|
_ => MiddleClickAction::ToggleMute,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<MiddleClickAction> for i32 {
|
|
|
|
fn from(action: MiddleClickAction) -> Self {
|
|
|
|
match action {
|
|
|
|
MiddleClickAction::ToggleMute => 0,
|
|
|
|
MiddleClickAction::ShowPreferences => 1,
|
|
|
|
MiddleClickAction::VolumeControl => 2,
|
|
|
|
MiddleClickAction::CustomCommand => 3,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug, Serialize)]
|
|
|
|
#[serde(default)]
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Device preferences tab.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub struct DevicePrefs {
|
|
|
|
pub card: String,
|
2017-07-19 10:12:08 +00:00
|
|
|
pub channel: String,
|
2017-06-26 07:08:37 +00:00
|
|
|
// TODO: normalize volume?
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for DevicePrefs {
|
|
|
|
fn default() -> DevicePrefs {
|
|
|
|
return DevicePrefs {
|
2017-07-19 10:12:08 +00:00
|
|
|
card: String::from("(default)"),
|
|
|
|
channel: String::from("Master"),
|
|
|
|
};
|
2017-06-26 07:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug, Serialize)]
|
|
|
|
#[serde(default)]
|
2017-07-14 15:23:42 +00:00
|
|
|
/// View preferences tab.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub struct ViewPrefs {
|
|
|
|
pub draw_vol_meter: bool,
|
|
|
|
pub vol_meter_offset: i32,
|
|
|
|
pub system_theme: bool,
|
2017-07-19 10:12:08 +00:00
|
|
|
pub vol_meter_color: VolColor,
|
2017-06-26 07:08:37 +00:00
|
|
|
// TODO: Display text folume/text volume pos?
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ViewPrefs {
|
|
|
|
fn default() -> ViewPrefs {
|
|
|
|
return ViewPrefs {
|
2017-07-19 10:12:08 +00:00
|
|
|
draw_vol_meter: true,
|
|
|
|
vol_meter_offset: 10,
|
|
|
|
system_theme: true,
|
|
|
|
vol_meter_color: VolColor::default(),
|
|
|
|
};
|
2017-06-26 07:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug, Serialize)]
|
|
|
|
#[serde(default)]
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Volume color setting in the view preferences tab.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub struct VolColor {
|
|
|
|
pub red: f64,
|
|
|
|
pub green: f64,
|
|
|
|
pub blue: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for VolColor {
|
|
|
|
fn default() -> VolColor {
|
|
|
|
return VolColor {
|
2017-07-19 10:12:08 +00:00
|
|
|
red: 0.960784313725,
|
|
|
|
green: 0.705882352941,
|
|
|
|
blue: 0.0,
|
|
|
|
};
|
2017-06-26 07:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug, Serialize)]
|
|
|
|
#[serde(default)]
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Behavior preferences tab.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub struct BehaviorPrefs {
|
|
|
|
pub unmute_on_vol_change: bool,
|
|
|
|
pub vol_control_cmd: Option<String>,
|
|
|
|
pub vol_scroll_step: f64,
|
|
|
|
pub vol_fine_scroll_step: f64,
|
|
|
|
pub middle_click_action: MiddleClickAction,
|
2017-09-01 22:54:45 +00:00
|
|
|
pub custom_command: Option<String>,
|
2017-06-26 07:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for BehaviorPrefs {
|
|
|
|
fn default() -> BehaviorPrefs {
|
|
|
|
return BehaviorPrefs {
|
2017-07-19 10:12:08 +00:00
|
|
|
unmute_on_vol_change: true,
|
|
|
|
vol_control_cmd: None,
|
|
|
|
vol_scroll_step: 5.0,
|
|
|
|
vol_fine_scroll_step: 1.0,
|
|
|
|
middle_click_action: MiddleClickAction::default(),
|
|
|
|
custom_command: None,
|
|
|
|
};
|
2017-06-26 07:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "notify")]
|
|
|
|
#[derive(Deserialize, Debug, Serialize)]
|
|
|
|
#[serde(default)]
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Notifications preferences tab.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub struct NotifyPrefs {
|
|
|
|
pub enable_notifications: bool,
|
|
|
|
pub notifcation_timeout: i64,
|
|
|
|
pub notify_mouse_scroll: bool,
|
|
|
|
pub notify_popup: bool,
|
2017-07-13 22:27:46 +00:00
|
|
|
pub notify_external: bool,
|
|
|
|
pub notify_hotkeys: bool,
|
2017-06-26 07:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "notify")]
|
|
|
|
impl Default for NotifyPrefs {
|
|
|
|
fn default() -> NotifyPrefs {
|
|
|
|
return NotifyPrefs {
|
2017-07-19 10:12:08 +00:00
|
|
|
enable_notifications: true,
|
|
|
|
notifcation_timeout: 1500,
|
|
|
|
notify_mouse_scroll: true,
|
|
|
|
notify_popup: true,
|
|
|
|
notify_external: true,
|
|
|
|
notify_hotkeys: true,
|
|
|
|
};
|
2017-06-26 07:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-13 22:27:46 +00:00
|
|
|
#[derive(Deserialize, Debug, Serialize, Default)]
|
|
|
|
/// Hotkey preferences.
|
|
|
|
/// The `String`s represent gtk accelerator strings.
|
|
|
|
pub struct HotkeyPrefs {
|
|
|
|
pub enable_hotkeys: bool,
|
|
|
|
pub mute_unmute_key: Option<String>,
|
|
|
|
pub vol_up_key: Option<String>,
|
|
|
|
pub vol_down_key: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-26 07:08:37 +00:00
|
|
|
#[derive(Deserialize, Debug, Serialize, Default)]
|
|
|
|
#[serde(default)]
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Main preferences struct, holding all sub-preferences.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub struct Prefs {
|
|
|
|
pub device_prefs: DevicePrefs,
|
|
|
|
pub view_prefs: ViewPrefs,
|
|
|
|
pub behavior_prefs: BehaviorPrefs,
|
|
|
|
#[cfg(feature = "notify")]
|
2017-07-13 22:27:46 +00:00
|
|
|
pub notify_prefs: NotifyPrefs,
|
|
|
|
pub hotkey_prefs: HotkeyPrefs,
|
2017-06-26 07:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Prefs {
|
|
|
|
pub fn new() -> Result<Prefs> {
|
|
|
|
let m_config_file = get_xdg_dirs().find_config_file("pnmixer.toml");
|
|
|
|
match m_config_file {
|
|
|
|
Some(c) => {
|
|
|
|
debug!("Config file present at {:?}, using it.", c);
|
|
|
|
|
|
|
|
let mut f = File::open(c)?;
|
|
|
|
let mut buffer = vec![];
|
|
|
|
f.read_to_end(&mut buffer)?;
|
|
|
|
|
|
|
|
let prefs = toml::from_slice(buffer.as_slice())?;
|
|
|
|
|
|
|
|
return Ok(prefs);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
debug!("No config file present, creating one with defaults.");
|
|
|
|
|
|
|
|
let prefs = Prefs::default();
|
|
|
|
prefs.store_config()?;
|
|
|
|
|
|
|
|
return Ok(prefs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-13 22:27:46 +00:00
|
|
|
// TODO: unused
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Reload the current preferences from the config file.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub fn reload_config(&mut self) -> Result<()> {
|
|
|
|
debug!("Reloading config...");
|
|
|
|
|
|
|
|
let new_prefs = Prefs::new()?;
|
|
|
|
*self = new_prefs;
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Store the current preferences to the config file.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub fn store_config(&self) -> Result<()> {
|
2017-07-19 10:12:08 +00:00
|
|
|
let config_path = get_xdg_dirs()
|
|
|
|
.place_config_file("pnmixer.toml")
|
2017-07-16 21:28:21 +00:00
|
|
|
.chain_err(|| {
|
2017-07-19 10:12:08 +00:00
|
|
|
format!(
|
|
|
|
"Could not create config directory at {:?}",
|
|
|
|
get_xdg_dirs().get_config_home()
|
|
|
|
)
|
|
|
|
})?;
|
2017-06-26 07:08:37 +00:00
|
|
|
|
|
|
|
debug!("Storing config in {:?}", config_path);
|
|
|
|
|
2017-07-19 10:12:08 +00:00
|
|
|
let mut f = File::create(config_path.clone()).chain_err(|| {
|
|
|
|
format!(
|
|
|
|
"Could not open/create config file {:?} for writing",
|
|
|
|
config_path
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
f.write_all(self.to_str().as_bytes()).chain_err(|| {
|
|
|
|
format!("Could not write to config file {:?}", config_path)
|
|
|
|
})?;
|
2017-06-26 07:08:37 +00:00
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Conver the current preferences to a viewable String.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub fn to_str(&self) -> String {
|
|
|
|
return toml::to_string(self).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Get an available volume control command, which must exist in `$PATH`.
|
|
|
|
/// Tries hard to fine one,
|
|
|
|
/// starting with the given preference setting and falling back to the
|
|
|
|
/// `VOL_CONTROL_COMMANDS` slice.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub fn get_avail_vol_control_cmd(&self) -> Option<String> {
|
|
|
|
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 {
|
2017-07-19 10:12:08 +00:00
|
|
|
fn fmt(
|
|
|
|
&self,
|
|
|
|
f: &mut Formatter,
|
|
|
|
) -> std::result::Result<(), std::fmt::Error> {
|
2017-06-26 07:08:37 +00:00
|
|
|
let s = self.to_str();
|
|
|
|
return write!(f, "{}", s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Get the set of XDG directories, relative to our project.
|
2017-06-26 07:08:37 +00:00
|
|
|
fn get_xdg_dirs() -> xdg::BaseDirectories {
|
|
|
|
return xdg::BaseDirectories::with_prefix("pnmixer-rs").unwrap();
|
|
|
|
}
|