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.
82 * @parent: parent object
84 * An object wrapping a Telepathy tube channel.
87 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpTube)
91 TpTubeChannelState state;
93 GSList *ready_callbacks;
109 static guint signals[LAST_SIGNAL];
111 G_DEFINE_TYPE (EmpathyTpTube, empathy_tp_tube, G_TYPE_OBJECT)
114 tp_tube_state_changed_cb (TpChannel *channel,
115 TpTubeChannelState state,
119 EmpathyTpTubePriv *priv = GET_PRIV (tube);
122 /* We didn't get the state yet */
125 DEBUG ("Tube state changed");
128 g_object_notify (tube, "state");
132 tp_tube_invalidated_cb (TpChannel *channel,
138 DEBUG ("Channel invalidated: %s", message);
139 g_signal_emit (tube, signals[DESTROY], 0);
143 tp_tube_async_cb (TpChannel *channel,
149 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
153 tp_tube_set_property (GObject *object,
158 EmpathyTpTubePriv *priv = GET_PRIV (object);
163 priv->channel = g_value_dup_object (value);
166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172 tp_tube_get_property (GObject *object,
177 EmpathyTpTubePriv *priv = GET_PRIV (object);
182 g_value_set_object (value, priv->channel);
185 g_value_set_uint (value, priv->state);
188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193 static void weak_object_notify (gpointer data,
194 GObject *old_object);
197 ready_cb_data_new (EmpathyTpTube *self,
198 EmpathyTpTubeReadyCb *callback,
200 GDestroyNotify destroy,
201 GObject *weak_object)
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;
209 if (weak_object != NULL)
210 g_object_weak_ref (weak_object, weak_object_notify, self);
216 ready_cb_data_free (ReadyCbData *data,
219 if (data->destroy != NULL)
220 data->destroy (data->user_data);
222 if (data->weak_object != NULL)
223 g_object_weak_unref (data->weak_object,
224 weak_object_notify, self);
226 g_slice_free (ReadyCbData, data);
230 weak_object_notify (gpointer data,
233 EmpathyTpTube *self = EMPATHY_TP_TUBE (data);
234 EmpathyTpTubePriv *priv = GET_PRIV (self);
237 for (l = priv->ready_callbacks ; l != NULL ; l = ln )
239 ReadyCbData *d = (ReadyCbData *) l->data;
240 ln = g_slist_next (l);
242 if (d->weak_object == old_object)
244 ready_cb_data_free (d, self);
245 priv->ready_callbacks = g_slist_delete_link (priv->ready_callbacks,
253 tube_is_ready (EmpathyTpTube *self,
256 EmpathyTpTubePriv *priv = GET_PRIV (self);
261 /* tube has to stay alive while we call the callbacks */
263 for (l = priv->ready_callbacks ; l != NULL ; l = g_slist_next (l))
265 ReadyCbData *data = (ReadyCbData *) l->data;
267 data->callback (self, error, data->user_data, data->weak_object);
268 ready_cb_data_free (data, self);
270 g_object_unref (self);
272 g_slist_free (priv->ready_callbacks);
273 priv->ready_callbacks = NULL;
277 got_tube_state_cb (TpProxy *proxy,
278 const GValue *out_value,
281 GObject *weak_object)
283 EmpathyTpTube *self = EMPATHY_TP_TUBE (user_data);
284 EmpathyTpTubePriv *priv = GET_PRIV (self);
288 DEBUG ("Error getting State property: %s", error->message);
292 priv->state = g_value_get_uint (out_value);
293 g_object_notify (G_OBJECT (self), "state");
296 tube_is_ready (self, error);
300 tp_tube_constructor (GType type,
302 GObjectConstructParam *props)
305 EmpathyTpTubePriv *priv;
307 self = G_OBJECT_CLASS (empathy_tp_tube_parent_class)->constructor (
308 type, n_props, props);
309 priv = GET_PRIV (self);
311 g_signal_connect (priv->channel, "invalidated",
312 G_CALLBACK (tp_tube_invalidated_cb), self);
316 tp_cli_channel_interface_tube_connect_to_tube_channel_state_changed (
317 priv->channel, tp_tube_state_changed_cb, NULL, NULL,
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));
328 tp_tube_finalize (GObject *object)
330 EmpathyTpTube *self = EMPATHY_TP_TUBE (object);
331 EmpathyTpTubePriv *priv = GET_PRIV (object);
334 DEBUG ("Finalizing: %p", object);
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);
345 for (l = priv->ready_callbacks; l != NULL; l = g_slist_next (l))
347 ReadyCbData *d = (ReadyCbData *) l->data;
349 ready_cb_data_free (d, self);
352 g_slist_free (priv->ready_callbacks);
353 priv->ready_callbacks = NULL;
355 G_OBJECT_CLASS (empathy_tp_tube_parent_class)->finalize (object);
359 empathy_tp_tube_class_init (EmpathyTpTubeClass *klass)
361 GObjectClass *object_class = G_OBJECT_CLASS (klass);
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;
369 * EmpathyTpTube:channel:
371 * The #TpChannel wrapped by the tube object.
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));
378 * EmpathyTpTube:state:
380 * The state of the tube.
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));
387 * EmpathyTpTube::destroy:
388 * @self: the tube object
390 * Emitted when then tube has been invalidated.
392 signals[DESTROY] = g_signal_new ("destroy",
393 G_TYPE_FROM_CLASS (klass),
396 g_cclosure_marshal_VOID__VOID,
399 g_type_class_add_private (klass, sizeof (EmpathyTpTubePriv));
403 empathy_tp_tube_init (EmpathyTpTube *tube)
405 EmpathyTpTubePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tube,
406 EMPATHY_TYPE_TP_TUBE, EmpathyTpTubePriv);
412 * empathy_tp_tube_new:
413 * @channel: a #TpChannel
415 * Creates a new #EmpathyTpTube.
417 * Return value: a new #EmpathyTpTube
420 empathy_tp_tube_new (TpChannel *channel)
422 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
424 return g_object_new (EMPATHY_TYPE_TP_TUBE, "channel", channel, NULL);
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
437 * Creates and offers a new #EmpathyTpTube of ChannelType StreamTube.
439 * Return value: a new #EmpathyTpTube
442 empathy_tp_tube_new_stream_tube (EmpathyContact *contact,
443 TpSocketAddressType type,
444 const gchar *hostname,
446 const gchar *service,
447 GHashTable *parameters)
449 TpConnection *connection;
454 GValue *control_param;
455 EmpathyTpTube *tube = NULL;
456 GError *error = NULL;
458 GHashTable *channel_properties;
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);
465 connection = empathy_contact_get_connection (contact);
467 request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
468 (GDestroyNotify) tp_g_value_slice_free);
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);
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);
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);
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);
491 if (!tp_cli_connection_interface_requests_run_create_channel (connection, -1,
492 request, &object_path, &channel_properties, &error, NULL))
494 DEBUG ("Error requesting channel: %s", error->message);
495 g_clear_error (&error);
496 g_object_unref (connection);
500 DEBUG ("Offering a new stream tube");
502 channel = tp_channel_new_from_properties (connection, object_path,
503 channel_properties, NULL);
505 tp_channel_run_until_ready (channel, NULL, NULL);
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);
515 if (parameters == NULL)
516 /* Pass an empty dict as parameters */
517 parameters = g_hash_table_new (g_str_hash, g_str_equal);
519 g_hash_table_ref (parameters);
521 if (!tp_cli_channel_type_stream_tube_run_offer (channel, -1, type, address,
522 TP_SOCKET_ACCESS_CONTROL_LOCALHOST, parameters,
525 DEBUG ("Couldn't offer tube: %s", error->message);
526 g_clear_error (&error);
530 DEBUG ("Stream tube offered");
532 tube = empathy_tp_tube_new (channel);
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);
548 tp_tube_accept_stream_cb (TpChannel *channel,
549 const GValue *address,
552 GObject *weak_object)
554 EmpathyTpTube *tube = EMPATHY_TP_TUBE (weak_object);
555 EmpathyTpTubeAcceptData *data = (EmpathyTpTubeAcceptData *) user_data;
556 EmpathyTpTubeAddress eaddress;
558 eaddress.type = data->type;
562 DEBUG ("Error accepting tube: %s", error->message);
563 data->callback (tube, NULL, error, data->user_data);
567 switch (eaddress.type)
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);
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);
581 data->callback (tube, &eaddress, NULL, data->user_data);
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
591 * Accepts @tube of ChannelType StreamTube and call @callback once it's done.
594 empathy_tp_tube_accept_stream_tube (EmpathyTpTube *tube,
595 TpSocketAddressType type,
596 EmpathyTpTubeAcceptStreamTubeCb *callback,
599 EmpathyTpTubePriv *priv = GET_PRIV (tube);
600 GValue *control_param;
601 EmpathyTpTubeAcceptData *data;
603 g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
605 DEBUG ("Accepting stream tube");
606 /* FIXME allow other acls */
607 control_param = tp_g_value_slice_new (G_TYPE_STRING);
609 data = new_empathy_tp_tube_accept_data (type, callback, user_data);
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));
616 tp_g_value_slice_free (control_param);
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()
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.
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
642 * If @tube is ready for use, call @callback immediately, then return.
643 * Otherwise, arrange for @callback to be called when @tube becomes
647 empathy_tp_tube_call_when_ready (EmpathyTpTube *self,
648 EmpathyTpTubeReadyCb *callback,
650 GDestroyNotify destroy,
651 GObject *weak_object)
653 EmpathyTpTubePriv *priv = GET_PRIV (self);
655 g_return_if_fail (self != NULL);
656 g_return_if_fail (callback != NULL);
660 callback (self, NULL, user_data, weak_object);
666 priv->ready_callbacks = g_slist_prepend (priv->ready_callbacks,
667 ready_cb_data_new (self, callback, user_data, destroy, weak_object));