aboutsummaryrefslogtreecommitdiffstats
path: root/libempathy/empathy-tp-contact-factory.c
blob: 48910e2e9289645424196ad9b6097146dbc94582 (plain) (blame)
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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2007-2008 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
 * 
 * Authors: Xavier Claessens <xclaesse@gmail.com>
 */

#include <config.h>

#include <string.h>

#include <telepathy-glib/util.h>
#include <telepathy-glib/connection.h>
#include <libmissioncontrol/mission-control.h>

#include "empathy-tp-contact-factory.h"
#include "empathy-utils.h"

#define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
#include "empathy-debug.h"

#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactFactory)
typedef struct {
    MissionControl *mc;
    McAccount      *account;
    TpConnection   *connection;
    gboolean        ready;

    GList          *contacts;
    EmpathyContact *user;
    gpointer        token;

    gchar         **avatar_mime_types;
    guint           avatar_min_width;
    guint           avatar_min_height;
    guint           avatar_max_width;
    guint           avatar_max_height;
    guint           avatar_max_size;
} EmpathyTpContactFactoryPriv;

G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);

enum {
    PROP_0,
    PROP_ACCOUNT,
    PROP_READY,

    PROP_MIME_TYPES,
    PROP_MIN_WIDTH,
    PROP_MIN_HEIGHT,
    PROP_MAX_WIDTH,
    PROP_MAX_HEIGHT,
    PROP_MAX_SIZE
};

static EmpathyContact *
tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
                   guint                    handle)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
    GList                       *l;

    for (l = priv->contacts; l; l = l->next) {
        if (empathy_contact_get_handle (l->data) == handle) {
            return l->data;
        }
    }

    return NULL;
}

static EmpathyContact *
tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
                   const gchar             *id)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
    GList                       *l;

    for (l = priv->contacts; l; l = l->next) {
        if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
            return l->data;
        }
    }

    return NULL;
}

static void
tp_contact_factory_weak_notify (gpointer data,
                GObject *where_the_object_was)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);

    DEBUG ("Remove finalized contact %p", where_the_object_was);

    priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
}

static void
tp_contact_factory_presences_table_foreach (const gchar    *state_str,
                        GHashTable     *presences_table,
                        EmpathyContact *contact)
{
    const GValue *message;
    const gchar  *message_str = NULL;

    empathy_contact_set_presence (contact,
                      empathy_presence_from_str (state_str));
    
    message = g_hash_table_lookup (presences_table, "message");
    if (message) {
        message_str = g_value_get_string (message);
    }

    if (!G_STR_EMPTY (message_str)) {
        empathy_contact_set_presence_message (contact, message_str);
    } else {
        empathy_contact_set_presence_message (contact, NULL);
    }
}

static void
tp_contact_factory_parse_presence_foreach (guint                    handle,
                       GValueArray             *presence_struct,
                       EmpathyTpContactFactory *tp_factory)
{
    GHashTable      *presences_table;
    EmpathyContact  *contact;

    contact = tp_contact_factory_find_by_handle (tp_factory, handle);
    if (!contact) {
        return;
    }

    presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));

    g_hash_table_foreach (presences_table,
                  (GHFunc) tp_contact_factory_presences_table_foreach,
                  contact);

    DEBUG ("Changing presence for contact %s (%d) to '%s' (%d)",
        empathy_contact_get_id (contact),
        handle,
        empathy_contact_get_presence_message (contact),
        empathy_contact_get_presence (contact));
}

static void
tp_contact_factory_get_presence_cb (TpConnection *connection,
                    GHashTable   *handle_table,
                    const GError *error,
                    gpointer      user_data,
                    GObject      *tp_factory)
{
    if (error) {
        DEBUG ("Error getting presence: %s", error->message);
        if (error->domain == TP_DBUS_ERRORS &&
            error->code == TP_DBUS_ERROR_NO_INTERFACE) {
            guint *handles = user_data;

            /* We have no presence iface, set default presence
             * to available */
            while (*handles != 0) {
                EmpathyContact *contact;

                contact = tp_contact_factory_find_by_handle (
                    (EmpathyTpContactFactory*) tp_factory,
                    *handles);
                if (contact) {
                    empathy_contact_set_presence (contact,
                                      MC_PRESENCE_AVAILABLE);
                }

                handles++;
            }
        }

        return;
    }

    g_hash_table_foreach (handle_table,
                  (GHFunc) tp_contact_factory_parse_presence_foreach,
                  EMPATHY_TP_CONTACT_FACTORY (tp_factory));
}

