]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-protocol-chooser.c
Merge branch 'sasl'
[empathy.git] / libempathy-gtk / empathy-protocol-chooser.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 /*
3  * Copyright (C) 2007-2009 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  *          Jonny Lamb <jonny.lamb@collabora.co.uk>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include <telepathy-glib/util.h>
28
29 #include <gtk/gtk.h>
30
31 #include <glib/gi18n-lib.h>
32
33 #include <libempathy/empathy-utils.h>
34 #include <libempathy/empathy-connection-managers.h>
35
36 #include "empathy-protocol-chooser.h"
37 #include "empathy-ui-utils.h"
38
39 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
40 #include <libempathy/empathy-debug.h>
41
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
47  *
48  * #EmpathyProtocolChooser is a widget which extends #GtkComboBox to provides a
49  * chooser of available protocols.
50  */
51
52 /**
53  * EmpathyProtocolChooser:
54  * @parent: parent object
55  *
56  * Widget which extends #GtkComboBox to provide a chooser of available
57  * protocols.
58  */
59
60 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyProtocolChooser)
61 typedef struct
62 {
63   GtkListStore *store;
64
65   gboolean dispose_run;
66   EmpathyConnectionManagers *cms;
67
68   EmpathyProtocolChooserFilterFunc filter_func;
69   gpointer filter_user_data;
70
71   GHashTable *protocols;
72 } EmpathyProtocolChooserPriv;
73
74 enum
75 {
76   COL_ICON,
77   COL_LABEL,
78   COL_CM,
79   COL_PROTOCOL_NAME,
80   COL_SERVICE,
81   COL_COUNT
82 };
83
84 G_DEFINE_TYPE (EmpathyProtocolChooser, empathy_protocol_chooser,
85     GTK_TYPE_COMBO_BOX);
86
87 static gint
88 protocol_chooser_sort_protocol_value (const gchar *protocol_name)
89 {
90   guint i;
91   const gchar *names[] = {
92     "jabber",
93     "local-xmpp",
94     "gtalk",
95     NULL
96   };
97
98   for (i = 0 ; names[i]; i++)
99     {
100       if (strcmp (protocol_name, names[i]) == 0)
101         return i;
102     }
103
104   return i;
105 }
106
107 static gint
108 protocol_chooser_sort_func (GtkTreeModel *model,
109     GtkTreeIter  *iter_a,
110     GtkTreeIter  *iter_b,
111     gpointer      user_data)
112 {
113   gchar *protocol_a;
114   gchar *protocol_b;
115   gint cmp = 0;
116
117   gtk_tree_model_get (model, iter_a,
118       COL_PROTOCOL_NAME, &protocol_a,
119       -1);
120   gtk_tree_model_get (model, iter_b,
121       COL_PROTOCOL_NAME, &protocol_b,
122       -1);
123
124   cmp = protocol_chooser_sort_protocol_value (protocol_a);
125   cmp -= protocol_chooser_sort_protocol_value (protocol_b);
126   if (cmp == 0)
127     {
128       cmp = strcmp (protocol_a, protocol_b);
129       /* only happens for jabber where there is one entry for gtalk and one for
130        * non-gtalk */
131       if (cmp == 0)
132         {
133           gchar *service;
134
135           gtk_tree_model_get (model, iter_a,
136             COL_SERVICE, &service,
137             -1);
138
139           if (service != NULL)
140             cmp = 1;
141           else
142             cmp = -1;
143
144           g_free (service);
145         }
146     }
147
148   g_free (protocol_a);
149   g_free (protocol_b);
150   return cmp;
151 }
152
153 static void
154 protocol_choosers_add_cm (EmpathyProtocolChooser *chooser,
155     TpConnectionManager *cm)
156 {
157   EmpathyProtocolChooserPriv *priv = GET_PRIV (chooser);
158   const TpConnectionManagerProtocol * const *iter;
159
160   for (iter = cm->protocols; iter != NULL && *iter != NULL; iter++)
161     {
162       const TpConnectionManagerProtocol *proto = *iter;
163       gchar *icon_name;
164       const gchar *display_name;
165       const gchar *saved_cm_name;
166
167       saved_cm_name = g_hash_table_lookup (priv->protocols, proto->name);
168
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
172          * have; drop it.
173          */
174         continue;
175
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 */
180         continue;
181
182       if (!tp_strdiff (cm->name, "haze") &&
183           !tp_strdiff (proto->name, "sip"))
184         /* Haze's SIP implementation is pretty useless (bgo #629736) */
185         continue;
186
187       if (tp_strdiff (cm->name, "haze") && !tp_strdiff (saved_cm_name, "haze"))
188         {
189           GtkTreeIter titer;
190           gboolean valid;
191           TpConnectionManager *haze_cm;
192
193           /* let's this CM replace the haze implementation */
194           valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->store),
195               &titer);
196
197           while (valid)
198             {
199               gchar *haze_proto_name = NULL;
200
201               gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &titer,
202                   COL_PROTOCOL_NAME, &haze_proto_name,
203                   COL_CM, &haze_cm, -1);
204
205               if (haze_cm == NULL)
206                 continue;
207
208               if (!tp_strdiff (haze_cm->name, "haze") &&
209                   !tp_strdiff (haze_proto_name, proto->name))
210                 {
211                   gtk_list_store_remove (priv->store, &titer);
212                   g_object_unref (haze_cm);
213                   g_free (haze_proto_name);
214                   break;
215                 }
216
217               g_object_unref (haze_cm);
218               g_free (haze_proto_name);
219               valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->store),
220                   &titer);
221             }
222         }
223
224       g_hash_table_insert (priv->protocols,
225           g_strdup (proto->name), g_strdup (cm->name));
226
227       icon_name = empathy_protocol_icon_name (proto->name);
228
229       display_name = empathy_protocol_name_to_display_name (proto->name);
230
231       gtk_list_store_insert_with_values (priv->store,
232           NULL, 0,
233           COL_ICON, icon_name,
234           COL_LABEL, display_name,
235           COL_CM, cm,
236           COL_PROTOCOL_NAME, proto->name,
237           -1);
238
239       if (!tp_strdiff (proto->name, "jabber") &&
240           !tp_strdiff (cm->name, "gabble"))
241         {
242           display_name = empathy_service_name_to_display_name ("google-talk");
243           gtk_list_store_insert_with_values (priv->store,
244              NULL, 0,
245              COL_ICON, "im-google-talk",
246              COL_LABEL, display_name,
247              COL_CM, cm,
248              COL_PROTOCOL_NAME, proto->name,
249              COL_SERVICE, "google-talk",
250              -1);
251
252           display_name = empathy_service_name_to_display_name ("facebook");
253           gtk_list_store_insert_with_values (priv->store,
254              NULL, 0,
255              COL_ICON, "im-facebook",
256              COL_LABEL, display_name,
257              COL_CM, cm,
258              COL_PROTOCOL_NAME, proto->name,
259              COL_SERVICE, "facebook",
260              -1);
261         }
262
263       g_free (icon_name);
264     }
265 }
266
267 static void
268 protocol_chooser_add_cms_list (EmpathyProtocolChooser *protocol_chooser,
269     GList *cms)
270 {
271   GList *l;
272
273   for (l = cms; l != NULL; l = l->next)
274     protocol_choosers_add_cm (protocol_chooser, l->data);
275
276   gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
277 }
278
279 static void
280 protocol_chooser_cms_prepare_cb (GObject *source,
281     GAsyncResult *result,
282     gpointer user_data)
283 {
284   EmpathyConnectionManagers *cms = EMPATHY_CONNECTION_MANAGERS (source);
285   EmpathyProtocolChooser *protocol_chooser = user_data;
286
287   if (!empathy_connection_managers_prepare_finish (cms, result, NULL))
288     return;
289
290   protocol_chooser_add_cms_list (protocol_chooser,
291       empathy_connection_managers_get_cms (cms));
292 }
293
294 static void
295 protocol_chooser_constructed (GObject *object)
296 {
297   EmpathyProtocolChooser *protocol_chooser;
298   EmpathyProtocolChooserPriv *priv;
299   GtkCellRenderer *renderer;
300
301   priv = GET_PRIV (object);
302   protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
303
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_STRING);   /* service */
311
312   /* Set the protocol sort function */
313   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
314       COL_PROTOCOL_NAME,
315       protocol_chooser_sort_func,
316       NULL, NULL);
317   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
318       COL_PROTOCOL_NAME,
319       GTK_SORT_ASCENDING);
320
321   gtk_combo_box_set_model (GTK_COMBO_BOX (object),
322       GTK_TREE_MODEL (priv->store));
323
324   renderer = gtk_cell_renderer_pixbuf_new ();
325   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
326   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
327       "icon-name", COL_ICON,
328       NULL);
329   g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
330
331   renderer = gtk_cell_renderer_text_new ();
332   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
333   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
334       "text", COL_LABEL,
335       NULL);
336
337   empathy_connection_managers_prepare_async (priv->cms,
338       protocol_chooser_cms_prepare_cb, protocol_chooser);
339
340   if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
341     G_OBJECT_CLASS
342       (empathy_protocol_chooser_parent_class)->constructed (object);
343 }
344
345 static void
346 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
347 {
348   EmpathyProtocolChooserPriv *priv =
349     G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
350         EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
351
352   priv->dispose_run = FALSE;
353   priv->cms = empathy_connection_managers_dup_singleton ();
354   priv->protocols = g_hash_table_new_full (g_str_hash, g_str_equal,
355       g_free, g_free);
356
357   protocol_chooser->priv = priv;
358 }
359
360 static void
361 protocol_chooser_finalize (GObject *object)
362 {
363   EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
364   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
365
366   if (priv->protocols)
367     {
368       g_hash_table_destroy (priv->protocols);
369       priv->protocols = NULL;
370     }
371
372   (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->finalize) (object);
373 }
374
375 static void
376 protocol_chooser_dispose (GObject *object)
377 {
378   EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
379   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
380
381   if (priv->dispose_run)
382     return;
383
384   priv->dispose_run = TRUE;
385
386   if (priv->store)
387     {
388       g_object_unref (priv->store);
389       priv->store = NULL;
390     }
391
392   if (priv->cms)
393     {
394       g_object_unref (priv->cms);
395       priv->cms = NULL;
396     }
397
398   (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
399 }
400
401 static void
402 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
403 {
404   GObjectClass *object_class = G_OBJECT_CLASS (klass);
405
406   object_class->constructed = protocol_chooser_constructed;
407   object_class->dispose = protocol_chooser_dispose;
408   object_class->finalize = protocol_chooser_finalize;
409
410   g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
411 }
412
413 static gboolean
414 protocol_chooser_filter_visible_func (GtkTreeModel *model,
415     GtkTreeIter *iter,
416     gpointer user_data)
417 {
418   EmpathyProtocolChooser *protocol_chooser = user_data;
419   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
420   TpConnectionManager *cm = NULL;
421   gchar *protocol_name = NULL;
422   gboolean visible = FALSE;
423   gchar *service;
424
425   gtk_tree_model_get (model, iter,
426       COL_CM, &cm,
427       COL_PROTOCOL_NAME, &protocol_name,
428       COL_SERVICE, &service,
429       -1);
430
431   if (cm != NULL && protocol_name != NULL)
432     {
433       TpConnectionManagerProtocol *protocol;
434
435       protocol = (TpConnectionManagerProtocol *)
436         tp_connection_manager_get_protocol (cm, protocol_name);
437
438       if (protocol != NULL)
439         {
440           visible = priv->filter_func (cm, protocol, service,
441               priv->filter_user_data);
442         }
443     }
444
445   if (cm != NULL)
446     g_object_unref (cm);
447
448   g_free (service);
449   return visible;
450 }
451
452 /* public methods */
453
454 /**
455  * empathy_protocol_chooser_get_selected_protocol:
456  * @protocol_chooser: an #EmpathyProtocolChooser
457  *
458  * Returns a pointer to the selected #TpConnectionManagerProtocol in
459  * @protocol_chooser.
460  *
461  * Return value: a pointer to the selected #TpConnectionManagerProtocol
462  */
463 TpConnectionManager *
464 empathy_protocol_chooser_dup_selected (
465     EmpathyProtocolChooser *protocol_chooser,
466     TpConnectionManagerProtocol **protocol,
467     gchar **service)
468 {
469   GtkTreeIter iter;
470   TpConnectionManager *cm = NULL;
471   GtkTreeModel *cur_model;
472
473   g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
474
475   /* get the current model from the chooser, as we could either be filtering
476    * or not.
477    */
478   cur_model = gtk_combo_box_get_model (GTK_COMBO_BOX (protocol_chooser));
479
480   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
481     {
482       gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
483           COL_CM, &cm,
484           -1);
485
486       if (protocol != NULL)
487         {
488           gchar *protocol_name = NULL;
489
490           gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
491               COL_PROTOCOL_NAME, &protocol_name,
492               -1);
493
494           *protocol = (TpConnectionManagerProtocol *)
495             tp_connection_manager_get_protocol (cm, protocol_name);
496
497           g_free (protocol_name);
498
499           if (*protocol == NULL)
500             {
501               /* For some reason the CM doesn't know about this protocol
502                * any more */
503               g_object_unref (cm);
504               return NULL;
505             }
506         }
507
508       if (service != NULL)
509         {
510           gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
511               COL_SERVICE, service,
512               -1);
513         }
514     }
515
516   return cm;
517 }
518
519 /**
520  * empathy_protocol_chooser_new:
521  *
522  * Triggers the creation of a new #EmpathyProtocolChooser.
523  *
524  * Return value: a new #EmpathyProtocolChooser widget
525  */
526
527 GtkWidget *
528 empathy_protocol_chooser_new (void)
529 {
530   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));
531 }
532
533 void
534 empathy_protocol_chooser_set_visible (EmpathyProtocolChooser *protocol_chooser,
535     EmpathyProtocolChooserFilterFunc func,
536     gpointer user_data)
537 {
538   EmpathyProtocolChooserPriv *priv;
539   GtkTreeModel *filter_model;
540
541   g_return_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser));
542
543   priv = GET_PRIV (protocol_chooser);
544   priv->filter_func = func;
545   priv->filter_user_data = user_data;
546
547   filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store),
548       NULL);
549   gtk_combo_box_set_model (GTK_COMBO_BOX (protocol_chooser), filter_model);
550   g_object_unref (filter_model);
551
552   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER
553       (filter_model), protocol_chooser_filter_visible_func,
554       protocol_chooser, NULL);
555
556   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
557
558   gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
559 }
560
561 EmpathyAccountSettings *
562 empathy_protocol_chooser_create_account_settings (EmpathyProtocolChooser *self)
563 {
564   EmpathyAccountSettings *settings;
565   gchar *str;
566   const gchar *display_name;
567   TpConnectionManager *cm;
568   TpConnectionManagerProtocol *proto;
569   gchar *service;
570
571   cm = empathy_protocol_chooser_dup_selected (self, &proto, &service);
572   if (cm == NULL || proto == NULL)
573     return NULL;
574
575   if (service != NULL)
576     display_name = empathy_service_name_to_display_name (service);
577   else
578     display_name = empathy_protocol_name_to_display_name (proto->name);
579
580   /* Create account */
581   /* To translator: %s is the name of the protocol, such as "Google Talk" or
582    * "Yahoo!"
583    */
584   str = g_strdup_printf (_("New %s account"), display_name);
585   settings = empathy_account_settings_new (cm->name, proto->name, service, str);
586
587   g_free (str);
588
589   if (!tp_strdiff (service, "google-talk"))
590     {
591       gchar *fallback_servers[] = {
592           "talkx.l.google.com",
593           "talkx.l.google.com:443,oldssl",
594           "talkx.l.google.com:80",
595           NULL};
596
597       empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
598           NULL, NULL);
599
600       empathy_account_settings_set_strv (settings, "fallback-servers",
601           fallback_servers);
602     }
603   else if (!tp_strdiff (service, "facebook"))
604     {
605       empathy_account_settings_set_icon_name_async (settings, "im-facebook",
606           NULL, NULL);
607
608       empathy_account_settings_set_string (settings, "server",
609           "chat.facebook.com");
610     }
611
612   g_object_unref (cm);
613   g_free (service);
614   return settings;
615 }