pnmixer-rust/src/app_state.rs

163 lines
4.4 KiB
Rust
Raw Normal View History

2017-07-14 15:23:42 +00:00
//! Global application state.
2017-07-19 10:12:08 +00:00
use audio::alsa::backend::*;
2017-10-10 06:43:08 +00:00
use audio::pulseaudio::*;
2017-07-19 10:12:08 +00:00
use audio::frontend::*;
2017-06-26 07:08:37 +00:00
use errors::*;
use gtk;
use hotkeys::Hotkeys;
2017-06-26 07:08:37 +00:00
use prefs::*;
use std::cell::RefCell;
use std::rc::Rc;
2017-07-19 10:12:08 +00:00
use support::audio::*;
use ui::entry::Gui;
2017-06-26 07:08:37 +00:00
#[cfg(feature = "notify")]
use notif::*;
// TODO: destructors
2017-07-14 15:23:42 +00:00
/// The global application state struct.
pub struct AppS<T>
2017-07-19 10:12:08 +00:00
where
T: AudioFrontend,
{
2017-06-26 07:08:37 +00:00
_cant_construct: (),
2017-07-14 15:23:42 +00:00
/// Mostly static GUI state.
2017-06-26 07:08:37 +00:00
pub gui: Gui,
2017-07-14 15:23:42 +00:00
/// Audio state.
pub audio: Rc<T>,
2017-07-14 15:23:42 +00:00
/// Preferences state.
2017-06-26 07:08:37 +00:00
pub prefs: RefCell<Prefs>,
#[cfg(feature = "notify")]
/// Notification state. In case of initialization failure, this
/// is set to `None`.
pub notif: Option<Notif>,
/// Hotkey state.
pub hotkeys: RefCell<Box<Hotkeys<T>>>, // Gets an Rc to Audio.
2017-06-26 07:08:37 +00:00
}
/// Create a new application state using the `AlsaBackend`.
pub fn new_alsa_appstate() -> AppS<AlsaBackend> {
let prefs = RefCell::new(unwrap_error!(Prefs::new(), None));
2017-07-19 10:12:08 +00:00
let card_name = prefs.borrow().device_prefs.card.clone();
let chan_name = prefs.borrow().device_prefs.channel.clone();
let audio = Rc::new(unwrap_error!(
AlsaBackend::new(Some(card_name), Some(chan_name)),
None
));
return AppS::new(prefs, audio);
}
2017-10-10 06:43:08 +00:00
/// Create a new application state using the `PABackend`.
pub fn new_pa_appstate() -> AppS<PABackend> {
let prefs = RefCell::new(unwrap_error!(Prefs::new(), None));
let card_name = prefs.borrow().device_prefs.card.clone();
let chan_name = prefs.borrow().device_prefs.channel.clone();
let audio = Rc::new(unwrap_error!(
PABackend::new(Some(card_name), Some(chan_name)),
None
));
return AppS::new(prefs, audio);
}
impl<T> AppS<T>
2017-07-19 10:12:08 +00:00
where
T: AudioFrontend,
{
2017-07-14 15:23:42 +00:00
/// Create an application state instance. There should really only be one.
pub fn new(prefs: RefCell<Prefs>, audio: Rc<T>) -> Self {
2017-06-26 07:08:37 +00:00
let builder_popup_window =
2017-07-19 10:12:08 +00:00
gtk::Builder::new_from_string(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/data/ui/popup-window.glade"
)));
2017-06-26 07:08:37 +00:00
let builder_popup_menu =
2017-07-19 10:12:08 +00:00
gtk::Builder::new_from_string(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/data/ui/popup-menu.glade"
)));
2017-06-26 07:08:37 +00:00
// TODO: better error handling
#[cfg(feature = "notify")]
let notif = result_warn!(Notif::new(&prefs.borrow()), None).ok();
2017-07-19 10:12:08 +00:00
let hotkeys =
unwrap_error!(
wresult_warn!(Hotkeys::new(&prefs.borrow(), audio.clone()), None),
None
);
let gui =
Gui::new(builder_popup_window, builder_popup_menu, &prefs.borrow());
2017-06-26 07:08:37 +00:00
return AppS {
2017-07-19 10:12:08 +00:00
_cant_construct: (),
gui,
audio,
prefs,
2017-07-19 11:09:04 +00:00
#[cfg(feature = "notify")]
2017-07-19 10:12:08 +00:00
notif,
hotkeys: RefCell::new(hotkeys),
};
2017-06-26 07:08:37 +00:00
}
/* some functions that need to be easily accessible */
2017-07-14 15:23:42 +00:00
/// Update the tray icon state.
2017-06-26 07:08:37 +00:00
pub fn update_tray_icon(&self) -> Result<()> {
debug!("Update tray icon!");
2017-07-19 10:12:08 +00:00
return self.gui.tray_icon.update_all(
&self.prefs.borrow(),
self.audio.as_ref(),
None,
);
2017-06-26 07:08:37 +00:00
}
2017-07-14 15:23:42 +00:00
/// Update the Popup Window state.
2017-06-26 07:08:37 +00:00
pub fn update_popup_window(&self) -> Result<()> {
debug!("Update PopupWindow!");
return self.gui.popup_window.update(self.audio.as_ref());
2017-06-26 07:08:37 +00:00
}
#[cfg(feature = "notify")]
2017-07-14 15:23:42 +00:00
/// Update the notification state.
pub fn update_notify(&self) {
match self.notif {
Some(ref n) => n.reload(&self.prefs.borrow()),
2017-07-19 10:12:08 +00:00
None => {
warn!("Notification system not unitialized, skipping update")
}
}
2017-06-26 07:08:37 +00:00
}
#[cfg(not(feature = "notify"))]
2017-07-14 15:23:42 +00:00
/// Update the notification state.
pub fn update_notify(&self) {}
2017-06-26 07:08:37 +00:00
2017-07-14 15:23:42 +00:00
/// Update the audio state.
2017-06-26 07:08:37 +00:00
pub fn update_audio(&self, user: AudioUser) -> Result<()> {
return audio_reload(self.audio.as_ref(), &self.prefs.borrow(), user);
2017-06-26 07:08:37 +00:00
}
2017-07-14 15:23:42 +00:00
/// Update the config file.
2017-06-26 07:08:37 +00:00
pub fn update_config(&self) -> Result<()> {
let prefs = self.prefs.borrow_mut();
return prefs.store_config();
}
/// Update hotkey state.
pub fn update_hotkeys(&self) -> Result<()> {
let prefs = self.prefs.borrow();
return self.hotkeys.borrow_mut().reload(&prefs);
}
2017-06-26 07:08:37 +00:00
}