Notify calls and SMSes
authorEnrico Zini <enrico@enricozini.org>
Sun, 28 Mar 2010 15:41:25 +0000 (16:41 +0100)
committerEnrico Zini <enrico@enricozini.org>
Sun, 28 Mar 2010 15:41:25 +0000 (16:41 +0100)
src/app_notify.vala
src/gsm.vala

index 2a4b14c52fa852d95aa0661f90db6d9742b1de55..dd8d9e11f2dfc6528a53949bc812c7c6ef9795cc 100644 (file)
@@ -69,8 +69,131 @@ public abstract class Notifier : Gtk.Dialog
     }
 }
 
+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;
 }
 
 }
index ed9c60b9c9835466b5ca34e15ed999474e59f047..de96ee389dd51c508680d0df58837fab083dcdfb 100644 (file)
@@ -138,6 +138,9 @@ public class GSM: zavai.ScriptMonitorService
 
     public signal void status_changed(string message);
     public signal void info_changed();
+    public signal void new_call(int index);
+    public signal void end_call(int index);
+    public signal void new_sms();
 
     protected void dump_table(HashTable<string, Value?> vals)
     {
@@ -342,12 +345,15 @@ stderr.printf("ACQUIRE SIG %d\n", info_signal_strength);
 
     public void on_call_status(int index, string status, HashTable<string, Value?> properties)
     {
+        bool is_new = false;
+        bool is_done = false;
         stderr.printf("CALL STATUS %d %s\n", index, status);
         dump_table(properties);
 
         CallInfo? info = lookup_call(index);
         if (info == null)
         {
+            is_new = true;
             Value? v = properties.lookup("peer");
             string title;
             if (v != null)
@@ -380,6 +386,7 @@ stderr.printf("ACQUIRE SIG %d\n", info_signal_strength);
                     calls.delete_link(i);
                     break;
                 }
+            is_done = true;
         }
 
         /*
@@ -399,6 +406,11 @@ stderr.printf("ACQUIRE SIG %d\n", info_signal_strength);
             os.system("vibrator-stop")
             os.system("ledctrl --off gta02-aux:red");
         */
+
+        if (is_new && !is_done)
+            new_call(index);
+        if (is_done)
+            end_call(index);
     }
 
     public async void sms_log_and_delete(int idx)
@@ -421,6 +433,9 @@ stderr.printf("ACQUIRE SIG %d\n", info_signal_strength);
 
         // Delete message
         yield sim.delete_message(idx);
+
+        // Notify reception of the sms
+        new_sms();
     }
 }