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 "empathy-contact-selector.h"
32 G_DEFINE_TYPE (EmpathyContactSelector, empathy_contact_selector,
35 #define GET_PRIV(object) (G_TYPE_INSTANCE_GET_PRIVATE \
36 ((object), EMPATHY_TYPE_CONTACT_SELECTOR, EmpathyContactSelectorPriv))
44 typedef struct _EmpathyContactSelectorPriv EmpathyContactSelectorPriv;
46 struct _EmpathyContactSelectorPriv
48 EmpathyContactListStore *store;
49 gboolean is_blank_set;
52 static void changed_cb (GtkComboBox *widget, gpointer data);
53 static gboolean get_iter_for_contact (GtkTreeStore *store,
54 GtkTreeIter *list_iter, EmpathyContact *contact);
58 empathy_contact_selector_get_selected (EmpathyContactSelector *selector)
60 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
61 EmpathyContact *contact = NULL;
64 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (selector), &iter))
67 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
68 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact, -1);
75 set_blank_contact (EmpathyContactSelector *selector)
77 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
78 GtkTreeIter blank_iter;
80 gtk_tree_store_insert (GTK_TREE_STORE (priv->store), &blank_iter, NULL, 0);
81 gtk_tree_store_set (GTK_TREE_STORE (priv->store), &blank_iter,
82 EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, NULL,
83 EMPATHY_CONTACT_LIST_STORE_COL_NAME, ("Select a contact"), -1);
84 g_signal_handlers_block_by_func(selector, changed_cb, NULL);
85 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (selector), &blank_iter);
86 g_signal_handlers_unblock_by_func(selector, changed_cb, NULL);
87 priv->is_blank_set = TRUE;
92 notify_popup_shown_cb (GtkComboBox *widget,
96 EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (widget);
97 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
98 GtkTreeIter blank_iter;
101 g_object_get (widget, property->name, &shown, NULL);
105 if (get_iter_for_contact (GTK_TREE_STORE (priv->store), &blank_iter, NULL))
107 gtk_tree_store_remove (GTK_TREE_STORE (priv->store), &blank_iter);
108 priv->is_blank_set = FALSE;
113 if (gtk_combo_box_get_active (widget) == -1)
115 set_blank_contact (selector);
116 if (gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->store),
118 gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
125 changed_cb (GtkComboBox *widget,
128 EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (widget);
129 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
130 GtkTreeIter blank_iter;
133 g_object_get (widget, "popup-shown", &shown, NULL);
138 if (gtk_combo_box_get_active (widget) == -1)
140 set_blank_contact (selector);
141 if (gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->store),
143 gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
147 if (get_iter_for_contact (GTK_TREE_STORE (priv->store), &blank_iter, NULL))
149 gtk_tree_store_remove (GTK_TREE_STORE (priv->store), &blank_iter);
150 priv->is_blank_set = FALSE;
157 get_iter_for_contact (GtkTreeStore *store,
158 GtkTreeIter *list_iter,
159 EmpathyContact *contact)
162 GtkTreeIter tmp_iter;
163 EmpathyContact *tmp_contact;
164 gboolean found = FALSE;
166 /* Do a linear search to find the row with CONTACT_COL set to contact. */
167 path = gtk_tree_path_new_first ();
168 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &tmp_iter, path))
172 gtk_tree_model_get (GTK_TREE_MODEL (store),
173 &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
175 found = (tmp_contact == contact);
178 *list_iter = tmp_iter;
181 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store),
185 gtk_tree_path_free (path);
191 empathy_store_row_changed_cb (EmpathyContactListStore *empathy_store,
192 GtkTreePath *empathy_path,
193 GtkTreeIter *empathy_iter,
196 EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (data);
197 EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
200 children = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->store), NULL);
202 if (children == 1 && priv->is_blank_set)
203 gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
205 gtk_widget_set_sensitive (GTK_WIDGET (selector), TRUE);
207 gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
212 empathy_contact_selector_constructor (GType type,
213 guint n_construct_params,
214 GObjectConstructParam *construct_params)
217 G_OBJECT_CLASS (empathy_contact_selector_parent_class)->constructor (type,
218 n_construct_params, construct_params);
219 EmpathyContactSelector *contact_selector = EMPATHY_CONTACT_SELECTOR (object);
220 EmpathyContactSelectorPriv *priv = GET_PRIV (contact_selector);
221 GtkCellRenderer *renderer;
223 g_object_set (priv->store, "is-compact", TRUE, "show-avatars", FALSE,
224 "show-offline", FALSE, "sort-criterium", "show-groups", FALSE,
225 EMPATHY_CONTACT_LIST_STORE_SORT_NAME, NULL);
226 empathy_contact_list_store_set_show_groups (priv->store, FALSE);
228 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
229 EMPATHY_CONTACT_LIST_STORE_COL_NAME, GTK_SORT_ASCENDING);
231 g_signal_connect (priv->store, "row-changed",
232 G_CALLBACK (empathy_store_row_changed_cb), (gpointer) contact_selector);
233 g_signal_connect (GTK_COMBO_BOX (contact_selector), "changed",
234 G_CALLBACK (changed_cb), NULL);
235 g_signal_connect (GTK_COMBO_BOX (contact_selector), "notify::popup-shown",
236 G_CALLBACK (notify_popup_shown_cb), NULL);
238 gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector),
239 GTK_TREE_MODEL (priv->store));
240 gtk_widget_set_sensitive (GTK_WIDGET (contact_selector), FALSE);
243 renderer = gtk_cell_renderer_pixbuf_new ();
244 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (contact_selector),
246 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (contact_selector), renderer,
247 "icon-name", EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL);
250 renderer = gtk_cell_renderer_text_new ();
251 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (contact_selector),
253 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (contact_selector), renderer,
254 "text", EMPATHY_CONTACT_LIST_STORE_COL_NAME, NULL);
256 set_blank_contact (contact_selector);
258 object = G_OBJECT (contact_selector);
264 empathy_contact_selector_init (EmpathyContactSelector *empathy_contact_selector)
270 empathy_contact_selector_set_property (GObject *object,
275 EmpathyContactSelector *contact_selector = EMPATHY_CONTACT_SELECTOR (object);
276 EmpathyContactSelectorPriv *priv = GET_PRIV (contact_selector);
281 priv->store = g_value_dup_object (value);
284 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291 empathy_contact_selector_get_property (GObject *object,
296 EmpathyContactSelector *contact_selector = EMPATHY_CONTACT_SELECTOR (object);
297 EmpathyContactSelectorPriv *priv = GET_PRIV (contact_selector);
302 g_value_set_object (value, priv->store);
305 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
312 empathy_contact_selector_dispose (GObject *object)
314 g_debug ("EmpathyContactSelector - dispose: %p", object);
316 (G_OBJECT_CLASS (empathy_contact_selector_parent_class)->dispose) (object);
321 empathy_contact_selector_finalize (GObject *object)
323 g_debug ("EmpathyContactSelector - finalize: %p", object);
325 (G_OBJECT_CLASS (empathy_contact_selector_parent_class)->finalize) (object);
330 empathy_contact_selector_class_init (EmpathyContactSelectorClass *klass)
332 GObjectClass *object_class = G_OBJECT_CLASS (klass);
333 object_class->constructor = empathy_contact_selector_constructor;
334 object_class->dispose = empathy_contact_selector_dispose;
335 object_class->finalize = empathy_contact_selector_finalize;
336 object_class->set_property = empathy_contact_selector_set_property;
337 object_class->get_property = empathy_contact_selector_get_property;
338 g_type_class_add_private (klass, sizeof (EmpathyContactSelectorPriv));
340 g_object_class_install_property (object_class, PROP_STORE,
341 g_param_spec_object ("store", "store", "store",
342 EMPATHY_TYPE_CONTACT_LIST_STORE, G_PARAM_CONSTRUCT_ONLY |
343 G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
347 EmpathyContactSelector *
348 empathy_contact_selector_new (EmpathyContactListStore *store)
350 return g_object_new (EMPATHY_TYPE_CONTACT_SELECTOR, "store", store, NULL);