]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-tube.c
Remove conditional build of VOIP.
[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
27 #include "empathy-contact-factory.h"
28 #include "empathy-debug.h"
29 #include "empathy-enum-types.h"
30 #include "empathy-tp-tube.h"
31 #include "empathy-utils.h"
32
33 #define DEBUG_DOMAIN "TpTube"
34
35 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_TP_TUBE, \
36     EmpathyTpTubePriv))
37
38 typedef struct _EmpathyTpTubePriv EmpathyTpTubePriv;
39
40 struct _EmpathyTpTubePriv
41 {
42   TpChannel *channel;
43   guint id;
44   guint initiator;
45   guint type;
46   gchar *service;
47   GHashTable *parameters;
48   guint state;
49   EmpathyContact *initiator_contact;
50   EmpathyContactFactory *factory;
51 };
52
53 enum
54 {
55   PROP_0,
56   PROP_CHANNEL,
57   PROP_TP_TUBES,
58   PROP_ID,
59   PROP_INITIATOR,
60   PROP_TYPE,
61   PROP_SERVICE,
62   PROP_PARAMETERS,
63   PROP_STATE,
64   PROP_INITIATOR_CONTACT
65 };
66
67 enum
68 {
69   DESTROY,
70   LAST_SIGNAL
71 };
72
73 static guint signals[LAST_SIGNAL];
74
75 G_DEFINE_TYPE (EmpathyTpTube, empathy_tp_tube, G_TYPE_OBJECT)
76
77 static void
78 tp_tube_state_changed_cb (TpChannel *channel,
79                           guint id,
80                           guint state,
81                           gpointer user_data,
82                           GObject *tube)
83 {
84   EmpathyTpTubePriv *priv = GET_PRIV (tube);
85
86   if (id != priv->id)
87       return;
88
89   empathy_debug (DEBUG_DOMAIN, "Tube state changed");
90
91   priv->state = state;
92   g_object_notify (tube, "state");
93 }
94
95 static void
96 tp_tube_invalidated_cb (TpChannel     *channel,
97                         GQuark         domain,
98                         gint           code,
99                         gchar         *message,
100                         EmpathyTpTube *tube)
101 {
102   empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
103   g_signal_emit (tube, signals[DESTROY], 0);
104 }
105
106 static void
107 tp_tube_closed_cb (TpChannel *channel,
108                    guint id,
109                    gpointer user_data,
110                    GObject *tube)
111 {
112   EmpathyTpTubePriv *priv = GET_PRIV (tube);
113
114   if (id != priv->id)
115       return;
116
117   empathy_debug (DEBUG_DOMAIN, "Tube closed");
118   g_signal_emit (tube, signals[DESTROY], 0);
119 }
120
121 static void
122 tp_tube_async_cb (TpChannel *channel,
123                   const GError *error,
124                   gpointer user_data,
125                   GObject *tube)
126 {
127   if (error)
128       empathy_debug (DEBUG_DOMAIN, "Error %s: %s", user_data, error->message);
129 }
130
131 static void
132 tp_tube_set_property (GObject *object,
133                       guint prop_id,
134                       const GValue *value,
135                       GParamSpec *pspec)
136 {
137   EmpathyTpTubePriv *priv = GET_PRIV (object);
138
139   switch (prop_id)
140     {
141       case PROP_CHANNEL:
142         priv->channel = g_value_dup_object (value);
143         break;
144       case PROP_ID:
145         priv->id = g_value_get_uint (value);
146         break;
147       default:
148         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
149         break;
150   }
151 }
152
153 static void
154 tp_tube_get_property (GObject *object,
155                       guint prop_id,
156                       GValue *value,
157                       GParamSpec *pspec)
158 {
159   EmpathyTpTubePriv *priv = GET_PRIV (object);
160
161   switch (prop_id)
162     {
163       case PROP_CHANNEL:
164         g_value_set_object (value, priv->channel);
165         break;
166       case PROP_ID:
167         g_value_set_uint (value, priv->id);
168         break;
169       case PROP_INITIATOR:
170         g_value_set_uint (value, priv->initiator);
171         break;
172       case PROP_TYPE:
173         g_value_set_uint (value, priv->type);
174         break;
175       case PROP_SERVICE:
176         g_value_set_string (value, priv->service);
177         break;
178       case PROP_PARAMETERS:
179         g_value_set_boxed (value, priv->parameters);
180         break;
181       case PROP_STATE:
182         g_value_set_uint (value, priv->state);
183         break;
184       case PROP_INITIATOR_CONTACT:
185         g_value_set_object (value, priv->initiator_contact);
186         break;
187       default:
188         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189         break;
190   }
191 }
192
193 static GObject *
194 tp_tube_constructor (GType type,
195                      guint n_props,
196                      GObjectConstructParam *props)
197 {
198   GObject *self;
199   EmpathyTpTubePriv *priv;
200   GPtrArray *tubes;
201   guint i;
202   GError *error = NULL;
203
204   self = G_OBJECT_CLASS (empathy_tp_tube_parent_class)->constructor (
205       type, n_props, props);
206   priv = GET_PRIV (self);
207
208   g_signal_connect (priv->channel, "invalidated",
209       G_CALLBACK (tp_tube_invalidated_cb), self);
210   tp_cli_channel_type_tubes_connect_to_tube_closed (priv->channel,
211       tp_tube_closed_cb, NULL, NULL, self, NULL);
212   tp_cli_channel_type_tubes_connect_to_tube_state_changed (priv->channel,
213       tp_tube_state_changed_cb, NULL, NULL, self, NULL);
214
215   /* FIXME: It is absolutely not opimized to list all tubes to get information
216    * about our tube, but we don't really have the choice to avoid races. */
217   if (!tp_cli_channel_type_tubes_run_list_tubes (priv->channel, -1, &tubes,
218       &error, NULL))
219     {
220       empathy_debug (DEBUG_DOMAIN, "Couldn't list tubes: %s",
221           error->message);
222       g_clear_error (&error);
223       return self;
224     }
225
226   for (i = 0; i < tubes->len; i++)
227     {
228       GValueArray *values;
229       guint id;
230
231       values = g_ptr_array_index (tubes, i);
232       id = g_value_get_uint (g_value_array_get_nth (values, 0));
233
234       if (id == priv->id)
235         {
236           TpConnection *connection;
237           MissionControl *mc;
238           McAccount *account;
239
240           g_object_get (priv->channel, "connection", &connection, NULL);
241           mc = empathy_mission_control_new ();
242           account = mission_control_get_account_for_tpconnection (mc,
243               connection, NULL);
244
245           priv->initiator = g_value_get_uint (g_value_array_get_nth (values, 1));
246           priv->type = g_value_get_uint (g_value_array_get_nth (values, 2));
247           priv->service = g_value_dup_string (g_value_array_get_nth (values, 3));
248           priv->parameters = g_value_dup_boxed (g_value_array_get_nth (values, 4));
249           priv->state = g_value_get_uint (g_value_array_get_nth (values, 5));
250           priv->initiator_contact = empathy_contact_factory_get_from_handle (
251               priv->factory, account, priv->initiator);
252
253           g_object_unref (connection);
254           g_object_unref (mc);
255           g_object_unref (account);
256         }
257
258       g_value_array_free (values);
259     }
260   g_ptr_array_free (tubes, TRUE);
261
262   return self;
263 }
264
265 static void
266 tp_tube_finalize (GObject *object)
267 {
268   EmpathyTpTubePriv *priv = GET_PRIV (object);
269
270   empathy_debug (DEBUG_DOMAIN, "Finalizing: %p", object);
271
272   if (priv->channel)
273     {
274       g_signal_handlers_disconnect_by_func (priv->channel,
275           tp_tube_invalidated_cb, object);
276       tp_cli_channel_type_tubes_call_close_tube (priv->channel, -1, priv->id,
277           tp_tube_async_cb, "closing tube", NULL, NULL);
278       g_object_unref (priv->channel);
279     }
280   if (priv->initiator_contact)
281       g_object_unref (priv->initiator_contact);
282   if (priv->factory)
283       g_object_unref (priv->factory);
284
285   g_free (priv->service);
286   g_hash_table_destroy (priv->parameters);
287
288   G_OBJECT_CLASS (empathy_tp_tube_parent_class)->finalize (object);
289 }
290
291 static void
292 empathy_tp_tube_class_init (EmpathyTpTubeClass *klass)
293 {
294   GObjectClass *object_class = G_OBJECT_CLASS (klass);
295
296   object_class->constructor = tp_tube_constructor;
297   object_class->finalize = tp_tube_finalize;
298   object_class->set_property = tp_tube_set_property;
299   object_class->get_property = tp_tube_get_property;
300
301   g_object_class_install_property (object_class, PROP_CHANNEL,
302       g_param_spec_object ("channel", "channel", "channel", TP_TYPE_CHANNEL,
303       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME |
304         G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
305
306   g_object_class_install_property (object_class, PROP_ID,
307       g_param_spec_uint ("id", "id", "id", 0, G_MAXUINT, 0,
308         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME |
309         G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
310
311   g_object_class_install_property (object_class, PROP_INITIATOR,
312       g_param_spec_uint ("initiator", "initiator", "initiator",
313         0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_NAME |
314         G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
315
316   g_object_class_install_property (object_class, PROP_TYPE,
317       g_param_spec_uint ("type", "type", "type", 0, G_MAXUINT, 0,
318         G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
319         G_PARAM_STATIC_BLURB));
320
321   g_object_class_install_property (object_class, PROP_SERVICE,
322       g_param_spec_string ("service", "service", "service", NULL,
323       G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
324       G_PARAM_STATIC_BLURB));
325
326   g_object_class_install_property (object_class, PROP_PARAMETERS,
327       g_param_spec_boxed ("parameters", "parameters", "parameters",
328       G_TYPE_HASH_TABLE, G_PARAM_READABLE | G_PARAM_STATIC_NAME |
329       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
330
331   g_object_class_install_property (object_class, PROP_STATE,
332       g_param_spec_uint ("state", "state", "state", 0, G_MAXUINT, 0,
333         G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
334         G_PARAM_STATIC_BLURB));
335
336   g_object_class_install_property (object_class, PROP_INITIATOR_CONTACT,
337      g_param_spec_object ("initiator-contact", "initiator contact",
338      "initiator contact", EMPATHY_TYPE_CONTACT, G_PARAM_READABLE |
339      G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
340
341   signals[DESTROY] = g_signal_new ("destroy",
342       G_TYPE_FROM_CLASS (klass),
343       G_SIGNAL_RUN_LAST,
344       0, NULL, NULL,
345       g_cclosure_marshal_VOID__VOID,
346       G_TYPE_NONE, 0);
347
348   g_type_class_add_private (klass, sizeof (EmpathyTpTubePriv));
349 }
350
351 static void
352 empathy_tp_tube_init (EmpathyTpTube *tube)
353 {
354   EmpathyTpTubePriv *priv = GET_PRIV (tube);
355
356   priv->factory = empathy_contact_factory_new ();
357 }
358
359 EmpathyTpTube *
360 empathy_tp_tube_new (TpChannel *channel, guint tube_id)
361 {
362   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
363
364   return g_object_new (EMPATHY_TYPE_TP_TUBE,
365       "channel", channel, "id", tube_id, NULL);
366 }
367
368 EmpathyTpTube *
369 empathy_tp_tube_new_stream_tube (EmpathyContact *contact,
370                                  TpSocketAddressType type,
371                                  const gchar *hostname,
372                                  guint port,
373                                  const gchar *service)
374 {
375   MissionControl *mc;
376   McAccount *account;
377   TpConnection *connection;
378   TpChannel *channel;
379   gchar *object_path;
380   guint id;
381   GHashTable *params;
382   GValue *address;
383   GValue *control_param;
384   EmpathyTpTube *tube = NULL;
385   GError *error = NULL;
386
387   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
388   g_return_val_if_fail (hostname != NULL, NULL);
389   g_return_val_if_fail (service != NULL, NULL);
390
391   mc = empathy_mission_control_new ();
392   account = empathy_contact_get_account (contact);
393   connection = mission_control_get_tpconnection (mc, account, NULL);
394   g_object_unref (mc);
395
396   if (!tp_cli_connection_run_request_channel (connection, -1,
397       TP_IFACE_CHANNEL_TYPE_TUBES, TP_HANDLE_TYPE_CONTACT,
398       empathy_contact_get_handle (contact), FALSE, &object_path, &error, NULL))
399     {
400       empathy_debug (DEBUG_DOMAIN, "Error requesting channel: %s", error->message);
401       g_clear_error (&error);
402       g_object_unref (connection);
403       return NULL;
404     }
405
406   empathy_debug (DEBUG_DOMAIN, "Offering a new stream tube");
407
408   channel = tp_channel_new (connection, object_path,
409       TP_IFACE_CHANNEL_TYPE_TUBES, TP_HANDLE_TYPE_CONTACT,
410       empathy_contact_get_handle (contact), NULL);
411
412   #define ADDRESS_TYPE dbus_g_type_get_struct ("GValueArray",\
413       G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID)
414   params = g_hash_table_new (g_str_hash, g_str_equal);
415   address = tp_g_value_slice_new (ADDRESS_TYPE);
416   g_value_take_boxed (address, dbus_g_type_specialized_construct (ADDRESS_TYPE));
417   dbus_g_type_struct_set (address, 0, hostname, 1, port, G_MAXUINT);
418   control_param = tp_g_value_slice_new (G_TYPE_STRING);
419
420   if (!tp_cli_channel_type_tubes_run_offer_stream_tube (channel, -1,
421         service, params, type, address,
422         TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param, &id, &error, NULL))
423     {
424       empathy_debug (DEBUG_DOMAIN, "Couldn't offer tube: %s", error->message);
425       g_clear_error (&error);
426       goto OUT;
427     }
428
429   empathy_debug (DEBUG_DOMAIN, "Stream tube id=%d offered", id);
430
431   tube = empathy_tp_tube_new (channel, id);
432
433 OUT:
434   g_object_unref (channel);
435   g_free (object_path);
436   g_hash_table_destroy (params);
437   tp_g_value_slice_free (address);
438   tp_g_value_slice_free (control_param);
439   g_object_unref (connection);
440
441   return tube;
442 }
443
444 static void
445 tp_tube_accept_stream_cb (TpChannel *proxy,
446                           const GValue *address,
447                           const GError *error,
448                           gpointer user_data,
449                           GObject *weak_object)
450 {
451   if (error)
452       empathy_debug (DEBUG_DOMAIN, "Error accepting tube: %s", error->message);
453 }
454
455 void
456 empathy_tp_tube_accept_stream_tube (EmpathyTpTube *tube,
457                                     TpSocketAddressType type)
458 {
459   EmpathyTpTubePriv *priv = GET_PRIV (tube);
460   GValue *control_param;
461
462   g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
463
464   empathy_debug (DEBUG_DOMAIN, "Accepting stream tube - id: %d", priv->id);
465
466   control_param = tp_g_value_slice_new (G_TYPE_STRING);
467   tp_cli_channel_type_tubes_call_accept_stream_tube (priv->channel, -1, priv->id,
468       type, TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param,
469       tp_tube_accept_stream_cb, NULL, NULL, G_OBJECT (tube));
470
471   tp_g_value_slice_free (control_param);
472 }
473
474 void
475 empathy_tp_tube_get_socket (EmpathyTpTube *tube,
476                             gchar **hostname,
477                             guint *port)
478 {
479   EmpathyTpTubePriv *priv = GET_PRIV (tube);
480   GValue *address;
481   guint address_type;
482   GError *error = NULL;
483
484   g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
485
486   empathy_debug (DEBUG_DOMAIN, "Getting stream tube socket address");
487
488   address = g_slice_new0 (GValue);
489   if (!tp_cli_channel_type_tubes_run_get_stream_tube_socket_address (priv->channel,
490       -1, priv->id, &address_type, &address, &error, NULL))
491     {
492       empathy_debug (DEBUG_DOMAIN, "Couldn't get socket address: %s",
493           error->message);
494       g_clear_error (&error);
495       return;
496     }
497
498   switch (address_type)
499     {
500     case TP_SOCKET_ADDRESS_TYPE_UNIX:
501     case TP_SOCKET_ADDRESS_TYPE_ABSTRACT_UNIX:
502         dbus_g_type_struct_get (address, 0, hostname, G_MAXUINT);
503         break;
504     case TP_SOCKET_ADDRESS_TYPE_IPV4:
505     case TP_SOCKET_ADDRESS_TYPE_IPV6:
506         dbus_g_type_struct_get (address, 0, hostname, 1, port, G_MAXUINT);    
507         break;
508     }
509
510   tp_g_value_slice_free (address);
511 }
512