]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-store-channel.c
use the 48x48 version of the local-xmpp icon
[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 <string.h>
29
30 #include <glib.h>
31 #include <glib/gi18n-lib.h>
32 #include <gtk/gtk.h>
33
34 #include <folks/folks.h>
35 #include <folks/folks-telepathy.h>
36 #include <telepathy-glib/util.h>
37
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-enum-types.h>
40
41 #include "empathy-individual-store-channel.h"
42
43 #include "empathy-ui-utils.h"
44 #include "empathy-gtk-enum-types.h"
45
46 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
47 #include <libempathy/empathy-debug.h>
48
49 struct _EmpathyIndividualStoreChannelPriv
50 {
51   TpChannel *channel;
52
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;
57 };
58
59 enum
60 {
61   PROP_0,
62   PROP_CHANNEL,
63 };
64
65
66 G_DEFINE_TYPE (EmpathyIndividualStoreChannel, empathy_individual_store_channel,
67     EMPATHY_TYPE_INDIVIDUAL_STORE);
68
69 static void
70 add_members (EmpathyIndividualStoreChannel *self,
71     GPtrArray *members)
72 {
73   EmpathyIndividualStore *store = (EmpathyIndividualStore *) self;
74   guint i;
75
76   for (i = 0; i < members->len; i++)
77     {
78       TpContact *contact = g_ptr_array_index (members, i);
79       FolksIndividual *individual;
80
81       if (g_hash_table_lookup (self->priv->individuals, contact) != NULL)
82         continue;
83
84       individual = empathy_create_individual_from_tp_contact (contact);
85       if (individual == NULL)
86         return;
87
88       DEBUG ("%s joined channel %s", tp_contact_get_identifier (contact),
89           tp_proxy_get_object_path (self->priv->channel));
90
91       individual_store_add_individual_and_connect (store, individual);
92
93       /* Pass the individual reference to the hash table */
94       g_hash_table_insert (self->priv->individuals, g_object_ref (contact),
95           individual);
96     }
97 }
98
99 static void
100 remove_members (EmpathyIndividualStoreChannel *self,
101     GPtrArray *members)
102 {
103   EmpathyIndividualStore *store = (EmpathyIndividualStore *) self;
104   guint i;
105
106   for (i = 0; i < members->len; i++)
107     {
108       TpContact *contact = g_ptr_array_index (members, i);
109       FolksIndividual *individual;
110
111       individual = g_hash_table_lookup (self->priv->individuals, contact);
112       if (individual == NULL)
113         continue;
114
115       DEBUG ("%s left channel %s", tp_contact_get_identifier (contact),
116           tp_proxy_get_object_path (self->priv->channel));
117
118       individual_store_remove_individual_and_disconnect (store, individual);
119
120       g_hash_table_remove (self->priv->individuals, contact);
121     }
122 }
123
124 static void
125 group_contacts_changed_cb (TpChannel *channel,
126     GPtrArray *added,
127     GPtrArray *removed,
128     GPtrArray *local_pending,
129     GPtrArray *remote_pending,
130     TpContact *actor,
131     GHashTable *details,
132     gpointer user_data)
133 {
134   EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
135       user_data);
136
137   remove_members (self, removed);
138   add_members (self, added);
139 }
140
141 static void
142 channel_prepare_cb (GObject *source,
143     GAsyncResult *result,
144     gpointer user_data)
145 {
146   EmpathyIndividualStoreChannel *self = user_data;
147   TpChannel *channel = (TpChannel *) source;
148   GError *error = NULL;
149   GPtrArray *members;
150
151   if (!tp_proxy_prepare_finish (source, result, &error))
152     {
153       DEBUG ("Failed to prepare %s: %s", tp_proxy_get_object_path (source),
154           error->message);
155
156       g_error_free (error);
157     }
158
159   /* Add initial members */
160   members = tp_channel_group_dup_members_contacts (channel);
161   if (members != NULL)
162     {
163       add_members (self, members);
164       g_ptr_array_unref (members);
165     }
166
167   tp_g_signal_connect_object (channel, "group-contacts-changed",
168       G_CALLBACK (group_contacts_changed_cb), self, 0);
169 }
170
171 static void
172 individual_store_channel_set_individual_channel (
173     EmpathyIndividualStoreChannel *self,
174     TpChannel *channel)
175 {
176   GQuark features[] = { TP_CHANNEL_FEATURE_CONTACTS, 0 };
177
178   g_assert (self->priv->channel == NULL); /* construct only */
179   self->priv->channel = g_object_ref (channel);
180
181   tp_proxy_prepare_async (channel, features, channel_prepare_cb, self);
182 }
183
184 static void
185 individual_store_channel_dispose (GObject *object)
186 {
187   EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
188       object);
189   EmpathyIndividualStore *store = EMPATHY_INDIVIDUAL_STORE (object);
190   GHashTableIter iter;
191   gpointer v;
192
193   g_hash_table_iter_init (&iter, self->priv->individuals);
194   while (g_hash_table_iter_next (&iter, NULL, &v))
195     {
196       FolksIndividual *individual = v;
197
198       empathy_individual_store_disconnect_individual (store, individual);
199     }
200
201   tp_clear_pointer (&self->priv->individuals, g_hash_table_unref);
202   g_clear_object (&self->priv->channel);
203
204   G_OBJECT_CLASS (empathy_individual_store_channel_parent_class)->dispose (
205       object);
206 }
207
208 static void
209 individual_store_channel_get_property (GObject *object,
210     guint param_id,
211     GValue *value,
212     GParamSpec *pspec)
213 {
214   EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
215       object);
216
217   switch (param_id)
218     {
219     case PROP_CHANNEL:
220       g_value_set_object (value, self->priv->channel);
221       break;
222     default:
223       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
224       break;
225     };
226 }
227
228 static void
229 individual_store_channel_set_property (GObject *object,
230     guint param_id,
231     const GValue *value,
232     GParamSpec *pspec)
233 {
234   switch (param_id)
235     {
236     case PROP_CHANNEL:
237       individual_store_channel_set_individual_channel (
238           EMPATHY_INDIVIDUAL_STORE_CHANNEL (object),
239           g_value_get_object (value));
240       break;
241     default:
242       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
243       break;
244     };
245 }
246
247 static void
248 individual_store_channel_reload_individuals (EmpathyIndividualStore *store)
249 {
250   EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
251       store);
252   GPtrArray *members;
253   GList *list, *l;
254
255   /* remove all. The list returned by g_hash_table_get_keys() is valid until
256    * the hash table is modified so we can't remove the contact directly in the
257    * iteration. */
258   members = g_ptr_array_new_with_free_func (g_object_unref);
259
260   list = g_hash_table_get_keys (self->priv->individuals);
261   for (l = list; l != NULL; l = g_list_next (l))
262     {
263       g_ptr_array_add (members, g_object_ref (l->data));
264     }
265
266   remove_members (self, members);
267
268   g_list_free (list);
269   g_ptr_array_unref (members);
270
271   /* re-add members */
272   members = tp_channel_group_dup_members_contacts (self->priv->channel);
273   if (members == NULL)
274     return;
275
276   add_members (self, members);
277   g_ptr_array_unref (members);
278 }
279
280 static gboolean
281 individual_store_channel_initial_loading (EmpathyIndividualStore *store)
282 {
283   EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
284       store);
285
286   return !tp_proxy_is_prepared (self->priv->channel,
287       TP_CHANNEL_FEATURE_CONTACTS);
288 }
289
290 static void
291 empathy_individual_store_channel_class_init (
292     EmpathyIndividualStoreChannelClass *klass)
293 {
294   GObjectClass *object_class = G_OBJECT_CLASS (klass);
295   EmpathyIndividualStoreClass *store_class = EMPATHY_INDIVIDUAL_STORE_CLASS (
296       klass);
297
298   object_class->dispose = individual_store_channel_dispose;
299   object_class->get_property = individual_store_channel_get_property;
300   object_class->set_property = individual_store_channel_set_property;
301
302   store_class->reload_individuals = individual_store_channel_reload_individuals;
303   store_class->initial_loading = individual_store_channel_initial_loading;
304
305   g_object_class_install_property (object_class,
306       PROP_CHANNEL,
307       g_param_spec_object ("individual-channel",
308           "Individual channel",
309           "Individual channel",
310           TP_TYPE_CHANNEL,
311           G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
312
313   g_type_class_add_private (object_class,
314       sizeof (EmpathyIndividualStoreChannelPriv));
315 }
316
317 static void
318 empathy_individual_store_channel_init (EmpathyIndividualStoreChannel *self)
319 {
320   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
321       EMPATHY_TYPE_INDIVIDUAL_STORE_CHANNEL, EmpathyIndividualStoreChannelPriv);
322
323   self->priv->individuals = g_hash_table_new_full (NULL, NULL, g_object_unref,
324       g_object_unref);
325 }
326
327 EmpathyIndividualStoreChannel *
328 empathy_individual_store_channel_new (TpChannel *channel)
329 {
330   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
331
332   return g_object_new (EMPATHY_TYPE_INDIVIDUAL_STORE_CHANNEL,
333       "individual-channel", channel, NULL);
334 }
335
336 TpChannel *
337 empathy_individual_store_channel_get_channel (
338     EmpathyIndividualStoreChannel *self)
339 {
340   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE_CHANNEL (self), FALSE);
341
342   return self->priv->channel;
343 }