]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-location-manager.c
Merge branch 'sasl'
[empathy.git] / libempathy-gtk / empathy-location-manager.c
index 2018b5c5a569d3829bcb55374dd9fda277d218c0..1c09c358c0cec9602464353b865a10f48174fcf1 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>
 
 #include <glib/gi18n.h>
 
+#include <telepathy-glib/account-manager.h>
 #include <telepathy-glib/util.h>
 
 #include <geoclue/geoclue-master.h>
 #include <extensions/extensions.h>
 
 #include "empathy-location-manager.h"
-#include "empathy-conf.h"
 
-#include "libempathy/empathy-account-manager.h"
 #include "libempathy/empathy-enum-types.h"
+#include "libempathy/empathy-gsettings.h"
 #include "libempathy/empathy-location.h"
-#include "libempathy/empathy-tp-contact-factory.h"
 #include "libempathy/empathy-utils.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
@@ -61,13 +56,15 @@ typedef struct {
      */
     GHashTable *location;
 
+    GSettings *gsettings_loc;
+
     GeoclueResourceFlags resources;
     GeoclueMasterClient *gc_client;
     GeocluePosition *gc_position;
     GeoclueAddress *gc_address;
 
     gboolean reduce_accuracy;
-    EmpathyAccountManager *account_manager;
+    TpAccountManager *account_manager;
 
     /* The idle id for publish_on_idle func */
     guint timeout_id;
@@ -102,6 +99,8 @@ static void
 location_manager_dispose (GObject *object)
 {
   EmpathyLocationManagerPriv *priv = GET_PRIV (object);
+  void (*dispose) (GObject *) =
+    G_OBJECT_CLASS (empathy_location_manager_parent_class)->dispose;
 
   if (priv->account_manager != NULL)
   {
@@ -109,6 +108,12 @@ location_manager_dispose (GObject *object)
     priv->account_manager = NULL;
   }
 
+  if (priv->gsettings_loc != NULL)
+  {
+    g_object_unref (priv->gsettings_loc);
+    priv->gsettings_loc = NULL;
+  }
+
   if (priv->gc_client != NULL)
   {
     g_object_unref (priv->gc_client);
@@ -133,7 +138,8 @@ location_manager_dispose (GObject *object)
     priv->location = NULL;
   }
 
-  G_OBJECT_CLASS (empathy_location_manager_parent_class)->finalize (object);
+  if (dispose != NULL)
+    dispose (object);
 }
 
 static void
@@ -184,26 +190,30 @@ empathy_location_manager_class_init (EmpathyLocationManagerClass *class)
 }
 
 static void
-publish_location (EmpathyLocationManager *location_manager,
+publish_location_cb (TpConnection *connection,
+                     const GError *error,
+                     gpointer user_data,
+                     GObject *weak_object)
+{
+  if (error != NULL)
+      DEBUG ("Error setting location: %s", error->message);
+}
+
+static void
+publish_location (EmpathyLocationManager *self,
     TpConnection *conn,
     gboolean force_publication)
 {
-  EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
+  EmpathyLocationManagerPriv *priv = GET_PRIV (self);
   guint connection_status = -1;
-  gboolean can_publish;
-  EmpathyConf *conf = empathy_conf_get ();
-  EmpathyTpContactFactory *factory;
 
   if (!conn)
     return;
 
   if (!force_publication)
     {
-      if (!empathy_conf_get_bool (conf, EMPATHY_PREFS_LOCATION_PUBLISH,
-            &can_publish))
-        return;
-
-      if (!can_publish)
+      if (!g_settings_get_boolean (priv->gsettings_loc,
+            EMPATHY_PREFS_LOCATION_PUBLISH))
         return;
     }
 
@@ -216,26 +226,61 @@ publish_location (EmpathyLocationManager *location_manager,
       (g_hash_table_size (priv->location) == 0 ? "empty" : ""),
       conn);
 
-  factory = empathy_tp_contact_factory_dup_singleton (conn);
-  empathy_tp_contact_factory_set_location (factory, priv->location);
-  g_object_unref (factory);
+  tp_cli_connection_interface_location_call_set_location (conn, -1,
+      priv->location, publish_location_cb, NULL, NULL, G_OBJECT (self));
 }
 
+typedef struct
+{
+  EmpathyLocationManager *self;
+  gboolean force_publication;
+} PublishToAllData;
+
 static void
-publish_to_all_connections (EmpathyLocationManager *location_manager,
-    gboolean force_publication)
+publish_to_all_am_prepared_cb (GObject *source_object,
+    GAsyncResult *result,
+    gpointer user_data)
 {
-  EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
-  GList *connections = NULL, *l;
+  TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
+  PublishToAllData *data = user_data;
+  GList *accounts, *l;
+  GError *error = NULL;
+
+  if (!tp_account_manager_prepare_finish (manager, result, &error))
+    {
+      DEBUG ("Failed to prepare account manager: %s", error->message);
+      g_error_free (error);
+      goto out;
+    }
 
-  connections = empathy_account_manager_dup_connections (priv->account_manager);
-  for (l = connections; l; l = l->next)
+  accounts = tp_account_manager_get_valid_accounts (manager);
+  for (l = accounts; l; l = l->next)
     {
-      publish_location (location_manager, l->data, force_publication);
-      g_object_unref (l->data);
+      TpConnection *conn = tp_account_get_connection (TP_ACCOUNT (l->data));
+
+      if (conn != NULL)
+        publish_location (data->self, conn, data->force_publication);
     }
-  g_list_free (connections);
+  g_list_free (accounts);
+
+out:
+  g_object_unref (data->self);
+  g_slice_free (PublishToAllData, data);
+}
+
+static void
+publish_to_all_connections (EmpathyLocationManager *self,
+    gboolean force_publication)
+{
+  EmpathyLocationManagerPriv *priv = GET_PRIV (self);
+  PublishToAllData *data;
+
+  data = g_slice_new0 (PublishToAllData);
+  data->self = g_object_ref (self);
+  data->force_publication = force_publication;
 
+  tp_account_manager_prepare_async (priv->account_manager, NULL,
+      publish_to_all_am_prepared_cb, data);
 }
 
 static gboolean
@@ -250,25 +295,33 @@ publish_on_idle (gpointer user_data)
 }
 
 static void
