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;
138 icon_name = g_strdup_printf ("im-%s", proto->name);
140 if (!tp_strdiff (cm->name, "haze") && !tp_strdiff (proto->name, "msn"))
141 display_name = g_strdup_printf ("msn (Haze)");
143 display_name = g_strdup (proto->name);
145 gtk_list_store_insert_with_values (priv->store, NULL, 0,
147 COL_LABEL, display_name,
152 g_free (display_name);
159 protocol_choosers_cms_listed (TpConnectionManager * const *cms,
163 GObject *weak_object)
165 TpConnectionManager * const *iter;
169 DEBUG ("Failed to get connection managers: %s", error->message);
173 for (iter = cms ; iter != NULL && *iter != NULL; iter++)
174 protocol_choosers_add_cm (EMPATHY_PROTOCOL_CHOOSER (weak_object),
177 gtk_combo_box_set_active (GTK_COMBO_BOX (weak_object), 0);
181 protocol_chooser_constructed (GObject *object)
183 EmpathyProtocolChooser *protocol_chooser;
184 EmpathyProtocolChooserPriv *priv;
186 GtkCellRenderer *renderer;
189 priv = GET_PRIV (object);
190 protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
192 /* set up combo box with new store */
193 priv->store = gtk_list_store_new (COL_COUNT,
194 G_TYPE_STRING, /* Icon name */
195 G_TYPE_STRING, /* Label */
196 G_TYPE_OBJECT, /* CM */
197 G_TYPE_POINTER); /* protocol */
199 gtk_combo_box_set_model (GTK_COMBO_BOX (object),
200 GTK_TREE_MODEL (priv->store));
202 renderer = gtk_cell_renderer_pixbuf_new ();
203 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
204 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
205 "icon-name", COL_ICON,
207 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
209 renderer = gtk_cell_renderer_text_new ();
210 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
211 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
215 dbus = tp_dbus_daemon_dup (NULL);
216 tp_list_connection_managers (dbus, protocol_choosers_cms_listed,
218 g_object_unref (dbus);
220 /* Set the protocol sort function */
221 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
223 protocol_chooser_sort_func,
225 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
229 if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
230 G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed (object);
234 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
236 EmpathyProtocolChooserPriv *priv =
237 G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
238 EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
240 priv->dispose_run = FALSE;
242 protocol_chooser->priv = priv;
246 protocol_chooser_dispose (GObject *object)
248 EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
249 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
251 if (priv->dispose_run)
254 priv->dispose_run = TRUE;
258 g_object_unref (priv->store);
262 (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
266 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
268 GObjectClass *object_class = G_OBJECT_CLASS (klass);
270 object_class->constructed = protocol_chooser_constructed;
271 object_class->dispose = protocol_chooser_dispose;
273 g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
277 * empathy_protocol_chooser_get_selected_protocol:
278 * @protocol_chooser: an #EmpathyProtocolChooser
280 * Returns a pointer to the selected #TpConnectionManagerProtocol in
283 * Return value: a pointer to the selected #TpConnectionManagerProtocol
285 TpConnectionManager *empathy_protocol_chooser_dup_selected (
286 EmpathyProtocolChooser *protocol_chooser,
287 TpConnectionManagerProtocol **protocol)
289 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
291 TpConnectionManager *cm = NULL;
293 g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
295 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
297 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
301 if (protocol != NULL)
302 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
303 COL_PROTOCOL, protocol,
311 * empathy_protocol_chooser_n_protocols:
312 * @protocol_chooser: an #EmpathyProtocolChooser
314 * Returns the number of protocols in @protocol_chooser.
316 * Return value: the number of protocols in @protocol_chooser
319 empathy_protocol_chooser_n_protocols (EmpathyProtocolChooser *protocol_chooser)
321 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
323 g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), 0);
325 return gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->store), NULL);
329 * empathy_protocol_chooser_new:
331 * Creates a new #EmpathyProtocolChooser widget.
333 * Return value: a new #EmpathyProtocolChooser widget
336 empathy_protocol_chooser_new (void)
338 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));