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