2 * Copyright (C) 2007 Marco Barisione <marco@barisione.org>
3 * Copyright (C) 2008 Collabora Ltd.
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.
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.
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
19 * Authors: Marco Barisione <marco@barisione.org>
20 * Elliot Fairweather <elliot.fairweather@collabora.co.uk>
25 #include <glib/gi18n.h>
28 #include <libempathy/empathy-contact.h>
29 #include <libempathy-gtk/empathy-contact-list-store.h>
30 #include <libempathy/empathy-utils.h>
32 #include "empathy-contact-selector.h"
34 G_DEFINE_TYPE (EmpathyContactSelector, empathy_contact_selector,
43 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactSelector)
46 EmpathyContactList *contact_list;
47 EmpathyContactListStore *store;
50 } EmpathyContactSelectorPriv;
52 static void contact_selector_manage_blank_contact (
53 EmpathyContactSelector *selector);
56 contact_selector_get_number_online_contacts (GtkTreeModel *model)
60 guint number_online_contacts = 0;
63 for (ok = gtk_tree_model_get_iter_first (model, &tmp_iter);
64 ok; ok = gtk_tree_model_iter_next (model, &tmp_iter))
66 gtk_tree_model_get (model,
67 &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE,
70 number_online_contacts++;
73 return number_online_contacts;
77 contact_selector_get_iter_for_blank_contact (GtkTreeModel *model,
78 GtkTreeIter *blank_iter)
81 EmpathyContact *tmp_contact;
82 gboolean is_present = FALSE;
85 for (ok = gtk_tree_model_get_iter_first (model, &tmp_iter);
86 ok; ok = gtk_tree_model_iter_next (model, &tmp_iter))
88 gtk_tree_model_get (model,
89 &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
91 if (tmp_contact == NULL)
93 *blank_iter = tmp_iter;
97 g_object_unref (tmp_contact);
104 contact_selector_add_blank_contact (EmpathyContactSelector *selector)
106 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
107 GtkTreeIter blank_iter;
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);
122 contact_selector_remove_blank_contact (EmpathyContactSelector *selector)
124 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
125 GtkTreeIter blank_iter;
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);
132 contact_selector_manage_sensitivity (EmpathyContactSelector *selector)
134 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
135 guint number_online_contacts;
137 number_online_contacts = contact_selector_get_number_online_contacts (priv->model);
139 if (number_online_contacts != 0)
140 gtk_widget_set_sensitive (GTK_WIDGET (selector), TRUE);
142 gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
146 contact_selector_manage_blank_contact (EmpathyContactSelector *selector)
148 gboolean is_popup_shown;
150 g_object_get (selector, "popup-shown", &is_popup_shown, NULL);
154 contact_selector_remove_blank_contact (selector);
158 if (gtk_combo_box_get_active (GTK_COMBO_BOX (selector)) == -1)
160 contact_selector_add_blank_contact (selector);
164 contact_selector_remove_blank_contact (selector);
168 contact_selector_manage_sensitivity (selector);
172 contact_selector_constructor (GType type,
173 guint n_construct_params,
174 GObjectConstructParam *construct_params)
177 EmpathyContactSelector *contact_selector;
178 EmpathyContactSelectorPriv *priv;
179 GtkCellLayout *cell_layout;
180 GtkCellRenderer *renderer;
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);
188 priv->store = empathy_contact_list_store_new (priv->contact_list);
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);
194 g_signal_connect_swapped (priv->store, "row-changed",
195 G_CALLBACK (contact_selector_manage_sensitivity),
197 g_signal_connect_swapped (contact_selector, "changed",
198 G_CALLBACK (contact_selector_manage_blank_contact),
200 g_signal_connect_swapped (contact_selector, "notify::popup-shown",
201 G_CALLBACK (contact_selector_manage_blank_contact),
204 priv->model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store), NULL);
206 gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector), priv->model);
207 gtk_widget_set_sensitive (GTK_WIDGET (contact_selector), FALSE);
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);
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);
219 contact_selector_manage_blank_contact (contact_selector);
220 contact_selector_manage_sensitivity (contact_selector);
226 empathy_contact_selector_init (EmpathyContactSelector *empathy_contact_selector)
228 EmpathyContactSelectorPriv *priv =
229 G_TYPE_INSTANCE_GET_PRIVATE (empathy_contact_selector,
230 EMPATHY_TYPE_CONTACT_SELECTOR, EmpathyContactSelectorPriv);
232 empathy_contact_selector->priv = priv;
234 priv->dispose_run = FALSE;
238 contact_selector_set_property (GObject *object,
243 EmpathyContactSelectorPriv *priv = GET_PRIV (object);
247 case PROP_CONTACT_LIST:
248 priv->contact_list = g_value_dup_object (value);
251 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
257 contact_selector_get_property (GObject *object,
262 EmpathyContactSelectorPriv *priv = GET_PRIV (object);
266 case PROP_CONTACT_LIST:
267 g_value_set_object (value, priv->contact_list);
270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
276 contact_selector_dispose (GObject *object)
278 EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (object);
279 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
281 if (priv->dispose_run)
284 priv->dispose_run = TRUE;
286 if (priv->contact_list)
288 g_object_unref (priv->contact_list);
289 priv->contact_list = NULL;
294 g_object_unref (priv->model);
300 g_object_unref (priv->store);
304 (G_OBJECT_CLASS (empathy_contact_selector_parent_class)->dispose) (object);
308 empathy_contact_selector_class_init (EmpathyContactSelectorClass *klass)
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));
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));
326 empathy_contact_selector_new (EmpathyContactList *contact_list)
328 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (contact_list), NULL);
330 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_CONTACT_SELECTOR,
331 "contact-list", contact_list, NULL));
335 empathy_contact_selector_dup_selected (EmpathyContactSelector *selector)
337 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
338 EmpathyContact *contact = NULL;
341 g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR (selector), NULL);
343 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (selector), &iter))
346 gtk_tree_model_get (priv->model, &iter,
347 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact, -1);
354 EmpathyContactSelectorFilterFunc func;
359 filter_data_free (gpointer data)
361 g_slice_free (FilterData, data);
365 contact_selector_filter_visible_func (GtkTreeModel *model,
369 EmpathyContact *contact;
370 gboolean visible = FALSE;
371 FilterData *data = (FilterData *) user_data;
373 gtk_tree_model_get (model, iter, 0, &contact, -1);
377 visible = data->func(contact, data->user_data);
379 g_object_unref (contact);
387 empathy_contact_selector_set_visible (EmpathyContactSelector *selector,
388 EmpathyContactSelectorFilterFunc func,
391 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
394 data = g_slice_new0 (FilterData);
396 data->user_data = user_data;
398 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->model),
399 contact_selector_filter_visible_func, data, filter_data_free);
401 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->model));