summaryrefslogtreecommitdiff
path: root/telepathy-yell/call-channel.c
blob: 8e91adf044b3b76229c9a4f36ecd3a80e31822e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
/*
 * call-channel.c - Source for TpyCallChannel
 * Copyright (C) 2011 Collabora Ltd. <http://www.collabora.co.uk/>
 * @author Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * SECTION:call-channel
 * @title: TpyCallChannel
 * @short_description: help class for a call channel
 *
 * #TpyCallChannel is a sub-class of #TpChannel providing convenient API
 * to make audio and video calls.
 *
 * Since:
 */

/**
 * TpyCallChannel:
 *
 * Data structure representing a #TpyCallChannel.
 *
 * Since:
 */

/**
 * TpyCallChannelClass:
 *
 * The class of a #TpyCallChannel.
 *
 * Since:
 */

#include <config.h>

#include "telepathy-yell/call-channel.h"
#include "telepathy-yell/call-stream.h"

#include <telepathy-glib/dbus.h>
#include <telepathy-glib/enums.h>
#include <telepathy-glib/gtypes.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/proxy-subclass.h>
#include <telepathy-glib/util.h>

#include "extensions.h"
#include "interfaces.h"
#include "_gen/signals-marshal.h"

#define DEBUG_FLAG TPY_DEBUG_CALL
#include "debug.h"

G_DEFINE_TYPE (TpyCallChannel, tpy_call_channel, TP_TYPE_CHANNEL)

struct _TpyCallChannelPrivate
{
  TpyCallState state;
  TpyCallFlags flags;
  GHashTable *details;

  gboolean initial_audio;
  gboolean initial_video;

  /* Hash of Handle => CallMemberFlags */
  GHashTable *members;

  /* Array of TpyCallContents */
  GPtrArray *contents;

  GSimpleAsyncResult *result;
};

enum /* props */
{
  PROP_CONTENTS = 1,
  PROP_STATE,
  PROP_STATE_DETAILS,
  PROP_STATE_REASON,
  PROP_FLAGS,
  PROP_HARDWARE_STREAMING,
  PROP_MEMBERS,
  PROP_INITIAL_TRANSPORT,
  PROP_INITIAL_AUDIO,
  PROP_INITIAL_AUDIO_NAME,
  PROP_INITIAL_VIDEO,
  PROP_INITIAL_VIDEO_NAME,
  PROP_MUTABLE_CONTENTS
};

enum /* signals */
{
  CONTENT_ADDED,
  CONTENT_REMOVED,
  STATE_CHANGED,
  MEMBERS_CHANGED,
  LAST_SIGNAL
};

static guint _signals[LAST_SIGNAL] = { 0, };

static void
on_content_added_cb (TpProxy *proxy,
    const gchar *content_path,
    gpointer user_data,
    GObject *weak_object)
{
  TpyCallChannel *self = TPY_CALL_CHANNEL (proxy);
  TpyCallContent *content;

  DEBUG ("Content added: %s", content_path);

  content = g_object_new (TPY_TYPE_CALL_CONTENT,
          "bus-name", tp_proxy_get_bus_name (self),
          "dbus-daemon", tp_proxy_get_dbus_daemon (self),
          "dbus-connection", tp_proxy_get_dbus_connection (self),
          "object-path", content_path,
          NULL);

  if (content == NULL)
    {
      g_warning ("Could not create a CallContent for path %s", content_path);
      return;
    }

  g_ptr_array_add (self->priv->contents, content);

  g_signal_emit (self, _signals[CONTENT_ADDED], 0, content);
}

static void
on_content_removed_cb (TpProxy *proxy,
    const gchar *content_path,
    gpointer user_data,
    GObject *weak_object)
{
  TpyCallChannel *self = TPY_CALL_CHANNEL (proxy);
  TpyCallContent *content = NULL, *c;
  guint i;

  DEBUG ("Content removed: %s", content_path);

  for (i = 0; i < self->priv->contents->len; i++)
    {
      c = g_ptr_array_index (self->priv->contents, i);
      if (g_strcmp0 (tp_proxy_get_object_path (c), content_path) == 0)
        {
          content = c;
          break;
        }
    }

  if (content != NULL)
    {
      g_signal_emit (self, _signals[CONTENT_REMOVED], 0, content);
      g_ptr_array_remove (self->priv->contents, content);
    }
  else
    {
      g_warning ("The removed content '%s' isn't in the call!", content_path);
    }
}