static void
tp_contact_factory_presence_update_cb (TpConnection *connection,
                       GHashTable   *handle_table,
                       gpointer      user_data,
                       GObject      *tp_factory)
{
    g_hash_table_foreach (handle_table,
                  (GHFunc) tp_contact_factory_parse_presence_foreach,
                  EMPATHY_TP_CONTACT_FACTORY (tp_factory));
}

static void
tp_contact_factory_set_aliases_cb (TpConnection *connection,
                   const GError *error,
                   gpointer      user_data,
                   GObject      *tp_factory)
{
    if (error) {
        DEBUG ("Error setting alias: %s", error->message);
    }
}

static void
tp_contact_factory_request_aliases_cb (TpConnection *connection,
                       const gchar  **contact_names,
                       const GError  *error,
                       gpointer       user_data,
                       GObject       *tp_factory)
{
    guint        *handles = user_data;
    guint         i = 0;
    const gchar **name;

    if (error) {
        DEBUG ("Error requesting aliases: %s", error->message);

        /* If we failed to get alias set it to NULL, like that if
         * someone is waiting for the name to be ready it won't wait
         * infinitely */
        while (*handles != 0) {
            EmpathyContact *contact;

            contact = tp_contact_factory_find_by_handle (
                (EmpathyTpContactFactory*) tp_factory,
                *handles);
            if (contact) {
                empathy_contact_set_name (contact, NULL);
            }

            handles++;
        }
        return;
    }

    for (name = contact_names; *name; name++) {
        EmpathyContact *contact;

        contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
                                 handles[i]);
        if (!contact) {
            continue;
        }

        DEBUG ("Renaming contact %s (%d) to %s (request cb)",
            empathy_contact_get_id (contact),
            empathy_contact_get_handle (contact),
            *name);

        empathy_contact_set_name (contact, *name);

        i++;
    }
}

static void
tp_contact_factory_aliases_changed_cb (TpConnection    *connection,
                       const GPtrArray *renamed_handlers,
                       gpointer         user_data,
                       GObject         *weak_object)
{
    EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
    guint                    i;

    for (i = 0; renamed_handlers->len > i; i++) {
        guint           handle;
        const gchar    *alias;
        GValueArray    *renamed_struct;
        EmpathyContact *contact;

        renamed_struct = g_ptr_array_index (renamed_handlers, i);
        handle = g_value_get_uint (g_value_array_get_nth (renamed_struct, 0));
        alias = g_value_get_string (g_value_array_get_nth (renamed_struct, 1));
        contact = tp_contact_factory_find_by_handle (tp_factory, handle);

        if (!contact) {
            /* We don't know this contact, skip */
            continue;
        }

        DEBUG ("Renaming contact %s (%d) to %s (changed cb)",
            empathy_contact_get_id (contact),
            handle, alias);

        empathy_contact_set_name (contact, alias);
    }
}

static void
tp_contact_factory_set_avatar_cb (TpConnection *connection,
                  const gchar  *token,
                  const GError *error,
                  gpointer      user_data,
                  GObject      *tp_factory)
{
    if (error) {
        DEBUG ("Error setting avatar: %s", error->message);
    }
}

static void
tp_contact_factory_clear_avatar_cb (TpConnection *connection,
                    const GError *error,
                    gpointer      user_data,
                    GObject      *tp_factory)
{
    if (error) {
        DEBUG ("Error clearing avatar: %s", error->message);
    }
}

static void
tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
                    guint         handle,
                    const gchar  *token,
                    const GArray *avatar_data,
                    const gchar  *mime_type,
                    gpointer      user_data,
                    GObject      *tp_factory)
{
    EmpathyContact *contact;

    contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
                             handle);
    if (!contact) {
        return;
    }

    DEBUG ("Avatar retrieved for contact %s (%d)",
        empathy_contact_get_id (contact),
        handle);

    empathy_contact_load_avatar_data (contact,
                      avatar_data->data,
                      avatar_data->len,
                      mime_type,
                      token);
}

static void
tp_contact_factory_request_avatars_cb (TpConnection *connection,
                       const GError *error,
                       gpointer      user_data,
                       GObject      *tp_factory)
{
    if (error) {
        DEBUG ("Error requesting avatars: %s", error->message);
    }
}

