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>
26 #include "empathy-contact-factory.h"
27 #include "empathy-debug.h"
28 #include "empathy-enum-types.h"
29 #include "empathy-tp-tube.h"
30 #include "empathy-utils.h"
32 #define DEBUG_DOMAIN "TpTube"
34 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_TP_TUBE, \
37 typedef struct _EmpathyTpTubePriv EmpathyTpTubePriv;
39 struct _EmpathyTpTubePriv
46 GHashTable *parameters;
48 EmpathyContact *initiator_contact;
49 EmpathyContactFactory *factory;
63 PROP_INITIATOR_CONTACT
72 static guint signals[LAST_SIGNAL];
74 G_DEFINE_TYPE (EmpathyTpTube, empathy_tp_tube, G_TYPE_OBJECT)
77 tp_tube_state_changed_cb (TpChannel *channel,
83 EmpathyTpTubePriv *priv = GET_PRIV (tube);
88 empathy_debug (DEBUG_DOMAIN, "Tube state changed");
91 g_object_notify (tube, "state");
95 tp_tube_invalidated_cb (TpChannel *channel,
101 empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
102 g_signal_emit (tube, signals[DESTROY], 0);
106 tp_tube_closed_cb (TpChannel *channel,
111 EmpathyTpTubePriv *priv = GET_PRIV (tube);
116 empathy_debug (DEBUG_DOMAIN, "Tube closed");
117 g_signal_emit (tube, signals[DESTROY], 0);
121 tp_tube_async_cb (TpChannel *channel,
127 empathy_debug (DEBUG_DOMAIN, "Error %s: %s", user_data, error->message);
131 tp_tube_set_property (GObject *object,
136 EmpathyTpTubePriv *priv = GET_PRIV (object);
141 priv->channel = g_value_dup_object (value);
144 priv->id = g_value_get_uint (value);
147 priv->initiator = g_value_get_uint (value);
150 priv->type = g_value_get_uint (value);
153 priv->service = g_value_dup_string (value);
155 case PROP_PARAMETERS:
156 priv->parameters = g_value_dup_boxed (value);
159 priv->state = g_value_get_uint (value);
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
168 tp_tube_get_property (GObject *object,
173 EmpathyTpTubePriv *priv = GET_PRIV (object);
178 g_value_set_object (value, priv->channel);
181 g_value_set_uint (value, priv->id);
184 g_value_set_uint (value, priv->initiator);
187 g_value_set_uint (value, priv->type);
190 g_value_set_string (value, priv->service);
192 case PROP_PARAMETERS:
193 g_value_set_boxed (value, priv->parameters);
196 g_value_set_uint (value, priv->state);
198 case PROP_INITIATOR_CONTACT:
199 g_value_set_object (value, priv->initiator_contact);
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
208 tp_tube_constructor (GType type,
210 GObjectConstructParam *props)
213 EmpathyTpTubePriv *priv;
214 TpConnection *connection;
218 self = G_OBJECT_CLASS (empathy_tp_tube_parent_class)->constructor (
219 type, n_props, props);
220 priv = GET_PRIV (self);
222 g_object_get (priv->channel, "connection", &connection, NULL);
223 mc = empathy_mission_control_new ();
224 account = mission_control_get_account_for_tpconnection (mc, connection, NULL);
226 priv->factory = empathy_contact_factory_new ();
227 priv->initiator_contact = empathy_contact_factory_get_from_handle (priv->factory,
228 account, priv->initiator);
229 g_object_ref (priv->initiator_contact);
231 g_signal_connect (priv->channel, "invalidated",
232 G_CALLBACK (tp_tube_invalidated_cb), self);
234 tp_cli_channel_type_tubes_connect_to_tube_closed (priv->channel,
235 tp_tube_closed_cb, NULL, NULL, self, NULL);
236 tp_cli_channel_type_tubes_connect_to_tube_state_changed (priv->channel,
237 tp_tube_state_changed_cb, NULL, NULL, self, NULL);
239 g_object_unref (connection);
241 g_object_unref (account);
247 tp_tube_finalize (GObject *object)
249 EmpathyTpTubePriv *priv = GET_PRIV (object);
251 empathy_debug (DEBUG_DOMAIN, "Finalizing: %p", object);
255 g_signal_handlers_disconnect_by_func (priv->channel,
256 tp_tube_invalidated_cb, object);
257 tp_cli_channel_type_tubes_call_close_tube (priv->channel, -1, priv->id,
258 tp_tube_async_cb, "closing tube", NULL, NULL);
259 g_object_unref (priv->channel);
261 if (priv->initiator_contact)
262 g_object_unref (priv->initiator_contact);
264 g_object_unref (priv->factory);
266 g_free (priv->service);
267 g_hash_table_destroy (priv->parameters);
269 G_OBJECT_CLASS (empathy_tp_tube_parent_class)->finalize (object);
273 empathy_tp_tube_class_init (EmpathyTpTubeClass *klass)
275 GObjectClass *object_class = G_OBJECT_CLASS (klass);
277 object_class->constructor = tp_tube_constructor;
278 object_class->finalize = tp_tube_finalize;
279 object_class->set_property = tp_tube_set_property;
280 object_class->get_property = tp_tube_get_property;
282 g_object_class_install_property (object_class, PROP_CHANNEL,
283 g_param_spec_object ("channel", "channel", "channel", TP_TYPE_CHANNEL,
284 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME |
285 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
287 g_object_class_install_property (object_class, PROP_ID,
288 g_param_spec_uint ("id", "id", "id", 0, G_MAXUINT, 0,
289 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME |
290 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
292 g_object_class_install_property (object_class, PROP_INITIATOR,
293 g_param_spec_uint ("initiator", "initiator", "initiator",
294 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
295 G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
297 g_object_class_install_property (object_class, PROP_TYPE,
298 g_param_spec_uint ("type", "type", "type", 0, G_MAXUINT, 0,
299 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME |
300 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
302 g_object_class_install_property (object_class, PROP_SERVICE,
303 g_param_spec_string ("service", "service", "service", NULL,
304 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME |
305 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
307 g_object_class_install_property (object_class, PROP_PARAMETERS,
308 g_param_spec_boxed ("parameters", "parameters", "parameters",
309 G_TYPE_HASH_TABLE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
310 G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
312 g_object_class_install_property (object_class, PROP_STATE,
313 g_param_spec_uint ("state", "state", "state", 0, G_MAXUINT, 0,
314 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
315 G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
317 g_object_class_install_property (object_class, PROP_INITIATOR_CONTACT,
318 g_param_spec_object ("initiator-contact", "initiator contact",
319 "initiator contact", EMPATHY_TYPE_CONTACT, G_PARAM_READABLE |
320 G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
322 signals[DESTROY] = g_signal_new ("destroy",
323 G_TYPE_FROM_CLASS (klass),
326 g_cclosure_marshal_VOID__VOID,
329 g_type_class_add_private (klass, sizeof (EmpathyTpTubePriv));
333 empathy_tp_tube_init (EmpathyTpTube *tp_tubes)
338 empathy_tp_tube_new (TpChannel *channel, guint tube_id)
340 EmpathyTpTube *tube = NULL;
343 GError *error = NULL;
345 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
347 if (!tp_cli_channel_type_tubes_run_list_tubes (channel, -1, &tubes,
350 empathy_debug (DEBUG_DOMAIN, "Couldn't list tubes: %s",
352 g_clear_error (&error);
356 for (i = 0; i < tubes->len; i++)
361 values = g_ptr_array_index (tubes, i);
362 id = g_value_get_uint (g_value_array_get_nth (values, 0));
366 g_value_array_free (values);
370 tube = g_object_new (EMPATHY_TYPE_TP_TUBE,
373 "initiator", g_value_get_uint (g_value_array_get_nth (values, 1)),
374 "type", g_value_get_uint (g_value_array_get_nth (values, 2)),
375 "service", g_value_get_string (g_value_array_get_nth (values, 3)),
376 "parameters", g_value_get_boxed (g_value_array_get_nth (values, 4)),
377 "state", g_value_get_uint (g_value_array_get_nth (values, 5)),
380 g_value_array_free (values);
382 g_ptr_array_free (tubes, TRUE);
388 tp_tube_accept_stream_cb (TpChannel *proxy,
389 const GValue *address,
392 GObject *weak_object)
395 empathy_debug (DEBUG_DOMAIN, "Error accepting tube: %s", error->message);
399 tp_tube_accept_stream_tube (EmpathyTpTube *tube,
400 TpSocketAddressType address_type,
401 TpSocketAccessControl access_type,
402 GValue *control_param)
404 EmpathyTpTubePriv *priv = GET_PRIV (tube);
406 empathy_debug (DEBUG_DOMAIN, "Accepting stream tube - id: %d", priv->id);
408 tp_cli_channel_type_tubes_call_accept_stream_tube (priv->channel, -1, priv->id,
409 address_type, access_type, control_param,
410 tp_tube_accept_stream_cb, NULL, NULL, G_OBJECT (tube));
414 empathy_tp_tube_accept_unix_stream_tube (EmpathyTpTube *tube)
416 GValue control_param = {0, };
418 g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
420 g_value_init (&control_param, G_TYPE_STRING);
421 tp_tube_accept_stream_tube (tube, TP_SOCKET_ADDRESS_TYPE_UNIX,
422 TP_SOCKET_ACCESS_CONTROL_LOCALHOST, &control_param);
424 g_value_reset (&control_param);
428 empathy_tp_tube_accept_ipv4_stream_tube (EmpathyTpTube *tube)
430 GValue control_param = {0, };
432 g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
434 g_value_init (&control_param, G_TYPE_STRING);
435 tp_tube_accept_stream_tube (tube, TP_SOCKET_ADDRESS_TYPE_IPV4,
436 TP_SOCKET_ACCESS_CONTROL_LOCALHOST, &control_param);
438 g_value_reset (&control_param);
442 empathy_tp_tube_get_unix_socket (EmpathyTpTube *tube)
444 EmpathyTpTubePriv *priv = GET_PRIV (tube);
445 GValue *address = g_new0 (GValue, 1);;
447 gchar *address_name = NULL;
448 GError *error = NULL;
450 g_return_val_if_fail (EMPATHY_IS_TP_TUBE (tube), NULL);
452 empathy_debug (DEBUG_DOMAIN, "Getting stream tube socket address");
454 /* FIXME: We shouldn't use _run_ here because the user may not expect to
455 * reenter the mainloop.
456 * FIXME: Do we have to give an initialised GValue for address? Are we
457 * freeing it correctly? */
458 if (!tp_cli_channel_type_tubes_run_get_stream_tube_socket_address (priv->channel,
459 -1, priv->id, &address_type, &address, &error, NULL))
461 empathy_debug (DEBUG_DOMAIN, "Couldn't get socket address: %s",
463 g_clear_error (&error);
467 dbus_g_type_struct_get (address, 0, &address_name, G_MAXUINT);
470 empathy_debug (DEBUG_DOMAIN, "UNIX Socket - %s", address_name);
476 empathy_tp_tube_get_ipv4_socket (EmpathyTpTube *tube,
480 EmpathyTpTubePriv *priv = GET_PRIV (tube);
481 GValue *address = g_new0 (GValue, 1);
483 GError *error = NULL;
485 g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
487 empathy_debug (DEBUG_DOMAIN, "Getting stream tube socket address");
489 /* FIXME: Same than for empathy_tp_tube_get_unix_socket() */
490 if (!tp_cli_channel_type_tubes_run_get_stream_tube_socket_address (priv->channel,
491 -1, priv->id, &address_type, &address, &error, NULL))
493 empathy_debug (DEBUG_DOMAIN, "Couldn't get socket address: %s",
495 g_clear_error (&error);
499 dbus_g_type_struct_get (address, 0, hostname, 1, port, G_MAXUINT);
505 empathy_offer_ipv4_stream_tube (EmpathyContact *contact,
506 const gchar *hostname,
508 const gchar *service)
512 TpConnection *connection;
518 GValue *control_param;
519 GError *error = NULL;
521 mc = empathy_mission_control_new ();
522 account = empathy_contact_get_account (contact);
523 connection = mission_control_get_tpconnection (mc, account, NULL);
525 if (!tp_cli_connection_run_request_channel (connection, -1,
526 TP_IFACE_CHANNEL_TYPE_TUBES, TP_HANDLE_TYPE_CONTACT,
527 empathy_contact_get_handle (contact), FALSE, &object_path, &error, NULL))
529 g_clear_error (&error);
531 g_object_unref (account);
532 g_object_unref (connection);
536 empathy_debug (DEBUG_DOMAIN, "Offering a new stream tube");
538 channel = tp_channel_new (connection, object_path,
539 TP_IFACE_CHANNEL_TYPE_TUBES, TP_HANDLE_TYPE_CONTACT,
540 empathy_contact_get_handle (contact), NULL);
542 params = g_hash_table_new (g_str_hash, g_str_equal);
543 address = g_new0 (GValue, 1);
544 g_value_init (address,
545 dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_UINT,
547 g_value_take_boxed (address,
548 dbus_g_type_specialized_construct (dbus_g_type_get_struct ("GValueArray",
549 G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID)));
550 dbus_g_type_struct_set (address, 0, hostname, 1, port, G_MAXUINT);
552 /* localhost access control, variant is ignored */
553 control_param = g_new0 (GValue, 1);
554 g_value_init (control_param, G_TYPE_STRING);
556 if (!tp_cli_channel_type_tubes_run_offer_stream_tube (channel, -1,
557 service, params, TP_SOCKET_ADDRESS_TYPE_IPV4, address,
558 TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param, &id, &error, NULL))
560 empathy_debug (DEBUG_DOMAIN, "Couldn't offer tube: %s", error->message);
561 g_clear_error (&error);
564 empathy_debug (DEBUG_DOMAIN, "Stream tube id=%d offered", id);
566 g_object_unref (channel);
567 g_free (object_path);
568 g_hash_table_destroy (params);
569 g_value_reset (address);
570 g_value_reset (control_param);
572 g_free (control_param);
574 g_object_unref (account);
575 g_object_unref (connection);