]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-protocol-chooser.c
Use proper display names in the chooser
[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 <libempathy/empathy-utils.h>
32 #include <libempathy/empathy-connection-managers.h>
33
34 #include "empathy-protocol-chooser.h"
35 #include "empathy-ui-utils.h"
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
38 #include <libempathy/empathy-debug.h>
39
40 /**
41  * SECTION:empathy-protocol-chooser
42  * @title: EmpathyProtocolChooser
43  * @short_description: A widget used to choose from a list of protocols
44  * @include: libempathy-gtk/empathy-protocol-chooser.h
45  *
46  * #EmpathyProtocolChooser is a widget which extends #GtkComboBox to provides a
47  * chooser of available protocols.
48  */
49
50 /**
51  * EmpathyProtocolChooser:
52  * @parent: parent object
53  *
54  * Widget which extends #GtkComboBox to provide a chooser of available
55  * protocols.
56  */
57
58 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyProtocolChooser)
59 typedef struct
60 {
61   GtkListStore *store;
62   gboolean dispose_run;
63   EmpathyConnectionManagers *cms;
64
65 } EmpathyProtocolChooserPriv;
66
67 enum
68 {
69   COL_ICON,
70   COL_LABEL,
71   COL_CM,
72   COL_PROTOCOL,
73   COL_COUNT
74 };
75
76 G_DEFINE_TYPE (EmpathyProtocolChooser, empathy_protocol_chooser,
77     GTK_TYPE_COMBO_BOX);
78
79 static gint
80 protocol_chooser_sort_protocol_value (TpConnectionManagerProtocol *protocol)
81 {
82   guint i;
83   const gchar *names[] = {
84     "jabber",
85     "salut",
86     "gtalk",
87     NULL
88   };
89
90   for (i = 0 ; names[i]; i++)
91     {
92       if (strcmp (protocol->name, names[i]) == 0)
93         return i;
94     }
95
96   return i;
97 }
98
99 static gint
100 protocol_chooser_sort_func (GtkTreeModel *model,
101     GtkTreeIter  *iter_a,
102     GtkTreeIter  *iter_b,
103     gpointer      user_data)
104 {
105   TpConnectionManagerProtocol *protocol_a;
106   TpConnectionManagerProtocol *protocol_b;
107   gint cmp;
108
109   gtk_tree_model_get (model, iter_a,
110       COL_PROTOCOL, &protocol_a,
111       -1);
112   gtk_tree_model_get (model, iter_b,
113       COL_PROTOCOL, &protocol_b,
114       -1);
115
116   cmp = protocol_chooser_sort_protocol_value (protocol_a);
117   cmp -= protocol_chooser_sort_protocol_value (protocol_b);
118   if (cmp == 0)
119     {
120       cmp = strcmp (protocol_a->name, protocol_b->name);
121     }
122
123   return cmp;
124 }
125
126 static const char *
127 protocol_chooser_proto_name_to_display_name (const gchar *proto_name)
128 {
129   int i;
130   
131   static struct {
132     const gchar *proto;
133     const gchar *display;
134   } names[] = {
135     { "jabber", "XMPP" },
136     { "msn", "MSN" },
137     { "local-xmpp", "Salut" },
138     { "irc", "IRC" },
139     { "icq", "ICQ" },
140     { "aim", "AIM" },
141     { "yahoo", "Yahoo" },
142     { "groupwise", "GroupWise" },
143     { "sip", "SIP" },
144     { NULL, NULL }
145   };
146
147   for (i = 0; names[i].proto != NULL; i++)
148     {
149       if (!tp_strdiff (proto_name, names[i].proto))
150         return names[i].display;
151     }
152
153   return NULL;
154 }
155
156 static void
157 protocol_choosers_add_cm (EmpathyProtocolChooser *chooser,
158     TpConnectionManager *cm)
159 {
160   EmpathyProtocolChooserPriv *priv = GET_PRIV (chooser);
161   const TpConnectionManagerProtocol * const *iter;
162
163   for (iter = cm->protocols; iter != NULL && *iter != NULL; iter++)
164     {
165       const TpConnectionManagerProtocol *proto = *iter;
166       gchar *icon_name;
167       const gchar *display_name;
168       gchar *display_name_set;
169
170       icon_name = g_strdup_printf ("im-%s", proto->name);
171       display_name = protocol_chooser_proto_name_to_display_name (proto->name);
172
173       if (display_name == NULL)
174         display_name = proto->name;
175
176       if (!tp_strdiff (cm->name, "haze"))
177         display_name_set = g_strdup_printf ("%s (Haze)", display_name);
178       else
179         display_name_set = g_strdup (display_name);
180
181       gtk_list_store_insert_with_values (priv->store, NULL, 0,
182           COL_ICON, icon_name,
183           COL_LABEL, display_name_set,
184           COL_CM, cm,
185           COL_PROTOCOL, proto,
186           -1);
187
188       g_free (display_name_set);
189       g_free (icon_name);
190     }
191 }
192
193 static void
194 protocol_chooser_add_cms_list (EmpathyProtocolChooser *protocol_chooser,
195     GList *cms)
196 {
197   GList *l;
198
199   for (l = cms; l != NULL; l = l->next)
200     protocol_choosers_add_cm (protocol_chooser, l->data);
201
202   gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
203 }
204
205 static void
206 protocol_chooser_cms_ready_cb (EmpathyConnectionManagers *cms,
207     GParamSpec *pspec,
208     EmpathyProtocolChooser *protocol_chooser)
209 {
210   if (empathy_connection_managers_is_ready (cms))
211     protocol_chooser_add_cms_list
212         (protocol_chooser, empathy_connection_managers_get_cms (cms));
213 }
214
215 static void
216 protocol_chooser_constructed (GObject *object)
217 {
218   EmpathyProtocolChooser *protocol_chooser;
219   EmpathyProtocolChooserPriv *priv;
220   GtkCellRenderer *renderer;
221
222   priv = GET_PRIV (object);
223   protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
224
225   /* set up combo box with new store */
226   priv->store = gtk_list_store_new (COL_COUNT,
227           G_TYPE_STRING,    /* Icon name */
228           G_TYPE_STRING,    /* Label     */
229           G_TYPE_OBJECT,    /* CM */
230           G_TYPE_POINTER);  /* protocol   */
231
232   gtk_combo_box_set_model (GTK_COMBO_BOX (object),
233       GTK_TREE_MODEL (priv->store));
234
235   renderer = gtk_cell_renderer_pixbuf_new ();
236   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
237   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
238       "icon-name", COL_ICON,
239       NULL);
240   g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
241
242   renderer = gtk_cell_renderer_text_new ();
243   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
244   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
245       "text", COL_LABEL,
246       NULL);
247
248   /* Set the protocol sort function */
249   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
250       COL_PROTOCOL,
251       protocol_chooser_sort_func,
252       NULL, NULL);
253   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
254       COL_PROTOCOL,
255       GTK_SORT_ASCENDING);
256
257   if (empathy_connection_managers_is_ready (priv->cms))
258     protocol_chooser_add_cms_list (protocol_chooser,
259         empathy_connection_managers_get_cms (priv->cms));
260   else
261     g_signal_connect (priv->cms, "notify::ready",
262         G_CALLBACK (protocol_chooser_cms_ready_cb), protocol_chooser);
263
264   if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
265     G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed (object);
266 }
267
268 static void
269 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
270 {
271   EmpathyProtocolChooserPriv *priv =
272     G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
273         EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
274
275   priv->dispose_run = FALSE;
276   priv->cms = empathy_connection_managers_dup_singleton ();
277
278   protocol_chooser->priv = priv;
279 }
280
281 static void
282 protocol_chooser_dispose (GObject *object)
283 {
284   EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
285   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
286
287   if (priv->dispose_run)
288     return;
289
290   priv->dispose_run = TRUE;
291
292   if (priv->store)
293     {
294       g_object_unref (priv->store);
295       priv->store = NULL;
296     }
297
298   if (priv->cms)
299     {
300       g_object_unref (priv->cms);
301       priv->cms = NULL;
302     }
303
304   (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
305 }
306
307 static void
308 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
309 {
310   GObjectClass *object_class = G_OBJECT_CLASS (klass);
311
312   object_class->constructed = protocol_chooser_constructed;
313   object_class->dispose = protocol_chooser_dispose;
314
315   g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
316 }
317
318 /* public methods */
319
320 /**
321  * empathy_protocol_chooser_get_selected_protocol:
322  * @protocol_chooser: an #EmpathyProtocolChooser
323  *
324  * Returns a pointer to the selected #TpConnectionManagerProtocol in
325  * @protocol_chooser.
326  *
327  * Return value: a pointer to the selected #TpConnectionManagerProtocol
328  */
329 TpConnectionManager *empathy_protocol_chooser_dup_selected (
330     EmpathyProtocolChooser *protocol_chooser,
331     TpConnectionManagerProtocol **protocol)
332 {
333   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
334   GtkTreeIter iter;
335   TpConnectionManager *cm = NULL;
336
337   g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
338
339   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
340     {
341       gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
342           COL_CM, &cm,
343           -1);
344
345       if (protocol != NULL)
346         gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
347             COL_PROTOCOL, protocol,
348             -1);
349     }
350
351   return cm;
352 }
353
354 /**
355  * empathy_protocol_chooser_new:
356  *
357  * Triggers the creation of a new #EmpathyProtocolChooser.
358  *
359  * Return value: a new #EmpathyProtocolChooser widget
360  */
361
362 GtkWidget *
363 empathy_protocol_chooser_new (void)
364 {
365   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));
366 }