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>
27 #include "empathy-individual-store-channel.h"
29 #include "empathy-utils.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
32 #include "empathy-debug.h"
34 struct _EmpathyIndividualStoreChannelPriv
38 /* TpContact => FolksIndividual
39 * We keep the individuals we have added to the store so can easily remove
40 * them when their TpContact leaves the channel. */
41 GHashTable *individuals;
51 G_DEFINE_TYPE (EmpathyIndividualStoreChannel, empathy_individual_store_channel,
52 EMPATHY_TYPE_INDIVIDUAL_STORE);
55 add_members (EmpathyIndividualStoreChannel *self,
58 EmpathyIndividualStore *store = (EmpathyIndividualStore *) self;
61 for (i = 0; i < members->len; i++)
63 TpContact *contact = g_ptr_array_index (members, i);
64 FolksIndividual *individual;
66 if (g_hash_table_lookup (self->priv->individuals, contact) != NULL)
69 individual = empathy_create_individual_from_tp_contact (contact);
70 if (individual == NULL)
73 DEBUG ("%s joined channel %s", tp_contact_get_identifier (contact),
74 tp_proxy_get_object_path (self->priv->channel));
76 individual_store_add_individual_and_connect (store, individual);
78 /* Pass the individual reference to the hash table */
79 g_hash_table_insert (self->priv->individuals, g_object_ref (contact),
85 remove_members (EmpathyIndividualStoreChannel *self,
88 EmpathyIndividualStore *store = (EmpathyIndividualStore *) self;
91 for (i = 0; i < members->len; i++)
93 TpContact *contact = g_ptr_array_index (members, i);
94 FolksIndividual *individual;
96 individual = g_hash_table_lookup (self->priv->individuals, contact);
97 if (individual == NULL)
100 DEBUG ("%s left channel %s", tp_contact_get_identifier (contact),
101 tp_proxy_get_object_path (self->priv->channel));
103 individual_store_remove_individual_and_disconnect (store, individual);
105 g_hash_table_remove (self->priv->individuals, contact);
110 group_contacts_changed_cb (TpChannel *channel,
113 GPtrArray *local_pending,
114 GPtrArray *remote_pending,
119 EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
122 remove_members (self, removed);
123 add_members (self, added);
127 individual_store_channel_set_individual_channel (
128 EmpathyIndividualStoreChannel *self,
133 g_assert (self->priv->channel == NULL); /* construct only */
134 self->priv->channel = g_object_ref (channel);
136 /* Add initial members */
137 members = tp_channel_group_dup_members_contacts (channel);
140 add_members (self, members);
141 g_ptr_array_unref (members);
144 tp_g_signal_connect_object (channel, "group-contacts-changed",
145 G_CALLBACK (group_contacts_changed_cb), self, 0);
149 individual_store_channel_dispose (GObject *object)
151 EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
153 EmpathyIndividualStore *store = EMPATHY_INDIVIDUAL_STORE (object);
157 g_hash_table_iter_init (&iter, self->priv->individuals);
158 while (g_hash_table_iter_next (&iter, NULL, &v))
160 FolksIndividual *individual = v;
162 empathy_individual_store_disconnect_individual (store, individual);
165 tp_clear_pointer (&self->priv->individuals, g_hash_table_unref);
166 g_clear_object (&self->priv->channel);
168 G_OBJECT_CLASS (empathy_individual_store_channel_parent_class)->dispose (
173 individual_store_channel_get_property (GObject *object,
178 EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
184 g_value_set_object (value, self->priv->channel);
187 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
193 individual_store_channel_set_property (GObject *object,
201 individual_store_channel_set_individual_channel (
202 EMPATHY_INDIVIDUAL_STORE_CHANNEL (object),
203 g_value_get_object (value));
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
212 individual_store_channel_reload_individuals (EmpathyIndividualStore *store)
214 EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
219 /* remove all. The list returned by g_hash_table_get_keys() is valid until
220 * the hash table is modified so we can't remove the contact directly in the
222 members = g_ptr_array_new_with_free_func (g_object_unref);
224 list = g_hash_table_get_keys (self->priv->individuals);
225 for (l = list; l != NULL; l = g_list_next (l))
227 g_ptr_array_add (members, g_object_ref (l->data));
230 remove_members (self, members);
233 g_ptr_array_unref (members);
236 members = tp_channel_group_dup_members_contacts (self->priv->channel);
240 add_members (self, members);
241 g_ptr_array_unref (members);
245 individual_store_channel_initial_loading (EmpathyIndividualStore *store)
247 EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
250 return !tp_proxy_is_prepared (self->priv->channel,
251 TP_CHANNEL_FEATURE_CONTACTS);
255 empathy_individual_store_channel_class_init (
256 EmpathyIndividualStoreChannelClass *klass)
258 GObjectClass *object_class = G_OBJECT_CLASS (klass);
259 EmpathyIndividualStoreClass *store_class = EMPATHY_INDIVIDUAL_STORE_CLASS (
262 object_class->dispose = individual_store_channel_dispose;
263 object_class->get_property = individual_store_channel_get_property;
264 object_class->set_property = individual_store_channel_set_property;
266 store_class->reload_individuals = individual_store_channel_reload_individuals;
267 store_class->initial_loading = individual_store_channel_initial_loading;
269 g_object_class_install_property (object_class,
271 g_param_spec_object ("individual-channel",
272 "Individual channel",
273 "Individual channel",
275 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
277 g_type_class_add_private (object_class,
278 sizeof (EmpathyIndividualStoreChannelPriv));
282 empathy_individual_store_channel_init (EmpathyIndividualStoreChannel *self)
284 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
285 EMPATHY_TYPE_INDIVIDUAL_STORE_CHANNEL, EmpathyIndividualStoreChannelPriv);
287 self->priv->individuals = g_hash_table_new_full (NULL, NULL, g_object_unref,
291 EmpathyIndividualStoreChannel *
292 empathy_individual_store_channel_new (TpChannel *channel)
294 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
296 return g_object_new (EMPATHY_TYPE_INDIVIDUAL_STORE_CHANNEL,
297 "individual-channel", channel, NULL);
301 empathy_individual_store_channel_get_channel (
302 EmpathyIndividualStoreChannel *self)
304 g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE_CHANNEL (self), FALSE);
306 return self->priv->channel;