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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//! The notification subsystem.
//!
//! This subsystem utilizes libnotify to send notifications as popups
//! to the desktop.


use app_state::*;
use audio::*;
use errors::*;
use glib::prelude::*;
use libnotify;
use prefs::*;
use std::cell::Cell;
use std::rc::Rc;



/// An expression of our notification system. Holds all the relevant information
/// needed by Gtk+ callbacks to interact with libnotify.
pub struct Notif {
    enabled: Cell<bool>,
    from_popup: Cell<bool>,
    from_tray: Cell<bool>,
    from_hotkeys: Cell<bool>,
    from_external: Cell<bool>,

    volume_notif: libnotify::Notification,
    text_notif: libnotify::Notification,
}

impl Notif {
    /// Create a new notification instance from the current preferences.
    /// This should only be done once at startup. This also initializes
    /// the libnotify system.
    pub fn new(prefs: &Prefs) -> Result<Self> {
        libnotify::init("PNMixer-rs")?;

        let notif = Notif {
            enabled: Cell::new(false),
            from_popup: Cell::new(false),
            from_tray: Cell::new(false),
            from_hotkeys: Cell::new(false),
            from_external: Cell::new(false),

            volume_notif: libnotify::Notification::new("", None, None),
            text_notif: libnotify::Notification::new("", None, None),
        };

        notif.reload(prefs);

        return Ok(notif);
    }

    /// Reload the notification instance from the current
    /// preferences.
    pub fn reload(&self, prefs: &Prefs) {
        let timeout = prefs.notify_prefs.notifcation_timeout;

        self.enabled.set(prefs.notify_prefs.enable_notifications);
        self.from_popup.set(prefs.notify_prefs.notify_popup);
        self.from_tray.set(prefs.notify_prefs.notify_mouse_scroll);
        self.from_hotkeys.set(prefs.notify_prefs.notify_hotkeys);
        self.from_external.set(prefs.notify_prefs.notify_external);

        self.volume_notif.set_timeout(timeout as i32);
        self.volume_notif.set_hint("x-canonical-private-synchronous",
                                   Some("".to_variant()));


        self.text_notif.set_timeout(timeout as i32);
        self.text_notif.set_hint("x-canonical-private-synchronous",
                                 Some("".to_variant()));
    }

    /// Shows a volume notification, e.g. for volume or mute state change.
    pub fn show_volume_notif(&self, audio: &Audio) -> Result<()> {
        let vol = audio.vol()?;
        let vol_level = audio.vol_level();

        let icon = {
            match vol_level {
                VolLevel::Muted => "audio-volume-muted",
                VolLevel::Off => "audio-volume-off",
                VolLevel::Low => "audio-volume-low",
                VolLevel::Medium => "audio-volume-medium",
                VolLevel::High => "audio-volume-high",
            }
        };

        let summary = {
            match vol_level {
                VolLevel::Muted => String::from("Volume muted"),
                _ => {
                    format!("{} ({})\nVolume: {}",
                            audio.acard
                                .borrow()
                                .card_name()?,
                            audio.acard
                                .borrow()
                                .chan_name()?,
                            vol as i32)
                }
            }
        };

        // TODO: error handling
        self.volume_notif.update(summary.as_str(), None, Some(icon)).unwrap();
        self.volume_notif.set_hint("value", Some((vol as i32).to_variant()));
        // TODO: error handling
        self.volume_notif.show().unwrap();

        return Ok(());
    }


    /// Shows a text notification, e.g. for warnings or errors.
    pub fn show_text_notif(&self, summary: &str, body: &str) -> Result<()> {
        // TODO: error handling
        self.text_notif.update(summary, Some(body), None).unwrap();
        // TODO: error handling
        self.text_notif.show().unwrap();

        return Ok(());
    }
}

impl Drop for Notif {
    fn drop(&mut self) {
        libnotify::uninit();
    }
}



/// Initialize the notification subsystem.
pub fn init_notify(appstate: Rc<AppS>) {
    {
        /* connect handler */
        let apps = appstate.clone();
        appstate.audio.connect_handler(Box::new(move |s, u| {
            let notif = &apps.notif;
            if notif.is_none() || !notif.as_ref().unwrap().enabled.get() {
                return;
            }
            let notif = notif.as_ref().unwrap();
            match (s,
                   u,
                   (notif.from_popup.get(),
                    notif.from_tray.get(),
                    notif.from_external.get(),
                    notif.from_hotkeys.get())) {
                (AudioSignal::NoCard, _, _) => try_w!(notif.show_text_notif("No sound card", "No playable soundcard found")),
                (AudioSignal::CardDisconnected, _, _) => try_w!(notif.show_text_notif("Soundcard disconnected", "Soundcard has been disconnected, reloading sound system...")),
                (AudioSignal::CardError, _, _) => (),
                (AudioSignal::ValuesChanged,
                 AudioUser::TrayIcon,
                 (_, true, _, _)) => try_w!(notif.show_volume_notif(&apps.audio)),
                (AudioSignal::ValuesChanged,
                 AudioUser::Popup,
                 (true, _, _, _)) => try_w!(notif.show_volume_notif(&apps.audio)),
                (AudioSignal::ValuesChanged,
                 AudioUser::Unknown,
                 (_, _, true, _)) => try_w!(notif.show_volume_notif(&apps.audio)),
                (AudioSignal::ValuesChanged,
                 AudioUser::Hotkeys,
                 (_, _, _, true)) => try_w!(notif.show_volume_notif(&apps.audio)),
                _ => (),
            }
        }));

    }
}