]> ToastFreeware Gitweb - gregoa/zavai.git/blobdiff - src/app_notify.vala
Merge branch 'master' into gregoa
[gregoa/zavai.git] / src / app_notify.vala
diff --git a/src/app_notify.vala b/src/app_notify.vala
new file mode 100644 (file)
index 0000000..804277d
--- /dev/null
@@ -0,0 +1,202 @@
+/*
+ * app_notify - zavai notification system
+ *
+ * Copyright (C) 2010  Enrico Zini <enrico@enricozini.org>
+ *
+ * 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 notify {
+
+public abstract class Notifier : Gtk.Dialog
+{
+    public string notifier_name { get; construct; }
+
+    construct {
+        set_transient_for(zavai.app);
+        set_modal(true);
+        set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
+    }
+
+    protected virtual bool push_aux_state()
+    {
+        return false;
+    }
+
+    protected virtual bool push_vibrator_state()
+    {
+        return false;
+    }
+
+    protected virtual bool push_ringtone_state()
+    {
+        return false;
+    }
+
+    public int run()
+    {
+        ui.power.backlight.request(notifier_name);
+
+        // Setup our attention seeking strategy
+        bool has_aux = zavai.led.aux != null && push_aux_state();
+        bool has_vibrator = zavai.led.vibrator != null && push_vibrator_state();
+        bool has_ringtone = zavai.audio.soundplayer != null && push_ringtone_state();
+
+        // Run dialog
+        int res = base.run();
+
+        if (has_ringtone) zavai.audio.soundplayer.pop_state(notifier_name);
+        if (has_vibrator) zavai.led.vibrator.pop_state(notifier_name);
+        if (has_aux) zavai.led.aux.pop_state(notifier_name);
+
+        ui.power.backlight.release(notifier_name);
+        return res;
+    }
+}
+
+protected class CallNotifierDialog : ui.notify.Notifier
+{
+    protected Gtk.Label message;
+    protected Gtk.Button message_button;
+
+    public CallNotifierDialog(string title, string text)
+    {
+        Object(
+            notifier_name: "call",
+            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_call, true);
+        zavai.audio.soundplayer.push_state(state);
+        return true;
+    }
+}
+
+public class CallNotifier : zavai.Resource, ui.notify.Notifier
+{
+    protected int call_index = -1;
+    protected CallNotifierDialog dialog = null;
+
+    public CallNotifier()
+    {
+        zavai.registry.register(this);
+
+        zavai.gsm.gsm.new_call += trigger;
+        zavai.gsm.gsm.end_call += done;
+    }
+
+    public void trigger(int index)
+    {
+        call_index = index;
+        dialog = new CallNotifierDialog("Call", "TODO");
+
+        if (zavai.led.auxbutton != null)
+            zavai.led.auxbutton.event += on_auxbutton;
+
+        int res = dialog.run();
+        switch (res)
+        {
+            case Gtk.ResponseType.OK:
+                // TODO clock.alarm_trigger_queue.ack(current);
+                break;
+            case Gtk.ResponseType.CANCEL:
+                // TODO clock.alarm_trigger_queue.cancel(current);
+                break;
+        }
+
+        if (zavai.led.auxbutton != null)
+            zavai.led.auxbutton.event -= on_auxbutton;
+
+        dialog.destroy();
+        dialog = null;
+        call_index = -1;
+    }
+
+    protected bool on_auxbutton(ulong time, bool pressed)
+    {
+        if (dialog == null) return false;
+        dialog.response(Gtk.ResponseType.OK);
+        return true;
+    }
+
+    public void done(int index)
+    {
+        if (call_index != index || dialog == null) return;
+        dialog.response(Gtk.ResponseType.NONE);
+    }
+
+    public void shutdown()
+    {
+        if (dialog == null) return;
+        dialog.response(Gtk.ResponseType.CANCEL);
+    }
+}
+
+public CallNotifier call = null;
+
+public void sms()
+{
+    var ps = new zavai.audio.PlayerState("gsm", zavai.config.ringtone_sms, false);
+    zavai.audio.soundplayer.push_state(ps);
+}
+
+public void init()
+{
+    call = new CallNotifier();
+
+    zavai.gsm.gsm.new_sms += sms;
+}
+
+}
+}
+}