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/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"
42 #include "libempathy/empathy-time.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 struct _EmpathyLocationManagerPrivate {
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 */
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 EmpathyLocationManager *self = (EmpathyLocationManager *) object;
102 void (*dispose) (GObject *) =
103 G_OBJECT_CLASS (empathy_location_manager_parent_class)->dispose;
105 tp_clear_object (&self->priv->account_manager);
106 tp_clear_object (&self->priv->gsettings_loc);
107 tp_clear_object (&self->priv->gc_client);
108 tp_clear_object (&self->priv->gc_position);
109 tp_clear_object (&self->priv->gc_address);
110 tp_clear_pointer (&self->priv->location, g_hash_table_unref);
117 empathy_location_manager_class_init (EmpathyLocationManagerClass *class)
119 GObjectClass *object_class;
121 object_class = G_OBJECT_CLASS (class);
123 object_class->constructor = location_manager_constructor;
124 object_class->dispose = location_manager_dispose;
126 g_type_class_add_private (object_class, sizeof (EmpathyLocationManagerPrivate));
130 publish_location_cb (TpConnection *connection,
133 GObject *weak_object)
136 DEBUG ("Error setting location: %s", error->message);
140 publish_location (EmpathyLocationManager *self,
142 gboolean force_publication)
144 guint connection_status = -1;
149 if (!force_publication)
151 if (!g_settings_get_boolean (self->priv->gsettings_loc,
152 EMPATHY_PREFS_LOCATION_PUBLISH))
156 connection_status = tp_connection_get_status (conn, NULL);
158 if (connection_status != TP_CONNECTION_STATUS_CONNECTED)
161 DEBUG ("Publishing %s location to connection %p",
162 (g_hash_table_size (self->priv->location) == 0 ? "empty" : ""),
165 tp_cli_connection_interface_location_call_set_location (conn, -1,
166 self->priv->location, publish_location_cb, NULL, NULL, G_OBJECT (self));
171 EmpathyLocationManager *self;
172 gboolean force_publication;
176 publish_to_all_am_prepared_cb (GObject *source_object,
177 GAsyncResult *result,
180 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
181 PublishToAllData *data = user_data;
183 GError *error = NULL;
185 if (!tp_account_manager_prepare_finish (manager, result, &error))
187 DEBUG ("Failed to prepare account manager: %s", error->message);
188 g_error_free (error);
192 accounts = tp_account_manager_get_valid_accounts (manager);
193 for (l = accounts; l; l = l->next)
195 TpConnection *conn = tp_account_get_connection (TP_ACCOUNT (l->data));
198 publish_location (data->self, conn, data->force_publication);
200 g_list_free (accounts);
203 g_object_unref (data->self);
204 g_slice_free (PublishToAllData, data);
208 publish_to_all_connections (EmpathyLocationManager *self,
209 gboolean force_publication)
211 PublishToAllData *data;
213 data = g_slice_new0 (PublishToAllData);
214 data->self = g_object_ref (self);
215 data->force_publication = force_publication;
217 tp_account_manager_prepare_async (self->priv->account_manager, NULL,
218 publish_to_all_am_prepared_cb, data);
222 publish_on_idle (gpointer user_data)
224 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
226 manager->priv->timeout_id = 0;
227 publish_to_all_connections (manager, TRUE);
232 new_connection_cb (TpAccount *account,
236 gchar *dbus_error_name,
240 EmpathyLocationManager *self = user_data;
243 conn = tp_account_get_connection (account);
245 DEBUG ("New connection %p", conn);
247 /* Don't publish if it is already planned (ie startup) */
248 if (self->priv->timeout_id == 0)
250 publish_location (EMPATHY_LOCATION_MANAGER (self), conn,
256 update_timestamp (EmpathyLocationManager *self)
260 timestamp = empathy_time_get_current ();
261 tp_asv_set_int64 (self->priv->location, EMPATHY_LOCATION_TIMESTAMP,
264 DEBUG ("\t - Timestamp: %" G_GINT64_FORMAT, timestamp);
268 address_changed_cb (GeoclueAddress *address,
271 GeoclueAccuracy *accuracy,
274 EmpathyLocationManager *self = user_data;
275 GeoclueAccuracyLevel level;
279 geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
280 DEBUG ("New address (accuracy level %d):", level);
281 /* FIXME: Publish accuracy level also considering the position's */
283 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_STREET);
284 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_AREA);
285 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_REGION);
286 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_COUNTRY);
287 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_COUNTRY_CODE);
288 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_POSTAL_CODE);
290 if (g_hash_table_size (details) == 0)
292 DEBUG ("\t - (Empty)");
296 g_hash_table_iter_init (&iter, details);
297 while (g_hash_table_iter_next (&iter, &key, &value))
299 /* Discard street information if reduced accuracy is on */
300 if (self->priv->reduce_accuracy &&
301 !tp_strdiff (key, EMPATHY_LOCATION_STREET))
304 tp_asv_set_string (self->priv->location, key, value);
306 DEBUG ("\t - %s: %s", (gchar *) key, (gchar *) value);
309 update_timestamp (self);
310 if (self->priv->timeout_id == 0)
311 self->priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle,
316 initial_address_cb (GeoclueAddress *address,
319 GeoclueAccuracy *accuracy,
325 DEBUG ("Error: %s", error->message);
326 g_error_free (error);
330 address_changed_cb (address, timestamp, details, accuracy, self);
335 position_changed_cb (GeocluePosition *position,
336 GeocluePositionFields fields,
341 GeoclueAccuracy *accuracy,
344 EmpathyLocationManager *self = user_data;
345 GeoclueAccuracyLevel level;
346 gdouble mean, horizontal, vertical;
348 geoclue_accuracy_get_details (accuracy, &level, &horizontal, &vertical);
349 DEBUG ("New position (accuracy level %d)", level);
350 if (level == GEOCLUE_ACCURACY_LEVEL_NONE)
353 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
356 if (self->priv->reduce_accuracy)
357 /* Truncate at 1 decimal place */
358 longitude = ((int) (longitude * 10)) / 10.0;
360 tp_asv_set_double (self->priv->location, EMPATHY_LOCATION_LON, longitude);
362 DEBUG ("\t - Longitude: %f", longitude);
366 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_LON);
369 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
371 if (self->priv->reduce_accuracy)
372 /* Truncate at 1 decimal place */
373 latitude = ((int) (latitude * 10)) / 10.0;
375 tp_asv_set_double (self->priv->location, EMPATHY_LOCATION_LAT, latitude);
377 DEBUG ("\t - Latitude: %f", latitude);
381 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_LAT);
384 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
386 tp_asv_set_double (self->priv->location, EMPATHY_LOCATION_ALT, altitude);
388 DEBUG ("\t - Altitude: %f", altitude);
392 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_ALT);
395 if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
397 mean = (horizontal + vertical) / 2.0;
398 tp_asv_set_double (self->priv->location, EMPATHY_LOCATION_ACCURACY, mean);
400 DEBUG ("\t - Accuracy: %f", mean);
404 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_ACCURACY);
407 update_timestamp (self);
408 if (self->priv->timeout_id == 0)
409 self->priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle,
414 initial_position_cb (GeocluePosition *position,
415 GeocluePositionFields fields,
420 GeoclueAccuracy *accuracy,
426 DEBUG ("Error: %s", error->message);
427 g_error_free (error);
431 position_changed_cb (position, fields, timestamp, latitude, longitude,
432 altitude, accuracy, self);
437 set_requirements (EmpathyLocationManager *self)
439 GError *error = NULL;
441 if (!geoclue_master_client_set_requirements (self->priv->gc_client,
442 GEOCLUE_ACCURACY_LEVEL_COUNTRY, 0, FALSE, self->priv->resources,
445 DEBUG ("set_requirements failed: %s", error->message);
446 g_error_free (error);
454 update_resources (EmpathyLocationManager *self)
456 DEBUG ("Updating resources %d", self->priv->resources);
458 if (!self->priv->geoclue_is_setup)
461 /* As per Geoclue bug #15126, using NONE results in no address
462 * being found as geoclue-manual report an empty address with
464 if (!set_requirements (self))
467 geoclue_address_get_address_async (self->priv->gc_address,
468 initial_address_cb, self);
469 geoclue_position_get_position_async (self->priv->gc_position,
470 initial_position_cb, self);
474 setup_geoclue (EmpathyLocationManager *self)
476 GeoclueMaster *master;
477 GError *error = NULL;
479 DEBUG ("Setting up Geoclue");
480 master = geoclue_master_get_default ();
481 self->priv->gc_client = geoclue_master_create_client (master, NULL, &error);
482 g_object_unref (master);
484 if (self->priv->gc_client == NULL)
486 DEBUG ("Failed to GeoclueMasterClient: %s", error->message);
487 g_error_free (error);
491 if (!set_requirements (self))
494 /* Get updated when the position is changes */
495 self->priv->gc_position = geoclue_master_client_create_position (
496 self->priv->gc_client, &error);
497 if (self->priv->gc_position == NULL)
499 DEBUG ("Failed to create GeocluePosition: %s", error->message);
500 g_error_free (error);
504 g_signal_connect (G_OBJECT (self->priv->gc_position), "position-changed",
505 G_CALLBACK (position_changed_cb), self);
507 /* Get updated when the address changes */
508 self->priv->gc_address = geoclue_master_client_create_address (
509 self->priv->gc_client, &error);
510 if (self->priv->gc_address == NULL)
512 DEBUG ("Failed to create GeoclueAddress: %s", error->message);
513 g_error_free (error);
517 g_signal_connect (G_OBJECT (self->priv->gc_address), "address-changed",
518 G_CALLBACK (address_changed_cb), self);
520 self->priv->geoclue_is_setup = TRUE;
524 publish_cb (GSettings *gsettings_loc,
528 EmpathyLocationManager *self = EMPATHY_LOCATION_MANAGER (user_data);
530 DEBUG ("Publish Conf changed");
532 if (g_settings_get_boolean (gsettings_loc, key))
534 if (!self->priv->geoclue_is_setup)
535 setup_geoclue (self);
536 /* if still not setup than the init failed */
537 if (!self->priv->geoclue_is_setup)
540 geoclue_address_get_address_async (self->priv->gc_address,
541 initial_address_cb, self);
542 geoclue_position_get_position_async (self->priv->gc_position,
543 initial_position_cb, self);
547 /* As per XEP-0080: send an empty location to have remove current
548 * location from the servers
550 g_hash_table_remove_all (self->priv->location);
551 publish_to_all_connections (self, TRUE);
557 resource_cb (GSettings *gsettings_loc,
561 EmpathyLocationManager *self = EMPATHY_LOCATION_MANAGER (user_data);
562 GeoclueResourceFlags resource = 0;
564 DEBUG ("%s changed", key);
566 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK))
567 resource = GEOCLUE_RESOURCE_NETWORK;
568 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL))
569 resource = GEOCLUE_RESOURCE_CELL;
570 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS))
571 resource = GEOCLUE_RESOURCE_GPS;
573 if (g_settings_get_boolean (gsettings_loc, key))
574 self->priv->resources |= resource;
576 self->priv->resources &= ~resource;
578 if (self->priv->geoclue_is_setup)
579 update_resources (self);
583 accuracy_cb (GSettings *gsettings_loc,
587 EmpathyLocationManager *self = EMPATHY_LOCATION_MANAGER (user_data);
589 DEBUG ("%s changed", key);
591 self->priv->reduce_accuracy = g_settings_get_boolean (gsettings_loc, key);
593 if (!self->priv->geoclue_is_setup)
596 geoclue_address_get_address_async (self->priv->gc_address,
597 initial_address_cb, self);
598 geoclue_position_get_position_async (self->priv->gc_position,
599 initial_position_cb, self);
603 account_manager_prepared_cb (GObject *source_object,
604 GAsyncResult *result,
608 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
609 EmpathyLocationManager *self = user_data;
610 GError *error = NULL;
612 if (!tp_account_manager_prepare_finish (account_manager, result, &error))
614 DEBUG ("Failed to prepare account manager: %s", error->message);
615 g_error_free (error);
619 accounts = tp_account_manager_get_valid_accounts (account_manager);
620 for (l = accounts; l != NULL; l = l->next)
622 TpAccount *account = TP_ACCOUNT (l->data);
624 tp_g_signal_connect_object (account, "status-changed",
625 G_CALLBACK (new_connection_cb), self, 0);
627 g_list_free (accounts);
631 empathy_location_manager_init (EmpathyLocationManager *self)
633 EmpathyLocationManagerPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
634 EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPrivate);
637 priv->geoclue_is_setup = FALSE;
638 priv->location = tp_asv_new (NULL, NULL);
639 priv->gsettings_loc = g_settings_new (EMPATHY_PREFS_LOCATION_SCHEMA);
641 /* Setup account status callbacks */
642 priv->account_manager = tp_account_manager_dup ();
644 tp_account_manager_prepare_async (priv->account_manager, NULL,
645 account_manager_prepared_cb, self);
647 /* Setup settings status callbacks */
648 g_signal_connect (priv->gsettings_loc,
649 "changed::" EMPATHY_PREFS_LOCATION_PUBLISH,
650 G_CALLBACK (publish_cb), self);
651 g_signal_connect (priv->gsettings_loc,
652 "changed::" EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
653 G_CALLBACK (resource_cb), self);
654 g_signal_connect (priv->gsettings_loc,
655 "changed::" EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
656 G_CALLBACK (resource_cb), self);
657 g_signal_connect (priv->gsettings_loc,
658 "changed::" EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
659 G_CALLBACK (resource_cb), self);
660 g_signal_connect (priv->gsettings_loc,
661 "changed::" EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
662 G_CALLBACK (accuracy_cb), self);
664 resource_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
666 resource_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_RESOURCE_CELL, self);
667 resource_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_RESOURCE_GPS, self);
668 accuracy_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
670 publish_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_PUBLISH, self);
673 EmpathyLocationManager *
674 empathy_location_manager_dup_singleton (void)
676 return EMPATHY_LOCATION_MANAGER (g_object_new (EMPATHY_TYPE_LOCATION_MANAGER,