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>
23 #include "empathy-location-manager.h"
25 #include <geoclue/geoclue-master.h>
27 #include "empathy-gsettings.h"
28 #include "empathy-location.h"
29 #include "empathy-time.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
32 #include "empathy-debug.h"
34 /* Seconds before updating the location */
36 static EmpathyLocationManager *location_manager = NULL;
38 struct _EmpathyLocationManagerPrivate {
39 gboolean geoclue_is_setup;
40 /* Contains the location to be sent to accounts. Geoclue is used
41 * to populate it. This HashTable uses Telepathy's style (string,
42 * GValue). Keys are defined in empathy-location.h
46 GSettings *gsettings_loc;
48 GeoclueResourceFlags resources;
49 GeoclueMasterClient *gc_client;
50 GeocluePosition *gc_position;
51 GeoclueAddress *gc_address;
53 gboolean reduce_accuracy;
54 TpAccountManager *account_manager;
56 /* The idle id for publish_on_idle func */
60 G_DEFINE_TYPE (EmpathyLocationManager, empathy_location_manager, G_TYPE_OBJECT);
63 location_manager_constructor (GType type,
64 guint n_construct_params,
65 GObjectConstructParam *construct_params)
69 if (location_manager == NULL)
71 retval = G_OBJECT_CLASS (empathy_location_manager_parent_class)->constructor
72 (type, n_construct_params, construct_params);
74 location_manager = EMPATHY_LOCATION_MANAGER (retval);
75 g_object_add_weak_pointer (retval, (gpointer) &location_manager);
79 retval = g_object_ref (location_manager);
86 location_manager_dispose (GObject *object)
88 EmpathyLocationManager *self = (EmpathyLocationManager *) object;
89 void (*dispose) (GObject *) =
90 G_OBJECT_CLASS (empathy_location_manager_parent_class)->dispose;
92 tp_clear_object (&self->priv->account_manager);
93 tp_clear_object (&self->priv->gsettings_loc);
94 tp_clear_object (&self->priv->gc_client);
95 tp_clear_object (&self->priv->gc_position);
96 tp_clear_object (&self->priv->gc_address);
97 tp_clear_pointer (&self->priv->location, g_hash_table_unref);
104 empathy_location_manager_class_init (EmpathyLocationManagerClass *class)
106 GObjectClass *object_class;
108 object_class = G_OBJECT_CLASS (class);
110 object_class->constructor = location_manager_constructor;
111 object_class->dispose = location_manager_dispose;
113 g_type_class_add_private (object_class, sizeof (EmpathyLocationManagerPrivate));
117 publish_location_cb (TpConnection *connection,
120 GObject *weak_object)
123 DEBUG ("Error setting location: %s", error->message);
127 publish_location (EmpathyLocationManager *self,
129 gboolean force_publication)
131 guint connection_status = -1;
136 if (!force_publication)
138 if (!g_settings_get_boolean (self->priv->gsettings_loc,
139 EMPATHY_PREFS_LOCATION_PUBLISH))
143 connection_status = tp_connection_get_status (conn, NULL);
145 if (connection_status != TP_CONNECTION_STATUS_CONNECTED)
148 DEBUG ("Publishing %s location to connection %p",
149 (g_hash_table_size (self->priv->location) == 0 ? "empty" : ""),
152 tp_cli_connection_interface_location_call_set_location (conn, -1,
153 self->priv->location, publish_location_cb, NULL, NULL, G_OBJECT (self));
158 EmpathyLocationManager *self;
159 gboolean force_publication;
163 publish_to_all_am_prepared_cb (GObject *source_object,
164 GAsyncResult *result,
167 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
168 PublishToAllData *data = user_data;
170 GError *error = NULL;
172 if (!tp_proxy_prepare_finish (manager, result, &error))
174 DEBUG ("Failed to prepare account manager: %s", error->message);
175 g_error_free (error);
179 accounts = tp_account_manager_dup_valid_accounts (manager);
180 for (l = accounts; l; l = l->next)
182 TpConnection *conn = tp_account_get_connection (TP_ACCOUNT (l->data));
185 publish_location (data->self, conn, data->force_publication);
187 g_list_free_full (accounts, g_object_unref);
190 g_object_unref (data->self);
191 g_slice_free (PublishToAllData, data);
195 publish_to_all_connections (EmpathyLocationManager *self,
196 gboolean force_publication)
198 PublishToAllData *data;
200 data = g_slice_new0 (PublishToAllData);
201 data->self = g_object_ref (self);
202 data->force_publication = force_publication;
204 tp_proxy_prepare_async (self->priv->account_manager, NULL,
205 publish_to_all_am_prepared_cb, data);
209 publish_on_idle (gpointer user_data)
211 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
213 manager->priv->timeout_id = 0;
214 publish_to_all_connections (manager, TRUE);
219 new_connection_cb (TpAccount *account,
223 gchar *dbus_error_name,
227 EmpathyLocationManager *self = user_data;
230 conn = tp_account_get_connection (account);
232 DEBUG ("New connection %p", conn);
234 /* Don't publish if it is already planned (ie startup) */
235 if (self->priv->timeout_id == 0)
237 publish_location (EMPATHY_LOCATION_MANAGER (self), conn,
243 update_timestamp (EmpathyLocationManager *self)
247 timestamp = empathy_time_get_current ();
248 tp_asv_set_int64 (self->priv->location, EMPATHY_LOCATION_TIMESTAMP,
251 DEBUG ("\t - Timestamp: %" G_GINT64_FORMAT, timestamp);
255 address_changed_cb (GeoclueAddress *address,
258 GeoclueAccuracy *accuracy,
261 EmpathyLocationManager *self = user_data;
262 GeoclueAccuracyLevel level;
266 geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
267 DEBUG ("New address (accuracy level %d):", level);
268 /* FIXME: Publish accuracy level also considering the position's */
270 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_STREET);
271 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_AREA);
272 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_REGION);
273 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_COUNTRY);
274 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_COUNTRY_CODE);
275 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_POSTAL_CODE);
277 if (g_hash_table_size (details) == 0)
279 DEBUG ("\t - (Empty)");
283 g_hash_table_iter_init (&iter, details);
284 while (g_hash_table_iter_next (&iter, &key, &value))
286 /* Discard street information if reduced accuracy is on */
287 if (self->priv->reduce_accuracy &&
288 !tp_strdiff (key, EMPATHY_LOCATION_STREET))
291 tp_asv_set_string (self->priv->location, key, value);
293 DEBUG ("\t - %s: %s", (gchar *) key, (gchar *) value);
296 update_timestamp (self);
297 if (self->priv->timeout_id == 0)
298 self->priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle,
303 initial_address_cb (GeoclueAddress *address,
306 GeoclueAccuracy *accuracy,
312 DEBUG ("Error: %s", error->message);
313 g_error_free (error);
317 address_changed_cb (address, timestamp, details, accuracy, self);
322 position_changed_cb (GeocluePosition *position,
323 GeocluePositionFields fields,
328 GeoclueAccuracy *accuracy,
331 EmpathyLocationManager *self = user_data;
332 GeoclueAccuracyLevel level;
333 gdouble mean, horizontal, vertical;
335 geoclue_accuracy_get_details (accuracy, &level, &horizontal, &vertical);
336 DEBUG ("New position (accuracy level %d)", level);
337 if (level == GEOCLUE_ACCURACY_LEVEL_NONE)
340 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
343 if (self->priv->reduce_accuracy)
344 /* Truncate at 1 decimal place */
345 longitude = ((int) (longitude * 10)) / 10.0;
347 tp_asv_set_double (self->priv->location, EMPATHY_LOCATION_LON, longitude);
349 DEBUG ("\t - Longitude: %f", longitude);
353 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_LON);
356 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
358 if (self->priv->reduce_accuracy)
359 /* Truncate at 1 decimal place */
360 latitude = ((int) (latitude * 10)) / 10.0;
362 tp_asv_set_double (self->priv->location, EMPATHY_LOCATION_LAT, latitude);
364 DEBUG ("\t - Latitude: %f", latitude);
368 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_LAT);
371 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
373 tp_asv_set_double (self->priv->location, EMPATHY_LOCATION_ALT, altitude);
375 DEBUG ("\t - Altitude: %f", altitude);
379 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_ALT);
382 if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
384 mean = (horizontal + vertical) / 2.0;
385 tp_asv_set_double (self->priv->location, EMPATHY_LOCATION_ACCURACY, mean);
387 DEBUG ("\t - Accuracy: %f", mean);
391 g_hash_table_remove (self->priv->location, EMPATHY_LOCATION_ACCURACY);
394 update_timestamp (self);
395 if (self->priv->timeout_id == 0)
396 self->priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle,
401 initial_position_cb (GeocluePosition *position,
402 GeocluePositionFields fields,
407 GeoclueAccuracy *accuracy,
413 DEBUG ("Error: %s", error->message);
414 g_error_free (error);
418 position_changed_cb (position, fields, timestamp, latitude, longitude,
419 altitude, accuracy, self);
424 set_requirements (EmpathyLocationManager *self,
425 GeoclueSetRequirementsCallback callback)
427 geoclue_master_client_set_requirements_async (self->priv->gc_client,
428 GEOCLUE_ACCURACY_LEVEL_COUNTRY, 0, FALSE, self->priv->resources,
433 update_resources_set_requirements_cb (GeoclueMasterClient *client,
437 EmpathyLocationManager *self = userdata;
441 DEBUG ("set_requirements failed: %s", error->message);
442 g_error_free (error);
446 geoclue_address_get_address_async (self->priv->gc_address,
447 initial_address_cb, self);
448 geoclue_position_get_position_async (self->priv->gc_position,
449 initial_position_cb, self);
453 update_resources (EmpathyLocationManager *self)
455 DEBUG ("Updating resources %d", self->priv->resources);
457 if (!self->priv->geoclue_is_setup)
460 /* As per Geoclue bug #15126, using NONE results in no address
461 * being found as geoclue-manual report an empty address with
463 set_requirements (self, update_resources_set_requirements_cb);
467 create_address_cb (GeoclueMasterClient *client,
468 GeoclueAddress *address,
472 EmpathyLocationManager *self = userdata;
476 DEBUG ("Failed to create GeoclueAddress: %s", error->message);
477 g_error_free (error);
481 self->priv->gc_address = address;
483 g_signal_connect (G_OBJECT (self->priv->gc_address), "address-changed",
484 G_CALLBACK (address_changed_cb), self);
486 self->priv->geoclue_is_setup = TRUE;
490 create_position_cb (GeoclueMasterClient *client,
491 GeocluePosition *position,
495 EmpathyLocationManager *self = userdata;
499 DEBUG ("Failed to create GeocluePosition: %s", error->message);
500 g_error_free (error);
504 self->priv->gc_position = position;
506 g_signal_connect (G_OBJECT (self->priv->gc_position), "position-changed",
507 G_CALLBACK (position_changed_cb), self);
509 /* Get updated when the address changes */
510 geoclue_master_client_create_address_async (self->priv->gc_client,
511 create_address_cb, self);
515 create_client_set_requirements_cb (GeoclueMasterClient *client,
519 EmpathyLocationManager *self = userdata;
523 DEBUG ("set_requirements failed: %s", error->message);
524 g_error_free (error);
528 /* Get updated when the position is changes */
529 geoclue_master_client_create_position_async (self->priv->gc_client,
530 create_position_cb, self);
534 create_client_cb (GeoclueMaster *master,
535 GeoclueMasterClient *client,
540 EmpathyLocationManager *self = userdata;
544 DEBUG ("Failed to create GeoclueMasterClient: %s", error->message);
545 g_error_free (error);
549 /* @client seems be (transfer full) looking at the geoclue code; yeah for
550 * undocumented API... */
551 self->priv->gc_client = client;
553 set_requirements (self, create_client_set_requirements_cb);
557 setup_geoclue (EmpathyLocationManager *self)
559 GeoclueMaster *master;
561 DEBUG ("Setting up Geoclue");
562 master = geoclue_master_get_default ();
564 geoclue_master_create_client_async (master, create_client_cb, self);
566 g_object_unref (master);
570 publish_cb (GSettings *gsettings_loc,
574 EmpathyLocationManager *self = EMPATHY_LOCATION_MANAGER (user_data);
576 DEBUG ("Publish Conf changed");
578 if (g_settings_get_boolean (gsettings_loc, key))
580 if (!self->priv->geoclue_is_setup)
581 setup_geoclue (self);
582 /* if still not setup than the init failed */
583 if (!self->priv->geoclue_is_setup)
586 geoclue_address_get_address_async (self->priv->gc_address,
587 initial_address_cb, self);
588 geoclue_position_get_position_async (self->priv->gc_position,
589 initial_position_cb, self);
593 /* As per XEP-0080: send an empty location to have remove current
594 * location from the servers
596 g_hash_table_remove_all (self->priv->location);
597 publish_to_all_connections (self, TRUE);
603 resource_cb (GSettings *gsettings_loc,
607 EmpathyLocationManager *self = EMPATHY_LOCATION_MANAGER (user_data);
608 GeoclueResourceFlags resource = 0;
610 DEBUG ("%s changed", key);
612 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK))
613 resource = GEOCLUE_RESOURCE_NETWORK;
614 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL))
615 resource = GEOCLUE_RESOURCE_CELL;
616 if (!tp_strdiff (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS))
617 resource = GEOCLUE_RESOURCE_GPS;
619 if (g_settings_get_boolean (gsettings_loc, key))
620 self->priv->resources |= resource;
622 self->priv->resources &= ~resource;
624 if (self->priv->geoclue_is_setup)
625 update_resources (self);
629 accuracy_cb (GSettings *gsettings_loc,
633 EmpathyLocationManager *self = EMPATHY_LOCATION_MANAGER (user_data);
635 DEBUG ("%s changed", key);
637 self->priv->reduce_accuracy = g_settings_get_boolean (gsettings_loc, key);
639 if (!self->priv->geoclue_is_setup)
642 geoclue_address_get_address_async (self->priv->gc_address,
643 initial_address_cb, self);
644 geoclue_position_get_position_async (self->priv->gc_position,
645 initial_position_cb, self);
649 account_manager_prepared_cb (GObject *source_object,
650 GAsyncResult *result,
654 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
655 EmpathyLocationManager *self = user_data;
656 GError *error = NULL;
658 if (!tp_proxy_prepare_finish (account_manager, result, &error))
660 DEBUG ("Failed to prepare account manager: %s", error->message);
661 g_error_free (error);
665 accounts = tp_account_manager_dup_valid_accounts (account_manager);
666 for (l = accounts; l != NULL; l = l->next)
668 TpAccount *account = TP_ACCOUNT (l->data);
670 tp_g_signal_connect_object (account, "status-changed",
671 G_CALLBACK (new_connection_cb), self, 0);
673 g_list_free_full (accounts, g_object_unref);
677 empathy_location_manager_init (EmpathyLocationManager *self)
679 EmpathyLocationManagerPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
680 EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPrivate);
683 priv->geoclue_is_setup = FALSE;
684 priv->location = tp_asv_new (NULL, NULL);
685 priv->gsettings_loc = g_settings_new (EMPATHY_PREFS_LOCATION_SCHEMA);
687 /* Setup account status callbacks */
688 priv->account_manager = tp_account_manager_dup ();
690 tp_proxy_prepare_async (priv->account_manager, NULL,
691 account_manager_prepared_cb, self);
693 /* Setup settings status callbacks */
694 g_signal_connect (priv->gsettings_loc,
695 "changed::" EMPATHY_PREFS_LOCATION_PUBLISH,
696 G_CALLBACK (publish_cb), self);
697 g_signal_connect (priv->gsettings_loc,
698 "changed::" EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
699 G_CALLBACK (resource_cb), self);
700 g_signal_connect (priv->gsettings_loc,
701 "changed::" EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
702 G_CALLBACK (resource_cb), self);
703 g_signal_connect (priv->gsettings_loc,
704 "changed::" EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
705 G_CALLBACK (resource_cb), self);
706 g_signal_connect (priv->gsettings_loc,
707 "changed::" EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
708 G_CALLBACK (accuracy_cb), self);
710 resource_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
712 resource_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_RESOURCE_CELL, self);
713 resource_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_RESOURCE_GPS, self);
714 accuracy_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
716 publish_cb (priv->gsettings_loc, EMPATHY_PREFS_LOCATION_PUBLISH, self);
719 EmpathyLocationManager *
720 empathy_location_manager_dup_singleton (void)
722 return EMPATHY_LOCATION_MANAGER (g_object_new (EMPATHY_TYPE_LOCATION_MANAGER,