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 (GtkTreeStore *model,
78 GtkTreeIter *blank_iter)
81 EmpathyContact *tmp_contact;
82 gboolean is_present = FALSE;
85 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &tmp_iter);
86 ok; ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &tmp_iter))
88 gtk_tree_model_get (GTK_TREE_MODEL (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, 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);
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));
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);
127 contact_selector_remove_blank_contact (EmpathyContactSelector *selector)
129 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
130 GtkTreeIter blank_iter;
132 if (contact_selector_get_iter_for_blank_contact
133 (GTK_TREE_STORE (priv->store), &blank_iter))
134 gtk_tree_store_remove (GTK_TREE_STORE (priv->store), &blank_iter);
138 contact_selector_manage_sensitivity (EmpathyContactSelector *selector)
140 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
141 guint number_online_contacts;
143 number_online_contacts = contact_selector_get_number_online_contacts (priv->model);
145 if (number_online_contacts != 0)
146 gtk_widget_set_sensitive (GTK_WIDGET (selector), TRUE);
148 gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
152 contact_selector_manage_blank_contact (EmpathyContactSelector *selector)
154 gboolean is_popup_shown;
156 g_object_get (selector, "popup-shown", &is_popup_shown, NULL);
160 contact_selector_remove_blank_contact (selector);
164 if (gtk_combo_box_get_active (GTK_COMBO_BOX (selector)) == -1)
166 contact_selector_add_blank_contact (selector);
170 contact_selector_remove_blank_contact (selector);
174 contact_selector_manage_sensitivity (selector);
178 contact_selector_constructor (GType type,
179 guint n_construct_params,
180 GObjectConstructParam *construct_params)
183 EmpathyContactSelector *contact_selector;
184 EmpathyContactSelectorPriv *priv;
185 GtkCellLayout *cell_layout;
186 GtkCellRenderer *renderer;
188 object = G_OBJECT_CLASS (empathy_contact_selector_parent_class)->constructor
189 (type, n_construct_params, construct_params);
190 priv = GET_PRIV (object);
191 contact_selector = EMPATHY_CONTACT_SELECTOR (object);
192 cell_layout = GTK_CELL_LAYOUT (object);
194 priv->store = empathy_contact_list_store_new (priv->contact_list);
196 g_object_set (priv->store, "is-compact", TRUE, "show-avatars", FALSE,
197 "show-offline", FALSE, "show-groups", FALSE,
198 "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME, NULL);
200 g_signal_connect_swapped (priv->store, "row-changed",
201 G_CALLBACK (contact_selector_manage_sensitivity),
203 g_signal_connect_swapped (contact_selector, "changed",
204 G_CALLBACK (contact_selector_manage_blank_contact),
206 g_signal_connect_swapped (contact_selector, "notify::popup-shown",
207 G_CALLBACK (contact_selector_manage_blank_contact),
210 priv->model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store), NULL);
212 gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector), priv->model);
213 gtk_widget_set_sensitive (GTK_WIDGET (contact_selector), FALSE);
215 renderer = gtk_cell_renderer_pixbuf_new ();
216 gtk_cell_layout_pack_start (cell_layout, renderer, FALSE);
217 gtk_cell_layout_set_attributes (cell_layout, renderer,
218 "icon-name", EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL);
220 renderer = gtk_cell_renderer_text_new ();
221 gtk_cell_layout_pack_start (cell_layout, renderer, TRUE);
222 gtk_cell_layout_set_attributes (cell_layout, renderer,
223 "text", EMPATHY_CONTACT_LIST_STORE_COL_NAME, NULL);
225 contact_selector_manage_blank_contact (contact_selector);
226 contact_selector_manage_sensitivity (contact_selector);
232 empathy_contact_selector_init (EmpathyContactSelector *empathy_contact_selector)
234 EmpathyContactSelectorPriv *priv =
235 G_TYPE_INSTANCE_GET_PRIVATE (empathy_contact_selector,
236 EMPATHY_TYPE_CONTACT_SELECTOR, EmpathyContactSelectorPriv);
238 empathy_contact_selector->priv = priv;
240 priv->dispose_run = FALSE;
244 contact_selector_set_property (GObject *object,
249 EmpathyContactSelectorPriv *priv = GET_PRIV (object);
253 case PROP_CONTACT_LIST:
254 priv->contact_list = g_value_dup_object (value);
257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
263 contact_selector_get_property (GObject *object,
268 EmpathyContactSelectorPriv *priv = GET_PRIV (object);
272 case PROP_CONTACT_LIST:
273 g_value_set_object (value, priv->contact_list);
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282 contact_selector_dispose (GObject *object)
284 EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (object);
285 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
287 if (priv->dispose_run)
290 priv->dispose_run = TRUE;
292 if (priv->contact_list)
294 g_object_unref (priv->contact_list);
295 priv->contact_list = NULL;
300 g_object_unref (priv->model);
306 g_object_unref (priv->store);
310 (G_OBJECT_CLASS (empathy_contact_selector_parent_class)->dispose) (object);
314 empathy_contact_selector_class_init (EmpathyContactSelectorClass *klass)
316 GObjectClass *object_class = G_OBJECT_CLASS (klass);
317 object_class->constructor = contact_selector_constructor;
318 object_class->dispose = contact_selector_dispose;
319 object_class->set_property = contact_selector_set_property;
320 object_class->get_property = contact_selector_get_property;
321 g_type_class_add_private (klass, sizeof (EmpathyContactSelectorPriv));
323 g_object_class_install_property (object_class, PROP_CONTACT_LIST,
324 g_param_spec_object ("contact-list", "contact list", "contact list",
325 EMPATHY_TYPE_CONTACT_LIST, G_PARAM_CONSTRUCT_ONLY |
326 G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
332 empathy_contact_selector_new (EmpathyContactList *contact_list)
334 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (contact_list), NULL);
336 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_CONTACT_SELECTOR,
337 "contact-list", contact_list, NULL));
341 empathy_contact_selector_dup_selected (EmpathyContactSelector *selector)
343 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
344 EmpathyContact *contact = NULL;
347 g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR (selector), NULL);
349 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (selector), &iter))
352 gtk_tree_model_get (priv->model, &iter,
353 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact, -1);
360 EmpathyContactSelectorFilterFunc func;
365 filter_data_free (gpointer data)
367 g_slice_free (FilterData, data);
371 contact_selector_filter_visible_func (GtkTreeModel *model,
375 EmpathyContact *contact;
376 gboolean visible = TRUE;
377 FilterData *data = (FilterData *) user_data;
379 gtk_tree_model_get (model, iter,
380 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
385 visible = data->func(contact, data->user_data);
387 g_object_unref (contact);
394 empathy_contact_selector_set_visible (EmpathyContactSelector *selector,
395 EmpathyContactSelectorFilterFunc func,
398 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
401 data = g_slice_new0 (FilterData);
403 data->user_data = user_data;
405 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->model),
406 contact_selector_filter_visible_func, data, filter_data_free);
408 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->model));