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