]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-factory.c
EmpathyTpContactFactory: split on-ready code out of got_self_handle_cb
[empathy.git] / libempathy / empathy-tp-contact-factory.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  * 
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <telepathy-glib/util.h>
27 #include <telepathy-glib/connection.h>
28 #include <libmissioncontrol/mission-control.h>
29
30 #include "empathy-tp-contact-factory.h"
31 #include "empathy-utils.h"
32
33 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
34 #include "empathy-debug.h"
35
36 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactFactory)
37 typedef struct {
38         MissionControl *mc;
39         McAccount      *account;
40         TpConnection   *connection;
41         gboolean        ready;
42
43         GList          *contacts;
44         EmpathyContact *user;
45         gpointer        token;
46
47         const gchar   **avatar_mime_types;
48         guint           avatar_min_width;
49         guint           avatar_min_height;
50         guint           avatar_max_width;
51         guint           avatar_max_height;
52         guint           avatar_max_size;
53 } EmpathyTpContactFactoryPriv;
54
55 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
56
57 enum {
58         PROP_0,
59         PROP_ACCOUNT,
60         PROP_READY
61 };
62
63 /* Prototypes */
64 static void tp_contact_factory_maybe_ready (EmpathyTpContactFactory *tp_factory);
65
66
67 static EmpathyContact *
68 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
69                                    guint                    handle)
70 {
71         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
72         GList                       *l;
73
74         for (l = priv->contacts; l; l = l->next) {
75                 if (empathy_contact_get_handle (l->data) == handle) {
76                         return l->data;
77                 }
78         }
79
80         return NULL;
81 }
82
83 static EmpathyContact *
84 tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
85                                const gchar             *id)
86 {
87         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
88         GList                       *l;
89
90         for (l = priv->contacts; l; l = l->next) {
91                 if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
92                         return l->data;
93                 }
94         }
95
96         return NULL;
97 }
98
99 static void
100 tp_contact_factory_weak_notify (gpointer data,
101                                 GObject *where_the_object_was)
102 {
103         EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
104
105         DEBUG ("Remove finalized contact %p", where_the_object_was);
106
107         priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
108 }
109
110 static void
111 tp_contact_factory_presences_table_foreach (const gchar    *state_str,
112                                             GHashTable     *presences_table,
113                                             EmpathyContact *contact)
114 {
115         const GValue *message;
116         const gchar  *message_str = NULL;
117
118         empathy_contact_set_presence (contact,
119                                       empathy_presence_from_str (state_str));
120         
121         message = g_hash_table_lookup (presences_table, "message");
122         if (message) {
123                 message_str = g_value_get_string (message);
124         }
125
126         if (!G_STR_EMPTY (message_str)) {
127                 empathy_contact_set_presence_message (contact, message_str);
128         } else {
129                 empathy_contact_set_presence_message (contact, NULL);
130         }
131 }
132
133 static void
134 tp_contact_factory_parse_presence_foreach (guint                    handle,
135                                            GValueArray             *presence_struct,
136                                            EmpathyTpContactFactory *tp_factory)
137 {
138         GHashTable      *presences_table;
139         EmpathyContact  *contact;
140
141         contact = tp_contact_factory_find_by_handle (tp_factory, handle);
142         if (!contact) {
143                 return;
144         }
145
146         presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
147
148         g_hash_table_foreach (presences_table,
149                               (GHFunc) tp_contact_factory_presences_table_foreach,
150                               contact);
151
152         DEBUG ("Changing presence for contact %s (%d) to '%s' (%d)",
153                 empathy_contact_get_id (contact),
154                 handle,
155                 empathy_contact_get_presence_message (contact),
156                 empathy_contact_get_presence (contact));
157 }
158
159 static void
160 tp_contact_factory_get_presence_cb (TpConnection *connection,
161                                     GHashTable   *handle_table,
162                                     const GError *error,
163                                     gpointer      user_data,
164                                     GObject      *tp_factory)
165 {
166         if (error) {
167                 DEBUG ("Error getting presence: %s", error->message);
168                 if (error->domain == TP_DBUS_ERRORS &&
169                     error->code == TP_DBUS_ERROR_NO_INTERFACE) {
170                         guint *handles = user_data;
171
172                         /* We have no presence iface, set default presence
173                          * to available */
174                         while (*handles != 0) {
175                                 EmpathyContact *contact;
176
177                                 contact = tp_contact_factory_find_by_handle (
178                                         (EmpathyTpContactFactory*) tp_factory,
179                                         *handles);
180                                 if (contact) {
181                                         empathy_contact_set_presence (contact,
182                                                                       MC_PRESENCE_AVAILABLE);
183                                 }
184
185                                 handles++;
186                         }
187                 }
188
189                 return;
190         }
191
192         g_hash_table_foreach (handle_table,
193                               (GHFunc) tp_contact_factory_parse_presence_foreach,
194                               EMPATHY_TP_CONTACT_FACTORY (tp_factory));
195 }
196
197 static void
198 tp_contact_factory_presence_update_cb (TpConnection *connection,
199                                        GHashTable   *handle_table,
200                                        gpointer      user_data,
201                                        GObject      *tp_factory)
202 {
203         g_hash_table_foreach (handle_table,
204                               (GHFunc) tp_contact_factory_parse_presence_foreach,
205                               EMPATHY_TP_CONTACT_FACTORY (tp_factory));
206 }
207
208 static void
209 tp_contact_factory_set_aliases_cb (TpConnection *connection,
210                                    const GError *error,
211                                    gpointer      user_data,
212                                    GObject      *tp_factory)
213 {
214         if (error) {
215                 DEBUG ("Error setting alias: %s", error->message);
216         }
217 }
218
219 static void
220 tp_contact_factory_request_aliases_cb (TpConnection *connection,
221                                        const gchar  **contact_names,
222                                        const GError  *error,
223                                        gpointer       user_data,
224                                        GObject       *tp_factory)
225 {
226         guint        *handles = user_data;
227         guint         i = 0;
228         const gchar **name;
229
230         if (error) {
231                 DEBUG ("Error requesting aliases: %s", error->message);
232
233                 /* If we failed to get alias set it to NULL, like that if
234                  * someone is waiting for the name to be ready it won't wait
235                  * infinitely */
236                 while (*handles != 0) {
237                         EmpathyContact *contact;
238
239                         contact = tp_contact_factory_find_by_handle (
240                                 (EmpathyTpContactFactory*) tp_factory,
241                                 *handles);
242                         if (contact) {
243                                 empathy_contact_set_name (contact, NULL);
244                         }
245
246                         handles++;
247                 }
248                 return;
249         }
250
251         for (name = contact_names; *name; name++) {
252                 EmpathyContact *contact;
253
254                 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
255                                                              handles[i]);
256                 if (!contact) {
257                         continue;
258                 }
259
260                 DEBUG ("Renaming contact %s (%d) to %s (request cb)",
261                         empathy_contact_get_id (contact),
262                         empathy_contact_get_handle (contact),
263                         *name);
264
265                 empathy_contact_set_name (contact, *name);
266
267                 i++;
268         }
269 }
270
271 static void
272 tp_contact_factory_aliases_changed_cb (TpConnection    *connection,
273                                        const GPtrArray *renamed_handlers,
274                                        gpointer         user_data,
275                                        GObject         *weak_object)
276 {
277         EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
278         guint                    i;
279
280         for (i = 0; renamed_handlers->len > i; i++) {
281                 guint           handle;
282                 const gchar    *alias;
283                 GValueArray    *renamed_struct;
284                 EmpathyContact *contact;
285
286                 renamed_struct = g_ptr_array_index (renamed_handlers, i);
287                 handle = g_value_get_uint (g_value_array_get_nth (renamed_struct, 0));
288                 alias = g_value_get_string (g_value_array_get_nth (renamed_struct, 1));
289                 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
290
291                 if (!contact) {
292                         /* We don't know this contact, skip */
293                         continue;
294                 }
295
296                 DEBUG ("Renaming contact %s (%d) to %s (changed cb)",
297                         empathy_contact_get_id (contact),
298                         handle, alias);
299
300                 empathy_contact_set_name (contact, alias);
301         }
302 }
303
304 static void
305 tp_contact_factory_set_avatar_cb (TpConnection *connection,
306                                   const gchar  *token,
307                                   const GError *error,
308                                   gpointer      user_data,
309                                   GObject      *tp_factory)
310 {
311         if (error) {
312                 DEBUG ("Error setting avatar: %s", error->message);
313         }
314 }
315
316 static void
317 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
318                                     const GError *error,
319                                     gpointer      user_data,
320                                     GObject      *tp_factory)
321 {
322         if (error) {
323                 DEBUG ("Error clearing avatar: %s", error->message);
324         }
325 }
326
327 static void
328 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
329                                         guint         handle,
330                                         const gchar  *token,
331                                         const GArray *avatar_data,
332                                         const gchar  *mime_type,
333                                         gpointer      user_data,
334                                         GObject      *tp_factory)
335 {
336         EmpathyContact *contact;
337
338         contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
339                                                      handle);
340         if (!contact) {
341                 return;
342         }
343
344         DEBUG ("Avatar retrieved for contact %s (%d)",
345                 empathy_contact_get_id (contact),
346                 handle);
347
348         empathy_contact_load_avatar_data (contact,
349                                           avatar_data->data,
350                                           avatar_data->len,
351                                           mime_type,
352                                           token);
353 }
354
355 static void
356 tp_contact_factory_request_avatars_cb (TpConnection *connection,
357                                        const GError *error,
358                                        gpointer      user_data,
359                                        GObject      *tp_factory)
360 {
361         if (error) {
362                 DEBUG ("Error requesting avatars: %s", error->message);
363         }
364 }
365
366 static gboolean
367 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
368                                         guint                    handle,
369                                         const gchar             *token)
370 {
371         EmpathyContact *contact;
372         EmpathyAvatar  *avatar;
373
374         contact = tp_contact_factory_find_by_handle (tp_factory, handle);
375         if (!contact) {
376                 return TRUE;
377         }
378
379         /* Check if we have an avatar */
380         if (G_STR_EMPTY (token)) {
381                 empathy_contact_set_avatar (contact, NULL);
382                 return TRUE;
383         }
384
385         /* Check if the avatar changed */
386         avatar = empathy_contact_get_avatar (contact);
387         if (avatar && !tp_strdiff (avatar->token, token)) {
388                 return TRUE;
389         }
390
391         /* The avatar changed, search the new one in the cache */
392         if (empathy_contact_load_avatar_cache (contact, token)) {
393                 /* Got from cache, use it */
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
735         if (error) {
736                 DEBUG ("Failed to get self handles: %s", error->message);
737                 return;
738         }
739
740         empathy_contact_set_handle (priv->user, handle);
741
742         tp_contact_factory_maybe_ready (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
743 }
744
745 static void
746 tp_contact_factory_maybe_ready (EmpathyTpContactFactory *tp_factory)
747 {
748         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
749         GList                       *l;
750         GArray                      *handle_needed;
751         GArray                      *id_needed;
752         GList                       *handle_needed_contacts = NULL;
753         GList                       *id_needed_contacts = NULL;
754
755         if (empathy_contact_get_handle (priv->user) == 0) {
756                 DEBUG ("Connection not ready: still waiting for self handle");
757                 return;
758         }
759
760         DEBUG ("Connection ready");
761
762         priv->ready = TRUE;
763         g_object_notify (G_OBJECT (tp_factory), "ready");
764
765         /* Connect signals */
766         tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
767                                                                          tp_contact_factory_aliases_changed_cb,
768                                                                          NULL, NULL,
769                                                                          G_OBJECT (tp_factory),
770                                                                          NULL);
771         tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
772                                                                        tp_contact_factory_avatar_updated_cb,
773                                                                        NULL, NULL,
774                                                                        G_OBJECT (tp_factory),
775                                                                        NULL);
776         tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
777                                                                          tp_contact_factory_avatar_retrieved_cb,
778                                                                          NULL, NULL,
779                                                                          G_OBJECT (tp_factory),
780                                                                          NULL);
781         tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
782                                                                          tp_contact_factory_presence_update_cb,
783                                                                          NULL, NULL,
784                                                                          G_OBJECT (tp_factory),
785                                                                          NULL);
786         tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
787                                                                                   tp_contact_factory_capabilities_changed_cb,
788                                                                                   NULL, NULL,
789                                                                                   G_OBJECT (tp_factory),
790                                                                                   NULL);
791
792         /* Request needed info for all existing contacts */
793         handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
794         id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
795         for (l = priv->contacts; l; l = l->next) {
796                 EmpathyContact *contact;
797                 guint           handle;
798                 const gchar    *id;
799
800                 contact = l->data;
801                 handle = empathy_contact_get_handle (contact);
802                 id = empathy_contact_get_id (contact);
803                 if (handle == 0) {
804                         g_assert (!G_STR_EMPTY (id));
805                         g_array_append_val (handle_needed, id);
806                         handle_needed_contacts = g_list_prepend (handle_needed_contacts,
807                                                                  g_object_ref (contact));
808                 }
809                 if (G_STR_EMPTY (id)) {
810                         g_array_append_val (id_needed, handle);
811                         id_needed_contacts = g_list_prepend (id_needed_contacts,
812                                                              g_object_ref (contact));
813                 }
814         }
815         handle_needed_contacts = g_list_reverse (handle_needed_contacts);
816         id_needed_contacts = g_list_reverse (id_needed_contacts);
817
818         tp_cli_connection_call_request_handles (priv->connection,
819                                                 -1,
820                                                 TP_HANDLE_TYPE_CONTACT,
821                                                 (const gchar**) handle_needed->data,
822                                                 tp_contact_factory_request_handles_cb,
823                                                 handle_needed_contacts, tp_contact_factory_list_free,
824                                                 G_OBJECT (tp_factory));
825
826         tp_cli_connection_call_inspect_handles (priv->connection,
827                                                 -1,
828                                                 TP_HANDLE_TYPE_CONTACT,
829                                                 id_needed,
830                                                 tp_contact_factory_inspect_handles_cb,
831                                                 id_needed_contacts, tp_contact_factory_list_free,
832                                                 G_OBJECT (tp_factory));
833
834         tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
835                                                id_needed);
836
837         g_array_free (handle_needed, TRUE);
838         g_array_free (id_needed, TRUE);
839 }
840
841 static void
842 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
843 {
844         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
845
846         /* Get our own handle */
847         tp_cli_connection_call_get_self_handle (priv->connection,
848                                                 -1,
849                                                 tp_contact_factory_got_self_handle_cb,
850                                                 NULL, NULL,
851                                                 G_OBJECT (tp_factory));
852 }
853
854 static void
855 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
856 {
857         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
858         gboolean                     connection_ready;
859
860         if (priv->connection) {
861                 /* We already have our connection object */
862                 return;
863         }
864
865         priv->connection = mission_control_get_tpconnection (priv->mc, priv->account, NULL);
866         if (!priv->connection) {
867                 return;
868         }
869
870         /* We got a new connection, wait for it to be ready */
871         g_signal_connect_swapped (priv->connection, "invalidated",
872                                   G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
873                                   tp_factory);
874
875         g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
876         if (connection_ready) {
877                 tp_contact_factory_connection_ready_cb (tp_factory);
878         } else {
879                 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
880                                           G_CALLBACK (tp_contact_factory_connection_ready_cb),
881                                           tp_factory);
882         }
883 }
884
885 static void
886 tp_contact_factory_status_changed_cb (MissionControl           *mc,
887                                       TpConnectionStatus        status,
888                                       McPresence                presence,
889                                       TpConnectionStatusReason  reason,
890                                       const gchar              *unique_name,
891                                       EmpathyTpContactFactory  *tp_factory)
892 {
893         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
894         McAccount                   *account;
895
896         account = mc_account_lookup (unique_name);
897         if (account && empathy_account_equal (account, priv->account)) {
898                 tp_contact_factory_status_updated (tp_factory);
899         }
900         g_object_unref (account);
901 }
902
903 static void
904 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
905                                 EmpathyContact          *contact)
906 {
907         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
908
909         g_object_weak_ref (G_OBJECT (contact),
910                            tp_contact_factory_weak_notify,
911                            tp_factory);
912         priv->contacts = g_list_prepend (priv->contacts, contact);
913
914         DEBUG ("Contact added: %s (%d)",
915                 empathy_contact_get_id (contact),
916                 empathy_contact_get_handle (contact));
917 }
918
919 static void
920 tp_contact_factory_hold_handles_cb (TpConnection *connection,
921                                     const GError *error,
922                                     gpointer      userdata,
923                                     GObject      *tp_factory)
924 {
925         if (error) {
926                 DEBUG ("Failed to hold handles: %s", error->message);
927         }
928 }
929
930 EmpathyContact *
931 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
932 {
933         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
934
935         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
936
937         return g_object_ref (priv->user);
938 }
939
940 EmpathyContact *
941 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
942                                         const gchar             *id)
943 {
944         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
945         EmpathyContact              *contact;
946
947         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
948         g_return_val_if_fail (id != NULL, NULL);
949
950         /* Check if the contact already exists */
951         contact = tp_contact_factory_find_by_id (tp_factory, id);
952         if (contact) {
953                 return g_object_ref (contact);
954         }
955
956         /* Create new contact */
957         contact = g_object_new (EMPATHY_TYPE_CONTACT,
958                                 "account", priv->account,
959                                 "id", id,
960                                 NULL);
961         tp_contact_factory_add_contact (tp_factory, contact);
962
963         if (priv->ready) {
964                 const gchar *contact_ids[] = {id, NULL};
965                 GList       *contacts;
966                 
967                 contacts = g_list_prepend (NULL, g_object_ref (contact));
968                 tp_cli_connection_call_request_handles (priv->connection,
969                                                         -1,
970                                                         TP_HANDLE_TYPE_CONTACT,
971                                                         contact_ids,
972                                                         tp_contact_factory_request_handles_cb,
973                                                         contacts, tp_contact_factory_list_free,
974                                                         G_OBJECT (tp_factory));
975         }
976
977         return contact;
978 }
979
980 EmpathyContact *
981 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
982                                             guint                    handle)
983 {
984         EmpathyContact *contact;
985         GArray         *handles;
986         GList          *contacts;
987
988         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
989
990         handles = g_array_new (FALSE, FALSE, sizeof (guint));
991         g_array_append_val (handles, handle);
992
993         contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
994         g_array_free (handles, TRUE);
995
996         contact = contacts ? contacts->data : NULL;
997         g_list_free (contacts);
998
999         return contact;
1000 }
1001
1002 GList *
1003 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1004                                              const GArray            *handles)
1005 {
1006         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1007         GList                       *contacts = NULL;
1008         GArray                      *new_handles;
1009         GList                       *new_contacts = NULL;
1010         guint                        i;
1011
1012         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1013         g_return_val_if_fail (handles != NULL, NULL);
1014
1015         /* Search all contacts we already have */
1016         new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1017         for (i = 0; i < handles->len; i++) {
1018                 EmpathyContact *contact;
1019                 guint           handle;
1020
1021                 handle = g_array_index (handles, guint, i);
1022                 if (handle == 0) {
1023                         continue;
1024                 }
1025
1026                 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1027                 if (contact) {
1028                         contacts = g_list_prepend (contacts, g_object_ref (contact));
1029                 } else {
1030                         g_array_append_val (new_handles, handle);
1031                 }
1032         }
1033
1034         if (new_handles->len == 0) {
1035                 g_array_free (new_handles, TRUE);
1036                 return contacts;
1037         }
1038
1039         /* Create new contacts */
1040         for (i = 0; i < new_handles->len; i++) {
1041                 EmpathyContact *contact;
1042                 guint           handle;
1043
1044                 handle = g_array_index (new_handles, guint, i);
1045
1046                 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1047                                         "account", priv->account,
1048                                         "handle", handle,
1049                                         NULL);
1050                 tp_contact_factory_add_contact (tp_factory, contact);
1051                 contacts = g_list_prepend (contacts, contact);
1052                 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1053         }
1054         new_contacts = g_list_reverse (new_contacts);
1055
1056         if (priv->ready) {
1057                 /* Get the IDs of all new handles */
1058                 tp_cli_connection_call_inspect_handles (priv->connection,
1059                                                         -1,
1060                                                         TP_HANDLE_TYPE_CONTACT,
1061                                                         new_handles,
1062                                                         tp_contact_factory_inspect_handles_cb,
1063                                                         new_contacts, tp_contact_factory_list_free,
1064                                                         G_OBJECT (tp_factory));
1065
1066                 /* Hold all new handles. */
1067                 /* FIXME: Should be unholded when removed from the factory */
1068                 tp_cli_connection_call_hold_handles (priv->connection,
1069                                                      -1,
1070                                                      TP_HANDLE_TYPE_CONTACT,
1071                                                      new_handles,
1072                                                      tp_contact_factory_hold_handles_cb,
1073                                                      NULL, NULL,
1074                                                      G_OBJECT (tp_factory));
1075
1076                 tp_contact_factory_request_everything (tp_factory, new_handles);
1077         }
1078
1079         g_array_free (new_handles, TRUE);
1080
1081         return contacts;
1082 }
1083
1084 void
1085 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1086                                       EmpathyContact          *contact,
1087                                       const gchar             *alias)
1088 {
1089         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1090         GHashTable                  *new_alias;
1091         guint                        handle;
1092
1093         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1094         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1095         g_return_if_fail (priv->ready);
1096         g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1097                                                  priv->account));
1098
1099         handle = empathy_contact_get_handle (contact);
1100
1101         DEBUG ("Setting alias for contact %s (%d) to %s",
1102                 empathy_contact_get_id (contact),
1103                 handle, alias);
1104
1105         new_alias = g_hash_table_new_full (g_direct_hash,
1106                                            g_direct_equal,
1107                                            NULL,
1108                                            g_free);
1109
1110         g_hash_table_insert (new_alias,
1111                              GUINT_TO_POINTER (handle),
1112                              g_strdup (alias));
1113
1114         tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1115                                                                -1,
1116                                                                new_alias,
1117                                                                tp_contact_factory_set_aliases_cb,
1118                                                                NULL, NULL,
1119                                                                G_OBJECT (tp_factory));
1120
1121         g_hash_table_destroy (new_alias);
1122 }
1123
1124 void
1125 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1126                                        const gchar             *data,
1127                                        gsize                    size,
1128                                        const gchar             *mime_type)
1129 {
1130         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1131
1132         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1133         g_return_if_fail (priv->ready);
1134
1135         if (data && size > 0 && size < G_MAXUINT) {
1136                 GArray avatar;
1137
1138                 avatar.data = (gchar*) data;
1139                 avatar.len = size;
1140
1141                 DEBUG ("Setting avatar on account %s",
1142                         mc_account_get_unique_name (priv->account));
1143
1144                 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1145                                                                      -1,
1146                                                                      &avatar,
1147                                                                      mime_type,
1148                                                                      tp_contact_factory_set_avatar_cb,
1149                                                                      NULL, NULL,
1150                                                                      G_OBJECT (tp_factory));
1151         } else {
1152                 DEBUG ("Clearing avatar on account %s",
1153                         mc_account_get_unique_name (priv->account));
1154
1155                 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1156                                                                        -1,
1157                                                                        tp_contact_factory_clear_avatar_cb,
1158                                                                        NULL, NULL,
1159                                                                        G_OBJECT (tp_factory));
1160         }
1161 }
1162
1163 gboolean
1164 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1165 {
1166         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1167
1168         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1169
1170         return priv->ready;
1171 }
1172
1173 static void
1174 tp_contact_factory_get_property (GObject    *object,
1175                                  guint       param_id,
1176                                  GValue     *value,
1177                                  GParamSpec *pspec)
1178 {
1179         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1180
1181         switch (param_id) {
1182         case PROP_ACCOUNT:
1183                 g_value_set_object (value, priv->account);
1184                 break;
1185         case PROP_READY:
1186                 g_value_set_boolean (value, priv->ready);
1187                 break;
1188         default:
1189                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1190                 break;
1191         };
1192 }
1193
1194 static void
1195 tp_contact_factory_set_property (GObject      *object,
1196                                  guint         param_id,
1197                                  const GValue *value,
1198                                  GParamSpec   *pspec)
1199 {
1200         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1201
1202         switch (param_id) {
1203         case PROP_ACCOUNT:
1204                 priv->account = g_object_ref (g_value_get_object (value));
1205                 break;
1206         default:
1207                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1208                 break;
1209         };
1210 }
1211
1212 static void
1213 tp_contact_factory_finalize (GObject *object)
1214 {
1215         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1216         GList                       *l;
1217
1218         DEBUG ("Finalized: %p (%s)", object,
1219                 mc_account_get_normalized_name (priv->account));
1220
1221         empathy_disconnect_account_status_changed (priv->token);
1222
1223         for (l = priv->contacts; l; l = l->next) {
1224                 g_object_weak_unref (G_OBJECT (l->data),
1225                                      tp_contact_factory_weak_notify,
1226                                      object);
1227         }
1228
1229         g_list_free (priv->contacts);
1230         g_object_unref (priv->mc);
1231         g_object_unref (priv->account);
1232         g_object_unref (priv->user);
1233
1234         if (priv->connection) {
1235                 g_signal_handlers_disconnect_by_func (priv->connection,
1236                                                       tp_contact_factory_connection_invalidated_cb,
1237                                                       object);
1238                 g_object_unref (priv->connection);
1239         }
1240
1241         g_strfreev (priv->avatar_mime_types);
1242
1243         G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1244 }
1245
1246 static GObject *
1247 tp_contact_factory_constructor (GType                  type,
1248                                 guint                  n_props,
1249                                 GObjectConstructParam *props)
1250 {
1251         GObject *tp_factory;
1252         EmpathyTpContactFactoryPriv *priv;
1253
1254         tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1255         priv = GET_PRIV (tp_factory);
1256
1257         priv->ready = FALSE;
1258         priv->user = empathy_contact_new (priv->account);
1259         empathy_contact_set_is_user (priv->user, TRUE);
1260         tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1261         tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1262
1263         return tp_factory;
1264 }
1265
1266 static void
1267 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1268 {
1269         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1270
1271         object_class->finalize = tp_contact_factory_finalize;
1272         object_class->constructor = tp_contact_factory_constructor;
1273         object_class->get_property = tp_contact_factory_get_property;
1274         object_class->set_property = tp_contact_factory_set_property;
1275
1276         g_object_class_install_property (object_class,
1277                                          PROP_ACCOUNT,
1278                                          g_param_spec_object ("account",
1279                                                               "Factory's Account",
1280                                                               "The account associated with the factory",
1281                                                               MC_TYPE_ACCOUNT,
1282                                                               G_PARAM_READWRITE |
1283                                                               G_PARAM_CONSTRUCT_ONLY));
1284         g_object_class_install_property (object_class,
1285                                          PROP_READY,
1286                                          g_param_spec_boolean ("ready",
1287                                                                "Wheter the factor is ready",
1288                                                                "Is the factory ready",
1289                                                                FALSE,
1290                                                                G_PARAM_READABLE));
1291
1292         g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1293 }
1294
1295 static void
1296 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1297 {
1298         EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1299                 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1300
1301         tp_factory->priv = priv;
1302         priv->mc = empathy_mission_control_new ();
1303         priv->token = empathy_connect_to_account_status_changed (priv->mc,
1304                                                    G_CALLBACK (tp_contact_factory_status_changed_cb),
1305                                                    tp_factory, NULL);
1306 }
1307
1308 EmpathyTpContactFactory *
1309 empathy_tp_contact_factory_new (McAccount *account)
1310 {
1311         return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,
1312                              "account", account,
1313                              NULL);
1314 }
1315