]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-geoclue-helper.c
Merge branch 'gnome-3-8'
[empathy.git] / libempathy-gtk / empathy-geoclue-helper.c
1 /*
2  * empathy-geoclue-helper.c
3  *
4  * Copyright (C) 2013 Collabora Ltd. <http://www.collabora.co.uk/>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "config.h"
22
23 #include <gio/gio.h>
24
25 #include "empathy-geoclue-helper.h"
26
27 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
28 #include "empathy-debug.h"
29
30 #define GEOCLUE_BUS_NAME "org.freedesktop.GeoClue2"
31
32 /**
33  * SECTION: empathy-geoclue-helper
34  * @title: EmpathyGeoclueHelper
35  * @short_description: TODO
36  *
37  * TODO
38  */
39
40 /**
41  * EmpathyGeoclueHelper:
42  *
43  * Data structure representing a #EmpathyGeoclueHelper.
44  *
45  * Since: UNRELEASED
46  */
47
48 /**
49  * EmpathyGeoclueHelperClass:
50  *
51  * The class of a #EmpathyGeoclueHelper.
52  *
53  * Since: UNRELEASED
54  */
55
56 static void async_initable_iface_init (GAsyncInitableIface *iface);
57
58 G_DEFINE_TYPE_WITH_CODE (EmpathyGeoclueHelper, empathy_geoclue_helper,
59     G_TYPE_OBJECT,
60     G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init));
61
62 enum
63 {
64   PROP_DISTANCE_THRESHOLD = 1,
65   PROP_LOCATION,
66   N_PROPS
67 };
68
69 enum
70 {
71   SIG_LOCATION_CHANGED,
72   LAST_SIGNAL
73 };
74
75 static guint signals[LAST_SIGNAL];
76
77 struct _EmpathyGeoclueHelperPriv
78 {
79   guint distance_threshold;
80   GClueLocation *location;
81
82   gboolean started;
83   GClueClient *client;
84 };
85
86 static void
87 empathy_geoclue_helper_get_property (GObject *object,
88     guint property_id,
89     GValue *value,
90     GParamSpec *pspec)
91 {
92   EmpathyGeoclueHelper *self = EMPATHY_GEOCLUE_HELPER (object);
93
94   switch (property_id)
95     {
96       case PROP_DISTANCE_THRESHOLD:
97         g_value_set_uint (value, self->priv->distance_threshold);
98         break;
99       case PROP_LOCATION:
100         g_value_set_object (value, self->priv->location);
101         break;
102       default:
103         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
104         break;
105     }
106 }
107
108 static void
109 empathy_geoclue_helper_set_property (GObject *object,
110     guint property_id,
111     const GValue *value,
112     GParamSpec *pspec)
113 {
114   EmpathyGeoclueHelper *self = EMPATHY_GEOCLUE_HELPER (object);
115
116   switch (property_id)
117     {
118       case PROP_DISTANCE_THRESHOLD:
119         self->priv->distance_threshold = g_value_get_uint (value);
120         break;
121       default:
122         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
123         break;
124     }
125 }
126
127 static void
128 empathy_geoclue_helper_constructed (GObject *object)
129 {
130   //EmpathyGeoclueHelper *self = EMPATHY_GEOCLUE_HELPER (object);
131   void (*chain_up) (GObject *) =
132       ((GObjectClass *) empathy_geoclue_helper_parent_class)->constructed;
133
134   chain_up (object);
135 }
136
137 static void
138 empathy_geoclue_helper_dispose (GObject *object)
139 {
140   EmpathyGeoclueHelper *self = EMPATHY_GEOCLUE_HELPER (object);
141   void (*chain_up) (GObject *) =
142       ((GObjectClass *) empathy_geoclue_helper_parent_class)->dispose;
143
144   if (self->priv->started)
145     {
146       gclue_client_call_stop (self->priv->client, NULL, NULL, NULL);
147
148       self->priv->started = FALSE;
149     }
150
151   g_clear_object (&self->priv->location);
152   g_clear_object (&self->priv->client);
153
154   chain_up (object);
155 }
156
157 static void
158 empathy_geoclue_helper_finalize (GObject *object)
159 {
160   //EmpathyGeoclueHelper *self = EMPATHY_GEOCLUE_HELPER (object);
161   void (*chain_up) (GObject *) =
162       ((GObjectClass *) empathy_geoclue_helper_parent_class)->finalize;
163
164   chain_up (object);
165 }
166
167 static void
168 empathy_geoclue_helper_class_init (
169     EmpathyGeoclueHelperClass *klass)
170 {
171   GObjectClass *oclass = G_OBJECT_CLASS (klass);
172   GParamSpec *spec;
173
174   oclass->get_property = empathy_geoclue_helper_get_property;
175   oclass->set_property = empathy_geoclue_helper_set_property;
176   oclass->constructed = empathy_geoclue_helper_constructed;
177   oclass->dispose = empathy_geoclue_helper_dispose;
178   oclass->finalize = empathy_geoclue_helper_finalize;
179
180   /**
181    * EmpathyGeoclueHelper:distance-threshold:
182    *
183    * TODO
184    *
185    * Since: UNRELEASED
186    */
187   spec = g_param_spec_uint ("distance-threshold", "distance-threshold",
188       "DistanceThreshold",
189       0, G_MAXUINT32, 0,
190       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
191   g_object_class_install_property (oclass, PROP_DISTANCE_THRESHOLD, spec);
192
193   /**
194    * EmpathyGeoclueHelper:location:
195    *
196    * TODO
197    *
198    * Since: UNRELEASED
199    */
200   spec = g_param_spec_object ("location", "location", "GClueLocation",
201       GCLUE_TYPE_LOCATION,
202       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
203   g_object_class_install_property (oclass, PROP_LOCATION, spec);
204
205   /**
206    * EmpathyGeoclueHelper::location-changed:
207    * @self: a #EmpathyGeoclueHelper
208    *
209    * TODO
210    *
211    * Since: UNRELEASED
212    */
213   signals[SIG_LOCATION_CHANGED] = g_signal_new ("location-changed",
214       G_OBJECT_CLASS_TYPE (klass),
215       G_SIGNAL_RUN_LAST,
216       0, NULL, NULL, NULL,
217       G_TYPE_NONE,
218       1, GCLUE_TYPE_LOCATION);
219
220   g_type_class_add_private (klass, sizeof (EmpathyGeoclueHelperPriv));
221 }
222
223 static void
224 empathy_geoclue_helper_init (EmpathyGeoclueHelper *self)
225 {
226   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
227       EMPATHY_TYPE_GEOCLUE_HELPER, EmpathyGeoclueHelperPriv);
228 }
229
230 static void
231 location_cb (GObject *source,
232     GAsyncResult *result,
233     gpointer user_data)
234 {
235   EmpathyGeoclueHelper *self = user_data;
236   GError *error = NULL;
237
238   g_clear_object (&self->priv->location);
239
240   self->priv->location = gclue_location_proxy_new_finish (result, &error);
241   if (self->priv->location == NULL)
242     {
243       DEBUG ("Failed to create Location proxy: %s", error->message);
244       g_error_free (error);
245     }
246
247   g_signal_emit (self, signals[SIG_LOCATION_CHANGED], 0, self->priv->location);
248
249   g_object_notify (G_OBJECT (self), "location");
250 }
251
252 static void
253 location_updated_cb (GClueClient *client,
254     const gchar *old,
255     const gchar *new,
256     EmpathyGeoclueHelper *self)
257 {
258   gclue_location_proxy_new_for_bus (G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE,
259       GEOCLUE_BUS_NAME, new,
260       NULL, location_cb, self);
261 }
262
263 static void
264 client_start_cb (GObject *source,
265     GAsyncResult *result,
266     gpointer user_data)
267 {
268   GTask *task = user_data;
269   EmpathyGeoclueHelper *self = g_task_get_source_object (task);
270   GClueClient *client = GCLUE_CLIENT (source);
271   GError *error = NULL;
272
273   if (!gclue_client_call_start_finish (client, result, &error))
274     {
275       DEBUG ("Failed to start Geoclue client: %s", error->message);
276       g_error_free (error);
277       return;
278     }
279
280   self->priv->started = TRUE;
281
282   g_task_return_boolean (task, TRUE);
283   g_object_unref (task);
284 }
285
286 static void
287 client_cb (GObject *source,
288     GAsyncResult *result,
289     gpointer user_data)
290 {
291   GTask *task = user_data;
292   EmpathyGeoclueHelper *self = g_task_get_source_object (task);
293   GError *error = NULL;
294
295   self->priv->client = gclue_client_proxy_new_finish (result, &error);
296   if (!gclue_client_proxy_new_for_bus_finish (result, &error))
297     {
298       DEBUG ("Failed to create Geoclue client: %s", error->message);
299       g_task_return_error (task, error);
300       goto out;
301     }
302
303   g_signal_connect_object (self->priv->client, "location-updated",
304       G_CALLBACK (location_updated_cb), self, 0);
305
306   g_object_set (self->priv->client,
307       "distance-threshold", self->priv->distance_threshold,
308       NULL);
309
310   g_task_return_boolean (task, TRUE);
311
312 out:
313   g_object_unref (task);
314 }
315
316 static void
317 get_client_cb (GObject *source,
318     GAsyncResult *result,
319     gpointer user_data)
320 {
321   GTask *task = user_data;
322   GError *error = NULL;
323   gchar *path;
324
325   if (!gclue_manager_call_get_client_finish (GCLUE_MANAGER (source), &path,
326         result, &error))
327     {
328       DEBUG ("GetClient failed: %s", error->message);
329       g_task_return_error (task, error);
330       g_object_unref (task);
331       return;
332     }
333
334   gclue_client_proxy_new_for_bus (G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE,
335       GEOCLUE_BUS_NAME, path, NULL, client_cb, task);
336
337   g_free (path);
338 }
339
340 static void
341 manager_cb (GObject *source,
342     GAsyncResult *result,
343     gpointer user_data)
344 {
345   GTask *task = user_data;
346   GError *error = NULL;
347   GClueManager *mgr;
348
349   mgr = gclue_manager_proxy_new_for_bus_finish (result, &error);
350   if (mgr == NULL)
351     {
352       DEBUG ("Failed to create Geoclue manager: %s", error->message);
353       g_task_return_error (task, error);
354       g_object_unref (task);
355       return;
356     }
357
358   gclue_manager_call_get_client (mgr, NULL, get_client_cb, task);
359   g_object_unref (mgr);
360 }
361
362 void
363 empathy_geoclue_helper_start_async (EmpathyGeoclueHelper *self,
364     GAsyncReadyCallback callback,
365     gpointer user_data)
366 {
367   GTask *task;
368
369   task = g_task_new (self, NULL, callback, user_data);
370
371   if (self->priv->started)
372     {
373       g_task_return_boolean (task, TRUE);
374       g_object_unref (task);
375       return;
376     }
377
378   gclue_client_call_start (self->priv->client, NULL, client_start_cb, task);
379 }
380
381 gboolean
382 empathy_geoclue_helper_start_finish (EmpathyGeoclueHelper *self,
383     GAsyncResult *result,
384     GError **error)
385 {
386   g_return_val_if_fail (g_task_is_valid (result, self), FALSE);
387
388   return g_task_propagate_boolean (G_TASK (result), error);
389 }
390
391 GClueLocation *
392 empathy_geoclue_helper_get_location (EmpathyGeoclueHelper *self)
393 {
394   return self->priv->location;
395 }
396
397 static void
398 empathy_geoclue_helper_init_async (GAsyncInitable *initable,
399     gint io_priority,
400     GCancellable *cancellable,
401     GAsyncReadyCallback callback,
402     gpointer user_data)
403 {
404   GTask *task;
405
406   task = g_task_new (initable, cancellable, callback, user_data);
407
408   gclue_manager_proxy_new_for_bus (G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE,
409       GEOCLUE_BUS_NAME, "/org/freedesktop/GeoClue2/Manager",
410       NULL, manager_cb, task);
411 }
412
413 static gboolean
414 empathy_geoclue_helper_init_finish (GAsyncInitable *initable,
415     GAsyncResult *result,
416     GError **error)
417 {
418   g_return_val_if_fail (g_task_is_valid (result, initable), FALSE);
419
420   return g_task_propagate_boolean (G_TASK (result), error);
421 }
422
423 static void
424 async_initable_iface_init (GAsyncInitableIface *iface)
425 {
426   iface->init_async = empathy_geoclue_helper_init_async;
427   iface->init_finish = empathy_geoclue_helper_init_finish;
428 }
429
430 void
431 empathy_geoclue_helper_new_async (guint distance_threshold,
432     GAsyncReadyCallback callback,
433     gpointer user_data)
434 {
435   g_async_initable_new_async (EMPATHY_TYPE_GEOCLUE_HELPER,
436       G_PRIORITY_DEFAULT, NULL, callback, user_data,
437       "distance-threshold", distance_threshold,
438       NULL);
439 }
440
441 EmpathyGeoclueHelper *
442 empathy_geoclue_helper_new_finish (GAsyncResult *result,
443     GError **error)
444 {
445   GObject *object, *source_object;
446
447   source_object = g_async_result_get_source_object (result);
448
449   object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
450       result, error);
451   g_object_unref (source_object);
452
453   if (object != NULL)
454     return EMPATHY_GEOCLUE_HELPER (object);
455   else
456     return NULL;
457 }
458
459 static void
460 new_started_cb (GObject *source,
461     GAsyncResult *result,
462     gpointer user_data)
463 {
464   EmpathyGeoclueHelper *self = EMPATHY_GEOCLUE_HELPER (source);
465   GTask *new_started_task = user_data;
466   GError *error = NULL;
467
468   if (!empathy_geoclue_helper_start_finish (self, result, &error))
469     {
470       g_task_return_error (new_started_task, error);
471       g_object_unref (self);
472       goto out;
473     }
474
475   /* pass ownership of self to g_task_return_error */
476   g_task_return_pointer (new_started_task, self, g_object_unref);
477
478 out:
479   g_object_unref (new_started_task);
480 }
481
482 static void
483 new_started_init_cb (GObject *source,
484     GAsyncResult *result,
485     gpointer user_data)
486 {
487   GTask *new_started_task = user_data;
488   EmpathyGeoclueHelper *self;
489   GError *error = NULL;
490
491   self = empathy_geoclue_helper_new_finish (result, &error);
492   if (self == NULL)
493     {
494       g_task_return_error (new_started_task, error);
495       g_object_unref (new_started_task);
496       return;
497     }
498
499   /* Pass owernship of 'self' to new_started_cb */
500   empathy_geoclue_helper_start_async (self, new_started_cb, new_started_task);
501 }
502
503 void
504 empathy_geoclue_helper_new_started_async (guint distance_threshold,
505     GAsyncReadyCallback callback,
506     gpointer user_data)
507 {
508   GTask *new_started_task;
509
510   new_started_task = g_task_new (NULL, NULL, callback, user_data);
511
512   empathy_geoclue_helper_new_async (distance_threshold, new_started_init_cb,
513       new_started_task);
514 }
515
516 EmpathyGeoclueHelper *
517 empathy_geoclue_helper_new_started_finish (GAsyncResult *result,
518     GError **error)
519 {
520   g_return_val_if_fail (g_task_is_valid (result, NULL), NULL);
521
522   return g_task_propagate_pointer (G_TASK (result), error);
523 }