]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-store-channel.c
0fefb9c85837885209611b64db1feaa880a8bf3e
[empathy.git] / libempathy-gtk / empathy-individual-store-channel.c
1 /*
2  * Copyright (C) 2005-2007 Imendio AB
3  * Copyright (C) 2007-2011 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
19  *
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>
24  */
25
26 #include "config.h"
27
28 #include <libempathy/empathy-utils.h>
29
30 #include "empathy-individual-store-channel.h"
31
32 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
33 #include <libempathy/empathy-debug.h>
34
35 struct _EmpathyIndividualStoreChannelPriv
36 {
37   TpChannel *channel;
38
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;
43 };
44
45 enum
46 {
47   PROP_0,
48   PROP_CHANNEL,
49 };
50
51
52 G_DEFINE_TYPE (EmpathyIndividualStoreChannel, empathy_individual_store_channel,
53     EMPATHY_TYPE_INDIVIDUAL_STORE);
54
55 static void
56 add_members (EmpathyIndividualStoreChannel *self,
57     GPtrArray *members)
58 {
59   EmpathyIndividualStore *store = (EmpathyIndividualStore *) self;
60   guint i;
61
62   for (i = 0; i < members->len; i++)
63     {
64       TpContact *contact = g_ptr_array_index (members, i);
65       FolksIndividual *individual;
66
67       if (g_hash_table_lookup (self->priv->individuals, contact) != NULL)
68         continue;
69
70       individual = empathy_create_individual_from_tp_contact (contact);
71       if (individual == NULL)
72         return;
73
74       DEBUG ("%s joined channel %s", tp_contact_get_identifier (contact),
75           tp_proxy_get_object_path (self->priv->channel));
76
77       individual_store_add_individual_and_connect (store, individual);
78
79       /* Pass the individual reference to the hash table */
80       g_hash_table_insert (self->priv->individuals, g_object_ref (contact),
81           individual);
82     }
83 }
84
85 static void
86 remove_members (EmpathyIndividualStoreChannel *self,
87     GPtrArray *members)
88 {
89   EmpathyIndividualStore *store = (EmpathyIndividualStore *) self;
90   guint i;
91
92   for (i = 0; i < members->len; i++)
93     {
94       TpContact *contact = g_ptr_array_index (members, i);
95       FolksIndividual *individual;
96
97       individual = g_hash_table_lookup (self->priv->individuals, contact);
98       if (individual == NULL)
99         continue;
100
101       DEBUG ("%s left channel %s", tp_contact_get_identifier (contact),
102           tp_proxy_get_object_path (self->priv->channel));
103
104       individual_store_remove_individual_and_disconnect (store, individual);
105
106       g_hash_table_remove (self->priv->individuals, contact);
107     }
108 }
109
110 static void
111 group_contacts_changed_cb (TpChannel *channel,
112     GPtrArray *added,
113     GPtrArray *removed,
114     GPtrArray *local_pending,
115     GPtrArray *remote_pending,
116     TpContact *actor,
117     GHashTable *details,
118     gpointer user_data)
119 {
120   EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
121       user_data);
122
123   remove_members (self, removed);
124   add_members (self, added);
125 }
126
127 static void
128 individual_store_channel_set_individual_channel (
129     EmpathyIndividualStoreChannel *self,
130     TpChannel *channel)
131 {
132   GPtrArray *members;
133
134   g_assert (self->priv->channel == NULL); /* construct only */
135   self->priv->channel = g_object_ref (channel);
136
137   /* Add initial members */
138   members = tp_channel_group_dup_members_contacts (channel);
139   if (members != NULL)
140     {
141       add_members (self, members);
142       g_ptr_array_unref (members);
143     }
144
145   tp_g_signal_connect_object (channel, "group-contacts-changed",
146       G_CALLBACK (group_contacts_changed_cb), self, 0);
147 }
148
149 static void
150 individual_store_channel_dispose (GObject *object)
151 {
152   EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
153       object);
154   EmpathyIndividualStore *store = EMPATHY_INDIVIDUAL_STORE (object);
155   GHashTableIter iter;
156   gpointer v;
157
158   g_hash_table_iter_init (&iter, self->priv->individuals);
159   while (g_hash_table_iter_next (&iter, NULL, &v))
160     {
161       FolksIndividual *individual = v;
162
163       empathy_individual_store_disconnect_individual (store, individual);
164     }
165
166   tp_clear_pointer (&self->priv->individuals, g_hash_table_unref);
167   g_clear_object (&self->priv->channel);
168
169   G_OBJECT_CLASS (empathy_individual_store_channel_parent_class)->dispose (
170       object);
171 }
172
173 static void
174 individual_store_channel_get_property (GObject *object,
175     guint param_id,
176     GValue *value,
177     GParamSpec *pspec)
178 {
179   EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
180       object);
181
182   switch (param_id)
183     {
184     case PROP_CHANNEL:
185       g_value_set_object (value, self->priv->channel);
186       break;
187     default:
188       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
189       break;
190     };
191 }
192
193 static void
194 individual_store_channel_set_property (GObject *object,
195     guint param_id,
196     const GValue *value,
197     GParamSpec *pspec)
198 {
199   switch (param_id)
200     {
201     case PROP_CHANNEL:
202       individual_store_channel_set_individual_channel (
203           EMPATHY_INDIVIDUAL_STORE_CHANNEL (object),
204           g_value_get_object (value));
205       break;
206     default:
207       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
208       break;
209     };
210 }
211
212 static void
213 individual_store_channel_reload_individuals (EmpathyIndividualStore *store)
214 {
215   EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
216       store);
217   GPtrArray *members;
218   GList *list, *l;
219
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
222    * iteration. */
223   members = g_ptr_array_new_with_free_func (g_object_unref);
224
225   list = g_hash_table_get_keys (self->priv->individuals);
226   for (l = list; l != NULL; l = g_list_next (l))
227     {
228       g_ptr_array_add (members, g_object_ref (l->data));
229     }
230
231   remove_members (self, members);
232
233   g_list_free (list);
234   g_ptr_array_unref (members);
235
236   /* re-add members */
237   members = tp_channel_group_dup_members_contacts (self->priv->channel);
238   if (members == NULL)
239     return;
240
241   add_members (self, members);
242   g_ptr_array_unref (members);
243 }
244
245 static gboolean
246 individual_store_channel_initial_loading (EmpathyIndividualStore *store)
247 {
248   EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
249       store);
250
251   return !tp_proxy_is_prepared (self->priv->channel,
252       TP_CHANNEL_FEATURE_CONTACTS);
253 }
254
255 static void
256 empathy_individual_store_channel_class_init (
257     EmpathyIndividualStoreChannelClass *klass)
258 {
259   GObjectClass *object_class = G_OBJECT_CLASS (klass);
260   EmpathyIndividualStoreClass *store_class = EMPATHY_INDIVIDUAL_STORE_CLASS (
261       klass);
262
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;
266
267   store_class->reload_individuals = individual_store_channel_reload_individuals;
268   store_class->initial_loading = individual_store_channel_initial_loading;
269
270   g_object_class_install_property (object_class,
271       PROP_CHANNEL,
272       g_param_spec_object ("individual-channel",
273           "Individual channel",
274           "Individual channel",
275           TP_TYPE_CHANNEL,
276           G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
277
278   g_type_class_add_private (object_class,
279       sizeof (EmpathyIndividualStoreChannelPriv));
280 }
281
282 static void
283 empathy_individual_store_channel_init (EmpathyIndividualStoreChannel *self)
284 {
285   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
286       EMPATHY_TYPE_INDIVIDUAL_STORE_CHANNEL, EmpathyIndividualStoreChannelPriv);
287
288   self->priv->individuals = g_hash_table_new_full (NULL, NULL, g_object_unref,
289       g_object_unref);
290 }
291
292 EmpathyIndividualStoreChannel *
293 empathy_individual_store_channel_new (TpChannel *channel)
294 {
295   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
296
297   return g_object_new (EMPATHY_TYPE_INDIVIDUAL_STORE_CHANNEL,
298       "individual-channel", channel, NULL);
299 }
300
301 TpChannel *
302 empathy_individual_store_channel_get_channel (
303     EmpathyIndividualStoreChannel *self)
304 {
305   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE_CHANNEL (self), FALSE);
306
307   return self->priv->channel;
308 }