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/account-manager.h>
30 #include <telepathy-glib/util.h>
32 #include <geoclue/geoclue-master.h>
34 #include <extensions/extensions.h>
36 #include "empathy-location-manager.h"
37 #include "empathy-conf.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;
66 TpAccountManager *account_manager;
68 /* The idle id for publish_on_idle func */
70 } EmpathyLocationManagerPriv;
72 G_DEFINE_TYPE (EmpathyLocationManager, empathy_location_manager, G_TYPE_OBJECT);
75 location_manager_constructor (GType type,
76 guint n_construct_params,
77 GObjectConstructParam *construct_params)
81 if (location_manager == NULL)
83 retval = G_OBJECT_CLASS (empathy_location_manager_parent_class)->constructor
84 (type, n_construct_params, construct_params);
86 location_manager = EMPATHY_LOCATION_MANAGER (retval);
87 g_object_add_weak_pointer (retval, (gpointer) &location_manager);
91 retval = g_object_ref (location_manager);
98 location_manager_dispose (GObject *object)
100 EmpathyLocationManagerPriv *priv = GET_PRIV (object);
102 if (priv->account_manager != NULL)
104 g_object_unref (priv->account_manager);
105 priv->account_manager = NULL;
108 if (priv->gc_client != NULL)
110 g_object_unref (priv->gc_client);
111 priv->gc_client = NULL;
114 if (priv->gc_position != NULL)
116 g_object_unref (priv->gc_position);
117 priv->gc_position = NULL;
120 if (priv->gc_address != NULL)
122 g_object_unref (priv->gc_address);
123 priv->gc_address = NULL;
126 if (priv->location != NULL)
128 g_hash_table_unref (priv->location);
129 priv->location = NULL;
132 G_OBJECT_CLASS (empathy_location_manager_parent_class)->finalize (object);
136 location_manager_get_property (GObject *object,
141 /*EmpathyLocationManagerPriv *priv = GET_PRIV (object); */
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
152 location_manager_set_property (GObject *object,
157 /* EmpathyLocationManagerPriv *priv = GET_PRIV (object); */
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
168 empathy_location_manager_class_init (EmpathyLocationManagerClass *class)
170 GObjectClass *object_class;
172 object_class = G_OBJECT_CLASS (class);
174 object_class->constructor = location_manager_constructor;
175 object_class->dispose = location_manager_dispose;
176 object_class->get_property = location_manager_get_property;
177 object_class->set_property = location_manager_set_property;
179 g_type_class_add_private (object_class, sizeof (EmpathyLocationManagerPriv));
183 publish_location (EmpathyLocationManager *self,
185 gboolean force_publication)
187 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
188 guint connection_status = -1;
189 gboolean can_publish;
190 EmpathyConf *conf = empathy_conf_get ();
191 EmpathyTpContactFactory *factory;
196 if (!force_publication)
198 if (!empathy_conf_get_bool (conf, EMPATHY_PREFS_LOCATION_PUBLISH,
206 connection_status = tp_connection_get_status (conn, NULL);
208 if (connection_status != TP_CONNECTION_STATUS_CONNECTED)
211 DEBUG ("Publishing %s location to connection %p",
212 (g_hash_table_size (priv->location) == 0 ? "empty" : ""),
215 factory = empathy_tp_contact_factory_dup_singleton (conn);
216 empathy_tp_contact_factory_set_location (factory, priv->location);
217 g_object_unref (factory);
222 EmpathyLocationManager *self;
223 gboolean force_publication;
227 publish_to_all_am_prepared_cb (GObject *source_object,
228 GAsyncResult *result,
231 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
232 PublishToAllData *data = user_data;
234 GError *error = NULL;
236 if (!tp_account_manager_prepare_finish (manager, result, &error))
238 DEBUG ("Failed to prepare account manager: %s", error->message);
239 g_error_free (error);
243 accounts = tp_account_manager_get_valid_accounts (manager);
244 for (l = accounts; l; l = l->next)
246 TpConnection *conn = tp_account_get_connection (TP_ACCOUNT (l->data));
249 publish_location (data->self, conn, data->force_publication);
251 g_list_free (accounts);
254 g_slice_free (PublishToAllData, data);
258 publish_to_all_connections (EmpathyLocationManager *self,
259 gboolean force_publication)
261 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
262 PublishToAllData *data;
264 data = g_slice_new0 (PublishToAllData);
266 data->force_publication = force_publication;
268 tp_account_manager_prepare_async (priv->account_manager, NULL,
269 publish_to_all_am_prepared_cb, data);
273 publish_on_idle (gpointer user_data)
275 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
276 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
278 priv->timeout_id = 0;
279 publish_to_all_connections (manager, TRUE);
284 new_connection_cb (TpAccount *account,
288 gchar *dbus_error_name,
292 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
295 conn = tp_account_get_connection (account);
297 DEBUG ("New connection %p", conn);
299 /* Don't publish if it is already planned (ie startup) */
300 if (priv->timeout_id == 0)
302 publish_location (EMPATHY_LOCATION_MANAGER (self), conn,
308 update_timestamp (EmpathyLocationManager *self)
310 EmpathyLocationManagerPriv *priv= GET_PRIV (self);
315 timestamp = time (NULL);
316 stamp64 = (gint64) timestamp;
317 new_value = tp_g_value_slice_new_int64 (stamp64);
318 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_TIMESTAMP),
320 DEBUG ("\t - Timestamp: %" G_GINT64_FORMAT, stamp64);
324 address_changed_cb (GeoclueAddress *address,
327 GeoclueAccuracy *accuracy,
330 GeoclueAccuracyLevel level;
331 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
335 geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
336 DEBUG ("New address (accuracy level %d):", level);
337 /* FIXME: Publish accuracy level also considering the position's */
339 g_hash_table_remove (priv->location, EMPATHY_LOCATION_STREET);
340 g_hash_table_remove (priv->location, EMPATHY_LOCATION_AREA);
341 g_hash_table_remove (priv->location, EMPATHY_LOCATION_REGION);
342 g_hash_table_remove (priv->location, EMPATHY_LOCATION_COUNTRY);
343 g_hash_table_remove (priv->location, EMPATHY_LOCATION_COUNTRY_CODE);
344 g_hash_table_remove (priv->location, EMPATHY_LOCATION_POSTAL_CODE);
346 if (g_hash_table_size (details) == 0)
348 DEBUG ("\t - (Empty)");
352 g_hash_table_iter_init (&iter, details);
353 while (g_hash_table_iter_next (&iter, &key, &value))
356 /* Discard street information if reduced accuracy is on */
357 if (priv->reduce_accuracy &&
358 !tp_strdiff (key, EMPATHY_LOCATION_STREET))
361 new_value = tp_g_value_slice_new_string (value);
362 g_hash_table_insert (priv->location, g_strdup (key), new_value);
364 DEBUG ("\t - %s: %s", (gchar *) key, (gchar *) value);
367 update_timestamp (self);
368 if (priv->timeout_id == 0)
369 priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, self);
373 initial_address_cb (GeoclueAddress *address,
376 GeoclueAccuracy *accuracy,
382 DEBUG ("Error: %s", error->message);
383 g_error_free (error);
387 address_changed_cb (address, timestamp, details, accuracy, self);
392 position_changed_cb (GeocluePosition *position,
393 GeocluePositionFields fields,
398 GeoclueAccuracy *accuracy,
401 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
402 GeoclueAccuracyLevel level;
403 gdouble mean, horizontal, vertical;
407 geoclue_accuracy_get_details (accuracy, &level, &horizontal, &vertical);
408 DEBUG ("New position (accuracy level %d)", level);
409 if (level == GEOCLUE_ACCURACY_LEVEL_NONE)
412 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
415 if (priv->reduce_accuracy)
416 /* Truncate at 1 decimal place */
417 longitude = ((int) (longitude * 10)) / 10.0;
419 new_value = tp_g_value_slice_new_double (longitude);
420 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LON),
422 DEBUG ("\t - Longitude: %f", longitude);
426 g_hash_table_remove (priv->location, EMPATHY_LOCATION_LON);
429 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
431 if (priv->reduce_accuracy)
432 /* Truncate at 1 decimal place */
433 latitude = ((int) (latitude * 10)) / 10.0;
435 new_value = tp_g_value_slice_new_double (latitude);
436 g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_LAT),
438 DEBUG ("\t - Latitude: %f", latitude);
442 g_hash_table_remove (priv->location, EMPATHY_LOCATION_LAT);
445 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
447 new_value = tp_g_value_slice_new_double (altitude);
448 g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_ALT),
450 DEBUG ("\t - Altitude: %f", altitude);
454 g_hash_table_remove (priv->location, EMPATHY_LOCATION_ALT);
457 if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
459 mean = (horizontal + vertical) / 2.0;
460 new_value = tp_g_value_slice_new_double (mean);
461 g_hash_table_replace (priv->location,
462 g_strdup (EMPATHY_LOCATION_ACCURACY), new_value);
463 DEBUG ("\t - Accuracy: %f", mean);
467 g_hash_table_remove (priv->location, EMPATHY_LOCATION_ACCURACY);
470 update_timestamp (self);
471 if (priv->timeout_id == 0)
472 priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, self);
476 initial_position_cb (GeocluePosition *position,
477 GeocluePositionFields fields,
482 GeoclueAccuracy *accuracy,
488 DEBUG ("Error: %s", error->message);
489 g_error_free (error);
493 position_changed_cb (position, fields, timestamp, latitude, longitude,
494 altitude, accuracy, self);
499 update_resources (EmpathyLocationManager *self)
501 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
503 DEBUG ("Updating resources %d", priv->resources);
505 if (!priv->geoclue_is_setup)
508 /* As per Geoclue bug #15126, using NONE results in no address
509 * being found as geoclue-manual report an empty address with
511 if (!geoclue_master_client_set_requirements (priv->gc_client,
512 GEOCLUE_ACCURACY_LEVEL_COUNTRY, 0, TRUE, priv->resources,
515 DEBUG ("set_requirements failed");
519 geoclue_address_get_address_async (priv->gc_address,
520 initial_address_cb, self);
521 geoclue_position_get_position_async (priv->gc_position,
522 initial_position_cb, self);
526 setup_geoclue (EmpathyLocationManager *self)
528 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
530 GeoclueMaster *master;
531 GError *error = NULL;
533 DEBUG ("Setting up Geoclue");
534 master = geoclue_master_get_default ();
535 priv->gc_client = geoclue_master_create_client (master, NULL, &error);
536 g_object_unref (master);
538 if (priv->gc_client == NULL)
540 DEBUG ("Failed to GeoclueMasterClient: %s", error->message);
541 g_error_free (error);
545 update_resources (self);
547 /* Get updated when the position is changes */
548 priv->gc_position = geoclue_master_client_create_position (
549 priv->gc_client, &error);
550 if (priv->gc_position == NULL)
552 DEBUG ("Failed to create GeocluePosition: %s", error->message);
553 g_error_free (error);
557 g_signal_connect (G_OBJECT (priv->gc_position), "position-changed",
558 G_CALLBACK (position_changed_cb), self);
560 /* Get updated when the address changes */
561 priv->gc_address = geoclue_master_client_create_address (
562 priv->gc_client, &error);
563 if (priv->gc_address == NULL)
565 DEBUG ("Failed to create GeoclueAddress: %s", error->message);
566 g_error_free (error);
570 g_signal_connect (G_OBJECT (priv->gc_address), "address-changed",
571 G_CALLBACK (address_changed_cb), self);
573 priv->geoclue_is_setup = TRUE;
577 publish_cb (EmpathyConf *conf,
581 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
582 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
583 gboolean can_publish;
585 DEBUG ("Publish Conf changed");
588 if (!empathy_conf_get_bool (conf, key, &can_publish))
593 if (!priv->geoclue_is_setup)
594 setup_geoclue (manager);
595 /* if still not setup than the init failed */
596 if (!priv->geoclue_is_setup)
599 geoclue_address_get_address_async (priv->gc_address,
600 initial_address_cb, manager);
601 geoclue_position_get_position_async (priv->gc_position,
602 initial_position_cb, manager);
606 /* As per XEP-0080: send an empty location to have remove current
607 * location from the servers
609 g_hash_table_remove_all (priv->location);
610 publish_to_all_connections (manager, TRUE);
616 resource_cb (EmpathyConf *conf,
620 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
621 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
622 GeoclueResourceFlags resource = 0;
623 gboolean resource_enabled;
625 DEBUG ("%s changed", key);
627 if (!empathy_conf_get_bool (conf, key, &resource_enabled))
630 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK))
631 resource = GEOCLUE_RESOURCE_NETWORK;
632 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL))
633 resource = GEOCLUE_RESOURCE_CELL;
634 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS))
635 resource = GEOCLUE_RESOURCE_GPS;
637 if (resource_enabled)
638 priv->resources |= resource;
640 priv->resources &= ~resource;
642 if (priv->geoclue_is_setup)
643 update_resources (manager);
647 accuracy_cb (EmpathyConf *conf,
651 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
652 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
656 DEBUG ("%s changed", key);
658 if (!empathy_conf_get_bool (conf, key, &enabled))
660 priv->reduce_accuracy = enabled;
662 if (!priv->geoclue_is_setup)
665 geoclue_address_get_address_async (priv->gc_address,
666 initial_address_cb, manager);
667 geoclue_position_get_position_async (priv->gc_position,
668 initial_position_cb, manager);
672 account_manager_prepared_cb (GObject *source_object,
673 GAsyncResult *result,
677 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
678 EmpathyLocationManager *self = user_data;
679 GError *error = NULL;
681 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
683 DEBUG ("Failed to prepare account manager: %s", error->message);
684 g_error_free (error);
688 accounts = tp_account_manager_get_valid_accounts (account_manager);
689 for (l = accounts; l != NULL; l = l->next)
691 TpAccount *account = TP_ACCOUNT (l->data);
693 empathy_signal_connect_weak (account, "status-changed",
694 G_CALLBACK (new_connection_cb), G_OBJECT (self));
696 g_list_free (accounts);
700 empathy_location_manager_init (EmpathyLocationManager *self)
703 EmpathyLocationManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
704 EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPriv);
707 priv->geoclue_is_setup = FALSE;
708 priv->location = g_hash_table_new_full (g_direct_hash, g_direct_equal,
709 g_free, (GDestroyNotify) tp_g_value_slice_free);
711 /* Setup account status callbacks */
712 priv->account_manager = tp_account_manager_dup ();
714 tp_account_manager_prepare_async (priv->account_manager, NULL,
715 account_manager_prepared_cb, self);
717 /* Setup settings status callbacks */
718 conf = empathy_conf_get ();
719 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_PUBLISH, publish_cb,
721 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
723 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
725 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
727 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
730 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK, self);
731 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL, self);
732 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS, self);
733 accuracy_cb (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY, self);
734 publish_cb (conf, EMPATHY_PREFS_LOCATION_PUBLISH, self);
737 EmpathyLocationManager *
738 empathy_location_manager_dup_singleton (void)
740 return EMPATHY_LOCATION_MANAGER (g_object_new (EMPATHY_TYPE_LOCATION_MANAGER,