]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-location-manager.c
Any location accuracy will do
[empathy.git] / libempathy-gtk / empathy-location-manager.c
1 /*
2  * Copyright (C) 2008 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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Authors: Pierre-Luc Beaudoin <pierre-luc@pierlux.com>
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 #if HAVE_GEOCLUE
32 #include <geoclue/geoclue-master.h>
33
34 #include <extensions/extensions.h>
35
36 #include "empathy-location-manager.h"
37 #include "empathy-conf.h"
38
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"
44
45 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
46 #include "libempathy/empathy-debug.h"
47
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLocationManager)
49 typedef struct {
50     gboolean is_setup;
51     MissionControl *mc;
52     GHashTable *location;
53     gpointer token;
54
55     GeoclueResourceFlags resources;
56     GeoclueMasterClient *gc_client;
57     GeocluePosition *gc_position;
58     GeoclueAddress *gc_address;
59
60     gboolean reduce_accuracy;
61     gdouble reduce_value;
62     EmpathyAccountManager *account_manager;
63 } EmpathyLocationManagerPriv;
64
65 static void location_manager_finalize (GObject *object);
66 static void location_manager_get_property (GObject *object, guint param_id,
67     GValue *value, GParamSpec *pspec);
68 static void location_manager_set_property (GObject *object, guint param_id,
69     const GValue *value, GParamSpec *pspec);
70 static void position_changed_cb (GeocluePosition *position,
71     GeocluePositionFields fields, int timestamp, double latitude,
72     double longitude, double altitude, GeoclueAccuracy *accuracy,
73     gpointer user_data);
74 static void address_changed_cb (GeoclueAddress *address, int timestamp,
75     GHashTable *details, GeoclueAccuracy *accuracy, gpointer user_data);
76 static void setup_geoclue (EmpathyLocationManager *location_manager);
77 static void publish_cb (EmpathyConf  *conf, const gchar *key,
78     gpointer user_data);
79 static void update_resources (EmpathyLocationManager *location_manager);
80 static void resource_cb (EmpathyConf  *conf, const gchar *key,
81     gpointer user_data);
82 static void accuracy_cb (EmpathyConf  *conf, const gchar *key,
83     gpointer user_data);
84 static void account_connection_changed_cb (EmpathyAccountManager *manager,
85     McAccount *account, TpConnectionStatusReason reason,
86     TpConnectionStatus current, TpConnectionStatus previous,
87     gpointer *location_manager);
88
89 G_DEFINE_TYPE (EmpathyLocationManager, empathy_location_manager, G_TYPE_OBJECT);
90
91 enum
92 {
93   PROP_0,
94 };
95
96 static void
97 empathy_location_manager_class_init (EmpathyLocationManagerClass *class)
98 {
99   GObjectClass *object_class;
100
101   object_class = G_OBJECT_CLASS (class);
102
103   object_class->finalize = location_manager_finalize;
104   object_class->get_property = location_manager_get_property;
105   object_class->set_property = location_manager_set_property;
106
107   g_type_class_add_private (object_class, sizeof (EmpathyLocationManagerPriv));
108 }
109
110 static void
111 publish_location (EmpathyLocationManager *location_manager,
112                   McAccount *account,
113                   gboolean force_publication)
114 {
115   EmpathyLocationManagerPriv *priv;
116   guint connection_status = -1;
117   gboolean can_publish;
118   EmpathyConf *conf = empathy_conf_get ();
119   TpConnection *conn;
120   EmpathyTpContactFactory *factory;
121
122   priv = GET_PRIV (location_manager);
123
124   conn = mission_control_get_tpconnection (priv->mc, account, NULL);
125   if (!conn)
126     return;
127
128   factory = empathy_tp_contact_factory_dup_singleton (conn);
129
130   if (force_publication == FALSE)
131     {
132       if (!empathy_conf_get_bool (conf, EMPATHY_PREFS_LOCATION_PUBLISH,
133             &can_publish))
134         return;
135
136       if (can_publish == FALSE)
137         return;
138     }
139
140   connection_status = mission_control_get_connection_status (priv->mc,
141       account, NULL);
142
143   if (connection_status != TP_CONNECTION_STATUS_CONNECTED)
144     return;
145
146   DEBUG ("Publishing location to account %s",
147       mc_account_get_display_name (account));
148
149   empathy_tp_contact_factory_set_location (factory, priv->location);
150   g_object_unref (factory);
151 }
152
153 static void
154 publish_location_to_all_accounts (EmpathyLocationManager *location_manager,
155                                   gboolean force_publication)
156 {
157   GList *accounts = NULL, *l;
158
159   accounts = mc_accounts_list_by_enabled (TRUE);
160   for (l = accounts; l; l = l->next)
161     {
162       publish_location (location_manager, l->data, force_publication);
163     }
164
165   mc_accounts_list_free (accounts);
166 }
167
168 static void
169 account_connection_changed_cb (EmpathyAccountManager *manager,
170                                McAccount *account,
171                                TpConnectionStatusReason reason,
172                                TpConnectionStatus current,
173                                TpConnectionStatus previous,
174                                gpointer *location_manager)
175 {
176   DEBUG ("Account %s changed status from %d to %d", mc_account_get_display_name (account),
177       previous, current);
178
179   if (account && current == TP_CONNECTION_STATUS_CONNECTED)
180     publish_location (EMPATHY_LOCATION_MANAGER (location_manager), account,
181         FALSE);
182 }
183
184 static void
185 empathy_location_manager_init (EmpathyLocationManager *location_manager)
186 {
187   EmpathyConf               *conf;
188   EmpathyLocationManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (location_manager,
189       EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPriv);
190
191   location_manager->priv = priv;
192   priv->is_setup = FALSE;
193   priv->mc = empathy_mission_control_dup_singleton ();
194   priv->location = g_hash_table_new_full (g_direct_hash, g_direct_equal,
195       g_free, (GDestroyNotify) tp_g_value_slice_free);
196
197   // Setup settings status callbacks
198   conf = empathy_conf_get ();
199   empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_PUBLISH, publish_cb,
200       location_manager);
201   empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
202       resource_cb, location_manager);
203   empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
204       resource_cb, location_manager);
205   empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
206       resource_cb, location_manager);
207   empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY,
208       accuracy_cb, location_manager);
209
210   resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK, location_manager);
211   resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL, location_manager);
212   resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS, location_manager);
213   accuracy_cb (conf, EMPATHY_PREFS_LOCATION_REDUCE_ACCURACY, location_manager);
214   publish_cb (conf, EMPATHY_PREFS_LOCATION_PUBLISH, location_manager);
215
216   // Setup account status callbacks
217   priv->account_manager = empathy_account_manager_dup_singleton ();
218   g_signal_connect (priv->account_manager,
219     "account-connection-changed",
220     G_CALLBACK (account_connection_changed_cb), location_manager);
221 }
222
223
224 static void
225 location_manager_finalize (GObject *object)
226 {
227   EmpathyLocationManagerPriv *priv;
228
229   priv = GET_PRIV (object);
230
231   DEBUG ("finalize: %p", object);
232   g_object_unref (priv->account_manager);
233
234   G_OBJECT_CLASS (empathy_location_manager_parent_class)->finalize (object);
235 }
236
237
238 static void
239 location_manager_get_property (GObject *object,
240                       guint param_id,
241                       GValue *value,
242                       GParamSpec *pspec)
243 {
244   EmpathyLocationManagerPriv *priv;
245
246   priv = GET_PRIV (object);
247
248   switch (param_id)
249     {
250       default:
251         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
252         break;
253     };
254 }
255
256
257 static void
258 location_manager_set_property (GObject *object,
259                       guint param_id,
260                       const GValue *value,
261                       GParamSpec *pspec)
262 {
263   EmpathyLocationManagerPriv *priv;
264
265   priv = GET_PRIV (object);
266
267   switch (param_id)
268     {
269       default:
270         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
271         break;
272     };
273 }
274
275
276 EmpathyLocationManager *
277 empathy_location_manager_get_default (void)
278 {
279   static EmpathyLocationManager *singleton = NULL;
280   if (singleton == NULL)
281     singleton = g_object_new (EMPATHY_TYPE_LOCATION_MANAGER, NULL);
282   return singleton;
283 }
284
285 static void
286 update_timestamp (EmpathyLocationManager *location_manager)
287 {
288   EmpathyLocationManagerPriv *priv;
289   priv = GET_PRIV (location_manager);
290   GValue *new_value;
291   gint64 stamp64;
292   time_t timestamp;
293
294   timestamp = time (NULL);
295   stamp64 = (gint64) timestamp;
296   new_value = tp_g_value_slice_new (G_TYPE_INT64);
297   g_value_set_int64 (new_value, stamp64);
298   g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_TIMESTAMP),
299       new_value);
300   DEBUG ("\t - Timestamp: %" G_GINT64_FORMAT, stamp64);
301 }
302
303 static void
304 initial_position_cb (GeocluePosition *position,
305                      GeocluePositionFields fields,
306                      int timestamp,
307                      double latitude,
308                      double longitude,
309                      double altitude,
310                      GeoclueAccuracy *accuracy,
311                      GError *error,
312                      gpointer location_manager)
313 {
314   if (error)
315     {
316       DEBUG ("Error: %s", error->message);
317       g_error_free (error);
318     }
319   else
320     position_changed_cb (position, fields, timestamp, latitude, longitude,
321       altitude, accuracy, location_manager);
322 }
323
324 static void
325 position_changed_cb (GeocluePosition *position,
326                      GeocluePositionFields fields,
327                      int timestamp,
328                      double latitude,
329                      double longitude,
330                      double altitude,
331                      GeoclueAccuracy *accuracy,
332                      gpointer location_manager)
333 {
334   EmpathyLocationManagerPriv *priv;
335   priv = GET_PRIV (location_manager);
336   GeoclueAccuracyLevel level;
337   gdouble mean, horizontal, vertical;
338   GValue *new_value;
339
340   geoclue_accuracy_get_details (accuracy, &level, &horizontal, &vertical);
341   DEBUG ("New position (accuracy level %d)", level);
342   if (level == GEOCLUE_ACCURACY_LEVEL_NONE)
343     return;
344
345   if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
346     {
347       longitude += priv->reduce_value;
348       new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
349       g_value_set_double (new_value, longitude);
350       g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LON),
351           new_value);
352       DEBUG ("\t - Longitude: %f", longitude);
353     }
354   if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
355     {
356       latitude += priv->reduce_value;
357       new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
358       g_value_set_double (new_value, latitude);
359       g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_LAT),
360           new_value);
361       DEBUG ("\t - Latitude: %f", latitude);
362     }
363   if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
364     {
365       new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
366       g_value_set_double (new_value, altitude);
367       g_hash_table_insert (priv->location, g_strdup (EMPATHY_LOCATION_ALT),
368           new_value);
369       DEBUG ("\t - Altitude: %f", altitude);
370     }
371
372   if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
373     {
374       mean = (horizontal + vertical) / 2.0;
375       new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
376       g_value_set_double (new_value, mean);
377       g_hash_table_insert (priv->location,
378           g_strdup (EMPATHY_LOCATION_ACCURACY), new_value);
379       DEBUG ("\t - Accuracy: %f", mean);
380     }
381
382   update_timestamp (location_manager);
383   publish_location_to_all_accounts (EMPATHY_LOCATION_MANAGER (location_manager),
384       FALSE);
385 }
386
387
388 static void
389 address_foreach_cb (gpointer key,
390                     gpointer value,
391                     gpointer location_manager)
392 {
393   if (location_manager == NULL)
394     return;
395
396   EmpathyLocationManagerPriv *priv;
397   priv = GET_PRIV (location_manager);
398
399   // Discard street information if reduced accuracy is on
400   if (priv->reduce_accuracy && strcmp (key, EMPATHY_LOCATION_STREET) == 0)
401     return;
402
403   GValue *new_value = tp_g_value_slice_new (G_TYPE_STRING);
404   g_value_set_string (new_value, value);
405
406   g_hash_table_insert (priv->location, g_strdup (key), new_value);
407   DEBUG ("\t - %s: %s", (char*) key, (char*) value);
408 }
409
410 static void
411 initial_address_cb (GeoclueAddress *address,
412                     int timestamp,
413                     GHashTable *details,
414                     GeoclueAccuracy *accuracy,
415                     GError *error,
416                     gpointer location_manager)
417 {
418   if (error)
419     {
420       DEBUG ("Error: %s", error->message);
421       g_error_free (error);
422     }
423   else
424     address_changed_cb (address, timestamp, details, accuracy, location_manager);
425 }
426
427 static void
428 address_changed_cb (GeoclueAddress *address,
429                     int timestamp,
430                     GHashTable *details,
431                     GeoclueAccuracy *accuracy,
432                     gpointer location_manager)
433 {
434   GeoclueAccuracyLevel level;
435   geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
436   EmpathyLocationManagerPriv *priv;
437
438   DEBUG ("New address (accuracy level %d):", level);
439
440   priv = GET_PRIV (location_manager);
441   g_hash_table_remove_all (priv->location);
442
443   if (g_hash_table_size (details) == 0)
444     return;
445
446   g_hash_table_foreach (details, address_foreach_cb, (gpointer)location_manager);
447
448   update_timestamp (location_manager);
449   publish_location_to_all_accounts (EMPATHY_LOCATION_MANAGER (location_manager),
450       FALSE);
451 }
452
453
454 static void
455 update_resources (EmpathyLocationManager *location_manager)
456 {
457   EmpathyLocationManagerPriv *priv;
458
459   priv = GET_PRIV (location_manager);
460
461   DEBUG ("Updating resources %d", priv->resources);
462
463   if (!geoclue_master_client_set_requirements (priv->gc_client,
464           GEOCLUE_ACCURACY_LEVEL_NONE, 0, TRUE, priv->resources,
465           NULL))
466     {
467       g_printerr ("set_requirements failed");
468       return;
469     }
470
471   if (!priv->is_setup)
472     return;
473
474   geoclue_address_get_address_async (priv->gc_address,
475       initial_address_cb, location_manager);
476   geoclue_position_get_position_async (priv->gc_position,
477       initial_position_cb, location_manager);
478
479 }
480
481
482 static void
483 setup_geoclue (EmpathyLocationManager *location_manager)
484 {
485   EmpathyLocationManagerPriv *priv;
486
487   priv = GET_PRIV (location_manager);
488
489   GeoclueMaster *master;
490   GError *error = NULL;
491
492   DEBUG ("Setting up Geoclue");
493   master = geoclue_master_get_default ();
494   priv->gc_client = geoclue_master_create_client (master, NULL, NULL);
495   g_object_unref (master);
496
497   update_resources (location_manager);
498
499   /* Get updated when the position is changes */
500   priv->gc_position = geoclue_master_client_create_position (
501       priv->gc_client, &error);
502   if (priv->gc_position == NULL)
503     {
504       g_printerr ("Failed to create GeocluePosition: %s", error->message);
505       g_error_free (error);
506       return;
507     }
508
509   g_signal_connect (G_OBJECT (priv->gc_position), "position-changed",
510       G_CALLBACK (position_changed_cb), location_manager);
511
512   /* Get updated when the address changes */
513   priv->gc_address = geoclue_master_client_create_address (
514       priv->gc_client, &error);
515   if (priv->gc_address == NULL)
516     {
517       g_printerr ("Failed to create GeoclueAddress: %s", error->message);
518       g_error_free (error);
519       return;
520     }
521
522   g_signal_connect (G_OBJECT (priv->gc_address), "address-changed",
523       G_CALLBACK (address_changed_cb), location_manager);
524
525   priv->is_setup = TRUE;
526 }
527
528 static void
529 publish_cb (EmpathyConf *conf,
530             const gchar *key,
531             gpointer user_data)
532 {
533   EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
534   EmpathyLocationManagerPriv *priv;
535   gboolean can_publish;
536
537   DEBUG ("Publish Conf changed");
538   priv = GET_PRIV (manager);
539
540
541   if (empathy_conf_get_bool (conf, key, &can_publish) == FALSE)
542     return;
543
544   if (can_publish == TRUE)
545     {
546       if (priv->is_setup == FALSE)
547         setup_geoclue (manager);
548       /* if still not setup than the init failed */
549       if (priv->is_setup == FALSE)
550         return;
551
552       geoclue_address_get_address_async (priv->gc_address,
553           initial_address_cb, manager);
554       geoclue_position_get_position_async (priv->gc_position,
555           initial_position_cb, manager);
556       publish_location_to_all_accounts (manager, FALSE);
557     }
558   else
559     {
560       /* As per XEP-0080: send an empty location to have remove current
561        * location from the servers
562        */
563       g_hash_table_remove_all (priv->location);
564       publish_location_to_all_accounts (manager, TRUE);
565     }
566
567 }
568
569
570 static void
571 accuracy_cb (EmpathyConf  *conf,
572              const gchar *key,
573              gpointer user_data)
574 {
575   EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
576   EmpathyLocationManagerPriv *priv;
577
578   gboolean enabled;
579
580   priv = GET_PRIV (manager);
581   DEBUG ("%s changed", key);
582
583   if (!empathy_conf_get_bool (conf, key, &enabled))
584     return;
585   priv->reduce_accuracy = enabled;
586
587   if (enabled)
588     {
589       GRand *rand = g_rand_new_with_seed (time (NULL));
590       priv->reduce_value = g_rand_double_range (rand, -0.25, 0.25);
591       g_rand_free (rand);
592     }
593   else
594     priv->reduce_value = 0.0;
595
596   if (!priv->is_setup)
597     return;
598
599   geoclue_address_get_address_async (priv->gc_address,
600       initial_address_cb, manager);
601   geoclue_position_get_position_async (priv->gc_position,
602       initial_position_cb, manager);
603 }
604
605 static void
606 resource_cb (EmpathyConf  *conf,
607              const gchar *key,
608              gpointer user_data)
609 {
610   EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
611   EmpathyLocationManagerPriv *priv;
612   GeoclueResourceFlags resource = 0;
613   gboolean resource_enabled;
614
615   priv = GET_PRIV (manager);
616   DEBUG ("%s changed", key);
617
618   if (!empathy_conf_get_bool (conf, key, &resource_enabled))
619     return;
620
621   if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK) == 0)
622     resource = GEOCLUE_RESOURCE_NETWORK;
623   if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL) == 0)
624     resource = GEOCLUE_RESOURCE_CELL;
625   if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS) == 0)
626     resource = GEOCLUE_RESOURCE_GPS;
627
628   if (resource_enabled)
629     priv->resources |= resource;
630   else
631     priv->resources &= ~resource;
632
633   if (priv->is_setup)
634     update_resources (manager);
635 }
636
637 #endif