]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-selector.c
46e6e63a3a977d4cd72d477056fff247b4ddf53a
[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, 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
115   /* look up blank_iter in the filter model */
116   g_return_if_fail (gtk_tree_model_filter_convert_child_iter_to_iter (
117       GTK_TREE_MODEL_FILTER (priv->model), &iter, &blank_iter));
118
119   g_signal_handlers_block_by_func (selector,
120       contact_selector_manage_blank_contact, selector);
121   gtk_combo_box_set_active_iter (GTK_COMBO_BOX (selector), &iter);
122   g_signal_handlers_unblock_by_func (selector,
123       contact_selector_manage_blank_contact, selector);
124 }
125
126 static void
127 contact_selector_remove_blank_contact (EmpathyContactSelector *selector)
128 {
129   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
130   GtkTreeIter blank_iter;
131
132   if (contact_selector_get_iter_for_blank_contact (priv->model, &blank_iter))
133     gtk_tree_store_remove (GTK_TREE_STORE (priv->store), &blank_iter);
134 }
135
136 static void
137 contact_selector_manage_sensitivity (EmpathyContactSelector *selector)
138 {
139   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
140   guint number_online_contacts;
141
142   number_online_contacts = contact_selector_get_number_online_contacts (priv->model);
143
144   if (number_online_contacts != 0)
145     gtk_widget_set_sensitive (GTK_WIDGET (selector), TRUE);
146   else
147     gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
148 }
149
150 static void
151 contact_selector_manage_blank_contact (EmpathyContactSelector *selector)
152 {
153   gboolean is_popup_shown;
154
155   g_object_get (selector, "popup-shown", &is_popup_shown, NULL);
156
157   if (is_popup_shown)
158     {
159       contact_selector_remove_blank_contact (selector);
160     }
161   else
162     {
163       if (gtk_combo_box_get_active (GTK_COMBO_BOX (selector)) == -1)
164         {
165           contact_selector_add_blank_contact (selector);
166         }
167       else
168         {
169           contact_selector_remove_blank_contact (selector);
170         }
171     }
172
173   contact_selector_manage_sensitivity (selector);
174 }
175
176 static GObject *
177 contact_selector_constructor (GType type,
178                               guint n_construct_params,
179                               GObjectConstructParam *construct_params)
180 {
181   GObject *object;
182   EmpathyContactSelector *contact_selector;
183   EmpathyContactSelectorPriv *priv;
184   GtkCellLayout *cell_layout;
185   GtkCellRenderer *renderer;
186
187   object = G_OBJECT_CLASS (empathy_contact_selector_parent_class)->constructor 
188     (type, n_construct_params, construct_params);
189   priv = GET_PRIV (object);
190   contact_selector = EMPATHY_CONTACT_SELECTOR (object);
191   cell_layout = GTK_CELL_LAYOUT (object);
192
193   priv->store = empathy_contact_list_store_new (priv->contact_list);
194
195   g_object_set (priv->store, "is-compact", TRUE, "show-avatars", FALSE,
196       "show-offline", FALSE, "show-groups", FALSE,
197       "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME, NULL);
198
199   g_signal_connect_swapped (priv->store, "row-changed",
200       G_CALLBACK (contact_selector_manage_sensitivity),
201       contact_selector);
202   g_signal_connect_swapped (contact_selector, "changed",
203       G_CALLBACK (contact_selector_manage_blank_contact),
204       contact_selector);
205   g_signal_connect_swapped (contact_selector, "notify::popup-shown",
206       G_CALLBACK (contact_selector_manage_blank_contact),
207       contact_selector);
208
209   priv->model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store), NULL);
210
211   gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector), priv->model);
212   gtk_widget_set_sensitive (GTK_WIDGET (contact_selector), FALSE);
213
214   renderer = gtk_cell_renderer_pixbuf_new ();
215   gtk_cell_layout_pack_start (cell_layout, renderer, FALSE);
216   gtk_cell_layout_set_attributes (cell_layout, renderer,
217       "icon-name", EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL);
218
219   renderer = gtk_cell_renderer_text_new ();
220   gtk_cell_layout_pack_start (cell_layout, renderer, TRUE);
221   gtk_cell_layout_set_attributes (cell_layout, renderer,
222       "text", EMPATHY_CONTACT_LIST_STORE_COL_NAME, NULL);
223
224   contact_selector_manage_blank_contact (contact_selector);
225   contact_selector_manage_sensitivity (contact_selector);
226
227   return object;
228 }
229
230 static void
231 empathy_contact_selector_init (EmpathyContactSelector *empathy_contact_selector)
232 {
233   EmpathyContactSelectorPriv *priv =
234       G_TYPE_INSTANCE_GET_PRIVATE (empathy_contact_selector,
235       EMPATHY_TYPE_CONTACT_SELECTOR, EmpathyContactSelectorPriv);
236
237   empathy_contact_selector->priv = priv;
238
239   priv->dispose_run = FALSE;
240 }
241
242 static void
243 contact_selector_set_property (GObject *object,
244                                guint prop_id,
245                                const GValue *value,
246                                GParamSpec *pspec)
247 {
248   EmpathyContactSelectorPriv *priv = GET_PRIV (object);
249
250   switch (prop_id)
251     {
252       case PROP_CONTACT_LIST:
253         priv->contact_list = g_value_dup_object (value);
254         break;
255       default:
256         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
257         break;
258     }
259 }
260
261 static void
262 contact_selector_get_property (GObject *object,
263                                guint prop_id,
264                                GValue *value,
265                                GParamSpec *pspec)
266 {
267   EmpathyContactSelectorPriv *priv = GET_PRIV (object);
268
269   switch (prop_id)
270     {
271       case PROP_CONTACT_LIST:
272         g_value_set_object (value, priv->contact_list);
273         break;
274       default:
275         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
276         break;
277     }
278 }
279
280 static void
281 contact_selector_dispose (GObject *object)
282 {
283   EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (object);
284   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
285
286   if (priv->dispose_run)
287     return;
288
289   priv->dispose_run = TRUE;
290
291   if (priv->contact_list)
292     {
293       g_object_unref (priv->contact_list);
294       priv->contact_list = NULL;
295     }
296
297   if (priv->model)
298     {
299       g_object_unref (priv->model);
300       priv->model = NULL;
301     }
302
303   if (priv->store)
304     {
305       g_object_unref (priv->store);
306       priv->store = NULL;
307     }
308
309   (G_OBJECT_CLASS (empathy_contact_selector_parent_class)->dispose) (object);
310 }
311
312 static void
313 empathy_contact_selector_class_init (EmpathyContactSelectorClass *klass)
314 {
315   GObjectClass *object_class = G_OBJECT_CLASS (klass);
316   object_class->constructor = contact_selector_constructor;
317   object_class->dispose = contact_selector_dispose;
318   object_class->set_property = contact_selector_set_property;
319   object_class->get_property = contact_selector_get_property;
320   g_type_class_add_private (klass, sizeof (EmpathyContactSelectorPriv));
321
322   g_object_class_install_property (object_class, PROP_CONTACT_LIST,
323       g_param_spec_object ("contact-list", "contact list", "contact list",
324       EMPATHY_TYPE_CONTACT_LIST, G_PARAM_CONSTRUCT_ONLY |
325       G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
326 }
327
328 /* public methods */
329
330 GtkWidget *
331 empathy_contact_selector_new (EmpathyContactList *contact_list)
332 {
333   g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (contact_list), NULL);
334
335   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_CONTACT_SELECTOR,
336       "contact-list", contact_list, NULL));
337 }
338
339 EmpathyContact *
340 empathy_contact_selector_dup_selected (EmpathyContactSelector *selector)
341 {
342   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
343   EmpathyContact *contact = NULL;
344   GtkTreeIter iter;
345
346   g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR (selector), NULL);
347
348   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (selector), &iter))
349     return NULL;
350
351   gtk_tree_model_get (priv->model, &iter,
352       EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact, -1);
353
354   return contact;
355 }
356
357 typedef struct
358 {
359   EmpathyContactSelectorFilterFunc func;
360   gpointer user_data;
361 } FilterData;
362
363 static void
364 filter_data_free (gpointer data)
365 {
366   g_slice_free (FilterData, data);
367 }
368
369 static gboolean
370 contact_selector_filter_visible_func (GtkTreeModel *model,
371                                       GtkTreeIter *iter,
372                                       gpointer user_data)
373 {
374   EmpathyContact *contact;
375   gboolean visible = TRUE;
376   FilterData *data = (FilterData *) user_data;
377
378   gtk_tree_model_get (model, iter,
379       EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
380       -1);
381
382   if (contact != NULL)
383     {
384       visible = data->func(contact, data->user_data);
385
386       g_object_unref (contact);
387     }
388
389   return visible;
390 }
391
392 void
393 empathy_contact_selector_set_visible (EmpathyContactSelector *selector,
394                                       EmpathyContactSelectorFilterFunc func,
395                                       gpointer user_data)
396 {
397   EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
398   FilterData *data;
399
400   data = g_slice_new0 (FilterData);
401   data->func = func;
402   data->user_data = user_data;
403
404   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->model),
405       contact_selector_filter_visible_func, data, filter_data_free);
406
407   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->model));
408 }