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;
48 } EmpathyContactSelectorPriv;
50 static void contact_selector_manage_blank_contact (
51 EmpathyContactSelector *selector);
54 contact_selector_get_number_online_contacts (GtkTreeStore *store)
58 guint number_online_contacts = 0;
61 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &tmp_iter);
62 ok; ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &tmp_iter))
64 gtk_tree_model_get (GTK_TREE_MODEL (store),
65 &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE,
68 number_online_contacts++;
71 return number_online_contacts;
75 contact_selector_get_iter_for_blank_contact (GtkTreeStore *store,
76 GtkTreeIter *blank_iter)
79 EmpathyContact *tmp_contact;
80 gboolean is_present = FALSE;
83 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &tmp_iter);
84 ok; ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &tmp_iter))
86 gtk_tree_model_get (GTK_TREE_MODEL (store),
87 &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
89 if (tmp_contact == NULL)
91 *blank_iter = tmp_iter;
95 g_object_unref (tmp_contact);
102 contact_selector_add_blank_contact (EmpathyContactSelector *selector)
104 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
105 GtkTreeIter blank_iter;
107 gtk_tree_store_insert_with_values (
108 GTK_TREE_STORE (priv->store),&blank_iter, NULL, 0,
109 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, NULL,
110 EMPATHY_CONTACT_LIST_STORE_COL_NAME, (_("Select a contact")),
111 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, FALSE, -1);
112 g_signal_handlers_block_by_func (selector,
113 contact_selector_manage_blank_contact, selector);
114 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (selector), &blank_iter);
115 g_signal_handlers_unblock_by_func (selector,
116 contact_selector_manage_blank_contact, selector);
120 contact_selector_remove_blank_contact (EmpathyContactSelector *selector)
122 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
123 GtkTreeIter blank_iter;
125 if (contact_selector_get_iter_for_blank_contact (
126 GTK_TREE_STORE (priv->store), &blank_iter))
127 gtk_tree_store_remove (GTK_TREE_STORE (priv->store), &blank_iter);
131 contact_selector_manage_sensitivity (EmpathyContactSelector *selector)
133 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
135 /* FIXME - make this work when offline contacts are shown.
136 * The following value needs to be the number of entries shown
137 * excluding the blank entry (if present).
139 guint number_online_contacts = contact_selector_get_number_online_contacts (
140 GTK_TREE_STORE (priv->store));
142 if (number_online_contacts != 0)
143 gtk_widget_set_sensitive (GTK_WIDGET (selector), TRUE);
145 gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
149 contact_selector_manage_blank_contact (EmpathyContactSelector *selector)
151 gboolean is_popup_shown;
153 g_object_get (selector, "popup-shown", &is_popup_shown, NULL);
157 contact_selector_remove_blank_contact (selector);
161 if (gtk_combo_box_get_active (GTK_COMBO_BOX (selector)) == -1)
163 contact_selector_add_blank_contact (selector);
167 contact_selector_remove_blank_contact (selector);
171 contact_selector_manage_sensitivity (selector);
175 contact_selector_constructor (GType type,
176 guint n_construct_params,
177 GObjectConstructParam *construct_params)
180 EmpathyContactSelector *contact_selector;
181 EmpathyContactSelectorPriv *priv;
182 GtkCellLayout *cell_layout;
183 GtkCellRenderer *renderer;
185 object = G_OBJECT_CLASS (empathy_contact_selector_parent_class)->constructor
186 (type, n_construct_params, construct_params);
187 priv = GET_PRIV (object);
188 contact_selector = EMPATHY_CONTACT_SELECTOR (object);
189 cell_layout = GTK_CELL_LAYOUT (object);
191 g_object_set (priv->store, "is-compact", TRUE, "show-avatars", FALSE,
192 "show-offline", FALSE, "show-groups", FALSE,
193 "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME, NULL);
195 g_signal_connect_swapped (priv->store, "row-changed",
196 G_CALLBACK (contact_selector_manage_sensitivity),
198 g_signal_connect_swapped (contact_selector, "changed",
199 G_CALLBACK (contact_selector_manage_blank_contact),
201 g_signal_connect_swapped (contact_selector, "notify::popup-shown",
202 G_CALLBACK (contact_selector_manage_blank_contact),
205 gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector),
206 GTK_TREE_MODEL (priv->store));
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);
248 priv->store = 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);
267 g_value_set_object (value, priv->store);
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;
288 g_object_unref (priv->store);
292 (G_OBJECT_CLASS (empathy_contact_selector_parent_class)->dispose) (object);
296 empathy_contact_selector_class_init (EmpathyContactSelectorClass *klass)
298 GObjectClass *object_class = G_OBJECT_CLASS (klass);
299 object_class->constructor = contact_selector_constructor;
300 object_class->dispose = contact_selector_dispose;
301 object_class->set_property = contact_selector_set_property;
302 object_class->get_property = contact_selector_get_property;
303 g_type_class_add_private (klass, sizeof (EmpathyContactSelectorPriv));
305 g_object_class_install_property (object_class, PROP_STORE,
306 g_param_spec_object ("store", "store", "store",
307 EMPATHY_TYPE_CONTACT_LIST_STORE, G_PARAM_CONSTRUCT_ONLY |
308 G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
314 empathy_contact_selector_new (EmpathyContactListStore *store)
316 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), NULL);
318 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_CONTACT_SELECTOR, "store", store, NULL));
322 empathy_contact_selector_get_selected (EmpathyContactSelector *selector)
324 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
325 EmpathyContact *contact = NULL;
328 g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR (selector), NULL);
330 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (selector), &iter))
333 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
334 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact, -1);