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