]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-factory.c
Make InspectHandles call async
[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_inspect_handles_cb (TpConnection  *connection,
826                                        const gchar  **ids,
827                                        const GError  *error,
828                                        gpointer       user_data,
829                                        GObject       *tp_factory)
830 {
831         guint *handles = user_data;
832         guint  i;
833         const gchar **id;
834
835         if (error) {
836                 empathy_debug (DEBUG_DOMAIN, "Failed to inspect handles: %s",
837                                error->message);
838         }
839
840         i = 0;
841         for (id = ids; *id; id++) {
842                 EmpathyContact *contact;
843
844                 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
845                                                              handles[i]);
846                 empathy_contact_set_id (contact, *id);
847
848                 i++;
849         }
850 }
851
852 static void
853 tp_contact_factory_hold_handles_cb (TpConnection *connection,
854                                     const GError *error,
855                                     gpointer      userdata,
856                                     GObject      *tp_factory)
857 {
858         if (error) {
859                 empathy_debug (DEBUG_DOMAIN, "Failed to hold handles: %s",
860                                error->message);
861         }
862 }
863
864 EmpathyContact *
865 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
866 {
867         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
868
869         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
870
871         return empathy_tp_contact_factory_get_from_handle (tp_factory,
872                                                            priv->self_handle);
873 }
874
875 EmpathyContact *
876 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
877                                         const gchar             *id)
878 {
879         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
880         EmpathyContact              *contact;
881
882         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
883         g_return_val_if_fail (id != NULL, NULL);
884
885         /* Check if the contact already exists */
886         contact = tp_contact_factory_find_by_id (tp_factory, id);
887         if (contact) {
888                 return g_object_ref (contact);
889         }
890
891         /* Create new contact */
892         contact = g_object_new (EMPATHY_TYPE_CONTACT,
893                                 "account", priv->account,
894                                 "id", id,
895                                 NULL);
896         tp_contact_factory_add_contact (tp_factory, contact);
897
898         /* If the account is connected, request contact's handle */
899         if (priv->connection) {
900                 const gchar *contact_ids[] = {id, NULL};
901                 GList       *contacts;
902                 
903                 contacts = g_list_prepend (NULL, g_object_ref (contact));
904                 tp_cli_connection_call_request_handles (priv->connection,
905                                                         -1,
906                                                         TP_HANDLE_TYPE_CONTACT,
907                                                         contact_ids,
908                                                         tp_contact_factory_request_handles_cb,
909                                                         contacts, tp_contact_factory_list_free,
910                                                         G_OBJECT (tp_factory));
911         }
912
913         return contact;
914 }
915
916 EmpathyContact *
917 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
918                                             guint                    handle)
919 {
920         EmpathyContact *contact;
921         GArray         *handles;
922         GList          *contacts;
923
924         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
925
926         handles = g_array_new (FALSE, FALSE, sizeof (guint));
927         g_array_append_val (handles, handle);
928
929         contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
930         g_array_free (handles, TRUE);
931
932         contact = contacts ? contacts->data : NULL;
933         g_list_free (contacts);
934
935         return contact;
936 }
937
938 GList *
939 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
940                                              const GArray            *handles)
941 {
942         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
943         GList                       *contacts = NULL;
944         GArray                      *new_handles;
945         guint                       *dup_handles;
946         guint                        i;
947
948         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
949         g_return_val_if_fail (handles != NULL, NULL);
950
951         /* Search all contacts we already have */
952         new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
953         for (i = 0; i < handles->len; i++) {
954                 EmpathyContact *contact;
955                 guint           handle;
956
957                 handle = g_array_index (handles, guint, i);
958                 if (handle == 0) {
959                         continue;
960                 }
961
962                 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
963                 if (contact) {
964                         contacts = g_list_prepend (contacts, g_object_ref (contact));
965                 } else {
966                         g_array_append_val (new_handles, handle);
967                 }
968         }
969
970         if (new_handles->len == 0) {
971                 g_array_free (new_handles, TRUE);
972                 return contacts;
973         }
974
975         /* Create new contacts */
976         for (i = 0; i < new_handles->len; i++) {
977                 EmpathyContact *contact;
978                 guint           handle;
979                 gboolean        is_user;
980
981                 handle = g_array_index (new_handles, guint, i);
982
983                 is_user = (handle == priv->self_handle);
984                 contact = g_object_new (EMPATHY_TYPE_CONTACT,
985                                         "account", priv->account,
986                                         "handle", handle,
987                                         "is-user", is_user,
988                                         NULL);
989                 tp_contact_factory_add_contact (tp_factory, contact);
990                 contacts = g_list_prepend (contacts, contact);
991         }
992
993         tp_contact_factory_request_everything (tp_factory, new_handles);
994
995         /* Get the IDs of all new handles */
996         dup_handles = g_memdup (new_handles->data, new_handles->len * sizeof (guint));
997         tp_cli_connection_call_inspect_handles (priv->connection,
998                                                 -1,
999                                                 TP_HANDLE_TYPE_CONTACT,
1000                                                 new_handles,
1001                                                 tp_contact_factory_inspect_handles_cb,
1002                                                 dup_handles, g_free,
1003                                                 G_OBJECT (tp_factory));
1004
1005         /* Hold all new handles. */
1006         /* FIXME: Should be unholded when removed from the factory */
1007         tp_cli_connection_call_hold_handles (priv->connection,
1008                                              -1,
1009                                              TP_HANDLE_TYPE_CONTACT,
1010                                              new_handles,
1011                                              tp_contact_factory_hold_handles_cb,
1012                                              NULL, NULL,
1013                                              G_OBJECT (tp_factory));
1014
1015         return contacts;
1016 }
1017
1018 void
1019 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1020                                       EmpathyContact          *contact,
1021                                       const gchar             *alias)
1022 {
1023         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1024         GHashTable                  *new_alias;
1025         guint                        handle;
1026
1027         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1028         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1029         g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1030                                                  priv->account));
1031
1032         handle = empathy_contact_get_handle (contact);
1033
1034         empathy_debug (DEBUG_DOMAIN, "Setting alias for contact %s (%d) to %s",
1035                        empathy_contact_get_id (contact),
1036                        handle, alias);
1037
1038         new_alias = g_hash_table_new_full (g_direct_hash,
1039                                            g_direct_equal,
1040                                            NULL,
1041                                            g_free);
1042
1043         g_hash_table_insert (new_alias,
1044                              GUINT_TO_POINTER (handle),
1045                              g_strdup (alias));
1046
1047         tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1048                                                                -1,
1049                                                                new_alias,
1050                                                                tp_contact_factory_set_aliases_cb,
1051                                                                NULL, NULL,
1052                                                                G_OBJECT (tp_factory));
1053
1054         g_hash_table_destroy (new_alias);
1055 }
1056
1057 void
1058 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1059                                        const gchar             *data,
1060                                        gsize                    size,
1061                                        const gchar             *mime_type)
1062 {
1063         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1064
1065         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1066
1067         if (data && size > 0 && size < G_MAXUINT) {
1068                 GArray avatar;
1069
1070                 avatar.data = (gchar*) data;
1071                 avatar.len = size;
1072
1073                 empathy_debug (DEBUG_DOMAIN, "Setting avatar on account %s",
1074                                mc_account_get_unique_name (priv->account));
1075
1076                 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1077                                                                      -1,
1078                                                                      &avatar,
1079                                                                      mime_type,
1080                                                                      tp_contact_factory_set_avatar_cb,
1081                                                                      NULL, NULL,
1082                                                                      G_OBJECT (tp_factory));
1083         } else {
1084                 empathy_debug (DEBUG_DOMAIN, "Clearing avatar on account %s",
1085                                mc_account_get_unique_name (priv->account));
1086
1087                 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1088                                                                        -1,
1089                                                                        tp_contact_factory_clear_avatar_cb,
1090                                                                        NULL, NULL,
1091                                                                        G_OBJECT (tp_factory));
1092         }
1093 }
1094
1095 static void
1096 tp_contact_factory_get_property (GObject    *object,
1097                                  guint       param_id,
1098                                  GValue     *value,
1099                                  GParamSpec *pspec)
1100 {
1101         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1102
1103         switch (param_id) {
1104         case PROP_ACCOUNT:
1105                 g_value_set_object (value, priv->account);
1106                 break;
1107         default:
1108                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1109                 break;
1110         };
1111 }
1112
1113 static void
1114 tp_contact_factory_set_property (GObject      *object,
1115                                  guint         param_id,
1116                                  const GValue *value,
1117                                  GParamSpec   *pspec)
1118 {
1119         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1120
1121         switch (param_id) {
1122         case PROP_ACCOUNT:
1123                 priv->account = g_object_ref (g_value_get_object (value));
1124                 break;
1125         default:
1126                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1127                 break;
1128         };
1129 }
1130
1131 static void
1132 tp_contact_factory_finalize (GObject *object)
1133 {
1134         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1135         GList                       *l;
1136
1137         empathy_debug (DEBUG_DOMAIN, "Finalized: %p (%s)",
1138                        object,
1139                        mc_account_get_normalized_name (priv->account));
1140
1141         dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
1142                                         "AccountStatusChanged",
1143                                         G_CALLBACK (tp_contact_factory_status_changed_cb),
1144                                         object);
1145
1146         for (l = priv->contacts; l; l = l->next) {
1147                 g_object_weak_unref (G_OBJECT (l->data),
1148                                      tp_contact_factory_weak_notify,
1149                                      object);
1150         }
1151
1152         g_list_free (priv->contacts);
1153         g_object_unref (priv->mc);
1154         g_object_unref (priv->account);
1155
1156         if (priv->connection) {
1157                 g_object_unref (priv->connection);
1158         }
1159
1160         G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1161 }
1162
1163 static GObject *
1164 tp_contact_factory_constructor (GType                  type,
1165                                 guint                  n_props,
1166                                 GObjectConstructParam *props)
1167 {
1168         GObject *tp_factory;
1169         EmpathyTpContactFactoryPriv *priv;
1170
1171         tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1172         priv = GET_PRIV (tp_factory);
1173
1174         priv->ready = FALSE;
1175         tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1176
1177         return tp_factory;
1178 }
1179
1180
1181 static void
1182 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1183 {
1184         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1185
1186         object_class->finalize = tp_contact_factory_finalize;
1187         object_class->constructor = tp_contact_factory_constructor;
1188         object_class->get_property = tp_contact_factory_get_property;
1189         object_class->set_property = tp_contact_factory_set_property;
1190
1191         /* Construct-only properties */
1192         g_object_class_install_property (object_class,
1193                                          PROP_ACCOUNT,
1194                                          g_param_spec_object ("account",
1195                                                               "Factory's Account",
1196                                                               "The account associated with the factory",
1197                                                               MC_TYPE_ACCOUNT,
1198                                                               G_PARAM_READWRITE |
1199                                                               G_PARAM_CONSTRUCT_ONLY));
1200
1201         g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1202 }
1203
1204 static void
1205 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1206 {
1207         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1208
1209         priv->mc = empathy_mission_control_new ();
1210         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
1211                                      "AccountStatusChanged",
1212                                      G_CALLBACK (tp_contact_factory_status_changed_cb),
1213                                      tp_factory, NULL);
1214 }
1215
1216 EmpathyTpContactFactory *
1217 empathy_tp_contact_factory_new (McAccount *account)
1218 {
1219         return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,
1220                              "account", account,
1221                              NULL);
1222 }
1223