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