]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-factory.c
Add G_PARAM_STATIC_STRINGS to TpContactFactory properties
[empathy.git] / libempathy / empathy-tp-contact-factory.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  * 
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <telepathy-glib/util.h>
27 #include <telepathy-glib/connection.h>
28 #include <libmissioncontrol/mission-control.h>
29
30 #include "empathy-tp-contact-factory.h"
31 #include "empathy-utils.h"
32
33 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
34 #include "empathy-debug.h"
35
36 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactFactory)
37 typedef struct {
38         MissionControl *mc;
39         McAccount      *account;
40         TpConnection   *connection;
41         gboolean        ready;
42
43         GList          *contacts;
44         EmpathyContact *user;
45         gpointer        token;
46
47         gchar         **avatar_mime_types;
48         guint           avatar_min_width;
49         guint           avatar_min_height;
50         guint           avatar_max_width;
51         guint           avatar_max_height;
52         guint           avatar_max_size;
53 } EmpathyTpContactFactoryPriv;
54
55 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
56
57 enum {
58         PROP_0,
59         PROP_ACCOUNT,
60         PROP_READY,
61
62         PROP_MIME_TYPES,
63         PROP_MIN_WIDTH,
64         PROP_MIN_HEIGHT,
65         PROP_MAX_WIDTH,
66         PROP_MAX_HEIGHT,
67         PROP_MAX_SIZE
68 };
69
70 /* Prototypes */
71 static void tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
72                                                            const gchar **mime_types,
73                                                            guint         min_width,
74                                                            guint         min_height,
75                                                            guint         max_width,
76                                                            guint         max_height,
77                                                            guint         max_size,
78                                                            const GError *error,
79                                                            gpointer      user_data,
80                                                            GObject      *tp_factory);
81 static void tp_contact_factory_ready (EmpathyTpContactFactory *tp_factory);
82
83
84 static EmpathyContact *
85 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
86                                    guint                    handle)
87 {
88         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
89         GList                       *l;
90
91         for (l = priv->contacts; l; l = l->next) {
92                 if (empathy_contact_get_handle (l->data) == handle) {
93                         return l->data;
94                 }
95         }
96
97         return NULL;
98 }
99
100 static EmpathyContact *
101 tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
102                                const gchar             *id)
103 {
104         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
105         GList                       *l;
106
107         for (l = priv->contacts; l; l = l->next) {
108                 if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
109                         return l->data;
110                 }
111         }
112
113         return NULL;
114 }
115
116 static void
117 tp_contact_factory_weak_notify (gpointer data,
118                                 GObject *where_the_object_was)
119 {
120         EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
121
122         DEBUG ("Remove finalized contact %p", where_the_object_was);
123
124         priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
125 }
126
127 static void
128 tp_contact_factory_presences_table_foreach (const gchar    *state_str,
129                                             GHashTable     *presences_table,
130                                             EmpathyContact *contact)
131 {
132         const GValue *message;
133         const gchar  *message_str = NULL;
134
135         empathy_contact_set_presence (contact,
136                                       empathy_presence_from_str (state_str));
137         
138         message = g_hash_table_lookup (presences_table, "message");
139         if (message) {
140                 message_str = g_value_get_string (message);
141         }
142
143         if (!G_STR_EMPTY (message_str)) {
144                 empathy_contact_set_presence_message (contact, message_str);
145         } else {
146                 empathy_contact_set_presence_message (contact, NULL);
147         }
148 }
149
150 static void
151 tp_contact_factory_parse_presence_foreach (guint                    handle,
152                                            GValueArray             *presence_struct,
153                                            EmpathyTpContactFactory *tp_factory)
154 {
155         GHashTable      *presences_table;
156         EmpathyContact  *contact;
157
158         contact = tp_contact_factory_find_by_handle (tp_factory, handle);
159         if (!contact) {
160                 return;
161         }
162
163         presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
164
165         g_hash_table_foreach (presences_table,
166                               (GHFunc) tp_contact_factory_presences_table_foreach,
167                               contact);
168
169         DEBUG ("Changing presence for contact %s (%d) to '%s' (%d)",
170                 empathy_contact_get_id (contact),
171                 handle,
172                 empathy_contact_get_presence_message (contact),
173                 empathy_contact_get_presence (contact));
174 }
175
176 static void
177 tp_contact_factory_get_presence_cb (TpConnection *connection,
178                                     GHashTable   *handle_table,
179                                     const GError *error,
180                                     gpointer      user_data,
181                                     GObject      *tp_factory)
182 {
183         if (error) {
184                 DEBUG ("Error getting presence: %s", error->message);
185                 if (error->domain == TP_DBUS_ERRORS &&
186                     error->code == TP_DBUS_ERROR_NO_INTERFACE) {
187                         guint *handles = user_data;
188
189                         /* We have no presence iface, set default presence
190                          * to available */
191                         while (*handles != 0) {
192                                 EmpathyContact *contact;
193
194                                 contact = tp_contact_factory_find_by_handle (
195                                         (EmpathyTpContactFactory*) tp_factory,
196                                         *handles);
197                                 if (contact) {
198                                         empathy_contact_set_presence (contact,
199                                                                       MC_PRESENCE_AVAILABLE);
200                                 }
201
202                                 handles++;
203                         }
204                 }
205
206                 return;
207         }
208
209         g_hash_table_foreach (handle_table,
210                               (GHFunc) tp_contact_factory_parse_presence_foreach,
211                               EMPATHY_TP_CONTACT_FACTORY (tp_factory));
212 }
213
214 static void
215 tp_contact_factory_presence_update_cb (TpConnection *connection,
216                                        GHashTable   *handle_table,
217                                        gpointer      user_data,
218                                        GObject      *tp_factory)
219 {
220         g_hash_table_foreach (handle_table,
221                               (GHFunc) tp_contact_factory_parse_presence_foreach,
222                               EMPATHY_TP_CONTACT_FACTORY (tp_factory));
223 }
224
225 static void
226 tp_contact_factory_set_aliases_cb (TpConnection *connection,
227                                    const GError *error,
228                                    gpointer      user_data,
229                                    GObject      *tp_factory)
230 {
231         if (error) {
232                 DEBUG ("Error setting alias: %s", error->message);
233         }
234 }
235
236 static void
237 tp_contact_factory_request_aliases_cb (TpConnection *connection,
238                                        const gchar  **contact_names,
239                                        const GError  *error,
240                                        gpointer       user_data,
241                                        GObject       *tp_factory)
242 {
243         guint        *handles = user_data;
244         guint         i = 0;
245         const gchar **name;
246
247         if (error) {
248                 DEBUG ("Error requesting aliases: %s", error->message);
249
250                 /* If we failed to get alias set it to NULL, like that if
251                  * someone is waiting for the name to be ready it won't wait
252                  * infinitely */
253                 while (*handles != 0) {
254                         EmpathyContact *contact;
255
256                         contact = tp_contact_factory_find_by_handle (
257                                 (EmpathyTpContactFactory*) tp_factory,
258                                 *handles);
259                         if (contact) {
260                                 empathy_contact_set_name (contact, NULL);
261                         }
262
263                         handles++;
264                 }
265                 return;
266         }
267
268         for (name = contact_names; *name; name++) {
269                 EmpathyContact *contact;
270
271                 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
272                                                              handles[i]);
273                 if (!contact) {
274                         continue;
275                 }
276
277                 DEBUG ("Renaming contact %s (%d) to %s (request cb)",
278                         empathy_contact_get_id (contact),
279                         empathy_contact_get_handle (contact),
280                         *name);
281
282                 empathy_contact_set_name (contact, *name);
283
284                 i++;
285         }
286 }
287
288 static void
289 tp_contact_factory_aliases_changed_cb (TpConnection    *connection,
290                                        const GPtrArray *renamed_handlers,
291                                        gpointer         user_data,
292                                        GObject         *weak_object)
293 {
294         EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
295         guint                    i;
296
297         for (i = 0; renamed_handlers->len > i; i++) {
298                 guint           handle;
299                 const gchar    *alias;
300                 GValueArray    *renamed_struct;
301                 EmpathyContact *contact;
302
303                 renamed_struct = g_ptr_array_index (renamed_handlers, i);
304                 handle = g_value_get_uint (g_value_array_get_nth (renamed_struct, 0));
305                 alias = g_value_get_string (g_value_array_get_nth (renamed_struct, 1));
306                 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
307
308                 if (!contact) {
309                         /* We don't know this contact, skip */
310                         continue;
311                 }
312
313                 DEBUG ("Renaming contact %s (%d) to %s (changed cb)",
314                         empathy_contact_get_id (contact),
315                         handle, alias);
316
317                 empathy_contact_set_name (contact, alias);
318         }
319 }
320
321 static void
322 tp_contact_factory_set_avatar_cb (TpConnection *connection,
323                                   const gchar  *token,
324                                   const GError *error,
325                                   gpointer      user_data,
326                                   GObject      *tp_factory)
327 {
328         if (error) {
329                 DEBUG ("Error setting avatar: %s", error->message);
330         }
331 }
332
333 static void
334 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
335                                     const GError *error,
336                                     gpointer      user_data,
337                                     GObject      *tp_factory)
338 {
339         if (error) {
340                 DEBUG ("Error clearing avatar: %s", error->message);
341         }
342 }
343
344 static void
345 tp_contact_factory_avatar_retrieved_cb (TpConnection *connection,
346                                         guint         handle,
347                                         const gchar  *token,
348                                         const GArray *avatar_data,
349                                         const gchar  *mime_type,
350                                         gpointer      user_data,
351                                         GObject      *tp_factory)
352 {
353         EmpathyContact *contact;
354
355         contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
356                                                      handle);
357         if (!contact) {
358                 return;
359         }
360
361         DEBUG ("Avatar retrieved for contact %s (%d)",
362                 empathy_contact_get_id (contact),
363                 handle);
364
365         empathy_contact_load_avatar_data (contact,
366                                           avatar_data->data,
367                                           avatar_data->len,
368                                           mime_type,
369                                           token);
370 }
371
372 static void
373 tp_contact_factory_request_avatars_cb (TpConnection *connection,
374                                        const GError *error,
375                                        gpointer      user_data,
376                                        GObject      *tp_factory)
377 {
378         if (error) {
379                 DEBUG ("Error requesting avatars: %s", error->message);
380         }
381 }
382
383 static gboolean
384 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
385                                         guint                    handle,
386                                         const gchar             *token)
387 {
388         EmpathyContact *contact;
389         EmpathyAvatar  *avatar;
390
391         contact = tp_contact_factory_find_by_handle (tp_factory, handle);
392         if (!contact) {
393                 return TRUE;
394         }
395
396         /* Check if we have an avatar */
397         if (G_STR_EMPTY (token)) {
398                 empathy_contact_set_avatar (contact, NULL);
399                 return TRUE;
400         }
401
402         /* Check if the avatar changed */
403         avatar = empathy_contact_get_avatar (contact);
404         if (avatar && !tp_strdiff (avatar->token, token)) {
405                 return TRUE;
406         }
407
408         /* The avatar changed, search the new one in the cache */
409         if (empathy_contact_load_avatar_cache (contact, token)) {
410                 /* Got from cache, use it */
411                 return TRUE;
412         }
413
414         /* Avatar is not up-to-date, we have to request it. */
415         return FALSE;
416 }
417
418 typedef struct {
419         EmpathyTpContactFactory *tp_factory;
420         GArray                  *handles;
421 } TokensData;
422
423 static void
424 tp_contact_factory_avatar_tokens_foreach (gpointer key,
425                                           gpointer value,
426                                           gpointer user_data)
427 {
428         TokensData  *data = user_data;
429         const gchar *token = value;
430         guint        handle = GPOINTER_TO_UINT (key);
431
432         if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
433                                                      handle, token)) {
434                 g_array_append_val (data->handles, handle);
435         }
436 }
437
438 static void
439 tp_contact_factory_get_known_avatar_tokens_cb (TpConnection *connection,
440                                                GHashTable   *tokens,
441                                                const GError *error,
442                                                gpointer      user_data,
443                                                GObject      *tp_factory)
444 {
445         TokensData data;
446
447         if (error) {
448                 DEBUG ("Error getting known avatars tokens: %s", error->message);
449                 return;
450         }
451
452         data.tp_factory = EMPATHY_TP_CONTACT_FACTORY (tp_factory);
453         data.handles = g_array_new (FALSE, FALSE, sizeof (guint));
454         g_hash_table_foreach (tokens,
455                               tp_contact_factory_avatar_tokens_foreach,
456                               &data);
457
458         DEBUG ("Got %d tokens, need to request %d avatars",
459                 g_hash_table_size (tokens), data.handles->len);
460
461         /* Request needed avatars */
462         if (data.handles->len > 0) {
463                 tp_cli_connection_interface_avatars_call_request_avatars (connection,
464                                                                           -1,
465                                                                           data.handles,
466                                                                           tp_contact_factory_request_avatars_cb,
467                                                                           NULL, NULL,
468                                                                           tp_factory);
469         }
470
471         g_array_free (data.handles, TRUE);
472 }
473
474 static void
475 tp_contact_factory_avatar_updated_cb (TpConnection *connection,
476                                       guint         handle,
477                                       const gchar  *new_token,
478                                       gpointer      user_data,
479                                       GObject      *tp_factory)
480 {
481         GArray *handles;
482
483         if (tp_contact_factory_avatar_maybe_update (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
484                                                     handle, new_token)) {
485                 /* Avatar was cached, nothing to do */
486                 return;
487         }
488
489         DEBUG ("Need to request avatar for token %s", new_token);
490
491         handles = g_array_new (FALSE, FALSE, sizeof (guint));
492         g_array_append_val (handles, handle);
493
494         tp_cli_connection_interface_avatars_call_request_avatars (connection,
495                                                                   -1,
496                                                                   handles,
497                                                                   tp_contact_factory_request_avatars_cb,
498                                                                   NULL, NULL,
499                                                                   tp_factory);
500         g_array_free (handles, TRUE);
501 }
502
503 static void
504 tp_contact_factory_update_capabilities (EmpathyTpContactFactory *tp_factory,
505                                         guint                    handle,
506                                         const gchar             *channel_type,
507                                         guint                    generic,
508                                         guint                    specific)
509 {
510         EmpathyContact      *contact;
511         EmpathyCapabilities  capabilities;
512
513         contact = tp_contact_factory_find_by_handle (tp_factory, handle);
514         if (!contact) {
515                 return;
516         }
517
518         capabilities = empathy_contact_get_capabilities (contact);
519         capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
520
521         if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA) == 0) {
522                 capabilities &= ~EMPATHY_CAPABILITIES_AUDIO;
523                 capabilities &= ~EMPATHY_CAPABILITIES_VIDEO;
524                 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_AUDIO) {
525                         capabilities |= EMPATHY_CAPABILITIES_AUDIO;
526                 }
527                 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
528                         capabilities |= EMPATHY_CAPABILITIES_VIDEO;
529                 }
530         }
531
532         DEBUG ("Changing capabilities for contact %s (%d) to %d",
533                 empathy_contact_get_id (contact),
534                 empathy_contact_get_handle (contact),
535                 capabilities);
536
537         empathy_contact_set_capabilities (contact, capabilities);
538 }
539
540 static void
541 tp_contact_factory_get_capabilities_cb (TpConnection    *connection,
542                                         const GPtrArray *capabilities,
543                                         const GError    *error,
544                                         gpointer         user_data,
545                                         GObject         *weak_object)
546 {
547         EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
548         guint                    i;
549
550         if (error) {
551                 DEBUG ("Error getting capabilities: %s", error->message);
552                 /* FIXME Should set the capabilities of the contacts for which this request
553                  * originated to NONE */
554                 return;
555         }
556
557         for (i = 0; i < capabilities->len; i++) {
558                 GValueArray *values;
559                 guint        handle;
560                 const gchar *channel_type;
561                 guint        generic;
562                 guint        specific;
563
564                 values = g_ptr_array_index (capabilities, i);
565                 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
566                 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
567                 generic = g_value_get_uint (g_value_array_get_nth (values, 2));
568                 specific = g_value_get_uint (g_value_array_get_nth (values, 3));
569
570                 tp_contact_factory_update_capabilities (tp_factory,
571                                                         handle,
572                                                         channel_type,
573                                                         generic,
574                                                         specific);
575         }
576 }
577
578 static void
579 tp_contact_factory_capabilities_changed_cb (TpConnection    *connection,
580                                             const GPtrArray *capabilities,
581                                             gpointer         user_data,
582                                             GObject         *weak_object)
583 {
584         EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
585         guint                    i;
586
587         for (i = 0; i < capabilities->len; i++) {
588                 GValueArray *values;
589                 guint        handle;
590                 const gchar *channel_type;
591                 guint        generic;
592                 guint        specific;
593
594                 values = g_ptr_array_index (capabilities, i);
595                 handle = g_value_get_uint (g_value_array_get_nth (values, 0));
596                 channel_type = g_value_get_string (g_value_array_get_nth (values, 1));
597                 generic = g_value_get_uint (g_value_array_get_nth (values, 3));
598                 specific = g_value_get_uint (g_value_array_get_nth (values, 5));
599
600                 tp_contact_factory_update_capabilities (tp_factory,
601                                                         handle,
602                                                         channel_type,
603                                                         generic,
604                                                         specific);
605         }
606 }
607
608 static void
609 tp_contact_factory_request_everything (EmpathyTpContactFactory *tp_factory,
610                                        const GArray            *handles)
611 {
612         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
613         guint                       *dup_handles;
614
615         g_return_if_fail (priv->ready);
616
617         dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
618         g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
619         tp_cli_connection_interface_presence_call_get_presence (priv->connection,
620                                                                 -1,
621                                                                 handles,
622                                                                 tp_contact_factory_get_presence_cb,
623                                                                 dup_handles, g_free,
624                                                                 G_OBJECT (tp_factory));
625
626         /* FIXME: Sometimes the dbus call timesout because CM takes
627          * too much time to request all aliases from the server,
628          * that's why we increase the timeout here. See fd.o bug #14795 */
629         dup_handles = g_malloc0 ((handles->len + 1) * sizeof (guint));
630         g_memmove (dup_handles, handles->data, handles->len * sizeof (guint));
631         tp_cli_connection_interface_aliasing_call_request_aliases (priv->connection,
632                                                                    5*60*1000,
633                                                                    handles,
634                                                                    tp_contact_factory_request_aliases_cb,
635                                                                    dup_handles, g_free,
636                                                                    G_OBJECT (tp_factory));
637
638         tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
639                                                                           -1,
640                                                                           handles,
641                                                                           tp_contact_factory_get_known_avatar_tokens_cb,
642                                                                           NULL, NULL,
643                                                                           G_OBJECT (tp_factory));
644
645         tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
646                                                                         -1,
647                                                                         handles,
648                                                                         tp_contact_factory_get_capabilities_cb,
649                                                                         NULL, NULL,
650                                                                         G_OBJECT (tp_factory));
651 }
652
653 static void
654 tp_contact_factory_list_free (gpointer data)
655 {
656         GList *l = data;
657
658         g_list_foreach (l, (GFunc) g_object_unref, NULL);
659         g_list_free (l);
660 }
661
662 static void
663 tp_contact_factory_request_handles_cb (TpConnection *connection,
664                                        const GArray *handles,
665                                        const GError *error,
666                                        gpointer      user_data,
667                                        GObject      *tp_factory)
668 {
669         GList *contacts = user_data;
670         GList *l;
671         guint  i = 0;
672
673         if (error) {
674                 DEBUG ("Failed to request handles: %s", error->message);
675                 return;
676         }
677
678         for (l = contacts; l; l = l->next) {
679                 guint handle;
680
681                 handle = g_array_index (handles, guint, i);
682                 empathy_contact_set_handle (l->data, handle);
683
684                 i++;
685         }
686
687         tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
688                                                handles);
689 }
690
691 static void
692 tp_contact_factory_inspect_handles_cb (TpConnection  *connection,
693                                        const gchar  **ids,
694                                        const GError  *error,
695                                        gpointer       user_data,
696                                        GObject       *tp_factory)
697 {
698         const gchar **id;
699         GList        *contacts = user_data;
700         GList        *l;
701
702         if (error) {
703                 DEBUG ("Failed to inspect handles: %s", error->message);
704                 return;
705         }
706
707         id = ids;
708         for (l = contacts; l; l = l->next) {
709                 empathy_contact_set_id (l->data, *id);
710                 id++;
711         }
712 }
713
714 static void
715 tp_contact_factory_disconnect_contact_foreach (gpointer data,
716                                                gpointer user_data)
717 {
718         EmpathyContact *contact = data;
719         
720         empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
721         empathy_contact_set_handle (contact, 0);
722 }
723
724 static void
725 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
726 {
727         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
728
729         DEBUG ("Connection invalidated");
730
731         g_object_unref (priv->connection);
732         priv->connection = NULL;
733         priv->ready = FALSE;
734         g_object_notify (G_OBJECT (tp_factory), "ready");
735
736
737         g_list_foreach (priv->contacts,
738                         tp_contact_factory_disconnect_contact_foreach,
739                         tp_factory);
740 }
741
742
743 static void
744 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
745                                        guint         handle,
746                                        const GError *error,
747                                        gpointer      user_data,
748                                        GObject      *tp_factory)
749 {
750         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
751
752         if (error) {
753                 DEBUG ("Failed to get self handles: %s", error->message);
754                 return;
755         }
756
757         empathy_contact_set_handle (priv->user, handle);
758
759         /* Get avatar requirements for this connection */
760         tp_cli_connection_interface_avatars_call_get_avatar_requirements (
761                 priv->connection,
762                 -1,
763                 tp_contact_factory_got_avatar_requirements_cb,
764                 NULL, NULL,
765                 tp_factory);
766 }
767
768
769 static void
770 tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
771                                                const gchar **mime_types,
772                                                guint         min_width,
773                                                guint         min_height,
774                                                guint         max_width,
775                                                guint         max_height,
776                                                guint         max_size,
777                                                const GError *error,
778                                                gpointer      user_data,
779                                                GObject      *tp_factory)
780 {
781         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
782
783         if (error) {
784                 DEBUG ("Failed to get avatar requirements: %s", error->message);
785                 /* We'll just leave avatar_mime_types as NULL; the
786                  * avatar-setting code can use this as a signal that you can't
787                  * set avatars.
788                  */
789         } else {
790                 priv->avatar_mime_types = g_strdupv ((gchar **)mime_types);
791                 priv->avatar_min_width = min_width;
792                 priv->avatar_min_height = min_height;
793                 priv->avatar_max_width = max_width;
794                 priv->avatar_max_height = max_height;
795                 priv->avatar_max_size = max_size;
796         }
797
798         tp_contact_factory_ready (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
799 }
800
801
802 static void
803 tp_contact_factory_ready (EmpathyTpContactFactory *tp_factory)
804 {
805         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
806         GList                       *l;
807         GArray                      *handle_needed;
808         GArray                      *id_needed;
809         GList                       *handle_needed_contacts = NULL;
810         GList                       *id_needed_contacts = NULL;
811
812         DEBUG ("Connection ready");
813
814         priv->ready = TRUE;
815         g_object_notify (G_OBJECT (tp_factory), "ready");
816
817         /* Connect signals */
818         tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
819                                                                          tp_contact_factory_aliases_changed_cb,
820                                                                          NULL, NULL,
821                                                                          G_OBJECT (tp_factory),
822                                                                          NULL);
823         tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
824                                                                        tp_contact_factory_avatar_updated_cb,
825                                                                        NULL, NULL,
826                                                                        G_OBJECT (tp_factory),
827                                                                        NULL);
828         tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
829                                                                          tp_contact_factory_avatar_retrieved_cb,
830                                                                          NULL, NULL,
831                                                                          G_OBJECT (tp_factory),
832                                                                          NULL);
833         tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
834                                                                          tp_contact_factory_presence_update_cb,
835                                                                          NULL, NULL,
836                                                                          G_OBJECT (tp_factory),
837                                                                          NULL);
838         tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
839                                                                                   tp_contact_factory_capabilities_changed_cb,
840                                                                                   NULL, NULL,
841                                                                                   G_OBJECT (tp_factory),
842                                                                                   NULL);
843
844         /* Request needed info for all existing contacts */
845         handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
846         id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
847         for (l = priv->contacts; l; l = l->next) {
848                 EmpathyContact *contact;
849                 guint           handle;
850                 const gchar    *id;
851
852                 contact = l->data;
853                 handle = empathy_contact_get_handle (contact);
854                 id = empathy_contact_get_id (contact);
855                 if (handle == 0) {
856                         g_assert (!G_STR_EMPTY (id));
857                         g_array_append_val (handle_needed, id);
858                         handle_needed_contacts = g_list_prepend (handle_needed_contacts,
859                                                                  g_object_ref (contact));
860                 }
861                 if (G_STR_EMPTY (id)) {
862                         g_array_append_val (id_needed, handle);
863                         id_needed_contacts = g_list_prepend (id_needed_contacts,
864                                                              g_object_ref (contact));
865                 }
866         }
867         handle_needed_contacts = g_list_reverse (handle_needed_contacts);
868         id_needed_contacts = g_list_reverse (id_needed_contacts);
869
870         tp_cli_connection_call_request_handles (priv->connection,
871                                                 -1,
872                                                 TP_HANDLE_TYPE_CONTACT,
873                                                 (const gchar**) handle_needed->data,
874                                                 tp_contact_factory_request_handles_cb,
875                                                 handle_needed_contacts, tp_contact_factory_list_free,
876                                                 G_OBJECT (tp_factory));
877
878         tp_cli_connection_call_inspect_handles (priv->connection,
879                                                 -1,
880                                                 TP_HANDLE_TYPE_CONTACT,
881                                                 id_needed,
882                                                 tp_contact_factory_inspect_handles_cb,
883                                                 id_needed_contacts, tp_contact_factory_list_free,
884                                                 G_OBJECT (tp_factory));
885
886         tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
887                                                id_needed);
888
889         g_array_free (handle_needed, TRUE);
890         g_array_free (id_needed, TRUE);
891 }
892
893 static void
894 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
895 {
896         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
897
898         /* Get our own handle */
899         tp_cli_connection_call_get_self_handle (priv->connection,
900                                                 -1,
901                                                 tp_contact_factory_got_self_handle_cb,
902                                                 NULL, NULL,
903                                                 G_OBJECT (tp_factory));
904 }
905
906 static void
907 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
908 {
909         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
910         gboolean                     connection_ready;
911
912         if (priv->connection) {
913                 /* We already have our connection object */
914                 return;
915         }
916
917         priv->connection = mission_control_get_tpconnection (priv->mc, priv->account, NULL);
918         if (!priv->connection) {
919                 return;
920         }
921
922         /* We got a new connection, wait for it to be ready */
923         g_signal_connect_swapped (priv->connection, "invalidated",
924                                   G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
925                                   tp_factory);
926
927         g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
928         if (connection_ready) {
929                 tp_contact_factory_connection_ready_cb (tp_factory);
930         } else {
931                 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
932                                           G_CALLBACK (tp_contact_factory_connection_ready_cb),
933                                           tp_factory);
934         }
935 }
936
937 static void
938 tp_contact_factory_status_changed_cb (MissionControl           *mc,
939                                       TpConnectionStatus        status,
940                                       McPresence                presence,
941                                       TpConnectionStatusReason  reason,
942                                       const gchar              *unique_name,
943                                       EmpathyTpContactFactory  *tp_factory)
944 {
945         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
946         McAccount                   *account;
947
948         account = mc_account_lookup (unique_name);
949         if (account && empathy_account_equal (account, priv->account)) {
950                 tp_contact_factory_status_updated (tp_factory);
951         }
952         g_object_unref (account);
953 }
954
955 static void
956 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
957                                 EmpathyContact          *contact)
958 {
959         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
960
961         g_object_weak_ref (G_OBJECT (contact),
962                            tp_contact_factory_weak_notify,
963                            tp_factory);
964         priv->contacts = g_list_prepend (priv->contacts, contact);
965
966         DEBUG ("Contact added: %s (%d)",
967                 empathy_contact_get_id (contact),
968                 empathy_contact_get_handle (contact));
969 }
970
971 static void
972 tp_contact_factory_hold_handles_cb (TpConnection *connection,
973                                     const GError *error,
974                                     gpointer      userdata,
975                                     GObject      *tp_factory)
976 {
977         if (error) {
978                 DEBUG ("Failed to hold handles: %s", error->message);
979         }
980 }
981
982 EmpathyContact *
983 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
984 {
985         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
986
987         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
988
989         return g_object_ref (priv->user);
990 }
991
992 EmpathyContact *
993 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
994                                         const gchar             *id)
995 {
996         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
997         EmpathyContact              *contact;
998
999         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1000         g_return_val_if_fail (id != NULL, NULL);
1001
1002         /* Check if the contact already exists */
1003         contact = tp_contact_factory_find_by_id (tp_factory, id);
1004         if (contact) {
1005                 return g_object_ref (contact);
1006         }
1007
1008         /* Create new contact */
1009         contact = g_object_new (EMPATHY_TYPE_CONTACT,
1010                                 "account", priv->account,
1011                                 "id", id,
1012                                 NULL);
1013         tp_contact_factory_add_contact (tp_factory, contact);
1014
1015         if (priv->ready) {
1016                 const gchar *contact_ids[] = {id, NULL};
1017                 GList       *contacts;
1018                 
1019                 contacts = g_list_prepend (NULL, g_object_ref (contact));
1020                 tp_cli_connection_call_request_handles (priv->connection,
1021                                                         -1,
1022                                                         TP_HANDLE_TYPE_CONTACT,
1023                                                         contact_ids,
1024                                                         tp_contact_factory_request_handles_cb,
1025                                                         contacts, tp_contact_factory_list_free,
1026                                                         G_OBJECT (tp_factory));
1027         }
1028
1029         return contact;
1030 }
1031
1032 EmpathyContact *
1033 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1034                                             guint                    handle)
1035 {
1036         EmpathyContact *contact;
1037         GArray         *handles;
1038         GList          *contacts;
1039
1040         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1041
1042         handles = g_array_new (FALSE, FALSE, sizeof (guint));
1043         g_array_append_val (handles, handle);
1044
1045         contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
1046         g_array_free (handles, TRUE);
1047
1048         contact = contacts ? contacts->data : NULL;
1049         g_list_free (contacts);
1050
1051         return contact;
1052 }
1053
1054 GList *
1055 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1056                                              const GArray            *handles)
1057 {
1058         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1059         GList                       *contacts = NULL;
1060         GArray                      *new_handles;
1061         GList                       *new_contacts = NULL;
1062         guint                        i;
1063
1064         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1065         g_return_val_if_fail (handles != NULL, NULL);
1066
1067         /* Search all contacts we already have */
1068         new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1069         for (i = 0; i < handles->len; i++) {
1070                 EmpathyContact *contact;
1071                 guint           handle;
1072
1073                 handle = g_array_index (handles, guint, i);
1074                 if (handle == 0) {
1075                         continue;
1076                 }
1077
1078                 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1079                 if (contact) {
1080                         contacts = g_list_prepend (contacts, g_object_ref (contact));
1081                 } else {
1082                         g_array_append_val (new_handles, handle);
1083                 }
1084         }
1085
1086         if (new_handles->len == 0) {
1087                 g_array_free (new_handles, TRUE);
1088                 return contacts;
1089         }
1090
1091         /* Create new contacts */
1092         for (i = 0; i < new_handles->len; i++) {
1093                 EmpathyContact *contact;
1094                 guint           handle;
1095
1096                 handle = g_array_index (new_handles, guint, i);
1097
1098                 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1099                                         "account", priv->account,
1100                                         "handle", handle,
1101                                         NULL);
1102                 tp_contact_factory_add_contact (tp_factory, contact);
1103                 contacts = g_list_prepend (contacts, contact);
1104                 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1105         }
1106         new_contacts = g_list_reverse (new_contacts);
1107
1108         if (priv->ready) {
1109                 /* Get the IDs of all new handles */
1110                 tp_cli_connection_call_inspect_handles (priv->connection,
1111                                                         -1,
1112                                                         TP_HANDLE_TYPE_CONTACT,
1113                                                         new_handles,
1114                                                         tp_contact_factory_inspect_handles_cb,
1115                                                         new_contacts, tp_contact_factory_list_free,
1116                                                         G_OBJECT (tp_factory));
1117
1118                 /* Hold all new handles. */
1119                 /* FIXME: Should be unholded when removed from the factory */
1120                 tp_cli_connection_call_hold_handles (priv->connection,
1121                                                      -1,
1122                                                      TP_HANDLE_TYPE_CONTACT,
1123                                                      new_handles,
1124                                                      tp_contact_factory_hold_handles_cb,
1125                                                      NULL, NULL,
1126                                                      G_OBJECT (tp_factory));
1127
1128                 tp_contact_factory_request_everything (tp_factory, new_handles);
1129         }
1130
1131         g_array_free (new_handles, TRUE);
1132
1133         return contacts;
1134 }
1135
1136 void
1137 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1138                                       EmpathyContact          *contact,
1139                                       const gchar             *alias)
1140 {
1141         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1142         GHashTable                  *new_alias;
1143         guint                        handle;
1144
1145         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1146         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1147         g_return_if_fail (priv->ready);
1148         g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1149                                                  priv->account));
1150
1151         handle = empathy_contact_get_handle (contact);
1152
1153         DEBUG ("Setting alias for contact %s (%d) to %s",
1154                 empathy_contact_get_id (contact),
1155                 handle, alias);
1156
1157         new_alias = g_hash_table_new_full (g_direct_hash,
1158                                            g_direct_equal,
1159                                            NULL,
1160                                            g_free);
1161
1162         g_hash_table_insert (new_alias,
1163                              GUINT_TO_POINTER (handle),
1164                              g_strdup (alias));
1165
1166         tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1167                                                                -1,
1168                                                                new_alias,
1169                                                                tp_contact_factory_set_aliases_cb,
1170                                                                NULL, NULL,
1171                                                                G_OBJECT (tp_factory));
1172
1173         g_hash_table_destroy (new_alias);
1174 }
1175
1176 void
1177 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1178                                        const gchar             *data,
1179                                        gsize                    size,
1180                                        const gchar             *mime_type)
1181 {
1182         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1183
1184         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1185         g_return_if_fail (priv->ready);
1186
1187         if (data && size > 0 && size < G_MAXUINT) {
1188                 GArray avatar;
1189
1190                 avatar.data = (gchar*) data;
1191                 avatar.len = size;
1192
1193                 DEBUG ("Setting avatar on account %s",
1194                         mc_account_get_unique_name (priv->account));
1195
1196                 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1197                                                                      -1,
1198                                                                      &avatar,
1199                                                                      mime_type,
1200                                                                      tp_contact_factory_set_avatar_cb,
1201                                                                      NULL, NULL,
1202                                                                      G_OBJECT (tp_factory));
1203         } else {
1204                 DEBUG ("Clearing avatar on account %s",
1205                         mc_account_get_unique_name (priv->account));
1206
1207                 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1208                                                                        -1,
1209                                                                        tp_contact_factory_clear_avatar_cb,
1210                                                                        NULL, NULL,
1211                                                                        G_OBJECT (tp_factory));
1212         }
1213 }
1214
1215 gboolean
1216 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1217 {
1218         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1219
1220         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1221
1222         return priv->ready;
1223 }
1224
1225 static void
1226 tp_contact_factory_get_property (GObject    *object,
1227                                  guint       param_id,
1228                                  GValue     *value,
1229                                  GParamSpec *pspec)
1230 {
1231         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1232
1233         switch (param_id) {
1234         case PROP_ACCOUNT:
1235                 g_value_set_object (value, priv->account);
1236                 break;
1237         case PROP_READY:
1238                 g_value_set_boolean (value, priv->ready);
1239                 break;
1240         case PROP_MIME_TYPES:
1241                 g_value_set_boxed (value, priv->avatar_mime_types);
1242                 break;
1243         case PROP_MIN_WIDTH:
1244                 g_value_set_uint (value, priv->avatar_min_width);
1245                 break;
1246         case PROP_MIN_HEIGHT:
1247                 g_value_set_uint (value, priv->avatar_min_height);
1248                 break;
1249         case PROP_MAX_WIDTH:
1250                 g_value_set_uint (value, priv->avatar_max_width);
1251                 break;
1252         case PROP_MAX_HEIGHT:
1253                 g_value_set_uint (value, priv->avatar_max_height);
1254                 break;
1255         case PROP_MAX_SIZE:
1256                 g_value_set_uint (value, priv->avatar_max_size);
1257                 break;
1258         default:
1259                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1260                 break;
1261         };
1262 }
1263
1264 static void
1265 tp_contact_factory_set_property (GObject      *object,
1266                                  guint         param_id,
1267                                  const GValue *value,
1268                                  GParamSpec   *pspec)
1269 {
1270         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1271
1272         switch (param_id) {
1273         case PROP_ACCOUNT:
1274                 priv->account = g_object_ref (g_value_get_object (value));
1275                 break;
1276         default:
1277                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1278                 break;
1279         };
1280 }
1281
1282 static void
1283 tp_contact_factory_finalize (GObject *object)
1284 {
1285         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1286         GList                       *l;
1287
1288         DEBUG ("Finalized: %p (%s)", object,
1289                 mc_account_get_normalized_name (priv->account));
1290
1291         empathy_disconnect_account_status_changed (priv->token);
1292
1293         for (l = priv->contacts; l; l = l->next) {
1294                 g_object_weak_unref (G_OBJECT (l->data),
1295                                      tp_contact_factory_weak_notify,
1296                                      object);
1297         }
1298
1299         g_list_free (priv->contacts);
1300         g_object_unref (priv->mc);
1301         g_object_unref (priv->account);
1302         g_object_unref (priv->user);
1303
1304         if (priv->connection) {
1305                 g_signal_handlers_disconnect_by_func (priv->connection,
1306                                                       tp_contact_factory_connection_invalidated_cb,
1307                                                       object);
1308                 g_object_unref (priv->connection);
1309         }
1310
1311         g_strfreev (priv->avatar_mime_types);
1312
1313         G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1314 }
1315
1316 static GObject *
1317 tp_contact_factory_constructor (GType                  type,
1318                                 guint                  n_props,
1319                                 GObjectConstructParam *props)
1320 {
1321         GObject *tp_factory;
1322         EmpathyTpContactFactoryPriv *priv;
1323
1324         tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1325         priv = GET_PRIV (tp_factory);
1326
1327         priv->ready = FALSE;
1328         priv->user = empathy_contact_new (priv->account);
1329         empathy_contact_set_is_user (priv->user, TRUE);
1330         tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1331         tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1332
1333         return tp_factory;
1334 }
1335
1336 static void
1337 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1338 {
1339         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1340
1341         object_class->finalize = tp_contact_factory_finalize;
1342         object_class->constructor = tp_contact_factory_constructor;
1343         object_class->get_property = tp_contact_factory_get_property;
1344         object_class->set_property = tp_contact_factory_set_property;
1345
1346         g_object_class_install_property (object_class,
1347                                          PROP_ACCOUNT,
1348                                          g_param_spec_object ("account",
1349                                                               "Factory's Account",
1350                                                               "The account associated with the factory",
1351                                                               MC_TYPE_ACCOUNT,
1352                                                               G_PARAM_READWRITE |
1353                                                               G_PARAM_CONSTRUCT_ONLY |
1354                                                               G_PARAM_STATIC_STRINGS));
1355         g_object_class_install_property (object_class,
1356                                          PROP_READY,
1357                                          g_param_spec_boolean ("ready",
1358                                                                "Whether the factory is ready",
1359                                                                "TRUE once the factory is ready to be used",
1360                                                                FALSE,
1361                                                                G_PARAM_READABLE |
1362                                                                G_PARAM_STATIC_STRINGS));
1363         g_object_class_install_property (object_class,
1364                                          PROP_MIME_TYPES,
1365                                          g_param_spec_boxed ("avatar-mime-types",
1366                                                              "Supported MIME types for avatars",
1367                                                              "Types of images that may be set as "
1368                                                              "avatars on this connection.  Only valid "
1369                                                              "once 'ready' becomes TRUE.",
1370                                                              G_TYPE_STRV,
1371                                                              G_PARAM_READABLE |
1372                                                              G_PARAM_STATIC_STRINGS));
1373         g_object_class_install_property (object_class,
1374                                          PROP_MIN_WIDTH,
1375                                          g_param_spec_uint ("avatar-min-width",
1376                                                             "Minimum width for avatars",
1377                                                             "Minimum width of avatar that may be set. "
1378                                                             "Only valid once 'ready' becomes TRUE.",
1379                                                             0,
1380                                                             G_MAXUINT,
1381                                                             0,
1382                                                             G_PARAM_READABLE |
1383                                                             G_PARAM_STATIC_STRINGS));
1384         g_object_class_install_property (object_class,
1385                                          PROP_MIN_HEIGHT,
1386                                          g_param_spec_uint ("avatar-min-height",
1387                                                             "Minimum height for avatars",
1388                                                             "Minimum height of avatar that may be set. "
1389                                                             "Only valid once 'ready' becomes TRUE.",
1390                                                             0,
1391                                                             G_MAXUINT,
1392                                                             0,
1393                                                             G_PARAM_READABLE |
1394                                                             G_PARAM_STATIC_STRINGS));
1395         g_object_class_install_property (object_class,
1396                                          PROP_MAX_WIDTH,
1397                                          g_param_spec_uint ("avatar-max-width",
1398                                                             "Maximum width for avatars",
1399                                                             "Maximum width of avatar that may be set "
1400                                                             "or 0 if there is no maximum. "
1401                                                             "Only valid once 'ready' becomes TRUE.",
1402                                                             0,
1403                                                             G_MAXUINT,
1404                                                             0,
1405                                                             G_PARAM_READABLE |
1406                                                             G_PARAM_STATIC_STRINGS));
1407         g_object_class_install_property (object_class,
1408                                          PROP_MAX_HEIGHT,
1409                                          g_param_spec_uint ("avatar-max-height",
1410                                                             "Maximum height for avatars",
1411                                                             "Maximum height of avatar that may be set "
1412                                                             "or 0 if there is no maximum. "
1413                                                             "Only valid once 'ready' becomes TRUE.",
1414                                                             0,
1415                                                             G_MAXUINT,
1416                                                             0,
1417                                                             G_PARAM_READABLE |
1418                                                             G_PARAM_STATIC_STRINGS));
1419         g_object_class_install_property (object_class,
1420                                          PROP_MAX_SIZE,
1421                                          g_param_spec_uint ("avatar-max-size",
1422                                                             "Maximum size for avatars in bytes",
1423                                                             "Maximum file size of avatar that may be "
1424                                                             "set or 0 if there is no maximum. "
1425                                                             "Only valid once 'ready' becomes TRUE.",
1426                                                             0,
1427                                                             G_MAXUINT,
1428                                                             0,
1429                                                             G_PARAM_READABLE |
1430                                                             G_PARAM_STATIC_STRINGS));
1431
1432
1433         g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1434 }
1435
1436 static void
1437 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1438 {
1439         EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1440                 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1441
1442         tp_factory->priv = priv;
1443         priv->mc = empathy_mission_control_new ();
1444         priv->token = empathy_connect_to_account_status_changed (priv->mc,
1445                                                    G_CALLBACK (tp_contact_factory_status_changed_cb),
1446                                                    tp_factory, NULL);
1447 }
1448
1449 EmpathyTpContactFactory *
1450 empathy_tp_contact_factory_new (McAccount *account)
1451 {
1452         return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,
1453                              "account", account,
1454                              NULL);
1455 }
1456