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 EmpathyContactListStore *store;
47 } EmpathyContactSelectorPriv;
49 static void contact_selector_changed_cb (GtkComboBox *widget, gpointer data);
52 empathy_contact_selector_get_selected (EmpathyContactSelector *selector)
54 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
55 EmpathyContact *contact = NULL;
58 g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR (selector), NULL);
60 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (selector), &iter))
63 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
64 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact, -1);
70 contact_selector_get_number_online_contacts (GtkTreeStore *store)
75 guint number_online_contacts = 0;
77 path = gtk_tree_path_new_first ();
79 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &tmp_iter, path))
83 gtk_tree_model_get (GTK_TREE_MODEL (store),
84 &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE,
87 number_online_contacts++;
88 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &tmp_iter));
91 gtk_tree_path_free (path);
93 return number_online_contacts;
97 contact_selector_get_iter_for_blank_contact (GtkTreeStore *store,
98 GtkTreeIter *blank_iter)
101 GtkTreeIter tmp_iter;
102 EmpathyContact *tmp_contact;
103 gboolean is_present = FALSE;
105 path = gtk_tree_path_new_first ();
107 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &tmp_iter, path))
111 gtk_tree_model_get (GTK_TREE_MODEL (store),
112 &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
114 if (tmp_contact == NULL)
116 *blank_iter = tmp_iter;
120 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &tmp_iter));
123 gtk_tree_path_free (path);
129 contact_selector_add_blank_contact (EmpathyContactSelector *selector)
131 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
132 GtkTreeIter blank_iter;
134 gtk_tree_store_insert (GTK_TREE_STORE (priv->store), &blank_iter, NULL, 0);
135 gtk_tree_store_set (GTK_TREE_STORE (priv->store), &blank_iter,
136 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, NULL,
137 EMPATHY_CONTACT_LIST_STORE_COL_NAME, (_("Select a contact")),
138 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, FALSE, -1);
139 g_signal_handlers_block_by_func (selector,
140 contact_selector_changed_cb, NULL);
141 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (selector), &blank_iter);
142 g_signal_handlers_unblock_by_func (selector,
143 contact_selector_changed_cb, NULL);
147 contact_selector_remove_blank_contact (EmpathyContactSelector *selector)
149 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
150 GtkTreeIter blank_iter;
152 if (contact_selector_get_iter_for_blank_contact (
153 GTK_TREE_STORE (priv->store), &blank_iter))
154 gtk_tree_store_remove (GTK_TREE_STORE (priv->store), &blank_iter);
158 contact_selector_manage_sensitivity (EmpathyContactSelector *selector)
160 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
162 /* FIXME - make this work when offline contacts are shown.
163 * The following value needs to be the number of entries shown
164 * excluding the blank entry (if present).
166 guint number_online_contacts = contact_selector_get_number_online_contacts (
167 GTK_TREE_STORE (priv->store));
169 if (number_online_contacts)
170 gtk_widget_set_sensitive (GTK_WIDGET (selector), TRUE);
172 gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
176 contact_selector_manage_blank_contact (EmpathyContactSelector *selector)
178 gboolean is_popup_shown;
180 g_object_get (selector, "popup-shown", &is_popup_shown, NULL);
184 contact_selector_remove_blank_contact (selector);
188 if (gtk_combo_box_get_active (GTK_COMBO_BOX (selector)) == -1)
190 contact_selector_add_blank_contact (selector);
194 contact_selector_remove_blank_contact (selector);
198 contact_selector_manage_sensitivity (selector);
202 contact_selector_notify_popup_shown_cb (GtkComboBox *widget,
203 GParamSpec *property,
206 EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (widget);
208 contact_selector_manage_blank_contact (selector);
212 contact_selector_changed_cb (GtkComboBox *widget,
215 EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (widget);
217 contact_selector_manage_blank_contact (selector);
221 contact_selector_store_row_changed_cb (EmpathyContactListStore *empathy_store,
222 GtkTreePath *empathy_path,
223 GtkTreeIter *empathy_iter,
226 EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (data);
228 contact_selector_manage_sensitivity (selector);
233 contact_selector_constructor (GType type,
234 guint n_construct_params,
235 GObjectConstructParam *construct_params)
238 G_OBJECT_CLASS (empathy_contact_selector_parent_class)->constructor (
239 type, n_construct_params, construct_params);
240 EmpathyContactSelector *contact_selector = EMPATHY_CONTACT_SELECTOR (object);
241 EmpathyContactSelectorPriv *priv = GET_PRIV (contact_selector);
242 GtkCellRenderer *renderer;
244 g_object_set (priv->store, "is-compact", TRUE, "show-avatars", FALSE,
245 "show-offline", FALSE, "show-groups", FALSE,
246 "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME, NULL);
248 g_signal_connect (priv->store, "row-changed",
249 G_CALLBACK (contact_selector_store_row_changed_cb),
250 (gpointer) contact_selector);
251 g_signal_connect (GTK_COMBO_BOX (contact_selector), "changed",
252 G_CALLBACK (contact_selector_changed_cb), NULL);
253 g_signal_connect (GTK_COMBO_BOX (contact_selector), "notify::popup-shown",
254 G_CALLBACK (contact_selector_notify_popup_shown_cb), NULL);
256 gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector),
257 GTK_TREE_MODEL (priv->store));
258 gtk_widget_set_sensitive (GTK_WIDGET (contact_selector), FALSE);
260 renderer = gtk_cell_renderer_pixbuf_new ();
261 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (contact_selector),
263 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (contact_selector), renderer,
264 "icon-name", EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL);
266 renderer = gtk_cell_renderer_text_new ();
267 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (contact_selector),
269 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (contact_selector), renderer,
270 "text", EMPATHY_CONTACT_LIST_STORE_COL_NAME, NULL);
272 contact_selector_manage_blank_contact (contact_selector);
273 contact_selector_manage_sensitivity (contact_selector);
275 object = G_OBJECT (contact_selector);
280 empathy_contact_selector_init (EmpathyContactSelector *empathy_contact_selector)
282 EmpathyContactSelectorPriv *priv =
283 G_TYPE_INSTANCE_GET_PRIVATE (empathy_contact_selector,
284 EMPATHY_TYPE_CONTACT_SELECTOR, EmpathyContactSelectorPriv);
286 empathy_contact_selector->priv = priv;
290 contact_selector_set_property (GObject *object,
295 EmpathyContactSelector *contact_selector = EMPATHY_CONTACT_SELECTOR (object);
296 EmpathyContactSelectorPriv *priv = GET_PRIV (contact_selector);
301 priv->store = g_value_dup_object (value);
304 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
310 contact_selector_get_property (GObject *object,
315 EmpathyContactSelector *contact_selector = EMPATHY_CONTACT_SELECTOR (object);
316 EmpathyContactSelectorPriv *priv = GET_PRIV (contact_selector);
321 g_value_set_object (value, priv->store);
324 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
330 contact_selector_dispose (GObject *object)
332 EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (object);
333 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
337 g_object_unref (priv->store);
341 (G_OBJECT_CLASS (empathy_contact_selector_parent_class)->dispose) (object);
345 empathy_contact_selector_class_init (EmpathyContactSelectorClass *klass)
347 GObjectClass *object_class = G_OBJECT_CLASS (klass);
348 object_class->constructor = contact_selector_constructor;
349 object_class->dispose = contact_selector_dispose;
350 object_class->set_property = contact_selector_set_property;
351 object_class->get_property = contact_selector_get_property;
352 g_type_class_add_private (klass, sizeof (EmpathyContactSelectorPriv));
354 g_object_class_install_property (object_class, PROP_STORE,
355 g_param_spec_object ("store", "store", "store",
356 EMPATHY_TYPE_CONTACT_LIST_STORE, G_PARAM_CONSTRUCT_ONLY |
357 G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
360 EmpathyContactSelector *
361 empathy_contact_selector_new (EmpathyContactListStore *store)
363 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), NULL);
365 return g_object_new (EMPATHY_TYPE_CONTACT_SELECTOR, "store", store, NULL);