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