]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-contact-factory.c
Coding style: only one blank line between functions.
[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 static void
743 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
744                                        guint         handle,
745                                        const GError *error,
746                                        gpointer      user_data,
747                                        GObject      *tp_factory)
748 {
749         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
750
751         if (error) {
752                 DEBUG ("Failed to get self handles: %s", error->message);
753                 return;
754         }
755
756         empathy_contact_set_handle (priv->user, handle);
757
758         /* Get avatar requirements for this connection */
759         tp_cli_connection_interface_avatars_call_get_avatar_requirements (
760                 priv->connection,
761                 -1,
762                 tp_contact_factory_got_avatar_requirements_cb,
763                 NULL, NULL,
764                 tp_factory);
765 }
766
767 static void
768 tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
769                                                const gchar **mime_types,
770                                                guint         min_width,
771                                                guint         min_height,
772                                                guint         max_width,
773                                                guint         max_height,
774                                                guint         max_size,
775                                                const GError *error,
776                                                gpointer      user_data,
777                                                GObject      *tp_factory)
778 {
779         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
780
781         if (error) {
782                 DEBUG ("Failed to get avatar requirements: %s", error->message);
783                 /* We'll just leave avatar_mime_types as NULL; the
784                  * avatar-setting code can use this as a signal that you can't
785                  * set avatars.
786                  */
787         } else {
788                 priv->avatar_mime_types = g_strdupv ((gchar **)mime_types);
789                 priv->avatar_min_width = min_width;
790                 priv->avatar_min_height = min_height;
791                 priv->avatar_max_width = max_width;
792                 priv->avatar_max_height = max_height;
793                 priv->avatar_max_size = max_size;
794         }
795
796         tp_contact_factory_ready (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
797 }
798
799 static void
800 tp_contact_factory_ready (EmpathyTpContactFactory *tp_factory)
801 {
802         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
803         GList                       *l;
804         GArray                      *handle_needed;
805         GArray                      *id_needed;
806         GList                       *handle_needed_contacts = NULL;
807         GList                       *id_needed_contacts = NULL;
808
809         DEBUG ("Connection ready");
810
811         priv->ready = TRUE;
812         g_object_notify (G_OBJECT (tp_factory), "ready");
813
814         /* Connect signals */
815         tp_cli_connection_interface_aliasing_connect_to_aliases_changed (priv->connection,
816                                                                          tp_contact_factory_aliases_changed_cb,
817                                                                          NULL, NULL,
818                                                                          G_OBJECT (tp_factory),
819                                                                          NULL);
820         tp_cli_connection_interface_avatars_connect_to_avatar_updated (priv->connection,
821                                                                        tp_contact_factory_avatar_updated_cb,
822                                                                        NULL, NULL,
823                                                                        G_OBJECT (tp_factory),
824                                                                        NULL);
825         tp_cli_connection_interface_avatars_connect_to_avatar_retrieved (priv->connection,
826                                                                          tp_contact_factory_avatar_retrieved_cb,
827                                                                          NULL, NULL,
828                                                                          G_OBJECT (tp_factory),
829                                                                          NULL);
830         tp_cli_connection_interface_presence_connect_to_presence_update (priv->connection,
831                                                                          tp_contact_factory_presence_update_cb,
832                                                                          NULL, NULL,
833                                                                          G_OBJECT (tp_factory),
834                                                                          NULL);
835         tp_cli_connection_interface_capabilities_connect_to_capabilities_changed (priv->connection,
836                                                                                   tp_contact_factory_capabilities_changed_cb,
837                                                                                   NULL, NULL,
838                                                                                   G_OBJECT (tp_factory),
839                                                                                   NULL);
840
841         /* Request needed info for all existing contacts */
842         handle_needed = g_array_new (TRUE, FALSE, sizeof (gchar*));
843         id_needed = g_array_new (FALSE, FALSE, sizeof (guint));
844         for (l = priv->contacts; l; l = l->next) {
845                 EmpathyContact *contact;
846                 guint           handle;
847                 const gchar    *id;
848
849                 contact = l->data;
850                 handle = empathy_contact_get_handle (contact);
851                 id = empathy_contact_get_id (contact);
852                 if (handle == 0) {
853                         g_assert (!G_STR_EMPTY (id));
854                         g_array_append_val (handle_needed, id);
855                         handle_needed_contacts = g_list_prepend (handle_needed_contacts,
856                                                                  g_object_ref (contact));
857                 }
858                 if (G_STR_EMPTY (id)) {
859                         g_array_append_val (id_needed, handle);
860                         id_needed_contacts = g_list_prepend (id_needed_contacts,
861                                                              g_object_ref (contact));
862                 }
863         }
864         handle_needed_contacts = g_list_reverse (handle_needed_contacts);
865         id_needed_contacts = g_list_reverse (id_needed_contacts);
866
867         tp_cli_connection_call_request_handles (priv->connection,
868                                                 -1,
869                                                 TP_HANDLE_TYPE_CONTACT,
870                                                 (const gchar**) handle_needed->data,
871                                                 tp_contact_factory_request_handles_cb,
872                                                 handle_needed_contacts, tp_contact_factory_list_free,
873                                                 G_OBJECT (tp_factory));
874
875         tp_cli_connection_call_inspect_handles (priv->connection,
876                                                 -1,
877                                                 TP_HANDLE_TYPE_CONTACT,
878                                                 id_needed,
879                                                 tp_contact_factory_inspect_handles_cb,
880                                                 id_needed_contacts, tp_contact_factory_list_free,
881                                                 G_OBJECT (tp_factory));
882
883         tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
884                                                id_needed);
885
886         g_array_free (handle_needed, TRUE);
887         g_array_free (id_needed, TRUE);
888 }
889
890 static void
891 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
892 {
893         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
894
895         /* Get our own handle */
896         tp_cli_connection_call_get_self_handle (priv->connection,
897                                                 -1,
898                                                 tp_contact_factory_got_self_handle_cb,
899                                                 NULL, NULL,
900                                                 G_OBJECT (tp_factory));
901 }
902
903 static void
904 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
905 {
906         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
907         gboolean                     connection_ready;
908
909         if (priv->connection) {
910                 /* We already have our connection object */
911                 return;
912         }
913
914         priv->connection = mission_control_get_tpconnection (priv->mc, priv->account, NULL);
915         if (!priv->connection) {
916                 return;
917         }
918
919         /* We got a new connection, wait for it to be ready */
920         g_signal_connect_swapped (priv->connection, "invalidated",
921                                   G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
922                                   tp_factory);
923
924         g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
925         if (connection_ready) {
926                 tp_contact_factory_connection_ready_cb (tp_factory);
927         } else {
928                 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
929                                           G_CALLBACK (tp_contact_factory_connection_ready_cb),
930                                           tp_factory);
931         }
932 }
933
934 static void
935 tp_contact_factory_status_changed_cb (MissionControl           *mc,
936                                       TpConnectionStatus        status,
937                                       McPresence                presence,
938                                       TpConnectionStatusReason  reason,
939                                       const gchar              *unique_name,
940                                       EmpathyTpContactFactory  *tp_factory)
941 {
942         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
943         McAccount                   *account;
944
945         account = mc_account_lookup (unique_name);
946         if (account && empathy_account_equal (account, priv->account)) {
947                 tp_contact_factory_status_updated (tp_factory);
948         }
949         g_object_unref (account);
950 }
951
952 static void
953 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
954                                 EmpathyContact          *contact)
955 {
956         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
957
958         g_object_weak_ref (G_OBJECT (contact),
959                            tp_contact_factory_weak_notify,
960                            tp_factory);
961         priv->contacts = g_list_prepend (priv->contacts, contact);
962
963         DEBUG ("Contact added: %s (%d)",
964                 empathy_contact_get_id (contact),
965                 empathy_contact_get_handle (contact));
966 }
967
968 static void
969 tp_contact_factory_hold_handles_cb (TpConnection *connection,
970                                     const GError *error,
971                                     gpointer      userdata,
972                                     GObject      *tp_factory)
973 {
974         if (error) {
975                 DEBUG ("Failed to hold handles: %s", error->message);
976         }
977 }
978
979 EmpathyContact *
980 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
981 {
982         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
983
984         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
985
986         return g_object_ref (priv->user);
987 }
988
989 EmpathyContact *
990 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
991                                         const gchar             *id)
992 {
993         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
994         EmpathyContact              *contact;
995
996         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
997         g_return_val_if_fail (id != NULL, NULL);
998
999         /* Check if the contact already exists */
1000         contact = tp_contact_factory_find_by_id (tp_factory, id);
1001         if (contact) {
1002                 return g_object_ref (contact);
1003         }
1004
1005         /* Create new contact */
1006         contact = g_object_new (EMPATHY_TYPE_CONTACT,
1007                                 "account", priv->account,
1008                                 "id", id,
1009                                 NULL);
1010         tp_contact_factory_add_contact (tp_factory, contact);
1011
1012         if (priv->ready) {
1013                 const gchar *contact_ids[] = {id, NULL};
1014                 GList       *contacts;
1015                 
1016                 contacts = g_list_prepend (NULL, g_object_ref (contact));
1017                 tp_cli_connection_call_request_handles (priv->connection,
1018                                                         -1,
1019                                                         TP_HANDLE_TYPE_CONTACT,
1020                                                         contact_ids,
1021                                                         tp_contact_factory_request_handles_cb,
1022                                                         contacts, tp_contact_factory_list_free,
1023                                                         G_OBJECT (tp_factory));
1024         }
1025
1026         return contact;
1027 }
1028
1029 EmpathyContact *
1030 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1031                                             guint                    handle)
1032 {
1033         EmpathyContact *contact;
1034         GArray         *handles;
1035         GList          *contacts;
1036
1037         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1038
1039         handles = g_array_new (FALSE, FALSE, sizeof (guint));
1040         g_array_append_val (handles, handle);
1041
1042         contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
1043         g_array_free (handles, TRUE);
1044
1045         contact = contacts ? contacts->data : NULL;
1046         g_list_free (contacts);
1047
1048         return contact;
1049 }
1050
1051 GList *
1052 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1053                                              const GArray            *handles)
1054 {
1055         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1056         GList                       *contacts = NULL;
1057         GArray                      *new_handles;
1058         GList                       *new_contacts = NULL;
1059         guint                        i;
1060
1061         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1062         g_return_val_if_fail (handles != NULL, NULL);
1063
1064         /* Search all contacts we already have */
1065         new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1066         for (i = 0; i < handles->len; i++) {
1067                 EmpathyContact *contact;
1068                 guint           handle;
1069
1070                 handle = g_array_index (handles, guint, i);
1071                 if (handle == 0) {
1072                         continue;
1073                 }
1074
1075                 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1076                 if (contact) {
1077                         contacts = g_list_prepend (contacts, g_object_ref (contact));
1078                 } else {
1079                         g_array_append_val (new_handles, handle);
1080                 }
1081         }
1082
1083         if (new_handles->len == 0) {
1084                 g_array_free (new_handles, TRUE);
1085                 return contacts;
1086         }
1087
1088         /* Create new contacts */
1089         for (i = 0; i < new_handles->len; i++) {
1090                 EmpathyContact *contact;
1091                 guint           handle;
1092
1093                 handle = g_array_index (new_handles, guint, i);
1094
1095                 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1096                                         "account", priv->account,
1097                                         "handle", handle,
1098                                         NULL);
1099                 tp_contact_factory_add_contact (tp_factory, contact);
1100                 contacts = g_list_prepend (contacts, contact);
1101                 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1102         }
1103         new_contacts = g_list_reverse (new_contacts);
1104
1105         if (priv->ready) {
1106                 /* Get the IDs of all new handles */
1107                 tp_cli_connection_call_inspect_handles (priv->connection,
1108                                                         -1,
1109                                                         TP_HANDLE_TYPE_CONTACT,
1110                                                         new_handles,
1111                                                         tp_contact_factory_inspect_handles_cb,
1112                                                         new_contacts, tp_contact_factory_list_free,
1113                                                         G_OBJECT (tp_factory));
1114
1115                 /* Hold all new handles. */
1116                 /* FIXME: Should be unholded when removed from the factory */
1117                 tp_cli_connection_call_hold_handles (priv->connection,
1118                                                      -1,
1119                                                      TP_HANDLE_TYPE_CONTACT,
1120                                                      new_handles,
1121                                                      tp_contact_factory_hold_handles_cb,
1122                                                      NULL, NULL,
1123                                                      G_OBJECT (tp_factory));
1124
1125                 tp_contact_factory_request_everything (tp_factory, new_handles);
1126         }
1127
1128         g_array_free (new_handles, TRUE);
1129
1130         return contacts;
1131 }
1132
1133 void
1134 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1135                                       EmpathyContact          *contact,
1136                                       const gchar             *alias)
1137 {
1138         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1139         GHashTable                  *new_alias;
1140         guint                        handle;
1141
1142         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1143         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1144         g_return_if_fail (priv->ready);
1145         g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1146                                                  priv->account));
1147
1148         handle = empathy_contact_get_handle (contact);
1149
1150         DEBUG ("Setting alias for contact %s (%d) to %s",
1151                 empathy_contact_get_id (contact),
1152                 handle, alias);
1153
1154         new_alias = g_hash_table_new_full (g_direct_hash,
1155                                            g_direct_equal,
1156                                            NULL,
1157                                            g_free);
1158
1159         g_hash_table_insert (new_alias,
1160                              GUINT_TO_POINTER (handle),
1161                              g_strdup (alias));
1162
1163         tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1164                                                                -1,
1165                                                                new_alias,
1166                                                                tp_contact_factory_set_aliases_cb,
1167                                                                NULL, NULL,
1168                                                                G_OBJECT (tp_factory));
1169
1170         g_hash_table_destroy (new_alias);
1171 }
1172
1173 void
1174 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1175                                        const gchar             *data,
1176                                        gsize                    size,
1177                                        const gchar             *mime_type)
1178 {
1179         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1180
1181         g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1182         g_return_if_fail (priv->ready);
1183
1184         if (data && size > 0 && size < G_MAXUINT) {
1185                 GArray avatar;
1186
1187                 avatar.data = (gchar*) data;
1188                 avatar.len = size;
1189
1190                 DEBUG ("Setting avatar on account %s",
1191                         mc_account_get_unique_name (priv->account));
1192
1193                 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1194                                                                      -1,
1195                                                                      &avatar,
1196                                                                      mime_type,
1197                                                                      tp_contact_factory_set_avatar_cb,
1198                                                                      NULL, NULL,
1199                                                                      G_OBJECT (tp_factory));
1200         } else {
1201                 DEBUG ("Clearing avatar on account %s",
1202                         mc_account_get_unique_name (priv->account));
1203
1204                 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1205                                                                        -1,
1206                                                                        tp_contact_factory_clear_avatar_cb,
1207                                                                        NULL, NULL,
1208                                                                        G_OBJECT (tp_factory));
1209         }
1210 }
1211
1212 gboolean
1213 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1214 {
1215         EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1216
1217         g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1218
1219         return priv->ready;
1220 }
1221
1222 static void
1223 tp_contact_factory_get_property (GObject    *object,
1224                                  guint       param_id,
1225                                  GValue     *value,
1226                                  GParamSpec *pspec)
1227 {
1228         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1229
1230         switch (param_id) {
1231         case PROP_ACCOUNT:
1232                 g_value_set_object (value, priv->account);
1233                 break;
1234         case PROP_READY:
1235                 g_value_set_boolean (value, priv->ready);
1236                 break;
1237         case PROP_MIME_TYPES:
1238                 g_value_set_boxed (value, priv->avatar_mime_types);
1239                 break;
1240         case PROP_MIN_WIDTH:
1241                 g_value_set_uint (value, priv->avatar_min_width);
1242                 break;
1243         case PROP_MIN_HEIGHT:
1244                 g_value_set_uint (value, priv->avatar_min_height);
1245                 break;
1246         case PROP_MAX_WIDTH:
1247                 g_value_set_uint (value, priv->avatar_max_width);
1248                 break;
1249         case PROP_MAX_HEIGHT:
1250                 g_value_set_uint (value, priv->avatar_max_height);
1251                 break;
1252         case PROP_MAX_SIZE:
1253                 g_value_set_uint (value, priv->avatar_max_size);
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_set_property (GObject      *object,
1263                                  guint         param_id,
1264                                  const GValue *value,
1265                                  GParamSpec   *pspec)
1266 {
1267         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1268
1269         switch (param_id) {
1270         case PROP_ACCOUNT:
1271                 priv->account = g_object_ref (g_value_get_object (value));
1272                 break;
1273         default:
1274                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1275                 break;
1276         };
1277 }
1278
1279 static void
1280 tp_contact_factory_finalize (GObject *object)
1281 {
1282         EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1283         GList                       *l;
1284
1285         DEBUG ("Finalized: %p (%s)", object,
1286                 mc_account_get_normalized_name (priv->account));
1287
1288         empathy_disconnect_account_status_changed (priv->token);
1289
1290         for (l = priv->contacts; l; l = l->next) {
1291                 g_object_weak_unref (G_OBJECT (l->data),
1292                                      tp_contact_factory_weak_notify,
1293                                      object);
1294         }
1295
1296         g_list_free (priv->contacts);
1297         g_object_unref (priv->mc);
1298         g_object_unref (priv->account);
1299         g_object_unref (priv->user);
1300
1301         if (priv->connection) {
1302                 g_signal_handlers_disconnect_by_func (priv->connection,
1303                                                       tp_contact_factory_connection_invalidated_cb,
1304                                                       object);
1305                 g_object_unref (priv->connection);
1306         }
1307
1308         g_strfreev (priv->avatar_mime_types);
1309
1310         G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1311 }
1312
1313 static GObject *
1314 tp_contact_factory_constructor (GType                  type,
1315                                 guint                  n_props,
1316                                 GObjectConstructParam *props)
1317 {
1318         GObject *tp_factory;
1319         EmpathyTpContactFactoryPriv *priv;
1320
1321         tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1322         priv = GET_PRIV (tp_factory);
1323
1324         priv->ready = FALSE;
1325         priv->user = empathy_contact_new (priv->account);
1326         empathy_contact_set_is_user (priv->user, TRUE);
1327         tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1328         tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1329
1330         return tp_factory;
1331 }
1332
1333 static void
1334 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1335 {
1336         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1337
1338         object_class->finalize = tp_contact_factory_finalize;
1339         object_class->constructor = tp_contact_factory_constructor;
1340         object_class->get_property = tp_contact_factory_get_property;
1341         object_class->set_property = tp_contact_factory_set_property;
1342
1343         g_object_class_install_property (object_class,
1344                                          PROP_ACCOUNT,
1345                                          g_param_spec_object ("account",
1346                                                               "Factory's Account",
1347                                                               "The account associated with the factory",
1348                                                               MC_TYPE_ACCOUNT,
1349                                                               G_PARAM_READWRITE |
1350                                                               G_PARAM_CONSTRUCT_ONLY |
1351                                                               G_PARAM_STATIC_STRINGS));
1352         g_object_class_install_property (object_class,
1353                                          PROP_READY,
1354                                          g_param_spec_boolean ("ready",
1355                                                                "Whether the factory is ready",
1356                                                                "TRUE once the factory is ready to be used",
1357                                                                FALSE,
1358                                                                G_PARAM_READABLE |
1359                                                                G_PARAM_STATIC_STRINGS));
1360         g_object_class_install_property (object_class,
1361                                          PROP_MIME_TYPES,
1362                                          g_param_spec_boxed ("avatar-mime-types",
1363                                                              "Supported MIME types for avatars",
1364                                                              "Types of images that may be set as "
1365                                                              "avatars on this connection.  Only valid "
1366                                                              "once 'ready' becomes TRUE.",
1367                                                              G_TYPE_STRV,
1368                                                              G_PARAM_READABLE |
1369                                                              G_PARAM_STATIC_STRINGS));
1370         g_object_class_install_property (object_class,
1371                                          PROP_MIN_WIDTH,
1372                                          g_param_spec_uint ("avatar-min-width",
1373                                                             "Minimum width for avatars",
1374                                                             "Minimum width of avatar that may be set. "
1375                                                             "Only valid once 'ready' becomes TRUE.",
1376                                                             0,
1377                                                             G_MAXUINT,
1378                                                             0,
1379                                                             G_PARAM_READABLE |
1380                                                             G_PARAM_STATIC_STRINGS));
1381         g_object_class_install_property (object_class,
1382                                          PROP_MIN_HEIGHT,
1383                                          g_param_spec_uint ("avatar-min-height",
1384                                                             "Minimum height for avatars",
1385                                                             "Minimum height of avatar that may be set. "
1386                                                             "Only valid once 'ready' becomes TRUE.",
1387                                                             0,
1388                                                             G_MAXUINT,
1389                                                             0,
1390                                                             G_PARAM_READABLE |
1391                                                             G_PARAM_STATIC_STRINGS));
1392         g_object_class_install_property (object_class,
1393                                          PROP_MAX_WIDTH,
1394                                          g_param_spec_uint ("avatar-max-width",
1395                                                             "Maximum width for avatars",
1396                                                             "Maximum width of avatar that may be set "
1397                                                             "or 0 if there is no maximum. "
1398                                                             "Only valid once 'ready' becomes TRUE.",
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_HEIGHT,
1406                                          g_param_spec_uint ("avatar-max-height",
1407                                                             "Maximum height for avatars",
1408                                                             "Maximum height of avatar that may be set "
1409                                                             "or 0 if there is no maximum. "
1410                                                             "Only valid once 'ready' becomes TRUE.",
1411                                                             0,
1412                                                             G_MAXUINT,
1413                                                             0,
1414                                                             G_PARAM_READABLE |
1415                                                             G_PARAM_STATIC_STRINGS));
1416         g_object_class_install_property (object_class,
1417                                          PROP_MAX_SIZE,
1418                                          g_param_spec_uint ("avatar-max-size",
1419                                                             "Maximum size for avatars in bytes",
1420                                                             "Maximum file size of avatar that may be "
1421                                                             "set or 0 if there is no maximum. "
1422                                                             "Only valid once 'ready' becomes TRUE.",
1423                                                             0,
1424                                                             G_MAXUINT,
1425                                                             0,
1426                                                             G_PARAM_READABLE |
1427                                                             G_PARAM_STATIC_STRINGS));
1428
1429
1430         g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1431 }
1432
1433 static void
1434 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1435 {
1436         EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1437                 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1438
1439         tp_factory->priv = priv;
1440         priv->mc = empathy_mission_control_new ();
1441         priv->token = empathy_connect_to_account_status_changed (priv->mc,
1442                                                    G_CALLBACK (tp_contact_factory_status_changed_cb),
1443                                                    tp_factory, NULL);
1444 }
1445
1446 EmpathyTpContactFactory *
1447 empathy_tp_contact_factory_new (McAccount *account)
1448 {
1449         return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,
1450                              "account", account,
1451                              NULL);
1452 }
1453