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