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 const TpConnectionManagerProtocol * const *iter;
160 for (iter = cm->protocols; iter != NULL && *iter != NULL; iter++)
162 const TpConnectionManagerProtocol *proto = *iter;
164 const gchar *display_name;
165 const gchar *saved_cm_name;
167 saved_cm_name = g_hash_table_lookup (priv->protocols, proto->name);
169 if (!tp_strdiff (cm->name, "haze") && saved_cm_name != NULL &&
170 tp_strdiff (saved_cm_name, "haze"))
171 /* the CM we're adding is a haze implementation of something we already
176 if (!tp_strdiff (cm->name, "haze") &&
177 !tp_strdiff (proto->name, "facebook"))
178 /* Facebook now supports XMPP so drop the purple facebook plugin; user
179 * should use Gabble */
182 if (!tp_strdiff (cm->name, "haze") &&
183 !tp_strdiff (proto->name, "sip"))
184 /* Haze's SIP implementation is pretty useless (bgo #629736) */
187 if (!tp_strdiff (cm->name, "butterfly"))
188 /* Butterfly isn't supported any more */
191 if (tp_strdiff (cm->name, "haze") && !tp_strdiff (saved_cm_name, "haze"))
195 TpConnectionManager *haze_cm;
197 /* let's this CM replace the haze implementation */
198 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->store),
203 gchar *haze_proto_name = NULL;
205 gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &titer,
206 COL_PROTOCOL_NAME, &haze_proto_name,
207 COL_CM, &haze_cm, -1);
212 if (!tp_strdiff (haze_cm->name, "haze") &&
213 !tp_strdiff (haze_proto_name, proto->name))
215 gtk_list_store_remove (priv->store, &titer);
216 g_object_unref (haze_cm);
217 g_free (haze_proto_name);
221 g_object_unref (haze_cm);
222 g_free (haze_proto_name);
223 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->store),
228 g_hash_table_insert (priv->protocols,
229 g_strdup (proto->name), g_strdup (cm->name));
231 icon_name = empathy_protocol_icon_name (proto->name);
233 display_name = empathy_protocol_name_to_display_name (proto->name);
235 gtk_list_store_insert_with_values (priv->store,
238 COL_LABEL, display_name,
240 COL_PROTOCOL_NAME, proto->name,
243 if (!tp_strdiff (proto->name, "jabber") &&
244 !tp_strdiff (cm->name, "gabble"))
246 display_name = empathy_service_name_to_display_name ("google-talk");
247 gtk_list_store_insert_with_values (priv->store,
249 COL_ICON, "im-google-talk",
250 COL_LABEL, display_name,
252 COL_PROTOCOL_NAME, proto->name,
253 COL_SERVICE, "google-talk",
256 display_name = empathy_service_name_to_display_name ("facebook");
257 gtk_list_store_insert_with_values (priv->store,
259 COL_ICON, "im-facebook",
260 COL_LABEL, display_name,
262 COL_PROTOCOL_NAME, proto->name,
263 COL_SERVICE, "facebook",
272 protocol_chooser_add_cms_list (EmpathyProtocolChooser *protocol_chooser,
277 for (l = cms; l != NULL; l = l->next)
278 protocol_choosers_add_cm (protocol_chooser, l->data);
280 gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
284 protocol_chooser_cms_prepare_cb (GObject *source,
285 GAsyncResult *result,
288 EmpathyConnectionManagers *cms = EMPATHY_CONNECTION_MANAGERS (source);
289 EmpathyProtocolChooser *protocol_chooser = user_data;
291 if (!empathy_connection_managers_prepare_finish (cms, result, NULL))
294 protocol_chooser_add_cms_list (protocol_chooser,
295 empathy_connection_managers_get_cms (cms));
299 protocol_chooser_constructed (GObject *object)
301 EmpathyProtocolChooser *protocol_chooser;
302 EmpathyProtocolChooserPriv *priv;
303 GtkCellRenderer *renderer;
305 priv = GET_PRIV (object);
306 protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
308 /* set up combo box with new store */
309 priv->store = gtk_list_store_new (COL_COUNT,
310 G_TYPE_STRING, /* Icon name */
311 G_TYPE_STRING, /* Label */
312 G_TYPE_OBJECT, /* CM */
313 G_TYPE_STRING, /* protocol name */
314 G_TYPE_STRING); /* service */
316 /* Set the protocol sort function */
317 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
319 protocol_chooser_sort_func,
321 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
325 gtk_combo_box_set_model (GTK_COMBO_BOX (object),
326 GTK_TREE_MODEL (priv->store));
328 renderer = gtk_cell_renderer_pixbuf_new ();
329 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
330 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
331 "icon-name", COL_ICON,
333 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
335 renderer = gtk_cell_renderer_text_new ();
336 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
337 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
341 empathy_connection_managers_prepare_async (priv->cms,
342 protocol_chooser_cms_prepare_cb, protocol_chooser);
344 if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
346 (empathy_protocol_chooser_parent_class)->constructed (object);
350 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
352 EmpathyProtocolChooserPriv *priv =
353 G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
354 EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
356 priv->dispose_run = FALSE;
357 priv->cms = empathy_connection_managers_dup_singleton ();
358 priv->protocols = g_hash_table_new_full (g_str_hash, g_str_equal,
361 protocol_chooser->priv = priv;
365 protocol_chooser_finalize (GObject *object)
367 EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
368 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
372 g_hash_table_unref (priv->protocols);
373 priv->protocols = NULL;
376 (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->finalize) (object);
380 protocol_chooser_dispose (GObject *object)
382 EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
383 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
385 if (priv->dispose_run)
388 priv->dispose_run = TRUE;
392 g_object_unref (priv->store);
398 g_object_unref (priv->cms);
402 (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
406 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
408 GObjectClass *object_class = G_OBJECT_CLASS (klass);
410 object_class->constructed = protocol_chooser_constructed;
411 object_class->dispose = protocol_chooser_dispose;
412 object_class->finalize = protocol_chooser_finalize;
414 g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
418 protocol_chooser_filter_visible_func (GtkTreeModel *model,
422 EmpathyProtocolChooser *protocol_chooser = user_data;
423 EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
424 TpConnectionManager *cm = NULL;
425 gchar *protocol_name = NULL;
426 gboolean visible = FALSE;
429 gtk_tree_model_get (model, iter,
431 COL_PROTOCOL_NAME, &protocol_name,
432 COL_SERVICE, &service,
435 if (cm != NULL && protocol_name != NULL)
437 TpConnectionManagerProtocol *protocol;
439 protocol = (TpConnectionManagerProtocol *)
440 tp_connection_manager_get_protocol (cm, protocol_name);
442 if (protocol != NULL)
444 visible = priv->filter_func (cm, protocol, service,
445 priv->filter_user_data);
459 * empathy_protocol_chooser_get_selected_protocol:
460 * @protocol_chooser: an #EmpathyProtocolChooser
462 * Returns a pointer to the selected #TpConnectionManagerProtocol in
465 * Return value: a pointer to the selected #TpConnectionManagerProtocol
467 TpConnectionManager *
468 empathy_protocol_chooser_dup_selected (
469 EmpathyProtocolChooser *protocol_chooser,
470 TpConnectionManagerProtocol **protocol,
474 TpConnectionManager *cm = NULL;
475 GtkTreeModel *cur_model;
477 g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
479 /* get the current model from the chooser, as we could either be filtering
482 cur_model = gtk_combo_box_get_model (GTK_COMBO_BOX (protocol_chooser));
484 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
486 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
490 if (protocol != NULL)
492 gchar *protocol_name = NULL;
494 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
495 COL_PROTOCOL_NAME, &protocol_name,
498 *protocol = (TpConnectionManagerProtocol *)
499 tp_connection_manager_get_protocol (cm, protocol_name);
501 g_free (protocol_name);
503 if (*protocol == NULL)
505 /* For some reason the CM doesn't know about this protocol
514 gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
515 COL_SERVICE, service,
524 * empathy_protocol_chooser_new:
526 * Triggers the creation of a new #EmpathyProtocolChooser.
528 * Return value: a new #EmpathyProtocolChooser widget
532 empathy_protocol_chooser_new (void)
534 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));
538 empathy_protocol_chooser_set_visible (EmpathyProtocolChooser *protocol_chooser,
539 EmpathyProtocolChooserFilterFunc func,
542 EmpathyProtocolChooserPriv *priv;
543 GtkTreeModel *filter_model;
545 g_return_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser));
547 priv = GET_PRIV (protocol_chooser);
548 priv->filter_func = func;
549 priv->filter_user_data = user_data;
551 filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store),
553 gtk_combo_box_set_model (GTK_COMBO_BOX (protocol_chooser), filter_model);
554 g_object_unref (filter_model);
556 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER
557 (filter_model), protocol_chooser_filter_visible_func,
558 protocol_chooser, NULL);
560 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
562 gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
565 EmpathyAccountSettings *
566 empathy_protocol_chooser_create_account_settings (EmpathyProtocolChooser *self)
568 EmpathyAccountSettings *settings = NULL;
570 const gchar *display_name;
571 TpConnectionManager *cm;
572 TpConnectionManagerProtocol *proto;
573 gchar *service = NULL;
575 cm = empathy_protocol_chooser_dup_selected (self, &proto, &service);
576 if (cm == NULL || proto == NULL)
580 display_name = empathy_service_name_to_display_name (service);
582 display_name = empathy_protocol_name_to_display_name (proto->name);
585 /* To translator: %s is the name of the protocol, such as "Google Talk" or
588 str = g_strdup_printf (_("New %s account"), display_name);
589 settings = empathy_account_settings_new (cm->name, proto->name, service, str);
593 if (!tp_strdiff (service, "google-talk"))
595 gchar *fallback_servers[] = {
596 "talkx.l.google.com",
597 "talkx.l.google.com:443,oldssl",
598 "talkx.l.google.com:80",
601 gchar *extra_certificate_identities[] = {
605 empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
607 empathy_account_settings_set_string (settings, "server",
608 extra_certificate_identities[0]);
609 empathy_account_settings_set_boolean (settings, "require-encryption",
611 empathy_account_settings_set_strv (settings, "fallback-servers",
614 if (empathy_account_settings_have_tp_param (settings,
615 "extra-certificate-identities"))
617 empathy_account_settings_set_strv (settings,
618 "extra-certificate-identities", extra_certificate_identities);
621 else if (!tp_strdiff (service, "facebook"))
623 gchar *fallback_servers[] = {
624 "chat.facebook.com:443",
627 empathy_account_settings_set_icon_name_async (settings, "im-facebook",
629 empathy_account_settings_set_boolean (settings, "require-encryption",
631 empathy_account_settings_set_string (settings, "server",
632 "chat.facebook.com");
633 empathy_account_settings_set_strv (settings, "fallback-servers",
638 tp_clear_object (&cm);