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