1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Xavier Claessens <xclaesse@gmail.com>
4 * Copyright (C) 2007 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Xavier Claessens <xclaesse@gmail.com>
28 #include <libtelepathy/tp-helpers.h>
29 #include <libtelepathy/tp-conn.h>
30 #include <libtelepathy/tp-chan.h>
31 #include <libtelepathy/tp-chan-type-contact-list-gen.h>
32 #include <libtelepathy/tp-conn-iface-aliasing-gen.h>
33 #include <libtelepathy/tp-conn-iface-presence-gen.h>
34 #include <libtelepathy/tp-conn-iface-avatars-gen.h>
36 #include "empathy-tp-contact-list.h"
37 #include "empathy-contact-list.h"
38 #include "empathy-tp-group.h"
39 #include "empathy-debug.h"
40 #include "empathy-utils.h"
42 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
43 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv))
45 #define DEBUG_DOMAIN "TpContactList"
46 #define MAX_AVATAR_REQUESTS 10
48 struct _EmpathyTpContactListPriv {
52 EmpathyContact *user_contact;
55 EmpathyTpGroup *publish;
56 EmpathyTpGroup *subscribe;
63 DBusGProxy *aliasing_iface;
64 DBusGProxy *avatars_iface;
65 DBusGProxy *presence_iface;
67 GList *avatar_requests_queue;
71 TP_CONTACT_LIST_TYPE_PUBLISH,
72 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
73 TP_CONTACT_LIST_TYPE_UNKNOWN,
74 TP_CONTACT_LIST_TYPE_COUNT
83 EmpathyTpContactList *list;
85 } TpContactListAvatarRequestData;
88 EmpathyTpContactList *list;
90 } TpContactListAliasesRequestData;
92 static void empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass);
93 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
94 static void empathy_tp_contact_list_init (EmpathyTpContactList *list);
95 static void tp_contact_list_finalize (GObject *object);
96 static void tp_contact_list_finalize_proxies (EmpathyTpContactList *list);
97 static void tp_contact_list_setup (EmpathyContactList *list);
98 static EmpathyContact * tp_contact_list_find (EmpathyContactList *list,
100 static void tp_contact_list_add (EmpathyContactList *list,
101 EmpathyContact *contact,
102 const gchar *message);
103 static void tp_contact_list_remove (EmpathyContactList *list,
104 EmpathyContact *contact,
105 const gchar *message);
106 static GList * tp_contact_list_get_members (EmpathyContactList *list);
107 static GList * tp_contact_list_get_local_pending (EmpathyContactList *list);
108 static void tp_contact_list_process_pending (EmpathyContactList *list,
109 EmpathyContact *contact,
111 static void tp_contact_list_remove_local_pending (EmpathyTpContactList *list,
112 EmpathyContact *contact);
113 static void tp_contact_list_contact_removed_foreach (guint handle,
114 EmpathyContact *contact,
115 EmpathyTpContactList *list);
116 static void tp_contact_list_destroy_cb (DBusGProxy *proxy,
117 EmpathyTpContactList *list);
118 static gboolean tp_contact_list_find_foreach (guint handle,
119 EmpathyContact *contact,
121 static void tp_contact_list_newchannel_cb (DBusGProxy *proxy,
122 const gchar *object_path,
123 const gchar *channel_type,
124 TelepathyHandleType handle_type,
125 guint channel_handle,
126 gboolean suppress_handle,
127 EmpathyTpContactList *list);
128 static TpContactListType tp_contact_list_get_type (EmpathyTpContactList *list,
129 EmpathyTpGroup *group);
130 static void tp_contact_list_added_cb (EmpathyTpGroup *group,
134 const gchar *message,
135 EmpathyTpContactList *list);
136 static void tp_contact_list_removed_cb (EmpathyTpGroup *group,
140 const gchar *message,
141 EmpathyTpContactList *list);
142 static void tp_contact_list_pending_cb (EmpathyTpGroup *group,
146 const gchar *message,
147 EmpathyTpContactList *list);
148 static void tp_contact_list_groups_updated_cb (EmpathyContact *contact,
150 EmpathyTpContactList *list);
151 static void tp_contact_list_name_updated_cb (EmpathyContact *contact,
153 EmpathyTpContactList *list);
154 static void tp_contact_list_update_groups_foreach (gchar *object_path,
155 EmpathyTpGroup *group,
156 TpContactListData *data);
157 static EmpathyTpGroup * tp_contact_list_get_group (EmpathyTpContactList *list,
159 static gboolean tp_contact_list_find_group (gchar *key,
160 EmpathyTpGroup *group,
162 static void tp_contact_list_get_groups_foreach (gchar *key,
163 EmpathyTpGroup *group,
165 static void tp_contact_list_group_channel_closed_cb (TpChan *channel,
166 EmpathyTpContactList *list);
167 static void tp_contact_list_group_members_added_cb (EmpathyTpGroup *group,
171 const gchar *message,
172 EmpathyTpContactList *list);
173 static void tp_contact_list_group_members_removed_cb (EmpathyTpGroup *group,
177 const gchar *message,
178 EmpathyTpContactList *list);
179 static void tp_contact_list_get_info (EmpathyTpContactList *list,
181 static void tp_contact_list_request_avatar (EmpathyTpContactList *list,
183 static void tp_contact_list_start_avatar_requests (EmpathyTpContactList *list);
184 static void tp_contact_list_avatar_update_cb (DBusGProxy *proxy,
187 EmpathyTpContactList *list);
188 static void tp_contact_list_request_avatar_cb (DBusGProxy *proxy,
192 TpContactListAvatarRequestData *data);
193 static void tp_contact_list_aliases_update_cb (DBusGProxy *proxy,
195 EmpathyTpContactList *list);
196 static void tp_contact_list_request_aliases_cb (DBusGProxy *proxy,
197 gchar **contact_names,
199 TpContactListAliasesRequestData *data);
200 static void tp_contact_list_presence_update_cb (DBusGProxy *proxy,
201 GHashTable *handle_table,
202 EmpathyTpContactList *list);
203 static void tp_contact_list_parse_presence_foreach (guint handle,
204 GValueArray *presence_struct,
205 EmpathyTpContactList *list);
206 static void tp_contact_list_presences_table_foreach (const gchar *state_str,
207 GHashTable *presences_table,
208 EmpathyPresence **presence);
209 static void tp_contact_list_status_changed_cb (MissionControl *mc,
210 TelepathyConnectionStatus status,
212 TelepathyConnectionStatusReason reason,
213 const gchar *unique_name,
214 EmpathyTpContactList *list);
221 static guint signals[LAST_SIGNAL];
222 static guint n_avatar_requests = 0;
224 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
225 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
226 tp_contact_list_iface_init));
229 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
231 GObjectClass *object_class = G_OBJECT_CLASS (klass);
233 object_class->finalize = tp_contact_list_finalize;
236 g_signal_new ("destroy",
237 G_TYPE_FROM_CLASS (klass),
241 g_cclosure_marshal_VOID__VOID,
245 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
249 tp_contact_list_iface_init (EmpathyContactListIface *iface)
251 iface->setup = tp_contact_list_setup;
252 iface->find = tp_contact_list_find;
253 iface->add = tp_contact_list_add;
254 iface->remove = tp_contact_list_remove;
255 iface->get_members = tp_contact_list_get_members;
256 iface->get_local_pending = tp_contact_list_get_local_pending;
257 iface->process_pending = tp_contact_list_process_pending;
261 empathy_tp_contact_list_init (EmpathyTpContactList *list)
263 EmpathyTpContactListPriv *priv;
265 priv = GET_PRIV (list);
267 priv->groups = g_hash_table_new_full (g_str_hash,
269 (GDestroyNotify) g_free,
270 (GDestroyNotify) g_object_unref);
271 priv->contacts = g_hash_table_new_full (g_direct_hash,
274 (GDestroyNotify) g_object_unref);
278 tp_contact_list_finalize (GObject *object)
280 EmpathyTpContactListPriv *priv;
281 EmpathyTpContactList *list;
283 list = EMPATHY_TP_CONTACT_LIST (object);
284 priv = GET_PRIV (list);
286 empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
288 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
289 "AccountStatusChanged",
290 G_CALLBACK (tp_contact_list_status_changed_cb),
293 tp_contact_list_finalize_proxies (list);
296 g_object_unref (priv->tp_conn);
298 if (priv->subscribe) {
299 g_object_unref (priv->subscribe);
302 g_object_unref (priv->publish);
305 g_object_unref (priv->account);
306 g_object_unref (priv->user_contact);
307 g_object_unref (priv->mc);
308 g_hash_table_destroy (priv->groups);
309 g_hash_table_destroy (priv->contacts);
311 g_list_foreach (priv->local_pending, (GFunc) empathy_contact_list_info_free, NULL);
312 g_list_free (priv->local_pending);
314 g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
315 g_list_free (priv->members);
317 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
320 EmpathyTpContactList *
321 empathy_tp_contact_list_new (McAccount *account)
323 EmpathyTpContactListPriv *priv;
324 EmpathyTpContactList *list;
327 GError *error = NULL;
329 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
331 mc = empathy_mission_control_new ();
333 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
334 /* The account is not connected, nothing to do. */
338 list = g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST, NULL);
339 priv = GET_PRIV (list);
341 priv->tp_conn = mission_control_get_connection (mc, account, NULL);
342 priv->account = g_object_ref (account);
345 g_signal_connect (priv->tp_conn, "destroy",
346 G_CALLBACK (tp_contact_list_destroy_cb),
348 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
349 "AccountStatusChanged",
350 G_CALLBACK (tp_contact_list_status_changed_cb),
353 priv->aliasing_iface = tp_conn_get_interface (priv->tp_conn,
354 TELEPATHY_CONN_IFACE_ALIASING_QUARK);
355 priv->avatars_iface = tp_conn_get_interface (priv->tp_conn,
356 TELEPATHY_CONN_IFACE_AVATARS_QUARK);
357 priv->presence_iface = tp_conn_get_interface (priv->tp_conn,
358 TELEPATHY_CONN_IFACE_PRESENCE_QUARK);
360 if (priv->aliasing_iface) {
361 dbus_g_proxy_connect_signal (priv->aliasing_iface,
363 G_CALLBACK (tp_contact_list_aliases_update_cb),
367 if (priv->avatars_iface) {
368 dbus_g_proxy_connect_signal (priv->avatars_iface,
370 G_CALLBACK (tp_contact_list_avatar_update_cb),
374 if (priv->presence_iface) {
375 dbus_g_proxy_connect_signal (priv->presence_iface,
377 G_CALLBACK (tp_contact_list_presence_update_cb),
381 /* Get our own handle and contact */
382 if (!tp_conn_get_self_handle (DBUS_G_PROXY (priv->tp_conn),
384 empathy_debug (DEBUG_DOMAIN, "GetSelfHandle Error: %s",
385 error ? error->message : "No error given");
386 g_clear_error (&error);
388 priv->user_contact = empathy_tp_contact_list_get_from_handle (list, handle);
389 empathy_contact_set_is_user (priv->user_contact, TRUE);
396 tp_contact_list_setup (EmpathyContactList *list)
398 EmpathyTpContactListPriv *priv;
400 GError *error = NULL;
403 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
405 priv = GET_PRIV (list);
407 empathy_debug (DEBUG_DOMAIN, "setup contact list: %p", list);
410 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
411 G_CALLBACK (tp_contact_list_newchannel_cb),
414 /* Get existing channels */
415 if (!tp_conn_list_channels (DBUS_G_PROXY (priv->tp_conn),
418 empathy_debug (DEBUG_DOMAIN,
419 "Failed to get list of open channels: %s",
420 error ? error->message : "No error given");
421 g_clear_error (&error);
425 for (i = 0; channels->len > i; i++) {
426 GValueArray *chan_struct;
427 const gchar *object_path;
428 const gchar *chan_iface;
429 TelepathyHandleType handle_type;
432 chan_struct = g_ptr_array_index (channels, i);
433 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
434 chan_iface = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
435 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
436 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
438 tp_contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
439 object_path, chan_iface,
442 EMPATHY_TP_CONTACT_LIST (list));
444 g_value_array_free (chan_struct);
447 g_ptr_array_free (channels, TRUE);
450 static EmpathyContact *
451 tp_contact_list_find (EmpathyContactList *list,
454 EmpathyTpContactListPriv *priv;
456 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
458 priv = GET_PRIV (list);
460 return g_hash_table_find (priv->contacts,
461 (GHRFunc) tp_contact_list_find_foreach,
466 tp_contact_list_add (EmpathyContactList *list,
467 EmpathyContact *contact,
468 const gchar *message)
470 EmpathyTpContactListPriv *priv;
473 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
475 priv = GET_PRIV (list);
477 handle = empathy_contact_get_handle (contact);
478 empathy_tp_group_add_member (priv->subscribe, handle, message);
482 tp_contact_list_remove (EmpathyContactList *list,
483 EmpathyContact *contact,
484 const gchar *message)
486 EmpathyTpContactListPriv *priv;
489 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
491 priv = GET_PRIV (list);
493 handle = empathy_contact_get_handle (contact);
494 empathy_tp_group_remove_member (priv->subscribe, handle, message);
498 tp_contact_list_get_members (EmpathyContactList *list)
500 EmpathyTpContactListPriv *priv;
502 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
504 priv = GET_PRIV (list);
506 g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
507 return g_list_copy (priv->members);
511 tp_contact_list_get_local_pending (EmpathyContactList *list)
513 EmpathyTpContactListPriv *priv;
515 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
517 priv = GET_PRIV (list);
519 return g_list_copy (priv->local_pending);
523 tp_contact_list_process_pending (EmpathyContactList *list,
524 EmpathyContact *contact,
527 EmpathyTpContactListPriv *priv;
530 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
531 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
533 priv = GET_PRIV (list);
535 handle = empathy_contact_get_handle (contact);
537 empathy_tp_group_add_member (priv->publish, handle, NULL);
538 empathy_tp_group_add_member (priv->subscribe, handle, NULL);
540 empathy_tp_group_remove_member (priv->publish, handle, NULL);
545 empathy_tp_contact_list_get_account (EmpathyTpContactList *list)
547 EmpathyTpContactListPriv *priv;
549 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
551 priv = GET_PRIV (list);
553 return priv->account;
557 empathy_tp_contact_list_get_user (EmpathyTpContactList *list)
559 EmpathyTpContactListPriv *priv;
561 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
563 priv = GET_PRIV (list);
565 return priv->user_contact;
569 empathy_tp_contact_list_get_from_id (EmpathyTpContactList *list,
572 EmpathyTpContactListPriv *priv;
573 EmpathyContact *contact;
574 const gchar *contact_ids[] = {id, NULL};
577 GError *error = NULL;
579 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
580 g_return_val_if_fail (id != NULL, NULL);
582 priv = GET_PRIV (list);
584 contact = tp_contact_list_find (EMPATHY_CONTACT_LIST (list), id);
586 return g_object_ref (contact);
589 /* The id is unknown, requests a new handle */
590 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
591 TP_HANDLE_TYPE_CONTACT,
594 empathy_debug (DEBUG_DOMAIN,
595 "RequestHandle for %s failed: %s", id,
596 error ? error->message : "No error given");
597 g_clear_error (&error);
601 handle = g_array_index(handles, guint, 0);
602 g_array_free (handles, TRUE);
604 return empathy_tp_contact_list_get_from_handle (list, handle);
608 empathy_tp_contact_list_get_from_handle (EmpathyTpContactList *list,
611 EmpathyContact *contact;
615 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
617 handles = g_array_new (FALSE, FALSE, sizeof (guint));
618 g_array_append_val (handles, handle);
620 contacts = empathy_tp_contact_list_get_from_handles (list, handles);
621 g_array_free (handles, TRUE);
627 contact = contacts->data;
628 g_list_free (contacts);
634 empathy_tp_contact_list_get_from_handles (EmpathyTpContactList *list,
637 EmpathyTpContactListPriv *priv;
638 gchar **handles_names;
641 GList *contacts = NULL;
643 GError *error = NULL;
645 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
646 g_return_val_if_fail (handles != NULL, NULL);
648 priv = GET_PRIV (list);
650 /* Search all handles we already have */
651 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
652 for (i = 0; i < handles->len; i++) {
653 EmpathyContact *contact;
656 handle = g_array_index (handles, guint, i);
662 contact = g_hash_table_lookup (priv->contacts,
663 GUINT_TO_POINTER (handle));
666 contacts = g_list_prepend (contacts,
667 g_object_ref (contact));
669 g_array_append_val (new_handles, handle);
673 if (new_handles->len == 0) {
674 g_array_free (new_handles, TRUE);
678 /* Holds all handles we don't have yet.
679 * FIXME: We should release them at some point. */
680 if (!tp_conn_hold_handles (DBUS_G_PROXY (priv->tp_conn),
681 TP_HANDLE_TYPE_CONTACT,
682 new_handles, &error)) {
683 empathy_debug (DEBUG_DOMAIN,
684 "HoldHandles Error: %s",
685 error ? error->message : "No error given");
686 g_clear_error (&error);
687 g_array_free (new_handles, TRUE);
691 /* Get the IDs of all new handles */
692 if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
693 TP_HANDLE_TYPE_CONTACT,
697 empathy_debug (DEBUG_DOMAIN,
698 "InspectHandle Error: %s",
699 error ? error->message : "No error given");
700 g_clear_error (&error);
701 g_array_free (new_handles, TRUE);
705 /* Create contact objects */
706 for (i = 0, id = handles_names; *id && i < new_handles->len; id++, i++) {
707 EmpathyContact *contact;
710 handle = g_array_index (new_handles, guint, i);
711 contact = g_object_new (EMPATHY_TYPE_CONTACT,
712 "account", priv->account,
717 if (!priv->presence_iface) {
718 EmpathyPresence *presence;
720 /* We have no presence iface, set default presence
722 presence = empathy_presence_new_full (MC_PRESENCE_AVAILABLE,
725 empathy_contact_set_presence (contact, presence);
726 g_object_unref (presence);
729 g_signal_connect (contact, "notify::groups",
730 G_CALLBACK (tp_contact_list_groups_updated_cb),
732 g_signal_connect (contact, "notify::name",
733 G_CALLBACK (tp_contact_list_name_updated_cb),
736 empathy_debug (DEBUG_DOMAIN, "new contact created: %s (%d)",
739 g_hash_table_insert (priv->contacts,
740 GUINT_TO_POINTER (handle),
743 contacts = g_list_prepend (contacts, g_object_ref (contact));
746 tp_contact_list_get_info (list, new_handles);
748 g_array_free (new_handles, TRUE);
749 g_strfreev (handles_names);
755 empathy_tp_contact_list_rename_group (EmpathyTpContactList *list,
756 const gchar *old_group,
757 const gchar *new_group)
759 EmpathyTpContactListPriv *priv;
760 EmpathyTpGroup *group;
763 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
764 g_return_if_fail (old_group != NULL);
765 g_return_if_fail (new_group != NULL);
767 priv = GET_PRIV (list);
769 group = g_hash_table_find (priv->groups,
770 (GHRFunc) tp_contact_list_find_group,
773 /* The group doesn't exists on this account */
777 empathy_debug (DEBUG_DOMAIN, "rename group %s to %s", group, new_group);
779 /* Remove all members from the old group */
780 members = empathy_tp_group_get_members (group);
781 empathy_tp_group_remove_members (group, members, "");
782 tp_contact_list_group_members_removed_cb (group, members,
784 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
786 g_hash_table_remove (priv->groups,
787 empathy_tp_group_get_object_path (group));
789 /* Add all members to the new group */
790 group = tp_contact_list_get_group (list, new_group);
792 empathy_tp_group_add_members (group, members, "");
797 empathy_tp_contact_list_get_groups (EmpathyTpContactList *list)
799 EmpathyTpContactListPriv *priv;
800 GList *groups = NULL;
802 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
804 priv = GET_PRIV (list);
806 g_hash_table_foreach (priv->groups,
807 (GHFunc) tp_contact_list_get_groups_foreach,
810 groups = g_list_sort (groups, (GCompareFunc) strcmp);
816 tp_contact_list_finalize_proxies (EmpathyTpContactList *list)
818 EmpathyTpContactListPriv *priv;
820 priv = GET_PRIV (list);
823 g_signal_handlers_disconnect_by_func (priv->tp_conn,
824 tp_contact_list_destroy_cb,
826 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
827 G_CALLBACK (tp_contact_list_newchannel_cb),
831 if (priv->aliasing_iface) {
832 dbus_g_proxy_disconnect_signal (priv->aliasing_iface,
834 G_CALLBACK (tp_contact_list_aliases_update_cb),
838 if (priv->avatars_iface) {
839 dbus_g_proxy_disconnect_signal (priv->avatars_iface,
841 G_CALLBACK (tp_contact_list_avatar_update_cb),
845 if (priv->presence_iface) {
846 dbus_g_proxy_disconnect_signal (priv->presence_iface,
848 G_CALLBACK (tp_contact_list_presence_update_cb),
854 tp_contact_list_destroy_cb (DBusGProxy *proxy,
855 EmpathyTpContactList *list)
857 EmpathyTpContactListPriv *priv;
859 priv = GET_PRIV (list);
861 empathy_debug (DEBUG_DOMAIN, "Connection destroyed... "
862 "Account disconnected or CM crashed");
864 /* DBus proxies should NOT be used anymore */
865 g_object_unref (priv->tp_conn);
866 priv->tp_conn = NULL;
867 priv->aliasing_iface = NULL;
868 priv->avatars_iface = NULL;
869 priv->presence_iface = NULL;
871 /* Remove all contacts */
872 g_hash_table_foreach (priv->contacts,
873 (GHFunc) tp_contact_list_contact_removed_foreach,
875 g_hash_table_remove_all (priv->contacts);
877 /* Tell the world to not use us anymore */
878 g_signal_emit (list, signals[DESTROY], 0);
882 tp_contact_list_contact_removed_foreach (guint handle,
883 EmpathyContact *contact,
884 EmpathyTpContactList *list)
886 g_signal_handlers_disconnect_by_func (contact,
887 tp_contact_list_groups_updated_cb,
889 g_signal_handlers_disconnect_by_func (contact,
890 tp_contact_list_name_updated_cb,
893 g_signal_emit_by_name (list, "contact-removed", contact);
897 tp_contact_list_block_contact (EmpathyTpContactList *list,
898 EmpathyContact *contact)
900 g_signal_handlers_block_by_func (contact,
901 tp_contact_list_groups_updated_cb,
903 g_signal_handlers_block_by_func (contact,
904 tp_contact_list_name_updated_cb,
909 tp_contact_list_unblock_contact (EmpathyTpContactList *list,
910 EmpathyContact *contact)
912 g_signal_handlers_unblock_by_func (contact,
913 tp_contact_list_groups_updated_cb,
915 g_signal_handlers_unblock_by_func (contact,
916 tp_contact_list_name_updated_cb,
921 tp_contact_list_find_foreach (guint handle,
922 EmpathyContact *contact,
925 if (strcmp (empathy_contact_get_id (contact), id) == 0) {
933 tp_contact_list_newchannel_cb (DBusGProxy *proxy,
934 const gchar *object_path,
935 const gchar *channel_type,
936 TelepathyHandleType handle_type,
937 guint channel_handle,
938 gboolean suppress_handle,
939 EmpathyTpContactList *list)
941 EmpathyTpContactListPriv *priv;
942 EmpathyTpGroup *group;
944 const gchar *bus_name;
947 priv = GET_PRIV (list);
949 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
955 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn));
956 new_chan = tp_chan_new (tp_get_bus (),
959 channel_type, handle_type, channel_handle);
961 if (handle_type == TP_HANDLE_TYPE_LIST) {
962 TpContactListType list_type;
964 group = empathy_tp_group_new (new_chan, priv->tp_conn);
966 list_type = tp_contact_list_get_type (list, group);
967 if (list_type == TP_CONTACT_LIST_TYPE_UNKNOWN) {
968 empathy_debug (DEBUG_DOMAIN,
969 "Type of contact list channel unknown: %s",
970 empathy_tp_group_get_name (group));
972 g_object_unref (new_chan);
973 g_object_unref (group);
976 empathy_debug (DEBUG_DOMAIN,
977 "New contact list channel of type: %d",
981 g_signal_connect (group, "members-added",
982 G_CALLBACK (tp_contact_list_added_cb),
984 g_signal_connect (group, "members-removed",
985 G_CALLBACK (tp_contact_list_removed_cb),
988 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
992 g_object_unref (priv->publish);
994 priv->publish = group;
996 /* Makes no sense to be in remote-pending */
997 g_signal_connect (group, "local-pending",
998 G_CALLBACK (tp_contact_list_pending_cb),
1001 pendings = empathy_tp_group_get_local_pending_members_with_info (group);
1005 pending = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1006 for (l = pendings; l; l = l->next) {
1007 EmpathyTpGroupInfo *info;
1011 g_array_insert_val (pending, 0, info->member);
1012 tp_contact_list_pending_cb (group, pending,
1018 g_array_free (pending, TRUE);
1019 empathy_tp_group_info_list_free (pendings);
1022 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1023 GArray *remote_pendings = NULL;
1025 if (priv->subscribe) {
1026 g_object_unref (priv->subscribe);
1028 priv->subscribe = group;
1030 /* Makes no sense to be in local-pending */
1031 g_signal_connect (group, "remote-pending",
1032 G_CALLBACK (tp_contact_list_pending_cb),
1034 empathy_tp_group_get_all_members (group,
1039 tp_contact_list_pending_cb (group, remote_pendings, 0,
1040 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1043 g_array_free (remote_pendings, TRUE);
1045 members = empathy_tp_group_get_members (group);
1048 tp_contact_list_added_cb (group, members, 0,
1049 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1051 g_array_free (members, TRUE);
1053 else if (handle_type == TP_HANDLE_TYPE_GROUP) {
1054 const gchar *object_path;
1056 object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (new_chan));
1057 if (g_hash_table_lookup (priv->groups, object_path)) {
1058 g_object_unref (new_chan);
1062 group = empathy_tp_group_new (new_chan, priv->tp_conn);
1064 empathy_debug (DEBUG_DOMAIN, "New server-side group channel: %s",
1065 empathy_tp_group_get_name (group));
1067 dbus_g_proxy_connect_signal (DBUS_G_PROXY (new_chan), "Closed",
1069 (tp_contact_list_group_channel_closed_cb),
1072 g_hash_table_insert (priv->groups, g_strdup (object_path), group);
1073 g_signal_connect (group, "members-added",
1074 G_CALLBACK (tp_contact_list_group_members_added_cb),
1076 g_signal_connect (group, "members-removed",
1077 G_CALLBACK (tp_contact_list_group_members_removed_cb),
1080 members = empathy_tp_group_get_members (group);
1081 tp_contact_list_group_members_added_cb (group, members, 0,
1082 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1084 g_array_free (members, TRUE);
1087 g_object_unref (new_chan);
1090 static TpContactListType
1091 tp_contact_list_get_type (EmpathyTpContactList *list,
1092 EmpathyTpGroup *group)
1094 EmpathyTpContactListPriv *priv;
1095 TpContactListType list_type;
1098 priv = GET_PRIV (list);
1100 name = empathy_tp_group_get_name (group);
1101 if (strcmp (name, "subscribe") == 0) {
1102 list_type = TP_CONTACT_LIST_TYPE_SUBSCRIBE;
1103 } else if (strcmp (name, "publish") == 0) {
1104 list_type = TP_CONTACT_LIST_TYPE_PUBLISH;
1106 list_type = TP_CONTACT_LIST_TYPE_UNKNOWN;
1113 tp_contact_list_added_cb (EmpathyTpGroup *group,
1117 const gchar *message,
1118 EmpathyTpContactList *list)
1120 EmpathyTpContactListPriv *priv;
1121 GList *added_list, *l;
1122 TpContactListType list_type;
1124 priv = GET_PRIV (list);
1126 list_type = tp_contact_list_get_type (list, group);
1128 added_list = empathy_tp_contact_list_get_from_handles (list, handles);
1129 for (l = added_list; l; l = l->next) {
1130 EmpathyContact *contact;
1131 EmpathySubscription subscription;
1133 contact = EMPATHY_CONTACT (l->data);
1135 empathy_debug (DEBUG_DOMAIN, "Contact '%s' added to list type %d",
1136 empathy_contact_get_name (contact),
1139 subscription = empathy_contact_get_subscription (contact);
1140 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1141 subscription |= EMPATHY_SUBSCRIPTION_FROM;
1143 else if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1144 subscription |= EMPATHY_SUBSCRIPTION_TO;
1145 tp_contact_list_remove_local_pending (list, contact);
1148 tp_contact_list_block_contact (list, contact);
1149 empathy_contact_set_subscription (contact, subscription);
1150 tp_contact_list_unblock_contact (list, contact);
1152 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1153 if (!g_list_find (priv->members, contact)) {
1154 priv->members = g_list_prepend (priv->members,
1155 g_object_ref (contact));
1156 g_signal_emit_by_name (list, "contact-added", contact);
1160 g_object_unref (contact);
1163 g_list_free (added_list);
1167 tp_contact_list_removed_cb (EmpathyTpGroup *group,
1171 const gchar *message,
1172 EmpathyTpContactList *list)
1174 EmpathyTpContactListPriv *priv;
1175 GList *removed_list, *l;
1176 TpContactListType list_type;
1178 priv = GET_PRIV (list);
1180 list_type = tp_contact_list_get_type (list, group);
1182 removed_list = empathy_tp_contact_list_get_from_handles (list, handles);
1183 for (l = removed_list; l; l = l->next) {
1184 EmpathyContact *contact;
1185 EmpathySubscription subscription;
1187 contact = EMPATHY_CONTACT (l->data);
1189 empathy_debug (DEBUG_DOMAIN, "Contact '%s' removed from list type %d",
1190 empathy_contact_get_name (contact),
1193 subscription = empathy_contact_get_subscription (contact);
1194 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1195 subscription &= !EMPATHY_SUBSCRIPTION_FROM;
1197 else if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1198 subscription &= !EMPATHY_SUBSCRIPTION_TO;
1199 tp_contact_list_remove_local_pending (list, contact);
1202 tp_contact_list_block_contact (list, contact);
1203 empathy_contact_set_subscription (contact, subscription);
1204 tp_contact_list_unblock_contact (list, contact);
1206 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1209 if ((l = g_list_find (priv->members, contact))) {
1210 g_signal_emit_by_name (list, "contact-removed", contact);
1211 priv->members = g_list_delete_link (priv->members, l);
1212 g_object_unref (contact);
1215 g_object_unref (contact);
1218 g_list_free (removed_list);
1222 tp_contact_list_pending_cb (EmpathyTpGroup *group,
1226 const gchar *message,
1227 EmpathyTpContactList *list)
1229 EmpathyTpContactListPriv *priv;
1230 GList *pending_list, *l;
1231 TpContactListType list_type;
1233 priv = GET_PRIV (list);
1235 list_type = tp_contact_list_get_type (list, group);
1237 pending_list = empathy_tp_contact_list_get_from_handles (list, handles);
1238 for (l = pending_list; l; l = l->next) {
1239 EmpathyContact *contact;
1241 contact = EMPATHY_CONTACT (l->data);
1243 empathy_debug (DEBUG_DOMAIN, "Contact '%s' pending in list type %d",
1244 empathy_contact_get_name (contact),
1247 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1248 if (!g_list_find (priv->members, contact)) {
1249 EmpathyContactListInfo *info;
1251 info = empathy_contact_list_info_new (contact, message);
1252 priv->local_pending = g_list_prepend (priv->local_pending,
1255 g_signal_emit_by_name (list, "local-pending",
1260 /* That contact wants our presence and he is
1261 * in our roster. Accept to publish our presence
1262 * without asking the user. */
1263 handle = empathy_contact_get_handle (contact);
1264 empathy_tp_group_add_member (priv->publish,
1268 else if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1269 if (!g_list_find (priv->members, contact)) {
1270 priv->members = g_list_prepend (priv->members,
1271 g_object_ref (contact));
1272 g_signal_emit_by_name (list, "contact-added", contact);
1276 g_object_unref (contact);
1279 g_list_free (pending_list);
1283 tp_contact_list_remove_local_pending (EmpathyTpContactList *list,
1284 EmpathyContact *contact)
1286 EmpathyTpContactListPriv *priv;
1289 priv = GET_PRIV (list);
1291 for (l = priv->local_pending; l; l = l->next) {
1292 EmpathyContactListInfo *info;
1295 if (empathy_contact_equal (contact, info->contact)) {
1296 empathy_debug (DEBUG_DOMAIN, "Contact no more local-pending: %s",
1297 empathy_contact_get_name (contact));
1299 priv->local_pending = g_list_delete_link (priv->local_pending, l);
1300 empathy_contact_list_info_free (info);
1307 tp_contact_list_groups_updated_cb (EmpathyContact *contact,
1309 EmpathyTpContactList *list)
1311 EmpathyTpContactListPriv *priv;
1312 TpContactListData data;
1315 priv = GET_PRIV (list);
1317 /* Make sure all groups are created */
1318 groups = empathy_contact_get_groups (contact);
1319 for (l = groups; l; l = l->next) {
1320 tp_contact_list_get_group (list, l->data);
1323 data.handle = empathy_contact_get_handle (contact);
1324 data.new_groups = groups;
1326 g_hash_table_foreach (priv->groups,
1327 (GHFunc) tp_contact_list_update_groups_foreach,
1332 tp_contact_list_name_updated_cb (EmpathyContact *contact,
1334 EmpathyTpContactList *list)
1336 EmpathyTpContactListPriv *priv;
1337 GHashTable *new_alias;
1338 const gchar *new_name;
1340 GError *error = NULL;
1342 priv = GET_PRIV (list);
1344 if (!priv->aliasing_iface) {
1348 handle = empathy_contact_get_handle (contact);
1349 new_name = empathy_contact_get_name (contact);
1351 empathy_debug (DEBUG_DOMAIN, "renaming handle %d to %s",
1354 new_alias = g_hash_table_new_full (g_direct_hash,
1359 g_hash_table_insert (new_alias,
1360 GUINT_TO_POINTER (handle),
1361 g_strdup (new_name));
1363 if (!tp_conn_iface_aliasing_set_aliases (priv->aliasing_iface,
1366 empathy_debug (DEBUG_DOMAIN,
1367 "Couldn't rename contact: %s",
1368 error ? error->message : "No error given");
1369 g_clear_error (&error);
1372 g_hash_table_destroy (new_alias);
1376 tp_contact_list_update_groups_foreach (gchar *object_path,
1377 EmpathyTpGroup *group,
1378 TpContactListData *data)
1381 gboolean found = FALSE;
1382 const gchar *group_name;
1385 is_member = empathy_tp_group_is_member (group, data->handle);
1386 group_name = empathy_tp_group_get_name (group);
1388 for (l = data->new_groups; l; l = l->next) {
1389 if (strcmp (group_name, l->data) == 0) {
1395 if (is_member && !found) {
1396 /* We are no longer member of this group */
1397 empathy_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1398 data->handle, group_name);
1399 empathy_tp_group_remove_member (group, data->handle, "");
1402 if (!is_member && found) {
1403 /* We are now member of this group */
1404 empathy_debug (DEBUG_DOMAIN, "Contact %d added to group '%s'",
1405 data->handle, group_name);
1406 empathy_tp_group_add_member (group, data->handle, "");
1410 static EmpathyTpGroup *
1411 tp_contact_list_get_group (EmpathyTpContactList *list,
1414 EmpathyTpContactListPriv *priv;
1415 EmpathyTpGroup *group;
1416 TpChan *group_channel;
1419 char *group_object_path;
1420 const char *names[2] = {name, NULL};
1421 GError *error = NULL;
1423 priv = GET_PRIV (list);
1425 group = g_hash_table_find (priv->groups,
1426 (GHRFunc) tp_contact_list_find_group,
1432 empathy_debug (DEBUG_DOMAIN, "creating new group: %s", name);
1434 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
1435 TP_HANDLE_TYPE_GROUP,
1439 empathy_debug (DEBUG_DOMAIN,
1440 "Couldn't request the creation of a new handle for group: %s",
1441 error ? error->message : "No error given");
1442 g_clear_error (&error);
1445 group_handle = g_array_index (handles, guint, 0);
1446 g_array_free (handles, TRUE);
1448 if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
1449 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1450 TP_HANDLE_TYPE_GROUP,
1455 empathy_debug (DEBUG_DOMAIN,
1456 "Couldn't request the creation of a new group channel: %s",
1457 error ? error->message : "No error given");
1458 g_clear_error (&error);
1462 group_channel = tp_chan_new (tp_get_bus (),
1463 dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn)),
1465 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1466 TP_HANDLE_TYPE_GROUP,
1469 dbus_g_proxy_connect_signal (DBUS_G_PROXY (group_channel),
1472 (tp_contact_list_group_channel_closed_cb),
1476 group = empathy_tp_group_new (group_channel, priv->tp_conn);
1477 g_hash_table_insert (priv->groups, group_object_path, group);
1478 g_signal_connect (group, "members-added",
1479 G_CALLBACK (tp_contact_list_group_members_added_cb),
1481 g_signal_connect (group, "members-removed",
1482 G_CALLBACK (tp_contact_list_group_members_removed_cb),
1489 tp_contact_list_find_group (gchar *key,
1490 EmpathyTpGroup *group,
1493 if (strcmp (group_name, empathy_tp_group_get_name (group)) == 0) {
1501 tp_contact_list_get_groups_foreach (gchar *key,
1502 EmpathyTpGroup *group,
1507 name = empathy_tp_group_get_name (group);
1508 *groups = g_list_append (*groups, g_strdup (name));
1512 tp_contact_list_group_channel_closed_cb (TpChan *channel,
1513 EmpathyTpContactList *list)
1515 EmpathyTpContactListPriv *priv;
1517 priv = GET_PRIV (list);
1519 g_hash_table_remove (priv->groups,
1520 dbus_g_proxy_get_path (DBUS_G_PROXY (channel)));
1524 tp_contact_list_group_members_added_cb (EmpathyTpGroup *group,
1528 const gchar *message,
1529 EmpathyTpContactList *list)
1531 EmpathyTpContactListPriv *priv;
1532 GList *added_list, *l;
1533 const gchar *group_name;
1535 priv = GET_PRIV (list);
1537 group_name = empathy_tp_group_get_name (group);
1538 added_list = empathy_tp_contact_list_get_from_handles (list, members);
1540 for (l = added_list; l; l = l->next) {
1541 EmpathyContact *contact;
1543 contact = EMPATHY_CONTACT (l->data);
1545 tp_contact_list_block_contact (list, contact);
1546 empathy_contact_add_group (contact, group_name);
1547 tp_contact_list_unblock_contact (list, contact);
1549 g_object_unref (contact);
1552 g_list_free (added_list);
1556 tp_contact_list_group_members_removed_cb (EmpathyTpGroup *group,
1560 const gchar *message,
1561 EmpathyTpContactList *list)
1563 EmpathyTpContactListPriv *priv;
1564 GList *removed_list, *l;
1565 const gchar *group_name;
1567 priv = GET_PRIV (list);
1569 group_name = empathy_tp_group_get_name (group);
1570 removed_list = empathy_tp_contact_list_get_from_handles (list, members);
1572 for (l = removed_list; l; l = l->next) {
1573 EmpathyContact *contact;
1577 tp_contact_list_block_contact (list, contact);
1578 empathy_contact_remove_group (contact, group_name);
1579 tp_contact_list_unblock_contact (list, contact);
1581 g_object_unref (contact);
1584 g_list_free (removed_list);
1588 tp_contact_list_get_info (EmpathyTpContactList *list,
1591 EmpathyTpContactListPriv *priv;
1592 GError *error = NULL;
1594 priv = GET_PRIV (list);
1596 if (priv->presence_iface) {
1597 /* FIXME: We should use GetPresence instead */
1598 if (!tp_conn_iface_presence_request_presence (priv->presence_iface,
1600 empathy_debug (DEBUG_DOMAIN,
1601 "Could not request presences: %s",
1602 error ? error->message : "No error given");
1603 g_clear_error (&error);
1607 if (priv->aliasing_iface) {
1608 TpContactListAliasesRequestData *data;
1610 data = g_slice_new (TpContactListAliasesRequestData);
1612 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
1614 tp_conn_iface_aliasing_request_aliases_async (priv->aliasing_iface,
1616 (tp_conn_iface_aliasing_request_aliases_reply)
1617 tp_contact_list_request_aliases_cb,
1621 if (priv->avatars_iface) {
1624 for (i = 0; i < handles->len; i++) {
1627 handle = g_array_index (handles, gint, i);
1628 tp_contact_list_request_avatar (list, handle);
1634 tp_contact_list_request_avatar (EmpathyTpContactList *list,
1637 EmpathyTpContactListPriv *priv;
1639 priv = GET_PRIV (list);
1641 /* We queue avatar requests to not send too many dbus async
1642 * calls at once. If we don't we reach the dbus's limit of
1644 priv->avatar_requests_queue = g_list_append (priv->avatar_requests_queue,
1645 GUINT_TO_POINTER (handle));
1646 tp_contact_list_start_avatar_requests (list);
1650 tp_contact_list_start_avatar_requests (EmpathyTpContactList *list)
1652 EmpathyTpContactListPriv *priv;
1653 TpContactListAvatarRequestData *data;
1655 priv = GET_PRIV (list);
1657 while (n_avatar_requests < MAX_AVATAR_REQUESTS &&
1658 priv->avatar_requests_queue) {
1659 data = g_slice_new (TpContactListAvatarRequestData);
1661 data->handle = GPOINTER_TO_UINT (priv->avatar_requests_queue->data);
1663 n_avatar_requests++;
1664 priv->avatar_requests_queue = g_list_remove (priv->avatar_requests_queue,
1665 priv->avatar_requests_queue->data);
1667 tp_conn_iface_avatars_request_avatar_async (priv->avatars_iface,
1669 (tp_conn_iface_avatars_request_avatar_reply)
1670 tp_contact_list_request_avatar_cb,
1676 tp_contact_list_avatar_update_cb (DBusGProxy *proxy,
1679 EmpathyTpContactList *list)
1681 EmpathyTpContactListPriv *priv;
1683 priv = GET_PRIV (list);
1685 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1686 /* We don't know this contact, skip */
1690 empathy_debug (DEBUG_DOMAIN, "Changing avatar for %d to %s",
1693 tp_contact_list_request_avatar (list, handle);
1697 tp_contact_list_request_avatar_cb (DBusGProxy *proxy,
1698 GArray *avatar_data,
1701 TpContactListAvatarRequestData *data)
1703 EmpathyContact *contact;
1705 contact = empathy_tp_contact_list_get_from_handle (data->list, data->handle);
1708 empathy_debug (DEBUG_DOMAIN, "Error requesting avatar for %s: %s",
1709 empathy_contact_get_name (contact),
1710 error ? error->message : "No error given");
1712 EmpathyAvatar *avatar;
1714 avatar = empathy_avatar_new (avatar_data->data,
1717 tp_contact_list_block_contact (data->list, contact);
1718 empathy_contact_set_avatar (contact, avatar);
1719 tp_contact_list_unblock_contact (data->list, contact);
1720 empathy_avatar_unref (avatar);
1723 n_avatar_requests--;
1724 tp_contact_list_start_avatar_requests (data->list);
1726 g_object_unref (contact);
1727 g_slice_free (TpContactListAvatarRequestData, data);
1731 tp_contact_list_aliases_update_cb (DBusGProxy *proxy,
1732 GPtrArray *renamed_handlers,
1733 EmpathyTpContactList *list)
1735 EmpathyTpContactListPriv *priv;
1738 priv = GET_PRIV (list);
1740 for (i = 0; renamed_handlers->len > i; i++) {
1743 GValueArray *renamed_struct;
1744 EmpathyContact *contact;
1746 renamed_struct = g_ptr_array_index (renamed_handlers, i);
1747 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
1748 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
1750 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1751 /* We don't know this contact, skip */
1755 if (G_STR_EMPTY (alias)) {
1759 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1760 tp_contact_list_block_contact (list, contact);
1761 empathy_contact_set_name (contact, alias);
1762 tp_contact_list_unblock_contact (list, contact);
1763 g_object_unref (contact);
1765 empathy_debug (DEBUG_DOMAIN, "contact %d renamed to %s (update cb)",
1771 tp_contact_list_request_aliases_cb (DBusGProxy *proxy,
1772 gchar **contact_names,
1774 TpContactListAliasesRequestData *data)
1780 empathy_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
1784 for (name = contact_names; *name && !error; name++) {
1785 EmpathyContact *contact;
1787 contact = empathy_tp_contact_list_get_from_handle (data->list,
1789 tp_contact_list_block_contact (data->list, contact);
1790 empathy_contact_set_name (contact, *name);
1791 tp_contact_list_unblock_contact (data->list, contact);
1792 g_object_unref (contact);
1794 empathy_debug (DEBUG_DOMAIN, "contact %d renamed to %s (request cb)",
1795 data->handles[i], *name);
1800 g_free (data->handles);
1801 g_slice_free (TpContactListAliasesRequestData, data);
1805 tp_contact_list_presence_update_cb (DBusGProxy *proxy,
1806 GHashTable *handle_table,
1807 EmpathyTpContactList *list)
1809 g_hash_table_foreach (handle_table,
1810 (GHFunc) tp_contact_list_parse_presence_foreach,
1815 tp_contact_list_parse_presence_foreach (guint handle,
1816 GValueArray *presence_struct,
1817 EmpathyTpContactList *list)
1819 EmpathyTpContactListPriv *priv;
1820 GHashTable *presences_table;
1821 EmpathyContact *contact;
1822 EmpathyPresence *presence = NULL;
1824 priv = GET_PRIV (list);
1826 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1827 /* We don't know this contact, skip */
1831 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1832 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
1834 g_hash_table_foreach (presences_table,
1835 (GHFunc) tp_contact_list_presences_table_foreach,
1838 empathy_debug (DEBUG_DOMAIN, "Presence changed for %s (%d) to %s (%d)",
1839 empathy_contact_get_name (contact),
1841 presence ? empathy_presence_get_status (presence) : "unset",
1842 presence ? empathy_presence_get_state (presence) : MC_PRESENCE_UNSET);
1844 tp_contact_list_block_contact (list, contact);
1845 empathy_contact_set_presence (contact, presence);
1846 tp_contact_list_unblock_contact (list, contact);
1848 g_object_unref (contact);
1852 tp_contact_list_presences_table_foreach (const gchar *state_str,
1853 GHashTable *presences_table,
1854 EmpathyPresence **presence)
1857 const GValue *message;
1859 state = empathy_presence_state_from_str (state_str);
1860 if ((state == MC_PRESENCE_UNSET) || (state == MC_PRESENCE_OFFLINE)) {
1865 g_object_unref (*presence);
1869 *presence = empathy_presence_new ();
1870 empathy_presence_set_state (*presence, state);
1872 message = g_hash_table_lookup (presences_table, "message");
1873 if (message != NULL) {
1874 empathy_presence_set_status (*presence,
1875 g_value_get_string (message));
1880 tp_contact_list_status_changed_cb (MissionControl *mc,
1881 TelepathyConnectionStatus status,
1882 McPresence presence,
1883 TelepathyConnectionStatusReason reason,
1884 const gchar *unique_name,
1885 EmpathyTpContactList *list)
1887 EmpathyTpContactListPriv *priv;
1890 priv = GET_PRIV (list);
1892 account = mc_account_lookup (unique_name);
1893 if (status != TP_CONN_STATUS_DISCONNECTED ||
1894 !empathy_account_equal (account, priv->account) ||
1896 g_object_unref (account);
1900 /* We are disconnected, do just like if the connection was destroyed */
1901 g_signal_handlers_disconnect_by_func (priv->tp_conn,
1902 tp_contact_list_destroy_cb,
1904 tp_contact_list_destroy_cb (DBUS_G_PROXY (priv->tp_conn), list);
1906 g_object_unref (account);