]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-tube.c
libempathy/empathy-tp-tube.[ch]: port to tp coding style
[empathy.git] / libempathy / empathy-tp-tube.c
1 /*
2  * Copyright (C) 2008 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
19  *          Elliot Fairweather <elliot.fairweather@collabora.co.uk>
20  */
21
22 #include <config.h>
23
24 #include <telepathy-glib/connection.h>
25 #include <telepathy-glib/proxy.h>
26 #include <telepathy-glib/util.h>
27 #include <extensions/extensions.h>
28
29 #include "empathy-enum-types.h"
30 #include "empathy-tp-tube.h"
31 #include "empathy-utils.h"
32
33 #define DEBUG_FLAG EMPATHY_DEBUG_TP
34 #include "empathy-debug.h"
35
36 typedef struct {
37   TpSocketAddressType type;
38   EmpathyTpTubeAcceptStreamTubeCb *callback;
39   gpointer user_data;
40 } EmpathyTpTubeAcceptData;
41
42 static EmpathyTpTubeAcceptData *
43 new_empathy_tp_tube_accept_data (TpSocketAddressType type,
44   EmpathyTpTubeAcceptStreamTubeCb *callback,
45   gpointer user_data)
46 {
47   EmpathyTpTubeAcceptData *r;
48
49   r = g_slice_new0 (EmpathyTpTubeAcceptData);
50   r->type = type;
51   r->callback = callback;
52   r->user_data = user_data;
53
54   return r;
55 }
56
57 static void
58 free_empathy_tp_tube_accept_data (gpointer data)
59 {
60   g_slice_free (EmpathyTpTubeAcceptData, data);
61 }
62
63
64 typedef struct {
65     EmpathyTpTubeReadyCb *callback;
66     gpointer user_data;
67     GDestroyNotify destroy;
68     GObject *weak_object;
69 } ReadyCbData;
70
71
72 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpTube)
73 typedef struct
74 {
75   TpChannel *channel;
76   EmpTubeChannelState state;
77   gboolean ready;
78   GSList *ready_callbacks;
79 } EmpathyTpTubePriv;
80
81 enum
82 {
83   PROP_0,
84   PROP_CHANNEL,
85   PROP_STATE,
86 };
87
88 enum
89 {
90   DESTROY,
91   LAST_SIGNAL
92 };
93
94 static guint signals[LAST_SIGNAL];
95
96 G_DEFINE_TYPE (EmpathyTpTube, empathy_tp_tube, G_TYPE_OBJECT)
97
98 static void
99 tp_tube_state_changed_cb (TpProxy *proxy,
100                           EmpTubeChannelState state,
101                           gpointer user_data,
102                           GObject *tube)
103 {
104   EmpathyTpTubePriv *priv = GET_PRIV (tube);
105
106   if (!priv->ready)
107     /* We didn't get the state yet */
108     return;
109
110   DEBUG ("Tube state changed");
111
112   priv->state = state;
113   g_object_notify (tube, "state");
114 }
115
116 static void
117 tp_tube_invalidated_cb (TpChannel *channel,
118     GQuark domain,
119     gint code,
120     gchar *message,
121     EmpathyTpTube *tube)
122 {
123   DEBUG ("Channel invalidated: %s", message);
124   g_signal_emit (tube, signals[DESTROY], 0);
125 }
126
127 static void
128 tp_tube_async_cb (TpChannel *channel,
129     const GError *error,
130     gpointer user_data,
131     GObject *tube)
132 {
133   if (error)
134       DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
135 }
136
137 static void
138 tp_tube_set_property (GObject *object,
139     guint prop_id,
140     const GValue *value,
141     GParamSpec *pspec)
142 {
143   EmpathyTpTubePriv *priv = GET_PRIV (object);
144
145   switch (prop_id)
146     {
147       case PROP_CHANNEL:
148         priv->channel = g_value_dup_object (value);
149         break;
150       default:
151         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
152         break;
153   }
154 }
155
156 static void
157 tp_tube_get_property (GObject *object,
158     guint prop_id,
159     GValue *value,
160     GParamSpec *pspec)
161 {
162   EmpathyTpTubePriv *priv = GET_PRIV (object);
163
164   switch (prop_id)
165     {
166       case PROP_CHANNEL:
167         g_value_set_object (value, priv->channel);
168         break;
169       case PROP_STATE:
170         g_value_set_uint (value, priv->state);
171         break;
172       default:
173         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174         break;
175   }
176 }
177
178 static void weak_object_notify (gpointer data,
179     GObject *old_object);
180
181 static ReadyCbData *
182 ready_cb_data_new (EmpathyTpTube *self,
183     EmpathyTpTubeReadyCb *callback,
184     gpointer user_data,
185     GDestroyNotify destroy,
186     GObject *weak_object)
187 {
188   ReadyCbData *d = g_slice_new0 (ReadyCbData);
189   d->callback = callback;
190   d->user_data = user_data;
191   d->destroy = destroy;
192   d->weak_object = weak_object;
193
194   if (weak_object != NULL)
195     g_object_weak_ref (weak_object, weak_object_notify, self);
196
197   return d;
198 }
199
200 static void
201 ready_cb_data_free (ReadyCbData *data,
202     EmpathyTpTube *self)
203 {
204   if (data->destroy != NULL)
205     data->destroy (data->user_data);
206
207   if (data->weak_object != NULL)
208     g_object_weak_unref (data->weak_object,
209         weak_object_notify, self);
210
211   g_slice_free (ReadyCbData, data);
212 }
213
214 static void
215 weak_object_notify (gpointer data,
216     GObject *old_object)
217 {
218   EmpathyTpTube *self = EMPATHY_TP_TUBE (data);
219   EmpathyTpTubePriv *priv = GET_PRIV (self);
220   GSList *l, *ln;
221
222   for (l = priv->ready_callbacks ; l != NULL ; l = ln )
223     {
224       ReadyCbData *d = (ReadyCbData *) l->data;
225       ln = g_slist_next (l);
226
227       if (d->weak_object == old_object)
228         {
229           ready_cb_data_free (d, self);
230           priv->ready_callbacks = g_slist_delete_link (priv->ready_callbacks,
231             l);
232         }
233     }
234 }
235
236
237 static void
238 tube_is_ready (EmpathyTpTube *self,
239     const GError *error)
240 {
241   EmpathyTpTubePriv *priv = GET_PRIV (self);
242   GSList *l;
243
244   priv->ready = TRUE;
245
246   for (l = priv->ready_callbacks ; l != NULL ; l = g_slist_next (l))
247     {
248       ReadyCbData *data = (ReadyCbData *) l->data;
249
250       data->callback (self, error, data->user_data, data->weak_object);
251       ready_cb_data_free (data, self);
252     }
253
254   g_slist_free (priv->ready_callbacks);
255   priv->ready_callbacks = NULL;
256 }
257
258 static void
259 got_tube_state_cb (TpProxy *proxy,
260     const GValue *out_value,
261     const GError *error,
262     gpointer user_data,
263     GObject *weak_object)
264 {
265   EmpathyTpTube *self = EMPATHY_TP_TUBE (user_data);
266   EmpathyTpTubePriv *priv = GET_PRIV (self);
267
268   if (error != NULL)
269     {
270       DEBUG ("Error getting State property: %s", error->message);
271     }
272   else
273     {
274       priv->state = g_value_get_uint (out_value);
275       g_object_notify (G_OBJECT (self), "state");
276     }
277
278   tube_is_ready (self, error);
279 }
280
281 static GObject *
282 tp_tube_constructor (GType type,
283     guint n_props,
284     GObjectConstructParam *props)
285 {
286   GObject *self;
287   EmpathyTpTubePriv *priv;
288
289   self = G_OBJECT_CLASS (empathy_tp_tube_parent_class)->constructor (
290       type, n_props, props);
291   priv = GET_PRIV (self);
292
293   g_signal_connect (priv->channel, "invalidated",
294       G_CALLBACK (tp_tube_invalidated_cb), self);
295
296   priv->ready = FALSE;
297
298   emp_cli_channel_interface_tube_connect_to_tube_channel_state_changed (
299     TP_PROXY (priv->channel), tp_tube_state_changed_cb, NULL, NULL,
300     self, NULL);
301
302   tp_cli_dbus_properties_call_get (priv->channel, -1,
303       EMP_IFACE_CHANNEL_INTERFACE_TUBE, "State", got_tube_state_cb,
304       self, NULL, G_OBJECT (self));
305
306   return self;
307 }
308
309 static void
310 tp_tube_finalize (GObject *object)
311 {
312   EmpathyTpTube *self = EMPATHY_TP_TUBE (object);
313   EmpathyTpTubePriv *priv = GET_PRIV (object);
314   GSList *l;
315
316   DEBUG ("Finalizing: %p", object);
317
318   if (priv->channel)
319     {
320       g_signal_handlers_disconnect_by_func (priv->channel,
321           tp_tube_invalidated_cb, object);
322       tp_cli_channel_call_close (priv->channel, -1, tp_tube_async_cb,
323         "closing tube", NULL, NULL);
324       g_object_unref (priv->channel);
325     }
326
327   for (l = priv->ready_callbacks; l != NULL; l = g_slist_next (l))
328     {
329       ReadyCbData *d = (ReadyCbData *) l->data;
330
331       ready_cb_data_free (d, self);
332     }
333
334   g_slist_free (priv->ready_callbacks);
335   priv->ready_callbacks = NULL;
336
337   G_OBJECT_CLASS (empathy_tp_tube_parent_class)->finalize (object);
338 }
339
340 static void
341 empathy_tp_tube_class_init (EmpathyTpTubeClass *klass)
342 {
343   GObjectClass *object_class = G_OBJECT_CLASS (klass);
344
345   object_class->constructor = tp_tube_constructor;
346   object_class->finalize = tp_tube_finalize;
347   object_class->set_property = tp_tube_set_property;
348   object_class->get_property = tp_tube_get_property;
349
350   g_object_class_install_property (object_class, PROP_CHANNEL,
351       g_param_spec_object ("channel", "channel", "channel", TP_TYPE_CHANNEL,
352       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
353
354   g_object_class_install_property (object_class, PROP_STATE,
355       g_param_spec_uint ("state", "state", "state",
356         0, NUM_EMP_TUBE_CHANNEL_STATES, 0,
357         G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_STRINGS));
358
359   signals[DESTROY] = g_signal_new ("destroy",
360       G_TYPE_FROM_CLASS (klass),
361       G_SIGNAL_RUN_LAST,
362       0, NULL, NULL,
363       g_cclosure_marshal_VOID__VOID,
364       G_TYPE_NONE, 0);
365
366   g_type_class_add_private (klass, sizeof (EmpathyTpTubePriv));
367 }
368
369 static void
370 empathy_tp_tube_init (EmpathyTpTube *tube)
371 {
372   EmpathyTpTubePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tube,
373       EMPATHY_TYPE_TP_TUBE, EmpathyTpTubePriv);
374
375   tube->priv = priv;
376 }
377
378 EmpathyTpTube *
379 empathy_tp_tube_new (TpChannel *channel)
380 {
381   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
382
383   return g_object_new (EMPATHY_TYPE_TP_TUBE, "channel", channel,  NULL);
384 }
385
386 EmpathyTpTube *
387 empathy_tp_tube_new_stream_tube (EmpathyContact *contact,
388     TpSocketAddressType type,
389     const gchar *hostname,
390     guint port,
391     const gchar *service,
392     GHashTable *parameters)
393 {
394   TpConnection *connection;
395   TpChannel *channel;
396   gchar *object_path;
397   GHashTable *params;
398   GValue *address;
399   GValue *control_param;
400   EmpathyTpTube *tube = NULL;
401   GError *error = NULL;
402   GHashTable *request;
403   GHashTable *channel_properties;
404   GValue *value;
405
406   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
407   g_return_val_if_fail (hostname != NULL, NULL);
408   g_return_val_if_fail (service != NULL, NULL);
409
410   connection = empathy_contact_get_connection (contact);
411
412   request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
413       (GDestroyNotify) tp_g_value_slice_free);
414
415   /* org.freedesktop.Telepathy.Channel.ChannelType */
416   value = tp_g_value_slice_new (G_TYPE_STRING);
417   g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE);
418   g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
419
420   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
421   value = tp_g_value_slice_new (G_TYPE_UINT);
422   g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
423   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
424
425   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
426   value = tp_g_value_slice_new (G_TYPE_UINT);
427   g_value_set_uint (value, empathy_contact_get_handle (contact));
428   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
429
430   /* org.freedesktop.Telepathy.Channel.Type.StreamTube.Service */
431   value = tp_g_value_slice_new (G_TYPE_STRING);
432   g_value_set_string (value, service);
433   g_hash_table_insert (request,
434     EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE  ".Service", value);
435
436   if (!tp_cli_connection_interface_requests_run_create_channel (connection, -1,
437     request, &object_path, &channel_properties, &error, NULL))
438     {
439       DEBUG ("Error requesting channel: %s", error->message);
440       g_clear_error (&error);
441       g_object_unref (connection);
442       return NULL;
443     }
444
445   DEBUG ("Offering a new stream tube");
446
447   channel = tp_channel_new_from_properties (connection, object_path,
448       channel_properties, NULL);
449
450   tp_channel_run_until_ready (channel, NULL, NULL);
451
452   #define ADDRESS_TYPE dbus_g_type_get_struct ("GValueArray",\
453       G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID)
454   params = g_hash_table_new (g_str_hash, g_str_equal);
455   address = tp_g_value_slice_new (ADDRESS_TYPE);
456   g_value_take_boxed (address, dbus_g_type_specialized_construct (ADDRESS_TYPE));
457   dbus_g_type_struct_set (address, 0, hostname, 1, port, G_MAXUINT);
458   control_param = tp_g_value_slice_new (G_TYPE_STRING);
459
460   if (parameters == NULL)
461     /* Pass an empty dict as parameters */
462     parameters = g_hash_table_new (g_str_hash, g_str_equal);
463   else
464     g_hash_table_ref (parameters);
465
466   if (!emp_cli_channel_type_stream_tube_run_offer_stream_tube (
467         TP_PROXY(channel), -1, type, address,
468         TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param, parameters,
469         &error, NULL))
470     {
471       DEBUG ("Couldn't offer tube: %s", error->message);
472       g_clear_error (&error);
473       goto OUT;
474     }
475
476   DEBUG ("Stream tube offered");
477
478   tube = empathy_tp_tube_new (channel);
479
480 OUT:
481   g_object_unref (channel);
482   g_free (object_path);
483   g_hash_table_destroy (request);
484   g_hash_table_destroy (channel_properties);
485   tp_g_value_slice_free (address);
486   tp_g_value_slice_free (control_param);
487   g_object_unref (connection);
488   g_hash_table_unref (parameters);
489
490   return tube;
491 }
492
493 static void
494 tp_tube_accept_stream_cb (TpProxy *proxy,
495     const GValue *address,
496     const GError *error,
497     gpointer user_data,
498     GObject *weak_object)
499 {
500   EmpathyTpTube *tube = EMPATHY_TP_TUBE (weak_object);
501   EmpathyTpTubeAcceptData *data = (EmpathyTpTubeAcceptData *)user_data;
502   EmpathyTpTubeAddress eaddress;
503
504   eaddress.type = data->type;
505
506   if (error)
507     {
508       DEBUG ("Error accepting tube: %s", error->message);
509       data->callback (tube, NULL, error, data->user_data);
510       return;
511     }
512
513   switch (eaddress.type)
514     {
515       case TP_SOCKET_ADDRESS_TYPE_UNIX:
516       case TP_SOCKET_ADDRESS_TYPE_ABSTRACT_UNIX:
517         eaddress.a.socket.path = g_value_get_boxed (address);
518         break;
519      case TP_SOCKET_ADDRESS_TYPE_IPV4:
520      case TP_SOCKET_ADDRESS_TYPE_IPV6:
521         dbus_g_type_struct_get (address,
522           0, &eaddress.a.inet.hostname,
523           1, &eaddress.a.inet.port, G_MAXUINT);
524         break;
525     }
526
527    data->callback (tube, &eaddress, NULL, data->user_data);
528 }
529
530 void
531 empathy_tp_tube_accept_stream_tube (EmpathyTpTube *tube,
532   TpSocketAddressType type,
533   EmpathyTpTubeAcceptStreamTubeCb *callback,
534   gpointer user_data)
535 {
536   EmpathyTpTubePriv *priv = GET_PRIV (tube);
537   GValue *control_param;
538   EmpathyTpTubeAcceptData *data;
539
540   g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
541
542   DEBUG ("Accepting stream tube");
543   /* FIXME allow other acls */
544   control_param = tp_g_value_slice_new (G_TYPE_STRING);
545
546   data = new_empathy_tp_tube_accept_data (type, callback, user_data);
547
548   emp_cli_channel_type_stream_tube_call_accept_stream_tube (
549      TP_PROXY (priv->channel), -1, type, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
550      control_param, tp_tube_accept_stream_cb, data,
551      free_empathy_tp_tube_accept_data, G_OBJECT (tube));
552
553   tp_g_value_slice_free (control_param);
554 }
555
556 /**
557  * EmpathyTpTubeReadyCb:
558  * @tube: an #EmpathyTpTube
559  * @error: %NULL on success, or the reason why the tube can't be ready
560  * @user_data: the @user_data passed to empathy_tp_tube_call_when_ready()
561  * @weak_object: the @weak_object passed to
562  *               empathy_tp_tube_call_when_ready()
563  *
564  * Called as the result of empathy_tp_tube_call_when_ready(). If the
565  * tube's properties could be retrieved,
566  * @error is %NULL and @tube is considered to be ready. Otherwise, @error is
567  * non-%NULL and @tube is not ready.
568  */
569
570 /**
571  * empathy_tp_tube_call_when_ready:
572  * @tube: an #EmpathyTpTube
573  * @callback: called when the tube becomes ready
574  * @user_data: arbitrary user-supplied data passed to the callback
575  * @destroy: called to destroy @user_data
576  * @weak_object: object to reference weakly; if it is destroyed, @callback
577  *               will not be called, but @destroy will still be called
578  *
579  * If @tube is ready for use, call @callback immediately, then return.
580  * Otherwise, arrange for @callback to be called when @tube becomes
581  * ready for use.
582  */
583 void
584 empathy_tp_tube_call_when_ready (EmpathyTpTube *self,
585     EmpathyTpTubeReadyCb *callback,
586     gpointer user_data,
587     GDestroyNotify destroy,
588     GObject *weak_object)
589 {
590   EmpathyTpTubePriv *priv = GET_PRIV (self);
591
592   g_return_if_fail (self != NULL);
593   g_return_if_fail (callback != NULL);
594
595   if (priv->ready)
596     {
597       callback (self, NULL, user_data, weak_object);
598       if (destroy != NULL)
599         destroy (user_data);
600     }
601   else
602     {
603       priv->ready_callbacks = g_slist_prepend (priv->ready_callbacks,
604           ready_cb_data_new (self, callback, user_data, destroy, weak_object));
605     }
606 }