prepare 2.30.1.1 release
[empathy-mirror.git] / libempathy-gtk / empathy-protocol-chooser.c
blob9f57d921ab8d87696ba57883cffb06da124e963d
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
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>
23 #include <config.h>
25 #include <string.h>
27 #include <telepathy-glib/util.h>
29 #include <gtk/gtk.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>
42 /**
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.
52 /**
53 * EmpathyProtocolChooser:
54 * @parent: parent object
56 * Widget which extends #GtkComboBox to provide a chooser of available
57 * protocols.
60 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyProtocolChooser)
61 typedef struct
63 GtkListStore *store;
65 gboolean dispose_run;
66 EmpathyConnectionManagers *cms;
68 EmpathyProtocolChooserFilterFunc filter_func;
69 gpointer filter_user_data;
71 GHashTable *protocols;
72 } EmpathyProtocolChooserPriv;
74 enum
76 COL_ICON,
77 COL_LABEL,
78 COL_CM,
79 COL_PROTOCOL_NAME,
80 COL_IS_GTALK,
81 COL_IS_FACEBOOK,
82 COL_COUNT
85 G_DEFINE_TYPE (EmpathyProtocolChooser, empathy_protocol_chooser,
86 GTK_TYPE_COMBO_BOX);
88 static gint
89 protocol_chooser_sort_protocol_value (const gchar *protocol_name)
91 guint i;
92 const gchar *names[] = {
93 "jabber",
94 "local-xmpp",
95 "gtalk",
96 NULL
99 for (i = 0 ; names[i]; i++)
101 if (strcmp (protocol_name, names[i]) == 0)
102 return i;
105 return i;
108 static gint
109 protocol_chooser_sort_func (GtkTreeModel *model,
110 GtkTreeIter *iter_a,
111 GtkTreeIter *iter_b,
112 gpointer user_data)
114 gchar *protocol_a;
115 gchar *protocol_b;
116 gint cmp = 0;
118 gtk_tree_model_get (model, iter_a,
119 COL_PROTOCOL_NAME, &protocol_a,
120 -1);
121 gtk_tree_model_get (model, iter_b,
122 COL_PROTOCOL_NAME, &protocol_b,
123 -1);
125 cmp = protocol_chooser_sort_protocol_value (protocol_a);
126 cmp -= protocol_chooser_sort_protocol_value (protocol_b);
127 if (cmp == 0)
129 cmp = strcmp (protocol_a, protocol_b);
130 /* only happens for jabber where there is one entry for gtalk and one for
131 * non-gtalk */
132 if (cmp == 0)
134 gboolean is_gtalk, is_facebook;
135 gtk_tree_model_get (model, iter_a,
136 COL_IS_GTALK, &is_gtalk,
137 COL_IS_FACEBOOK, &is_facebook,
138 -1);
140 if (is_gtalk || is_facebook)
141 cmp = 1;
142 else
143 cmp = -1;
147 g_free (protocol_a);
148 g_free (protocol_b);
149 return cmp;
152 static void
153 protocol_choosers_add_cm (EmpathyProtocolChooser *chooser,
154 TpConnectionManager *cm)
156 EmpathyProtocolChooserPriv *priv = GET_PRIV (chooser);
157 const TpConnectionManagerProtocol * const *iter;
159 for (iter = cm->protocols; iter != NULL && *iter != NULL; iter++)
161 const TpConnectionManagerProtocol *proto = *iter;
162 gchar *icon_name;
163 const gchar *display_name;
164 const gchar *saved_cm_name;
166 saved_cm_name = g_hash_table_lookup (priv->protocols, proto->name);
168 if (!tp_strdiff (cm->name, "haze") && saved_cm_name != NULL &&
169 tp_strdiff (saved_cm_name, "haze"))
170 /* the CM we're adding is a haze implementation of something we already
171 * have; drop it.
173 continue;
175 if (!tp_strdiff (cm->name, "haze") &&
176 !tp_strdiff (proto->name, "facebook"))
177 /* Facebook now supports XMPP so drop the purple facebook plugin; user
178 * should use Gabble */
179 continue;
181 if (tp_strdiff (cm->name, "haze") && !tp_strdiff (saved_cm_name, "haze"))
183 GtkTreeIter titer;
184 gboolean valid;
185 TpConnectionManager *haze_cm;
187 /* let's this CM replace the haze implementation */
188 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->store),
189 &titer);
191 while (valid)
193 gchar *haze_proto_name = NULL;
195 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &titer,
196 COL_PROTOCOL_NAME, &haze_proto_name,
197 COL_CM, &haze_cm, -1);
199 if (haze_cm == NULL)
200 continue;
202 if (!tp_strdiff (haze_cm->name, "haze") &&
203 !tp_strdiff (haze_proto_name, proto->name))
205 gtk_list_store_remove (priv->store, &titer);
206 g_object_unref (haze_cm);
207 g_free (haze_proto_name);
208 break;
211 g_object_unref (haze_cm);
212 g_free (haze_proto_name);
213 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->store),
214 &titer);
218 g_hash_table_insert (priv->protocols,
219 g_strdup (proto->name), g_strdup (cm->name));
221 icon_name = empathy_protocol_icon_name (proto->name);
222 display_name = empathy_protocol_name_to_display_name (proto->name);
224 if (display_name == NULL)
225 display_name = proto->name;
227 gtk_list_store_insert_with_values (priv->store,
228 NULL, 0,
229 COL_ICON, icon_name,
230 COL_LABEL, display_name,
231 COL_CM, cm,
232 COL_PROTOCOL_NAME, proto->name,
233 COL_IS_GTALK, FALSE,
234 COL_IS_FACEBOOK, FALSE,
235 -1);
237 if (!tp_strdiff (proto->name, "jabber") &&
238 !tp_strdiff (cm->name, "gabble"))
240 display_name = empathy_protocol_name_to_display_name ("gtalk");
241 gtk_list_store_insert_with_values (priv->store,
242 NULL, 0,
243 COL_ICON, "im-google-talk",
244 COL_LABEL, display_name,
245 COL_CM, cm,
246 COL_PROTOCOL_NAME, proto->name,
247 COL_IS_GTALK, TRUE,
248 COL_IS_FACEBOOK, FALSE,
249 -1);
251 display_name = empathy_protocol_name_to_display_name ("facebook");
252 gtk_list_store_insert_with_values (priv->store,
253 NULL, 0,
254 COL_ICON, "im-facebook",
255 COL_LABEL, display_name,
256 COL_CM, cm,
257 COL_PROTOCOL_NAME, proto->name,
258 COL_IS_GTALK, FALSE,
259 COL_IS_FACEBOOK, TRUE,
260 -1);
263 g_free (icon_name);
267 static void
268 protocol_chooser_add_cms_list (EmpathyProtocolChooser *protocol_chooser,
269 GList *cms)
271 GList *l;
273 for (l = cms; l != NULL; l = l->next)
274 protocol_choosers_add_cm (protocol_chooser, l->data);
276 gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
279 static void
280 protocol_chooser_cms_prepare_cb (GObject *source,
281 GAsyncResult *result,
282 gpointer user_data)
284 EmpathyConnectionManagers *cms = EMPATHY_CONNECTION_MANAGERS (source);
285 EmpathyProtocolChooser *protocol_chooser = user_data;
287 if (!empathy_connection_managers_prepare_finish (cms, result, NULL))
288 return;
290 protocol_chooser_add_cms_list (protocol_chooser,
291 empathy_connection_managers_get_cms (cms));
294 static void
295 protocol_chooser_constructed (GObject *object)
297 EmpathyProtocolChooser *protocol_chooser;
298 EmpathyProtocolChooserPriv *priv;
299 GtkCellRenderer *renderer;
301 priv = GET_PRIV (object);
302 protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
304 /* set up combo box with new store */
305 priv->store = gtk_list_store_new (COL_COUNT,
306 G_TYPE_STRING, /* Icon name */
307 G_TYPE_STRING, /* Label */
308 G_TYPE_OBJECT, /* CM */
309 G_TYPE_STRING, /* protocol name */
310 G_TYPE_BOOLEAN, /* is gtalk */
311 G_TYPE_BOOLEAN); /* is facebook */
313 /* Set the protocol sort function */
314 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
315 COL_PROTOCOL_NAME,
316 protocol_chooser_sort_func,
317 NULL, NULL);
318 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
319 COL_PROTOCOL_NAME,
320 GTK_SORT_ASCENDING);
322 gtk_combo_box_set_model (GTK_COMBO_BOX (object),
323 GTK_TREE_MODEL (priv->store));
325 renderer = gtk_cell_renderer_pixbuf_new ();
326 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
327 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
328 "icon-name", COL_ICON,
329 NULL);
330 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
332 renderer = gtk_cell_renderer_text_new ();
333 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
334 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
335 "text", COL_LABEL,
336 NULL);
338 empathy_connection_managers_prepare_async (priv->cms,
339 protocol_chooser_cms_prepare_cb, protocol_chooser);
341 if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
342 G_OBJECT_CLASS
343 (empathy_protocol_chooser_parent_class)->constructed (object);
346 static void
347 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
349 EmpathyProtocolChooserPriv *priv =
350 G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
351 EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
353 priv->dispose_run = FALSE;
354 priv->cms = empathy_connection_managers_dup_singleton ();
355 priv->protocols = g_hash_table_new_full (g_str_hash, g_str_equal,
356 g_free, g_free);
358 protocol_chooser->priv = priv;
361 static void
362 protocol_chooser_finalize (GObject *object)
364 EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
365 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
367 if (priv->protocols)
369 g_hash_table_destroy (priv->protocols);
370 priv->protocols = NULL;
373 (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->finalize) (object);
376 static void
377 protocol_chooser_dispose (GObject *object)
379 EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
380 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
382 if (priv->dispose_run)
383 return;
385 priv->dispose_run = TRUE;
387 if (priv->store)
389 g_object_unref (priv->store);
390 priv->store = NULL;
393 if (priv->cms)
395 g_object_unref (priv->cms);
396 priv->cms = NULL;
399 (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
402 static void
403 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
405 GObjectClass *object_class = G_OBJECT_CLASS (klass);
407 object_class->constructed = protocol_chooser_constructed;
408 object_class->dispose = protocol_chooser_dispose;
409 object_class->finalize = protocol_chooser_finalize;
411 g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
414 static gboolean
415 protocol_chooser_filter_visible_func (GtkTreeModel *model,
416 GtkTreeIter *iter,
417 gpointer user_data)
419 EmpathyProtocolChooser *protocol_chooser = user_data;
420 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
421 TpConnectionManager *cm = NULL;
422 gchar *protocol_name = NULL;
423 gboolean visible = FALSE;
424 gboolean is_gtalk, is_facebook;
426 gtk_tree_model_get (model, iter,
427 COL_CM, &cm,
428 COL_PROTOCOL_NAME, &protocol_name,
429 COL_IS_GTALK, &is_gtalk,
430 COL_IS_FACEBOOK, &is_facebook,
431 -1);
433 if (cm != NULL && protocol_name != NULL)
435 TpConnectionManagerProtocol *protocol;
437 protocol = (TpConnectionManagerProtocol *)
438 tp_connection_manager_get_protocol (cm, protocol_name);
440 if (protocol != NULL)
442 visible = priv->filter_func (cm, protocol, is_gtalk, is_facebook,
443 priv->filter_user_data);
447 if (cm != NULL)
448 g_object_unref (cm);
450 return visible;
453 /* public methods */
456 * empathy_protocol_chooser_get_selected_protocol:
457 * @protocol_chooser: an #EmpathyProtocolChooser
459 * Returns a pointer to the selected #TpConnectionManagerProtocol in
460 * @protocol_chooser.
462 * Return value: a pointer to the selected #TpConnectionManagerProtocol
464 TpConnectionManager *
465 empathy_protocol_chooser_dup_selected (
466 EmpathyProtocolChooser *protocol_chooser,
467 TpConnectionManagerProtocol **protocol,
468 gboolean *is_gtalk,
469 gboolean *is_facebook)
471 GtkTreeIter iter;
472 TpConnectionManager *cm = NULL;
473 GtkTreeModel *cur_model;
475 g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
477 /* get the current model from the chooser, as we could either be filtering
478 * or not.
480 cur_model = gtk_combo_box_get_model (GTK_COMBO_BOX (protocol_chooser));
482 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
484 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
485 COL_CM, &cm,
486 -1);
488 if (protocol != NULL)
490 gchar *protocol_name = NULL;
492 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
493 COL_PROTOCOL_NAME, &protocol_name,
494 -1);
496 *protocol = (TpConnectionManagerProtocol *)
497 tp_connection_manager_get_protocol (cm, protocol_name);
499 g_free (protocol_name);
501 if (*protocol == NULL)
503 /* For some reason the CM doesn't know about this protocol
504 * any more */
505 g_object_unref (cm);
506 return NULL;
510 if (is_gtalk != NULL)
512 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
513 COL_IS_GTALK, is_gtalk,
514 -1);
517 if (is_facebook != NULL)
519 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
520 COL_IS_FACEBOOK, is_facebook,
521 -1);
525 return cm;
529 * empathy_protocol_chooser_new:
531 * Triggers the creation of a new #EmpathyProtocolChooser.
533 * Return value: a new #EmpathyProtocolChooser widget
536 GtkWidget *
537 empathy_protocol_chooser_new (void)
539 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));
542 void
543 empathy_protocol_chooser_set_visible (EmpathyProtocolChooser *protocol_chooser,
544 EmpathyProtocolChooserFilterFunc func,
545 gpointer user_data)
547 EmpathyProtocolChooserPriv *priv;
548 GtkTreeModel *filter_model;
550 g_return_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser));
552 priv = GET_PRIV (protocol_chooser);
553 priv->filter_func = func;
554 priv->filter_user_data = user_data;
556 filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store),
557 NULL);
558 gtk_combo_box_set_model (GTK_COMBO_BOX (protocol_chooser), filter_model);
559 g_object_unref (filter_model);
561 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER
562 (filter_model), protocol_chooser_filter_visible_func,
563 protocol_chooser, NULL);
565 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
567 gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);