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