]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-protocol-chooser.c
Fix some coding style issues
[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   GtkCellRenderer *renderer;
185   TpDBusDaemon *dbus;
186
187   priv = GET_PRIV (object);
188   protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
189
190   /* set up combo box with new store */
191   priv->store = gtk_list_store_new (COL_COUNT,
192           G_TYPE_STRING,    /* Icon name */
193           G_TYPE_STRING,    /* Label     */
194           G_TYPE_OBJECT,    /* CM */
195           G_TYPE_POINTER);  /* protocol   */
196
197   gtk_combo_box_set_model (GTK_COMBO_BOX (object),
198       GTK_TREE_MODEL (priv->store));
199
200   renderer = gtk_cell_renderer_pixbuf_new ();
201   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
202   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
203       "icon-name", COL_ICON,
204       NULL);
205   g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
206
207   renderer = gtk_cell_renderer_text_new ();
208   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
209   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
210       "text", COL_LABEL,
211       NULL);
212
213   dbus = tp_dbus_daemon_dup (NULL);
214   tp_list_connection_managers (dbus, protocol_choosers_cms_listed,
215     NULL, NULL, object);
216   g_object_unref (dbus);
217
218   /* Set the protocol sort function */
219   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
220       COL_PROTOCOL,
221       protocol_chooser_sort_func,
222       NULL, NULL);
223   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
224       COL_PROTOCOL,
225       GTK_SORT_ASCENDING);
226
227   if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
228     G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed (object);
229 }
230
231 static void
232 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
233 {
234   EmpathyProtocolChooserPriv *priv =
235     G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
236         EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
237
238   priv->dispose_run = FALSE;
239
240   protocol_chooser->priv = priv;
241 }
242
243 static void
244 protocol_chooser_dispose (GObject *object)
245 {
246   EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
247   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
248
249   if (priv->dispose_run)
250     return;
251
252   priv->dispose_run = TRUE;
253
254   if (priv->store)
255     {
256       g_object_unref (priv->store);
257       priv->store = NULL;
258     }
259
260   (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
261 }
262
263 static void
264 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
265 {
266   GObjectClass *object_class = G_OBJECT_CLASS (klass);
267
268   object_class->constructed = protocol_chooser_constructed;
269   object_class->dispose = protocol_chooser_dispose;
270
271   g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
272 }
273
274 /**
275  * empathy_protocol_chooser_get_selected_protocol:
276  * @protocol_chooser: an #EmpathyProtocolChooser
277  *
278  * Returns a pointer to the selected #TpConnectionManagerProtocol in
279  * @protocol_chooser.
280  *
281  * Return value: a pointer to the selected #TpConnectionManagerProtocol
282  */
283 TpConnectionManager *
284 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 }