Tagged for release 2.26.0.
[empathy-mirror.git] / libempathy / empathy-tp-contact-factory.c
blobd83ba12a8ce146203497f4e86d664272c96ddb09
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2007-2008 Collabora Ltd.
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.
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.
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
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
22 #include <config.h>
24 #include <string.h>
26 #include <telepathy-glib/util.h>
27 #include <telepathy-glib/connection.h>
28 #include <telepathy-glib/gtypes.h>
29 #include <libmissioncontrol/mission-control.h>
31 #include <extensions/extensions.h>
33 #include "empathy-tp-contact-factory.h"
34 #include "empathy-utils.h"
35 #include "empathy-account-manager.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
38 #include "empathy-debug.h"
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpContactFactory)
41 typedef struct {
42 EmpathyAccountManager *account_manager;
43 McAccount *account;
44 TpConnection *connection;
45 gboolean ready;
47 GList *contacts;
48 EmpathyContact *user;
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;
59 G_DEFINE_TYPE (EmpathyTpContactFactory, empathy_tp_contact_factory, G_TYPE_OBJECT);
61 enum {
62 PROP_0,
63 PROP_ACCOUNT,
64 PROP_READY,
66 PROP_MIME_TYPES,
67 PROP_MIN_WIDTH,
68 PROP_MIN_HEIGHT,
69 PROP_MAX_WIDTH,
70 PROP_MAX_HEIGHT,
71 PROP_MAX_SIZE
74 static EmpathyContact *
75 tp_contact_factory_find_by_handle (EmpathyTpContactFactory *tp_factory,
76 guint handle)
78 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
79 GList *l;
81 for (l = priv->contacts; l; l = l->next) {
82 if (empathy_contact_get_handle (l->data) == handle) {
83 return l->data;
87 return NULL;
90 static EmpathyContact *
91 tp_contact_factory_find_by_id (EmpathyTpContactFactory *tp_factory,
92 const gchar *id)
94 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
95 GList *l;
97 for (l = priv->contacts; l; l = l->next) {
98 if (!tp_strdiff (empathy_contact_get_id (l->data), id)) {
99 return l->data;
103 return NULL;
106 static void
107 tp_contact_factory_weak_notify (gpointer data,
108 GObject *where_the_object_was)
110 EmpathyTpContactFactoryPriv *priv = GET_PRIV (data);
112 DEBUG ("Remove finalized contact %p", where_the_object_was);
114 priv->contacts = g_list_remove (priv->contacts, where_the_object_was);
117 static void
118 tp_contact_factory_presences_table_foreach (const gchar *state_str,
119 GHashTable *presences_table,
120 EmpathyContact *contact)
122 const GValue *message;
123 const gchar *message_str = NULL;
125 empathy_contact_set_presence (contact,
126 empathy_presence_from_str (state_str));
128 message = g_hash_table_lookup (presences_table, "message");
129 if (message) {
130 message_str = g_value_get_string (message);
133 if (!EMP_STR_EMPTY (message_str)) {
134 empathy_contact_set_presence_message (contact, message_str);
135 } else {
136 empathy_contact_set_presence_message (contact, NULL);
140 static void
141 tp_contact_factory_parse_presence_foreach (guint handle,
142 GValueArray *presence_struct,
143 EmpathyTpContactFactory *tp_factory)
145 GHashTable *presences_table;
146 EmpathyContact *contact;
148 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
149 if (!contact) {
150 return;
153 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
155 g_hash_table_foreach (presences_table,
156 (GHFunc) tp_contact_factory_presences_table_foreach,
157 contact);
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));
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)
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;
179 /* We have no presence iface, set default presence
180 * to available */
181 while (*handles != 0) {
182 EmpathyContact *contact;
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);
192 handles++;
196 return;
199 g_hash_table_foreach (handle_table,
200 (GHFunc) tp_contact_factory_parse_presence_foreach,
201 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
204 static void
205 tp_contact_factory_presence_update_cb (TpConnection *connection,
206 GHashTable *handle_table,
207 gpointer user_data,
208 GObject *tp_factory)
210 g_hash_table_foreach (handle_table,
211 (GHFunc) tp_contact_factory_parse_presence_foreach,
212 EMPATHY_TP_CONTACT_FACTORY (tp_factory));
215 static void
216 tp_contact_factory_set_aliases_cb (TpConnection *connection,
217 const GError *error,
218 gpointer user_data,
219 GObject *tp_factory)
221 if (error) {
222 DEBUG ("Error setting alias: %s", error->message);
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)
233 guint *handles = user_data;
234 guint i = 0;
235 const gchar **name;
237 if (error) {
238 DEBUG ("Error requesting aliases: %s", error->message);
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;
246 contact = tp_contact_factory_find_by_handle (
247 (EmpathyTpContactFactory*) tp_factory,
248 *handles);
249 if (contact) {
250 empathy_contact_set_name (contact, NULL);
253 handles++;
255 return;
258 for (name = contact_names; *name; name++) {
259 EmpathyContact *contact;
261 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
262 handles[i]);
263 if (!contact) {
264 continue;
267 DEBUG ("Renaming contact %s (%d) to %s (request cb)",
268 empathy_contact_get_id (contact),
269 empathy_contact_get_handle (contact),
270 *name);
272 empathy_contact_set_name (contact, *name);
274 i++;
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)
284 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
285 guint i;
287 for (i = 0; renamed_handlers->len > i; i++) {
288 guint handle;
289 const gchar *alias;
290 GValueArray *renamed_struct;
291 EmpathyContact *contact;
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);
298 if (!contact) {
299 /* We don't know this contact, skip */
300 continue;
303 DEBUG ("Renaming contact %s (%d) to %s (changed cb)",
304 empathy_contact_get_id (contact),
305 handle, alias);
307 empathy_contact_set_name (contact, alias);
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)
318 if (error) {
319 DEBUG ("Error setting avatar: %s", error->message);
323 static void
324 tp_contact_factory_clear_avatar_cb (TpConnection *connection,
325 const GError *error,
326 gpointer user_data,
327 GObject *tp_factory)
329 if (error) {
330 DEBUG ("Error clearing avatar: %s", error->message);
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)
343 EmpathyContact *contact;
345 contact = tp_contact_factory_find_by_handle (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
346 handle);
347 if (!contact) {
348 return;
351 DEBUG ("Avatar retrieved for contact %s (%d)",
352 empathy_contact_get_id (contact),
353 handle);
355 empathy_contact_load_avatar_data (contact,
356 avatar_data->data,
357 avatar_data->len,
358 mime_type,
359 token);
362 static void
363 tp_contact_factory_request_avatars_cb (TpConnection *connection,
364 const GError *error,
365 gpointer user_data,
366 GObject *tp_factory)
368 if (error) {
369 DEBUG ("Error requesting avatars: %s", error->message);
373 static gboolean
374 tp_contact_factory_avatar_maybe_update (EmpathyTpContactFactory *tp_factory,
375 guint handle,
376 const gchar *token)
378 EmpathyContact *contact;
379 EmpathyAvatar *avatar;
381 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
382 if (!contact) {
383 return TRUE;
386 /* Check if we have an avatar */
387 if (EMP_STR_EMPTY (token)) {
388 empathy_contact_set_avatar (contact, NULL);
389 return TRUE;
392 /* Check if the avatar changed */
393 avatar = empathy_contact_get_avatar (contact);
394 if (avatar && !tp_strdiff (avatar->token, token)) {
395 return TRUE;
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;
404 /* Avatar is not up-to-date, we have to request it. */
405 return FALSE;
408 typedef struct {
409 EmpathyTpContactFactory *tp_factory;
410 GArray *handles;
411 } TokensData;
413 static void
414 tp_contact_factory_avatar_tokens_foreach (gpointer key,
415 gpointer value,
416 gpointer user_data)
418 TokensData *data = user_data;
419 const gchar *token = value;
420 guint handle = GPOINTER_TO_UINT (key);
422 if (!tp_contact_factory_avatar_maybe_update (data->tp_factory,
423 handle, token)) {
424 g_array_append_val (data->handles, handle);
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)
435 TokensData data;
437 if (error) {
438 DEBUG ("Error getting known avatars tokens: %s", error->message);
439 return;
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);
448 DEBUG ("Got %d tokens, need to request %d avatars",
449 g_hash_table_size (tokens), data.handles->len);
451 /* Request needed avatars */
452 if (data.handles->len > 0) {
453 tp_cli_connection_interface_avatars_call_request_avatars (connection,
455 data.handles,
456 tp_contact_factory_request_avatars_cb,
457 NULL, NULL,
458 tp_factory);
461 g_array_free (data.handles, TRUE);
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)
471 GArray *handles;
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;
479 DEBUG ("Need to request avatar for token %s", new_token);
481 handles = g_array_new (FALSE, FALSE, sizeof (guint));
482 g_array_append_val (handles, handle);
484 tp_cli_connection_interface_avatars_call_request_avatars (connection,
486 handles,
487 tp_contact_factory_request_avatars_cb,
488 NULL, NULL,
489 tp_factory);
490 g_array_free (handles, TRUE);
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)
500 EmpathyContact *contact;
501 EmpathyCapabilities capabilities;
503 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
504 if (!contact) {
505 return;
508 capabilities = empathy_contact_get_capabilities (contact);
509 capabilities &= ~EMPATHY_CAPABILITIES_UNKNOWN;
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;
517 if (specific & TP_CHANNEL_MEDIA_CAPABILITY_VIDEO) {
518 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
522 DEBUG ("Changing capabilities for contact %s (%d) to %d",
523 empathy_contact_get_id (contact),
524 empathy_contact_get_handle (contact),
525 capabilities);
527 empathy_contact_set_capabilities (contact, capabilities);
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)
537 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
538 guint i;
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;
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;
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));
560 tp_contact_factory_update_capabilities (tp_factory,
561 handle,
562 channel_type,
563 generic,
564 specific);
568 static void
569 tp_contact_factory_capabilities_changed_cb (TpConnection *connection,
570 const GPtrArray *capabilities,
571 gpointer user_data,
572 GObject *weak_object)
574 EmpathyTpContactFactory *tp_factory = EMPATHY_TP_CONTACT_FACTORY (weak_object);
575 guint i;
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;
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));
590 tp_contact_factory_update_capabilities (tp_factory,
591 handle,
592 channel_type,
593 generic,
594 specific);
598 static void
599 tp_contact_factory_request_everything (EmpathyTpContactFactory *tp_factory,
600 const GArray *handles)
602 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
603 guint *dup_handles;
605 g_return_if_fail (priv->ready);
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,
611 handles,
612 tp_contact_factory_get_presence_cb,
613 dup_handles, g_free,
614 G_OBJECT (tp_factory));
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));
628 tp_cli_connection_interface_avatars_call_get_known_avatar_tokens (priv->connection,
630 handles,
631 tp_contact_factory_get_known_avatar_tokens_cb,
632 NULL, NULL,
633 G_OBJECT (tp_factory));
635 tp_cli_connection_interface_capabilities_call_get_capabilities (priv->connection,
637 handles,
638 tp_contact_factory_get_capabilities_cb,
639 NULL, NULL,
640 G_OBJECT (tp_factory));
643 static void
644 tp_contact_factory_list_free (gpointer data)
646 GList *l = data;
648 g_list_foreach (l, (GFunc) g_object_unref, NULL);
649 g_list_free (l);
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)
659 GList *contacts = user_data;
660 GList *l;
661 guint i = 0;
663 if (error) {
664 DEBUG ("Failed to request handles: %s", error->message);
665 return;
668 for (l = contacts; l; l = l->next) {
669 guint handle;
671 handle = g_array_index (handles, guint, i);
672 empathy_contact_set_handle (l->data, handle);
674 i++;
677 tp_contact_factory_request_everything (EMPATHY_TP_CONTACT_FACTORY (tp_factory),
678 handles);
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)
688 const gchar **id;
689 GList *contacts = user_data;
690 GList *l;
692 if (error) {
693 DEBUG ("Failed to inspect handles: %s", error->message);
694 return;
697 id = ids;
698 for (l = contacts; l; l = l->next) {
699 empathy_contact_set_id (l->data, *id);
700 id++;
704 static void
705 tp_contact_factory_disconnect_contact_foreach (gpointer data,
706 gpointer user_data)
708 EmpathyContact *contact = data;
710 empathy_contact_set_presence (contact, MC_PRESENCE_UNSET);
711 empathy_contact_set_handle (contact, 0);
714 static void
715 tp_contact_factory_connection_invalidated_cb (EmpathyTpContactFactory *tp_factory)
717 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
719 DEBUG ("Connection invalidated");
721 g_object_unref (priv->connection);
722 priv->connection = NULL;
723 priv->ready = FALSE;
724 g_object_notify (G_OBJECT (tp_factory), "ready");
727 g_list_foreach (priv->contacts,
728 tp_contact_factory_disconnect_contact_foreach,
729 tp_factory);
732 static void
733 tp_contact_factory_ready (EmpathyTpContactFactory *tp_factory)
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;
742 DEBUG ("Connection ready");
744 priv->ready = TRUE;
745 g_object_notify (G_OBJECT (tp_factory), "ready");
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);
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;
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 (!EMP_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));
791 if (EMP_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));
797 handle_needed_contacts = g_list_reverse (handle_needed_contacts);
798 id_needed_contacts = g_list_reverse (id_needed_contacts);
800 tp_cli_connection_call_request_handles (priv->connection,
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));
808 tp_cli_connection_call_inspect_handles (priv->connection,
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));
816 tp_contact_factory_request_everything ((EmpathyTpContactFactory*) tp_factory,
817 id_needed);
819 g_array_free (handle_needed, TRUE);
820 g_array_free (id_needed, TRUE);
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)
830 EmpathyTpContactFactory *self = EMPATHY_TP_CONTACT_FACTORY (weak_object);
831 EmpathyTpContactFactoryPriv *priv = GET_PRIV (self);
832 GPtrArray *classes;
833 guint i;
835 if (error != NULL) {
836 DEBUG ("Error: %s", error->message);
837 tp_contact_factory_ready (self);
838 return;
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 GList *l;
848 g_value_init (&class, TP_STRUCT_TYPE_REQUESTABLE_CHANNEL_CLASS);
849 g_value_set_static_boxed (&class, g_ptr_array_index (classes, i));
851 dbus_g_type_struct_get (&class,
852 0, &fixed_prop,
853 G_MAXUINT);
855 chan_type = g_hash_table_lookup (fixed_prop,
856 TP_IFACE_CHANNEL ".ChannelType");
857 if (chan_type == NULL ||
858 tp_strdiff (g_value_get_string (chan_type),
859 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER)) {
860 continue;
863 handle_type = g_hash_table_lookup (fixed_prop,
864 TP_IFACE_CHANNEL ".TargetHandleType");
865 if (handle_type == NULL ||
866 g_value_get_uint (handle_type) != TP_HANDLE_TYPE_CONTACT) {
867 continue;
870 /* We can request file transfer channel to contacts. */
871 priv->can_request_ft = TRUE;
873 /* Update the capabilities of all contacts */
874 for (l = priv->contacts; l != NULL; l = g_list_next (l)) {
875 EmpathyContact *contact = l->data;
876 EmpathyCapabilities caps;
878 caps = empathy_contact_get_capabilities (contact);
879 empathy_contact_set_capabilities (contact, caps |
880 EMPATHY_CAPABILITIES_FT);
882 break;
885 tp_contact_factory_ready (self);
888 static void
889 tp_contact_factory_got_avatar_requirements_cb (TpConnection *proxy,
890 const gchar **mime_types,
891 guint min_width,
892 guint min_height,
893 guint max_width,
894 guint max_height,
895 guint max_size,
896 const GError *error,
897 gpointer user_data,
898 GObject *tp_factory)
900 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
902 if (error) {
903 DEBUG ("Failed to get avatar requirements: %s", error->message);
904 /* We'll just leave avatar_mime_types as NULL; the
905 * avatar-setting code can use this as a signal that you can't
906 * set avatars.
908 } else {
909 priv->avatar_mime_types = g_strdupv ((gchar **) mime_types);
910 priv->avatar_min_width = min_width;
911 priv->avatar_min_height = min_height;
912 priv->avatar_max_width = max_width;
913 priv->avatar_max_height = max_height;
914 priv->avatar_max_size = max_size;
917 /* Can we request file transfer channels? */
918 tp_cli_dbus_properties_call_get (priv->connection, -1,
919 TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
920 "RequestableChannelClasses",
921 get_requestable_channel_classes_cb, NULL, NULL,
922 G_OBJECT (tp_factory));
925 static void
926 tp_contact_factory_got_self_handle_cb (TpConnection *proxy,
927 guint handle,
928 const GError *error,
929 gpointer user_data,
930 GObject *tp_factory)
932 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
934 if (error) {
935 DEBUG ("Failed to get self handles: %s", error->message);
936 return;
939 empathy_contact_set_handle (priv->user, handle);
941 /* Get avatar requirements for this connection */
942 tp_cli_connection_interface_avatars_call_get_avatar_requirements (
943 priv->connection,
945 tp_contact_factory_got_avatar_requirements_cb,
946 NULL, NULL,
947 tp_factory);
950 static void
951 tp_contact_factory_connection_ready_cb (EmpathyTpContactFactory *tp_factory)
953 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
955 /* Get our own handle */
956 tp_cli_connection_call_get_self_handle (priv->connection,
958 tp_contact_factory_got_self_handle_cb,
959 NULL, NULL,
960 G_OBJECT (tp_factory));
963 static void
964 tp_contact_factory_status_updated (EmpathyTpContactFactory *tp_factory)
966 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
967 gboolean connection_ready;
968 MissionControl *mc;
970 if (priv->connection) {
971 /* We already have our connection object */
972 return;
975 mc = empathy_mission_control_dup_singleton ();
976 priv->connection = mission_control_get_tpconnection (mc, priv->account, NULL);
977 if (!priv->connection) {
978 return;
981 /* We got a new connection, wait for it to be ready */
982 g_signal_connect_swapped (priv->connection, "invalidated",
983 G_CALLBACK (tp_contact_factory_connection_invalidated_cb),
984 tp_factory);
986 g_object_get (priv->connection, "connection-ready", &connection_ready, NULL);
987 if (connection_ready) {
988 tp_contact_factory_connection_ready_cb (tp_factory);
989 } else {
990 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
991 G_CALLBACK (tp_contact_factory_connection_ready_cb),
992 tp_factory);
995 g_object_unref (mc);
998 static void
999 tp_contact_factory_account_connection_cb (EmpathyAccountManager *account_manager,
1000 McAccount *account,
1001 TpConnectionStatusReason reason,
1002 TpConnectionStatus current,
1003 TpConnectionStatus previous,
1004 EmpathyTpContactFactory *tp_factory)
1006 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1008 if (account && empathy_account_equal (account, priv->account)) {
1009 tp_contact_factory_status_updated (tp_factory);
1013 static void
1014 tp_contact_factory_add_contact (EmpathyTpContactFactory *tp_factory,
1015 EmpathyContact *contact)
1017 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1019 g_object_weak_ref (G_OBJECT (contact),
1020 tp_contact_factory_weak_notify,
1021 tp_factory);
1022 priv->contacts = g_list_prepend (priv->contacts, contact);
1024 DEBUG ("Contact added: %s (%d)",
1025 empathy_contact_get_id (contact),
1026 empathy_contact_get_handle (contact));
1029 static void
1030 tp_contact_factory_hold_handles_cb (TpConnection *connection,
1031 const GError *error,
1032 gpointer userdata,
1033 GObject *tp_factory)
1035 if (error) {
1036 DEBUG ("Failed to hold handles: %s", error->message);
1040 EmpathyContact *
1041 empathy_tp_contact_factory_get_user (EmpathyTpContactFactory *tp_factory)
1043 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1045 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1047 return g_object_ref (priv->user);
1050 static void
1051 contact_created (EmpathyTpContactFactory *self,
1052 EmpathyContact *contact)
1054 EmpathyTpContactFactoryPriv *priv = GET_PRIV (self);
1056 if (priv->can_request_ft)
1058 /* Set the FT capability */
1059 /* FIXME: We should use the futur ContactCapabilities interface */
1060 EmpathyCapabilities caps;
1062 caps = empathy_contact_get_capabilities (contact);
1063 caps |= EMPATHY_CAPABILITIES_FT;
1065 empathy_contact_set_capabilities (contact, caps);
1068 tp_contact_factory_add_contact (self, contact);
1071 EmpathyContact *
1072 empathy_tp_contact_factory_get_from_id (EmpathyTpContactFactory *tp_factory,
1073 const gchar *id)
1075 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1076 EmpathyContact *contact;
1078 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1079 g_return_val_if_fail (id != NULL, NULL);
1081 /* Check if the contact already exists */
1082 contact = tp_contact_factory_find_by_id (tp_factory, id);
1083 if (contact) {
1084 return g_object_ref (contact);
1087 /* Create new contact */
1088 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1089 "account", priv->account,
1090 "id", id,
1091 NULL);
1092 contact_created (tp_factory, contact);
1094 if (priv->ready) {
1095 const gchar *contact_ids[] = {id, NULL};
1096 GList *contacts;
1098 contacts = g_list_prepend (NULL, g_object_ref (contact));
1099 tp_cli_connection_call_request_handles (priv->connection,
1101 TP_HANDLE_TYPE_CONTACT,
1102 contact_ids,
1103 tp_contact_factory_request_handles_cb,
1104 contacts, tp_contact_factory_list_free,
1105 G_OBJECT (tp_factory));
1108 return contact;
1111 EmpathyContact *
1112 empathy_tp_contact_factory_get_from_handle (EmpathyTpContactFactory *tp_factory,
1113 guint handle)
1115 EmpathyContact *contact;
1116 GArray *handles;
1117 GList *contacts;
1119 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1121 handles = g_array_new (FALSE, FALSE, sizeof (guint));
1122 g_array_append_val (handles, handle);
1124 contacts = empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
1125 g_array_free (handles, TRUE);
1127 contact = contacts ? contacts->data : NULL;
1128 g_list_free (contacts);
1130 return contact;
1133 GList *
1134 empathy_tp_contact_factory_get_from_handles (EmpathyTpContactFactory *tp_factory,
1135 const GArray *handles)
1137 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1138 GList *contacts = NULL;
1139 GArray *new_handles;
1140 GList *new_contacts = NULL;
1141 guint i;
1143 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), NULL);
1144 g_return_val_if_fail (handles != NULL, NULL);
1146 /* Search all contacts we already have */
1147 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
1148 for (i = 0; i < handles->len; i++) {
1149 EmpathyContact *contact;
1150 guint handle;
1152 handle = g_array_index (handles, guint, i);
1153 if (handle == 0) {
1154 continue;
1157 contact = tp_contact_factory_find_by_handle (tp_factory, handle);
1158 if (contact) {
1159 contacts = g_list_prepend (contacts, g_object_ref (contact));
1160 } else {
1161 g_array_append_val (new_handles, handle);
1165 if (new_handles->len == 0) {
1166 g_array_free (new_handles, TRUE);
1167 return contacts;
1170 /* Create new contacts */
1171 for (i = 0; i < new_handles->len; i++) {
1172 EmpathyContact *contact;
1173 guint handle;
1175 handle = g_array_index (new_handles, guint, i);
1177 contact = g_object_new (EMPATHY_TYPE_CONTACT,
1178 "account", priv->account,
1179 "handle", handle,
1180 NULL);
1181 contact_created (tp_factory, contact);
1182 contacts = g_list_prepend (contacts, contact);
1183 new_contacts = g_list_prepend (new_contacts, g_object_ref (contact));
1185 new_contacts = g_list_reverse (new_contacts);
1187 if (priv->ready) {
1188 /* Get the IDs of all new handles */
1189 tp_cli_connection_call_inspect_handles (priv->connection,
1191 TP_HANDLE_TYPE_CONTACT,
1192 new_handles,
1193 tp_contact_factory_inspect_handles_cb,
1194 new_contacts, tp_contact_factory_list_free,
1195 G_OBJECT (tp_factory));
1197 /* Hold all new handles. */
1198 /* FIXME: Should be unholded when removed from the factory */
1199 tp_cli_connection_call_hold_handles (priv->connection,
1201 TP_HANDLE_TYPE_CONTACT,
1202 new_handles,
1203 tp_contact_factory_hold_handles_cb,
1204 NULL, NULL,
1205 G_OBJECT (tp_factory));
1207 tp_contact_factory_request_everything (tp_factory, new_handles);
1210 g_array_free (new_handles, TRUE);
1212 return contacts;
1215 void
1216 empathy_tp_contact_factory_set_alias (EmpathyTpContactFactory *tp_factory,
1217 EmpathyContact *contact,
1218 const gchar *alias)
1220 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1221 GHashTable *new_alias;
1222 guint handle;
1224 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1225 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1226 g_return_if_fail (priv->ready);
1227 g_return_if_fail (empathy_account_equal (empathy_contact_get_account (contact),
1228 priv->account));
1230 handle = empathy_contact_get_handle (contact);
1232 DEBUG ("Setting alias for contact %s (%d) to %s",
1233 empathy_contact_get_id (contact),
1234 handle, alias);
1236 new_alias = g_hash_table_new_full (g_direct_hash,
1237 g_direct_equal,
1238 NULL,
1239 g_free);
1241 g_hash_table_insert (new_alias,
1242 GUINT_TO_POINTER (handle),
1243 g_strdup (alias));
1245 tp_cli_connection_interface_aliasing_call_set_aliases (priv->connection,
1247 new_alias,
1248 tp_contact_factory_set_aliases_cb,
1249 NULL, NULL,
1250 G_OBJECT (tp_factory));
1252 g_hash_table_destroy (new_alias);
1255 void
1256 empathy_tp_contact_factory_set_avatar (EmpathyTpContactFactory *tp_factory,
1257 const gchar *data,
1258 gsize size,
1259 const gchar *mime_type)
1261 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1263 g_return_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory));
1264 g_return_if_fail (priv->ready);
1266 if (data && size > 0 && size < G_MAXUINT) {
1267 GArray avatar;
1269 avatar.data = (gchar*) data;
1270 avatar.len = size;
1272 DEBUG ("Setting avatar on account %s",
1273 mc_account_get_unique_name (priv->account));
1275 tp_cli_connection_interface_avatars_call_set_avatar (priv->connection,
1277 &avatar,
1278 mime_type,
1279 tp_contact_factory_set_avatar_cb,
1280 NULL, NULL,
1281 G_OBJECT (tp_factory));
1282 } else {
1283 DEBUG ("Clearing avatar on account %s",
1284 mc_account_get_unique_name (priv->account));
1286 tp_cli_connection_interface_avatars_call_clear_avatar (priv->connection,
1288 tp_contact_factory_clear_avatar_cb,
1289 NULL, NULL,
1290 G_OBJECT (tp_factory));
1294 gboolean
1295 empathy_tp_contact_factory_is_ready (EmpathyTpContactFactory *tp_factory)
1297 EmpathyTpContactFactoryPriv *priv = GET_PRIV (tp_factory);
1299 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_FACTORY (tp_factory), FALSE);
1301 return priv->ready;
1304 static void
1305 tp_contact_factory_get_property (GObject *object,
1306 guint param_id,
1307 GValue *value,
1308 GParamSpec *pspec)
1310 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1312 switch (param_id) {
1313 case PROP_ACCOUNT:
1314 g_value_set_object (value, priv->account);
1315 break;
1316 case PROP_READY:
1317 g_value_set_boolean (value, priv->ready);
1318 break;
1319 case PROP_MIME_TYPES:
1320 g_value_set_boxed (value, priv->avatar_mime_types);
1321 break;
1322 case PROP_MIN_WIDTH:
1323 g_value_set_uint (value, priv->avatar_min_width);
1324 break;
1325 case PROP_MIN_HEIGHT:
1326 g_value_set_uint (value, priv->avatar_min_height);
1327 break;
1328 case PROP_MAX_WIDTH:
1329 g_value_set_uint (value, priv->avatar_max_width);
1330 break;
1331 case PROP_MAX_HEIGHT:
1332 g_value_set_uint (value, priv->avatar_max_height);
1333 break;
1334 case PROP_MAX_SIZE:
1335 g_value_set_uint (value, priv->avatar_max_size);
1336 break;
1337 default:
1338 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1339 break;
1343 static void
1344 tp_contact_factory_set_property (GObject *object,
1345 guint param_id,
1346 const GValue *value,
1347 GParamSpec *pspec)
1349 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1351 switch (param_id) {
1352 case PROP_ACCOUNT:
1353 priv->account = g_object_ref (g_value_get_object (value));
1354 break;
1355 default:
1356 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1357 break;
1361 static void
1362 tp_contact_factory_finalize (GObject *object)
1364 EmpathyTpContactFactoryPriv *priv = GET_PRIV (object);
1365 GList *l;
1367 DEBUG ("Finalized: %p (%s)", object,
1368 mc_account_get_normalized_name (priv->account));
1370 g_signal_handlers_disconnect_by_func (priv->account_manager,
1371 tp_contact_factory_account_connection_cb,
1372 object);
1374 for (l = priv->contacts; l; l = l->next) {
1375 g_object_weak_unref (G_OBJECT (l->data),
1376 tp_contact_factory_weak_notify,
1377 object);
1380 g_list_free (priv->contacts);
1381 g_object_unref (priv->account_manager);
1382 g_object_unref (priv->account);
1383 g_object_unref (priv->user);
1385 if (priv->connection) {
1386 g_signal_handlers_disconnect_by_func (priv->connection,
1387 tp_contact_factory_connection_invalidated_cb,
1388 object);
1389 g_object_unref (priv->connection);
1392 g_strfreev (priv->avatar_mime_types);
1394 G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->finalize (object);
1397 static GObject *
1398 tp_contact_factory_constructor (GType type,
1399 guint n_props,
1400 GObjectConstructParam *props)
1402 GObject *tp_factory;
1403 EmpathyTpContactFactoryPriv *priv;
1405 tp_factory = G_OBJECT_CLASS (empathy_tp_contact_factory_parent_class)->constructor (type, n_props, props);
1406 priv = GET_PRIV (tp_factory);
1408 priv->ready = FALSE;
1409 priv->user = empathy_contact_new (priv->account);
1410 empathy_contact_set_is_user (priv->user, TRUE);
1411 tp_contact_factory_add_contact ((EmpathyTpContactFactory*) tp_factory, priv->user);
1412 tp_contact_factory_status_updated (EMPATHY_TP_CONTACT_FACTORY (tp_factory));
1414 return tp_factory;
1417 static void
1418 empathy_tp_contact_factory_class_init (EmpathyTpContactFactoryClass *klass)
1420 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1422 object_class->finalize = tp_contact_factory_finalize;
1423 object_class->constructor = tp_contact_factory_constructor;
1424 object_class->get_property = tp_contact_factory_get_property;
1425 object_class->set_property = tp_contact_factory_set_property;
1427 g_object_class_install_property (object_class,
1428 PROP_ACCOUNT,
1429 g_param_spec_object ("account",
1430 "Factory's Account",
1431 "The account associated with the factory",
1432 MC_TYPE_ACCOUNT,
1433 G_PARAM_READWRITE |
1434 G_PARAM_CONSTRUCT_ONLY |
1435 G_PARAM_STATIC_STRINGS));
1436 g_object_class_install_property (object_class,
1437 PROP_READY,
1438 g_param_spec_boolean ("ready",
1439 "Whether the factory is ready",
1440 "TRUE once the factory is ready to be used",
1441 FALSE,
1442 G_PARAM_READABLE |
1443 G_PARAM_STATIC_STRINGS));
1444 g_object_class_install_property (object_class,
1445 PROP_MIME_TYPES,
1446 g_param_spec_boxed ("avatar-mime-types",
1447 "Supported MIME types for avatars",
1448 "Types of images that may be set as "
1449 "avatars on this connection. Only valid "
1450 "once 'ready' becomes TRUE.",
1451 G_TYPE_STRV,
1452 G_PARAM_READABLE |
1453 G_PARAM_STATIC_STRINGS));
1454 g_object_class_install_property (object_class,
1455 PROP_MIN_WIDTH,
1456 g_param_spec_uint ("avatar-min-width",
1457 "Minimum width for avatars",
1458 "Minimum width of avatar that may be set. "
1459 "Only valid once 'ready' becomes TRUE.",
1461 G_MAXUINT,
1463 G_PARAM_READABLE |
1464 G_PARAM_STATIC_STRINGS));
1465 g_object_class_install_property (object_class,
1466 PROP_MIN_HEIGHT,
1467 g_param_spec_uint ("avatar-min-height",
1468 "Minimum height for avatars",
1469 "Minimum height of avatar that may be set. "
1470 "Only valid once 'ready' becomes TRUE.",
1472 G_MAXUINT,
1474 G_PARAM_READABLE |
1475 G_PARAM_STATIC_STRINGS));
1476 g_object_class_install_property (object_class,
1477 PROP_MAX_WIDTH,
1478 g_param_spec_uint ("avatar-max-width",
1479 "Maximum width for avatars",
1480 "Maximum width of avatar that may be set "
1481 "or 0 if there is no maximum. "
1482 "Only valid once 'ready' becomes TRUE.",
1484 G_MAXUINT,
1486 G_PARAM_READABLE |
1487 G_PARAM_STATIC_STRINGS));
1488 g_object_class_install_property (object_class,
1489 PROP_MAX_HEIGHT,
1490 g_param_spec_uint ("avatar-max-height",
1491 "Maximum height for avatars",
1492 "Maximum height of avatar that may be set "
1493 "or 0 if there is no maximum. "
1494 "Only valid once 'ready' becomes TRUE.",
1496 G_MAXUINT,
1498 G_PARAM_READABLE |
1499 G_PARAM_STATIC_STRINGS));
1500 g_object_class_install_property (object_class,
1501 PROP_MAX_SIZE,
1502 g_param_spec_uint ("avatar-max-size",
1503 "Maximum size for avatars in bytes",
1504 "Maximum file size of avatar that may be "
1505 "set or 0 if there is no maximum. "
1506 "Only valid once 'ready' becomes TRUE.",
1508 G_MAXUINT,
1510 G_PARAM_READABLE |
1511 G_PARAM_STATIC_STRINGS));
1514 g_type_class_add_private (object_class, sizeof (EmpathyTpContactFactoryPriv));
1517 static void
1518 empathy_tp_contact_factory_init (EmpathyTpContactFactory *tp_factory)
1520 EmpathyTpContactFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (tp_factory,
1521 EMPATHY_TYPE_TP_CONTACT_FACTORY, EmpathyTpContactFactoryPriv);
1523 tp_factory->priv = priv;
1524 priv->account_manager = empathy_account_manager_dup_singleton ();
1526 g_signal_connect (priv->account_manager, "account-connection-changed",
1527 G_CALLBACK (tp_contact_factory_account_connection_cb),
1528 tp_factory);
1530 priv->can_request_ft = FALSE;
1533 EmpathyTpContactFactory *
1534 empathy_tp_contact_factory_new (McAccount *account)
1536 return g_object_new (EMPATHY_TYPE_TP_CONTACT_FACTORY,
1537 "account", account,
1538 NULL);