]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-location-manager.c
Publish location when enabling it
[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
26 #include <glib/gi18n.h>
27
28 #include <telepathy-glib/util.h>
29
30 #if HAVE_GEOCLUE
31 #include <geoclue/geoclue-master.h>
32 #endif
33
34 #include <extensions/extensions.h>
35
36 #include "empathy-location-manager.h"
37 #include "empathy-conf.h"
38
39 #include "libempathy/empathy-enum-types.h"
40 #include "libempathy/empathy-location.h"
41 #include "libempathy/empathy-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 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLocationManager)
48 typedef struct {
49     gboolean is_setup;
50     MissionControl *mc;
51     GHashTable *location;
52     gpointer token;
53 #if HAVE_GEOCLUE
54     GeoclueResourceFlags resources;
55     GeoclueMasterClient *gc_client;
56     GeocluePosition *gc_position;
57     GeoclueAddress *gc_address;
58 #endif
59 } EmpathyLocationManagerPriv;
60
61 static void location_manager_finalize (GObject *object);
62 static void location_manager_get_property (GObject *object, guint param_id,
63     GValue *value, GParamSpec *pspec);
64 static void location_manager_set_property (GObject *object, guint param_id,
65     const GValue *value, GParamSpec *pspec);
66 #if HAVE_GEOCLUE
67 static void position_changed_cb (GeocluePosition *position,
68     GeocluePositionFields fields, int timestamp, double latitude,
69     double longitude, double altitude, GeoclueAccuracy *accuracy,
70     gpointer user_data);
71 static void address_changed_cb (GeoclueAddress *address, int timestamp,
72     GHashTable *details, GeoclueAccuracy *accuracy, gpointer user_data);
73 static void setup_geoclue (EmpathyLocationManager *location_manager);
74 static void publish_cb (EmpathyConf  *conf, const gchar *key,
75     gpointer user_data);
76 static void update_resources (EmpathyLocationManager *location_manager);
77 static void resource_cb (EmpathyConf  *conf, const gchar *key,
78     gpointer user_data);
79 #endif
80
81 G_DEFINE_TYPE (EmpathyLocationManager, empathy_location_manager, G_TYPE_OBJECT);
82
83 enum
84 {
85   PROP_0,
86 };
87
88 static void
89 empathy_location_manager_class_init (EmpathyLocationManagerClass *class)
90 {
91   GObjectClass *object_class;
92
93   object_class = G_OBJECT_CLASS (class);
94
95   object_class->finalize = location_manager_finalize;
96   object_class->get_property = location_manager_get_property;
97   object_class->set_property = location_manager_set_property;
98
99   g_type_class_add_private (object_class, sizeof (EmpathyLocationManagerPriv));
100 }
101
102 static void
103 publish_location (EmpathyLocationManager *location_manager, McAccount *account)
104 {
105   EmpathyLocationManagerPriv *priv;
106   guint connection_status = -1;
107   gboolean can_publish;
108   EmpathyConf *conf = empathy_conf_get ();
109   EmpathyContactFactory *factory = empathy_contact_factory_new ();
110   priv = GET_PRIV (location_manager);
111
112   if (!empathy_conf_get_bool (conf, EMPATHY_PREFS_LOCATION_PUBLISH, &can_publish))
113     return;
114
115   if (!can_publish)
116     return;
117
118   connection_status = mission_control_get_connection_status (priv->mc,
119       account, NULL);
120
121   if (connection_status != TP_CONNECTION_STATUS_CONNECTED)
122     return;
123
124   DEBUG ("Publishing location to account %s", mc_account_get_display_name (account));
125
126   empathy_contact_factory_set_location (factory, account, priv->location);
127 }
128
129 static void
130 publish_location_to_all_accounts (EmpathyLocationManager *location_manager)
131 {
132   GList *accounts = NULL, *l;
133
134   accounts = mc_accounts_list_by_enabled (TRUE);
135   for (l = accounts; l; l = l->next)
136     {
137       publish_location (location_manager, l->data);
138     }
139
140   mc_accounts_list_free (accounts);
141 }
142
143 static void
144 account_status_changed_cb (MissionControl *mc,
145                            TpConnectionStatus status,
146                            McPresence presence,
147                            TpConnectionStatusReason reason,
148                            const gchar *unique_name,
149                            gpointer *location_manager)
150 {
151   DEBUG ("Account %s changed status to %d", unique_name, status);
152   McAccount *account = mc_account_lookup (unique_name);
153   if (account && status == TP_CONNECTION_STATUS_CONNECTED)
154     publish_location (EMPATHY_LOCATION_MANAGER (location_manager), account);
155 }
156
157 static void
158 empathy_location_manager_init (EmpathyLocationManager *location_manager)
159 {
160   EmpathyConf               *conf;
161   EmpathyLocationManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (location_manager,
162     EMPATHY_TYPE_LOCATION_MANAGER, EmpathyLocationManagerPriv);
163
164   location_manager->priv = priv;
165   priv->is_setup = FALSE;
166   priv->mc = empathy_mission_control_new ();
167   priv->location = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
168         (GDestroyNotify) tp_g_value_slice_free);
169
170   // Setup settings status callbacks
171   conf = empathy_conf_get ();
172   empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_PUBLISH, publish_cb,
173       location_manager);
174   empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK,
175       resource_cb, location_manager);
176   empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL,
177       resource_cb, location_manager);
178   empathy_conf_notify_add (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS,
179       resource_cb, location_manager);
180
181   publish_cb (conf, EMPATHY_PREFS_LOCATION_PUBLISH, location_manager);
182   resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK, location_manager);
183   resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_CELL, location_manager);
184   resource_cb (conf, EMPATHY_PREFS_LOCATION_RESOURCE_GPS, location_manager);
185
186   // Setup account status callbacks
187   priv->token = empathy_connect_to_account_status_changed (priv->mc,
188       G_CALLBACK (account_status_changed_cb), location_manager, NULL);
189 }
190
191
192 static void
193 location_manager_finalize (GObject *object)
194 {
195   EmpathyLocationManagerPriv *priv;
196
197   priv = GET_PRIV (object);
198
199   DEBUG ("finalize: %p", object);
200
201   G_OBJECT_CLASS (empathy_location_manager_parent_class)->finalize (object);
202 }
203
204
205 static void
206 location_manager_get_property (GObject *object,
207                       guint param_id,
208                       GValue *value,
209                       GParamSpec *pspec)
210 {
211   EmpathyLocationManagerPriv *priv;
212
213   priv = GET_PRIV (object);
214
215   switch (param_id)
216     {
217       default:
218         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
219         break;
220     };
221 }
222
223
224 static void
225 location_manager_set_property (GObject *object,
226                       guint param_id,
227                       const GValue *value,
228                       GParamSpec *pspec)
229 {
230   EmpathyLocationManagerPriv *priv;
231
232   priv = GET_PRIV (object);
233
234   switch (param_id)
235     {
236       default:
237         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
238         break;
239     };
240 }
241
242
243 EmpathyLocationManager *
244 empathy_location_manager_get_default (void)
245 {
246   static EmpathyLocationManager *singleton = NULL;
247   if (singleton == NULL)
248     singleton = g_object_new (EMPATHY_TYPE_LOCATION_MANAGER, NULL);
249   return singleton;
250 }
251
252
253 #if HAVE_GEOCLUE
254 static void
255 position_changed_cb (GeocluePosition *position,
256                      GeocluePositionFields fields,
257                      int timestamp,
258                      double latitude,
259                      double longitude,
260                      double altitude,
261                      GeoclueAccuracy *accuracy,
262                      gpointer location_manager)
263 {
264   EmpathyLocationManagerPriv *priv;
265   priv = GET_PRIV (location_manager);
266   GeoclueAccuracyLevel level;
267   GValue *new_value;
268
269   geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
270   DEBUG ("New position (accuracy level %d)", level);
271   if (level == GEOCLUE_ACCURACY_LEVEL_NONE)
272     return;
273
274   if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
275     {
276       new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
277       g_value_set_double (new_value, latitude);
278       g_hash_table_insert (priv->location, EMPATHY_LOCATION_LON, new_value);
279       DEBUG ("\t - Longitude: %f", longitude);
280     }
281   else if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
282     {
283       new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
284       g_value_set_double (new_value, latitude);
285       g_hash_table_insert (priv->location, EMPATHY_LOCATION_LAT, new_value);
286       DEBUG ("\t - Latitude: %f", latitude);
287     }
288   else if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
289     {
290       new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
291       g_value_set_double (new_value, altitude);
292       g_hash_table_insert (priv->location, EMPATHY_LOCATION_ALT, new_value);
293       DEBUG ("\t - Altitude: %f", altitude);
294     }
295
296   if (level == GEOCLUE_ACCURACY_LEVEL_DETAILED)
297     {
298       gdouble mean, horizontal, vertical;
299
300       geoclue_accuracy_get_details (accuracy, &level, &horizontal, &vertical);
301       mean = (horizontal + vertical) / 2.0;
302
303       new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
304       g_value_set_double (new_value, mean);
305       g_hash_table_insert (priv->location, EMPATHY_LOCATION_ACCURACY, new_value);
306       DEBUG ("\t - Accuracy: %f", mean);
307     }
308
309   publish_location_to_all_accounts (EMPATHY_LOCATION_MANAGER (location_manager));
310 }
311
312
313 static void
314 address_foreach_cb (gpointer key,
315                     gpointer value,
316                     gpointer location_manager)
317 {
318   if (location_manager == NULL)
319     return;
320
321   EmpathyLocationManagerPriv *priv;
322   priv = GET_PRIV (location_manager);
323
324   GValue *new_value = tp_g_value_slice_new (G_TYPE_STRING);
325   g_value_set_string (new_value, value);
326
327   g_hash_table_insert (priv->location, g_strdup (key), new_value);
328   DEBUG ("\t - %s: %s", (char*) key, (char*) value);
329 }
330
331
332 static void
333 address_changed_cb (GeoclueAddress *address,
334                     int timestamp,
335                     GHashTable *details,
336                     GeoclueAccuracy *accuracy,
337                     gpointer location_manager)
338 {
339   GeoclueAccuracyLevel level;
340   geoclue_accuracy_get_details (accuracy, &level, NULL, NULL);
341   EmpathyLocationManagerPriv *priv;
342
343   DEBUG ("New address (accuracy level %d):", level);
344
345   priv = GET_PRIV (location_manager);
346   g_hash_table_remove_all (priv->location);
347
348   g_hash_table_foreach (details, address_foreach_cb, (gpointer)location_manager);
349
350   publish_location_to_all_accounts (EMPATHY_LOCATION_MANAGER (location_manager));
351 }
352
353
354 static void
355 update_resources (EmpathyLocationManager *location_manager)
356 {
357   EmpathyLocationManagerPriv *priv;
358
359   priv = GET_PRIV (location_manager);
360
361   if (!priv->is_setup)
362     return;
363
364   DEBUG ("Updating resources");
365
366   if (!geoclue_master_client_set_requirements (priv->gc_client,
367           GEOCLUE_ACCURACY_LEVEL_COUNTRY, 0, TRUE, priv->resources,
368           NULL))
369     g_printerr ("set_requirements failed");
370 }
371
372
373 static void
374 setup_geoclue (EmpathyLocationManager *location_manager)
375 {
376   EmpathyLocationManagerPriv *priv;
377
378   priv = GET_PRIV (location_manager);
379
380   GeoclueMaster *master;
381   GError *error = NULL;
382
383   DEBUG ("Setting up Geoclue");
384   master = geoclue_master_get_default ();
385   priv->gc_client = geoclue_master_create_client (master, NULL, NULL);
386   g_object_unref (master);
387
388   update_resources (location_manager);
389
390   /* Get updated when the position is changes */
391   priv->gc_position = geoclue_master_client_create_position (
392       priv->gc_client, &error);
393   if (priv->gc_position == NULL)
394     {
395       g_printerr ("Failed to create GeocluePosition: %s", error->message);
396       return;
397     }
398
399   g_signal_connect (G_OBJECT (priv->gc_position), "position-changed",
400       G_CALLBACK (position_changed_cb), location_manager);
401
402   /* Get updated when the address changes */
403   priv->gc_address = geoclue_master_client_create_address (
404       priv->gc_client, &error);
405   if (priv->gc_address == NULL)
406     {
407       g_printerr ("Failed to create GeoclueAddress: %s", error->message);
408       return;
409     }
410
411   g_signal_connect (G_OBJECT (priv->gc_address), "address-changed",
412       G_CALLBACK (address_changed_cb), location_manager);
413
414   priv->is_setup = TRUE;
415 }
416
417 static void
418 publish_cb (EmpathyConf *conf,
419             const gchar *key,
420             gpointer user_data)
421 {
422   EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
423   EmpathyLocationManagerPriv *priv;
424   gboolean can_publish;
425
426   DEBUG ("Publish Conf changed");
427   priv = GET_PRIV (manager);
428   if (!empathy_conf_get_bool (conf, key, &can_publish))
429     return;
430
431   if (can_publish)
432     {
433       if (!priv->is_setup)
434         setup_geoclue (manager);
435       publish_location_to_all_accounts (manager);
436     }
437 }
438
439
440 static void 
441 resource_cb (EmpathyConf  *conf,
442              const gchar *key,
443              gpointer user_data)
444 {
445   EmpathyLocationManager *manager = EMPATHY_LOCATION_MANAGER (user_data);
446   EmpathyLocationManagerPriv *priv;
447   GeoclueResourceFlags resource = 0;
448   gboolean resource_enabled;
449
450   priv = GET_PRIV (manager);
451   DEBUG ("A Resource Conf changed");
452
453   if (empathy_conf_get_bool (conf, key, &resource_enabled))
454     {
455       if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK))
456         resource = GEOCLUE_RESOURCE_NETWORK;
457       if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_CELL))
458         resource = GEOCLUE_RESOURCE_CELL;
459       if (strcmp (key, EMPATHY_PREFS_LOCATION_RESOURCE_GPS))
460         resource = GEOCLUE_RESOURCE_GPS;
461     }
462   if (resource_enabled)
463     priv->resources |= resource;
464   else
465     priv->resources &= resource;
466
467   update_resources (manager);
468 }
469
470 #endif