]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-protocol-chooser.c
Updated Spanish translation
[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 #include "empathy-protocol-chooser.h"
25
26 #include <glib/gi18n-lib.h>
27 #include <tp-account-widgets/tpaw-connection-managers.h>
28 #include <tp-account-widgets/tpaw-pixbuf-utils.h>
29 #include <tp-account-widgets/tpaw-utils.h>
30
31 #include "empathy-ui-utils.h"
32 #include "empathy-utils.h"
33
34 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
35 #include "empathy-debug.h"
36
37 /**
38  * SECTION:empathy-protocol-chooser
39  * @title: EmpathyProtocolChooser
40  * @short_description: A widget used to choose from a list of protocols
41  * @include: libempathy-gtk/empathy-protocol-chooser.h
42  *
43  * #EmpathyProtocolChooser is a widget which extends #GtkComboBox to provides a
44  * chooser of available protocols.
45  */
46
47 /**
48  * EmpathyProtocolChooser:
49  * @parent: parent object
50  *
51  * Widget which extends #GtkComboBox to provide a chooser of available
52  * protocols.
53  */
54
55 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyProtocolChooser)
56 typedef struct
57 {
58   GtkListStore *store;
59
60   gboolean dispose_run;
61
62   EmpathyProtocolChooserFilterFunc filter_func;
63   gpointer filter_user_data;
64 } EmpathyProtocolChooserPriv;
65
66 enum
67 {
68   COL_ICON,
69   COL_LABEL,
70   COL_PROTOCOL,
71   COL_COUNT
72 };
73
74 G_DEFINE_TYPE (EmpathyProtocolChooser, empathy_protocol_chooser,
75     GTK_TYPE_COMBO_BOX);
76
77 static void
78 protocol_chooser_add_protocol (EmpathyProtocolChooser *chooser,
79     TpawProtocol *protocol)
80 {
81   EmpathyProtocolChooserPriv *priv = GET_PRIV (chooser);
82   GdkPixbuf *pixbuf;
83
84   pixbuf = tpaw_pixbuf_from_icon_name (tpaw_protocol_get_icon_name (protocol),
85       GTK_ICON_SIZE_BUTTON);
86
87   gtk_list_store_insert_with_values (priv->store,
88       NULL, -1,
89       COL_ICON, pixbuf,
90       COL_LABEL, tpaw_protocol_get_display_name (protocol),
91       COL_PROTOCOL, protocol,
92       -1);
93
94   g_clear_object (&pixbuf);
95 }
96
97 static void
98 protocol_chooser_get_protocols_cb (GObject *source,
99     GAsyncResult *result,
100     gpointer user_data)
101 {
102   EmpathyProtocolChooser *protocol_chooser = user_data;
103   GList *protocols = NULL;
104   GList *l;
105
106   if (!tpaw_protocol_get_all_finish (&protocols, result, NULL))
107     return;
108
109   for (l = protocols; l != NULL; l = l->next)
110     protocol_chooser_add_protocol (protocol_chooser, l->data);
111
112   gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
113
114   g_list_free_full (protocols, g_object_unref);
115 }
116
117 static void
118 protocol_chooser_constructed (GObject *object)
119 {
120   EmpathyProtocolChooser *protocol_chooser;
121   EmpathyProtocolChooserPriv *priv;
122   GtkCellRenderer *renderer;
123
124   priv = GET_PRIV (object);
125   protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
126
127   /* set up combo box with new store */
128   priv->store = gtk_list_store_new (COL_COUNT,
129           GDK_TYPE_PIXBUF,  /* Icon */
130           G_TYPE_STRING,    /* Label     */
131           G_TYPE_OBJECT);   /* protocol */
132
133   gtk_combo_box_set_model (GTK_COMBO_BOX (object),
134       GTK_TREE_MODEL (priv->store));
135
136   renderer = gtk_cell_renderer_pixbuf_new ();
137   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
138   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
139       "pixbuf", COL_ICON,
140       NULL);
141
142   renderer = gtk_cell_renderer_text_new ();
143   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
144   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
145       "text", COL_LABEL,
146       NULL);
147
148   tpaw_protocol_get_all_async (protocol_chooser_get_protocols_cb, protocol_chooser);
149
150   if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
151     G_OBJECT_CLASS
152       (empathy_protocol_chooser_parent_class)->constructed (object);
153 }
154
155 static void
156 empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
157 {
158   EmpathyProtocolChooserPriv *priv =
159     G_TYPE_INSTANCE_GET_PRIVATE (protocol_chooser,
160         EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
161
162   priv->dispose_run = FALSE;
163   protocol_chooser->priv = priv;
164 }
165
166 static void
167 protocol_chooser_dispose (GObject *object)
168 {
169   EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
170   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
171
172   if (priv->dispose_run)
173     return;
174
175   priv->dispose_run = TRUE;
176
177   if (priv->store)
178     {
179       g_object_unref (priv->store);
180       priv->store = NULL;
181     }
182
183   (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
184 }
185
186 static void
187 empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
188 {
189   GObjectClass *object_class = G_OBJECT_CLASS (klass);
190
191   object_class->constructed = protocol_chooser_constructed;
192   object_class->dispose = protocol_chooser_dispose;
193
194   g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
195 }
196
197 static gboolean
198 protocol_chooser_filter_visible_func (GtkTreeModel *model,
199     GtkTreeIter *iter,
200     gpointer user_data)
201 {
202   EmpathyProtocolChooser *protocol_chooser = user_data;
203   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
204   TpawProtocol *protocol;
205   TpProtocol *tp_protocol;
206   gboolean visible = FALSE;
207
208   gtk_tree_model_get (model, iter,
209       COL_PROTOCOL, &protocol,
210       -1);
211
212   tp_protocol = tp_connection_manager_get_protocol_object (
213       tpaw_protocol_get_cm (protocol),
214       tpaw_protocol_get_protocol_name (protocol));
215
216   if (tp_protocol != NULL)
217     {
218       visible = priv->filter_func (tpaw_protocol_get_cm (protocol),
219           tp_protocol, tpaw_protocol_get_service_name (protocol),
220           priv->filter_user_data);
221     }
222
223   return visible;
224 }
225
226 /* public methods */
227
228 /**
229  * empathy_protocol_chooser_dup_selected:
230  * @protocol_chooser: an #EmpathyProtocolChooser
231  *
232  * Returns a pointer to the selected #TpawProtocol in
233  * @protocol_chooser.
234  *
235  * Return value: a pointer to the selected #TpawProtocol
236  */
237 TpawProtocol *
238 empathy_protocol_chooser_dup_selected (
239     EmpathyProtocolChooser *protocol_chooser)
240 {
241   GtkTreeIter iter;
242   GtkTreeModel *cur_model;
243   TpawProtocol *protocol = NULL;
244
245   g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
246
247   /* get the current model from the chooser, as we could either be filtering
248    * or not.
249    */
250   cur_model = gtk_combo_box_get_model (GTK_COMBO_BOX (protocol_chooser));
251
252   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
253     {
254       gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
255           COL_PROTOCOL, &protocol,
256           -1);
257     }
258
259   return protocol;
260 }
261
262 /**
263  * empathy_protocol_chooser_new:
264  *
265  * Triggers the creation of a new #EmpathyProtocolChooser.
266  *
267  * Return value: a new #EmpathyProtocolChooser widget
268  */
269
270 GtkWidget *
271 empathy_protocol_chooser_new (void)
272 {
273   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));
274 }
275
276 void
277 empathy_protocol_chooser_set_visible (EmpathyProtocolChooser *protocol_chooser,
278     EmpathyProtocolChooserFilterFunc func,
279     gpointer user_data)
280 {
281   EmpathyProtocolChooserPriv *priv;
282   GtkTreeModel *filter_model;
283
284   g_return_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser));
285
286   priv = GET_PRIV (protocol_chooser);
287   priv->filter_func = func;
288   priv->filter_user_data = user_data;
289
290   filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store),
291       NULL);
292   gtk_combo_box_set_model (GTK_COMBO_BOX (protocol_chooser), filter_model);
293   g_object_unref (filter_model);
294
295   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER
296       (filter_model), protocol_chooser_filter_visible_func,
297       protocol_chooser, NULL);
298
299   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
300
301   gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
302 }
303
304 TpawAccountSettings *
305 empathy_protocol_chooser_create_account_settings (EmpathyProtocolChooser *self)
306 {
307   TpawProtocol *protocol;
308   TpawAccountSettings *settings;
309
310   protocol = empathy_protocol_chooser_dup_selected (self);
311   if (protocol == NULL)
312     return NULL;
313
314   settings = tpaw_protocol_create_account_settings (protocol);
315
316   tp_clear_object (&protocol);
317
318   return settings;
319 }