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