2 * Copyright (C) 2008 Collabora Ltd.
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.
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.
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
18 * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
19 * Elliot Fairweather <elliot.fairweather@collabora.co.uk>
24 #include <telepathy-glib/connection.h>
25 #include <telepathy-glib/proxy.h>
26 #include <telepathy-glib/util.h>
27 #include <extensions/extensions.h>
29 #include "empathy-enum-types.h"
30 #include "empathy-tp-tube.h"
31 #include "empathy-utils.h"
33 #define DEBUG_FLAG EMPATHY_DEBUG_TP
34 #include "empathy-debug.h"
37 TpSocketAddressType type;
38 EmpathyTpTubeAcceptStreamTubeCb *callback;
40 } EmpathyTpTubeAcceptData;
42 static EmpathyTpTubeAcceptData *
43 new_empathy_tp_tube_accept_data (TpSocketAddressType type,
44 EmpathyTpTubeAcceptStreamTubeCb *callback,
47 EmpathyTpTubeAcceptData *r;
49 r = g_slice_new0 (EmpathyTpTubeAcceptData);
51 r->callback = callback;
52 r->user_data = user_data;
58 free_empathy_tp_tube_accept_data (gpointer data)
60 g_slice_free (EmpathyTpTubeAcceptData, data);
65 EmpathyTpTubeReadyCb *callback;
67 GDestroyNotify destroy;
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
77 * #EmpathyTpTube is a convenient object wrapping a Telepathy tube channel.
80 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpTube)
84 EmpTubeChannelState state;
86 GSList *ready_callbacks;
102 static guint signals[LAST_SIGNAL];
104 G_DEFINE_TYPE (EmpathyTpTube, empathy_tp_tube, G_TYPE_OBJECT)
107 tp_tube_state_changed_cb (TpProxy *proxy,
108 EmpTubeChannelState state,
112 EmpathyTpTubePriv *priv = GET_PRIV (tube);
115 /* We didn't get the state yet */
118 DEBUG ("Tube state changed");
121 g_object_notify (tube, "state");
125 tp_tube_invalidated_cb (TpChannel *channel,
131 DEBUG ("Channel invalidated: %s", message);
132 g_signal_emit (tube, signals[DESTROY], 0);
136 tp_tube_async_cb (TpChannel *channel,
142 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
146 tp_tube_set_property (GObject *object,
151 EmpathyTpTubePriv *priv = GET_PRIV (object);
156 priv->channel = g_value_dup_object (value);
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 tp_tube_get_property (GObject *object,
170 EmpathyTpTubePriv *priv = GET_PRIV (object);
175 g_value_set_object (value, priv->channel);
178 g_value_set_uint (value, priv->state);
181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186 static void weak_object_notify (gpointer data,
187 GObject *old_object);
190 ready_cb_data_new (EmpathyTpTube *self,
191 EmpathyTpTubeReadyCb *callback,
193 GDestroyNotify destroy,
194 GObject *weak_object)
196 ReadyCbData *d = g_slice_new0 (ReadyCbData);
197 d->callback = callback;
198 d->user_data = user_data;
199 d->destroy = destroy;
200 d->weak_object = weak_object;
202 if (weak_object != NULL)
203 g_object_weak_ref (weak_object, weak_object_notify, self);
209 ready_cb_data_free (ReadyCbData *data,
212 if (data->destroy != NULL)
213 data->destroy (data->user_data);
215 if (data->weak_object != NULL)
216 g_object_weak_unref (data->weak_object,
217 weak_object_notify, self);
219 g_slice_free (ReadyCbData, data);
223 weak_object_notify (gpointer data,
226 EmpathyTpTube *self = EMPATHY_TP_TUBE (data);
227 EmpathyTpTubePriv *priv = GET_PRIV (self);
230 for (l = priv->ready_callbacks ; l != NULL ; l = ln )
232 ReadyCbData *d = (ReadyCbData *) l->data;
233 ln = g_slist_next (l);
235 if (d->weak_object == old_object)
237 ready_cb_data_free (d, self);
238 priv->ready_callbacks = g_slist_delete_link (priv->ready_callbacks,
246 tube_is_ready (EmpathyTpTube *self,
249 EmpathyTpTubePriv *priv = GET_PRIV (self);
254 for (l = priv->ready_callbacks ; l != NULL ; l = g_slist_next (l))
256 ReadyCbData *data = (ReadyCbData *) l->data;
258 data->callback (self, error, data->user_data, data->weak_object);
259 ready_cb_data_free (data, self);
262 g_slist_free (priv->ready_callbacks);
263 priv->ready_callbacks = NULL;
267 got_tube_state_cb (TpProxy *proxy,
268 const GValue *out_value,
271 GObject *weak_object)
273 EmpathyTpTube *self = EMPATHY_TP_TUBE (user_data);
274 EmpathyTpTubePriv *priv = GET_PRIV (self);
278 DEBUG ("Error getting State property: %s", error->message);
282 priv->state = g_value_get_uint (out_value);
283 g_object_notify (G_OBJECT (self), "state");
286 tube_is_ready (self, error);
290 tp_tube_constructor (GType type,
292 GObjectConstructParam *props)
295 EmpathyTpTubePriv *priv;
297 self = G_OBJECT_CLASS (empathy_tp_tube_parent_class)->constructor (
298 type, n_props, props);
299 priv = GET_PRIV (self);
301 g_signal_connect (priv->channel, "invalidated",
302 G_CALLBACK (tp_tube_invalidated_cb), self);
306 emp_cli_channel_interface_tube_connect_to_tube_channel_state_changed (
307 TP_PROXY (priv->channel), tp_tube_state_changed_cb, NULL, NULL,
310 tp_cli_dbus_properties_call_get (priv->channel, -1,
311 EMP_IFACE_CHANNEL_INTERFACE_TUBE, "State", got_tube_state_cb,
312 self, NULL, G_OBJECT (self));
318 tp_tube_finalize (GObject *object)
320 EmpathyTpTube *self = EMPATHY_TP_TUBE (object);
321 EmpathyTpTubePriv *priv = GET_PRIV (object);
324 DEBUG ("Finalizing: %p", object);
328 g_signal_handlers_disconnect_by_func (priv->channel,
329 tp_tube_invalidated_cb, object);
330 tp_cli_channel_call_close (priv->channel, -1, tp_tube_async_cb,
331 "closing tube", NULL, NULL);
332 g_object_unref (priv->channel);
335 for (l = priv->ready_callbacks; l != NULL; l = g_slist_next (l))
337 ReadyCbData *d = (ReadyCbData *) l->data;
339 ready_cb_data_free (d, self);
342 g_slist_free (priv->ready_callbacks);
343 priv->ready_callbacks = NULL;
345 G_OBJECT_CLASS (empathy_tp_tube_parent_class)->finalize (object);
349 empathy_tp_tube_class_init (EmpathyTpTubeClass *klass)
351 GObjectClass *object_class = G_OBJECT_CLASS (klass);
353 object_class->constructor = tp_tube_constructor;
354 object_class->finalize = tp_tube_finalize;
355 object_class->set_property = tp_tube_set_property;
356 object_class->get_property = tp_tube_get_property;
359 * EmpathyTpTube:channel:
361 * The #TpChannel wrapped by the tube object.
363 g_object_class_install_property (object_class, PROP_CHANNEL,
364 g_param_spec_object ("channel", "channel", "channel", TP_TYPE_CHANNEL,
365 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
368 * EmpathyTpTube:state:
370 * The state of the tube.
372 g_object_class_install_property (object_class, PROP_STATE,
373 g_param_spec_uint ("state", "state", "state",
374 0, NUM_EMP_TUBE_CHANNEL_STATES, 0,
375 G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_STRINGS));
377 * EmpathyTpTube::destroy:
378 * @self: the tube object
380 * Emitted when then tube has been invalidated.
382 signals[DESTROY] = g_signal_new ("destroy",
383 G_TYPE_FROM_CLASS (klass),
386 g_cclosure_marshal_VOID__VOID,
389 g_type_class_add_private (klass, sizeof (EmpathyTpTubePriv));
393 empathy_tp_tube_init (EmpathyTpTube *tube)
395 EmpathyTpTubePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tube,
396 EMPATHY_TYPE_TP_TUBE, EmpathyTpTubePriv);
402 * empathy_tp_tube_new:
403 * @channel: a #TpChannel
405 * Creates a new #EmpathyTpTube.
407 * Return value: a new #EmpathyTpTube
410 empathy_tp_tube_new (TpChannel *channel)
412 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
414 return g_object_new (EMPATHY_TYPE_TP_TUBE, "channel", channel, NULL);
418 * empathy_tp_tube_new_stream_tube:
419 * @contact: the #EmpathyContact to which the tube is offered
420 * @type: the type of the listening address of the local service. Either
421 * TP_SOCKET_ADDRESS_TYPE_IPV4 or TP_SOCKET_ADDRESS_TYPE_IPV6.
422 * @hostname: the address of the local service
423 * @port: the port of the local service
424 * @service: the service name of the tube
425 * @parameters: the parameters of the tube
427 * Creates and offers a new #EmpathyTpTube of ChannelType StreamTube.
429 * Return value: a new #EmpathyTpTube
432 empathy_tp_tube_new_stream_tube (EmpathyContact *contact,
433 TpSocketAddressType type,
434 const gchar *hostname,
436 const gchar *service,
437 GHashTable *parameters)
439 TpConnection *connection;
444 GValue *control_param;
445 EmpathyTpTube *tube = NULL;
446 GError *error = NULL;
448 GHashTable *channel_properties;
451 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
452 g_return_val_if_fail (hostname != NULL, NULL);
453 g_return_val_if_fail (service != NULL, NULL);
455 connection = empathy_contact_get_connection (contact);
457 request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
458 (GDestroyNotify) tp_g_value_slice_free);
460 /* org.freedesktop.Telepathy.Channel.ChannelType */
461 value = tp_g_value_slice_new (G_TYPE_STRING);
462 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE);
463 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
465 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
466 value = tp_g_value_slice_new (G_TYPE_UINT);
467 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
468 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
470 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
471 value = tp_g_value_slice_new (G_TYPE_UINT);
472 g_value_set_uint (value, empathy_contact_get_handle (contact));
473 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
475 /* org.freedesktop.Telepathy.Channel.Type.StreamTube.Service */
476 value = tp_g_value_slice_new (G_TYPE_STRING);
477 g_value_set_string (value, service);
478 g_hash_table_insert (request,
479 EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE ".Service", value);
481 if (!tp_cli_connection_interface_requests_run_create_channel (connection, -1,
482 request, &object_path, &channel_properties, &error, NULL))
484 DEBUG ("Error requesting channel: %s", error->message);
485 g_clear_error (&error);
486 g_object_unref (connection);
490 DEBUG ("Offering a new stream tube");
492 channel = tp_channel_new_from_properties (connection, object_path,
493 channel_properties, NULL);
495 tp_channel_run_until_ready (channel, NULL, NULL);
497 #define ADDRESS_TYPE dbus_g_type_get_struct ("GValueArray",\
498 G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID)
499 params = g_hash_table_new (g_str_hash, g_str_equal);
500 address = tp_g_value_slice_new (ADDRESS_TYPE);
501 g_value_take_boxed (address, dbus_g_type_specialized_construct (ADDRESS_TYPE));
502 dbus_g_type_struct_set (address, 0, hostname, 1, port, G_MAXUINT);
503 control_param = tp_g_value_slice_new (G_TYPE_STRING);
505 if (parameters == NULL)
506 /* Pass an empty dict as parameters */
507 parameters = g_hash_table_new (g_str_hash, g_str_equal);
509 g_hash_table_ref (parameters);
511 if (!emp_cli_channel_type_stream_tube_run_offer_stream_tube (
512 TP_PROXY(channel), -1, type, address,
513 TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param, parameters,
516 DEBUG ("Couldn't offer tube: %s", error->message);
517 g_clear_error (&error);
521 DEBUG ("Stream tube offered");
523 tube = empathy_tp_tube_new (channel);
526 g_object_unref (channel);
527 g_free (object_path);
528 g_hash_table_destroy (request);
529 g_hash_table_destroy (channel_properties);
530 tp_g_value_slice_free (address);
531 tp_g_value_slice_free (control_param);
532 g_object_unref (connection);
533 g_hash_table_unref (parameters);
539 tp_tube_accept_stream_cb (TpProxy *proxy,
540 const GValue *address,
543 GObject *weak_object)
545 EmpathyTpTube *tube = EMPATHY_TP_TUBE (weak_object);
546 EmpathyTpTubeAcceptData *data = (EmpathyTpTubeAcceptData *)user_data;
547 EmpathyTpTubeAddress eaddress;
549 eaddress.type = data->type;
553 DEBUG ("Error accepting tube: %s", error->message);
554 data->callback (tube, NULL, error, data->user_data);
558 switch (eaddress.type)
560 case TP_SOCKET_ADDRESS_TYPE_UNIX:
561 case TP_SOCKET_ADDRESS_TYPE_ABSTRACT_UNIX:
562 eaddress.a.socket.path = g_value_get_boxed (address);
564 case TP_SOCKET_ADDRESS_TYPE_IPV4:
565 case TP_SOCKET_ADDRESS_TYPE_IPV6:
566 dbus_g_type_struct_get (address,
567 0, &eaddress.a.inet.hostname,
568 1, &eaddress.a.inet.port, G_MAXUINT);
572 data->callback (tube, &eaddress, NULL, data->user_data);
576 * empathy_tp_tube_accept_stream_tube:
577 * @tube: an #EmpathyTpTube
578 * @type: the type of address the connection manager should listen on
579 * @callback: called when the tube has been accepted
580 * @user_data: arbitrary user-supplied data passed to the callback
582 * Accepts @tube of ChannelType StreamTube and call @callback once it's done.
585 empathy_tp_tube_accept_stream_tube (EmpathyTpTube *tube,
586 TpSocketAddressType type,
587 EmpathyTpTubeAcceptStreamTubeCb *callback,
590 EmpathyTpTubePriv *priv = GET_PRIV (tube);
591 GValue *control_param;
592 EmpathyTpTubeAcceptData *data;
594 g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
596 DEBUG ("Accepting stream tube");
597 /* FIXME allow other acls */
598 control_param = tp_g_value_slice_new (G_TYPE_STRING);
600 data = new_empathy_tp_tube_accept_data (type, callback, user_data);
602 emp_cli_channel_type_stream_tube_call_accept_stream_tube (
603 TP_PROXY (priv->channel), -1, type, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
604 control_param, tp_tube_accept_stream_cb, data,
605 free_empathy_tp_tube_accept_data, G_OBJECT (tube));
607 tp_g_value_slice_free (control_param);
611 * EmpathyTpTubeReadyCb:
612 * @tube: an #EmpathyTpTube
613 * @error: %NULL on success, or the reason why the tube can't be ready
614 * @user_data: the @user_data passed to empathy_tp_tube_call_when_ready()
615 * @weak_object: the @weak_object passed to
616 * empathy_tp_tube_call_when_ready()
618 * Called as the result of empathy_tp_tube_call_when_ready(). If the
619 * tube's properties could be retrieved,
620 * @error is %NULL and @tube is considered to be ready. Otherwise, @error is
621 * non-%NULL and @tube is not ready.
625 * empathy_tp_tube_call_when_ready:
626 * @tube: an #EmpathyTpTube
627 * @callback: called when the tube becomes ready
628 * @user_data: arbitrary user-supplied data passed to the callback
629 * @destroy: called to destroy @user_data
630 * @weak_object: object to reference weakly; if it is destroyed, @callback
631 * will not be called, but @destroy will still be called
633 * If @tube is ready for use, call @callback immediately, then return.
634 * Otherwise, arrange for @callback to be called when @tube becomes
638 empathy_tp_tube_call_when_ready (EmpathyTpTube *self,
639 EmpathyTpTubeReadyCb *callback,
641 GDestroyNotify destroy,
642 GObject *weak_object)
644 EmpathyTpTubePriv *priv = GET_PRIV (self);
646 g_return_if_fail (self != NULL);
647 g_return_if_fail (callback != NULL);
651 callback (self, NULL, user_data, weak_object);
657 priv->ready_callbacks = g_slist_prepend (priv->ready_callbacks,
658 ready_cb_data_new (self, callback, user_data, destroy, weak_object));