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-lib.h>
29 #include <telepathy-glib/telepathy-glib.h>
31 #include <geoclue/geoclue-master.h>
33 #include <extensions/extensions.h>
35 #include "empathy-location-manager.h"
37 #include "libempathy/empathy-enum-types.h"
38 #include "libempathy/empathy-gsettings.h"
39 #include "libempathy/empathy-location.h"
40 #include "libempathy/empathy-utils.h"
41 #include "libempathy/empathy-time.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 struct _EmpathyLocationManagerPrivate {
51 gboolean geoclue_is_setup;
52 /* Contains the location to be sent to accounts. Geoclue is used
53 * to populate it. This HashTable uses Telepathy's style (string,
54 * GValue). Keys are defined in empathy-location.h
58 GSettings *gsettings_loc;
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 */
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 EmpathyLocationManager *self = (EmpathyLocationManager *) object;
101 void (*dispose) (GObject *) =
102 G_OBJECT_CLASS (empathy_location_manager_parent_class)->dispose;
104 tp_clear_object (&self->priv->account_manager);
105 tp_clear_object (&self->priv->gsettings_loc);
106 tp_clear_object (&self->priv->gc_client);
107 tp_clear_object (&self->priv->gc_position);
108 tp_clear_object (&self->priv->gc_address);
109 tp_clear_pointer (&self->priv->location, g_hash_table_unref);
116 empathy_location_manager_class_init (EmpathyLocationManagerClass *class)
118 GObjectClass *object_class;
120 object_class = G_OBJECT_CLASS (class);
122 object_class->constructor = location_manager_constructor;
123 object_class->dispose = location_manager_dispose;
125 g_type_class_add_private (object_class, sizeof (EmpathyLocationManagerPrivate));
129 publish_location_cb (TpConnection *connection,
132 GObject *weak_object)
135 DEBUG ("Error setting location: %s", error->message);
139 publish_location (EmpathyLocationManager *self,
141 gboolean force_publication)
143 guint connection_status = -1;
148 if (!force_publication)
150 if (!g_settings_get_boolean (self->priv->gsettings_loc,
151 EMPATHY_PREFS_LOCATION_PUBLISH))
155 connection_status = tp_connection_get_status (conn, NULL);
157 if (connection_status != TP_CONNECTION_STATUS_CONNECTED)
160 DEBUG ("Publishing %s location to connection %p",
161 (g_hash_table_size (self->priv->location) == 0 ? "empty" : ""),
164 tp_cli_connection_interface_location_call_set_location (conn, -1,
165 self->priv->location, publish_location_cb, NULL, NULL, G_OBJECT (self));
170 EmpathyLocationManager *self;
171 gboolean force_publication;
175 publish_to_all_am_prepared_cb (GObject *source_object,
176 GAsyncResult *result,
179 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
180 PublishToAllData *data = user_data;
182 GError *error = NULL;
184 if (!tp_proxy_prepare_finish (manager, result, &error))
186 DEBUG ("Failed to prepare account manager: %s", error->message);
187 g_error_free (error);
191 accounts = tp_account_manager_dup_valid_accounts (manager);
192 for (l = accounts; l; l = l->next)
194 TpConnection *conn = tp_account_get_connection (TP_ACCOUNT (l->data));
197 publish_location (data->self, conn, data->force_publication);
199 g_list_free_full (accounts, g_object_unref);
202 g_object_unref (data->self);
203 g_slice_free (PublishToAllData, data);
207 publish_to_all_connections (EmpathyLocationManager *self,
208 gboolean force_publication)
210 PublishToAllData *data;
212 data = g_slice_new0 (PublishToAllData);
213 data->self = g_object_ref (self);
214 data->force_publication = force_publication;
216 tp_proxy_prepare_async (self->priv->account_manager, NULL,
217 publish_to_all_am_prepared_cb, data);
221 publish_on_idle (gpointer user_data)
223 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
225 manager->priv->timeout_id = 0;
226 publish_to_all_connections (manager, TRUE);
231 new_connection_cb (TpAccount *account,
235 gchar *dbus_error_name,
239 EmpathyLocationManager *self = user_data;
242 conn = tp_account_get_connection (account);
244 DEBUG ("New connection %p", conn);
246 /* Don't publish if it is already planned (ie startup) */
247 if (self->priv->timeout_id == 0)
249 publish_location (EMPATHY_LOCATION_MANAGER (self), conn,
255 update_timestamp (EmpathyLocationManager *self)
259 timestamp = empathy_time_get_current ();
260 tp_asv_set_int64 (self->priv->location, EMPATHY_LOCATION_TIMESTAMP,
263 DEBUG ("\t - Timestamp: %" G_GINT64_FORMAT, timestamp);
267 address_changed_cb (GeoclueAddress *address,
270 GeoclueAccuracy *accuracy,
273 EmpathyLocationManager *self = user_data;
274 GeoclueAccuracyLevel level;
278 geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
279 DEBUG ("New address (accuracy level %d):", level);
280 /* FIXME: Publish accuracy level also considering the position's */
282 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_STREET);
283 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_AREA);
284 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_REGION);
285 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_COUNTRY);
286 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_COUNTRY_CODE);
287 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_POSTAL_CODE);
289 if (g_hash_table_size (details) == 0)
291 DEBUG ("\t - (Empty)");
295 g_hash_table_iter_init (&iter, details);
296 while (g_hash_table_iter_next (&iter, &key, &value))
298 /* Discard street information if reduced accuracy is on */
299 if (self->priv->reduce_accuracy &&
300 !tp_strdiff (key, EMPATHY_LOCATION_STREET))
303 tp_asv_set_string (self->priv->location, key, value);
305 DEBUG ("\t - %s: %s", (gchar *) key, (gchar *) value);
308 update_timestamp (self);
309 if (self->priv->timeout_id == 0)
310 self->priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle,
315 initial_address_cb (GeoclueAddress *address,
318 GeoclueAccuracy *accuracy,
324 DEBUG ("Error: %s", error->message);
325 g_error_free (error);
329 address_changed_cb (address, timestamp, details, accuracy, self);
334 position_changed_cb (GeocluePosition *position,
335 GeocluePositionFields fields,
340 GeoclueAccuracy *accuracy,
343 EmpathyLocationManager *self = user_data;
344 GeoclueAccuracyLevel level;
345 gdouble mean, horizontal, vertical;
347 geoclue_accuracy_get_details (accuracy, &level, &horizontal, &vertical);
348 DEBUG ("New position (accuracy level %d)", level);
349 if (level == GEOCLUE_ACCURACY_LEVEL_NONE)
352 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
355 if (self->priv->reduce_accuracy)
356 /* Truncate at 1 decimal place */
357 longitude = ((int) (longitude * 10)) / 10.0;
359 tp_asv_set_double (self->priv->location, EMPATHY_LOCATION_LON, longitude);
361 DEBUG ("\t - Longitude: %f", longitude);
365 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_LON);
368 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
370 if (self->priv->reduce_accuracy)
371 /* Truncate at 1 decimal place */
372 latitude = ((int) (latitude * 10)) / 10.0;
374 tp_asv_set_double (self->priv->location, EMPATHY_LOCATION_LAT, latitude);
376 DEBUG ("\t - Latitude: %f", latitude);
380 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_LAT);
383 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
385 tp_asv_set_double (self->priv->location, EMPATHY_LOCATION_ALT, altitude);
387 DEBUG ("\t - Altitude: %f", altitude);
391 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_ALT);
394 if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
396 mean = (horizontal + vertical) / 2.0;
397 tp_asv_set_double (self->priv->location, EMPATHY_LOCATION_ACCURACY, mean);
399 DEBUG ("\t - Accuracy: %f", mean);
403 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_ACCURACY);
406 update_timestamp (self);
407 if (self->priv->timeout_id == 0)
408 self->priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle,
413 initial_position_cb (GeocluePosition *position,
414 GeocluePositionFields fields,
419 GeoclueAccuracy *accuracy,
425 DEBUG ("Error: %s", error->message);
426 g_error_free (error);
430 position_changed_cb (position, fields, timestamp, latitude, longitude,
431 altitude, accuracy, self);
436 set_requirements (EmpathyLocationManager *self,
437 GeoclueSetRequirementsCallback callback)
439 geoclue_master_client_set_requirements_async (self->priv->gc_client,
440 GEOCLUE_ACCURACY_LEVEL_COUNTRY, 0, FALSE, self->priv->resources,
445 update_resources_set_requirements_cb (GeoclueMasterClient *client,
449 EmpathyLocationManager *self = userdata;
453 DEBUG ("set_requirements failed: %s", error->message);
454 g_error_free (error);
458 geoclue_address_get_address_async (self->priv->gc_address,
459 initial_address_cb, self);
460 geoclue_position_get_position_async (self->priv->gc_position,
461 initial_position_cb, self);
465 update_resources (EmpathyLocationManager *self)
467 DEBUG ("Updating resources %d", self->priv->resources);
469 if (!self->priv->geoclue_is_setup)
472 /* As per Geoclue bug #15126, using NONE results in no address
473 * being found as geoclue-manual report an empty address with
475 set_requirements (self, update_resources_set_requirements_cb);
479 create_address_cb (GeoclueMasterClient *client,
480 GeoclueAddress *address,
484 EmpathyLocationManager *self = userdata;
488 DEBUG ("Failed to create GeoclueAddress: %s", error->message);
489 g_error_free (error);
493 self->priv->gc_address = address;
495 g_signal_connect (G_OBJECT (self->priv->gc_address), "address-changed",
496 G_CALLBACK (address_changed_cb), self);
498 self->priv->geoclue_is_setup = TRUE;
502 create_position_cb (GeoclueMasterClient *client,
503 GeocluePosition *position,
507 EmpathyLocationManager *self = userdata;
511 DEBUG ("Failed to create GeocluePosition: %s", error->message);
512 g_error_free (error);
516 self->priv->gc_position = position;
518 g_signal_connect (G_OBJECT (self->priv->gc_position), "position-changed",
519 G_CALLBACK (position_changed_cb), self);
521 /* Get updated when the address changes */
522 geoclue_master_client_create_address_async (self->priv->gc_client,
523 create_address_cb, self);
527 create_client_set_requirements_cb (GeoclueMasterClient *client,
531 EmpathyLocationManager *self = userdata;
535 DEBUG ("set_requirements failed: %s", error->message);
536 g_error_free (error);
540 /* Get updated when the position is changes */
541 geoclue_master_client_create_position_async (self->priv->gc_client,
542 create_position_cb, self);
546 create_client_cb (GeoclueMaster *master,
547 GeoclueMasterClient *client,
552 EmpathyLocationManager *self = userdata;
556 DEBUG ("Failed to create GeoclueMasterClient: %s", error->message);
557 g_error_free (error);
561 /* @client seems be (transfer full) looking at the geoclue code; yeah for
562 * undocumented API... */
563 self->priv->gc_client = client;
565 set_requirements (self, create_client_set_requirements_cb);
569 setup_geoclue (EmpathyLocationManager *self)
571 GeoclueMaster *master;
573 DEBUG ("Setting up Geoclue");
574 master = geoclue_master_get_default ();
576 geoclue_master_create_client_async (master, create_client_cb, self);
578 g_object_unref (master);
582 publish_cb (GSettings *gsettings_loc,
586 EmpathyLocationManager *self = EMPATHY_LOCATION_MANAGER (user_data);
588 DEBUG ("Publish Conf changed");
590 if (g_settings_get_boolean (gsettings_loc, key))
592 if (!self->priv->geoclue_is_setup)
593 setup_geoclue (self);
594 /* if still not setup than the init failed */
595 if (!self->priv->geoclue_is_setup)
598 geoclue_address_get_address_async (self->priv->gc_address,
599 initial_address_cb, self);
600 geoclue_position_get_position_async (self->priv->gc_position,
601 initial_position_cb, self);
605 /* As per XEP-0080: send an empty location to have remove current
606 * location from the servers
608 g_hash_table_remove_all (self->priv->location);
609 publish_to_all_connections (self, TRUE);
615 resource_cb (GSettings *gsettings_loc,
619 EmpathyLocationManager *self = EMPATHY_LOCATION_MANAGER (user_data);
620 GeoclueResourceFlags resource = 0;
622 DEBUG ("%s changed", key);
624 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK))
625 resource = GEOCLUE_RESOURCE_NETWORK;
626 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL))
627 resource = GEOCLUE_RESOURCE_CELL;
628 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS))
629 resource = GEOCLUE_RESOURCE_GPS;
631 if (g_settings_get_boolean (gsettings_loc, key))
632 self->priv->resources |= resource;
634 self->priv->resources &= ~resource;
636 if (self->priv->geoclue_is_setup)
637 update_resources (self);
641 accuracy_cb (GSettings *gsettings_loc,
645 EmpathyLocationManager *self = EMPATHY_LOCATION_MANAGER (user_data);
647 DEBUG ("%s changed", key);
649 self->priv->reduce_accuracy = g_settings_get_boolean (gsettings_loc, key);
651 if (!self->priv->geoclue_is_setup)
654 geoclue_address_get_address_async (self->priv->gc_address,
655 initial_address_cb, self);
656 geoclue_position_get_position_async (self->priv->gc_position,
657 initial_position_cb, self);
661 account_manager_prepared_cb (GObject *source_object,
662 GAsyncResult *result,
666 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
667 EmpathyLocationManager *self = user_data;
668 GError *error = NULL;
670 if (!tp_proxy_prepare_finish (account_manager, result, &error))
672 DEBUG ("Failed to prepare account manager: %s", error->message);
673 g_error_free (error);
677 accounts = tp_account_manager_dup_valid_accounts (account_manager);
678 for (l = accounts; l != NULL; l = l->next)
680 TpAccount *account = TP_ACCOUNT (l->data);
682 tp_g_signal_connect_object (account, "status-changed",
683 G_CALLBACK (new_connection_cb), self, 0);
685 g_list_free_full (accounts, g_object_unref);
689 empathy_location_manager_init (EmpathyLocationManager *self)
691 EmpathyLocationManagerPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
692 EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPrivate);
695 priv->geoclue_is_setup = FALSE;
696 priv->location = tp_asv_new (NULL, NULL);
697 priv->gsettings_loc = g_settings_new (EMPATHY_PREFS_LOCATION_SCHEMA);
699 /* Setup account status callbacks */
700 priv->account_manager = tp_account_manager_dup ();
702 tp_proxy_prepare_async (priv->account_manager, NULL,
703 account_manager_prepared_cb, self);
705 /* Setup settings status callbacks */
706 g_signal_connect (priv->gsettings_loc,
707 "changed::" EMPATHY_PREFS_LOCATION_PUBLISH,
708 G_CALLBACK (publish_cb), self);
709 g_signal_connect (priv->gsettings_loc,
710 "changed::" EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
711 G_CALLBACK (resource_cb), self);
712 g_signal_connect (priv->gsettings_loc,
713 "changed::" EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
714 G_CALLBACK (resource_cb), self);
715 g_signal_connect (priv->gsettings_loc,
716 "changed::" EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
717 G_CALLBACK (resource_cb), self);
718 g_signal_connect (priv->gsettings_loc,
719 "changed::" EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
720 G_CALLBACK (accuracy_cb), self);
722 resource_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
724 resource_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_RESOURCE_CELL, self);
725 resource_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_RESOURCE_GPS, self);
726 accuracy_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
728 publish_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_PUBLISH, self);
731 EmpathyLocationManager *
732 empathy_location_manager_dup_singleton (void)
734 return EMPATHY_LOCATION_MANAGER (g_object_new (EMPATHY_TYPE_LOCATION_MANAGER,