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"
38 #include "libempathy/empathy-enum-types.h"
39 #include "libempathy/empathy-gsettings.h"
40 #include "libempathy/empathy-location.h"
41 #include "libempathy/empathy-utils.h"
43 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
44 #include "libempathy/empathy-debug.h"
46 /* Seconds before updating the location */
48 static EmpathyLocationManager *location_manager = NULL;
50 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLocationManager)
52 gboolean geoclue_is_setup;
53 /* Contains the location to be sent to accounts. Geoclue is used
54 * to populate it. This HashTable uses Telepathy's style (string,
55 * GValue). Keys are defined in empathy-location.h
59 GSettings *gsettings_loc;
61 GeoclueResourceFlags resources;
62 GeoclueMasterClient *gc_client;
63 GeocluePosition *gc_position;
64 GeoclueAddress *gc_address;
66 gboolean reduce_accuracy;
67 TpAccountManager *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);
102 void (*dispose) (GObject *) =
103 G_OBJECT_CLASS (empathy_location_manager_parent_class)->dispose;
105 if (priv->account_manager != NULL)
107 g_object_unref (priv->account_manager);
108 priv->account_manager = NULL;
111 if (priv->gsettings_loc != NULL)
113 g_object_unref (priv->gsettings_loc);
114 priv->gsettings_loc = NULL;
117 if (priv->gc_client != NULL)
119 g_object_unref (priv->gc_client);
120 priv->gc_client = NULL;
123 if (priv->gc_position != NULL)
125 g_object_unref (priv->gc_position);
126 priv->gc_position = NULL;
129 if (priv->gc_address != NULL)
131 g_object_unref (priv->gc_address);
132 priv->gc_address = NULL;
135 if (priv->location != NULL)
137 g_hash_table_unref (priv->location);
138 priv->location = NULL;
146 location_manager_get_property (GObject *object,
151 /*EmpathyLocationManagerPriv *priv = GET_PRIV (object); */
156 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
162 location_manager_set_property (GObject *object,
167 /* EmpathyLocationManagerPriv *priv = GET_PRIV (object); */
172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
178 empathy_location_manager_class_init (EmpathyLocationManagerClass *class)
180 GObjectClass *object_class;
182 object_class = G_OBJECT_CLASS (class);
184 object_class->constructor = location_manager_constructor;
185 object_class->dispose = location_manager_dispose;
186 object_class->get_property = location_manager_get_property;
187 object_class->set_property = location_manager_set_property;
189 g_type_class_add_private (object_class, sizeof (EmpathyLocationManagerPriv));
193 publish_location_cb (TpConnection *connection,
196 GObject *weak_object)
199 DEBUG ("Error setting location: %s", error->message);
203 publish_location (EmpathyLocationManager *self,
205 gboolean force_publication)
207 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
208 guint connection_status = -1;
213 if (!force_publication)
215 if (!g_settings_get_boolean (priv->gsettings_loc,
216 EMPATHY_PREFS_LOCATION_PUBLISH))
220 connection_status = tp_connection_get_status (conn, NULL);
222 if (connection_status != TP_CONNECTION_STATUS_CONNECTED)
225 DEBUG ("Publishing %s location to connection %p",
226 (g_hash_table_size (priv->location) == 0 ? "empty" : ""),
229 tp_cli_connection_interface_location_call_set_location (conn, -1,
230 priv->location, publish_location_cb, NULL, NULL, G_OBJECT (self));
235 EmpathyLocationManager *self;
236 gboolean force_publication;
240 publish_to_all_am_prepared_cb (GObject *source_object,
241 GAsyncResult *result,
244 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
245 PublishToAllData *data = user_data;
247 GError *error = NULL;
249 if (!tp_account_manager_prepare_finish (manager, result, &error))
251 DEBUG ("Failed to prepare account manager: %s", error->message);
252 g_error_free (error);
256 accounts = tp_account_manager_get_valid_accounts (manager);
257 for (l = accounts; l; l = l->next)
259 TpConnection *conn = tp_account_get_connection (TP_ACCOUNT (l->data));
262 publish_location (data->self, conn, data->force_publication);
264 g_list_free (accounts);
267 g_object_unref (data->self);
268 g_slice_free (PublishToAllData, data);
272 publish_to_all_connections (EmpathyLocationManager *self,
273 gboolean force_publication)
275 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
276 PublishToAllData *data;
278 data = g_slice_new0 (PublishToAllData);
279 data->self = g_object_ref (self);
280 data->force_publication = force_publication;
282 tp_account_manager_prepare_async (priv->account_manager, NULL,
283 publish_to_all_am_prepared_cb, data);
287 publish_on_idle (gpointer user_data)
289 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
290 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
292 priv->timeout_id = 0;
293 publish_to_all_connections (manager, TRUE);
298 new_connection_cb (TpAccount *account,
302 gchar *dbus_error_name,
306 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
309 conn = tp_account_get_connection (account);
311 DEBUG ("New connection %p", conn);
313 /* Don't publish if it is already planned (ie startup) */
314 if (priv->timeout_id == 0)
316 publish_location (EMPATHY_LOCATION_MANAGER (self), conn,
322 update_timestamp (EmpathyLocationManager *self)
324 EmpathyLocationManagerPriv *priv= GET_PRIV (self);
329 timestamp = time (NULL);
330 stamp64 = (gint64) timestamp;
331 new_value = tp_g_value_slice_new_int64 (stamp64);
332 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_TIMESTAMP),
334 DEBUG ("\t - Timestamp: %" G_GINT64_FORMAT, stamp64);
338 address_changed_cb (GeoclueAddress *address,
341 GeoclueAccuracy *accuracy,
344 GeoclueAccuracyLevel level;
345 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
349 geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
350 DEBUG ("New address (accuracy level %d):", level);
351 /* FIXME: Publish accuracy level also considering the position's */
353 g_hash_table_remove (priv->location, EMPATHY_LOCATION_STREET);
354 g_hash_table_remove (priv->location, EMPATHY_LOCATION_AREA);
355 g_hash_table_remove (priv->location, EMPATHY_LOCATION_REGION);
356 g_hash_table_remove (priv->location, EMPATHY_LOCATION_COUNTRY);
357 g_hash_table_remove (priv->location, EMPATHY_LOCATION_COUNTRY_CODE);
358 g_hash_table_remove (priv->location, EMPATHY_LOCATION_POSTAL_CODE);
360 if (g_hash_table_size (details) == 0)
362 DEBUG ("\t - (Empty)");
366 g_hash_table_iter_init (&iter, details);
367 while (g_hash_table_iter_next (&iter, &key, &value))
370 /* Discard street information if reduced accuracy is on */
371 if (priv->reduce_accuracy &&
372 !tp_strdiff (key, EMPATHY_LOCATION_STREET))
375 new_value = tp_g_value_slice_new_string (value);
376 g_hash_table_insert (priv->location, g_strdup (key), new_value);
378 DEBUG ("\t - %s: %s", (gchar *) key, (gchar *) value);
381 update_timestamp (self);
382 if (priv->timeout_id == 0)
383 priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, self);
387 initial_address_cb (GeoclueAddress *address,
390 GeoclueAccuracy *accuracy,
396 DEBUG ("Error: %s", error->message);
397 g_error_free (error);
401 address_changed_cb (address, timestamp, details, accuracy, self);
406 position_changed_cb (GeocluePosition *position,
407 GeocluePositionFields fields,
412 GeoclueAccuracy *accuracy,
415 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
416 GeoclueAccuracyLevel level;
417 gdouble mean, horizontal, vertical;
421 geoclue_accuracy_get_details (accuracy, &level, &horizontal, &vertical);
422 DEBUG ("New position (accuracy level %d)", level);
423 if (level == GEOCLUE_ACCURACY_LEVEL_NONE)
426 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
429 if (priv->reduce_accuracy)
430 /* Truncate at 1 decimal place */
431 longitude = ((int) (longitude * 10)) / 10.0;
433 new_value = tp_g_value_slice_new_double (longitude);
434 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LON),
436 DEBUG ("\t - Longitude: %f", longitude);
440 g_hash_table_remove (priv->location, EMPATHY_LOCATION_LON);
443 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
445 if (priv->reduce_accuracy)
446 /* Truncate at 1 decimal place */
447 latitude = ((int) (latitude * 10)) / 10.0;
449 new_value = tp_g_value_slice_new_double (latitude);
450 g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_LAT),
452 DEBUG ("\t - Latitude: %f", latitude);
456 g_hash_table_remove (priv->location, EMPATHY_LOCATION_LAT);
459 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
461 new_value = tp_g_value_slice_new_double (altitude);
462 g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_ALT),
464 DEBUG ("\t - Altitude: %f", altitude);
468 g_hash_table_remove (priv->location, EMPATHY_LOCATION_ALT);
471 if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
473 mean = (horizontal + vertical) / 2.0;
474 new_value = tp_g_value_slice_new_double (mean);
475 g_hash_table_replace (priv->location,
476 g_strdup (EMPATHY_LOCATION_ACCURACY), new_value);
477 DEBUG ("\t - Accuracy: %f", mean);
481 g_hash_table_remove (priv->location, EMPATHY_LOCATION_ACCURACY);
484 update_timestamp (self);
485 if (priv->timeout_id == 0)
486 priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, self);
490 initial_position_cb (GeocluePosition *position,
491 GeocluePositionFields fields,
496 GeoclueAccuracy *accuracy,
502 DEBUG ("Error: %s", error->message);
503 g_error_free (error);
507 position_changed_cb (position, fields, timestamp, latitude, longitude,
508 altitude, accuracy, self);
513 set_requirements (EmpathyLocationManager *self)
515 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
516 GError *error = NULL;
518 if (!geoclue_master_client_set_requirements (priv->gc_client,
519 GEOCLUE_ACCURACY_LEVEL_COUNTRY, 0, FALSE, priv->resources,
522 DEBUG ("set_requirements failed: %s", error->message);
523 g_error_free (error);
531 update_resources (EmpathyLocationManager *self)
533 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
535 DEBUG ("Updating resources %d", priv->resources);
537 if (!priv->geoclue_is_setup)
540 /* As per Geoclue bug #15126, using NONE results in no address
541 * being found as geoclue-manual report an empty address with
543 if (!set_requirements (self))
546 geoclue_address_get_address_async (priv->gc_address,
547 initial_address_cb, self);
548 geoclue_position_get_position_async (priv->gc_position,
549 initial_position_cb, self);
553 setup_geoclue (EmpathyLocationManager *self)
555 EmpathyLocationManagerPriv *priv = GET_PRIV (self);
557 GeoclueMaster *master;
558 GError *error = NULL;
560 DEBUG ("Setting up Geoclue");
561 master = geoclue_master_get_default ();
562 priv->gc_client = geoclue_master_create_client (master, NULL, &error);
563 g_object_unref (master);
565 if (priv->gc_client == NULL)
567 DEBUG ("Failed to GeoclueMasterClient: %s", error->message);
568 g_error_free (error);
572 if (!set_requirements (self))
575 /* Get updated when the position is changes */
576 priv->gc_position = geoclue_master_client_create_position (
577 priv->gc_client, &error);
578 if (priv->gc_position == NULL)
580 DEBUG ("Failed to create GeocluePosition: %s", error->message);
581 g_error_free (error);
585 g_signal_connect (G_OBJECT (priv->gc_position), "position-changed",
586 G_CALLBACK (position_changed_cb), self);
588 /* Get updated when the address changes */
589 priv->gc_address = geoclue_master_client_create_address (
590 priv->gc_client, &error);
591 if (priv->gc_address == NULL)
593 DEBUG ("Failed to create GeoclueAddress: %s", error->message);
594 g_error_free (error);
598 g_signal_connect (G_OBJECT (priv->gc_address), "address-changed",
599 G_CALLBACK (address_changed_cb), self);
601 priv->geoclue_is_setup = TRUE;
605 publish_cb (GSettings *gsettings_loc,
609 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
610 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
612 DEBUG ("Publish Conf changed");
614 if (g_settings_get_boolean (gsettings_loc, key))
616 if (!priv->geoclue_is_setup)
617 setup_geoclue (manager);
618 /* if still not setup than the init failed */
619 if (!priv->geoclue_is_setup)
622 geoclue_address_get_address_async (priv->gc_address,
623 initial_address_cb, manager);
624 geoclue_position_get_position_async (priv->gc_position,
625 initial_position_cb, manager);
629 /* As per XEP-0080: send an empty location to have remove current
630 * location from the servers
632 g_hash_table_remove_all (priv->location);
633 publish_to_all_connections (manager, TRUE);
639 resource_cb (GSettings *gsettings_loc,
643 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
644 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
645 GeoclueResourceFlags resource = 0;
647 DEBUG ("%s changed", key);
649 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK))
650 resource = GEOCLUE_RESOURCE_NETWORK;
651 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL))
652 resource = GEOCLUE_RESOURCE_CELL;
653 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS))
654 resource = GEOCLUE_RESOURCE_GPS;
656 if (g_settings_get_boolean (gsettings_loc, key))
657 priv->resources |= resource;
659 priv->resources &= ~resource;
661 if (priv->geoclue_is_setup)
662 update_resources (manager);
666 accuracy_cb (GSettings *gsettings_loc,
670 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
671 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
673 DEBUG ("%s changed", key);
675 priv->reduce_accuracy = g_settings_get_boolean (gsettings_loc, key);
677 if (!priv->geoclue_is_setup)
680 geoclue_address_get_address_async (priv->gc_address,
681 initial_address_cb, manager);
682 geoclue_position_get_position_async (priv->gc_position,
683 initial_position_cb, manager);
687 account_manager_prepared_cb (GObject *source_object,
688 GAsyncResult *result,
692 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
693 EmpathyLocationManager *self = user_data;
694 GError *error = NULL;
696 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
698 DEBUG ("Failed to prepare account manager: %s", error->message);
699 g_error_free (error);
703 accounts = tp_account_manager_get_valid_accounts (account_manager);
704 for (l = accounts; l != NULL; l = l->next)
706 TpAccount *account = TP_ACCOUNT (l->data);
708 tp_g_signal_connect_object (account, "status-changed",
709 G_CALLBACK (new_connection_cb), self, 0);
711 g_list_free (accounts);
715 empathy_location_manager_init (EmpathyLocationManager *self)
717 EmpathyLocationManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
718 EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPriv);
721 priv->geoclue_is_setup = FALSE;
722 priv->location = g_hash_table_new_full (g_direct_hash, g_direct_equal,
723 g_free, (GDestroyNotify) tp_g_value_slice_free);
724 priv->gsettings_loc = g_settings_new (EMPATHY_PREFS_LOCATION_SCHEMA);
726 /* Setup account status callbacks */
727 priv->account_manager = tp_account_manager_dup ();
729 tp_account_manager_prepare_async (priv->account_manager, NULL,
730 account_manager_prepared_cb, self);
732 /* Setup settings status callbacks */
733 g_signal_connect (priv->gsettings_loc,
734 "changed::" EMPATHY_PREFS_LOCATION_PUBLISH,
735 G_CALLBACK (publish_cb), self);
736 g_signal_connect (priv->gsettings_loc,
737 "changed::" EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
738 G_CALLBACK (resource_cb), self);
739 g_signal_connect (priv->gsettings_loc,
740 "changed::" EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
741 G_CALLBACK (resource_cb), self);
742 g_signal_connect (priv->gsettings_loc,
743 "changed::" EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
744 G_CALLBACK (resource_cb), self);
745 g_signal_connect (priv->gsettings_loc,
746 "changed::" EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
747 G_CALLBACK (accuracy_cb), self);
749 resource_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
751 resource_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_RESOURCE_CELL, self);
752 resource_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_RESOURCE_GPS, self);
753 accuracy_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
755 publish_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_PUBLISH, self);
758 EmpathyLocationManager *
759 empathy_location_manager_dup_singleton (void)
761 return EMPATHY_LOCATION_MANAGER (g_object_new (EMPATHY_TYPE_LOCATION_MANAGER,