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-2008 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 <telepathy-glib/channel.h>
29 #include <telepathy-glib/connection.h>
30 #include <telepathy-glib/util.h>
31 #include <telepathy-glib/dbus.h>
33 #include "empathy-tp-contact-list.h"
34 #include "empathy-contact-list.h"
35 #include "empathy-tp-group.h"
36 #include "empathy-utils.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_TP | EMPATHY_DEBUG_CONTACT
39 #include "empathy-debug.h"
41 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
42 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv))
44 struct _EmpathyTpContactListPriv {
46 TpConnection *connection;
47 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);
79 static guint signals[LAST_SIGNAL];
81 G_DEFINE_TYPE_WITH_CODE (EmpathyTpContactList, empathy_tp_contact_list, G_TYPE_OBJECT,
82 G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
83 tp_contact_list_iface_init));
86 tp_contact_list_group_destroy_cb (EmpathyTpGroup *group,
87 EmpathyTpContactList *list)
89 EmpathyTpContactListPriv *priv = GET_PRIV (list);
91 DEBUG ("Group destroyed: %s", empathy_tp_group_get_name (group));
93 priv->groups = g_list_remove (priv->groups, group);
94 g_object_unref (group);
98 tp_contact_list_group_member_added_cb (EmpathyTpGroup *group,
99 EmpathyContact *contact,
100 EmpathyContact *actor,
102 const gchar *message,
103 EmpathyTpContactList *list)
105 EmpathyTpContactListPriv *priv = GET_PRIV (list);
106 const gchar *group_name;
109 if (!g_list_find (priv->members, contact)) {
113 groups = g_hash_table_lookup (priv->contacts_groups, contact);
115 groups = g_slice_new0 (GList*);
116 g_hash_table_insert (priv->contacts_groups,
117 g_object_ref (contact),
121 group_name = empathy_tp_group_get_name (group);
122 if (!g_list_find_custom (*groups, group_name, (GCompareFunc) strcmp)) {
123 DEBUG ("Contact %s (%d) added to group %s",
124 empathy_contact_get_id (contact),
125 empathy_contact_get_handle (contact),
127 *groups = g_list_prepend (*groups, g_strdup (group_name));
128 g_signal_emit_by_name (list, "groups-changed", contact,
135 tp_contact_list_group_member_removed_cb (EmpathyTpGroup *group,
136 EmpathyContact *contact,
137 EmpathyContact *actor,
139 const gchar *message,
140 EmpathyTpContactList *list)
142 EmpathyTpContactListPriv *priv = GET_PRIV (list);
143 const gchar *group_name;
146 if (!g_list_find (priv->members, contact)) {
150 groups = g_hash_table_lookup (priv->contacts_groups, contact);
155 group_name = empathy_tp_group_get_name (group);
156 if ((l = g_list_find_custom (*groups, group_name, (GCompareFunc) strcmp))) {
157 DEBUG ("Contact %s (%d) removed from group %s",
158 empathy_contact_get_id (contact),
159 empathy_contact_get_handle (contact),
161 *groups = g_list_delete_link (*groups, l);
162 g_signal_emit_by_name (list, "groups-changed", contact,
168 static EmpathyTpGroup *
169 tp_contact_list_find_group (EmpathyTpContactList *list,
172 EmpathyTpContactListPriv *priv = GET_PRIV (list);
175 for (l = priv->groups; l; l = l->next) {
176 if (!tp_strdiff (group, empathy_tp_group_get_name (l->data))) {
183 static TpContactListType
184 tp_contact_list_get_type (EmpathyTpContactList *list,
185 EmpathyTpGroup *group)
189 name = empathy_tp_group_get_name (group);
190 if (!tp_strdiff (name, "subscribe")) {
191 return TP_CONTACT_LIST_TYPE_SUBSCRIBE;
192 } else if (!tp_strdiff (name, "publish")) {
193 return TP_CONTACT_LIST_TYPE_PUBLISH;
196 return TP_CONTACT_LIST_TYPE_UNKNOWN;
200 tp_contact_list_add_member (EmpathyTpContactList *list,
201 EmpathyContact *contact,
202 EmpathyContact *actor,
204 const gchar *message)
206 EmpathyTpContactListPriv *priv = GET_PRIV (list);
209 /* Add to the list and emit signal */
210 priv->members = g_list_prepend (priv->members, g_object_ref (contact));
211 g_signal_emit_by_name (list, "members-changed",
212 contact, actor, reason, message,
215 /* This contact is now member, implicitly accept pending. */
216 if (g_list_find (priv->pendings, contact)) {
217 empathy_tp_group_add_member (priv->publish, contact, "");
220 /* Update groups of the contact */
221 for (l = priv->groups; l; l = l->next) {
222 if (empathy_tp_group_is_member (l->data, contact)) {
223 tp_contact_list_group_member_added_cb (l->data, contact,
231 tp_contact_list_added_cb (EmpathyTpGroup *group,
232 EmpathyContact *contact,
233 EmpathyContact *actor,
235 const gchar *message,
236 EmpathyTpContactList *list)
238 EmpathyTpContactListPriv *priv = GET_PRIV (list);
239 TpContactListType list_type;
241 list_type = tp_contact_list_get_type (list, group);
242 DEBUG ("Contact %s (%d) added to list type %d",
243 empathy_contact_get_id (contact),
244 empathy_contact_get_handle (contact),
247 /* We now get the presence of that contact, add it to members */
248 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
249 !g_list_find (priv->members, contact)) {
250 tp_contact_list_add_member (list, contact, actor, reason, message);
253 /* We now send our presence to that contact, remove it from pendings */
254 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
255 g_list_find (priv->pendings, contact)) {
256 g_signal_emit_by_name (list, "pendings-changed",
257 contact, actor, reason, message,
259 priv->pendings = g_list_remove (priv->pendings, contact);
260 g_object_unref (contact);
265 tp_contact_list_removed_cb (EmpathyTpGroup *group,
266 EmpathyContact *contact,
267 EmpathyContact *actor,
269 const gchar *message,
270 EmpathyTpContactList *list)
272 EmpathyTpContactListPriv *priv = GET_PRIV (list);
273 TpContactListType list_type;
275 list_type = tp_contact_list_get_type (list, group);
276 DEBUG ("Contact %s (%d) removed from list type %d",
277 empathy_contact_get_id (contact),
278 empathy_contact_get_handle (contact),
281 /* This contact refuses to send us his presence, remove from members. */
282 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
283 g_list_find (priv->members, contact)) {
284 g_signal_emit_by_name (list, "members-changed",
285 contact, actor, reason, message,
287 priv->members = g_list_remove (priv->members, contact);
288 g_object_unref (contact);
291 /* We refuse to send our presence to that contact, remove from pendings */
292 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
293 g_list_find (priv->pendings, contact)) {
294 g_signal_emit_by_name (list, "pendings-changed",
295 contact, actor, reason, message,
297 priv->pendings = g_list_remove (priv->pendings, contact);
298 g_object_unref (contact);
303 tp_contact_list_pending_cb (EmpathyTpGroup *group,
304 EmpathyContact *contact,
305 EmpathyContact *actor,
307 const gchar *message,
308 EmpathyTpContactList *list)
310 EmpathyTpContactListPriv *priv = GET_PRIV (list);
311 TpContactListType list_type;
313 list_type = tp_contact_list_get_type (list, group);
314 DEBUG ("Contact %s (%d) pending in list type %d",
315 empathy_contact_get_id (contact),
316 empathy_contact_get_handle (contact),
319 /* We want this contact in our contact list but we don't get its
320 * presence yet. Add to members anyway. */
321 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
322 !g_list_find (priv->members, contact)) {
323 tp_contact_list_add_member (list, contact, actor, reason, message);
326 /* This contact wants our presence, auto accept if he is member,
327 * otherwise he is pending. */
328 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
329 !g_list_find (priv->pendings, contact)) {
330 if (g_list_find (priv->members, contact)) {
331 empathy_tp_group_add_member (priv->publish, contact, "");
333 priv->pendings = g_list_prepend (priv->pendings,
334 g_object_ref (contact));
335 g_signal_emit_by_name (list, "pendings-changed",
336 contact, actor, reason, message,
343 tp_contact_list_invalidated_cb (TpConnection *connection,
347 EmpathyTpContactList *list)
349 EmpathyTpContactListPriv *priv = GET_PRIV (list);
352 DEBUG ("Connection invalidated");
354 /* Remove all contacts */
355 for (l = priv->members; l; l = l->next) {
356 g_signal_emit_by_name (list, "members-changed", l->data,
359 g_object_unref (l->data);
361 for (l = priv->pendings; l; l = l->next) {
362 g_signal_emit_by_name (list, "pendings-changed", l->data,
365 g_object_unref (l->data);
367 g_list_free (priv->members);
368 g_list_free (priv->pendings);
369 priv->members = NULL;
370 priv->pendings = NULL;
372 /* Tell the world to not use us anymore */
373 g_signal_emit (list, signals[DESTROY], 0);
377 tp_contact_list_group_list_free (GList **groups)
379 g_list_foreach (*groups, (GFunc) g_free, NULL);
380 g_list_free (*groups);
381 g_slice_free (GList*, groups);
385 tp_contact_list_add_channel (EmpathyTpContactList *list,
386 const gchar *object_path,
387 const gchar *channel_type,
388 TpHandleType handle_type,
391 EmpathyTpContactListPriv *priv = GET_PRIV (list);
392 EmpathyTpGroup *group;
395 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
396 (handle_type != TP_HANDLE_TYPE_LIST &&
397 handle_type != TP_HANDLE_TYPE_GROUP)) {
401 channel = tp_channel_new (priv->connection,
402 object_path, channel_type,
403 handle_type, handle, NULL);
405 group = empathy_tp_group_new (channel);
406 empathy_run_until_ready (group);
407 g_object_unref (channel);
409 if (handle_type == TP_HANDLE_TYPE_LIST) {
410 TpContactListType list_type;
413 list_type = tp_contact_list_get_type (list, group);
414 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH && !priv->publish) {
415 priv->publish = g_object_ref (group);
417 /* Publish is the list of contacts to who we send our
418 * presence. Makes no sense to be in remote-pending */
419 g_signal_connect (group, "local-pending",
420 G_CALLBACK (tp_contact_list_pending_cb),
423 contacts = empathy_tp_group_get_local_pendings (group);
424 for (l = contacts; l; l = l->next) {
425 EmpathyPendingInfo *info = l->data;
427 tp_contact_list_pending_cb (group,
433 empathy_pending_info_free (info);
435 g_list_free (contacts);
437 else if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE && !priv->subscribe) {
438 priv->subscribe = g_object_ref (group);
440 /* Subscribe is the list of contacts from who we
441 * receive presence. Makes no sense to be in
443 g_signal_connect (group, "remote-pending",
444 G_CALLBACK (tp_contact_list_pending_cb),
447 contacts = empathy_tp_group_get_remote_pendings (group);
448 for (l = contacts; l; l = l->next) {
449 tp_contact_list_pending_cb (group,
453 g_object_unref (l->data);
455 g_list_free (contacts);
457 DEBUG ("Type of contact list channel unknown or aleady "
458 "have that list: %s",
459 empathy_tp_group_get_name (group));
462 DEBUG ("New contact list channel of type: %d", list_type);
464 g_signal_connect (group, "member-added",
465 G_CALLBACK (tp_contact_list_added_cb),
467 g_signal_connect (group, "member-removed",
468 G_CALLBACK (tp_contact_list_removed_cb),
471 contacts = empathy_tp_group_get_members (group);
472 for (l = contacts; l; l = l->next) {
473 tp_contact_list_added_cb (group,
477 g_object_unref (l->data);
479 g_list_free (contacts);
481 else if (handle_type == TP_HANDLE_TYPE_GROUP) {
482 const gchar *group_name;
485 /* Check if already exists */
486 group_name = empathy_tp_group_get_name (group);
487 if (tp_contact_list_find_group (list, group_name)) {
491 DEBUG ("New server-side group channel: %s", group_name);
493 priv->groups = g_list_prepend (priv->groups, g_object_ref (group));
495 g_signal_connect (group, "member-added",
496 G_CALLBACK (tp_contact_list_group_member_added_cb),
498 g_signal_connect (group, "member-removed",
499 G_CALLBACK (tp_contact_list_group_member_removed_cb),
501 g_signal_connect (group, "destroy",
502 G_CALLBACK (tp_contact_list_group_destroy_cb),
505 contacts = empathy_tp_group_get_members (group);
506 for (l = contacts; l; l = l->next) {
507 tp_contact_list_group_member_added_cb (group, l->data,
510 g_object_unref (l->data);
512 g_list_free (contacts);
514 DEBUG ("Unknown handle type (%d) for contact list channel",
519 g_object_unref (group);
523 tp_contact_list_new_channel_cb (TpConnection *proxy,
524 const gchar *object_path,
525 const gchar *channel_type,
528 gboolean suppress_handler,
532 EmpathyTpContactListPriv *priv = GET_PRIV (list);
534 if (!suppress_handler && priv->ready) {
535 tp_contact_list_add_channel (EMPATHY_TP_CONTACT_LIST (list),
536 object_path, channel_type,
537 handle_type, handle);
542 tp_contact_list_list_channels_cb (TpConnection *connection,
543 const GPtrArray *channels,
548 EmpathyTpContactListPriv *priv = GET_PRIV (list);
552 DEBUG ("Failed to get list of open channels: %s",
553 error ? error->message : "No error given");
557 for (i = 0; i < channels->len; i++) {
558 GValueArray *chan_struct;
559 const gchar *object_path;
560 const gchar *channel_type;
561 TpHandleType handle_type;
564 chan_struct = g_ptr_array_index (channels, i);
565 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
566 channel_type = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
567 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
568 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
570 tp_contact_list_add_channel (EMPATHY_TP_CONTACT_LIST (list),
571 object_path, channel_type,
572 handle_type, handle);
579 tp_contact_list_finalize (GObject *object)
581 EmpathyTpContactListPriv *priv;
582 EmpathyTpContactList *list;
584 list = EMPATHY_TP_CONTACT_LIST (object);
585 priv = GET_PRIV (list);
587 DEBUG ("finalize: %p", object);
589 if (priv->subscribe) {
590 g_object_unref (priv->subscribe);
593 g_object_unref (priv->publish);
596 g_object_unref (priv->account);
598 if (priv->connection) {
599 g_signal_handlers_disconnect_by_func (priv->connection,
600 tp_contact_list_invalidated_cb,
602 g_object_unref (priv->connection);
605 g_hash_table_destroy (priv->contacts_groups);
606 g_list_foreach (priv->groups, (GFunc) g_object_unref, NULL);
607 g_list_free (priv->groups);
608 g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
609 g_list_free (priv->members);
610 g_list_foreach (priv->pendings, (GFunc) g_object_unref, NULL);
611 g_list_free (priv->pendings);
613 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
617 tp_contact_list_ready_cb (EmpathyTpContactList *list)
619 EmpathyTpContactListPriv *priv = GET_PRIV (list);
621 tp_cli_connection_call_list_channels (priv->connection, -1,
622 tp_contact_list_list_channels_cb,
626 tp_cli_connection_connect_to_new_channel (priv->connection,
627 tp_contact_list_new_channel_cb,
629 G_OBJECT (list), NULL);
633 tp_contact_list_constructed (GObject *list)
636 EmpathyTpContactListPriv *priv = GET_PRIV (list);
641 const gchar *protocol_name;
643 /* Get the connection. status==0 means CONNECTED */
644 mc = empathy_mission_control_new ();
645 status = mission_control_get_connection_status (mc, priv->account, NULL);
646 g_return_if_fail (status == 0);
647 priv->connection = mission_control_get_tpconnection (mc, priv->account, NULL);
648 g_return_if_fail (priv->connection != NULL);
651 g_signal_connect (priv->connection, "invalidated",
652 G_CALLBACK (tp_contact_list_invalidated_cb),
654 g_object_get (priv->connection, "connection-ready", &ready, NULL);
656 tp_contact_list_ready_cb (EMPATHY_TP_CONTACT_LIST (list));
658 g_signal_connect_swapped (priv->connection, "notify::connection-ready",
659 G_CALLBACK (tp_contact_list_ready_cb),
663 /* Check for protocols that does not support contact groups. We can
664 * put all contacts into a special group in that case.
665 * FIXME: Default group should be an information in the profile */
666 profile = mc_account_get_profile (priv->account);
667 protocol_name = mc_profile_get_protocol_name (profile);
668 if (strcmp (protocol_name, "local-xmpp") == 0) {
669 priv->protocol_group = _("People nearby");
671 g_object_unref (profile);
675 tp_contact_list_get_property (GObject *object,
680 EmpathyTpContactListPriv *priv = GET_PRIV (object);
684 g_value_set_object (value, priv->account);
687 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
693 tp_contact_list_set_property (GObject *object,
698 EmpathyTpContactListPriv *priv = GET_PRIV (object);
702 priv->account = g_object_ref (g_value_get_object (value));
705 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
711 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
713 GObjectClass *object_class = G_OBJECT_CLASS (klass);
715 object_class->finalize = tp_contact_list_finalize;
716 object_class->constructed = tp_contact_list_constructed;
717 object_class->get_property = tp_contact_list_get_property;
718 object_class->set_property = tp_contact_list_set_property;
720 g_object_class_install_property (object_class,
722 g_param_spec_object ("account",
724 "The account associated with the contact list",
727 G_PARAM_CONSTRUCT_ONLY));
730 g_signal_new ("destroy",
731 G_TYPE_FROM_CLASS (klass),
735 g_cclosure_marshal_VOID__VOID,
739 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
743 empathy_tp_contact_list_init (EmpathyTpContactList *list)
745 EmpathyTpContactListPriv *priv = GET_PRIV (list);
747 priv->contacts_groups = g_hash_table_new_full (g_direct_hash,
749 (GDestroyNotify) g_object_unref,
750 (GDestroyNotify) tp_contact_list_group_list_free);
753 EmpathyTpContactList *
754 empathy_tp_contact_list_new (McAccount *account)
756 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
762 empathy_tp_contact_list_get_account (EmpathyTpContactList *list)
764 EmpathyTpContactListPriv *priv;
766 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
768 priv = GET_PRIV (list);
770 return priv->account;
774 tp_contact_list_add (EmpathyContactList *list,
775 EmpathyContact *contact,
776 const gchar *message)
778 EmpathyTpContactListPriv *priv = GET_PRIV (list);
780 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
782 empathy_tp_group_add_member (priv->subscribe, contact, message);
783 if (g_list_find (priv->pendings, contact)) {
784 empathy_tp_group_add_member (priv->publish, contact, message);
789 tp_contact_list_remove (EmpathyContactList *list,
790 EmpathyContact *contact,
791 const gchar *message)
793 EmpathyTpContactListPriv *priv = GET_PRIV (list);
795 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
797 empathy_tp_group_remove_member (priv->subscribe, contact, message);
798 empathy_tp_group_remove_member (priv->publish, contact, message);
802 tp_contact_list_get_members (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->members, (GFunc) g_object_ref, NULL);
809 return g_list_copy (priv->members);
813 tp_contact_list_get_pendings (EmpathyContactList *list)
815 EmpathyTpContactListPriv *priv = GET_PRIV (list);
817 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
819 g_list_foreach (priv->pendings, (GFunc) g_object_ref, NULL);
820 return g_list_copy (priv->pendings);
824 tp_contact_list_get_all_groups (EmpathyContactList *list)
826 EmpathyTpContactListPriv *priv = GET_PRIV (list);
827 GList *groups = NULL, *l;
829 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
831 if (priv->protocol_group) {
832 groups = g_list_prepend (groups, g_strdup (priv->protocol_group));
835 for (l = priv->groups; l; l = l->next) {
838 name = empathy_tp_group_get_name (l->data);
839 groups = g_list_prepend (groups, g_strdup (name));
846 tp_contact_list_get_groups (EmpathyContactList *list,
847 EmpathyContact *contact)
849 EmpathyTpContactListPriv *priv = GET_PRIV (list);
851 GList *ret = NULL, *l;
853 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
855 if (priv->protocol_group) {
856 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
859 groups = g_hash_table_lookup (priv->contacts_groups, contact);
864 for (l = *groups; l; l = l->next) {
865 ret = g_list_prepend (ret, g_strdup (l->data));
872 static EmpathyTpGroup *
873 tp_contact_list_get_group (EmpathyTpContactList *list,
876 EmpathyTpContactListPriv *priv = GET_PRIV (list);
877 EmpathyTpGroup *tp_group;
881 const char *names[2] = {group, NULL};
882 GError *error = NULL;
884 tp_group = tp_contact_list_find_group (list, group);
889 DEBUG ("creating new group: %s", group);
891 if (!tp_cli_connection_run_request_handles (priv->connection, -1,
892 TP_HANDLE_TYPE_GROUP,
896 DEBUG ("Failed to RequestHandles: %s",
897 error ? error->message : "No error given");
898 g_clear_error (&error);
901 handle = g_array_index (handles, guint, 0);
902 g_array_free (handles, TRUE);
904 if (!tp_cli_connection_run_request_channel (priv->connection, -1,
905 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
906 TP_HANDLE_TYPE_GROUP,
911 DEBUG ("Failed to RequestChannel: %s",
912 error ? error->message : "No error given");
913 g_clear_error (&error);
917 tp_contact_list_add_channel (EMPATHY_TP_CONTACT_LIST (list),
919 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
920 TP_HANDLE_TYPE_GROUP, handle);
922 g_free (object_path);
924 return tp_contact_list_find_group (list, group);
928 tp_contact_list_add_to_group (EmpathyContactList *list,
929 EmpathyContact *contact,
932 EmpathyTpGroup *tp_group;
934 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
936 tp_group = tp_contact_list_get_group (EMPATHY_TP_CONTACT_LIST (list),
939 empathy_tp_group_add_member (tp_group, contact, "");
943 tp_contact_list_remove_from_group (EmpathyContactList *list,
944 EmpathyContact *contact,
947 EmpathyTpGroup *tp_group;
949 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
951 tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
955 empathy_tp_group_remove_member (tp_group, contact, "");
960 tp_contact_list_rename_group (EmpathyContactList *list,
961 const gchar *old_group,
962 const gchar *new_group)
964 EmpathyTpGroup *tp_group;
967 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
969 tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
975 DEBUG ("rename group %s to %s", old_group, new_group);
977 /* Remove all members from the old group */
978 members = empathy_tp_group_get_members (tp_group);
979 empathy_tp_group_remove_members (tp_group, members, "");
980 empathy_tp_group_close (tp_group);
982 /* Add all members to the new group */
983 tp_group = tp_contact_list_get_group (EMPATHY_TP_CONTACT_LIST (list),
985 empathy_tp_group_add_members (tp_group, members, "");
987 g_list_foreach (members, (GFunc) g_object_unref, NULL);
988 g_list_free (members);
992 tp_contact_list_remove_group (EmpathyContactList *list,
995 EmpathyTpGroup *tp_group;
998 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1000 tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
1007 DEBUG ("remove group %s", group);
1009 /* Remove all members of the group */
1010 members = empathy_tp_group_get_members (tp_group);
1011 empathy_tp_group_remove_members (tp_group, members, "");
1012 empathy_tp_group_close (tp_group);
1014 g_list_foreach (members, (GFunc) g_object_unref, NULL);
1015 g_list_free (members);
1019 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1021 iface->add = tp_contact_list_add;
1022 iface->remove = tp_contact_list_remove;
1023 iface->get_members = tp_contact_list_get_members;
1024 iface->get_pendings = tp_contact_list_get_pendings;
1025 iface->get_all_groups = tp_contact_list_get_all_groups;
1026 iface->get_groups = tp_contact_list_get_groups;
1027 iface->add_to_group = tp_contact_list_add_to_group;
1028 iface->remove_from_group = tp_contact_list_remove_from_group;
1029 iface->rename_group = tp_contact_list_rename_group;
1030 iface->remove_group = tp_contact_list_remove_group;