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) EMPATHY_GET_PRIV (obj, EmpathyTpContactList)
44 TpConnection *connection;
45 const gchar *protocol_group;
48 EmpathyTpGroup *publish;
49 EmpathyTpGroup *subscribe;
54 GHashTable *contacts_groups;
55 } EmpathyTpContactListPriv;
58 TP_CONTACT_LIST_TYPE_PUBLISH,
59 TP_CONTACT_LIST_TYPE_SUBSCRIBE,
60 TP_CONTACT_LIST_TYPE_UNKNOWN
63 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 DEBUG ("Group destroyed: %s", 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 DEBUG ("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 DEBUG ("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 (!tp_strdiff (group, empathy_tp_group_get_name (l->data))) {
179 static TpContactListType
180 tp_contact_list_get_type (EmpathyTpContactList *list,
181 EmpathyTpGroup *group)
185 name = empathy_tp_group_get_name (group);
186 if (!tp_strdiff (name, "subscribe")) {
187 return TP_CONTACT_LIST_TYPE_SUBSCRIBE;
188 } else if (!tp_strdiff (name, "publish")) {
189 return TP_CONTACT_LIST_TYPE_PUBLISH;
192 return TP_CONTACT_LIST_TYPE_UNKNOWN;
196 tp_contact_list_add_member (EmpathyTpContactList *list,
197 EmpathyContact *contact,
198 EmpathyContact *actor,
200 const gchar *message)
202 EmpathyTpContactListPriv *priv = GET_PRIV (list);
205 /* Add to the list and emit signal */
206 priv->members = g_list_prepend (priv->members, g_object_ref (contact));
207 g_signal_emit_by_name (list, "members-changed",
208 contact, actor, reason, message,
211 /* This contact is now member, implicitly accept pending. */
212 if (g_list_find (priv->pendings, contact)) {
213 empathy_tp_group_add_member (priv->publish, contact, "");
216 /* Update groups of the contact */
217 for (l = priv->groups; l; l = l->next) {
218 if (empathy_tp_group_is_member (l->data, contact)) {
219 tp_contact_list_group_member_added_cb (l->data, contact,
227 tp_contact_list_added_cb (EmpathyTpGroup *group,
228 EmpathyContact *contact,
229 EmpathyContact *actor,
231 const gchar *message,
232 EmpathyTpContactList *list)
234 EmpathyTpContactListPriv *priv = GET_PRIV (list);
235 TpContactListType list_type;
237 list_type = tp_contact_list_get_type (list, group);
238 DEBUG ("Contact %s (%d) added to list type %d",
239 empathy_contact_get_id (contact),
240 empathy_contact_get_handle (contact),
243 /* We now get the presence of that contact, add it to members */
244 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
245 !g_list_find (priv->members, contact)) {
246 tp_contact_list_add_member (list, contact, actor, reason, message);
249 /* We now send our presence to that contact, remove it from pendings */
250 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
251 g_list_find (priv->pendings, contact)) {
252 g_signal_emit_by_name (list, "pendings-changed",
253 contact, actor, reason, message,
255 priv->pendings = g_list_remove (priv->pendings, contact);
256 g_object_unref (contact);
261 tp_contact_list_removed_cb (EmpathyTpGroup *group,
262 EmpathyContact *contact,
263 EmpathyContact *actor,
265 const gchar *message,
266 EmpathyTpContactList *list)
268 EmpathyTpContactListPriv *priv = GET_PRIV (list);
269 TpContactListType list_type;
271 list_type = tp_contact_list_get_type (list, group);
272 DEBUG ("Contact %s (%d) removed from list type %d",
273 empathy_contact_get_id (contact),
274 empathy_contact_get_handle (contact),
277 /* This contact refuses to send us his presence, remove from members. */
278 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
279 g_list_find (priv->members, contact)) {
280 g_signal_emit_by_name (list, "members-changed",
281 contact, actor, reason, message,
283 priv->members = g_list_remove (priv->members, contact);
284 g_object_unref (contact);
287 /* We refuse to send our presence to that contact, remove from pendings */
288 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
289 g_list_find (priv->pendings, contact)) {
290 g_signal_emit_by_name (list, "pendings-changed",
291 contact, actor, reason, message,
293 priv->pendings = g_list_remove (priv->pendings, contact);
294 g_object_unref (contact);
299 tp_contact_list_pending_cb (EmpathyTpGroup *group,
300 EmpathyContact *contact,
301 EmpathyContact *actor,
303 const gchar *message,
304 EmpathyTpContactList *list)
306 EmpathyTpContactListPriv *priv = GET_PRIV (list);
307 TpContactListType list_type;
309 list_type = tp_contact_list_get_type (list, group);
310 DEBUG ("Contact %s (%d) pending in list type %d",
311 empathy_contact_get_id (contact),
312 empathy_contact_get_handle (contact),
315 /* We want this contact in our contact list but we don't get its
316 * presence yet. Add to members anyway. */
317 if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE &&
318 !g_list_find (priv->members, contact)) {
319 tp_contact_list_add_member (list, contact, actor, reason, message);
322 /* This contact wants our presence, auto accept if he is member,
323 * otherwise he is pending. */
324 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH &&
325 !g_list_find (priv->pendings, contact)) {
326 if (g_list_find (priv->members, contact)) {
327 empathy_tp_group_add_member (priv->publish, contact, "");
329 priv->pendings = g_list_prepend (priv->pendings,
330 g_object_ref (contact));
331 g_signal_emit_by_name (list, "pendings-changed",
332 contact, actor, reason, message,
339 tp_contact_list_invalidated_cb (TpConnection *connection,
343 EmpathyTpContactList *list)
345 EmpathyTpContactListPriv *priv = GET_PRIV (list);
348 DEBUG ("Connection invalidated");
350 /* Remove all contacts */
351 for (l = priv->members; l; l = l->next) {
352 g_signal_emit_by_name (list, "members-changed", l->data,
355 g_object_unref (l->data);
357 for (l = priv->pendings; l; l = l->next) {
358 g_signal_emit_by_name (list, "pendings-changed", l->data,
361 g_object_unref (l->data);
363 g_list_free (priv->members);
364 g_list_free (priv->pendings);
365 priv->members = NULL;
366 priv->pendings = NULL;
368 /* Tell the world to not use us anymore */
369 g_signal_emit (list, signals[DESTROY], 0);
373 tp_contact_list_group_list_free (GList **groups)
375 g_list_foreach (*groups, (GFunc) g_free, NULL);
376 g_list_free (*groups);
377 g_slice_free (GList*, groups);
381 tp_contact_list_add_channel (EmpathyTpContactList *list,
382 const gchar *object_path,
383 const gchar *channel_type,
384 TpHandleType handle_type,
387 EmpathyTpContactListPriv *priv = GET_PRIV (list);
389 EmpathyTpGroup *group;
390 const gchar *group_name;
393 if (strcmp (channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST) != 0 ||
394 handle_type != TP_HANDLE_TYPE_GROUP) {
398 channel = tp_channel_new (priv->connection,
399 object_path, channel_type,
400 handle_type, handle, NULL);
402 group = empathy_tp_group_new (channel);
403 empathy_run_until_ready (group);
404 g_object_unref (channel);
406 /* Check if already exists */
407 group_name = empathy_tp_group_get_name (group);
408 if (tp_contact_list_find_group (list, group_name)) {
409 g_object_unref (group);
414 DEBUG ("New server-side group: %s", group_name);
415 priv->groups = g_list_prepend (priv->groups, group);
416 g_signal_connect (group, "member-added",
417 G_CALLBACK (tp_contact_list_group_member_added_cb),
419 g_signal_connect (group, "member-removed",
420 G_CALLBACK (tp_contact_list_group_member_removed_cb),
422 g_signal_connect (group, "destroy",
423 G_CALLBACK (tp_contact_list_group_destroy_cb),
426 /* Get initial members */
427 contacts = empathy_tp_group_get_members (group);
428 for (l = contacts; l; l = l->next) {
429 tp_contact_list_group_member_added_cb (group, l->data,
432 g_object_unref (l->data);
434 g_list_free (contacts);
438 tp_contact_list_new_channel_cb (TpConnection *proxy,
439 const gchar *object_path,
440 const gchar *channel_type,
443 gboolean suppress_handler,
447 EmpathyTpContactListPriv *priv = GET_PRIV (list);
449 if (!suppress_handler && priv->ready) {
450 tp_contact_list_add_channel (EMPATHY_TP_CONTACT_LIST (list),
451 object_path, channel_type,
452 handle_type, handle);
457 tp_contact_list_list_channels_cb (TpConnection *connection,
458 const GPtrArray *channels,
463 EmpathyTpContactListPriv *priv = GET_PRIV (list);
467 DEBUG ("Error: %s", error->message);
471 for (i = 0; i < channels->len; i++) {
472 GValueArray *chan_struct;
473 const gchar *object_path;
474 const gchar *channel_type;
475 TpHandleType handle_type;
478 chan_struct = g_ptr_array_index (channels, i);
479 object_path = g_value_get_boxed (g_value_array_get_nth (chan_struct, 0));
480 channel_type = g_value_get_string (g_value_array_get_nth (chan_struct, 1));
481 handle_type = g_value_get_uint (g_value_array_get_nth (chan_struct, 2));
482 handle = g_value_get_uint (g_value_array_get_nth (chan_struct, 3));
484 tp_contact_list_add_channel (EMPATHY_TP_CONTACT_LIST (list),
485 object_path, channel_type,
486 handle_type, handle);
493 tp_contact_list_request_channel_cb (TpConnection *connection,
494 const gchar *object_path,
497 GObject *weak_object)
499 EmpathyTpContactList *list = EMPATHY_TP_CONTACT_LIST (weak_object);
500 EmpathyTpContactListPriv *priv = GET_PRIV (list);
501 EmpathyTpGroup *group;
503 TpContactListType list_type;
507 DEBUG ("Error: %s", error->message);
511 channel = tp_channel_new (connection, object_path,
512 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
514 GPOINTER_TO_UINT (user_data),
516 group = empathy_tp_group_new (channel);
517 empathy_run_until_ready (group);
519 list_type = tp_contact_list_get_type (list, group);
520 if (list_type == TP_CONTACT_LIST_TYPE_PUBLISH && !priv->publish) {
521 DEBUG ("Got publish list");
522 priv->publish = group;
524 /* Publish is the list of contacts to who we send our
525 * presence. Makes no sense to be in remote-pending */
526 g_signal_connect (group, "local-pending",
527 G_CALLBACK (tp_contact_list_pending_cb),
530 contacts = empathy_tp_group_get_local_pendings (group);
531 for (l = contacts; l; l = l->next) {
532 EmpathyPendingInfo *info = l->data;
533 tp_contact_list_pending_cb (group,
539 empathy_pending_info_free (info);
541 g_list_free (contacts);
543 else if (list_type == TP_CONTACT_LIST_TYPE_SUBSCRIBE && !priv->subscribe) {
544 DEBUG ("Got subscribe list");
545 priv->subscribe = group;
547 /* Subscribe is the list of contacts from who we
548 * receive presence. Makes no sense to be in
550 g_signal_connect (group, "remote-pending",
551 G_CALLBACK (tp_contact_list_pending_cb),
554 contacts = empathy_tp_group_get_remote_pendings (group);
555 for (l = contacts; l; l = l->next) {
556 tp_contact_list_pending_cb (group,
560 g_object_unref (l->data);
562 g_list_free (contacts);
564 DEBUG ("Type of contact list channel unknown or aleady "
565 "have that list: %s",
566 empathy_tp_group_get_name (group));
567 g_object_unref (group);
571 /* For all list types when need to get members */
572 g_signal_connect (group, "member-added",
573 G_CALLBACK (tp_contact_list_added_cb),
575 g_signal_connect (group, "member-removed",
576 G_CALLBACK (tp_contact_list_removed_cb),
579 contacts = empathy_tp_group_get_members (group);
580 for (l = contacts; l; l = l->next) {
581 tp_contact_list_added_cb (group,
585 g_object_unref (l->data);
587 g_list_free (contacts);
591 tp_contact_list_request_handle_cb (TpConnection *connection,
592 const GArray *handles,
600 DEBUG ("Error: %s", error->message);
604 handle = g_array_index (handles, guint, 0);
605 tp_cli_connection_call_request_channel (connection, -1,
606 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
610 tp_contact_list_request_channel_cb,
611 GUINT_TO_POINTER (handle), NULL,
616 tp_contact_list_request_list (EmpathyTpContactList *list,
619 EmpathyTpContactListPriv *priv = GET_PRIV (list);
620 const gchar *names[] = {type, NULL};
622 tp_cli_connection_call_request_handles (priv->connection,
626 tp_contact_list_request_handle_cb,
632 tp_contact_list_finalize (GObject *object)
634 EmpathyTpContactListPriv *priv;
635 EmpathyTpContactList *list;
637 list = EMPATHY_TP_CONTACT_LIST (object);
638 priv = GET_PRIV (list);
640 DEBUG ("finalize: %p", object);
642 if (priv->subscribe) {
643 g_object_unref (priv->subscribe);
646 g_object_unref (priv->publish);
649 g_object_unref (priv->account);
651 if (priv->connection) {
652 g_signal_handlers_disconnect_by_func (priv->connection,
653 tp_contact_list_invalidated_cb,
655 g_object_unref (priv->connection);
658 g_hash_table_destroy (priv->contacts_groups);
659 g_list_foreach (priv->groups, (GFunc) g_object_unref, NULL);
660 g_list_free (priv->groups);
661 g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
662 g_list_free (priv->members);
663 g_list_foreach (priv->pendings, (GFunc) g_object_unref, NULL);
664 g_list_free (priv->pendings);
666 G_OBJECT_CLASS (empathy_tp_contact_list_parent_class)->finalize (object);
670 tp_contact_list_connection_ready (TpConnection *connection,
674 EmpathyTpContactListPriv *priv = GET_PRIV (list);
677 tp_contact_list_invalidated_cb (connection,
681 EMPATHY_TP_CONTACT_LIST (list));
685 g_signal_connect (priv->connection, "invalidated",
686 G_CALLBACK (tp_contact_list_invalidated_cb),
689 tp_contact_list_request_list (list, "publish");
690 tp_contact_list_request_list (list, "subscribe");
692 tp_cli_connection_call_list_channels (priv->connection, -1,
693 tp_contact_list_list_channels_cb,
697 tp_cli_connection_connect_to_new_channel (priv->connection,
698 tp_contact_list_new_channel_cb,
704 tp_contact_list_constructed (GObject *list)
707 EmpathyTpContactListPriv *priv = GET_PRIV (list);
711 const gchar *protocol_name;
713 /* Get the connection. status==0 means CONNECTED */
714 mc = empathy_mission_control_new ();
715 status = mission_control_get_connection_status (mc, priv->account, NULL);
716 g_return_if_fail (status == 0);
717 priv->connection = mission_control_get_tpconnection (mc, priv->account, NULL);
718 g_return_if_fail (priv->connection != NULL);
721 tp_connection_call_when_ready (priv->connection,
722 tp_contact_list_connection_ready,
725 /* Check for protocols that does not support contact groups. We can
726 * put all contacts into a special group in that case.
727 * FIXME: Default group should be an information in the profile */
728 profile = mc_account_get_profile (priv->account);
729 protocol_name = mc_profile_get_protocol_name (profile);
730 if (strcmp (protocol_name, "local-xmpp") == 0) {
731 priv->protocol_group = _("People nearby");
733 g_object_unref (profile);
737 tp_contact_list_get_property (GObject *object,
742 EmpathyTpContactListPriv *priv = GET_PRIV (object);
746 g_value_set_object (value, priv->account);
749 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
755 tp_contact_list_set_property (GObject *object,
760 EmpathyTpContactListPriv *priv = GET_PRIV (object);
764 priv->account = g_object_ref (g_value_get_object (value));
767 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
773 empathy_tp_contact_list_class_init (EmpathyTpContactListClass *klass)
775 GObjectClass *object_class = G_OBJECT_CLASS (klass);
777 object_class->finalize = tp_contact_list_finalize;
778 object_class->constructed = tp_contact_list_constructed;
779 object_class->get_property = tp_contact_list_get_property;
780 object_class->set_property = tp_contact_list_set_property;
782 g_object_class_install_property (object_class,
784 g_param_spec_object ("account",
786 "The account associated with the contact list",
789 G_PARAM_CONSTRUCT_ONLY));
792 g_signal_new ("destroy",
793 G_TYPE_FROM_CLASS (klass),
797 g_cclosure_marshal_VOID__VOID,
801 g_type_class_add_private (object_class, sizeof (EmpathyTpContactListPriv));
805 empathy_tp_contact_list_init (EmpathyTpContactList *list)
807 EmpathyTpContactListPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
808 EMPATHY_TYPE_TP_CONTACT_LIST, EmpathyTpContactListPriv);
811 priv->contacts_groups = g_hash_table_new_full (g_direct_hash,
813 (GDestroyNotify) g_object_unref,
814 (GDestroyNotify) tp_contact_list_group_list_free);
817 EmpathyTpContactList *
818 empathy_tp_contact_list_new (McAccount *account)
820 return g_object_new (EMPATHY_TYPE_TP_CONTACT_LIST,
826 empathy_tp_contact_list_get_account (EmpathyTpContactList *list)
828 EmpathyTpContactListPriv *priv;
830 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
832 priv = GET_PRIV (list);
834 return priv->account;
838 tp_contact_list_add (EmpathyContactList *list,
839 EmpathyContact *contact,
840 const gchar *message)
842 EmpathyTpContactListPriv *priv = GET_PRIV (list);
844 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
846 if (priv->subscribe) {
847 empathy_tp_group_add_member (priv->subscribe, contact, message);
850 if (priv->publish && g_list_find (priv->pendings, contact)) {
851 empathy_tp_group_add_member (priv->publish, contact, message);
856 tp_contact_list_remove (EmpathyContactList *list,
857 EmpathyContact *contact,
858 const gchar *message)
860 EmpathyTpContactListPriv *priv = GET_PRIV (list);
862 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
864 if (priv->subscribe) {
865 empathy_tp_group_remove_member (priv->subscribe, contact, message);
868 empathy_tp_group_remove_member (priv->publish, contact, message);
873 tp_contact_list_get_members (EmpathyContactList *list)
875 EmpathyTpContactListPriv *priv = GET_PRIV (list);
877 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
879 g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
880 return g_list_copy (priv->members);
884 tp_contact_list_get_pendings (EmpathyContactList *list)
886 EmpathyTpContactListPriv *priv = GET_PRIV (list);
888 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
890 g_list_foreach (priv->pendings, (GFunc) g_object_ref, NULL);
891 return g_list_copy (priv->pendings);
895 tp_contact_list_get_all_groups (EmpathyContactList *list)
897 EmpathyTpContactListPriv *priv = GET_PRIV (list);
898 GList *groups = NULL, *l;
900 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
902 if (priv->protocol_group) {
903 groups = g_list_prepend (groups, g_strdup (priv->protocol_group));
906 for (l = priv->groups; l; l = l->next) {
909 name = empathy_tp_group_get_name (l->data);
910 groups = g_list_prepend (groups, g_strdup (name));
917 tp_contact_list_get_groups (EmpathyContactList *list,
918 EmpathyContact *contact)
920 EmpathyTpContactListPriv *priv = GET_PRIV (list);
922 GList *ret = NULL, *l;
924 g_return_val_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list), NULL);
926 if (priv->protocol_group) {
927 ret = g_list_prepend (ret, g_strdup (priv->protocol_group));
930 groups = g_hash_table_lookup (priv->contacts_groups, contact);
935 for (l = *groups; l; l = l->next) {
936 ret = g_list_prepend (ret, g_strdup (l->data));
943 static EmpathyTpGroup *
944 tp_contact_list_get_group (EmpathyTpContactList *list,
947 EmpathyTpContactListPriv *priv = GET_PRIV (list);
948 EmpathyTpGroup *tp_group;
952 const char *names[2] = {group, NULL};
953 GError *error = NULL;
955 tp_group = tp_contact_list_find_group (list, group);
960 DEBUG ("creating new group: %s", group);
962 if (!tp_cli_connection_run_request_handles (priv->connection, -1,
963 TP_HANDLE_TYPE_GROUP,
967 DEBUG ("Failed to RequestHandles: %s",
968 error ? error->message : "No error given");
969 g_clear_error (&error);
972 handle = g_array_index (handles, guint, 0);
973 g_array_free (handles, TRUE);
975 if (!tp_cli_connection_run_request_channel (priv->connection, -1,
976 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
977 TP_HANDLE_TYPE_GROUP,
982 DEBUG ("Failed to RequestChannel: %s",
983 error ? error->message : "No error given");
984 g_clear_error (&error);
988 tp_contact_list_add_channel (EMPATHY_TP_CONTACT_LIST (list),
990 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
991 TP_HANDLE_TYPE_GROUP, handle);
993 g_free (object_path);
995 return tp_contact_list_find_group (list, group);
999 tp_contact_list_add_to_group (EmpathyContactList *list,
1000 EmpathyContact *contact,
1003 EmpathyTpGroup *tp_group;
1005 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1007 tp_group = tp_contact_list_get_group (EMPATHY_TP_CONTACT_LIST (list),
1010 empathy_tp_group_add_member (tp_group, contact, "");
1014 tp_contact_list_remove_from_group (EmpathyContactList *list,
1015 EmpathyContact *contact,
1018 EmpathyTpGroup *tp_group;
1020 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1022 tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
1026 empathy_tp_group_remove_member (tp_group, contact, "");
1031 tp_contact_list_rename_group (EmpathyContactList *list,
1032 const gchar *old_group,
1033 const gchar *new_group)
1035 EmpathyTpGroup *tp_group;
1038 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1040 tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
1046 DEBUG ("rename group %s to %s", old_group, new_group);
1048 /* Remove all members from the old group */
1049 members = empathy_tp_group_get_members (tp_group);
1050 empathy_tp_group_remove_members (tp_group, members, "");
1051 empathy_tp_group_close (tp_group);
1053 /* Add all members to the new group */
1054 tp_group = tp_contact_list_get_group (EMPATHY_TP_CONTACT_LIST (list),
1056 empathy_tp_group_add_members (tp_group, members, "");
1058 g_list_foreach (members, (GFunc) g_object_unref, NULL);
1059 g_list_free (members);
1063 tp_contact_list_remove_group (EmpathyContactList *list,
1066 EmpathyTpGroup *tp_group;
1069 g_return_if_fail (EMPATHY_IS_TP_CONTACT_LIST (list));
1071 tp_group = tp_contact_list_find_group (EMPATHY_TP_CONTACT_LIST (list),
1078 DEBUG ("remove group %s", group);
1080 /* Remove all members of the group */
1081 members = empathy_tp_group_get_members (tp_group);
1082 empathy_tp_group_remove_members (tp_group, members, "");
1083 empathy_tp_group_close (tp_group);
1085 g_list_foreach (members, (GFunc) g_object_unref, NULL);
1086 g_list_free (members);
1090 tp_contact_list_iface_init (EmpathyContactListIface *iface)
1092 iface->add = tp_contact_list_add;
1093 iface->remove = tp_contact_list_remove;
1094 iface->get_members = tp_contact_list_get_members;
1095 iface->get_pendings = tp_contact_list_get_pendings;
1096 iface->get_all_groups = tp_contact_list_get_all_groups;
1097 iface->get_groups = tp_contact_list_get_groups;
1098 iface->add_to_group = tp_contact_list_add_to_group;
1099 iface->remove_from_group = tp_contact_list_remove_from_group;
1100 iface->rename_group = tp_contact_list_rename_group;
1101 iface->remove_group = tp_contact_list_remove_group;