Updated Spanish translation
[evolution.git] / e-util / e-proxy-combo-box.c
blobe0bd7a0d582d97063f3415568f4e570c7fb84f0b
1 /*
2 * e-proxy-combo-box.c
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 * for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 /**
19 * SECTION: e-proxy-combo-box
20 * @include: e-util/e-util.h
21 * @short_description: Combo box of proxy profiles
23 * #EProxyComboBox is a combo box of available proxy profiles, as described
24 * by #ESource instances with an #ESourceProxy extension. For convenience,
25 * the combo box model's #GtkComboBox:id-column is populated with #ESource
26 * #ESource:uid strings.
27 **/
29 #include "e-proxy-combo-box.h"
31 #define E_PROXY_COMBO_BOX_GET_PRIVATE(obj) \
32 (G_TYPE_INSTANCE_GET_PRIVATE \
33 ((obj), E_TYPE_PROXY_COMBO_BOX, EProxyComboBoxPrivate))
35 struct _EProxyComboBoxPrivate {
36 ESourceRegistry *registry;
37 gulong source_added_handler_id;
38 gulong source_changed_handler_id;
39 gulong source_removed_handler_id;
41 guint refresh_idle_id;
44 enum {
45 PROP_0,
46 PROP_REGISTRY
49 enum {
50 COLUMN_DISPLAY_NAME,
51 COLUMN_UID
54 G_DEFINE_TYPE (
55 EProxyComboBox,
56 e_proxy_combo_box,
57 GTK_TYPE_COMBO_BOX)
59 static gboolean
60 proxy_combo_box_refresh_idle_cb (gpointer user_data)
62 EProxyComboBox *combo_box = user_data;
64 /* The refresh function will clear the idle ID. */
65 e_proxy_combo_box_refresh (combo_box);
67 return FALSE;
70 static void
71 proxy_combo_box_schedule_refresh (EProxyComboBox *combo_box)
73 /* Use an idle callback to limit how frequently we refresh
74 * the tree model in case the registry is emitting lots of
75 * signals at once. */
77 if (combo_box->priv->refresh_idle_id == 0) {
78 combo_box->priv->refresh_idle_id = g_idle_add (
79 proxy_combo_box_refresh_idle_cb, combo_box);
83 static void
84 proxy_combo_box_source_added_cb (ESourceRegistry *registry,
85 ESource *source,
86 EProxyComboBox *combo_box)
88 if (e_source_has_extension (source, E_SOURCE_EXTENSION_PROXY))
89 proxy_combo_box_schedule_refresh (combo_box);
92 static void
93 proxy_combo_box_source_changed_cb (ESourceRegistry *registry,
94 ESource *source,
95 EProxyComboBox *combo_box)
97 if (e_source_has_extension (source, E_SOURCE_EXTENSION_PROXY))
98 proxy_combo_box_schedule_refresh (combo_box);
101 static void
102 proxy_combo_box_source_removed_cb (ESourceRegistry *registry,
103 ESource *source,
104 EProxyComboBox *combo_box)
106 if (e_source_has_extension (source, E_SOURCE_EXTENSION_PROXY))
107 proxy_combo_box_schedule_refresh (combo_box);
110 static void
111 proxy_combo_box_set_registry (EProxyComboBox *combo_box,
112 ESourceRegistry *registry)
114 gulong handler_id;
116 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
117 g_return_if_fail (combo_box->priv->registry == NULL);
119 combo_box->priv->registry = g_object_ref (registry);
121 handler_id = g_signal_connect (
122 registry, "source-added",
123 G_CALLBACK (proxy_combo_box_source_added_cb), combo_box);
124 combo_box->priv->source_added_handler_id = handler_id;
126 handler_id = g_signal_connect (
127 registry, "source-changed",
128 G_CALLBACK (proxy_combo_box_source_changed_cb), combo_box);
129 combo_box->priv->source_changed_handler_id = handler_id;
131 handler_id = g_signal_connect (
132 registry, "source-removed",
133 G_CALLBACK (proxy_combo_box_source_removed_cb), combo_box);
134 combo_box->priv->source_removed_handler_id = handler_id;
137 static void
138 proxy_combo_box_set_property (GObject *object,
139 guint property_id,
140 const GValue *value,
141 GParamSpec *pspec)
143 switch (property_id) {
144 case PROP_REGISTRY:
145 proxy_combo_box_set_registry (
146 E_PROXY_COMBO_BOX (object),
147 g_value_get_object (value));
148 return;
151 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
154 static void
155 proxy_combo_box_get_property (GObject *object,
156 guint property_id,
157 GValue *value,
158 GParamSpec *pspec)
160 switch (property_id) {
161 case PROP_REGISTRY:
162 g_value_set_object (
163 value,
164 e_proxy_combo_box_get_registry (
165 E_PROXY_COMBO_BOX (object)));
166 return;
169 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
172 static void
173 proxy_combo_box_dispose (GObject *object)
175 EProxyComboBoxPrivate *priv;
177 priv = E_PROXY_COMBO_BOX_GET_PRIVATE (object);
179 if (priv->source_added_handler_id > 0) {
180 g_signal_handler_disconnect (
181 priv->registry,
182 priv->source_added_handler_id);
183 priv->source_added_handler_id = 0;
186 if (priv->source_changed_handler_id > 0) {
187 g_signal_handler_disconnect (
188 priv->registry,
189 priv->source_changed_handler_id);
190 priv->source_changed_handler_id = 0;
193 if (priv->source_removed_handler_id > 0) {
194 g_signal_handler_disconnect (
195 priv->registry,
196 priv->source_removed_handler_id);
197 priv->source_removed_handler_id = 0;
200 if (priv->refresh_idle_id > 0) {
201 g_source_remove (priv->refresh_idle_id);
202 priv->refresh_idle_id = 0;
205 g_clear_object (&priv->registry);
207 /* Chain up to parent's dispose() method. */
208 G_OBJECT_CLASS (e_proxy_combo_box_parent_class)->dispose (object);
211 static void
212 proxy_combo_box_constructed (GObject *object)
214 GtkListStore *list_store;
215 GtkComboBox *combo_box;
216 GtkCellLayout *cell_layout;
217 GtkCellRenderer *cell_renderer;
219 /* Chain up to parent's constructed() method. */
220 G_OBJECT_CLASS (e_proxy_combo_box_parent_class)->constructed (object);
222 combo_box = GTK_COMBO_BOX (object);
223 cell_layout = GTK_CELL_LAYOUT (object);
225 list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
226 gtk_combo_box_set_model (combo_box, GTK_TREE_MODEL (list_store));
227 gtk_combo_box_set_id_column (combo_box, COLUMN_UID);
228 g_object_unref (list_store);
230 cell_renderer = gtk_cell_renderer_text_new ();
231 gtk_cell_layout_pack_start (cell_layout, cell_renderer, TRUE);
232 gtk_cell_layout_add_attribute (
233 cell_layout, cell_renderer, "text", COLUMN_DISPLAY_NAME);
235 e_proxy_combo_box_refresh (E_PROXY_COMBO_BOX (object));
238 static void
239 e_proxy_combo_box_class_init (EProxyComboBoxClass *class)
241 GObjectClass *object_class;
243 g_type_class_add_private (class, sizeof (EProxyComboBoxPrivate));
245 object_class = G_OBJECT_CLASS (class);
246 object_class->set_property = proxy_combo_box_set_property;
247 object_class->get_property = proxy_combo_box_get_property;
248 object_class->dispose = proxy_combo_box_dispose;
249 object_class->constructed = proxy_combo_box_constructed;
251 g_object_class_install_property (
252 object_class,
253 PROP_REGISTRY,
254 g_param_spec_object (
255 "registry",
256 "Registry",
257 "Data source registry",
258 E_TYPE_SOURCE_REGISTRY,
259 G_PARAM_READWRITE |
260 G_PARAM_CONSTRUCT_ONLY |
261 G_PARAM_STATIC_STRINGS));
264 static void
265 e_proxy_combo_box_init (EProxyComboBox *combo_box)
267 combo_box->priv = E_PROXY_COMBO_BOX_GET_PRIVATE (combo_box);
271 * e_proxy_combo_box_new:
272 * @registry: an #ESourceRegistry
274 * Creates a new #EProxyComboBox widget using #ESource instances in @registry.
276 * Returns: a new #EProxyComboBox
278 GtkWidget *
279 e_proxy_combo_box_new (ESourceRegistry *registry)
281 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
283 return g_object_new (
284 E_TYPE_PROXY_COMBO_BOX,
285 "registry", registry, NULL);
289 * e_proxy_combo_box_refresh:
290 * @combo_box: an #EProxyComboBox
292 * Rebuilds the combo box model with an updated list of #ESource instances
293 * that describe a network proxy profile, without disrupting the previously
294 * active item (if possible).
296 * This function is called automatically in response to #ESourceRegistry
297 * signals which are pertinent to the @combo_box.
299 void
300 e_proxy_combo_box_refresh (EProxyComboBox *combo_box)
302 ESourceRegistry *registry;
303 GtkTreeModel *tree_model;
304 GtkComboBox *gtk_combo_box;
305 ESource *builtin_source;
306 GList *list, *link;
307 const gchar *extension_name;
308 const gchar *saved_uid;
310 g_return_if_fail (E_IS_PROXY_COMBO_BOX (combo_box));
312 if (combo_box->priv->refresh_idle_id > 0) {
313 g_source_remove (combo_box->priv->refresh_idle_id);
314 combo_box->priv->refresh_idle_id = 0;
317 gtk_combo_box = GTK_COMBO_BOX (combo_box);
318 tree_model = gtk_combo_box_get_model (gtk_combo_box);
320 /* This is an interned string, which means it's safe
321 * to use even after clearing the combo box model. */
322 saved_uid = gtk_combo_box_get_active_id (gtk_combo_box);
324 gtk_list_store_clear (GTK_LIST_STORE (tree_model));
326 extension_name = E_SOURCE_EXTENSION_PROXY;
327 registry = e_proxy_combo_box_get_registry (combo_box);
328 list = e_source_registry_list_enabled (registry, extension_name);
330 builtin_source = e_source_registry_ref_builtin_proxy (registry);
331 g_warn_if_fail (builtin_source != NULL);
333 /* Always list the built-in proxy profile first. */
334 link = g_list_find (list, builtin_source);
335 if (link != NULL && list != link) {
336 list = g_list_remove_link (list, link);
337 list = g_list_concat (link, list);
340 for (link = list; link != NULL; link = g_list_next (link)) {
341 ESource *source;
342 GtkTreeIter iter;
343 const gchar *display_name;
344 const gchar *uid;
346 source = E_SOURCE (link->data);
347 display_name = e_source_get_display_name (source);
348 uid = e_source_get_uid (source);
350 gtk_list_store_append (GTK_LIST_STORE (tree_model), &iter);
352 gtk_list_store_set (
353 GTK_LIST_STORE (tree_model), &iter,
354 COLUMN_DISPLAY_NAME, display_name,
355 COLUMN_UID, uid, -1);
358 g_clear_object (&builtin_source);
360 g_list_free_full (list, (GDestroyNotify) g_object_unref);
362 /* Try and restore the previous selected source or else pick
363 * the built-in proxy profile, which is always listed first. */
365 if (saved_uid != NULL)
366 gtk_combo_box_set_active_id (gtk_combo_box, saved_uid);
368 if (gtk_combo_box_get_active_id (gtk_combo_box) == NULL)
369 gtk_combo_box_set_active (gtk_combo_box, 0);
373 * e_proxy_combo_box_get_registry:
374 * @combo_box: an #EProxyComboBox
376 * Returns the #ESourceRegistry passed to e_proxy_combo_box_new().
378 * Returns: an #ESourceRegistry
380 ESourceRegistry *
381 e_proxy_combo_box_get_registry (EProxyComboBox *combo_box)
383 g_return_val_if_fail (E_IS_PROXY_COMBO_BOX (combo_box), NULL);
385 return combo_box->priv->registry;