]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-protocol-chooser.c
Update Simplifid Chinese translation.
[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_IS_GTALK,
81   COL_IS_FACEBOOK,
82   COL_COUNT
83 };
84
85 G_DEFINE_TYPE (EmpathyProtocolChooser, empathy_protocol_chooser,
86     GTK_TYPE_COMBO_BOX);
87
88 static gint
89 protocol_chooser_sort_protocol_value (const gchar *protocol_name)
90 {
91   guint i;
92   const gchar *names[] = {
93     "jabber",
94     "local-xmpp",
95     "gtalk",
96     NULL
97   };
98
99   for (i = 0 ; names[i]; i++)
100     {
101       if (strcmp (protocol_name, names[i]) == 0)
102         return i;
103     }
104
105   return i;
106 }
107
108 static gint
109 protocol_chooser_sort_func (GtkTreeModel *model,
110     GtkTreeIter  *iter_a,
111     GtkTreeIter  *iter_b,
112     gpointer      user_data)
113 {
114   gchar *protocol_a;
115   gchar *protocol_b;
116   gint cmp = 0;
117
118   gtk_tree_model_get (model, iter_a,
119       COL_PROTOCOL_NAME, &protocol_a,
120       -1);
121   gtk_tree_model_get (model, iter_b,
122       COL_PROTOCOL_NAME, &protocol_b,
123       -1);
124
125   cmp = protocol_chooser_sort_protocol_value (protocol_a);
126   cmp -= protocol_chooser_sort_protocol_value (protocol_b);
127   if (cmp == 0)
128     {
129       cmp = strcmp (protocol_a, protocol_b);
130       /* only happens for jabber where there is one entry for gtalk and one for
131        * non-gtalk */
132       if (cmp == 0)
133         {
134           gboolean is_gtalk, is_facebook;
135           gtk_tree_model_get (model, iter_a,
136             COL_IS_GTALK, &is_gtalk,
137             COL_IS_FACEBOOK, &is_facebook,
138             -1);
139
140           if (is_gtalk || is_facebook)
141             cmp = 1;
142           else
143             cmp = -1;
144         }
145     }
146
147   g_free (protocol_a);
148   g_free (protocol_b);
149   return cmp;
150 }
151
152 static void
153 protocol_choosers_add_cm (EmpathyProtocolChooser *chooser,
154     TpConnectionManager *cm)
155 {
156   EmpathyProtocolChooserPriv *priv = GET_PRIV (chooser);
157   const TpConnectionManagerProtocol * const *iter;
158
159   for (iter = cm->protocols; iter != NULL && *iter != NULL; iter++)
160     {
161       const TpConnectionManagerProtocol *proto = *iter;
162       gchar *icon_name;
163       const gchar *display_name;
164       const gchar *saved_cm_name;
165
166       saved_cm_name = g_hash_table_lookup (priv->protocols, proto->name);
167
168       if (!tp_strdiff (cm->name, "haze") && saved_cm_name != NULL &&
169           tp_strdiff (saved_cm_name, "haze"))
170         /* the CM we're adding is a haze implementation of something we already
171          * have; drop it.
172          */
173         continue;
174
175       if (!tp_strdiff (cm->name, "haze") &&
176           !tp_strdiff (proto->name, "facebook"))
177         /* Facebook now supports XMPP so drop the purple facebook plugin; user
178          * should use Gabble */
179         continue;
180
181       if (!tp_strdiff (cm->name, "haze") &&
182           !tp_strdiff (proto->name, "sip"))
183         /* Haze's SIP implementation is pretty useless (bgo #629736) */
184         continue;
185
186       if (tp_strdiff (cm->name, "haze") && !tp_strdiff (saved_cm_name, "haze"))
187         {
188           GtkTreeIter titer;
189           gboolean valid;
190           TpConnectionManager *haze_cm;
191
192           /* let's this CM replace the haze implementation */
193           valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->store),
194               &titer);
195
196           while (valid)
197             {
198               gchar *haze_proto_name = NULL;
199
200               gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &titer,
201                   COL_PROTOCOL_NAME, &haze_proto_name,
202                   COL_CM, &haze_cm, -1);
203
204               if (haze_cm == NULL)
205                 continue;
206
207               if (!tp_strdiff (haze_cm->name, "haze") &&
208                   !tp_strdiff (haze_proto_name, proto->name))
209                 {
210                   gtk_list_store_remove (priv->store, &titer);
211                   g_object_unref (haze_cm);
212                   g_free (haze_proto_name);
213                   break;
214                 }
215
216               g_object_unref (haze_cm);
217               g_free (haze_proto_name);
218               valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->store),
219                   &titer);
220             }
221         }
222
223       g_hash_table_insert (priv->protocols,
224           g_strdup (proto->name), g_strdup (cm->name));
225
226       icon_name = empathy_protocol_icon_name (proto->name);
227       display_name = empathy_protocol_name_to_display_name (proto->name);
228
229       if (display_name == NULL)
230         display_name = proto->name;
231
232       gtk_list_store_insert_with_values (priv->store,
233           NULL, 0,
234           COL_ICON, icon_name,
235           COL_LABEL, display_name,
236           COL_CM, cm,
237           COL_PROTOCOL_NAME, proto->name,
238           COL_IS_GTALK, FALSE,
239           COL_IS_FACEBOOK, FALSE,
240           -1);
241
242       if (!tp_strdiff (proto->name, "jabber") &&
243           !tp_strdiff (cm->name, "gabble"))
244         {
245           display_name = empathy_protocol_name_to_display_name ("gtalk");
246           gtk_list_store_insert_with_values (priv->store,
247              NULL, 0,
248              COL_ICON, "im-google-talk",
249              COL_LABEL, display_name,
250              COL_CM, cm,
251              COL_PROTOCOL_NAME, proto->name,
252              COL_IS_GTALK, TRUE,
253              COL_IS_FACEBOOK, FALSE,
254              -1);
255
256           display_name = empathy_protocol_name_to_display_name ("facebook");
257           gtk_list_store_insert_with_values (priv->store,
258              NULL, 0,
259              COL_ICON, "im-facebook",
260              COL_LABEL, display_name,
261              COL_CM, cm,
262              COL_PROTOCOL_NAME, proto->name,
263              COL_IS_GTALK, FALSE,
264              COL_IS_FACEBOOK, TRUE,
265              -1);
266         }
267
268       g_free (icon_name);
269     }
270 }
271
272 static void
273 protocol_chooser_add_cms_list (EmpathyProtocolChooser *protocol_chooser,
274     GList *cms)
275 {
276   GList *l;
277
278   for (l = cms; l != NULL; l = l->next)
279     protocol_choosers_add_cm (protocol_chooser, l->data);
280
281   gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
282 }
283
284 static void
285 protocol_chooser_cms_prepare_cb (GObject *source,
286     GAsyncResult *result,
287     gpointer user_data)
288 {
289   EmpathyConnectionManagers *cms = EMPATHY_CONNECTION_MANAGERS (source);
290   EmpathyProtocolChooser *protocol_chooser = user_data;
291
292   if (!empathy_connection_managers_prepare_finish (cms, result, NULL))
293     return;
294
295   protocol_chooser_add_cms_list (protocol_chooser,
296       empathy_connection_managers_get_cms (cms));
297 }
298
299 static void
300 protocol_chooser_constructed (GObject *object)
301 {
302   EmpathyProtocolChooser *protocol_chooser;
303   EmpathyProtocolChooserPriv *priv;
304   GtkCellRenderer *renderer;
305
306   priv = GET_PRIV (object);
307   protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
308
309   /* set up combo box with new store */
310   priv->store = gtk_list_store_new (COL_COUNT,
311           G_TYPE_STRING,    /* Icon name */
312           G_TYPE_STRING,    /* Label     */
313           G_TYPE_OBJECT,    /* CM */
314           G_TYPE_STRING,    /* protocol name  */
315           G_TYPE_BOOLEAN,   /* is gtalk  */
316           G_TYPE_BOOLEAN);  /* is facebook  */
317
318   /* Set the protocol sort function */
319   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
320       COL_PROTOCOL_NAME,
321       protocol_chooser_sort_func,
322       NULL, NULL);
323   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
324       COL_PROTOCOL_NAME,
325       GTK_SORT_ASCENDING);
326
327   gtk_combo_box_set_model (GTK_COMBO_BOX (object),
328       GTK_TREE_MODEL (priv->store));
329
330   renderer = gtk_cell_renderer_pixbuf_new ();
331   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
332   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
333       "icon-name", COL_ICON,
334       NULL);
335   g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
336
337   renderer = gtk_cell_renderer_text_new ();
338   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
339   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
340       "text", COL_LABEL,
341       NULL);
342
343   empathy_connection_managers_prepare_async (priv->cms,
344       protocol_chooser_cms_prepare_cb, protocol_chooser);
345
346   if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
347     G_OBJECT_CLASS
348       (empathy_protocol_chooser_parent_class)->constructed (object);
349 }
350
351 static void
352 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
353 {
354   EmpathyProtocolChooserPriv *priv =
355     G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
356         EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
357
358   priv->dispose_run = FALSE;
359   priv->cms = empathy_connection_managers_dup_singleton ();
360   priv->protocols = g_hash_table_new_full (g_str_hash, g_str_equal,
361       g_free, g_free);
362
363   protocol_chooser->priv = priv;
364 }
365
366 static void
367 protocol_chooser_finalize (GObject *object)
368 {
369   EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
370   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
371
372   if (priv->protocols)
373     {
374       g_hash_table_destroy (priv->protocols);
375       priv->protocols = NULL;
376     }
377
378   (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->finalize) (object);
379 }
380
381 static void
382 protocol_chooser_dispose (GObject *object)
383 {
384   EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
385   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
386
387   if (priv->dispose_run)
388     return;
389
390   priv->dispose_run = TRUE;
391
392   if (priv->store)
393     {
394       g_object_unref (priv->store);
395       priv->store = NULL;
396     }
397
398   if (priv->cms)
399     {
400       g_object_unref (priv->cms);
401       priv->cms = NULL;
402     }
403
404   (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
405 }
406
407 static void
408 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
409 {
410   GObjectClass *object_class = G_OBJECT_CLASS (klass);
411
412   object_class->constructed = protocol_chooser_constructed;
413   object_class->dispose = protocol_chooser_dispose;
414   object_class->finalize = protocol_chooser_finalize;
415
416   g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
417 }
418
419 static gboolean
420 protocol_chooser_filter_visible_func (GtkTreeModel *model,
421     GtkTreeIter *iter,
422     gpointer user_data)
423 {
424   EmpathyProtocolChooser *protocol_chooser = user_data;
425   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
426   TpConnectionManager *cm = NULL;
427   gchar *protocol_name = NULL;
428   gboolean visible = FALSE;
429   gboolean is_gtalk, is_facebook;
430
431   gtk_tree_model_get (model, iter,
432       COL_CM, &cm,
433       COL_PROTOCOL_NAME, &protocol_name,
434       COL_IS_GTALK, &is_gtalk,
435       COL_IS_FACEBOOK, &is_facebook,
436       -1);
437
438   if (cm != NULL && protocol_name != NULL)
439     {
440       TpConnectionManagerProtocol *protocol;
441
442       protocol = (TpConnectionManagerProtocol *)
443         tp_connection_manager_get_protocol (cm, protocol_name);
444
445       if (protocol != NULL)
446         {
447           visible = priv->filter_func (cm, protocol, is_gtalk, is_facebook,
448               priv->filter_user_data);
449         }
450     }
451
452   if (cm != NULL)
453     g_object_unref (cm);
454
455   return visible;
456 }
457
458 /* public methods */
459
460 /**
461  * empathy_protocol_chooser_get_selected_protocol:
462  * @protocol_chooser: an #EmpathyProtocolChooser
463  *
464  * Returns a pointer to the selected #TpConnectionManagerProtocol in
465  * @protocol_chooser.
466  *
467  * Return value: a pointer to the selected #TpConnectionManagerProtocol
468  */
469 TpConnectionManager *
470 empathy_protocol_chooser_dup_selected (
471     EmpathyProtocolChooser *protocol_chooser,
472     TpConnectionManagerProtocol **protocol,
473     gboolean *is_gtalk,
474     gboolean *is_facebook)
475 {
476   GtkTreeIter iter;
477   TpConnectionManager *cm = NULL;
478   GtkTreeModel *cur_model;
479
480   g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
481
482   /* get the current model from the chooser, as we could either be filtering
483    * or not.
484    */
485   cur_model = gtk_combo_box_get_model (GTK_COMBO_BOX (protocol_chooser));
486
487   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
488     {
489       gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
490           COL_CM, &cm,
491           -1);
492
493       if (protocol != NULL)
494         {
495           gchar *protocol_name = NULL;
496
497           gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
498               COL_PROTOCOL_NAME, &protocol_name,
499               -1);
500
501           *protocol = (TpConnectionManagerProtocol *)
502             tp_connection_manager_get_protocol (cm, protocol_name);
503
504           g_free (protocol_name);
505
506           if (*protocol == NULL)
507             {
508               /* For some reason the CM doesn't know about this protocol
509                * any more */
510               g_object_unref (cm);
511               return NULL;
512             }
513         }
514
515       if (is_gtalk != NULL)
516         {
517           gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
518               COL_IS_GTALK, is_gtalk,
519               -1);
520         }
521
522       if (is_facebook != NULL)
523         {
524           gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
525               COL_IS_FACEBOOK, is_facebook,
526               -1);
527         }
528     }
529
530   return cm;
531 }
532
533 /**
534  * empathy_protocol_chooser_new:
535  *
536  * Triggers the creation of a new #EmpathyProtocolChooser.
537  *
538  * Return value: a new #EmpathyProtocolChooser widget
539  */
540
541 GtkWidget *
542 empathy_protocol_chooser_new (void)
543 {
544   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));
545 }
546
547 void
548 empathy_protocol_chooser_set_visible (EmpathyProtocolChooser *protocol_chooser,
549     EmpathyProtocolChooserFilterFunc func,
550     gpointer user_data)
551 {
552   EmpathyProtocolChooserPriv *priv;
553   GtkTreeModel *filter_model;
554
555   g_return_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser));
556
557   priv = GET_PRIV (protocol_chooser);
558   priv->filter_func = func;
559   priv->filter_user_data = user_data;
560
561   filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store),
562       NULL);
563   gtk_combo_box_set_model (GTK_COMBO_BOX (protocol_chooser), filter_model);
564   g_object_unref (filter_model);
565
566   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER
567       (filter_model), protocol_chooser_filter_visible_func,
568       protocol_chooser, NULL);
569
570   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
571
572   gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
573 }