]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-location-manager.c
Updated Basque language
[empathy.git] / libempathy-gtk / empathy-location-manager.c
index 27cfb4ee619925c606b5b3aa3647169aed6609c7..2018b5c5a569d3829bcb55374dd9fda277d218c0 100644 (file)
 
 #include "config.h"
 
+/* Needed for trunc */
+#define _ISOC9X_SOURCE 1
+#define _ISOC99_SOURCE 1
+#include <math.h>
 #include <string.h>
 #include <time.h>
 
@@ -63,7 +67,6 @@ typedef struct {
     GeoclueAddress *gc_address;
 
     gboolean reduce_accuracy;
-    gdouble reduce_value;
     EmpathyAccountManager *account_manager;
 
     /* The idle id for publish_on_idle func */
@@ -194,13 +197,13 @@ publish_location (EmpathyLocationManager *location_manager,
   if (!conn)
     return;
 
-  if (force_publication == FALSE)
+  if (!force_publication)
     {
       if (!empathy_conf_get_bool (conf, EMPATHY_PREFS_LOCATION_PUBLISH,
             &can_publish))
         return;
 
-      if (can_publish == FALSE)
+      if (!can_publish)
         return;
     }
 
@@ -251,7 +254,7 @@ new_connection_cb (EmpathyAccountManager *manager,
     TpConnection *conn,
     gpointer *location_manager)
 {
-  EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
+  EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
   DEBUG ("New connection %p", conn);
 
   /* Don't publish if it is already planned (ie startup) */
@@ -290,20 +293,30 @@ address_changed_cb (GeoclueAddress *address,
   GHashTableIter iter;
   gpointer key, value;
 
+  geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
   DEBUG ("New address (accuracy level %d):", level);
+  /* FIXME: Publish accuracy level also considering the position's */
 
-  geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
-  g_hash_table_remove_all (priv->location);
+  g_hash_table_remove (priv->location, EMPATHY_LOCATION_STREET);
+  g_hash_table_remove (priv->location, EMPATHY_LOCATION_AREA);
+  g_hash_table_remove (priv->location, EMPATHY_LOCATION_REGION);
+  g_hash_table_remove (priv->location, EMPATHY_LOCATION_COUNTRY);
+  g_hash_table_remove (priv->location, EMPATHY_LOCATION_COUNTRY_CODE);
+  g_hash_table_remove (priv->location, EMPATHY_LOCATION_POSTAL_CODE);
 
   if (g_hash_table_size (details) == 0)
-    return;
+    {
+      DEBUG ("\t - (Empty)");
+      return;
+    }
 
   g_hash_table_iter_init (&iter, details);
   while (g_hash_table_iter_next (&iter, &key, &value))
     {
       GValue *new_value;
       /* Discard street information if reduced accuracy is on */
-      if (priv->reduce_accuracy && strcmp (key, EMPATHY_LOCATION_STREET) == 0)
+      if (priv->reduce_accuracy &&
+          !tp_strdiff (key, EMPATHY_LOCATION_STREET))
         continue;
 
       new_value = tp_g_value_slice_new_string (value);
@@ -351,6 +364,7 @@ position_changed_cb (GeocluePosition *position,
   gdouble mean, horizontal, vertical;
   GValue *new_value;
 
+
   geoclue_accuracy_get_details (accuracy, &level, &horizontal, &vertical);
   DEBUG ("New position (accuracy level %d)", level);
   if (level == GEOCLUE_ACCURACY_LEVEL_NONE)
@@ -358,36 +372,61 @@ position_changed_cb (GeocluePosition *position,
 
   if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
     {
-      longitude += priv->reduce_value;
+
+      if (priv->reduce_accuracy)
+        /* Truncate at 1 decimal place */
+        longitude = trunc (longitude * 10.0) / 10.0;
+
       new_value = tp_g_value_slice_new_double (longitude);
       g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LON),
           new_value);
       DEBUG ("\t - Longitude: %f", longitude);
     }
+  else
+    {
+      g_hash_table_remove (priv->location, EMPATHY_LOCATION_LON);
+    }
+
   if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
     {
-      latitude += priv->reduce_value;
+      if (priv->reduce_accuracy)
+        /* Truncate at 1 decimal place */
+        latitude = trunc (latitude * 10.0) / 10.0;
+
       new_value = tp_g_value_slice_new_double (latitude);
-      g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LAT),
+      g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_LAT),
           new_value);
       DEBUG ("\t - Latitude: %f", latitude);
     }
+  else
+    {
+      g_hash_table_remove (priv->location, EMPATHY_LOCATION_LAT);
+    }
+
   if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
     {
       new_value = tp_g_value_slice_new_double (altitude);
-      g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_ALT),
+      g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_ALT),
           new_value);
       DEBUG ("\t - Altitude: %f", altitude);
     }
