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