static gboolean
tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
                    guint                    handle,
                    const gchar             *token)
{
    EmpathyContact *contact;
    EmpathyAvatar  *avatar;

    contact = tp_contact_factory_find_by_handle (tp_factory, handle);
    if (!contact) {
        return TRUE;
    }

    /* Check if we have an avatar */
    if (G_STR_EMPTY (token)) {
        empathy_contact_set_avatar (contact, NULL);
        return TRUE;
    }

    /* Check if the avatar changed */
    avatar = empathy_contact_get_avatar (contact);
    if (avatar && !tp_strdiff (avatar->token, token)) {
        return TRUE;
    }

    /* The avatar changed, search the new one in the cache */
    if (empathy_contact_load_avatar_cache (contact, token)) {
        /* Got from cache, use it */
        return TRUE;
    }

    /* Avatar is not up-to-date, we have to request it. */
    return FALSE;
}

typedef struct {
    EmpathyTpContactFactory *tp_factory;
    GArray                  *handles;
} TokensData;

static void
tp_contact_factory_avatar_tokens_foreach (gpointer key,
                      gpointer value,
                      gpointer user_data)
{
    TokensData  *data = user_data;
    const gchar *token = value;
    guint        handle = GPOINTER_TO_UINT (key);

    if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
                             handle, token)) {
        g_array_append_val (data->handles, handle);
    }
}

static void
tp_contact_factory_get_known_avatar_tokens_cb (TpConnection *connection,
                           GHashTable   *tokens,
                           const GError *error,
                           gpointer      user_data,
                           GObject      *tp_factory)
{
    TokensData data;

    if (error) {
        DEBUG ("Error getting known avatars tokens: %s", error->message);
        return;
    }

    data.tp_factory = EMPATHY_TP_CONTACT_FACTORY (tp_factory);
    data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
    g_hash_table_foreach (tokens,
                  tp_contact_factory_avatar_tokens_foreach,
                  &data);

    DEBUG ("Got %d tokens, need to request %d avatars",
        g_hash_table_size (tokens), data.handles->len);

    /* Request needed avatars */
    if (data.handles->len > 0) {
        tp_cli_connection_interface_avatars_call_request_avatars (connection,
                                      -1,
                                      data.handles,
                                      tp_contact_factory_request_avatars_cb,
                                      NULL, NULL,
                                      tp_factory);
    }

    g_array_free (data.handles, TRUE);
}

static void
tp_contact_factory_avatar_updated_cb (TpConnection *connection,
                      guint         handle,
                      const gchar  *new_token,
                      gpointer      user_data,
                      GObject      *tp_factory)
{
    GArray *handles;

    if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
                            handle, new_token)) {
        /* Avatar was cached, nothing to do */
        return;
    }

    DEBUG ("Need to request avatar for token %s", new_token);

    handles = g_array_new (FALSE, FALSE, sizeof (guint));
    g_array_append_val (handles, handle);

    tp_cli_connection_interface_avatars_call_request_avatars (connection,
                                  -1,
                                  handles,
                                  tp_contact_factory_request_avatars_cb,
                                  NULL, NULL,
                                  tp_factory);
    g_array_free (handles, TRUE);
}

static void
tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
                    guint                    handle,
                    const gchar             *channel_type,
                    guint                    generic,
                    guint                    specific)
{
    EmpathyContact      *contact;
    EmpathyCapabilities  capabilities;

    contact = tp_contact_factory_find_by_handle (tp_factory, handle);
    if (!contact) {
        return;
    }

    capabilities = empathy_contact_get_capabilities (contact);
    capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;

    if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
        capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
        capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
        if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
            capabilities |= EMPATHY_CAPABILITIES_AUDIO;
        }
        if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
            capabilities |= EMPATHY_CAPABILITIES_VIDEO;
        }
    }

    DEBUG ("Changing capabilities for contact %s (%d) to %d",
        empathy_contact_get_id (contact),
        empathy_contact_get_handle (contact),
        capabilities);

    empathy_contact_set_capabilities (contact, capabilities);
}

static void
tp_contact_factory_get_capabilities_cb (TpConnection    *connection,
                    const GPtrArray *capabilities,
                    const GError    *error,
                    gpointer         user_data,
                    GObject         *weak_object)
{
    EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
    guint                    i;

    if (error) {
        DEBUG ("Error getting capabilities: %s", error->message);
        /* FIXME Should set the capabilities of the contacts for which this request
         * originated to NONE */
        return;
    }

    for (i = 0; i < capabilities->len; i++) {
        GValueArray *values;
        guint        handle;
        const gchar *channel_type;
        guint        generic;
        guint        specific;

        values = g_ptr_array_index (capabilities, i);
        handle = g_value_get_uint (g_value_array_get_nth (values, 0));
        channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
        generic = g_value_get_uint (g_value_array_get_nth (values, 2));
        specific = g_value_get_uint (g_value_array_get_nth (values, 3));

        tp_contact_factory_update_capabilities (tp_factory,
                            handle,
                            channel_type,
                            generic,
                            specific);
    }
}