+  else
+    {
+      g_hash_table_remove (priv->location, EMPATHY_LOCATION_ALT);
+    }
 
   if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
     {
       mean = (horizontal + vertical) / 2.0;
       new_value = tp_g_value_slice_new_double (mean);
-      g_hash_table_insert (priv->location,
+      g_hash_table_replace (priv->location,
           g_strdup (EMPATHY_LOCATION_ACCURACY), new_value);
       DEBUG ("\t - Accuracy: %f", mean);
     }
+  else
+    {
+      g_hash_table_remove (priv->location, EMPATHY_LOCATION_ACCURACY);
+    }
 
   update_timestamp (location_manager);
   if (priv->timeout_id == 0)
@@ -424,17 +463,20 @@ update_resources (EmpathyLocationManager *location_manager)
 
   DEBUG ("Updating resources %d", priv->resources);
 
+  if (!priv->geoclue_is_setup)
+    return;
+
+  /* As per Geoclue bug #15126, using NONE results in no address
+   * being found as geoclue-manual report an empty address with
+   * accuracy = NONE */
   if (!geoclue_master_client_set_requirements (priv->gc_client,
-          GEOCLUE_ACCURACY_LEVEL_NONE, 0, TRUE, priv->resources,
+          GEOCLUE_ACCURACY_LEVEL_COUNTRY, 0, TRUE, priv->resources,
           NULL))
     {
       DEBUG ("set_requirements failed");
       return;
     }
 
-  if (!priv->geoclue_is_setup)
-    return;
-
   geoclue_address_get_address_async (priv->gc_address,
       initial_address_cb, location_manager);
   geoclue_position_get_position_async (priv->gc_position,
@@ -451,9 +493,16 @@ setup_geoclue (EmpathyLocationManager *location_manager)
 
   DEBUG ("Setting up Geoclue");
   master = geoclue_master_get_default ();
-  priv->gc_client = geoclue_master_create_client (master, NULL, NULL);
+  priv->gc_client = geoclue_master_create_client (master, NULL, &error);
   g_object_unref (master);
 
+  if (priv->gc_client == NULL)
+    {
+      DEBUG ("Failed to GeoclueMasterClient: %s", error->message);
+      g_error_free (error);
+      return;
+    }
+
   update_resources (location_manager);
 
   /* Get updated when the position is changes */
@@ -497,15 +546,15 @@ publish_cb (EmpathyConf *conf,
   DEBUG ("Publish Conf changed");
 
 
-  if (empathy_conf_get_bool (conf, key, &can_publish) == FALSE)
+  if (!empathy_conf_get_bool (conf, key, &can_publish))
     return;
 
-  if (can_publish == TRUE)
+  if (can_publish)
     {
-      if (priv->geoclue_is_setup == FALSE)
+      if (!priv->geoclue_is_setup)
         setup_geoclue (manager);
       /* if still not setup than the init failed */
-      if (priv->geoclue_is_setup == FALSE)
+      if (!priv->geoclue_is_setup)
         return;
 
       geoclue_address_get_address_async (priv->gc_address,
@@ -539,11 +588,11 @@ resource_cb (EmpathyConf  *conf,
   if (!empathy_conf_get_bool (conf, key, &resource_enabled))
     return;
 
-  if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK) == 0)
+  if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK))
     resource = GEOCLUE_RESOURCE_NETWORK;
-  if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL) == 0)
+  if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL))
     resource = GEOCLUE_RESOURCE_CELL;
-  if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS) == 0)
+  if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS))
     resource = GEOCLUE_RESOURCE_GPS;
 
   if (resource_enabled)
@@ -571,17 +620,6 @@ accuracy_cb (EmpathyConf  *conf,
     return;
   priv->reduce_accuracy = enabled;
 
-  if (enabled)
-    {
-      GRand *rand = g_rand_new_with_seed (time (NULL));
-      priv->reduce_value = g_rand_double_range (rand, -0.25, 0.25);
-      g_rand_free (rand);
-    }
-  else
-    {
-      priv->reduce_value = 0.0;
-    }
-
   if (!priv->geoclue_is_setup)
     return;
 
@@ -603,6 +641,12 @@ empathy_location_manager_init (EmpathyLocationManager *location_manager)
   priv->location = g_hash_table_new_full (g_direct_hash, g_direct_equal,
       g_free, (GDestroyNotify) tp_g_value_slice_free);
 
+  /* Setup account status callbacks */
+  priv->account_manager = empathy_account_manager_dup_singleton ();
+  g_signal_connect (priv->account_manager,
+    "new-connection",
+    G_CALLBACK (new_connection_cb), location_manager);
+
   /* Setup settings status callbacks */
   conf = empathy_conf_get ();
   empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_PUBLISH, publish_cb,
@@ -621,12 +665,6 @@ empathy_location_manager_init (EmpathyLocationManager *location_manager)
   resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS, location_manager);
   accuracy_cb (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY, location_manager);
   publish_cb (conf, EMPATHY_PREFS_LOCATION_PUBLISH, location_manager);
-
-  /* Setup account status callbacks */
-  priv->account_manager = empathy_account_manager_dup_singleton ();
-  g_signal_connect (priv->account_manager,
-    "new-connection",
-    G_CALLBACK (new_connection_cb), location_manager);
 }
 
 EmpathyLocationManager *