]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-tube.c
disconnect from invalidated signal before unreferencing connections
[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  * SECTION:empathy-tp-tube
73  * @title:EmpathyTpTube
74  * @short_description: A wrapper around a Telepathy tube channel
75  * @include: libempathy/empathy-tp-tube.h
76  *
77  * #EmpathyTpTube is a convenient object wrapping a Telepathy tube channel.
78  */
79
80 /**
81  * EmpathyTpTube:
82  * @parent: parent object
83  *
84  * An object wrapping a Telepathy tube channel.
85  */
86
87 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpTube)
88 typedef struct
89 {
90   TpChannel *channel;
91   TpTubeChannelState state;
92   gboolean ready;
93   GSList *ready_callbacks;
94 } EmpathyTpTubePriv;
95
96 enum
97 {
98   PROP_0,
99   PROP_CHANNEL,
100   PROP_STATE,
101 };
102
103 enum
104 {
105   DESTROY,
106   LAST_SIGNAL
107 };
108
109 static guint signals[LAST_SIGNAL];
110
111 G_DEFINE_TYPE (EmpathyTpTube, empathy_tp_tube, G_TYPE_OBJECT)
112
113 static void
114 tp_tube_state_changed_cb (TpChannel *channel,
115                           TpTubeChannelState state,
116                           gpointer user_data,
117                           GObject *tube)
118 {
119   EmpathyTpTubePriv *priv = GET_PRIV (tube);
120
121   if (!priv->ready)
122     /* We didn't get the state yet */
123     return;
124
125   DEBUG ("Tube state changed");
126
127   priv->state = state;
128   g_object_notify (tube, "state");
129 }
130
131 static void
132 tp_tube_invalidated_cb (TpChannel *channel,
133     GQuark domain,
134     gint code,
135     gchar *message,
136     EmpathyTpTube *tube)
137 {
138   DEBUG ("Channel invalidated: %s", message);
139   g_signal_emit (tube, signals[DESTROY], 0);
140 }
141
142 static void
143 tp_tube_async_cb (TpChannel *channel,
144     const GError *error,
145     gpointer user_data,
146     GObject *tube)
147 {
148   if (error)
149       DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
150 }
151
152 static void
153 tp_tube_set_property (GObject *object,
154     guint prop_id,
155     const GValue *value,
156     GParamSpec *pspec)
157 {
158   EmpathyTpTubePriv *priv = GET_PRIV (object);
159
160   switch (prop_id)
161     {
162       case PROP_CHANNEL:
163         priv->channel = g_value_dup_object (value);
164         break;
165       default:
166         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167         break;
168   }
169 }
170
171 static void
172 tp_tube_get_property (GObject *object,
173     guint prop_id,
174     GValue *value,
175     GParamSpec *pspec)
176 {
177   EmpathyTpTubePriv *priv = GET_PRIV (object);
178
179   switch (prop_id)
180     {
181       case PROP_CHANNEL:
182         g_value_set_object (value, priv->channel);
183         break;
184       case PROP_STATE:
185         g_value_set_uint (value, priv->state);
186         break;
187       default:
188         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189         break;
190   }
191 }
192
193 static void weak_object_notify (gpointer data,
194     GObject *old_object);
195
196 static ReadyCbData *
197 ready_cb_data_new (EmpathyTpTube *self,
198     EmpathyTpTubeReadyCb *callback,
199     gpointer user_data,
200     GDestroyNotify destroy,
201     GObject *weak_object)
202 {
203   ReadyCbData *d = g_slice_new0 (ReadyCbData);
204   d->callback = callback;
205   d->user_data = user_data;
206   d->destroy = destroy;
207   d->weak_object = weak_object;
208
209   if (weak_object != NULL)
210     g_object_weak_ref (weak_object, weak_object_notify, self);
211
212   return d;
213 }
214
215 static void
216 ready_cb_data_free (ReadyCbData *data,
217     EmpathyTpTube *self)
218 {
219   if (data->destroy != NULL)
220     data->destroy (data->user_data);
221
222   if (data->weak_object != NULL)
223     g_object_weak_unref (data->weak_object,
224         weak_object_notify, self);
225
226   g_slice_free (ReadyCbData, data);
227 }
228
229 static void
230 weak_object_notify (gpointer data,
231     GObject *old_object)
232 {
233   EmpathyTpTube *self = EMPATHY_TP_TUBE (data);
234   EmpathyTpTubePriv *priv = GET_PRIV (self);
235   GSList *l, *ln;
236
237   for (l = priv->ready_callbacks ; l != NULL ; l = ln )
238     {
239       ReadyCbData *d = (ReadyCbData *) l->data;
240       ln = g_slist_next (l);
241
242       if (d->weak_object == old_object)
243         {
244           ready_cb_data_free (d, self);
245           priv->ready_callbacks = g_slist_delete_link (priv->ready_callbacks,
246             l);
247         }
248     }
249 }
250
251
252 static void
253 tube_is_ready (EmpathyTpTube *self,
254     const GError *error)
255 {
256   EmpathyTpTubePriv *priv = GET_PRIV (self);
257   GSList *l;
258
259   priv->ready = TRUE;
260
261   /* tube has to stay alive while we call the callbacks */
262   g_object_ref (self);
263   for (l = priv->ready_callbacks ; l != NULL ; l = g_slist_next (l))
264     {
265       ReadyCbData *data = (ReadyCbData *) l->data;
266
267       data->callback (self, error, data->user_data, data->weak_object);
268       ready_cb_data_free (data, self);
269     }
270   g_object_unref (self);
271
272   g_slist_free (priv->ready_callbacks);
273   priv->ready_callbacks = NULL;
274 }
275
276 static void
277 got_tube_state_cb (TpProxy *proxy,
278     const GValue *out_value,
279     const GError *error,
280     gpointer user_data,
281     GObject *weak_object)
282 {
283   EmpathyTpTube *self = EMPATHY_TP_TUBE (user_data);
284   EmpathyTpTubePriv *priv = GET_PRIV (self);
285
286   if (error != NULL)
287     {
288       DEBUG ("Error getting State property: %s", error->message);
289     }
290   else
291     {
292       priv->state = g_value_get_uint (out_value);
293       g_object_notify (G_OBJECT (self), "state");
294     }
295
296   tube_is_ready (self, error);
297 }
298
299 static GObject *
300 tp_tube_constructor (GType type,
301     guint n_props,
302     GObjectConstructParam *props)
303 {
304   GObject *self;
305   EmpathyTpTubePriv *priv;
306
307   self = G_OBJECT_CLASS (empathy_tp_tube_parent_class)->constructor (
308       type, n_props, props);
309   priv = GET_PRIV (self);
310
311   g_signal_connect (priv->channel, "invalidated",
312       G_CALLBACK (tp_tube_invalidated_cb), self);
313
314   priv->ready = FALSE;
315
316   tp_cli_channel_interface_tube_connect_to_tube_channel_state_changed (
317     priv->channel, tp_tube_state_changed_cb, NULL, NULL,
318     self, NULL);
319
320   tp_cli_dbus_properties_call_get (priv->channel, -1,
321       TP_IFACE_CHANNEL_INTERFACE_TUBE, "State", got_tube_state_cb,
322       self, NULL, G_OBJECT (self));
323
324   return self;
325 }
326
327 static void
328 tp_tube_finalize (GObject *object)
329 {
330   EmpathyTpTube *self = EMPATHY_TP_TUBE (object);
331   EmpathyTpTubePriv *priv = GET_PRIV (object);
332   GSList *l;
333
334   DEBUG ("Finalizing: %p", object);
335
336   if (priv->channel)
337     {
338       g_signal_handlers_disconnect_by_func (priv->channel,
339           tp_tube_invalidated_cb, object);
340       tp_cli_channel_call_close (priv->channel, -1, tp_tube_async_cb,
341         "closing tube", NULL, NULL);
342       g_object_unref (priv->channel);
343     }
344
345   for (l = priv->ready_callbacks; l != NULL; l = g_slist_next (l))
346     {
347       ReadyCbData *d = (ReadyCbData *) l->data;
348
349       ready_cb_data_free (d, self);
350     }
351
352   g_slist_free (priv->ready_callbacks);
353   priv->ready_callbacks = NULL;
354
355   G_OBJECT_CLASS (empathy_tp_tube_parent_class)->finalize (object);
356 }
357
358 static void
359 empathy_tp_tube_class_init (EmpathyTpTubeClass *klass)
360 {
361   GObjectClass *object_class = G_OBJECT_CLASS (klass);
362
363   object_class->constructor = tp_tube_constructor;
364   object_class->finalize = tp_tube_finalize;
365   object_class->set_property = tp_tube_set_property;
366   object_class->get_property = tp_tube_get_property;
367
368   /**
369    * EmpathyTpTube:channel:
370    *
371    * The #TpChannel wrapped by the tube object.
372    */
373   g_object_class_install_property (object_class, PROP_CHANNEL,
374       g_param_spec_object ("channel", "channel", "channel", TP_TYPE_CHANNEL,
375       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
376
377   /**
378    * EmpathyTpTube:state:
379    *
380    * The state of the tube.
381    */
382   g_object_class_install_property (object_class, PROP_STATE,
383       g_param_spec_uint ("state", "state", "state",
384         0, NUM_TP_TUBE_CHANNEL_STATES, 0,
385         G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_STRINGS));
386   /**
387    * EmpathyTpTube::destroy:
388    * @self: the tube object
389    *
390    * Emitted when then tube has been invalidated.
391    */
392   signals[DESTROY] = g_signal_new ("destroy",
393       G_TYPE_FROM_CLASS (klass),
394       G_SIGNAL_RUN_LAST,
395       0, NULL, NULL,
396       g_cclosure_marshal_VOID__VOID,
397       G_TYPE_NONE, 0);
398
399   g_type_class_add_private (klass, sizeof (EmpathyTpTubePriv));
400 }
401
402 static void
403 empathy_tp_tube_init (EmpathyTpTube *tube)
404 {
405   EmpathyTpTubePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tube,
406       EMPATHY_TYPE_TP_TUBE, EmpathyTpTubePriv);
407
408   tube->priv = priv;
409 }
410
411 /**
412  * empathy_tp_tube_new:
413  * @channel: a #TpChannel
414  *
415  * Creates a new #EmpathyTpTube.
416  *
417  * Return value: a new #EmpathyTpTube
418  */
419 EmpathyTpTube *
420 empathy_tp_tube_new (TpChannel *channel)
421 {
422   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
423
424   return g_object_new (EMPATHY_TYPE_TP_TUBE, "channel", channel,  NULL);
425 }
426
427 /**
428  * empathy_tp_tube_new_stream_tube:
429  * @contact: the #EmpathyContact to which the tube is offered
430  * @type: the type of the listening address of the local service. Either
431  * %TP_SOCKET_ADDRESS_TYPE_IPV4 or %TP_SOCKET_ADDRESS_TYPE_IPV6.
432  * @hostname: the address of the local service
433  * @port: the port of the local service
434  * @service: the service name of the tube
435  * @parameters: the parameters of the tube
436  *
437  * Creates and offers a new #EmpathyTpTube of ChannelType StreamTube.
438  *
439  * Return value: a new #EmpathyTpTube
440  */
441 EmpathyTpTube *
442 empathy_tp_tube_new_stream_tube (EmpathyContact *contact,
443     TpSocketAddressType type,
444     const gchar *hostname,
445     guint port,
446     const gchar *service,
447     GHashTable *parameters)
448 {
449   TpConnection *connection;
450   TpChannel *channel;
451   gchar *object_path;
452   GHashTable *params;
453   GValue *address;
454   GValue *control_param;
455   EmpathyTpTube *tube = NULL;
456   GError *error = NULL;
457   GHashTable *request;
458   GHashTable *channel_properties;
459   GValue *value;
460
461   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
462   g_return_val_if_fail (hostname != NULL, NULL);
463   g_return_val_if_fail (service != NULL, NULL);
464
465   connection = empathy_contact_get_connection (contact);
466
467   request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
468       (GDestroyNotify) tp_g_value_slice_free);
469
470   /* org.freedesktop.Telepathy.Channel.ChannelType */
471   value = tp_g_value_slice_new (G_TYPE_STRING);
472   g_value_set_string (value, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE);
473   g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
474
475   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
476   value = tp_g_value_slice_new (G_TYPE_UINT);
477   g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
478   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
479
480   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
481   value = tp_g_value_slice_new (G_TYPE_UINT);
482   g_value_set_uint (value, empathy_contact_get_handle (contact));
483   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
484
485   /* org.freedesktop.Telepathy.Channel.Type.StreamTube.Service */
486   value = tp_g_value_slice_new (G_TYPE_STRING);
487   g_value_set_string (value, service);
488   g_hash_table_insert (request,
489     TP_IFACE_CHANNEL_TYPE_STREAM_TUBE  ".Service", value);
490
491   if (!tp_cli_connection_interface_requests_run_create_channel (connection, -1,
492     request, &object_path, &channel_properties, &error, NULL))
493     {
494       DEBUG ("Error requesting channel: %s", error->message);
495       g_clear_error (&error);
496       g_object_unref (connection);
497       return NULL;
498     }
499
500   DEBUG ("Offering a new stream tube");
501
502   channel = tp_channel_new_from_properties (connection, object_path,
503       channel_properties, NULL);
504
505   tp_channel_run_until_ready (channel, NULL, NULL);
506
507   #define ADDRESS_TYPE dbus_g_type_get_struct ("GValueArray",\
508       G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID)
509   params = g_hash_table_new (g_str_hash, g_str_equal);
510   address = tp_g_value_slice_new (ADDRESS_TYPE);
511   g_value_take_boxed (address, dbus_g_type_specialized_construct (ADDRESS_TYPE));
512   dbus_g_type_struct_set (address, 0, hostname, 1, port, G_MAXUINT);
513   control_param = tp_g_value_slice_new (G_TYPE_STRING);
514
515   if (parameters == NULL)
516     /* Pass an empty dict as parameters */
517     parameters = g_hash_table_new (g_str_hash, g_str_equal);
518   else
519     g_hash_table_ref (parameters);
520
521   if (!tp_cli_channel_type_stream_tube_run_offer (channel, -1, type, address,
522         TP_SOCKET_ACCESS_CONTROL_LOCALHOST, parameters,
523         &error, NULL))
524     {
525       DEBUG ("Couldn't offer tube: %s", error->message);
526       g_clear_error (&error);
527       goto OUT;
528     }
529
530   DEBUG ("Stream tube offered");
531
532   tube = empathy_tp_tube_new (channel);
533
534 OUT:
535   g_object_unref (channel);
536   g_free (object_path);
537   g_hash_table_destroy (request);
538   g_hash_table_destroy (channel_properties);
539   tp_g_value_slice_free (address);
540   tp_g_value_slice_free (control_param);
541   g_object_unref (connection);
542   g_hash_table_unref (parameters);
543
544   return tube;
545 }
546
547 static void
548 tp_tube_accept_stream_cb (TpChannel *channel,
549     const GValue *address,
550     const GError *error,
551     gpointer user_data,
552     GObject *weak_object)
553 {
554   EmpathyTpTube *tube = EMPATHY_TP_TUBE (weak_object);
555   EmpathyTpTubeAcceptData *data = (EmpathyTpTubeAcceptData *) user_data;
556   EmpathyTpTubeAddress eaddress;
557
558   eaddress.type = data->type;
559
560   if (error)
561     {
562       DEBUG ("Error accepting tube: %s", error->message);
563       data->callback (tube, NULL, error, data->user_data);
564       return;
565     }
566
567   switch (eaddress.type)
568     {
569       case TP_SOCKET_ADDRESS_TYPE_UNIX:
570       case TP_SOCKET_ADDRESS_TYPE_ABSTRACT_UNIX:
571         eaddress.a.socket.path = g_value_get_boxed (address);
572         break;
573      case TP_SOCKET_ADDRESS_TYPE_IPV4:
574      case TP_SOCKET_ADDRESS_TYPE_IPV6:
575         dbus_g_type_struct_get (address,
576           0, &eaddress.a.inet.hostname,
577           1, &eaddress.a.inet.port, G_MAXUINT);
578         break;
579     }
580
581    data->callback (tube, &eaddress, NULL, data->user_data);
582 }
583
584 /**
585  * empathy_tp_tube_accept_stream_tube:
586  * @tube: an #EmpathyTpTube
587  * @type: the type of address the connection manager should listen on
588  * @callback: called when the tube has been accepted
589  * @user_data: arbitrary user-supplied data passed to the callback
590  *
591  * Accepts @tube of ChannelType StreamTube and call @callback once it's done.
592  */
593 void
594 empathy_tp_tube_accept_stream_tube (EmpathyTpTube *tube,
595   TpSocketAddressType type,
596   EmpathyTpTubeAcceptStreamTubeCb *callback,
597   gpointer user_data)
598 {
599   EmpathyTpTubePriv *priv = GET_PRIV (tube);
600   GValue *control_param;
601   EmpathyTpTubeAcceptData *data;
602
603   g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
604
605   DEBUG ("Accepting stream tube");
606   /* FIXME allow other acls */
607   control_param = tp_g_value_slice_new (G_TYPE_STRING);
608
609   data = new_empathy_tp_tube_accept_data (type, callback, user_data);
610
611   tp_cli_channel_type_stream_tube_call_accept (
612      priv->channel, -1, type, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
613      control_param, tp_tube_accept_stream_cb, data,
614      free_empathy_tp_tube_accept_data, G_OBJECT (tube));
615
616   tp_g_value_slice_free (control_param);
617 }
618
619 /**
620  * EmpathyTpTubeReadyCb:
621  * @tube: an #EmpathyTpTube
622  * @error: %NULL on success, or the reason why the tube can't be ready
623  * @user_data: the @user_data passed to empathy_tp_tube_call_when_ready()
624  * @weak_object: the @weak_object passed to
625  *               empathy_tp_tube_call_when_ready()
626  *
627  * Called as the result of empathy_tp_tube_call_when_ready(). If the
628  * tube's properties could be retrieved,
629  * @error is %NULL and @tube is considered to be ready. Otherwise, @error is
630  * non-%NULL and @tube is not ready.
631  */
632
633 /**
634  * empathy_tp_tube_call_when_ready:
635  * @tube: an #EmpathyTpTube
636  * @callback: called when the tube becomes ready
637  * @user_data: arbitrary user-supplied data passed to the callback
638  * @destroy: called to destroy @user_data
639  * @weak_object: object to reference weakly; if it is destroyed, @callback
640  *               will not be called, but @destroy will still be called
641  *
642  * If @tube is ready for use, call @callback immediately, then return.
643  * Otherwise, arrange for @callback to be called when @tube becomes
644  * ready for use.
645  */
646 void
647 empathy_tp_tube_call_when_ready (EmpathyTpTube *self,
648     EmpathyTpTubeReadyCb *callback,
649     gpointer user_data,
650     GDestroyNotify destroy,
651     GObject *weak_object)
652 {
653   EmpathyTpTubePriv *priv = GET_PRIV (self);
654
655   g_return_if_fail (self != NULL);
656   g_return_if_fail (callback != NULL);
657
658   if (priv->ready)
659     {
660       callback (self, NULL, user_data, weak_object);
661       if (destroy != NULL)
662         destroy (user_data);
663     }
664   else
665     {
666       priv->ready_callbacks = g_slist_prepend (priv->ready_callbacks,
667           ready_cb_data_new (self, callback, user_data, destroy, weak_object));
668     }
669 }