]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-protocol-chooser.c
0dc60d4ebd12ce68b5ffb2262637b02cfa4724e3
[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_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           gboolean is_gtalk;
134           gtk_tree_model_get (model, iter_a,
135             COL_IS_GTALK, &is_gtalk,
136             -1);
137
138           cmp = is_gtalk ? 1 : -1;
139         }
140     }
141
142   g_free (protocol_a);
143   g_free (protocol_b);
144   return cmp;
145 }
146
147 static void
148 protocol_choosers_add_cm (EmpathyProtocolChooser *chooser,
149     TpConnectionManager *cm)
150 {
151   EmpathyProtocolChooserPriv *priv = GET_PRIV (chooser);
152   const TpConnectionManagerProtocol * const *iter;
153
154   for (iter = cm->protocols; iter != NULL && *iter != NULL; iter++)
155     {
156       const TpConnectionManagerProtocol *proto = *iter;
157       gchar *icon_name;
158       const gchar *display_name;
159       const gchar *saved_cm_name;
160
161       saved_cm_name = g_hash_table_lookup (priv->protocols, proto->name);
162
163       if (!tp_strdiff (cm->name, "haze") && saved_cm_name != NULL &&
164           tp_strdiff (saved_cm_name, "haze"))
165         /* the CM we're adding is a haze implementation of something we already
166          * have; drop it.
167          */
168         continue;
169
170       if (tp_strdiff (cm->name, "haze") && !tp_strdiff (saved_cm_name, "haze"))
171         {
172           GtkTreeIter titer;
173           gboolean valid;
174           TpConnectionManager *haze_cm;
175
176           /* let's this CM replace the haze implementation */
177           valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->store),
178               &titer);
179
180           while (valid)
181             {
182               gchar *haze_proto_name = NULL;
183
184               gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &titer,
185                   COL_PROTOCOL_NAME, &haze_proto_name,
186                   COL_CM, &haze_cm, -1);
187
188               if (haze_cm == NULL)
189                 continue;
190
191               if (!tp_strdiff (haze_cm->name, "haze") &&
192                   !tp_strdiff (haze_proto_name, proto->name))
193                 {
194                   gtk_list_store_remove (priv->store, &titer);
195                   g_object_unref (haze_cm);
196                   g_free (haze_proto_name);
197                   break;
198                 }
199
200               g_object_unref (haze_cm);
201               g_free (haze_proto_name);
202               valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->store),
203                   &titer);
204             }
205         }
206
207       g_hash_table_insert (priv->protocols,
208           g_strdup (proto->name), g_strdup (cm->name));
209
210       icon_name = empathy_protocol_icon_name (proto->name);
211       display_name = empathy_protocol_name_to_display_name (proto->name);
212
213       if (display_name == NULL)
214         display_name = proto->name;
215
216       gtk_list_store_insert_with_values (priv->store,
217           NULL, 0,
218           COL_ICON, icon_name,
219           COL_LABEL, display_name,
220           COL_CM, cm,
221           COL_PROTOCOL_NAME, proto->name,
222           COL_IS_GTALK, FALSE,
223           -1);
224
225       if (!tp_strdiff (proto->name, "jabber") &&
226           !tp_strdiff (cm->name, "gabble"))
227         {
228           display_name = empathy_protocol_name_to_display_name ("gtalk");
229           gtk_list_store_insert_with_values (priv->store,
230              NULL, 0,
231              COL_ICON, "im-google-talk",
232              COL_LABEL, display_name,
233              COL_CM, cm,
234              COL_PROTOCOL_NAME, proto->name,
235              COL_IS_GTALK, TRUE,
236              -1);
237         }
238
239       g_free (icon_name);
240     }
241 }
242
243 static void
244 protocol_chooser_add_cms_list (EmpathyProtocolChooser *protocol_chooser,
245     GList *cms)
246 {
247   GList *l;
248
249   for (l = cms; l != NULL; l = l->next)
250     protocol_choosers_add_cm (protocol_chooser, l->data);
251
252   gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
253 }
254
255 static void
256 protocol_chooser_cms_ready_cb (EmpathyConnectionManagers *cms,
257     const GError *error,
258     gpointer user_data)
259 {
260   EmpathyProtocolChooser *protocol_chooser = user_data;
261
262   if (error != NULL)
263     return;
264
265   protocol_chooser_add_cms_list (protocol_chooser,
266       empathy_connection_managers_get_cms (cms));
267 }
268
269 static void
270 protocol_chooser_constructed (GObject *object)
271 {
272   EmpathyProtocolChooser *protocol_chooser;
273   EmpathyProtocolChooserPriv *priv;
274   GtkCellRenderer *renderer;
275
276   priv = GET_PRIV (object);
277   protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
278
279   /* set up combo box with new store */
280   priv->store = gtk_list_store_new (COL_COUNT,
281           G_TYPE_STRING,    /* Icon name */
282           G_TYPE_STRING,    /* Label     */
283           G_TYPE_OBJECT,    /* CM */
284           G_TYPE_STRING,    /* protocol name  */
285           G_TYPE_BOOLEAN);  /* is gtalk  */
286
287   /* Set the protocol sort function */
288   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
289       COL_PROTOCOL_NAME,
290       protocol_chooser_sort_func,
291       NULL, NULL);
292   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
293       COL_PROTOCOL_NAME,
294       GTK_SORT_ASCENDING);
295
296   gtk_combo_box_set_model (GTK_COMBO_BOX (object),
297       GTK_TREE_MODEL (priv->store));
298
299   renderer = gtk_cell_renderer_pixbuf_new ();
300   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
301   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
302       "icon-name", COL_ICON,
303       NULL);
304   g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
305
306   renderer = gtk_cell_renderer_text_new ();
307   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
308   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
309       "text", COL_LABEL,
310       NULL);
311
312   empathy_connection_managers_call_when_ready (priv->cms,
313       protocol_chooser_cms_ready_cb, protocol_chooser);
314
315   if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
316     G_OBJECT_CLASS
317       (empathy_protocol_chooser_parent_class)->constructed (object);
318 }
319
320 static void
321 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
322 {
323   EmpathyProtocolChooserPriv *priv =
324     G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
325         EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
326
327   priv->dispose_run = FALSE;
328   priv->cms = empathy_connection_managers_dup_singleton ();
329   priv->protocols = g_hash_table_new_full (g_str_hash, g_str_equal,
330       g_free, g_free);
331
332   protocol_chooser->priv = priv;
333 }
334
335 static void
336 protocol_chooser_finalize (GObject *object)
337 {
338   EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
339   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
340
341   if (priv->protocols)
342     {
343       g_hash_table_destroy (priv->protocols);
344       priv->protocols = NULL;
345     }
346
347   (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->finalize) (object);
348 }
349
350 static void
351 protocol_chooser_dispose (GObject *object)
352 {
353   EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
354   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
355
356   if (priv->dispose_run)
357     return;
358
359   priv->dispose_run = TRUE;
360
361   if (priv->store)
362     {
363       g_object_unref (priv->store);
364       priv->store = NULL;
365     }
366
367   if (priv->cms)
368     {
369       g_object_unref (priv->cms);
370       priv->cms = NULL;
371     }
372
373   (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
374 }
375
376 static void
377 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
378 {
379   GObjectClass *object_class = G_OBJECT_CLASS (klass);
380
381   object_class->constructed = protocol_chooser_constructed;
382   object_class->dispose = protocol_chooser_dispose;
383   object_class->finalize = protocol_chooser_finalize;
384
385   g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
386 }
387
388 static gboolean
389 protocol_chooser_filter_visible_func (GtkTreeModel *model,
390     GtkTreeIter *iter,
391     gpointer user_data)
392 {
393   EmpathyProtocolChooser *protocol_chooser = user_data;
394   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
395   TpConnectionManager *cm = NULL;
396   gchar *protocol_name = NULL;
397   gboolean visible = FALSE;
398   gboolean is_gtalk;
399
400   gtk_tree_model_get (model, iter,
401       COL_CM, &cm,
402       COL_PROTOCOL_NAME, &protocol_name,
403       COL_IS_GTALK, &is_gtalk,
404       -1);
405
406   if (cm != NULL && protocol_name != NULL)
407     {
408       TpConnectionManagerProtocol *protocol;
409
410       protocol = (TpConnectionManagerProtocol *)
411         tp_connection_manager_get_protocol (cm, protocol_name);
412
413       if (protocol != NULL)
414         {
415           visible = priv->filter_func (cm, protocol, is_gtalk,
416               priv->filter_user_data);
417         }
418     }
419
420   if (cm != NULL)
421     g_object_unref (cm);
422
423   return visible;
424 }
425
426 /* public methods */
427
428 /**
429  * empathy_protocol_chooser_get_selected_protocol:
430  * @protocol_chooser: an #EmpathyProtocolChooser
431  *
432  * Returns a pointer to the selected #TpConnectionManagerProtocol in
433  * @protocol_chooser.
434  *
435  * Return value: a pointer to the selected #TpConnectionManagerProtocol
436  */
437 TpConnectionManager *
438 empathy_protocol_chooser_dup_selected (
439     EmpathyProtocolChooser *protocol_chooser,
440     TpConnectionManagerProtocol **protocol,
441     gboolean *is_gtalk)
442 {
443   GtkTreeIter iter;
444   TpConnectionManager *cm = NULL;
445   GtkTreeModel *cur_model;
446
447   g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
448
449   /* get the current model from the chooser, as we could either be filtering
450    * or not.
451    */
452   cur_model = gtk_combo_box_get_model (GTK_COMBO_BOX (protocol_chooser));
453
454   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
455     {
456       gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
457           COL_CM, &cm,
458           -1);
459
460       if (protocol != NULL)
461         {
462           gchar *protocol_name = NULL;
463
464           gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
465               COL_PROTOCOL_NAME, &protocol_name,
466               -1);
467
468           *protocol = (TpConnectionManagerProtocol *)
469             tp_connection_manager_get_protocol (cm, protocol_name);
470
471           g_free (protocol_name);
472         }
473
474       if (is_gtalk != NULL)
475         {
476           gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
477               COL_IS_GTALK, is_gtalk,
478               -1);
479         }
480     }
481
482   return cm;
483 }
484
485 /**
486  * empathy_protocol_chooser_new:
487  *
488  * Triggers the creation of a new #EmpathyProtocolChooser.
489  *
490  * Return value: a new #EmpathyProtocolChooser widget
491  */
492
493 GtkWidget *
494 empathy_protocol_chooser_new (void)
495 {
496   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));
497 }
498
499 void
500 empathy_protocol_chooser_set_visible (EmpathyProtocolChooser *protocol_chooser,
501     EmpathyProtocolChooserFilterFunc func,
502     gpointer user_data)
503 {
504   EmpathyProtocolChooserPriv *priv;
505   GtkTreeModel *filter_model;
506
507   g_return_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser));
508
509   priv = GET_PRIV (protocol_chooser);
510   priv->filter_func = func;
511   priv->filter_user_data = user_data;
512
513   filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store),
514       NULL);
515   gtk_combo_box_set_model (GTK_COMBO_BOX (protocol_chooser), filter_model);
516   g_object_unref (filter_model);
517
518   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER
519       (filter_model), protocol_chooser_filter_visible_func,
520       protocol_chooser, NULL);
521
522   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
523
524   gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
525 }