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