2 * Copyright (C) 2005-2007 Imendio AB
3 * Copyright (C) 2007-2011 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 USA
20 * Authors: Mikael Hallendal <micke@imendio.com>
21 * Martyn Russell <martyn@imendio.com>
22 * Xavier Claessens <xclaesse@gmail.com>
23 * Travis Reitter <travis.reitter@collabora.co.uk>
31 #include <glib/gi18n-lib.h>
34 #include <folks/folks.h>
35 #include <folks/folks-telepathy.h>
36 #include <telepathy-glib/util.h>
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-enum-types.h>
41 #include "empathy-individual-store-channel.h"
43 #include "empathy-ui-utils.h"
44 #include "empathy-gtk-enum-types.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
47 #include <libempathy/empathy-debug.h>
49 struct _EmpathyIndividualStoreChannelPriv
53 /* TpContact => FolksIndividual
54 * We keep the individuals we have added to the store so can easily remove
55 * them when their TpContact leaves the channel. */
56 GHashTable *individuals;
66 G_DEFINE_TYPE (EmpathyIndividualStoreChannel, empathy_individual_store_channel,
67 EMPATHY_TYPE_INDIVIDUAL_STORE);
70 add_members (EmpathyIndividualStoreChannel *self,
73 EmpathyIndividualStore *store = (EmpathyIndividualStore *) self;
76 for (i = 0; i < members->len; i++)
78 TpContact *contact = g_ptr_array_index (members, i);
79 FolksIndividual *individual;
81 if (g_hash_table_lookup (self->priv->individuals, contact) != NULL)
84 individual = empathy_create_individual_from_tp_contact (contact);
85 if (individual == NULL)
88 DEBUG ("%s joined channel %s", tp_contact_get_identifier (contact),
89 tp_proxy_get_object_path (self->priv->channel));
91 individual_store_add_individual_and_connect (store, individual);
93 /* Pass the individual reference to the hash table */
94 g_hash_table_insert (self->priv->individuals, g_object_ref (contact),
100 remove_members (EmpathyIndividualStoreChannel *self,
103 EmpathyIndividualStore *store = (EmpathyIndividualStore *) self;
106 for (i = 0; i < members->len; i++)
108 TpContact *contact = g_ptr_array_index (members, i);
109 FolksIndividual *individual;
111 individual = g_hash_table_lookup (self->priv->individuals, contact);
112 if (individual == NULL)
115 DEBUG ("%s left channel %s", tp_contact_get_identifier (contact),
116 tp_proxy_get_object_path (self->priv->channel));
118 individual_store_remove_individual_and_disconnect (store, individual);
120 g_hash_table_remove (self->priv->individuals, contact);
125 group_contacts_changed_cb (TpChannel *channel,
128 GPtrArray *local_pending,
129 GPtrArray *remote_pending,
134 EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
137 remove_members (self, removed);
138 add_members (self, added);
142 individual_store_channel_set_individual_channel (
143 EmpathyIndividualStoreChannel *self,
148 g_assert (self->priv->channel == NULL); /* construct only */
149 self->priv->channel = g_object_ref (channel);
151 /* Add initial members */
152 members = tp_channel_group_dup_members_contacts (channel);
155 add_members (self, members);
156 g_ptr_array_unref (members);
159 tp_g_signal_connect_object (channel, "group-contacts-changed",
160 G_CALLBACK (group_contacts_changed_cb), self, 0);
164 individual_store_channel_dispose (GObject *object)
166 EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
168 EmpathyIndividualStore *store = EMPATHY_INDIVIDUAL_STORE (object);
172 g_hash_table_iter_init (&iter, self->priv->individuals);
173 while (g_hash_table_iter_next (&iter, NULL, &v))
175 FolksIndividual *individual = v;
177 empathy_individual_store_disconnect_individual (store, individual);
180 tp_clear_pointer (&self->priv->individuals, g_hash_table_unref);
181 g_clear_object (&self->priv->channel);
183 G_OBJECT_CLASS (empathy_individual_store_channel_parent_class)->dispose (
188 individual_store_channel_get_property (GObject *object,
193 EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
199 g_value_set_object (value, self->priv->channel);
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
208 individual_store_channel_set_property (GObject *object,
216 individual_store_channel_set_individual_channel (
217 EMPATHY_INDIVIDUAL_STORE_CHANNEL (object),
218 g_value_get_object (value));
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
227 individual_store_channel_reload_individuals (EmpathyIndividualStore *store)
229 EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
234 /* remove all. The list returned by g_hash_table_get_keys() is valid until
235 * the hash table is modified so we can't remove the contact directly in the
237 members = g_ptr_array_new_with_free_func (g_object_unref);
239 list = g_hash_table_get_keys (self->priv->individuals);
240 for (l = list; l != NULL; l = g_list_next (l))
242 g_ptr_array_add (members, g_object_ref (l->data));
245 remove_members (self, members);
248 g_ptr_array_unref (members);
251 members = tp_channel_group_dup_members_contacts (self->priv->channel);
255 add_members (self, members);
256 g_ptr_array_unref (members);
260 individual_store_channel_initial_loading (EmpathyIndividualStore *store)
262 EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
265 return !tp_proxy_is_prepared (self->priv->channel,
266 TP_CHANNEL_FEATURE_CONTACTS);
270 empathy_individual_store_channel_class_init (
271 EmpathyIndividualStoreChannelClass *klass)
273 GObjectClass *object_class = G_OBJECT_CLASS (klass);
274 EmpathyIndividualStoreClass *store_class = EMPATHY_INDIVIDUAL_STORE_CLASS (
277 object_class->dispose = individual_store_channel_dispose;
278 object_class->get_property = individual_store_channel_get_property;
279 object_class->set_property = individual_store_channel_set_property;
281 store_class->reload_individuals = individual_store_channel_reload_individuals;
282 store_class->initial_loading = individual_store_channel_initial_loading;
284 g_object_class_install_property (object_class,
286 g_param_spec_object ("individual-channel",
287 "Individual channel",
288 "Individual channel",
290 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
292 g_type_class_add_private (object_class,
293 sizeof (EmpathyIndividualStoreChannelPriv));
297 empathy_individual_store_channel_init (EmpathyIndividualStoreChannel *self)
299 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
300 EMPATHY_TYPE_INDIVIDUAL_STORE_CHANNEL, EmpathyIndividualStoreChannelPriv);
302 self->priv->individuals = g_hash_table_new_full (NULL, NULL, g_object_unref,
306 EmpathyIndividualStoreChannel *
307 empathy_individual_store_channel_new (TpChannel *channel)
309 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
311 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_STORE_CHANNEL,
312 "individual-channel", channel, NULL);
316 empathy_individual_store_channel_get_channel (
317 EmpathyIndividualStoreChannel *self)
319 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE_CHANNEL (self), FALSE);
321 return self->priv->channel;