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>
32 #include <libempathy/empathy-connection-managers.h>
34 #include "empathy-protocol-chooser.h"
35 #include "empathy-ui-utils.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
38 #include <libempathy/empathy-debug.h>
41 * SECTION:empathy-protocol-chooser
42 * @title: EmpathyProtocolChooser
43 * @short_description: A widget used to choose from a list of protocols
44 * @include: libempathy-gtk/empathy-protocol-chooser.h
46 * #EmpathyProtocolChooser is a widget which extends #GtkComboBox to provides a
47 * chooser of available protocols.
51 * EmpathyProtocolChooser:
52 * @parent: parent object
54 * Widget which extends #GtkComboBox to provide a chooser of available
58 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyProtocolChooser)
63 EmpathyConnectionManagers *cms;
65 } EmpathyProtocolChooserPriv;
76 G_DEFINE_TYPE (EmpathyProtocolChooser, empathy_protocol_chooser,
80 protocol_chooser_sort_protocol_value (TpConnectionManagerProtocol *protocol)
83 const gchar *names[] = {
90 for (i = 0 ; names[i]; i++)
92 if (strcmp (protocol->name, names[i]) == 0)
100 protocol_chooser_sort_func (GtkTreeModel *model,
105 TpConnectionManagerProtocol *protocol_a;
106 TpConnectionManagerProtocol *protocol_b;
109 gtk_tree_model_get (model, iter_a,
110 COL_PROTOCOL, &protocol_a,
112 gtk_tree_model_get (model, iter_b,
113 COL_PROTOCOL, &protocol_b,
116 cmp = protocol_chooser_sort_protocol_value (protocol_a);
117 cmp -= protocol_chooser_sort_protocol_value (protocol_b);
120 cmp = strcmp (protocol_a->name, protocol_b->name);
127 protocol_choosers_add_cm (EmpathyProtocolChooser *chooser,
128 TpConnectionManager *cm)
130 EmpathyProtocolChooserPriv *priv = GET_PRIV (chooser);
131 const TpConnectionManagerProtocol * const *iter;
133 for (iter = cm->protocols; iter != NULL && *iter != NULL; iter++)
135 const TpConnectionManagerProtocol *proto = *iter;
140 icon_name = g_strdup_printf ("im-%s", proto->name);
142 if (!tp_strdiff (cm->name, "haze"))
143 display_name = g_strdup_printf ("%s (Haze)", proto->name);
145 display_name = g_strdup (proto->name);
147 gtk_list_store_insert_with_values (priv->store, NULL, 0,
149 COL_LABEL, display_name,
154 g_free (display_name);
160 protocol_chooser_add_cms_list (EmpathyProtocolChooser *protocol_chooser,
165 for (l = cms; l != NULL; l = l->next)
166 protocol_choosers_add_cm (protocol_chooser, l->data);
168 gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
172 protocol_chooser_cms_ready_cb (EmpathyConnectionManagers *cms,
174 EmpathyProtocolChooser *protocol_chooser)
176 if (empathy_connection_managers_is_ready (cms))
177 protocol_chooser_add_cms_list
178 (protocol_chooser, empathy_connection_managers_get_cms (cms));
182 protocol_chooser_constructed (GObject *object)
184 EmpathyProtocolChooser *protocol_chooser;
185 EmpathyProtocolChooserPriv *priv;
186 GtkCellRenderer *renderer;
188 priv = GET_PRIV (object);
189 protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
191 /* set up combo box with new store */
192 priv->store = gtk_list_store_new (COL_COUNT,
193 G_TYPE_STRING, /* Icon name */
194 G_TYPE_STRING, /* Label */
195 G_TYPE_OBJECT, /* CM */
196 G_TYPE_POINTER); /* protocol */
198 gtk_combo_box_set_model (GTK_COMBO_BOX (object),
199 GTK_TREE_MODEL (priv->store));
201 renderer = gtk_cell_renderer_pixbuf_new ();
202 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
203 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
204 "icon-name", COL_ICON,
206 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
208 renderer = gtk_cell_renderer_text_new ();
209 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
210 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
214 /* Set the protocol sort function */
215 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
217 protocol_chooser_sort_func,
219 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
223 if (empathy_connection_managers_is_ready (priv->cms))
224 protocol_chooser_add_cms_list (protocol_chooser,
225 empathy_connection_managers_get_cms (priv->cms));
227 g_signal_connect (priv->cms, "notify::ready",
228 G_CALLBACK (protocol_chooser_cms_ready_cb), protocol_chooser);
230 if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
231 G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed (object);
235 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
237 EmpathyProtocolChooserPriv *priv =
238 G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
239 EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
241 priv->dispose_run = FALSE;
242 priv->cms = empathy_connection_managers_dup_singleton ();
244 protocol_chooser->priv = priv;
248 protocol_chooser_dispose (GObject *object)
250 EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
251 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
253 if (priv->dispose_run)
256 priv->dispose_run = TRUE;
260 g_object_unref (priv->store);
266 g_object_unref (priv->cms);
270 (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
274 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
276 GObjectClass *object_class = G_OBJECT_CLASS (klass);
278 object_class->constructed = protocol_chooser_constructed;
279 object_class->dispose = protocol_chooser_dispose;
281 g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
287 * empathy_protocol_chooser_get_selected_protocol:
288 * @protocol_chooser: an #EmpathyProtocolChooser
290 * Returns a pointer to the selected #TpConnectionManagerProtocol in
293 * Return value: a pointer to the selected #TpConnectionManagerProtocol
295 TpConnectionManager *empathy_protocol_chooser_dup_selected (
296 EmpathyProtocolChooser *protocol_chooser,
297 TpConnectionManagerProtocol **protocol)
299 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
301 TpConnectionManager *cm = NULL;
303 g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
305 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
307 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
311 if (protocol != NULL)
312 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
313 COL_PROTOCOL, protocol,
321 * empathy_protocol_chooser_new:
323 * Triggers the creation of a new #EmpathyProtocolChooser.
325 * Return value: a new #EmpathyProtocolChooser widget
329 empathy_protocol_chooser_new (void)
331 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));