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>
32 #include <telepathy-glib/util.h>
34 #include "empathy-tp-contact-list.h"
35 #include "empathy-contact-list.h"
36 #include "empathy-tp-group.h"
37 #include "empathy-debug.h"
38 #include "empathy-utils.h"
40 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
41 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv))
43 #define DEBUG_DOMAIN "TpContactList"
45 struct _EmpathyTpContactListPriv {
49 const gchar *protocol_group;
51 EmpathyTpGroup *publish;
52 EmpathyTpGroup *subscribe;
57 GHashTable *contacts_groups;
61 TP_CONTACT_LIST_TYPE_PUBLISH,
62 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
63 TP_CONTACT_LIST_TYPE_UNKNOWN
66 static void empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass);
67 static void empathy_tp_contact_list_init (EmpathyTpContactList *list);
68 static void tp_contact_list_iface_init (EmpathyContactListIface *iface);
75 static guint signals[LAST_SIGNAL];
77 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
78 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
79 tp_contact_list_iface_init));
82 tp_contact_list_group_destroy_cb (EmpathyTpGroup *group,
83 EmpathyTpContactList *list)
85 EmpathyTpContactListPriv *priv = GET_PRIV (list);
87 empathy_debug (DEBUG_DOMAIN, "Group destroyed: %s",
88 empathy_tp_group_get_name (group));
90 priv->groups = g_list_remove (priv->groups, group);
91 g_object_unref (group);
95 tp_contact_list_group_member_added_cb (EmpathyTpGroup *group,
96 EmpathyContact *contact,
97 EmpathyContact *actor,
100 EmpathyTpContactList *list)
102 EmpathyTpContactListPriv *priv = GET_PRIV (list);
103 const gchar *group_name;
106 if (!g_list_find (priv->members, contact)) {
110 groups = g_hash_table_lookup (priv->contacts_groups, contact);
112 groups = g_slice_new0 (GList*);
113 g_hash_table_insert (priv->contacts_groups,
114 g_object_ref (contact),
118 group_name = empathy_tp_group_get_name (group);
119 if (!g_list_find_custom (*groups, group_name, (GCompareFunc) strcmp)) {
120 empathy_debug (DEBUG_DOMAIN, "Contact %s (%d) added to group %s",
121 empathy_contact_get_id (contact),
122 empathy_contact_get_handle (contact),
124 *groups = g_list_prepend (*groups, g_strdup (group_name));
125 g_signal_emit_by_name (list, "groups-changed", contact,
132 tp_contact_list_group_member_removed_cb (EmpathyTpGroup *group,
133 EmpathyContact *contact,
134 EmpathyContact *actor,
136 const gchar *message,
137 EmpathyTpContactList *list)
139 EmpathyTpContactListPriv *priv = GET_PRIV (list);
140 const gchar *group_name;
143 if (!g_list_find (priv->members, contact)) {
147 groups = g_hash_table_lookup (priv->contacts_groups, contact);
152 group_name = empathy_tp_group_get_name (group);
153 if ((l = g_list_find_custom (*groups, group_name, (GCompareFunc) strcmp))) {
154 empathy_debug (DEBUG_DOMAIN, "Contact %s (%d) removed from group %s",
155 empathy_contact_get_id (contact),
156 empathy_contact_get_handle (contact),
158 *groups = g_list_delete_link (*groups, l);
159 g_signal_emit_by_name (list, "groups-changed", contact,
165 static EmpathyTpGroup *
166 tp_contact_list_find_group (EmpathyTpContactList *list,
169 EmpathyTpContactListPriv *priv = GET_PRIV (list);
172 for (l = priv->groups; l; l = l->next) {
173 if (!tp_strdiff (group, empathy_tp_group_get_name (l->data))) {
180 static TpContactListType
181 tp_contact_list_get_type (EmpathyTpContactList *list,
182 EmpathyTpGroup *group)
186 name = empathy_tp_group_get_name (group);
187 if (!tp_strdiff (name, "subscribe")) {
188 return TP_CONTACT_LIST_TYPE_SUBSCRIBE;
189 } else if (!tp_strdiff (name, "publish")) {
190 return TP_CONTACT_LIST_TYPE_PUBLISH;
193 return TP_CONTACT_LIST_TYPE_UNKNOWN;
197 tp_contact_list_add_member (EmpathyTpContactList *list,
198 EmpathyContact *contact,
199 EmpathyContact *actor,
201 const gchar *message)
203 EmpathyTpContactListPriv *priv = GET_PRIV (list);
206 /* Add to the list and emit signal */
207 priv->members = g_list_prepend (priv->members, g_object_ref (contact));
208 g_signal_emit_by_name (list, "members-changed",
209 contact, actor, reason, message,
212 /* This contact is now member, implicitly accept pending. */
213 if (g_list_find (priv->pendings, contact)) {
214 empathy_tp_group_add_member (priv->publish, contact, "");
217 /* Update groups of the contact */
218 for (l = priv->groups; l; l = l->next) {
219 if (empathy_tp_group_is_member (l->data, contact)) {
220 tp_contact_list_group_member_added_cb (l->data, contact,
228 tp_contact_list_added_cb (EmpathyTpGroup *group,
229 EmpathyContact *contact,
230 EmpathyContact *actor,
232 const gchar *message,
233 EmpathyTpContactList *list)
235 EmpathyTpContactListPriv *priv = GET_PRIV (list);
236 TpContactListType list_type;
238 list_type = tp_contact_list_get_type (list, group);
239 empathy_debug (DEBUG_DOMAIN, "Contact %s (%d) added to list type %d",
240 empathy_contact_get_id (contact),
241 empathy_contact_get_handle (contact),
244 /* We now get the presence of that contact, add it to members */
245 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
246 !g_list_find (priv->members, contact)) {
247 tp_contact_list_add_member (list, contact, actor, reason, message);
250 /* We now send our presence to that contact, remove it from pendings */
251 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
252 g_list_find (priv->pendings, contact)) {
253 g_signal_emit_by_name (list, "pendings-changed",
254 contact, actor, reason, message,
256 priv->pendings = g_list_remove (priv->pendings, contact);
257 g_object_unref (contact);
262 tp_contact_list_removed_cb (EmpathyTpGroup *group,
263 EmpathyContact *contact,
264 EmpathyContact *actor,
266 const gchar *message,
267 EmpathyTpContactList *list)
269 EmpathyTpContactListPriv *priv = GET_PRIV (list);
270 TpContactListType list_type;
272 list_type = tp_contact_list_get_type (list, group);
273 empathy_debug (DEBUG_DOMAIN, "Contact %s (%d) removed from list type %d",
274 empathy_contact_get_id (contact),
275 empathy_contact_get_handle (contact),
278 /* This contact refuses to send us his presence, remove from members. */
279 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
280 g_list_find (priv->members, contact)) {
281 g_signal_emit_by_name (list, "members-changed",
282 contact, actor, reason, message,
284 priv->members = g_list_remove (priv->members, contact);
285 g_object_unref (contact);
288 /* We refuse to send our presence to that contact, remove from pendings */
289 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
290 g_list_find (priv->pendings, contact)) {
291 g_signal_emit_by_name (list, "pendings-changed",
292 contact, actor, reason, message,
294 priv->pendings = g_list_remove (priv->pendings, contact);
295 g_object_unref (contact);
300 tp_contact_list_pending_cb (EmpathyTpGroup *group,
301 EmpathyContact *contact,
302 EmpathyContact *actor,
304 const gchar *message,
305 EmpathyTpContactList *list)
307 EmpathyTpContactListPriv *priv = GET_PRIV (list);
308 TpContactListType list_type;
310 list_type = tp_contact_list_get_type (list, group);
311 empathy_debug (DEBUG_DOMAIN, "Contact %s (%d) pending in list type %d",
312 empathy_contact_get_id (contact),
313 empathy_contact_get_handle (contact),
316 /* We want this contact in our contact list but we don't get its
317 * presence yet. Add to members anyway. */
318 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
319 !g_list_find (priv->members, contact)) {
320 tp_contact_list_add_member (list, contact, actor, reason, message);
323 /* This contact wants our presence, auto accept if he is member,
324 * otherwise he is pending. */
325 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
326 !g_list_find (priv->pendings, contact)) {
327 if (g_list_find (priv->members, contact)) {
328 empathy_tp_group_add_member (priv->publish, contact, "");
330 priv->pendings = g_list_prepend (priv->pendings,
331 g_object_ref (contact));
332 g_signal_emit_by_name (list, "pendings-changed",
333 contact, actor, reason, message,
340 tp_contact_list_newchannel_cb (DBusGProxy *proxy,
341 const gchar *object_path,
342 const gchar *channel_type,
343 TpHandleType handle_type,
344 guint channel_handle,
345 gboolean suppress_handler,
346 EmpathyTpContactList *list)
348 EmpathyTpContactListPriv *priv = GET_PRIV (list);
349 EmpathyTpGroup *group;
351 const gchar *bus_name;
353 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
358 bus_name = dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->tp_conn));
359 new_chan = tp_chan_new (tp_get_bus (),
365 g_return_if_fail (TELEPATHY_IS_CHAN (new_chan));
367 group = empathy_tp_group_new (priv->account, new_chan);
368 g_object_unref (new_chan);
370 if (handle_type == TP_HANDLE_TYPE_LIST) {
371 TpContactListType list_type;
374 list_type = tp_contact_list_get_type (list, group);
375 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH && !priv->publish) {
376 priv->publish = group;
378 /* Publish is the list of contacts to who we send our
379 * presence. Makes no sense to be in remote-pending */
380 g_signal_connect (group, "local-pending",
381 G_CALLBACK (tp_contact_list_pending_cb),
384 contacts = empathy_tp_group_get_local_pendings (group);
385 for (l = contacts; l; l = l->next) {
386 EmpathyPendingInfo *info = l->data;
388 tp_contact_list_pending_cb (group,
394 empathy_pending_info_free (info);
396 g_list_free (contacts);
398 else if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE && !priv->subscribe) {
399 priv->subscribe = group;
401 /* Subscribe is the list of contacts from who we
402 * receive presence. Makes no sense to be in
404 g_signal_connect (group, "remote-pending",
405 G_CALLBACK (tp_contact_list_pending_cb),
408 contacts = empathy_tp_group_get_remote_pendings (group);
409 for (l = contacts; l; l = l->next) {
410 tp_contact_list_pending_cb (group,
414 g_object_unref (l->data);
416 g_list_free (contacts);
418 empathy_debug (DEBUG_DOMAIN,
419 "Type of contact list channel unknown "
420 "or aleady have that list: %s",
421 empathy_tp_group_get_name (group));
422 g_object_unref (group);
425 empathy_debug (DEBUG_DOMAIN,
426 "New contact list channel of type: %d",
429 g_signal_connect (group, "member-added",
430 G_CALLBACK (tp_contact_list_added_cb),
432 g_signal_connect (group, "member-removed",
433 G_CALLBACK (tp_contact_list_removed_cb),
436 contacts = empathy_tp_group_get_members (group);
437 for (l = contacts; l; l = l->next) {
438 tp_contact_list_added_cb (group,
442 g_object_unref (l->data);
444 g_list_free (contacts);
446 else if (handle_type == TP_HANDLE_TYPE_GROUP) {
447 const gchar *group_name;
450 /* Check if already exists */
451 group_name = empathy_tp_group_get_name (group);
452 if (tp_contact_list_find_group (list, group_name)) {
453 g_object_unref (group);
457 empathy_debug (DEBUG_DOMAIN, "New server-side group channel: %s",
460 priv->groups = g_list_prepend (priv->groups, group);
462 g_signal_connect (group, "member-added",
463 G_CALLBACK (tp_contact_list_group_member_added_cb),
465 g_signal_connect (group, "member-removed",
466 G_CALLBACK (tp_contact_list_group_member_removed_cb),
468 g_signal_connect (group, "destroy",
469 G_CALLBACK (tp_contact_list_group_destroy_cb),
472 contacts = empathy_tp_group_get_members (group);
473 for (l = contacts; l; l = l->next) {
474 tp_contact_list_group_member_added_cb (group, l->data,
477 g_object_unref (l->data);
479 g_list_free (contacts);
481 empathy_debug (DEBUG_DOMAIN,
482 "Unknown handle type (%d) for contact list channel",
484 g_object_unref (group);
489 tp_contact_list_destroy_cb (TpConn *tp_conn,
490 EmpathyTpContactList *list)
492 EmpathyTpContactListPriv *priv = GET_PRIV (list);
495 empathy_debug (DEBUG_DOMAIN, "Account disconnected or CM crashed");
497 /* DBus proxie should NOT be used anymore */
498 g_object_unref (priv->tp_conn);
499 priv->tp_conn = NULL;
501 /* Remove all contacts */
502 for (l = priv->members; l; l = l->next) {
503 g_signal_emit_by_name (list, "members-changed", l->data,
506 g_object_unref (l->data);
508 for (l = priv->pendings; l; l = l->next) {
509 g_signal_emit_by_name (list, "pendings-changed", l->data,
512 g_object_unref (l->data);
514 g_list_free (priv->members);
515 g_list_free (priv->pendings);
516 priv->members = NULL;
517 priv->pendings = NULL;
519 /* Tell the world to not use us anymore */
520 g_signal_emit (list, signals[DESTROY], 0);
524 tp_contact_list_disconnect (EmpathyTpContactList *list)
526 EmpathyTpContactListPriv *priv = GET_PRIV (list);
529 g_signal_handlers_disconnect_by_func (priv->tp_conn,
530 tp_contact_list_destroy_cb,
532 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
533 G_CALLBACK (tp_contact_list_newchannel_cb),
539 tp_contact_list_status_changed_cb (MissionControl *mc,
540 TpConnectionStatus status,
542 TpConnectionStatusReason reason,
543 const gchar *unique_name,
544 EmpathyTpContactList *list)
546 EmpathyTpContactListPriv *priv = GET_PRIV (list);
549 account = mc_account_lookup (unique_name);
550 if (status != TP_CONNECTION_STATUS_CONNECTED &&
551 empathy_account_equal (account, priv->account)) {
552 /* We are disconnected */
553 tp_contact_list_disconnect (list);
554 tp_contact_list_destroy_cb (priv->tp_conn, list);
557 g_object_unref (account);
561 tp_contact_list_group_list_free (GList **groups)
563 g_list_foreach (*groups, (GFunc) g_free, NULL);
564 g_list_free (*groups);
565 g_slice_free (GList*, groups);
569 tp_contact_list_finalize (GObject *object)
571 EmpathyTpContactListPriv *priv;
572 EmpathyTpContactList *list;
574 list = EMPATHY_TP_CONTACT_LIST (object);
575 priv = GET_PRIV (list);
577 empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
579 tp_contact_list_disconnect (list);
582 dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
583 "AccountStatusChanged",
584 G_CALLBACK (tp_contact_list_status_changed_cb),
586 g_object_unref (priv->mc);
589 if (priv->subscribe) {
590 g_object_unref (priv->subscribe);
593 g_object_unref (priv->publish);
596 g_object_unref (priv->account);
599 g_object_unref (priv->tp_conn);
602 g_hash_table_destroy (priv->contacts_groups);
603 g_list_foreach (priv->groups, (GFunc) g_object_unref, NULL);
604 g_list_free (priv->groups);
605 g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
606 g_list_free (priv->members);
607 g_list_foreach (priv->pendings, (GFunc) g_object_unref, NULL);
608 g_list_free (priv->pendings);
610 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
614 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
616 GObjectClass *object_class = G_OBJECT_CLASS (klass);
618 object_class->finalize = tp_contact_list_finalize;
621 g_signal_new ("destroy",
622 G_TYPE_FROM_CLASS (klass),
626 g_cclosure_marshal_VOID__VOID,
630 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
634 empathy_tp_contact_list_init (EmpathyTpContactList *list)
639 tp_contact_list_setup (EmpathyTpContactList *list)
641 EmpathyTpContactListPriv *priv = GET_PRIV (list);
644 GError *error = NULL;
646 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
648 /* Get existing channels */
649 if (!tp_conn_list_channels (DBUS_G_PROXY (priv->tp_conn),
652 empathy_debug (DEBUG_DOMAIN,
653 "Failed to get list of open channels: %s",
654 error ? error->message : "No error given");
655 g_clear_error (&error);
659 for (i = 0; i < channels->len; i++) {
660 GValueArray *chan_struct;
661 const gchar *object_path;
662 const gchar *chan_iface;
663 TpHandleType handle_type;
666 chan_struct = g_ptr_array_index (channels, i);
667 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
668 chan_iface = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
669 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
670 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
672 tp_contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
673 object_path, chan_iface,
678 g_value_array_free (chan_struct);
680 g_ptr_array_free (channels, TRUE);
683 EmpathyTpContactList *
684 empathy_tp_contact_list_new (McAccount *account)
686 EmpathyTpContactListPriv *priv;
687 EmpathyTpContactList *list;
689 TpConn *tp_conn = NULL;
691 const gchar *protocol_name;
693 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
695 mc = empathy_mission_control_new ();
697 /* status==0 means CONNECTED */
698 if (mission_control_get_connection_status (mc, account, NULL) == 0) {
699 tp_conn = mission_control_get_connection (mc, account, NULL);
702 /* The account is not connected, nothing to do. */
707 list = g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST, NULL);
708 priv = GET_PRIV (list);
710 priv->tp_conn = tp_conn;
711 priv->account = g_object_ref (account);
713 priv->contacts_groups = g_hash_table_new_full (empathy_contact_hash,
714 empathy_contact_equal,
715 (GDestroyNotify) g_object_unref,
716 (GDestroyNotify) tp_contact_list_group_list_free);
718 /* Check for protocols that does not support contact groups. We can
719 * put all contacts into a special group in that case.
720 * FIXME: Default group should be an information in the profile */
721 profile = mc_account_get_profile (account);
722 protocol_name = mc_profile_get_protocol_name (profile);
723 if (strcmp (protocol_name, "local-xmpp") == 0) {
724 priv->protocol_group = _("People nearby");
726 g_object_unref (profile);
728 /* Connect signals */
729 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
730 "AccountStatusChanged",
731 G_CALLBACK (tp_contact_list_status_changed_cb),
733 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->tp_conn), "NewChannel",
734 G_CALLBACK (tp_contact_list_newchannel_cb),
736 g_signal_connect (priv->tp_conn, "destroy",
737 G_CALLBACK (tp_contact_list_destroy_cb),
740 tp_contact_list_setup (list);
746 empathy_tp_contact_list_get_account (EmpathyTpContactList *list)
748 EmpathyTpContactListPriv *priv;
750 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
752 priv = GET_PRIV (list);
754 return priv->account;
758 tp_contact_list_add (EmpathyContactList *list,
759 EmpathyContact *contact,
760 const gchar *message)
762 EmpathyTpContactListPriv *priv = GET_PRIV (list);
764 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
766 empathy_tp_group_add_member (priv->subscribe, contact, message);
767 if (g_list_find (priv->pendings, contact)) {
768 empathy_tp_group_add_member (priv->publish, contact, message);
773 tp_contact_list_remove (EmpathyContactList *list,
774 EmpathyContact *contact,
775 const gchar *message)
777 EmpathyTpContactListPriv *priv = GET_PRIV (list);
779 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
781 empathy_tp_group_remove_member (priv->subscribe, contact, message);
782 empathy_tp_group_remove_member (priv->publish, contact, message);
786 tp_contact_list_get_members (EmpathyContactList *list)
788 EmpathyTpContactListPriv *priv = GET_PRIV (list);
790 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
792 g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
793 return g_list_copy (priv->members);
797 tp_contact_list_get_pendings (EmpathyContactList *list)
799 EmpathyTpContactListPriv *priv = GET_PRIV (list);
801 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
803 g_list_foreach (priv->pendings, (GFunc) g_object_ref, NULL);
804 return g_list_copy (priv->pendings);
808 tp_contact_list_get_all_groups (EmpathyContactList *list)
810 EmpathyTpContactListPriv *priv = GET_PRIV (list);
811 GList *groups = NULL, *l;
813 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
815 if (priv->protocol_group) {
816 groups = g_list_prepend (groups, g_strdup (priv->protocol_group));
819 for (l = priv->groups; l; l = l->next) {
822 name = empathy_tp_group_get_name (l->data);
823 groups = g_list_prepend (groups, g_strdup (name));
830 tp_contact_list_get_groups (EmpathyContactList *list,
831 EmpathyContact *contact)
833 EmpathyTpContactListPriv *priv = GET_PRIV (list);
835 GList *ret = NULL, *l;
837 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
839 if (priv->protocol_group) {
840 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
843 groups = g_hash_table_lookup (priv->contacts_groups, contact);
848 for (l = *groups; l; l = l->next) {
849 ret = g_list_prepend (ret, g_strdup (l->data));
856 static EmpathyTpGroup *
857 tp_contact_list_get_group (EmpathyTpContactList *list,
860 EmpathyTpContactListPriv *priv = GET_PRIV (list);
861 EmpathyTpGroup *tp_group;
865 const char *names[2] = {group, NULL};
866 GError *error = NULL;
868 tp_group = tp_contact_list_find_group (list, group);
873 empathy_debug (DEBUG_DOMAIN, "creating new group: %s", group);
875 if (!tp_conn_request_handles (DBUS_G_PROXY (priv->tp_conn),
876 TP_HANDLE_TYPE_GROUP,
880 empathy_debug (DEBUG_DOMAIN,
881 "Failed to RequestHandles: %s",
882 error ? error->message : "No error given");
883 g_clear_error (&error);
886 handle = g_array_index (handles, guint, 0);
887 g_array_free (handles, TRUE);
889 if (!tp_conn_request_channel (DBUS_G_PROXY (priv->tp_conn),
890 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
891 TP_HANDLE_TYPE_GROUP,
896 empathy_debug (DEBUG_DOMAIN,
897 "Failed to RequestChannel: %s",
898 error ? error->message : "No error given");
899 g_clear_error (&error);
903 tp_contact_list_newchannel_cb (DBUS_G_PROXY (priv->tp_conn),
905 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
906 TP_HANDLE_TYPE_GROUP,
909 g_free (object_path);
911 return tp_contact_list_find_group (list, group);
915 tp_contact_list_add_to_group (EmpathyContactList *list,
916 EmpathyContact *contact,
919 EmpathyTpGroup *tp_group;
921 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
923 tp_group = tp_contact_list_get_group (EMPATHY_TP_CONTACT_LIST (list),
926 empathy_tp_group_add_member (tp_group, contact, "");
930 tp_contact_list_remove_from_group (EmpathyContactList *list,
931 EmpathyContact *contact,
934 EmpathyTpGroup *tp_group;
936 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
938 tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
942 empathy_tp_group_remove_member (tp_group, contact, "");
947 tp_contact_list_rename_group (EmpathyContactList *list,
948 const gchar *old_group,
949 const gchar *new_group)
951 EmpathyTpGroup *tp_group;
954 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
956 tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
962 empathy_debug (DEBUG_DOMAIN, "rename group %s to %s", old_group, new_group);
964 /* Remove all members from the old group */
965 members = empathy_tp_group_get_members (tp_group);
966 empathy_tp_group_remove_members (tp_group, members, "");
967 empathy_tp_group_close (tp_group);
969 /* Add all members to the new group */
970 tp_group = tp_contact_list_get_group (EMPATHY_TP_CONTACT_LIST (list),
972 empathy_tp_group_add_members (tp_group, members, "");
974 g_list_foreach (members, (GFunc) g_object_unref, NULL);
975 g_list_free (members);
979 tp_contact_list_remove_group (EmpathyContactList *list,
982 EmpathyTpGroup *tp_group;
985 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
987 tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
994 empathy_debug (DEBUG_DOMAIN, "remove group %s", group);
996 /* Remove all members of the group */
997 members = empathy_tp_group_get_members (tp_group);
998 empathy_tp_group_remove_members (tp_group, members, "");
999 empathy_tp_group_close (tp_group);
1001 g_list_foreach (members, (GFunc) g_object_unref, NULL);
1002 g_list_free (members);
1006 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1008 iface->add = tp_contact_list_add;
1009 iface->remove = tp_contact_list_remove;
1010 iface->get_members = tp_contact_list_get_members;
1011 iface->get_pendings = tp_contact_list_get_pendings;
1012 iface->get_all_groups = tp_contact_list_get_all_groups;
1013 iface->get_groups = tp_contact_list_get_groups;
1014 iface->add_to_group = tp_contact_list_add_to_group;
1015 iface->remove_from_group = tp_contact_list_remove_from_group;
1016 iface->rename_group = tp_contact_list_rename_group;
1017 iface->remove_group = tp_contact_list_remove_group;