static void
on_call_members_changed_cb (TpProxy *proxy,
    GHashTable *flags_changed,
    const GArray *removed,
    gpointer user_data,
    GObject *weak_object)
{
  TpyCallChannel *self = TPY_CALL_CHANNEL (proxy);

  DEBUG ("Call members changed: %d members",
      g_hash_table_size (flags_changed));

  g_hash_table_unref (self->priv->members);
  self->priv->members = g_hash_table_ref (flags_changed);

  g_signal_emit (self, _signals[MEMBERS_CHANGED], 0, self->priv->members);
}

static void
on_call_state_changed_cb (TpProxy *proxy,
    guint call_state,
    guint call_flags,
    const GValueArray *call_state_reason,
    GHashTable *call_state_details,
    gpointer user_data,
    GObject *weak_object)
{
  TpyCallChannel *self = TPY_CALL_CHANNEL (proxy);

  DEBUG ("Call state changed");

  self->priv->state = call_state;
  self->priv->flags = call_flags;

  tp_clear_pointer (&self->priv->details, g_hash_table_unref);
  self->priv->details = g_hash_table_ref (call_state_details);

  g_signal_emit (self, _signals[STATE_CHANGED], 0,
      call_state, call_flags, call_state_reason, call_state_details);
}

static void
on_call_channel_get_all_properties_cb (TpProxy *proxy,
    GHashTable *properties,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  TpyCallChannel *self = TPY_CALL_CHANNEL (proxy);
  GHashTable *hash_table;
  GPtrArray *contents;
  guint i;

  if (error != NULL)
    {
      g_warning ("Could not get the channel properties: %s", error->message);
      return;
    }

  self->priv->state = tp_asv_get_uint32 (properties,
      "CallState", NULL);
  self->priv->flags = tp_asv_get_uint32 (properties,
      "CallFlags", NULL);
  self->priv->initial_audio = tp_asv_get_boolean (properties,
      "InitialAudio", NULL);
  self->priv->initial_video = tp_asv_get_boolean (properties,
      "InitialVideo", NULL);

  hash_table = tp_asv_get_boxed (properties,
      "CallStateDetails", TP_HASH_TYPE_STRING_VARIANT_MAP);
  if (hash_table != NULL)
    self->priv->details = g_boxed_copy (TP_HASH_TYPE_STRING_VARIANT_MAP,
        hash_table);

  hash_table = tp_asv_get_boxed (properties,
      "CallMembers", TPY_HASH_TYPE_CALL_MEMBER_MAP);
  if (hash_table != NULL)
    self->priv->members = g_boxed_copy (TPY_HASH_TYPE_CALL_MEMBER_MAP,
        hash_table);

  contents = tp_asv_get_boxed (properties,
      "Contents", TP_ARRAY_TYPE_OBJECT_PATH_LIST);

  for (i = 0; i < contents->len; i++)
    {
      const gchar *content_path = g_ptr_array_index (contents, i);
      TpyCallContent *content;

      DEBUG ("Content added: %s", content_path);

      content = g_object_new (TPY_TYPE_CALL_CONTENT,
              "bus-name", tp_proxy_get_bus_name (self),
              "dbus-daemon", tp_proxy_get_dbus_daemon (self),
              "dbus-connection", tp_proxy_get_dbus_connection (self),
              "object-path", content_path,
              NULL);

      if (content == NULL)
        {
          g_warning ("Could not create a CallContent for path %s", content_path);
          return;
        }

      g_ptr_array_add (self->priv->contents, content);
    }

  g_signal_emit (self, _signals[MEMBERS_CHANGED], 0, self->priv->members);
}

static void
tpy_call_channel_dispose (GObject *obj)
{
  TpyCallChannel *self = (TpyCallChannel *) obj;

  tp_clear_pointer (&self->priv->contents, g_ptr_array_unref);
  tp_clear_pointer (&self->priv->details, g_hash_table_unref);

  tp_clear_object (&self->priv->result);

  G_OBJECT_CLASS (tpy_call_channel_parent_class)->dispose (obj);
}