static void
tp_contact_factory_capabilities_changed_cb (TpConnection    *connection,
                        const GPtrArray *capabilities,
                        gpointer         user_data,
                        GObject         *weak_object)
{
    EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
    guint                    i;

    for (i = 0; i < capabilities->len; i++) {
        GValueArray *values;
        guint        handle;
        const gchar *channel_type;
        guint        generic;
        guint        specific;

        values = g_ptr_array_index (capabilities, i);
        handle = g_value_get_uint (g_value_array_get_nth (values, 0));
        channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
        generic = g_value_get_uint (g_value_array_get_nth (values, 3));
        specific = g_value_get_uint (g_value_array_get_nth (values, 5));

        tp_contact_factory_update_capabilities (tp_factory,
                            handle,
                            channel_type,
                            generic,
                            specific);
    }
}

static void
tp_contact_factory_request_everything (EmpathyTpContactFactory *tp_factory,
                       const GArray            *handles)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
    guint                       *dup_handles;

    g_return_if_fail (priv->ready);

    dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
    g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
    tp_cli_connection_interface_presence_call_get_presence (priv->connection,
                                -1,
                                handles,
                                tp_contact_factory_get_presence_cb,
                                dup_handles, g_free,
                                G_OBJECT (tp_factory));

    /* FIXME: Sometimes the dbus call timesout because CM takes
     * too much time to request all aliases from the server,
     * that's why we increase the timeout here. See fd.o bug #14795 */
    dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
    g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
    tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
                                   5*60*1000,
                                   handles,
                                   tp_contact_factory_request_aliases_cb,
                                   dup_handles, g_free,
                                   G_OBJECT (tp_factory));

    tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
                                      -1,
                                      handles,
                                      tp_contact_factory_get_known_avatar_tokens_cb,
                                      NULL, NULL,
                                      G_OBJECT (tp_factory));

    tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
                                    -1,
                                    handles,
                                    tp_contact_factory_get_capabilities_cb,
                                    NULL, NULL,
                                    G_OBJECT (tp_factory));
}

static void
tp_contact_factory_list_free (gpointer data)
{
    GList *l = data;

    g_list_foreach (l, (GFunc) g_object_unref, NULL);
    g_list_free (l);
}

static void
tp_contact_factory_request_handles_cb (TpConnection *connection,
                       const GArray *handles,
                       const GError *error,
                       gpointer      user_data,
                       GObject      *tp_factory)
{
    GList *contacts = user_data;
    GList *l;
    guint  i = 0;

    if (error) {
        DEBUG ("Failed to request handles: %s", error->message);
        return;
    }

    for (l = contacts; l; l = l->next) {
        guint handle;

        handle = g_array_index (handles, guint, i);
        empathy_contact_set_handle (l->data, handle);

        i++;
    }

    tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
                           handles);
}

static void
tp_contact_factory_inspect_handles_cb (TpConnection  *connection,
                       const gchar  **ids,
                       const GError  *error,
                       gpointer       user_data,
                       GObject       *tp_factory)
{
    const gchar **id;
    GList        *contacts = user_data;
    GList        *l;

    if (error) {
        DEBUG ("Failed to inspect handles: %s", error->message);
        return;
    }

    id = ids;
    for (l = contacts; l; l = l->next) {
        empathy_contact_set_id (l->data, *id);
        id++;
    }
}

static void
tp_contact_factory_disconnect_contact_foreach (gpointer data,
                           gpointer user_data)
{
    EmpathyContact *contact = data;
    
    empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
    empathy_contact_set_handle (contact, 0);
}

static void
tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);

    DEBUG ("Connection invalidated");

    g_object_unref (priv->connection);
    priv->connection = NULL;
    priv->ready = FALSE;
    g_object_notify (G_OBJECT (tp_factory), "ready");


    g_list_foreach (priv->contacts,
            tp_contact_factory_disconnect_contact_foreach,
            tp_factory);
}

