]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-location-manager.c
empathy-location-manager: remove a trailing space
[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     gdouble reduce_value;
67     EmpathyAccountManager *account_manager;
68
69     /* The idle id for publish_on_idle func */
70     guint timeout_id;
71 } EmpathyLocationManagerPriv;
72
73 G_DEFINE_TYPE (EmpathyLocationManager, empathy_location_manager, G_TYPE_OBJECT);
74
75 static GObject *
76 location_manager_constructor (GType type,
77     guint n_construct_params,
78     GObjectConstructParam *construct_params)
79 {
80   GObject *retval;
81
82   if (location_manager == NULL)
83     {
84       retval = G_OBJECT_CLASS (empathy_location_manager_parent_class)->constructor
85           (type, n_construct_params, construct_params);
86
87       location_manager = EMPATHY_LOCATION_MANAGER (retval);
88       g_object_add_weak_pointer (retval, (gpointer) &location_manager);
89     }
90   else
91     {
92       retval = g_object_ref (location_manager);
93     }
94
95   return retval;
96 }
97
98 static void
99 location_manager_dispose (GObject *object)
100 {
101   EmpathyLocationManagerPriv *priv = GET_PRIV (object);
102
103   if (priv->account_manager != NULL)
104   {
105     g_object_unref (priv->account_manager);
106     priv->account_manager = NULL;
107   }
108
109   if (priv->gc_client != NULL)
110   {
111     g_object_unref (priv->gc_client);
112     priv->gc_client = NULL;
113   }
114
115   if (priv->gc_position != NULL)
116   {
117     g_object_unref (priv->gc_position);
118     priv->gc_position = NULL;
119   }
120
121   if (priv->gc_address != NULL)
122   {
123     g_object_unref (priv->gc_address);
124     priv->gc_address = NULL;
125   }
126
127   if (priv->location != NULL)
128   {
129     g_hash_table_unref (priv->location);
130     priv->location = NULL;
131   }
132
133   G_OBJECT_CLASS (empathy_location_manager_parent_class)->finalize (object);
134 }
135
136 static void
137 location_manager_get_property (GObject *object,
138                       guint param_id,
139                       GValue *value,
140                       GParamSpec *pspec)
141 {
142   /*EmpathyLocationManagerPriv *priv = GET_PRIV (object); */
143
144   switch (param_id)
145     {
146       default:
147         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
148         break;
149     };
150 }
151
152 static void
153 location_manager_set_property (GObject *object,
154                       guint param_id,
155                       const GValue *value,
156                       GParamSpec *pspec)
157 {
158   /* EmpathyLocationManagerPriv *priv = GET_PRIV (object); */
159
160   switch (param_id)
161     {
162       default:
163         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
164         break;
165     };
166 }
167
168 static void
169 empathy_location_manager_class_init (EmpathyLocationManagerClass *class)
170 {
171   GObjectClass *object_class;
172
173   object_class = G_OBJECT_CLASS (class);
174
175   object_class->constructor = location_manager_constructor;
176   object_class->dispose = location_manager_dispose;
177   object_class->get_property = location_manager_get_property;
178   object_class->set_property = location_manager_set_property;
179
180   g_type_class_add_private (object_class, sizeof (EmpathyLocationManagerPriv));
181 }
182
183 static void
184 publish_location (EmpathyLocationManager *location_manager,
185     TpConnection *conn,
186     gboolean force_publication)
187 {
188   EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
189   guint connection_status = -1;
190   gboolean can_publish;
191   EmpathyConf *conf = empathy_conf_get ();
192   EmpathyTpContactFactory *factory;
193
194   if (!conn)
195     return;
196
197   if (force_publication == FALSE)
198     {
199       if (!empathy_conf_get_bool (conf, EMPATHY_PREFS_LOCATION_PUBLISH,
200             &can_publish))
201         return;
202
203       if (can_publish == FALSE)
204         return;
205     }
206
207   connection_status = tp_connection_get_status (conn, NULL);
208
209   if (connection_status != TP_CONNECTION_STATUS_CONNECTED)
210     return;
211
212   DEBUG ("Publishing %s location to connection %p",
213       (g_hash_table_size (priv->location) == 0 ? "empty" : ""),
214       conn);
215
216   factory = empathy_tp_contact_factory_dup_singleton (conn);
217   empathy_tp_contact_factory_set_location (factory, priv->location);
218   g_object_unref (factory);
219 }
220
221 static void
222 publish_to_all_connections (EmpathyLocationManager *location_manager,
223     gboolean force_publication)
224 {
225   EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
226   GList *connections = NULL, *l;
227
228   connections = empathy_account_manager_dup_connections (priv->account_manager);
229   for (l = connections; l; l = l->next)
230     {
231       publish_location (location_manager, l->data, force_publication);
232       g_object_unref (l->data);
233     }
234   g_list_free (connections);
235
236 }
237
238 static gboolean
239 publish_on_idle (gpointer user_data)
240 {
241   EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
242   EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
243
244   priv->timeout_id = 0;
245   publish_to_all_connections (manager, TRUE);
246   return FALSE;
247 }
248
249 static void
250 new_connection_cb (EmpathyAccountManager *manager,
251     TpConnection *conn,
252     gpointer *location_manager)
253 {
254   EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
255   DEBUG ("New connection %p", conn);
256
257   /* Don't publish if it is already planned (ie startup) */
258   if (priv->timeout_id == 0)
259     {
260       publish_location (EMPATHY_LOCATION_MANAGER (location_manager), conn,
261           FALSE);
262     }
263 }
264
265 static void
266 update_timestamp (EmpathyLocationManager *location_manager)
267 {
268   EmpathyLocationManagerPriv *priv= GET_PRIV (location_manager);
269   GValue *new_value;
270   gint64 stamp64;
271   time_t timestamp;
272
273   timestamp = time (NULL);
274   stamp64 = (gint64) timestamp;
275   new_value = tp_g_value_slice_new_int64 (stamp64);
276   g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_TIMESTAMP),
277       new_value);
278   DEBUG ("\t - Timestamp: %" G_GINT64_FORMAT, stamp64);
279 }
280
281 static void
282 address_changed_cb (GeoclueAddress *address,
283                     int timestamp,
284                     GHashTable *details,
285                     GeoclueAccuracy *accuracy,
286                     gpointer location_manager)
287 {
288   GeoclueAccuracyLevel level;
289   EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
290   GHashTableIter iter;
291   gpointer key, value;
292
293   geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
294   DEBUG ("New address (accuracy level %d):", level);
295   /* FIXME: Publish accuracy level also considering the position's */
296
297   g_hash_table_remove (priv->location, EMPATHY_LOCATION_STREET);
298   g_hash_table_remove (priv->location, EMPATHY_LOCATION_AREA);
299   g_hash_table_remove (priv->location, EMPATHY_LOCATION_REGION);
300   g_hash_table_remove (priv->location, EMPATHY_LOCATION_COUNTRY);
301   g_hash_table_remove (priv->location, EMPATHY_LOCATION_COUNTRY_CODE);
302   g_hash_table_remove (priv->location, EMPATHY_LOCATION_POSTAL_CODE);
303
304   if (g_hash_table_size (details) == 0)
305     {
306       DEBUG ("\t - (Empty)");
307       return;
308     }
309
310   g_hash_table_iter_init (&iter, details);
311   while (g_hash_table_iter_next (&iter, &key, &value))
312     {
313       GValue *new_value;
314       /* Discard street information if reduced accuracy is on */
315       if (priv->reduce_accuracy && strcmp (key, EMPATHY_LOCATION_STREET) == 0)
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       longitude += priv->reduce_value;
372       new_value = tp_g_value_slice_new_double (longitude);
373       g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LON),
374           new_value);
375       DEBUG ("\t - Longitude: %f", longitude);
376     }
377   else
378     {
379       g_hash_table_remove (priv->location, EMPATHY_LOCATION_LON);
380     }
381
382   if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
383     {
384       latitude += priv->reduce_value;
385       new_value = tp_g_value_slice_new_double (latitude);
386       g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_LAT),
387           new_value);
388       DEBUG ("\t - Latitude: %f", latitude);
389     }
390   else
391     {
392       g_hash_table_remove (priv->location, EMPATHY_LOCATION_LAT);
393     }
394
395   if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
396     {
397       new_value = tp_g_value_slice_new_double (altitude);
398       g_hash_table_replace (priv->location, g_strdup (EMPATHY_LOCATION_ALT),
399           new_value);
400       DEBUG ("\t - Altitude: %f", altitude);
401     }
402   else
403     {
404       g_hash_table_remove (priv->location, EMPATHY_LOCATION_ALT);
405     }
406
407   if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
408     {
409       mean = (horizontal + vertical) / 2.0;
410       new_value = tp_g_value_slice_new_double (mean);
411       g_hash_table_replace (priv->location,
412           g_strdup (EMPATHY_LOCATION_ACCURACY), new_value);
413       DEBUG ("\t - Accuracy: %f", mean);
414     }
415   else
416     {
417       g_hash_table_remove (priv->location, EMPATHY_LOCATION_ACCURACY);
418     }
419
420   update_timestamp (location_manager);
421   if (priv->timeout_id == 0)
422     priv->timeout_id = g_timeout_add_seconds (TIMEOUT, publish_on_idle, location_manager);
423 }
424
425 static void
426 initial_position_cb (GeocluePosition *position,
427                      GeocluePositionFields fields,
428                      int timestamp,
429                      double latitude,
430                      double longitude,
431                      double altitude,
432                      GeoclueAccuracy *accuracy,
433                      GError *error,
434                      gpointer location_manager)
435 {
436   if (error)
437     {
438       DEBUG ("Error: %s", error->message);
439       g_error_free (error);
440     }
441   else
442     {
443       position_changed_cb (position, fields, timestamp, latitude, longitude,
444           altitude, accuracy, location_manager);
445     }
446 }
447
448 static void
449 update_resources (EmpathyLocationManager *location_manager)
450 {
451   EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
452
453   DEBUG ("Updating resources %d", priv->resources);
454
455   /* As per Geoclue bug #15126, using NONE results in no address
456    * being found as geoclue-manual report an empty address with
457    * accuracy = NONE */
458   if (!geoclue_master_client_set_requirements (priv->gc_client,
459           GEOCLUE_ACCURACY_LEVEL_COUNTRY, 0, TRUE, priv->resources,
460           NULL))
461     {
462       DEBUG ("set_requirements failed");
463       return;
464     }
465
466   if (!priv->geoclue_is_setup)
467     return;
468
469   geoclue_address_get_address_async (priv->gc_address,
470       initial_address_cb, location_manager);
471   geoclue_position_get_position_async (priv->gc_position,
472       initial_position_cb, location_manager);
473 }
474
475 static void
476 setup_geoclue (EmpathyLocationManager *location_manager)
477 {
478   EmpathyLocationManagerPriv *priv = GET_PRIV (location_manager);
479
480   GeoclueMaster *master;
481   GError *error = NULL;
482
483   DEBUG ("Setting up Geoclue");
484   master = geoclue_master_get_default ();
485   priv->gc_client = geoclue_master_create_client (master, NULL, NULL);
486   g_object_unref (master);
487
488   update_resources (location_manager);
489
490   /* Get updated when the position is changes */
491   priv->gc_position = geoclue_master_client_create_position (
492       priv->gc_client, &error);
493   if (priv->gc_position == NULL)
494     {
495       DEBUG ("Failed to create GeocluePosition: %s", error->message);
496       g_error_free (error);
497       return;
498     }
499
500   g_signal_connect (G_OBJECT (priv->gc_position), "position-changed",
501       G_CALLBACK (position_changed_cb), location_manager);
502
503   /* Get updated when the address changes */
504   priv->gc_address = geoclue_master_client_create_address (
505       priv->gc_client, &error);
506   if (priv->gc_address == NULL)
507     {
508       DEBUG ("Failed to create GeoclueAddress: %s", error->message);
509       g_error_free (error);
510       return;
511     }
512
513   g_signal_connect (G_OBJECT (priv->gc_address), "address-changed",
514       G_CALLBACK (address_changed_cb), location_manager);
515
516   priv->geoclue_is_setup = TRUE;
517 }
518
519 static void
520 publish_cb (EmpathyConf *conf,
521             const gchar *key,
522             gpointer user_data)
523 {
524   EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
525   EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
526   gboolean can_publish;
527
528   DEBUG ("Publish Conf changed");
529
530
531   if (empathy_conf_get_bool (conf, key, &can_publish) == FALSE)
532     return;
533
534   if (can_publish == TRUE)
535     {
536       if (priv->geoclue_is_setup == FALSE)
537         setup_geoclue (manager);
538       /* if still not setup than the init failed */
539       if (priv->geoclue_is_setup == FALSE)
540         return;
541
542       geoclue_address_get_address_async (priv->gc_address,
543           initial_address_cb, manager);
544       geoclue_position_get_position_async (priv->gc_position,
545           initial_position_cb, manager);
546     }
547   else
548     {
549       /* As per XEP-0080: send an empty location to have remove current
550        * location from the servers
551        */
552       g_hash_table_remove_all (priv->location);
553       publish_to_all_connections (manager, TRUE);
554     }
555
556 }
557
558 static void
559 resource_cb (EmpathyConf  *conf,
560              const gchar *key,
561              gpointer user_data)
562 {
563   EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
564   EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
565   GeoclueResourceFlags resource = 0;
566   gboolean resource_enabled;
567
568   DEBUG ("%s changed", key);
569
570   if (!empathy_conf_get_bool (conf, key, &resource_enabled))
571     return;
572
573   if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK) == 0)
574     resource = GEOCLUE_RESOURCE_NETWORK;
575   if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL) == 0)
576     resource = GEOCLUE_RESOURCE_CELL;
577   if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS) == 0)
578     resource = GEOCLUE_RESOURCE_GPS;
579
580   if (resource_enabled)
581     priv->resources |= resource;
582   else
583     priv->resources &= ~resource;
584
585   if (priv->geoclue_is_setup)
586     update_resources (manager);
587 }
588
589 static void
590 accuracy_cb (EmpathyConf  *conf,
591              const gchar *key,
592              gpointer user_data)
593 {
594   EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
595   EmpathyLocationManagerPriv *priv = GET_PRIV (manager);
596
597   gboolean enabled;
598
599   DEBUG ("%s changed", key);
600
601   if (!empathy_conf_get_bool (conf, key, &enabled))
602     return;
603   priv->reduce_accuracy = enabled;
604
605   if (enabled)
606     {
607       GRand *rand = g_rand_new_with_seed (time (NULL));
608       priv->reduce_value = g_rand_double_range (rand, -0.25, 0.25);
609       g_rand_free (rand);
610     }
611   else
612     {
613       priv->reduce_value = 0.0;
614     }
615
616   if (!priv->geoclue_is_setup)
617     return;
618
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);
623 }
624
625 static void
626 empathy_location_manager_init (EmpathyLocationManager *location_manager)
627 {
628   EmpathyConf               *conf;
629   EmpathyLocationManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (location_manager,
630       EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPriv);
631
632   location_manager->priv = priv;
633   priv->geoclue_is_setup = FALSE;
634   priv->location = g_hash_table_new_full (g_direct_hash, g_direct_equal,
635       g_free, (GDestroyNotify) tp_g_value_slice_free);
636
637   /* Setup account status callbacks */
638   priv->account_manager = empathy_account_manager_dup_singleton ();
639   g_signal_connect (priv->account_manager,
640     "new-connection",
641     G_CALLBACK (new_connection_cb), location_manager);
642
643   /* Setup settings status callbacks */
644   conf = empathy_conf_get ();
645   empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_PUBLISH, publish_cb,
646       location_manager);
647   empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
648       resource_cb, location_manager);
649   empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
650       resource_cb, location_manager);
651   empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
652       resource_cb, location_manager);
653   empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
654       accuracy_cb, location_manager);
655
656   resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK, location_manager);
657   resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL, location_manager);
658   resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS, location_manager);
659   accuracy_cb (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY, location_manager);
660   publish_cb (conf, EMPATHY_PREFS_LOCATION_PUBLISH, location_manager);
661 }
662
663 EmpathyLocationManager *
664 empathy_location_manager_dup_singleton (void)
665 {
666   return EMPATHY_LOCATION_MANAGER (g_object_new (EMPATHY_TYPE_LOCATION_MANAGER,
667       NULL));
668 }