]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-factory.c
Make sure we don't expect contact id and handle to be directly ready. Add some _run_u...
[empathy.git] / libempathy / empathy-tp-contact-factory.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  * 
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <telepathy-glib/util.h>
27 #include <telepathy-glib/connection.h>
28 #include <libmissioncontrol/mission-control.h>
29
30 #include "empathy-tp-contact-factory.h"
31 #include "empathy-utils.h"
32 #include "empathy-debug.h"
33
34 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
35                        EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv))
36
37 #define DEBUG_DOMAIN "TpContactFactory"
38
39 struct _EmpathyTpContactFactoryPriv {
40         MissionControl *mc;
41         McAccount      *account;
42         TpConnection   *connection;
43         gboolean        ready;
44
45         GList          *contacts;
46         EmpathyContact *user;
47 };
48
49 static void empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass);
50 static void empathy_tp_contact_factory_init       (EmpathyTpContactFactory      *factory);
51
52 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
53
54 enum {
55         PROP_0,
56         PROP_ACCOUNT,
57 };
58
59 static EmpathyContact *
60 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
61                                    guint                    handle)
62 {
63         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
64         GList                       *l;
65
66         for (l = priv->contacts; l; l = l->next) {
67                 if (empathy_contact_get_handle (l->data) == handle) {
68                         return l->data;
69                 }
70         }
71
72         return NULL;
73 }
74
75 static EmpathyContact *
76 tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
77                                const gchar             *id)
78 {
79         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
80         GList                       *l;
81
82         for (l = priv->contacts; l; l = l->next) {
83                 if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
84                         return l->data;
85                 }
86         }
87
88         return NULL;
89 }
90
91 static void
92 tp_contact_factory_weak_notify (gpointer data,
93                                 GObject *where_the_object_was)
94 {
95         EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
96
97         empathy_debug (DEBUG_DOMAIN, "Remove finalized contact %p",
98                        where_the_object_was);
99
100         priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
101 }
102
103 static void
104 tp_contact_factory_presences_table_foreach (const gchar    *state_str,
105                                             GHashTable     *presences_table,
106                                             EmpathyContact *contact)
107 {
108         const GValue *message;
109
110         empathy_contact_set_presence (contact,
111                                       empathy_presence_from_str (state_str));
112         
113         message = g_hash_table_lookup (presences_table, "message");
114         if (message != NULL) {
115                 empathy_contact_set_presence_message (contact,
116                                                       g_value_get_string (message));
117         } else {
118                 empathy_contact_set_presence_message (contact, NULL);
119         }
120 }
121
122 static void
123 tp_contact_factory_parse_presence_foreach (guint                    handle,
124                                            GValueArray             *presence_struct,
125                                            EmpathyTpContactFactory *tp_factory)
126 {
127         GHashTable      *presences_table;
128         EmpathyContact  *contact;
129
130         contact = tp_contact_factory_find_by_handle (tp_factory, handle);
131         if (!contact) {
132                 return;
133         }
134
135         presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
136
137         g_hash_table_foreach (presences_table,
138                               (GHFunc) tp_contact_factory_presences_table_foreach,
139                               contact);
140
141         empathy_debug (DEBUG_DOMAIN, "Changing presence for contact %s (%d) to %s (%d)",
142                       empathy_contact_get_id (contact),
143                       handle,
144                       empathy_contact_get_presence_message (contact),
145                       empathy_contact_get_presence (contact));
146 }
147
148 static void
149 tp_contact_factory_get_presence_cb (TpConnection *connection,
150                                     GHashTable   *handle_table,
151                                     const GError *error,
152                                     gpointer      user_data,
153                                     GObject      *tp_factory)
154 {
155         if (error) {
156                 empathy_debug (DEBUG_DOMAIN, "Error getting presence: %s",
157                               error->message);
158                 if (error->domain == TP_DBUS_ERRORS &&
159                     error->code == TP_DBUS_ERROR_NO_INTERFACE) {
160                         guint *handles = user_data;
161
162                         /* We have no presence iface, set default presence
163                          * to available */
164                         while (*handles != 0) {
165                                 EmpathyContact *contact;
166
167                                 contact = tp_contact_factory_find_by_handle (
168                                         (EmpathyTpContactFactory*) tp_factory,
169                                         *handles);
170                                 if (contact) {
171                                         empathy_contact_set_presence (contact,
172                                                                       MC_PRESENCE_AVAILABLE);
173                                 }
174
175                                 handles++;
176                         }
177                 }
178
179                 return;
180         }
181
182         g_hash_table_foreach (handle_table,
183                               (GHFunc) tp_contact_factory_parse_presence_foreach,
184                               EMPATHY_TP_CONTACT_FACTORY (tp_factory));
185 }
186
187 static void
188 tp_contact_factory_presence_update_cb (TpConnection *connection,
189                                        GHashTable   *handle_table,
190                                        gpointer      user_data,
191                                        GObject      *tp_factory)
192 {
193         g_hash_table_foreach (handle_table,
194                               (GHFunc) tp_contact_factory_parse_presence_foreach,
195                               EMPATHY_TP_CONTACT_FACTORY (tp_factory));
196 }
197
198 static void
199 tp_contact_factory_set_aliases_cb (TpConnection *connection,
200                                    const GError *error,
201                                    gpointer      user_data,
202                                    GObject      *tp_factory)
203 {
204         if (error) {
205                 empathy_debug (DEBUG_DOMAIN, "Error setting alias: %s",
206                                error->message);
207         }
208 }
209
210 static void
211 tp_contact_factory_request_aliases_cb (TpConnection *connection,
212                                        const gchar  **contact_names,
213                                        const GError  *error,
214                                        gpointer       user_data,
215                                        GObject       *tp_factory)
216 {
217         guint        *handles = user_data;
218         guint         i = 0;
219         const gchar **name;
220
221         if (error) {
222                 empathy_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
223                               error->message);
224                 return;
225         }
226
227         for (name = contact_names; *name; name++) {
228                 EmpathyContact *contact;
229
230                 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
231                                                              handles[i]);
232                 if (!contact) {
233                         continue;
234                 }
235
236                 empathy_debug (DEBUG_DOMAIN, "Renaming contact %s (%d) to %s (request cb)",
237                                empathy_contact_get_id (contact),
238                                empathy_contact_get_handle (contact),
239                                *name);
240
241                 empathy_contact_set_name (contact, *name);
242
243                 i++;
244         }
245 }
246
247 static void
248 tp_contact_factory_aliases_changed_cb (TpConnection    *connection,
249                                        const GPtrArray *renamed_handlers,
250                                        gpointer         user_data,
251                                        GObject         *weak_object)
252 {
253         EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
254         guint                    i;
255
256         for (i = 0; renamed_handlers->len > i; i++) {
257                 guint           handle;
258                 const gchar    *alias;
259                 GValueArray    *renamed_struct;
260                 EmpathyContact *contact;
261
262                 renamed_struct = g_ptr_array_index (renamed_handlers, i);
263                 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
264                 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
265                 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
266
267                 if (!contact) {
268                         /* We don't know this contact, skip */
269                         continue;
270                 }
271
272                 empathy_debug (DEBUG_DOMAIN, "Renaming contact %s (%d) to %s (changed cb)",
273                                empathy_contact_get_id (contact),
274                                handle, alias);
275
276                 empathy_contact_set_name (contact, alias);
277         }
278 }
279
280 static void
281 tp_contact_factory_set_avatar_cb (TpConnection *connection,
282                                   const gchar  *token,
283                                   const GError *error,
284                                   gpointer      user_data,
285                                   GObject      *tp_factory)
286 {
287         if (error) {
288                 empathy_debug (DEBUG_DOMAIN, "Error setting avatar: %s",
289                                error->message);
290         }
291 }
292
293 static void
294 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
295                                     const GError *error,
296                                     gpointer      user_data,
297                                     GObject      *tp_factory)
298 {
299         if (error) {
300                 empathy_debug (DEBUG_DOMAIN, "Error clearing avatar: %s",
301                                error->message);
302         }
303 }
304
305 static void
306 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
307                                         guint         handle,
308                                         const gchar  *token,
309                                         const GArray *avatar_data,
310                                         const gchar  *mime_type,
311                                         gpointer      user_data,
312                                         GObject      *tp_factory)
313 {
314         EmpathyContact *contact;
315         EmpathyAvatar  *avatar;
316
317         contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
318                                                      handle);
319         if (!contact) {
320                 return;
321         }
322
323         empathy_debug (DEBUG_DOMAIN, "Avatar retrieved for contact %s (%d)",
324                        empathy_contact_get_id (contact),
325                        handle);
326
327         avatar = empathy_avatar_new (avatar_data->data,
328                                      avatar_data->len,
329                                      mime_type,
330                                      token);
331
332         empathy_contact_set_avatar (contact, avatar);
333         empathy_avatar_unref (avatar);
334 }
335
336 static void
337 tp_contact_factory_request_avatars_cb (TpConnection *connection,
338                                        const GError *error,
339                                        gpointer      user_data,
340                                        GObject      *tp_factory)
341 {
342         if (error) {
343                 empathy_debug (DEBUG_DOMAIN, "Error requesting avatars: %s",
344                                error->message);
345         }
346 }
347
348 static gboolean
349 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
350                                         guint                    handle,
351                                         const gchar             *token)
352 {
353         EmpathyContact *contact;
354         EmpathyAvatar  *avatar;
355
356         contact = tp_contact_factory_find_by_handle (tp_factory, handle);
357         if (!contact) {
358                 return TRUE;
359         }
360
361         /* Check if we have an avatar */
362         if (G_STR_EMPTY (token)) {
363                 empathy_contact_set_avatar (contact, NULL);
364                 return TRUE;
365         }
366
367         /* Check if the avatar changed */
368         avatar = empathy_contact_get_avatar (contact);
369         if (avatar && !tp_strdiff (avatar->token, token)) {
370                 return TRUE;
371         }
372
373         /* The avatar changed, search the new one in the cache */
374         avatar = empathy_avatar_new_from_cache (token);
375         if (avatar) {
376                 /* Got from cache, use it */
377                 empathy_contact_set_avatar (contact, avatar);
378                 empathy_avatar_unref (avatar);
379                 return TRUE;
380         }
381
382         /* Avatar is not up-to-date, we have to request it. */
383         return FALSE;
384 }
385
386 typedef struct {
387         EmpathyTpContactFactory *tp_factory;
388         GArray                  *handles;
389 } TokensData;
390
391 static void
392 tp_contact_factory_avatar_tokens_foreach (gpointer key,
393                                           gpointer value,
394                                           gpointer user_data)
395 {
396         TokensData  *data = user_data;
397         const gchar *token = value;
398         guint        handle = GPOINTER_TO_UINT (key);
399
400         if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
401                                                      handle, token)) {
402                 g_array_append_val (data->handles, handle);
403         }
404 }
405
406 static void
407 tp_contact_factory_get_known_avatar_tokens_cb (TpConnection *connection,
408                                                GHashTable   *tokens,
409                                                const GError *error,
410                                                gpointer      user_data,
411                                                GObject      *tp_factory)
412 {
413         TokensData data;
414
415         if (error) {
416                 empathy_debug (DEBUG_DOMAIN,
417                                "Error getting known avatars tokens: %s",
418                                error->message);
419                 return;
420         }
421
422         data.tp_factory = EMPATHY_TP_CONTACT_FACTORY (tp_factory);
423         data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
424         g_hash_table_foreach (tokens,
425                               tp_contact_factory_avatar_tokens_foreach,
426                               &data);
427
428         empathy_debug (DEBUG_DOMAIN, "Got %d tokens, need to request %d avatars",
429                        g_hash_table_size (tokens),
430                        data.handles->len);
431
432         /* Request needed avatars */
433         if (data.handles->len > 0) {
434                 tp_cli_connection_interface_avatars_call_request_avatars (connection,
435                                                                           -1,
436                                                                           data.handles,
437                                                                           tp_contact_factory_request_avatars_cb,
438                                                                           NULL, NULL,
439                                                                           tp_factory);
440         }
441
442         g_array_free (data.handles, TRUE);
443 }
444
445 static void
446 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
447                                       guint         handle,
448                                       const gchar  *new_token,
449                                       gpointer      user_data,
450                                       GObject      *tp_factory)
451 {
452         GArray *handles;
453
454         if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
455                                                     handle, new_token)) {
456                 /* Avatar was cached, nothing to do */
457                 return;
458         }
459
460         empathy_debug (DEBUG_DOMAIN, "Need to request avatar for token %s",
461                        new_token);
462
463         handles = g_array_new (FALSE, FALSE, sizeof (guint));
464         g_array_append_val (handles, handle);
465
466         tp_cli_connection_interface_avatars_call_request_avatars (connection,
467                                                                   -1,
468                                                                   handles,
469                                                                   tp_contact_factory_request_avatars_cb,
470                                                                   NULL, NULL,
471                                                                   tp_factory);
472         g_array_free (handles, TRUE);
473 }
474
475 static void
476 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
477                                         guint                    handle,
478                                         const gchar             *channel_type,
479                                         guint                    generic,
480                                         guint                    specific)
481 {
482         EmpathyContact      *contact;
483         EmpathyCapabilities  capabilities;
484
485         contact = tp_contact_factory_find_by_handle (tp_factory, handle);
486         if (!contact) {
487                 return;
488         }
489
490         capabilities = empathy_contact_get_capabilities (contact);
491         capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
492
493         if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
494                 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
495                 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
496                 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
497                         capabilities |= EMPATHY_CAPABILITIES_AUDIO;
498                 }
499                 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
500                         capabilities |= EMPATHY_CAPABILITIES_VIDEO;
501                 }
502         }
503
504         empathy_debug (DEBUG_DOMAIN, "Changing capabilities for contact %s (%d) to %d",
505                        empathy_contact_get_id (contact),
506                        empathy_contact_get_handle (contact),
507                        capabilities);
508
509         empathy_contact_set_capabilities (contact, capabilities);
510 }
511
512 static void
513 tp_contact_factory_get_capabilities_cb (TpConnection    *connection,
514                                         const GPtrArray *capabilities,
515                                         const GError    *error,
516                                         gpointer         user_data,
517                                         GObject         *weak_object)
518 {
519         EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
520         guint                    i;
521
522         if (error) {
523                 empathy_debug (DEBUG_DOMAIN, "Error getting capabilities: %s",
524                                error->message);
525                 /* FIXME Should set the capabilities of the contacts for which this request
526                  * originated to NONE */
527                 return;
528         }
529
530         for (i = 0; i < capabilities->len; i++) {
531                 GValueArray *values;
532                 guint        handle;
533                 const gchar *channel_type;
534                 guint        generic;
535                 guint        specific;
536
537                 values = g_ptr_array_index (capabilities, i);
538                 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
539                 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
540                 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
541                 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
542
543                 tp_contact_factory_update_capabilities (tp_factory,
544                                                         handle,
545                                                         channel_type,
546                                                         generic,
547                                                         specific);
548         }
549 }
550
551 static void
552 tp_contact_factory_capabilities_changed_cb (TpConnection    *connection,
553                                             const GPtrArray *capabilities,
554                                             gpointer         user_data,
555                                             GObject         *weak_object)
556 {
557         EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
558         guint                    i;
559
560         for (i = 0; i < capabilities->len; i++) {
561                 GValueArray *values;
562                 guint        handle;
563                 const gchar *channel_type;
564                 guint        generic;
565                 guint        specific;
566
567                 values = g_ptr_array_index (capabilities, i);
568                 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
569                 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
570                 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
571                 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
572
573                 tp_contact_factory_update_capabilities (tp_factory,
574                                                         handle,
575                                                         channel_type,
576                                                         generic,
577                                                         specific);
578         }
579 }
580
581 static void
582 tp_contact_factory_request_everything (EmpathyTpContactFactory *tp_factory,
583                                        const GArray            *handles)
584 {
585         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
586         guint                       *dup_handles;
587
588         if (!priv->ready) {
589                 return;
590         }
591
592         dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
593         g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
594         tp_cli_connection_interface_presence_call_get_presence (priv->connection,
595                                                                 -1,
596                                                                 handles,
597                                                                 tp_contact_factory_get_presence_cb,
598                                                                 dup_handles, g_free,
599                                                                 G_OBJECT (tp_factory));
600
601         dup_handles = g_memdup (handles->data, handles->len * sizeof (guint));
602         tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
603                                                                    -1,
604                                                                    handles,
605                                                                    tp_contact_factory_request_aliases_cb,
606                                                                    dup_handles, g_free,
607                                                                    G_OBJECT (tp_factory));
608
609         tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
610                                                                           -1,
611                                                                           handles,
612                                                                           tp_contact_factory_get_known_avatar_tokens_cb,
613                                                                           NULL, NULL,
614                                                                           G_OBJECT (tp_factory));
615
616         tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
617                                                                         -1,
618                                                                         handles,
619                                                                         tp_contact_factory_get_capabilities_cb,
620                                                                         NULL, NULL,
621                                                                         G_OBJECT (tp_factory));
622 }
623
624 static void
625 tp_contact_factory_list_free (gpointer data)
626 {
627         GList *l = data;
628
629         g_list_foreach (l, (GFunc) g_object_unref, NULL);
630         g_list_free (l);
631 }
632
633 static void
634 tp_contact_factory_request_handles_cb (TpConnection *connection,
635                                        const GArray *handles,
636                                        const GError *error,
637                                        gpointer      user_data,
638                                        GObject      *tp_factory)
639 {
640         GList *contacts = user_data;
641         GList *l;
642         guint  i = 0;
643
644         if (error) {
645                 empathy_debug (DEBUG_DOMAIN, "Failed to request handles: %s",
646                                error->message);
647                 return;
648         }
649
650         for (l = contacts; l; l = l->next) {
651                 guint handle;
652
653                 handle = g_array_index (handles, guint, i);
654                 empathy_contact_set_handle (l->data, handle);
655
656                 i++;
657         }
658
659         tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
660                                                handles);
661 }
662
663 static void
664 tp_contact_factory_inspect_handles_cb (TpConnection  *connection,
665                                        const gchar  **ids,
666                                        const GError  *error,
667                                        gpointer       user_data,
668                                        GObject       *tp_factory)
669 {
670         const gchar **id;
671         GList        *contacts = user_data;
672         GList        *l;
673
674         if (error) {
675                 empathy_debug (DEBUG_DOMAIN, "Failed to inspect handles: %s",
676                                error->message);
677                 return;
678         }
679
680         id = ids;
681         for (l = contacts; l; l = l->next) {
682                 empathy_contact_set_id (l->data, *id);
683                 id++;
684         }
685 }
686
687 static void
688 tp_contact_factory_disconnect_contact_foreach (gpointer data,
689                                                gpointer user_data)
690 {
691         EmpathyContact *contact = data;
692         
693         empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
694         empathy_contact_set_handle (contact, 0);
695 }
696
697 static void
698 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
699 {
700         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
701
702         empathy_debug (DEBUG_DOMAIN, "Connection invalidated");
703
704         g_object_unref (priv->connection);
705         priv->connection = NULL;
706         priv->ready = FALSE;
707
708         g_list_foreach (priv->contacts,
709                         tp_contact_factory_disconnect_contact_foreach,
710                         tp_factory);
711 }
712
713
714 static void
715 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
716                                        guint         handle,
717                                        const GError *error,
718                                        gpointer      user_data,
719                                        GObject      *tp_factory)
720 {
721         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
722         GList                       *l;
723         GArray                      *handle_needed;
724         GArray                      *id_needed;
725         GList                       *handle_needed_contacts = NULL;
726         GList                       *id_needed_contacts = NULL;
727
728         if (error) {
729                 empathy_debug (DEBUG_DOMAIN, "Failed to get self handles: %s",
730                                error->message);
731                 return;
732         }
733
734         empathy_debug (DEBUG_DOMAIN, "Connection ready");
735
736         empathy_contact_set_handle (priv->user, handle);
737         priv->ready = TRUE;
738
739         /* Connect signals */
740         tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
741                                                                          tp_contact_factory_aliases_changed_cb,
742                                                                          NULL, NULL,
743                                                                          G_OBJECT (tp_factory),
744                                                                          NULL);
745         tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
746                                                                        tp_contact_factory_avatar_updated_cb,
747                                                                        NULL, NULL,
748                                                                        G_OBJECT (tp_factory),
749                                                                        NULL);
750         tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
751                                                                          tp_contact_factory_avatar_retrieved_cb,
752                                                                          NULL, NULL,
753                                                                          G_OBJECT (tp_factory),
754                                                                          NULL);
755         tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
756                                                                          tp_contact_factory_presence_update_cb,
757                                                                          NULL, NULL,
758                                                                          G_OBJECT (tp_factory),
759                                                                          NULL);
760         tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
761                                                                                   tp_contact_factory_capabilities_changed_cb,
762                                                                                   NULL, NULL,
763                                                                                   G_OBJECT (tp_factory),
764                                                                                   NULL);
765
766         /* Request needed info for all existing contacts */
767         handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
768         id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
769         for (l = priv->contacts; l; l = l->next) {
770                 EmpathyContact *contact;
771                 guint           handle;
772                 const gchar    *id;
773
774                 contact = l->data;
775                 handle = empathy_contact_get_handle (contact);
776                 id = empathy_contact_get_id (contact);
777                 if (handle == 0) {
778                         g_assert (!G_STR_EMPTY (id));
779                         g_array_append_val (handle_needed, id);
780                         handle_needed_contacts = g_list_prepend (handle_needed_contacts,
781                                                                  g_object_ref (contact));
782                 }
783                 if (G_STR_EMPTY (id)) {
784                         g_array_append_val (id_needed, handle);
785                         id_needed_contacts = g_list_prepend (id_needed_contacts,
786                                                              g_object_ref (contact));
787                 }
788         }
789         handle_needed_contacts = g_list_reverse (handle_needed_contacts);
790         id_needed_contacts = g_list_reverse (id_needed_contacts);
791
792         tp_cli_connection_call_request_handles (priv->connection,
793                                                 -1,
794                                                 TP_HANDLE_TYPE_CONTACT,
795                                                 (const gchar**) handle_needed->data,
796                                                 tp_contact_factory_request_handles_cb,
797                                                 handle_needed_contacts, tp_contact_factory_list_free,
798                                                 G_OBJECT (tp_factory));
799
800         tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
801                                                id_needed);
802         tp_cli_connection_call_inspect_handles (priv->connection,
803                                                 -1,
804                                                 TP_HANDLE_TYPE_CONTACT,
805                                                 id_needed,
806                                                 tp_contact_factory_inspect_handles_cb,
807                                                 id_needed_contacts, tp_contact_factory_list_free,
808                                                 G_OBJECT (tp_factory));
809
810         g_array_free (handle_needed, TRUE);
811         g_array_free (id_needed, TRUE);
812 }
813
814 static void
815 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
816 {
817         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
818
819         /* Get our own handle */
820         tp_cli_connection_call_get_self_handle (priv->connection,
821                                                 -1,
822                                                 tp_contact_factory_got_self_handle_cb,
823                                                 NULL, NULL,
824                                                 G_OBJECT (tp_factory));
825 }
826
827 static void
828 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
829 {
830         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
831         TpConn                      *tp_conn;
832
833         if (priv->connection) {
834                 /* We already have our connection object */
835                 return;
836         }
837
838         tp_conn = mission_control_get_connection (priv->mc, priv->account, NULL);
839         if (!tp_conn) {
840                 return;
841         }
842
843         /* We got a new connection, wait for it to be ready */
844         priv->connection = tp_conn_dup_connection (tp_conn);
845         g_object_unref (tp_conn);
846
847         g_signal_connect_swapped (priv->connection, "invalidated",
848                                   G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
849                                   tp_factory);
850         g_signal_connect_swapped (priv->connection, "notify::connection-ready",
851                                   G_CALLBACK (tp_contact_factory_connection_ready_cb),
852                                   tp_factory);
853 }
854
855 static void
856 tp_contact_factory_status_changed_cb (MissionControl           *mc,
857                                       TpConnectionStatus        status,
858                                       McPresence                presence,
859                                       TpConnectionStatusReason  reason,
860                                       const gchar              *unique_name,
861                                       EmpathyTpContactFactory  *tp_factory)
862 {
863         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
864         McAccount                   *account;
865
866         account = mc_account_lookup (unique_name);
867         if (account && empathy_account_equal (account, priv->account)) {
868                 tp_contact_factory_status_updated (tp_factory);
869         }
870         g_object_unref (account);
871 }
872
873 static void
874 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
875                                 EmpathyContact          *contact)
876 {
877         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
878
879         g_object_weak_ref (G_OBJECT (contact),
880                            tp_contact_factory_weak_notify,
881                            tp_factory);
882         priv->contacts = g_list_prepend (priv->contacts, contact);
883
884         empathy_debug (DEBUG_DOMAIN, "Contact added: %s (%d)",
885                        empathy_contact_get_id (contact),
886                        empathy_contact_get_handle (contact));
887 }
888
889 static void
890 tp_contact_factory_hold_handles_cb (TpConnection *connection,
891                                     const GError *error,
892                                     gpointer      userdata,
893                                     GObject      *tp_factory)
894 {
895         if (error) {
896                 empathy_debug (DEBUG_DOMAIN, "Failed to hold handles: %s",
897                                error->message);
898         }
899 }
900
901 EmpathyContact *
902 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
903 {
904         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
905
906         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
907
908         return g_object_ref (priv->user);
909 }
910
911 EmpathyContact *
912 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
913                                         const gchar             *id)
914 {
915         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
916         EmpathyContact              *contact;
917
918         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
919         g_return_val_if_fail (id != NULL, NULL);
920
921         /* Check if the contact already exists */
922         contact = tp_contact_factory_find_by_id (tp_factory, id);
923         if (contact) {
924                 return g_object_ref (contact);
925         }
926
927         /* Create new contact */
928         contact = g_object_new (EMPATHY_TYPE_CONTACT,
929                                 "account", priv->account,
930                                 "id", id,
931                                 NULL);
932         tp_contact_factory_add_contact (tp_factory, contact);
933
934         if (priv->ready) {
935                 const gchar *contact_ids[] = {id, NULL};
936                 GList       *contacts;
937                 
938                 contacts = g_list_prepend (NULL, g_object_ref (contact));
939                 tp_cli_connection_call_request_handles (priv->connection,
940                                                         -1,
941                                                         TP_HANDLE_TYPE_CONTACT,
942                                                         contact_ids,
943                                                         tp_contact_factory_request_handles_cb,
944                                                         contacts, tp_contact_factory_list_free,
945                                                         G_OBJECT (tp_factory));
946         }
947
948         return contact;
949 }
950
951 EmpathyContact *
952 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
953                                             guint                    handle)
954 {
955         EmpathyContact *contact;
956         GArray         *handles;
957         GList          *contacts;
958
959         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
960
961         handles = g_array_new (FALSE, FALSE, sizeof (guint));
962         g_array_append_val (handles, handle);
963
964         contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
965         g_array_free (handles, TRUE);
966
967         contact = contacts ? contacts->data : NULL;
968         g_list_free (contacts);
969
970         return contact;
971 }
972
973 GList *
974 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
975                                              const GArray            *handles)
976 {
977         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
978         GList                       *contacts = NULL;
979         GArray                      *new_handles;
980         GList                       *new_contacts = NULL;
981         guint                        i;
982
983         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
984         g_return_val_if_fail (handles != NULL, NULL);
985
986         /* Search all contacts we already have */
987         new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
988         for (i = 0; i < handles->len; i++) {
989                 EmpathyContact *contact;
990                 guint           handle;
991
992                 handle = g_array_index (handles, guint, i);
993                 if (handle == 0) {
994                         continue;
995                 }
996
997                 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
998                 if (contact) {
999                         contacts = g_list_prepend (contacts, g_object_ref (contact));
1000                 } else {
1001                         g_array_append_val (new_handles, handle);
1002                 }
1003         }
1004
1005         if (new_handles->len == 0) {
1006                 g_array_free (new_handles, TRUE);
1007                 return contacts;
1008         }
1009
1010         /* Create new contacts */
1011         for (i = 0; i < new_handles->len; i++) {
1012                 EmpathyContact *contact;
1013                 guint           handle;
1014
1015                 handle = g_array_index (new_handles, guint, i);
1016
1017                 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1018                                         "account", priv->account,
1019                                         "handle", handle,
1020                                         NULL);
1021                 tp_contact_factory_add_contact (tp_factory, contact);
1022                 contacts = g_list_prepend (contacts, contact);
1023                 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1024         }
1025         new_contacts = g_list_reverse (new_contacts);
1026
1027         if (priv->ready) {
1028                 /* Get the IDs of all new handles */
1029                 tp_cli_connection_call_inspect_handles (priv->connection,
1030                                                         -1,
1031                                                         TP_HANDLE_TYPE_CONTACT,
1032                                                         new_handles,
1033                                                         tp_contact_factory_inspect_handles_cb,
1034                                                         new_contacts, tp_contact_factory_list_free,
1035                                                         G_OBJECT (tp_factory));
1036
1037                 /* Hold all new handles. */
1038                 /* FIXME: Should be unholded when removed from the factory */
1039                 tp_cli_connection_call_hold_handles (priv->connection,
1040                                                      -1,
1041                                                      TP_HANDLE_TYPE_CONTACT,
1042                                                      new_handles,
1043                                                      tp_contact_factory_hold_handles_cb,
1044                                                      NULL, NULL,
1045                                                      G_OBJECT (tp_factory));
1046
1047                 tp_contact_factory_request_everything (tp_factory, new_handles);
1048         }
1049
1050         return contacts;
1051 }
1052
1053 void
1054 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1055                                       EmpathyContact          *contact,
1056                                       const gchar             *alias)
1057 {
1058         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1059         GHashTable                  *new_alias;
1060         guint                        handle;
1061
1062         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1063         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1064         g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1065                                                  priv->account));
1066
1067         if (!priv->ready) {
1068                 return;
1069         }
1070
1071         handle = empathy_contact_get_handle (contact);
1072
1073         empathy_debug (DEBUG_DOMAIN, "Setting alias for contact %s (%d) to %s",
1074                        empathy_contact_get_id (contact),
1075                        handle, alias);
1076
1077         new_alias = g_hash_table_new_full (g_direct_hash,
1078                                            g_direct_equal,
1079                                            NULL,
1080                                            g_free);
1081
1082         g_hash_table_insert (new_alias,
1083                              GUINT_TO_POINTER (handle),
1084                              g_strdup (alias));
1085
1086         tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1087                                                                -1,
1088                                                                new_alias,
1089                                                                tp_contact_factory_set_aliases_cb,
1090                                                                NULL, NULL,
1091                                                                G_OBJECT (tp_factory));
1092
1093         g_hash_table_destroy (new_alias);
1094 }
1095
1096 void
1097 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1098                                        const gchar             *data,
1099                                        gsize                    size,
1100                                        const gchar             *mime_type)
1101 {
1102         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1103
1104         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1105
1106         if (!priv->ready) {
1107                 return;
1108         }
1109
1110         if (data && size > 0 && size < G_MAXUINT) {
1111                 GArray avatar;
1112
1113                 avatar.data = (gchar*) data;
1114                 avatar.len = size;
1115
1116                 empathy_debug (DEBUG_DOMAIN, "Setting avatar on account %s",
1117                                mc_account_get_unique_name (priv->account));
1118
1119                 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1120                                                                      -1,
1121                                                                      &avatar,
1122                                                                      mime_type,
1123                                                                      tp_contact_factory_set_avatar_cb,
1124                                                                      NULL, NULL,
1125                                                                      G_OBJECT (tp_factory));
1126         } else {
1127                 empathy_debug (DEBUG_DOMAIN, "Clearing avatar on account %s",
1128                                mc_account_get_unique_name (priv->account));
1129
1130                 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1131                                                                        -1,
1132                                                                        tp_contact_factory_clear_avatar_cb,
1133                                                                        NULL, NULL,
1134                                                                        G_OBJECT (tp_factory));
1135         }
1136 }
1137
1138 static void
1139 tp_contact_factory_get_property (GObject    *object,
1140                                  guint       param_id,
1141                                  GValue     *value,
1142                                  GParamSpec *pspec)
1143 {
1144         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1145
1146         switch (param_id) {
1147         case PROP_ACCOUNT:
1148                 g_value_set_object (value, priv->account);
1149                 break;
1150         default:
1151                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1152                 break;
1153         };
1154 }
1155
1156 static void
1157 tp_contact_factory_set_property (GObject      *object,
1158                                  guint         param_id,
1159                                  const GValue *value,
1160                                  GParamSpec   *pspec)
1161 {
1162         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1163
1164         switch (param_id) {
1165         case PROP_ACCOUNT:
1166                 priv->account = g_object_ref (g_value_get_object (value));
1167                 break;
1168         default:
1169                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1170                 break;
1171         };
1172 }
1173
1174 static void
1175 tp_contact_factory_finalize (GObject *object)
1176 {
1177         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1178         GList                       *l;
1179
1180         empathy_debug (DEBUG_DOMAIN, "Finalized: %p (%s)",
1181                        object,
1182                        mc_account_get_normalized_name (priv->account));
1183
1184         dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
1185                                         "AccountStatusChanged",
1186                                         G_CALLBACK (tp_contact_factory_status_changed_cb),
1187                                         object);
1188
1189         for (l = priv->contacts; l; l = l->next) {
1190                 g_object_weak_unref (G_OBJECT (l->data),
1191                                      tp_contact_factory_weak_notify,
1192                                      object);
1193         }
1194
1195         g_list_free (priv->contacts);
1196         g_object_unref (priv->mc);
1197         g_object_unref (priv->account);
1198
1199         if (priv->connection) {
1200                 g_object_unref (priv->connection);
1201         }
1202
1203         G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1204 }
1205
1206 static GObject *
1207 tp_contact_factory_constructor (GType                  type,
1208                                 guint                  n_props,
1209                                 GObjectConstructParam *props)
1210 {
1211         GObject *tp_factory;
1212         EmpathyTpContactFactoryPriv *priv;
1213
1214         tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1215         priv = GET_PRIV (tp_factory);
1216
1217         priv->ready = FALSE;
1218         priv->user = empathy_contact_new (priv->account);
1219         empathy_contact_set_is_user (priv->user, TRUE);
1220         tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1221         tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1222
1223         return tp_factory;
1224 }
1225
1226 static void
1227 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1228 {
1229         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1230
1231         object_class->finalize = tp_contact_factory_finalize;
1232         object_class->constructor = tp_contact_factory_constructor;
1233         object_class->get_property = tp_contact_factory_get_property;
1234         object_class->set_property = tp_contact_factory_set_property;
1235
1236         /* Construct-only properties */
1237         g_object_class_install_property (object_class,
1238                                          PROP_ACCOUNT,
1239                                          g_param_spec_object ("account",
1240                                                               "Factory's Account",
1241                                                               "The account associated with the factory",
1242                                                               MC_TYPE_ACCOUNT,
1243                                                               G_PARAM_READWRITE |
1244                                                               G_PARAM_CONSTRUCT_ONLY));
1245
1246         g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1247 }
1248
1249 static void
1250 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1251 {
1252         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1253
1254         priv->mc = empathy_mission_control_new ();
1255         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
1256                                      "AccountStatusChanged",
1257                                      G_CALLBACK (tp_contact_factory_status_changed_cb),
1258                                      tp_factory, NULL);
1259 }
1260
1261 EmpathyTpContactFactory *
1262 empathy_tp_contact_factory_new (McAccount *account)
1263 {
1264         return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,
1265                              "account", account,
1266                              NULL);
1267 }
1268