-new_connection_cb (EmpathyAccountManager *manager,
-    TpConnection *conn,
-    gpointer *location_manager)
+new_connection_cb (TpAccount *account,
+    guint old_status,
+    guint new_status,
+    guint reason,
+    gchar *dbus_error_name,
+    GHashTable *details,
+    gpointer *self)
 {
-  EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
+  EmpathyLocationManagerPriv *priv = GET_PRIV (self);
+  TpConnection *conn;
+
+  conn = tp_account_get_connection (account);
+
   DEBUG ("New connection %p", conn);
 
   /* Don't publish if it is already planned (ie startup) */
   if (priv->timeout_id == 0)
     {
-      publish_location (EMPATHY_LOCATION_MANAGER (location_manager), conn,
+      publish_location (EMPATHY_LOCATION_MANAGER (self), conn,
           FALSE);
     }
 }
 
 static void
-update_timestamp (EmpathyLocationManager *location_manager)
+update_timestamp (EmpathyLocationManager *self)
 {
-  EmpathyLocationManagerPriv *priv= GET_PRIV (location_manager);
+  EmpathyLocationManagerPriv *priv= GET_PRIV (self);
   GValue *new_value;
   gint64 stamp64;
   time_t timestamp;
@@ -286,10 +339,10 @@ address_changed_cb (GeoclueAddress *address,
                     int timestamp,
                     GHashTable *details,
                     GeoclueAccuracy *accuracy,
-                    gpointer location_manager)
+                    gpointer self)
 {
   GeoclueAccuracyLevel level;
-  EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
+  EmpathyLocationManagerPriv *priv = GET_PRIV (self);
   GHashTableIter iter;
   gpointer key, value;
 
@@ -325,9 +378,9 @@ address_changed_cb (GeoclueAddress *address,
       DEBUG ("\t - %s: %s", (gchar *) key, (gchar *) value);
     }
 
-  update_timestamp (location_manager);
+  update_timestamp (self);
   if (priv->timeout_id == 0)
-    priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, location_manager);
+    priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, self);
 }
 
 static void
@@ -336,7 +389,7 @@ initial_address_cb (GeoclueAddress *address,
                     GHashTable *details,
                     GeoclueAccuracy *accuracy,
                     GError *error,
-                    gpointer location_manager)
+                    gpointer self)
 {
   if (error)
     {
@@ -345,7 +398,7 @@ initial_address_cb (GeoclueAddress *address,
     }
   else
     {
-      address_changed_cb (address, timestamp, details, accuracy, location_manager);
+      address_changed_cb (address, timestamp, details, accuracy, self);
     }
 }
 
@@ -357,9 +410,9 @@ position_changed_cb (GeocluePosition *position,
                      double longitude,
                      double altitude,
                      GeoclueAccuracy *accuracy,
-                     gpointer location_manager)
+                     gpointer self)
 {
-  EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
+  EmpathyLocationManagerPriv *priv = GET_PRIV (self);
   GeoclueAccuracyLevel level;
   gdouble mean, horizontal, vertical;
   GValue *new_value;
@@ -375,7 +428,7 @@ position_changed_cb (GeocluePosition *position,
 
       if (priv->reduce_accuracy)
         /* Truncate at 1 decimal place */
-        longitude = trunc (longitude * 10.0) / 10.0;
+        longitude = ((int) (longitude * 10)) / 10.0;
 
       new_value = tp_g_value_slice_new_double (longitude);
       g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LON),
@@ -391,7 +444,7 @@ position_changed_cb (GeocluePosition *position,
     {
       if (priv->reduce_accuracy)
         /* Truncate at 1 decimal place */
-        latitude = trunc (latitude * 10.0) / 10.0;
+        latitude = ((int) (latitude * 10)) / 10.0;
 
       new_value = tp_g_value_slice_new_double (latitude);
       g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_LAT),
@@ -428,9 +481,9 @@ position_changed_cb (GeocluePosition *position,
       g_hash_table_remove (priv->location, EMPATHY_LOCATION_ACCURACY);
     }
 
-  update_timestamp (location_manager);
+  update_timestamp (self);
   if (priv->timeout_id == 0)
-    priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, location_manager);
+    priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, self);
 }
 
 static void
