pnmixer-rust/src/app_state.rs

93 lines
2.6 KiB
Rust
Raw Normal View History

2017-07-10 19:27:57 +00:00
use audio::{Audio, AudioUser};
2017-07-07 10:03:53 +00:00
use errors::*;
2017-07-05 22:14:24 +00:00
use gtk;
2017-07-06 22:28:55 +00:00
use prefs::*;
2017-07-05 22:14:24 +00:00
use std::cell::RefCell;
2017-07-10 19:27:57 +00:00
use support_audio::*;
2017-07-07 10:03:53 +00:00
use ui_entry::Gui;
2017-07-02 16:11:56 +00:00
2017-07-10 19:07:59 +00:00
#[cfg(feature = "notify")]
use notif::*;
2017-06-27 22:23:12 +00:00
2017-07-07 10:03:53 +00:00
2017-06-28 15:53:19 +00:00
2017-06-30 15:24:26 +00:00
// TODO: destructors
2017-06-27 22:23:12 +00:00
pub struct AppS {
2017-07-07 10:03:53 +00:00
_cant_construct: (),
2017-06-30 15:24:26 +00:00
pub gui: Gui,
2017-07-01 22:03:21 +00:00
pub audio: Audio,
2017-07-05 22:14:24 +00:00
pub prefs: RefCell<Prefs>,
2017-07-10 19:07:59 +00:00
#[cfg(feature = "notify")]
pub notif: Notif,
2017-06-30 15:24:26 +00:00
}
impl AppS {
pub fn new() -> AppS {
2017-07-07 20:10:45 +00:00
let builder_popup_window =
2017-07-08 00:38:49 +00:00
gtk::Builder::new_from_string(include_str!(concat!(env!("CARGO_MANIFEST_DIR"),
"/data/ui/popup-window.glade")));
let builder_popup_menu =
gtk::Builder::new_from_string(include_str!(concat!(env!("CARGO_MANIFEST_DIR"),
"/data/ui/popup-menu.glade")));
2017-07-05 22:14:24 +00:00
let prefs = RefCell::new(Prefs::new().unwrap());
let gui =
Gui::new(builder_popup_window, builder_popup_menu, &prefs.borrow());
2017-07-02 17:08:17 +00:00
2017-07-08 00:38:49 +00:00
let card_name = prefs.borrow()
.device_prefs
.card
.clone();
let chan_name = prefs.borrow()
.device_prefs
.channel
.clone();
2017-07-10 19:07:59 +00:00
// TODO: better error handling
#[cfg(feature = "notify")]
let notif = Notif::new(&prefs.borrow()).unwrap();
2017-07-08 22:14:49 +00:00
2017-06-30 15:24:26 +00:00
return AppS {
2017-07-07 20:10:45 +00:00
_cant_construct: (),
2017-07-08 22:14:49 +00:00
gui,
2017-07-08 00:38:49 +00:00
audio: Audio::new(Some(card_name), Some(chan_name)).unwrap(),
2017-07-08 22:14:49 +00:00
prefs,
2017-07-10 19:07:59 +00:00
#[cfg(feature = "notify")]
notif,
2017-07-07 20:10:45 +00:00
};
2017-06-30 15:24:26 +00:00
}
2017-07-07 15:00:04 +00:00
/* some functions that need to be easily accessible */
2017-07-07 10:03:53 +00:00
pub fn update_tray_icon(&self) -> Result<()> {
debug!("Update tray icon!");
2017-07-07 20:10:45 +00:00
return self.gui.tray_icon.update_all(&self.prefs.borrow(),
&self.audio,
None);
2017-06-30 15:24:26 +00:00
}
2017-07-06 22:28:55 +00:00
2017-07-07 10:03:53 +00:00
pub fn update_popup_window(&self) -> Result<()> {
debug!("Update PopupWindow!");
return self.gui.popup_window.update(&self.audio);
2017-07-06 14:53:19 +00:00
}
2017-07-10 19:27:57 +00:00
#[cfg(feature = "notify")]
pub fn update_notify(&self) -> Result<()> {
return self.notif.reload(&self.prefs.borrow());
}
#[cfg(not(feature = "notify"))]
pub fn update_notify(&self) -> Result<()> {
return Ok(());
}
pub fn update_audio(&self, user: AudioUser) -> Result<()> {
return audio_reload(&self.audio, &self.prefs.borrow(), user);
}
pub fn update_config(&self) -> Result<()> {
let prefs = self.prefs.borrow_mut();
return prefs.store_config();
}
2017-07-07 15:00:04 +00:00
}