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 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpTube)
76 EmpTubeChannelState state;
78 GSList *ready_callbacks;
94 static guint signals[LAST_SIGNAL];
96 G_DEFINE_TYPE (EmpathyTpTube, empathy_tp_tube, G_TYPE_OBJECT)
99 tp_tube_state_changed_cb (TpProxy *proxy,
100 EmpTubeChannelState state,
104 EmpathyTpTubePriv *priv = GET_PRIV (tube);
107 /* We didn't get the state yet */
110 DEBUG ("Tube state changed");
113 g_object_notify (tube, "state");
117 tp_tube_invalidated_cb (TpChannel *channel,
123 DEBUG ("Channel invalidated: %s", message);
124 g_signal_emit (tube, signals[DESTROY], 0);
128 tp_tube_async_cb (TpChannel *channel,
134 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
138 tp_tube_set_property (GObject *object,
143 EmpathyTpTubePriv *priv = GET_PRIV (object);
148 priv->channel = g_value_dup_object (value);
151 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157 tp_tube_get_property (GObject *object,
162 EmpathyTpTubePriv *priv = GET_PRIV (object);
167 g_value_set_object (value, priv->channel);
170 g_value_set_uint (value, priv->state);
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178 static void weak_object_notify (gpointer data,
179 GObject *old_object);
182 ready_cb_data_new (EmpathyTpTube *self,
183 EmpathyTpTubeReadyCb *callback,
185 GDestroyNotify destroy,
186 GObject *weak_object)
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;
194 if (weak_object != NULL)
195 g_object_weak_ref (weak_object, weak_object_notify, self);
201 ready_cb_data_free (ReadyCbData *data,
204 if (data->destroy != NULL)
205 data->destroy (data->user_data);
207 if (data->weak_object != NULL)
208 g_object_weak_unref (data->weak_object,
209 weak_object_notify, self);
211 g_slice_free (ReadyCbData, data);
215 weak_object_notify (gpointer data,
218 EmpathyTpTube *self = EMPATHY_TP_TUBE (data);
219 EmpathyTpTubePriv *priv = GET_PRIV (self);
222 for (l = priv->ready_callbacks ; l != NULL ; l = ln )
224 ReadyCbData *d = (ReadyCbData *) l->data;
225 ln = g_slist_next (l);
227 if (d->weak_object == old_object)
229 ready_cb_data_free (d, self);
230 priv->ready_callbacks = g_slist_delete_link (priv->ready_callbacks,
238 tube_is_ready (EmpathyTpTube *self,
241 EmpathyTpTubePriv *priv = GET_PRIV (self);
246 for (l = priv->ready_callbacks ; l != NULL ; l = g_slist_next (l))
248 ReadyCbData *data = (ReadyCbData *) l->data;
250 data->callback (self, error, data->user_data, data->weak_object);
251 ready_cb_data_free (data, self);
254 g_slist_free (priv->ready_callbacks);
255 priv->ready_callbacks = NULL;
259 got_tube_state_cb (TpProxy *proxy,
260 const GValue *out_value,
263 GObject *weak_object)
265 EmpathyTpTube *self = EMPATHY_TP_TUBE (user_data);
266 EmpathyTpTubePriv *priv = GET_PRIV (self);
270 DEBUG ("Error getting State property: %s", error->message);
274 priv->state = g_value_get_uint (out_value);
275 g_object_notify (G_OBJECT (self), "state");
278 tube_is_ready (self, error);
282 tp_tube_constructor (GType type,
284 GObjectConstructParam *props)
287 EmpathyTpTubePriv *priv;
289 self = G_OBJECT_CLASS (empathy_tp_tube_parent_class)->constructor (
290 type, n_props, props);
291 priv = GET_PRIV (self);
293 g_signal_connect (priv->channel, "invalidated",
294 G_CALLBACK (tp_tube_invalidated_cb), self);
298 emp_cli_channel_interface_tube_connect_to_tube_channel_state_changed (
299 TP_PROXY (priv->channel), tp_tube_state_changed_cb, NULL, NULL,
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));
310 tp_tube_finalize (GObject *object)
312 EmpathyTpTube *self = EMPATHY_TP_TUBE (object);
313 EmpathyTpTubePriv *priv = GET_PRIV (object);
316 DEBUG ("Finalizing: %p", object);
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);
327 for (l = priv->ready_callbacks; l != NULL; l = g_slist_next (l))
329 ReadyCbData *d = (ReadyCbData *) l->data;
331 ready_cb_data_free (d, self);
334 g_slist_free (priv->ready_callbacks);
335 priv->ready_callbacks = NULL;
337 G_OBJECT_CLASS (empathy_tp_tube_parent_class)->finalize (object);
341 empathy_tp_tube_class_init (EmpathyTpTubeClass *klass)
343 GObjectClass *object_class = G_OBJECT_CLASS (klass);
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;
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));
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));
359 signals[DESTROY] = g_signal_new ("destroy",
360 G_TYPE_FROM_CLASS (klass),
363 g_cclosure_marshal_VOID__VOID,
366 g_type_class_add_private (klass, sizeof (EmpathyTpTubePriv));
370 empathy_tp_tube_init (EmpathyTpTube *tube)
372 EmpathyTpTubePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tube,
373 EMPATHY_TYPE_TP_TUBE, EmpathyTpTubePriv);
379 empathy_tp_tube_new (TpChannel *channel)
381 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
383 return g_object_new (EMPATHY_TYPE_TP_TUBE, "channel", channel, NULL);
387 empathy_tp_tube_new_stream_tube (EmpathyContact *contact,
388 TpSocketAddressType type,
389 const gchar *hostname,
391 const gchar *service,
392 GHashTable *parameters)
394 TpConnection *connection;
399 GValue *control_param;
400 EmpathyTpTube *tube = NULL;
401 GError *error = NULL;
403 GHashTable *channel_properties;
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);
410 connection = empathy_contact_get_connection (contact);
412 request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
413 (GDestroyNotify) tp_g_value_slice_free);
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);
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);
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);
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);
436 if (!tp_cli_connection_interface_requests_run_create_channel (connection, -1,
437 request, &object_path, &channel_properties, &error, NULL))
439 DEBUG ("Error requesting channel: %s", error->message);
440 g_clear_error (&error);
441 g_object_unref (connection);
445 DEBUG ("Offering a new stream tube");
447 channel = tp_channel_new_from_properties (connection, object_path,
448 channel_properties, NULL);
450 tp_channel_run_until_ready (channel, NULL, NULL);
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);
460 if (parameters == NULL)
461 /* Pass an empty dict as parameters */
462 parameters = g_hash_table_new (g_str_hash, g_str_equal);
464 g_hash_table_ref (parameters);
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,
471 DEBUG ("Couldn't offer tube: %s", error->message);
472 g_clear_error (&error);
476 DEBUG ("Stream tube offered");
478 tube = empathy_tp_tube_new (channel);
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);
494 tp_tube_accept_stream_cb (TpProxy *proxy,
495 const GValue *address,
498 GObject *weak_object)
500 EmpathyTpTube *tube = EMPATHY_TP_TUBE (weak_object);
501 EmpathyTpTubeAcceptData *data = (EmpathyTpTubeAcceptData *)user_data;
502 EmpathyTpTubeAddress eaddress;
504 eaddress.type = data->type;
508 DEBUG ("Error accepting tube: %s", error->message);
509 data->callback (tube, NULL, error, data->user_data);
513 switch (eaddress.type)
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);
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);
527 data->callback (tube, &eaddress, NULL, data->user_data);
531 empathy_tp_tube_accept_stream_tube (EmpathyTpTube *tube,
532 TpSocketAddressType type,
533 EmpathyTpTubeAcceptStreamTubeCb *callback,
536 EmpathyTpTubePriv *priv = GET_PRIV (tube);
537 GValue *control_param;
538 EmpathyTpTubeAcceptData *data;
540 g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
542 DEBUG ("Accepting stream tube");
543 /* FIXME allow other acls */
544 control_param = tp_g_value_slice_new (G_TYPE_STRING);
546 data = new_empathy_tp_tube_accept_data (type, callback, user_data);
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));
553 tp_g_value_slice_free (control_param);
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()
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.
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
579 * If @tube is ready for use, call @callback immediately, then return.
580 * Otherwise, arrange for @callback to be called when @tube becomes
584 empathy_tp_tube_call_when_ready (EmpathyTpTube *self,
585 EmpathyTpTubeReadyCb *callback,
587 GDestroyNotify destroy,
588 GObject *weak_object)
590 EmpathyTpTubePriv *priv = GET_PRIV (self);
592 g_return_if_fail (self != NULL);
593 g_return_if_fail (callback != NULL);
597 callback (self, NULL, user_data, weak_object);
603 priv->ready_callbacks = g_slist_prepend (priv->ready_callbacks,
604 ready_cb_data_new (self, callback, user_data, destroy, weak_object));