]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-location-manager.c
Do not use math.h for trunc(). It is useless and it does not build because we don...
[empathy.git] / libempathy-gtk / empathy-location-manager.c
1 /*
2  * Copyright (C) 2009 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
18  *
19  * Authors: Pierre-Luc Beaudoin <pierre-luc.beaudoin@collabora.co.uk>
20  */
21
22 #include "config.h"
23
24 #include <string.h>
25 #include <time.h>
26
27 #include <glib/gi18n.h>
28
29 #include <telepathy-glib/util.h>
30
31 #include <geoclue/geoclue-master.h>
32
33 #include <extensions/extensions.h>
34
35 #include "empathy-location-manager.h"
36 #include "empathy-conf.h"
37
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"
43
44 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
45 #include "libempathy/empathy-debug.h"
46
47 /* Seconds before updating the location */
48 #define TIMEOUT 10
49 static EmpathyLocationManager *location_manager = NULL;
50
51 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLocationManager)
52 typedef struct {
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
57      */
58     GHashTable *location;
59
60     GeoclueResourceFlags resources;
61     GeoclueMasterClient *gc_client;
62     GeocluePosition *gc_position;
63     GeoclueAddress *gc_address;
64
65     gboolean reduce_accuracy;
66     EmpathyAccountManager *account_manager;
67
68     /* The idle id for publish_on_idle func */
69     guint timeout_id;
70 } EmpathyLocationManagerPriv;
71
72 G_DEFINE_TYPE (EmpathyLocationManager, empathy_location_manager, G_TYPE_OBJECT);
73
74 static GObject *
75 location_manager_constructor (GType type,
76     guint n_construct_params,
77     GObjectConstructParam *construct_params)
78 {
79   GObject *retval;
80
81   if (location_manager == NULL)
82     {
83       retval = G_OBJECT_CLASS (empathy_location_manager_parent_class)->constructor
84           (type, n_construct_params, construct_params);
85
86       location_manager = EMPATHY_LOCATION_MANAGER (retval);
87       g_object_add_weak_pointer (retval, (gpointer) &location_manager);
88     }
89   else
90     {
91       retval = g_object_ref (location_manager);
92     }
93
94   return retval;
95 }
96
97 static void
98 location_manager_dispose (GObject *object)
99 {
100   EmpathyLocationManagerPriv *priv = GET_PRIV (object);
101
102   if (priv->account_manager != NULL)
103   {
104     g_object_unref (priv->account_manager);
105     priv->account_manager = NULL;
106   }
107
108   if (priv->gc_client != NULL)
109   {
110     g_object_unref (priv->gc_client);
111     priv->gc_client = NULL;
112   }
113
114   if (priv->gc_position != NULL)
115   {
116     g_object_unref (priv->gc_position);
117     priv->gc_position = NULL;
118   }
119
120   if (priv->gc_address != NULL)
121   {
122     g_object_unref (priv->gc_address);
123     priv->gc_address = NULL;
124   }
125
126   if (priv->location != NULL)
127   {
128     g_hash_table_unref (priv->location);
129     priv->location = NULL;
130   }
131
132   G_OBJECT_CLASS (empathy_location_manager_parent_class)->finalize (object);
133 }
134
135 static void
136 location_manager_get_property (GObject *object,
137                       guint param_id,
138                       GValue *value,
139                       GParamSpec *pspec)
140 {
141   /*EmpathyLocationManagerPriv *priv = GET_PRIV (object); */
142
143   switch (param_id)
144     {
145       default:
146         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
147         break;
148     };
149 }
150
151 static void
152 location_manager_set_property (GObject *object,
153                       guint param_id,
154                       const GValue *value,
155                       GParamSpec *pspec)
156 {
157   /* EmpathyLocationManagerPriv *priv = GET_PRIV (object); */
158
159   switch (param_id)
160     {
161       default:
162         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
163         break;
164     };
165 }
166
167 static void
168 empathy_location_manager_class_init (EmpathyLocationManagerClass *class)
169 {
170   GObjectClass *object_class;
171
172   object_class = G_OBJECT_CLASS (class);
173
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;
178
179   g_type_class_add_private (object_class, sizeof (EmpathyLocationManagerPriv));
180 }
181
182 static void
183 publish_location (EmpathyLocationManager *location_manager,
184     TpConnection *conn,
185     gboolean force_publication)
186 {
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;
192
193   if (!conn)
194     return;
195
196   if (!force_publication)
197     {
198       if (!empathy_conf_get_bool (conf, EMPATHY_PREFS_LOCATION_PUBLISH,
199             &can_publish))
200         return;
201
202       if (!can_publish)
203         return;
204     }
205
206   connection_status = tp_connection_get_status (conn, NULL);
207
208   if (connection_status != TP_CONNECTION_STATUS_CONNECTED)
209     return;
210
211   DEBUG ("Publishing %s location to connection %p",
212       (g_hash_table_size (priv->location) == 0 ? "empty" : ""),
213       conn);
214
215   factory = empathy_tp_contact_factory_dup_singleton (conn);
216   empathy_tp_contact_factory_set_location (factory, priv->location);
217   g_object_unref (factory);
218 }
219
220 static void
221 publish_to_all_connections (EmpathyLocationManager *location_manager,
222     gboolean force_publication)
223 {
224   EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
225   GList *connections = NULL, *l;
226
227   connections = empathy_account_manager_dup_connections (priv->account_manager);
228   for (l = connections; l; l = l->next)
229     {
230       publish_location (location_manager, l->data, force_publication);
231       g_object_unref (l->data);
232     }
233   g_list_free (connections);
234
235 }
236
237 static gboolean
238 publish_on_idle (gpointer user_data)
239 {
240   EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
241   EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
242
243   priv->timeout_id = 0;
244   publish_to_all_connections (manager, TRUE);
245   return FALSE;
246 }
247
248 static void
249 new_connection_cb (EmpathyAccountManager *manager,
250     TpConnection *conn,
251     gpointer *location_manager)
252 {
253   EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
254   DEBUG ("New connection %p", conn);
255
256   /* Don't publish if it is already planned (ie startup) */
257   if (priv->timeout_id == 0)
258     {
259       publish_location (EMPATHY_LOCATION_MANAGER (location_manager), conn,
260           FALSE);
261     }
262 }
263
264 static void
265 update_timestamp (EmpathyLocationManager *location_manager)
266 {
267   EmpathyLocationManagerPriv *priv= GET_PRIV (location_manager);
268   GValue *new_value;
269   gint64 stamp64;
270   time_t timestamp;
271
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),
276       new_value);
277   DEBUG ("\t - Timestamp: %" G_GINT64_FORMAT, stamp64);
278 }
279
280 static void
281 address_changed_cb (GeoclueAddress *address,
282                     int timestamp,
283                     GHashTable *details,
284                     GeoclueAccuracy *accuracy,
285                     gpointer location_manager)
286 {
287   GeoclueAccuracyLevel level;
288   EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
289   GHashTableIter iter;
290   gpointer key, value;
291
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 */
295
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);
302
303   if (g_hash_table_size (details) == 0)
304     {
305       DEBUG ("\t - (Empty)");
306       return;
307     }
308
309   g_hash_table_iter_init (&iter, details);
310   while (g_hash_table_iter_next (&iter, &key, &value))
311     {
312       GValue *new_value;
313       /* Discard street information if reduced accuracy is on */
314       if (priv->reduce_accuracy &&
315           !tp_strdiff (key, EMPATHY_LOCATION_STREET))
316         continue;
317
318       new_value = tp_g_value_slice_new_string (value);
319       g_hash_table_insert (priv->location, g_strdup (key), new_value);
320
321       DEBUG ("\t - %s: %s", (gchar *) key, (gchar *) value);
322     }
323
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);
327 }
328
329 static void
330 initial_address_cb (GeoclueAddress *address,
331                     int timestamp,
332                     GHashTable *details,
333                     GeoclueAccuracy *accuracy,
334                     GError *error,
335                     gpointer location_manager)
336 {
337   if (error)
338     {
339       DEBUG ("Error: %s", error->message);
340       g_error_free (error);
341     }
342   else
343     {
344       address_changed_cb (address, timestamp, details, accuracy, location_manager);
345     }
346 }
347
348 static void
349 position_changed_cb (GeocluePosition *position,
350                      GeocluePositionFields fields,
351                      int timestamp,
352                      double latitude,
353                      double longitude,
354                      double altitude,
355                      GeoclueAccuracy *accuracy,
356                      gpointer location_manager)
357 {
358   EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
359   GeoclueAccuracyLevel level;
360   gdouble mean, horizontal, vertical;
361   GValue *new_value;
362
363
364   geoclue_accuracy_get_details (accuracy, &level, &horizontal, &vertical);
365   DEBUG ("New position (accuracy level %d)", level);
366   if (level == GEOCLUE_ACCURACY_LEVEL_NONE)
367     return;
368
369   if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
370     {
371
372       if (priv->reduce_accuracy)
373         /* Truncate at 1 decimal place */
374         longitude = ((int) (longitude * 10)) / 10.0;
375
376       new_value = tp_g_value_slice_new_double (longitude);
377       g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LON),
378           new_value);
379       DEBUG ("\t - Longitude: %f", longitude);
380     }
381   else
382     {
383       g_hash_table_remove (priv->location, EMPATHY_LOCATION_LON);
384     }
385
386   if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
387     {
388       if (priv->reduce_accuracy)
389         /* Truncate at 1 decimal place */
390         latitude = ((int) (latitude * 10)) / 10.0;
391
392       new_value = tp_g_value_slice_new_double (latitude);
393       g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_LAT),
394           new_value);
395       DEBUG ("\t - Latitude: %f", latitude);
396     }
397   else
398     {
399       g_hash_table_remove (priv->location, EMPATHY_LOCATION_LAT);
400     }
401
402   if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
403     {
404       new_value = tp_g_value_slice_new_double (altitude);
405       g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_ALT),
406           new_value);
407       DEBUG ("\t - Altitude: %f", altitude);
408     }
409   else
410     {
411       g_hash_table_remove (priv->location, EMPATHY_LOCATION_ALT);
412     }
413
414   if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
415     {
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);
421     }
422   else
423     {
424       g_hash_table_remove (priv->location, EMPATHY_LOCATION_ACCURACY);
425     }
426
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);
430 }
431
432 static void
433 initial_position_cb (GeocluePosition *position,
434                      GeocluePositionFields fields,
435                      int timestamp,
436                      double latitude,
437                      double longitude,
438                      double altitude,
439                      GeoclueAccuracy *accuracy,
440                      GError *error,
441                      gpointer location_manager)
442 {
443   if (error)
444     {
445       DEBUG ("Error: %s", error->message);
446       g_error_free (error);
447     }
448   else
449     {
450       position_changed_cb (position, fields, timestamp, latitude, longitude,
451           altitude, accuracy, location_manager);
452     }
453 }
454
455 static void
456 update_resources (EmpathyLocationManager *location_manager)
457 {
458   EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
459
460   DEBUG ("Updating resources %d", priv->resources);
461
462   if (!priv->geoclue_is_setup)
463     return;
464
465   /* As per Geoclue bug #15126, using NONE results in no address
466    * being found as geoclue-manual report an empty address with
467    * accuracy = NONE */
468   if (!geoclue_master_client_set_requirements (priv->gc_client,
469           GEOCLUE_ACCURACY_LEVEL_COUNTRY, 0, TRUE, priv->resources,
470           NULL))
471     {
472       DEBUG ("set_requirements failed");
473       return;
474     }
475
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);
480 }
481
482 static void
483 setup_geoclue (EmpathyLocationManager *location_manager)
484 {
485   EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
486
487   GeoclueMaster *master;
488   GError *error = NULL;
489
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);
494
495   if (priv->gc_client == NULL)
496     {
497       DEBUG ("Failed to GeoclueMasterClient: %s", error->message);
498       g_error_free (error);
499       return;
500     }
501
502   update_resources (location_manager);
503
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)
508     {
509       DEBUG ("Failed to create GeocluePosition: %s", error->message);
510       g_error_free (error);
511       return;
512     }
513
514   g_signal_connect (G_OBJECT (priv->gc_position), "position-changed",
515       G_CALLBACK (position_changed_cb), location_manager);
516
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)
521     {
522       DEBUG ("Failed to create GeoclueAddress: %s", error->message);
523       g_error_free (error);
524       return;
525     }
526
527   g_signal_connect (G_OBJECT (priv->gc_address), "address-changed",
528       G_CALLBACK (address_changed_cb), location_manager);
529
530   priv->geoclue_is_setup = TRUE;
531 }
532
533 static void
534 publish_cb (EmpathyConf *conf,
535             const gchar *key,
536             gpointer user_data)
537 {
538   EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
539   EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
540   gboolean can_publish;
541
542   DEBUG ("Publish Conf changed");
543
544
545   if (!empathy_conf_get_bool (conf, key, &can_publish))
546     return;
547
548   if (can_publish)
549     {
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)
554         return;
555
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);
560     }
561   else
562     {
563       /* As per XEP-0080: send an empty location to have remove current
564        * location from the servers
565        */
566       g_hash_table_remove_all (priv->location);
567       publish_to_all_connections (manager, TRUE);
568     }
569
570 }
571
572 static void
573 resource_cb (EmpathyConf  *conf,
574              const gchar *key,
575              gpointer user_data)
576 {
577   EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
578   EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
579   GeoclueResourceFlags resource = 0;
580   gboolean resource_enabled;
581
582   DEBUG ("%s changed", key);
583
584   if (!empathy_conf_get_bool (conf, key, &resource_enabled))
585     return;
586
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;
593
594   if (resource_enabled)
595     priv->resources |= resource;
596   else
597     priv->resources &= ~resource;
598
599   if (priv->geoclue_is_setup)
600     update_resources (manager);
601 }
602
603 static void
604 accuracy_cb (EmpathyConf  *conf,
605              const gchar *key,
606              gpointer user_data)
607 {
608   EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
609   EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
610
611   gboolean enabled;
612
613   DEBUG ("%s changed", key);
614
615   if (!empathy_conf_get_bool (conf, key, &enabled))
616     return;
617   priv->reduce_accuracy = enabled;
618
619   if (!priv->geoclue_is_setup)
620     return;
621
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);
626 }
627
628 static void
629 empathy_location_manager_init (EmpathyLocationManager *location_manager)
630 {
631   EmpathyConf               *conf;
632   EmpathyLocationManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (location_manager,
633       EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPriv);
634
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);
639
640   /* Setup account status callbacks */
641   priv->account_manager = empathy_account_manager_dup_singleton ();
642   g_signal_connect (priv->account_manager,
643     "new-connection",
644     G_CALLBACK (new_connection_cb), location_manager);
645
646   /* Setup settings status callbacks */
647   conf = empathy_conf_get ();
648   empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_PUBLISH, publish_cb,
649       location_manager);
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);
658
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);
664 }
665
666 EmpathyLocationManager *
667 empathy_location_manager_dup_singleton (void)
668 {
669   return EMPATHY_LOCATION_MANAGER (g_object_new (EMPATHY_TYPE_LOCATION_MANAGER,
670       NULL));
671 }