Move GPS code to Azimuth
[azimuth] / src / azimuth.c
index 57d3a76..392805c 100644 (file)
@@ -25,6 +25,8 @@
 #include <gconf/gconf-client.h>
 #include <telepathy-glib/util.h>
 
+#include <location/location-gpsd-control.h>
+
 #include "azimuth.h"
 #include "azimuth-gconf.h"
 #include "position-publisher.h"
@@ -39,11 +41,33 @@ struct _AzimuthPrivate
   GMainLoop *loop;
   PositionPublisher *publisher;
   GConfClient *gconf;
+  LocationGPSDControl *gps_control;
 };
 
 #define AZIMUTH_GET_PRIVATE(o)     (G_TYPE_INSTANCE_GET_PRIVATE ((o), AZIMUTH_TYPE, AzimuthPrivate))
 
 static void
+update_gps (Azimuth *self)
+{
+  AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
+  gboolean start_gps;
+
+  start_gps = gconf_client_get_bool (priv->gconf,
+      AZIMUTH_GCONF_KEY_START_GPS, NULL);
+
+  if (priv->publisher != NULL && start_gps)
+    {
+      g_print ("starting GPS\n");
+      location_gpsd_control_start (priv->gps_control);
+    }
+  else
+    {
+      g_print ("stopping GPS\n");
+      location_gpsd_control_stop (priv->gps_control);
+    }
+}
+
+static void
 enabled_changed (Azimuth *self,
     gboolean enabled)
 {
@@ -51,15 +75,11 @@ enabled_changed (Azimuth *self,
 
   if (enabled)
     {
-      gboolean start_gps;
       if (priv->publisher != NULL)
         return;
 
-      start_gps = gconf_client_get_bool (priv->gconf,
-          AZIMUTH_GCONF_KEY_START_GPS, NULL);
-      g_print ("enable publishing (start gps: %s)\n",
-          start_gps ? "yes" : "no");
-      priv->publisher = position_publisher_new (TRUE, start_gps);
+      g_print ("enable publishing\n");
+      priv->publisher = position_publisher_new (TRUE);
     }
   else
     {
@@ -70,19 +90,8 @@ enabled_changed (Azimuth *self,
       g_object_unref (priv->publisher);
       priv->publisher = NULL;
     }
-}
 
-static void
-start_gps_changed (Azimuth *self,
-    gboolean start_gps)
-{
-  AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
-
-  if (priv->publisher)
-    {
-      g_print ("%s GPS\n", start_gps ? "Start" : "Stop");
-      g_object_set (priv->publisher, "start-gps", start_gps, NULL);
-    }
+  update_gps (self);
 }
 
 static void
@@ -106,9 +115,7 @@ gconf_notification_cb (GConfClient *client,
   if (!tp_strdiff (key, AZIMUTH_GCONF_KEY_START_GPS) &&
       value->type == GCONF_VALUE_BOOL)
     {
-      gboolean start_gps = gconf_value_get_bool (value);
-
-      start_gps_changed (self, start_gps);
+      update_gps (self);
     }
 }
 
@@ -127,6 +134,14 @@ azimuth_init (Azimuth *self)
 
   gconf_client_notify_add (priv->gconf, AZIMUTH_GCONF_SECTION,
       gconf_notification_cb, self, NULL, NULL);
+
+  /* GPS controller */
+  priv->gps_control = location_gpsd_control_get_default();
+
+  g_object_set (G_OBJECT(priv->gps_control),
+    "preferred-method", LOCATION_METHOD_USER_SELECTED,
+    "preferred-interval", LOCATION_INTERVAL_120S,
+    NULL);
 }
 
 static void
@@ -153,6 +168,13 @@ azimuth_dispose (GObject *object)
       priv->gconf = NULL;
     }
 
+  if (priv->gps_control != NULL)
+    {
+      location_gpsd_control_stop (priv->gps_control);
+      g_object_unref (priv->gps_control);
+      priv->gps_control = NULL;
+    }
+
   if (G_OBJECT_CLASS (azimuth_parent_class)->dispose)
     G_OBJECT_CLASS (azimuth_parent_class)->dispose (object);
 }
@@ -179,18 +201,16 @@ azimuth_run (Azimuth *self)
 {
   AzimuthPrivate *priv = AZIMUTH_GET_PRIVATE (self);
   gboolean enabled;
-  gboolean start_gps;
 
   enabled = gconf_client_get_bool (priv->gconf, AZIMUTH_GCONF_KEY_ENABLED,
       NULL);
-  start_gps = gconf_client_get_bool (priv->gconf, AZIMUTH_GCONF_KEY_START_GPS,
-      NULL);
   if (enabled)
     {
-      g_print ("publishing is enabled (start gps: %s)\n",
-          start_gps ? "yes" : "no");
+      g_print ("publishing is enabled\n");
       g_assert (priv->publisher == NULL);
-      priv->publisher = position_publisher_new (TRUE, start_gps);
+      priv->publisher = position_publisher_new (TRUE);
+
+      update_gps (self);
     }
   else
     {