]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-protocol-chooser.c
Put (Haze) behind all protocols coming from haze
[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
138       icon_name = g_strdup_printf ("im-%s", proto->name);
139
140       if (!tp_strdiff (cm->name, "haze"))
141         display_name = g_strdup_printf ("%s (Haze)", proto->name);
142       else
143         display_name = g_strdup (proto->name);
144
145       gtk_list_store_insert_with_values (priv->store, NULL, 0,
146           COL_ICON, icon_name,
147           COL_LABEL, display_name,
148           COL_CM, cm,
149           COL_PROTOCOL, proto,
150           -1);
151
152       g_free (display_name);
153       g_free (icon_name);
154     }
155 }
156
157
158 static void
159 protocol_choosers_cms_listed (TpConnectionManager * const *cms,
160     gsize n_cms,
161     const GError *error,
162     gpointer user_data,
163     GObject *weak_object)
164 {
165   TpConnectionManager * const *iter;
166
167   if (error !=NULL)
168     {
169       DEBUG ("Failed to get connection managers: %s", error->message);
170       return;
171     }
172
173   for (iter = cms ; iter != NULL && *iter != NULL; iter++)
174     protocol_choosers_add_cm (EMPATHY_PROTOCOL_CHOOSER (weak_object),
175       *iter);
176
177   gtk_combo_box_set_active (GTK_COMBO_BOX (weak_object), 0);
178 }
179
180 static void
181 protocol_chooser_constructed (GObject *object)
182 {
183   EmpathyProtocolChooser *protocol_chooser;
184   EmpathyProtocolChooserPriv *priv;
185
186   GtkCellRenderer *renderer;
187   TpDBusDaemon *dbus;
188
189   priv = GET_PRIV (object);
190   protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
191
192   /* set up combo box with new store */
193   priv->store = gtk_list_store_new (COL_COUNT,
194           G_TYPE_STRING,    /* Icon name */
195           G_TYPE_STRING,    /* Label     */
196           G_TYPE_OBJECT,    /* CM */
197           G_TYPE_POINTER);  /* protocol   */
198
199   gtk_combo_box_set_model (GTK_COMBO_BOX (object),
200       GTK_TREE_MODEL (priv->store));
201
202   renderer = gtk_cell_renderer_pixbuf_new ();
203   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
204   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
205       "icon-name", COL_ICON,
206       NULL);
207   g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
208
209   renderer = gtk_cell_renderer_text_new ();
210   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
211   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
212       "text", COL_LABEL,
213       NULL);
214
215   dbus = tp_dbus_daemon_dup (NULL);
216   tp_list_connection_managers (dbus, protocol_choosers_cms_listed,
217     NULL, NULL, object);
218   g_object_unref (dbus);
219
220   /* Set the protocol sort function */
221   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
222       COL_PROTOCOL,
223       protocol_chooser_sort_func,
224       NULL, NULL);
225   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
226       COL_PROTOCOL,
227       GTK_SORT_ASCENDING);
228
229   if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
230     G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed (object);
231 }
232
233 static void
234 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
235 {
236   EmpathyProtocolChooserPriv *priv =
237     G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
238         EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
239
240   priv->dispose_run = FALSE;
241
242   protocol_chooser->priv = priv;
243 }
244
245 static void
246 protocol_chooser_dispose (GObject *object)
247 {
248   EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
249   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
250
251   if (priv->dispose_run)
252     return;
253
254   priv->dispose_run = TRUE;
255
256   if (priv->store)
257     {
258       g_object_unref (priv->store);
259       priv->store = NULL;
260     }
261
262   (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
263 }
264
265 static void
266 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
267 {
268   GObjectClass *object_class = G_OBJECT_CLASS (klass);
269
270   object_class->constructed = protocol_chooser_constructed;
271   object_class->dispose = protocol_chooser_dispose;
272
273   g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
274 }
275
276 /**
277  * empathy_protocol_chooser_get_selected_protocol:
278  * @protocol_chooser: an #EmpathyProtocolChooser
279  *
280  * Returns a pointer to the selected #TpConnectionManagerProtocol in
281  * @protocol_chooser.
282  *
283  * Return value: a pointer to the selected #TpConnectionManagerProtocol
284  */
285 TpConnectionManager *empathy_protocol_chooser_dup_selected (
286     EmpathyProtocolChooser *protocol_chooser,
287     TpConnectionManagerProtocol **protocol)
288 {
289   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
290   GtkTreeIter iter;
291   TpConnectionManager *cm = NULL;
292
293   g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
294
295   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
296     {
297       gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
298           COL_CM, &cm,
299           -1);
300
301       if (protocol != NULL)
302         gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
303             COL_PROTOCOL, protocol,
304             -1);
305     }
306
307   return cm;
308 }
309
310 /**
311  * empathy_protocol_chooser_n_protocols:
312  * @protocol_chooser: an #EmpathyProtocolChooser
313  *
314  * Returns the number of protocols in @protocol_chooser.
315  *
316  * Return value: the number of protocols in @protocol_chooser
317  */
318 gint
319 empathy_protocol_chooser_n_protocols (EmpathyProtocolChooser *protocol_chooser)
320 {
321   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
322
323   g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), 0);
324
325   return gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->store), NULL);
326 }
327
328 /**
329  * empathy_protocol_chooser_new:
330  *
331  * Creates a new #EmpathyProtocolChooser widget.
332  *
333  * Return value: a new #EmpathyProtocolChooser widget
334  */
335 GtkWidget *
336 empathy_protocol_chooser_new (void)
337 {
338   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));
339 }