1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! Global application state.


use audio::{Audio, AudioUser};
use errors::*;
use gtk;
use hotkeys::Hotkeys;
use prefs::*;
use std::cell::RefCell;
use std::rc::Rc;
use support_audio::*;
use ui_entry::Gui;

#[cfg(feature = "notify")]
use notif::*;



// TODO: destructors
/// The global application state struct.
pub struct AppS {
    _cant_construct: (),
    /// Mostly static GUI state.
    pub gui: Gui,
    /// Audio state.
    pub audio: Rc<Audio>,
    /// Preferences state.
    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>>, // Gets an Rc to Audio.
}


impl AppS {
    /// Create an application state instance. There should really only be one.
    pub fn new() -> AppS {
        let builder_popup_window =
            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")));
        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();
        // TODO: better error handling
        #[cfg(feature = "notify")]
        let notif = result_warn!(Notif::new(&prefs.borrow()), None).ok();

        let audio = Rc::new(unwrap_error!(Audio::new(Some(card_name),
                                                     Some(chan_name)),
                                          None));
        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());

        return AppS {
                   _cant_construct: (),
                   gui,
                   audio: audio,
                   prefs,
                   #[cfg(feature = "notify")]
                   notif,
                   hotkeys: RefCell::new(hotkeys),
               };
    }


    /* some functions that need to be easily accessible */

    /// Update the tray icon state.
    pub fn update_tray_icon(&self) -> Result<()> {
        debug!("Update tray icon!");
        return self.gui.tray_icon.update_all(&self.prefs.borrow(),
                                             &self.audio,
                                             None);
    }

    /// Update the Popup Window state.
    pub fn update_popup_window(&self) -> Result<()> {
        debug!("Update PopupWindow!");
        return self.gui.popup_window.update(&self.audio);
    }

    #[cfg(feature = "notify")]
    /// Update the notification state.
    pub fn update_notify(&self) {
        match self.notif {
            Some(ref n) => n.reload(&self.prefs.borrow()),
            None => warn!("Notification system not unitialized, skipping update"),
        }
    }

    #[cfg(not(feature = "notify"))]
    /// Update the notification state.
    pub fn update_notify(&self) {}

    /// Update the audio state.
    pub fn update_audio(&self, user: AudioUser) -> Result<()> {
        return audio_reload(&self.audio, &self.prefs.borrow(), user);
    }

    /// Update the config file.
    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);
    }
}