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