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_object_unref (data->self);
255 g_slice_free (PublishToAllData, data);
259 publish_to_all_connections (EmpathyLocationManager *self,
260 gboolean force_publication)
262 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
263 PublishToAllData *data;
265 data = g_slice_new0 (PublishToAllData);
266 data->self = g_object_ref (self);
267 data->force_publication = force_publication;
269 tp_account_manager_prepare_async (priv->account_manager, NULL,
270 publish_to_all_am_prepared_cb, data);
274 publish_on_idle (gpointer user_data)
276 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
277 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
279 priv->timeout_id = 0;
280 publish_to_all_connections (manager, TRUE);
285 new_connection_cb (TpAccount *account,
289 gchar *dbus_error_name,
293 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
296 conn = tp_account_get_connection (account);
298 DEBUG ("New connection %p", conn);
300 /* Don't publish if it is already planned (ie startup) */
301 if (priv->timeout_id == 0)
303 publish_location (EMPATHY_LOCATION_MANAGER (self), conn,
309 update_timestamp (EmpathyLocationManager *self)
311 EmpathyLocationManagerPriv *priv= GET_PRIV (self);
316 timestamp = time (NULL);
317 stamp64 = (gint64) timestamp;
318 new_value = tp_g_value_slice_new_int64 (stamp64);
319 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_TIMESTAMP),
321 DEBUG ("\t - Timestamp: %" G_GINT64_FORMAT, stamp64);
325 address_changed_cb (GeoclueAddress *address,
328 GeoclueAccuracy *accuracy,
331 GeoclueAccuracyLevel level;
332 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
336 geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
337 DEBUG ("New address (accuracy level %d):", level);
338 /* FIXME: Publish accuracy level also considering the position's */
340 g_hash_table_remove (priv->location, EMPATHY_LOCATION_STREET);
341 g_hash_table_remove (priv->location, EMPATHY_LOCATION_AREA);
342 g_hash_table_remove (priv->location, EMPATHY_LOCATION_REGION);
343 g_hash_table_remove (priv->location, EMPATHY_LOCATION_COUNTRY);
344 g_hash_table_remove (priv->location, EMPATHY_LOCATION_COUNTRY_CODE);
345 g_hash_table_remove (priv->location, EMPATHY_LOCATION_POSTAL_CODE);
347 if (g_hash_table_size (details) == 0)
349 DEBUG ("\t - (Empty)");
353 g_hash_table_iter_init (&iter, details);
354 while (g_hash_table_iter_next (&iter, &key, &value))
357 /* Discard street information if reduced accuracy is on */
358 if (priv->reduce_accuracy &&
359 !tp_strdiff (key, EMPATHY_LOCATION_STREET))
362 new_value = tp_g_value_slice_new_string (value);
363 g_hash_table_insert (priv->location, g_strdup (key), new_value);
365 DEBUG ("\t - %s: %s", (gchar *) key, (gchar *) value);
368 update_timestamp (self);
369 if (priv->timeout_id == 0)
370 priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, self);
374 initial_address_cb (GeoclueAddress *address,
377 GeoclueAccuracy *accuracy,
383 DEBUG ("Error: %s", error->message);
384 g_error_free (error);
388 address_changed_cb (address, timestamp, details, accuracy, self);
393 position_changed_cb (GeocluePosition *position,
394 GeocluePositionFields fields,
399 GeoclueAccuracy *accuracy,
402 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
403 GeoclueAccuracyLevel level;
404 gdouble mean, horizontal, vertical;
408 geoclue_accuracy_get_details (accuracy, &level, &horizontal, &vertical);
409 DEBUG ("New position (accuracy level %d)", level);
410 if (level == GEOCLUE_ACCURACY_LEVEL_NONE)
413 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
416 if (priv->reduce_accuracy)
417 /* Truncate at 1 decimal place */
418 longitude = ((int) (longitude * 10)) / 10.0;
420 new_value = tp_g_value_slice_new_double (longitude);
421 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LON),
423 DEBUG ("\t - Longitude: %f", longitude);
427 g_hash_table_remove (priv->location, EMPATHY_LOCATION_LON);
430 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
432 if (priv->reduce_accuracy)
433 /* Truncate at 1 decimal place */
434 latitude = ((int) (latitude * 10)) / 10.0;
436 new_value = tp_g_value_slice_new_double (latitude);
437 g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_LAT),
439 DEBUG ("\t - Latitude: %f", latitude);
443 g_hash_table_remove (priv->location, EMPATHY_LOCATION_LAT);
446 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
448 new_value = tp_g_value_slice_new_double (altitude);
449 g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_ALT),
451 DEBUG ("\t - Altitude: %f", altitude);
455 g_hash_table_remove (priv->location, EMPATHY_LOCATION_ALT);
458 if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
460 mean = (horizontal + vertical) / 2.0;
461 new_value = tp_g_value_slice_new_double (mean);
462 g_hash_table_replace (priv->location,
463 g_strdup (EMPATHY_LOCATION_ACCURACY), new_value);
464 DEBUG ("\t - Accuracy: %f", mean);
468 g_hash_table_remove (priv->location, EMPATHY_LOCATION_ACCURACY);
471 update_timestamp (self);
472 if (priv->timeout_id == 0)
473 priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, self);
477 initial_position_cb (GeocluePosition *position,
478 GeocluePositionFields fields,
483 GeoclueAccuracy *accuracy,
489 DEBUG ("Error: %s", error->message);
490 g_error_free (error);
494 position_changed_cb (position, fields, timestamp, latitude, longitude,
495 altitude, accuracy, self);
500 update_resources (EmpathyLocationManager *self)
502 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
504 DEBUG ("Updating resources %d", priv->resources);
506 if (!priv->geoclue_is_setup)
509 /* As per Geoclue bug #15126, using NONE results in no address
510 * being found as geoclue-manual report an empty address with
512 if (!geoclue_master_client_set_requirements (priv->gc_client,
513 GEOCLUE_ACCURACY_LEVEL_COUNTRY, 0, TRUE, priv->resources,
516 DEBUG ("set_requirements failed");
520 geoclue_address_get_address_async (priv->gc_address,
521 initial_address_cb, self);
522 geoclue_position_get_position_async (priv->gc_position,
523 initial_position_cb, self);
527 setup_geoclue (EmpathyLocationManager *self)
529 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
531 GeoclueMaster *master;
532 GError *error = NULL;
534 DEBUG ("Setting up Geoclue");
535 master = geoclue_master_get_default ();
536 priv->gc_client = geoclue_master_create_client (master, NULL, &error);
537 g_object_unref (master);
539 if (priv->gc_client == NULL)
541 DEBUG ("Failed to GeoclueMasterClient: %s", error->message);
542 g_error_free (error);
546 update_resources (self);
548 /* Get updated when the position is changes */
549 priv->gc_position = geoclue_master_client_create_position (
550 priv->gc_client, &error);
551 if (priv->gc_position == NULL)
553 DEBUG ("Failed to create GeocluePosition: %s", error->message);
554 g_error_free (error);
558 g_signal_connect (G_OBJECT (priv->gc_position), "position-changed",
559 G_CALLBACK (position_changed_cb), self);
561 /* Get updated when the address changes */
562 priv->gc_address = geoclue_master_client_create_address (
563 priv->gc_client, &error);
564 if (priv->gc_address == NULL)
566 DEBUG ("Failed to create GeoclueAddress: %s", error->message);
567 g_error_free (error);
571 g_signal_connect (G_OBJECT (priv->gc_address), "address-changed",
572 G_CALLBACK (address_changed_cb), self);
574 priv->geoclue_is_setup = TRUE;
578 publish_cb (EmpathyConf *conf,
582 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
583 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
584 gboolean can_publish;
586 DEBUG ("Publish Conf changed");
589 if (!empathy_conf_get_bool (conf, key, &can_publish))
594 if (!priv->geoclue_is_setup)
595 setup_geoclue (manager);
596 /* if still not setup than the init failed */
597 if (!priv->geoclue_is_setup)
600 geoclue_address_get_address_async (priv->gc_address,
601 initial_address_cb, manager);
602 geoclue_position_get_position_async (priv->gc_position,
603 initial_position_cb, manager);
607 /* As per XEP-0080: send an empty location to have remove current
608 * location from the servers
610 g_hash_table_remove_all (priv->location);
611 publish_to_all_connections (manager, TRUE);
617 resource_cb (EmpathyConf *conf,
621 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
622 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
623 GeoclueResourceFlags resource = 0;
624 gboolean resource_enabled;
626 DEBUG ("%s changed", key);
628 if (!empathy_conf_get_bool (conf, key, &resource_enabled))
631 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK))
632 resource = GEOCLUE_RESOURCE_NETWORK;
633 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL))
634 resource = GEOCLUE_RESOURCE_CELL;
635 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS))
636 resource = GEOCLUE_RESOURCE_GPS;
638 if (resource_enabled)
639 priv->resources |= resource;
641 priv->resources &= ~resource;
643 if (priv->geoclue_is_setup)
644 update_resources (manager);
648 accuracy_cb (EmpathyConf *conf,
652 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
653 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
657 DEBUG ("%s changed", key);
659 if (!empathy_conf_get_bool (conf, key, &enabled))
661 priv->reduce_accuracy = enabled;
663 if (!priv->geoclue_is_setup)
666 geoclue_address_get_address_async (priv->gc_address,
667 initial_address_cb, manager);
668 geoclue_position_get_position_async (priv->gc_position,
669 initial_position_cb, manager);
673 account_manager_prepared_cb (GObject *source_object,
674 GAsyncResult *result,
678 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
679 EmpathyLocationManager *self = user_data;
680 GError *error = NULL;
682 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
684 DEBUG ("Failed to prepare account manager: %s", error->message);
685 g_error_free (error);
689 accounts = tp_account_manager_get_valid_accounts (account_manager);
690 for (l = accounts; l != NULL; l = l->next)
692 TpAccount *account = TP_ACCOUNT (l->data);
694 empathy_signal_connect_weak (account, "status-changed",
695 G_CALLBACK (new_connection_cb), G_OBJECT (self));
697 g_list_free (accounts);
701 empathy_location_manager_init (EmpathyLocationManager *self)
704 EmpathyLocationManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
705 EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPriv);
708 priv->geoclue_is_setup = FALSE;
709 priv->location = g_hash_table_new_full (g_direct_hash, g_direct_equal,
710 g_free, (GDestroyNotify) tp_g_value_slice_free);
712 /* Setup account status callbacks */
713 priv->account_manager = tp_account_manager_dup ();
715 tp_account_manager_prepare_async (priv->account_manager, NULL,
716 account_manager_prepared_cb, self);
718 /* Setup settings status callbacks */
719 conf = empathy_conf_get ();
720 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_PUBLISH, publish_cb,
722 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
724 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
726 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
728 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
731 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK, self);
732 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL, self);
733 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS, self);
734 accuracy_cb (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY, self);
735 publish_cb (conf, EMPATHY_PREFS_LOCATION_PUBLISH, self);
738 EmpathyLocationManager *
739 empathy_location_manager_dup_singleton (void)
741 return EMPATHY_LOCATION_MANAGER (g_object_new (EMPATHY_TYPE_LOCATION_MANAGER,