static void
tp_contact_factory_ready (EmpathyTpContactFactory *tp_factory)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
    GList                       *l;
    GArray                      *handle_needed;
    GArray                      *id_needed;
    GList                       *handle_needed_contacts = NULL;
    GList                       *id_needed_contacts = NULL;

    DEBUG ("Connection ready");

    priv->ready = TRUE;
    g_object_notify (G_OBJECT (tp_factory), "ready");

    /* Connect signals */
    tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
                                     tp_contact_factory_aliases_changed_cb,
                                     NULL, NULL,
                                     G_OBJECT (tp_factory),
                                     NULL);
    tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
                                       tp_contact_factory_avatar_updated_cb,
                                       NULL, NULL,
                                       G_OBJECT (tp_factory),
                                       NULL);
    tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
                                     tp_contact_factory_avatar_retrieved_cb,
                                     NULL, NULL,
                                     G_OBJECT (tp_factory),
                                     NULL);
    tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
                                     tp_contact_factory_presence_update_cb,
                                     NULL, NULL,
                                     G_OBJECT (tp_factory),
                                     NULL);
    tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
                                          tp_contact_factory_capabilities_changed_cb,
                                          NULL, NULL,
                                          G_OBJECT (tp_factory),
                                          NULL);

    /* Request needed info for all existing contacts */
    handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
    id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
    for (l = priv->contacts; l; l = l->next) {
        EmpathyContact *contact;
        guint           handle;
        const gchar    *id;

        contact = l->data;
        handle = empathy_contact_get_handle (contact);
        id = empathy_contact_get_id (contact);
        if (handle == 0) {
            g_assert (!G_STR_EMPTY (id));
            g_array_append_val (handle_needed, id);
            handle_needed_contacts = g_list_prepend (handle_needed_contacts,
                                 g_object_ref (contact));
        }
        if (G_STR_EMPTY (id)) {
            g_array_append_val (id_needed, handle);
            id_needed_contacts = g_list_prepend (id_needed_contacts,
                                 g_object_ref (contact));
        }
    }
    handle_needed_contacts = g_list_reverse (handle_needed_contacts);
    id_needed_contacts = g_list_reverse (id_needed_contacts);

    tp_cli_connection_call_request_handles (priv->connection,
                        -1,
                        TP_HANDLE_TYPE_CONTACT,
                        (const gchar**) handle_needed->data,
                        tp_contact_factory_request_handles_cb,
                        handle_needed_contacts, tp_contact_factory_list_free,
                        G_OBJECT (tp_factory));

    tp_cli_connection_call_inspect_handles (priv->connection,
                        -1,
                        TP_HANDLE_TYPE_CONTACT,
                        id_needed,
                        tp_contact_factory_inspect_handles_cb,
                        id_needed_contacts, tp_contact_factory_list_free,
                        G_OBJECT (tp_factory));

    tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
                           id_needed);

    g_array_free (handle_needed, TRUE);
    g_array_free (id_needed, TRUE);
}

static void
tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
                           const gchar **mime_types,
                           guint         min_width,
                           guint         min_height,
                           guint         max_width,
                           guint         max_height,
                           guint         max_size,
                           const GError *error,
                           gpointer      user_data,
                           GObject      *tp_factory)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);

    if (error) {
        DEBUG ("Failed to get avatar requirements: %s", error->message);
        /* We'll just leave avatar_mime_types as NULL; the
         * avatar-setting code can use this as a signal that you can't
         * set avatars.
         */
    } else {
        priv->avatar_mime_types = g_strdupv ((gchar **) mime_types);
        priv->avatar_min_width = min_width;
        priv->avatar_min_height = min_height;
        priv->avatar_max_width = max_width;
        priv->avatar_max_height = max_height;
        priv->avatar_max_size = max_size;
    }

    tp_contact_factory_ready (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
}

static void
tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
                       guint         handle,
                       const GError *error,
                       gpointer      user_data,
                       GObject      *tp_factory)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);

    if (error) {
        DEBUG ("Failed to get self handles: %s", error->message);
        return;
    }

    empathy_contact_set_handle (priv->user, handle);

    /* Get avatar requirements for this connection */
    tp_cli_connection_interface_avatars_call_get_avatar_requirements (
        priv->connection,
        -1,
        tp_contact_factory_got_avatar_requirements_cb,
        NULL, NULL,
        tp_factory);
}

static void
tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);

    /* Get our own handle */
    tp_cli_connection_call_get_self_handle (priv->connection,
                        -1,
                        tp_contact_factory_got_self_handle_cb,
                        NULL, NULL,
                        G_OBJECT (tp_factory));
}

static void
tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
    gboolean                     connection_ready;

    if (priv->connection) {
        /* We already have our connection object */
        return;
    }

    priv->connection = mission_control_get_tpconnection (priv->mc, priv->account, NULL);
    if (!priv->connection) {
        return;
    }

    /* We got a new connection, wait for it to be ready */
    g_signal_connect_swapped (priv->connection, "invalidated",
                  G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
                  tp_factory);

    g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
    if (connection_ready) {
        tp_contact_factory_connection_ready_cb (tp_factory);
    } else {
        g_signal_connect_swapped (priv->connection, "notify::connection-ready",
                      G_CALLBACK (tp_contact_factory_connection_ready_cb),
                      tp_factory);
    }
}

