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/util.h>
27 #include "empathy-contact-factory.h"
28 #include "empathy-debug.h"
29 #include "empathy-enum-types.h"
30 #include "empathy-tp-tube.h"
31 #include "empathy-utils.h"
33 #define DEBUG_DOMAIN "TpTube"
35 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_TP_TUBE, \
38 typedef struct _EmpathyTpTubePriv EmpathyTpTubePriv;
40 struct _EmpathyTpTubePriv
47 GHashTable *parameters;
49 EmpathyContact *initiator_contact;
50 EmpathyContactFactory *factory;
64 PROP_INITIATOR_CONTACT
73 static guint signals[LAST_SIGNAL];
75 G_DEFINE_TYPE (EmpathyTpTube, empathy_tp_tube, G_TYPE_OBJECT)
78 tp_tube_state_changed_cb (TpChannel *channel,
84 EmpathyTpTubePriv *priv = GET_PRIV (tube);
89 empathy_debug (DEBUG_DOMAIN, "Tube state changed");
92 g_object_notify (tube, "state");
96 tp_tube_invalidated_cb (TpChannel *channel,
102 empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
103 g_signal_emit (tube, signals[DESTROY], 0);
107 tp_tube_closed_cb (TpChannel *channel,
112 EmpathyTpTubePriv *priv = GET_PRIV (tube);
117 empathy_debug (DEBUG_DOMAIN, "Tube closed");
118 g_signal_emit (tube, signals[DESTROY], 0);
122 tp_tube_async_cb (TpChannel *channel,
128 empathy_debug (DEBUG_DOMAIN, "Error %s: %s", user_data, error->message);
132 tp_tube_set_property (GObject *object,
137 EmpathyTpTubePriv *priv = GET_PRIV (object);
142 priv->channel = g_value_dup_object (value);
145 priv->id = g_value_get_uint (value);
148 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154 tp_tube_get_property (GObject *object,
159 EmpathyTpTubePriv *priv = GET_PRIV (object);
164 g_value_set_object (value, priv->channel);
167 g_value_set_uint (value, priv->id);
170 g_value_set_uint (value, priv->initiator);
173 g_value_set_uint (value, priv->type);
176 g_value_set_string (value, priv->service);
178 case PROP_PARAMETERS:
179 g_value_set_boxed (value, priv->parameters);
182 g_value_set_uint (value, priv->state);
184 case PROP_INITIATOR_CONTACT:
185 g_value_set_object (value, priv->initiator_contact);
188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194 tp_tube_constructor (GType type,
196 GObjectConstructParam *props)
199 EmpathyTpTubePriv *priv;
202 GError *error = NULL;
204 self = G_OBJECT_CLASS (empathy_tp_tube_parent_class)->constructor (
205 type, n_props, props);
206 priv = GET_PRIV (self);
208 g_signal_connect (priv->channel, "invalidated",
209 G_CALLBACK (tp_tube_invalidated_cb), self);
210 tp_cli_channel_type_tubes_connect_to_tube_closed (priv->channel,
211 tp_tube_closed_cb, NULL, NULL, self, NULL);
212 tp_cli_channel_type_tubes_connect_to_tube_state_changed (priv->channel,
213 tp_tube_state_changed_cb, NULL, NULL, self, NULL);
215 /* FIXME: It is absolutely not opimized to list all tubes to get information
216 * about our tube, but we don't really have the choice to avoid races. */
217 if (!tp_cli_channel_type_tubes_run_list_tubes (priv->channel, -1, &tubes,
220 empathy_debug (DEBUG_DOMAIN, "Couldn't list tubes: %s",
222 g_clear_error (&error);
226 for (i = 0; i < tubes->len; i++)
231 values = g_ptr_array_index (tubes, i);
232 id = g_value_get_uint (g_value_array_get_nth (values, 0));
236 TpConnection *connection;
240 g_object_get (priv->channel, "connection", &connection, NULL);
241 mc = empathy_mission_control_new ();
242 account = mission_control_get_account_for_tpconnection (mc,
245 priv->initiator = g_value_get_uint (g_value_array_get_nth (values, 1));
246 priv->type = g_value_get_uint (g_value_array_get_nth (values, 2));
247 priv->service = g_value_dup_string (g_value_array_get_nth (values, 3));
248 priv->parameters = g_value_dup_boxed (g_value_array_get_nth (values, 4));
249 priv->state = g_value_get_uint (g_value_array_get_nth (values, 5));
250 priv->initiator_contact = empathy_contact_factory_get_from_handle (
251 priv->factory, account, priv->initiator);
253 g_object_unref (connection);
255 g_object_unref (account);
258 g_value_array_free (values);
260 g_ptr_array_free (tubes, TRUE);
266 tp_tube_finalize (GObject *object)
268 EmpathyTpTubePriv *priv = GET_PRIV (object);
270 empathy_debug (DEBUG_DOMAIN, "Finalizing: %p", object);
274 g_signal_handlers_disconnect_by_func (priv->channel,
275 tp_tube_invalidated_cb, object);
276 tp_cli_channel_type_tubes_call_close_tube (priv->channel, -1, priv->id,
277 tp_tube_async_cb, "closing tube", NULL, NULL);
278 g_object_unref (priv->channel);
280 if (priv->initiator_contact)
281 g_object_unref (priv->initiator_contact);
283 g_object_unref (priv->factory);
285 g_free (priv->service);
286 g_hash_table_destroy (priv->parameters);
288 G_OBJECT_CLASS (empathy_tp_tube_parent_class)->finalize (object);
292 empathy_tp_tube_class_init (EmpathyTpTubeClass *klass)
294 GObjectClass *object_class = G_OBJECT_CLASS (klass);
296 object_class->constructor = tp_tube_constructor;
297 object_class->finalize = tp_tube_finalize;
298 object_class->set_property = tp_tube_set_property;
299 object_class->get_property = tp_tube_get_property;
301 g_object_class_install_property (object_class, PROP_CHANNEL,
302 g_param_spec_object ("channel", "channel", "channel", TP_TYPE_CHANNEL,
303 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME |
304 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
306 g_object_class_install_property (object_class, PROP_ID,
307 g_param_spec_uint ("id", "id", "id", 0, G_MAXUINT, 0,
308 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME |
309 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
311 g_object_class_install_property (object_class, PROP_INITIATOR,
312 g_param_spec_uint ("initiator", "initiator", "initiator",
313 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_NAME |
314 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
316 g_object_class_install_property (object_class, PROP_TYPE,
317 g_param_spec_uint ("type", "type", "type", 0, G_MAXUINT, 0,
318 G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
319 G_PARAM_STATIC_BLURB));
321 g_object_class_install_property (object_class, PROP_SERVICE,
322 g_param_spec_string ("service", "service", "service", NULL,
323 G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
324 G_PARAM_STATIC_BLURB));
326 g_object_class_install_property (object_class, PROP_PARAMETERS,
327 g_param_spec_boxed ("parameters", "parameters", "parameters",
328 G_TYPE_HASH_TABLE, G_PARAM_READABLE | G_PARAM_STATIC_NAME |
329 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
331 g_object_class_install_property (object_class, PROP_STATE,
332 g_param_spec_uint ("state", "state", "state", 0, G_MAXUINT, 0,
333 G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
334 G_PARAM_STATIC_BLURB));
336 g_object_class_install_property (object_class, PROP_INITIATOR_CONTACT,
337 g_param_spec_object ("initiator-contact", "initiator contact",
338 "initiator contact", EMPATHY_TYPE_CONTACT, G_PARAM_READABLE |
339 G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
341 signals[DESTROY] = g_signal_new ("destroy",
342 G_TYPE_FROM_CLASS (klass),
345 g_cclosure_marshal_VOID__VOID,
348 g_type_class_add_private (klass, sizeof (EmpathyTpTubePriv));
352 empathy_tp_tube_init (EmpathyTpTube *tube)
354 EmpathyTpTubePriv *priv = GET_PRIV (tube);
356 priv->factory = empathy_contact_factory_new ();
360 empathy_tp_tube_new (TpChannel *channel, guint tube_id)
362 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
364 return g_object_new (EMPATHY_TYPE_TP_TUBE,
365 "channel", channel, "id", tube_id, NULL);
369 empathy_tp_tube_new_stream_tube (EmpathyContact *contact,
370 TpSocketAddressType type,
371 const gchar *hostname,
373 const gchar *service)
377 TpConnection *connection;
383 GValue *control_param;
384 EmpathyTpTube *tube = NULL;
385 GError *error = NULL;
387 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
388 g_return_val_if_fail (hostname != NULL, NULL);
389 g_return_val_if_fail (service != NULL, NULL);
391 mc = empathy_mission_control_new ();
392 account = empathy_contact_get_account (contact);
393 connection = mission_control_get_tpconnection (mc, account, NULL);
396 if (!tp_cli_connection_run_request_channel (connection, -1,
397 TP_IFACE_CHANNEL_TYPE_TUBES, TP_HANDLE_TYPE_CONTACT,
398 empathy_contact_get_handle (contact), FALSE, &object_path, &error, NULL))
400 empathy_debug (DEBUG_DOMAIN, "Error requesting channel: %s", error->message);
401 g_clear_error (&error);
402 g_object_unref (connection);
406 empathy_debug (DEBUG_DOMAIN, "Offering a new stream tube");
408 channel = tp_channel_new (connection, object_path,
409 TP_IFACE_CHANNEL_TYPE_TUBES, TP_HANDLE_TYPE_CONTACT,
410 empathy_contact_get_handle (contact), NULL);
412 #define ADDRESS_TYPE dbus_g_type_get_struct ("GValueArray",\
413 G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID)
414 params = g_hash_table_new (g_str_hash, g_str_equal);
415 address = tp_g_value_slice_new (ADDRESS_TYPE);
416 g_value_take_boxed (address, dbus_g_type_specialized_construct (ADDRESS_TYPE));
417 dbus_g_type_struct_set (address, 0, hostname, 1, port, G_MAXUINT);
418 control_param = tp_g_value_slice_new (G_TYPE_STRING);
420 if (!tp_cli_channel_type_tubes_run_offer_stream_tube (channel, -1,
421 service, params, type, address,
422 TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param, &id, &error, NULL))
424 empathy_debug (DEBUG_DOMAIN, "Couldn't offer tube: %s", error->message);
425 g_clear_error (&error);
429 empathy_debug (DEBUG_DOMAIN, "Stream tube id=%d offered", id);
431 tube = empathy_tp_tube_new (channel, id);
434 g_object_unref (channel);
435 g_free (object_path);
436 g_hash_table_destroy (params);
437 tp_g_value_slice_free (address);
438 tp_g_value_slice_free (control_param);
439 g_object_unref (connection);
445 tp_tube_accept_stream_cb (TpChannel *proxy,
446 const GValue *address,
449 GObject *weak_object)
452 empathy_debug (DEBUG_DOMAIN, "Error accepting tube: %s", error->message);
456 empathy_tp_tube_accept_stream_tube (EmpathyTpTube *tube,
457 TpSocketAddressType type)
459 EmpathyTpTubePriv *priv = GET_PRIV (tube);
460 GValue *control_param;
462 g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
464 empathy_debug (DEBUG_DOMAIN, "Accepting stream tube - id: %d", priv->id);
466 control_param = tp_g_value_slice_new (G_TYPE_STRING);
467 tp_cli_channel_type_tubes_call_accept_stream_tube (priv->channel, -1, priv->id,
468 type, TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param,
469 tp_tube_accept_stream_cb, NULL, NULL, G_OBJECT (tube));
471 tp_g_value_slice_free (control_param);
475 empathy_tp_tube_get_socket (EmpathyTpTube *tube,
479 EmpathyTpTubePriv *priv = GET_PRIV (tube);
482 GError *error = NULL;
484 g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
486 empathy_debug (DEBUG_DOMAIN, "Getting stream tube socket address");
488 address = g_slice_new0 (GValue);
489 if (!tp_cli_channel_type_tubes_run_get_stream_tube_socket_address (priv->channel,
490 -1, priv->id, &address_type, &address, &error, NULL))
492 empathy_debug (DEBUG_DOMAIN, "Couldn't get socket address: %s",
494 g_clear_error (&error);
498 switch (address_type)
500 case TP_SOCKET_ADDRESS_TYPE_UNIX:
501 case TP_SOCKET_ADDRESS_TYPE_ABSTRACT_UNIX:
502 dbus_g_type_struct_get (address, 0, hostname, G_MAXUINT);
504 case TP_SOCKET_ADDRESS_TYPE_IPV4:
505 case TP_SOCKET_ADDRESS_TYPE_IPV6:
506 dbus_g_type_struct_get (address, 0, hostname, 1, port, G_MAXUINT);
510 tp_g_value_slice_free (address);