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>
24 #include "empathy-protocol-chooser.h"
26 #include <glib/gi18n-lib.h>
28 #include "empathy-connection-managers.h"
29 #include "empathy-ui-utils.h"
30 #include "empathy-utils.h"
32 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
33 #include "empathy-debug.h"
36 * SECTION:empathy-protocol-chooser
37 * @title: EmpathyProtocolChooser
38 * @short_description: A widget used to choose from a list of protocols
39 * @include: libempathy-gtk/empathy-protocol-chooser.h
41 * #EmpathyProtocolChooser is a widget which extends #GtkComboBox to provides a
42 * chooser of available protocols.
46 * EmpathyProtocolChooser:
47 * @parent: parent object
49 * Widget which extends #GtkComboBox to provide a chooser of available
53 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyProtocolChooser)
59 EmpathyConnectionManagers *cms;
61 EmpathyProtocolChooserFilterFunc filter_func;
62 gpointer filter_user_data;
64 GHashTable *protocols;
65 } EmpathyProtocolChooserPriv;
77 G_DEFINE_TYPE (EmpathyProtocolChooser, empathy_protocol_chooser,
81 protocol_chooser_sort_protocol_value (const gchar *protocol_name)
84 const gchar *names[] = {
91 for (i = 0 ; names[i]; i++)
93 if (strcmp (protocol_name, names[i]) == 0)
101 protocol_chooser_sort_func (GtkTreeModel *model,
110 gtk_tree_model_get (model, iter_a,
111 COL_PROTOCOL_NAME, &protocol_a,
113 gtk_tree_model_get (model, iter_b,
114 COL_PROTOCOL_NAME, &protocol_b,
117 cmp = protocol_chooser_sort_protocol_value (protocol_a);
118 cmp -= protocol_chooser_sort_protocol_value (protocol_b);
121 cmp = strcmp (protocol_a, protocol_b);
122 /* only happens for jabber where there is one entry for gtalk and one for
128 gtk_tree_model_get (model, iter_a,
129 COL_SERVICE, &service,
147 protocol_choosers_add_cm (EmpathyProtocolChooser *chooser,
148 TpConnectionManager *cm)
150 EmpathyProtocolChooserPriv *priv = GET_PRIV (chooser);
151 GList *protocols, *l;
152 const gchar *cm_name;
154 cm_name = tp_connection_manager_get_name (cm);
156 protocols = tp_connection_manager_dup_protocols (cm);
158 for (l = protocols; l != NULL; l = g_list_next (l))
160 TpProtocol *protocol = l->data;
162 const gchar *display_name;
163 const gchar *saved_cm_name;
164 const gchar *proto_name;
167 proto_name = tp_protocol_get_name (protocol);
168 saved_cm_name = g_hash_table_lookup (priv->protocols, proto_name);
170 if (!tp_strdiff (cm_name, "haze") && saved_cm_name != NULL &&
171 tp_strdiff (saved_cm_name, "haze"))
172 /* the CM we're adding is a haze implementation of something we already
177 if (!tp_strdiff (cm_name, "haze") &&
178 !tp_strdiff (proto_name, "facebook"))
179 /* Facebook now supports XMPP so drop the purple facebook plugin; user
180 * should use Gabble */
183 if (!tp_strdiff (cm_name, "haze") &&
184 !tp_strdiff (proto_name, "sip"))
185 /* Haze's SIP implementation is pretty useless (bgo #629736) */
188 if (!tp_strdiff (cm_name, "butterfly"))
189 /* Butterfly isn't supported any more */
192 if (tp_strdiff (cm_name, "haze") && !tp_strdiff (saved_cm_name, "haze"))
196 TpConnectionManager *haze_cm;
198 /* let's this CM replace the haze implementation */
199 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->store),
204 gchar *haze_proto_name = NULL;
206 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &titer,
207 COL_PROTOCOL_NAME, &haze_proto_name,
208 COL_CM, &haze_cm, -1);
213 if (!tp_strdiff (tp_connection_manager_get_name (haze_cm), "haze")
214 && !tp_strdiff (haze_proto_name, proto_name))
216 gtk_list_store_remove (priv->store, &titer);
217 g_object_unref (haze_cm);
218 g_free (haze_proto_name);
222 g_object_unref (haze_cm);
223 g_free (haze_proto_name);
224 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->store),
229 g_hash_table_insert (priv->protocols,
230 g_strdup (proto_name), g_strdup (cm_name));
232 icon_name = empathy_protocol_icon_name (proto_name);
233 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
235 display_name = empathy_protocol_name_to_display_name (proto_name);
237 gtk_list_store_insert_with_values (priv->store,
240 COL_LABEL, display_name,
242 COL_PROTOCOL_NAME, proto_name,
245 g_clear_object (&pixbuf);
247 if (!tp_strdiff (proto_name, "jabber") &&
248 !tp_strdiff (cm_name, "gabble"))
250 display_name = empathy_service_name_to_display_name ("google-talk");
251 pixbuf = empathy_pixbuf_from_icon_name ("im-google-talk",
252 GTK_ICON_SIZE_BUTTON);
254 gtk_list_store_insert_with_values (priv->store,
257 COL_LABEL, display_name,
259 COL_PROTOCOL_NAME, proto_name,
260 COL_SERVICE, "google-talk",
263 g_clear_object (&pixbuf);
265 display_name = empathy_service_name_to_display_name ("facebook");
266 pixbuf = empathy_pixbuf_from_icon_name ("im-facebook",
267 GTK_ICON_SIZE_BUTTON);
269 gtk_list_store_insert_with_values (priv->store,
272 COL_LABEL, display_name,
274 COL_PROTOCOL_NAME, proto_name,
275 COL_SERVICE, "facebook",
278 g_clear_object (&pixbuf);
284 g_list_free_full (protocols, g_object_unref);
288 protocol_chooser_add_cms_list (EmpathyProtocolChooser *protocol_chooser,
293 for (l = cms; l != NULL; l = l->next)
294 protocol_choosers_add_cm (protocol_chooser, l->data);
296 gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
300 protocol_chooser_cms_prepare_cb (GObject *source,
301 GAsyncResult *result,
304 EmpathyConnectionManagers *cms = EMPATHY_CONNECTION_MANAGERS (source);
305 EmpathyProtocolChooser *protocol_chooser = user_data;
307 if (!empathy_connection_managers_prepare_finish (cms, result, NULL))
310 protocol_chooser_add_cms_list (protocol_chooser,
311 empathy_connection_managers_get_cms (cms));
315 protocol_chooser_constructed (GObject *object)
317 EmpathyProtocolChooser *protocol_chooser;
318 EmpathyProtocolChooserPriv *priv;
319 GtkCellRenderer *renderer;
321 priv = GET_PRIV (object);
322 protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
324 /* set up combo box with new store */
325 priv->store = gtk_list_store_new (COL_COUNT,
326 GDK_TYPE_PIXBUF, /* Icon */
327 G_TYPE_STRING, /* Label */
328 G_TYPE_OBJECT, /* CM */
329 G_TYPE_STRING, /* protocol name */
330 G_TYPE_STRING); /* service */
332 /* Set the protocol sort function */
333 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
335 protocol_chooser_sort_func,
337 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
341 gtk_combo_box_set_model (GTK_COMBO_BOX (object),
342 GTK_TREE_MODEL (priv->store));
344 renderer = gtk_cell_renderer_pixbuf_new ();
345 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
346 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
350 renderer = gtk_cell_renderer_text_new ();
351 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
352 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
356 empathy_connection_managers_prepare_async (priv->cms,
357 protocol_chooser_cms_prepare_cb, protocol_chooser);
359 if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
361 (empathy_protocol_chooser_parent_class)->constructed (object);
365 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
367 EmpathyProtocolChooserPriv *priv =
368 G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
369 EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
371 priv->dispose_run = FALSE;
372 priv->cms = empathy_connection_managers_dup_singleton ();
373 priv->protocols = g_hash_table_new_full (g_str_hash, g_str_equal,
376 protocol_chooser->priv = priv;
380 protocol_chooser_finalize (GObject *object)
382 EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
383 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
387 g_hash_table_unref (priv->protocols);
388 priv->protocols = NULL;
391 (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->finalize) (object);
395 protocol_chooser_dispose (GObject *object)
397 EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
398 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
400 if (priv->dispose_run)
403 priv->dispose_run = TRUE;
407 g_object_unref (priv->store);
413 g_object_unref (priv->cms);
417 (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
421 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
423 GObjectClass *object_class = G_OBJECT_CLASS (klass);
425 object_class->constructed = protocol_chooser_constructed;
426 object_class->dispose = protocol_chooser_dispose;
427 object_class->finalize = protocol_chooser_finalize;
429 g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
433 protocol_chooser_filter_visible_func (GtkTreeModel *model,
437 EmpathyProtocolChooser *protocol_chooser = user_data;
438 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
439 TpConnectionManager *cm = NULL;
440 gchar *protocol_name = NULL;
441 gboolean visible = FALSE;
444 gtk_tree_model_get (model, iter,
446 COL_PROTOCOL_NAME, &protocol_name,
447 COL_SERVICE, &service,
450 if (cm != NULL && protocol_name != NULL)
452 TpProtocol *protocol;
454 protocol = tp_connection_manager_get_protocol_object (cm, protocol_name);
456 if (protocol != NULL)
458 visible = priv->filter_func (cm, protocol, service,
459 priv->filter_user_data);
473 * empathy_protocol_chooser_get_selected_protocol:
474 * @protocol_chooser: an #EmpathyProtocolChooser
476 * Returns a pointer to the selected #TpConnectionManagerProtocol in
479 * Return value: a pointer to the selected #TpConnectionManagerProtocol
481 TpConnectionManager *
482 empathy_protocol_chooser_dup_selected (
483 EmpathyProtocolChooser *protocol_chooser,
484 TpProtocol **protocol,
488 TpConnectionManager *cm = NULL;
489 GtkTreeModel *cur_model;
491 g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
493 /* get the current model from the chooser, as we could either be filtering
496 cur_model = gtk_combo_box_get_model (GTK_COMBO_BOX (protocol_chooser));
498 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
500 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
504 if (protocol != NULL)
506 gchar *protocol_name = NULL;
508 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
509 COL_PROTOCOL_NAME, &protocol_name,
512 *protocol = tp_connection_manager_get_protocol_object (cm,
515 g_free (protocol_name);
517 if (*protocol == NULL)
519 /* For some reason the CM doesn't know about this protocol
528 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
529 COL_SERVICE, service,
538 * empathy_protocol_chooser_new:
540 * Triggers the creation of a new #EmpathyProtocolChooser.
542 * Return value: a new #EmpathyProtocolChooser widget
546 empathy_protocol_chooser_new (void)
548 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));
552 empathy_protocol_chooser_set_visible (EmpathyProtocolChooser *protocol_chooser,
553 EmpathyProtocolChooserFilterFunc func,
556 EmpathyProtocolChooserPriv *priv;
557 GtkTreeModel *filter_model;
559 g_return_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser));
561 priv = GET_PRIV (protocol_chooser);
562 priv->filter_func = func;
563 priv->filter_user_data = user_data;
565 filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store),
567 gtk_combo_box_set_model (GTK_COMBO_BOX (protocol_chooser), filter_model);
568 g_object_unref (filter_model);
570 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER
571 (filter_model), protocol_chooser_filter_visible_func,
572 protocol_chooser, NULL);
574 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
576 gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
579 EmpathyAccountSettings *
580 empathy_protocol_chooser_create_account_settings (EmpathyProtocolChooser *self)
582 EmpathyAccountSettings *settings = NULL;
584 const gchar *display_name;
585 TpConnectionManager *cm;
587 gchar *service = NULL;
589 cm = empathy_protocol_chooser_dup_selected (self, &proto, &service);
590 if (cm == NULL || proto == NULL)
594 display_name = empathy_service_name_to_display_name (service);
596 display_name = empathy_protocol_name_to_display_name (
597 tp_protocol_get_name (proto));
600 /* To translator: %s is the name of the protocol, such as "Google Talk" or
603 str = g_strdup_printf (_("New %s account"), display_name);
605 settings = empathy_account_settings_new (tp_connection_manager_get_name (cm),
606 tp_protocol_get_name (proto), service, str);
610 if (!tp_strdiff (service, "google-talk"))
612 const gchar *fallback_servers[] = {
613 "talkx.l.google.com",
614 "talkx.l.google.com:443,oldssl",
615 "talkx.l.google.com:80",
618 const gchar *extra_certificate_identities[] = {
622 empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
624 empathy_account_settings_set (settings, "server",
625 g_variant_new_string (extra_certificate_identities[0]));
626 empathy_account_settings_set (settings, "require-encryption",
627 g_variant_new_boolean (TRUE));
628 empathy_account_settings_set (settings, "fallback-servers",
629 g_variant_new_strv (fallback_servers, -1));
631 if (empathy_account_settings_have_tp_param (settings,
632 "extra-certificate-identities"))
634 empathy_account_settings_set (settings,
635 "extra-certificate-identities",
636 g_variant_new_strv (extra_certificate_identities, -1));
639 else if (!tp_strdiff (service, "facebook"))
641 const gchar *fallback_servers[] = {
642 "chat.facebook.com:443",
645 empathy_account_settings_set_icon_name_async (settings, "im-facebook",
647 empathy_account_settings_set (settings, "require-encryption",
648 g_variant_new_boolean (TRUE));
649 empathy_account_settings_set (settings, "server",
650 g_variant_new_string ("chat.facebook.com"));
651 empathy_account_settings_set (settings, "fallback-servers",
652 g_variant_new_strv (fallback_servers, -1));
656 tp_clear_object (&cm);