static void
tp_contact_factory_status_changed_cb (MissionControl           *mc,
                      TpConnectionStatus        status,
                      McPresence                presence,
                      TpConnectionStatusReason  reason,
                      const gchar              *unique_name,
                      EmpathyTpContactFactory  *tp_factory)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
    McAccount                   *account;

    account = mc_account_lookup (unique_name);
    if (account && empathy_account_equal (account, priv->account)) {
        tp_contact_factory_status_updated (tp_factory);
    }
    g_object_unref (account);
}

static void
tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
                EmpathyContact          *contact)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);

    g_object_weak_ref (G_OBJECT (contact),
               tp_contact_factory_weak_notify,
               tp_factory);
    priv->contacts = g_list_prepend (priv->contacts, contact);

    DEBUG ("Contact added: %s (%d)",
        empathy_contact_get_id (contact),
        empathy_contact_get_handle (contact));
}

static void
tp_contact_factory_hold_handles_cb (TpConnection *connection,
                    const GError *error,
                    gpointer      userdata,
                    GObject      *tp_factory)
{
    if (error) {
        DEBUG ("Failed to hold handles: %s", error->message);
    }
}

EmpathyContact *
empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);

    g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);

    return g_object_ref (priv->user);
}

EmpathyContact *
empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
                    const gchar             *id)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
    EmpathyContact              *contact;

    g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
    g_return_val_if_fail (id != NULL, NULL);

    /* Check if the contact already exists */
    contact = tp_contact_factory_find_by_id (tp_factory, id);
    if (contact) {
        return g_object_ref (contact);
    }

    /* Create new contact */
    contact = g_object_new (EMPATHY_TYPE_CONTACT,
                "account", priv->account,
                "id", id,
                NULL);
    tp_contact_factory_add_contact (tp_factory, contact);

    if (priv->ready) {
        const gchar *contact_ids[] = {id, NULL};
        GList       *contacts;
        
        contacts = g_list_prepend (NULL, g_object_ref (contact));
        tp_cli_connection_call_request_handles (priv->connection,
                            -1,
                            TP_HANDLE_TYPE_CONTACT,
                            contact_ids,
                            tp_contact_factory_request_handles_cb,
                            contacts, tp_contact_factory_list_free,
                            G_OBJECT (tp_factory));
    }

    return contact;
}

EmpathyContact *
empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
                        guint                    handle)
{
    EmpathyContact *contact;
    GArray         *handles;
    GList          *contacts;

    g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);

    handles = g_array_new (FALSE, FALSE, sizeof (guint));
    g_array_append_val (handles, handle);

    contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
    g_array_free (handles, TRUE);

    contact = contacts ? contacts->data : NULL;
    g_list_free (contacts);

    return contact;
}

GList *
empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
                         const GArray            *handles)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
    GList                       *contacts = NULL;
    GArray                      *new_handles;
    GList                       *new_contacts = NULL;
    guint                        i;

    g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
    g_return_val_if_fail (handles != NULL, NULL);

    /* Search all contacts we already have */
    new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
    for (i = 0; i < handles->len; i++) {
        EmpathyContact *contact;
        guint           handle;

        handle = g_array_index (handles, guint, i);
        if (handle == 0) {
            continue;
        }

        contact = tp_contact_factory_find_by_handle (tp_factory, handle);
        if (contact) {
            contacts = g_list_prepend (contacts, g_object_ref (contact));
        } else {
            g_array_append_val (new_handles, handle);
        }
    }

    if (new_handles->len == 0) {
        g_array_free (new_handles, TRUE);
        return contacts;
    }

    /* Create new contacts */
    for (i = 0; i < new_handles->len; i++) {
        EmpathyContact *contact;
        guint           handle;

        handle = g_array_index (new_handles, guint, i);

        contact = g_object_new (EMPATHY_TYPE_CONTACT,
                    "account", priv->account,
                    "handle", handle,
                    NULL);
        tp_contact_factory_add_contact (tp_factory, contact);
        contacts = g_list_prepend (contacts, contact);
        new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
    }
    new_contacts = g_list_reverse (new_contacts);

    if (priv->ready) {
        /* Get the IDs of all new handles */
        tp_cli_connection_call_inspect_handles (priv->connection,
                            -1,
                            TP_HANDLE_TYPE_CONTACT,
                            new_handles,
                            tp_contact_factory_inspect_handles_cb,
                            new_contacts, tp_contact_factory_list_free,
                            G_OBJECT (tp_factory));

        /* Hold all new handles. */
        /* FIXME: Should be unholded when removed from the factory */
        tp_cli_connection_call_hold_handles (priv->connection,
                             -1,
                             TP_HANDLE_TYPE_CONTACT,
                             new_handles,
                             tp_contact_factory_hold_handles_cb,
                             NULL, NULL,
                             G_OBJECT (tp_factory));

        tp_contact_factory_request_everything (tp_factory, new_handles);
    }

    g_array_free (new_handles, TRUE);

    return contacts;
}

