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