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 library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <glib/gi18n.h>
28 #include <libtelepathy/tp-helpers.h>
29 #include <libtelepathy/tp-conn.h>
30 #include <libtelepathy/tp-chan.h>
31 #include <libtelepathy/tp-chan-type-contact-list-gen.h>
33 #include "empathy-tp-contact-list.h"
34 #include "empathy-contact-list.h"
35 #include "empathy-tp-group.h"
36 #include "empathy-debug.h"
37 #include "empathy-utils.h"
39 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
40 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv))
42 #define DEBUG_DOMAIN "TpContactList"
44 struct _EmpathyTpContactListPriv {
48 const gchar *protocol_group;
50 EmpathyTpGroup *publish;
51 EmpathyTpGroup *subscribe;
56 GHashTable *contacts_groups;
60 TP_CONTACT_LIST_TYPE_PUBLISH,
61 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
62 TP_CONTACT_LIST_TYPE_UNKNOWN
65 static void empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass);
66 static void empathy_tp_contact_list_init (EmpathyTpContactList *list);
67 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
74 static guint signals[LAST_SIGNAL];
76 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
77 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
78 tp_contact_list_iface_init));
81 tp_contact_list_group_destroy_cb (EmpathyTpGroup *group,
82 EmpathyTpContactList *list)
84 EmpathyTpContactListPriv *priv = GET_PRIV (list);
86 empathy_debug (DEBUG_DOMAIN, "Group destroyed: %s",
87 empathy_tp_group_get_name (group));
89 priv->groups = g_list_remove (priv->groups, group);
90 g_object_unref (group);
94 tp_contact_list_group_member_added_cb (EmpathyTpGroup *group,
95 EmpathyContact *contact,
96 EmpathyContact *actor,
99 EmpathyTpContactList *list)
101 EmpathyTpContactListPriv *priv = GET_PRIV (list);
102 const gchar *group_name;
105 if (!g_list_find (priv->members, contact)) {
109 groups = g_hash_table_lookup (priv->contacts_groups, contact);
111 groups = g_slice_new0 (GList*);
112 g_hash_table_insert (priv->contacts_groups,
113 g_object_ref (contact),
117 group_name = empathy_tp_group_get_name (group);
118 if (!g_list_find_custom (*groups, group_name, (GCompareFunc) strcmp)) {
119 empathy_debug (DEBUG_DOMAIN, "Contact %s (%d) added to group %s",
120 empathy_contact_get_id (contact),
121 empathy_contact_get_handle (contact),
123 *groups = g_list_prepend (*groups, g_strdup (group_name));
124 g_signal_emit_by_name (list, "groups-changed", contact,
131 tp_contact_list_group_member_removed_cb (EmpathyTpGroup *group,
132 EmpathyContact *contact,
133 EmpathyContact *actor,
135 const gchar *message,
136 EmpathyTpContactList *list)
138 EmpathyTpContactListPriv *priv = GET_PRIV (list);
139 const gchar *group_name;
142 if (!g_list_find (priv->members, contact)) {
146 groups = g_hash_table_lookup (priv->contacts_groups, contact);
151 group_name = empathy_tp_group_get_name (group);
152 if ((l = g_list_find_custom (*groups, group_name, (GCompareFunc) strcmp))) {
153 empathy_debug (DEBUG_DOMAIN, "Contact %s (%d) removed from group %s",
154 empathy_contact_get_id (contact),
155 empathy_contact_get_handle (contact),
157 *groups = g_list_delete_link (*groups, l);
158 g_signal_emit_by_name (list, "groups-changed", contact,
164 static EmpathyTpGroup *
165 tp_contact_list_find_group (EmpathyTpContactList *list,
168 EmpathyTpContactListPriv *priv = GET_PRIV (list);
171 for (l = priv->groups; l; l = l->next) {
172 if (strcmp (group, empathy_tp_group_get_name (l->data)) == 0) {
179 static TpContactListType
180 tp_contact_list_get_type (EmpathyTpContactList *list,
181 EmpathyTpGroup *group)
183 EmpathyTpContactListPriv *priv;
184 TpContactListType list_type;
187 priv = GET_PRIV (list);
189 name = empathy_tp_group_get_name (group);
190 if (strcmp (name, "subscribe") == 0) {
191 list_type = TP_CONTACT_LIST_TYPE_SUBSCRIBE;
192 } else if (strcmp (name, "publish") == 0) {
193 list_type = TP_CONTACT_LIST_TYPE_PUBLISH;
195 list_type = TP_CONTACT_LIST_TYPE_UNKNOWN;
202 tp_contact_list_add_member (EmpathyTpContactList *list,
203 EmpathyContact *contact,
204 EmpathyContact *actor,
206 const gchar *message)
208 EmpathyTpContactListPriv *priv = GET_PRIV (list);
211 /* Add to the list and emit signal */
212 priv->members = g_list_prepend (priv->members, g_object_ref (contact));
213 g_signal_emit_by_name (list, "members-changed",
214 contact, actor, reason, message,
217 /* This contact is now member, implicitly accept pending. */
218 if (g_list_find (priv->pendings, contact)) {
219 empathy_tp_group_add_member (priv->publish, contact, "");
222 /* Update groups of the contact */
223 for (l = priv->groups; l; l = l->next) {
224 if (empathy_tp_group_is_member (l->data, contact)) {
225 tp_contact_list_group_member_added_cb (l->data, contact,
233 tp_contact_list_added_cb (EmpathyTpGroup *group,
234 EmpathyContact *contact,
235 EmpathyContact *actor,
237 const gchar *message,
238 EmpathyTpContactList *list)
240 EmpathyTpContactListPriv *priv = GET_PRIV (list);
241 TpContactListType list_type;
243 list_type = tp_contact_list_get_type (list, group);
244 empathy_debug (DEBUG_DOMAIN, "Contact %s (%d) added to list type %d",
245 empathy_contact_get_id (contact),
246 empathy_contact_get_handle (contact),
249 /* We now get the presence of that contact, add it to members */
250 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
251 !g_list_find (priv->members, contact)) {
252 tp_contact_list_add_member (list, contact, actor, reason, message);
255 /* We now send our presence to that contact, remove it from pendings */
256 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
257 g_list_find (priv->pendings, contact)) {
258 g_signal_emit_by_name (list, "pendings-changed",
259 contact, actor, reason, message,
261 priv->pendings = g_list_remove (priv->pendings, contact);
262 g_object_unref (contact);
267 tp_contact_list_removed_cb (EmpathyTpGroup *group,
268 EmpathyContact *contact,
269 EmpathyContact *actor,
271 const gchar *message,
272 EmpathyTpContactList *list)
274 EmpathyTpContactListPriv *priv = GET_PRIV (list);
275 TpContactListType list_type;
277 list_type = tp_contact_list_get_type (list, group);
278 empathy_debug (DEBUG_DOMAIN, "Contact %s (%d) removed from list type %d",
279 empathy_contact_get_id (contact),
280 empathy_contact_get_handle (contact),
283 /* This contact refuses to send us his presence, remove from members. */
284 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
285 g_list_find (priv->members, contact)) {
286 g_signal_emit_by_name (list, "members-changed",
287 contact, actor, reason, message,
289 priv->members = g_list_remove (priv->members, contact);
290 g_object_unref (contact);
293 /* We refuse to send our presence to that contact, remove from pendings */
294 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
295 g_list_find (priv->pendings, contact)) {
296 g_signal_emit_by_name (list, "pendings-changed",
297 contact, actor, reason, message,
299 priv->pendings = g_list_remove (priv->pendings, contact);
300 g_object_unref (contact);
305 tp_contact_list_pending_cb (EmpathyTpGroup *group,
306 EmpathyContact *contact,
307 EmpathyContact *actor,
309 const gchar *message,
310 EmpathyTpContactList *list)
312 EmpathyTpContactListPriv *priv = GET_PRIV (list);
313 TpContactListType list_type;
315 list_type = tp_contact_list_get_type (list, group);
316 empathy_debug (DEBUG_DOMAIN, "Contact %s (%d) pending in list type %d",
317 empathy_contact_get_id (contact),
318 empathy_contact_get_handle (contact),
321 /* We want this contact in our contact list but we don't get its
322 * presence yet. Add to members anyway. */
323 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
324 !g_list_find (priv->members, contact)) {
325 tp_contact_list_add_member (list, contact, actor, reason, message);
328 /* This contact wants our presence, auto accept if he is member,
329 * otherwise he is pending. */
330 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
331 !g_list_find (priv->pendings, contact)) {
332 if (g_list_find (priv->members, contact)) {
333 empathy_tp_group_add_member (priv->publish, contact, "");
335 priv->pendings = g_list_prepend (priv->pendings,
336 g_object_ref (contact));
337 g_signal_emit_by_name (list, "pendings-changed",
338 contact, actor, reason, message,
345 tp_contact_list_newchannel_cb (DBusGProxy *proxy,
346 const gchar *object_path,
347 const gchar *channel_type,
348 TpHandleType handle_type,
349 guint channel_handle,
350 gboolean suppress_handler,
351 EmpathyTpContactList *list)
353 EmpathyTpContactListPriv *priv = GET_PRIV (list);
354 EmpathyTpGroup *group;
356 const gchar *bus_name;
358 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
363 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn));
364 new_chan = tp_chan_new (tp_get_bus (),
370 g_return_if_fail (TELEPATHY_IS_CHAN (new_chan));
372 group = empathy_tp_group_new (priv->account, new_chan);
373 g_object_unref (new_chan);
375 if (handle_type == TP_HANDLE_TYPE_LIST) {
376 TpContactListType list_type;
379 list_type = tp_contact_list_get_type (list, group);
380 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH && !priv->publish) {
381 priv->publish = group;
383 /* Publish is the list of contacts to who we send our
384 * presence. Makes no sense to be in remote-pending */
385 g_signal_connect (group, "local-pending",
386 G_CALLBACK (tp_contact_list_pending_cb),
389 contacts = empathy_tp_group_get_local_pendings (group);
390 for (l = contacts; l; l = l->next) {
391 EmpathyPendingInfo *info = l->data;
393 tp_contact_list_pending_cb (group,
399 empathy_pending_info_free (info);
401 g_list_free (contacts);
403 else if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE && !priv->subscribe) {
404 priv->subscribe = group;
406 /* Subscribe is the list of contacts from who we
407 * receive presence. Makes no sense to be in
409 g_signal_connect (group, "remote-pending",
410 G_CALLBACK (tp_contact_list_pending_cb),
413 contacts = empathy_tp_group_get_remote_pendings (group);
414 for (l = contacts; l; l = l->next) {
415 tp_contact_list_pending_cb (group,
419 g_object_unref (l->data);
421 g_list_free (contacts);
423 empathy_debug (DEBUG_DOMAIN,
424 "Type of contact list channel unknown "
425 "or aleady have that list: %s",
426 empathy_tp_group_get_name (group));
427 g_object_unref (group);
430 empathy_debug (DEBUG_DOMAIN,
431 "New contact list channel of type: %d",
434 g_signal_connect (group, "member-added",
435 G_CALLBACK (tp_contact_list_added_cb),
437 g_signal_connect (group, "member-removed",
438 G_CALLBACK (tp_contact_list_removed_cb),
441 contacts = empathy_tp_group_get_members (group);
442 for (l = contacts; l; l = l->next) {
443 tp_contact_list_added_cb (group,
447 g_object_unref (l->data);
449 g_list_free (contacts);
451 else if (handle_type == TP_HANDLE_TYPE_GROUP) {
452 const gchar *group_name;
455 /* Check if already exists */
456 group_name = empathy_tp_group_get_name (group);
457 if (tp_contact_list_find_group (list, group_name)) {
458 g_object_unref (group);
462 empathy_debug (DEBUG_DOMAIN, "New server-side group channel: %s",
465 priv->groups = g_list_prepend (priv->groups, group);
467 g_signal_connect (group, "member-added",
468 G_CALLBACK (tp_contact_list_group_member_added_cb),
470 g_signal_connect (group, "member-removed",
471 G_CALLBACK (tp_contact_list_group_member_removed_cb),
473 g_signal_connect (group, "destroy",
474 G_CALLBACK (tp_contact_list_group_destroy_cb),
477 contacts = empathy_tp_group_get_members (group);
478 for (l = contacts; l; l = l->next) {
479 tp_contact_list_group_member_added_cb (group, l->data,
482 g_object_unref (l->data);
484 g_list_free (contacts);
486 empathy_debug (DEBUG_DOMAIN,
487 "Unknown handle type (%d) for contact list channel",
489 g_object_unref (group);
494 tp_contact_list_destroy_cb (TpConn *tp_conn,
495 EmpathyTpContactList *list)
497 EmpathyTpContactListPriv *priv = GET_PRIV (list);
500 empathy_debug (DEBUG_DOMAIN, "Account disconnected or CM crashed");
502 /* DBus proxie should NOT be used anymore */
503 g_object_unref (priv->tp_conn);
504 priv->tp_conn = NULL;
506 /* Remove all contacts */
507 for (l = priv->members; l; l = l->next) {
508 g_signal_emit_by_name (list, "members-changed", l->data,
511 g_object_unref (l->data);
513 for (l = priv->pendings; l; l = l->next) {
514 g_signal_emit_by_name (list, "pendings-changed", l->data,
517 g_object_unref (l->data);
519 g_list_free (priv->members);
520 g_list_free (priv->pendings);
521 priv->members = NULL;
522 priv->pendings = NULL;
524 /* Tell the world to not use us anymore */
525 g_signal_emit (list, signals[DESTROY], 0);
529 tp_contact_list_disconnect (EmpathyTpContactList *list)
531 EmpathyTpContactListPriv *priv = GET_PRIV (list);
534 g_signal_handlers_disconnect_by_func (priv->tp_conn,
535 tp_contact_list_destroy_cb,
537 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
538 G_CALLBACK (tp_contact_list_newchannel_cb),
544 tp_contact_list_status_changed_cb (MissionControl *mc,
545 TpConnectionStatus status,
547 TpConnectionStatusReason reason,
548 const gchar *unique_name,
549 EmpathyTpContactList *list)
551 EmpathyTpContactListPriv *priv = GET_PRIV (list);
554 account = mc_account_lookup (unique_name);
555 if (status != TP_CONNECTION_STATUS_CONNECTED &&
556 empathy_account_equal (account, priv->account)) {
557 /* We are disconnected */
558 tp_contact_list_disconnect (list);
559 tp_contact_list_destroy_cb (priv->tp_conn, list);
562 g_object_unref (account);
566 tp_contact_list_group_list_free (GList **groups)
568 g_list_foreach (*groups, (GFunc) g_free, NULL);
569 g_list_free (*groups);
570 g_slice_free (GList*, groups);
574 tp_contact_list_finalize (GObject *object)
576 EmpathyTpContactListPriv *priv;
577 EmpathyTpContactList *list;
579 list = EMPATHY_TP_CONTACT_LIST (object);
580 priv = GET_PRIV (list);
582 empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
584 tp_contact_list_disconnect (list);
587 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
588 "AccountStatusChanged",
589 G_CALLBACK (tp_contact_list_status_changed_cb),
591 g_object_unref (priv->mc);
594 if (priv->subscribe) {
595 g_object_unref (priv->subscribe);
598 g_object_unref (priv->publish);
601 g_object_unref (priv->account);
604 g_object_unref (priv->tp_conn);
607 g_hash_table_destroy (priv->contacts_groups);
608 g_list_foreach (priv->groups, (GFunc) g_object_unref, NULL);
609 g_list_free (priv->groups);
610 g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
611 g_list_free (priv->members);
612 g_list_foreach (priv->pendings, (GFunc) g_object_unref, NULL);
613 g_list_free (priv->pendings);
615 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
619 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
621 GObjectClass *object_class = G_OBJECT_CLASS (klass);
623 object_class->finalize = tp_contact_list_finalize;
626 g_signal_new ("destroy",
627 G_TYPE_FROM_CLASS (klass),
631 g_cclosure_marshal_VOID__VOID,
635 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
639 empathy_tp_contact_list_init (EmpathyTpContactList *list)
644 tp_contact_list_setup (EmpathyTpContactList *list)
646 EmpathyTpContactListPriv *priv = GET_PRIV (list);
649 GError *error = NULL;
651 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
653 /* Get existing channels */
654 if (!tp_conn_list_channels (DBUS_G_PROXY (priv->tp_conn),
657 empathy_debug (DEBUG_DOMAIN,
658 "Failed to get list of open channels: %s",
659 error ? error->message : "No error given");
660 g_clear_error (&error);
664 for (i = 0; i < channels->len; i++) {
665 GValueArray *chan_struct;
666 const gchar *object_path;
667 const gchar *chan_iface;
668 TpHandleType handle_type;
671 chan_struct = g_ptr_array_index (channels, i);
672 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
673 chan_iface = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
674 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
675 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
677 tp_contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
678 object_path, chan_iface,
683 g_value_array_free (chan_struct);
685 g_ptr_array_free (channels, TRUE);
688 EmpathyTpContactList *
689 empathy_tp_contact_list_new (McAccount *account)
691 EmpathyTpContactListPriv *priv;
692 EmpathyTpContactList *list;
694 TpConn *tp_conn = NULL;
696 const gchar *protocol_name;
698 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
700 mc = empathy_mission_control_new ();
702 /* status==0 means CONNECTED */
703 if (mission_control_get_connection_status (mc, account, NULL) == 0) {
704 tp_conn = mission_control_get_connection (mc, account, NULL);
707 /* The account is not connected, nothing to do. */
712 list = g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST, NULL);
713 priv = GET_PRIV (list);
715 priv->tp_conn = tp_conn;
716 priv->account = g_object_ref (account);
718 priv->contacts_groups = g_hash_table_new_full (empathy_contact_hash,
719 empathy_contact_equal,
720 (GDestroyNotify) g_object_unref,
721 (GDestroyNotify) tp_contact_list_group_list_free);
723 /* Check for protocols that does not support contact groups. We can
724 * put all contacts into a special group in that case.
725 * FIXME: Default group should be an information in the profile */
726 profile = mc_account_get_profile (account);
727 protocol_name = mc_profile_get_protocol_name (profile);
728 if (strcmp (protocol_name, "local-xmpp") == 0) {
729 priv->protocol_group = _("People nearby");
731 g_object_unref (profile);
733 /* Connect signals */
734 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
735 "AccountStatusChanged",
736 G_CALLBACK (tp_contact_list_status_changed_cb),
738 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
739 G_CALLBACK (tp_contact_list_newchannel_cb),
741 g_signal_connect (priv->tp_conn, "destroy",
742 G_CALLBACK (tp_contact_list_destroy_cb),
745 tp_contact_list_setup (list);
751 empathy_tp_contact_list_get_account (EmpathyTpContactList *list)
753 EmpathyTpContactListPriv *priv;
755 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
757 priv = GET_PRIV (list);
759 return priv->account;
763 tp_contact_list_add (EmpathyContactList *list,
764 EmpathyContact *contact,
765 const gchar *message)
767 EmpathyTpContactListPriv *priv = GET_PRIV (list);
769 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
771 empathy_tp_group_add_member (priv->subscribe, contact, message);
772 if (g_list_find (priv->pendings, contact)) {
773 empathy_tp_group_add_member (priv->publish, contact, message);
778 tp_contact_list_remove (EmpathyContactList *list,
779 EmpathyContact *contact,
780 const gchar *message)
782 EmpathyTpContactListPriv *priv = GET_PRIV (list);
784 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
786 empathy_tp_group_remove_member (priv->subscribe, contact, message);
787 empathy_tp_group_remove_member (priv->publish, contact, message);
791 tp_contact_list_get_members (EmpathyContactList *list)
793 EmpathyTpContactListPriv *priv = GET_PRIV (list);
795 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
797 g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
798 return g_list_copy (priv->members);
802 tp_contact_list_get_pendings (EmpathyContactList *list)
804 EmpathyTpContactListPriv *priv = GET_PRIV (list);
806 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
808 g_list_foreach (priv->pendings, (GFunc) g_object_ref, NULL);
809 return g_list_copy (priv->pendings);
813 tp_contact_list_get_all_groups (EmpathyContactList *list)
815 EmpathyTpContactListPriv *priv = GET_PRIV (list);
816 GList *groups = NULL, *l;
818 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
820 if (priv->protocol_group) {
821 groups = g_list_prepend (groups, g_strdup (priv->protocol_group));
824 for (l = priv->groups; l; l = l->next) {
827 name = empathy_tp_group_get_name (l->data);
828 groups = g_list_prepend (groups, g_strdup (name));
835 tp_contact_list_get_groups (EmpathyContactList *list,
836 EmpathyContact *contact)
838 EmpathyTpContactListPriv *priv = GET_PRIV (list);
840 GList *ret = NULL, *l;
842 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
844 if (priv->protocol_group) {
845 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
848 groups = g_hash_table_lookup (priv->contacts_groups, contact);
853 for (l = *groups; l; l = l->next) {
854 ret = g_list_prepend (ret, g_strdup (l->data));
861 static EmpathyTpGroup *
862 tp_contact_list_get_group (EmpathyTpContactList *list,
865 EmpathyTpContactListPriv *priv = GET_PRIV (list);
866 EmpathyTpGroup *tp_group;
870 const char *names[2] = {group, NULL};
871 GError *error = NULL;
873 tp_group = tp_contact_list_find_group (list, group);
878 empathy_debug (DEBUG_DOMAIN, "creating new group: %s", group);
880 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
881 TP_HANDLE_TYPE_GROUP,
885 empathy_debug (DEBUG_DOMAIN,
886 "Failed to RequestHandles: %s",
887 error ? error->message : "No error given");
888 g_clear_error (&error);
891 handle = g_array_index (handles, guint, 0);
892 g_array_free (handles, TRUE);
894 if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
895 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
896 TP_HANDLE_TYPE_GROUP,
901 empathy_debug (DEBUG_DOMAIN,
902 "Failed to RequestChannel: %s",
903 error ? error->message : "No error given");
904 g_clear_error (&error);
908 tp_contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
910 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
911 TP_HANDLE_TYPE_GROUP,
914 g_free (object_path);
916 return tp_contact_list_find_group (list, group);
920 tp_contact_list_add_to_group (EmpathyContactList *list,
921 EmpathyContact *contact,
924 EmpathyTpGroup *tp_group;
926 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
928 tp_group = tp_contact_list_get_group (EMPATHY_TP_CONTACT_LIST (list),
931 empathy_tp_group_add_member (tp_group, contact, "");
935 tp_contact_list_remove_from_group (EmpathyContactList *list,
936 EmpathyContact *contact,
939 EmpathyTpGroup *tp_group;
941 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
943 tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
947 empathy_tp_group_remove_member (tp_group, contact, "");
952 tp_contact_list_rename_group (EmpathyContactList *list,
953 const gchar *old_group,
954 const gchar *new_group)
956 EmpathyTpGroup *tp_group;
959 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
961 tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
967 empathy_debug (DEBUG_DOMAIN, "rename group %s to %s", old_group, new_group);
969 /* Remove all members from the old group */
970 members = empathy_tp_group_get_members (tp_group);
971 empathy_tp_group_remove_members (tp_group, members, "");
972 empathy_tp_group_close (tp_group);
974 /* Add all members to the new group */
975 tp_group = tp_contact_list_get_group (EMPATHY_TP_CONTACT_LIST (list),
977 empathy_tp_group_add_members (tp_group, members, "");
979 g_list_foreach (members, (GFunc) g_object_unref, NULL);
980 g_list_free (members);
984 tp_contact_list_iface_init (EmpathyContactListIface *iface)
986 iface->add = tp_contact_list_add;
987 iface->remove = tp_contact_list_remove;
988 iface->get_members = tp_contact_list_get_members;
989 iface->get_pendings = tp_contact_list_get_pendings;
990 iface->get_all_groups = tp_contact_list_get_all_groups;
991 iface->get_groups = tp_contact_list_get_groups;
992 iface->add_to_group = tp_contact_list_add_to_group;
993 iface->remove_from_group = tp_contact_list_remove_from_group;
994 iface->rename_group = tp_contact_list_rename_group;