1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
3 * Copyright (C) 2007-2009 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: Xavier Claessens <xclaesse@gmail.com>
20 * Jonny Lamb <jonny.lamb@collabora.co.uk>
27 #include <telepathy-glib/util.h>
31 #include <libempathy/empathy-utils.h>
33 #include "empathy-protocol-chooser.h"
34 #include "empathy-ui-utils.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
37 #include <libempathy/empathy-debug.h>
40 * SECTION:empathy-protocol-chooser
41 * @title: EmpathyProtocolChooser
42 * @short_description: A widget used to choose from a list of protocols
43 * @include: libempathy-gtk/empathy-protocol-chooser.h
45 * #EmpathyProtocolChooser is a widget which extends #GtkComboBox to provides a
46 * chooser of available protocols.
50 * EmpathyProtocolChooser:
51 * @parent: parent object
53 * Widget which extends #GtkComboBox to provide a chooser of available
57 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyProtocolChooser)
63 } EmpathyProtocolChooserPriv;
74 G_DEFINE_TYPE (EmpathyProtocolChooser, empathy_protocol_chooser,
78 protocol_chooser_sort_protocol_value (TpConnectionManagerProtocol *protocol)
81 const gchar *names[] = {
88 for (i = 0 ; names[i]; i++)
90 if (strcmp (protocol->name, names[i]) == 0)
98 protocol_chooser_sort_func (GtkTreeModel *model,
103 TpConnectionManagerProtocol *protocol_a;
104 TpConnectionManagerProtocol *protocol_b;
107 gtk_tree_model_get (model, iter_a,
108 COL_PROTOCOL, &protocol_a,
110 gtk_tree_model_get (model, iter_b,
111 COL_PROTOCOL, &protocol_b,
114 cmp = protocol_chooser_sort_protocol_value (protocol_a);
115 cmp -= protocol_chooser_sort_protocol_value (protocol_b);
118 cmp = strcmp (protocol_a->name, protocol_b->name);
125 protocol_choosers_add_cm (EmpathyProtocolChooser *chooser,
126 TpConnectionManager *cm)
128 EmpathyProtocolChooserPriv *priv = GET_PRIV (chooser);
129 const TpConnectionManagerProtocol * const *iter;
131 for (iter = cm->protocols; iter != NULL && *iter != NULL; iter++)
133 const TpConnectionManagerProtocol *proto = *iter;
137 icon_name = empathy_protocol_icon_name (proto->name);
139 if (!tp_strdiff (cm->name, "haze"))
140 display_name = g_strdup_printf ("%s (Haze)", proto->name);
142 display_name = g_strdup (proto->name);
144 gtk_list_store_insert_with_values (priv->store, NULL, 0,
146 COL_LABEL, display_name,
151 g_free (display_name);
158 protocol_choosers_cms_listed (TpConnectionManager * const *cms,
162 GObject *weak_object)
164 TpConnectionManager * const *iter;
168 DEBUG ("Failed to get connection managers: %s", error->message);
172 for (iter = cms ; iter != NULL && *iter != NULL; iter++)
173 protocol_choosers_add_cm (EMPATHY_PROTOCOL_CHOOSER (weak_object),
176 gtk_combo_box_set_active (GTK_COMBO_BOX (weak_object), 0);
180 protocol_chooser_constructed (GObject *object)
182 EmpathyProtocolChooser *protocol_chooser;
183 EmpathyProtocolChooserPriv *priv;
184 GtkCellRenderer *renderer;
187 priv = GET_PRIV (object);
188 protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
190 /* set up combo box with new store */
191 priv->store = gtk_list_store_new (COL_COUNT,
192 G_TYPE_STRING, /* Icon name */
193 G_TYPE_STRING, /* Label */
194 G_TYPE_OBJECT, /* CM */
195 G_TYPE_POINTER); /* protocol */
197 gtk_combo_box_set_model (GTK_COMBO_BOX (object),
198 GTK_TREE_MODEL (priv->store));
200 renderer = gtk_cell_renderer_pixbuf_new ();
201 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
202 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
203 "icon-name", COL_ICON,
205 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
207 renderer = gtk_cell_renderer_text_new ();
208 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
209 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
213 dbus = tp_dbus_daemon_dup (NULL);
214 tp_list_connection_managers (dbus, protocol_choosers_cms_listed,
216 g_object_unref (dbus);
218 /* Set the protocol sort function */
219 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
221 protocol_chooser_sort_func,
223 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
227 if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
228 G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed (object);
232 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
234 EmpathyProtocolChooserPriv *priv =
235 G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
236 EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
238 priv->dispose_run = FALSE;
240 protocol_chooser->priv = priv;
244 protocol_chooser_dispose (GObject *object)
246 EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
247 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
249 if (priv->dispose_run)
252 priv->dispose_run = TRUE;
256 g_object_unref (priv->store);
260 (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
264 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
266 GObjectClass *object_class = G_OBJECT_CLASS (klass);
268 object_class->constructed = protocol_chooser_constructed;
269 object_class->dispose = protocol_chooser_dispose;
271 g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
275 * empathy_protocol_chooser_get_selected_protocol:
276 * @protocol_chooser: an #EmpathyProtocolChooser
278 * Returns a pointer to the selected #TpConnectionManagerProtocol in
281 * Return value: a pointer to the selected #TpConnectionManagerProtocol
283 TpConnectionManager *
284 empathy_protocol_chooser_dup_selected (
285 EmpathyProtocolChooser *protocol_chooser,
286 TpConnectionManagerProtocol **protocol)
288 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
290 TpConnectionManager *cm = NULL;
292 g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
294 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
296 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
300 if (protocol != NULL)
301 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
302 COL_PROTOCOL, protocol,
310 * empathy_protocol_chooser_n_protocols:
311 * @protocol_chooser: an #EmpathyProtocolChooser
313 * Returns the number of protocols in @protocol_chooser.
315 * Return value: the number of protocols in @protocol_chooser
318 empathy_protocol_chooser_n_protocols (EmpathyProtocolChooser *protocol_chooser)
320 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
322 g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), 0);
324 return gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->store), NULL);
328 * empathy_protocol_chooser_new:
330 * Creates a new #EmpathyProtocolChooser widget.
332 * Return value: a new #EmpathyProtocolChooser widget
335 empathy_protocol_chooser_new (void)
337 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));