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/util.h>
31 #include <geoclue/geoclue-master.h>
33 #include <extensions/extensions.h>
35 #include "empathy-location-manager.h"
36 #include "empathy-conf.h"
38 #include "libempathy/empathy-account-manager.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 EmpathyAccountManager *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 *location_manager,
185 gboolean force_publication)
187 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
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);
221 publish_to_all_connections (EmpathyLocationManager *location_manager,
222 gboolean force_publication)
224 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
225 GList *connections = NULL, *l;
227 connections = empathy_account_manager_dup_connections (priv->account_manager);
228 for (l = connections; l; l = l->next)
230 publish_location (location_manager, l->data, force_publication);
231 g_object_unref (l->data);
233 g_list_free (connections);
238 publish_on_idle (gpointer user_data)
240 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
241 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
243 priv->timeout_id = 0;
244 publish_to_all_connections (manager, TRUE);
249 new_connection_cb (EmpathyAccountManager *manager,
251 gpointer *location_manager)
253 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
254 DEBUG ("New connection %p", conn);
256 /* Don't publish if it is already planned (ie startup) */
257 if (priv->timeout_id == 0)
259 publish_location (EMPATHY_LOCATION_MANAGER (location_manager), conn,
265 update_timestamp (EmpathyLocationManager *location_manager)
267 EmpathyLocationManagerPriv *priv= GET_PRIV (location_manager);
272 timestamp = time (NULL);
273 stamp64 = (gint64) timestamp;
274 new_value = tp_g_value_slice_new_int64 (stamp64);
275 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_TIMESTAMP),
277 DEBUG ("\t - Timestamp: %" G_GINT64_FORMAT, stamp64);
281 address_changed_cb (GeoclueAddress *address,
284 GeoclueAccuracy *accuracy,
285 gpointer location_manager)
287 GeoclueAccuracyLevel level;
288 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
292 geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
293 DEBUG ("New address (accuracy level %d):", level);
294 /* FIXME: Publish accuracy level also considering the position's */
296 g_hash_table_remove (priv->location, EMPATHY_LOCATION_STREET);
297 g_hash_table_remove (priv->location, EMPATHY_LOCATION_AREA);
298 g_hash_table_remove (priv->location, EMPATHY_LOCATION_REGION);
299 g_hash_table_remove (priv->location, EMPATHY_LOCATION_COUNTRY);
300 g_hash_table_remove (priv->location, EMPATHY_LOCATION_COUNTRY_CODE);
301 g_hash_table_remove (priv->location, EMPATHY_LOCATION_POSTAL_CODE);
303 if (g_hash_table_size (details) == 0)
305 DEBUG ("\t - (Empty)");
309 g_hash_table_iter_init (&iter, details);
310 while (g_hash_table_iter_next (&iter, &key, &value))
313 /* Discard street information if reduced accuracy is on */
314 if (priv->reduce_accuracy &&
315 !tp_strdiff (key, EMPATHY_LOCATION_STREET))
318 new_value = tp_g_value_slice_new_string (value);
319 g_hash_table_insert (priv->location, g_strdup (key), new_value);
321 DEBUG ("\t - %s: %s", (gchar *) key, (gchar *) value);
324 update_timestamp (location_manager);
325 if (priv->timeout_id == 0)
326 priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, location_manager);
330 initial_address_cb (GeoclueAddress *address,
333 GeoclueAccuracy *accuracy,
335 gpointer location_manager)
339 DEBUG ("Error: %s", error->message);
340 g_error_free (error);
344 address_changed_cb (address, timestamp, details, accuracy, location_manager);
349 position_changed_cb (GeocluePosition *position,
350 GeocluePositionFields fields,
355 GeoclueAccuracy *accuracy,
356 gpointer location_manager)
358 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
359 GeoclueAccuracyLevel level;
360 gdouble mean, horizontal, vertical;
364 geoclue_accuracy_get_details (accuracy, &level, &horizontal, &vertical);
365 DEBUG ("New position (accuracy level %d)", level);
366 if (level == GEOCLUE_ACCURACY_LEVEL_NONE)
369 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
372 if (priv->reduce_accuracy)
373 /* Truncate at 1 decimal place */
374 longitude = ((int) (longitude * 10)) / 10.0;
376 new_value = tp_g_value_slice_new_double (longitude);
377 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LON),
379 DEBUG ("\t - Longitude: %f", longitude);
383 g_hash_table_remove (priv->location, EMPATHY_LOCATION_LON);
386 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
388 if (priv->reduce_accuracy)
389 /* Truncate at 1 decimal place */
390 latitude = ((int) (latitude * 10)) / 10.0;
392 new_value = tp_g_value_slice_new_double (latitude);
393 g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_LAT),
395 DEBUG ("\t - Latitude: %f", latitude);
399 g_hash_table_remove (priv->location, EMPATHY_LOCATION_LAT);
402 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
404 new_value = tp_g_value_slice_new_double (altitude);
405 g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_ALT),
407 DEBUG ("\t - Altitude: %f", altitude);
411 g_hash_table_remove (priv->location, EMPATHY_LOCATION_ALT);
414 if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
416 mean = (horizontal + vertical) / 2.0;
417 new_value = tp_g_value_slice_new_double (mean);
418 g_hash_table_replace (priv->location,
419 g_strdup (EMPATHY_LOCATION_ACCURACY), new_value);
420 DEBUG ("\t - Accuracy: %f", mean);
424 g_hash_table_remove (priv->location, EMPATHY_LOCATION_ACCURACY);
427 update_timestamp (location_manager);
428 if (priv->timeout_id == 0)
429 priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, location_manager);
433 initial_position_cb (GeocluePosition *position,
434 GeocluePositionFields fields,
439 GeoclueAccuracy *accuracy,
441 gpointer location_manager)
445 DEBUG ("Error: %s", error->message);
446 g_error_free (error);
450 position_changed_cb (position, fields, timestamp, latitude, longitude,
451 altitude, accuracy, location_manager);
456 update_resources (EmpathyLocationManager *location_manager)
458 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
460 DEBUG ("Updating resources %d", priv->resources);
462 if (!priv->geoclue_is_setup)
465 /* As per Geoclue bug #15126, using NONE results in no address
466 * being found as geoclue-manual report an empty address with
468 if (!geoclue_master_client_set_requirements (priv->gc_client,
469 GEOCLUE_ACCURACY_LEVEL_COUNTRY, 0, TRUE, priv->resources,
472 DEBUG ("set_requirements failed");
476 geoclue_address_get_address_async (priv->gc_address,
477 initial_address_cb, location_manager);
478 geoclue_position_get_position_async (priv->gc_position,
479 initial_position_cb, location_manager);
483 setup_geoclue (EmpathyLocationManager *location_manager)
485 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
487 GeoclueMaster *master;
488 GError *error = NULL;
490 DEBUG ("Setting up Geoclue");
491 master = geoclue_master_get_default ();
492 priv->gc_client = geoclue_master_create_client (master, NULL, &error);
493 g_object_unref (master);
495 if (priv->gc_client == NULL)
497 DEBUG ("Failed to GeoclueMasterClient: %s", error->message);
498 g_error_free (error);
502 update_resources (location_manager);
504 /* Get updated when the position is changes */
505 priv->gc_position = geoclue_master_client_create_position (
506 priv->gc_client, &error);
507 if (priv->gc_position == NULL)
509 DEBUG ("Failed to create GeocluePosition: %s", error->message);
510 g_error_free (error);
514 g_signal_connect (G_OBJECT (priv->gc_position), "position-changed",
515 G_CALLBACK (position_changed_cb), location_manager);
517 /* Get updated when the address changes */
518 priv->gc_address = geoclue_master_client_create_address (
519 priv->gc_client, &error);
520 if (priv->gc_address == NULL)
522 DEBUG ("Failed to create GeoclueAddress: %s", error->message);
523 g_error_free (error);
527 g_signal_connect (G_OBJECT (priv->gc_address), "address-changed",
528 G_CALLBACK (address_changed_cb), location_manager);
530 priv->geoclue_is_setup = TRUE;
534 publish_cb (EmpathyConf *conf,
538 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
539 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
540 gboolean can_publish;
542 DEBUG ("Publish Conf changed");
545 if (!empathy_conf_get_bool (conf, key, &can_publish))
550 if (!priv->geoclue_is_setup)
551 setup_geoclue (manager);
552 /* if still not setup than the init failed */
553 if (!priv->geoclue_is_setup)
556 geoclue_address_get_address_async (priv->gc_address,
557 initial_address_cb, manager);
558 geoclue_position_get_position_async (priv->gc_position,
559 initial_position_cb, manager);
563 /* As per XEP-0080: send an empty location to have remove current
564 * location from the servers
566 g_hash_table_remove_all (priv->location);
567 publish_to_all_connections (manager, TRUE);
573 resource_cb (EmpathyConf *conf,
577 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
578 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
579 GeoclueResourceFlags resource = 0;
580 gboolean resource_enabled;
582 DEBUG ("%s changed", key);
584 if (!empathy_conf_get_bool (conf, key, &resource_enabled))
587 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK))
588 resource = GEOCLUE_RESOURCE_NETWORK;
589 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL))
590 resource = GEOCLUE_RESOURCE_CELL;
591 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS))
592 resource = GEOCLUE_RESOURCE_GPS;
594 if (resource_enabled)
595 priv->resources |= resource;
597 priv->resources &= ~resource;
599 if (priv->geoclue_is_setup)
600 update_resources (manager);
604 accuracy_cb (EmpathyConf *conf,
608 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
609 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
613 DEBUG ("%s changed", key);
615 if (!empathy_conf_get_bool (conf, key, &enabled))
617 priv->reduce_accuracy = enabled;
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 empathy_location_manager_init (EmpathyLocationManager *location_manager)
632 EmpathyLocationManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (location_manager,
633 EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPriv);
635 location_manager->priv = priv;
636 priv->geoclue_is_setup = FALSE;
637 priv->location = g_hash_table_new_full (g_direct_hash, g_direct_equal,
638 g_free, (GDestroyNotify) tp_g_value_slice_free);
640 /* Setup account status callbacks */
641 priv->account_manager = empathy_account_manager_dup_singleton ();
642 g_signal_connect (priv->account_manager,
644 G_CALLBACK (new_connection_cb), location_manager);
646 /* Setup settings status callbacks */
647 conf = empathy_conf_get ();
648 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_PUBLISH, publish_cb,
650 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
651 resource_cb, location_manager);
652 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
653 resource_cb, location_manager);
654 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
655 resource_cb, location_manager);
656 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
657 accuracy_cb, location_manager);
659 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK, location_manager);
660 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL, location_manager);
661 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS, location_manager);
662 accuracy_cb (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY, location_manager);
663 publish_cb (conf, EMPATHY_PREFS_LOCATION_PUBLISH, location_manager);
666 EmpathyLocationManager *
667 empathy_location_manager_dup_singleton (void)
669 return EMPATHY_LOCATION_MANAGER (g_object_new (EMPATHY_TYPE_LOCATION_MANAGER,