summaryrefslogtreecommitdiff
path: root/src/capabilities.c
blob: be0dc00c64991030acf44a053251944414ed305a (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
/*
 * capabilities.c - Connection.Interface.Capabilities constants and utilities
 * Copyright (C) 2005 Collabora Ltd.
 * Copyright (C) 2005 Nokia Corporation
 *
 * 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
 */

#include "config.h"
#include "gabble/capabilities.h"

#include <stdlib.h>
#include <string.h>

#include <telepathy-glib/telepathy-glib.h>
#include <telepathy-glib/telepathy-glib-dbus.h>

#define DEBUG_FLAG GABBLE_DEBUG_PRESENCE
#include "debug.h"
#include "namespaces.h"

typedef struct _Feature Feature;

struct _Feature
{
  enum {
    FEATURE_FIXED,
    FEATURE_OPTIONAL,
    FEATURE_OLPC
  } feature_type;
  gchar *ns;
};

static const Feature self_advertised_features[] =
{
  { FEATURE_FIXED, NS_GOOGLE_FEAT_SESSION },

#ifdef ENABLE_VOIP
  { FEATURE_FIXED, NS_JINGLE_TRANSPORT_RAWUDP },
  { FEATURE_FIXED, NS_JINGLE015 },
  { FEATURE_FIXED, NS_JINGLE032 },
#endif

  { FEATURE_FIXED, NS_DISCO_INFO },
  { FEATURE_FIXED, NS_CHAT_STATES },
  { FEATURE_FIXED, NS_NICK },
  { FEATURE_FIXED, NS_NICK "+notify" },
  { FEATURE_FIXED, NS_SI },
  { FEATURE_FIXED, NS_IBB },
  { FEATURE_FIXED, NS_TUBES },
  { FEATURE_FIXED, NS_BYTESTREAMS },
  { FEATURE_FIXED, NS_VERSION },
  { FEATURE_FIXED, NS_LAST },
  { FEATURE_FIXED, NS_RECEIPTS },

#ifdef ENABLE_FILE_TRANSFER
  { FEATURE_OPTIONAL, NS_FILE_TRANSFER },
  { FEATURE_OPTIONAL, NS_TP_FT_METADATA },
#endif

#ifdef ENABLE_VOIP
  { FEATURE_OPTIONAL, NS_GOOGLE_TRANSPORT_P2P },
  { FEATURE_OPTIONAL, NS_JINGLE_TRANSPORT_ICEUDP },

  { FEATURE_OPTIONAL, NS_GOOGLE_FEAT_SHARE },
  { FEATURE_OPTIONAL, NS_GOOGLE_FEAT_VOICE },
  { FEATURE_OPTIONAL, NS_GOOGLE_FEAT_VIDEO },
  { FEATURE_OPTIONAL, NS_GOOGLE_FEAT_CAMERA },
  { FEATURE_OPTIONAL, NS_JINGLE_DESCRIPTION_AUDIO },
  { FEATURE_OPTIONAL, NS_JINGLE_DESCRIPTION_VIDEO },
  { FEATURE_OPTIONAL, NS_JINGLE_RTP },
  { FEATURE_OPTIONAL, NS_JINGLE_RTP_AUDIO },
  { FEATURE_OPTIONAL, NS_JINGLE_RTP_VIDEO },
#endif

  { FEATURE_OLPC, NS_OLPC_BUDDY_PROPS "+notify" },
  { FEATURE_OLPC, NS_OLPC_ACTIVITIES "+notify" },
  { FEATURE_OLPC, NS_OLPC_CURRENT_ACTIVITY "+notify" },
  { FEATURE_OLPC, NS_OLPC_ACTIVITY_PROPS "+notify" },

  { FEATURE_OPTIONAL, NS_GEOLOC "+notify" },

  { 0, NULL }
};

static const Feature quirks[] = {
      { 0, QUIRK_OMITS_CONTENT_CREATORS },
      { 0, NULL }
};

static GabbleCapabilitySet *legacy_caps = NULL;
static GabbleCapabilitySet *share_v1_caps = NULL;
static GabbleCapabilitySet *voice_v1_caps = NULL;
static GabbleCapabilitySet *video_v1_caps = NULL;
static GabbleCapabilitySet *camera_v1_caps = NULL;
static GabbleCapabilitySet *any_audio_caps = NULL;
static GabbleCapabilitySet *any_video_caps = NULL;
static GabbleCapabilitySet *any_audio_video_caps = NULL;
static GabbleCapabilitySet *any_google_av_caps = NULL;
static GabbleCapabilitySet *any_jingle_av_caps = NULL;
static GabbleCapabilitySet *any_transport_caps = NULL;
static GabbleCapabilitySet *fixed_caps = NULL;
static GabbleCapabilitySet *geoloc_caps = NULL;
static GabbleCapabilitySet *olpc_caps = NULL;

const GabbleCapabilitySet *
gabble_capabilities_get_legacy (void)
{
  return legacy_caps;
}

const GabbleCapabilitySet *
gabble_capabilities_get_bundle_share_v1 (void)
{
  return share_v1_caps;
}

const GabbleCapabilitySet *
gabble_capabilities_get_bundle_voice_v1 (void)
{
  return voice_v1_caps;
}

const GabbleCapabilitySet *
gabble_capabilities_get_bundle_video_v1 (void)
{
  return video_v1_caps;
}

const GabbleCapabilitySet *
gabble_capabilities_get_bundle_camera_v1 (void)
{
  return camera_v1_caps;
}

const GabbleCapabilitySet *
gabble_capabilities_get_any_audio (void)
{
  return any_audio_caps;
}

const GabbleCapabilitySet *
gabble_capabilities_get_any_video (void)
{
  return any_video_caps;
}

const GabbleCapabilitySet *
gabble_capabilities_get_any_audio_video (void)
{
  return any_audio_video_caps;
}

const GabbleCapabilitySet *
gabble_capabilities_get_any_google_av (void)
{
  return any_google_av_caps;
}

const GabbleCapabilitySet *
gabble_capabilities_get_any_jingle_av (void)
{
  return any_jingle_av_caps;
}

const GabbleCapabilitySet *
gabble_capabilities_get_any_transport (void)
{
  return any_transport_caps;
}

const GabbleCapabilitySet *
gabble_capabilities_get_fixed_caps (void)
{
  return fixed_caps;
}

const GabbleCapabilitySet *
gabble_capabilities_get_geoloc_notify (void)
{
  return geoloc_caps;
}

const GabbleCapabilitySet *
gabble_capabilities_get_olpc_notify (void)
{
  return olpc_caps;
}

static gboolean
omits_content_creators (WockyNode *identity)
{
  const gchar *name, *suffix;
  gchar *end;
  int ver;

  name = wocky_node_get_attribute (identity, "name");

  if (name == NULL)
    return FALSE;

#define PREFIX "Telepathy Gabble 0.7."

  if (!g_str_has_prefix (name, PREFIX))
    return FALSE;

  suffix = name + strlen (PREFIX);
  ver = strtol (suffix, &end, 10);

  if (*end != '\0')
    return FALSE;

  /* Gabble versions since 0.7.16 did not send the creator='' attribute for
   * contents. The bug is fixed in 0.7.29.
   */
  if (ver >= 16 && ver < 29)
    {
      DEBUG ("contact is using '%s' which omits 'creator'", name);
      return TRUE;
    }
  else
    {
      return FALSE;
    }
}

static gsize feature_handles_refcount = 0;
/* The handles in this repository are not really handles in the tp-spec sense
 * of the word; we're just using it as a convenient implementation of a
 * refcounted string pool. Their string values are either XMPP namespaces,
 * or "quirk" pseudo-namespaces starting with QUIRK_PREFIX_CHAR (like
 * QUIRK_OMITS_CONTENT_CREATORS). */
static TpHandleRepoIface *feature_handles = NULL;

void
gabble_capabilities_init (gpointer conn)
{
  DEBUG ("%p", conn);

  if (feature_handles_refcount++ == 0)
    {
      const Feature *feat;

      g_assert (feature_handles == NULL);
      /* TpDynamicHandleRepo wants a handle type, which isn't relevant here
       * (we're just using it as a string pool). Use an arbitrary handle type
       * to shut it up. */
      feature_handles = tp_dynamic_handle_repo_new (TP_HANDLE_TYPE_CONTACT,
          NULL, NULL);

      /* make the pre-cooked bundles */

      legacy_caps = gabble_capability_set_new ();

      for (feat = self_advertised_features; feat->ns != NULL; feat++)
        {
          gabble_capability_set_add (legacy_caps, feat->ns);
        }

#ifdef ENABLE_VOIP
      share_v1_caps = gabble_capability_set_new ();
      gabble_capability_set_add (share_v1_caps, NS_GOOGLE_FEAT_SHARE);

      voice_v1_caps = gabble_capability_set_new ();
      gabble_capability_set_add (voice_v1_caps, NS_GOOGLE_FEAT_VOICE);

      video_v1_caps = gabble_capability_set_new ();
      gabble_capability_set_add (video_v1_caps, NS_GOOGLE_FEAT_VIDEO);

      camera_v1_caps = gabble_capability_set_new ();
      gabble_capability_set_add (camera_v1_caps, NS_GOOGLE_FEAT_CAMERA);

      any_audio_caps = gabble_capability_set_new ();
      gabble_capability_set_add (any_audio_caps, NS_JINGLE_RTP_AUDIO);
      gabble_capability_set_add (any_audio_caps, NS_JINGLE_DESCRIPTION_AUDIO);
      gabble_capability_set_add (any_audio_caps, NS_GOOGLE_FEAT_VOICE);

      any_video_caps = gabble_capability_set_new ();
      gabble_capability_set_add (any_video_caps, NS_JINGLE_RTP_VIDEO);
      gabble_capability_set_add (any_video_caps, NS_JINGLE_DESCRIPTION_VIDEO);
      gabble_capability_set_add (any_video_caps, NS_GOOGLE_FEAT_VIDEO);

      any_audio_video_caps = gabble_capability_set_copy (any_audio_caps);
      gabble_capability_set_update (any_audio_video_caps, any_video_caps);

      any_google_av_caps = gabble_capability_set_new ();
      gabble_capability_set_add (any_google_av_caps, NS_GOOGLE_FEAT_VOICE);
      gabble_capability_set_add (any_google_av_caps, NS_GOOGLE_FEAT_VIDEO);

      any_jingle_av_caps = gabble_capability_set_copy (any_audio_caps);
      gabble_capability_set_update (any_jingle_av_caps, any_video_caps);
      gabble_capability_set_exclude (any_jingle_av_caps, any_google_av_caps);

      any_transport_caps = gabble_capability_set_new ();
      gabble_capability_set_add (any_transport_caps, NS_GOOGLE_TRANSPORT_P2P);
      gabble_capability_set_add (any_transport_caps, NS_JINGLE_TRANSPORT_ICEUDP);
      gabble_capability_set_add (any_transport_caps, NS_JINGLE_TRANSPORT_RAWUDP);
#endif

      fixed_caps = gabble_capability_set_new ();

      for (feat = self_advertised_features; feat->ns != NULL; feat++)
        {
          if (feat->feature_type == FEATURE_FIXED)
            gabble_capability_set_add (fixed_caps, feat->ns);
        }

      geoloc_caps = gabble_capability_set_new ();
      gabble_capability_set_add (geoloc_caps, NS_GEOLOC "+notify");

      olpc_caps = gabble_capability_set_new ();

      for (feat = self_advertised_features; feat->ns != NULL; feat++)
        {
          if (feat->feature_type == FEATURE_OLPC)
            gabble_capability_set_add (olpc_caps, feat->ns);
        }
    }

  g_assert (feature_handles != NULL);
}

void
gabble_capabilities_finalize (gpointer conn)
{
  DEBUG ("%p", conn);

  g_assert (feature_handles_refcount > 0);

  if (--feature_handles_refcount == 0)
    {
      gabble_capability_set_free (legacy_caps);
#ifdef ENABLE_VOIP
      gabble_capability_set_free (share_v1_caps);
      gabble_capability_set_free (voice_v1_caps);
      gabble_capability_set_free (video_v1_caps);
      gabble_capability_set_free (camera_v1_caps);
      gabble_capability_set_free (any_audio_caps);
      gabble_capability_set_free (any_video_caps);
      gabble_capability_set_free (any_audio_video_caps);
      gabble_capability_set_free (any_google_av_caps);
      gabble_capability_set_free (any_jingle_av_caps);
      gabble_capability_set_free (any_transport_caps);
#endif
      gabble_capability_set_free (fixed_caps);
      gabble_capability_set_free (geoloc_caps);
      gabble_capability_set_free (olpc_caps);

      legacy_caps = NULL;
      share_v1_caps = NULL;
      voice_v1_caps = NULL;
      video_v1_caps = NULL;
      camera_v1_caps = NULL;
      any_audio_caps = NULL;
      any_video_caps = NULL;
      any_audio_video_caps = NULL;
      any_google_av_caps = NULL;
      any_jingle_av_caps = NULL;
      any_transport_caps = NULL;
      fixed_caps = NULL;
      geoloc_caps = NULL;
      olpc_caps = NULL;

      tp_clear_object (&feature_handles);
    }
}

struct _GabbleCapabilitySet {
    TpHandleSet *handles;
};

GabbleCapabilitySet *
gabble_capability_set_new (void)
{
  GabbleCapabilitySet *ret = g_slice_new0 (GabbleCapabilitySet);

  g_assert (feature_handles != NULL);
  ret->handles = tp_handle_set_new (feature_handles);
  return ret;
}

GabbleCapabilitySet *
gabble_capability_set_new_from_stanza (WockyNode *query_result)
{
  GabbleCapabilitySet *ret;
  const gchar *var;
  GSList *ni;

  g_return_val_if_fail (query_result != NULL, NULL);

  ret = gabble_capability_set_new ();

  for (ni = query_result->children; ni != NULL; ni = g_slist_next (ni))
    {
      WockyNode *child = ni->data;

      if (!tp_strdiff (child->name, "identity"))
        {
          if (omits_content_creators (child))
            gabble_capability_set_add (ret, QUIRK_OMITS_CONTENT_CREATORS);

          continue;
        }

      if (tp_strdiff (child->name, "feature"))
        continue;

      var = wocky_node_get_attribute (child, "var");

      if (NULL == var)
        continue;

      if (G_UNLIKELY (var[0] == QUIRK_PREFIX_CHAR))
        {
          /* I think not! (It's not allowed in XML...) */
          continue;
        }

      /* TODO: only store namespaces we understand. */
      gabble_capability_set_add (ret, var);
    }

  return ret;
}

GabbleCapabilitySet *
gabble_capability_set_copy (const GabbleCapabilitySet *caps)
{
  GabbleCapabilitySet *ret;

  g_return_val_if_fail (caps != NULL, NULL);

  ret = gabble_capability_set_new ();

  gabble_capability_set_update (ret, caps);

  return ret;
}

void
gabble_capability_set_update (GabbleCapabilitySet *target,
    const GabbleCapabilitySet *source)
{
  TpIntset *ret;
  g_return_if_fail (target != NULL);
  g_return_if_fail (source != NULL);

  ret = tp_handle_set_update (target->handles,
    tp_handle_set_peek (source->handles));

  tp_intset_destroy (ret);
}

typedef struct {
    GSList *deleted;
    TpHandleSet *intersect_with;
} IntersectHelper;

static void
intersect_helper (TpHandleSet *unused G_GNUC_UNUSED,
    TpHandle handle,
    gpointer p)
{
  IntersectHelper *data = p;

  if (!tp_handle_set_is_member (data->intersect_with, handle))
    data->deleted = g_slist_prepend (data->deleted, GUINT_TO_POINTER (handle));
}

void
gabble_capability_set_intersect (GabbleCapabilitySet *target,
    const GabbleCapabilitySet *source)
{
  IntersectHelper data = { NULL, NULL };

  g_return_if_fail (target != NULL);
  g_return_if_fail (source != NULL);

  if (target == source)
    return;

  data.intersect_with = source->handles;

  tp_handle_set_foreach (target->handles, intersect_helper, &data);

  while (data.deleted != NULL)
    {
      DEBUG ("dropping %s", tp_handle_inspect (feature_handles,
            GPOINTER_TO_UINT (data.deleted->data)));
      tp_handle_set_remove (target->handles,
          GPOINTER_TO_UINT (data.deleted->data));
      data.deleted = g_slist_delete_link (data.deleted, data.deleted);
    }
}

static void
remove_from_set (TpHandleSet *unused G_GNUC_UNUSED,
    TpHandle handle,
    gpointer handles)
{
  tp_handle_set_remove (handles, handle);
}

void
gabble_capability_set_exclude (GabbleCapabilitySet *caps,
    const GabbleCapabilitySet *removed)
{
  g_return_if_fail (caps != NULL);
  g_return_if_fail (removed != NULL);

  if (caps == removed)
    {
      gabble_capability_set_clear (caps);
      return;
    }

  tp_handle_set_foreach (removed->handles, remove_from_set, caps->handles);
}

void
gabble_capability_set_add (GabbleCapabilitySet *caps,
    const gchar *cap)
{
  TpHandle handle;

  g_return_if_fail (caps != NULL);
  g_return_if_fail (cap != NULL);

  handle = tp_handle_ensure (feature_handles, cap, NULL, NULL);
  tp_handle_set_add (caps->handles, handle);
}

gboolean
gabble_capability_set_remove (GabbleCapabilitySet *caps,
    const gchar *cap)
{
  TpHandle handle;

  g_return_val_if_fail (caps != NULL, FALSE);
  g_return_val_if_fail (cap != NULL, FALSE);

  handle = tp_handle_lookup (feature_handles, cap, NULL, NULL);

  if (handle == 0)
    return FALSE;

  return tp_handle_set_remove (caps->handles, handle);
}

void
gabble_capability_set_clear (GabbleCapabilitySet *caps)
{
  g_return_if_fail (caps != NULL);

  /* There is no tp_handle_set_clear, so do the next best thing */
  tp_handle_set_destroy (caps->handles);
  caps->handles = tp_handle_set_new (feature_handles);
}

void
gabble_capability_set_free (GabbleCapabilitySet *caps)
{
  g_return_if_fail (caps != NULL);

  tp_handle_set_destroy (caps->handles);
  g_slice_free (GabbleCapabilitySet, caps);
}

gint
gabble_capability_set_size (const GabbleCapabilitySet *caps)
{
  g_return_val_if_fail (caps != NULL, 0);
  return tp_handle_set_size (caps->handles);
}

/* By design, this function can be used as a GabbleCapabilitySetPredicate */
gboolean
gabble_capability_set_has (const GabbleCapabilitySet *caps,
    const gchar *cap)
{
  TpHandle handle;

  g_return_val_if_fail (caps != NULL, FALSE);
  g_return_val_if_fail (cap != NULL, FALSE);

  handle = tp_handle_lookup (feature_handles, cap, NULL, NULL);

  if (handle == 0)
    {
      /* nobody in the whole CM has this capability */
      return FALSE;
    }

  return tp_handle_set_is_member (caps->handles, handle);
}

/* By design, this function can be used as a GabbleCapabilitySetPredicate */
gboolean
gabble_capability_set_has_one (const GabbleCapabilitySet *caps,
    const GabbleCapabilitySet *alternatives)
{
  TpIntsetFastIter iter;
  guint element;

  g_return_val_if_fail (caps != NULL, FALSE);
  g_return_val_if_fail (alternatives != NULL, FALSE);

  tp_intset_fast_iter_init (&iter, tp_handle_set_peek (alternatives->handles));

  while (tp_intset_fast_iter_next (&iter, &element))
    {
      if (tp_handle_set_is_member (caps->handles, element))
        {
          return TRUE;
        }
    }

  return FALSE;
}

/* By design, this function can be used as a GabbleCapabilitySetPredicate */
gboolean
gabble_capability_set_at_least (const GabbleCapabilitySet *caps,
    const GabbleCapabilitySet *query)
{
  TpIntsetFastIter iter;
  guint element;

  g_return_val_if_fail (caps != NULL, FALSE);
  g_return_val_if_fail (query != NULL, FALSE);

  tp_intset_fast_iter_init (&iter, tp_handle_set_peek (query->handles));

  while (tp_intset_fast_iter_next (&iter, &element))
    {
      if (!tp_handle_set_is_member (caps->handles, element))
        {
          return FALSE;
        }
    }

  return TRUE;
}

gboolean
gabble_capability_set_equals (const GabbleCapabilitySet *a,
    const GabbleCapabilitySet *b)
{
  g_return_val_if_fail (a != NULL, FALSE);
  g_return_val_if_fail (b != NULL, FALSE);

  return tp_intset_is_equal (tp_handle_set_peek (a->handles),
      tp_handle_set_peek (b->handles));
}

/* Does not iterate over quirks, only real features. */
void
gabble_capability_set_foreach (const GabbleCapabilitySet *caps,
    GFunc func, gpointer user_data)
{
  TpIntsetFastIter iter;
  guint element;

  g_return_if_fail (caps != NULL);
  g_return_if_fail (func != NULL);

  tp_intset_fast_iter_init (&iter, tp_handle_set_peek (caps->handles));

  while (tp_intset_fast_iter_next (&iter, &element))
    {
      const gchar *var = tp_handle_inspect (feature_handles, element);

      g_return_if_fail (var != NULL);

      if (var[0] != QUIRK_PREFIX_CHAR)
        func ((gchar *) var, user_data);
    }
}

static void
append_intset (GString *ret,
    const TpIntset *cap_ints,
    const gchar *indent)
{
  TpIntsetFastIter iter;
  guint element;

  tp_intset_fast_iter_init (&iter, cap_ints);

  while (tp_intset_fast_iter_next (&iter, &element))
    {
      const gchar *var = tp_handle_inspect (feature_handles, element);

      g_return_if_fail (var != NULL);

      if (var[0] == QUIRK_PREFIX_CHAR)
        {
          g_string_append_printf (ret, "%sQuirk:   %s\n", indent, var + 1);
        }
      else
        {
          g_string_append_printf (ret, "%sFeature: %s\n", indent, var);
        }
    }
}

gchar *
gabble_capability_set_dump (const GabbleCapabilitySet *caps,
    const gchar *indent)
{
  GString *ret;

  g_return_val_if_fail (caps != NULL, NULL);

  if (indent == NULL)
    indent = "";

  ret = g_string_new (indent);
  g_string_append (ret, "--begin--\n");
  append_intset (ret, tp_handle_set_peek (caps->handles), indent);
  g_string_append (ret, indent);
  g_string_append (ret, "--end--\n");
  return g_string_free (ret, FALSE);
}

gchar *
gabble_capability_set_dump_diff (const GabbleCapabilitySet *old_caps,
    const GabbleCapabilitySet *new_caps,
    const gchar *indent)
{
  TpIntset *old_ints, *new_ints, *rem, *add;
  GString *ret;

  g_return_val_if_fail (old_caps != NULL, NULL);
  g_return_val_if_fail (new_caps != NULL, NULL);

  old_ints = tp_handle_set_peek (old_caps->handles);
  new_ints = tp_handle_set_peek (new_caps->handles);

  if (tp_intset_is_equal (old_ints, new_ints))
    return g_strdup_printf ("%s--no change--", indent);

  rem = tp_intset_difference (old_ints, new_ints);
  add = tp_intset_difference (new_ints, old_ints);

  ret = g_string_new ("");

  if (!tp_intset_is_empty (rem))
    {
      g_string_append (ret, indent);
      g_string_append (ret, "--removed--\n");
      append_intset (ret, rem, indent);
    }

  if (!tp_intset_is_empty (add))
    {
      g_string_append (ret, indent);
      g_string_append (ret, "--added--\n");
      append_intset (ret, add, indent);
    }

  g_string_append (ret, indent);
  g_string_append (ret, "--end--");

  tp_intset_destroy (add);
  tp_intset_destroy (rem);

  return g_string_free (ret, FALSE);
}