Show remaining time to last deadline
authorEnrico Zini <enrico@enricozini.org>
Mon, 17 Aug 2009 17:12:25 +0000 (18:12 +0100)
committerEnrico Zini <enrico@enricozini.org>
Mon, 17 Aug 2009 17:12:25 +0000 (18:12 +0100)
src/app_main.vala

index fbe32a8f1b5974a0cb1f2ce29f2af9875a614651..db4bd580e6f925415117426e27dd0a84b56f45bd 100644 (file)
@@ -38,9 +38,18 @@ public class Clock : Gtk.VBox
     protected Gtk.Label l_date;
     protected Gtk.Label l_time;
     protected Gtk.Label l_deadline;
+    protected time_t last_time;
+    protected zavai.clock.SourceType last_time_type;
+    protected time_t last_deadline;
+    protected string last_deadline_label;
 
     public Clock()
     {
+        last_time = time_t();
+        last_time_type = zavai.clock.SourceType.SYSTEM;
+        last_deadline = 0;
+        last_deadline_label = "";
+
         l_date = new Gtk.Label("--");
         l_date.modify_font(Pango.FontDescription.from_string("Sans 40"));
         pack_start(l_date, false, false, 0);
@@ -55,25 +64,29 @@ public class Clock : Gtk.VBox
         zavai.clock.clock.schedule_changed += on_schedule_changed;
 
         zavai.clock.clock.request("ui.main.clock");
-
-        // Read the current schedule
-        on_schedule_changed();
     }
 
     private void on_schedule_changed()
     {
         zavai.clock.Alarm a = zavai.clock.clock.next_alarm();
         if (a == null)
-            l_deadline.set_text("");
+        {
+            last_deadline = 0;
+            last_deadline_label = "";
+        }
         else
         {
-            var t = Time.local(a.deadline);
-            l_deadline.set_text(a.label + ": " + t.format("%a %d %b %H:%M"));
+            last_deadline = a.deadline;
+            last_deadline_label = a.label;
         }
+        on_minute_changed((long)last_time, last_time_type);
     }
 
     private void on_minute_changed(long ts, zavai.clock.SourceType type)
     {
+        last_time = (time_t)ts;
+        last_time_type = type;
+
         string typetag = "unknown";
         switch (type)
         {
@@ -88,6 +101,19 @@ public class Clock : Gtk.VBox
         var t = Time.local((time_t)ts);
         l_date.set_text(t.format("%a %d %b"));
         l_time.set_text("%2d:%02d (%s)".printf(t.hour, t.minute, typetag));
+
+        if (last_deadline == 0)
+            l_deadline.set_text("");
+        else
+        {
+            int remaining = (int)(last_time - last_deadline);
+            int hours = remaining / 3600;
+            int minutes = (remaining % 3600) / 60;
+            if (hours == 0 && minutes == 0)
+                l_deadline.set_text(last_deadline_label + "\nanytime now");
+            else
+                l_deadline.set_text("%s\non %02dh %02dm".printf(last_deadline_label, hours, minutes));
+        }
     }
 }