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
61 GeoclueResourceFlags resources;
62 GeoclueMasterClient *gc_client;
63 GeocluePosition *gc_position;
64 GeoclueAddress *gc_address;
66 gboolean reduce_accuracy;
68 EmpathyAccountManager *account_manager;
70 /* The idle id for publish_on_idle func */
72 } EmpathyLocationManagerPriv;
74 G_DEFINE_TYPE (EmpathyLocationManager, empathy_location_manager, G_TYPE_OBJECT);
77 location_manager_constructor (GType type,
78 guint n_construct_params,
79 GObjectConstructParam *construct_params)
83 if (location_manager == NULL)
85 retval = G_OBJECT_CLASS (empathy_location_manager_parent_class)->constructor
86 (type, n_construct_params, construct_params);
88 location_manager = EMPATHY_LOCATION_MANAGER (retval);
89 g_object_add_weak_pointer (retval, (gpointer) &location_manager);
93 retval = g_object_ref (location_manager);
100 location_manager_dispose (GObject *object)
102 EmpathyLocationManagerPriv *priv = GET_PRIV (object);
104 if (priv->account_manager != NULL)
106 g_object_unref (priv->account_manager);
107 priv->account_manager = NULL;
110 if (priv->gc_client != NULL)
112 g_object_unref (priv->gc_client);
113 priv->gc_client = NULL;
116 if (priv->gc_position != NULL)
118 g_object_unref (priv->gc_position);
119 priv->gc_position = NULL;
122 if (priv->gc_address != NULL)
124 g_object_unref (priv->gc_address);
125 priv->gc_address = NULL;
128 if (priv->location != NULL)
130 g_hash_table_unref (priv->location);
131 priv->location = NULL;
134 G_OBJECT_CLASS (empathy_location_manager_parent_class)->finalize (object);
138 location_manager_get_property (GObject *object,
143 /*EmpathyLocationManagerPriv *priv = GET_PRIV (object); */
148 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
154 location_manager_set_property (GObject *object,
159 /* EmpathyLocationManagerPriv *priv = GET_PRIV (object); */
164 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
170 empathy_location_manager_class_init (EmpathyLocationManagerClass *class)
172 GObjectClass *object_class;
174 object_class = G_OBJECT_CLASS (class);
176 object_class->constructor = location_manager_constructor;
177 object_class->dispose = location_manager_dispose;
178 object_class->get_property = location_manager_get_property;
179 object_class->set_property = location_manager_set_property;
181 g_type_class_add_private (object_class, sizeof (EmpathyLocationManagerPriv));
185 publish_location (EmpathyLocationManager *location_manager,
187 gboolean force_publication)
189 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
190 guint connection_status = -1;
191 gboolean can_publish;
193 EmpathyConf *conf = empathy_conf_get ();
194 EmpathyTpContactFactory *factory;
196 conn = empathy_account_manager_get_connection (priv->account_manager,
201 if (force_publication == FALSE)
203 if (!empathy_conf_get_bool (conf, EMPATHY_PREFS_LOCATION_PUBLISH,
207 if (can_publish == FALSE)
211 connection_status = tp_connection_get_status (conn, NULL);
213 if (connection_status != TP_CONNECTION_STATUS_CONNECTED)
216 DEBUG ("Publishing %s location to connection %p",
217 (g_hash_table_size (priv->location) == 0 ? "empty" : ""),
220 factory = empathy_tp_contact_factory_dup_singleton (conn);
221 empathy_tp_contact_factory_set_location (factory, priv->location);
222 g_object_unref (factory);
226 publish_to_all_accounts (EmpathyLocationManager *location_manager,
227 gboolean force_publication)
229 GList *accounts = NULL, *l;
231 accounts = mc_accounts_list_by_enabled (TRUE);
232 for (l = accounts; l; l = l->next)
234 publish_location (location_manager, l->data, force_publication);
237 mc_accounts_list_free (accounts);
241 publish_on_idle (gpointer user_data)
243 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
244 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
246 priv->timeout_id = 0;
247 publish_to_all_accounts (manager, TRUE);
252 account_connection_changed_cb (EmpathyAccountManager *manager,
254 TpConnectionStatusReason reason,
255 TpConnectionStatus current,
256 TpConnectionStatus previous,
257 gpointer *location_manager)
259 DEBUG ("Account %s changed status from %d to %d", mc_account_get_display_name (account),
262 if (account && current == TP_CONNECTION_STATUS_CONNECTED)
263 publish_location (EMPATHY_LOCATION_MANAGER (location_manager), account,
268 update_timestamp (EmpathyLocationManager *location_manager)
270 EmpathyLocationManagerPriv *priv= GET_PRIV (location_manager);
275 timestamp = time (NULL);
276 stamp64 = (gint64) timestamp;
277 new_value = tp_g_value_slice_new_int64 (stamp64);
278 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_TIMESTAMP),
280 DEBUG ("\t - Timestamp: %" G_GINT64_FORMAT, stamp64);
284 address_changed_cb (GeoclueAddress *address,
287 GeoclueAccuracy *accuracy,
288 gpointer location_manager)
290 GeoclueAccuracyLevel level;
291 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
295 DEBUG ("New address (accuracy level %d):", level);
297 geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
298 g_hash_table_remove_all (priv->location);
300 if (g_hash_table_size (details) == 0)
303 g_hash_table_iter_init (&iter, details);
304 while (g_hash_table_iter_next (&iter, &key, &value))
307 /* Discard street information if reduced accuracy is on */
308 if (priv->reduce_accuracy && strcmp (key, EMPATHY_LOCATION_STREET) == 0)
311 new_value = tp_g_value_slice_new_string (value);
312 g_hash_table_insert (priv->location, g_strdup (key), new_value);
314 DEBUG ("\t - %s: %s", (gchar *) key, (gchar *) value);
317 update_timestamp (location_manager);
318 if (priv->timeout_id == 0)
319 priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, location_manager);
323 initial_address_cb (GeoclueAddress *address,
326 GeoclueAccuracy *accuracy,
328 gpointer location_manager)
332 DEBUG ("Error: %s", error->message);
333 g_error_free (error);
337 address_changed_cb (address, timestamp, details, accuracy, location_manager);
342 position_changed_cb (GeocluePosition *position,
343 GeocluePositionFields fields,
348 GeoclueAccuracy *accuracy,
349 gpointer location_manager)
351 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
352 GeoclueAccuracyLevel level;
353 gdouble mean, horizontal, vertical;
356 geoclue_accuracy_get_details (accuracy, &level, &horizontal, &vertical);
357 DEBUG ("New position (accuracy level %d)", level);
358 if (level == GEOCLUE_ACCURACY_LEVEL_NONE)
361 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
363 longitude += priv->reduce_value;
364 new_value = tp_g_value_slice_new_double (longitude);
365 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LON),
367 DEBUG ("\t - Longitude: %f", longitude);
369 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
371 latitude += priv->reduce_value;
372 new_value = tp_g_value_slice_new_double (latitude);
373 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LAT),
375 DEBUG ("\t - Latitude: %f", latitude);
377 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
379 new_value = tp_g_value_slice_new_double (altitude);
380 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_ALT),
382 DEBUG ("\t - Altitude: %f", altitude);
385 if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
387 mean = (horizontal + vertical) / 2.0;
388 new_value = tp_g_value_slice_new_double (mean);
389 g_hash_table_insert (priv->location,
390 g_strdup (EMPATHY_LOCATION_ACCURACY), new_value);
391 DEBUG ("\t - Accuracy: %f", mean);
394 update_timestamp (location_manager);
395 if (priv->timeout_id == 0)
396 priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, location_manager);
400 initial_position_cb (GeocluePosition *position,
401 GeocluePositionFields fields,
406 GeoclueAccuracy *accuracy,
408 gpointer location_manager)
412 DEBUG ("Error: %s", error->message);
413 g_error_free (error);
417 position_changed_cb (position, fields, timestamp, latitude, longitude,
418 altitude, accuracy, location_manager);
423 update_resources (EmpathyLocationManager *location_manager)
425 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
427 DEBUG ("Updating resources %d", priv->resources);
429 if (!geoclue_master_client_set_requirements (priv->gc_client,
430 GEOCLUE_ACCURACY_LEVEL_NONE, 0, TRUE, priv->resources,
433 DEBUG ("set_requirements failed");
437 if (!priv->geoclue_is_setup)
440 geoclue_address_get_address_async (priv->gc_address,
441 initial_address_cb, location_manager);
442 geoclue_position_get_position_async (priv->gc_position,
443 initial_position_cb, location_manager);
447 setup_geoclue (EmpathyLocationManager *location_manager)
449 EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
451 GeoclueMaster *master;
452 GError *error = NULL;
454 DEBUG ("Setting up Geoclue");
455 master = geoclue_master_get_default ();
456 priv->gc_client = geoclue_master_create_client (master, NULL, NULL);
457 g_object_unref (master);
459 update_resources (location_manager);
461 /* Get updated when the position is changes */
462 priv->gc_position = geoclue_master_client_create_position (
463 priv->gc_client, &error);
464 if (priv->gc_position == NULL)
466 DEBUG ("Failed to create GeocluePosition: %s", error->message);
467 g_error_free (error);
471 g_signal_connect (G_OBJECT (priv->gc_position), "position-changed",
472 G_CALLBACK (position_changed_cb), location_manager);
474 /* Get updated when the address changes */
475 priv->gc_address = geoclue_master_client_create_address (
476 priv->gc_client, &error);
477 if (priv->gc_address == NULL)
479 DEBUG ("Failed to create GeoclueAddress: %s", error->message);
480 g_error_free (error);
484 g_signal_connect (G_OBJECT (priv->gc_address), "address-changed",
485 G_CALLBACK (address_changed_cb), location_manager);
487 priv->geoclue_is_setup = TRUE;
491 publish_cb (EmpathyConf *conf,
495 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
496 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
497 gboolean can_publish;
499 DEBUG ("Publish Conf changed");
502 if (empathy_conf_get_bool (conf, key, &can_publish) == FALSE)
505 if (can_publish == TRUE)
507 if (priv->geoclue_is_setup == FALSE)
508 setup_geoclue (manager);
509 /* if still not setup than the init failed */
510 if (priv->geoclue_is_setup == FALSE)
513 geoclue_address_get_address_async (priv->gc_address,
514 initial_address_cb, manager);
515 geoclue_position_get_position_async (priv->gc_position,
516 initial_position_cb, manager);
520 /* As per XEP-0080: send an empty location to have remove current
521 * location from the servers
523 g_hash_table_remove_all (priv->location);
524 publish_to_all_accounts (manager, TRUE);
530 resource_cb (EmpathyConf *conf,
534 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
535 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
536 GeoclueResourceFlags resource = 0;
537 gboolean resource_enabled;
539 DEBUG ("%s changed", key);
541 if (!empathy_conf_get_bool (conf, key, &resource_enabled))
544 if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK) == 0)
545 resource = GEOCLUE_RESOURCE_NETWORK;
546 if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL) == 0)
547 resource = GEOCLUE_RESOURCE_CELL;
548 if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS) == 0)
549 resource = GEOCLUE_RESOURCE_GPS;
551 if (resource_enabled)
552 priv->resources |= resource;
554 priv->resources &= ~resource;
556 if (priv->geoclue_is_setup)
557 update_resources (manager);
561 accuracy_cb (EmpathyConf *conf,
565 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
566 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
570 DEBUG ("%s changed", key);
572 if (!empathy_conf_get_bool (conf, key, &enabled))
574 priv->reduce_accuracy = enabled;
578 GRand *rand = g_rand_new_with_seed (time (NULL));
579 priv->reduce_value = g_rand_double_range (rand, -0.25, 0.25);
584 priv->reduce_value = 0.0;
587 if (!priv->geoclue_is_setup)
590 geoclue_address_get_address_async (priv->gc_address,
591 initial_address_cb, manager);
592 geoclue_position_get_position_async (priv->gc_position,
593 initial_position_cb, manager);
597 empathy_location_manager_init (EmpathyLocationManager *location_manager)
600 EmpathyLocationManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (location_manager,
601 EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPriv);
603 location_manager->priv = priv;
604 priv->geoclue_is_setup = FALSE;
605 priv->location = g_hash_table_new_full (g_direct_hash, g_direct_equal,
606 g_free, (GDestroyNotify) tp_g_value_slice_free);
608 /* Setup settings status callbacks */
609 conf = empathy_conf_get ();
610 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_PUBLISH, publish_cb,
612 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
613 resource_cb, location_manager);
614 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
615 resource_cb, location_manager);
616 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
617 resource_cb, location_manager);
618 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
619 accuracy_cb, location_manager);
621 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK, location_manager);
622 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL, location_manager);
623 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS, location_manager);
624 accuracy_cb (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY, location_manager);
625 publish_cb (conf, EMPATHY_PREFS_LOCATION_PUBLISH, location_manager);
627 /* Setup account status callbacks */
628 priv->account_manager = empathy_account_manager_dup_singleton ();
629 g_signal_connect (priv->account_manager,
630 "account-connection-changed",
631 G_CALLBACK (account_connection_changed_cb), location_manager);
634 EmpathyLocationManager *
635 empathy_location_manager_dup_singleton (void)
637 return EMPATHY_LOCATION_MANAGER (g_object_new (EMPATHY_TYPE_LOCATION_MANAGER,