@@ -442,7 +495,7 @@ initial_position_cb (GeocluePosition *position,
                      double altitude,
                      GeoclueAccuracy *accuracy,
                      GError *error,
-                     gpointer location_manager)
+                     gpointer self)
 {
   if (error)
     {
@@ -452,14 +505,32 @@ initial_position_cb (GeocluePosition *position,
   else
     {
       position_changed_cb (position, fields, timestamp, latitude, longitude,
-          altitude, accuracy, location_manager);
+          altitude, accuracy, self);
+    }
+}
+
+static gboolean
+set_requirements (EmpathyLocationManager *self)
+{
+  EmpathyLocationManagerPriv *priv = GET_PRIV (self);
+  GError *error = NULL;
+
+  if (!geoclue_master_client_set_requirements (priv->gc_client,
+          GEOCLUE_ACCURACY_LEVEL_COUNTRY, 0, FALSE, priv->resources,
+          &error))
+    {
+      DEBUG ("set_requirements failed: %s", error->message);
+      g_error_free (error);
+      return FALSE;
     }
+
+  return TRUE;
 }
 
 static void
-update_resources (EmpathyLocationManager *location_manager)
+update_resources (EmpathyLocationManager *self)
 {
-  EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
+  EmpathyLocationManagerPriv *priv = GET_PRIV (self);
 
   DEBUG ("Updating resources %d", priv->resources);
 
@@ -469,24 +540,19 @@ update_resources (EmpathyLocationManager *location_manager)
   /* 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_COUNTRY, 0, TRUE, priv->resources,
-          NULL))
-    {
-      DEBUG ("set_requirements failed");
-      return;
-    }
+  if (!set_requirements (self))
+    return;
 
   geoclue_address_get_address_async (priv->gc_address,
-      initial_address_cb, location_manager);
+      initial_address_cb, self);
   geoclue_position_get_position_async (priv->gc_position,
-      initial_position_cb, location_manager);
+      initial_position_cb, self);
 }
 
 static void
-setup_geoclue (EmpathyLocationManager *location_manager)
+setup_geoclue (EmpathyLocationManager *self)
 {
-  EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
+  EmpathyLocationManagerPriv *priv = GET_PRIV (self);
 
   GeoclueMaster *master;
   GError *error = NULL;
@@ -503,7 +569,8 @@ setup_geoclue (EmpathyLocationManager *location_manager)
       return;
     }
 
-  update_resources (location_manager);
+  if (!set_requirements (self))
+    return;
 
   /* Get updated when the position is changes */
   priv->gc_position = geoclue_master_client_create_position (
@@ -516,7 +583,7 @@ setup_geoclue (EmpathyLocationManager *location_manager)
     }
 
   g_signal_connect (G_OBJECT (priv->gc_position), "position-changed",
-      G_CALLBACK (position_changed_cb), location_manager);
+      G_CALLBACK (position_changed_cb), self);
 
   /* Get updated when the address changes */
   priv->gc_address = geoclue_master_client_create_address (
@@ -529,27 +596,22 @@ setup_geoclue (EmpathyLocationManager *location_manager)
     }
 
   g_signal_connect (G_OBJECT (priv->gc_address), "address-changed",
-      G_CALLBACK (address_changed_cb), location_manager);
+      G_CALLBACK (address_changed_cb), self);
 
   priv->geoclue_is_setup = TRUE;
 }
 
 static void
