64e45962d1eb50810d1e821b9a248c916084e179
[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   MissionControl *mc;
258   McAccount *account;
259   TpConnection *connection;
260   TpChannel *channel;
261   gchar *object_path;
262   GHashTable *params;
263   GValue *address;
264   GValue *control_param;
265   EmpathyTpTube *tube = NULL;
266   GError *error = NULL;
267   GHashTable *request;
268   GHashTable *channel_properties;
269   GValue *value;
270
271   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
272   g_return_val_if_fail (hostname != NULL, NULL);
273   g_return_val_if_fail (service != NULL, NULL);
274
275   mc = empathy_mission_control_dup_singleton ();
276   account = empathy_contact_get_account (contact);
277   connection = mission_control_get_tpconnection (mc, account, NULL);
278   g_object_unref (mc);
279
280   tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
281
282   request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
283       (GDestroyNotify) tp_g_value_slice_free);
284
285   /* org.freedesktop.Telepathy.Channel.ChannelType */
286   value = tp_g_value_slice_new (G_TYPE_STRING);
287   g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE);
288   g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
289
290   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
291   value = tp_g_value_slice_new (G_TYPE_UINT);
292   g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
293   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
294
295   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
296   value = tp_g_value_slice_new (G_TYPE_UINT);
297   g_value_set_uint (value, empathy_contact_get_handle (contact));
298   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
299
300   /* org.freedesktop.Telepathy.Channel.Type.StreamTube.Service */
301   value = tp_g_value_slice_new (G_TYPE_STRING);
302   g_value_set_string (value, service);
303   g_hash_table_insert (request,
304     EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE  ".Service", value);
305
306   if (!tp_cli_connection_interface_requests_run_create_channel (connection, -1,
307     request, &object_path, &channel_properties, &error, NULL))
308     {
309       DEBUG ("Error requesting channel: %s", error->message);
310       g_clear_error (&error);
311       g_object_unref (connection);
312       return NULL;
313     }
314
315   DEBUG ("Offering a new stream tube");
316
317   channel = tp_channel_new_from_properties (connection, object_path,
318       channel_properties, NULL);
319
320   tp_channel_run_until_ready (channel, NULL, NULL);
321
322   #define ADDRESS_TYPE dbus_g_type_get_struct ("GValueArray",\
323       G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID)
324   params = g_hash_table_new (g_str_hash, g_str_equal);
325   address = tp_g_value_slice_new (ADDRESS_TYPE);
326   g_value_take_boxed (address, dbus_g_type_specialized_construct (ADDRESS_TYPE));
327   dbus_g_type_struct_set (address, 0, hostname, 1, port, G_MAXUINT);
328   control_param = tp_g_value_slice_new (G_TYPE_STRING);
329
330   if (parameters == NULL)
331     /* Pass an empty dict as parameters */
332     parameters = g_hash_table_new (g_str_hash, g_str_equal);
333   else
334     g_hash_table_ref (parameters);
335
336   if (!emp_cli_channel_type_stream_tube_run_offer_stream_tube (
337         TP_PROXY(channel), -1, type, address,
338         TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param, parameters,
339         &error, NULL))
340     {
341       DEBUG ("Couldn't offer tube: %s", error->message);
342       g_clear_error (&error);
343       goto OUT;
344     }
345
346   DEBUG ("Stream tube offered");
347
348   tube = empathy_tp_tube_new (channel);
349
350 OUT:
351   g_object_unref (channel);
352   g_free (object_path);
353   g_hash_table_destroy (request);
354   g_hash_table_destroy (channel_properties);
355   tp_g_value_slice_free (address);
356   tp_g_value_slice_free (control_param);
357   g_object_unref (connection);
358   g_hash_table_unref (parameters);
359
360   return tube;
361 }
362
363 static void
364 tp_tube_accept_stream_cb (TpProxy *proxy,
365                           const GValue *address,
366                           const GError *error,
367                           gpointer user_data,
368                           GObject *weak_object)
369 {
370   EmpathyTpTube *tube = EMPATHY_TP_TUBE (weak_object);
371   EmpathyTpTubeAcceptData *data = (EmpathyTpTubeAcceptData *)user_data;
372   EmpathyTpTubeAddress eaddress;
373
374   eaddress.type = data->type;
375
376   if (error)
377     {
378       DEBUG ("Error accepting tube: %s", error->message);
379       data->callback (tube, NULL, error, data->user_data);
380       return;
381     }
382
383   switch (eaddress.type)
384     {
385       case TP_SOCKET_ADDRESS_TYPE_UNIX:
386       case TP_SOCKET_ADDRESS_TYPE_ABSTRACT_UNIX:
387         eaddress.a.socket.path = g_value_get_boxed (address);
388         break;
389      case TP_SOCKET_ADDRESS_TYPE_IPV4:
390      case TP_SOCKET_ADDRESS_TYPE_IPV6:
391         dbus_g_type_struct_get (address,
392           0, &eaddress.a.inet.hostname,
393           1, &eaddress.a.inet.port, G_MAXUINT);
394         break;
395     }
396
397    data->callback (tube, &eaddress, NULL, data->user_data);
398 }
399
400 void
401 empathy_tp_tube_accept_stream_tube (EmpathyTpTube *tube,
402   TpSocketAddressType type, EmpatyTpTubeAcceptStreamTubeCb *callback,
403   gpointer user_data)
404 {
405   EmpathyTpTubePriv *priv = GET_PRIV (tube);
406   GValue *control_param;
407   EmpathyTpTubeAcceptData *data;
408
409   g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
410
411   DEBUG ("Accepting stream tube");
412   /* FIXME allow other acls */
413   control_param = tp_g_value_slice_new (G_TYPE_STRING);
414
415   data = new_empathy_tp_tube_accept_data (type, callback, user_data);
416
417   emp_cli_channel_type_stream_tube_call_accept_stream_tube (
418      TP_PROXY (priv->channel), -1, type, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
419      control_param, tp_tube_accept_stream_cb, data,
420      free_empathy_tp_tube_accept_data, G_OBJECT (tube));
421
422   tp_g_value_slice_free (control_param);
423 }