]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-tube.c
Make use of tp-glib debug system.
[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-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 #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   DEBUG ("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   DEBUG ("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   DEBUG ("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       DEBUG ("Error %s: %s", (gchar*) 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       DEBUG ("Couldn't list tubes: %s", error->message);
221       g_clear_error (&error);
222       return self;
223     }
224
225   for (i = 0; i < tubes->len; i++)
226     {
227       GValueArray *values;
228       guint id;
229
230       values = g_ptr_array_index (tubes, i);
231       id = g_value_get_uint (g_value_array_get_nth (values, 0));
232
233       if (id == priv->id)
234         {
235           TpConnection *connection;
236           MissionControl *mc;
237           McAccount *account;
238
239           g_object_get (priv->channel, "connection", &connection, NULL);
240           mc = empathy_mission_control_new ();
241           account = mission_control_get_account_for_tpconnection (mc,
242               connection, NULL);
243
244           priv->initiator = g_value_get_uint (g_value_array_get_nth (values, 1));
245           priv->type = g_value_get_uint (g_value_array_get_nth (values, 2));
246           priv->service = g_value_dup_string (g_value_array_get_nth (values, 3));
247           priv->parameters = g_value_dup_boxed (g_value_array_get_nth (values, 4));
248           priv->state = g_value_get_uint (g_value_array_get_nth (values, 5));
249           priv->initiator_contact = empathy_contact_factory_get_from_handle (
250               priv->factory, account, priv->initiator);
251
252           g_object_unref (connection);
253           g_object_unref (mc);
254           g_object_unref (account);
255         }
256
257       g_value_array_free (values);
258     }
259   g_ptr_array_free (tubes, TRUE);
260
261   return self;
262 }
263
264 static void
265 tp_tube_finalize (GObject *object)
266 {
267   EmpathyTpTubePriv *priv = GET_PRIV (object);
268
269   DEBUG ("Finalizing: %p", object);
270
271   if (priv->channel)
272     {
273       g_signal_handlers_disconnect_by_func (priv->channel,
274           tp_tube_invalidated_cb, object);
275       tp_cli_channel_type_tubes_call_close_tube (priv->channel, -1, priv->id,
276           tp_tube_async_cb, "closing tube", NULL, NULL);
277       g_object_unref (priv->channel);
278     }
279   if (priv->initiator_contact)
280       g_object_unref (priv->initiator_contact);
281   if (priv->factory)
282       g_object_unref (priv->factory);
283
284   g_free (priv->service);
285   g_hash_table_destroy (priv->parameters);
286
287   G_OBJECT_CLASS (empathy_tp_tube_parent_class)->finalize (object);
288 }
289
290 static void
291 empathy_tp_tube_class_init (EmpathyTpTubeClass *klass)
292 {
293   GObjectClass *object_class = G_OBJECT_CLASS (klass);
294
295   object_class->constructor = tp_tube_constructor;
296   object_class->finalize = tp_tube_finalize;
297   object_class->set_property = tp_tube_set_property;
298   object_class->get_property = tp_tube_get_property;
299
300   g_object_class_install_property (object_class, PROP_CHANNEL,
301       g_param_spec_object ("channel", "channel", "channel", TP_TYPE_CHANNEL,
302       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME |
303         G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
304
305   g_object_class_install_property (object_class, PROP_ID,
306       g_param_spec_uint ("id", "id", "id", 0, G_MAXUINT, 0,
307         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME |
308         G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
309
310   g_object_class_install_property (object_class, PROP_INITIATOR,
311       g_param_spec_uint ("initiator", "initiator", "initiator",
312         0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_NAME |
313         G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
314
315   g_object_class_install_property (object_class, PROP_TYPE,
316       g_param_spec_uint ("type", "type", "type", 0, G_MAXUINT, 0,
317         G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
318         G_PARAM_STATIC_BLURB));
319
320   g_object_class_install_property (object_class, PROP_SERVICE,
321       g_param_spec_string ("service", "service", "service", NULL,
322       G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
323       G_PARAM_STATIC_BLURB));
324
325   g_object_class_install_property (object_class, PROP_PARAMETERS,
326       g_param_spec_boxed ("parameters", "parameters", "parameters",
327       G_TYPE_HASH_TABLE, G_PARAM_READABLE | G_PARAM_STATIC_NAME |
328       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
329
330   g_object_class_install_property (object_class, PROP_STATE,
331       g_param_spec_uint ("state", "state", "state", 0, G_MAXUINT, 0,
332         G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK |
333         G_PARAM_STATIC_BLURB));
334
335   g_object_class_install_property (object_class, PROP_INITIATOR_CONTACT,
336      g_param_spec_object ("initiator-contact", "initiator contact",
337      "initiator contact", EMPATHY_TYPE_CONTACT, G_PARAM_READABLE |
338      G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
339
340   signals[DESTROY] = g_signal_new ("destroy",
341       G_TYPE_FROM_CLASS (klass),
342       G_SIGNAL_RUN_LAST,
343       0, NULL, NULL,
344       g_cclosure_marshal_VOID__VOID,
345       G_TYPE_NONE, 0);
346
347   g_type_class_add_private (klass, sizeof (EmpathyTpTubePriv));
348 }
349
350 static void
351 empathy_tp_tube_init (EmpathyTpTube *tube)
352 {
353   EmpathyTpTubePriv *priv = GET_PRIV (tube);
354
355   priv->factory = empathy_contact_factory_new ();
356 }
357
358 EmpathyTpTube *
359 empathy_tp_tube_new (TpChannel *channel, guint tube_id)
360 {
361   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
362
363   return g_object_new (EMPATHY_TYPE_TP_TUBE,
364       "channel", channel, "id", tube_id, NULL);
365 }
366
367 EmpathyTpTube *
368 empathy_tp_tube_new_stream_tube (EmpathyContact *contact,
369                                  TpSocketAddressType type,
370                                  const gchar *hostname,
371                                  guint port,
372                                  const gchar *service)
373 {
374   MissionControl *mc;
375   McAccount *account;
376   TpConnection *connection;
377   TpChannel *channel;
378   gchar *object_path;
379   guint id;
380   GHashTable *params;
381   GValue *address;
382   GValue *control_param;
383   EmpathyTpTube *tube = NULL;
384   GError *error = NULL;
385
386   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
387   g_return_val_if_fail (hostname != NULL, NULL);
388   g_return_val_if_fail (service != NULL, NULL);
389
390   mc = empathy_mission_control_new ();
391   account = empathy_contact_get_account (contact);
392   connection = mission_control_get_tpconnection (mc, account, NULL);
393   g_object_unref (mc);
394
395   if (!tp_cli_connection_run_request_channel (connection, -1,
396       TP_IFACE_CHANNEL_TYPE_TUBES, TP_HANDLE_TYPE_CONTACT,
397       empathy_contact_get_handle (contact), FALSE, &object_path, &error, NULL))
398     {
399       DEBUG ("Error requesting channel: %s", error->message);
400       g_clear_error (&error);
401       g_object_unref (connection);
402       return NULL;
403     }
404
405   DEBUG ("Offering a new stream tube");
406
407   channel = tp_channel_new (connection, object_path,
408       TP_IFACE_CHANNEL_TYPE_TUBES, TP_HANDLE_TYPE_CONTACT,
409       empathy_contact_get_handle (contact), NULL);
410
411   #define ADDRESS_TYPE dbus_g_type_get_struct ("GValueArray",\
412       G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID)
413   params = g_hash_table_new (g_str_hash, g_str_equal);
414   address = tp_g_value_slice_new (ADDRESS_TYPE);
415   g_value_take_boxed (address, dbus_g_type_specialized_construct (ADDRESS_TYPE));
416   dbus_g_type_struct_set (address, 0, hostname, 1, port, G_MAXUINT);
417   control_param = tp_g_value_slice_new (G_TYPE_STRING);
418
419   if (!tp_cli_channel_type_tubes_run_offer_stream_tube (channel, -1,
420         service, params, type, address,
421         TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param, &id, &error, NULL))
422     {
423       DEBUG ("Couldn't offer tube: %s", error->message);
424       g_clear_error (&error);
425       goto OUT;
426     }
427
428   DEBUG ("Stream tube id=%d offered", id);
429
430   tube = empathy_tp_tube_new (channel, id);
431
432 OUT:
433   g_object_unref (channel);
434   g_free (object_path);
435   g_hash_table_destroy (params);
436   tp_g_value_slice_free (address);
437   tp_g_value_slice_free (control_param);
438   g_object_unref (connection);
439
440   return tube;
441 }
442
443 static void
444 tp_tube_accept_stream_cb (TpChannel *proxy,
445                           const GValue *address,
446                           const GError *error,
447                           gpointer user_data,
448                           GObject *weak_object)
449 {
450   if (error)
451       DEBUG ("Error accepting tube: %s", error->message);
452 }
453
454 void
455 empathy_tp_tube_accept_stream_tube (EmpathyTpTube *tube,
456                                     TpSocketAddressType type)
457 {
458   EmpathyTpTubePriv *priv = GET_PRIV (tube);
459   GValue *control_param;
460
461   g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
462
463   DEBUG ("Accepting stream tube - id: %d", priv->id);
464
465   control_param = tp_g_value_slice_new (G_TYPE_STRING);
466   tp_cli_channel_type_tubes_call_accept_stream_tube (priv->channel, -1, priv->id,
467       type, TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param,
468       tp_tube_accept_stream_cb, NULL, NULL, G_OBJECT (tube));
469
470   tp_g_value_slice_free (control_param);
471 }
472
473 void
474 empathy_tp_tube_get_socket (EmpathyTpTube *tube,
475                             gchar **hostname,
476                             guint *port)
477 {
478   EmpathyTpTubePriv *priv = GET_PRIV (tube);
479   GValue *address;
480   guint address_type;
481   GError *error = NULL;
482
483   g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
484
485   DEBUG ("Getting stream tube socket address");
486
487   address = g_slice_new0 (GValue);
488   if (!tp_cli_channel_type_tubes_run_get_stream_tube_socket_address (priv->channel,
489       -1, priv->id, &address_type, &address, &error, NULL))
490     {
491       DEBUG ("Couldn't get socket address: %s", error->message);
492       g_clear_error (&error);
493       return;
494     }
495
496   switch (address_type)
497     {
498     case TP_SOCKET_ADDRESS_TYPE_UNIX:
499     case TP_SOCKET_ADDRESS_TYPE_ABSTRACT_UNIX:
500         dbus_g_type_struct_get (address, 0, hostname, G_MAXUINT);
501         break;
502     case TP_SOCKET_ADDRESS_TYPE_IPV4:
503     case TP_SOCKET_ADDRESS_TYPE_IPV6:
504         dbus_g_type_struct_get (address, 0, hostname, 1, port, G_MAXUINT);    
505         break;
506     }
507
508   tp_g_value_slice_free (address);
509 }
510