2 * Copyright (C) 2008 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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Authors: Pierre-Luc Beaudoin <pierre-luc@pierlux.com>
27 #include <glib/gi18n.h>
29 #include <telepathy-glib/util.h>
32 #include <geoclue/geoclue-master.h>
34 #include <extensions/extensions.h>
36 #include "empathy-location-manager.h"
37 #include "empathy-conf.h"
39 #include "libempathy/empathy-account-manager.h"
40 #include "libempathy/empathy-enum-types.h"
41 #include "libempathy/empathy-location.h"
42 #include "libempathy/empathy-tp-contact-factory.h"
43 #include "libempathy/empathy-utils.h"
45 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
46 #include "libempathy/empathy-debug.h"
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLocationManager)
55 GeoclueResourceFlags resources;
56 GeoclueMasterClient *gc_client;
57 GeocluePosition *gc_position;
58 GeoclueAddress *gc_address;
60 gboolean reduce_accuracy;
62 EmpathyAccountManager *account_manager;
64 /* The idle id for publish_on_idle func */
66 } EmpathyLocationManagerPriv;
68 static void location_manager_finalize (GObject *object);
69 static void location_manager_get_property (GObject *object, guint param_id,
70 GValue *value, GParamSpec *pspec);
71 static void location_manager_set_property (GObject *object, guint param_id,
72 const GValue *value, GParamSpec *pspec);
73 static void position_changed_cb (GeocluePosition *position,
74 GeocluePositionFields fields, int timestamp, double latitude,
75 double longitude, double altitude, GeoclueAccuracy *accuracy,
77 static void address_changed_cb (GeoclueAddress *address, int timestamp,
78 GHashTable *details, GeoclueAccuracy *accuracy, gpointer user_data);
79 static void setup_geoclue (EmpathyLocationManager *location_manager);
80 static void publish_cb (EmpathyConf *conf, const gchar *key,
82 static void update_resources (EmpathyLocationManager *location_manager);
83 static void resource_cb (EmpathyConf *conf, const gchar *key,
85 static void accuracy_cb (EmpathyConf *conf, const gchar *key,
87 static void account_connection_changed_cb (EmpathyAccountManager *manager,
88 McAccount *account, TpConnectionStatusReason reason,
89 TpConnectionStatus current, TpConnectionStatus previous,
90 gpointer *location_manager);
91 static void publish_to_all_accounts (EmpathyLocationManager *location_manager,
92 gboolean force_publication);
93 static gboolean publish_on_idle (gpointer user_data);
95 G_DEFINE_TYPE (EmpathyLocationManager, empathy_location_manager, G_TYPE_OBJECT);
103 empathy_location_manager_class_init (EmpathyLocationManagerClass *class)
105 GObjectClass *object_class;
107 object_class = G_OBJECT_CLASS (class);
109 object_class->finalize = location_manager_finalize;
110 object_class->get_property = location_manager_get_property;
111 object_class->set_property = location_manager_set_property;
113 g_type_class_add_private (object_class, sizeof (EmpathyLocationManagerPriv));
117 publish_on_idle (gpointer user_data)
119 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
120 EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
123 publish_to_all_accounts (manager, TRUE);
128 publish_location (EmpathyLocationManager *location_manager,
130 gboolean force_publication)
132 EmpathyLocationManagerPriv *priv;
133 guint connection_status = -1;
134 gboolean can_publish;
135 EmpathyConf *conf = empathy_conf_get ();
137 EmpathyTpContactFactory *factory;
139 priv = GET_PRIV (location_manager);
141 conn = mission_control_get_tpconnection (priv->mc, account, NULL);
145 factory = empathy_tp_contact_factory_dup_singleton (conn);
147 if (force_publication == FALSE)
149 if (!empathy_conf_get_bool (conf, EMPATHY_PREFS_LOCATION_PUBLISH,
153 if (can_publish == FALSE)
157 connection_status = mission_control_get_connection_status (priv->mc,
160 if (connection_status != TP_CONNECTION_STATUS_CONNECTED)
163 DEBUG ("Publishing %s location to account %s",
164 (g_hash_table_size (priv->location) == 0 ? "empty" : ""),
165 mc_account_get_display_name (account));
167 empathy_tp_contact_factory_set_location (factory, priv->location);
168 g_object_unref (factory);
172 publish_to_all_accounts (EmpathyLocationManager *location_manager,
173 gboolean force_publication)
175 GList *accounts = NULL, *l;
177 accounts = mc_accounts_list_by_enabled (TRUE);
178 for (l = accounts; l; l = l->next)
180 publish_location (location_manager, l->data, force_publication);
183 mc_accounts_list_free (accounts);
187 account_connection_changed_cb (EmpathyAccountManager *manager,
189 TpConnectionStatusReason reason,
190 TpConnectionStatus current,
191 TpConnectionStatus previous,
192 gpointer *location_manager)
194 DEBUG ("Account %s changed status from %d to %d", mc_account_get_display_name (account),
197 if (account && current == TP_CONNECTION_STATUS_CONNECTED)
198 publish_location (EMPATHY_LOCATION_MANAGER (location_manager), account,
203 empathy_location_manager_init (EmpathyLocationManager *location_manager)
206 EmpathyLocationManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (location_manager,
207 EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPriv);
209 location_manager->priv = priv;
210 priv->is_setup = FALSE;
211 priv->mc = empathy_mission_control_dup_singleton ();
212 priv->location = g_hash_table_new_full (g_direct_hash, g_direct_equal,
213 g_free, (GDestroyNotify) tp_g_value_slice_free);
215 // Setup settings status callbacks
216 conf = empathy_conf_get ();
217 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_PUBLISH, publish_cb,
219 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
220 resource_cb, location_manager);
221 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
222 resource_cb, location_manager);
223 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
224 resource_cb, location_manager);
225 empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
226 accuracy_cb, location_manager);
228 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK, location_manager);
229 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL, location_manager);
230 resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS, location_manager);
231 accuracy_cb (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY, location_manager);
232 publish_cb (conf, EMPATHY_PREFS_LOCATION_PUBLISH, location_manager);
234 // Setup account status callbacks
235 priv->account_manager = empathy_account_manager_dup_singleton ();
236 g_signal_connect (priv->account_manager,
237 "account-connection-changed",
238 G_CALLBACK (account_connection_changed_cb), location_manager);
243 location_manager_finalize (GObject *object)
245 EmpathyLocationManagerPriv *priv;
247 priv = GET_PRIV (object);
249 DEBUG ("finalize: %p", object);
250 g_object_unref (priv->account_manager);
252 G_OBJECT_CLASS (empathy_location_manager_parent_class)->finalize (object);
257 location_manager_get_property (GObject *object,
262 EmpathyLocationManagerPriv *priv;
264 priv = GET_PRIV (object);
269 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
276 location_manager_set_property (GObject *object,
281 EmpathyLocationManagerPriv *priv;
283 priv = GET_PRIV (object);
288 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
294 EmpathyLocationManager *
295 empathy_location_manager_dup_default (void)
297 static EmpathyLocationManager *singleton = NULL;
298 if (singleton == NULL)
300 singleton = g_object_new (EMPATHY_TYPE_LOCATION_MANAGER, NULL);
301 g_object_add_weak_pointer (G_OBJECT (singleton), (gpointer *)&singleton);
304 return g_object_ref (singleton);
308 update_timestamp (EmpathyLocationManager *location_manager)
310 EmpathyLocationManagerPriv *priv;
311 priv = GET_PRIV (location_manager);
316 timestamp = time (NULL);
317 stamp64 = (gint64) timestamp;
318 new_value = tp_g_value_slice_new (G_TYPE_INT64);
319 g_value_set_int64 (new_value, stamp64);
320 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_TIMESTAMP),
322 DEBUG ("\t - Timestamp: %" G_GINT64_FORMAT, stamp64);
326 initial_position_cb (GeocluePosition *position,
327 GeocluePositionFields fields,
332 GeoclueAccuracy *accuracy,
334 gpointer location_manager)
338 DEBUG ("Error: %s", error->message);
339 g_error_free (error);
342 position_changed_cb (position, fields, timestamp, latitude, longitude,
343 altitude, accuracy, location_manager);
347 position_changed_cb (GeocluePosition *position,
348 GeocluePositionFields fields,
353 GeoclueAccuracy *accuracy,
354 gpointer location_manager)
356 EmpathyLocationManagerPriv *priv;
357 priv = GET_PRIV (location_manager);
358 GeoclueAccuracyLevel level;
359 gdouble mean, horizontal, vertical;
362 geoclue_accuracy_get_details (accuracy, &level, &horizontal, &vertical);
363 DEBUG ("New position (accuracy level %d)", level);
364 if (level == GEOCLUE_ACCURACY_LEVEL_NONE)
367 if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
369 longitude += priv->reduce_value;
370 new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
371 g_value_set_double (new_value, longitude);
372 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LON),
374 DEBUG ("\t - Longitude: %f", longitude);
376 if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
378 latitude += priv->reduce_value;
379 new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
380 g_value_set_double (new_value, latitude);
381 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LAT),
383 DEBUG ("\t - Latitude: %f", latitude);
385 if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
387 new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
388 g_value_set_double (new_value, altitude);
389 g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_ALT),
391 DEBUG ("\t - Altitude: %f", altitude);
394 if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
396 mean = (horizontal + vertical) / 2.0;
397 new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
398 g_value_set_double (new_value, mean);
399 g_hash_table_insert (priv->location,
400 g_strdup (EMPATHY_LOCATION_ACCURACY), new_value);
401 DEBUG ("\t - Accuracy: %f", mean);
404 update_timestamp (location_manager);
405 if (priv->idle_id == 0)
406 g_idle_add (publish_on_idle, location_manager);
411 address_foreach_cb (gpointer key,
413 gpointer location_manager)
415 if (location_manager == NULL)
418 EmpathyLocationManagerPriv *priv;
419 priv = GET_PRIV (location_manager);
421 // Discard street information if reduced accuracy is on
422 if (priv->reduce_accuracy && strcmp (key, EMPATHY_LOCATION_STREET) == 0)
425 GValue *new_value = tp_g_value_slice_new (G_TYPE_STRING);
426 g_value_set_string (new_value, value);
428 g_hash_table_insert (priv->location, g_strdup (key), new_value);
429 DEBUG ("\t - %s: %s", (char*) key, (char*) value);
433 initial_address_cb (GeoclueAddress *address,
436 GeoclueAccuracy *accuracy,
438 gpointer location_manager)
442 DEBUG ("Error: %s", error->message);
443 g_error_free (error);
446 address_changed_cb (address, timestamp, details, accuracy, location_manager);
450 address_changed_cb (GeoclueAddress *address,
453 GeoclueAccuracy *accuracy,
454 gpointer location_manager)
456 GeoclueAccuracyLevel level;
457 geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
458 EmpathyLocationManagerPriv *priv;
460 DEBUG ("New address (accuracy level %d):", level);
462 priv = GET_PRIV (location_manager);
463 g_hash_table_remove_all (priv->location);
465 if (g_hash_table_size (details) == 0)
468 g_hash_table_foreach (details, address_foreach_cb, (gpointer)location_manager);
470 update_timestamp (location_manager);
471 if (priv->idle_id == 0)
472 g_idle_add (publish_on_idle, location_manager);
477 update_resources (EmpathyLocationManager *location_manager)
479 EmpathyLocationManagerPriv *priv;
481 priv = GET_PRIV (location_manager);
483 DEBUG ("Updating resources %d", priv->resources);
485 if (!geoclue_master_client_set_requirements (priv->gc_client,
486 GEOCLUE_ACCURACY_LEVEL_NONE, 0, TRUE, priv->resources,
489 g_printerr ("set_requirements failed");
496 geoclue_address_get_address_async (priv->gc_address,
497 initial_address_cb, location_manager);
498 geoclue_position_get_position_async (priv->gc_position,
499 initial_position_cb, location_manager);
504 setup_geoclue (EmpathyLocationManager *location_manager)
506 EmpathyLocationManagerPriv *priv;
508 priv = GET_PRIV (location_manager);
510 GeoclueMaster *master;
511 GError *error = NULL;
513 DEBUG ("Setting up Geoclue");
514 master = geoclue_master_get_default ();
515 priv->gc_client = geoclue_master_create_client (master, NULL, NULL);
516 g_object_unref (master);
518 update_resources (location_manager);
520 /* Get updated when the position is changes */
521 priv->gc_position = geoclue_master_client_create_position (
522 priv->gc_client, &error);
523 if (priv->gc_position == NULL)
525 g_printerr ("Failed to create GeocluePosition: %s", error->message);
526 g_error_free (error);
530 g_signal_connect (G_OBJECT (priv->gc_position), "position-changed",
531 G_CALLBACK (position_changed_cb), location_manager);
533 /* Get updated when the address changes */
534 priv->gc_address = geoclue_master_client_create_address (
535 priv->gc_client, &error);
536 if (priv->gc_address == NULL)
538 g_printerr ("Failed to create GeoclueAddress: %s", error->message);
539 g_error_free (error);
543 g_signal_connect (G_OBJECT (priv->gc_address), "address-changed",
544 G_CALLBACK (address_changed_cb), location_manager);
546 priv->is_setup = TRUE;
550 publish_cb (EmpathyConf *conf,
554 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
555 EmpathyLocationManagerPriv *priv;
556 gboolean can_publish;
558 DEBUG ("Publish Conf changed");
559 priv = GET_PRIV (manager);
562 if (empathy_conf_get_bool (conf, key, &can_publish) == FALSE)
565 if (can_publish == TRUE)
567 if (priv->is_setup == FALSE)
568 setup_geoclue (manager);
569 /* if still not setup than the init failed */
570 if (priv->is_setup == FALSE)
573 geoclue_address_get_address_async (priv->gc_address,
574 initial_address_cb, manager);
575 geoclue_position_get_position_async (priv->gc_position,
576 initial_position_cb, manager);
580 /* As per XEP-0080: send an empty location to have remove current
581 * location from the servers
583 g_hash_table_remove_all (priv->location);
584 publish_to_all_accounts (manager, TRUE);
591 accuracy_cb (EmpathyConf *conf,
595 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
596 EmpathyLocationManagerPriv *priv;
600 priv = GET_PRIV (manager);
601 DEBUG ("%s changed", key);
603 if (!empathy_conf_get_bool (conf, key, &enabled))
605 priv->reduce_accuracy = enabled;
609 GRand *rand = g_rand_new_with_seed (time (NULL));
610 priv->reduce_value = g_rand_double_range (rand, -0.25, 0.25);
614 priv->reduce_value = 0.0;
619 geoclue_address_get_address_async (priv->gc_address,
620 initial_address_cb, manager);
621 geoclue_position_get_position_async (priv->gc_position,
622 initial_position_cb, manager);
626 resource_cb (EmpathyConf *conf,
630 EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
631 EmpathyLocationManagerPriv *priv;
632 GeoclueResourceFlags resource = 0;
633 gboolean resource_enabled;
635 priv = GET_PRIV (manager);
636 DEBUG ("%s changed", key);
638 if (!empathy_conf_get_bool (conf, key, &resource_enabled))
641 if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK) == 0)
642 resource = GEOCLUE_RESOURCE_NETWORK;
643 if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL) == 0)
644 resource = GEOCLUE_RESOURCE_CELL;
645 if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS) == 0)
646 resource = GEOCLUE_RESOURCE_GPS;
648 if (resource_enabled)
649 priv->resources |= resource;
651 priv->resources &= ~resource;
654 update_resources (manager);