]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-tube.c
tp_tube_constructor: get State property not priv->state is actually set
[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   EmpatyTpTubeAcceptStreamTubeCb *callback;
39   gpointer user_data;
40 } EmpathyTpTubeAcceptData;
41
42 static EmpathyTpTubeAcceptData *
43 new_empathy_tp_tube_accept_data (TpSocketAddressType type,
44   EmpatyTpTubeAcceptStreamTubeCb *callback, gpointer user_data)
45 {
46   EmpathyTpTubeAcceptData *r;
47
48   r = g_slice_new0 (EmpathyTpTubeAcceptData);
49   r->type = type;
50   r->callback = callback;
51   r->user_data = user_data;
52
53   return r;
54 }
55
56 static void
57 free_empathy_tp_tube_accept_data (gpointer data)
58 {
59   g_slice_free (EmpathyTpTubeAcceptData, data);
60 }
61
62
63 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpTube)
64 typedef struct
65 {
66   TpChannel *channel;
67   EmpTubeChannelState state;
68   gboolean ready;
69 } EmpathyTpTubePriv;
70
71 enum
72 {
73   PROP_0,
74   PROP_CHANNEL,
75   PROP_STATE,
76 };
77
78 enum
79 {
80   DESTROY,
81   LAST_SIGNAL
82 };
83
84 static guint signals[LAST_SIGNAL];
85
86 G_DEFINE_TYPE (EmpathyTpTube, empathy_tp_tube, G_TYPE_OBJECT)
87
88 static void
89 tp_tube_state_changed_cb (TpProxy *proxy,
90                           EmpTubeChannelState state,
91                           gpointer user_data,
92                           GObject *tube)
93 {
94   EmpathyTpTubePriv *priv = GET_PRIV (tube);
95
96   if (!priv->ready)
97     /* We didn't get the state yet */
98     return;
99
100   DEBUG ("Tube state changed");
101
102   priv->state = state;
103   g_object_notify (tube, "state");
104 }
105
106 static void
107 tp_tube_invalidated_cb (TpChannel     *channel,
108                         GQuark         domain,
109                         gint           code,
110                         gchar         *message,
111                         EmpathyTpTube *tube)
112 {
113   DEBUG ("Channel invalidated: %s", message);
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       default:
141         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142         break;
143   }
144 }
145
146 static void
147 tp_tube_get_property (GObject *object,
148                       guint prop_id,
149                       GValue *value,
150                       GParamSpec *pspec)
151 {
152   EmpathyTpTubePriv *priv = GET_PRIV (object);
153
154   switch (prop_id)
155     {
156       case PROP_CHANNEL:
157         g_value_set_object (value, priv->channel);
158         break;
159       case PROP_STATE:
160         g_value_set_uint (value, priv->state);
161         break;
162       default:
163         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
164         break;
165   }
166 }
167
168 static void
169 got_tube_state_cb (TpProxy *proxy,
170                    const GValue *out_value,
171                    const GError *error,
172                    gpointer user_data,
173                    GObject *weak_object)
174 {
175   EmpathyTpTube *self = EMPATHY_TP_TUBE (user_data);
176   EmpathyTpTubePriv *priv = GET_PRIV (self);
177
178   priv->ready = TRUE;
179
180   if (error != NULL)
181     {
182       DEBUG ("Error getting State property: %s", error->message);
183       return;
184     }
185
186   priv->state = g_value_get_uint (out_value);
187   g_object_notify (G_OBJECT (self), "state");
188 }
189
190 static GObject *
191 tp_tube_constructor (GType type,
192                      guint n_props,
193                      GObjectConstructParam *props)
194 {
195   GObject *self;
196   EmpathyTpTubePriv *priv;
197
198   self = G_OBJECT_CLASS (empathy_tp_tube_parent_class)->constructor (
199       type, n_props, props);
200   priv = GET_PRIV (self);
201
202   g_signal_connect (priv->channel, "invalidated",
203       G_CALLBACK (tp_tube_invalidated_cb), self);
204
205   priv->ready = FALSE;
206
207   emp_cli_channel_interface_tube_connect_to_tube_channel_state_changed (
208     TP_PROXY (priv->channel), tp_tube_state_changed_cb, NULL, NULL,
209     self, NULL);
210
211   tp_cli_dbus_properties_call_get (priv->channel, -1,
212       EMP_IFACE_CHANNEL_INTERFACE_TUBE, "State", got_tube_state_cb,
213       self, NULL, G_OBJECT (self));
214
215   return self;
216 }
217
218 static void
219 tp_tube_finalize (GObject *object)
220 {
221   EmpathyTpTubePriv *priv = GET_PRIV (object);
222
223   DEBUG ("Finalizing: %p", object);
224
225   if (priv->channel)
226     {
227       g_signal_handlers_disconnect_by_func (priv->channel,
228           tp_tube_invalidated_cb, object);
229       tp_cli_channel_call_close (priv->channel, -1, tp_tube_async_cb,
230         "closing tube", NULL, NULL);
231       g_object_unref (priv->channel);
232     }
233
234   G_OBJECT_CLASS (empathy_tp_tube_parent_class)->finalize (object);
235 }
236
237 static void
238 empathy_tp_tube_class_init (EmpathyTpTubeClass *klass)
239 {
240   GObjectClass *object_class = G_OBJECT_CLASS (klass);
241
242   object_class->constructor = tp_tube_constructor;
243   object_class->finalize = tp_tube_finalize;
244   object_class->set_property = tp_tube_set_property;
245   object_class->get_property = tp_tube_get_property;
246
247   g_object_class_install_property (object_class, PROP_CHANNEL,
248       g_param_spec_object ("channel", "channel", "channel", TP_TYPE_CHANNEL,
249       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
250
251   g_object_class_install_property (object_class, PROP_STATE,
252       g_param_spec_uint ("state", "state", "state",
253         0, NUM_EMP_TUBE_CHANNEL_STATES, 0,
254         G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_STRINGS));
255
256   signals[DESTROY] = g_signal_new ("destroy",
257       G_TYPE_FROM_CLASS (klass),
258       G_SIGNAL_RUN_LAST,
259       0, NULL, NULL,
260       g_cclosure_marshal_VOID__VOID,
261       G_TYPE_NONE, 0);
262
263   g_type_class_add_private (klass, sizeof (EmpathyTpTubePriv));
264 }
265
266 static void
267 empathy_tp_tube_init (EmpathyTpTube *tube)
268 {
269   EmpathyTpTubePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tube,
270                 EMPATHY_TYPE_TP_TUBE, EmpathyTpTubePriv);
271
272   tube->priv = priv;
273 }
274
275 EmpathyTpTube *
276 empathy_tp_tube_new (TpChannel *channel)
277 {
278   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
279
280   return g_object_new (EMPATHY_TYPE_TP_TUBE, "channel", channel,  NULL);
281 }
282
283 EmpathyTpTube *
284 empathy_tp_tube_new_stream_tube (EmpathyContact *contact,
285                                  TpSocketAddressType type,
286                                  const gchar *hostname,
287                                  guint port,
288                                  const gchar *service,
289                                  GHashTable *parameters)
290 {
291   MissionControl *mc;
292   McAccount *account;
293   TpConnection *connection;
294   TpChannel *channel;
295   gchar *object_path;
296   GHashTable *params;
297   GValue *address;
298   GValue *control_param;
299   EmpathyTpTube *tube = NULL;
300   GError *error = NULL;
301   GHashTable *request;
302   GHashTable *channel_properties;
303   GValue *value;
304
305   g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
306   g_return_val_if_fail (hostname != NULL, NULL);
307   g_return_val_if_fail (service != NULL, NULL);
308
309   mc = empathy_mission_control_dup_singleton ();
310   account = empathy_contact_get_account (contact);
311   connection = mission_control_get_tpconnection (mc, account, NULL);
312   g_object_unref (mc);
313
314   tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
315
316   request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
317       (GDestroyNotify) tp_g_value_slice_free);
318
319   /* org.freedesktop.Telepathy.Channel.ChannelType */
320   value = tp_g_value_slice_new (G_TYPE_STRING);
321   g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE);
322   g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
323
324   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
325   value = tp_g_value_slice_new (G_TYPE_UINT);
326   g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
327   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
328
329   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
330   value = tp_g_value_slice_new (G_TYPE_UINT);
331   g_value_set_uint (value, empathy_contact_get_handle (contact));
332   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
333
334   /* org.freedesktop.Telepathy.Channel.Type.StreamTube.Service */
335   value = tp_g_value_slice_new (G_TYPE_STRING);
336   g_value_set_string (value, service);
337   g_hash_table_insert (request,
338     EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE  ".Service", value);
339
340   if (!tp_cli_connection_interface_requests_run_create_channel (connection, -1,
341     request, &object_path, &channel_properties, &error, NULL))
342     {
343       DEBUG ("Error requesting channel: %s", error->message);
344       g_clear_error (&error);
345       g_object_unref (connection);
346       return NULL;
347     }
348
349   DEBUG ("Offering a new stream tube");
350
351   channel = tp_channel_new_from_properties (connection, object_path,
352       channel_properties, NULL);
353
354   tp_channel_run_until_ready (channel, NULL, NULL);
355
356   #define ADDRESS_TYPE dbus_g_type_get_struct ("GValueArray",\
357       G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INVALID)
358   params = g_hash_table_new (g_str_hash, g_str_equal);
359   address = tp_g_value_slice_new (ADDRESS_TYPE);
360   g_value_take_boxed (address, dbus_g_type_specialized_construct (ADDRESS_TYPE));
361   dbus_g_type_struct_set (address, 0, hostname, 1, port, G_MAXUINT);
362   control_param = tp_g_value_slice_new (G_TYPE_STRING);
363
364   if (parameters == NULL)
365     /* Pass an empty dict as parameters */
366     parameters = g_hash_table_new (g_str_hash, g_str_equal);
367   else
368     g_hash_table_ref (parameters);
369
370   if (!emp_cli_channel_type_stream_tube_run_offer_stream_tube (
371         TP_PROXY(channel), -1, type, address,
372         TP_SOCKET_ACCESS_CONTROL_LOCALHOST, control_param, parameters,
373         &error, NULL))
374     {
375       DEBUG ("Couldn't offer tube: %s", error->message);
376       g_clear_error (&error);
377       goto OUT;
378     }
379
380   DEBUG ("Stream tube offered");
381
382   tube = empathy_tp_tube_new (channel);
383
384 OUT:
385   g_object_unref (channel);
386   g_free (object_path);
387   g_hash_table_destroy (request);
388   g_hash_table_destroy (channel_properties);
389   tp_g_value_slice_free (address);
390   tp_g_value_slice_free (control_param);
391   g_object_unref (connection);
392   g_hash_table_unref (parameters);
393
394   return tube;
395 }
396
397 static void
398 tp_tube_accept_stream_cb (TpProxy *proxy,
399                           const GValue *address,
400                           const GError *error,
401                           gpointer user_data,
402                           GObject *weak_object)
403 {
404   EmpathyTpTube *tube = EMPATHY_TP_TUBE (weak_object);
405   EmpathyTpTubeAcceptData *data = (EmpathyTpTubeAcceptData *)user_data;
406   EmpathyTpTubeAddress eaddress;
407
408   eaddress.type = data->type;
409
410   if (error)
411     {
412       DEBUG ("Error accepting tube: %s", error->message);
413       data->callback (tube, NULL, error, data->user_data);
414       return;
415     }
416
417   switch (eaddress.type)
418     {
419       case TP_SOCKET_ADDRESS_TYPE_UNIX:
420       case TP_SOCKET_ADDRESS_TYPE_ABSTRACT_UNIX:
421         eaddress.a.socket.path = g_value_get_boxed (address);
422         break;
423      case TP_SOCKET_ADDRESS_TYPE_IPV4:
424      case TP_SOCKET_ADDRESS_TYPE_IPV6:
425         dbus_g_type_struct_get (address,
426           0, &eaddress.a.inet.hostname,
427           1, &eaddress.a.inet.port, G_MAXUINT);
428         break;
429     }
430
431    data->callback (tube, &eaddress, NULL, data->user_data);
432 }
433
434 void
435 empathy_tp_tube_accept_stream_tube (EmpathyTpTube *tube,
436   TpSocketAddressType type, EmpatyTpTubeAcceptStreamTubeCb *callback,
437   gpointer user_data)
438 {
439   EmpathyTpTubePriv *priv = GET_PRIV (tube);
440   GValue *control_param;
441   EmpathyTpTubeAcceptData *data;
442
443   g_return_if_fail (EMPATHY_IS_TP_TUBE (tube));
444
445   DEBUG ("Accepting stream tube");
446   /* FIXME allow other acls */
447   control_param = tp_g_value_slice_new (G_TYPE_STRING);
448
449   data = new_empathy_tp_tube_accept_data (type, callback, user_data);
450
451   emp_cli_channel_type_stream_tube_call_accept_stream_tube (
452      TP_PROXY (priv->channel), -1, type, TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
453      control_param, tp_tube_accept_stream_cb, data,
454      free_empathy_tp_tube_accept_data, G_OBJECT (tube));
455
456   tp_g_value_slice_free (control_param);
457 }