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