]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-selector.c
Initial empathy_contact_selector_set_visible implementation.
[empathy.git] / libempathy-gtk / empathy-contact-selector.c
1 /*
2 *  Copyright (C) 2007 Marco Barisione <marco@barisione.org>
3 *  Copyright (C) 2008 Collabora Ltd.
4 *
5 *  This library is free software; you can redistribute it and/or
6 *  modify it under the terms of the GNU Lesser General Public
7 *  License as published by the Free Software Foundation; either
8 *  version 2.1 of the License, or (at your option) any later version.
9 *
10 *  This library 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 *  Lesser General Public License for more details.
14 *
15 *  You should have received a copy of the GNU Lesser General Public
16 *  License along with this library; if not, write to the Free Software
17 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 *
19 *  Authors: Marco Barisione <marco@barisione.org>
20 *           Elliot Fairweather <elliot.fairweather@collabora.co.uk>
21 */
22
23 #include "config.h"
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27
28 #include <libempathy/empathy-contact.h>
29 #include <libempathy-gtk/empathy-contact-list-store.h>
30 #include <libempathy/empathy-utils.h>
31
32 #include "empathy-contact-selector.h"
33
34 G_DEFINE_TYPE (EmpathyContactSelector, empathy_contact_selector,
35     GTK_TYPE_COMBO_BOX)
36
37 enum
38 {
39   PROP_0,
40   PROP_CONTACT_LIST
41 };
42
43 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactSelector)
44 typedef struct
45 {
46   EmpathyContactList *contact_list;
47   EmpathyContactListStore *store;
48   GtkTreeModel *model;
49   gboolean dispose_run;
50 } EmpathyContactSelectorPriv;
51
52 static void contact_selector_manage_blank_contact (
53     EmpathyContactSelector *selector);
54
55 static guint
56 contact_selector_get_number_online_contacts (GtkTreeModel *model)
57 {
58   GtkTreeIter tmp_iter;
59   gboolean is_online;
60   guint number_online_contacts = 0;
61   gboolean ok;
62
63   for (ok = gtk_tree_model_get_iter_first (model, &tmp_iter);
64       ok; ok = gtk_tree_model_iter_next (model, &tmp_iter))
65     {
66       gtk_tree_model_get (model,
67           &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE,
68           &is_online, -1);
69       if (is_online)
70         number_online_contacts++;
71     }
72
73   return number_online_contacts;
74 }
75
76 static gboolean
77 contact_selector_get_iter_for_blank_contact (GtkTreeModel *model,
78                                              GtkTreeIter *blank_iter)
79 {
80   GtkTreeIter tmp_iter;
81   EmpathyContact *tmp_contact;
82   gboolean is_present = FALSE;
83   gboolean ok;
84
85   for (ok = gtk_tree_model_get_iter_first (model, &tmp_iter);
86       ok; ok = gtk_tree_model_iter_next (model, &tmp_iter))
87     {
88       gtk_tree_model_get (model,
89           &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
90           &tmp_contact, -1);
91       if (tmp_contact == NULL)
92         {
93           *blank_iter = tmp_iter;
94           is_present = TRUE;
95           break;
96         }
97       g_object_unref (tmp_contact);
98     }
99
100   return is_present;
101 }
102
103 static void
104 contact_selector_add_blank_contact (EmpathyContactSelector *selector)
105 {
106   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
107   GtkTreeIter blank_iter;
108
109   gtk_tree_store_insert_with_values (
110       GTK_TREE_STORE (priv->store), &blank_iter, NULL, 0,
111       EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, NULL,
112       EMPATHY_CONTACT_LIST_STORE_COL_NAME, (_("Select a contact")),
113       EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, FALSE, -1);
114   g_signal_handlers_block_by_func (selector,
115       contact_selector_manage_blank_contact, selector);
116   gtk_combo_box_set_active_iter (GTK_COMBO_BOX (selector), &blank_iter);
117   g_signal_handlers_unblock_by_func (selector,
118       contact_selector_manage_blank_contact, selector);
119 }
120
121 static void
122 contact_selector_remove_blank_contact (EmpathyContactSelector *selector)
123 {
124   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
125   GtkTreeIter blank_iter;
126
127   if (contact_selector_get_iter_for_blank_contact (priv->model, &blank_iter))
128     gtk_tree_store_remove (GTK_TREE_STORE (priv->store), &blank_iter);
129 }
130
131 static void
132 contact_selector_manage_sensitivity (EmpathyContactSelector *selector)
133 {
134   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
135   guint number_online_contacts;
136
137   number_online_contacts = contact_selector_get_number_online_contacts (priv->model);
138
139   if (number_online_contacts != 0)
140     gtk_widget_set_sensitive (GTK_WIDGET (selector), TRUE);
141   else
142     gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
143 }
144
145 static void
146 contact_selector_manage_blank_contact (EmpathyContactSelector *selector)
147 {
148   gboolean is_popup_shown;
149
150   g_object_get (selector, "popup-shown", &is_popup_shown, NULL);
151
152   if (is_popup_shown)
153     {
154       contact_selector_remove_blank_contact (selector);
155     }
156   else
157     {
158       if (gtk_combo_box_get_active (GTK_COMBO_BOX (selector)) == -1)
159         {
160           contact_selector_add_blank_contact (selector);
161         }
162       else
163         {
164           contact_selector_remove_blank_contact (selector);
165         }
166     }
167
168   contact_selector_manage_sensitivity (selector);
169 }
170
171 static GObject *
172 contact_selector_constructor (GType type,
173                               guint n_construct_params,
174                               GObjectConstructParam *construct_params)
175 {
176   GObject *object;
177   EmpathyContactSelector *contact_selector;
178   EmpathyContactSelectorPriv *priv;
179   GtkCellLayout *cell_layout;
180   GtkCellRenderer *renderer;
181
182   object = G_OBJECT_CLASS (empathy_contact_selector_parent_class)->constructor 
183     (type, n_construct_params, construct_params);
184   priv = GET_PRIV (object);
185   contact_selector = EMPATHY_CONTACT_SELECTOR (object);
186   cell_layout = GTK_CELL_LAYOUT (object);
187
188   priv->store = empathy_contact_list_store_new (priv->contact_list);
189
190   g_object_set (priv->store, "is-compact", TRUE, "show-avatars", FALSE,
191       "show-offline", FALSE, "show-groups", FALSE,
192       "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME, NULL);
193
194   g_signal_connect_swapped (priv->store, "row-changed",
195       G_CALLBACK (contact_selector_manage_sensitivity),
196       contact_selector);
197   g_signal_connect_swapped (contact_selector, "changed",
198       G_CALLBACK (contact_selector_manage_blank_contact),
199       contact_selector);
200   g_signal_connect_swapped (contact_selector, "notify::popup-shown",
201       G_CALLBACK (contact_selector_manage_blank_contact),
202       contact_selector);
203
204   priv->model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store), NULL);
205
206   gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector), priv->model);
207   gtk_widget_set_sensitive (GTK_WIDGET (contact_selector), FALSE);
208
209   renderer = gtk_cell_renderer_pixbuf_new ();
210   gtk_cell_layout_pack_start (cell_layout, renderer, FALSE);
211   gtk_cell_layout_set_attributes (cell_layout, renderer,
212       "icon-name", EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL);
213
214   renderer = gtk_cell_renderer_text_new ();
215   gtk_cell_layout_pack_start (cell_layout, renderer, TRUE);
216   gtk_cell_layout_set_attributes (cell_layout, renderer,
217       "text", EMPATHY_CONTACT_LIST_STORE_COL_NAME, NULL);
218
219   contact_selector_manage_blank_contact (contact_selector);
220   contact_selector_manage_sensitivity (contact_selector);
221
222   return object;
223 }
224
225 static void
226 empathy_contact_selector_init (EmpathyContactSelector *empathy_contact_selector)
227 {
228   EmpathyContactSelectorPriv *priv =
229       G_TYPE_INSTANCE_GET_PRIVATE (empathy_contact_selector,
230       EMPATHY_TYPE_CONTACT_SELECTOR, EmpathyContactSelectorPriv);
231
232   empathy_contact_selector->priv = priv;
233
234   priv->dispose_run = FALSE;
235 }
236
237 static void
238 contact_selector_set_property (GObject *object,
239                                guint prop_id,
240                                const GValue *value,
241                                GParamSpec *pspec)
242 {
243   EmpathyContactSelectorPriv *priv = GET_PRIV (object);
244
245   switch (prop_id)
246     {
247       case PROP_CONTACT_LIST:
248         priv->contact_list = g_value_dup_object (value);
249         break;
250       default:
251         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252         break;
253     }
254 }
255
256 static void
257 contact_selector_get_property (GObject *object,
258                                guint prop_id,
259                                GValue *value,
260                                GParamSpec *pspec)
261 {
262   EmpathyContactSelectorPriv *priv = GET_PRIV (object);
263
264   switch (prop_id)
265     {
266       case PROP_CONTACT_LIST:
267         g_value_set_object (value, priv->contact_list);
268         break;
269       default:
270         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
271         break;
272     }
273 }
274
275 static void
276 contact_selector_dispose (GObject *object)
277 {
278   EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (object);
279   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
280
281   if (priv->dispose_run)
282     return;
283
284   priv->dispose_run = TRUE;
285
286   if (priv->contact_list)
287     {
288       g_object_unref (priv->contact_list);
289       priv->contact_list = NULL;
290     }
291
292   if (priv->model)
293     {
294       g_object_unref (priv->model);
295       priv->model = NULL;
296     }
297
298   if (priv->store)
299     {
300       g_object_unref (priv->store);
301       priv->store = NULL;
302     }
303
304   (G_OBJECT_CLASS (empathy_contact_selector_parent_class)->dispose) (object);
305 }
306
307 static void
308 empathy_contact_selector_class_init (EmpathyContactSelectorClass *klass)
309 {
310   GObjectClass *object_class = G_OBJECT_CLASS (klass);
311   object_class->constructor = contact_selector_constructor;
312   object_class->dispose = contact_selector_dispose;
313   object_class->set_property = contact_selector_set_property;
314   object_class->get_property = contact_selector_get_property;
315   g_type_class_add_private (klass, sizeof (EmpathyContactSelectorPriv));
316
317   g_object_class_install_property (object_class, PROP_CONTACT_LIST,
318       g_param_spec_object ("contact-list", "contact list", "contact list",
319       EMPATHY_TYPE_CONTACT_LIST, G_PARAM_CONSTRUCT_ONLY |
320       G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
321 }
322
323 /* public methods */
324
325 GtkWidget *
326 empathy_contact_selector_new (EmpathyContactList *contact_list)
327 {
328   g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (contact_list), NULL);
329
330   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_CONTACT_SELECTOR,
331       "contact-list", contact_list, NULL));
332 }
333
334 EmpathyContact *
335 empathy_contact_selector_dup_selected (EmpathyContactSelector *selector)
336 {
337   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
338   EmpathyContact *contact = NULL;
339   GtkTreeIter iter;
340
341   g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR (selector), NULL);
342
343   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (selector), &iter))
344     return NULL;
345
346   gtk_tree_model_get (priv->model, &iter,
347       EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact, -1);
348
349   return contact;
350 }
351
352 typedef struct
353 {
354   EmpathyContactSelectorFilterFunc func;
355   gpointer user_data;
356 } FilterData;
357
358 static void
359 filter_data_free (gpointer data)
360 {
361   g_slice_free (FilterData, data);
362 }
363
364 static gboolean
365 contact_selector_filter_visible_func (GtkTreeModel *model,
366                                       GtkTreeIter *iter,
367                                       gpointer user_data)
368 {
369   EmpathyContact *contact;
370   gboolean visible = FALSE;
371   FilterData *data = (FilterData *) user_data;
372
373   gtk_tree_model_get (model, iter, 0, &contact, -1);
374
375   if (contact != NULL)
376     {
377       visible = data->func(contact, data->user_data);
378
379       g_object_unref (contact);
380     }
381
382   return visible;
383
384 }
385
386 void
387 empathy_contact_selector_set_visible (EmpathyContactSelector *selector,
388                                       EmpathyContactSelectorFilterFunc func,
389                                       gpointer user_data)
390 {
391   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
392   FilterData *data;
393
394   data = g_slice_new0 (FilterData);
395   data->func = func;
396   data->user_data = user_data;
397
398   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->model),
399       contact_selector_filter_visible_func, data, filter_data_free);
400
401   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->model));
402 }