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 <glib/gi18n-lib.h>
33 #include <libempathy/empathy-utils.h>
34 #include <libempathy/empathy-connection-managers.h>
36 #include "empathy-protocol-chooser.h"
37 #include "empathy-ui-utils.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
40 #include <libempathy/empathy-debug.h>
43 * SECTION:empathy-protocol-chooser
44 * @title: EmpathyProtocolChooser
45 * @short_description: A widget used to choose from a list of protocols
46 * @include: libempathy-gtk/empathy-protocol-chooser.h
48 * #EmpathyProtocolChooser is a widget which extends #GtkComboBox to provides a
49 * chooser of available protocols.
53 * EmpathyProtocolChooser:
54 * @parent: parent object
56 * Widget which extends #GtkComboBox to provide a chooser of available
60 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyProtocolChooser)
66 EmpathyConnectionManagers *cms;
68 EmpathyProtocolChooserFilterFunc filter_func;
69 gpointer filter_user_data;
71 GHashTable *protocols;
72 } EmpathyProtocolChooserPriv;
84 G_DEFINE_TYPE (EmpathyProtocolChooser, empathy_protocol_chooser,
88 protocol_chooser_sort_protocol_value (const gchar *protocol_name)
91 const gchar *names[] = {
98 for (i = 0 ; names[i]; i++)
100 if (strcmp (protocol_name, names[i]) == 0)
108 protocol_chooser_sort_func (GtkTreeModel *model,
117 gtk_tree_model_get (model, iter_a,
118 COL_PROTOCOL_NAME, &protocol_a,
120 gtk_tree_model_get (model, iter_b,
121 COL_PROTOCOL_NAME, &protocol_b,
124 cmp = protocol_chooser_sort_protocol_value (protocol_a);
125 cmp -= protocol_chooser_sort_protocol_value (protocol_b);
128 cmp = strcmp (protocol_a, protocol_b);
129 /* only happens for jabber where there is one entry for gtalk and one for
134 gtk_tree_model_get (model, iter_a,
135 COL_IS_GTALK, &is_gtalk,
138 cmp = is_gtalk ? 1 : -1;
148 protocol_choosers_add_cm (EmpathyProtocolChooser *chooser,
149 TpConnectionManager *cm)
151 EmpathyProtocolChooserPriv *priv = GET_PRIV (chooser);
152 const TpConnectionManagerProtocol * const *iter;
154 for (iter = cm->protocols; iter != NULL && *iter != NULL; iter++)
156 const TpConnectionManagerProtocol *proto = *iter;
158 const gchar *display_name;
159 const gchar *saved_cm_name;
161 saved_cm_name = g_hash_table_lookup (priv->protocols, proto->name);
163 if (!tp_strdiff (cm->name, "haze") && saved_cm_name != NULL &&
164 tp_strdiff (saved_cm_name, "haze"))
165 /* the CM we're adding is a haze implementation of something we already
170 if (tp_strdiff (cm->name, "haze") && !tp_strdiff (saved_cm_name, "haze"))
174 TpConnectionManager *haze_cm;
176 /* let's this CM replace the haze implementation */
177 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->store),
182 gchar *haze_proto_name = NULL;
184 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &titer,
185 COL_PROTOCOL_NAME, &haze_proto_name,
186 COL_CM, &haze_cm, -1);
191 if (!tp_strdiff (haze_cm->name, "haze") &&
192 !tp_strdiff (haze_proto_name, proto->name))
194 gtk_list_store_remove (priv->store, &titer);
195 g_object_unref (haze_cm);
196 g_free (haze_proto_name);
200 g_object_unref (haze_cm);
201 g_free (haze_proto_name);
202 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->store),
207 g_hash_table_insert (priv->protocols,
208 g_strdup (proto->name), g_strdup (cm->name));
210 icon_name = empathy_protocol_icon_name (proto->name);
211 display_name = empathy_protocol_name_to_display_name (proto->name);
213 if (display_name == NULL)
214 display_name = proto->name;
216 gtk_list_store_insert_with_values (priv->store,
219 COL_LABEL, display_name,
221 COL_PROTOCOL_NAME, proto->name,
225 if (!tp_strdiff (proto->name, "jabber") &&
226 !tp_strdiff (cm->name, "gabble"))
228 display_name = empathy_protocol_name_to_display_name ("gtalk");
229 gtk_list_store_insert_with_values (priv->store,
231 COL_ICON, "im-google-talk",
232 COL_LABEL, display_name,
234 COL_PROTOCOL_NAME, proto->name,
244 protocol_chooser_add_cms_list (EmpathyProtocolChooser *protocol_chooser,
249 for (l = cms; l != NULL; l = l->next)
250 protocol_choosers_add_cm (protocol_chooser, l->data);
252 gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
256 protocol_chooser_cms_prepare_cb (GObject *source,
257 GAsyncResult *result,
260 EmpathyConnectionManagers *cms = EMPATHY_CONNECTION_MANAGERS (source);
261 EmpathyProtocolChooser *protocol_chooser = user_data;
263 if (!empathy_connection_managers_prepare_finish (cms, result, NULL))
266 protocol_chooser_add_cms_list (protocol_chooser,
267 empathy_connection_managers_get_cms (cms));
271 protocol_chooser_constructed (GObject *object)
273 EmpathyProtocolChooser *protocol_chooser;
274 EmpathyProtocolChooserPriv *priv;
275 GtkCellRenderer *renderer;
277 priv = GET_PRIV (object);
278 protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
280 /* set up combo box with new store */
281 priv->store = gtk_list_store_new (COL_COUNT,
282 G_TYPE_STRING, /* Icon name */
283 G_TYPE_STRING, /* Label */
284 G_TYPE_OBJECT, /* CM */
285 G_TYPE_STRING, /* protocol name */
286 G_TYPE_BOOLEAN); /* is gtalk */
288 /* Set the protocol sort function */
289 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
291 protocol_chooser_sort_func,
293 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
297 gtk_combo_box_set_model (GTK_COMBO_BOX (object),
298 GTK_TREE_MODEL (priv->store));
300 renderer = gtk_cell_renderer_pixbuf_new ();
301 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
302 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
303 "icon-name", COL_ICON,
305 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
307 renderer = gtk_cell_renderer_text_new ();
308 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
309 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
313 empathy_connection_managers_prepare_async (priv->cms,
314 protocol_chooser_cms_prepare_cb, protocol_chooser);
316 if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
318 (empathy_protocol_chooser_parent_class)->constructed (object);
322 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
324 EmpathyProtocolChooserPriv *priv =
325 G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
326 EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
328 priv->dispose_run = FALSE;
329 priv->cms = empathy_connection_managers_dup_singleton ();
330 priv->protocols = g_hash_table_new_full (g_str_hash, g_str_equal,
333 protocol_chooser->priv = priv;
337 protocol_chooser_finalize (GObject *object)
339 EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
340 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
344 g_hash_table_destroy (priv->protocols);
345 priv->protocols = NULL;
348 (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->finalize) (object);
352 protocol_chooser_dispose (GObject *object)
354 EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
355 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
357 if (priv->dispose_run)
360 priv->dispose_run = TRUE;
364 g_object_unref (priv->store);
370 g_object_unref (priv->cms);
374 (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
378 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
380 GObjectClass *object_class = G_OBJECT_CLASS (klass);
382 object_class->constructed = protocol_chooser_constructed;
383 object_class->dispose = protocol_chooser_dispose;
384 object_class->finalize = protocol_chooser_finalize;
386 g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
390 protocol_chooser_filter_visible_func (GtkTreeModel *model,
394 EmpathyProtocolChooser *protocol_chooser = user_data;
395 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
396 TpConnectionManager *cm = NULL;
397 gchar *protocol_name = NULL;
398 gboolean visible = FALSE;
401 gtk_tree_model_get (model, iter,
403 COL_PROTOCOL_NAME, &protocol_name,
404 COL_IS_GTALK, &is_gtalk,
407 if (cm != NULL && protocol_name != NULL)
409 TpConnectionManagerProtocol *protocol;
411 protocol = (TpConnectionManagerProtocol *)
412 tp_connection_manager_get_protocol (cm, protocol_name);
414 if (protocol != NULL)
416 visible = priv->filter_func (cm, protocol, is_gtalk,
417 priv->filter_user_data);
430 * empathy_protocol_chooser_get_selected_protocol:
431 * @protocol_chooser: an #EmpathyProtocolChooser
433 * Returns a pointer to the selected #TpConnectionManagerProtocol in
436 * Return value: a pointer to the selected #TpConnectionManagerProtocol
438 TpConnectionManager *
439 empathy_protocol_chooser_dup_selected (
440 EmpathyProtocolChooser *protocol_chooser,
441 TpConnectionManagerProtocol **protocol,
445 TpConnectionManager *cm = NULL;
446 GtkTreeModel *cur_model;
448 g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
450 /* get the current model from the chooser, as we could either be filtering
453 cur_model = gtk_combo_box_get_model (GTK_COMBO_BOX (protocol_chooser));
455 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
457 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
461 if (protocol != NULL)
463 gchar *protocol_name = NULL;
465 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
466 COL_PROTOCOL_NAME, &protocol_name,
469 *protocol = (TpConnectionManagerProtocol *)
470 tp_connection_manager_get_protocol (cm, protocol_name);
472 g_free (protocol_name);
475 if (is_gtalk != NULL)
477 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
478 COL_IS_GTALK, is_gtalk,
487 * empathy_protocol_chooser_new:
489 * Triggers the creation of a new #EmpathyProtocolChooser.
491 * Return value: a new #EmpathyProtocolChooser widget
495 empathy_protocol_chooser_new (void)
497 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));
501 empathy_protocol_chooser_set_visible (EmpathyProtocolChooser *protocol_chooser,
502 EmpathyProtocolChooserFilterFunc func,
505 EmpathyProtocolChooserPriv *priv;
506 GtkTreeModel *filter_model;
508 g_return_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser));
510 priv = GET_PRIV (protocol_chooser);
511 priv->filter_func = func;
512 priv->filter_user_data = user_data;
514 filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store),
516 gtk_combo_box_set_model (GTK_COMBO_BOX (protocol_chooser), filter_model);
517 g_object_unref (filter_model);
519 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER
520 (filter_model), protocol_chooser_filter_visible_func,
521 protocol_chooser, NULL);
523 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
525 gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);