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
135 gtk_tree_model_get (model, iter_a,
136 COL_SERVICE, &service,
154 protocol_choosers_add_cm (EmpathyProtocolChooser *chooser,
155 TpConnectionManager *cm)
157 EmpathyProtocolChooserPriv *priv = GET_PRIV (chooser);
158 GList *protocols, *l;
159 const gchar *cm_name;
161 cm_name = tp_connection_manager_get_name (cm);
163 protocols = tp_connection_manager_dup_protocols (cm);
165 for (l = protocols; l != NULL; l = g_list_next (l))
167 TpProtocol *protocol = l->data;
169 const gchar *display_name;
170 const gchar *saved_cm_name;
171 const gchar *proto_name;
173 proto_name = tp_protocol_get_name (protocol);
174 saved_cm_name = g_hash_table_lookup (priv->protocols, proto_name);
176 if (!tp_strdiff (cm_name, "haze") && saved_cm_name != NULL &&
177 tp_strdiff (saved_cm_name, "haze"))
178 /* the CM we're adding is a haze implementation of something we already
183 if (!tp_strdiff (cm_name, "haze") &&
184 !tp_strdiff (proto_name, "facebook"))
185 /* Facebook now supports XMPP so drop the purple facebook plugin; user
186 * should use Gabble */
189 if (!tp_strdiff (cm_name, "haze") &&
190 !tp_strdiff (proto_name, "sip"))
191 /* Haze's SIP implementation is pretty useless (bgo #629736) */
194 if (!tp_strdiff (cm_name, "butterfly"))
195 /* Butterfly isn't supported any more */
198 if (tp_strdiff (cm_name, "haze") && !tp_strdiff (saved_cm_name, "haze"))
202 TpConnectionManager *haze_cm;
204 /* let's this CM replace the haze implementation */
205 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->store),
210 gchar *haze_proto_name = NULL;
212 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &titer,
213 COL_PROTOCOL_NAME, &haze_proto_name,
214 COL_CM, &haze_cm, -1);
219 if (!tp_strdiff (tp_connection_manager_get_name (haze_cm), "haze")
220 && !tp_strdiff (haze_proto_name, proto_name))
222 gtk_list_store_remove (priv->store, &titer);
223 g_object_unref (haze_cm);
224 g_free (haze_proto_name);
228 g_object_unref (haze_cm);
229 g_free (haze_proto_name);
230 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->store),
235 g_hash_table_insert (priv->protocols,
236 g_strdup (proto_name), g_strdup (cm_name));
238 icon_name = empathy_protocol_icon_name (proto_name);
240 display_name = empathy_protocol_name_to_display_name (proto_name);
242 gtk_list_store_insert_with_values (priv->store,
245 COL_LABEL, display_name,
247 COL_PROTOCOL_NAME, proto_name,
250 if (!tp_strdiff (proto_name, "jabber") &&
251 !tp_strdiff (cm_name, "gabble"))
253 display_name = empathy_service_name_to_display_name ("google-talk");
254 gtk_list_store_insert_with_values (priv->store,
256 COL_ICON, "im-google-talk",
257 COL_LABEL, display_name,
259 COL_PROTOCOL_NAME, proto_name,
260 COL_SERVICE, "google-talk",
263 display_name = empathy_service_name_to_display_name ("facebook");
264 gtk_list_store_insert_with_values (priv->store,
266 COL_ICON, "im-facebook",
267 COL_LABEL, display_name,
269 COL_PROTOCOL_NAME, proto_name,
270 COL_SERVICE, "facebook",
277 g_list_free_full (protocols, g_object_unref);
281 protocol_chooser_add_cms_list (EmpathyProtocolChooser *protocol_chooser,
286 for (l = cms; l != NULL; l = l->next)
287 protocol_choosers_add_cm (protocol_chooser, l->data);
289 gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
293 protocol_chooser_cms_prepare_cb (GObject *source,
294 GAsyncResult *result,
297 EmpathyConnectionManagers *cms = EMPATHY_CONNECTION_MANAGERS (source);
298 EmpathyProtocolChooser *protocol_chooser = user_data;
300 if (!empathy_connection_managers_prepare_finish (cms, result, NULL))
303 protocol_chooser_add_cms_list (protocol_chooser,
304 empathy_connection_managers_get_cms (cms));
308 protocol_chooser_constructed (GObject *object)
310 EmpathyProtocolChooser *protocol_chooser;
311 EmpathyProtocolChooserPriv *priv;
312 GtkCellRenderer *renderer;
314 priv = GET_PRIV (object);
315 protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
317 /* set up combo box with new store */
318 priv->store = gtk_list_store_new (COL_COUNT,
319 G_TYPE_STRING, /* Icon name */
320 G_TYPE_STRING, /* Label */
321 G_TYPE_OBJECT, /* CM */
322 G_TYPE_STRING, /* protocol name */
323 G_TYPE_STRING); /* service */
325 /* Set the protocol sort function */
326 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
328 protocol_chooser_sort_func,
330 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
334 gtk_combo_box_set_model (GTK_COMBO_BOX (object),
335 GTK_TREE_MODEL (priv->store));
337 renderer = gtk_cell_renderer_pixbuf_new ();
338 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
339 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
340 "icon-name", COL_ICON,
342 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
344 renderer = gtk_cell_renderer_text_new ();
345 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
346 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
350 empathy_connection_managers_prepare_async (priv->cms,
351 protocol_chooser_cms_prepare_cb, protocol_chooser);
353 if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
355 (empathy_protocol_chooser_parent_class)->constructed (object);
359 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
361 EmpathyProtocolChooserPriv *priv =
362 G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
363 EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
365 priv->dispose_run = FALSE;
366 priv->cms = empathy_connection_managers_dup_singleton ();
367 priv->protocols = g_hash_table_new_full (g_str_hash, g_str_equal,
370 protocol_chooser->priv = priv;
374 protocol_chooser_finalize (GObject *object)
376 EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
377 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
381 g_hash_table_unref (priv->protocols);
382 priv->protocols = NULL;
385 (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->finalize) (object);
389 protocol_chooser_dispose (GObject *object)
391 EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
392 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
394 if (priv->dispose_run)
397 priv->dispose_run = TRUE;
401 g_object_unref (priv->store);
407 g_object_unref (priv->cms);
411 (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
415 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
417 GObjectClass *object_class = G_OBJECT_CLASS (klass);
419 object_class->constructed = protocol_chooser_constructed;
420 object_class->dispose = protocol_chooser_dispose;
421 object_class->finalize = protocol_chooser_finalize;
423 g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
427 protocol_chooser_filter_visible_func (GtkTreeModel *model,
431 EmpathyProtocolChooser *protocol_chooser = user_data;
432 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
433 TpConnectionManager *cm = NULL;
434 gchar *protocol_name = NULL;
435 gboolean visible = FALSE;
438 gtk_tree_model_get (model, iter,
440 COL_PROTOCOL_NAME, &protocol_name,
441 COL_SERVICE, &service,
444 if (cm != NULL && protocol_name != NULL)
446 TpProtocol *protocol;
448 protocol = tp_connection_manager_get_protocol_object (cm, protocol_name);
450 if (protocol != NULL)
452 visible = priv->filter_func (cm, protocol, service,
453 priv->filter_user_data);
467 * empathy_protocol_chooser_get_selected_protocol:
468 * @protocol_chooser: an #EmpathyProtocolChooser
470 * Returns a pointer to the selected #TpConnectionManagerProtocol in
473 * Return value: a pointer to the selected #TpConnectionManagerProtocol
475 TpConnectionManager *
476 empathy_protocol_chooser_dup_selected (
477 EmpathyProtocolChooser *protocol_chooser,
478 TpProtocol **protocol,
482 TpConnectionManager *cm = NULL;
483 GtkTreeModel *cur_model;
485 g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
487 /* get the current model from the chooser, as we could either be filtering
490 cur_model = gtk_combo_box_get_model (GTK_COMBO_BOX (protocol_chooser));
492 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
494 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
498 if (protocol != NULL)
500 gchar *protocol_name = NULL;
502 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
503 COL_PROTOCOL_NAME, &protocol_name,
506 *protocol = tp_connection_manager_get_protocol_object (cm,
509 g_free (protocol_name);
511 if (*protocol == NULL)
513 /* For some reason the CM doesn't know about this protocol
522 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
523 COL_SERVICE, service,
532 * empathy_protocol_chooser_new:
534 * Triggers the creation of a new #EmpathyProtocolChooser.
536 * Return value: a new #EmpathyProtocolChooser widget
540 empathy_protocol_chooser_new (void)
542 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));
546 empathy_protocol_chooser_set_visible (EmpathyProtocolChooser *protocol_chooser,
547 EmpathyProtocolChooserFilterFunc func,
550 EmpathyProtocolChooserPriv *priv;
551 GtkTreeModel *filter_model;
553 g_return_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser));
555 priv = GET_PRIV (protocol_chooser);
556 priv->filter_func = func;
557 priv->filter_user_data = user_data;
559 filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store),
561 gtk_combo_box_set_model (GTK_COMBO_BOX (protocol_chooser), filter_model);
562 g_object_unref (filter_model);
564 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER
565 (filter_model), protocol_chooser_filter_visible_func,
566 protocol_chooser, NULL);
568 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
570 gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
573 EmpathyAccountSettings *
574 empathy_protocol_chooser_create_account_settings (EmpathyProtocolChooser *self)
576 EmpathyAccountSettings *settings = NULL;
578 const gchar *display_name;
579 TpConnectionManager *cm;
581 gchar *service = NULL;
583 cm = empathy_protocol_chooser_dup_selected (self, &proto, &service);
584 if (cm == NULL || proto == NULL)
588 display_name = empathy_service_name_to_display_name (service);
590 display_name = empathy_protocol_name_to_display_name (
591 tp_protocol_get_name (proto));
594 /* To translator: %s is the name of the protocol, such as "Google Talk" or
597 str = g_strdup_printf (_("New %s account"), display_name);
599 settings = empathy_account_settings_new (tp_connection_manager_get_name (cm),
600 tp_protocol_get_name (proto), service, str);
604 if (!tp_strdiff (service, "google-talk"))
606 const gchar *fallback_servers[] = {
607 "talkx.l.google.com",
608 "talkx.l.google.com:443,oldssl",
609 "talkx.l.google.com:80",
612 const gchar *extra_certificate_identities[] = {
616 empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
618 empathy_account_settings_set (settings, "server",
619 g_variant_new_string (extra_certificate_identities[0]));
620 empathy_account_settings_set (settings, "require-encryption",
621 g_variant_new_boolean (TRUE));
622 empathy_account_settings_set (settings, "fallback-servers",
623 g_variant_new_strv (fallback_servers, -1));
625 if (empathy_account_settings_have_tp_param (settings,
626 "extra-certificate-identities"))
628 empathy_account_settings_set (settings,
629 "extra-certificate-identities",
630 g_variant_new_strv (extra_certificate_identities, -1));
633 else if (!tp_strdiff (service, "facebook"))
635 const gchar *fallback_servers[] = {
636 "chat.facebook.com:443",
639 empathy_account_settings_set_icon_name_async (settings, "im-facebook",
641 empathy_account_settings_set (settings, "require-encryption",
642 g_variant_new_boolean (TRUE));
643 empathy_account_settings_set (settings, "server",
644 g_variant_new_string ("chat.facebook.com"));
645 empathy_account_settings_set (settings, "fallback-servers",
646 g_variant_new_strv (fallback_servers, -1));
650 tp_clear_object (&cm);