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>
27 #include <glib/gi18n.h>
29 #include <libtelepathy/tp-helpers.h>
30 #include <libtelepathy/tp-conn.h>
31 #include <libtelepathy/tp-chan.h>
32 #include <libtelepathy/tp-chan-type-contact-list-gen.h>
33 #include <libtelepathy/tp-conn-iface-aliasing-gen.h>
34 #include <libtelepathy/tp-conn-iface-presence-gen.h>
35 #include <libtelepathy/tp-conn-iface-avatars-gen.h>
37 #include "empathy-tp-contact-list.h"
38 #include "empathy-contact-list.h"
39 #include "empathy-tp-group.h"
40 #include "empathy-debug.h"
41 #include "empathy-utils.h"
43 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
44 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv))
46 #define DEBUG_DOMAIN "TpContactList"
47 #define MAX_AVATAR_REQUESTS 10
49 struct _EmpathyTpContactListPriv {
53 EmpathyContact *user_contact;
55 const gchar *protocol_group;
57 EmpathyTpGroup *publish;
58 EmpathyTpGroup *subscribe;
65 DBusGProxy *aliasing_iface;
66 DBusGProxy *avatars_iface;
67 DBusGProxy *presence_iface;
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 (void);
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 GList *avatar_requests_queue = NULL;
223 guint n_avatar_requests = 0;
225 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
226 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
227 tp_contact_list_iface_init));
230 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
232 GObjectClass *object_class = G_OBJECT_CLASS (klass);
234 object_class->finalize = tp_contact_list_finalize;
237 g_signal_new ("destroy",
238 G_TYPE_FROM_CLASS (klass),
242 g_cclosure_marshal_VOID__VOID,
246 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
250 tp_contact_list_iface_init (EmpathyContactListIface *iface)
252 iface->setup = tp_contact_list_setup;
253 iface->find = tp_contact_list_find;
254 iface->add = tp_contact_list_add;
255 iface->remove = tp_contact_list_remove;
256 iface->get_members = tp_contact_list_get_members;
257 iface->get_local_pending = tp_contact_list_get_local_pending;
258 iface->process_pending = tp_contact_list_process_pending;
262 empathy_tp_contact_list_init (EmpathyTpContactList *list)
264 EmpathyTpContactListPriv *priv;
266 priv = GET_PRIV (list);
268 priv->groups = g_hash_table_new_full (g_str_hash,
270 (GDestroyNotify) g_free,
271 (GDestroyNotify) g_object_unref);
272 priv->contacts = g_hash_table_new_full (g_direct_hash,
275 (GDestroyNotify) g_object_unref);
279 tp_contact_list_finalize (GObject *object)
281 EmpathyTpContactListPriv *priv;
282 EmpathyTpContactList *list;
284 list = EMPATHY_TP_CONTACT_LIST (object);
285 priv = GET_PRIV (list);
287 empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
289 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
290 "AccountStatusChanged",
291 G_CALLBACK (tp_contact_list_status_changed_cb),
294 tp_contact_list_finalize_proxies (list);
297 g_object_unref (priv->tp_conn);
299 if (priv->subscribe) {
300 g_object_unref (priv->subscribe);
303 g_object_unref (priv->publish);
306 g_object_unref (priv->account);
307 g_object_unref (priv->user_contact);
308 g_object_unref (priv->mc);
309 g_hash_table_destroy (priv->groups);
310 g_hash_table_destroy (priv->contacts);
312 g_list_foreach (priv->local_pending, (GFunc) empathy_contact_list_info_free, NULL);
313 g_list_free (priv->local_pending);
315 g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
316 g_list_free (priv->members);
318 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
321 EmpathyTpContactList *
322 empathy_tp_contact_list_new (McAccount *account)
324 EmpathyTpContactListPriv *priv;
325 EmpathyTpContactList *list;
328 const gchar *protocol_name;
330 GError *error = NULL;
332 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
334 mc = empathy_mission_control_new ();
336 if (mission_control_get_connection_status (mc, account, NULL) != 0) {
337 /* The account is not connected, nothing to do. */
341 list = g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST, NULL);
342 priv = GET_PRIV (list);
344 priv->tp_conn = mission_control_get_connection (mc, account, NULL);
345 priv->account = g_object_ref (account);
348 /* Check for protocols that does not support contact groups. We can
349 * put all contacts into a special group in that case.
350 * FIXME: Default group should be an information in the profile */
351 profile = mc_account_get_profile (account);
352 protocol_name = mc_profile_get_protocol_name (profile);
353 if (strcmp (protocol_name, "salut") == 0) {
354 priv->protocol_group = _("Local Network");
356 g_object_unref (profile);
358 g_signal_connect (priv->tp_conn, "destroy",
359 G_CALLBACK (tp_contact_list_destroy_cb),
361 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
362 "AccountStatusChanged",
363 G_CALLBACK (tp_contact_list_status_changed_cb),
366 priv->aliasing_iface = tp_conn_get_interface (priv->tp_conn,
367 TELEPATHY_CONN_IFACE_ALIASING_QUARK);
368 priv->avatars_iface = tp_conn_get_interface (priv->tp_conn,
369 TELEPATHY_CONN_IFACE_AVATARS_QUARK);
370 priv->presence_iface = tp_conn_get_interface (priv->tp_conn,
371 TELEPATHY_CONN_IFACE_PRESENCE_QUARK);
373 if (priv->aliasing_iface) {
374 dbus_g_proxy_connect_signal (priv->aliasing_iface,
376 G_CALLBACK (tp_contact_list_aliases_update_cb),
380 if (priv->avatars_iface) {
381 dbus_g_proxy_connect_signal (priv->avatars_iface,
383 G_CALLBACK (tp_contact_list_avatar_update_cb),
387 if (priv->presence_iface) {
388 dbus_g_proxy_connect_signal (priv->presence_iface,
390 G_CALLBACK (tp_contact_list_presence_update_cb),
394 /* Get our own handle and contact */
395 if (!tp_conn_get_self_handle (DBUS_G_PROXY (priv->tp_conn),
397 empathy_debug (DEBUG_DOMAIN, "GetSelfHandle Error: %s",
398 error ? error->message : "No error given");
399 g_clear_error (&error);
401 priv->user_contact = empathy_tp_contact_list_get_from_handle (list, handle);
402 empathy_contact_set_is_user (priv->user_contact, TRUE);
409 tp_contact_list_setup (EmpathyContactList *list)
411 EmpathyTpContactListPriv *priv;
413 GError *error = NULL;
416 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
418 priv = GET_PRIV (list);
420 empathy_debug (DEBUG_DOMAIN, "setup contact list: %p", list);
423 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
424 G_CALLBACK (tp_contact_list_newchannel_cb),
427 /* Get existing channels */
428 if (!tp_conn_list_channels (DBUS_G_PROXY (priv->tp_conn),
431 empathy_debug (DEBUG_DOMAIN,
432 "Failed to get list of open channels: %s",
433 error ? error->message : "No error given");
434 g_clear_error (&error);
438 for (i = 0; channels->len > i; i++) {
439 GValueArray *chan_struct;
440 const gchar *object_path;
441 const gchar *chan_iface;
442 TelepathyHandleType handle_type;
445 chan_struct = g_ptr_array_index (channels, i);
446 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
447 chan_iface = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
448 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
449 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
451 tp_contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
452 object_path, chan_iface,
455 EMPATHY_TP_CONTACT_LIST (list));
457 g_value_array_free (chan_struct);
460 g_ptr_array_free (channels, TRUE);
463 static EmpathyContact *
464 tp_contact_list_find (EmpathyContactList *list,
467 EmpathyTpContactListPriv *priv;
469 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
471 priv = GET_PRIV (list);
473 return g_hash_table_find (priv->contacts,
474 (GHRFunc) tp_contact_list_find_foreach,
479 tp_contact_list_add (EmpathyContactList *list,
480 EmpathyContact *contact,
481 const gchar *message)
483 EmpathyTpContactListPriv *priv;
486 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
488 priv = GET_PRIV (list);
490 handle = empathy_contact_get_handle (contact);
491 empathy_tp_group_add_member (priv->subscribe, handle, message);
495 tp_contact_list_remove (EmpathyContactList *list,
496 EmpathyContact *contact,
497 const gchar *message)
499 EmpathyTpContactListPriv *priv;
502 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
504 priv = GET_PRIV (list);
506 handle = empathy_contact_get_handle (contact);
507 empathy_tp_group_remove_member (priv->subscribe, handle, message);
511 tp_contact_list_get_members (EmpathyContactList *list)
513 EmpathyTpContactListPriv *priv;
515 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
517 priv = GET_PRIV (list);
519 g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
520 return g_list_copy (priv->members);
524 tp_contact_list_get_local_pending (EmpathyContactList *list)
526 EmpathyTpContactListPriv *priv;
528 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
530 priv = GET_PRIV (list);
532 return g_list_copy (priv->local_pending);
536 tp_contact_list_process_pending (EmpathyContactList *list,
537 EmpathyContact *contact,
540 EmpathyTpContactListPriv *priv;
543 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
544 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
546 priv = GET_PRIV (list);
548 handle = empathy_contact_get_handle (contact);
550 empathy_tp_group_add_member (priv->publish, handle, NULL);
551 empathy_tp_group_add_member (priv->subscribe, handle, NULL);
553 empathy_tp_group_remove_member (priv->publish, handle, NULL);
558 empathy_tp_contact_list_get_account (EmpathyTpContactList *list)
560 EmpathyTpContactListPriv *priv;
562 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
564 priv = GET_PRIV (list);
566 return priv->account;
570 empathy_tp_contact_list_get_user (EmpathyTpContactList *list)
572 EmpathyTpContactListPriv *priv;
574 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
576 priv = GET_PRIV (list);
578 return priv->user_contact;
582 empathy_tp_contact_list_get_from_id (EmpathyTpContactList *list,
585 EmpathyTpContactListPriv *priv;
586 EmpathyContact *contact;
587 const gchar *contact_ids[] = {id, NULL};
590 GError *error = NULL;
592 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
593 g_return_val_if_fail (id != NULL, NULL);
595 priv = GET_PRIV (list);
597 contact = tp_contact_list_find (EMPATHY_CONTACT_LIST (list), id);
599 return g_object_ref (contact);
602 /* The id is unknown, requests a new handle */
603 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
604 TP_HANDLE_TYPE_CONTACT,
607 empathy_debug (DEBUG_DOMAIN,
608 "RequestHandle for %s failed: %s", id,
609 error ? error->message : "No error given");
610 g_clear_error (&error);
614 handle = g_array_index(handles, guint, 0);
615 g_array_free (handles, TRUE);
617 return empathy_tp_contact_list_get_from_handle (list, handle);
621 empathy_tp_contact_list_get_from_handle (EmpathyTpContactList *list,
624 EmpathyContact *contact;
628 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
630 handles = g_array_new (FALSE, FALSE, sizeof (guint));
631 g_array_append_val (handles, handle);
633 contacts = empathy_tp_contact_list_get_from_handles (list, handles);
634 g_array_free (handles, TRUE);
640 contact = contacts->data;
641 g_list_free (contacts);
647 empathy_tp_contact_list_get_from_handles (EmpathyTpContactList *list,
650 EmpathyTpContactListPriv *priv;
651 gchar **handles_names;
654 GList *contacts = NULL;
656 GError *error = NULL;
658 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
659 g_return_val_if_fail (handles != NULL, NULL);
661 priv = GET_PRIV (list);
663 /* Search all handles we already have */
664 new_handles = g_array_new (FALSE, FALSE, sizeof (guint));
665 for (i = 0; i < handles->len; i++) {
666 EmpathyContact *contact;
669 handle = g_array_index (handles, guint, i);
675 contact = g_hash_table_lookup (priv->contacts,
676 GUINT_TO_POINTER (handle));
679 contacts = g_list_prepend (contacts,
680 g_object_ref (contact));
682 g_array_append_val (new_handles, handle);
686 if (new_handles->len == 0) {
687 g_array_free (new_handles, TRUE);
691 /* Holds all handles we don't have yet.
692 * FIXME: We should release them at some point. */
693 if (!tp_conn_hold_handles (DBUS_G_PROXY (priv->tp_conn),
694 TP_HANDLE_TYPE_CONTACT,
695 new_handles, &error)) {
696 empathy_debug (DEBUG_DOMAIN,
697 "HoldHandles Error: %s",
698 error ? error->message : "No error given");
699 g_clear_error (&error);
700 g_array_free (new_handles, TRUE);
704 /* Get the IDs of all new handles */
705 if (!tp_conn_inspect_handles (DBUS_G_PROXY (priv->tp_conn),
706 TP_HANDLE_TYPE_CONTACT,
710 empathy_debug (DEBUG_DOMAIN,
711 "InspectHandle Error: %s",
712 error ? error->message : "No error given");
713 g_clear_error (&error);
714 g_array_free (new_handles, TRUE);
718 /* Create contact objects */
719 for (i = 0, id = handles_names; *id && i < new_handles->len; id++, i++) {
720 EmpathyContact *contact;
723 handle = g_array_index (new_handles, guint, i);
724 contact = g_object_new (EMPATHY_TYPE_CONTACT,
725 "account", priv->account,
730 if (priv->protocol_group) {
731 empathy_contact_add_group (contact, priv->protocol_group);
734 if (!priv->presence_iface) {
735 EmpathyPresence *presence;
737 /* We have no presence iface, set default presence
739 presence = empathy_presence_new_full (MC_PRESENCE_AVAILABLE,
742 empathy_contact_set_presence (contact, presence);
743 g_object_unref (presence);
746 g_signal_connect (contact, "notify::groups",
747 G_CALLBACK (tp_contact_list_groups_updated_cb),
749 g_signal_connect (contact, "notify::name",
750 G_CALLBACK (tp_contact_list_name_updated_cb),
753 empathy_debug (DEBUG_DOMAIN, "new contact created: %s (%d)",
756 g_hash_table_insert (priv->contacts,
757 GUINT_TO_POINTER (handle),
760 contacts = g_list_prepend (contacts, g_object_ref (contact));
763 tp_contact_list_get_info (list, new_handles);
765 g_array_free (new_handles, TRUE);
766 g_strfreev (handles_names);
772 empathy_tp_contact_list_rename_group (EmpathyTpContactList *list,
773 const gchar *old_group,
774 const gchar *new_group)
776 EmpathyTpContactListPriv *priv;
777 EmpathyTpGroup *group;
780 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
781 g_return_if_fail (old_group != NULL);
782 g_return_if_fail (new_group != NULL);
784 priv = GET_PRIV (list);
786 group = g_hash_table_find (priv->groups,
787 (GHRFunc) tp_contact_list_find_group,
790 /* The group doesn't exists on this account */
794 empathy_debug (DEBUG_DOMAIN, "rename group %s to %s", group, new_group);
796 /* Remove all members from the old group */
797 members = empathy_tp_group_get_members (group);
798 empathy_tp_group_remove_members (group, members, "");
799 tp_contact_list_group_members_removed_cb (group, members,
801 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
803 g_hash_table_remove (priv->groups,
804 empathy_tp_group_get_object_path (group));
806 /* Add all members to the new group */
807 group = tp_contact_list_get_group (list, new_group);
809 empathy_tp_group_add_members (group, members, "");
814 empathy_tp_contact_list_get_groups (EmpathyTpContactList *list)
816 EmpathyTpContactListPriv *priv;
817 GList *groups = NULL;
819 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
821 priv = GET_PRIV (list);
823 g_hash_table_foreach (priv->groups,
824 (GHFunc) tp_contact_list_get_groups_foreach,
827 groups = g_list_sort (groups, (GCompareFunc) strcmp);
833 tp_contact_list_finalize_proxies (EmpathyTpContactList *list)
835 EmpathyTpContactListPriv *priv;
837 priv = GET_PRIV (list);
840 g_signal_handlers_disconnect_by_func (priv->tp_conn,
841 tp_contact_list_destroy_cb,
843 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
844 G_CALLBACK (tp_contact_list_newchannel_cb),
848 if (priv->aliasing_iface) {
849 dbus_g_proxy_disconnect_signal (priv->aliasing_iface,
851 G_CALLBACK (tp_contact_list_aliases_update_cb),
855 if (priv->avatars_iface) {
856 dbus_g_proxy_disconnect_signal (priv->avatars_iface,
858 G_CALLBACK (tp_contact_list_avatar_update_cb),
862 if (priv->presence_iface) {
863 dbus_g_proxy_disconnect_signal (priv->presence_iface,
865 G_CALLBACK (tp_contact_list_presence_update_cb),
871 tp_contact_list_destroy_cb (DBusGProxy *proxy,
872 EmpathyTpContactList *list)
874 EmpathyTpContactListPriv *priv;
876 priv = GET_PRIV (list);
878 empathy_debug (DEBUG_DOMAIN, "Connection destroyed... "
879 "Account disconnected or CM crashed");
881 /* DBus proxies should NOT be used anymore */
882 g_object_unref (priv->tp_conn);
883 priv->tp_conn = NULL;
884 priv->aliasing_iface = NULL;
885 priv->avatars_iface = NULL;
886 priv->presence_iface = NULL;
888 /* Remove all contacts */
889 g_hash_table_foreach (priv->contacts,
890 (GHFunc) tp_contact_list_contact_removed_foreach,
892 g_hash_table_remove_all (priv->contacts);
894 /* Tell the world to not use us anymore */
895 g_signal_emit (list, signals[DESTROY], 0);
899 tp_contact_list_contact_removed_foreach (guint handle,
900 EmpathyContact *contact,
901 EmpathyTpContactList *list)
903 g_signal_handlers_disconnect_by_func (contact,
904 tp_contact_list_groups_updated_cb,
906 g_signal_handlers_disconnect_by_func (contact,
907 tp_contact_list_name_updated_cb,
910 g_signal_emit_by_name (list, "contact-removed", contact);
914 tp_contact_list_block_contact (EmpathyTpContactList *list,
915 EmpathyContact *contact)
917 g_signal_handlers_block_by_func (contact,
918 tp_contact_list_groups_updated_cb,
920 g_signal_handlers_block_by_func (contact,
921 tp_contact_list_name_updated_cb,
926 tp_contact_list_unblock_contact (EmpathyTpContactList *list,
927 EmpathyContact *contact)
929 g_signal_handlers_unblock_by_func (contact,
930 tp_contact_list_groups_updated_cb,
932 g_signal_handlers_unblock_by_func (contact,
933 tp_contact_list_name_updated_cb,
938 tp_contact_list_find_foreach (guint handle,
939 EmpathyContact *contact,
942 if (strcmp (empathy_contact_get_id (contact), id) == 0) {
950 tp_contact_list_newchannel_cb (DBusGProxy *proxy,
951 const gchar *object_path,
952 const gchar *channel_type,
953 TelepathyHandleType handle_type,
954 guint channel_handle,
955 gboolean suppress_handle,
956 EmpathyTpContactList *list)
958 EmpathyTpContactListPriv *priv;
959 EmpathyTpGroup *group;
961 const gchar *bus_name;
964 priv = GET_PRIV (list);
966 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
972 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn));
973 new_chan = tp_chan_new (tp_get_bus (),
976 channel_type, handle_type, channel_handle);
977 g_return_if_fail (TELEPATHY_IS_CHAN (new_chan));
979 if (handle_type == TP_HANDLE_TYPE_LIST) {
980 TpContactListType list_type;
982 group = empathy_tp_group_new (priv->account, new_chan);
984 list_type = tp_contact_list_get_type (list, group);
985 if (list_type == TP_CONTACT_LIST_TYPE_UNKNOWN) {
986 empathy_debug (DEBUG_DOMAIN,
987 "Type of contact list channel unknown: %s",
988 empathy_tp_group_get_name (group));
990 g_object_unref (new_chan);
991 g_object_unref (group);
994 empathy_debug (DEBUG_DOMAIN,
995 "New contact list channel of type: %d",
999 g_signal_connect (group, "members-added",
1000 G_CALLBACK (tp_contact_list_added_cb),
1002 g_signal_connect (group, "members-removed",
1003 G_CALLBACK (tp_contact_list_removed_cb),
1006 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1007 GList *pendings, *l;
1009 if (priv->publish) {
1010 g_object_unref (priv->publish);
1012 priv->publish = group;
1014 /* Makes no sense to be in remote-pending */
1015 g_signal_connect (group, "local-pending",
1016 G_CALLBACK (tp_contact_list_pending_cb),
1019 pendings = empathy_tp_group_get_local_pending_members_with_info (group);
1023 pending = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1024 for (l = pendings; l; l = l->next) {
1025 EmpathyTpGroupInfo *info;
1029 g_array_insert_val (pending, 0, info->member);
1030 tp_contact_list_pending_cb (group, pending,
1036 g_array_free (pending, TRUE);
1037 empathy_tp_group_info_list_free (pendings);
1040 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1041 GArray *remote_pendings = NULL;
1043 if (priv->subscribe) {
1044 g_object_unref (priv->subscribe);
1046 priv->subscribe = group;
1048 /* Makes no sense to be in local-pending */
1049 g_signal_connect (group, "remote-pending",
1050 G_CALLBACK (tp_contact_list_pending_cb),
1052 empathy_tp_group_get_all_members (group,
1057 tp_contact_list_pending_cb (group, remote_pendings, 0,
1058 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1061 g_array_free (remote_pendings, TRUE);
1063 members = empathy_tp_group_get_members (group);
1066 tp_contact_list_added_cb (group, members, 0,
1067 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1069 g_array_free (members, TRUE);
1071 else if (handle_type == TP_HANDLE_TYPE_GROUP) {
1072 const gchar *object_path;
1074 object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (new_chan));
1075 if (g_hash_table_lookup (priv->groups, object_path)) {
1076 g_object_unref (new_chan);
1080 group = empathy_tp_group_new (priv->account, new_chan);
1082 empathy_debug (DEBUG_DOMAIN, "New server-side group channel: %s",
1083 empathy_tp_group_get_name (group));
1085 dbus_g_proxy_connect_signal (DBUS_G_PROXY (new_chan), "Closed",
1087 (tp_contact_list_group_channel_closed_cb),
1090 g_hash_table_insert (priv->groups, g_strdup (object_path), group);
1091 g_signal_connect (group, "members-added",
1092 G_CALLBACK (tp_contact_list_group_members_added_cb),
1094 g_signal_connect (group, "members-removed",
1095 G_CALLBACK (tp_contact_list_group_members_removed_cb),
1098 members = empathy_tp_group_get_members (group);
1099 tp_contact_list_group_members_added_cb (group, members, 0,
1100 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1102 g_array_free (members, TRUE);
1105 g_object_unref (new_chan);
1108 static TpContactListType
1109 tp_contact_list_get_type (EmpathyTpContactList *list,
1110 EmpathyTpGroup *group)
1112 EmpathyTpContactListPriv *priv;
1113 TpContactListType list_type;
1116 priv = GET_PRIV (list);
1118 name = empathy_tp_group_get_name (group);
1119 if (strcmp (name, "subscribe") == 0) {
1120 list_type = TP_CONTACT_LIST_TYPE_SUBSCRIBE;
1121 } else if (strcmp (name, "publish") == 0) {
1122 list_type = TP_CONTACT_LIST_TYPE_PUBLISH;
1124 list_type = TP_CONTACT_LIST_TYPE_UNKNOWN;
1131 tp_contact_list_added_cb (EmpathyTpGroup *group,
1135 const gchar *message,
1136 EmpathyTpContactList *list)
1138 EmpathyTpContactListPriv *priv;
1139 GList *added_list, *l;
1140 TpContactListType list_type;
1142 priv = GET_PRIV (list);
1144 list_type = tp_contact_list_get_type (list, group);
1146 added_list = empathy_tp_contact_list_get_from_handles (list, handles);
1147 for (l = added_list; l; l = l->next) {
1148 EmpathyContact *contact;
1149 EmpathySubscription subscription;
1151 contact = EMPATHY_CONTACT (l->data);
1153 empathy_debug (DEBUG_DOMAIN, "Contact '%s' added to list type %d",
1154 empathy_contact_get_name (contact),
1157 subscription = empathy_contact_get_subscription (contact);
1158 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1159 subscription |= EMPATHY_SUBSCRIPTION_FROM;
1161 else if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1162 subscription |= EMPATHY_SUBSCRIPTION_TO;
1163 tp_contact_list_remove_local_pending (list, contact);
1166 tp_contact_list_block_contact (list, contact);
1167 empathy_contact_set_subscription (contact, subscription);
1168 tp_contact_list_unblock_contact (list, contact);
1170 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1171 if (!g_list_find (priv->members, contact)) {
1172 priv->members = g_list_prepend (priv->members,
1173 g_object_ref (contact));
1174 g_signal_emit_by_name (list, "contact-added", contact);
1178 g_object_unref (contact);
1181 g_list_free (added_list);
1185 tp_contact_list_removed_cb (EmpathyTpGroup *group,
1189 const gchar *message,
1190 EmpathyTpContactList *list)
1192 EmpathyTpContactListPriv *priv;
1193 GList *removed_list, *l;
1194 TpContactListType list_type;
1196 priv = GET_PRIV (list);
1198 list_type = tp_contact_list_get_type (list, group);
1200 removed_list = empathy_tp_contact_list_get_from_handles (list, handles);
1201 for (l = removed_list; l; l = l->next) {
1202 EmpathyContact *contact;
1203 EmpathySubscription subscription;
1205 contact = EMPATHY_CONTACT (l->data);
1207 empathy_debug (DEBUG_DOMAIN, "Contact '%s' removed from list type %d",
1208 empathy_contact_get_name (contact),
1211 subscription = empathy_contact_get_subscription (contact);
1212 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1213 subscription &= !EMPATHY_SUBSCRIPTION_FROM;
1215 else if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1216 subscription &= !EMPATHY_SUBSCRIPTION_TO;
1217 tp_contact_list_remove_local_pending (list, contact);
1220 tp_contact_list_block_contact (list, contact);
1221 empathy_contact_set_subscription (contact, subscription);
1222 tp_contact_list_unblock_contact (list, contact);
1224 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1227 if ((l = g_list_find (priv->members, contact))) {
1228 g_signal_emit_by_name (list, "contact-removed", contact);
1229 priv->members = g_list_delete_link (priv->members, l);
1230 g_object_unref (contact);
1233 g_object_unref (contact);
1236 g_list_free (removed_list);
1240 tp_contact_list_pending_cb (EmpathyTpGroup *group,
1244 const gchar *message,
1245 EmpathyTpContactList *list)
1247 EmpathyTpContactListPriv *priv;
1248 GList *pending_list, *l;
1249 TpContactListType list_type;
1251 priv = GET_PRIV (list);
1253 list_type = tp_contact_list_get_type (list, group);
1255 pending_list = empathy_tp_contact_list_get_from_handles (list, handles);
1256 for (l = pending_list; l; l = l->next) {
1257 EmpathyContact *contact;
1259 contact = EMPATHY_CONTACT (l->data);
1261 empathy_debug (DEBUG_DOMAIN, "Contact '%s' pending in list type %d",
1262 empathy_contact_get_name (contact),
1265 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1266 if (!g_list_find (priv->members, contact)) {
1267 EmpathyContactListInfo *info;
1269 info = empathy_contact_list_info_new (contact, message);
1270 priv->local_pending = g_list_prepend (priv->local_pending,
1273 g_signal_emit_by_name (list, "local-pending",
1278 /* That contact wants our presence and he is
1279 * in our roster. Accept to publish our presence
1280 * without asking the user. */
1281 handle = empathy_contact_get_handle (contact);
1282 empathy_tp_group_add_member (priv->publish,
1286 else if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1287 if (!g_list_find (priv->members, contact)) {
1288 priv->members = g_list_prepend (priv->members,
1289 g_object_ref (contact));
1290 g_signal_emit_by_name (list, "contact-added", contact);
1294 g_object_unref (contact);
1297 g_list_free (pending_list);
1301 tp_contact_list_remove_local_pending (EmpathyTpContactList *list,
1302 EmpathyContact *contact)
1304 EmpathyTpContactListPriv *priv;
1307 priv = GET_PRIV (list);
1309 for (l = priv->local_pending; l; l = l->next) {
1310 EmpathyContactListInfo *info;
1313 if (empathy_contact_equal (contact, info->contact)) {
1314 empathy_debug (DEBUG_DOMAIN, "Contact no more local-pending: %s",
1315 empathy_contact_get_name (contact));
1317 priv->local_pending = g_list_delete_link (priv->local_pending, l);
1318 empathy_contact_list_info_free (info);
1325 tp_contact_list_groups_updated_cb (EmpathyContact *contact,
1327 EmpathyTpContactList *list)
1329 EmpathyTpContactListPriv *priv;
1330 TpContactListData data;
1333 priv = GET_PRIV (list);
1335 /* Make sure all groups are created */
1336 groups = empathy_contact_get_groups (contact);
1337 for (l = groups; l; l = l->next) {
1338 tp_contact_list_get_group (list, l->data);
1341 data.handle = empathy_contact_get_handle (contact);
1342 data.new_groups = groups;
1344 g_hash_table_foreach (priv->groups,
1345 (GHFunc) tp_contact_list_update_groups_foreach,
1350 tp_contact_list_name_updated_cb (EmpathyContact *contact,
1352 EmpathyTpContactList *list)
1354 EmpathyTpContactListPriv *priv;
1355 GHashTable *new_alias;
1356 const gchar *new_name;
1358 GError *error = NULL;
1360 priv = GET_PRIV (list);
1362 if (!priv->aliasing_iface) {
1366 handle = empathy_contact_get_handle (contact);
1367 new_name = empathy_contact_get_name (contact);
1369 empathy_debug (DEBUG_DOMAIN, "renaming handle %d to %s",
1372 new_alias = g_hash_table_new_full (g_direct_hash,
1377 g_hash_table_insert (new_alias,
1378 GUINT_TO_POINTER (handle),
1379 g_strdup (new_name));
1381 if (!tp_conn_iface_aliasing_set_aliases (priv->aliasing_iface,
1384 empathy_debug (DEBUG_DOMAIN,
1385 "Couldn't rename contact: %s",
1386 error ? error->message : "No error given");
1387 g_clear_error (&error);
1390 g_hash_table_destroy (new_alias);
1394 tp_contact_list_update_groups_foreach (gchar *object_path,
1395 EmpathyTpGroup *group,
1396 TpContactListData *data)
1399 gboolean found = FALSE;
1400 const gchar *group_name;
1403 is_member = empathy_tp_group_is_member (group, data->handle);
1404 group_name = empathy_tp_group_get_name (group);
1406 for (l = data->new_groups; l; l = l->next) {
1407 if (strcmp (group_name, l->data) == 0) {
1413 if (is_member && !found) {
1414 /* We are no longer member of this group */
1415 empathy_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1416 data->handle, group_name);
1417 empathy_tp_group_remove_member (group, data->handle, "");
1420 if (!is_member && found) {
1421 /* We are now member of this group */
1422 empathy_debug (DEBUG_DOMAIN, "Contact %d added to group '%s'",
1423 data->handle, group_name);
1424 empathy_tp_group_add_member (group, data->handle, "");
1428 static EmpathyTpGroup *
1429 tp_contact_list_get_group (EmpathyTpContactList *list,
1432 EmpathyTpContactListPriv *priv;
1433 EmpathyTpGroup *group;
1434 TpChan *group_channel;
1437 char *group_object_path;
1438 const char *names[2] = {name, NULL};
1439 GError *error = NULL;
1441 priv = GET_PRIV (list);
1443 group = g_hash_table_find (priv->groups,
1444 (GHRFunc) tp_contact_list_find_group,
1450 empathy_debug (DEBUG_DOMAIN, "creating new group: %s", name);
1452 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
1453 TP_HANDLE_TYPE_GROUP,
1457 empathy_debug (DEBUG_DOMAIN,
1458 "Couldn't request the creation of a new handle for group: %s",
1459 error ? error->message : "No error given");
1460 g_clear_error (&error);
1463 group_handle = g_array_index (handles, guint, 0);
1464 g_array_free (handles, TRUE);
1466 if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
1467 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1468 TP_HANDLE_TYPE_GROUP,
1473 empathy_debug (DEBUG_DOMAIN,
1474 "Couldn't request the creation of a new group channel: %s",
1475 error ? error->message : "No error given");
1476 g_clear_error (&error);
1480 group_channel = tp_chan_new (tp_get_bus (),
1481 dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn)),
1483 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1484 TP_HANDLE_TYPE_GROUP,
1487 dbus_g_proxy_connect_signal (DBUS_G_PROXY (group_channel),
1490 (tp_contact_list_group_channel_closed_cb),
1494 group = empathy_tp_group_new (priv->account, group_channel);
1495 g_hash_table_insert (priv->groups, group_object_path, group);
1496 g_signal_connect (group, "members-added",
1497 G_CALLBACK (tp_contact_list_group_members_added_cb),
1499 g_signal_connect (group, "members-removed",
1500 G_CALLBACK (tp_contact_list_group_members_removed_cb),
1507 tp_contact_list_find_group (gchar *key,
1508 EmpathyTpGroup *group,
1511 if (strcmp (group_name, empathy_tp_group_get_name (group)) == 0) {
1519 tp_contact_list_get_groups_foreach (gchar *key,
1520 EmpathyTpGroup *group,
1525 name = empathy_tp_group_get_name (group);
1526 *groups = g_list_append (*groups, g_strdup (name));
1530 tp_contact_list_group_channel_closed_cb (TpChan *channel,
1531 EmpathyTpContactList *list)
1533 EmpathyTpContactListPriv *priv;
1535 priv = GET_PRIV (list);
1537 g_hash_table_remove (priv->groups,
1538 dbus_g_proxy_get_path (DBUS_G_PROXY (channel)));
1542 tp_contact_list_group_members_added_cb (EmpathyTpGroup *group,
1546 const gchar *message,
1547 EmpathyTpContactList *list)
1549 EmpathyTpContactListPriv *priv;
1550 GList *added_list, *l;
1551 const gchar *group_name;
1553 priv = GET_PRIV (list);
1555 group_name = empathy_tp_group_get_name (group);
1556 added_list = empathy_tp_contact_list_get_from_handles (list, members);
1558 for (l = added_list; l; l = l->next) {
1559 EmpathyContact *contact;
1561 contact = EMPATHY_CONTACT (l->data);
1563 tp_contact_list_block_contact (list, contact);
1564 empathy_contact_add_group (contact, group_name);
1565 tp_contact_list_unblock_contact (list, contact);
1567 g_object_unref (contact);
1570 g_list_free (added_list);
1574 tp_contact_list_group_members_removed_cb (EmpathyTpGroup *group,
1578 const gchar *message,
1579 EmpathyTpContactList *list)
1581 EmpathyTpContactListPriv *priv;
1582 GList *removed_list, *l;
1583 const gchar *group_name;
1585 priv = GET_PRIV (list);
1587 group_name = empathy_tp_group_get_name (group);
1588 removed_list = empathy_tp_contact_list_get_from_handles (list, members);
1590 for (l = removed_list; l; l = l->next) {
1591 EmpathyContact *contact;
1595 tp_contact_list_block_contact (list, contact);
1596 empathy_contact_remove_group (contact, group_name);
1597 tp_contact_list_unblock_contact (list, contact);
1599 g_object_unref (contact);
1602 g_list_free (removed_list);
1606 tp_contact_list_get_info (EmpathyTpContactList *list,
1609 EmpathyTpContactListPriv *priv;
1610 GError *error = NULL;
1612 priv = GET_PRIV (list);
1614 if (priv->presence_iface) {
1615 /* FIXME: We should use GetPresence instead */
1616 if (!tp_conn_iface_presence_request_presence (priv->presence_iface,
1618 empathy_debug (DEBUG_DOMAIN,
1619 "Could not request presences: %s",
1620 error ? error->message : "No error given");
1621 g_clear_error (&error);
1625 if (priv->aliasing_iface) {
1626 TpContactListAliasesRequestData *data;
1628 data = g_slice_new (TpContactListAliasesRequestData);
1630 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
1632 tp_conn_iface_aliasing_request_aliases_async (priv->aliasing_iface,
1634 (tp_conn_iface_aliasing_request_aliases_reply)
1635 tp_contact_list_request_aliases_cb,
1639 if (priv->avatars_iface) {
1642 for (i = 0; i < handles->len; i++) {
1645 handle = g_array_index (handles, gint, i);
1646 tp_contact_list_request_avatar (list, handle);
1652 tp_contact_list_request_avatar (EmpathyTpContactList *list,
1655 EmpathyTpContactListPriv *priv;
1656 TpContactListAvatarRequestData *data;
1658 priv = GET_PRIV (list);
1660 /* We queue avatar requests to not send too many dbus async
1661 * calls at once. If we don't we reach the dbus's limit of
1663 data = g_slice_new (TpContactListAvatarRequestData);
1664 data->list = g_object_ref (list);
1665 data->handle = handle;
1666 avatar_requests_queue = g_list_append (avatar_requests_queue, data);
1667 tp_contact_list_start_avatar_requests ();
1671 tp_contact_list_start_avatar_requests (void)
1673 empathy_debug (DEBUG_DOMAIN, "Start avatar requests, pending calls: %d",
1676 while (n_avatar_requests < MAX_AVATAR_REQUESTS && avatar_requests_queue) {
1677 EmpathyTpContactListPriv *priv;
1678 TpContactListAvatarRequestData *data;
1680 data = avatar_requests_queue->data;
1681 priv = GET_PRIV (data->list);
1683 n_avatar_requests++;
1684 avatar_requests_queue = g_list_delete_link (avatar_requests_queue,
1685 avatar_requests_queue);
1687 empathy_debug (DEBUG_DOMAIN, "Calling RequestAvatar async");
1688 tp_conn_iface_avatars_request_avatar_async (priv->avatars_iface,
1690 (tp_conn_iface_avatars_request_avatar_reply)
1691 tp_contact_list_request_avatar_cb,
1697 tp_contact_list_avatar_update_cb (DBusGProxy *proxy,
1700 EmpathyTpContactList *list)
1702 EmpathyTpContactListPriv *priv;
1704 priv = GET_PRIV (list);
1706 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1707 /* We don't know this contact, skip */
1711 empathy_debug (DEBUG_DOMAIN, "Changing avatar for %d to %s",
1714 tp_contact_list_request_avatar (list, handle);
1718 tp_contact_list_request_avatar_cb (DBusGProxy *proxy,
1719 GArray *avatar_data,
1722 TpContactListAvatarRequestData *data)
1724 EmpathyContact *contact;
1726 contact = empathy_tp_contact_list_get_from_handle (data->list, data->handle);
1729 empathy_debug (DEBUG_DOMAIN, "Error requesting avatar for %s: %s",
1730 empathy_contact_get_name (contact),
1731 error ? error->message : "No error given");
1733 EmpathyAvatar *avatar;
1735 empathy_debug (DEBUG_DOMAIN, "Avatar received for %s (%d)",
1736 empathy_contact_get_id (contact),
1739 avatar = empathy_avatar_new (avatar_data->data,
1742 tp_contact_list_block_contact (data->list, contact);
1743 empathy_contact_set_avatar (contact, avatar);
1744 tp_contact_list_unblock_contact (data->list, contact);
1745 empathy_avatar_unref (avatar);
1748 n_avatar_requests--;
1749 tp_contact_list_start_avatar_requests ();
1751 g_object_unref (contact);
1752 g_object_unref (data->list);
1753 g_slice_free (TpContactListAvatarRequestData, data);
1757 tp_contact_list_aliases_update_cb (DBusGProxy *proxy,
1758 GPtrArray *renamed_handlers,
1759 EmpathyTpContactList *list)
1761 EmpathyTpContactListPriv *priv;
1764 priv = GET_PRIV (list);
1766 for (i = 0; renamed_handlers->len > i; i++) {
1769 GValueArray *renamed_struct;
1770 EmpathyContact *contact;
1772 renamed_struct = g_ptr_array_index (renamed_handlers, i);
1773 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
1774 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
1776 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1777 /* We don't know this contact, skip */
1781 if (G_STR_EMPTY (alias)) {
1785 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1786 tp_contact_list_block_contact (list, contact);
1787 empathy_contact_set_name (contact, alias);
1788 tp_contact_list_unblock_contact (list, contact);
1789 g_object_unref (contact);
1791 empathy_debug (DEBUG_DOMAIN, "contact %d renamed to %s (update cb)",
1797 tp_contact_list_request_aliases_cb (DBusGProxy *proxy,
1798 gchar **contact_names,
1800 TpContactListAliasesRequestData *data)
1806 empathy_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
1810 for (name = contact_names; *name && !error; name++) {
1811 EmpathyContact *contact;
1813 contact = empathy_tp_contact_list_get_from_handle (data->list,
1815 tp_contact_list_block_contact (data->list, contact);
1816 empathy_contact_set_name (contact, *name);
1817 tp_contact_list_unblock_contact (data->list, contact);
1818 g_object_unref (contact);
1820 empathy_debug (DEBUG_DOMAIN, "contact %d renamed to %s (request cb)",
1821 data->handles[i], *name);
1826 g_free (data->handles);
1827 g_slice_free (TpContactListAliasesRequestData, data);
1831 tp_contact_list_presence_update_cb (DBusGProxy *proxy,
1832 GHashTable *handle_table,
1833 EmpathyTpContactList *list)
1835 g_hash_table_foreach (handle_table,
1836 (GHFunc) tp_contact_list_parse_presence_foreach,
1841 tp_contact_list_parse_presence_foreach (guint handle,
1842 GValueArray *presence_struct,
1843 EmpathyTpContactList *list)
1845 EmpathyTpContactListPriv *priv;
1846 GHashTable *presences_table;
1847 EmpathyContact *contact;
1848 EmpathyPresence *presence = NULL;
1850 priv = GET_PRIV (list);
1852 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1853 /* We don't know this contact, skip */
1857 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1858 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
1860 g_hash_table_foreach (presences_table,
1861 (GHFunc) tp_contact_list_presences_table_foreach,
1864 empathy_debug (DEBUG_DOMAIN, "Presence changed for %s (%d) to %s (%d)",
1865 empathy_contact_get_name (contact),
1867 presence ? empathy_presence_get_status (presence) : "unset",
1868 presence ? empathy_presence_get_state (presence) : MC_PRESENCE_UNSET);
1870 tp_contact_list_block_contact (list, contact);
1871 empathy_contact_set_presence (contact, presence);
1872 tp_contact_list_unblock_contact (list, contact);
1874 g_object_unref (contact);
1878 tp_contact_list_presences_table_foreach (const gchar *state_str,
1879 GHashTable *presences_table,
1880 EmpathyPresence **presence)
1883 const GValue *message;
1885 state = empathy_presence_state_from_str (state_str);
1886 if ((state == MC_PRESENCE_UNSET) || (state == MC_PRESENCE_OFFLINE)) {
1891 g_object_unref (*presence);
1895 *presence = empathy_presence_new ();
1896 empathy_presence_set_state (*presence, state);
1898 message = g_hash_table_lookup (presences_table, "message");
1899 if (message != NULL) {
1900 empathy_presence_set_status (*presence,
1901 g_value_get_string (message));
1906 tp_contact_list_status_changed_cb (MissionControl *mc,
1907 TelepathyConnectionStatus status,
1908 McPresence presence,
1909 TelepathyConnectionStatusReason reason,
1910 const gchar *unique_name,
1911 EmpathyTpContactList *list)
1913 EmpathyTpContactListPriv *priv;
1916 priv = GET_PRIV (list);
1918 account = mc_account_lookup (unique_name);
1919 if (status != TP_CONN_STATUS_DISCONNECTED ||
1920 !empathy_account_equal (account, priv->account) ||
1922 g_object_unref (account);
1926 /* We are disconnected, do just like if the connection was destroyed */
1927 g_signal_handlers_disconnect_by_func (priv->tp_conn,
1928 tp_contact_list_destroy_cb,
1930 tp_contact_list_destroy_cb (DBUS_G_PROXY (priv->tp_conn), list);
1932 g_object_unref (account);