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