GPS status icon synchronised with menu entry
authorEnrico Zini <enrico@enricozini.org>
Wed, 22 Jul 2009 16:06:12 +0000 (18:06 +0200)
committerEnrico Zini <enrico@enricozini.org>
Wed, 22 Jul 2009 16:06:12 +0000 (18:06 +0200)
vala/app.vala
vala/app_gps.vala

index 92220e27365f1a69b6b2f58be3828699e3234639..05e81667bcfa250b6a7ee5da9e33d30a017712ea 100644 (file)
@@ -200,9 +200,9 @@ stderr.printf("menu.add_applet.packpre me %s them %s\n", _label, target);
 stderr.printf("menu.add_applet.packpost me %s them %s\n", _label, target);
        }
 
-       public void add_service_toggle(string serviceName, string label_start, string label_stop)
+       public void add_service_toggle(string service_name, string label_start, string label_stop)
        {
-               pack_start(new ServiceRequestLink(serviceName, label_start, label_stop), false, false, 0);
+               pack_start(new ServiceRequestLink(service_name, label_start, label_stop), false, false, 0);
        }
 
        public void add_widget(Gtk.Widget w)
@@ -293,24 +293,23 @@ stderr.printf("push link: %s\n", _target);
 
 public class ServiceRequestLink : Gtk.ToggleButton
 {
-       protected string serviceName;
+       protected string service_name;
        protected string label_start;
        protected string label_stop;
 
        private void on_toggled(Gtk.Button src)
        {
-stderr.printf("toggle service request: service %s to %d\n", serviceName, (int)get_active());
-               Service s = zavai.registry.gets(serviceName);
+               Service s = zavai.registry.gets(service_name);
                if (get_active())
-stderr.printf(" service result: %d\n", (int)                   s.request("servicerequestlink"));
+                       s.request("servicerequestlink");
                else
-stderr.printf(" release result: %d\n", (int)                   s.release("servicerequestlink"));
+                       s.release("servicerequestlink");
                set_label(get_active() ? label_stop : label_start);
        }
 
-       public ServiceRequestLink(string serviceName, string label_start, string label_stop)
+       public ServiceRequestLink(string service_name, string label_start, string label_stop)
        {
-               this.serviceName = serviceName;
+               this.service_name = service_name;
                this.label_start = label_start;
                this.label_stop = label_stop;
                set_size_request(0, zavai.config.min_button_height);
index bb22edfb979af6e2c407347e583c69d64f545259..f414444eb7ce7ca795103d92d89b5c36cff4eb54 100644 (file)
@@ -147,53 +147,73 @@ class GPXAudioTracer(gtk.ToggleAction):
             self.recorder.stop()
 */
 
-public class GPSTrayIcon : Gtk.StatusIcon
+public class GPSRequestLink : Gtk.ToggleButton
 {
-    bool requested = false;
-    int fix_status = 0;
+       protected string service_name;
+       protected string label_start;
+       protected string label_stop;
+    protected Gtk.StatusIcon status_icon;
+    protected int fix_status = 0;
+
+       public GPSRequestLink()
+       {
+        service_name = "gps";
+        label_start = "Keep GPS on";
+        label_stop = "Stop keeping GPS on";
+               set_size_request(0, zavai.config.min_button_height);
+               toggled += on_toggled;
+
+               set_label(get_active() ? label_stop : label_start);
 
-    public string icon_fname()
-    {
-        if (fix_status == 2 || fix_status == 3)
-            return zavai.config.icondir + "/" + (requested ? "gps_fix_on.png" : "gps_fix_off.png");
-        else
-            return zavai.config.icondir + "/" + (requested ? "gps_nofix_on.png" : "gps_nofix_off.png");
-    }
-
-    public GPSTrayIcon()
-    {
         //tooltip = "GPS status";
-stderr.printf("ZAZAZA\n");
         try {
             fix_status = zavai.gps.gps.device.GetFixStatus();
         } catch (Error e) {
-stderr.printf("MRDMRDMRD\n");
+            fix_status = 0;
         }
-stderr.printf("NAME %s\n", icon_fname());
-        set_from_file(icon_fname());
-        activate += on_activate;
         zavai.gps.gps.device.FixStatusChanged += on_fix_status_changed;
+
+        // GPS status icon
+        status_icon = new Gtk.StatusIcon();
+        status_icon.set_visible(true);
+        status_icon.activate += on_status_activate;
+        update_icon();
+       }
+
+    protected void update_icon()
+    {
+        string name;
+        if (fix_status == 2 || fix_status == 3)
+            name = zavai.config.icondir + "/" + (get_active() ? "gps_fix_on.png" : "gps_fix_off.png");
+        else
+            name = zavai.config.icondir + "/" + (get_active() ? "gps_nofix_on.png" : "gps_nofix_off.png");
+stderr.printf("load icon from %s\n", name);
+        status_icon.set_from_file(name);
     }
 
-    void on_fix_status_changed(dynamic DBus.Object pos, int fix_status)
+    private void on_fix_status_changed(dynamic DBus.Object pos, int fix_status)
     {
         this.fix_status = fix_status;
-        set_from_file(icon_fname());
+        update_icon();
     }
 
-    void on_activate()
+       private void on_toggled(Gtk.Button src)
+       {
+               Service s = zavai.registry.gets(service_name);
+               if (get_active())
+                       s.request("servicerequestlink");
+               else
+                       s.release("servicerequestlink");
+               set_label(get_active() ? label_stop : label_start);
+        update_icon();
+       }
+
+    private void on_status_activate()
     {
-        if (!requested)
-            zavai.gps.gps.request("gpstrayicon");
-        else
-            zavai.gps.gps.release("gpstrayicon");
-        requested = !requested;
-        set_from_file(icon_fname());
+        set_active(!get_active());
     }
 }
 
-public GPSTrayIcon tray_icon = null;
-
 public void init()
 {
     /*
@@ -205,6 +225,7 @@ public void init()
     zavai.registry.register_applet("app.debug.useless", useless);
     */
     var menu_waypoint = new Waypoint();
+    var menu_gpsrequest = new GPSRequestLink();
                //label_on = "Stop GPX trace";
                //label_off = "Start GPX trace";
                //label_on = "Stop GPS monitor";
@@ -217,16 +238,12 @@ public void init()
     // Menus
     var menu_gps = new zavai.Menu("GPS");
     //menu_gps.add_applet("app.debug.useless");
-    menu_gps.add_service_toggle("gps", "Keep GPS on", "Stop keeping GPS on");
     menu_gps.add_service_toggle("gps.gpx", "Start GPX trace", "Stop GPX trace");
     menu_gps.add_widget(menu_waypoint);
+    menu_gps.add_widget(menu_gpsrequest);
 
     zavai.registry.register_menu("menu.gps", menu_gps);
     zavai.registry.getmenu("menu.main").add_applet("menu.gps");
-
-    // GPS status icon
-    tray_icon = new GPSTrayIcon();
-    tray_icon.set_visible(true);
 }
 
 }