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);
978 if (handle_type == TP_HANDLE_TYPE_LIST) {
979 TpContactListType list_type;
981 group = empathy_tp_group_new (priv->account, new_chan);
983 list_type = tp_contact_list_get_type (list, group);
984 if (list_type == TP_CONTACT_LIST_TYPE_UNKNOWN) {
985 empathy_debug (DEBUG_DOMAIN,
986 "Type of contact list channel unknown: %s",
987 empathy_tp_group_get_name (group));
989 g_object_unref (new_chan);
990 g_object_unref (group);
993 empathy_debug (DEBUG_DOMAIN,
994 "New contact list channel of type: %d",
998 g_signal_connect (group, "members-added",
999 G_CALLBACK (tp_contact_list_added_cb),
1001 g_signal_connect (group, "members-removed",
1002 G_CALLBACK (tp_contact_list_removed_cb),
1005 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1006 GList *pendings, *l;
1008 if (priv->publish) {
1009 g_object_unref (priv->publish);
1011 priv->publish = group;
1013 /* Makes no sense to be in remote-pending */
1014 g_signal_connect (group, "local-pending",
1015 G_CALLBACK (tp_contact_list_pending_cb),
1018 pendings = empathy_tp_group_get_local_pending_members_with_info (group);
1022 pending = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
1023 for (l = pendings; l; l = l->next) {
1024 EmpathyTpGroupInfo *info;
1028 g_array_insert_val (pending, 0, info->member);
1029 tp_contact_list_pending_cb (group, pending,
1035 g_array_free (pending, TRUE);
1036 empathy_tp_group_info_list_free (pendings);
1039 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1040 GArray *remote_pendings = NULL;
1042 if (priv->subscribe) {
1043 g_object_unref (priv->subscribe);
1045 priv->subscribe = group;
1047 /* Makes no sense to be in local-pending */
1048 g_signal_connect (group, "remote-pending",
1049 G_CALLBACK (tp_contact_list_pending_cb),
1051 empathy_tp_group_get_all_members (group,
1056 tp_contact_list_pending_cb (group, remote_pendings, 0,
1057 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1060 g_array_free (remote_pendings, TRUE);
1062 members = empathy_tp_group_get_members (group);
1065 tp_contact_list_added_cb (group, members, 0,
1066 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1068 g_array_free (members, TRUE);
1070 else if (handle_type == TP_HANDLE_TYPE_GROUP) {
1071 const gchar *object_path;
1073 object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (new_chan));
1074 if (g_hash_table_lookup (priv->groups, object_path)) {
1075 g_object_unref (new_chan);
1079 group = empathy_tp_group_new (priv->account, new_chan);
1081 empathy_debug (DEBUG_DOMAIN, "New server-side group channel: %s",
1082 empathy_tp_group_get_name (group));
1084 dbus_g_proxy_connect_signal (DBUS_G_PROXY (new_chan), "Closed",
1086 (tp_contact_list_group_channel_closed_cb),
1089 g_hash_table_insert (priv->groups, g_strdup (object_path), group);
1090 g_signal_connect (group, "members-added",
1091 G_CALLBACK (tp_contact_list_group_members_added_cb),
1093 g_signal_connect (group, "members-removed",
1094 G_CALLBACK (tp_contact_list_group_members_removed_cb),
1097 members = empathy_tp_group_get_members (group);
1098 tp_contact_list_group_members_added_cb (group, members, 0,
1099 TP_CHANNEL_GROUP_CHANGE_REASON_NONE,
1101 g_array_free (members, TRUE);
1104 g_object_unref (new_chan);
1107 static TpContactListType
1108 tp_contact_list_get_type (EmpathyTpContactList *list,
1109 EmpathyTpGroup *group)
1111 EmpathyTpContactListPriv *priv;
1112 TpContactListType list_type;
1115 priv = GET_PRIV (list);
1117 name = empathy_tp_group_get_name (group);
1118 if (strcmp (name, "subscribe") == 0) {
1119 list_type = TP_CONTACT_LIST_TYPE_SUBSCRIBE;
1120 } else if (strcmp (name, "publish") == 0) {
1121 list_type = TP_CONTACT_LIST_TYPE_PUBLISH;
1123 list_type = TP_CONTACT_LIST_TYPE_UNKNOWN;
1130 tp_contact_list_added_cb (EmpathyTpGroup *group,
1134 const gchar *message,
1135 EmpathyTpContactList *list)
1137 EmpathyTpContactListPriv *priv;
1138 GList *added_list, *l;
1139 TpContactListType list_type;
1141 priv = GET_PRIV (list);
1143 list_type = tp_contact_list_get_type (list, group);
1145 added_list = empathy_tp_contact_list_get_from_handles (list, handles);
1146 for (l = added_list; l; l = l->next) {
1147 EmpathyContact *contact;
1148 EmpathySubscription subscription;
1150 contact = EMPATHY_CONTACT (l->data);
1152 empathy_debug (DEBUG_DOMAIN, "Contact '%s' added to list type %d",
1153 empathy_contact_get_name (contact),
1156 subscription = empathy_contact_get_subscription (contact);
1157 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1158 subscription |= EMPATHY_SUBSCRIPTION_FROM;
1160 else if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1161 subscription |= EMPATHY_SUBSCRIPTION_TO;
1162 tp_contact_list_remove_local_pending (list, contact);
1165 tp_contact_list_block_contact (list, contact);
1166 empathy_contact_set_subscription (contact, subscription);
1167 tp_contact_list_unblock_contact (list, contact);
1169 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1170 if (!g_list_find (priv->members, contact)) {
1171 priv->members = g_list_prepend (priv->members,
1172 g_object_ref (contact));
1173 g_signal_emit_by_name (list, "contact-added", contact);
1177 g_object_unref (contact);
1180 g_list_free (added_list);
1184 tp_contact_list_removed_cb (EmpathyTpGroup *group,
1188 const gchar *message,
1189 EmpathyTpContactList *list)
1191 EmpathyTpContactListPriv *priv;
1192 GList *removed_list, *l;
1193 TpContactListType list_type;
1195 priv = GET_PRIV (list);
1197 list_type = tp_contact_list_get_type (list, group);
1199 removed_list = empathy_tp_contact_list_get_from_handles (list, handles);
1200 for (l = removed_list; l; l = l->next) {
1201 EmpathyContact *contact;
1202 EmpathySubscription subscription;
1204 contact = EMPATHY_CONTACT (l->data);
1206 empathy_debug (DEBUG_DOMAIN, "Contact '%s' removed from list type %d",
1207 empathy_contact_get_name (contact),
1210 subscription = empathy_contact_get_subscription (contact);
1211 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1212 subscription &= !EMPATHY_SUBSCRIPTION_FROM;
1214 else if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1215 subscription &= !EMPATHY_SUBSCRIPTION_TO;
1216 tp_contact_list_remove_local_pending (list, contact);
1219 tp_contact_list_block_contact (list, contact);
1220 empathy_contact_set_subscription (contact, subscription);
1221 tp_contact_list_unblock_contact (list, contact);
1223 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1226 if ((l = g_list_find (priv->members, contact))) {
1227 g_signal_emit_by_name (list, "contact-removed", contact);
1228 priv->members = g_list_delete_link (priv->members, l);
1229 g_object_unref (contact);
1232 g_object_unref (contact);
1235 g_list_free (removed_list);
1239 tp_contact_list_pending_cb (EmpathyTpGroup *group,
1243 const gchar *message,
1244 EmpathyTpContactList *list)
1246 EmpathyTpContactListPriv *priv;
1247 GList *pending_list, *l;
1248 TpContactListType list_type;
1250 priv = GET_PRIV (list);
1252 list_type = tp_contact_list_get_type (list, group);
1254 pending_list = empathy_tp_contact_list_get_from_handles (list, handles);
1255 for (l = pending_list; l; l = l->next) {
1256 EmpathyContact *contact;
1258 contact = EMPATHY_CONTACT (l->data);
1260 empathy_debug (DEBUG_DOMAIN, "Contact '%s' pending in list type %d",
1261 empathy_contact_get_name (contact),
1264 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH) {
1265 if (!g_list_find (priv->members, contact)) {
1266 EmpathyContactListInfo *info;
1268 info = empathy_contact_list_info_new (contact, message);
1269 priv->local_pending = g_list_prepend (priv->local_pending,
1272 g_signal_emit_by_name (list, "local-pending",
1277 /* That contact wants our presence and he is
1278 * in our roster. Accept to publish our presence
1279 * without asking the user. */
1280 handle = empathy_contact_get_handle (contact);
1281 empathy_tp_group_add_member (priv->publish,
1285 else if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE) {
1286 if (!g_list_find (priv->members, contact)) {
1287 priv->members = g_list_prepend (priv->members,
1288 g_object_ref (contact));
1289 g_signal_emit_by_name (list, "contact-added", contact);
1293 g_object_unref (contact);
1296 g_list_free (pending_list);
1300 tp_contact_list_remove_local_pending (EmpathyTpContactList *list,
1301 EmpathyContact *contact)
1303 EmpathyTpContactListPriv *priv;
1306 priv = GET_PRIV (list);
1308 for (l = priv->local_pending; l; l = l->next) {
1309 EmpathyContactListInfo *info;
1312 if (empathy_contact_equal (contact, info->contact)) {
1313 empathy_debug (DEBUG_DOMAIN, "Contact no more local-pending: %s",
1314 empathy_contact_get_name (contact));
1316 priv->local_pending = g_list_delete_link (priv->local_pending, l);
1317 empathy_contact_list_info_free (info);
1324 tp_contact_list_groups_updated_cb (EmpathyContact *contact,
1326 EmpathyTpContactList *list)
1328 EmpathyTpContactListPriv *priv;
1329 TpContactListData data;
1332 priv = GET_PRIV (list);
1334 /* Make sure all groups are created */
1335 groups = empathy_contact_get_groups (contact);
1336 for (l = groups; l; l = l->next) {
1337 tp_contact_list_get_group (list, l->data);
1340 data.handle = empathy_contact_get_handle (contact);
1341 data.new_groups = groups;
1343 g_hash_table_foreach (priv->groups,
1344 (GHFunc) tp_contact_list_update_groups_foreach,
1349 tp_contact_list_name_updated_cb (EmpathyContact *contact,
1351 EmpathyTpContactList *list)
1353 EmpathyTpContactListPriv *priv;
1354 GHashTable *new_alias;
1355 const gchar *new_name;
1357 GError *error = NULL;
1359 priv = GET_PRIV (list);
1361 if (!priv->aliasing_iface) {
1365 handle = empathy_contact_get_handle (contact);
1366 new_name = empathy_contact_get_name (contact);
1368 empathy_debug (DEBUG_DOMAIN, "renaming handle %d to %s",
1371 new_alias = g_hash_table_new_full (g_direct_hash,
1376 g_hash_table_insert (new_alias,
1377 GUINT_TO_POINTER (handle),
1378 g_strdup (new_name));
1380 if (!tp_conn_iface_aliasing_set_aliases (priv->aliasing_iface,
1383 empathy_debug (DEBUG_DOMAIN,
1384 "Couldn't rename contact: %s",
1385 error ? error->message : "No error given");
1386 g_clear_error (&error);
1389 g_hash_table_destroy (new_alias);
1393 tp_contact_list_update_groups_foreach (gchar *object_path,
1394 EmpathyTpGroup *group,
1395 TpContactListData *data)
1398 gboolean found = FALSE;
1399 const gchar *group_name;
1402 is_member = empathy_tp_group_is_member (group, data->handle);
1403 group_name = empathy_tp_group_get_name (group);
1405 for (l = data->new_groups; l; l = l->next) {
1406 if (strcmp (group_name, l->data) == 0) {
1412 if (is_member && !found) {
1413 /* We are no longer member of this group */
1414 empathy_debug (DEBUG_DOMAIN, "Contact %d removed from group '%s'",
1415 data->handle, group_name);
1416 empathy_tp_group_remove_member (group, data->handle, "");
1419 if (!is_member && found) {
1420 /* We are now member of this group */
1421 empathy_debug (DEBUG_DOMAIN, "Contact %d added to group '%s'",
1422 data->handle, group_name);
1423 empathy_tp_group_add_member (group, data->handle, "");
1427 static EmpathyTpGroup *
1428 tp_contact_list_get_group (EmpathyTpContactList *list,
1431 EmpathyTpContactListPriv *priv;
1432 EmpathyTpGroup *group;
1433 TpChan *group_channel;
1436 char *group_object_path;
1437 const char *names[2] = {name, NULL};
1438 GError *error = NULL;
1440 priv = GET_PRIV (list);
1442 group = g_hash_table_find (priv->groups,
1443 (GHRFunc) tp_contact_list_find_group,
1449 empathy_debug (DEBUG_DOMAIN, "creating new group: %s", name);
1451 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
1452 TP_HANDLE_TYPE_GROUP,
1456 empathy_debug (DEBUG_DOMAIN,
1457 "Couldn't request the creation of a new handle for group: %s",
1458 error ? error->message : "No error given");
1459 g_clear_error (&error);
1462 group_handle = g_array_index (handles, guint, 0);
1463 g_array_free (handles, TRUE);
1465 if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
1466 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1467 TP_HANDLE_TYPE_GROUP,
1472 empathy_debug (DEBUG_DOMAIN,
1473 "Couldn't request the creation of a new group channel: %s",
1474 error ? error->message : "No error given");
1475 g_clear_error (&error);
1479 group_channel = tp_chan_new (tp_get_bus (),
1480 dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn)),
1482 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
1483 TP_HANDLE_TYPE_GROUP,
1486 dbus_g_proxy_connect_signal (DBUS_G_PROXY (group_channel),
1489 (tp_contact_list_group_channel_closed_cb),
1493 group = empathy_tp_group_new (priv->account, group_channel);
1494 g_hash_table_insert (priv->groups, group_object_path, group);
1495 g_signal_connect (group, "members-added",
1496 G_CALLBACK (tp_contact_list_group_members_added_cb),
1498 g_signal_connect (group, "members-removed",
1499 G_CALLBACK (tp_contact_list_group_members_removed_cb),
1506 tp_contact_list_find_group (gchar *key,
1507 EmpathyTpGroup *group,
1510 if (strcmp (group_name, empathy_tp_group_get_name (group)) == 0) {
1518 tp_contact_list_get_groups_foreach (gchar *key,
1519 EmpathyTpGroup *group,
1524 name = empathy_tp_group_get_name (group);
1525 *groups = g_list_append (*groups, g_strdup (name));
1529 tp_contact_list_group_channel_closed_cb (TpChan *channel,
1530 EmpathyTpContactList *list)
1532 EmpathyTpContactListPriv *priv;
1534 priv = GET_PRIV (list);
1536 g_hash_table_remove (priv->groups,
1537 dbus_g_proxy_get_path (DBUS_G_PROXY (channel)));
1541 tp_contact_list_group_members_added_cb (EmpathyTpGroup *group,
1545 const gchar *message,
1546 EmpathyTpContactList *list)
1548 EmpathyTpContactListPriv *priv;
1549 GList *added_list, *l;
1550 const gchar *group_name;
1552 priv = GET_PRIV (list);
1554 group_name = empathy_tp_group_get_name (group);
1555 added_list = empathy_tp_contact_list_get_from_handles (list, members);
1557 for (l = added_list; l; l = l->next) {
1558 EmpathyContact *contact;
1560 contact = EMPATHY_CONTACT (l->data);
1562 tp_contact_list_block_contact (list, contact);
1563 empathy_contact_add_group (contact, group_name);
1564 tp_contact_list_unblock_contact (list, contact);
1566 g_object_unref (contact);
1569 g_list_free (added_list);
1573 tp_contact_list_group_members_removed_cb (EmpathyTpGroup *group,
1577 const gchar *message,
1578 EmpathyTpContactList *list)
1580 EmpathyTpContactListPriv *priv;
1581 GList *removed_list, *l;
1582 const gchar *group_name;
1584 priv = GET_PRIV (list);
1586 group_name = empathy_tp_group_get_name (group);
1587 removed_list = empathy_tp_contact_list_get_from_handles (list, members);
1589 for (l = removed_list; l; l = l->next) {
1590 EmpathyContact *contact;
1594 tp_contact_list_block_contact (list, contact);
1595 empathy_contact_remove_group (contact, group_name);
1596 tp_contact_list_unblock_contact (list, contact);
1598 g_object_unref (contact);
1601 g_list_free (removed_list);
1605 tp_contact_list_get_info (EmpathyTpContactList *list,
1608 EmpathyTpContactListPriv *priv;
1609 GError *error = NULL;
1611 priv = GET_PRIV (list);
1613 if (priv->presence_iface) {
1614 /* FIXME: We should use GetPresence instead */
1615 if (!tp_conn_iface_presence_request_presence (priv->presence_iface,
1617 empathy_debug (DEBUG_DOMAIN,
1618 "Could not request presences: %s",
1619 error ? error->message : "No error given");
1620 g_clear_error (&error);
1624 if (priv->aliasing_iface) {
1625 TpContactListAliasesRequestData *data;
1627 data = g_slice_new (TpContactListAliasesRequestData);
1629 data->handles = g_memdup (handles->data, handles->len * sizeof (guint));
1631 tp_conn_iface_aliasing_request_aliases_async (priv->aliasing_iface,
1633 (tp_conn_iface_aliasing_request_aliases_reply)
1634 tp_contact_list_request_aliases_cb,
1638 if (priv->avatars_iface) {
1641 for (i = 0; i < handles->len; i++) {
1644 handle = g_array_index (handles, gint, i);
1645 tp_contact_list_request_avatar (list, handle);
1651 tp_contact_list_request_avatar (EmpathyTpContactList *list,
1654 EmpathyTpContactListPriv *priv;
1655 TpContactListAvatarRequestData *data;
1657 priv = GET_PRIV (list);
1659 /* We queue avatar requests to not send too many dbus async
1660 * calls at once. If we don't we reach the dbus's limit of
1662 data = g_slice_new (TpContactListAvatarRequestData);
1663 data->list = g_object_ref (list);
1664 data->handle = handle;
1665 avatar_requests_queue = g_list_append (avatar_requests_queue, data);
1666 tp_contact_list_start_avatar_requests ();
1670 tp_contact_list_start_avatar_requests (void)
1672 empathy_debug (DEBUG_DOMAIN, "Start avatar requests, pending calls: %d",
1675 while (n_avatar_requests < MAX_AVATAR_REQUESTS && avatar_requests_queue) {
1676 EmpathyTpContactListPriv *priv;
1677 TpContactListAvatarRequestData *data;
1679 data = avatar_requests_queue->data;
1680 priv = GET_PRIV (data->list);
1682 n_avatar_requests++;
1683 avatar_requests_queue = g_list_delete_link (avatar_requests_queue,
1684 avatar_requests_queue);
1686 empathy_debug (DEBUG_DOMAIN, "Calling RequestAvatar async");
1687 tp_conn_iface_avatars_request_avatar_async (priv->avatars_iface,
1689 (tp_conn_iface_avatars_request_avatar_reply)
1690 tp_contact_list_request_avatar_cb,
1696 tp_contact_list_avatar_update_cb (DBusGProxy *proxy,
1699 EmpathyTpContactList *list)
1701 EmpathyTpContactListPriv *priv;
1703 priv = GET_PRIV (list);
1705 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1706 /* We don't know this contact, skip */
1710 empathy_debug (DEBUG_DOMAIN, "Changing avatar for %d to %s",
1713 tp_contact_list_request_avatar (list, handle);
1717 tp_contact_list_request_avatar_cb (DBusGProxy *proxy,
1718 GArray *avatar_data,
1721 TpContactListAvatarRequestData *data)
1723 EmpathyContact *contact;
1725 contact = empathy_tp_contact_list_get_from_handle (data->list, data->handle);
1728 empathy_debug (DEBUG_DOMAIN, "Error requesting avatar for %s: %s",
1729 empathy_contact_get_name (contact),
1730 error ? error->message : "No error given");
1732 EmpathyAvatar *avatar;
1734 empathy_debug (DEBUG_DOMAIN, "Avatar received for %s (%d)",
1735 empathy_contact_get_id (contact),
1738 avatar = empathy_avatar_new (avatar_data->data,
1741 tp_contact_list_block_contact (data->list, contact);
1742 empathy_contact_set_avatar (contact, avatar);
1743 tp_contact_list_unblock_contact (data->list, contact);
1744 empathy_avatar_unref (avatar);
1747 n_avatar_requests--;
1748 tp_contact_list_start_avatar_requests ();
1750 g_object_unref (contact);
1751 g_object_unref (data->list);
1752 g_slice_free (TpContactListAvatarRequestData, data);
1756 tp_contact_list_aliases_update_cb (DBusGProxy *proxy,
1757 GPtrArray *renamed_handlers,
1758 EmpathyTpContactList *list)
1760 EmpathyTpContactListPriv *priv;
1763 priv = GET_PRIV (list);
1765 for (i = 0; renamed_handlers->len > i; i++) {
1768 GValueArray *renamed_struct;
1769 EmpathyContact *contact;
1771 renamed_struct = g_ptr_array_index (renamed_handlers, i);
1772 handle = g_value_get_uint(g_value_array_get_nth (renamed_struct, 0));
1773 alias = g_value_get_string(g_value_array_get_nth (renamed_struct, 1));
1775 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1776 /* We don't know this contact, skip */
1780 if (G_STR_EMPTY (alias)) {
1784 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1785 tp_contact_list_block_contact (list, contact);
1786 empathy_contact_set_name (contact, alias);
1787 tp_contact_list_unblock_contact (list, contact);
1788 g_object_unref (contact);
1790 empathy_debug (DEBUG_DOMAIN, "contact %d renamed to %s (update cb)",
1796 tp_contact_list_request_aliases_cb (DBusGProxy *proxy,
1797 gchar **contact_names,
1799 TpContactListAliasesRequestData *data)
1805 empathy_debug (DEBUG_DOMAIN, "Error requesting aliases: %s",
1809 for (name = contact_names; *name && !error; name++) {
1810 EmpathyContact *contact;
1812 contact = empathy_tp_contact_list_get_from_handle (data->list,
1814 tp_contact_list_block_contact (data->list, contact);
1815 empathy_contact_set_name (contact, *name);
1816 tp_contact_list_unblock_contact (data->list, contact);
1817 g_object_unref (contact);
1819 empathy_debug (DEBUG_DOMAIN, "contact %d renamed to %s (request cb)",
1820 data->handles[i], *name);
1825 g_free (data->handles);
1826 g_slice_free (TpContactListAliasesRequestData, data);
1830 tp_contact_list_presence_update_cb (DBusGProxy *proxy,
1831 GHashTable *handle_table,
1832 EmpathyTpContactList *list)
1834 g_hash_table_foreach (handle_table,
1835 (GHFunc) tp_contact_list_parse_presence_foreach,
1840 tp_contact_list_parse_presence_foreach (guint handle,
1841 GValueArray *presence_struct,
1842 EmpathyTpContactList *list)
1844 EmpathyTpContactListPriv *priv;
1845 GHashTable *presences_table;
1846 EmpathyContact *contact;
1847 EmpathyPresence *presence = NULL;
1849 priv = GET_PRIV (list);
1851 if (!g_hash_table_lookup (priv->contacts, GUINT_TO_POINTER (handle))) {
1852 /* We don't know this contact, skip */
1856 contact = empathy_tp_contact_list_get_from_handle (list, handle);
1857 presences_table = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
1859 g_hash_table_foreach (presences_table,
1860 (GHFunc) tp_contact_list_presences_table_foreach,
1863 empathy_debug (DEBUG_DOMAIN, "Presence changed for %s (%d) to %s (%d)",
1864 empathy_contact_get_name (contact),
1866 presence ? empathy_presence_get_status (presence) : "unset",
1867 presence ? empathy_presence_get_state (presence) : MC_PRESENCE_UNSET);
1869 tp_contact_list_block_contact (list, contact);
1870 empathy_contact_set_presence (contact, presence);
1871 tp_contact_list_unblock_contact (list, contact);
1873 g_object_unref (contact);
1877 tp_contact_list_presences_table_foreach (const gchar *state_str,
1878 GHashTable *presences_table,
1879 EmpathyPresence **presence)
1882 const GValue *message;
1884 state = empathy_presence_state_from_str (state_str);
1885 if ((state == MC_PRESENCE_UNSET) || (state == MC_PRESENCE_OFFLINE)) {
1890 g_object_unref (*presence);
1894 *presence = empathy_presence_new ();
1895 empathy_presence_set_state (*presence, state);
1897 message = g_hash_table_lookup (presences_table, "message");
1898 if (message != NULL) {
1899 empathy_presence_set_status (*presence,
1900 g_value_get_string (message));
1905 tp_contact_list_status_changed_cb (MissionControl *mc,
1906 TelepathyConnectionStatus status,
1907 McPresence presence,
1908 TelepathyConnectionStatusReason reason,
1909 const gchar *unique_name,
1910 EmpathyTpContactList *list)
1912 EmpathyTpContactListPriv *priv;
1915 priv = GET_PRIV (list);
1917 account = mc_account_lookup (unique_name);
1918 if (status != TP_CONN_STATUS_DISCONNECTED ||
1919 !empathy_account_equal (account, priv->account) ||
1921 g_object_unref (account);
1925 /* We are disconnected, do just like if the connection was destroyed */
1926 g_signal_handlers_disconnect_by_func (priv->tp_conn,
1927 tp_contact_list_destroy_cb,
1929 tp_contact_list_destroy_cb (DBUS_G_PROXY (priv->tp_conn), list);
1931 g_object_unref (account);