2 * Copyright (C) 2009 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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301 USA
19 * Authors: Pierre-Luc Beaudoin <pierre-luc.beaudoin@collabora.co.uk>
27 #include <glib/gi18n.h>
29 #include <telepathy-glib/util.h>
31 #include <geoclue/geoclue-master.h>
33 #include <extensions/extensions.h>
35 #include "empathy-location-manager.h"
36 #include "empathy-conf.h"
38 #include "libempathy/empathy-account-manager.h"
39 #include "libempathy/empathy-enum-types.h"
40 #include "libempathy/empathy-location.h"
41 #include "libempathy/empathy-tp-contact-factory.h"
42 #include "libempathy/empathy-utils.h"
44 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
45 #include "libempathy/empathy-debug.h"
47 /* Seconds before updating the location */
49 static EmpathyLocationManager *location_manager = NULL;
51 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLocationManager)
53 gboolean geoclue_is_setup;
54 /* Contains the location to be sent to accounts. Geoclue is used
55 * to populate it. This HashTable uses Telepathy's style (string,
56 * GValue). Keys are defined in empathy-location.h
60 GeoclueResourceFlags resources;
61 GeoclueMasterClient *gc_client;
62 GeocluePosition *gc_position;
63 GeoclueAddress *gc_address;
65 gboolean reduce_accuracy;
67 EmpathyAccountManager *account_manager;
69 /* The idle id for publish_on_idle func */
71 } EmpathyLocationManagerPriv;
73 G_DEFINE_TYPE (EmpathyLocationManager, empathy_location_manager, G_TYPE_OBJECT);
76 location_manager_constructor (GType type,
77 guint n_construct_params,
78 GObjectConstructParam *construct_params)
82 if (location_manager == NULL)
84 retval = G_OBJECT_CLASS (empathy_location_manager_parent_class)->constructor
85 (type, n_construct_params, construct_params);
87 location_manager = EMPATHY_LOCATION_MANAGER (retval);
88 g_object_add_weak_pointer (retval, (gpointer) &location_manager);
92 retval = g_object_ref (location_manager);
99 location_manager_dispose (GObject *object)
101 EmpathyLocationManagerPriv *priv = GET_PRIV (object);
103 if (priv->account_manager != NULL)
105 g_object_unref (priv->account_manager);
106 priv->account_manager = NULL;
109 if (priv->gc_client != NULL)
111 g_object_unref (priv->gc_client);
112 priv->gc_client = NULL;
115 if (priv->gc_position != NULL)
117 g_object_unref (priv->gc_position);
118 priv->gc_position = NULL;
121 if (priv->gc_address != NULL)
123 g_object_unref (priv->gc_address);
124 priv->gc_address = NULL;
127 if (priv->location != NULL)
129 g_hash_table_unref (priv->location);
130 priv->location = NULL;
133 G_OBJECT_CLASS (empathy_location_manager_parent_class)->finalize (object);
137 location_manager_get_property (GObject *object,
142 /*EmpathyLocationManagerPriv *priv = GET_PRIV (object); */
147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
153 location_manager_set_property (GObject *object,
158 /* EmpathyLocationManagerPriv *priv = GET_PRIV (object); */
163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
169 empathy_location_manager_class_init (EmpathyLocationManagerClass *class)
171 GObjectClass *object_class;
173 object_class = G_OBJECT_CLASS (class);
175 object_class->constructor = location_manager_constructor;
176 object_class->dispose = location_manager_dispose;
177 object_class->get_property = location_manager_get_property;
178 object_class->set_property = location_manager_set_property;
180 g_type_class_add_private (object_class, sizeof (EmpathyLocationManagerPriv));
184 publish_location (EmpathyLocationManager *location_manager,
186 gboolean force_publication)
188 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
189 guint connection_status = -1;
190 gboolean can_publish;
191 EmpathyConf *conf = empathy_conf_get ();
192 EmpathyTpContactFactory *factory;
197 if (force_publication == FALSE)
199 if (!empathy_conf_get_bool (conf, EMPATHY_PREFS_LOCATION_PUBLISH,
203 if (can_publish == FALSE)
207 connection_status = tp_connection_get_status (conn, NULL);
209 if (connection_status != TP_CONNECTION_STATUS_CONNECTED)
212 DEBUG ("Publishing %s location to connection %p",
213 (g_hash_table_size (priv->location) == 0 ? "empty" : ""),
216 factory = empathy_tp_contact_factory_dup_singleton (conn);
217 empathy_tp_contact_factory_set_location (factory, priv->location);
218 g_object_unref (factory);
222 publish_to_all_connections (EmpathyLocationManager *location_manager,
223 gboolean force_publication)
225 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
226 GList *connections = NULL, *l;
228 connections = empathy_account_manager_dup_connections (priv->account_manager);
229 for (l = connections; l; l = l->next)
231 publish_location (location_manager, l->data, force_publication);
232 g_object_unref (l->data);
234 g_list_free (connections);
239 publish_on_idle (gpointer user_data)
241 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
242 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
244 priv->timeout_id = 0;
245 publish_to_all_connections (manager, TRUE);
250 new_connection_cb (EmpathyAccountManager *manager,
252 gpointer *location_manager)
254 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
255 DEBUG ("New connection %p", conn);
257 /* Don't publish if it is already planned (ie startup) */
258 if (priv->timeout_id == 0)
260 publish_location (EMPATHY_LOCATION_MANAGER (location_manager), conn,
266 update_timestamp (EmpathyLocationManager *location_manager)
268 EmpathyLocationManagerPriv *priv= GET_PRIV (location_manager);
273 timestamp = time (NULL);
274 stamp64 = (gint64) timestamp;
275 new_value = tp_g_value_slice_new_int64 (stamp64);
276 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_TIMESTAMP),
278 DEBUG ("\t - Timestamp: %" G_GINT64_FORMAT, stamp64);
282 address_changed_cb (GeoclueAddress *address,
285 GeoclueAccuracy *accuracy,
286 gpointer location_manager)
288 GeoclueAccuracyLevel level;
289 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
293 geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
294 DEBUG ("New address (accuracy level %d):", level);
295 /* FIXME: Publish accuracy level also considering the position's */
297 g_hash_table_remove (priv->location, EMPATHY_LOCATION_STREET);
298 g_hash_table_remove (priv->location, EMPATHY_LOCATION_AREA);
299 g_hash_table_remove (priv->location, EMPATHY_LOCATION_REGION);
300 g_hash_table_remove (priv->location, EMPATHY_LOCATION_COUNTRY);
301 g_hash_table_remove (priv->location, EMPATHY_LOCATION_COUNTRY_CODE);
302 g_hash_table_remove (priv->location, EMPATHY_LOCATION_POSTAL_CODE);
304 if (g_hash_table_size (details) == 0)
306 DEBUG ("\t - (Empty)");
310 g_hash_table_iter_init (&iter, details);
311 while (g_hash_table_iter_next (&iter, &key, &value))
314 /* Discard street information if reduced accuracy is on */
315 if (priv->reduce_accuracy && strcmp (key, EMPATHY_LOCATION_STREET) == 0)
318 new_value = tp_g_value_slice_new_string (value);
319 g_hash_table_insert (priv->location, g_strdup (key), new_value);
321 DEBUG ("\t - %s: %s", (gchar *) key, (gchar *) value);
324 update_timestamp (location_manager);
325 if (priv->timeout_id == 0)
326 priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, location_manager);
330 initial_address_cb (GeoclueAddress *address,
333 GeoclueAccuracy *accuracy,
335 gpointer location_manager)
339 DEBUG ("Error: %s", error->message);
340 g_error_free (error);
344 address_changed_cb (address, timestamp, details, accuracy, location_manager);
349 position_changed_cb (GeocluePosition *position,
350 GeocluePositionFields fields,
355 GeoclueAccuracy *accuracy,
356 gpointer location_manager)
358 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
359 GeoclueAccuracyLevel level;
360 gdouble mean, horizontal, vertical;
364 geoclue_accuracy_get_details (accuracy, &level, &horizontal, &vertical);
365 DEBUG ("New position (accuracy level %d)", level);
366 if (level == GEOCLUE_ACCURACY_LEVEL_NONE)
369 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
371 longitude += priv->reduce_value;
372 new_value = tp_g_value_slice_new_double (longitude);
373 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LON),
375 DEBUG ("\t - Longitude: %f", longitude);
379 g_hash_table_remove (priv->location, EMPATHY_LOCATION_LON);
382 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
384 latitude += priv->reduce_value;
385 new_value = tp_g_value_slice_new_double (latitude);
386 g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_LAT),
388 DEBUG ("\t - Latitude: %f", latitude);
392 g_hash_table_remove (priv->location, EMPATHY_LOCATION_LAT);
395 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
397 new_value = tp_g_value_slice_new_double (altitude);
398 g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_ALT),
400 DEBUG ("\t - Altitude: %f", altitude);
404 g_hash_table_remove (priv->location, EMPATHY_LOCATION_ALT);
407 if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
409 mean = (horizontal + vertical) / 2.0;
410 new_value = tp_g_value_slice_new_double (mean);
411 g_hash_table_replace (priv->location,
412 g_strdup (EMPATHY_LOCATION_ACCURACY), new_value);
413 DEBUG ("\t - Accuracy: %f", mean);
417 g_hash_table_remove (priv->location, EMPATHY_LOCATION_ACCURACY);
420 update_timestamp (location_manager);
421 if (priv->timeout_id == 0)
422 priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, location_manager);
426 initial_position_cb (GeocluePosition *position,
427 GeocluePositionFields fields,
432 GeoclueAccuracy *accuracy,
434 gpointer location_manager)
438 DEBUG ("Error: %s", error->message);
439 g_error_free (error);
443 position_changed_cb (position, fields, timestamp, latitude, longitude,
444 altitude, accuracy, location_manager);
449 update_resources (EmpathyLocationManager *location_manager)
451 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
453 DEBUG ("Updating resources %d", priv->resources);
455 if (!priv->geoclue_is_setup)
458 /* As per Geoclue bug #15126, using NONE results in no address
459 * being found as geoclue-manual report an empty address with
461 if (!geoclue_master_client_set_requirements (priv->gc_client,
462 GEOCLUE_ACCURACY_LEVEL_COUNTRY, 0, TRUE, priv->resources,
465 DEBUG ("set_requirements failed");
469 geoclue_address_get_address_async (priv->gc_address,
470 initial_address_cb, location_manager);
471 geoclue_position_get_position_async (priv->gc_position,
472 initial_position_cb, location_manager);
476 setup_geoclue (EmpathyLocationManager *location_manager)
478 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
480 GeoclueMaster *master;
481 GError *error = NULL;
483 DEBUG ("Setting up Geoclue");
484 master = geoclue_master_get_default ();
485 priv->gc_client = geoclue_master_create_client (master, NULL, &error);
486 g_object_unref (master);
488 if (priv->gc_client == NULL)
490 DEBUG ("Failed to GeoclueMasterClient: %s", error->message);
491 g_error_free (error);
495 update_resources (location_manager);
497 /* Get updated when the position is changes */
498 priv->gc_position = geoclue_master_client_create_position (
499 priv->gc_client, &error);
500 if (priv->gc_position == NULL)
502 DEBUG ("Failed to create GeocluePosition: %s", error->message);
503 g_error_free (error);
507 g_signal_connect (G_OBJECT (priv->gc_position), "position-changed",
508 G_CALLBACK (position_changed_cb), location_manager);
510 /* Get updated when the address changes */
511 priv->gc_address = geoclue_master_client_create_address (
512 priv->gc_client, &error);
513 if (priv->gc_address == NULL)
515 DEBUG ("Failed to create GeoclueAddress: %s", error->message);
516 g_error_free (error);
520 g_signal_connect (G_OBJECT (priv->gc_address), "address-changed",
521 G_CALLBACK (address_changed_cb), location_manager);
523 priv->geoclue_is_setup = TRUE;
527 publish_cb (EmpathyConf *conf,
531 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
532 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
533 gboolean can_publish;
535 DEBUG ("Publish Conf changed");
538 if (empathy_conf_get_bool (conf, key, &can_publish) == FALSE)
541 if (can_publish == TRUE)
543 if (priv->geoclue_is_setup == FALSE)
544 setup_geoclue (manager);
545 /* if still not setup than the init failed */
546 if (priv->geoclue_is_setup == FALSE)
549 geoclue_address_get_address_async (priv->gc_address,
550 initial_address_cb, manager);
551 geoclue_position_get_position_async (priv->gc_position,
552 initial_position_cb, manager);
556 /* As per XEP-0080: send an empty location to have remove current
557 * location from the servers
559 g_hash_table_remove_all (priv->location);
560 publish_to_all_connections (manager, TRUE);
566 resource_cb (EmpathyConf *conf,
570 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
571 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
572 GeoclueResourceFlags resource = 0;
573 gboolean resource_enabled;
575 DEBUG ("%s changed", key);
577 if (!empathy_conf_get_bool (conf, key, &resource_enabled))
580 if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK) == 0)
581 resource = GEOCLUE_RESOURCE_NETWORK;
582 if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL) == 0)
583 resource = GEOCLUE_RESOURCE_CELL;
584 if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS) == 0)
585 resource = GEOCLUE_RESOURCE_GPS;
587 if (resource_enabled)
588 priv->resources |= resource;
590 priv->resources &= ~resource;
592 if (priv->geoclue_is_setup)
593 update_resources (manager);
597 accuracy_cb (EmpathyConf *conf,
601 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
602 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
606 DEBUG ("%s changed", key);
608 if (!empathy_conf_get_bool (conf, key, &enabled))
610 priv->reduce_accuracy = enabled;
614 GRand *rand = g_rand_new_with_seed (time (NULL));
615 priv->reduce_value = g_rand_double_range (rand, -0.25, 0.25);
620 priv->reduce_value = 0.0;
623 if (!priv->geoclue_is_setup)
626 geoclue_address_get_address_async (priv->gc_address,
627 initial_address_cb, manager);
628 geoclue_position_get_position_async (priv->gc_position,
629 initial_position_cb, manager);
633 empathy_location_manager_init (EmpathyLocationManager *location_manager)
636 EmpathyLocationManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (location_manager,
637 EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPriv);
639 location_manager->priv = priv;
640 priv->geoclue_is_setup = FALSE;
641 priv->location = g_hash_table_new_full (g_direct_hash, g_direct_equal,
642 g_free, (GDestroyNotify) tp_g_value_slice_free);
644 /* Setup account status callbacks */
645 priv->account_manager = empathy_account_manager_dup_singleton ();
646 g_signal_connect (priv->account_manager,
648 G_CALLBACK (new_connection_cb), location_manager);
650 /* Setup settings status callbacks */
651 conf = empathy_conf_get ();
652 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_PUBLISH, publish_cb,
654 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
655 resource_cb, location_manager);
656 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
657 resource_cb, location_manager);
658 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
659 resource_cb, location_manager);
660 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
661 accuracy_cb, location_manager);
663 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK, location_manager);
664 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL, location_manager);
665 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS, location_manager);
666 accuracy_cb (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY, location_manager);
667 publish_cb (conf, EMPATHY_PREFS_LOCATION_PUBLISH, location_manager);
670 EmpathyLocationManager *
671 empathy_location_manager_dup_singleton (void)
673 return EMPATHY_LOCATION_MANAGER (g_object_new (EMPATHY_TYPE_LOCATION_MANAGER,