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