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