]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-tube.c
add '%' in front of constants
[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/proxy.h>
26 #include <telepathy-glib/util.h>
27 #include <extensions/extensions.h>
28
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   EmpathyTpTubeAcceptStreamTubeCb *callback;
39   gpointer user_data;
40 } EmpathyTpTubeAcceptData;
41
42 static EmpathyTpTubeAcceptData *
43 new_empathy_tp_tube_accept_data (TpSocketAddressType type,
44   EmpathyTpTubeAcceptStreamTubeCb *callback,
45   gpointer user_data)
46 {
47   EmpathyTpTubeAcceptData *r;
48
49   r = g_slice_new0 (EmpathyTpTubeAcceptData);
50   r->type = type;
51   r->callback = callback;
52   r->user_data = user_data;
53
54   return r;
55 }
56
57 static void
58 free_empathy_tp_tube_accept_data (gpointer data)
59 {
60   g_slice_free (EmpathyTpTubeAcceptData, data);
61 }
62
63
64 typedef struct {
65     EmpathyTpTubeReadyCb *callback;
66     gpointer user_data;
67     GDestroyNotify destroy;
68     GObject *weak_object;
69 } ReadyCbData;
70
71 /**
72  * SECTION:empathy-tp-tube
73  * @title:EmpathyTpTube
74  * @short_description: A wrapper around a Telepathy tube channel
75  * @include: libempathy/empathy-tp-tube.h
76  *
77  * #EmpathyTpTube is a convenient object wrapping a Telepathy tube channel.
78  */
79
80 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpTube)
81 typedef struct
82 {
83   TpChannel *channel;
84   EmpTubeChannelState state;
85   gboolean ready;
86   GSList *ready_callbacks;
87 } EmpathyTpTubePriv;
88
89 enum
90 {
91   PROP_0,
92   PROP_CHANNEL,
93   PROP_STATE,
94 };
95
96 enum
97 {
98   DESTROY,
99   LAST_SIGNAL
100 };
101
102 static guint signals[LAST_SIGNAL];
103
104 G_DEFINE_TYPE (EmpathyTpTube, empathy_tp_tube, G_TYPE_OBJECT)
105
106 static void
107 tp_tube_state_changed_cb (TpProxy *proxy,
108                           EmpTubeChannelState state,
109                           gpointer user_data,
110                           GObject *tube)
111 {
112   EmpathyTpTubePriv *priv = GET_PRIV (tube);
113
114   if (!priv->ready)
115     /* We didn't get the state yet */
116     return;
117
118   DEBUG ("Tube state changed");
119
120   priv->state = state;
121   g_object_notify (tube, "state");
122 }
123
124 static void
125 tp_tube_invalidated_cb (TpChannel *channel,
126     GQuark domain,
127     gint code,
128     gchar *message,
129     EmpathyTpTube *tube)
130 {
131   DEBUG ("Channel invalidated: %s", message);
132   g_signal_emit (tube, signals[DESTROY], 0);
133 }
134
135 static void
136 tp_tube_async_cb (TpChannel *channel,
137     const GError *error,
138     gpointer user_data,
139     GObject *tube)
140 {
141   if (error)
142       DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
143 }
144
145 static void
146 tp_tube_set_property (GObject *object,
147     guint prop_id,
148     const GValue *value,
149     GParamSpec *pspec)
150 {
151   EmpathyTpTubePriv *priv = GET_PRIV (object);
152
153   switch (prop_id)
154     {
155       case PROP_CHANNEL:
156         priv->channel = g_value_dup_object (value);
157         break;
158       default:
159         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160         break;
161   }
162 }
163
164 static void
165 tp_tube_get_property (GObject *object,
166     guint prop_id,
167     GValue *value,
168     GParamSpec *pspec)
169 {
170   EmpathyTpTubePriv *priv = GET_PRIV (object);
171
172   switch (prop_id)
173     {
174       case PROP_CHANNEL:
175         g_value_set_object (value, priv->channel);
176         break;
177       case PROP_STATE:
178         g_value_set_uint (value, priv->state);
179         break;
180       default:
181         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182         break;
183   }
184 }
185
186 static void weak_object_notify (gpointer data,
187     GObject *old_object);
188
189 static ReadyCbData *
190 ready_cb_data_new (EmpathyTpTube *self,
191     EmpathyTpTubeReadyCb *callback,
192     gpointer user_data,
193     GDestroyNotify destroy,
194     GObject *weak_object)
195 {
196   ReadyCbData *d = g_slice_new0 (ReadyCbData);
197   d->callback = callback;
198   d->user_data = user_data;
199   d->destroy = destroy;
200   d->weak_object = weak_object;
201
202   if (weak_object != NULL)
203     g_object_weak_ref (weak_object, weak_object_notify, self);
204
205   return d;
206 }
207
208 static void
209 ready_cb_data_free (ReadyCbData *data,
210     EmpathyTpTube *self)
211 {
212   if (data->destroy != NULL)
213     data->destroy (data->user_data);
214
215   if (data->weak_object != NULL)
216     g_object_weak_unref (data->weak_object,
217         weak_object_notify, self);
218
219   g_slice_free (ReadyCbData, data);
220 }
221
222 static void
223 weak_object_notify (gpointer data,
224     GObject *old_object)
225 {
226   EmpathyTpTube *self = EMPATHY_TP_TUBE (data);
227   EmpathyTpTubePriv *priv = GET_PRIV (self);
228   GSList *l, *ln;
229
230   for (l = priv->ready_callbacks ; l != NULL ; l = ln )
231     {
232       ReadyCbData *d = (ReadyCbData *) l->data;
233       ln = g_slist_next (l);
234
235       if (d->weak_object == old_object)
236         {
237           ready_cb_data_free (d, self);
238           priv->ready_callbacks = g_slist_delete_link (priv->ready_callbacks,
239             l);
240         }
241     }
242 }
243
244
245 static void
246 tube_is_ready (EmpathyTpTube *self,
247     const GError *error)
248 {
249   EmpathyTpTubePriv *priv = GET_PRIV (self);
250   GSList *l;
251
252   priv->ready = TRUE;
253
254   for (l = priv->ready_callbacks ; l != NULL ; l = g_slist_next (l))
255     {
256       ReadyCbData *data = (ReadyCbData *) l->data;
257
258       data->callback (self, error, data->user_data, data->weak_object);
259       ready_cb_data_free (data, self);
260     }
261
262   g_slist_free (priv->ready_callbacks);
263   priv->ready_callbacks = NULL;
264 }
265
266 static void
267 got_tube_state_cb (TpProxy *proxy,
268     const GValue *out_value,
269     const GError *error,
270     gpointer user_data,
271     GObject *weak_object)
272 {
273   EmpathyTpTube *self = EMPATHY_TP_TUBE (user_data);
274   EmpathyTpTubePriv *priv = GET_PRIV (self);
275
276   if (error != NULL)
277     {
278       DEBUG ("Error getting State property: %s", error->message);
279     }
280   else
281     {
282       priv->state = g_value_get_uint (out_value);
283       g_object_notify (G_OBJECT (self), "state");
284     }
285
286   tube_is_ready (self, error);
287 }
288
289 static GObject *
290 tp_tube_constructor (GType type,
291     guint n_props,
292     GObjectConstructParam *props)
293 {
294   GObject *self;
295   EmpathyTpTubePriv *priv;
296
297   self = G_OBJECT_CLASS (empathy_tp_tube_parent_class)->constructor (
298       type, n_props, props);
299   priv = GET_PRIV (self);
300
301   g_signal_connect (priv->channel, "invalidated",
302       G_CALLBACK (tp_tube_invalidated_cb), self);
303
304   priv->ready = FALSE;
305
306   emp_cli_channel_interface_tube_connect_to_tube_channel_state_changed (
307     TP_PROXY (priv->channel), tp_tube_state_changed_cb, NULL, NULL,
308     self, NULL);
309
310   tp_cli_dbus_properties_call_get (priv->channel, -1,
311       EMP_IFACE_CHANNEL_INTERFACE_TUBE, "State", got_tube_state_cb,
312       self, NULL, G_OBJECT (self));
313
314   return self;
315 }
316
317 static void
318 tp_tube_finalize (GObject *object)
319 {
320   EmpathyTpTube *self = EMPATHY_TP_TUBE (object);
321   EmpathyTpTubePriv *priv = GET_PRIV (object);
322   GSList *l;
323
324   DEBUG ("Finalizing: %p", object);
325
326   if (priv->channel)
327     {
328       g_signal_handlers_disconnect_by_func (priv->channel,
329           tp_tube_invalidated_cb, object);
330       tp_cli_channel_call_close (priv->channel, -1, tp_tube_async_cb,
331         "closing tube", NULL, NULL);
332       g_object_unref (priv->channel);
333     }
334
335   for (l = priv->ready_callbacks; l != NULL; l = g_slist_next (l))
336     {
337       ReadyCbData *d = (ReadyCbData *) l->data;
338
339       ready_cb_data_free (d, self);
340     }
341
342   g_slist_free (priv->ready_callbacks);
343   priv->ready_callbacks = NULL;
344
345   G_OBJECT_CLASS (empathy_tp_tube_parent_class)->finalize (object);
346 }
347
348 static void
349 empathy_tp_tube_class_init (EmpathyTpTubeClass *klass)
350 {
351   GObjectClass *object_class = G_OBJECT_CLASS (klass);
352
353   object_class->constructor = tp_tube_constructor;
354   object_class->finalize = tp_tube_finalize;
355   object_class->set_property = tp_tube_set_property;
356   object_class->get_property = tp_tube_get_property;
357
358   /**
359    * EmpathyTpTube:channel:
360    *
361    * The #TpChannel wrapped by the tube object.
362    */
363   g_object_class_install_property (object_class, PROP_CHANNEL,
364       g_param_spec_object ("channel", "channel", "channel", TP_TYPE_CHANNEL,
365       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
366
367   /**
368    * EmpathyTpTube:state:
369    *
370    * The state of the tube.
371    */
372   g_object_class_install_property (object_class, PROP_STATE,
373       g_param_spec_uint ("state", "state", "state",
374         0, NUM_EMP_TUBE_CHANNEL_STATES, 0,
375         G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_STRINGS));
376   /**
377    * EmpathyTpTube::destroy:
378    * @self: the tube object
379    *
380    * Emitted when then tube has been invalidated.
381    */
382   signals[DESTROY] = g_signal_new ("destroy",
383       G_TYPE_FROM_CLASS (klass),
384       G_SIGNAL_RUN_LAST,
385       0, NULL, NULL,
386       g_cclosure_marshal_VOID__VOID,
387       G_TYPE_NONE, 0);
388
389   g_type_class_add_private (klass, sizeof (EmpathyTpTubePriv));
390 }
391
392 static void
393 empathy_tp_tube_init (EmpathyTpTube *tube)
394 {
395   EmpathyTpTubePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tube,
396       EMPATHY_TYPE_TP_TUBE, EmpathyTpTubePriv);
397
398   tube->priv = priv;
399 }
400
401 /**
402  * empathy_tp_tube_new:
403  * @channel: a #TpChannel
404  *
405  * Creates a new #EmpathyTpTube.
406  *
407  * Return value: a new #EmpathyTpTube
408  */
409 EmpathyTpTube *
410 empathy_tp_tube_new (TpChannel *channel)
411 {
412   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
413
414   return g_object_new (EMPATHY_TYPE_TP_TUBE, "channel", channel,  NULL);
415 }
416
417 /**
418  * empathy_tp_tube_new_stream_tube:
419  * @contact: the #EmpathyContact to which the tube is offered
420  * @type: the type of the listening address of the local service. Either
421  * %TP_SOCKET_ADDRESS_TYPE_IPV4 or %TP_SOCKET_ADDRESS_TYPE_IPV6.
422  * @hostname: the address of the local service
423  * @port: the port of the local service
424  * @service: the service name of the tube
425  * @parameters: the parameters of the tube
426  *
427  * Creates and offers a new #EmpathyTpTube of ChannelType StreamTube.
428  *
429  * Return value: a new #EmpathyTpTube
430  */
431 EmpathyTpTube *
432 empathy_tp_tube_new_stream_tube (EmpathyContact *contact,
433     TpSocketAddressType type,
434     const gchar *hostname,
435     guint port,
436     const gchar *service,
437     GHashTable *parameters)
438 {
439   TpConnection *connection;
440   TpChannel *channel;
441   gchar *object_path;
442   GHashTable *params;
443   GValue *address;
444   GValue *control_param;
445   EmpathyTpTube *tube = NULL;
446   GError *error = NULL;
447   GHashTable *request;
448   GHashTable *channel_properties;
449   GValue *value;
450
451   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
452   g_return_val_if_fail (hostname != NULL, NULL);
453   g_return_val_if_fail (service != NULL, NULL);
454
455   connection = empathy_contact_get_connection (contact);
456
457   request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
458       (GDestroyNotify) tp_g_value_slice_free);
459
460   /* org.freedesktop.Telepathy.Channel.ChannelType */
461   value = tp_g_value_slice_new (G_TYPE_STRING);
462   g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE);
463   g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
464
465   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
466   value = tp_g_value_slice_new (G_TYPE_UINT);
467   g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
468   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
469
470   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
471   value = tp_g_value_slice_new (G_TYPE_UINT);
472   g_value_set_uint (value, empathy_contact_get_handle (contact));
473   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
474
475   /* org.freedesktop.Telepathy.Channel.Type.StreamTube.Service */
476   value = tp_g_value_slice_new (G_TYPE_STRING);
477   g_value_set_string (value, service);
478   g_hash_table_insert (request,
479     EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE  ".Service", value);
480
481   if (!tp_cli_connection_interface_requests_run_create_channel (connection, -1,
482     request, &object_path, &channel_properties, &error, NULL))
483     {
484       DEBUG ("Error requesting channel: %s", error->message);
485       g_clear_error (&error);
486       g_object_unref (connection);
487       return NULL;
488     }
489
490   DEBUG ("Offering a new stream tube");
491
492   channel = tp_channel_new_from_properties (connection, object_path,
493       channel_properties, NULL);
494
495   tp_channel_run_until_ready (channel, NULL, NULL);
496
497   #define ADDRESS_TYPE dbus_g_type_get_struct ("GValueArray",\
498       G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID)
499   params = g_hash_table_new (g_str_hash, g_str_equal);
500   address = tp_g_value_slice_new (ADDRESS_TYPE);
501   g_value_take_boxed (address, dbus_g_type_specialized_construct (ADDRESS_TYPE));
502   dbus_g_type_struct_set (address, 0, hostname, 1, port, G_MAXUINT);
503   control_param = tp_g_value_slice_new (G_TYPE_STRING);
504
505   if (parameters == NULL)
506     /* Pass an empty dict as parameters */
507     parameters = g_hash_table_new (g_str_hash, g_str_equal);
508   else
509     g_hash_table_ref (parameters);
510
511   if (!emp_cli_channel_type_stream_tube_run_offer_stream_tube (
512         TP_PROXY(channel), -1, type, address,
513         TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param, parameters,
514         &error, NULL))
515     {
516       DEBUG ("Couldn't offer tube: %s", error->message);
517       g_clear_error (&error);
518       goto OUT;
519     }
520
521   DEBUG ("Stream tube offered");
522
523   tube = empathy_tp_tube_new (channel);
524
525 OUT:
526   g_object_unref (channel);
527   g_free (object_path);
528   g_hash_table_destroy (request);
529   g_hash_table_destroy (channel_properties);
530   tp_g_value_slice_free (address);
531   tp_g_value_slice_free (control_param);
532   g_object_unref (connection);
533   g_hash_table_unref (parameters);
534
535   return tube;
536 }
537
538 static void
539 tp_tube_accept_stream_cb (TpProxy *proxy,
540     const GValue *address,
541     const GError *error,
542     gpointer user_data,
543     GObject *weak_object)
544 {
545   EmpathyTpTube *tube = EMPATHY_TP_TUBE (weak_object);
546   EmpathyTpTubeAcceptData *data = (EmpathyTpTubeAcceptData *)user_data;
547   EmpathyTpTubeAddress eaddress;
548
549   eaddress.type = data->type;
550
551   if (error)
552     {
553       DEBUG ("Error accepting tube: %s", error->message);
554       data->callback (tube, NULL, error, data->user_data);
555       return;
556     }
557
558   switch (eaddress.type)
559     {
560       case TP_SOCKET_ADDRESS_TYPE_UNIX:
561       case TP_SOCKET_ADDRESS_TYPE_ABSTRACT_UNIX:
562         eaddress.a.socket.path = g_value_get_boxed (address);
563         break;
564      case TP_SOCKET_ADDRESS_TYPE_IPV4:
565      case TP_SOCKET_ADDRESS_TYPE_IPV6:
566         dbus_g_type_struct_get (address,
567           0, &eaddress.a.inet.hostname,
568           1, &eaddress.a.inet.port, G_MAXUINT);
569         break;
570     }
571
572    data->callback (tube, &eaddress, NULL, data->user_data);
573 }
574
575 /**
576  * empathy_tp_tube_accept_stream_tube:
577  * @tube: an #EmpathyTpTube
578  * @type: the type of address the connection manager should listen on
579  * @callback: called when the tube has been accepted
580  * @user_data: arbitrary user-supplied data passed to the callback
581  *
582  * Accepts @tube of ChannelType StreamTube and call @callback once it's done.
583  */
584 void
585 empathy_tp_tube_accept_stream_tube (EmpathyTpTube *tube,
586   TpSocketAddressType type,
587   EmpathyTpTubeAcceptStreamTubeCb *callback,
588   gpointer user_data)
589 {
590   EmpathyTpTubePriv *priv = GET_PRIV (tube);
591   GValue *control_param;
592   EmpathyTpTubeAcceptData *data;
593
594   g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
595
596   DEBUG ("Accepting stream tube");
597   /* FIXME allow other acls */
598   control_param = tp_g_value_slice_new (G_TYPE_STRING);
599
600   data = new_empathy_tp_tube_accept_data (type, callback, user_data);
601
602   emp_cli_channel_type_stream_tube_call_accept_stream_tube (
603      TP_PROXY (priv->channel), -1, type, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
604      control_param, tp_tube_accept_stream_cb, data,
605      free_empathy_tp_tube_accept_data, G_OBJECT (tube));
606
607   tp_g_value_slice_free (control_param);
608 }
609
610 /**
611  * EmpathyTpTubeReadyCb:
612  * @tube: an #EmpathyTpTube
613  * @error: %NULL on success, or the reason why the tube can't be ready
614  * @user_data: the @user_data passed to empathy_tp_tube_call_when_ready()
615  * @weak_object: the @weak_object passed to
616  *               empathy_tp_tube_call_when_ready()
617  *
618  * Called as the result of empathy_tp_tube_call_when_ready(). If the
619  * tube's properties could be retrieved,
620  * @error is %NULL and @tube is considered to be ready. Otherwise, @error is
621  * non-%NULL and @tube is not ready.
622  */
623
624 /**
625  * empathy_tp_tube_call_when_ready:
626  * @tube: an #EmpathyTpTube
627  * @callback: called when the tube becomes ready
628  * @user_data: arbitrary user-supplied data passed to the callback
629  * @destroy: called to destroy @user_data
630  * @weak_object: object to reference weakly; if it is destroyed, @callback
631  *               will not be called, but @destroy will still be called
632  *
633  * If @tube is ready for use, call @callback immediately, then return.
634  * Otherwise, arrange for @callback to be called when @tube becomes
635  * ready for use.
636  */
637 void
638 empathy_tp_tube_call_when_ready (EmpathyTpTube *self,
639     EmpathyTpTubeReadyCb *callback,
640     gpointer user_data,
641     GDestroyNotify destroy,
642     GObject *weak_object)
643 {
644   EmpathyTpTubePriv *priv = GET_PRIV (self);
645
646   g_return_if_fail (self != NULL);
647   g_return_if_fail (callback != NULL);
648
649   if (priv->ready)
650     {
651       callback (self, NULL, user_data, weak_object);
652       if (destroy != NULL)
653         destroy (user_data);
654     }
655   else
656     {
657       priv->ready_callbacks = g_slist_prepend (priv->ready_callbacks,
658           ready_cb_data_new (self, callback, user_data, destroy, weak_object));
659     }
660 }