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-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"
36 TpSocketAddressType type;
37 EmpatyTpTubeAcceptStreamTubeCb *callback;
39 } EmpathyTpTubeAcceptData;
41 static EmpathyTpTubeAcceptData *
42 new_empathy_tp_tube_accept_data (TpSocketAddressType type,
43 EmpatyTpTubeAcceptStreamTubeCb *callback, gpointer user_data)
45 EmpathyTpTubeAcceptData *r;
47 r = g_slice_new0 (EmpathyTpTubeAcceptData);
49 r->callback = callback;
50 r->user_data = user_data;
56 free_empathy_tp_tube_accept_data (gpointer data)
58 g_slice_free (EmpathyTpTubeAcceptData, data);
62 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpTube)
67 /* FIXME readd support for parameters.. */
68 GHashTable *parameters;
69 EmpTubeChannelState state;
85 static guint signals[LAST_SIGNAL];
87 G_DEFINE_TYPE (EmpathyTpTube, empathy_tp_tube, G_TYPE_OBJECT)
90 tp_tube_state_changed_cb (TpProxy *proxy,
91 EmpTubeChannelState state,
95 EmpathyTpTubePriv *priv = GET_PRIV (tube);
97 DEBUG ("Tube state changed");
100 g_object_notify (tube, "state");
104 tp_tube_invalidated_cb (TpChannel *channel,
110 DEBUG ("Channel invalidated: %s", message);
111 g_signal_emit (tube, signals[DESTROY], 0);
115 tp_tube_async_cb (TpChannel *channel,
121 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
125 tp_tube_set_property (GObject *object,
130 EmpathyTpTubePriv *priv = GET_PRIV (object);
135 priv->channel = g_value_dup_object (value);
138 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
144 tp_tube_get_property (GObject *object,
149 EmpathyTpTubePriv *priv = GET_PRIV (object);
154 g_value_set_object (value, priv->channel);
157 g_value_set_uint (value, priv->state);
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166 tp_tube_constructor (GType type,
168 GObjectConstructParam *props)
171 EmpathyTpTubePriv *priv;
173 self = G_OBJECT_CLASS (empathy_tp_tube_parent_class)->constructor (
174 type, n_props, props);
175 priv = GET_PRIV (self);
177 g_signal_connect (priv->channel, "invalidated",
178 G_CALLBACK (tp_tube_invalidated_cb), self);
180 emp_cli_channel_interface_tube_connect_to_tube_channel_state_changed (
181 TP_PROXY (priv->channel), tp_tube_state_changed_cb, NULL, NULL,
188 tp_tube_finalize (GObject *object)
190 EmpathyTpTubePriv *priv = GET_PRIV (object);
192 DEBUG ("Finalizing: %p", object);
196 g_signal_handlers_disconnect_by_func (priv->channel,
197 tp_tube_invalidated_cb, object);
198 tp_cli_channel_call_close (priv->channel, -1, tp_tube_async_cb,
199 "closing tube", NULL, NULL);
200 g_object_unref (priv->channel);
203 g_free (priv->service);
205 if (priv->parameters != NULL)
206 g_hash_table_destroy (priv->parameters);
208 G_OBJECT_CLASS (empathy_tp_tube_parent_class)->finalize (object);
212 empathy_tp_tube_class_init (EmpathyTpTubeClass *klass)
214 GObjectClass *object_class = G_OBJECT_CLASS (klass);
216 object_class->constructor = tp_tube_constructor;
217 object_class->finalize = tp_tube_finalize;
218 object_class->set_property = tp_tube_set_property;
219 object_class->get_property = tp_tube_get_property;
221 g_object_class_install_property (object_class, PROP_CHANNEL,
222 g_param_spec_object ("channel", "channel", "channel", TP_TYPE_CHANNEL,
223 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
225 g_object_class_install_property (object_class, PROP_STATE,
226 g_param_spec_uint ("state", "state", "state",
227 0, NUM_EMP_TUBE_CHANNEL_STATES, 0,
228 G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_STRINGS));
230 signals[DESTROY] = g_signal_new ("destroy",
231 G_TYPE_FROM_CLASS (klass),
234 g_cclosure_marshal_VOID__VOID,
237 g_type_class_add_private (klass, sizeof (EmpathyTpTubePriv));
241 empathy_tp_tube_init (EmpathyTpTube *tube)
243 EmpathyTpTubePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tube,
244 EMPATHY_TYPE_TP_TUBE, EmpathyTpTubePriv);
250 empathy_tp_tube_new (TpChannel *channel)
252 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
254 return g_object_new (EMPATHY_TYPE_TP_TUBE, "channel", channel, NULL);
258 empathy_tp_tube_new_stream_tube (EmpathyContact *contact,
259 TpSocketAddressType type,
260 const gchar *hostname,
262 const gchar *service,
263 GHashTable *parameters)
267 TpConnection *connection;
272 GValue *control_param;
273 EmpathyTpTube *tube = NULL;
274 GError *error = NULL;
276 GHashTable *channel_properties;
279 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
280 g_return_val_if_fail (hostname != NULL, NULL);
281 g_return_val_if_fail (service != NULL, NULL);
283 mc = empathy_mission_control_dup_singleton ();
284 account = empathy_contact_get_account (contact);
285 connection = mission_control_get_tpconnection (mc, account, NULL);
288 tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
290 request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
291 (GDestroyNotify) tp_g_value_slice_free);
293 /* org.freedesktop.Telepathy.Channel.ChannelType */
294 value = tp_g_value_slice_new (G_TYPE_STRING);
295 g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE);
296 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
298 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
299 value = tp_g_value_slice_new (G_TYPE_UINT);
300 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
301 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
303 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
304 value = tp_g_value_slice_new (G_TYPE_UINT);
305 g_value_set_uint (value, empathy_contact_get_handle (contact));
306 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
308 /* org.freedesktop.Telepathy.Channel.Type.StreamTube.Service */
309 value = tp_g_value_slice_new (G_TYPE_STRING);
310 g_value_set_string (value, service);
311 g_hash_table_insert (request,
312 EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE ".Service", value);
314 if (!tp_cli_connection_interface_requests_run_create_channel (connection, -1,
315 request, &object_path, &channel_properties, &error, NULL))
317 DEBUG ("Error requesting channel: %s", error->message);
318 g_clear_error (&error);
319 g_object_unref (connection);
323 DEBUG ("Offering a new stream tube");
325 channel = tp_channel_new_from_properties (connection, object_path,
326 channel_properties, NULL);
328 tp_channel_run_until_ready (channel, NULL, NULL);
330 #define ADDRESS_TYPE dbus_g_type_get_struct ("GValueArray",\
331 G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID)
332 params = g_hash_table_new (g_str_hash, g_str_equal);
333 address = tp_g_value_slice_new (ADDRESS_TYPE);
334 g_value_take_boxed (address, dbus_g_type_specialized_construct (ADDRESS_TYPE));
335 dbus_g_type_struct_set (address, 0, hostname, 1, port, G_MAXUINT);
336 control_param = tp_g_value_slice_new (G_TYPE_STRING);
338 if (parameters == NULL)
339 /* Pass an empty dict as parameters */
340 parameters = g_hash_table_new (g_str_hash, g_str_equal);
342 g_hash_table_ref (parameters);
344 if (!emp_cli_channel_type_stream_tube_run_offer_stream_tube (
345 TP_PROXY(channel), -1, type, address,
346 TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param, parameters,
349 DEBUG ("Couldn't offer tube: %s", error->message);
350 g_clear_error (&error);
354 DEBUG ("Stream tube offered");
356 tube = empathy_tp_tube_new (channel);
359 g_object_unref (channel);
360 g_free (object_path);
361 g_hash_table_destroy (request);
362 g_hash_table_destroy (channel_properties);
363 tp_g_value_slice_free (address);
364 tp_g_value_slice_free (control_param);
365 g_object_unref (connection);
366 g_hash_table_unref (parameters);
372 tp_tube_accept_stream_cb (TpProxy *proxy,
373 const GValue *address,
376 GObject *weak_object)
378 EmpathyTpTube *tube = EMPATHY_TP_TUBE (weak_object);
379 EmpathyTpTubeAcceptData *data = (EmpathyTpTubeAcceptData *)user_data;
380 EmpathyTpTubeAddress eaddress;
382 eaddress.type = data->type;
386 DEBUG ("Error accepting tube: %s", error->message);
387 data->callback (tube, NULL, error, data->user_data);
391 switch (eaddress.type)
393 case TP_SOCKET_ADDRESS_TYPE_UNIX:
394 case TP_SOCKET_ADDRESS_TYPE_ABSTRACT_UNIX:
395 eaddress.a.socket.path = g_value_get_boxed (address);
397 case TP_SOCKET_ADDRESS_TYPE_IPV4:
398 case TP_SOCKET_ADDRESS_TYPE_IPV6:
399 dbus_g_type_struct_get (address,
400 0, &eaddress.a.inet.hostname,
401 1, &eaddress.a.inet.port, G_MAXUINT);
405 data->callback (tube, &eaddress, NULL, data->user_data);
409 empathy_tp_tube_accept_stream_tube (EmpathyTpTube *tube,
410 TpSocketAddressType type, EmpatyTpTubeAcceptStreamTubeCb *callback,
413 EmpathyTpTubePriv *priv = GET_PRIV (tube);
414 GValue *control_param;
415 EmpathyTpTubeAcceptData *data;
417 g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
419 DEBUG ("Accepting stream tube");
420 /* FIXME allow other acls */
421 control_param = tp_g_value_slice_new (G_TYPE_STRING);
423 data = new_empathy_tp_tube_accept_data (type, callback, user_data);
425 emp_cli_channel_type_stream_tube_call_accept_stream_tube (
426 TP_PROXY (priv->channel), -1, type, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
427 control_param, tp_tube_accept_stream_cb, data,
428 free_empathy_tp_tube_accept_data, G_OBJECT (tube));
430 tp_g_value_slice_free (control_param);