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>
26 #include <extensions/extensions.h>
28 #include "empathy-contact-factory.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 EmpatyTpTubeAcceptStreamTubeCb *callback;
40 } EmpathyTpTubeAcceptData;
42 static EmpathyTpTubeAcceptData *
43 new_empathy_tp_tube_accept_data (TpSocketAddressType type,
44 EmpatyTpTubeAcceptStreamTubeCb *callback, gpointer user_data)
46 EmpathyTpTubeAcceptData *r;
48 r = g_slice_new0 (EmpathyTpTubeAcceptData);
50 r->callback = callback;
51 r->user_data = user_data;
57 free_empathy_tp_tube_accept_data (gpointer data)
59 g_slice_free (EmpathyTpTubeAcceptData, data);
63 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpTube)
68 /* FIXME readd support for parameters.. */
69 GHashTable *parameters;
70 EmpTubeChannelState state;
71 EmpathyContact *initiator_contact;
72 EmpathyContactFactory *factory;
88 static guint signals[LAST_SIGNAL];
90 G_DEFINE_TYPE (EmpathyTpTube, empathy_tp_tube, G_TYPE_OBJECT)
93 tp_tube_state_changed_cb (TpProxy *proxy,
94 EmpTubeChannelState state,
98 EmpathyTpTubePriv *priv = GET_PRIV (tube);
100 DEBUG ("Tube state changed");
103 g_object_notify (tube, "state");
107 tp_tube_invalidated_cb (TpChannel *channel,
113 DEBUG ("Channel invalidated: %s", message);
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 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147 tp_tube_get_property (GObject *object,
152 EmpathyTpTubePriv *priv = GET_PRIV (object);
157 g_value_set_object (value, priv->channel);
160 g_value_set_uint (value, priv->state);
163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169 tp_tube_constructor (GType type,
171 GObjectConstructParam *props)
174 EmpathyTpTubePriv *priv;
176 self = G_OBJECT_CLASS (empathy_tp_tube_parent_class)->constructor (
177 type, n_props, props);
178 priv = GET_PRIV (self);
180 g_signal_connect (priv->channel, "invalidated",
181 G_CALLBACK (tp_tube_invalidated_cb), self);
183 emp_cli_channel_interface_tube_connect_to_tube_channel_state_changed (
184 TP_PROXY (priv->channel), tp_tube_state_changed_cb, NULL, NULL,
191 tp_tube_finalize (GObject *object)
193 EmpathyTpTubePriv *priv = GET_PRIV (object);
195 DEBUG ("Finalizing: %p", object);
199 g_signal_handlers_disconnect_by_func (priv->channel,
200 tp_tube_invalidated_cb, object);
201 tp_cli_channel_call_close (priv->channel, -1, tp_tube_async_cb,
202 "closing tube", NULL, NULL);
203 g_object_unref (priv->channel);
205 if (priv->initiator_contact)
206 g_object_unref (priv->initiator_contact);
208 g_object_unref (priv->factory);
210 g_free (priv->service);
212 if (priv->parameters != NULL)
213 g_hash_table_destroy (priv->parameters);
215 G_OBJECT_CLASS (empathy_tp_tube_parent_class)->finalize (object);
219 empathy_tp_tube_class_init (EmpathyTpTubeClass *klass)
221 GObjectClass *object_class = G_OBJECT_CLASS (klass);
223 object_class->constructor = tp_tube_constructor;
224 object_class->finalize = tp_tube_finalize;
225 object_class->set_property = tp_tube_set_property;
226 object_class->get_property = tp_tube_get_property;
228 g_object_class_install_property (object_class, PROP_CHANNEL,
229 g_param_spec_object ("channel", "channel", "channel", TP_TYPE_CHANNEL,
230 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
232 g_object_class_install_property (object_class, PROP_STATE,
233 g_param_spec_uint ("state", "state", "state",
234 0, NUM_EMP_TUBE_CHANNEL_STATES, 0,
235 G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_STRINGS));
237 signals[DESTROY] = g_signal_new ("destroy",
238 G_TYPE_FROM_CLASS (klass),
241 g_cclosure_marshal_VOID__VOID,
244 g_type_class_add_private (klass, sizeof (EmpathyTpTubePriv));
248 empathy_tp_tube_init (EmpathyTpTube *tube)
250 EmpathyTpTubePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tube,
251 EMPATHY_TYPE_TP_TUBE, EmpathyTpTubePriv);
255 priv->factory = empathy_contact_factory_dup_singleton ();
259 empathy_tp_tube_new (TpChannel *channel)
261 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
263 return g_object_new (EMPATHY_TYPE_TP_TUBE, "channel", channel, NULL);
267 empathy_tp_tube_new_stream_tube (EmpathyContact *contact,
268 TpSocketAddressType type,
269 const gchar *hostname,
271 const gchar *service,
272 GHashTable *parameters)
276 TpConnection *connection;
281 GValue *control_param;
282 EmpathyTpTube *tube = NULL;
283 GError *error = NULL;
285 GHashTable *channel_properties;
288 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
289 g_return_val_if_fail (hostname != NULL, NULL);
290 g_return_val_if_fail (service != NULL, NULL);
292 mc = empathy_mission_control_dup_singleton ();
293 account = empathy_contact_get_account (contact);
294 connection = mission_control_get_tpconnection (mc, account, NULL);
297 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
299 request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
300 (GDestroyNotify) tp_g_value_slice_free);
302 /* org.freedesktop.Telepathy.Channel.ChannelType */
303 value = tp_g_value_slice_new (G_TYPE_STRING);
304 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE);
305 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
307 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
308 value = tp_g_value_slice_new (G_TYPE_UINT);
309 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
310 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
312 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
313 value = tp_g_value_slice_new (G_TYPE_UINT);
314 g_value_set_uint (value, empathy_contact_get_handle (contact));
315 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
317 /* org.freedesktop.Telepathy.Channel.Type.StreamTube.Service */
318 value = tp_g_value_slice_new (G_TYPE_STRING);
319 g_value_set_string (value, service);
320 g_hash_table_insert (request,
321 EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE ".Service", value);
323 if (!tp_cli_connection_interface_requests_run_create_channel (connection, -1,
324 request, &object_path, &channel_properties, &error, NULL))
326 DEBUG ("Error requesting channel: %s", error->message);
327 g_clear_error (&error);
328 g_object_unref (connection);
332 DEBUG ("Offering a new stream tube");
334 channel = tp_channel_new_from_properties (connection, object_path,
335 channel_properties, NULL);
337 tp_channel_run_until_ready (channel, NULL, NULL);
339 #define ADDRESS_TYPE dbus_g_type_get_struct ("GValueArray",\
340 G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID)
341 params = g_hash_table_new (g_str_hash, g_str_equal);
342 address = tp_g_value_slice_new (ADDRESS_TYPE);
343 g_value_take_boxed (address, dbus_g_type_specialized_construct (ADDRESS_TYPE));
344 dbus_g_type_struct_set (address, 0, hostname, 1, port, G_MAXUINT);
345 control_param = tp_g_value_slice_new (G_TYPE_STRING);
347 if (parameters == NULL)
348 /* Pass an empty dict as parameters */
349 parameters = g_hash_table_new (g_str_hash, g_str_equal);
351 g_hash_table_ref (parameters);
353 if (!emp_cli_channel_type_stream_tube_run_offer_stream_tube (
354 TP_PROXY(channel), -1, type, address,
355 TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param, parameters,
358 DEBUG ("Couldn't offer tube: %s", error->message);
359 g_clear_error (&error);
363 DEBUG ("Stream tube offered");
365 tube = empathy_tp_tube_new (channel);
368 g_object_unref (channel);
369 g_free (object_path);
370 g_hash_table_destroy (request);
371 g_hash_table_destroy (channel_properties);
372 tp_g_value_slice_free (address);
373 tp_g_value_slice_free (control_param);
374 g_object_unref (connection);
375 g_hash_table_unref (parameters);
381 tp_tube_accept_stream_cb (TpProxy *proxy,
382 const GValue *address,
385 GObject *weak_object)
387 EmpathyTpTube *tube = EMPATHY_TP_TUBE (weak_object);
388 EmpathyTpTubeAcceptData *data = (EmpathyTpTubeAcceptData *)user_data;
389 EmpathyTpTubeAddress eaddress;
391 eaddress.type = data->type;
395 DEBUG ("Error accepting tube: %s", error->message);
396 data->callback (tube, NULL, error, data->user_data);
400 switch (eaddress.type)
402 case TP_SOCKET_ADDRESS_TYPE_UNIX:
403 case TP_SOCKET_ADDRESS_TYPE_ABSTRACT_UNIX:
404 eaddress.a.socket.path = g_value_get_boxed (address);
406 case TP_SOCKET_ADDRESS_TYPE_IPV4:
407 case TP_SOCKET_ADDRESS_TYPE_IPV6:
408 dbus_g_type_struct_get (address,
409 0, &eaddress.a.inet.hostname,
410 1, &eaddress.a.inet.port, G_MAXUINT);
414 data->callback (tube, &eaddress, NULL, data->user_data);
418 empathy_tp_tube_accept_stream_tube (EmpathyTpTube *tube,
419 TpSocketAddressType type, EmpatyTpTubeAcceptStreamTubeCb *callback,
422 EmpathyTpTubePriv *priv = GET_PRIV (tube);
423 GValue *control_param;
424 EmpathyTpTubeAcceptData *data;
426 g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
428 DEBUG ("Accepting stream tube");
429 /* FIXME allow other acls */
430 control_param = tp_g_value_slice_new (G_TYPE_STRING);
432 data = new_empathy_tp_tube_accept_data (type, callback, user_data);
434 emp_cli_channel_type_stream_tube_call_accept_stream_tube (
435 TP_PROXY (priv->channel), -1, type, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
436 control_param, tp_tube_accept_stream_cb, data,
437 free_empathy_tp_tube_accept_data, G_OBJECT (tube));
439 tp_g_value_slice_free (control_param);