static void
tpy_call_channel_get_property (GObject *object,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  TpyCallChannel *self = (TpyCallChannel *) object;

  switch (property_id)
    {
      case PROP_CONTENTS:
        g_value_set_boxed (value, self->priv->contents);
        break;

      case PROP_STATE:
        g_value_set_uint (value, self->priv->state);
        break;

      case PROP_FLAGS:
        g_value_set_uint (value, self->priv->flags);
        break;

      case PROP_STATE_DETAILS:
        g_value_set_boxed (value, self->priv->details);
        break;

      case PROP_MEMBERS:
        g_value_set_boxed (value, self->priv->members);
        break;

      case PROP_INITIAL_AUDIO:
        g_value_set_boolean (value, self->priv->initial_audio);
        break;

      case PROP_INITIAL_VIDEO:
        g_value_set_boolean (value, self->priv->initial_video);
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
tpy_call_channel_constructed (GObject *obj)
{
  TpyCallChannel *self = (TpyCallChannel *) obj;
  TpChannel *chan = (TpChannel *) obj;
  GError *err = NULL;

  ((GObjectClass *) tpy_call_channel_parent_class)->constructed (obj);

  if (tp_channel_get_channel_type_id (chan) !=
      TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
    {
      GError error = { TP_DBUS_ERRORS, TP_DBUS_ERROR_INCONSISTENT,
          "Channel is not a Call" };

      DEBUG ("Channel is not a Call: %s", tp_channel_get_channel_type (chan));

      tp_proxy_invalidate (TP_PROXY (self), &error);
      return;
    }

  tpy_cli_channel_type_call_connect_to_content_added (TP_PROXY (self),
      on_content_added_cb, NULL, NULL, NULL, &err);

  if (err != NULL)
    {
      g_critical ("Failed to connect to ContentAdded signal: %s",
          err->message);

      g_error_free (err);
      return;
    }

  tpy_cli_channel_type_call_connect_to_content_removed (TP_PROXY (self),
      on_content_removed_cb, NULL, NULL, NULL, &err);

  if (err != NULL)
    {
      g_critical ("Failed to connect to ContentRemoved signal: %s",
          err->message);

      g_error_free (err);
      return;
    }

  tpy_cli_channel_type_call_connect_to_call_state_changed (TP_PROXY (self),
      on_call_state_changed_cb, NULL, NULL, NULL, &err);

  if (err != NULL)
    {
      g_critical ("Failed to connect to CallStateChanged signal: %s",
          err->message);

      g_error_free (err);
      return;
    }

  tpy_cli_channel_type_call_connect_to_call_members_changed (TP_PROXY (self),
      on_call_members_changed_cb, NULL, NULL, NULL, &err);

  if (err != NULL)
    {
      g_critical ("Failed to connect to CallMembersChanged signal: %s",
          err->message);

      g_error_free (err);
      return;
    }

  tp_cli_dbus_properties_call_get_all (self, -1,
      TPY_IFACE_CHANNEL_TYPE_CALL,
      on_call_channel_get_all_properties_cb, NULL, NULL, NULL);
}

static void
tpy_call_channel_class_init (TpyCallChannelClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GParamSpec *param_spec;

  gobject_class->constructed = tpy_call_channel_constructed;
  gobject_class->get_property = tpy_call_channel_get_property;
  gobject_class->dispose = tpy_call_channel_dispose;

  g_type_class_add_private (klass, sizeof (TpyCallChannelPrivate));

  /**
   * TpyCallChannel:contents:
   *
   * The list of content objects that are part of this call.
   *
   * Since:
   */
  param_spec = g_param_spec_boxed ("contents", "Contents",
      "The content objects of this call",
      G_TYPE_PTR_ARRAY,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (gobject_class, PROP_CONTENTS, param_spec);

  /**
   * TpyCallChannel:state:
   *
   * A #TpChannelCallState specifying the state of the call.
   *
   * Since:
   */
  param_spec = g_param_spec_uint ("state", "Call state",
      "The state of the call",
      0, G_MAXUINT, 0,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (gobject_class, PROP_STATE, param_spec);

  /**
   * TpyCallChannel:flags:
   *
   * A #TpChannelCallFlags specifying the flags of the call.
   *
   * Since:
   */
  param_spec = g_param_spec_uint ("flags", "Call flags",
      "The flags for the call",
      0, G_MAXUINT, 0,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (gobject_class, PROP_FLAGS, param_spec);

  /**
   * TpyCallChannel:state-details:
   *
   * The list of content objects that are part of this call.
   *
   * Since:
   */
  param_spec = g_param_spec_boxed ("state-details", "State details",
      "The details of the call",
      G_TYPE_HASH_TABLE,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (gobject_class,
      PROP_STATE_DETAILS, param_spec);

  /**
   * TpyCallChannel:members:
   *
   * The call participants, and their respective flags.
   *
   * Since:
   */
  param_spec = g_param_spec_boxed ("members", "Call members",
      "The call participants",
      G_TYPE_HASH_TABLE,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (gobject_class,
      PROP_MEMBERS, param_spec);

  /**
   * TpyCallChannel:initial-audio:
   *
   * Whether or not the Call was started with audio.
   *
   * Since:
   */
  param_spec = g_param_spec_boolean ("initial-audio", "Initial audio",
      "Initial audio",
      FALSE,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (gobject_class,
      PROP_INITIAL_AUDIO, param_spec);

  /**
   * TpyCallChannel:initial-video:
   *
   * Whether or not the Call was started with video.
   *
   * Since:
   */
  param_spec = g_param_spec_boolean ("initial-video", "Initial video",
      "Initial video",
      FALSE,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (gobject_class,
      PROP_INITIAL_VIDEO, param_spec);

  /**
   * TpyCallChannel::content-added
   * @self: the #TpyCallChannel
   * @content: the newly added content
   *
   * The ::content-added signal is emitted whenever a
   * #TpyCallContent is added to @self.
   */
  _signals[CONTENT_ADDED] = g_signal_new ("content-added",
      G_OBJECT_CLASS_TYPE (klass),
      G_SIGNAL_RUN_LAST,
      0, NULL, NULL,
      g_cclosure_marshal_VOID__OBJECT,
      G_TYPE_NONE,
      1, G_TYPE_OBJECT);

  /**
   * TpyCallChannel::content-removed
   * @self: the #TpyCallChannel
   * @content: the newly removed content
   *
   * The ::content-removed signal is emitted whenever a
   * #TpyCallContent is removed from @self.
   */
  _signals[CONTENT_REMOVED] = g_signal_new ("content-removed",
      G_OBJECT_CLASS_TYPE (klass),
      G_SIGNAL_RUN_LAST,
      0, NULL, NULL,
      g_cclosure_marshal_VOID__OBJECT,
      G_TYPE_NONE,
      1, G_TYPE_OBJECT);

  /**
   * TpyCallChannel::state-changed
   * @self: the #TpyCallChannel
   * @state: the new #TpyCallState
   * @flags: the new #TpyCallFlags
   * @state_reason: the reason for the change
   * @state_details: additional details
   *
   * The ::state-changed signal is emitted whenever the
   * call state changes.
   */
  _signals[STATE_CHANGED] = g_signal_new ("state-changed",
      G_OBJECT_CLASS_TYPE (klass),
      G_SIGNAL_RUN_LAST,
      0, NULL, NULL,
      _tpy_marshal_VOID__UINT_UINT_BOXED_BOXED,
      G_TYPE_NONE,
      4, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_PTR_ARRAY, G_TYPE_HASH_TABLE);

  /**
   * TpyCallChannel::members-changed
   * @self: the #TpyCallChannel
   * @state: the new #TpyCallState
   * @flags: the new #TpyCallFlags
   *
   * The ::members-changed signal is emitted whenever participants
   * are added, removed, or their flags change.
   */
  _signals[MEMBERS_CHANGED] = g_signal_new ("members-changed",
      G_OBJECT_CLASS_TYPE (klass),
      G_SIGNAL_RUN_LAST,
      0, NULL, NULL,
      g_cclosure_marshal_VOID__BOXED,
      G_TYPE_NONE,
      1, G_TYPE_HASH_TABLE);
}

static void
tpy_call_channel_init (TpyCallChannel *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      TPY_TYPE_CALL_CHANNEL, TpyCallChannelPrivate);

  self->priv->contents = g_ptr_array_new_with_free_func (g_object_unref);
}

/**
 * tpy_call_channel_new:
 * @conn: a #TpConnection; may not be %NULL
 * @object_path: the object path of the channel; may not be %NULL
 * @immutable_properties: (transfer none) (element-type utf8 GObject.Value):
 *  the immutable properties of the channel,
 *  as signalled by the NewChannel D-Bus signal or returned by the
 *  CreateChannel and EnsureChannel D-Bus methods: a mapping from
 *  strings (D-Bus interface name + "." + property name) to #GValue instances
 * @error: used to indicate the error if %NULL is returned
 *
 * Convenient function to create a new #TpyCallChannel
 *
 * Returns: (transfer full): a newly created #TpyCallChannel
 *
 * Since:
 */
TpyCallChannel *
tpy_call_channel_new (TpConnection *conn,
    const gchar *object_path,
    const GHashTable *immutable_properties,
    GError **error)
{
  TpProxy *conn_proxy = (TpProxy *) conn;

  g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
  g_return_val_if_fail (object_path != NULL, NULL);
  g_return_val_if_fail (immutable_properties != NULL, NULL);

  if (!tp_dbus_check_valid_object_path (object_path, error))
    return NULL;

  return g_object_new (TPY_TYPE_CALL_CHANNEL,
      "connection", conn,
       "dbus-daemon", conn_proxy->dbus_daemon,
       "bus-name", conn_proxy->bus_name,
       "object-path", object_path,
       "handle-type", (guint) TP_UNKNOWN_HANDLE_TYPE,
       "channel-properties", immutable_properties,
       NULL);
}

static void
channel_accept_cb (TpProxy *proxy,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  TpyCallChannel *self = TPY_CALL_CHANNEL (proxy);

  if (error != NULL)
    {
      DEBUG ("Failed to accept call: %s", error->message);

      g_simple_async_result_set_from_error (self->priv->result, error);
    }

  g_simple_async_result_set_op_res_gboolean (self->priv->result, TRUE);
  g_simple_async_result_complete (self->priv->result);
  tp_clear_object (&self->priv->result);
}

/**
 * tpy_call_channel_accept_async:
 * @self: an incoming #TpyCallChannel
 * @callback: a callback to call
 * @user_data: data to pass to @callback
 *
 * Accept an incoming call. When the call has been accepted, @callback
 * will be called. You can then call tpy_call_channel_accept_finish()
 * to finish the operation.
 *
 * Since:
 */
void
tpy_call_channel_accept_async (TpyCallChannel *self,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  g_return_if_fail (TPY_IS_CALL_CHANNEL (self));
  g_return_if_fail (self->priv->result == NULL);

  self->priv->result = g_simple_async_result_new (G_OBJECT (self), callback,
      user_data, tpy_call_channel_accept_async);

  tpy_cli_channel_type_call_call_accept (TP_PROXY (self), -1,
      channel_accept_cb, NULL, NULL, G_OBJECT (self));
}

/**
 * tpy_call_channel_accept_finish:
 * @self: a #TpyCallChannel
 * @result: a #GAsyncResult
 * @error: a #GError to fill
 *
 * Finishes to accept a call.
 *
 * Since:
 */
gboolean
tpy_call_channel_accept_finish (TpyCallChannel *self,
    GAsyncResult *result,
    GError **error)
{
  if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
      error))
    return FALSE;

  g_return_val_if_fail (g_simple_async_result_is_valid (result,
    G_OBJECT (self), tpy_call_channel_accept_async),
    FALSE);

  return g_simple_async_result_get_op_res_gboolean (
    G_SIMPLE_ASYNC_RESULT (result));
}

static void
channel_hangup_cb (TpProxy *proxy,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  TpyCallChannel *self = TPY_CALL_CHANNEL (proxy);

  if (error != NULL)
    {
      DEBUG ("Failed to hang up: %s", error->message);

      g_simple_async_result_set_from_error (self->priv->result, error);
    }

  g_simple_async_result_complete (self->priv->result);
  tp_clear_object (&self->priv->result);
}

void
tpy_call_channel_hangup_async (TpyCallChannel *self,
    TpyCallStateChangeReason reason,
    gchar *detailed_reason,
    gchar *message,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  g_return_if_fail (TPY_IS_CALL_CHANNEL (self));
  g_return_if_fail (self->priv->result == NULL);

  self->priv->result = g_simple_async_result_new (G_OBJECT (self), callback,
      user_data, tpy_call_channel_accept_async);

  tpy_cli_channel_type_call_call_hangup (TP_PROXY (self), -1,
      reason, detailed_reason, message,
      channel_hangup_cb, NULL, NULL, G_OBJECT (self));
}

void
tpy_call_channel_hangup_finish (TpyCallChannel *self,
    GAsyncResult *result,
    GError **error)
{
}

TpyCallState
tpy_call_channel_get_state (TpyCallChannel *self,
    TpyCallFlags *flags, GHashTable **details)
{
  g_return_val_if_fail (TPY_IS_CALL_CHANNEL (self), TPY_CALL_STATE_UNKNOWN);

  if (flags != NULL)
    *flags = self->priv->flags;

  if (details != NULL)
    {
      if (self->priv->details != NULL)
        g_hash_table_ref (self->priv->details);

      *details = self->priv->details;
    }
  return self->priv->state;
}

gboolean
tpy_call_channel_has_initial_video (TpyCallChannel *self)
{
  g_return_val_if_fail (TPY_IS_CALL_CHANNEL (self), FALSE);

  return self->priv->initial_video;
}

gboolean
tpy_call_channel_has_initial_audio (TpyCallChannel *self)
{
  g_return_val_if_fail (TPY_IS_CALL_CHANNEL (self), FALSE);

  return self->priv->initial_audio;
}

void
tpy_call_channel_send_video (TpyCallChannel *self,
    gboolean send)
{
  gboolean found = FALSE;
  guint i;

  g_return_if_fail (TPY_IS_CALL_CHANNEL (self));

  /* Loop over all the contents, if some of them a video set all their
   * streams to sending, otherwise request a video channel in case we want to
   * sent */
  for (i = 0 ; i < self->priv->contents->len ; i++)
    {
      TpyCallContent *content = g_ptr_array_index (self->priv->contents, i);

      if (tpy_call_content_get_media_type (content)
          == TP_MEDIA_STREAM_TYPE_VIDEO)
        {
          GList *l;
          found = TRUE;

          for (l = tpy_call_content_get_streams (content);
              l != NULL ; l = g_list_next (l))
            {
              TpyCallStream *stream = TPY_CALL_STREAM (l->data);
              tpy_call_stream_set_sending_async (stream,
                send, NULL, NULL);
            }
        }
    }

  if (send && !found)
    tpy_cli_channel_type_call_call_add_content (TP_PROXY (self), -1,
        "video", TP_MEDIA_STREAM_TYPE_VIDEO,
        NULL, NULL, NULL, NULL);
}

TpySendingState
tpy_call_channel_get_video_state (TpyCallChannel *self)
{
  TpySendingState result = TPY_SENDING_STATE_NONE;
  guint i;

  g_return_val_if_fail (TPY_IS_CALL_CHANNEL (self), TPY_SENDING_STATE_NONE);

  for (i = 0 ; i < self->priv->contents->len ; i++)
    {
      TpyCallContent *content = g_ptr_array_index (self->priv->contents, i);

      if (tpy_call_content_get_media_type (content)
          == TP_MEDIA_STREAM_TYPE_VIDEO)
        {
          GList *l;

          for (l = tpy_call_content_get_streams (content);
              l != NULL ; l = g_list_next (l))
            {
              TpyCallStream *stream = TPY_CALL_STREAM (l->data);
              TpySendingState state;

              g_object_get (stream, "local-sending-state", &state, NULL);
              if (state != TPY_SENDING_STATE_PENDING_STOP_SENDING &&
                  state > result)
                result = state;
            }
        }
    }

  return result;
}


gboolean
tpy_call_channel_has_dtmf (TpyCallChannel *self)
{
  g_return_val_if_fail (TPY_IS_CALL_CHANNEL (self), FALSE);

  return tp_proxy_has_interface_by_id (self,
      TP_IFACE_QUARK_CHANNEL_INTERFACE_DTMF);
}

static void
on_dtmf_tone_cb (TpChannel *proxy,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  if (error)
    DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
}

void
tpy_call_channel_dtmf_start_tone (TpyCallChannel *self,
    TpDTMFEvent event)
{
  g_return_if_fail (TPY_IS_CALL_CHANNEL (self));

  tp_cli_channel_interface_dtmf_call_start_tone (TP_CHANNEL (self), -1, 0,
      event,
      on_dtmf_tone_cb, "starting tone", NULL, G_OBJECT (self));
}

void
tpy_call_channel_dtmf_stop_tone (TpyCallChannel *self)
{
  g_return_if_fail (TPY_IS_CALL_CHANNEL (self));

  tp_cli_channel_interface_dtmf_call_stop_tone (TP_CHANNEL (self), -1, 0,
      on_dtmf_tone_cb, "stoping tone", NULL, G_OBJECT (self));
}