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;
49 } EmpathyContactSelectorPriv;
51 static void contact_selector_manage_blank_contact (
52 EmpathyContactSelector *selector);
55 contact_selector_get_number_online_contacts (GtkTreeStore *store)
59 guint number_online_contacts = 0;
62 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &tmp_iter);
63 ok; ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &tmp_iter))
65 gtk_tree_model_get (GTK_TREE_MODEL (store),
66 &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE,
69 number_online_contacts++;
72 return number_online_contacts;
76 contact_selector_get_iter_for_blank_contact (GtkTreeStore *store,
77 GtkTreeIter *blank_iter)
80 EmpathyContact *tmp_contact;
81 gboolean is_present = FALSE;
84 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &tmp_iter);
85 ok; ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &tmp_iter))
87 gtk_tree_model_get (GTK_TREE_MODEL (store),
88 &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
90 if (tmp_contact == NULL)
92 *blank_iter = tmp_iter;
96 g_object_unref (tmp_contact);
103 contact_selector_add_blank_contact (EmpathyContactSelector *selector)
105 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
106 GtkTreeIter blank_iter;
108 gtk_tree_store_insert_with_values (
109 GTK_TREE_STORE (priv->store),&blank_iter, NULL, 0,
110 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, NULL,
111 EMPATHY_CONTACT_LIST_STORE_COL_NAME, (_("Select a contact")),
112 EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, FALSE, -1);
113 g_signal_handlers_block_by_func (selector,
114 contact_selector_manage_blank_contact, selector);
115 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (selector), &blank_iter);
116 g_signal_handlers_unblock_by_func (selector,
117 contact_selector_manage_blank_contact, selector);
121 contact_selector_remove_blank_contact (EmpathyContactSelector *selector)
123 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
124 GtkTreeIter blank_iter;
126 if (contact_selector_get_iter_for_blank_contact (
127 GTK_TREE_STORE (priv->store), &blank_iter))
128 gtk_tree_store_remove (GTK_TREE_STORE (priv->store), &blank_iter);
132 contact_selector_manage_sensitivity (EmpathyContactSelector *selector)
134 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
135 guint number_online_contacts;
137 number_online_contacts = contact_selector_get_number_online_contacts (
138 GTK_TREE_STORE (priv->store));
140 if (number_online_contacts != 0)
141 gtk_widget_set_sensitive (GTK_WIDGET (selector), TRUE);
143 gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
147 contact_selector_manage_blank_contact (EmpathyContactSelector *selector)
149 gboolean is_popup_shown;
151 g_object_get (selector, "popup-shown", &is_popup_shown, NULL);
155 contact_selector_remove_blank_contact (selector);
159 if (gtk_combo_box_get_active (GTK_COMBO_BOX (selector)) == -1)
161 contact_selector_add_blank_contact (selector);
165 contact_selector_remove_blank_contact (selector);
169 contact_selector_manage_sensitivity (selector);
173 contact_selector_constructor (GType type,
174 guint n_construct_params,
175 GObjectConstructParam *construct_params)
178 EmpathyContactSelector *contact_selector;
179 EmpathyContactSelectorPriv *priv;
180 GtkCellLayout *cell_layout;
181 GtkCellRenderer *renderer;
183 object = G_OBJECT_CLASS (empathy_contact_selector_parent_class)->constructor
184 (type, n_construct_params, construct_params);
185 priv = GET_PRIV (object);
186 contact_selector = EMPATHY_CONTACT_SELECTOR (object);
187 cell_layout = GTK_CELL_LAYOUT (object);
189 priv->store = empathy_contact_list_store_new (priv->contact_list);
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);
247 case PROP_CONTACT_LIST:
248 priv->contact_list = 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);
266 case PROP_CONTACT_LIST:
267 g_value_set_object (value, priv->contact_list);
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;
286 if (priv->contact_list)
288 g_object_unref (priv->contact_list);
289 priv->contact_list = NULL;
294 g_object_unref (priv->store);
298 (G_OBJECT_CLASS (empathy_contact_selector_parent_class)->dispose) (object);
302 empathy_contact_selector_class_init (EmpathyContactSelectorClass *klass)
304 GObjectClass *object_class = G_OBJECT_CLASS (klass);
305 object_class->constructor = contact_selector_constructor;
306 object_class->dispose = contact_selector_dispose;
307 object_class->set_property = contact_selector_set_property;
308 object_class->get_property = contact_selector_get_property;
309 g_type_class_add_private (klass, sizeof (EmpathyContactSelectorPriv));
311 g_object_class_install_property (object_class, PROP_CONTACT_LIST,
312 g_param_spec_object ("contact-list", "contact list", "contact list",
313 EMPATHY_TYPE_CONTACT_LIST, G_PARAM_CONSTRUCT_ONLY |
314 G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
320 empathy_contact_selector_new (EmpathyContactList *contact_list)
322 g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (contact_list), NULL);
324 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_CONTACT_SELECTOR,
325 "contact-list", contact_list, NULL));
329 empathy_contact_selector_dup_selected (EmpathyContactSelector *selector)
331 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
332 EmpathyContact *contact = NULL;
335 g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR (selector), NULL);
337 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (selector), &iter))
340 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
341 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact, -1);