]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-tube.c
Merge branch 'presets-dialog'
[empathy.git] / libempathy / empathy-tp-tube.c
1 /*
2  * Copyright (C) 2008 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
17  *
18  * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
19  *          Elliot Fairweather <elliot.fairweather@collabora.co.uk>
20  */
21
22 #include <config.h>
23
24 #include <telepathy-glib/connection.h>
25 #include <telepathy-glib/util.h>
26 #include <extensions/extensions.h>
27
28 #include "empathy-enum-types.h"
29 #include "empathy-tp-tube.h"
30 #include "empathy-utils.h"
31
32 #define DEBUG_FLAG EMPATHY_DEBUG_TP
33 #include "empathy-debug.h"
34
35 typedef struct {
36   TpSocketAddressType type;
37   EmpatyTpTubeAcceptStreamTubeCb *callback;
38   gpointer user_data;
39 } EmpathyTpTubeAcceptData;
40
41 static EmpathyTpTubeAcceptData *
42 new_empathy_tp_tube_accept_data (TpSocketAddressType type,
43   EmpatyTpTubeAcceptStreamTubeCb *callback, gpointer user_data)
44 {
45   EmpathyTpTubeAcceptData *r;
46
47   r = g_slice_new0 (EmpathyTpTubeAcceptData);
48   r->type = type;
49   r->callback = callback;
50   r->user_data = user_data;
51
52   return r;
53 }
54
55 static void
56 free_empathy_tp_tube_accept_data (gpointer data)
57 {
58   g_slice_free (EmpathyTpTubeAcceptData, data);
59 }
60
61
62 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpTube)
63 typedef struct
64 {
65   TpChannel *channel;
66   EmpTubeChannelState state;
67 } EmpathyTpTubePriv;
68
69 enum
70 {
71   PROP_0,
72   PROP_CHANNEL,
73   PROP_STATE,
74 };
75
76 enum
77 {
78   DESTROY,
79   LAST_SIGNAL
80 };
81
82 static guint signals[LAST_SIGNAL];
83
84 G_DEFINE_TYPE (EmpathyTpTube, empathy_tp_tube, G_TYPE_OBJECT)
85
86 static void
87 tp_tube_state_changed_cb (TpProxy *proxy,
88                           EmpTubeChannelState state,
89                           gpointer user_data,
90                           GObject *tube)
91 {
92   EmpathyTpTubePriv *priv = GET_PRIV (tube);
93
94   DEBUG ("Tube state changed");
95
96   priv->state = state;
97   g_object_notify (tube, "state");
98 }
99
100 static void
101 tp_tube_invalidated_cb (TpChannel     *channel,
102                         GQuark         domain,
103                         gint           code,
104                         gchar         *message,
105                         EmpathyTpTube *tube)
106 {
107   DEBUG ("Channel invalidated: %s", message);
108   g_signal_emit (tube, signals[DESTROY], 0);
109 }
110
111 static void
112 tp_tube_async_cb (TpChannel *channel,
113                   const GError *error,
114                   gpointer user_data,
115                   GObject *tube)
116 {
117   if (error)
118       DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
119 }
120
121 static void
122 tp_tube_set_property (GObject *object,
123                       guint prop_id,
124                       const GValue *value,
125                       GParamSpec *pspec)
126 {
127   EmpathyTpTubePriv *priv = GET_PRIV (object);
128
129   switch (prop_id)
130     {
131       case PROP_CHANNEL:
132         priv->channel = g_value_dup_object (value);
133         break;
134       default:
135         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
136         break;
137   }
138 }
139
140 static void
141 tp_tube_get_property (GObject *object,
142                       guint prop_id,
143                       GValue *value,
144                       GParamSpec *pspec)
145 {
146   EmpathyTpTubePriv *priv = GET_PRIV (object);
147
148   switch (prop_id)
149     {
150       case PROP_CHANNEL:
151         g_value_set_object (value, priv->channel);
152         break;
153       case PROP_STATE:
154         g_value_set_uint (value, priv->state);
155         break;
156       default:
157         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
158         break;
159   }
160 }
161
162 static GObject *
163 tp_tube_constructor (GType type,
164                      guint n_props,
165                      GObjectConstructParam *props)
166 {
167   GObject *self;
168   EmpathyTpTubePriv *priv;
169
170   self = G_OBJECT_CLASS (empathy_tp_tube_parent_class)->constructor (
171       type, n_props, props);
172   priv = GET_PRIV (self);
173
174   g_signal_connect (priv->channel, "invalidated",
175       G_CALLBACK (tp_tube_invalidated_cb), self);
176
177   emp_cli_channel_interface_tube_connect_to_tube_channel_state_changed (
178     TP_PROXY (priv->channel), tp_tube_state_changed_cb, NULL, NULL,
179     self, NULL);
180
181   return self;
182 }
183
184 static void
185 tp_tube_finalize (GObject *object)
186 {
187   EmpathyTpTubePriv *priv = GET_PRIV (object);
188
189   DEBUG ("Finalizing: %p", object);
190
191   if (priv->channel)
192     {
193       g_signal_handlers_disconnect_by_func (priv->channel,
194           tp_tube_invalidated_cb, object);
195       tp_cli_channel_call_close (priv->channel, -1, tp_tube_async_cb,
196         "closing tube", NULL, NULL);
197       g_object_unref (priv->channel);
198     }
199
200   G_OBJECT_CLASS (empathy_tp_tube_parent_class)->finalize (object);
201 }
202
203 static void
204 empathy_tp_tube_class_init (EmpathyTpTubeClass *klass)
205 {
206   GObjectClass *object_class = G_OBJECT_CLASS (klass);
207
208   object_class->constructor = tp_tube_constructor;
209   object_class->finalize = tp_tube_finalize;
210   object_class->set_property = tp_tube_set_property;
211   object_class->get_property = tp_tube_get_property;
212
213   g_object_class_install_property (object_class, PROP_CHANNEL,
214       g_param_spec_object ("channel", "channel", "channel", TP_TYPE_CHANNEL,
215       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
216
217   g_object_class_install_property (object_class, PROP_STATE,
218       g_param_spec_uint ("state", "state", "state",
219         0, NUM_EMP_TUBE_CHANNEL_STATES, 0,
220         G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_STRINGS));
221
222   signals[DESTROY] = g_signal_new ("destroy",
223       G_TYPE_FROM_CLASS (klass),
224       G_SIGNAL_RUN_LAST,
225       0, NULL, NULL,
226       g_cclosure_marshal_VOID__VOID,
227       G_TYPE_NONE, 0);
228
229   g_type_class_add_private (klass, sizeof (EmpathyTpTubePriv));
230 }
231
232 static void
233 empathy_tp_tube_init (EmpathyTpTube *tube)
234 {
235   EmpathyTpTubePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tube,
236                 EMPATHY_TYPE_TP_TUBE, EmpathyTpTubePriv);
237
238   tube->priv = priv;
239 }
240
241 EmpathyTpTube *
242 empathy_tp_tube_new (TpChannel *channel)
243 {
244   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
245
246   return g_object_new (EMPATHY_TYPE_TP_TUBE, "channel", channel,  NULL);
247 }
248
249 EmpathyTpTube *
250 empathy_tp_tube_new_stream_tube (EmpathyContact *contact,
251                                  TpSocketAddressType type,
252                                  const gchar *hostname,
253                                  guint port,
254                                  const gchar *service,
255                                  GHashTable *parameters)
256 {
257   TpConnection *connection;
258   TpChannel *channel;
259   gchar *object_path;
260   GHashTable *params;
261   GValue *address;
262   GValue *control_param;
263   EmpathyTpTube *tube = NULL;
264   GError *error = NULL;
265   GHashTable *request;
266   GHashTable *channel_properties;
267   GValue *value;
268
269   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
270   g_return_val_if_fail (hostname != NULL, NULL);
271   g_return_val_if_fail (service != NULL, NULL);
272
273   connection = empathy_contact_get_connection (contact);
274
275   request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
276       (GDestroyNotify) tp_g_value_slice_free);
277
278   /* org.freedesktop.Telepathy.Channel.ChannelType */
279   value = tp_g_value_slice_new (G_TYPE_STRING);
280   g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE);
281   g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
282
283   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
284   value = tp_g_value_slice_new (G_TYPE_UINT);
285   g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
286   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
287
288   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
289   value = tp_g_value_slice_new (G_TYPE_UINT);
290   g_value_set_uint (value, empathy_contact_get_handle (contact));
291   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
292
293   /* org.freedesktop.Telepathy.Channel.Type.StreamTube.Service */
294   value = tp_g_value_slice_new (G_TYPE_STRING);
295   g_value_set_string (value, service);
296   g_hash_table_insert (request,
297     EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE  ".Service", value);
298
299   if (!tp_cli_connection_interface_requests_run_create_channel (connection, -1,
300     request, &object_path, &channel_properties, &error, NULL))
301     {
302       DEBUG ("Error requesting channel: %s", error->message);
303       g_clear_error (&error);
304       g_object_unref (connection);
305       return NULL;
306     }
307
308   DEBUG ("Offering a new stream tube");
309
310   channel = tp_channel_new_from_properties (connection, object_path,
311       channel_properties, NULL);
312
313   tp_channel_run_until_ready (channel, NULL, NULL);
314
315   #define ADDRESS_TYPE dbus_g_type_get_struct ("GValueArray",\
316       G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID)
317   params = g_hash_table_new (g_str_hash, g_str_equal);
318   address = tp_g_value_slice_new (ADDRESS_TYPE);
319   g_value_take_boxed (address, dbus_g_type_specialized_construct (ADDRESS_TYPE));
320   dbus_g_type_struct_set (address, 0, hostname, 1, port, G_MAXUINT);
321   control_param = tp_g_value_slice_new (G_TYPE_STRING);
322
323   if (parameters == NULL)
324     /* Pass an empty dict as parameters */
325     parameters = g_hash_table_new (g_str_hash, g_str_equal);
326   else
327     g_hash_table_ref (parameters);
328
329   if (!emp_cli_channel_type_stream_tube_run_offer_stream_tube (
330         TP_PROXY(channel), -1, type, address,
331         TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param, parameters,
332         &error, NULL))
333     {
334       DEBUG ("Couldn't offer tube: %s", error->message);
335       g_clear_error (&error);
336       goto OUT;
337     }
338
339   DEBUG ("Stream tube offered");
340
341   tube = empathy_tp_tube_new (channel);
342
343 OUT:
344   g_object_unref (channel);
345   g_free (object_path);
346   g_hash_table_destroy (request);
347   g_hash_table_destroy (channel_properties);
348   tp_g_value_slice_free (address);
349   tp_g_value_slice_free (control_param);
350   g_object_unref (connection);
351   g_hash_table_unref (parameters);
352
353   return tube;
354 }
355
356 static void
357 tp_tube_accept_stream_cb (TpProxy *proxy,
358                           const GValue *address,
359                           const GError *error,
360                           gpointer user_data,
361                           GObject *weak_object)
362 {
363   EmpathyTpTube *tube = EMPATHY_TP_TUBE (weak_object);
364   EmpathyTpTubeAcceptData *data = (EmpathyTpTubeAcceptData *)user_data;
365   EmpathyTpTubeAddress eaddress;
366
367   eaddress.type = data->type;
368
369   if (error)
370     {
371       DEBUG ("Error accepting tube: %s", error->message);
372       data->callback (tube, NULL, error, data->user_data);
373       return;
374     }
375
376   switch (eaddress.type)
377     {
378       case TP_SOCKET_ADDRESS_TYPE_UNIX:
379       case TP_SOCKET_ADDRESS_TYPE_ABSTRACT_UNIX:
380         eaddress.a.socket.path = g_value_get_boxed (address);
381         break;
382      case TP_SOCKET_ADDRESS_TYPE_IPV4:
383      case TP_SOCKET_ADDRESS_TYPE_IPV6:
384         dbus_g_type_struct_get (address,
385           0, &eaddress.a.inet.hostname,
386           1, &eaddress.a.inet.port, G_MAXUINT);
387         break;
388     }
389
390    data->callback (tube, &eaddress, NULL, data->user_data);
391 }
392
393 void
394 empathy_tp_tube_accept_stream_tube (EmpathyTpTube *tube,
395   TpSocketAddressType type, EmpatyTpTubeAcceptStreamTubeCb *callback,
396   gpointer user_data)
397 {
398   EmpathyTpTubePriv *priv = GET_PRIV (tube);
399   GValue *control_param;
400   EmpathyTpTubeAcceptData *data;
401
402   g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
403
404   DEBUG ("Accepting stream tube");
405   /* FIXME allow other acls */
406   control_param = tp_g_value_slice_new (G_TYPE_STRING);
407
408   data = new_empathy_tp_tube_accept_data (type, callback, user_data);
409
410   emp_cli_channel_type_stream_tube_call_accept_stream_tube (
411      TP_PROXY (priv->channel), -1, type, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
412      control_param, tp_tube_accept_stream_cb, data,
413      free_empathy_tp_tube_accept_data, G_OBJECT (tube));
414
415   tp_g_value_slice_free (control_param);
416 }