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>
28 #include <libempathy/empathy-utils.h>
30 #include "empathy-individual-store-channel.h"
32 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
33 #include <libempathy/empathy-debug.h>
35 struct _EmpathyIndividualStoreChannelPriv
39 /* TpContact => FolksIndividual
40 * We keep the individuals we have added to the store so can easily remove
41 * them when their TpContact leaves the channel. */
42 GHashTable *individuals;
52 G_DEFINE_TYPE (EmpathyIndividualStoreChannel, empathy_individual_store_channel,
53 EMPATHY_TYPE_INDIVIDUAL_STORE);
56 add_members (EmpathyIndividualStoreChannel *self,
59 EmpathyIndividualStore *store = (EmpathyIndividualStore *) self;
62 for (i = 0; i < members->len; i++)
64 TpContact *contact = g_ptr_array_index (members, i);
65 FolksIndividual *individual;
67 if (g_hash_table_lookup (self->priv->individuals, contact) != NULL)
70 individual = empathy_create_individual_from_tp_contact (contact);
71 if (individual == NULL)
74 DEBUG ("%s joined channel %s", tp_contact_get_identifier (contact),
75 tp_proxy_get_object_path (self->priv->channel));
77 individual_store_add_individual_and_connect (store, individual);
79 /* Pass the individual reference to the hash table */
80 g_hash_table_insert (self->priv->individuals, g_object_ref (contact),
86 remove_members (EmpathyIndividualStoreChannel *self,
89 EmpathyIndividualStore *store = (EmpathyIndividualStore *) self;
92 for (i = 0; i < members->len; i++)
94 TpContact *contact = g_ptr_array_index (members, i);
95 FolksIndividual *individual;
97 individual = g_hash_table_lookup (self->priv->individuals, contact);
98 if (individual == NULL)
101 DEBUG ("%s left channel %s", tp_contact_get_identifier (contact),
102 tp_proxy_get_object_path (self->priv->channel));
104 individual_store_remove_individual_and_disconnect (store, individual);
106 g_hash_table_remove (self->priv->individuals, contact);
111 group_contacts_changed_cb (TpChannel *channel,
114 GPtrArray *local_pending,
115 GPtrArray *remote_pending,
120 EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
123 remove_members (self, removed);
124 add_members (self, added);
128 individual_store_channel_set_individual_channel (
129 EmpathyIndividualStoreChannel *self,
134 g_assert (self->priv->channel == NULL); /* construct only */
135 self->priv->channel = g_object_ref (channel);
137 /* Add initial members */
138 members = tp_channel_group_dup_members_contacts (channel);
141 add_members (self, members);
142 g_ptr_array_unref (members);
145 tp_g_signal_connect_object (channel, "group-contacts-changed",
146 G_CALLBACK (group_contacts_changed_cb), self, 0);
150 individual_store_channel_dispose (GObject *object)
152 EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
154 EmpathyIndividualStore *store = EMPATHY_INDIVIDUAL_STORE (object);
158 g_hash_table_iter_init (&iter, self->priv->individuals);
159 while (g_hash_table_iter_next (&iter, NULL, &v))
161 FolksIndividual *individual = v;
163 empathy_individual_store_disconnect_individual (store, individual);
166 tp_clear_pointer (&self->priv->individuals, g_hash_table_unref);
167 g_clear_object (&self->priv->channel);
169 G_OBJECT_CLASS (empathy_individual_store_channel_parent_class)->dispose (
174 individual_store_channel_get_property (GObject *object,
179 EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
185 g_value_set_object (value, self->priv->channel);
188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
194 individual_store_channel_set_property (GObject *object,
202 individual_store_channel_set_individual_channel (
203 EMPATHY_INDIVIDUAL_STORE_CHANNEL (object),
204 g_value_get_object (value));
207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
213 individual_store_channel_reload_individuals (EmpathyIndividualStore *store)
215 EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
220 /* remove all. The list returned by g_hash_table_get_keys() is valid until
221 * the hash table is modified so we can't remove the contact directly in the
223 members = g_ptr_array_new_with_free_func (g_object_unref);
225 list = g_hash_table_get_keys (self->priv->individuals);
226 for (l = list; l != NULL; l = g_list_next (l))
228 g_ptr_array_add (members, g_object_ref (l->data));
231 remove_members (self, members);
234 g_ptr_array_unref (members);
237 members = tp_channel_group_dup_members_contacts (self->priv->channel);
241 add_members (self, members);
242 g_ptr_array_unref (members);
246 individual_store_channel_initial_loading (EmpathyIndividualStore *store)
248 EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
251 return !tp_proxy_is_prepared (self->priv->channel,
252 TP_CHANNEL_FEATURE_CONTACTS);
256 empathy_individual_store_channel_class_init (
257 EmpathyIndividualStoreChannelClass *klass)
259 GObjectClass *object_class = G_OBJECT_CLASS (klass);
260 EmpathyIndividualStoreClass *store_class = EMPATHY_INDIVIDUAL_STORE_CLASS (
263 object_class->dispose = individual_store_channel_dispose;
264 object_class->get_property = individual_store_channel_get_property;
265 object_class->set_property = individual_store_channel_set_property;
267 store_class->reload_individuals = individual_store_channel_reload_individuals;
268 store_class->initial_loading = individual_store_channel_initial_loading;
270 g_object_class_install_property (object_class,
272 g_param_spec_object ("individual-channel",
273 "Individual channel",
274 "Individual channel",
276 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
278 g_type_class_add_private (object_class,
279 sizeof (EmpathyIndividualStoreChannelPriv));
283 empathy_individual_store_channel_init (EmpathyIndividualStoreChannel *self)
285 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
286 EMPATHY_TYPE_INDIVIDUAL_STORE_CHANNEL, EmpathyIndividualStoreChannelPriv);
288 self->priv->individuals = g_hash_table_new_full (NULL, NULL, g_object_unref,
292 EmpathyIndividualStoreChannel *
293 empathy_individual_store_channel_new (TpChannel *channel)
295 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
297 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_STORE_CHANNEL,
298 "individual-channel", channel, NULL);
302 empathy_individual_store_channel_get_channel (
303 EmpathyIndividualStoreChannel *self)
305 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE_CHANNEL (self), FALSE);
307 return self->priv->channel;