2 * Copyright (C) 2008 Collabora Ltd.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Authors: Pierre-Luc Beaudoin <pierre-luc@pierlux.com>
27 #include <glib/gi18n.h>
29 #include <telepathy-glib/util.h>
32 #include <geoclue/geoclue-master.h>
35 #include <extensions/extensions.h>
37 #include "empathy-location-manager.h"
38 #include "empathy-conf.h"
40 #include "libempathy/empathy-enum-types.h"
41 #include "libempathy/empathy-location.h"
42 #include "libempathy/empathy-contact-factory.h"
43 #include "libempathy/empathy-utils.h"
45 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
46 #include "libempathy/empathy-debug.h"
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLocationManager)
55 GeoclueResourceFlags resources;
56 GeoclueMasterClient *gc_client;
57 GeocluePosition *gc_position;
58 GeoclueAddress *gc_address;
60 gboolean reduce_accuracy;
62 } EmpathyLocationManagerPriv;
64 static void location_manager_finalize (GObject *object);
65 static void location_manager_get_property (GObject *object, guint param_id,
66 GValue *value, GParamSpec *pspec);
67 static void location_manager_set_property (GObject *object, guint param_id,
68 const GValue *value, GParamSpec *pspec);
70 static void position_changed_cb (GeocluePosition *position,
71 GeocluePositionFields fields, int timestamp, double latitude,
72 double longitude, double altitude, GeoclueAccuracy *accuracy,
74 static void address_changed_cb (GeoclueAddress *address, int timestamp,
75 GHashTable *details, GeoclueAccuracy *accuracy, gpointer user_data);
76 static void setup_geoclue (EmpathyLocationManager *location_manager);
77 static void publish_cb (EmpathyConf *conf, const gchar *key,
79 static void update_resources (EmpathyLocationManager *location_manager);
80 static void resource_cb (EmpathyConf *conf, const gchar *key,
82 static void accuracy_cb (EmpathyConf *conf, const gchar *key,
86 G_DEFINE_TYPE (EmpathyLocationManager, empathy_location_manager, G_TYPE_OBJECT);
94 empathy_location_manager_class_init (EmpathyLocationManagerClass *class)
96 GObjectClass *object_class;
98 object_class = G_OBJECT_CLASS (class);
100 object_class->finalize = location_manager_finalize;
101 object_class->get_property = location_manager_get_property;
102 object_class->set_property = location_manager_set_property;
104 g_type_class_add_private (object_class, sizeof (EmpathyLocationManagerPriv));
108 publish_location (EmpathyLocationManager *location_manager,
110 gboolean force_publication)
112 EmpathyLocationManagerPriv *priv;
113 guint connection_status = -1;
114 gboolean can_publish;
115 EmpathyConf *conf = empathy_conf_get ();
116 EmpathyContactFactory *factory = empathy_contact_factory_new ();
118 priv = GET_PRIV (location_manager);
120 if (force_publication == FALSE)
122 if (!empathy_conf_get_bool (conf, EMPATHY_PREFS_LOCATION_PUBLISH,
126 if (can_publish == FALSE)
130 connection_status = mission_control_get_connection_status (priv->mc,
133 if (connection_status != TP_CONNECTION_STATUS_CONNECTED)
136 DEBUG ("Publishing location to account %s",
137 mc_account_get_display_name (account));
139 empathy_contact_factory_set_location (factory, account, priv->location);
143 publish_location_to_all_accounts (EmpathyLocationManager *location_manager,
144 gboolean force_publication)
146 GList *accounts = NULL, *l;
148 accounts = mc_accounts_list_by_enabled (TRUE);
149 for (l = accounts; l; l = l->next)
151 publish_location (location_manager, l->data, force_publication);
154 mc_accounts_list_free (accounts);
158 account_status_changed_cb (MissionControl *mc,
159 TpConnectionStatus status,
161 TpConnectionStatusReason reason,
162 const gchar *unique_name,
163 gpointer *location_manager)
165 DEBUG ("Account %s changed status to %d", unique_name, status);
166 McAccount *account = mc_account_lookup (unique_name);
167 if (account && status == TP_CONNECTION_STATUS_CONNECTED)
168 publish_location (EMPATHY_LOCATION_MANAGER (location_manager), account,
173 empathy_location_manager_init (EmpathyLocationManager *location_manager)
176 EmpathyLocationManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (location_manager,
177 EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPriv);
179 location_manager->priv = priv;
180 priv->is_setup = FALSE;
181 priv->mc = empathy_mission_control_new ();
182 priv->location = g_hash_table_new_full (g_direct_hash, g_direct_equal,
183 g_free, (GDestroyNotify) tp_g_value_slice_free);
185 // Setup settings status callbacks
186 conf = empathy_conf_get ();
187 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_PUBLISH, publish_cb,
189 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
190 resource_cb, location_manager);
191 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
192 resource_cb, location_manager);
193 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
194 resource_cb, location_manager);
195 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
196 accuracy_cb, location_manager);
198 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK, location_manager);
199 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL, location_manager);
200 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS, location_manager);
201 accuracy_cb (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY, location_manager);
202 publish_cb (conf, EMPATHY_PREFS_LOCATION_PUBLISH, location_manager);
204 // Setup account status callbacks
205 priv->token = empathy_connect_to_account_status_changed (priv->mc,
206 G_CALLBACK (account_status_changed_cb), location_manager, NULL);
211 location_manager_finalize (GObject *object)
213 EmpathyLocationManagerPriv *priv;
215 priv = GET_PRIV (object);
217 DEBUG ("finalize: %p", object);
219 G_OBJECT_CLASS (empathy_location_manager_parent_class)->finalize (object);
224 location_manager_get_property (GObject *object,
229 EmpathyLocationManagerPriv *priv;
231 priv = GET_PRIV (object);
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
243 location_manager_set_property (GObject *object,
248 EmpathyLocationManagerPriv *priv;
250 priv = GET_PRIV (object);
255 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
261 EmpathyLocationManager *
262 empathy_location_manager_get_default (void)
264 static EmpathyLocationManager *singleton = NULL;
265 if (singleton == NULL)
266 singleton = g_object_new (EMPATHY_TYPE_LOCATION_MANAGER, NULL);
271 update_timestamp (EmpathyLocationManager *location_manager,
274 EmpathyLocationManagerPriv *priv;
275 priv = GET_PRIV (location_manager);
277 gint64 stamp64 = (gint64) timestamp;
279 new_value = tp_g_value_slice_new (G_TYPE_INT64);
280 g_value_set_int64 (new_value, stamp64);
281 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_TIMESTAMP),
283 DEBUG ("\t - Timestamp: %" G_GINT64_FORMAT, stamp64);
288 initial_position_cb (GeocluePosition *position,
289 GeocluePositionFields fields,
294 GeoclueAccuracy *accuracy,
296 gpointer location_manager)
300 DEBUG ("Error: %s", error->message);
301 g_error_free (error);
304 position_changed_cb (position, fields, timestamp, latitude, longitude,
305 altitude, accuracy, location_manager);
309 position_changed_cb (GeocluePosition *position,
310 GeocluePositionFields fields,
315 GeoclueAccuracy *accuracy,
316 gpointer location_manager)
318 EmpathyLocationManagerPriv *priv;
319 priv = GET_PRIV (location_manager);
320 GeoclueAccuracyLevel level;
321 gdouble mean, horizontal, vertical;
324 geoclue_accuracy_get_details (accuracy, &level, &horizontal, &vertical);
325 DEBUG ("New position (accuracy level %d)", level);
326 if (level == GEOCLUE_ACCURACY_LEVEL_NONE)
329 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
331 longitude += priv->reduce_value;
332 new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
333 g_value_set_double (new_value, longitude);
334 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LON),
336 DEBUG ("\t - Longitude: %f", longitude);
338 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
340 latitude += priv->reduce_value;
341 new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
342 g_value_set_double (new_value, latitude);
343 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LAT),
345 DEBUG ("\t - Latitude: %f", latitude);
347 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
349 new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
350 g_value_set_double (new_value, altitude);
351 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_ALT),
353 DEBUG ("\t - Altitude: %f", altitude);
356 if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
358 mean = (horizontal + vertical) / 2.0;
359 new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
360 g_value_set_double (new_value, mean);
361 g_hash_table_insert (priv->location,
362 g_strdup (EMPATHY_LOCATION_ACCURACY), new_value);
363 DEBUG ("\t - Accuracy: %f", mean);
366 update_timestamp (location_manager, timestamp);
367 publish_location_to_all_accounts (EMPATHY_LOCATION_MANAGER (location_manager),
373 address_foreach_cb (gpointer key,
375 gpointer location_manager)
377 if (location_manager == NULL)
380 EmpathyLocationManagerPriv *priv;
381 priv = GET_PRIV (location_manager);
383 // Discard street information if reduced accuracy is on
384 if (priv->reduce_accuracy && strcmp (key, EMPATHY_LOCATION_STREET) == 0)
387 GValue *new_value = tp_g_value_slice_new (G_TYPE_STRING);
388 g_value_set_string (new_value, value);
390 g_hash_table_insert (priv->location, g_strdup (key), new_value);
391 DEBUG ("\t - %s: %s", (char*) key, (char*) value);
395 initial_address_cb (GeoclueAddress *address,
398 GeoclueAccuracy *accuracy,
400 gpointer location_manager)
404 DEBUG ("Error: %s", error->message);
405 g_error_free (error);
408 address_changed_cb (address, timestamp, details, accuracy, location_manager);
412 address_changed_cb (GeoclueAddress *address,
415 GeoclueAccuracy *accuracy,
416 gpointer location_manager)
418 GeoclueAccuracyLevel level;
419 geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
420 EmpathyLocationManagerPriv *priv;
422 DEBUG ("New address (accuracy level %d):", level);
424 priv = GET_PRIV (location_manager);
425 g_hash_table_remove_all (priv->location);
427 g_hash_table_foreach (details, address_foreach_cb, (gpointer)location_manager);
429 update_timestamp (location_manager, timestamp);
430 publish_location_to_all_accounts (EMPATHY_LOCATION_MANAGER (location_manager),
436 update_resources (EmpathyLocationManager *location_manager)
438 EmpathyLocationManagerPriv *priv;
440 priv = GET_PRIV (location_manager);
442 DEBUG ("Updating resources");
444 if (!geoclue_master_client_set_requirements (priv->gc_client,
445 GEOCLUE_ACCURACY_LEVEL_LOCALITY, 0, TRUE, priv->resources,
448 g_printerr ("set_requirements failed");
455 geoclue_address_get_address_async (priv->gc_address,
456 initial_address_cb, location_manager);
457 geoclue_position_get_position_async (priv->gc_position,
458 initial_position_cb, location_manager);
464 setup_geoclue (EmpathyLocationManager *location_manager)
466 EmpathyLocationManagerPriv *priv;
468 priv = GET_PRIV (location_manager);
470 GeoclueMaster *master;
471 GError *error = NULL;
473 DEBUG ("Setting up Geoclue");
474 master = geoclue_master_get_default ();
475 priv->gc_client = geoclue_master_create_client (master, NULL, NULL);
476 g_object_unref (master);
478 update_resources (location_manager);
480 /* Get updated when the position is changes */
481 priv->gc_position = geoclue_master_client_create_position (
482 priv->gc_client, &error);
483 if (priv->gc_position == NULL)
485 g_printerr ("Failed to create GeocluePosition: %s", error->message);
486 g_error_free (error);
490 g_signal_connect (G_OBJECT (priv->gc_position), "position-changed",
491 G_CALLBACK (position_changed_cb), location_manager);
493 /* Get updated when the address changes */
494 priv->gc_address = geoclue_master_client_create_address (
495 priv->gc_client, &error);
496 if (priv->gc_address == NULL)
498 g_printerr ("Failed to create GeoclueAddress: %s", error->message);
499 g_error_free (error);
503 g_signal_connect (G_OBJECT (priv->gc_address), "address-changed",
504 G_CALLBACK (address_changed_cb), location_manager);
506 priv->is_setup = TRUE;
510 publish_cb (EmpathyConf *conf,
514 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
515 EmpathyLocationManagerPriv *priv;
516 gboolean can_publish;
518 DEBUG ("Publish Conf changed");
519 priv = GET_PRIV (manager);
520 if (!empathy_conf_get_bool (conf, key, &can_publish))
526 setup_geoclue (manager);
527 geoclue_address_get_address_async (priv->gc_address,
528 initial_address_cb, manager);
529 geoclue_position_get_position_async (priv->gc_position,
530 initial_position_cb, manager);
531 publish_location_to_all_accounts (manager, FALSE);
535 /* As per XEP-0080: send an empty location to have remove current
536 * location from the servers
538 g_hash_table_remove_all (priv->location);
539 publish_location_to_all_accounts (manager, TRUE);
546 accuracy_cb (EmpathyConf *conf,
550 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
551 EmpathyLocationManagerPriv *priv;
555 priv = GET_PRIV (manager);
556 DEBUG ("%s changed", key);
558 if (!empathy_conf_get_bool (conf, key, &enabled))
560 priv->reduce_accuracy = enabled;
564 GRand *rand = g_rand_new_with_seed (time (NULL));
565 priv->reduce_value = g_rand_double_range (rand, -0.25, 0.25);
569 priv->reduce_value = 0.0;
574 geoclue_address_get_address_async (priv->gc_address,
575 initial_address_cb, manager);
576 geoclue_position_get_position_async (priv->gc_position,
577 initial_position_cb, manager);
581 resource_cb (EmpathyConf *conf,
585 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
586 EmpathyLocationManagerPriv *priv;
587 GeoclueResourceFlags resource = 0;
588 gboolean resource_enabled;
590 priv = GET_PRIV (manager);
591 DEBUG ("%s changed", key);
593 if (!empathy_conf_get_bool (conf, key, &resource_enabled))
596 if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK) == 0)
597 resource = GEOCLUE_RESOURCE_NETWORK;
598 if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL) == 0)
599 resource = GEOCLUE_RESOURCE_CELL;
600 if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS) == 0)
601 resource = GEOCLUE_RESOURCE_GPS;
603 if (resource_enabled)
604 priv->resources |= resource;
606 priv->resources &= ~resource;
609 update_resources (manager);