void
empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
                      EmpathyContact          *contact,
                      const gchar             *alias)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
    GHashTable                  *new_alias;
    guint                        handle;

    g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
    g_return_if_fail (EMPATHY_IS_CONTACT (contact));
    g_return_if_fail (priv->ready);
    g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
                         priv->account));

    handle = empathy_contact_get_handle (contact);

    DEBUG ("Setting alias for contact %s (%d) to %s",
        empathy_contact_get_id (contact),
        handle, alias);

    new_alias = g_hash_table_new_full (g_direct_hash,
                       g_direct_equal,
                       NULL,
                       g_free);

    g_hash_table_insert (new_alias,
                 GUINT_TO_POINTER (handle),
                 g_strdup (alias));

    tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
                                   -1,
                                   new_alias,
                                   tp_contact_factory_set_aliases_cb,
                                   NULL, NULL,
                                   G_OBJECT (tp_factory));

    g_hash_table_destroy (new_alias);
}

void
empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
                       const gchar             *data,
                       gsize                    size,
                       const gchar             *mime_type)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);

    g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
    g_return_if_fail (priv->ready);

    if (data && size > 0 && size < G_MAXUINT) {
        GArray avatar;

        avatar.data = (gchar*) data;
        avatar.len = size;

        DEBUG ("Setting avatar on account %s",
            mc_account_get_unique_name (priv->account));

        tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
                                     -1,
                                     &avatar,
                                     mime_type,
                                     tp_contact_factory_set_avatar_cb,
                                     NULL, NULL,
                                     G_OBJECT (tp_factory));
    } else {
        DEBUG ("Clearing avatar on account %s",
            mc_account_get_unique_name (priv->account));

        tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
                                       -1,
                                       tp_contact_factory_clear_avatar_cb,
                                       NULL, NULL,
                                       G_OBJECT (tp_factory));
    }
}

gboolean
empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);

    g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);

    return priv->ready;
}

static void
tp_contact_factory_get_property (GObject    *object,
                 guint       param_id,
                 GValue     *value,
                 GParamSpec *pspec)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);

    switch (param_id) {
    case PROP_ACCOUNT:
        g_value_set_object (value, priv->account);
        break;
    case PROP_READY:
        g_value_set_boolean (value, priv->ready);
        break;
    case PROP_MIME_TYPES:
        g_value_set_boxed (value, priv->avatar_mime_types);
        break;
    case PROP_MIN_WIDTH:
        g_value_set_uint (value, priv->avatar_min_width);
        break;
    case PROP_MIN_HEIGHT:
        g_value_set_uint (value, priv->avatar_min_height);
        break;
    case PROP_MAX_WIDTH:
        g_value_set_uint (value, priv->avatar_max_width);
        break;
    case PROP_MAX_HEIGHT:
        g_value_set_uint (value, priv->avatar_max_height);
        break;
    case PROP_MAX_SIZE:
        g_value_set_uint (value, priv->avatar_max_size);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    };
}

static void
tp_contact_factory_set_property (GObject      *object,
                 guint         param_id,
                 const GValue *value,
                 GParamSpec   *pspec)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);

    switch (param_id) {
    case PROP_ACCOUNT:
        priv->account = g_object_ref (g_value_get_object (value));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    };
}

static void
tp_contact_factory_finalize (GObject *object)
{
    EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
    GList                       *l;

    DEBUG ("Finalized: %p (%s)", object,
        mc_account_get_normalized_name (priv->account));

    empathy_disconnect_account_status_changed (priv->token);

    for (l = priv->contacts; l; l = l->next) {
        g_object_weak_unref (G_OBJECT (l->data),
                     tp_contact_factory_weak_notify,
                     object);
    }

    g_list_free (priv->contacts);
    g_object_unref (priv->mc);
    g_object_unref (priv->account);
    g_object_unref (priv->user);

    if (priv->connection) {
        g_signal_handlers_disconnect_by_func (priv->connection,
                              tp_contact_factory_connection_invalidated_cb,
                              object);
        g_object_unref (priv->connection);
    }

    g_strfreev (priv->avatar_mime_types);

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

static GObject *
tp_contact_factory_constructor (GType                  type,
                guint                  n_props,
                GObjectConstructParam *props)
{
    GObject *tp_factory;
    EmpathyTpContactFactoryPriv *priv;

    tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
    priv = GET_PRIV (tp_factory);

    priv->ready = FALSE;
    priv->user = empathy_contact_new (priv->account);
    empathy_contact_set_is_user (priv->user, TRUE);
    tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
    tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));

    return tp_factory;
}