-publish_cb (EmpathyConf *conf,
+publish_cb (GSettings *gsettings_loc,
             const gchar *key,
             gpointer user_data)
 {
   EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
   EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
-  gboolean can_publish;
 
   DEBUG ("Publish Conf changed");
 
-
-  if (!empathy_conf_get_bool (conf, key, &can_publish))
-    return;
-
-  if (can_publish)
+  if (g_settings_get_boolean (gsettings_loc, key))
     {
       if (!priv->geoclue_is_setup)
         setup_geoclue (manager);
@@ -574,20 +636,16 @@ publish_cb (EmpathyConf *conf,
 }
 
 static void
-resource_cb (EmpathyConf  *conf,
+resource_cb (GSettings *gsettings_loc,
              const gchar *key,
              gpointer user_data)
 {
   EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
   EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
   GeoclueResourceFlags resource = 0;
-  gboolean resource_enabled;
 
   DEBUG ("%s changed", key);
 
-  if (!empathy_conf_get_bool (conf, key, &resource_enabled))
-    return;
-
   if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK))
     resource = GEOCLUE_RESOURCE_NETWORK;
   if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL))
@@ -595,7 +653,7 @@ resource_cb (EmpathyConf  *conf,
   if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS))
     resource = GEOCLUE_RESOURCE_GPS;
 
-  if (resource_enabled)
+  if (g_settings_get_boolean (gsettings_loc, key))
     priv->resources |= resource;
   else
     priv->resources &= ~resource;
@@ -605,20 +663,16 @@ resource_cb (EmpathyConf  *conf,
 }
 
 static void
-accuracy_cb (EmpathyConf  *conf,
+accuracy_cb (GSettings *gsettings_loc,
              const gchar *key,
              gpointer user_data)
 {
   EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
   EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
 
-  gboolean enabled;
-
   DEBUG ("%s changed", key);
 
-  if (!empathy_conf_get_bool (conf, key, &enabled))
-    return;
-  priv->reduce_accuracy = enabled;
+  priv->reduce_accuracy = g_settings_get_boolean (gsettings_loc, key);
 
   if (!priv->geoclue_is_setup)
     return;
@@ -630,41 +684,75 @@ accuracy_cb (EmpathyConf  *conf,
 }
 
 static void
-empathy_location_manager_init (EmpathyLocationManager *location_manager)
+account_manager_prepared_cb (GObject *source_object,
+    GAsyncResult *result,
+    gpointer user_data)
 {
-  EmpathyConf               *conf;
-  EmpathyLocationManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (location_manager,
+  GList *accounts, *l;
+  TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
+  EmpathyLocationManager *self = user_data;
+  GError *error = NULL;
+
+  if (!tp_account_manager_prepare_finish (account_manager, result, &error))
+    {
+      DEBUG ("Failed to prepare account manager: %s", error->message);
+      g_error_free (error);
+      return;
+    }
+
+  accounts = tp_account_manager_get_valid_accounts (account_manager);
+  for (l = accounts; l != NULL; l = l->next)
+    {
+      TpAccount *account = TP_ACCOUNT (l->data);
+
+      tp_g_signal_connect_object (account, "status-changed",
+          G_CALLBACK (new_connection_cb), self, 0);
+    }
+  g_list_free (accounts);
+}
+
+static void
+empathy_location_manager_init (EmpathyLocationManager *self)
+{
+  EmpathyLocationManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
       EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPriv);
 
-  location_manager->priv = priv;
+  self->priv = priv;
   priv->geoclue_is_setup = FALSE;
   priv->location = g_hash_table_new_full (g_direct_hash, g_direct_equal,
       g_free, (GDestroyNotify) tp_g_value_slice_free);
+  priv->gsettings_loc = g_settings_new (EMPATHY_PREFS_LOCATION_SCHEMA);
 
   /* 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);
+  priv->account_manager = tp_account_manager_dup ();
+
+  tp_account_manager_prepare_async (priv->account_manager, NULL,
+      account_manager_prepared_cb, self);
 
   /* Setup settings status callbacks */
-  conf = empathy_conf_get ();
-  empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_PUBLISH, publish_cb,
-      location_manager);
-  empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
-      resource_cb, location_manager);
-  empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
-      resource_cb, location_manager);
-  empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
-      resource_cb, location_manager);
-  empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
-      accuracy_cb, location_manager);
-
-  resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK, location_manager);
-  resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL, 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);
+  g_signal_connect (priv->gsettings_loc,
+      "changed::" EMPATHY_PREFS_LOCATION_PUBLISH,
+      G_CALLBACK (publish_cb), self);
+  g_signal_connect (priv->gsettings_loc,
+      "changed::" EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
+      G_CALLBACK (resource_cb), self);
+  g_signal_connect (priv->gsettings_loc,
+      "changed::" EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
+      G_CALLBACK (resource_cb), self);
+  g_signal_connect (priv->gsettings_loc,
+      "changed::" EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
+      G_CALLBACK (resource_cb), self);
+  g_signal_connect (priv->gsettings_loc,
+      "changed::" EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
+      G_CALLBACK (accuracy_cb), self);
+
+  resource_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
+      self);
+  resource_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_RESOURCE_CELL, self);
+  resource_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_RESOURCE_GPS, self);
+  accuracy_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
+      self);
+  publish_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_PUBLISH, self);
 }
 
 EmpathyLocationManager *