]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-factory.c
Add empathy_idle_account_is_just_connected function.
[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 void
903 empathy_tp_contact_factory_get_from_ids (EmpathyTpContactFactory *tp_factory,
904                                          guint                    n_ids,
905                                          const gchar * const     *ids,
906                                          EmpathyTpContactFactoryContactsByIdCb callback,
907                                          gpointer                 user_data,
908                                          GDestroyNotify           destroy,
909                                          GObject                 *weak_object)
910 {
911         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
912         GetContactsData *data;
913
914         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
915         g_return_if_fail (ids != NULL);
916
917         data = g_slice_new (GetContactsData);
918         data->callback.ids_cb = callback;
919         data->user_data = user_data;
920         data->destroy = destroy;
921         data->tp_factory = g_object_ref (tp_factory);
922         tp_connection_get_contacts_by_id (priv->connection,
923                                           n_ids, ids,
924                                           G_N_ELEMENTS (contact_features),
925                                           contact_features,
926                                           get_contacts_by_id_cb,
927                                           data,
928                                           (GDestroyNotify) get_contacts_data_free,
929                                           weak_object);
930 }
931
932 static void
933 get_contact_by_id_cb (TpConnection *connection,
934                       guint n_contacts,
935                       TpContact * const *contacts,
936                       const gchar * const *requested_ids,
937                       GHashTable *failed_id_errors,
938                       const GError *error,
939                       gpointer user_data,
940                       GObject *weak_object)
941 {
942         GetContactsData *data = user_data;
943         EmpathyContact  *contact = NULL;
944
945         if (n_contacts == 1) {
946                 contact = dup_contact_for_tp_contact (data->tp_factory,
947                                                       contacts[0]);
948         }
949         else if (error == NULL) {
950                 GHashTableIter iter;
951                 gpointer       value;
952
953                 g_hash_table_iter_init (&iter, failed_id_errors);
954                 while (g_hash_table_iter_next (&iter, NULL, &value)) {
955                         if (value) {
956                                 error = value;
957                                 break;
958                         }
959                 }
960         }
961
962         if (data->callback.contact_cb) {
963                 data->callback.contact_cb (data->tp_factory,
964                                            contact,
965                                            error,
966                                            data->user_data, weak_object);
967         }
968 }
969
970 void
971 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
972                                         const gchar             *id,
973                                         EmpathyTpContactFactoryContactCb callback,
974                                         gpointer                 user_data,
975                                         GDestroyNotify           destroy,
976                                         GObject                 *weak_object)
977 {
978         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
979         GetContactsData *data;
980
981         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
982         g_return_if_fail (id != NULL);
983
984         data = g_slice_new (GetContactsData);
985         data->callback.contact_cb = callback;
986         data->user_data = user_data;
987         data->destroy = destroy;
988         data->tp_factory = g_object_ref (tp_factory);
989         tp_connection_get_contacts_by_id (priv->connection,
990                                           1, &id,
991                                           G_N_ELEMENTS (contact_features),
992                                           contact_features,
993                                           get_contact_by_id_cb,
994                                           data,
995                                           (GDestroyNotify) get_contacts_data_free,
996                                           weak_object);
997 }
998
999 static void
1000 get_contacts_by_handle_cb (TpConnection *connection,
1001                            guint n_contacts,
1002                            TpContact * const *contacts,
1003                            guint n_failed,
1004                            const TpHandle *failed,
1005                            const GError *error,
1006                            gpointer user_data,
1007                            GObject *weak_object)
1008 {
1009         GetContactsData *data = user_data;
1010         EmpathyContact **empathy_contacts;
1011
1012         empathy_contacts = contacts_array_new (data->tp_factory,
1013                                                n_contacts, contacts);
1014         if (data->callback.handles_cb) {
1015                 data->callback.handles_cb (data->tp_factory,
1016                                            n_contacts, empathy_contacts,
1017                                            n_failed, failed,
1018                                            error,
1019                                            data->user_data, weak_object);
1020         }
1021
1022         contacts_array_free (n_contacts, empathy_contacts);
1023 }
1024
1025 void
1026 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1027                                              guint n_handles,
1028                                              const TpHandle *handles,
1029                                              EmpathyTpContactFactoryContactsByHandleCb callback,
1030                                              gpointer                 user_data,
1031                                              GDestroyNotify           destroy,
1032                                              GObject                 *weak_object)
1033 {
1034         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1035         GetContactsData *data;
1036
1037         if (n_handles == 0) {
1038                 callback (tp_factory, 0, NULL, 0, NULL, NULL, user_data, weak_object);
1039                 return;
1040         }
1041
1042         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1043         g_return_if_fail (handles != NULL);
1044
1045         data = g_slice_new (GetContactsData);
1046         data->callback.handles_cb = callback;
1047         data->user_data = user_data;
1048         data->destroy = destroy;
1049         data->tp_factory = g_object_ref (tp_factory);
1050         tp_connection_get_contacts_by_handle (priv->connection,
1051                                               n_handles, handles,
1052                                               G_N_ELEMENTS (contact_features),
1053                                               contact_features,
1054                                               get_contacts_by_handle_cb,
1055                                               data,
1056                                               (GDestroyNotify) get_contacts_data_free,
1057                                               weak_object);
1058 }
1059
1060 static void
1061 get_contact_by_handle_cb (TpConnection *connection,
1062                           guint n_contacts,
1063                           TpContact * const *contacts,
1064                           guint n_failed,
1065                           const TpHandle *failed,
1066                           const GError *error,
1067                           gpointer user_data,
1068                           GObject *weak_object)
1069 {
1070         GetContactsData *data = user_data;
1071         EmpathyContact  *contact = NULL;
1072
1073         if (n_contacts == 1) {
1074                 contact = dup_contact_for_tp_contact (data->tp_factory,
1075                                                       contacts[0]);
1076         }
1077
1078         if (data->callback.contact_cb) {
1079                 data->callback.contact_cb (data->tp_factory,
1080                                            contact,
1081                                            error,
1082                                            data->user_data, weak_object);
1083         }
1084 }
1085
1086 void
1087 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1088                                             TpHandle                 handle,
1089                                             EmpathyTpContactFactoryContactCb callback,
1090                                             gpointer                 user_data,
1091                                             GDestroyNotify           destroy,
1092                                             GObject                 *weak_object)
1093 {
1094         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1095         GetContactsData *data;
1096
1097         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1098
1099         data = g_slice_new (GetContactsData);
1100         data->callback.contact_cb = callback;
1101         data->user_data = user_data;
1102         data->destroy = destroy;
1103         data->tp_factory = g_object_ref (tp_factory);
1104         tp_connection_get_contacts_by_handle (priv->connection,
1105                                               1, &handle,
1106                                               G_N_ELEMENTS (contact_features),
1107                                               contact_features,
1108                                               get_contact_by_handle_cb,
1109                                               data,
1110                                               (GDestroyNotify) get_contacts_data_free,
1111                                               weak_object);
1112 }
1113
1114 void
1115 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1116                                       EmpathyContact          *contact,
1117                                       const gchar             *alias)
1118 {
1119         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1120         GHashTable                  *new_alias;
1121         guint                        handle;
1122
1123         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1124         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1125
1126         handle = empathy_contact_get_handle (contact);
1127
1128         DEBUG ("Setting alias for contact %s (%d) to %s",
1129                 empathy_contact_get_id (contact),
1130                 handle, alias);
1131
1132         new_alias = g_hash_table_new_full (g_direct_hash,
1133                                            g_direct_equal,
1134                                            NULL,
1135                                            g_free);
1136
1137         g_hash_table_insert (new_alias,
1138                              GUINT_TO_POINTER (handle),
1139                              g_strdup (alias));
1140
1141         tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1142                                                                -1,
1143                                                                new_alias,
1144                                                                tp_contact_factory_set_aliases_cb,
1145                                                                NULL, NULL,
1146                                                                G_OBJECT (tp_factory));
1147
1148         g_hash_table_destroy (new_alias);
1149 }
1150
1151 void
1152 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1153                                        const gchar             *data,
1154                                        gsize                    size,
1155                                        const gchar             *mime_type)
1156 {
1157         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1158
1159         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1160
1161         if (data && size > 0 && size < G_MAXUINT) {
1162                 GArray avatar;
1163
1164                 avatar.data = (gchar *) data;
1165                 avatar.len = size;
1166
1167                 DEBUG ("Setting avatar on connection %s",
1168                         tp_proxy_get_object_path (TP_PROXY (priv->connection)));
1169
1170                 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1171                                                                      -1,
1172                                                                      &avatar,
1173                                                                      mime_type,
1174                                                                      tp_contact_factory_set_avatar_cb,
1175                                                                      NULL, NULL,
1176                                                                      G_OBJECT (tp_factory));
1177         } else {
1178                 DEBUG ("Clearing avatar on connection %s",
1179                         tp_proxy_get_object_path (TP_PROXY (priv->connection)));
1180
1181                 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1182                                                                        -1,
1183                                                                        tp_contact_factory_clear_avatar_cb,
1184                                                                        NULL, NULL,
1185                                                                        G_OBJECT (tp_factory));
1186         }
1187 }
1188
1189 void
1190 empathy_tp_contact_factory_set_location (EmpathyTpContactFactory *tp_factory,
1191                                          GHashTable              *location)
1192 {
1193         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1194
1195         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1196
1197         DEBUG ("Setting location");
1198
1199         tp_cli_connection_interface_location_call_set_location (priv->connection,
1200                                                                  -1,
1201                                                                  location,
1202                                                                  tp_contact_factory_set_location_cb,
1203                                                                  NULL, NULL,
1204                                                                  G_OBJECT (tp_factory));
1205 }
1206
1207 static void
1208 tp_contact_factory_get_property (GObject    *object,
1209                                  guint       param_id,
1210                                  GValue     *value,
1211                                  GParamSpec *pspec)
1212 {
1213         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1214
1215         switch (param_id) {
1216         case PROP_CONNECTION:
1217                 g_value_set_object (value, priv->connection);
1218                 break;
1219         case PROP_MIME_TYPES:
1220                 g_value_set_boxed (value, priv->avatar_mime_types);
1221                 break;
1222         case PROP_MIN_WIDTH:
1223                 g_value_set_uint (value, priv->avatar_min_width);
1224                 break;
1225         case PROP_MIN_HEIGHT:
1226                 g_value_set_uint (value, priv->avatar_min_height);
1227                 break;
1228         case PROP_MAX_WIDTH:
1229                 g_value_set_uint (value, priv->avatar_max_width);
1230                 break;
1231         case PROP_MAX_HEIGHT:
1232                 g_value_set_uint (value, priv->avatar_max_height);
1233                 break;
1234         case PROP_MAX_SIZE:
1235                 g_value_set_uint (value, priv->avatar_max_size);
1236                 break;
1237         default:
1238                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1239                 break;
1240         };
1241 }
1242
1243 static void
1244 tp_contact_factory_set_property (GObject      *object,
1245                                  guint         param_id,
1246                                  const GValue *value,
1247                                  GParamSpec   *pspec)
1248 {
1249         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1250
1251         switch (param_id) {
1252         case PROP_CONNECTION:
1253                 priv->connection = g_value_dup_object (value);
1254                 break;
1255         default:
1256                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1257                 break;
1258         };
1259 }
1260
1261 static void
1262 tp_contact_factory_finalize (GObject *object)
1263 {
1264         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1265         GList                       *l;
1266
1267         DEBUG ("Finalized: %p", object);
1268
1269         for (l = priv->contacts; l; l = l->next) {
1270                 g_object_weak_unref (G_OBJECT (l->data),
1271                                      tp_contact_factory_weak_notify,
1272                                      object);
1273         }
1274
1275         g_list_free (priv->contacts);
1276
1277         g_object_unref (priv->connection);
1278
1279         g_strfreev (priv->avatar_mime_types);
1280
1281         G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1282 }
1283
1284 static GObject *
1285 tp_contact_factory_constructor (GType                  type,
1286                                 guint                  n_props,
1287                                 GObjectConstructParam *props)
1288 {
1289         GObject *tp_factory;
1290         EmpathyTpContactFactoryPriv *priv;
1291
1292         tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1293         priv = GET_PRIV (tp_factory);
1294
1295         /* FIXME: This should be moved to TpContact */
1296         tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
1297                                                                        tp_contact_factory_avatar_updated_cb,
1298                                                                        NULL, NULL,
1299                                                                        tp_factory,
1300                                                                        NULL);
1301         tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
1302                                                                          tp_contact_factory_avatar_retrieved_cb,
1303                                                                          NULL, NULL,
1304                                                                          tp_factory,
1305                                                                          NULL);
1306         tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
1307                                                                                   tp_contact_factory_capabilities_changed_cb,
1308                                                                                   NULL, NULL,
1309                                                                                   tp_factory,
1310                                                                                   NULL);
1311
1312
1313         tp_cli_connection_interface_location_connect_to_location_updated (priv->connection,
1314                                                                            tp_contact_factory_location_updated_cb,
1315                                                                            NULL, NULL,
1316                                                                            G_OBJECT (tp_factory),
1317                                                                            NULL);
1318
1319         /* FIXME: This should be moved to TpConnection */
1320         tp_cli_connection_interface_avatars_call_get_avatar_requirements (priv->connection,
1321                                                                           -1,
1322                                                                           tp_contact_factory_got_avatar_requirements_cb,
1323                                                                           NULL, NULL,
1324                                                                           tp_factory);
1325         tp_cli_dbus_properties_call_get (priv->connection, -1,
1326                 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
1327                 "RequestableChannelClasses",
1328                 get_requestable_channel_classes_cb, NULL, NULL,
1329                 G_OBJECT (tp_factory));
1330
1331         return tp_factory;
1332 }
1333
1334 static void
1335 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1336 {
1337         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1338
1339         object_class->finalize = tp_contact_factory_finalize;
1340         object_class->constructor = tp_contact_factory_constructor;
1341         object_class->get_property = tp_contact_factory_get_property;
1342         object_class->set_property = tp_contact_factory_set_property;
1343
1344         g_object_class_install_property (object_class,
1345                                          PROP_CONNECTION,
1346                                          g_param_spec_object ("connection",
1347                                                               "Factory's Connection",
1348                                                               "The connection associated with the factory",
1349                                                               TP_TYPE_CONNECTION,
1350                                                               G_PARAM_READWRITE |
1351                                                               G_PARAM_CONSTRUCT_ONLY |
1352                                                               G_PARAM_STATIC_STRINGS));
1353         g_object_class_install_property (object_class,
1354                                          PROP_MIME_TYPES,
1355                                          g_param_spec_boxed ("avatar-mime-types",
1356                                                              "Supported MIME types for avatars",
1357                                                              "Types of images that may be set as "
1358                                                              "avatars on this connection.",
1359                                                              G_TYPE_STRV,
1360                                                              G_PARAM_READABLE |
1361                                                              G_PARAM_STATIC_STRINGS));
1362         g_object_class_install_property (object_class,
1363                                          PROP_MIN_WIDTH,
1364                                          g_param_spec_uint ("avatar-min-width",
1365                                                             "Minimum width for avatars",
1366                                                             "Minimum width of avatar that may be set.",
1367                                                             0,
1368                                                             G_MAXUINT,
1369                                                             0,
1370                                                             G_PARAM_READABLE |
1371                                                             G_PARAM_STATIC_STRINGS));
1372         g_object_class_install_property (object_class,
1373                                          PROP_MIN_HEIGHT,
1374                                          g_param_spec_uint ("avatar-min-height",
1375                                                             "Minimum height for avatars",
1376                                                             "Minimum height of avatar that may be set.",
1377                                                             0,
1378                                                             G_MAXUINT,
1379                                                             0,
1380                                                             G_PARAM_READABLE |
1381                                                             G_PARAM_STATIC_STRINGS));
1382         g_object_class_install_property (object_class,
1383                                          PROP_MAX_WIDTH,
1384                                          g_param_spec_uint ("avatar-max-width",
1385                                                             "Maximum width for avatars",
1386                                                             "Maximum width of avatar that may be set "
1387                                                             "or 0 if there is no maximum.",
1388                                                             0,
1389                                                             G_MAXUINT,
1390                                                             0,
1391                                                             G_PARAM_READABLE |
1392                                                             G_PARAM_STATIC_STRINGS));
1393         g_object_class_install_property (object_class,
1394                                          PROP_MAX_HEIGHT,
1395                                          g_param_spec_uint ("avatar-max-height",
1396                                                             "Maximum height for avatars",
1397                                                             "Maximum height of avatar that may be set "
1398                                                             "or 0 if there is no maximum.",
1399                                                             0,
1400                                                             G_MAXUINT,
1401                                                             0,
1402                                                             G_PARAM_READABLE |
1403                                                             G_PARAM_STATIC_STRINGS));
1404         g_object_class_install_property (object_class,
1405                                          PROP_MAX_SIZE,
1406                                          g_param_spec_uint ("avatar-max-size",
1407                                                             "Maximum size for avatars in bytes",
1408                                                             "Maximum file size of avatar that may be "
1409                                                             "set or 0 if there is no maximum.",
1410                                                             0,
1411                                                             G_MAXUINT,
1412                                                             0,
1413                                                             G_PARAM_READABLE |
1414                                                             G_PARAM_STATIC_STRINGS));
1415
1416
1417         g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1418 }
1419
1420 static void
1421 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1422 {
1423         EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1424                 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1425
1426         tp_factory->priv = priv;
1427         priv->can_request_ft = FALSE;
1428         priv->can_request_st = FALSE;
1429 }
1430
1431 static GHashTable *factories = NULL;
1432
1433 static void
1434 tp_contact_factory_connection_invalidated_cb (TpProxy *connection,
1435                                               guint    domain,
1436                                               gint     code,
1437                                               gchar   *message,
1438                                               gpointer user_data)
1439 {
1440         DEBUG ("Message: %s", message);
1441         g_hash_table_remove (factories, connection);
1442 }
1443
1444 static void
1445 tp_contact_factory_connection_weak_notify_cb (gpointer connection,
1446                                               GObject *where_the_object_was)
1447 {
1448         g_hash_table_remove (factories, connection);
1449 }
1450
1451 static void
1452 tp_contact_factory_remove_connection (gpointer connection)
1453 {
1454         g_signal_handlers_disconnect_by_func (connection,
1455                 tp_contact_factory_connection_invalidated_cb, NULL);
1456         g_object_unref (connection);
1457 }
1458
1459 EmpathyTpContactFactory *
1460 empathy_tp_contact_factory_dup_singleton (TpConnection *connection)
1461 {
1462         EmpathyTpContactFactory *tp_factory;
1463
1464         g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1465
1466         if (factories == NULL) {
1467                 factories = g_hash_table_new_full (empathy_proxy_hash,
1468                                                    empathy_proxy_equal,
1469                                                    tp_contact_factory_remove_connection,
1470                                                    NULL);
1471         }
1472
1473         tp_factory = g_hash_table_lookup (factories, connection);
1474         if (tp_factory == NULL) {
1475                 tp_factory = g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,
1476                                            "connection", connection,
1477                                            NULL);
1478                 g_hash_table_insert (factories, g_object_ref (connection),
1479                                      tp_factory);
1480                 g_object_weak_ref (G_OBJECT (tp_factory),
1481                                    tp_contact_factory_connection_weak_notify_cb,
1482                                    connection);
1483                 g_signal_connect (connection, "invalidated",
1484                                   G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
1485                                   NULL);
1486         } else {
1487                 g_object_ref (tp_factory);
1488         }
1489
1490         return tp_factory;
1491 }
1492