]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-call.c
Updated Galician translations
[empathy.git] / libempathy / empathy-tp-call.c
1 /*
2  * Copyright (C) 2007 Elliot Fairweather
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Elliot Fairweather <elliot.fairweather@collabora.co.uk>
20  *          Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <string.h>
24
25 #include <telepathy-glib/proxy-subclass.h>
26 #include <telepathy-glib/dbus.h>
27 #include <telepathy-glib/interfaces.h>
28 #include <telepathy-glib/util.h>
29
30 #include "empathy-tp-call.h"
31 #include "empathy-tp-contact-factory.h"
32 #include "empathy-utils.h"
33 #include "empathy-marshal.h"
34
35 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
36 #include "empathy-debug.h"
37
38 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpCall)
39 typedef struct
40 {
41   gboolean dispose_has_run;
42   TpChannel *channel;
43   EmpathyContact *contact;
44   gboolean is_incoming;
45   guint status;
46
47   EmpathyTpCallStream *audio;
48   EmpathyTpCallStream *video;
49 } EmpathyTpCallPriv;
50
51 /* signal enum */
52 enum {
53   AUDIO_STREAM_ERROR,
54   VIDEO_STREAM_ERROR,
55   LAST_SIGNAL
56 };
57
58 static guint signals[LAST_SIGNAL] = {0};
59
60 enum
61 {
62   PROP_0,
63   PROP_CHANNEL,
64   PROP_CONTACT,
65   PROP_STATUS,
66   PROP_AUDIO_STREAM,
67   PROP_VIDEO_STREAM
68 };
69
70 G_DEFINE_TYPE (EmpathyTpCall, empathy_tp_call, G_TYPE_OBJECT)
71
72 static void
73 tp_call_add_stream (EmpathyTpCall *call,
74                     guint stream_id,
75                     guint contact_handle,
76                     guint stream_type,
77                     guint stream_state,
78                     guint stream_direction)
79 {
80   EmpathyTpCallPriv *priv = GET_PRIV (call);
81
82   switch (stream_type)
83     {
84       case TP_MEDIA_STREAM_TYPE_AUDIO:
85         DEBUG ("Audio stream - id: %d, state: %d, direction: %d",
86             stream_id, stream_state, stream_direction);
87         priv->audio->exists = TRUE;
88         priv->audio->id = stream_id;
89         priv->audio->state = stream_state;
90         priv->audio->direction = stream_direction;
91         g_object_notify (G_OBJECT (call), "audio-stream");
92         break;
93       case TP_MEDIA_STREAM_TYPE_VIDEO:
94         DEBUG ("Video stream - id: %d, state: %d, direction: %d",
95             stream_id, stream_state, stream_direction);
96         priv->video->exists = TRUE;
97         priv->video->id = stream_id;
98         priv->video->state = stream_state;
99         priv->video->direction = stream_direction;
100         g_object_notify (G_OBJECT (call), "video-stream");
101         break;
102       default:
103         DEBUG ("Unknown stream type: %d", stream_type);
104     }
105 }
106
107 static void
108 tp_call_stream_added_cb (TpChannel *channel,
109                          guint stream_id,
110                          guint contact_handle,
111                          guint stream_type,
112                          gpointer user_data,
113                          GObject *call)
114 {
115   DEBUG ("Stream added - stream id: %d, contact handle: %d, stream type: %d",
116       stream_id, contact_handle, stream_type);
117
118   tp_call_add_stream (EMPATHY_TP_CALL (call), stream_id, contact_handle,
119       stream_type, TP_MEDIA_STREAM_STATE_DISCONNECTED,
120       TP_MEDIA_STREAM_DIRECTION_NONE);
121 }
122
123 static void
124 tp_call_stream_removed_cb (TpChannel *channel,
125                            guint stream_id,
126                            gpointer user_data,
127                            GObject *call)
128 {
129   EmpathyTpCallPriv *priv = GET_PRIV (call);
130
131   DEBUG ("Stream removed - stream id: %d", stream_id);
132
133   if (stream_id == priv->audio->id)
134     {
135       priv->audio->exists = FALSE;
136       g_object_notify (call, "audio-stream");
137     }
138   else if (stream_id == priv->video->id)
139     {
140       priv->video->exists = FALSE;
141       g_object_notify (call, "video-stream");
142     }
143 }
144
145 static void
146 tp_call_stream_state_changed_cb (TpChannel *proxy,
147                                  guint stream_id,
148                                  guint stream_state,
149                                  gpointer user_data,
150                                  GObject *call)
151 {
152   EmpathyTpCallPriv *priv = GET_PRIV (call);
153
154   DEBUG ("Stream state changed - stream id: %d, state state: %d",
155       stream_id, stream_state);
156
157   if (stream_id == priv->audio->id)
158     {
159       priv->audio->state = stream_state;
160       g_object_notify (call, "audio-stream");
161     }
162   else if (stream_id == priv->video->id)
163     {
164       priv->video->state = stream_state;
165       g_object_notify (call, "video-stream");
166     }
167 }
168
169 static void
170 tp_call_stream_direction_changed_cb (TpChannel *channel,
171                                      guint stream_id,
172                                      guint stream_direction,
173                                      guint pending_flags,
174                                      gpointer user_data,
175                                      GObject *call)
176 {
177   EmpathyTpCallPriv *priv = GET_PRIV (call);
178
179   DEBUG ("Stream direction changed - stream: %d, direction: %d",
180       stream_id, stream_direction);
181
182   if (stream_id == priv->audio->id)
183     {
184       priv->audio->direction = stream_direction;
185       g_object_notify (call, "audio-stream");
186     }
187   else if (stream_id == priv->video->id)
188     {
189       priv->video->direction = stream_direction;
190       g_object_notify (call, "video-stream");
191     }
192 }
193
194 static void
195 tp_call_request_streams_cb (TpChannel *channel,
196                             const GPtrArray *streams,
197                             const GError *error,
198                             gpointer user_data,
199                             GObject *call)
200 {
201   guint i;
202
203   if (error)
204     {
205       DEBUG ("Error requesting streams: %s", error->message);
206       return;
207     }
208
209   for (i = 0; i < streams->len; i++)
210     {
211       GValueArray *values;
212       guint stream_id;
213       guint contact_handle;
214       guint stream_type;
215       guint stream_state;
216       guint stream_direction;
217
218       values = g_ptr_array_index (streams, i);
219       stream_id = g_value_get_uint (g_value_array_get_nth (values, 0));
220       contact_handle = g_value_get_uint (g_value_array_get_nth (values, 1));
221       stream_type = g_value_get_uint (g_value_array_get_nth (values, 2));
222       stream_state = g_value_get_uint (g_value_array_get_nth (values, 3));
223       stream_direction = g_value_get_uint (g_value_array_get_nth (values, 4));
224
225       tp_call_add_stream (EMPATHY_TP_CALL (call), stream_id, contact_handle,
226           stream_type, stream_state, stream_direction);
227   }
228 }
229
230 static void
231 tp_call_request_streams_for_capabilities (EmpathyTpCall *call,
232                                           EmpathyCapabilities capabilities)
233 {
234   EmpathyTpCallPriv *priv = GET_PRIV (call);
235   GArray *stream_types;
236   guint handle;
237   guint stream_type;
238
239   if (capabilities == EMPATHY_CAPABILITIES_UNKNOWN)
240       capabilities = EMPATHY_CAPABILITIES_AUDIO | EMPATHY_CAPABILITIES_VIDEO;
241
242   DEBUG ("Requesting new stream for capabilities %d",
243       capabilities);
244
245   stream_types = g_array_new (FALSE, FALSE, sizeof (guint));
246   handle = empathy_contact_get_handle (priv->contact);
247
248   if (capabilities & EMPATHY_CAPABILITIES_AUDIO)
249     {
250       stream_type = TP_MEDIA_STREAM_TYPE_AUDIO;
251       g_array_append_val (stream_types, stream_type);
252     }
253   if (capabilities & EMPATHY_CAPABILITIES_VIDEO)
254     {
255       stream_type = TP_MEDIA_STREAM_TYPE_VIDEO;
256       g_array_append_val (stream_types, stream_type);
257     }
258
259   tp_cli_channel_type_streamed_media_call_request_streams (priv->channel, -1,
260       handle, stream_types, tp_call_request_streams_cb, NULL, NULL,
261       G_OBJECT (call));
262
263   g_array_free (stream_types, TRUE);
264 }
265
266 static void
267 tp_call_got_contact_cb (TpConnection            *connection,
268                         EmpathyContact          *contact,
269                         const GError            *error,
270                         gpointer                 user_data,
271                         GObject                 *call)
272 {
273   EmpathyTpCallPriv *priv = GET_PRIV (call);
274
275   if (error)
276     {
277       DEBUG ("Error: %s", error->message);
278       return;
279     }
280
281   priv->contact = g_object_ref (contact);
282
283   if (priv->status < EMPATHY_TP_CALL_STATUS_PENDING)
284     {
285       priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
286       g_object_notify (G_OBJECT (call), "status");
287     }
288
289   g_object_notify (G_OBJECT (call), "contact");
290 }
291
292 static void
293 tp_call_update_status (EmpathyTpCall *call)
294 {
295   EmpathyTpCallPriv *priv = GET_PRIV (call);
296   TpHandle self_handle;
297   const TpIntSet *set;
298   TpIntSetIter iter;
299
300   g_object_ref (call);
301
302   self_handle = tp_channel_group_get_self_handle (priv->channel);
303   set = tp_channel_group_get_members (priv->channel);
304   tp_intset_iter_init (&iter, set);
305   while (tp_intset_iter_next (&iter))
306     {
307       if (priv->status == EMPATHY_TP_CALL_STATUS_PENDING &&
308           ((priv->is_incoming && iter.element == self_handle) ||
309            (!priv->is_incoming && iter.element != self_handle)))
310         {
311           priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
312           g_object_notify (G_OBJECT (call), "status");
313         }
314     }
315
316   g_object_unref (call);
317 }
318
319 static void
320 tp_call_channel_invalidated_cb (TpChannel     *channel,
321                                 GQuark         domain,
322                                 gint           code,
323                                 gchar         *message,
324                                 EmpathyTpCall *call)
325 {
326   EmpathyTpCallPriv *priv = GET_PRIV (call);
327
328   DEBUG ("Channel invalidated: %s", message);
329   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
330   g_object_notify (G_OBJECT (call), "status");
331 }
332
333 static void
334 tp_call_async_cb (TpProxy *proxy,
335                   const GError *error,
336                   gpointer user_data,
337                   GObject *call)
338 {
339   if (error)
340       DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
341 }
342
343 static void
344 tp_call_stream_error_cb (TpChannel *channel,
345     guint stream_id,
346     guint error_code,
347     const gchar *msg,
348     gpointer user_data,
349     GObject *call)
350 {
351   EmpathyTpCall *self = EMPATHY_TP_CALL (call);
352   EmpathyTpCallPriv *priv = GET_PRIV (self);
353
354   DEBUG ("Stream error on stream %u: %s (code: %u)", stream_id, msg,
355       error_code);
356
357   if (priv->audio->id == stream_id)
358     {
359       g_signal_emit (call, signals[AUDIO_STREAM_ERROR], 0, error_code, msg);
360     }
361   else if (priv->video->id == stream_id)
362     {
363       g_signal_emit (call, signals[VIDEO_STREAM_ERROR], 0, error_code, msg);
364     }
365   else
366     {
367       DEBUG ("Unknown stream id: %u", stream_id);
368     }
369 }
370
371 static GObject *
372 tp_call_constructor (GType type,
373                      guint n_construct_params,
374                      GObjectConstructParam *construct_params)
375 {
376   GObject *object;
377   EmpathyTpCall *call;
378   EmpathyTpCallPriv *priv;
379   GHashTable *props;
380   gboolean requested;
381
382   object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
383       n_construct_params, construct_params);
384
385   call = EMPATHY_TP_CALL (object);
386   priv = GET_PRIV (call);
387
388   /* Setup streamed media channel */
389   g_signal_connect (priv->channel, "invalidated",
390       G_CALLBACK (tp_call_channel_invalidated_cb), call);
391   tp_cli_channel_type_streamed_media_connect_to_stream_added (priv->channel,
392       tp_call_stream_added_cb, NULL, NULL, G_OBJECT (call), NULL);
393   tp_cli_channel_type_streamed_media_connect_to_stream_removed (priv->channel,
394       tp_call_stream_removed_cb, NULL, NULL, G_OBJECT (call), NULL);
395   tp_cli_channel_type_streamed_media_connect_to_stream_state_changed (priv->channel,
396       tp_call_stream_state_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
397   tp_cli_channel_type_streamed_media_connect_to_stream_direction_changed (priv->channel,
398       tp_call_stream_direction_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
399   tp_cli_channel_type_streamed_media_connect_to_stream_error (priv->channel,
400       tp_call_stream_error_cb, NULL, NULL, G_OBJECT (call), NULL);
401   tp_cli_channel_type_streamed_media_call_list_streams (priv->channel, -1,
402       tp_call_request_streams_cb, NULL, NULL, G_OBJECT (call));
403
404   /* Is the call incoming? */
405   props = tp_channel_borrow_immutable_properties (priv->channel);
406   requested = tp_asv_get_boolean (props, TP_PROP_CHANNEL_REQUESTED, NULL);
407
408   priv->is_incoming = !requested;
409
410   /* Get the remote contact */
411   empathy_tp_contact_factory_get_from_handle (
412       tp_channel_borrow_connection (priv->channel),
413       tp_channel_get_handle (priv->channel, NULL), tp_call_got_contact_cb,
414       NULL, NULL, object);
415
416   /* Update status when members changes */
417   tp_call_update_status (call);
418   tp_g_signal_connect_object (priv->channel, "group-members-changed",
419       G_CALLBACK (tp_call_update_status), call, G_CONNECT_SWAPPED);
420
421   return object;
422 }
423 static void
424 tp_call_dispose (GObject *object)
425 {
426   EmpathyTpCallPriv *priv = GET_PRIV (object);
427
428   DEBUG ("Disposing: %p, %d", object, priv->dispose_has_run);
429
430   if (priv->dispose_has_run)
431     return;
432
433   priv->dispose_has_run = TRUE;
434
435   if (priv->channel != NULL)
436     {
437       g_signal_handlers_disconnect_by_func (priv->channel,
438         tp_call_channel_invalidated_cb, object);
439
440       g_object_unref (priv->channel);
441       priv->channel = NULL;
442     }
443
444   if (priv->contact != NULL)
445       g_object_unref (priv->contact);
446
447   if (G_OBJECT_CLASS (empathy_tp_call_parent_class)->dispose)
448     G_OBJECT_CLASS (empathy_tp_call_parent_class)->dispose (object);
449 }
450
451 static void
452 tp_call_finalize (GObject *object)
453 {
454   EmpathyTpCallPriv *priv = GET_PRIV (object);
455
456   DEBUG ("Finalizing: %p", object);
457
458   g_slice_free (EmpathyTpCallStream, priv->audio);
459   g_slice_free (EmpathyTpCallStream, priv->video);
460
461   (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
462 }
463
464 static void
465 tp_call_set_property (GObject *object,
466                       guint prop_id,
467                       const GValue *value,
468                       GParamSpec *pspec)
469 {
470   EmpathyTpCallPriv *priv = GET_PRIV (object);
471
472   switch (prop_id)
473     {
474     case PROP_CHANNEL:
475       priv->channel = g_value_dup_object (value);
476       break;
477     default:
478       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
479       break;
480   }
481 }
482
483 static void
484 tp_call_get_property (GObject *object,
485                       guint prop_id,
486                       GValue *value,
487                       GParamSpec *pspec)
488 {
489   EmpathyTpCallPriv *priv = GET_PRIV (object);
490
491   switch (prop_id)
492     {
493     case PROP_CHANNEL:
494       g_value_set_object (value, priv->channel);
495       break;
496     case PROP_CONTACT:
497       g_value_set_object (value, priv->contact);
498       break;
499     case PROP_STATUS:
500       g_value_set_uint (value, priv->status);
501       break;
502     case PROP_AUDIO_STREAM:
503       g_value_set_pointer (value, priv->audio);
504       break;
505     case PROP_VIDEO_STREAM:
506       g_value_set_pointer (value, priv->video);
507       break;
508     default:
509       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
510       break;
511   }
512 }
513
514 static void
515 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
516 {
517   GObjectClass *object_class = G_OBJECT_CLASS (klass);
518
519   object_class->constructor = tp_call_constructor;
520   object_class->dispose = tp_call_dispose;
521   object_class->finalize = tp_call_finalize;
522   object_class->set_property = tp_call_set_property;
523   object_class->get_property = tp_call_get_property;
524
525   g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
526
527   g_object_class_install_property (object_class, PROP_CHANNEL,
528       g_param_spec_object ("channel", "channel", "channel",
529       TP_TYPE_CHANNEL,
530       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
531       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
532   g_object_class_install_property (object_class, PROP_CONTACT,
533       g_param_spec_object ("contact", "Call contact", "Call contact",
534       EMPATHY_TYPE_CONTACT,
535       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
536   g_object_class_install_property (object_class, PROP_STATUS,
537       g_param_spec_uint ("status", "Call status",
538       "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
539       G_PARAM_STATIC_BLURB));
540   g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
541       g_param_spec_pointer ("audio-stream", "Audio stream data",
542       "Audio stream data",
543       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
544   g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
545       g_param_spec_pointer ("video-stream", "Video stream data",
546       "Video stream data",
547       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
548
549   signals[AUDIO_STREAM_ERROR] =
550     g_signal_new ("audio-stream-error",
551       G_TYPE_FROM_CLASS (klass),
552       G_SIGNAL_RUN_LAST, 0,
553       NULL, NULL,
554       _empathy_marshal_VOID__UINT_STRING,
555       G_TYPE_NONE,
556       2, G_TYPE_UINT, G_TYPE_STRING);
557
558   signals[VIDEO_STREAM_ERROR] =
559     g_signal_new ("video-stream-error",
560       G_TYPE_FROM_CLASS (klass),
561       G_SIGNAL_RUN_LAST, 0,
562       NULL, NULL,
563       _empathy_marshal_VOID__UINT_STRING,
564       G_TYPE_NONE,
565       2, G_TYPE_UINT, G_TYPE_STRING);
566 }
567
568 static void
569 empathy_tp_call_init (EmpathyTpCall *call)
570 {
571   EmpathyTpCallPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (call,
572     EMPATHY_TYPE_TP_CALL, EmpathyTpCallPriv);
573
574   call->priv = priv;
575   priv->status = EMPATHY_TP_CALL_STATUS_READYING;
576   priv->contact = NULL;
577   priv->audio = g_slice_new0 (EmpathyTpCallStream);
578   priv->video = g_slice_new0 (EmpathyTpCallStream);
579   priv->audio->exists = FALSE;
580   priv->video->exists = FALSE;
581 }
582
583 EmpathyTpCall *
584 empathy_tp_call_new (TpChannel *channel)
585 {
586   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
587
588   return g_object_new (EMPATHY_TYPE_TP_CALL,
589       "channel", channel,
590       NULL);
591 }
592
593 void
594 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
595 {
596   EmpathyTpCallPriv *priv = GET_PRIV (call);
597   TpHandle self_handle;
598   GArray handles = {(gchar *) &self_handle, 1};
599
600   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
601   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_PENDING);
602
603   if (!priv->is_incoming)
604     return;
605
606   DEBUG ("Accepting incoming call");
607
608   self_handle = tp_channel_group_get_self_handle (priv->channel);
609   tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
610       &handles, NULL, NULL, NULL, NULL, NULL);
611 }
612
613 void
614 empathy_tp_call_close (EmpathyTpCall *call)
615 {
616   EmpathyTpCallPriv *priv = GET_PRIV (call);
617
618   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
619
620   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
621       return;
622
623   DEBUG ("Closing channel");
624
625   tp_cli_channel_call_close (priv->channel, -1,
626       NULL, NULL, NULL, NULL);
627
628   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
629   g_object_notify (G_OBJECT (call), "status");
630 }
631
632 void
633 empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
634                                                 gboolean is_sending)
635 {
636   EmpathyTpCallPriv *priv = GET_PRIV (call);
637   guint new_direction;
638
639   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
640   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
641
642   DEBUG ("Requesting video stream direction - is_sending: %d", is_sending);
643
644   if (!priv->video->exists)
645     {
646       if (is_sending)
647           tp_call_request_streams_for_capabilities (call,
648               EMPATHY_CAPABILITIES_VIDEO);
649       return;
650     }
651
652   if (is_sending)
653       new_direction = priv->video->direction | TP_MEDIA_STREAM_DIRECTION_SEND;
654   else
655       new_direction = priv->video->direction & ~TP_MEDIA_STREAM_DIRECTION_SEND;
656
657   tp_cli_channel_type_streamed_media_call_request_stream_direction (priv->channel,
658       -1, priv->video->id, new_direction,
659       (tp_cli_channel_type_streamed_media_callback_for_request_stream_direction)
660       tp_call_async_cb, NULL, NULL, G_OBJECT (call));
661 }
662
663 void
664 empathy_tp_call_start_tone (EmpathyTpCall *call, TpDTMFEvent event)
665 {
666   EmpathyTpCallPriv *priv = GET_PRIV (call);
667
668   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
669   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
670
671   if (!priv->audio->exists)
672       return;
673
674   tp_cli_channel_interface_dtmf_call_start_tone (priv->channel, -1,
675       priv->audio->id, event,
676       (tp_cli_channel_interface_dtmf_callback_for_start_tone) tp_call_async_cb,
677       "starting tone", NULL, G_OBJECT (call));
678 }
679
680 void
681 empathy_tp_call_stop_tone (EmpathyTpCall *call)
682 {
683   EmpathyTpCallPriv *priv = GET_PRIV (call);
684
685   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
686   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
687
688   if (!priv->audio->exists)
689       return;
690
691   tp_cli_channel_interface_dtmf_call_stop_tone (priv->channel, -1,
692       priv->audio->id,
693       (tp_cli_channel_interface_dtmf_callback_for_stop_tone) tp_call_async_cb,
694       "stoping tone", NULL, G_OBJECT (call));
695 }
696
697 gboolean
698 empathy_tp_call_has_dtmf (EmpathyTpCall *call)
699 {
700   EmpathyTpCallPriv *priv = GET_PRIV (call);
701
702   g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
703
704   return tp_proxy_has_interface_by_id (priv->channel,
705       TP_IFACE_QUARK_CHANNEL_INTERFACE_DTMF);
706 }
707
708 /**
709  * empathy_tp_call_is_receiving_video:
710  * @call: the call
711  *
712  * Indicates if the call is receiving video or not.
713  *
714  * Returns: %TRUE if the call is currently receiving video, %FALSE otherwise.
715  */
716 gboolean
717 empathy_tp_call_is_receiving_video (EmpathyTpCall *call)
718 {
719   EmpathyTpCallPriv *priv = GET_PRIV (call);
720
721   g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
722
723   if (!priv->video->exists)
724     return FALSE;
725
726   return priv->video->direction & TP_MEDIA_STREAM_DIRECTION_RECEIVE ?
727       TRUE : FALSE;
728 }
729
730 /**
731  * empathy_tp_call_is_sending_video:
732  * @call: the call
733  *
734  * Indicates if the call is sending video or not.
735  *
736  * Returns: %TRUE if the call is currently sending video, %FALSE otherwise.
737  */
738 gboolean
739 empathy_tp_call_is_sending_video (EmpathyTpCall *call)
740 {
741   EmpathyTpCallPriv *priv = GET_PRIV (call);
742
743   g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
744
745   if (!priv->video->exists)
746     return FALSE;
747
748   return priv->video->direction & TP_MEDIA_STREAM_DIRECTION_SEND ?
749       TRUE : FALSE;
750 }
751
752 const gchar *
753 empathy_tp_call_get_connection_manager (EmpathyTpCall *self)
754 {
755   EmpathyTpCallPriv *priv = GET_PRIV (self);
756   TpConnection *conn;
757   TpAccount *account;
758
759   if (priv->channel == NULL)
760     return NULL;
761
762   conn = tp_channel_borrow_connection (priv->channel);
763   if (conn == NULL)
764     return NULL;
765
766   account = empathy_get_account_for_connection (conn);
767   if (account == NULL)
768     return NULL;
769
770   return tp_account_get_connection_manager (account);
771 }
772
773 gboolean
774 empathy_tp_call_has_initial_video (EmpathyTpCall *self)
775 {
776   EmpathyTpCallPriv *priv = GET_PRIV (self);
777   GHashTable *props;
778   gboolean initial_video;
779   gboolean valid;
780
781   if (priv->channel == NULL)
782     return FALSE;
783
784   g_object_get (priv->channel, "channel-properties", &props, NULL);
785
786   initial_video = tp_asv_get_boolean (props,
787     TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialVideo", &valid);
788   if (!valid)
789     initial_video = FALSE;
790
791   g_hash_table_unref (props);
792   return initial_video;
793 }
794
795 static void
796 leave_remove_members_cb (TpChannel *proxy,
797     const GError *error,
798     gpointer user_data,
799     GObject *weak_object)
800 {
801   EmpathyTpCall *self = user_data;
802
803   if (error == NULL)
804     return;
805
806   DEBUG ("RemoveMembers failed (%s); closing the channel", error->message);
807   empathy_tp_call_close (self);
808 }
809
810 void
811 empathy_tp_call_leave (EmpathyTpCall *self)
812 {
813   EmpathyTpCallPriv *priv = GET_PRIV (self);
814   TpHandle self_handle;
815   GArray array = { (gchar *) &self_handle, 1 };
816
817   if (!tp_proxy_has_interface_by_id (priv->channel,
818         TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
819     {
820       empathy_tp_call_close (self);
821       return;
822     }
823
824   self_handle = tp_channel_group_get_self_handle (priv->channel);
825   if (self_handle == 0)
826     {
827       /* we are not member of the channel */
828       empathy_tp_call_close (self);
829       return;
830     }
831
832   tp_cli_channel_interface_group_call_remove_members (priv->channel, -1, &array,
833       "", leave_remove_members_cb, self, NULL, G_OBJECT (self));
834 }
835
836 EmpathyTpCallStatus
837 empathy_tp_call_get_status (EmpathyTpCall *self)
838 {
839   EmpathyTpCallPriv *priv = GET_PRIV (self);
840
841   return priv->status;
842 }