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
use std::process;
use std::thread;
use std::time::Duration;
use std::io;

use notifications::{Urgency, NotificationOptions};
use battery::Battery;
use events::BatteryEventSubscriber;
use system;

pub struct Notifier {}

impl Notifier {
    pub fn new() -> Self {
        Notifier {}
    }

    /// Show a desktop notification
    fn notify(&self,
              title: &str,
              text: &str,
              icon: &str,
              urgency: Urgency)
              -> io::Result<process::Child> {
        NotificationOptions::new()
            .title(title)
            .text(text)
            .icon(icon)
            .urgency(urgency)
            .show()
    }
}

impl BatteryEventSubscriber for Notifier {
    // TODO: Choose an icon corresponding to the current battery level
    fn battery_charging(&self, battery: &Battery) {
        NotificationOptions::new()
            .title("Батарея заряжается")
            .text(match battery.capacity() {
                Ok(capacity) => {
                    format!("Уровень заряда батареи: {}%.", capacity)
                }
                _ => String::new(),
            })
            .icon(match battery.capacity() {
                Ok(x) if x <= 5 => "battery-caution-charging",
                Ok(x) if x <= 50 => "battery-low-charging",
                Ok(x) if x <= 95 => "battery-good-charging",
                _ => "battery-full-charging",
            })
            .show()
            .ok();
    }

    fn battery_full(&self, _battery: &Battery) {
        NotificationOptions::new()
            .title("Батарея заряжена")
            .text("Отключите зарядное устройство от ноутбука.")
            .icon("battery-full-charged")
            .eternal()
            .show()
            .ok();
    }

    fn battery_discharging(&self, battery: &Battery) {
        NotificationOptions::new()
            .title("Батарея разряжается")
            .text(match battery.capacity() {
                Ok(capacity) => {
                    format!("Уровень заряда батареи: {}%.", capacity)
                }
                _ => String::new(),
            })
            .icon(match battery.capacity() {
                Ok(x) if x <= 5 => "battery-caution",
                Ok(x) if x <= 50 => "battery-low",
                Ok(x) if x <= 95 => "battery-good",
                _ => "battery-full",
            })
            .show()
            .ok();
    }

    fn battery_low(&self, battery: &Battery) {
        NotificationOptions::new()
            .title("Батарея разряжена")
            .text(match battery.capacity() {
                Ok(capacity) => format!("Уровень заряда батареи: {}%. <b>Подключите зарядное устройство.</b>", capacity),
                Err(_) => String::from("<b>Подключите зарядное устройство.</b>"),
            })
            .icon("battery-low")
            .eternal()
            .show().ok();
    }

    fn battery_critical(&self, battery: &Battery) {
        NotificationOptions::new()
            .title("Батарея полностью разряжена")
            .text("Через минуту компьютер будет переведён в спящий режим. \
                   <b>Подключите зарядное устройство.</b>")
            .icon("battery-caution")
            .urgency(Urgency::Critical)
            .eternal()
            .show().ok();

        info!("Battery critical, hibernating in 1 minute");

        for _ in 0..12 {
            thread::sleep(Duration::from_secs(5));
            if battery.is_charging() {
                info!("Hibernation cancelled");
                NotificationOptions::new()
                    .title("Гибернация отменена")
                    .icon("weather-clear")
                    .show()
                    .ok();
                return;
            }
        }

        NotificationOptions::new()
            .title("Батарея полностью разряжена")
            .text("Компьютер будет немедленно переведён в спящий режим.")
            .icon("weather-clear-night")
            .urgency(Urgency::Critical)
            .show().ok();

        info!("Hibernating");
        system::hibernate();
    }

    fn battery_missing(&self, _battery: &Battery) {
        self.notify("Батарея отключена",
                    "Не отключайте компьютер от сети электропитания.",
                    "battery-missing",
                    Urgency::Normal).ok();
    }
}