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