static void
empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    object_class->finalize = tp_contact_factory_finalize;
    object_class->constructor = tp_contact_factory_constructor;
    object_class->get_property = tp_contact_factory_get_property;
    object_class->set_property = tp_contact_factory_set_property;

    g_object_class_install_property (object_class,
                     PROP_ACCOUNT,
                     g_param_spec_object ("account",
                                  "Factory's Account",
                                  "The account associated with the factory",
                                  MC_TYPE_ACCOUNT,
                                  G_PARAM_READWRITE |
                                  G_PARAM_CONSTRUCT_ONLY |
                                  G_PARAM_STATIC_STRINGS));
    g_object_class_install_property (object_class,
                     PROP_READY,
                     g_param_spec_boolean ("ready",
                                   "Whether the factory is ready",
                                   "TRUE once the factory is ready to be used",
                                   FALSE,
                                   G_PARAM_READABLE |
                                   G_PARAM_STATIC_STRINGS));
    g_object_class_install_property (object_class,
                     PROP_MIME_TYPES,
                     g_param_spec_boxed ("avatar-mime-types",
                                 "Supported MIME types for avatars",
                                 "Types of images that may be set as "
                                 "avatars on this connection.  Only valid "
                                 "once 'ready' becomes TRUE.",
                                 G_TYPE_STRV,
                                 G_PARAM_READABLE |
                                 G_PARAM_STATIC_STRINGS));
    g_object_class_install_property (object_class,
                     PROP_MIN_WIDTH,
                     g_param_spec_uint ("avatar-min-width",
                                "Minimum width for avatars",
                                "Minimum width of avatar that may be set. "
                                "Only valid once 'ready' becomes TRUE.",
                                0,
                                G_MAXUINT,
                                0,
                                G_PARAM_READABLE |
                                G_PARAM_STATIC_STRINGS));
    g_object_class_install_property (object_class,
                     PROP_MIN_HEIGHT,
                     g_param_spec_uint ("avatar-min-height",
                                "Minimum height for avatars",
                                "Minimum height of avatar that may be set. "
                                "Only valid once 'ready' becomes TRUE.",
                                0,
                                G_MAXUINT,
                                0,
                                G_PARAM_READABLE |
                                G_PARAM_STATIC_STRINGS));
    g_object_class_install_property (object_class,
                     PROP_MAX_WIDTH,
                     g_param_spec_uint ("avatar-max-width",
                                "Maximum width for avatars",
                                "Maximum width of avatar that may be set "
                                "or 0 if there is no maximum. "
                                "Only valid once 'ready' becomes TRUE.",
                                0,
                                G_MAXUINT,
                                0,
                                G_PARAM_READABLE |
                                G_PARAM_STATIC_STRINGS));
    g_object_class_install_property (object_class,
                     PROP_MAX_HEIGHT,
                     g_param_spec_uint ("avatar-max-height",
                                "Maximum height for avatars",
                                "Maximum height of avatar that may be set "
                                "or 0 if there is no maximum. "
                                "Only valid once 'ready' becomes TRUE.",
                                0,
                                G_MAXUINT,
                                0,
                                G_PARAM_READABLE |
                                G_PARAM_STATIC_STRINGS));
    g_object_class_install_property (object_class,
                     PROP_MAX_SIZE,
                     g_param_spec_uint ("avatar-max-size",
                                "Maximum size for avatars in bytes",
                                "Maximum file size of avatar that may be "
                                "set or 0 if there is no maximum. "
                                "Only valid once 'ready' becomes TRUE.",
                                0,
                                G_MAXUINT,
                                0,
                                G_PARAM_READABLE |
                                G_PARAM_STATIC_STRINGS));


    g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
}

static void
empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
{
    EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
        EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);

    tp_factory->priv = priv;
    priv->mc = empathy_mission_control_new ();
    priv->token = empathy_connect_to_account_status_changed (priv->mc,
                           G_CALLBACK (tp_contact_factory_status_changed_cb),
                           tp_factory, NULL);
}

EmpathyTpContactFactory *
empathy_tp_contact_factory_new (McAccount *account)
{
    return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,
                 "account", account,
                 NULL);
}