]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-tube.c
tp-tube: remove priv->initiator_contact and priv->factory as they are not used
[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   gchar *service;
67   /* FIXME readd support for parameters.. */
68   GHashTable *parameters;
69   EmpTubeChannelState state;
70 } EmpathyTpTubePriv;
71
72 enum
73 {
74   PROP_0,
75   PROP_CHANNEL,
76   PROP_STATE,
77 };
78
79 enum
80 {
81   DESTROY,
82   LAST_SIGNAL
83 };
84
85 static guint signals[LAST_SIGNAL];
86
87 G_DEFINE_TYPE (EmpathyTpTube, empathy_tp_tube, G_TYPE_OBJECT)
88
89 static void
90 tp_tube_state_changed_cb (TpProxy *proxy,
91                           EmpTubeChannelState state,
92                           gpointer user_data,
93                           GObject *tube)
94 {
95   EmpathyTpTubePriv *priv = GET_PRIV (tube);
96
97   DEBUG ("Tube state changed");
98
99   priv->state = state;
100   g_object_notify (tube, "state");
101 }
102
103 static void
104 tp_tube_invalidated_cb (TpChannel     *channel,
105                         GQuark         domain,
106                         gint           code,
107                         gchar         *message,
108                         EmpathyTpTube *tube)
109 {
110   DEBUG ("Channel invalidated: %s", message);
111   g_signal_emit (tube, signals[DESTROY], 0);
112 }
113
114 static void
115 tp_tube_async_cb (TpChannel *channel,
116                   const GError *error,
117                   gpointer user_data,
118                   GObject *tube)
119 {
120   if (error)
121       DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
122 }
123
124 static void
125 tp_tube_set_property (GObject *object,
126                       guint prop_id,
127                       const GValue *value,
128                       GParamSpec *pspec)
129 {
130   EmpathyTpTubePriv *priv = GET_PRIV (object);
131
132   switch (prop_id)
133     {
134       case PROP_CHANNEL:
135         priv->channel = g_value_dup_object (value);
136         break;
137       default:
138         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
139         break;
140   }
141 }
142
143 static void
144 tp_tube_get_property (GObject *object,
145                       guint prop_id,
146                       GValue *value,
147                       GParamSpec *pspec)
148 {
149   EmpathyTpTubePriv *priv = GET_PRIV (object);
150
151   switch (prop_id)
152     {
153       case PROP_CHANNEL:
154         g_value_set_object (value, priv->channel);
155         break;
156       case PROP_STATE:
157         g_value_set_uint (value, priv->state);
158         break;
159       default:
160         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161         break;
162   }
163 }
164
165 static GObject *
166 tp_tube_constructor (GType type,
167                      guint n_props,
168                      GObjectConstructParam *props)
169 {
170   GObject *self;
171   EmpathyTpTubePriv *priv;
172
173   self = G_OBJECT_CLASS (empathy_tp_tube_parent_class)->constructor (
174       type, n_props, props);
175   priv = GET_PRIV (self);
176
177   g_signal_connect (priv->channel, "invalidated",
178       G_CALLBACK (tp_tube_invalidated_cb), self);
179
180   emp_cli_channel_interface_tube_connect_to_tube_channel_state_changed (
181     TP_PROXY (priv->channel), tp_tube_state_changed_cb, NULL, NULL,
182     self, NULL);
183
184   return self;
185 }
186
187 static void
188 tp_tube_finalize (GObject *object)
189 {
190   EmpathyTpTubePriv *priv = GET_PRIV (object);
191
192   DEBUG ("Finalizing: %p", object);
193
194   if (priv->channel)
195     {
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);
201     }
202
203   g_free (priv->service);
204
205   if (priv->parameters != NULL)
206   g_hash_table_destroy (priv->parameters);
207
208   G_OBJECT_CLASS (empathy_tp_tube_parent_class)->finalize (object);
209 }
210
211 static void
212 empathy_tp_tube_class_init (EmpathyTpTubeClass *klass)
213 {
214   GObjectClass *object_class = G_OBJECT_CLASS (klass);
215
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;
220
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));
224
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));
229
230   signals[DESTROY] = g_signal_new ("destroy",
231       G_TYPE_FROM_CLASS (klass),
232       G_SIGNAL_RUN_LAST,
233       0, NULL, NULL,
234       g_cclosure_marshal_VOID__VOID,
235       G_TYPE_NONE, 0);
236
237   g_type_class_add_private (klass, sizeof (EmpathyTpTubePriv));
238 }
239
240 static void
241 empathy_tp_tube_init (EmpathyTpTube *tube)
242 {
243   EmpathyTpTubePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tube,
244                 EMPATHY_TYPE_TP_TUBE, EmpathyTpTubePriv);
245
246   tube->priv = priv;
247 }
248
249 EmpathyTpTube *
250 empathy_tp_tube_new (TpChannel *channel)
251 {
252   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
253
254   return g_object_new (EMPATHY_TYPE_TP_TUBE, "channel", channel,  NULL);
255 }
256
257 EmpathyTpTube *
258 empathy_tp_tube_new_stream_tube (EmpathyContact *contact,
259                                  TpSocketAddressType type,
260                                  const gchar *hostname,
261                                  guint port,
262                                  const gchar *service,
263                                  GHashTable *parameters)
264 {
265   MissionControl *mc;
266   McAccount *account;
267   TpConnection *connection;
268   TpChannel *channel;
269   gchar *object_path;
270   GHashTable *params;
271   GValue *address;
272   GValue *control_param;
273   EmpathyTpTube *tube = NULL;
274   GError *error = NULL;
275   GHashTable *request;
276   GHashTable *channel_properties;
277   GValue *value;
278
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);
282
283   mc = empathy_mission_control_dup_singleton ();
284   account = empathy_contact_get_account (contact);
285   connection = mission_control_get_tpconnection (mc, account, NULL);
286   g_object_unref (mc);
287
288   tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
289
290   request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
291       (GDestroyNotify) tp_g_value_slice_free);
292
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);
297
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);
302
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);
307
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);
313
314   if (!tp_cli_connection_interface_requests_run_create_channel (connection, -1,
315     request, &object_path, &channel_properties, &error, NULL))
316     {
317       DEBUG ("Error requesting channel: %s", error->message);
318       g_clear_error (&error);
319       g_object_unref (connection);
320       return NULL;
321     }
322
323   DEBUG ("Offering a new stream tube");
324
325   channel = tp_channel_new_from_properties (connection, object_path,
326       channel_properties, NULL);
327
328   tp_channel_run_until_ready (channel, NULL, NULL);
329
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);
337
338   if (parameters == NULL)
339     /* Pass an empty dict as parameters */
340     parameters = g_hash_table_new (g_str_hash, g_str_equal);
341   else
342     g_hash_table_ref (parameters);
343
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,
347         &error, NULL))
348     {
349       DEBUG ("Couldn't offer tube: %s", error->message);
350       g_clear_error (&error);
351       goto OUT;
352     }
353
354   DEBUG ("Stream tube offered");
355
356   tube = empathy_tp_tube_new (channel);
357
358 OUT:
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);
367
368   return tube;
369 }
370
371 static void
372 tp_tube_accept_stream_cb (TpProxy *proxy,
373                           const GValue *address,
374                           const GError *error,
375                           gpointer user_data,
376                           GObject *weak_object)
377 {
378   EmpathyTpTube *tube = EMPATHY_TP_TUBE (weak_object);
379   EmpathyTpTubeAcceptData *data = (EmpathyTpTubeAcceptData *)user_data;
380   EmpathyTpTubeAddress eaddress;
381
382   eaddress.type = data->type;
383
384   if (error)
385     {
386       DEBUG ("Error accepting tube: %s", error->message);
387       data->callback (tube, NULL, error, data->user_data);
388       return;
389     }
390
391   switch (eaddress.type)
392     {
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);
396         break;
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);
402         break;
403     }
404
405    data->callback (tube, &eaddress, NULL, data->user_data);
406 }
407
408 void
409 empathy_tp_tube_accept_stream_tube (EmpathyTpTube *tube,
410   TpSocketAddressType type, EmpatyTpTubeAcceptStreamTubeCb *callback,
411   gpointer user_data)
412 {
413   EmpathyTpTubePriv *priv = GET_PRIV (tube);
414   GValue *control_param;
415   EmpathyTpTubeAcceptData *data;
416
417   g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
418
419   DEBUG ("Accepting stream tube");
420   /* FIXME allow other acls */
421   control_param = tp_g_value_slice_new (G_TYPE_STRING);
422
423   data = new_empathy_tp_tube_accept_data (type, callback, user_data);
424
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));
429
430   tp_g_value_slice_free (control_param);
431 }