Forked out a new Clock service
authorEnrico Zini <enrico@enricozini.org>
Mon, 17 Aug 2009 12:24:02 +0000 (13:24 +0100)
committerEnrico Zini <enrico@enricozini.org>
Mon, 17 Aug 2009 12:24:02 +0000 (13:24 +0100)
src/Makefile.am
src/app_main.vala
src/clock.vala [new file with mode: 0644]
src/zavai.vala

index a432d686e6138be5d1f8fe2b1eabc6ed3848145f..21eda5045a96207da9ed2b2d927e56376b4644d7 100644 (file)
@@ -34,6 +34,7 @@ zavai_VALASOURCES = \
        registry.vala \
        input.vala \
        gps.vala \
+       clock.vala \
        app.vala \
        app_keyboard.vala \
        app_power.vala \
index cfb4a72bbf00369b43a638821abc9d43aa04f687..1d2882d93c9227b069062b9885c6e6991cea3617 100644 (file)
@@ -38,13 +38,6 @@ public class Clock : Gtk.VBox
     protected Gtk.Label l_date;
     protected Gtk.Label l_time;
 
-    protected time_t last_gps_time;
-    protected time_t last_gps_time_system_time;
-    protected time_t last_system_time;
-       protected dynamic DBus.Object gps_time;
-    protected uint system_time_timeout;
-    protected time_t last_minute;
-
     public Clock()
     {
         l_date = new Gtk.Label("--");
@@ -54,62 +47,27 @@ public class Clock : Gtk.VBox
         l_time.modify_font(Pango.FontDescription.from_string("Sans 60"));
         pack_start(l_time, false, false, 0);
 
-        last_minute = 0;
-        last_gps_time = 0;
-        last_gps_time_system_time = 0;
-        last_system_time = time_t();
-        
-               gps_time = zavai.registry.sbus.get_object(
-                       "org.freesmartphone.ogpsd",
-                       "/org/freedesktop/Gypsy",
-                       "org.freedesktop.Gypsy.Time");
+        zavai.clock.clock.minute_changed += on_minute_changed;
 
-        system_time_timeout = Timeout.add(5000, on_system_time);
-               gps_time.TimeChanged += on_gps_time;
-        update_time();
+        zavai.clock.clock.request("ui.main.clock");
     }
 
-    public void update_time()
+    private void on_minute_changed(long ts, zavai.clock.SourceType type)
     {
-        time_t new_last_minute;
-        string type;
-        if (last_gps_time_system_time + 10 > last_system_time)
-        {
-            new_last_minute = last_gps_time / 60;
-            type = "gps";
-        }
-        else
+        string typetag = "unknown";
+        switch (type)
         {
-            new_last_minute = last_system_time / 60;
-            type = "sys";
+            case zavai.clock.SourceType.GPS:
+                typetag = "gps";
+                break;
+            case zavai.clock.SourceType.SYSTEM:
+                typetag = "sys";
+                break;
         }
-        if (new_last_minute != last_minute)
-        {
-            last_minute = new_last_minute;
-            var t = Time.local(last_minute * 60);
-            l_date.set_text(t.format("%a %d %b"));
-            l_time.set_text("%2d:%02d (%s)".printf(t.hour, t.minute, type));
-        }
-    }
-
-       private void on_gps_time(dynamic DBus.Object pos, int t)
-       {
-        if (t == 0)
-        {
-            last_gps_time_system_time = 0;
-            update_time();
-        } else {
-            last_gps_time = (time_t)t;
-            last_gps_time_system_time = time_t();
-            update_time();
-        }
-       }
-
-    private bool on_system_time()
-    {
-        last_system_time = time_t();
-        update_time();
-        return true;
+                
+        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));
     }
 }
 
diff --git a/src/clock.vala b/src/clock.vala
new file mode 100644 (file)
index 0000000..166d537
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ * clock - clock resource for zavai
+ *
+ * Copyright (C) 2009  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 clock {
+
+public enum SourceType
+{
+    SYSTEM,
+    GPS
+}
+
+public class Clock: zavai.Service
+{
+    protected time_t last_gps_time;
+    protected time_t last_gps_time_system_time;
+    protected time_t last_system_time;
+       protected dynamic DBus.Object gps_time;
+    protected uint system_time_timeout;
+    protected time_t last_minute;
+
+    // Ticks once a minute
+    public signal void minute_changed(long time, SourceType source);
+
+       public Clock()
+       {
+               name = "clock";
+
+        last_minute = 0;
+        last_gps_time = 0;
+        last_gps_time_system_time = 0;
+        last_system_time = time_t();
+        
+               gps_time = zavai.registry.sbus.get_object(
+                       "org.freesmartphone.ogpsd",
+                       "/org/freedesktop/Gypsy",
+                       "org.freedesktop.Gypsy.Time");
+       }
+
+       private void on_gps_time(dynamic DBus.Object pos, int t)
+       {
+        if (t == 0)
+        {
+            last_gps_time_system_time = 0;
+            update_time();
+        } else {
+            last_gps_time = (time_t)t;
+            last_gps_time_system_time = time_t();
+            update_time();
+        }
+       }
+
+    private bool on_system_time()
+    {
+        last_system_time = time_t();
+        update_time();
+        return true;
+    }
+
+    private void update_time()
+    {
+        time_t chosen;
+        SourceType type;
+        if (last_gps_time_system_time + 10 > last_system_time)
+        {
+            chosen = last_gps_time;
+            type = SourceType.GPS;
+        }
+        else
+        {
+            chosen = last_system_time;
+            type = SourceType.SYSTEM;
+        }
+        if (chosen / 60 != last_minute)
+        {
+            last_minute = chosen / 60;
+            minute_changed(chosen, type);
+        }
+    }
+
+       /// Request GPS resource
+       public override void start()
+       {
+               if (started) return;
+
+        system_time_timeout = Timeout.add(5000, on_system_time);
+               gps_time.TimeChanged += on_gps_time;
+        last_system_time = time_t();
+        update_time();
+
+               base.start();
+       }
+
+       public override void stop()
+       {
+               if (!started) return;
+
+        Source.remove(system_time_timeout);
+               gps_time.TimeChanged -= on_gps_time;
+
+               base.stop();
+       }
+}
+
+public Clock clock = null;
+
+public void init()
+{
+    clock = new Clock();
+
+       zavai.registry.register_service(clock);
+}
+
+}
+}
index f3ba837011d1c2fe29fe913a02333f53800397ca..9672ec97ea921ca33158ea48dd51725ebcea9199 100644 (file)
@@ -56,6 +56,7 @@ static int main (string[] args) {
     // Additional infrastructure
        zavai.input.init();
        zavai.gps.init();
+       zavai.clock.init();
 
        zavai.registry.register_menu("menu.main", new zavai.Menu("Main menu"));