]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-protocol-chooser.c
8c0df8face1642611653930b4169ed4bec7d80a9
[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   GList *protocols, *l;
159   const gchar *cm_name;
160
161   cm_name = tp_connection_manager_get_name (cm);
162
163   protocols = tp_connection_manager_dup_protocols (cm);
164
165   for (l = protocols; l != NULL; l = g_list_next (l))
166     {
167       TpProtocol *protocol = l->data;
168       gchar *icon_name;
169       const gchar *display_name;
170       const gchar *saved_cm_name;
171       const gchar *proto_name;
172       GdkPixbuf *pixbuf;
173
174       proto_name = tp_protocol_get_name (protocol);
175       saved_cm_name = g_hash_table_lookup (priv->protocols, proto_name);
176
177       if (!tp_strdiff (cm_name, "haze") && saved_cm_name != NULL &&
178           tp_strdiff (saved_cm_name, "haze"))
179         /* the CM we're adding is a haze implementation of something we already
180          * have; drop it.
181          */
182         continue;
183
184       if (!tp_strdiff (cm_name, "haze") &&
185           !tp_strdiff (proto_name, "facebook"))
186         /* Facebook now supports XMPP so drop the purple facebook plugin; user
187          * should use Gabble */
188         continue;
189
190       if (!tp_strdiff (cm_name, "haze") &&
191           !tp_strdiff (proto_name, "sip"))
192         /* Haze's SIP implementation is pretty useless (bgo #629736) */
193         continue;
194
195       if (!tp_strdiff (cm_name, "butterfly"))
196         /* Butterfly isn't supported any more */
197         continue;
198
199       if (tp_strdiff (cm_name, "haze") && !tp_strdiff (saved_cm_name, "haze"))
200         {
201           GtkTreeIter titer;
202           gboolean valid;
203           TpConnectionManager *haze_cm;
204
205           /* let's this CM replace the haze implementation */
206           valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->store),
207               &titer);
208
209           while (valid)
210             {
211               gchar *haze_proto_name = NULL;
212
213               gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &titer,
214                   COL_PROTOCOL_NAME, &haze_proto_name,
215                   COL_CM, &haze_cm, -1);
216
217               if (haze_cm == NULL)
218                 continue;
219
220               if (!tp_strdiff (tp_connection_manager_get_name (haze_cm), "haze")
221                   && !tp_strdiff (haze_proto_name, proto_name))
222                 {
223                   gtk_list_store_remove (priv->store, &titer);
224                   g_object_unref (haze_cm);
225                   g_free (haze_proto_name);
226                   break;
227                 }
228
229               g_object_unref (haze_cm);
230               g_free (haze_proto_name);
231               valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->store),
232                   &titer);
233             }
234         }
235
236       g_hash_table_insert (priv->protocols,
237           g_strdup (proto_name), g_strdup (cm_name));
238
239       icon_name = empathy_protocol_icon_name (proto_name);
240       pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
241
242       display_name = empathy_protocol_name_to_display_name (proto_name);
243
244       gtk_list_store_insert_with_values (priv->store,
245           NULL, 0,
246           COL_ICON, pixbuf,
247           COL_LABEL, display_name,
248           COL_CM, cm,
249           COL_PROTOCOL_NAME, proto_name,
250           -1);
251
252       g_clear_object (&pixbuf);
253
254       if (!tp_strdiff (proto_name, "jabber") &&
255           !tp_strdiff (cm_name, "gabble"))
256         {
257           display_name = empathy_service_name_to_display_name ("google-talk");
258           pixbuf = empathy_pixbuf_from_icon_name ("im-google-talk",
259                   GTK_ICON_SIZE_BUTTON);
260
261           gtk_list_store_insert_with_values (priv->store,
262              NULL, 0,
263              COL_ICON, pixbuf,
264              COL_LABEL, display_name,
265              COL_CM, cm,
266              COL_PROTOCOL_NAME, proto_name,
267              COL_SERVICE, "google-talk",
268              -1);
269
270           g_clear_object (&pixbuf);
271
272           display_name = empathy_service_name_to_display_name ("facebook");
273           pixbuf = empathy_pixbuf_from_icon_name ("im-facebook",
274                   GTK_ICON_SIZE_BUTTON);
275
276           gtk_list_store_insert_with_values (priv->store,
277              NULL, 0,
278              COL_ICON, pixbuf,
279              COL_LABEL, display_name,
280              COL_CM, cm,
281              COL_PROTOCOL_NAME, proto_name,
282              COL_SERVICE, "facebook",
283              -1);
284
285           g_clear_object (&pixbuf);
286         }
287
288       g_free (icon_name);
289     }
290
291   g_list_free_full (protocols, g_object_unref);
292 }
293
294 static void
295 protocol_chooser_add_cms_list (EmpathyProtocolChooser *protocol_chooser,
296     GList *cms)
297 {
298   GList *l;
299
300   for (l = cms; l != NULL; l = l->next)
301     protocol_choosers_add_cm (protocol_chooser, l->data);
302
303   gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
304 }
305
306 static void
307 protocol_chooser_cms_prepare_cb (GObject *source,
308     GAsyncResult *result,
309     gpointer user_data)
310 {
311   EmpathyConnectionManagers *cms = EMPATHY_CONNECTION_MANAGERS (source);
312   EmpathyProtocolChooser *protocol_chooser = user_data;
313
314   if (!empathy_connection_managers_prepare_finish (cms, result, NULL))
315     return;
316
317   protocol_chooser_add_cms_list (protocol_chooser,
318       empathy_connection_managers_get_cms (cms));
319 }
320
321 static void
322 protocol_chooser_constructed (GObject *object)
323 {
324   EmpathyProtocolChooser *protocol_chooser;
325   EmpathyProtocolChooserPriv *priv;
326   GtkCellRenderer *renderer;
327
328   priv = GET_PRIV (object);
329   protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
330
331   /* set up combo box with new store */
332   priv->store = gtk_list_store_new (COL_COUNT,
333           GDK_TYPE_PIXBUF,  /* Icon */
334           G_TYPE_STRING,    /* Label     */
335           G_TYPE_OBJECT,    /* CM */
336           G_TYPE_STRING,    /* protocol name  */
337           G_TYPE_STRING);   /* service */
338
339   /* Set the protocol sort function */
340   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
341       COL_PROTOCOL_NAME,
342       protocol_chooser_sort_func,
343       NULL, NULL);
344   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
345       COL_PROTOCOL_NAME,
346       GTK_SORT_ASCENDING);
347
348   gtk_combo_box_set_model (GTK_COMBO_BOX (object),
349       GTK_TREE_MODEL (priv->store));
350
351   renderer = gtk_cell_renderer_pixbuf_new ();
352   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
353   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
354       "pixbuf", COL_ICON,
355       NULL);
356
357   renderer = gtk_cell_renderer_text_new ();
358   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
359   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
360       "text", COL_LABEL,
361       NULL);
362
363   empathy_connection_managers_prepare_async (priv->cms,
364       protocol_chooser_cms_prepare_cb, protocol_chooser);
365
366   if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
367     G_OBJECT_CLASS
368       (empathy_protocol_chooser_parent_class)->constructed (object);
369 }
370
371 static void
372 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
373 {
374   EmpathyProtocolChooserPriv *priv =
375     G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
376         EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
377
378   priv->dispose_run = FALSE;
379   priv->cms = empathy_connection_managers_dup_singleton ();
380   priv->protocols = g_hash_table_new_full (g_str_hash, g_str_equal,
381       g_free, g_free);
382
383   protocol_chooser->priv = priv;
384 }
385
386 static void
387 protocol_chooser_finalize (GObject *object)
388 {
389   EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
390   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
391
392   if (priv->protocols)
393     {
394       g_hash_table_unref (priv->protocols);
395       priv->protocols = NULL;
396     }
397
398   (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->finalize) (object);
399 }
400
401 static void
402 protocol_chooser_dispose (GObject *object)
403 {
404   EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
405   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
406
407   if (priv->dispose_run)
408     return;
409
410   priv->dispose_run = TRUE;
411
412   if (priv->store)
413     {
414       g_object_unref (priv->store);
415       priv->store = NULL;
416     }
417
418   if (priv->cms)
419     {
420       g_object_unref (priv->cms);
421       priv->cms = NULL;
422     }
423
424   (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
425 }
426
427 static void
428 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
429 {
430   GObjectClass *object_class = G_OBJECT_CLASS (klass);
431
432   object_class->constructed = protocol_chooser_constructed;
433   object_class->dispose = protocol_chooser_dispose;
434   object_class->finalize = protocol_chooser_finalize;
435
436   g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
437 }
438
439 static gboolean
440 protocol_chooser_filter_visible_func (GtkTreeModel *model,
441     GtkTreeIter *iter,
442     gpointer user_data)
443 {
444   EmpathyProtocolChooser *protocol_chooser = user_data;
445   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
446   TpConnectionManager *cm = NULL;
447   gchar *protocol_name = NULL;
448   gboolean visible = FALSE;
449   gchar *service;
450
451   gtk_tree_model_get (model, iter,
452       COL_CM, &cm,
453       COL_PROTOCOL_NAME, &protocol_name,
454       COL_SERVICE, &service,
455       -1);
456
457   if (cm != NULL && protocol_name != NULL)
458     {
459       TpProtocol *protocol;
460
461       protocol = tp_connection_manager_get_protocol_object (cm, protocol_name);
462
463       if (protocol != NULL)
464         {
465           visible = priv->filter_func (cm, protocol, service,
466               priv->filter_user_data);
467         }
468     }
469
470   if (cm != NULL)
471     g_object_unref (cm);
472
473   g_free (service);
474   return visible;
475 }
476
477 /* public methods */
478
479 /**
480  * empathy_protocol_chooser_get_selected_protocol:
481  * @protocol_chooser: an #EmpathyProtocolChooser
482  *
483  * Returns a pointer to the selected #TpConnectionManagerProtocol in
484  * @protocol_chooser.
485  *
486  * Return value: a pointer to the selected #TpConnectionManagerProtocol
487  */
488 TpConnectionManager *
489 empathy_protocol_chooser_dup_selected (
490     EmpathyProtocolChooser *protocol_chooser,
491     TpProtocol **protocol,
492     gchar **service)
493 {
494   GtkTreeIter iter;
495   TpConnectionManager *cm = NULL;
496   GtkTreeModel *cur_model;
497
498   g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
499
500   /* get the current model from the chooser, as we could either be filtering
501    * or not.
502    */
503   cur_model = gtk_combo_box_get_model (GTK_COMBO_BOX (protocol_chooser));
504
505   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
506     {
507       gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
508           COL_CM, &cm,
509           -1);
510
511       if (protocol != NULL)
512         {
513           gchar *protocol_name = NULL;
514
515           gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
516               COL_PROTOCOL_NAME, &protocol_name,
517               -1);
518
519           *protocol = tp_connection_manager_get_protocol_object (cm,
520               protocol_name);
521
522           g_free (protocol_name);
523
524           if (*protocol == NULL)
525             {
526               /* For some reason the CM doesn't know about this protocol
527                * any more */
528               g_object_unref (cm);
529               return NULL;
530             }
531         }
532
533       if (service != NULL)
534         {
535           gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
536               COL_SERVICE, service,
537               -1);
538         }
539     }
540
541   return cm;
542 }
543
544 /**
545  * empathy_protocol_chooser_new:
546  *
547  * Triggers the creation of a new #EmpathyProtocolChooser.
548  *
549  * Return value: a new #EmpathyProtocolChooser widget
550  */
551
552 GtkWidget *
553 empathy_protocol_chooser_new (void)
554 {
555   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));
556 }
557
558 void
559 empathy_protocol_chooser_set_visible (EmpathyProtocolChooser *protocol_chooser,
560     EmpathyProtocolChooserFilterFunc func,
561     gpointer user_data)
562 {
563   EmpathyProtocolChooserPriv *priv;
564   GtkTreeModel *filter_model;
565
566   g_return_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser));
567
568   priv = GET_PRIV (protocol_chooser);
569   priv->filter_func = func;
570   priv->filter_user_data = user_data;
571
572   filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store),
573       NULL);
574   gtk_combo_box_set_model (GTK_COMBO_BOX (protocol_chooser), filter_model);
575   g_object_unref (filter_model);
576
577   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER
578       (filter_model), protocol_chooser_filter_visible_func,
579       protocol_chooser, NULL);
580
581   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
582
583   gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
584 }
585
586 EmpathyAccountSettings *
587 empathy_protocol_chooser_create_account_settings (EmpathyProtocolChooser *self)
588 {
589   EmpathyAccountSettings *settings = NULL;
590   gchar *str;
591   const gchar *display_name;
592   TpConnectionManager *cm;
593   TpProtocol *proto;
594   gchar *service = NULL;
595
596   cm = empathy_protocol_chooser_dup_selected (self, &proto, &service);
597   if (cm == NULL || proto == NULL)
598     goto out;
599
600   if (service != NULL)
601     display_name = empathy_service_name_to_display_name (service);
602   else
603     display_name = empathy_protocol_name_to_display_name (
604         tp_protocol_get_name (proto));
605
606   /* Create account */
607   /* To translator: %s is the name of the protocol, such as "Google Talk" or
608    * "Yahoo!"
609    */
610   str = g_strdup_printf (_("New %s account"), display_name);
611
612   settings = empathy_account_settings_new (tp_connection_manager_get_name (cm),
613       tp_protocol_get_name (proto), service, str);
614
615   g_free (str);
616
617   if (!tp_strdiff (service, "google-talk"))
618     {
619       const gchar *fallback_servers[] = {
620           "talkx.l.google.com",
621           "talkx.l.google.com:443,oldssl",
622           "talkx.l.google.com:80",
623           NULL};
624
625       const gchar *extra_certificate_identities[] = {
626           "talk.google.com",
627           NULL};
628
629       empathy_account_settings_set_icon_name_async (settings, "im-google-talk",
630           NULL, NULL);
631       empathy_account_settings_set (settings, "server",
632           g_variant_new_string (extra_certificate_identities[0]));
633       empathy_account_settings_set (settings, "require-encryption",
634           g_variant_new_boolean (TRUE));
635       empathy_account_settings_set (settings, "fallback-servers",
636           g_variant_new_strv (fallback_servers, -1));
637
638       if (empathy_account_settings_have_tp_param (settings,
639               "extra-certificate-identities"))
640         {
641           empathy_account_settings_set (settings,
642               "extra-certificate-identities",
643               g_variant_new_strv (extra_certificate_identities, -1));
644         }
645     }
646   else if (!tp_strdiff (service, "facebook"))
647     {
648       const gchar *fallback_servers[] = {
649           "chat.facebook.com:443",
650           NULL };
651
652       empathy_account_settings_set_icon_name_async (settings, "im-facebook",
653           NULL, NULL);
654       empathy_account_settings_set (settings, "require-encryption",
655           g_variant_new_boolean (TRUE));
656       empathy_account_settings_set (settings, "server",
657           g_variant_new_string ("chat.facebook.com"));
658       empathy_account_settings_set (settings, "fallback-servers",
659           g_variant_new_strv (fallback_servers, -1));
660     }
661
662 out:
663   tp_clear_object (&cm);
664   g_free (service);
665   return settings;
666 }