/* * app_alarm - zavai alarm system * * Copyright (C) 2010 Enrico Zini * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ using GLib; namespace zavai { namespace ui { namespace alarm { /* // Compute a-b in microseconds static long timediff(Posix.timeval* a, Posix.timeval* b) { return (a->tv_sec - b->tv_sec) * 1000000 + (a->tv_usec - b->tv_usec); } */ protected class AlarmNotifierDialog : ui.notify.Notifier { protected Gtk.Label message; protected Gtk.Button message_button; public AlarmNotifierDialog(string title, string text) { Object( notifier_name: "alarm", title: title ); set_size_request(300, 500); add_button(Gtk.STOCK_OK, Gtk.ResponseType.OK); add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL); set_default_response(Gtk.ResponseType.OK); weak Gtk.VBox vbox = (Gtk.VBox)get_content_area(); message = new Gtk.Label(text); message_button = new Gtk.Button(); message_button.set_image(message); message_button.clicked += (b) => { response(Gtk.ResponseType.OK); }; vbox.pack_start(message_button, true, true, 0); message_button.show(); } protected override bool push_aux_state() { var state = new zavai.led.LedState(notifier_name); state.set_blink(255); zavai.led.aux.push_state(state); return true; } protected override bool push_vibrator_state() { var state = new zavai.led.LedState(notifier_name); state.set_blink(255); zavai.led.vibrator.push_state(state); return true; } protected override bool push_ringtone_state() { var state = new zavai.audio.PlayerState(notifier_name, config.ringtone_alarm, true); zavai.audio.soundplayer.push_state(state); return true; } } public class AlarmNotifier : zavai.Resource, Object { protected weak clock.AlarmTriggerInfo current = null; protected AlarmNotifierDialog dialog = null; public AlarmNotifier() { zavai.registry.register(this); clock.alarm_trigger_queue.triggered += on_trigger; clock.alarm_trigger_queue.acked += on_done; clock.alarm_trigger_queue.canceled += on_done; } public void on_trigger(clock.AlarmTriggerInfo info) { current = info; dialog = new AlarmNotifierDialog("Alarm", info.label); if (zavai.led.auxbutton != null) zavai.led.auxbutton.event += on_auxbutton; uint cancel_timeout = 0; cancel_timeout = Timeout.add(30 * 1000, () => { dialog.response(Gtk.ResponseType.CANCEL); cancel_timeout = 0; return false; }); int res = dialog.run(); if (cancel_timeout != 0) Source.remove(cancel_timeout); switch (res) { case Gtk.ResponseType.OK: clock.alarm_trigger_queue.ack(current); break; case Gtk.ResponseType.CANCEL: clock.alarm_trigger_queue.cancel(current); break; } if (zavai.led.auxbutton != null) zavai.led.auxbutton.event -= on_auxbutton; dialog.destroy(); dialog = null; current = null; } protected bool on_auxbutton(ulong time, bool pressed) { if (dialog == null) return false; dialog.response(Gtk.ResponseType.OK); return true; } public void on_done(clock.AlarmTriggerInfo info) { if (current == null || dialog == null || current != info) return; dialog.response(Gtk.ResponseType.NONE); } public void shutdown() { if (dialog == null) return; dialog.response(Gtk.ResponseType.CANCEL); } } public AlarmNotifier notifier = null; public void init() { if (clock.alarm_trigger_queue != null) notifier = new AlarmNotifier(); } } } }