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-enum-types.h"
29 #include "empathy-tp-tube.h"
30 #include "empathy-utils.h"
32 #define DEBUG_FLAG EMPATHY_DEBUG_TP
33 #include "empathy-debug.h"
35 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpTube)
43 GHashTable *parameters;
45 EmpathyContact *initiator_contact;
46 EmpathyContactFactory *factory;
60 PROP_INITIATOR_CONTACT
69 static guint signals[LAST_SIGNAL];
71 G_DEFINE_TYPE (EmpathyTpTube, empathy_tp_tube, G_TYPE_OBJECT)
74 tp_tube_state_changed_cb (TpChannel *channel,
80 EmpathyTpTubePriv *priv = GET_PRIV (tube);
85 DEBUG ("Tube state changed");
88 g_object_notify (tube, "state");
92 tp_tube_invalidated_cb (TpChannel *channel,
98 DEBUG ("Channel invalidated: %s", message);
99 g_signal_emit (tube, signals[DESTROY], 0);
103 tp_tube_closed_cb (TpChannel *channel,
108 EmpathyTpTubePriv *priv = GET_PRIV (tube);
113 DEBUG ("Tube closed");
114 g_signal_emit (tube, signals[DESTROY], 0);
118 tp_tube_async_cb (TpChannel *channel,
124 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
128 tp_tube_set_property (GObject *object,
133 EmpathyTpTubePriv *priv = GET_PRIV (object);
138 priv->channel = g_value_dup_object (value);
141 priv->id = g_value_get_uint (value);
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150 tp_tube_get_property (GObject *object,
155 EmpathyTpTubePriv *priv = GET_PRIV (object);
160 g_value_set_object (value, priv->channel);
163 g_value_set_uint (value, priv->id);
166 g_value_set_uint (value, priv->initiator);
169 g_value_set_uint (value, priv->type);
172 g_value_set_string (value, priv->service);
174 case PROP_PARAMETERS:
175 g_value_set_boxed (value, priv->parameters);
178 g_value_set_uint (value, priv->state);
180 case PROP_INITIATOR_CONTACT:
181 g_value_set_object (value, priv->initiator_contact);
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
190 tp_tube_constructor (GType type,
192 GObjectConstructParam *props)
195 EmpathyTpTubePriv *priv;
198 GError *error = NULL;
200 self = G_OBJECT_CLASS (empathy_tp_tube_parent_class)->constructor (
201 type, n_props, props);
202 priv = GET_PRIV (self);
204 g_signal_connect (priv->channel, "invalidated",
205 G_CALLBACK (tp_tube_invalidated_cb), self);
206 tp_cli_channel_type_tubes_connect_to_tube_closed (priv->channel,
207 tp_tube_closed_cb, NULL, NULL, self, NULL);
208 tp_cli_channel_type_tubes_connect_to_tube_state_changed (priv->channel,
209 tp_tube_state_changed_cb, NULL, NULL, self, NULL);
211 /* FIXME: It is absolutely not opimized to list all tubes to get information
212 * about our tube, but we don't really have the choice to avoid races. */
213 if (!tp_cli_channel_type_tubes_run_list_tubes (priv->channel, -1, &tubes,
216 DEBUG ("Couldn't list tubes: %s", error->message);
217 g_clear_error (&error);
221 for (i = 0; i < tubes->len; i++)
226 values = g_ptr_array_index (tubes, i);
227 id = g_value_get_uint (g_value_array_get_nth (values, 0));
231 TpConnection *connection;
235 g_object_get (priv->channel, "connection", &connection, NULL);
236 mc = empathy_mission_control_new ();
237 account = mission_control_get_account_for_tpconnection (mc,
240 priv->initiator = g_value_get_uint (g_value_array_get_nth (values, 1));
241 priv->type = g_value_get_uint (g_value_array_get_nth (values, 2));
242 priv->service = g_value_dup_string (g_value_array_get_nth (values, 3));
243 priv->parameters = g_value_dup_boxed (g_value_array_get_nth (values, 4));
244 priv->state = g_value_get_uint (g_value_array_get_nth (values, 5));
245 priv->initiator_contact = empathy_contact_factory_get_from_handle (
246 priv->factory, account, priv->initiator);
248 g_object_unref (connection);
250 g_object_unref (account);
253 g_value_array_free (values);
255 g_ptr_array_free (tubes, TRUE);
261 tp_tube_finalize (GObject *object)
263 EmpathyTpTubePriv *priv = GET_PRIV (object);
265 DEBUG ("Finalizing: %p", object);
269 g_signal_handlers_disconnect_by_func (priv->channel,
270 tp_tube_invalidated_cb, object);
271 tp_cli_channel_type_tubes_call_close_tube (priv->channel, -1, priv->id,
272 tp_tube_async_cb, "closing tube", NULL, NULL);
273 g_object_unref (priv->channel);
275 if (priv->initiator_contact)
276 g_object_unref (priv->initiator_contact);
278 g_object_unref (priv->factory);
280 g_free (priv->service);
281 g_hash_table_destroy (priv->parameters);
283 G_OBJECT_CLASS (empathy_tp_tube_parent_class)->finalize (object);
287 empathy_tp_tube_class_init (EmpathyTpTubeClass *klass)
289 GObjectClass *object_class = G_OBJECT_CLASS (klass);
291 object_class->constructor = tp_tube_constructor;
292 object_class->finalize = tp_tube_finalize;
293 object_class->set_property = tp_tube_set_property;
294 object_class->get_property = tp_tube_get_property;
296 g_object_class_install_property (object_class, PROP_CHANNEL,
297 g_param_spec_object ("channel", "channel", "channel", TP_TYPE_CHANNEL,
298 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME |
299 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
301 g_object_class_install_property (object_class, PROP_ID,
302 g_param_spec_uint ("id", "id", "id", 0, G_MAXUINT, 0,
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_INITIATOR,
307 g_param_spec_uint ("initiator", "initiator", "initiator",
308 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_NAME |
309 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
311 g_object_class_install_property (object_class, PROP_TYPE,
312 g_param_spec_uint ("type", "type", "type", 0, G_MAXUINT, 0,
313 G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
314 G_PARAM_STATIC_BLURB));
316 g_object_class_install_property (object_class, PROP_SERVICE,
317 g_param_spec_string ("service", "service", "service", NULL,
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_PARAMETERS,
322 g_param_spec_boxed ("parameters", "parameters", "parameters",
323 G_TYPE_HASH_TABLE, G_PARAM_READABLE | G_PARAM_STATIC_NAME |
324 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
326 g_object_class_install_property (object_class, PROP_STATE,
327 g_param_spec_uint ("state", "state", "state", 0, G_MAXUINT, 0,
328 G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
329 G_PARAM_STATIC_BLURB));
331 g_object_class_install_property (object_class, PROP_INITIATOR_CONTACT,
332 g_param_spec_object ("initiator-contact", "initiator contact",
333 "initiator contact", EMPATHY_TYPE_CONTACT, G_PARAM_READABLE |
334 G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
336 signals[DESTROY] = g_signal_new ("destroy",
337 G_TYPE_FROM_CLASS (klass),
340 g_cclosure_marshal_VOID__VOID,
343 g_type_class_add_private (klass, sizeof (EmpathyTpTubePriv));
347 empathy_tp_tube_init (EmpathyTpTube *tube)
349 EmpathyTpTubePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tube,
350 EMPATHY_TYPE_TP_TUBE, EmpathyTpTubePriv);
354 priv->factory = empathy_contact_factory_new ();
358 empathy_tp_tube_new (TpChannel *channel, guint tube_id)
360 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
362 return g_object_new (EMPATHY_TYPE_TP_TUBE,
363 "channel", channel, "id", tube_id, NULL);
367 empathy_tp_tube_new_stream_tube (EmpathyContact *contact,
368 TpSocketAddressType type,
369 const gchar *hostname,
371 const gchar *service)
375 TpConnection *connection;
381 GValue *control_param;
382 EmpathyTpTube *tube = NULL;
383 GError *error = NULL;
385 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
386 g_return_val_if_fail (hostname != NULL, NULL);
387 g_return_val_if_fail (service != NULL, NULL);
389 mc = empathy_mission_control_new ();
390 account = empathy_contact_get_account (contact);
391 connection = mission_control_get_tpconnection (mc, account, NULL);
394 if (!tp_cli_connection_run_request_channel (connection, -1,
395 TP_IFACE_CHANNEL_TYPE_TUBES, TP_HANDLE_TYPE_CONTACT,
396 empathy_contact_get_handle (contact), FALSE, &object_path, &error, NULL))
398 DEBUG ("Error requesting channel: %s", error->message);
399 g_clear_error (&error);
400 g_object_unref (connection);
404 DEBUG ("Offering a new stream tube");
406 channel = tp_channel_new (connection, object_path,
407 TP_IFACE_CHANNEL_TYPE_TUBES, TP_HANDLE_TYPE_CONTACT,
408 empathy_contact_get_handle (contact), NULL);
410 #define ADDRESS_TYPE dbus_g_type_get_struct ("GValueArray",\
411 G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID)
412 params = g_hash_table_new (g_str_hash, g_str_equal);
413 address = tp_g_value_slice_new (ADDRESS_TYPE);
414 g_value_take_boxed (address, dbus_g_type_specialized_construct (ADDRESS_TYPE));
415 dbus_g_type_struct_set (address, 0, hostname, 1, port, G_MAXUINT);
416 control_param = tp_g_value_slice_new (G_TYPE_STRING);
418 if (!tp_cli_channel_type_tubes_run_offer_stream_tube (channel, -1,
419 service, params, type, address,
420 TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param, &id, &error, NULL))
422 DEBUG ("Couldn't offer tube: %s", error->message);
423 g_clear_error (&error);
427 DEBUG ("Stream tube id=%d offered", id);
429 tube = empathy_tp_tube_new (channel, id);
432 g_object_unref (channel);
433 g_free (object_path);
434 g_hash_table_destroy (params);
435 tp_g_value_slice_free (address);
436 tp_g_value_slice_free (control_param);
437 g_object_unref (connection);
443 tp_tube_accept_stream_cb (TpChannel *proxy,
444 const GValue *address,
447 GObject *weak_object)
450 DEBUG ("Error accepting tube: %s", error->message);
454 empathy_tp_tube_accept_stream_tube (EmpathyTpTube *tube,
455 TpSocketAddressType type)
457 EmpathyTpTubePriv *priv = GET_PRIV (tube);
458 GValue *control_param;
460 g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
462 DEBUG ("Accepting stream tube - id: %d", priv->id);
464 control_param = tp_g_value_slice_new (G_TYPE_STRING);
465 tp_cli_channel_type_tubes_call_accept_stream_tube (priv->channel, -1, priv->id,
466 type, TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param,
467 tp_tube_accept_stream_cb, NULL, NULL, G_OBJECT (tube));
469 tp_g_value_slice_free (control_param);
473 empathy_tp_tube_get_socket (EmpathyTpTube *tube,
477 EmpathyTpTubePriv *priv = GET_PRIV (tube);
480 gchar *ret_hostname = NULL;
482 GError *error = NULL;
484 g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
485 g_return_if_fail (hostname != NULL || port != NULL);
487 DEBUG ("Getting stream tube socket address");
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 DEBUG ("Couldn't get socket address: %s", error->message);
493 g_clear_error (&error);
497 switch (address_type)
499 case TP_SOCKET_ADDRESS_TYPE_UNIX:
500 case TP_SOCKET_ADDRESS_TYPE_ABSTRACT_UNIX:
501 dbus_g_type_struct_get (address, 0, &ret_hostname, G_MAXUINT);
503 case TP_SOCKET_ADDRESS_TYPE_IPV4:
504 case TP_SOCKET_ADDRESS_TYPE_IPV6:
505 dbus_g_type_struct_get (address, 0, &ret_hostname, 1, &ret_port, G_MAXUINT);
510 *hostname = g_strdup (ret_hostname);
515 g_boxed_free (G_TYPE_VALUE, address);