]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-factory.c
add myself to AUTHORS
[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 #include <telepathy-glib/dbus.h>
29 #include <telepathy-glib/interfaces.h>
30
31 #if HAVE_GEOCLUE
32 #include <geoclue/geoclue-geocode.h>
33 #endif
34
35 #include <extensions/extensions.h>
36
37 #include "empathy-tp-contact-factory.h"
38 #include "empathy-utils.h"
39 #include "empathy-location.h"
40
41 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
42 #include "empathy-debug.h"
43
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactFactory)
45 typedef struct {
46         TpConnection   *connection;
47         GList          *contacts;
48
49         gchar         **avatar_mime_types;
50         guint           avatar_min_width;
51         guint           avatar_min_height;
52         guint           avatar_max_width;
53         guint           avatar_max_height;
54         guint           avatar_max_size;
55         gboolean        can_request_ft;
56         gboolean        can_request_st;
57 } EmpathyTpContactFactoryPriv;
58
59 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
60
61 enum {
62         PROP_0,
63         PROP_CONNECTION,
64
65         PROP_MIME_TYPES,
66         PROP_MIN_WIDTH,
67         PROP_MIN_HEIGHT,
68         PROP_MAX_WIDTH,
69         PROP_MAX_HEIGHT,
70         PROP_MAX_SIZE
71 };
72
73 static TpContactFeature contact_features[] = {
74         TP_CONTACT_FEATURE_ALIAS,
75         TP_CONTACT_FEATURE_PRESENCE,
76 };
77
78 static EmpathyContact *
79 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
80                                    guint                    handle)
81 {
82         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
83         GList                       *l;
84
85         for (l = priv->contacts; l; l = l->next) {
86                 if (empathy_contact_get_handle (l->data) == handle) {
87                         return l->data;
88                 }
89         }
90
91         return NULL;
92 }
93
94 static EmpathyContact *
95 tp_contact_factory_find_by_tp_contact (EmpathyTpContactFactory *tp_factory,
96                                        TpContact               *tp_contact)
97 {
98         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
99         GList                       *l;
100
101         for (l = priv->contacts; l; l = l->next) {
102                 if (empathy_contact_get_tp_contact (l->data) == tp_contact) {
103                         return l->data;
104                 }
105         }
106
107         return NULL;
108 }
109
110 static void
111 tp_contact_factory_weak_notify (gpointer data,
112                                 GObject *where_the_object_was)
113 {
114         EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
115
116         DEBUG ("Remove finalized contact %p", where_the_object_was);
117
118         priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
119 }
120
121 static void
122 tp_contact_factory_set_aliases_cb (TpConnection *connection,
123                                    const GError *error,
124                                    gpointer      user_data,
125                                    GObject      *tp_factory)
126 {
127         if (error) {
128                 DEBUG ("Error: %s", error->message);
129         }
130 }
131
132 static void
133 tp_contact_factory_set_location_cb (TpConnection *tp_conn,
134                                     const GError *error,
135                                     gpointer user_data,
136                                     GObject *weak_object)
137 {
138         if (error != NULL) {
139                 DEBUG ("Error setting location: %s", error->message);
140         }
141 }
142
143 static void
144 tp_contact_factory_set_avatar_cb (TpConnection *connection,
145                                   const gchar  *token,
146                                   const GError *error,
147                                   gpointer      user_data,
148                                   GObject      *tp_factory)
149 {
150         if (error) {
151                 DEBUG ("Error: %s", error->message);
152         }
153 }
154
155 static void
156 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
157                                     const GError *error,
158                                     gpointer      user_data,
159                                     GObject      *tp_factory)
160 {
161         if (error) {
162                 DEBUG ("Error: %s", error->message);
163         }
164 }
165
166 static void
167 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
168                                         guint         handle,
169                                         const gchar  *token,
170                                         const GArray *avatar_data,
171                                         const gchar  *mime_type,
172                                         gpointer      user_data,
173                                         GObject      *tp_factory)
174 {
175         EmpathyContact *contact;
176
177         contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
178                                                      handle);
179         if (!contact) {
180                 return;
181         }
182
183         DEBUG ("Avatar retrieved for contact %s (%d)",
184                 empathy_contact_get_id (contact),
185                 handle);
186
187         empathy_contact_load_avatar_data (contact,
188                                           (guchar *) avatar_data->data,
189                                           avatar_data->len,
190                                           mime_type,
191                                           token);
192 }
193
194 static void
195 tp_contact_factory_request_avatars_cb (TpConnection *connection,
196                                        const GError *error,
197                                        gpointer      user_data,
198                                        GObject      *tp_factory)
199 {
200         if (error) {
201                 DEBUG ("Error: %s", error->message);
202         }
203 }
204
205 static gboolean
206 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
207                                         guint                    handle,
208                                         const gchar             *token)
209 {
210         EmpathyContact *contact;
211         EmpathyAvatar  *avatar;
212
213         contact = tp_contact_factory_find_by_handle (tp_factory, handle);
214         if (!contact) {
215                 return TRUE;
216         }
217
218         /* Check if we have an avatar */
219         if (EMP_STR_EMPTY (token)) {
220                 empathy_contact_set_avatar (contact, NULL);
221                 return TRUE;
222         }
223
224         /* Check if the avatar changed */
225         avatar = empathy_contact_get_avatar (contact);
226         if (avatar && !tp_strdiff (avatar->token, token)) {
227                 return TRUE;
228         }
229
230         /* The avatar changed, search the new one in the cache */
231         if (empathy_contact_load_avatar_cache (contact, token)) {
232                 /* Got from cache, use it */
233                 return TRUE;
234         }
235
236         /* Avatar is not up-to-date, we have to request it. */
237         return FALSE;
238 }
239
240 static void
241 tp_contact_factory_got_known_avatar_tokens (TpConnection *connection,
242                                             GHashTable   *tokens,
243                                             const GError *error,
244                                             gpointer      user_data,
245                                             GObject      *weak_object)
246 {
247         EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
248         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
249         GArray *handles;
250         GHashTableIter iter;
251         gpointer key, value;
252
253         if (error) {
254                 DEBUG ("Error: %s", error->message);
255                 return;
256         }
257
258         handles = g_array_new (FALSE, FALSE, sizeof (guint));
259
260         g_hash_table_iter_init (&iter, tokens);
261         while (g_hash_table_iter_next (&iter, &key, &value)) {
262                 guint handle = GPOINTER_TO_UINT (key);
263                 const gchar *token = value;
264
265                 if (!tp_contact_factory_avatar_maybe_update (tp_factory,
266                                                              handle, token)) {
267                         g_array_append_val (handles, handle);
268                 }
269         }
270
271         DEBUG ("Got %d tokens, need to request %d avatars",
272                 g_hash_table_size (tokens), handles->len);
273
274         /* Request needed avatars */
275         if (handles->len > 0) {
276                 tp_cli_connection_interface_avatars_call_request_avatars (priv->connection,
277                                                                           -1,
278                                                                           handles,
279                                                                           tp_contact_factory_request_avatars_cb,
280                                                                           NULL, NULL,
281                                                                           G_OBJECT (tp_factory));
282         }
283
284         g_array_free (handles, TRUE);
285 }
286
287 static void
288 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
289                                       guint         handle,
290                                       const gchar  *new_token,
291                                       gpointer      user_data,
292                                       GObject      *tp_factory)
293 {
294         GArray *handles;
295
296         if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
297                                                     handle, new_token)) {
298                 /* Avatar was cached, nothing to do */
299                 return;
300         }
301
302         DEBUG ("Need to request avatar for token %s", new_token);
303
304         handles = g_array_new (FALSE, FALSE, sizeof (guint));
305         g_array_append_val (handles, handle);
306
307         tp_cli_connection_interface_avatars_call_request_avatars (connection,
308                                                                   -1,
309                                                                   handles,
310                                                                   tp_contact_factory_request_avatars_cb,
311                                                                   NULL, NULL,
312                                                                   tp_factory);
313         g_array_free (handles, TRUE);
314 }
315
316 static void
317 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
318                                         guint                    handle,
319                                         const gchar             *channel_type,
320                                         guint                    generic,
321                                         guint                    specific)
322 {
323         EmpathyContact      *contact;
324         EmpathyCapabilities  capabilities;
325
326         contact = tp_contact_factory_find_by_handle (tp_factory, handle);
327         if (!contact) {
328                 return;
329         }
330
331         capabilities = empathy_contact_get_capabilities (contact);
332         capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
333
334         if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
335                 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
336                 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
337                 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
338                         capabilities |= EMPATHY_CAPABILITIES_AUDIO;
339                 }
340                 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
341                         capabilities |= EMPATHY_CAPABILITIES_VIDEO;
342                 }
343         }
344
345         DEBUG ("Changing capabilities for contact %s (%d) to %d",
346                 empathy_contact_get_id (contact),
347                 empathy_contact_get_handle (contact),
348                 capabilities);
349
350         empathy_contact_set_capabilities (contact, capabilities);
351 }
352
353 static void
354 tp_contact_factory_got_capabilities (TpConnection    *connection,
355                                      const GPtrArray *capabilities,
356                                      const GError    *error,
357                                      gpointer         user_data,
358                                      GObject         *weak_object)
359 {
360         EmpathyTpContactFactory *tp_factory;
361         guint i;
362
363         tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
364
365         if (error) {
366                 DEBUG ("Error: %s", error->message);
367                 /* FIXME Should set the capabilities of the contacts for which this request
368                  * originated to NONE */
369                 return;
370         }
371
372         for (i = 0; i < capabilities->len; i++) {
373                 GValueArray *values;
374                 guint        handle;
375                 const gchar *channel_type;
376                 guint        generic;
377                 guint        specific;
378
379                 values = g_ptr_array_index (capabilities, i);
380                 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
381                 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
382                 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
383                 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
384
385                 tp_contact_factory_update_capabilities (tp_factory,
386                                                         handle,
387                                                         channel_type,
388                                                         generic,
389                                                         specific);
390         }
391 }
392
393 #if HAVE_GEOCLUE
394 #define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
395 #define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
396
397 /* This callback is called by geoclue when it found a position
398  * for the given address.  A position is necessary for a contact
399  * to show up on the map
400  */
401 static void
402 geocode_cb (GeoclueGeocode *geocode,
403             GeocluePositionFields fields,
404             double latitude,
405             double longitude,
406             double altitude,
407             GeoclueAccuracy *accuracy,
408             GError *error,
409             gpointer contact)
410 {
411         GValue *new_value;
412         GHashTable *location;
413
414         location = empathy_contact_get_location (EMPATHY_CONTACT (contact));
415
416         if (error != NULL) {
417                 DEBUG ("Error geocoding location : %s", error->message);
418                 g_object_unref (geocode);
419                 g_object_unref (contact);
420                 return;
421         }
422
423         if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE) {
424                 new_value = tp_g_value_slice_new_double (latitude);
425                 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LAT),
426                         new_value);
427                 DEBUG ("\t - Latitude: %f", latitude);
428         }
429         if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) {
430                 new_value = tp_g_value_slice_new_double (longitude);
431                 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LON),
432                         new_value);
433                 DEBUG ("\t - Longitude: %f", longitude);
434         }
435         if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE) {
436                 new_value = tp_g_value_slice_new_double (altitude);
437                 g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_ALT),
438                         new_value);
439                 DEBUG ("\t - Altitude: %f", altitude);
440         }
441
442         /* Don't change the accuracy as we used an address to get this position */
443         g_object_notify (contact, "location");
444         g_object_unref (geocode);
445         g_object_unref (contact);
446 }
447 #endif
448
449 #if HAVE_GEOCLUE
450 static gchar *
451 get_dup_string (GHashTable *location,
452     gchar *key)
453 {
454   GValue *value;
455
456   value = g_hash_table_lookup (location, key);
457   if (value != NULL)
458     return g_value_dup_string (value);
459
460   return NULL;
461 }
462 #endif
463
464 static void
465 tp_contact_factory_geocode (EmpathyContact *contact)
466 {
467 #if HAVE_GEOCLUE
468         static GeoclueGeocode *geocode;
469         gchar *str;
470         GHashTable *address;
471         GValue* value;
472         GHashTable *location;
473
474         location = empathy_contact_get_location (contact);
475         if (location == NULL)
476                 return;
477
478         value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
479         if (value != NULL)
480                 return;
481
482         if (geocode == NULL) {
483                 geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
484                 g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
485         }
486         else
487                 g_object_ref (geocode);
488
489         address = geoclue_address_details_new ();
490
491         str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
492         if (str != NULL) {
493                 g_hash_table_insert (address,
494                         g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
495                 DEBUG ("\t - countrycode: %s", str);
496         }
497
498         str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
499         if (str != NULL) {
500                 g_hash_table_insert (address,
501                         g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
502                 DEBUG ("\t - country: %s", str);
503         }
504
505         str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
506         if (str != NULL) {
507                 g_hash_table_insert (address,
508                         g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
509                 DEBUG ("\t - postalcode: %s", str);
510         }
511
512         str = get_dup_string (location, EMPATHY_LOCATION_REGION);
513         if (str != NULL) {
514                 g_hash_table_insert (address,
515                         g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
516                 DEBUG ("\t - region: %s", str);
517         }
518
519         str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
520         if (str != NULL) {
521                 g_hash_table_insert (address,
522                         g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
523                 DEBUG ("\t - locality: %s", str);
524         }
525
526         str = get_dup_string (location, EMPATHY_LOCATION_STREET);
527         if (str != NULL) {
528                 g_hash_table_insert (address,
529                         g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
530                 DEBUG ("\t - street: %s", str);
531         }
532
533         g_object_ref (contact);
534         geoclue_geocode_address_to_position_async (geocode, address,
535                 geocode_cb, contact);
536
537         g_hash_table_unref (address);
538 #endif
539 }
540
541 static void
542 tp_contact_factory_update_location (EmpathyTpContactFactory *tp_factory,
543                                     guint handle,
544                                     GHashTable *location)
545 {
546         EmpathyContact *contact;
547         GHashTable     *new_location;
548
549         contact = tp_contact_factory_find_by_handle (tp_factory, handle);
550
551         if (contact == NULL)
552                 return;
553
554         new_location = g_hash_table_new_full (g_str_hash, g_str_equal,
555                 (GDestroyNotify) g_free, (GDestroyNotify) tp_g_value_slice_free);
556         tp_g_hash_table_update (new_location, location, (GBoxedCopyFunc) g_strdup,
557                 (GBoxedCopyFunc) tp_g_value_slice_dup);
558         empathy_contact_set_location (contact, new_location);
559         g_hash_table_unref (new_location);
560
561         tp_contact_factory_geocode (contact);
562 }
563
564 static void
565 tp_contact_factory_got_locations (TpConnection                 *tp_conn,
566                                   GHashTable              *locations,
567                                   const GError            *error,
568                                   gpointer                 user_data,
569                                   GObject                 *weak_object)
570 {
571         GHashTableIter iter;
572         gpointer key, value;
573         EmpathyTpContactFactory *tp_factory;
574
575         tp_factory = EMPATHY_TP_CONTACT_FACTORY (user_data);
576         if (error != NULL) {
577                 DEBUG ("Error: %s", error->message);
578                 return;
579         }
580
581         g_hash_table_iter_init (&iter, locations);
582         while (g_hash_table_iter_next (&iter, &key, &value)) {
583                 guint           handle = GPOINTER_TO_INT (key);
584                 GHashTable     *location = value;
585
586                 tp_contact_factory_update_location (tp_factory, handle, location);
587         }
588 }
589
590 static void
591 tp_contact_factory_capabilities_changed_cb (TpConnection    *connection,
592                                             const GPtrArray *capabilities,
593                                             gpointer         user_data,
594                                             GObject         *weak_object)
595 {
596         EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
597         guint                    i;
598
599         for (i = 0; i < capabilities->len; i++) {
600                 GValueArray *values;
601                 guint        handle;
602                 const gchar *channel_type;
603                 guint        generic;
604                 guint        specific;
605
606                 values = g_ptr_array_index (capabilities, i);
607                 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
608                 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
609                 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
610                 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
611
612                 tp_contact_factory_update_capabilities (tp_factory,
613                                                         handle,
614                                                         channel_type,
615                                                         generic,
616                                                         specific);
617         }
618 }
619
620 static void
621 tp_contact_factory_location_updated_cb (TpConnection      *tp_conn,
622                                         guint         handle,
623                                         GHashTable   *location,
624                                         gpointer      user_data,
625                                         GObject      *weak_object)
626 {
627         EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
628         tp_contact_factory_update_location (tp_factory, handle, location);
629 }
630
631 static void
632 get_requestable_channel_classes_cb (TpProxy *connection,
633                                     const GValue *value,
634                                     const GError *error,
635                                     gpointer user_data,
636                                     GObject *weak_object)
637 {
638         EmpathyTpContactFactory     *self = EMPATHY_TP_CONTACT_FACTORY (weak_object);
639         EmpathyTpContactFactoryPriv *priv = GET_PRIV (self);
640         GPtrArray                   *classes;
641         guint                        i;
642         GList                       *l;
643
644         if (error != NULL) {
645                 DEBUG ("Error: %s", error->message);
646                 return;
647         }
648
649         classes = g_value_get_boxed (value);
650         for (i = 0; i < classes->len; i++) {
651                 GValueArray *class_struct;
652                 GHashTable *fixed_prop;
653                 GValue *chan_type, *handle_type;
654
655                 class_struct = g_ptr_array_index (classes, i);
656                 fixed_prop = g_value_get_boxed (g_value_array_get_nth (class_struct, 0));
657
658                 handle_type = g_hash_table_lookup (fixed_prop,
659                         TP_IFACE_CHANNEL ".TargetHandleType");
660                 if (handle_type == NULL ||
661                     g_value_get_uint (handle_type) != TP_HANDLE_TYPE_CONTACT)
662                         continue;
663
664                 chan_type = g_hash_table_lookup (fixed_prop,
665                         TP_IFACE_CHANNEL ".ChannelType");
666                 if (chan_type == NULL)
667                         continue;
668
669                 if (!tp_strdiff (g_value_get_string (chan_type),
670                     TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
671                         priv->can_request_ft = TRUE;
672                 else if (!tp_strdiff (g_value_get_string (chan_type),
673                          TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
674                         priv->can_request_st = TRUE;
675         }
676
677         if (!priv->can_request_ft && !priv->can_request_st)
678                 return ;
679
680         /* Update the capabilities of all contacts */
681         for (l = priv->contacts; l != NULL; l = g_list_next (l)) {
682                 EmpathyContact *contact = l->data;
683                 EmpathyCapabilities caps;
684
685                 caps = empathy_contact_get_capabilities (contact);
686
687                 if (priv->can_request_ft)
688                         caps |= EMPATHY_CAPABILITIES_FT;
689
690                 if (priv->can_request_st)
691                         caps |= EMPATHY_CAPABILITIES_STREAM_TUBE;
692
693                 empathy_contact_set_capabilities (contact, caps);
694         }
695 }
696
697 static void
698 tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
699                                                const gchar **mime_types,
700                                                guint         min_width,
701                                                guint         min_height,
702                                                guint         max_width,
703                                                guint         max_height,
704                                                guint         max_size,
705                                                const GError *error,
706                                                gpointer      user_data,
707                                                GObject      *tp_factory)
708 {
709         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
710
711         if (error) {
712                 DEBUG ("Failed to get avatar requirements: %s", error->message);
713                 /* We'll just leave avatar_mime_types as NULL; the
714                  * avatar-setting code can use this as a signal that you can't
715                  * set avatars.
716                  */
717         } else {
718                 priv->avatar_mime_types = g_strdupv ((gchar **) mime_types);
719                 priv->avatar_min_width = min_width;
720                 priv->avatar_min_height = min_height;
721                 priv->avatar_max_width = max_width;
722                 priv->avatar_max_height = max_height;
723                 priv->avatar_max_size = max_size;
724         }
725 }
726
727 static void
728 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
729                                 EmpathyContact          *contact)
730 {
731         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
732         TpHandle self_handle;
733         TpHandle handle;
734         GArray handles = {(gchar *) &handle, 1};
735         EmpathyCapabilities caps;
736
737         /* Keep a weak ref to that contact */
738         g_object_weak_ref (G_OBJECT (contact),
739                            tp_contact_factory_weak_notify,
740                            tp_factory);
741         priv->contacts = g_list_prepend (priv->contacts, contact);
742
743         /* The contact keeps a ref to its factory */
744         g_object_set_data_full (G_OBJECT (contact), "empathy-factory",
745                                 g_object_ref (tp_factory),
746                                 g_object_unref);
747
748         caps = empathy_contact_get_capabilities (contact);
749
750         /* Set the FT capability */
751         if (priv->can_request_ft) {
752                 caps |= EMPATHY_CAPABILITIES_FT;
753         }
754
755         /* Set the Stream Tube capability */
756         if (priv->can_request_st) {
757                 caps |= EMPATHY_CAPABILITIES_STREAM_TUBE;
758         }
759
760         empathy_contact_set_capabilities (contact, caps);
761
762         /* Set is-user property. Note that it could still be the handle is
763          * different from the connection's self handle, in the case the handle
764          * comes from a group interface. */
765         self_handle = tp_connection_get_self_handle (priv->connection);
766         handle = empathy_contact_get_handle (contact);
767         empathy_contact_set_is_user (contact, self_handle == handle);
768
769         /* FIXME: This should be done by TpContact */
770         if (tp_proxy_has_interface_by_id (priv->connection,
771                         TP_IFACE_QUARK_CONNECTION_INTERFACE_AVATARS)) {
772                 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (
773                         priv->connection, -1, &handles,
774                         tp_contact_factory_got_known_avatar_tokens, NULL, NULL,
775                         G_OBJECT (tp_factory));
776         }
777
778         if (tp_proxy_has_interface_by_id (priv->connection,
779                         TP_IFACE_QUARK_CONNECTION_INTERFACE_CAPABILITIES)) {
780                 tp_cli_connection_interface_capabilities_call_get_capabilities (
781                         priv->connection, -1, &handles,
782                         tp_contact_factory_got_capabilities, NULL, NULL,
783                         G_OBJECT (tp_factory));
784         }
785
786         if (tp_proxy_has_interface_by_id (TP_PROXY (priv->connection),
787                 TP_IFACE_QUARK_CONNECTION_INTERFACE_LOCATION)) {
788                 tp_cli_connection_interface_location_call_get_locations (priv->connection,
789                                                                          -1,
790                                                                          &handles,
791                                                                          tp_contact_factory_got_locations,
792                                                                          tp_factory,
793                                                                          NULL,
794                                                                          NULL);
795         }
796
797         DEBUG ("Contact added: %s (%d)",
798                 empathy_contact_get_id (contact),
799                 empathy_contact_get_handle (contact));
800 }
801
802 typedef union {
803         EmpathyTpContactFactoryContactsByIdCb ids_cb;
804         EmpathyTpContactFactoryContactsByHandleCb handles_cb;
805         EmpathyTpContactFactoryContactCb contact_cb;
806 } GetContactsCb;
807
808 typedef struct {
809         EmpathyTpContactFactory *tp_factory;
810         GetContactsCb callback;
811         gpointer user_data;
812         GDestroyNotify destroy;
813 } GetContactsData;
814
815 static void
816 get_contacts_data_free (gpointer user_data)
817 {
818         GetContactsData *data = user_data;
819
820         if (data->destroy) {
821                 data->destroy (data->user_data);
822         }
823         g_object_unref (data->tp_factory);
824
825         g_slice_free (GetContactsData, data);
826 }
827
828 static EmpathyContact *
829 dup_contact_for_tp_contact (EmpathyTpContactFactory *tp_factory,
830                             TpContact               *tp_contact)
831 {
832         EmpathyContact *contact;
833
834         contact = tp_contact_factory_find_by_tp_contact (tp_factory,
835                                                          tp_contact);
836
837         if (contact != NULL) {
838                 g_object_ref (contact);
839         } else {
840                 contact = empathy_contact_new (tp_contact);
841                 tp_contact_factory_add_contact (tp_factory, contact);
842         }
843
844         return contact;
845 }
846
847 static EmpathyContact **
848 contacts_array_new (EmpathyTpContactFactory *tp_factory,
849                     guint                    n_contacts,
850                     TpContact * const *      contacts)
851 {
852         EmpathyContact **ret;
853         guint            i;
854
855         ret = g_new0 (EmpathyContact *, n_contacts);
856         for (i = 0; i < n_contacts; i++) {
857                 ret[i] = dup_contact_for_tp_contact (tp_factory, contacts[i]);
858         }
859
860         return ret;
861 }
862
863 static void
864 contacts_array_free (guint            n_contacts,
865                      EmpathyContact **contacts)
866 {
867         guint i;
868
869         for (i = 0; i < n_contacts; i++) {
870                 g_object_unref (contacts[i]);
871         }
872         g_free (contacts);
873 }
874
875 static void
876 get_contacts_by_id_cb (TpConnection *connection,
877                        guint n_contacts,
878                        TpContact * const *contacts,
879                        const gchar * const *requested_ids,
880                        GHashTable *failed_id_errors,
881                        const GError *error,
882                        gpointer user_data,
883                        GObject *weak_object)
884 {
885         GetContactsData *data = user_data;
886         EmpathyContact **empathy_contacts;
887
888         empathy_contacts = contacts_array_new (data->tp_factory,
889                                                n_contacts, contacts);
890         if (data->callback.ids_cb) {
891                 data->callback.ids_cb (data->tp_factory,
892                                        n_contacts, empathy_contacts,
893                                        requested_ids,
894                                        failed_id_errors,
895                                        error,
896                                        data->user_data, weak_object);
897         }
898
899         contacts_array_free (n_contacts, empathy_contacts);
900 }
901
902 /* The callback is NOT given a reference to the EmpathyContact objects */
903 void
904 empathy_tp_contact_factory_get_from_ids (EmpathyTpContactFactory *tp_factory,
905                                          guint                    n_ids,
906                                          const gchar * const     *ids,
907                                          EmpathyTpContactFactoryContactsByIdCb callback,
908                                          gpointer                 user_data,
909                                          GDestroyNotify           destroy,
910                                          GObject                 *weak_object)
911 {
912         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
913         GetContactsData *data;
914
915         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
916         g_return_if_fail (ids != NULL);
917
918         data = g_slice_new (GetContactsData);
919         data->callback.ids_cb = callback;
920         data->user_data = user_data;
921         data->destroy = destroy;
922         data->tp_factory = g_object_ref (tp_factory);
923         tp_connection_get_contacts_by_id (priv->connection,
924                                           n_ids, ids,
925                                           G_N_ELEMENTS (contact_features),
926                                           contact_features,
927                                           get_contacts_by_id_cb,
928                                           data,
929                                           (GDestroyNotify) get_contacts_data_free,
930                                           weak_object);
931 }
932
933 static void
934 get_contact_by_id_cb (TpConnection *connection,
935                       guint n_contacts,
936                       TpContact * const *contacts,
937                       const gchar * const *requested_ids,
938                       GHashTable *failed_id_errors,
939                       const GError *error,
940                       gpointer user_data,
941                       GObject *weak_object)
942 {
943         GetContactsData *data = user_data;
944         EmpathyContact  *contact = NULL;
945
946         if (n_contacts == 1) {
947                 contact = dup_contact_for_tp_contact (data->tp_factory,
948                                                       contacts[0]);
949         }
950         else if (error == NULL) {
951                 GHashTableIter iter;
952                 gpointer       value;
953
954                 g_hash_table_iter_init (&iter, failed_id_errors);
955                 while (g_hash_table_iter_next (&iter, NULL, &value)) {
956                         if (value) {
957                                 error = value;
958                                 break;
959                         }
960                 }
961         }
962
963         if (data->callback.contact_cb) {
964                 data->callback.contact_cb (data->tp_factory,
965                                            contact,
966                                            error,
967                                            data->user_data, weak_object);
968         }
969
970         if (contact != NULL)
971                 g_object_unref (contact);
972 }
973
974 /* The callback is NOT given a reference to the EmpathyContact objects */
975 void
976 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
977                                         const gchar             *id,
978                                         EmpathyTpContactFactoryContactCb callback,
979                                         gpointer                 user_data,
980                                         GDestroyNotify           destroy,
981                                         GObject                 *weak_object)
982 {
983         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
984         GetContactsData *data;
985
986         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
987         g_return_if_fail (id != NULL);
988
989         data = g_slice_new (GetContactsData);
990         data->callback.contact_cb = callback;
991         data->user_data = user_data;
992         data->destroy = destroy;
993         data->tp_factory = g_object_ref (tp_factory);
994         tp_connection_get_contacts_by_id (priv->connection,
995                                           1, &id,
996                                           G_N_ELEMENTS (contact_features),
997                                           contact_features,
998                                           get_contact_by_id_cb,
999                                           data,
1000                                           (GDestroyNotify) get_contacts_data_free,
1001                                           weak_object);
1002 }
1003
1004 static void
1005 get_contacts_by_handle_cb (TpConnection *connection,
1006                            guint n_contacts,
1007                            TpContact * const *contacts,
1008                            guint n_failed,
1009                            const TpHandle *failed,
1010                            const GError *error,
1011                            gpointer user_data,
1012                            GObject *weak_object)
1013 {
1014         GetContactsData *data = user_data;
1015         EmpathyContact **empathy_contacts;
1016
1017         empathy_contacts = contacts_array_new (data->tp_factory,
1018                                                n_contacts, contacts);
1019         if (data->callback.handles_cb) {
1020                 data->callback.handles_cb (data->tp_factory,
1021                                            n_contacts, empathy_contacts,
1022                                            n_failed, failed,
1023                                            error,
1024                                            data->user_data, weak_object);
1025         }
1026
1027         contacts_array_free (n_contacts, empathy_contacts);
1028 }
1029
1030 /* The callback is NOT given a reference to the EmpathyContact objects */
1031 void
1032 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1033                                              guint n_handles,
1034                                              const TpHandle *handles,
1035                                              EmpathyTpContactFactoryContactsByHandleCb callback,
1036                                              gpointer                 user_data,
1037                                              GDestroyNotify           destroy,
1038                                              GObject                 *weak_object)
1039 {
1040         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1041         GetContactsData *data;
1042
1043         if (n_handles == 0) {
1044                 callback (tp_factory, 0, NULL, 0, NULL, NULL, user_data, weak_object);
1045                 return;
1046         }
1047
1048         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1049         g_return_if_fail (handles != NULL);
1050
1051         data = g_slice_new (GetContactsData);
1052         data->callback.handles_cb = callback;
1053         data->user_data = user_data;
1054         data->destroy = destroy;
1055         data->tp_factory = g_object_ref (tp_factory);
1056         tp_connection_get_contacts_by_handle (priv->connection,
1057                                               n_handles, handles,
1058                                               G_N_ELEMENTS (contact_features),
1059                                               contact_features,
1060                                               get_contacts_by_handle_cb,
1061                                               data,
1062                                               (GDestroyNotify) get_contacts_data_free,
1063                                               weak_object);
1064 }
1065
1066 /* The callback is NOT given a reference to the EmpathyContact objects */
1067 static void
1068 get_contact_by_handle_cb (TpConnection *connection,
1069                           guint n_contacts,
1070                           TpContact * const *contacts,
1071                           guint n_failed,
1072                           const TpHandle *failed,
1073                           const GError *error,
1074                           gpointer user_data,
1075                           GObject *weak_object)
1076 {
1077         GetContactsData *data = user_data;
1078         EmpathyContact  *contact = NULL;
1079         GError *err = NULL;
1080
1081         if (n_contacts == 1) {
1082                 contact = dup_contact_for_tp_contact (data->tp_factory,
1083                                                       contacts[0]);
1084         }
1085         else {
1086                 if (error == NULL) {
1087                         /* tp-glib will provide an error only if the whole operation failed,
1088                          * but not if, for example, the handle was invalid. We create an error
1089                          * so the caller of empathy_tp_contact_factory_get_from_handle can
1090                          * rely on the error to check if the operation succeeded or not. */
1091
1092                         err = g_error_new_literal (TP_ERRORS, TP_ERROR_INVALID_HANDLE,
1093                                                       "handle is invalid");
1094                 }
1095                 else {
1096                         err = g_error_copy (error);
1097                 }
1098         }
1099
1100         if (data->callback.contact_cb) {
1101                 data->callback.contact_cb (data->tp_factory,
1102                                            contact,
1103                                            err,
1104                                            data->user_data, weak_object);
1105         }
1106
1107         g_clear_error (&err);
1108         if (contact != NULL)
1109                 g_object_unref (contact);
1110 }
1111
1112 void
1113 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1114                                             TpHandle                 handle,
1115                                             EmpathyTpContactFactoryContactCb callback,
1116                                             gpointer                 user_data,
1117                                             GDestroyNotify           destroy,
1118                                             GObject                 *weak_object)
1119 {
1120         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1121         GetContactsData *data;
1122
1123         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1124
1125         data = g_slice_new (GetContactsData);
1126         data->callback.contact_cb = callback;
1127         data->user_data = user_data;
1128         data->destroy = destroy;
1129         data->tp_factory = g_object_ref (tp_factory);
1130         tp_connection_get_contacts_by_handle (priv->connection,
1131                                               1, &handle,
1132                                               G_N_ELEMENTS (contact_features),
1133                                               contact_features,
1134                                               get_contact_by_handle_cb,
1135                                               data,
1136                                               (GDestroyNotify) get_contacts_data_free,
1137                                               weak_object);
1138 }
1139
1140 void
1141 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1142                                       EmpathyContact          *contact,
1143                                       const gchar             *alias)
1144 {
1145         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1146         GHashTable                  *new_alias;
1147         guint                        handle;
1148
1149         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1150         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1151
1152         handle = empathy_contact_get_handle (contact);
1153
1154         DEBUG ("Setting alias for contact %s (%d) to %s",
1155                 empathy_contact_get_id (contact),
1156                 handle, alias);
1157
1158         new_alias = g_hash_table_new_full (g_direct_hash,
1159                                            g_direct_equal,
1160                                            NULL,
1161                                            g_free);
1162
1163         g_hash_table_insert (new_alias,
1164                              GUINT_TO_POINTER (handle),
1165                              g_strdup (alias));
1166
1167         tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1168                                                                -1,
1169                                                                new_alias,
1170                                                                tp_contact_factory_set_aliases_cb,
1171                                                                NULL, NULL,
1172                                                                G_OBJECT (tp_factory));
1173
1174         g_hash_table_destroy (new_alias);
1175 }
1176
1177 void
1178 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1179                                        const gchar             *data,
1180                                        gsize                    size,
1181                                        const gchar             *mime_type)
1182 {
1183         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1184
1185         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1186
1187         if (data && size > 0 && size < G_MAXUINT) {
1188                 GArray avatar;
1189
1190                 avatar.data = (gchar *) data;
1191                 avatar.len = size;
1192
1193                 DEBUG ("Setting avatar on connection %s",
1194                         tp_proxy_get_object_path (TP_PROXY (priv->connection)));
1195
1196                 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1197                                                                      -1,
1198                                                                      &avatar,
1199                                                                      mime_type,
1200                                                                      tp_contact_factory_set_avatar_cb,
1201                                                                      NULL, NULL,
1202                                                                      G_OBJECT (tp_factory));
1203         } else {
1204                 DEBUG ("Clearing avatar on connection %s",
1205                         tp_proxy_get_object_path (TP_PROXY (priv->connection)));
1206
1207                 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1208                                                                        -1,
1209                                                                        tp_contact_factory_clear_avatar_cb,
1210                                                                        NULL, NULL,
1211                                                                        G_OBJECT (tp_factory));
1212         }
1213 }
1214
1215 void
1216 empathy_tp_contact_factory_set_location (EmpathyTpContactFactory *tp_factory,
1217                                          GHashTable              *location)
1218 {
1219         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1220
1221         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1222
1223         DEBUG ("Setting location");
1224
1225         tp_cli_connection_interface_location_call_set_location (priv->connection,
1226                                                                  -1,
1227                                                                  location,
1228                                                                  tp_contact_factory_set_location_cb,
1229                                                                  NULL, NULL,
1230                                                                  G_OBJECT (tp_factory));
1231 }
1232
1233 static void
1234 tp_contact_factory_get_property (GObject    *object,
1235                                  guint       param_id,
1236                                  GValue     *value,
1237                                  GParamSpec *pspec)
1238 {
1239         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1240
1241         switch (param_id) {
1242         case PROP_CONNECTION:
1243                 g_value_set_object (value, priv->connection);
1244                 break;
1245         case PROP_MIME_TYPES:
1246                 g_value_set_boxed (value, priv->avatar_mime_types);
1247                 break;
1248         case PROP_MIN_WIDTH:
1249                 g_value_set_uint (value, priv->avatar_min_width);
1250                 break;
1251         case PROP_MIN_HEIGHT:
1252                 g_value_set_uint (value, priv->avatar_min_height);
1253                 break;
1254         case PROP_MAX_WIDTH:
1255                 g_value_set_uint (value, priv->avatar_max_width);
1256                 break;
1257         case PROP_MAX_HEIGHT:
1258                 g_value_set_uint (value, priv->avatar_max_height);
1259                 break;
1260         case PROP_MAX_SIZE:
1261                 g_value_set_uint (value, priv->avatar_max_size);
1262                 break;
1263         default:
1264                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1265                 break;
1266         };
1267 }
1268
1269 static void
1270 tp_contact_factory_set_property (GObject      *object,
1271                                  guint         param_id,
1272                                  const GValue *value,
1273                                  GParamSpec   *pspec)
1274 {
1275         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1276
1277         switch (param_id) {
1278         case PROP_CONNECTION:
1279                 priv->connection = g_value_dup_object (value);
1280                 break;
1281         default:
1282                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1283                 break;
1284         };
1285 }
1286
1287 static void
1288 tp_contact_factory_finalize (GObject *object)
1289 {
1290         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1291         GList                       *l;
1292
1293         DEBUG ("Finalized: %p", object);
1294
1295         for (l = priv->contacts; l; l = l->next) {
1296                 g_object_weak_unref (G_OBJECT (l->data),
1297                                      tp_contact_factory_weak_notify,
1298                                      object);
1299         }
1300
1301         g_list_free (priv->contacts);
1302
1303         g_object_unref (priv->connection);
1304
1305         g_strfreev (priv->avatar_mime_types);
1306
1307         G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1308 }
1309
1310 static void
1311 connection_ready_cb (TpConnection *connection,
1312                                 const GError *error,
1313                                 gpointer user_data)
1314 {
1315         EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (user_data);
1316         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1317
1318         if (error != NULL)
1319                 goto out;
1320
1321         /* FIXME: This should be moved to TpContact */
1322         tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
1323                                                                        tp_contact_factory_avatar_updated_cb,
1324                                                                        NULL, NULL,
1325                                                                        G_OBJECT (tp_factory),
1326                                                                        NULL);
1327         tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
1328                                                                          tp_contact_factory_avatar_retrieved_cb,
1329                                                                          NULL, NULL,
1330                                                                          G_OBJECT (tp_factory),
1331                                                                          NULL);
1332         tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
1333                                                                                   tp_contact_factory_capabilities_changed_cb,
1334                                                                                   NULL, NULL,
1335                                                                                   G_OBJECT (tp_factory),
1336                                                                                   NULL);
1337
1338
1339         tp_cli_connection_interface_location_connect_to_location_updated (priv->connection,
1340                                                                            tp_contact_factory_location_updated_cb,
1341                                                                            NULL, NULL,
1342                                                                            G_OBJECT (tp_factory),
1343                                                                            NULL);
1344
1345
1346         tp_cli_connection_interface_avatars_call_get_avatar_requirements (priv->connection,
1347                                                                           -1,
1348                                                                           tp_contact_factory_got_avatar_requirements_cb,
1349                                                                           NULL, NULL,
1350                                                                           G_OBJECT (tp_factory));
1351
1352         tp_cli_dbus_properties_call_get (priv->connection, -1,
1353                 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
1354                 "RequestableChannelClasses",
1355                 get_requestable_channel_classes_cb, NULL, NULL,
1356                 G_OBJECT (tp_factory));
1357
1358 out:
1359         g_object_unref (tp_factory);
1360 }
1361
1362 static GObject *
1363 tp_contact_factory_constructor (GType                  type,
1364                                 guint                  n_props,
1365                                 GObjectConstructParam *props)
1366 {
1367         GObject *tp_factory;
1368         EmpathyTpContactFactoryPriv *priv;
1369
1370         tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1371         priv = GET_PRIV (tp_factory);
1372
1373         /* Ensure to keep the self object alive while the call_when_ready is
1374          * running */
1375         g_object_ref (tp_factory);
1376         tp_connection_call_when_ready (priv->connection, connection_ready_cb,
1377                 tp_factory);
1378
1379         return tp_factory;
1380 }
1381
1382 static void
1383 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1384 {
1385         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1386
1387         object_class->finalize = tp_contact_factory_finalize;
1388         object_class->constructor = tp_contact_factory_constructor;
1389         object_class->get_property = tp_contact_factory_get_property;
1390         object_class->set_property = tp_contact_factory_set_property;
1391
1392         g_object_class_install_property (object_class,
1393                                          PROP_CONNECTION,
1394                                          g_param_spec_object ("connection",
1395                                                               "Factory's Connection",
1396                                                               "The connection associated with the factory",
1397                                                               TP_TYPE_CONNECTION,
1398                                                               G_PARAM_READWRITE |
1399                                                               G_PARAM_CONSTRUCT_ONLY |
1400                                                               G_PARAM_STATIC_STRINGS));
1401         g_object_class_install_property (object_class,
1402                                          PROP_MIME_TYPES,
1403                                          g_param_spec_boxed ("avatar-mime-types",
1404                                                              "Supported MIME types for avatars",
1405                                                              "Types of images that may be set as "
1406                                                              "avatars on this connection.",
1407                                                              G_TYPE_STRV,
1408                                                              G_PARAM_READABLE |
1409                                                              G_PARAM_STATIC_STRINGS));
1410         g_object_class_install_property (object_class,
1411                                          PROP_MIN_WIDTH,
1412                                          g_param_spec_uint ("avatar-min-width",
1413                                                             "Minimum width for avatars",
1414                                                             "Minimum width of avatar that may be set.",
1415                                                             0,
1416                                                             G_MAXUINT,
1417                                                             0,
1418                                                             G_PARAM_READABLE |
1419                                                             G_PARAM_STATIC_STRINGS));
1420         g_object_class_install_property (object_class,
1421                                          PROP_MIN_HEIGHT,
1422                                          g_param_spec_uint ("avatar-min-height",
1423                                                             "Minimum height for avatars",
1424                                                             "Minimum height of avatar that may be set.",
1425                                                             0,
1426                                                             G_MAXUINT,
1427                                                             0,
1428                                                             G_PARAM_READABLE |
1429                                                             G_PARAM_STATIC_STRINGS));
1430         g_object_class_install_property (object_class,
1431                                          PROP_MAX_WIDTH,
1432                                          g_param_spec_uint ("avatar-max-width",
1433                                                             "Maximum width for avatars",
1434                                                             "Maximum width of avatar that may be set "
1435                                                             "or 0 if there is no maximum.",
1436                                                             0,
1437                                                             G_MAXUINT,
1438                                                             0,
1439                                                             G_PARAM_READABLE |
1440                                                             G_PARAM_STATIC_STRINGS));
1441         g_object_class_install_property (object_class,
1442                                          PROP_MAX_HEIGHT,
1443                                          g_param_spec_uint ("avatar-max-height",
1444                                                             "Maximum height for avatars",
1445                                                             "Maximum height of avatar that may be set "
1446                                                             "or 0 if there is no maximum.",
1447                                                             0,
1448                                                             G_MAXUINT,
1449                                                             0,
1450                                                             G_PARAM_READABLE |
1451                                                             G_PARAM_STATIC_STRINGS));
1452         g_object_class_install_property (object_class,
1453                                          PROP_MAX_SIZE,
1454                                          g_param_spec_uint ("avatar-max-size",
1455                                                             "Maximum size for avatars in bytes",
1456                                                             "Maximum file size of avatar that may be "
1457                                                             "set or 0 if there is no maximum.",
1458                                                             0,
1459                                                             G_MAXUINT,
1460                                                             0,
1461                                                             G_PARAM_READABLE |
1462                                                             G_PARAM_STATIC_STRINGS));
1463
1464
1465         g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1466 }
1467
1468 static void
1469 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1470 {
1471         EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1472                 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1473
1474         tp_factory->priv = priv;
1475         priv->can_request_ft = FALSE;
1476         priv->can_request_st = FALSE;
1477 }
1478
1479 static GHashTable *factories = NULL;
1480
1481 static void
1482 tp_contact_factory_connection_invalidated_cb (TpProxy *connection,
1483                                               guint    domain,
1484                                               gint     code,
1485                                               gchar   *message,
1486                                               gpointer user_data)
1487 {
1488         DEBUG ("Message: %s", message);
1489         g_hash_table_remove (factories, connection);
1490 }
1491
1492 static void
1493 tp_contact_factory_connection_weak_notify_cb (gpointer connection,
1494                                               GObject *where_the_object_was)
1495 {
1496         g_hash_table_remove (factories, connection);
1497 }
1498
1499 static void
1500 tp_contact_factory_remove_connection (gpointer connection)
1501 {
1502         g_signal_handlers_disconnect_by_func (connection,
1503                 tp_contact_factory_connection_invalidated_cb, NULL);
1504         g_object_unref (connection);
1505 }
1506
1507 EmpathyTpContactFactory *
1508 empathy_tp_contact_factory_dup_singleton (TpConnection *connection)
1509 {
1510         EmpathyTpContactFactory *tp_factory;
1511
1512         g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1513
1514         if (factories == NULL) {
1515                 factories = g_hash_table_new_full (empathy_proxy_hash,
1516                                                    empathy_proxy_equal,
1517                                                    tp_contact_factory_remove_connection,
1518                                                    NULL);
1519         }
1520
1521         tp_factory = g_hash_table_lookup (factories, connection);
1522         if (tp_factory == NULL) {
1523                 tp_factory = g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,
1524                                            "connection", connection,
1525                                            NULL);
1526                 g_hash_table_insert (factories, g_object_ref (connection),
1527                                      tp_factory);
1528                 g_object_weak_ref (G_OBJECT (tp_factory),
1529                                    tp_contact_factory_connection_weak_notify_cb,
1530                                    connection);
1531                 g_signal_connect (connection, "invalidated",
1532                                   G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
1533                                   NULL);
1534         } else {
1535                 g_object_ref (tp_factory);
1536         }
1537
1538         return tp_factory;
1539 }
1540