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-lib.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"
35 * SECTION:empathy-contact-selector
36 * @title:EmpathyContactSelector
37 * @short_description: A widget used to choose from a list of contacts.
38 * @include: libempathy-gtk/empathy-contact-selector.h
40 * #EmpathyContactSelector is a widget which extends #GtkComboBox to provide
41 * a chooser of available contacts.
45 * EmpathyContactSelector:
46 * @parent: parent object
48 * Widget which extends #GtkComboBox to provide a chooser of available contacts.
51 G_DEFINE_TYPE (EmpathyContactSelector
, empathy_contact_selector
,
60 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactSelector)
63 EmpathyContactList
*contact_list
;
64 EmpathyContactListStore
*store
;
67 } EmpathyContactSelectorPriv
;
69 static void contact_selector_manage_blank_contact (
70 EmpathyContactSelector
*selector
);
73 contact_selector_get_number_online_contacts (GtkTreeModel
*model
)
77 guint number_online_contacts
= 0;
80 for (ok
= gtk_tree_model_get_iter_first (model
, &tmp_iter
);
81 ok
; ok
= gtk_tree_model_iter_next (model
, &tmp_iter
))
83 gtk_tree_model_get (model
,
84 &tmp_iter
, EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE
,
87 number_online_contacts
++;
90 return number_online_contacts
;
94 contact_selector_get_iter_for_blank_contact (GtkTreeStore
*model
,
95 GtkTreeIter
*blank_iter
)
98 EmpathyContact
*tmp_contact
;
99 gboolean is_present
= FALSE
;
102 for (ok
= gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model
), &tmp_iter
);
103 ok
; ok
= gtk_tree_model_iter_next (GTK_TREE_MODEL (model
), &tmp_iter
))
105 gtk_tree_model_get (GTK_TREE_MODEL (model
),
106 &tmp_iter
, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT
,
108 if (tmp_contact
== NULL
)
110 *blank_iter
= tmp_iter
;
114 g_object_unref (tmp_contact
);
121 contact_selector_add_blank_contact (EmpathyContactSelector
*selector
)
123 EmpathyContactSelectorPriv
*priv
= GET_PRIV (selector
);
124 GtkTreeIter blank_iter
, iter
;
126 gtk_tree_store_insert_with_values (
127 GTK_TREE_STORE (priv
->store
), &blank_iter
, NULL
, 0,
128 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT
, NULL
,
129 EMPATHY_CONTACT_LIST_STORE_COL_NAME
, (_("Select a contact")),
130 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE
, FALSE
, -1);
132 /* look up blank_iter in the filter model */
133 g_return_if_fail (gtk_tree_model_filter_convert_child_iter_to_iter (
134 GTK_TREE_MODEL_FILTER (priv
->model
), &iter
, &blank_iter
));
136 g_signal_handlers_block_by_func (selector
,
137 contact_selector_manage_blank_contact
, selector
);
138 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (selector
), &iter
);
139 g_signal_handlers_unblock_by_func (selector
,
140 contact_selector_manage_blank_contact
, selector
);
144 contact_selector_remove_blank_contact (EmpathyContactSelector
*selector
)
146 EmpathyContactSelectorPriv
*priv
= GET_PRIV (selector
);
147 GtkTreeIter blank_iter
;
149 if (contact_selector_get_iter_for_blank_contact
150 (GTK_TREE_STORE (priv
->store
), &blank_iter
))
151 gtk_tree_store_remove (GTK_TREE_STORE (priv
->store
), &blank_iter
);
155 contact_selector_manage_sensitivity (EmpathyContactSelector
*selector
)
157 EmpathyContactSelectorPriv
*priv
= GET_PRIV (selector
);
158 guint number_online_contacts
;
160 number_online_contacts
= contact_selector_get_number_online_contacts (priv
->model
);
162 if (number_online_contacts
!= 0)
163 gtk_widget_set_sensitive (GTK_WIDGET (selector
), TRUE
);
165 gtk_widget_set_sensitive (GTK_WIDGET (selector
), FALSE
);
169 contact_selector_manage_blank_contact (EmpathyContactSelector
*selector
)
171 gboolean is_popup_shown
;
173 g_object_get (selector
, "popup-shown", &is_popup_shown
, NULL
);
177 contact_selector_remove_blank_contact (selector
);
181 if (gtk_combo_box_get_active (GTK_COMBO_BOX (selector
)) == -1)
183 contact_selector_add_blank_contact (selector
);
187 contact_selector_remove_blank_contact (selector
);
191 contact_selector_manage_sensitivity (selector
);
195 contact_selector_constructor (GType type
,
196 guint n_construct_params
,
197 GObjectConstructParam
*construct_params
)
200 EmpathyContactSelector
*contact_selector
;
201 EmpathyContactSelectorPriv
*priv
;
202 GtkCellLayout
*cell_layout
;
203 GtkCellRenderer
*renderer
;
205 object
= G_OBJECT_CLASS (empathy_contact_selector_parent_class
)->constructor
206 (type
, n_construct_params
, construct_params
);
207 priv
= GET_PRIV (object
);
208 contact_selector
= EMPATHY_CONTACT_SELECTOR (object
);
209 cell_layout
= GTK_CELL_LAYOUT (object
);
211 priv
->store
= empathy_contact_list_store_new (priv
->contact_list
);
213 g_object_set (priv
->store
, "is-compact", TRUE
, "show-avatars", FALSE
,
214 "show-offline", FALSE
, "show-groups", FALSE
, "show-protocols", FALSE
,
215 "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME
, NULL
);
217 g_signal_connect_swapped (priv
->store
, "row-changed",
218 G_CALLBACK (contact_selector_manage_sensitivity
),
220 g_signal_connect_swapped (contact_selector
, "changed",
221 G_CALLBACK (contact_selector_manage_blank_contact
),
223 g_signal_connect_swapped (contact_selector
, "notify::popup-shown",
224 G_CALLBACK (contact_selector_manage_blank_contact
),
227 priv
->model
= gtk_tree_model_filter_new (GTK_TREE_MODEL (priv
->store
), NULL
);
229 gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector
), priv
->model
);
230 gtk_widget_set_sensitive (GTK_WIDGET (contact_selector
), FALSE
);
232 renderer
= gtk_cell_renderer_pixbuf_new ();
233 gtk_cell_layout_pack_start (cell_layout
, renderer
, FALSE
);
234 gtk_cell_layout_set_attributes (cell_layout
, renderer
,
235 "pixbuf", EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS
, NULL
);
237 renderer
= gtk_cell_renderer_text_new ();
238 gtk_cell_layout_pack_start (cell_layout
, renderer
, TRUE
);
239 gtk_cell_layout_set_attributes (cell_layout
, renderer
,
240 "text", EMPATHY_CONTACT_LIST_STORE_COL_NAME
, NULL
);
242 contact_selector_manage_blank_contact (contact_selector
);
243 contact_selector_manage_sensitivity (contact_selector
);
249 empathy_contact_selector_init (EmpathyContactSelector
*empathy_contact_selector
)
251 EmpathyContactSelectorPriv
*priv
=
252 G_TYPE_INSTANCE_GET_PRIVATE (empathy_contact_selector
,
253 EMPATHY_TYPE_CONTACT_SELECTOR
, EmpathyContactSelectorPriv
);
255 empathy_contact_selector
->priv
= priv
;
257 priv
->dispose_run
= FALSE
;
261 contact_selector_set_property (GObject
*object
,
266 EmpathyContactSelectorPriv
*priv
= GET_PRIV (object
);
270 case PROP_CONTACT_LIST
:
271 priv
->contact_list
= g_value_dup_object (value
);
274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
280 contact_selector_get_property (GObject
*object
,
285 EmpathyContactSelectorPriv
*priv
= GET_PRIV (object
);
289 case PROP_CONTACT_LIST
:
290 g_value_set_object (value
, priv
->contact_list
);
293 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
299 contact_selector_dispose (GObject
*object
)
301 EmpathyContactSelector
*selector
= EMPATHY_CONTACT_SELECTOR (object
);
302 EmpathyContactSelectorPriv
*priv
= GET_PRIV (selector
);
304 if (priv
->dispose_run
)
307 priv
->dispose_run
= TRUE
;
309 if (priv
->contact_list
)
311 g_object_unref (priv
->contact_list
);
312 priv
->contact_list
= NULL
;
317 g_object_unref (priv
->model
);
323 g_object_unref (priv
->store
);
327 (G_OBJECT_CLASS (empathy_contact_selector_parent_class
)->dispose
) (object
);
331 empathy_contact_selector_class_init (EmpathyContactSelectorClass
*klass
)
333 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
334 object_class
->constructor
= contact_selector_constructor
;
335 object_class
->dispose
= contact_selector_dispose
;
336 object_class
->set_property
= contact_selector_set_property
;
337 object_class
->get_property
= contact_selector_get_property
;
338 g_type_class_add_private (klass
, sizeof (EmpathyContactSelectorPriv
));
341 * EmpathyContactSelector:contact-list:
343 * An #EmpathyContactList containing the contacts for the
344 * #EmpathyContactSelector.
346 g_object_class_install_property (object_class
, PROP_CONTACT_LIST
,
347 g_param_spec_object ("contact-list", "contact list", "contact list",
348 EMPATHY_TYPE_CONTACT_LIST
, G_PARAM_CONSTRUCT_ONLY
|
349 G_PARAM_READWRITE
| G_PARAM_STATIC_NICK
| G_PARAM_STATIC_BLURB
));
353 * empathy_contact_selector_new:
354 * @contact_list: an #EmpathyContactList containing the contacts to list in
355 * the contact selector
357 * Creates a new #EmpathyContactSelector.
359 * Return value: A new #EmpathyContactSelector
362 empathy_contact_selector_new (EmpathyContactList
*contact_list
)
364 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (contact_list
), NULL
);
366 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_CONTACT_SELECTOR
,
367 "contact-list", contact_list
, NULL
));
371 * empathy_contact_selector_dup_selected:
372 * @selector: An #EmpathyContactSelector
374 * Returns a new reference to the contact which is currently selected in
375 * @selector, or %NULL if there is no contact selected. The returned contact
376 * should be unrefed with g_object_unref() when finished with.
378 * Return value: A new reference to the contact currently selected, or %NULL
381 empathy_contact_selector_dup_selected (EmpathyContactSelector
*selector
)
383 EmpathyContactSelectorPriv
*priv
= GET_PRIV (selector
);
384 EmpathyContact
*contact
= NULL
;
387 g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR (selector
), NULL
);
389 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (selector
), &iter
))
392 gtk_tree_model_get (priv
->model
, &iter
,
393 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT
, &contact
, -1);
400 EmpathyContactSelectorFilterFunc func
;
405 filter_data_free (gpointer data
)
407 g_slice_free (FilterData
, data
);
411 contact_selector_filter_visible_func (GtkTreeModel
*model
,
415 EmpathyContact
*contact
;
416 gboolean visible
= TRUE
;
417 FilterData
*data
= (FilterData
*) user_data
;
419 gtk_tree_model_get (model
, iter
,
420 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT
, &contact
,
425 visible
= data
->func (contact
, data
->user_data
);
427 g_object_unref (contact
);
434 * empathy_contact_selector_set_visible:
435 * @selector: an #EmpathyContactSelector
436 * @func: an #EmpathyContactSelectorFilterFunc to filter the contacts
437 * @user_data: data to pass to @func or %NULL
439 * Sets a filter on the @selector so only contacts that return %TRUE
440 * when passed into @func are visible.
442 * A typical usage for this function would be to only show contacts that
443 * can send or receive files. In this case, one could use the
444 * empathy_contact_can_send_files() function
447 empathy_contact_selector_set_visible (EmpathyContactSelector
*selector
,
448 EmpathyContactSelectorFilterFunc func
,
451 EmpathyContactSelectorPriv
*priv
= GET_PRIV (selector
);
454 data
= g_slice_new0 (FilterData
);
456 data
->user_data
= user_data
;
458 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv
->model
),
459 contact_selector_filter_visible_func
, data
, filter_data_free
);
461 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv
->model
));
465 * EmpathyContactSelectorFilterFunc:
466 * @contact: an #EmpathyContact
467 * @user_data: user data or %NULL
469 * A function which decides whether the contact indicated by @contact
472 * Return value: whether @contact is visible