summaryrefslogtreecommitdiff
path: root/src/salut-contact.c
blob: 239829bd1d297dfa637e317c114cc81243955b8d (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
/*
 * salut-contact.c - Source for salut_contact
 * Copyright (C) 2005-2006 Collabora Ltd.
 *
 * 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "salut-contact.h"
#include "salut-signals-marshal.h"
#include "salut-presence.h"
#include "salut-presence-cache.h"
#include "salut-enumtypes.h"

#include <telepathy-glib/util.h>

#include <wocky/wocky-xep-0115-capabilities.h>

#define DEBUG_FLAG DEBUG_CONTACTS
#include <debug.h>

#define DEBUG_CONTACT(contact, format, ...) G_STMT_START {      \
  DEBUG ("Contact %s: " format, contact->name, ##__VA_ARGS__);  \
} G_STMT_END

static void xep_0115_capabilities_iface_init (gpointer, gpointer);

G_DEFINE_TYPE_WITH_CODE (SalutContact, salut_contact, WOCKY_TYPE_LL_CONTACT,
    G_IMPLEMENT_INTERFACE (WOCKY_TYPE_XEP_0115_CAPABILITIES,
        xep_0115_capabilities_iface_init);
)

/* properties */
enum {
  PROP_CONNECTION = 1,
  PROP_NAME,
  LAST_PROP
};

/* signal enum */
enum
{
    FOUND,
    LOST,
    CONTACT_CHANGE,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = {0};


typedef struct {
  salut_contact_get_avatar_callback callback;
  gpointer user_data;
} AvatarRequest;

struct _SalutContactPrivate
{
  gboolean dispose_has_run;
  gchar *alias;
  GList *avatar_requests;
#ifdef ENABLE_OLPC
   /* room handle owned by the SalutOlpcActivity -> SalutOlpcActivity */
  GHashTable *olpc_activities;
#endif
  gboolean found;
  gboolean frozen;
  guint pending_changes;
};

static GObject *
salut_contact_constructor (GType type,
                           guint n_props,
                           GObjectConstructParam *props)
{
  GObject *obj;
  SalutContact *self;
  SalutContactPrivate *priv;
  TpHandleRepoIface *contact_repo;

  obj = G_OBJECT_CLASS (salut_contact_parent_class)->
    constructor (type, n_props, props);

  self = SALUT_CONTACT (obj);
  priv = self->priv;

  g_assert (self->connection != NULL);
  g_assert (self->name != NULL);

  contact_repo = tp_base_connection_get_handles
      ((TpBaseConnection *) self->connection, TP_HANDLE_TYPE_CONTACT);

  self->handle = tp_handle_ensure (contact_repo, self->name, NULL, NULL);

  self->caps = gabble_capability_set_new ();
  self->data_forms = g_ptr_array_new_with_free_func (g_object_unref);

  return obj;
}

static void
connection_status_changed_cb (SalutConnection *connection,
    guint status,
    guint reason,
    SalutContact *self)
{
  if (status == TP_CONNECTION_STATUS_DISCONNECTED)
    self->connection = NULL;
}

static void
salut_contact_constructed (GObject *obj)
{
  SalutContact *self = SALUT_CONTACT (obj);

  if (G_OBJECT_CLASS (salut_contact_parent_class)->constructed != NULL)
    G_OBJECT_CLASS (salut_contact_parent_class)->constructed (obj);

  tp_g_signal_connect_object (self->connection,
      "status-changed", G_CALLBACK (connection_status_changed_cb), self, 0);
}

static void
salut_contact_init (SalutContact *obj)
{
  SalutContactPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
      SALUT_TYPE_CONTACT, SalutContactPrivate);

  obj->priv = priv;

  /* allocate any data required by the object here */
  obj->name = NULL;
  obj->status = SALUT_PRESENCE_AVAILABLE;
  obj->status_message = NULL;
  obj->avatar_token = NULL;
  obj->jid = NULL;
#ifdef ENABLE_OLPC
  obj->olpc_key = NULL;
  obj->olpc_color = NULL;
  obj->olpc_cur_act = NULL;
  obj->olpc_cur_act_room = 0;
  obj->olpc_ip4 = NULL;
  obj->olpc_ip6 = NULL;
  priv->olpc_activities = g_hash_table_new_full (g_direct_hash, g_direct_equal,
      NULL, (GDestroyNotify) g_object_unref);
#endif
  priv->found = FALSE;
  priv->alias = NULL;
}

static void
salut_contact_get_property (GObject *object,
                            guint property_id,
                            GValue *value,
                            GParamSpec *pspec)
{
  SalutContact *self = SALUT_CONTACT (object);

  switch (property_id)
    {
      case PROP_CONNECTION:
        g_value_set_object (value, self->connection);
        break;
      case PROP_NAME:
        g_value_set_string (value, self->name);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
salut_contact_set_property (GObject *object,
                            guint property_id,
                            const GValue *value,
                            GParamSpec *pspec)
{
  SalutContact *self = SALUT_CONTACT (object);

  switch (property_id)
    {
      case PROP_CONNECTION:
        self->connection = g_value_get_object (value);
        break;
      case PROP_NAME:
        self->name = g_value_dup_string (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void salut_contact_dispose (GObject *object);
static void salut_contact_finalize (GObject *object);

static void
salut_contact_class_init (SalutContactClass *salut_contact_class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (salut_contact_class);
  GParamSpec *param_spec;

  g_type_class_add_private (salut_contact_class, sizeof (SalutContactPrivate));

  object_class->constructor = salut_contact_constructor;
  object_class->constructed = salut_contact_constructed;
  object_class->get_property = salut_contact_get_property;
  object_class->set_property = salut_contact_set_property;

  object_class->dispose = salut_contact_dispose;
  object_class->finalize = salut_contact_finalize;

  signals[FOUND] = g_signal_new ("found",
      G_OBJECT_CLASS_TYPE(salut_contact_class),
      G_SIGNAL_RUN_LAST,
      0,
      NULL, NULL,
      g_cclosure_marshal_VOID__VOID,
      G_TYPE_NONE, 0);

  signals[CONTACT_CHANGE] = g_signal_new ("contact-change",
      G_OBJECT_CLASS_TYPE(salut_contact_class),
      G_SIGNAL_RUN_LAST,
      0,
      NULL, NULL,
      g_cclosure_marshal_VOID__INT,
      G_TYPE_NONE, 1,
      G_TYPE_INT);

  signals[LOST] = g_signal_new ("lost",
      G_OBJECT_CLASS_TYPE(salut_contact_class),
      G_SIGNAL_RUN_LAST,
      0,
      NULL, NULL,
      g_cclosure_marshal_VOID__VOID,
      G_TYPE_NONE, 0);

  param_spec = g_param_spec_object (
      "connection",
      "SalutConnection object",
      "The Salut Connection associated with this contact",
      SALUT_TYPE_CONNECTION,
      G_PARAM_CONSTRUCT_ONLY |
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_CONNECTION,
      param_spec);

  param_spec = g_param_spec_string (
      "name",
      "name",
      "The name of this contact",
      NULL,
      G_PARAM_CONSTRUCT_ONLY |
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_NAME,
      param_spec);
}

#ifdef ENABLE_OLPC
static void
disconnect_activity_signal_foreach (TpHandle room,
                                    SalutOlpcActivity *activity,
                                    SalutContact *self)
{
  g_signal_handlers_disconnect_matched (activity, G_SIGNAL_MATCH_DATA, 0, 0,
      NULL, NULL, self);
}
#endif

void
salut_contact_dispose (GObject *object)
{
  SalutContact *self = SALUT_CONTACT (object);
  SalutContactPrivate *priv = self->priv;

  DEBUG_CONTACT (self, "Disposing contact");

  if (priv->dispose_has_run)
    return;

  priv->dispose_has_run = TRUE;

#ifdef ENABLE_OLPC
  if (self->olpc_cur_act_room != 0 && self->connection != NULL)
    {
      TpHandleRepoIface *room_repo = tp_base_connection_get_handles
          ((TpBaseConnection *) self->connection, TP_HANDLE_TYPE_ROOM);

      tp_handle_unref (room_repo, self->olpc_cur_act_room);
      self->olpc_cur_act_room = 0;
    }

  g_hash_table_foreach (priv->olpc_activities,
      (GHFunc) disconnect_activity_signal_foreach, self);
  g_hash_table_destroy (priv->olpc_activities);
#endif

  salut_contact_avatar_request_flush (self, NULL, 0);

  /* release any references held by the object here */

  if (self->handle != 0 && self->connection != NULL)
    {
      TpHandleRepoIface *contact_repo = tp_base_connection_get_handles
        ((TpBaseConnection *) self->connection, TP_HANDLE_TYPE_CONTACT);
      tp_handle_unref (contact_repo, self->handle);
    }

  if (self->data_forms != NULL)
    {
      g_ptr_array_unref (self->data_forms);
      self->data_forms = NULL;
    }

  if (G_OBJECT_CLASS (salut_contact_parent_class)->dispose)
    G_OBJECT_CLASS (salut_contact_parent_class)->dispose (object);
}

void
salut_contact_finalize (GObject *object)
{
  SalutContact *self = SALUT_CONTACT (object);
  SalutContactPrivate *priv = self->priv;

  /* free any data held directly by the object here */
  g_free (self->name);
  g_free (self->status_message);
  g_free (priv->alias);
  g_free (self->avatar_token);
  g_free (self->jid);

#ifdef ENABLE_OLPC
  if (self->olpc_key != NULL)
    {
      g_array_free (self->olpc_key, TRUE);
    }
  g_free (self->olpc_color);
  g_free (self->olpc_cur_act);
  g_free (self->olpc_ip4);
  g_free (self->olpc_ip6);
#endif

  G_OBJECT_CLASS (salut_contact_parent_class)->finalize (object);
}

static void
purge_cached_avatar (SalutContact *self,
                    const gchar *token)
{
  SalutContactPrivate *priv = self->priv;

  g_free (self->avatar_token);
  self->avatar_token = g_strdup (token);

  /* the avatar token has changed, restart retrieving the avatar if we were
   * retrieving it */
  if (priv->avatar_requests != NULL)
    SALUT_CONTACT_GET_CLASS (self)->retrieve_avatar (self);
}

#ifdef ENABLE_OLPC
typedef struct
{
  SalutContactOLPCActivityFunc foreach;
  gpointer user_data;
} foreach_olpc_activity_ctx;

static void
foreach_olpc_activity (gpointer key, gpointer value, gpointer user_data)
{
  foreach_olpc_activity_ctx *ctx = user_data;
  SalutOlpcActivity *activity = value;

  /* ignore activity without ID */
  if (activity->id == NULL)
    return;

  DEBUG ("%s => %u", activity->id, activity->room);
  (ctx->foreach) (activity, ctx->user_data);
}

void
salut_contact_foreach_olpc_activity (SalutContact *self,
                                     SalutContactOLPCActivityFunc foreach,
                                     gpointer user_data)
{
  SalutContactPrivate *priv = self->priv;
  foreach_olpc_activity_ctx ctx = { foreach, user_data };

  DEBUG ("called");

  g_hash_table_foreach (priv->olpc_activities, foreach_olpc_activity,
      &ctx);

  DEBUG ("end");
}

#endif

GArray *
salut_contact_get_addresses (SalutContact *self)
{
  return SALUT_CONTACT_GET_CLASS (self)->get_addresses (self);
}

gboolean
salut_contact_has_address (SalutContact *self,
                           struct sockaddr *address,
                           guint size)
{
  return SALUT_CONTACT_GET_CLASS (self)->has_address (self, address, size);
}

const gchar *
salut_contact_get_alias (SalutContact *contact)
{
  SalutContactPrivate *priv = contact->priv;
  if (priv->alias == NULL)
    {
      return contact->name;
    }
  return priv->alias;
}

void
salut_contact_avatar_request_flush (SalutContact *contact,
                                    guint8 *data,
                                    gsize size)
{
  SalutContactPrivate *priv = contact->priv;
  GList *list, *liststart;
  AvatarRequest *request;

  liststart = priv->avatar_requests;
  priv->avatar_requests = NULL;

  for (list = liststart; list != NULL; list = g_list_next (list)) {
    request = (AvatarRequest *) list->data;
    request->callback (contact, data, size, request->user_data);
    g_slice_free (AvatarRequest, request);
  }
  g_list_free (liststart);
}

void
salut_contact_get_avatar (SalutContact *contact,
    salut_contact_get_avatar_callback callback, gpointer user_data)
{
  SalutContactPrivate *priv = contact->priv;
  AvatarRequest *request;
  gboolean retrieve;

  g_assert (contact != NULL);

  if (contact->avatar_token == NULL)
    {
      DEBUG ("Avatar requestes for a contact without one (%s)", contact->name);
      callback (contact, NULL, 0, user_data);
      return;
    }

  DEBUG ("Requesting avatar for: %s", contact->name);
  request = g_slice_new0 (AvatarRequest);
  request->callback = callback;
  request->user_data = user_data;
  retrieve = (priv->avatar_requests == NULL);
  priv->avatar_requests = g_list_append (priv->avatar_requests, request);

  if (retrieve)
    SALUT_CONTACT_GET_CLASS (contact)->retrieve_avatar (contact);
}

void
salut_contact_set_capabilities (SalutContact *contact,
    const GabbleCapabilitySet *caps,
    const GPtrArray *data_forms)
{
  guint i;

  gabble_capability_set_free (contact->caps);
  contact->caps = gabble_capability_set_copy (caps);

  g_ptr_array_unref (contact->data_forms);

  contact->data_forms = g_ptr_array_new_with_free_func (g_object_unref);

  for (i = 0; i < data_forms->len; i++)
    {
      GObject *form = g_ptr_array_index (data_forms, i);
      g_ptr_array_add (contact->data_forms, g_object_ref (form));
    }

  g_signal_emit_by_name (contact, "capabilities-changed");
}

static void
salut_contact_change (SalutContact *self, guint changes)
{
  SalutContactPrivate *priv = self->priv;

  priv->pending_changes |= changes;

  if (!priv->frozen && priv->pending_changes != 0)
    {
      g_signal_emit (self, signals[CONTACT_CHANGE], 0,
        priv->pending_changes);
      priv->pending_changes = 0;
      return;
    }
}

void
salut_contact_change_alias (SalutContact *self, const gchar *alias)
{
  SalutContactPrivate *priv = self->priv;

  if (tp_strdiff (priv->alias, alias))
    {
      g_free (priv->alias);
      priv->alias = g_strdup (alias);
      salut_contact_change (self, SALUT_CONTACT_ALIAS_CHANGED);
    }
}

void
salut_contact_change_status (SalutContact *self, SalutPresenceId status)
{
  if (status != self->status && status < SALUT_PRESENCE_NR_PRESENCES)
    {
      self->status = status;
      salut_contact_change (self, SALUT_CONTACT_STATUS_CHANGED);
    }
}

void
salut_contact_change_status_message (SalutContact *self, const gchar *message)
{
  if (tp_strdiff (self->status_message, message))
    {
      g_free (self->status_message);
      self->status_message = g_strdup (message);
      salut_contact_change (self, SALUT_CONTACT_STATUS_CHANGED);
    }
}

void
salut_contact_change_avatar_token (SalutContact *self,
    const gchar *avatar_token)
{
  if (tp_strdiff (self->avatar_token, avatar_token))
    {
      /* Purge the cache */
      purge_cached_avatar (self, avatar_token);
      salut_contact_change (self, SALUT_CONTACT_AVATAR_CHANGED);
    }
}

void
salut_contact_change_jid (SalutContact *self, gchar *jid)
{
  if (tp_strdiff (self->jid, jid))
    {
      g_free (self->jid);
      self->jid = g_strdup (jid);
#ifdef ENABLE_OLPC
      salut_contact_change (self, SALUT_CONTACT_OLPC_PROPERTIES);
#endif
    }
}

void salut_contact_change_capabilities (SalutContact *self,
                                        const gchar *hash,
                                        const gchar *node,
                                        const gchar *ver)
{
  if (self->connection == NULL)
    return;

  salut_presence_cache_process_caps (self->connection->presence_cache, self,
      hash, node, ver);
}

#ifdef ENABLE_OLPC
void
salut_contact_change_olpc_color (SalutContact *self, const gchar *olpc_color)
{
  if (tp_strdiff (self->olpc_color, olpc_color))
    {
      g_free (self->olpc_color);
      self->olpc_color = g_strdup (olpc_color);
      salut_contact_change (self, SALUT_CONTACT_OLPC_PROPERTIES);
    }
}

void
salut_contact_change_olpc_key (SalutContact *self, GArray *olpc_key)
{
  if (olpc_key != NULL)
    {
      if (self->olpc_key == NULL || self->olpc_key->len != olpc_key->len ||
          memcmp (self->olpc_key->data, olpc_key->data, olpc_key->len) != 0)
        {
          if (self->olpc_key != NULL)
            {
              g_array_free (self->olpc_key, TRUE);
            }
            self->olpc_key = g_array_sized_new (FALSE, FALSE,
                sizeof (guint8), olpc_key->len);
            g_array_append_vals (self->olpc_key, olpc_key->data,
                olpc_key->len);
            salut_contact_change (self, SALUT_CONTACT_OLPC_PROPERTIES);
        }
    }
}

void
salut_contact_change_ipv4_addr (SalutContact *self, const gchar *ipv4_addr)
{
  if (tp_strdiff (ipv4_addr, self->olpc_ip4))
    {
      g_free (self->olpc_ip4);
      self->olpc_ip4 = g_strdup (ipv4_addr);
      salut_contact_change (self, SALUT_CONTACT_OLPC_PROPERTIES);
    }

}

void
salut_contact_change_ipv6_addr (SalutContact *self, const gchar *ipv6_addr)
{
  if (tp_strdiff (ipv6_addr, self->olpc_ip6))
    {
      g_free (self->olpc_ip6);
      self->olpc_ip6 = g_strdup (ipv6_addr);
      salut_contact_change (self, SALUT_CONTACT_OLPC_PROPERTIES);
    }
}

void
salut_contact_change_current_activity (SalutContact *self,
  const gchar *current_activity_id, const gchar *current_activity_room)
{
  TpHandleRepoIface *room_repo;
  TpHandle room_handle = 0;

  if (self->connection != NULL)
    return;

  room_repo = tp_base_connection_get_handles
    ((TpBaseConnection *) self->connection, TP_HANDLE_TYPE_ROOM);

  if (current_activity_room != NULL && *current_activity_room != '\0')
    {
      room_handle = tp_handle_ensure (room_repo, current_activity_room,
        NULL, NULL);
      if (room_handle == 0)
        {
          DEBUG ("Invalid room \"%s\" for current activity \"%s\": "
              "ignoring", current_activity_room, current_activity_id);
        }
    }

  if (current_activity_id == NULL || room_handle == 0)
    {
      DEBUG ("Unsetting current activity");
      if (self->olpc_cur_act != NULL || self->olpc_cur_act_room != 0)
        {
          g_free (self->olpc_cur_act);
          if (self->olpc_cur_act_room != 0)
            tp_handle_unref (room_repo, self->olpc_cur_act_room);
          self->olpc_cur_act = NULL;
          self->olpc_cur_act_room = 0;
           salut_contact_change (self, SALUT_CONTACT_OLPC_CURRENT_ACTIVITY);
        }
      if (room_handle != 0)
        {
          /* tp_handle_ensure gave us a ref */
          tp_handle_unref (room_repo, room_handle);
        }

     }
   else
     {
       DEBUG ("Current activity %s, room handle %d", current_activity_id,
           room_handle);
       if (tp_strdiff (self->olpc_cur_act, current_activity_id) ||
           self->olpc_cur_act_room != room_handle)
         {
           g_free (self->olpc_cur_act);
           if (self->olpc_cur_act_room != 0)
             tp_handle_unref (room_repo, self->olpc_cur_act_room);
           self->olpc_cur_act_room = room_handle;
           self->olpc_cur_act = g_strdup (current_activity_id);
           salut_contact_change (self, SALUT_CONTACT_OLPC_CURRENT_ACTIVITY);
         }
       else
         {
           /* tp_handle_ensure gave us a ref */
           tp_handle_unref (room_repo, room_handle);
         }
     }
}

#endif

void
salut_contact_found (SalutContact *self)
{
  SalutContactPrivate *priv = self->priv;
  if (priv->found)
    return;

  priv->found = TRUE;
  g_signal_emit (self, signals[FOUND], 0);
  /* When found everything changes */
  g_signal_emit (self, signals[CONTACT_CHANGE], 0, 0xff);
  priv->pending_changes = 0;
}

void
salut_contact_lost (SalutContact *self)
{
  SalutContactPrivate *priv = self->priv;

  self->status = SALUT_PRESENCE_OFFLINE;
  g_free (self->status_message);
  self->status_message = NULL;

  if (!priv->found)
    return;

  DEBUG_CONTACT (self, "disappeared from the local link");

  priv->found = FALSE;
  g_signal_emit (self, signals[CONTACT_CHANGE], 0,
      SALUT_CONTACT_STATUS_CHANGED);
  g_signal_emit (self, signals[LOST], 0);
}

void
salut_contact_freeze (SalutContact *self)
{
  SalutContactPrivate *priv = self->priv;

  priv->frozen = TRUE;
}

void
salut_contact_thaw (SalutContact *self)
{
  SalutContactPrivate *priv = self->priv;

  if (!priv->frozen)
    return;

  priv->frozen = FALSE;
  /* Triggers the emission of the changed signal */
  salut_contact_change (self, 0);
}

#ifdef ENABLE_OLPC
static void
activity_valid_cb (SalutOlpcActivity *activity,
                   SalutContact *self)
{
  /* Now we can emit the ActivitiesChanged signal */
  DEBUG ("activity in room %d (%s) is now valid", activity->room, activity->id);
  g_signal_emit (self, signals[CONTACT_CHANGE], 0,
      SALUT_CONTACT_OLPC_ACTIVITIES);
}

gboolean
salut_contact_joined_activity (SalutContact *self,
                               SalutOlpcActivity *activity)
{
  SalutContactPrivate *priv = self->priv;

  if (g_hash_table_lookup (priv->olpc_activities,
        GUINT_TO_POINTER (activity->room)) != NULL)
    return FALSE;

  DEBUG_CONTACT (self, "joined activity %s", activity->id);
  g_hash_table_insert (priv->olpc_activities, GUINT_TO_POINTER (activity->room),
      activity);
  g_object_ref (activity);

  if (activity->id == NULL)
    {
      /* we can't emit the ActivitiesChanged signal right now as we don't have
       * the activity ID. Thanks OLPC interface */
      DEBUG ("activity in room %d isn't valid yet", activity->room);
      g_signal_connect (activity, "valid", G_CALLBACK (activity_valid_cb),
          self);
    }
  else
    {
      g_signal_emit (self, signals[CONTACT_CHANGE], 0,
          SALUT_CONTACT_OLPC_ACTIVITIES);
    }

  return TRUE;
}

void
salut_contact_left_activity (SalutContact *self,
                             SalutOlpcActivity *activity)
{
  SalutContactPrivate *priv = self->priv;

  g_signal_handlers_disconnect_matched (activity, G_SIGNAL_MATCH_DATA, 0, 0,
      NULL, NULL, self);

  DEBUG_CONTACT (self, "left activity %s", activity->id);
  if (!g_hash_table_remove (priv->olpc_activities,
        GUINT_TO_POINTER (activity->room)))
    return;

  g_signal_emit (self, signals[CONTACT_CHANGE], 0,
      SALUT_CONTACT_OLPC_ACTIVITIES);
}
#endif

static const GPtrArray *
salut_contact_get_data_forms (WockyXep0115Capabilities *caps)
{
  SalutContact *self = SALUT_CONTACT (caps);

  return self->data_forms;
}

static void
xep_0115_capabilities_iface_init (gpointer g_iface,
    gpointer iface_data)
{
  WockyXep0115CapabilitiesInterface *iface = g_iface;

  iface->get_data_forms = salut_contact_get_data_forms;
}