Show next deadline
[gregoa/zavai.git] / src / clock.vala
index 083b566529fc88da258c6d083a0786f8f6493388..394ff29e08e5622fe55859e394a526b52be3d138 100644 (file)
@@ -48,6 +48,13 @@ private int alarm_compare(void* a, void* b)
     return (int)(((Alarm*)a)->deadline - ((Alarm*)b)->deadline);
 }
 
+[DBus (name = "org.freesmartphone.Notification")]
+public class AlarmNotification : Object {
+    public void Alarm () {
+        clock.check_alarms();
+    }
+}
+
 public class Clock: zavai.Service
 {
     protected time_t last_gps_time;
@@ -58,6 +65,7 @@ public class Clock: zavai.Service
     protected time_t last_minute;
     protected time_t chosen_time;
     protected SourceType chosen_type;
+    protected AlarmNotification listener;
 
        protected dynamic DBus.Object otimed_alarm;
        protected dynamic DBus.Object notification;
@@ -65,11 +73,13 @@ public class Clock: zavai.Service
 
     // Ticks once a minute
     public signal void minute_changed(long time, SourceType source);
+    public signal void schedule_changed();
 
        public Clock()
        {
                name = "clock";
         alarms = null;
+        listener = new AlarmNotification();
         last_minute = 0;
         last_gps_time = 0;
         last_gps_time_system_time = 0;
@@ -86,36 +96,50 @@ public class Clock: zavai.Service
                        "org.freesmartphone.otimed",
                        "/org/freesmartphone/Time/Alarm",
                        "org.freesmartphone.Time.Alarm");
-        notification = zavai.registry.sbus.get_object(
-            "org.freesmartphone",
-            "org/freesmartphone/Notification",
-            "org.freesmartphone.Notification");
-        notification.Alarm += on_alarm;
+        if (otimed_alarm == null)
+            zavai.log.error("ALARM IS NULL");
+
+        zavai.registry.sbus.register_object("/", listener);
+
+        //notification = zavai.registry.sbus.get_object(
+        //    "org.freesmartphone",
+        //    "org/freesmartphone/Notification",
+        //    "org.freesmartphone.Notification");
+        //notification.Alarm += on_alarm;
        }
 
+    public Alarm? next_alarm()
+    {
+        if (alarms == null)
+            return null;
+        return alarms.data;
+    }
+
     public void schedule(Alarm a)
     {
         alarms.insert_sorted(a, alarm_compare);
-        zavai.log.info("Next alarm: " + alarms.data.label + " at " + Time.local(alarms.data.deadline).to_string());
         otimed_reschedule();
     }
 
     private void otimed_reschedule()
     {
+        zavai.log.info("No alarms left to reschedule");
         if (alarms != null)
         {
+            zavai.log.info("Scheduling next alarm: " + alarms.data.label + " at " + Time.local(alarms.data.deadline).to_string());
+            
+            try {
+                otimed_alarm.ClearAlarm(zavai.registry.bus_name);
+            } catch (Error e) {
+                zavai.log.error("Cannot clear alarms: " + e.message);
+            }
             try {
-                otimed_alarm.ClearAlarm((string)null);
-                otimed_alarm.SetAlarm((string)null, alarms.data.deadline);
+                otimed_alarm.SetAlarm(zavai.registry.bus_name, (int)alarms.data.deadline);
             } catch (Error e) {
                 zavai.log.error("Cannot reschedule alarms: " + e.message);
             }
         }
-    }
-
-    private void on_alarm()
-    {
-        check_alarms();
+        schedule_changed();
     }
 
     public void check_alarms()