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