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 changed_cb (GtkComboBox *widget, gpointer data);
53 empathy_contact_selector_get_selected (EmpathyContactSelector *selector)
55 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
56 EmpathyContact *contact = NULL;
59 g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR (selector), NULL);
61 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (selector), &iter))
64 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
65 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact, -1);
72 get_number_online_contacts (GtkTreeStore *store)
77 guint number_online_contacts = 0;
79 path = gtk_tree_path_new_first ();
81 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &tmp_iter, path))
85 gtk_tree_model_get (GTK_TREE_MODEL (store),
86 &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE,
89 number_online_contacts++;
90 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &tmp_iter));
93 gtk_tree_path_free (path);
95 return number_online_contacts;
100 get_iter_for_blank_contact (GtkTreeStore *store,
101 GtkTreeIter *blank_iter)
104 GtkTreeIter tmp_iter;
105 EmpathyContact *tmp_contact;
106 gboolean is_present = FALSE;
108 path = gtk_tree_path_new_first ();
110 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &tmp_iter, path))
114 gtk_tree_model_get (GTK_TREE_MODEL (store),
115 &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
117 if (tmp_contact == NULL)
119 *blank_iter = tmp_iter;
123 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &tmp_iter));
126 gtk_tree_path_free (path);
133 add_blank_contact (EmpathyContactSelector *selector)
135 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
136 GtkTreeIter blank_iter;
138 gtk_tree_store_insert (GTK_TREE_STORE (priv->store), &blank_iter, NULL, 0);
139 gtk_tree_store_set (GTK_TREE_STORE (priv->store), &blank_iter,
140 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, NULL,
141 EMPATHY_CONTACT_LIST_STORE_COL_NAME, (_("Select a contact")),
142 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, FALSE, -1);
143 g_signal_handlers_block_by_func(selector, changed_cb, NULL);
144 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (selector), &blank_iter);
145 g_signal_handlers_unblock_by_func(selector, changed_cb, NULL);
150 remove_blank_contact (EmpathyContactSelector *selector)
152 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
153 GtkTreeIter blank_iter;
155 if (get_iter_for_blank_contact (GTK_TREE_STORE (priv->store), &blank_iter))
157 gtk_tree_store_remove (GTK_TREE_STORE (priv->store), &blank_iter);
163 manage_sensitivity (EmpathyContactSelector *selector)
165 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
167 /* FIXME - make this work when offline contacts are shown.
168 * The following value needs to be the number of entries shown
169 * excluding the blank entry (if present).
171 guint number_online_contacts =
172 get_number_online_contacts (GTK_TREE_STORE (priv->store));
174 if (number_online_contacts)
175 gtk_widget_set_sensitive (GTK_WIDGET (selector), TRUE);
177 gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
182 manage_blank_contact (EmpathyContactSelector *selector)
184 gboolean is_popup_shown;
186 g_object_get (selector, "popup-shown", &is_popup_shown, NULL);
190 remove_blank_contact (selector);
194 if (gtk_combo_box_get_active (GTK_COMBO_BOX (selector)) == -1)
196 add_blank_contact (selector);
200 remove_blank_contact (selector);
204 manage_sensitivity (selector);
209 notify_popup_shown_cb (GtkComboBox *widget,
210 GParamSpec *property,
213 EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (widget);
215 manage_blank_contact (selector);
220 changed_cb (GtkComboBox *widget,
223 EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (widget);
225 manage_blank_contact (selector);
230 empathy_store_row_changed_cb (EmpathyContactListStore *empathy_store,
231 GtkTreePath *empathy_path,
232 GtkTreeIter *empathy_iter,
235 EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (data);
237 manage_sensitivity (selector);
242 empathy_contact_selector_constructor (GType type,
243 guint n_construct_params,
244 GObjectConstructParam *construct_params)
247 G_OBJECT_CLASS (empathy_contact_selector_parent_class)->constructor (type,
248 n_construct_params, construct_params);
249 EmpathyContactSelector *contact_selector = EMPATHY_CONTACT_SELECTOR (object);
250 EmpathyContactSelectorPriv *priv = GET_PRIV (contact_selector);
251 GtkCellRenderer *renderer;
253 g_object_set (priv->store, "is-compact", TRUE, "show-avatars", FALSE,
254 "show-offline", FALSE, "show-groups", FALSE,
255 "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME, NULL);
257 g_signal_connect (priv->store, "row-changed",
258 G_CALLBACK (empathy_store_row_changed_cb), (gpointer) contact_selector);
259 g_signal_connect (GTK_COMBO_BOX (contact_selector), "changed",
260 G_CALLBACK (changed_cb), NULL);
261 g_signal_connect (GTK_COMBO_BOX (contact_selector), "notify::popup-shown",
262 G_CALLBACK (notify_popup_shown_cb), NULL);
264 gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector),
265 GTK_TREE_MODEL (priv->store));
266 gtk_widget_set_sensitive (GTK_WIDGET (contact_selector), FALSE);
268 renderer = gtk_cell_renderer_pixbuf_new ();
269 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (contact_selector),
271 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (contact_selector), renderer,
272 "icon-name", EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL);
274 renderer = gtk_cell_renderer_text_new ();
275 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (contact_selector),
277 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (contact_selector), renderer,
278 "text", EMPATHY_CONTACT_LIST_STORE_COL_NAME, NULL);
280 manage_blank_contact (contact_selector);
281 manage_sensitivity (contact_selector);
283 object = G_OBJECT (contact_selector);
289 empathy_contact_selector_init (EmpathyContactSelector *empathy_contact_selector)
291 EmpathyContactSelectorPriv *priv =
292 G_TYPE_INSTANCE_GET_PRIVATE (empathy_contact_selector,
293 EMPATHY_TYPE_CONTACT_SELECTOR, EmpathyContactSelectorPriv);
295 empathy_contact_selector->priv = priv;
300 empathy_contact_selector_set_property (GObject *object,
305 EmpathyContactSelector *contact_selector = EMPATHY_CONTACT_SELECTOR (object);
306 EmpathyContactSelectorPriv *priv = GET_PRIV (contact_selector);
311 priv->store = g_value_dup_object (value);
314 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
321 empathy_contact_selector_get_property (GObject *object,
326 EmpathyContactSelector *contact_selector = EMPATHY_CONTACT_SELECTOR (object);
327 EmpathyContactSelectorPriv *priv = GET_PRIV (contact_selector);
332 g_value_set_object (value, priv->store);
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342 empathy_contact_selector_dispose (GObject *object)
344 EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (object);
345 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
349 g_object_unref (priv->store);
353 (G_OBJECT_CLASS (empathy_contact_selector_parent_class)->dispose) (object);
358 empathy_contact_selector_class_init (EmpathyContactSelectorClass *klass)
360 GObjectClass *object_class = G_OBJECT_CLASS (klass);
361 object_class->constructor = empathy_contact_selector_constructor;
362 object_class->dispose = empathy_contact_selector_dispose;
363 object_class->set_property = empathy_contact_selector_set_property;
364 object_class->get_property = empathy_contact_selector_get_property;
365 g_type_class_add_private (klass, sizeof (EmpathyContactSelectorPriv));
367 g_object_class_install_property (object_class, PROP_STORE,
368 g_param_spec_object ("store", "store", "store",
369 EMPATHY_TYPE_CONTACT_LIST_STORE, G_PARAM_CONSTRUCT_ONLY |
370 G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
374 EmpathyContactSelector *
375 empathy_contact_selector_new (EmpathyContactListStore *store)
377 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_STORE (store), NULL);
379 return g_object_new (EMPATHY_TYPE_CONTACT_SELECTOR, "store", store, NULL);