]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-factory.c
Merge branch 'master' into tp-tube
[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-2009 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/gtypes.h>
28
29 #include <extensions/extensions.h>
30
31 #include "empathy-tp-contact-factory.h"
32 #include "empathy-utils.h"
33
34 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
35 #include "empathy-debug.h"
36
37 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactFactory)
38 typedef struct {
39         TpConnection   *connection;
40         GList          *contacts;
41
42         gchar         **avatar_mime_types;
43         guint           avatar_min_width;
44         guint           avatar_min_height;
45         guint           avatar_max_width;
46         guint           avatar_max_height;
47         guint           avatar_max_size;
48         gboolean        can_request_ft;
49 } EmpathyTpContactFactoryPriv;
50
51 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
52
53 enum {
54         PROP_0,
55         PROP_CONNECTION,
56
57         PROP_MIME_TYPES,
58         PROP_MIN_WIDTH,
59         PROP_MIN_HEIGHT,
60         PROP_MAX_WIDTH,
61         PROP_MAX_HEIGHT,
62         PROP_MAX_SIZE
63 };
64
65 static TpContactFeature contact_features[] = {
66         TP_CONTACT_FEATURE_ALIAS,
67         TP_CONTACT_FEATURE_PRESENCE,
68 };
69
70 static EmpathyContact *
71 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
72                                    guint                    handle)
73 {
74         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
75         GList                       *l;
76
77         for (l = priv->contacts; l; l = l->next) {
78                 if (empathy_contact_get_handle (l->data) == handle) {
79                         return l->data;
80                 }
81         }
82
83         return NULL;
84 }
85
86 static EmpathyContact *
87 tp_contact_factory_find_by_tp_contact (EmpathyTpContactFactory *tp_factory,
88                                        TpContact               *tp_contact)
89 {
90         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
91         GList                       *l;
92
93         for (l = priv->contacts; l; l = l->next) {
94                 if (empathy_contact_get_tp_contact (l->data) == tp_contact) {
95                         return l->data;
96                 }
97         }
98
99         return NULL;
100 }
101
102 static void
103 tp_contact_factory_weak_notify (gpointer data,
104                                 GObject *where_the_object_was)
105 {
106         EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
107
108         DEBUG ("Remove finalized contact %p", where_the_object_was);
109
110         priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
111 }
112
113 static void
114 tp_contact_factory_set_aliases_cb (TpConnection *connection,
115                                    const GError *error,
116                                    gpointer      user_data,
117                                    GObject      *tp_factory)
118 {
119         if (error) {
120                 DEBUG ("Error: %s", error->message);
121         }
122 }
123
124 static void
125 tp_contact_factory_set_avatar_cb (TpConnection *connection,
126                                   const gchar  *token,
127                                   const GError *error,
128                                   gpointer      user_data,
129                                   GObject      *tp_factory)
130 {
131         if (error) {
132                 DEBUG ("Error: %s", error->message);
133         }
134 }
135
136 static void
137 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
138                                     const GError *error,
139                                     gpointer      user_data,
140                                     GObject      *tp_factory)
141 {
142         if (error) {
143                 DEBUG ("Error: %s", error->message);
144         }
145 }
146
147 static void
148 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
149                                         guint         handle,
150                                         const gchar  *token,
151                                         const GArray *avatar_data,
152                                         const gchar  *mime_type,
153                                         gpointer      user_data,
154                                         GObject      *tp_factory)
155 {
156         EmpathyContact *contact;
157
158         contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
159                                                      handle);
160         if (!contact) {
161                 return;
162         }
163
164         DEBUG ("Avatar retrieved for contact %s (%d)",
165                 empathy_contact_get_id (contact),
166                 handle);
167
168         empathy_contact_load_avatar_data (contact,
169                                           avatar_data->data,
170                                           avatar_data->len,
171                                           mime_type,
172                                           token);
173 }
174
175 static void
176 tp_contact_factory_request_avatars_cb (TpConnection *connection,
177                                        const GError *error,
178                                        gpointer      user_data,
179                                        GObject      *tp_factory)
180 {
181         if (error) {
182                 DEBUG ("Error: %s", error->message);
183         }
184 }
185
186 static gboolean
187 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
188                                         guint                    handle,
189                                         const gchar             *token)
190 {
191         EmpathyContact *contact;
192         EmpathyAvatar  *avatar;
193
194         contact = tp_contact_factory_find_by_handle (tp_factory, handle);
195         if (!contact) {
196                 return TRUE;
197         }
198
199         /* Check if we have an avatar */
200         if (EMP_STR_EMPTY (token)) {
201                 empathy_contact_set_avatar (contact, NULL);
202                 return TRUE;
203         }
204
205         /* Check if the avatar changed */
206         avatar = empathy_contact_get_avatar (contact);
207         if (avatar && !tp_strdiff (avatar->token, token)) {
208                 return TRUE;
209         }
210
211         /* The avatar changed, search the new one in the cache */
212         if (empathy_contact_load_avatar_cache (contact, token)) {
213                 /* Got from cache, use it */
214                 return TRUE;
215         }
216
217         /* Avatar is not up-to-date, we have to request it. */
218         return FALSE;
219 }
220
221 typedef struct {
222         EmpathyTpContactFactory *tp_factory;
223         GArray                  *handles;
224 } TokensData;
225
226 static void
227 tp_contact_factory_avatar_tokens_foreach (gpointer key,
228                                           gpointer value,
229                                           gpointer user_data)
230 {
231         TokensData  *data = user_data;
232         const gchar *token = value;
233         guint        handle = GPOINTER_TO_UINT (key);
234
235         if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
236                                                      handle, token)) {
237                 g_array_append_val (data->handles, handle);
238         }
239 }
240
241 static void
242 tp_contact_factory_got_known_avatar_tokens (EmpathyTpContactFactory *tp_factory,
243                                             GHashTable              *tokens,
244                                             const GError            *error)
245 {
246         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
247         TokensData data;
248
249         if (error) {
250                 DEBUG ("Error: %s", error->message);
251                 return;
252         }
253
254         data.tp_factory = tp_factory;
255         data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
256         g_hash_table_foreach (tokens,
257                               tp_contact_factory_avatar_tokens_foreach,
258                               &data);
259
260         DEBUG ("Got %d tokens, need to request %d avatars",
261                 g_hash_table_size (tokens), data.handles->len);
262
263         /* Request needed avatars */
264         if (data.handles->len > 0) {
265                 tp_cli_connection_interface_avatars_call_request_avatars (priv->connection,
266                                                                           -1,
267                                                                           data.handles,
268                                                                           tp_contact_factory_request_avatars_cb,
269                                                                           NULL, NULL,
270                                                                           G_OBJECT (tp_factory));
271         }
272
273         g_array_free (data.handles, TRUE);
274         g_hash_table_destroy (tokens);
275 }
276
277 static void
278 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
279                                       guint         handle,
280                                       const gchar  *new_token,
281                                       gpointer      user_data,
282                                       GObject      *tp_factory)
283 {
284         GArray *handles;
285
286         if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
287                                                     handle, new_token)) {
288                 /* Avatar was cached, nothing to do */
289                 return;
290         }
291
292         DEBUG ("Need to request avatar for token %s", new_token);
293
294         handles = g_array_new (FALSE, FALSE, sizeof (guint));
295         g_array_append_val (handles, handle);
296
297         tp_cli_connection_interface_avatars_call_request_avatars (connection,
298                                                                   -1,
299                                                                   handles,
300                                                                   tp_contact_factory_request_avatars_cb,
301                                                                   NULL, NULL,
302                                                                   tp_factory);
303         g_array_free (handles, TRUE);
304 }
305
306 static void
307 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
308                                         guint                    handle,
309                                         const gchar             *channel_type,
310                                         guint                    generic,
311                                         guint                    specific)
312 {
313         EmpathyContact      *contact;
314         EmpathyCapabilities  capabilities;
315
316         contact = tp_contact_factory_find_by_handle (tp_factory, handle);
317         if (!contact) {
318                 return;
319         }
320
321         capabilities = empathy_contact_get_capabilities (contact);
322         capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
323
324         if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
325                 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
326                 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
327                 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
328                         capabilities |= EMPATHY_CAPABILITIES_AUDIO;
329                 }
330                 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
331                         capabilities |= EMPATHY_CAPABILITIES_VIDEO;
332                 }
333         }
334
335         DEBUG ("Changing capabilities for contact %s (%d) to %d",
336                 empathy_contact_get_id (contact),
337                 empathy_contact_get_handle (contact),
338                 capabilities);
339
340         empathy_contact_set_capabilities (contact, capabilities);
341 }
342
343 static void
344 tp_contact_factory_got_capabilities (EmpathyTpContactFactory *tp_factory,
345                                      GPtrArray *capabilities,
346                                      const GError    *error)
347 {
348         guint i;
349
350         if (error) {
351                 DEBUG ("Error: %s", error->message);
352                 /* FIXME Should set the capabilities of the contacts for which this request
353                  * originated to NONE */
354                 return;
355         }
356
357         for (i = 0; i < capabilities->len; i++) {
358                 GValueArray *values;
359                 guint        handle;
360                 const gchar *channel_type;
361                 guint        generic;
362                 guint        specific;
363
364                 values = g_ptr_array_index (capabilities, i);
365                 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
366                 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
367                 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
368                 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
369
370                 tp_contact_factory_update_capabilities (tp_factory,
371                                                         handle,
372                                                         channel_type,
373                                                         generic,
374                                                         specific);
375
376                 g_value_array_free (values);
377         }
378
379         g_ptr_array_free (capabilities, TRUE);
380 }
381
382 static void
383 tp_contact_factory_capabilities_changed_cb (TpConnection    *connection,
384                                             const GPtrArray *capabilities,
385                                             gpointer         user_data,
386                                             GObject         *weak_object)
387 {
388         EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
389         guint                    i;
390
391         for (i = 0; i < capabilities->len; i++) {
392                 GValueArray *values;
393                 guint        handle;
394                 const gchar *channel_type;
395                 guint        generic;
396                 guint        specific;
397
398                 values = g_ptr_array_index (capabilities, i);
399                 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
400                 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
401                 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
402                 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
403
404                 tp_contact_factory_update_capabilities (tp_factory,
405                                                         handle,
406                                                         channel_type,
407                                                         generic,
408                                                         specific);
409         }
410 }
411
412 static void
413 get_requestable_channel_classes_cb (TpProxy *connection,
414                                     const GValue *value,
415                                     const GError *error,
416                                     gpointer user_data,
417                                     GObject *weak_object)
418 {
419         EmpathyTpContactFactory     *self = EMPATHY_TP_CONTACT_FACTORY (weak_object);
420         EmpathyTpContactFactoryPriv *priv = GET_PRIV (self);
421         GPtrArray                   *classes;
422         guint                        i;
423
424         if (error != NULL) {
425                 DEBUG ("Error: %s", error->message);
426                 return;
427         }
428
429         classes = g_value_get_boxed (value);
430         for (i = 0; i < classes->len; i++) {
431                 GValueArray *class_struct;
432                 GHashTable *fixed_prop;
433                 GValue *chan_type, *handle_type;
434                 GList *l;
435
436                 class_struct = g_ptr_array_index (classes, i);
437                 fixed_prop = g_value_get_boxed (g_value_array_get_nth (class_struct, 0));
438
439                 chan_type = g_hash_table_lookup (fixed_prop,
440                         TP_IFACE_CHANNEL ".ChannelType");
441                 if (chan_type == NULL ||
442                     tp_strdiff (g_value_get_string (chan_type),
443                                 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER)) {
444                         continue;
445                 }
446
447                 handle_type = g_hash_table_lookup (fixed_prop,
448                         TP_IFACE_CHANNEL ".TargetHandleType");
449                 if (handle_type == NULL ||
450                     g_value_get_uint (handle_type) != TP_HANDLE_TYPE_CONTACT) {
451                         continue;
452                 }
453
454                 /* We can request file transfer channel to contacts. */
455                 priv->can_request_ft = TRUE;
456
457                 /* Update the capabilities of all contacts */
458                 for (l = priv->contacts; l != NULL; l = g_list_next (l)) {
459                         EmpathyContact *contact = l->data;
460                         EmpathyCapabilities caps;
461
462                         caps = empathy_contact_get_capabilities (contact);
463                         empathy_contact_set_capabilities (contact, caps |
464                                 EMPATHY_CAPABILITIES_FT);
465                 }
466                 break;
467         }
468 }
469
470 static void
471 tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
472                                                const gchar **mime_types,
473                                                guint         min_width,
474                                                guint         min_height,
475                                                guint         max_width,
476                                                guint         max_height,
477                                                guint         max_size,
478                                                const GError *error,
479                                                gpointer      user_data,
480                                                GObject      *tp_factory)
481 {
482         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
483
484         if (error) {
485                 DEBUG ("Failed to get avatar requirements: %s", error->message);
486                 /* We'll just leave avatar_mime_types as NULL; the
487                  * avatar-setting code can use this as a signal that you can't
488                  * set avatars.
489                  */
490         } else {
491                 priv->avatar_mime_types = g_strdupv ((gchar **) mime_types);
492                 priv->avatar_min_width = min_width;
493                 priv->avatar_min_height = min_height;
494                 priv->avatar_max_width = max_width;
495                 priv->avatar_max_height = max_height;
496                 priv->avatar_max_size = max_size;
497         }
498 }
499
500 static void
501 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
502                                 EmpathyContact          *contact)
503 {
504         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
505         TpHandle self_handle;
506         TpHandle handle;
507         GArray handles = {(gchar*) &handle, 1};
508         GHashTable *tokens;
509         GPtrArray *capabilities;
510         GError *error = NULL;
511
512         /* Keep a weak ref to that contact */
513         g_object_weak_ref (G_OBJECT (contact),
514                            tp_contact_factory_weak_notify,
515                            tp_factory);
516         priv->contacts = g_list_prepend (priv->contacts, contact);
517
518         /* The contact keeps a ref to its factory */
519         g_object_set_data_full (G_OBJECT (contact), "empathy-factory",
520                                 g_object_ref (tp_factory),
521                                 g_object_unref);
522
523         /* Set the FT capability */
524         if (priv->can_request_ft) {
525                 EmpathyCapabilities caps;
526
527                 caps = empathy_contact_get_capabilities (contact);
528                 caps |= EMPATHY_CAPABILITIES_FT;
529
530                 empathy_contact_set_capabilities (contact, caps);
531         }
532
533         /* Set is-user property. Note that it could still be the handle is
534          * different from the connection's self handle, in the case the handle
535          * comes from a group interface. */
536         self_handle = tp_connection_get_self_handle (priv->connection);
537         handle = empathy_contact_get_handle (contact);
538         empathy_contact_set_is_user (contact, self_handle == handle);
539
540         /* FIXME: This should be done by TpContact */
541         tp_cli_connection_interface_avatars_run_get_known_avatar_tokens (priv->connection,
542                                                                          -1,
543                                                                          &handles,
544                                                                          &tokens,
545                                                                          &error,
546                                                                          NULL);
547         tp_contact_factory_got_known_avatar_tokens (tp_factory, tokens, error);
548         g_clear_error (&error);
549
550         tp_cli_connection_interface_capabilities_run_get_capabilities (priv->connection,
551                                                                         -1,
552                                                                         &handles,
553                                                                         &capabilities,
554                                                                         &error,
555                                                                         NULL);
556         tp_contact_factory_got_capabilities (tp_factory, capabilities, error);
557         g_clear_error (&error);
558
559         DEBUG ("Contact added: %s (%d)",
560                 empathy_contact_get_id (contact),
561                 empathy_contact_get_handle (contact));
562 }
563
564 typedef union {
565         EmpathyTpContactFactoryContactsByIdCb ids_cb;
566         EmpathyTpContactFactoryContactsByHandleCb handles_cb;
567         EmpathyTpContactFactoryContactCb contact_cb;
568 } GetContactsCb;
569
570 typedef struct {
571         EmpathyTpContactFactory *tp_factory;
572         GetContactsCb callback;
573         gpointer user_data;
574         GDestroyNotify destroy;
575 } GetContactsData;
576
577 static void
578 get_contacts_data_free (gpointer user_data)
579 {
580         GetContactsData *data = user_data;
581
582         if (data->destroy) {
583                 data->destroy (data->user_data);
584         }
585         g_object_unref (data->tp_factory);
586
587         g_slice_free (GetContactsData, data);
588 }
589
590 static EmpathyContact *
591 dup_contact_for_tp_contact (EmpathyTpContactFactory *tp_factory,
592                             TpContact               *tp_contact)
593 {
594         EmpathyContact *contact;
595
596         contact = tp_contact_factory_find_by_tp_contact (tp_factory,
597                                                          tp_contact);
598
599         if (contact != NULL) {
600                 g_object_ref (contact);
601         } else {
602                 contact = empathy_contact_new (tp_contact);
603                 tp_contact_factory_add_contact (tp_factory, contact);
604         }
605
606         return contact;
607 }
608
609 static EmpathyContact **
610 contacts_array_new (EmpathyTpContactFactory *tp_factory,
611                     guint                    n_contacts,
612                     TpContact * const *      contacts)
613 {
614         EmpathyContact **ret;
615         guint            i;
616
617         ret = g_new0 (EmpathyContact *, n_contacts);
618         for (i = 0; i < n_contacts; i++) {
619                 ret[i] = dup_contact_for_tp_contact (tp_factory, contacts[i]);
620         }
621
622         return ret;
623 }
624
625 static void
626 contacts_array_free (guint            n_contacts,
627                      EmpathyContact **contacts)
628 {
629         guint i;
630
631         for (i = 0; i < n_contacts; i++) {
632                 g_object_unref (contacts[i]);
633         }
634         g_free (contacts);
635 }
636
637 static void
638 get_contacts_by_id_cb (TpConnection *connection,
639                        guint n_contacts,
640                        TpContact * const *contacts,
641                        const gchar * const *requested_ids,
642                        GHashTable *failed_id_errors,
643                        const GError *error,
644                        gpointer user_data,
645                        GObject *weak_object)
646 {
647         GetContactsData *data = user_data;
648         EmpathyContact **empathy_contacts;
649
650         empathy_contacts = contacts_array_new (data->tp_factory,
651                                                n_contacts, contacts);
652         if (data->callback.ids_cb) {
653                 data->callback.ids_cb (data->tp_factory,
654                                        n_contacts, empathy_contacts,
655                                        requested_ids,
656                                        failed_id_errors,
657                                        error,
658                                        data->user_data, weak_object);
659         }
660
661         contacts_array_free (n_contacts, empathy_contacts);
662 }
663
664 void
665 empathy_tp_contact_factory_get_from_ids (EmpathyTpContactFactory *tp_factory,
666                                          guint                    n_ids,
667                                          const gchar * const     *ids,
668                                          EmpathyTpContactFactoryContactsByIdCb callback,
669                                          gpointer                 user_data,
670                                          GDestroyNotify           destroy,
671                                          GObject                 *weak_object)
672 {
673         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
674         GetContactsData *data;
675
676         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
677         g_return_if_fail (ids != NULL);
678
679         data = g_slice_new (GetContactsData);
680         data->callback.ids_cb = callback;
681         data->user_data = user_data;
682         data->destroy = destroy;
683         data->tp_factory = g_object_ref (tp_factory);
684         tp_connection_get_contacts_by_id (priv->connection,
685                                           n_ids, ids,
686                                           G_N_ELEMENTS (contact_features),
687                                           contact_features,
688                                           get_contacts_by_id_cb,
689                                           data,
690                                           (GDestroyNotify) get_contacts_data_free,
691                                           weak_object);
692 }
693
694 static void
695 get_contact_by_id_cb (TpConnection *connection,
696                       guint n_contacts,
697                       TpContact * const *contacts,
698                       const gchar * const *requested_ids,
699                       GHashTable *failed_id_errors,
700                       const GError *error,
701                       gpointer user_data,
702                       GObject *weak_object)
703 {
704         GetContactsData *data = user_data;
705         EmpathyContact  *contact = NULL;
706
707         if (n_contacts == 1) {
708                 contact = dup_contact_for_tp_contact (data->tp_factory,
709                                                       contacts[0]);
710         }
711         else if (error == NULL) {
712                 GHashTableIter iter;
713                 gpointer       value;
714
715                 g_hash_table_iter_init (&iter, failed_id_errors);
716                 while (g_hash_table_iter_next (&iter, NULL, &value)) {
717                         if (value) {
718                                 error = value;
719                                 break;
720                         }
721                 }
722         }
723
724         if (data->callback.contact_cb) {
725                 data->callback.contact_cb (data->tp_factory,
726                                            contact,
727                                            error,
728                                            data->user_data, weak_object);
729         }
730 }
731
732 void
733 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
734                                         const gchar             *id,
735                                         EmpathyTpContactFactoryContactCb callback,
736                                         gpointer                 user_data,
737                                         GDestroyNotify           destroy,
738                                         GObject                 *weak_object)
739 {
740         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
741         GetContactsData *data;
742
743         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
744         g_return_if_fail (id != NULL);
745
746         data = g_slice_new (GetContactsData);
747         data->callback.contact_cb = callback;
748         data->user_data = user_data;
749         data->destroy = destroy;
750         data->tp_factory = g_object_ref (tp_factory);
751         tp_connection_get_contacts_by_id (priv->connection,
752                                           1, &id,
753                                           G_N_ELEMENTS (contact_features),
754                                           contact_features,
755                                           get_contact_by_id_cb,
756                                           data,
757                                           (GDestroyNotify) get_contacts_data_free,
758                                           weak_object);
759 }
760
761 static void
762 get_contacts_by_handle_cb (TpConnection *connection,
763                            guint n_contacts,
764                            TpContact * const *contacts,
765                            guint n_failed,
766                            const TpHandle *failed,
767                            const GError *error,
768                            gpointer user_data,
769                            GObject *weak_object)
770 {
771         GetContactsData *data = user_data;
772         EmpathyContact **empathy_contacts;
773
774         empathy_contacts = contacts_array_new (data->tp_factory,
775                                                n_contacts, contacts);
776         if (data->callback.handles_cb) {
777                 data->callback.handles_cb (data->tp_factory,
778                                            n_contacts, empathy_contacts,
779                                            n_failed, failed,
780                                            error,
781                                            data->user_data, weak_object);
782         }
783
784         contacts_array_free (n_contacts, empathy_contacts);
785 }
786
787 void
788 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
789                                              guint n_handles,
790                                              const TpHandle *handles,
791                                              EmpathyTpContactFactoryContactsByHandleCb callback,
792                                              gpointer                 user_data,
793                                              GDestroyNotify           destroy,
794                                              GObject                 *weak_object)
795 {
796         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
797         GetContactsData *data;
798
799         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
800         g_return_if_fail (handles != NULL);
801
802         data = g_slice_new (GetContactsData);
803         data->callback.handles_cb = callback;
804         data->user_data = user_data;
805         data->destroy = destroy;
806         data->tp_factory = g_object_ref (tp_factory);
807         tp_connection_get_contacts_by_handle (priv->connection,
808                                               n_handles, handles,
809                                               G_N_ELEMENTS (contact_features),
810                                               contact_features,
811                                               get_contacts_by_handle_cb,
812                                               data,
813                                               (GDestroyNotify) get_contacts_data_free,
814                                               weak_object);
815 }
816
817 static void
818 get_contact_by_handle_cb (TpConnection *connection,
819                           guint n_contacts,
820                           TpContact * const *contacts,
821                           guint n_failed,
822                           const TpHandle *failed,
823                           const GError *error,
824                           gpointer user_data,
825                           GObject *weak_object)
826 {
827         GetContactsData *data = user_data;
828         EmpathyContact  *contact = NULL;
829
830         if (n_contacts == 1) {
831                 contact = dup_contact_for_tp_contact (data->tp_factory,
832                                                       contacts[0]);
833         }
834
835         if (data->callback.contact_cb) {
836                 data->callback.contact_cb (data->tp_factory,
837                                            contact,
838                                            error,
839                                            data->user_data, weak_object);
840         }
841 }
842
843 void
844 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
845                                             TpHandle                 handle,
846                                             EmpathyTpContactFactoryContactCb callback,
847                                             gpointer                 user_data,
848                                             GDestroyNotify           destroy,
849                                             GObject                 *weak_object)
850 {
851         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
852         GetContactsData *data;
853
854         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
855
856         data = g_slice_new (GetContactsData);
857         data->callback.contact_cb = callback;
858         data->user_data = user_data;
859         data->destroy = destroy;
860         data->tp_factory = g_object_ref (tp_factory);
861         tp_connection_get_contacts_by_handle (priv->connection,
862                                               1, &handle,
863                                               G_N_ELEMENTS (contact_features),
864                                               contact_features,
865                                               get_contact_by_handle_cb,
866                                               data,
867                                               (GDestroyNotify) get_contacts_data_free,
868                                               weak_object);
869 }
870
871 void
872 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
873                                       EmpathyContact          *contact,
874                                       const gchar             *alias)
875 {
876         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
877         GHashTable                  *new_alias;
878         guint                        handle;
879
880         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
881         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
882
883         handle = empathy_contact_get_handle (contact);
884
885         DEBUG ("Setting alias for contact %s (%d) to %s",
886                 empathy_contact_get_id (contact),
887                 handle, alias);
888
889         new_alias = g_hash_table_new_full (g_direct_hash,
890                                            g_direct_equal,
891                                            NULL,
892                                            g_free);
893
894         g_hash_table_insert (new_alias,
895                              GUINT_TO_POINTER (handle),
896                              g_strdup (alias));
897
898         tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
899                                                                -1,
900                                                                new_alias,
901                                                                tp_contact_factory_set_aliases_cb,
902                                                                NULL, NULL,
903                                                                G_OBJECT (tp_factory));
904
905         g_hash_table_destroy (new_alias);
906 }
907
908 void
909 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
910                                        const gchar             *data,
911                                        gsize                    size,
912                                        const gchar             *mime_type)
913 {
914         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
915
916         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
917
918         if (data && size > 0 && size < G_MAXUINT) {
919                 GArray avatar;
920
921                 avatar.data = (gchar*) data;
922                 avatar.len = size;
923
924                 DEBUG ("Setting avatar on connection %s",
925                         tp_proxy_get_object_path (TP_PROXY (priv->connection)));
926
927                 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
928                                                                      -1,
929                                                                      &avatar,
930                                                                      mime_type,
931                                                                      tp_contact_factory_set_avatar_cb,
932                                                                      NULL, NULL,
933                                                                      G_OBJECT (tp_factory));
934         } else {
935                 DEBUG ("Clearing avatar on connection %s",
936                         tp_proxy_get_object_path (TP_PROXY (priv->connection)));
937
938                 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
939                                                                        -1,
940                                                                        tp_contact_factory_clear_avatar_cb,
941                                                                        NULL, NULL,
942                                                                        G_OBJECT (tp_factory));
943         }
944 }
945
946 static void
947 tp_contact_factory_get_property (GObject    *object,
948                                  guint       param_id,
949                                  GValue     *value,
950                                  GParamSpec *pspec)
951 {
952         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
953
954         switch (param_id) {
955         case PROP_CONNECTION:
956                 g_value_set_object (value, priv->connection);
957                 break;
958         case PROP_MIME_TYPES:
959                 g_value_set_boxed (value, priv->avatar_mime_types);
960                 break;
961         case PROP_MIN_WIDTH:
962                 g_value_set_uint (value, priv->avatar_min_width);
963                 break;
964         case PROP_MIN_HEIGHT:
965                 g_value_set_uint (value, priv->avatar_min_height);
966                 break;
967         case PROP_MAX_WIDTH:
968                 g_value_set_uint (value, priv->avatar_max_width);
969                 break;
970         case PROP_MAX_HEIGHT:
971                 g_value_set_uint (value, priv->avatar_max_height);
972                 break;
973         case PROP_MAX_SIZE:
974                 g_value_set_uint (value, priv->avatar_max_size);
975                 break;
976         default:
977                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
978                 break;
979         };
980 }
981
982 static void
983 tp_contact_factory_set_property (GObject      *object,
984                                  guint         param_id,
985                                  const GValue *value,
986                                  GParamSpec   *pspec)
987 {
988         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
989
990         switch (param_id) {
991         case PROP_CONNECTION:
992                 priv->connection = g_value_dup_object (value);
993                 break;
994         default:
995                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
996                 break;
997         };
998 }
999
1000 static void
1001 tp_contact_factory_finalize (GObject *object)
1002 {
1003         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1004         GList                       *l;
1005
1006         DEBUG ("Finalized: %p", object);
1007
1008         for (l = priv->contacts; l; l = l->next) {
1009                 g_object_weak_unref (G_OBJECT (l->data),
1010                                      tp_contact_factory_weak_notify,
1011                                      object);
1012         }
1013
1014         g_list_free (priv->contacts);
1015
1016         g_object_unref (priv->connection);
1017
1018         g_strfreev (priv->avatar_mime_types);
1019
1020         G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1021 }
1022
1023 static GObject *
1024 tp_contact_factory_constructor (GType                  type,
1025                                 guint                  n_props,
1026                                 GObjectConstructParam *props)
1027 {
1028         GObject *tp_factory;
1029         EmpathyTpContactFactoryPriv *priv;
1030
1031         tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1032         priv = GET_PRIV (tp_factory);
1033
1034         /* FIXME: This should be moved to TpContact */
1035         tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
1036                                                                        tp_contact_factory_avatar_updated_cb,
1037                                                                        NULL, NULL,
1038                                                                        tp_factory,
1039                                                                        NULL);
1040         tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
1041                                                                          tp_contact_factory_avatar_retrieved_cb,
1042                                                                          NULL, NULL,
1043                                                                          tp_factory,
1044                                                                          NULL);
1045         tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
1046                                                                                   tp_contact_factory_capabilities_changed_cb,
1047                                                                                   NULL, NULL,
1048                                                                                   tp_factory,
1049                                                                                   NULL);
1050
1051
1052         /* FIXME: This should be moved to TpConnection */
1053         tp_cli_connection_interface_avatars_call_get_avatar_requirements (priv->connection,
1054                                                                           -1,
1055                                                                           tp_contact_factory_got_avatar_requirements_cb,
1056                                                                           NULL, NULL,
1057                                                                           tp_factory);
1058         tp_cli_dbus_properties_call_get (priv->connection, -1,
1059                 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
1060                 "RequestableChannelClasses",
1061                 get_requestable_channel_classes_cb, NULL, NULL,
1062                 G_OBJECT (tp_factory));
1063
1064         return tp_factory;
1065 }
1066
1067 static void
1068 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1069 {
1070         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1071
1072         object_class->finalize = tp_contact_factory_finalize;
1073         object_class->constructor = tp_contact_factory_constructor;
1074         object_class->get_property = tp_contact_factory_get_property;
1075         object_class->set_property = tp_contact_factory_set_property;
1076
1077         g_object_class_install_property (object_class,
1078                                          PROP_CONNECTION,
1079                                          g_param_spec_object ("connection",
1080                                                               "Factory's Connection",
1081                                                               "The connection associated with the factory",
1082                                                               TP_TYPE_CONNECTION,
1083                                                               G_PARAM_READWRITE |
1084                                                               G_PARAM_CONSTRUCT_ONLY |
1085                                                               G_PARAM_STATIC_STRINGS));
1086         g_object_class_install_property (object_class,
1087                                          PROP_MIME_TYPES,
1088                                          g_param_spec_boxed ("avatar-mime-types",
1089                                                              "Supported MIME types for avatars",
1090                                                              "Types of images that may be set as "
1091                                                              "avatars on this connection.",
1092                                                              G_TYPE_STRV,
1093                                                              G_PARAM_READABLE |
1094                                                              G_PARAM_STATIC_STRINGS));
1095         g_object_class_install_property (object_class,
1096                                          PROP_MIN_WIDTH,
1097                                          g_param_spec_uint ("avatar-min-width",
1098                                                             "Minimum width for avatars",
1099                                                             "Minimum width of avatar that may be set.",
1100                                                             0,
1101                                                             G_MAXUINT,
1102                                                             0,
1103                                                             G_PARAM_READABLE |
1104                                                             G_PARAM_STATIC_STRINGS));
1105         g_object_class_install_property (object_class,
1106                                          PROP_MIN_HEIGHT,
1107                                          g_param_spec_uint ("avatar-min-height",
1108                                                             "Minimum height for avatars",
1109                                                             "Minimum height of avatar that may be set.",
1110                                                             0,
1111                                                             G_MAXUINT,
1112                                                             0,
1113                                                             G_PARAM_READABLE |
1114                                                             G_PARAM_STATIC_STRINGS));
1115         g_object_class_install_property (object_class,
1116                                          PROP_MAX_WIDTH,
1117                                          g_param_spec_uint ("avatar-max-width",
1118                                                             "Maximum width for avatars",
1119                                                             "Maximum width of avatar that may be set "
1120                                                             "or 0 if there is no maximum.",
1121                                                             0,
1122                                                             G_MAXUINT,
1123                                                             0,
1124                                                             G_PARAM_READABLE |
1125                                                             G_PARAM_STATIC_STRINGS));
1126         g_object_class_install_property (object_class,
1127                                          PROP_MAX_HEIGHT,
1128                                          g_param_spec_uint ("avatar-max-height",
1129                                                             "Maximum height for avatars",
1130                                                             "Maximum height of avatar that may be set "
1131                                                             "or 0 if there is no maximum.",
1132                                                             0,
1133                                                             G_MAXUINT,
1134                                                             0,
1135                                                             G_PARAM_READABLE |
1136                                                             G_PARAM_STATIC_STRINGS));
1137         g_object_class_install_property (object_class,
1138                                          PROP_MAX_SIZE,
1139                                          g_param_spec_uint ("avatar-max-size",
1140                                                             "Maximum size for avatars in bytes",
1141                                                             "Maximum file size of avatar that may be "
1142                                                             "set or 0 if there is no maximum.",
1143                                                             0,
1144                                                             G_MAXUINT,
1145                                                             0,
1146                                                             G_PARAM_READABLE |
1147                                                             G_PARAM_STATIC_STRINGS));
1148
1149
1150         g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1151 }
1152
1153 static void
1154 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1155 {
1156         EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1157                 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1158
1159         tp_factory->priv = priv;
1160         priv->can_request_ft = FALSE;
1161 }
1162
1163 static GHashTable *factories = NULL;
1164
1165 static void
1166 tp_contact_factory_connection_invalidated_cb (TpProxy *connection,
1167                                               guint    domain,
1168                                               gint     code,
1169                                               gchar   *message,
1170                                               gpointer user_data)
1171 {
1172         DEBUG ("Message: %s", message);
1173         g_hash_table_remove (factories, connection);
1174 }
1175
1176 static void
1177 tp_contact_factory_connection_weak_notify_cb (gpointer connection,
1178                                               GObject *where_the_object_was)
1179 {
1180         g_hash_table_remove (factories, connection);
1181 }
1182
1183 static void
1184 tp_contact_factory_remove_connection (gpointer connection)
1185 {
1186         g_signal_handlers_disconnect_by_func (connection,
1187                 tp_contact_factory_connection_invalidated_cb, NULL);
1188         g_object_unref (connection);
1189 }
1190
1191 EmpathyTpContactFactory *
1192 empathy_tp_contact_factory_dup_singleton (TpConnection *connection)
1193 {
1194         EmpathyTpContactFactory *tp_factory;
1195
1196         g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1197
1198         if (factories == NULL) {
1199                 factories = g_hash_table_new_full (empathy_proxy_hash,
1200                                                    empathy_proxy_equal,
1201                                                    tp_contact_factory_remove_connection,
1202                                                    NULL);
1203         }
1204
1205         tp_factory = g_hash_table_lookup (factories, connection);
1206         if (tp_factory == NULL) {
1207                 tp_factory = g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,
1208                                            "connection", connection,
1209                                            NULL);
1210                 g_hash_table_insert (factories, g_object_ref (connection),
1211                                      tp_factory);
1212                 g_object_weak_ref (G_OBJECT (tp_factory),
1213                                    tp_contact_factory_connection_weak_notify_cb,
1214                                    connection);
1215                 g_signal_connect (connection, "invalidated",
1216                                   G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
1217                                   NULL);
1218         } else {
1219                 g_object_ref (tp_factory);
1220         }
1221
1222         return tp_factory;
1223 }
1224