/*
- * Copyright (C) 2008 Collabora Ltd.
+ * Copyright (C) 2009 Collabora Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
*
* 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.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
*
- * Authors: Pierre-Luc Beaudoin <pierre-luc@pierlux.com>
+ * Authors: Pierre-Luc Beaudoin <pierre-luc.beaudoin@collabora.co.uk>
*/
#include "config.h"
-
-#include <string.h>
-#include <time.h>
-
-#include <glib/gi18n.h>
-
-#include <telepathy-glib/util.h>
-
-#if HAVE_GEOCLUE
-#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-location.h"
-#include "libempathy/empathy-tp-contact-factory.h"
-#include "libempathy/empathy-utils.h"
+#include <tp-account-widgets/tpaw-time.h>
+
+#include "empathy-gsettings.h"
+#include "empathy-location.h"
+#include "empathy-geoclue-helper.h"
#define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
-#include "libempathy/empathy-debug.h"
+#include "empathy-debug.h"
+
+/* Seconds before updating the location */
+#define TIMEOUT 10
+static EmpathyLocationManager *location_manager = NULL;
-#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLocationManager)
-typedef struct {
- gboolean is_setup;
- MissionControl *mc;
+typedef enum
+{
+ GEOCLUE_NONE = 0,
+ GEOCLUE_STARTING,
+ GEOCLUE_STARTED,
+ GEOCLUE_FAILED,
+} GeoclueStatus;
+
+struct _EmpathyLocationManagerPrivate {
+ GeoclueStatus geoclue_status;
+ /* Contains the location to be sent to accounts. Geoclue is used
+ * to populate it. This HashTable uses Telepathy's style (string,
+ * GValue). Keys are defined in empathy-location.h
+ */
GHashTable *location;
- gpointer token;
- GeoclueResourceFlags resources;
- GeoclueMasterClient *gc_client;
- GeocluePosition *gc_position;
- GeoclueAddress *gc_address;
+ GSettings *gsettings_loc;
gboolean reduce_accuracy;
- gdouble reduce_value;
- EmpathyAccountManager *account_manager;
-} EmpathyLocationManagerPriv;
-
-static void location_manager_finalize (GObject *object);
-static void location_manager_get_property (GObject *object, guint param_id,
- GValue *value, GParamSpec *pspec);
-static void location_manager_set_property (GObject *object, guint param_id,
- const GValue *value, GParamSpec *pspec);
-static void position_changed_cb (GeocluePosition *position,
- GeocluePositionFields fields, int timestamp, double latitude,
- double longitude, double altitude, GeoclueAccuracy *accuracy,
- gpointer user_data);
-static void address_changed_cb (GeoclueAddress *address, int timestamp,
- GHashTable *details, GeoclueAccuracy *accuracy, gpointer user_data);
-static void setup_geoclue (EmpathyLocationManager *location_manager);
-static void publish_cb (EmpathyConf *conf, const gchar *key,
- gpointer user_data);
-static void update_resources (EmpathyLocationManager *location_manager);
-static void resource_cb (EmpathyConf *conf, const gchar *key,
- gpointer user_data);
-static void accuracy_cb (EmpathyConf *conf, const gchar *key,
- gpointer user_data);
-static void account_connection_changed_cb (EmpathyAccountManager *manager,
- McAccount *account, TpConnectionStatusReason reason,
- TpConnectionStatus current, TpConnectionStatus previous,
- gpointer *location_manager);
-
-G_DEFINE_TYPE (EmpathyLocationManager, empathy_location_manager, G_TYPE_OBJECT);
+ TpAccountManager *account_manager;
+ EmpathyGeoclueHelper *geoclue;
-enum
-{
- PROP_0,
+ /* The idle id for publish_on_idle func */
+ guint timeout_id;
};
-static void
-empathy_location_manager_class_init (EmpathyLocationManagerClass *class)
-{
- GObjectClass *object_class;
-
- object_class = G_OBJECT_CLASS (class);
-
- object_class->finalize = location_manager_finalize;
- object_class->get_property = location_manager_get_property;
- object_class->set_property = location_manager_set_property;
-
- g_type_class_add_private (object_class, sizeof (EmpathyLocationManagerPriv));
-}
+G_DEFINE_TYPE (EmpathyLocationManager, empathy_location_manager, G_TYPE_OBJECT);
-static void
-publish_location (EmpathyLocationManager *location_manager,
- McAccount *account,
- gboolean force_publication)
+static GObject *
+location_manager_constructor (GType type,
+ guint n_construct_params,
+ GObjectConstructParam *construct_params)
{
- EmpathyLocationManagerPriv *priv;
- guint connection_status = -1;
- gboolean can_publish;
- EmpathyConf *conf = empathy_conf_get ();
- TpConnection *conn;
- EmpathyTpContactFactory *factory;
-
- priv = GET_PRIV (location_manager);
-
- conn = mission_control_get_tpconnection (priv->mc, account, NULL);
- if (!conn)
- return;
-
- factory = empathy_tp_contact_factory_dup_singleton (conn);
+ GObject *retval;
- if (force_publication == FALSE)
+ if (location_manager == NULL)
{
- if (!empathy_conf_get_bool (conf, EMPATHY_PREFS_LOCATION_PUBLISH,
- &can_publish))
- return;
+ retval = G_OBJECT_CLASS (empathy_location_manager_parent_class)->constructor
+ (type, n_construct_params, construct_params);
- if (can_publish == FALSE)
- return;
+ location_manager = EMPATHY_LOCATION_MANAGER (retval);
+ g_object_add_weak_pointer (retval, (gpointer) &location_manager);
}
-
- connection_status = mission_control_get_connection_status (priv->mc,
- account, NULL);
-
- if (connection_status != TP_CONNECTION_STATUS_CONNECTED)
- return;
-
- DEBUG ("Publishing location to account %s",
- mc_account_get_display_name (account));
-
- empathy_tp_contact_factory_set_location (factory, priv->location);
- g_object_unref (factory);
-}
-
-static void
-publish_location_to_all_accounts (EmpathyLocationManager *location_manager,
- gboolean force_publication)
-{
- GList *accounts = NULL, *l;
-
- accounts = mc_accounts_list_by_enabled (TRUE);
- for (l = accounts; l; l = l->next)
+ else
{
- publish_location (location_manager, l->data, force_publication);
+ retval = g_object_ref (location_manager);
}
- mc_accounts_list_free (accounts);
+ return retval;
}
static void
-account_connection_changed_cb (EmpathyAccountManager *manager,
- McAccount *account,
- TpConnectionStatusReason reason,
- TpConnectionStatus current,
- TpConnectionStatus previous,
- gpointer *location_manager)
+location_manager_dispose (GObject *object)
{
- DEBUG ("Account %s changed status from %d to %d", mc_account_get_display_name (account),
- previous, current);
+ EmpathyLocationManager *self = (EmpathyLocationManager *) object;
+ void (*dispose) (GObject *) =
+ G_OBJECT_CLASS (empathy_location_manager_parent_class)->dispose;
- if (account && current == TP_CONNECTION_STATUS_CONNECTED)
- publish_location (EMPATHY_LOCATION_MANAGER (location_manager), account,
- FALSE);
-}
+ tp_clear_object (&self->priv->account_manager);
+ tp_clear_object (&self->priv->gsettings_loc);
+ tp_clear_pointer (&self->priv->location, g_hash_table_unref);
-static void
-empathy_location_manager_init (EmpathyLocationManager *location_manager)
-{
- EmpathyConf *conf;
- EmpathyLocationManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (location_manager,
- EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPriv);
-
- location_manager->priv = priv;
- priv->is_setup = FALSE;
- priv->mc = empathy_mission_control_dup_singleton ();
- priv->location = g_hash_table_new_full (g_direct_hash, g_direct_equal,
- g_free, (GDestroyNotify) tp_g_value_slice_free);
-
- // 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);
-
- // Setup account status callbacks
- priv->account_manager = empathy_account_manager_dup_singleton ();
- g_signal_connect (priv->account_manager,
- "account-connection-changed",
- G_CALLBACK (account_connection_changed_cb), location_manager);
+ if (dispose != NULL)
+ dispose (object);
}
-
static void
-location_manager_finalize (GObject *object)
+empathy_location_manager_class_init (EmpathyLocationManagerClass *class)
{
- EmpathyLocationManagerPriv *priv;
+ GObjectClass *object_class;
- priv = GET_PRIV (object);
+ object_class = G_OBJECT_CLASS (class);
- DEBUG ("finalize: %p", object);
- g_object_unref (priv->account_manager);
+ object_class->constructor = location_manager_constructor;
+ object_class->dispose = location_manager_dispose;
- G_OBJECT_CLASS (empathy_location_manager_parent_class)->finalize (object);
+ g_type_class_add_private (object_class, sizeof (EmpathyLocationManagerPrivate));
}
-
static void
-location_manager_get_property (GObject *object,
- guint param_id,
- GValue *value,
- GParamSpec *pspec)
+publish_location_cb (TpConnection *connection,
+ const GError *error,
+ gpointer user_data,
+ GObject *weak_object)
{
- EmpathyLocationManagerPriv *priv;
-
- priv = GET_PRIV (object);
-
- switch (param_id)
- {
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
- break;
- };
+ if (error != NULL)
+ DEBUG ("Error setting location: %s", error->message);
}
-
static void
-location_manager_set_property (GObject *object,
- guint param_id,
- const GValue *value,
- GParamSpec *pspec)
+publish_location (EmpathyLocationManager *self,
+ TpConnection *conn,
+ gboolean force_publication)
{
- EmpathyLocationManagerPriv *priv;
+ guint connection_status = -1;
- priv = GET_PRIV (object);
+ if (!conn)
+ return;
- switch (param_id)
+ if (!force_publication)
{
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
- break;
- };
-}
+ if (!g_settings_get_boolean (self->priv->gsettings_loc,
+ EMPATHY_PREFS_LOCATION_PUBLISH))
+ return;
+ }
+ connection_status = tp_connection_get_status (conn, NULL);
-EmpathyLocationManager *
-empathy_location_manager_get_default (void)
-{
- static EmpathyLocationManager *singleton = NULL;
- if (singleton == NULL)
- singleton = g_object_new (EMPATHY_TYPE_LOCATION_MANAGER, NULL);
- return singleton;
-}
+ if (connection_status != TP_CONNECTION_STATUS_CONNECTED)
+ return;
-static void
-update_timestamp (EmpathyLocationManager *location_manager)
-{
- EmpathyLocationManagerPriv *priv;
- priv = GET_PRIV (location_manager);
- GValue *new_value;
- gint64 stamp64;
- time_t timestamp;
-
- timestamp = time (NULL);
- stamp64 = (gint64) timestamp;
- new_value = tp_g_value_slice_new (G_TYPE_INT64);
- g_value_set_int64 (new_value, stamp64);
- g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_TIMESTAMP),
- new_value);
- DEBUG ("\t - Timestamp: %" G_GINT64_FORMAT, stamp64);
+ DEBUG ("Publishing %s location to connection %p",
+ (g_hash_table_size (self->priv->location) == 0 ? "empty" : ""),
+ conn);
+
+ tp_cli_connection_interface_location_call_set_location (conn, -1,
+ self->priv->location, publish_location_cb, NULL, NULL, G_OBJECT (self));
}
-static void
-initial_position_cb (GeocluePosition *position,
- GeocluePositionFields fields,
- int timestamp,
- double latitude,
- double longitude,
- double altitude,
- GeoclueAccuracy *accuracy,
- GError *error,
- gpointer location_manager)
+typedef struct
{
- if (error)
- {
- DEBUG ("Error: %s", error->message);
- g_error_free (error);
- }
- else
- position_changed_cb (position, fields, timestamp, latitude, longitude,
- altitude, accuracy, location_manager);
-}
+ EmpathyLocationManager *self;
+ gboolean force_publication;
+} PublishToAllData;
static void
-position_changed_cb (GeocluePosition *position,
- GeocluePositionFields fields,
- int timestamp,
- double latitude,
- double longitude,
- double altitude,
- GeoclueAccuracy *accuracy,
- gpointer location_manager)
+publish_to_all_am_prepared_cb (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
{
- EmpathyLocationManagerPriv *priv;
- priv = GET_PRIV (location_manager);
- GeoclueAccuracyLevel level;
- 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)
- return;
+ TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
+ PublishToAllData *data = user_data;
+ GList *accounts, *l;
+ GError *error = NULL;
- if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
- {
- longitude += priv->reduce_value;
- new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
- g_value_set_double (new_value, longitude);
- g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LON),
- new_value);
- DEBUG ("\t - Longitude: %f", longitude);
- }
- if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
- {
- latitude += priv->reduce_value;
- new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
- g_value_set_double (new_value, latitude);
- g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LAT),
- new_value);
- DEBUG ("\t - Latitude: %f", latitude);
- }
- if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
+ if (!tp_proxy_prepare_finish (manager, result, &error))
{
- new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
- g_value_set_double (new_value, altitude);
- g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_ALT),
- new_value);
- DEBUG ("\t - Altitude: %f", altitude);
+ DEBUG ("Failed to prepare account manager: %s", error->message);
+ g_error_free (error);
+ goto out;
}
- if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
+ accounts = tp_account_manager_dup_valid_accounts (manager);
+ for (l = accounts; l; l = l->next)
{
- mean = (horizontal + vertical) / 2.0;
- new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
- g_value_set_double (new_value, mean);
- g_hash_table_insert (priv->location,
- g_strdup (EMPATHY_LOCATION_ACCURACY), new_value);
- DEBUG ("\t - Accuracy: %f", mean);
+ TpConnection *conn = tp_account_get_connection (TP_ACCOUNT (l->data));
+
+ if (conn != NULL)
+ publish_location (data->self, conn, data->force_publication);
}
+ g_list_free_full (accounts, g_object_unref);
- update_timestamp (location_manager);
- publish_location_to_all_accounts (EMPATHY_LOCATION_MANAGER (location_manager),
- FALSE);
+out:
+ g_object_unref (data->self);
+ g_slice_free (PublishToAllData, data);
}
-
static void
-address_foreach_cb (gpointer key,
- gpointer value,
- gpointer location_manager)
+publish_to_all_connections (EmpathyLocationManager *self,
+ gboolean force_publication)
{
- if (location_manager == NULL)
- return;
-
- EmpathyLocationManagerPriv *priv;
- priv = GET_PRIV (location_manager);
+ PublishToAllData *data;
- // Discard street information if reduced accuracy is on
- if (priv->reduce_accuracy && strcmp (key, EMPATHY_LOCATION_STREET) == 0)
- return;
-
- GValue *new_value = tp_g_value_slice_new (G_TYPE_STRING);
- g_value_set_string (new_value, value);
+ data = g_slice_new0 (PublishToAllData);
+ data->self = g_object_ref (self);
+ data->force_publication = force_publication;
- g_hash_table_insert (priv->location, g_strdup (key), new_value);
- DEBUG ("\t - %s: %s", (char*) key, (char*) value);
+ tp_proxy_prepare_async (self->priv->account_manager, NULL,
+ publish_to_all_am_prepared_cb, data);
}
-static void
-initial_address_cb (GeoclueAddress *address,
- int timestamp,
- GHashTable *details,
- GeoclueAccuracy *accuracy,
- GError *error,
- gpointer location_manager)
+static gboolean
+publish_on_idle (gpointer user_data)
{
- if (error)
- {
- DEBUG ("Error: %s", error->message);
- g_error_free (error);
- }
- else
- address_changed_cb (address, timestamp, details, accuracy, location_manager);
+ EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
+
+ manager->priv->timeout_id = 0;
+ publish_to_all_connections (manager, TRUE);
+ return FALSE;
}
static void
-address_changed_cb (GeoclueAddress *address,
- int timestamp,
- GHashTable *details,
- GeoclueAccuracy *accuracy,
- gpointer location_manager)
+new_connection_cb (TpAccount *account,
+ guint old_status,
+ guint new_status,
+ guint reason,
+ gchar *dbus_error_name,
+ GHashTable *details,
+ gpointer user_data)
{
- GeoclueAccuracyLevel level;
- geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
- EmpathyLocationManagerPriv *priv;
-
- DEBUG ("New address (accuracy level %d):", level);
-
- priv = GET_PRIV (location_manager);
- g_hash_table_remove_all (priv->location);
+ EmpathyLocationManager *self = user_data;
+ TpConnection *conn;
- if (g_hash_table_size (details) == 0)
- return;
+ conn = tp_account_get_connection (account);
- g_hash_table_foreach (details, address_foreach_cb, (gpointer)location_manager);
+ DEBUG ("New connection %p", conn);
- update_timestamp (location_manager);
- publish_location_to_all_accounts (EMPATHY_LOCATION_MANAGER (location_manager),
- FALSE);
+ /* Don't publish if it is already planned (ie startup) */
+ if (self->priv->timeout_id == 0)
+ {
+ publish_location (EMPATHY_LOCATION_MANAGER (self), conn,
+ FALSE);
+ }
}
-
static void
-update_resources (EmpathyLocationManager *location_manager)
+update_location (EmpathyLocationManager *self,
+ GClueLocation *proxy)
{
- EmpathyLocationManagerPriv *priv;
+ gdouble latitude, longitude, accuracy;
+ const gchar *desc;
+ gint64 timestamp;
- priv = GET_PRIV (location_manager);
+ latitude = gclue_location_get_latitude (proxy);
+ longitude = gclue_location_get_longitude (proxy);
+ accuracy = gclue_location_get_accuracy (proxy);
+ desc = gclue_location_get_description (proxy);
- DEBUG ("Updating resources");
+ DEBUG ("Location updated: (%f %f) accuracy: %f (%s)",
+ latitude, longitude, accuracy, desc);
- if (!geoclue_master_client_set_requirements (priv->gc_client,
- GEOCLUE_ACCURACY_LEVEL_LOCALITY, 0, TRUE, priv->resources,
- NULL))
+ if (self->priv->reduce_accuracy)
{
- g_printerr ("set_requirements failed");
- return;
+ /* Truncate at 1 decimal place */
+ latitude = ((int) (latitude * 10)) / 10.0;
+ longitude = ((int) (longitude * 10)) / 10.0;
+ }
+ else
+ {
+ /* Include the description only if we are not asked to reduce the
+ * accuracy as it can contains a pretty specific description of the
+ * location. */
+ tp_asv_set_string (self->priv->location, EMPATHY_LOCATION_DESCRIPTION,
+ desc);
}
- if (!priv->is_setup)
- return;
+ tp_asv_set_double (self->priv->location, EMPATHY_LOCATION_LAT, latitude);
+ tp_asv_set_double (self->priv->location, EMPATHY_LOCATION_LON, longitude);
+ tp_asv_set_double (self->priv->location, EMPATHY_LOCATION_ACCURACY, accuracy);
- geoclue_address_get_address_async (priv->gc_address,
- initial_address_cb, location_manager);
- geoclue_position_get_position_async (priv->gc_position,
- initial_position_cb, location_manager);
+ timestamp = tpaw_time_get_current ();
+ tp_asv_set_int64 (self->priv->location, EMPATHY_LOCATION_TIMESTAMP,
+ timestamp);
+ if (self->priv->timeout_id == 0)
+ self->priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle,
+ self);
}
-
static void
-setup_geoclue (EmpathyLocationManager *location_manager)
+location_changed_cb (EmpathyGeoclueHelper *geoclue,
+ GClueLocation *location,
+ EmpathyLocationManager *self)
{
- EmpathyLocationManagerPriv *priv;
-
- priv = GET_PRIV (location_manager);
+ update_location (self, location);
+}
- GeoclueMaster *master;
+static void
+geoclue_new_cb (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ EmpathyLocationManager *self = EMPATHY_LOCATION_MANAGER (user_data);
GError *error = NULL;
+ GClueLocation *location;
- DEBUG ("Setting up Geoclue");
- master = geoclue_master_get_default ();
- priv->gc_client = geoclue_master_create_client (master, NULL, NULL);
- g_object_unref (master);
-
- update_resources (location_manager);
+ self->priv->geoclue = empathy_geoclue_helper_new_started_finish (result,
+ &error);
- /* Get updated when the position is changes */
- priv->gc_position = geoclue_master_client_create_position (
- priv->gc_client, &error);
- if (priv->gc_position == NULL)
+ if (self->priv->geoclue == NULL)
{
- g_printerr ("Failed to create GeocluePosition: %s", error->message);
+ DEBUG ("Failed to create Geoclue client: %s", error->message);
g_error_free (error);
+ self->priv->geoclue_status = GEOCLUE_FAILED;
return;
}
- g_signal_connect (G_OBJECT (priv->gc_position), "position-changed",
- G_CALLBACK (position_changed_cb), location_manager);
+ self->priv->geoclue_status = GEOCLUE_STARTED;
+
+ g_signal_connect_object (self->priv->geoclue, "location-changed",
+ G_CALLBACK (location_changed_cb), self, 0);
+
+ location = empathy_geoclue_helper_get_location (self->priv->geoclue);
+ if (location != NULL)
+ update_location (self, location);
+}
- /* Get updated when the address changes */
- priv->gc_address = geoclue_master_client_create_address (
- priv->gc_client, &error);
- if (priv->gc_address == NULL)
+static void
+setup_geoclue (EmpathyLocationManager *self)
+{
+ switch (self->priv->geoclue_status)
{
- g_printerr ("Failed to create GeoclueAddress: %s", error->message);
- g_error_free (error);
+ case GEOCLUE_NONE:
+ g_assert (self->priv->geoclue == NULL);
+ self->priv->geoclue_status = GEOCLUE_STARTING;
+ empathy_geoclue_helper_new_started_async (0, geoclue_new_cb, self);
+ break;
+ case GEOCLUE_STARTED:
+ case GEOCLUE_STARTING:
+ case GEOCLUE_FAILED:
return;
}
-
- g_signal_connect (G_OBJECT (priv->gc_address), "address-changed",
- G_CALLBACK (address_changed_cb), location_manager);
-
- priv->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;
- gboolean can_publish;
+ EmpathyLocationManager *self = EMPATHY_LOCATION_MANAGER (user_data);
DEBUG ("Publish Conf changed");
- priv = GET_PRIV (manager);
-
-
- if (empathy_conf_get_bool (conf, key, &can_publish) == FALSE)
- return;
- if (can_publish == TRUE)
+ if (g_settings_get_boolean (gsettings_loc, key))
{
- if (priv->is_setup == FALSE)
- setup_geoclue (manager);
- /* if still not setup than the init failed */
- if (priv->is_setup == FALSE)
- return;
-
- geoclue_address_get_address_async (priv->gc_address,
- initial_address_cb, manager);
- geoclue_position_get_position_async (priv->gc_position,
- initial_position_cb, manager);
- publish_location_to_all_accounts (manager, FALSE);
+ setup_geoclue (self);
}
else
{
/* As per XEP-0080: send an empty location to have remove current
* location from the servers
*/
- g_hash_table_remove_all (priv->location);
- publish_location_to_all_accounts (manager, TRUE);
- }
+ g_hash_table_remove_all (self->priv->location);
+ publish_to_all_connections (self, TRUE);
+ g_clear_object (&self->priv->geoclue);
+ self->priv->geoclue_status = GEOCLUE_NONE;
+ }
}
-
static void
-accuracy_cb (EmpathyConf *conf,
- const gchar *key,
- gpointer user_data)
+account_manager_prepared_cb (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
{
- EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
- EmpathyLocationManagerPriv *priv;
-
- gboolean enabled;
-
- priv = GET_PRIV (manager);
- DEBUG ("%s changed", key);
-
- if (!empathy_conf_get_bool (conf, key, &enabled))
- return;
- priv->reduce_accuracy = enabled;
+ GList *accounts, *l;
+ TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
+ EmpathyLocationManager *self = user_data;
+ GError *error = NULL;
- if (enabled)
+ if (!tp_proxy_prepare_finish (account_manager, result, &error))
{
- 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);
+ DEBUG ("Failed to prepare account manager: %s", error->message);
+ g_error_free (error);
+ return;
}
- else
- priv->reduce_value = 0.0;
- if (!priv->is_setup)
- return;
+ accounts = tp_account_manager_dup_valid_accounts (account_manager);
+ for (l = accounts; l != NULL; l = l->next)
+ {
+ TpAccount *account = TP_ACCOUNT (l->data);
- geoclue_address_get_address_async (priv->gc_address,
- initial_address_cb, manager);
- geoclue_position_get_position_async (priv->gc_position,
- initial_position_cb, manager);
+ tp_g_signal_connect_object (account, "status-changed",
+ G_CALLBACK (new_connection_cb), self, 0);
+ }
+ g_list_free_full (accounts, g_object_unref);
}
static void
-resource_cb (EmpathyConf *conf,
- const gchar *key,
- gpointer user_data)
+empathy_location_manager_init (EmpathyLocationManager *self)
{
- EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
- EmpathyLocationManagerPriv *priv;
- GeoclueResourceFlags resource = 0;
- gboolean resource_enabled;
+ EmpathyLocationManagerPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+ EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPrivate);
- priv = GET_PRIV (manager);
- DEBUG ("%s changed", key);
+ self->priv = priv;
+ priv->location = tp_asv_new (NULL, NULL);
+ priv->gsettings_loc = g_settings_new (EMPATHY_PREFS_LOCATION_SCHEMA);
- if (!empathy_conf_get_bool (conf, key, &resource_enabled))
- return;
+ /* Setup account status callbacks */
+ priv->account_manager = tp_account_manager_dup ();
- if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK) == 0)
- resource = GEOCLUE_RESOURCE_NETWORK;
- if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL) == 0)
- resource = GEOCLUE_RESOURCE_CELL;
- if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS) == 0)
- resource = GEOCLUE_RESOURCE_GPS;
+ tp_proxy_prepare_async (priv->account_manager, NULL,
+ account_manager_prepared_cb, self);
- if (resource_enabled)
- priv->resources |= resource;
- else
- priv->resources &= ~resource;
+ /* Setup settings status callbacks */
+ g_signal_connect (priv->gsettings_loc,
+ "changed::" EMPATHY_PREFS_LOCATION_PUBLISH,
+ G_CALLBACK (publish_cb), self);
- if (priv->is_setup)
- update_resources (manager);
+ publish_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_PUBLISH, self);
}
-#endif
+EmpathyLocationManager *
+empathy_location_manager_dup_singleton (void)
+{
+ return EMPATHY_LOCATION_MANAGER (g_object_new (EMPATHY_TYPE_LOCATION_MANAGER,
+ NULL));
+}