Debian Codegraph @

src/EWS/evolution/e-mail-config-ews-oal-combo-box.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 2012 Red Hat, Inc. (www.redhat.com)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "evolution-ews-config.h"
7
8 #include <mail/e-mail-config-service-page.h>
9
10 #include "common/e-ews-connection.h"
11 #include "common/e-ews-connection-utils.h"
12
13 #include "e-ews-config-utils.h"
14 #include "e-mail-config-ews-oal-combo-box.h"
15
16 struct _EMailConfigEwsOalComboBoxPrivate {
17 EMailConfigServiceBackend *backend;
18
19 /* The try_password() method deposits results here, and the
20 * update_finish() function uses the results to re-populate
21 * the combo box. This avoids calling GTK+ functions from
22 * multiple threads. */
23 GSList *oal_items;
24 GMutex oal_items_lock;
25 };
26
27 enum {
28 PROP_0,
29 PROP_BACKEND
30 };
31
32 G_DEFINE_DYNAMIC_TYPE_EXTENDED (EMailConfigEwsOalComboBox, e_mail_config_ews_oal_combo_box, GTK_TYPE_COMBO_BOX_TEXT, 0,
33 G_ADD_PRIVATE_DYNAMIC (EMailConfigEwsOalComboBox))
34
35 typedef struct _AsyncContext {
36 EMailConfigEwsOalComboBox *combo_box;
37 GSimpleAsyncResult *simple;
38 ESource *source;
39 GObject *settings;
40 } AsyncContext;
41
42 static void
43 async_context_free (gpointer ptr)
44 {
45 AsyncContext *async_context = ptr;
46
47 if (!async_context)
48 return;
49
50 if (async_context->settings)
51 g_object_thaw_notify (async_context->settings);
52
53 g_clear_object (&async_context->combo_box);
54 g_clear_object (&async_context->simple);
55 g_clear_object (&async_context->source);
56 g_clear_object (&async_context->settings);
57
58 g_slice_free (AsyncContext, async_context);
59 }
60
61 static void
62 mail_config_ews_oal_combo_box_set_backend (EMailConfigEwsOalComboBox *combo_box,
63 EMailConfigServiceBackend *backend)
64 {
65 g_return_if_fail (E_IS_MAIL_CONFIG_SERVICE_BACKEND (backend));
66 g_return_if_fail (combo_box->priv->backend == NULL);
67
68 combo_box->priv->backend = g_object_ref (backend);
69 }
70
71 static void
72 mail_config_ews_oal_combo_box_set_property (GObject *object,
73 guint property_id,
74 const GValue *value,
75 GParamSpec *pspec)
76 {
77 switch (property_id) {
78 case PROP_BACKEND:
79 mail_config_ews_oal_combo_box_set_backend (
80 E_MAIL_CONFIG_EWS_OAL_COMBO_BOX (object),
81 g_value_get_object (value));
82 return;
83 }
84
85 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
86 }
87
88 static void
89 mail_config_ews_oal_combo_box_get_property (GObject *object,
90 guint property_id,
91 GValue *value,
92 GParamSpec *pspec)
93 {
94 switch (property_id) {
95 case PROP_BACKEND:
96 g_value_set_object (
97 value,
98 e_mail_config_ews_oal_combo_box_get_backend (
99 E_MAIL_CONFIG_EWS_OAL_COMBO_BOX (object)));
100 return;
101 }
102
103 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
104 }
105
106 static void
107 mail_config_ews_oal_combo_box_dispose (GObject *object)
108 {
109 EMailConfigEwsOalComboBox *ews_combo = E_MAIL_CONFIG_EWS_OAL_COMBO_BOX (object);
110
111 g_clear_object (&ews_combo->priv->backend);
112
113 /* Chain up to parent's dispose() method. */
114 G_OBJECT_CLASS (e_mail_config_ews_oal_combo_box_parent_class)->dispose (object);
115 }
116
117 static void
118 mail_config_ews_oal_combo_box_finalize (GObject *object)
119 {
120 EMailConfigEwsOalComboBox *ews_combo = E_MAIL_CONFIG_EWS_OAL_COMBO_BOX (object);
121
122 g_mutex_clear (&ews_combo->priv->oal_items_lock);
123
124 /* Chain up to parent's finalize() method. */
125 G_OBJECT_CLASS (e_mail_config_ews_oal_combo_box_parent_class)->finalize (object);
126 }
127
128 static void
129 e_mail_config_ews_oal_combo_box_class_init (EMailConfigEwsOalComboBoxClass *class)
130 {
131 GObjectClass *object_class;
132
133 object_class = G_OBJECT_CLASS (class);
134 object_class->set_property = mail_config_ews_oal_combo_box_set_property;
135 object_class->get_property = mail_config_ews_oal_combo_box_get_property;
136 object_class->dispose = mail_config_ews_oal_combo_box_dispose;
137 object_class->finalize = mail_config_ews_oal_combo_box_finalize;
138
139 g_object_class_install_property (
140 object_class,
141 PROP_BACKEND,
142 g_param_spec_object (
143 "backend",
144 "Backend",
145 "Service backend",
146 E_TYPE_MAIL_CONFIG_SERVICE_BACKEND,
147 G_PARAM_READWRITE |
148 G_PARAM_CONSTRUCT_ONLY));
149 }
150
151 static void
152 e_mail_config_ews_oal_combo_box_class_finalize (EMailConfigEwsOalComboBoxClass *class)
153 {
154 }
155
156 static void
157 e_mail_config_ews_oal_combo_box_init (EMailConfigEwsOalComboBox *combo_box)
158 {
159 combo_box->priv = e_mail_config_ews_oal_combo_box_get_instance_private (combo_box);
160
161 g_mutex_init (&combo_box->priv->oal_items_lock);
162 }
163
164 void
165 e_mail_config_ews_oal_combo_box_type_register (GTypeModule *type_module)
166 {
167 /* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
168 * function, so we have to wrap it with a public function in
169 * order to register types from a separate compilation unit. */
170 e_mail_config_ews_oal_combo_box_register_type (type_module);
171 }
172
173 GtkWidget *
174 e_mail_config_ews_oal_combo_box_new (EMailConfigServiceBackend *backend)
175 {
176 g_return_val_if_fail (E_IS_MAIL_CONFIG_SERVICE_BACKEND (backend), NULL);
177
178 return g_object_new (
179 E_TYPE_MAIL_CONFIG_EWS_OAL_COMBO_BOX,
180 "backend", backend, NULL);
181 }
182
183 EMailConfigServiceBackend *
184 e_mail_config_ews_oal_combo_box_get_backend (EMailConfigEwsOalComboBox *combo_box)
185 {
186 g_return_val_if_fail (
187 E_IS_MAIL_CONFIG_EWS_OAL_COMBO_BOX (combo_box), NULL);
188
189 return combo_box->priv->backend;
190 }
191
192 static ESourceAuthenticationResult
193 mail_config_ews_aol_combo_box_update_try_credentials_sync (EEwsConnection *connection,
194 const ENamedParameters *credentials,
195 gpointer user_data,
196 GCancellable *cancellable,
197 GError **error)
198 {
199 AsyncContext *async_context = user_data;
200 EMailConfigEwsOalComboBox *combo_box;
201 ESourceAuthenticationResult result;
202 GSList *oal_items = NULL;
203 GError *local_error = NULL;
204
205 combo_box = E_MAIL_CONFIG_EWS_OAL_COMBO_BOX (async_context->combo_box);
206
207 e_ews_connection_get_oal_list_sync (connection, &oal_items, cancellable, &local_error);
208
209 if (local_error == NULL) {
210 result = E_SOURCE_AUTHENTICATION_ACCEPTED;
211
212 /* Deposit results in the private struct for
213 * the update_finish() function to pick up. */
214 g_mutex_lock (&combo_box->priv->oal_items_lock);
215 g_slist_free_full (
216 combo_box->priv->oal_items,
217 (GDestroyNotify) ews_oal_free);
218 combo_box->priv->oal_items = oal_items;
219 g_mutex_unlock (&combo_box->priv->oal_items_lock);
220
221 } else if (g_error_matches (local_error, E_SOUP_SESSION_ERROR, SOUP_STATUS_UNAUTHORIZED)) {
222 result = E_SOURCE_AUTHENTICATION_REJECTED;
223 g_error_free (local_error);
224
225 } else {
226 result = E_SOURCE_AUTHENTICATION_ERROR;
227 g_propagate_error (error, local_error);
228 }
229
230 return result;
231 }
232
233 static void
234 mail_config_ews_aol_combo_box_update_thread_cb (GObject *with_object,
235 gpointer user_data,
236 GCancellable *cancellable,
237 GError **perror)
238 {
239 AsyncContext *async_context = user_data;
240 CamelEwsSettings *ews_settings;
241 const gchar *oab_url;
242 EEwsConnection *connection;
243
244 if (g_cancellable_set_error_if_cancelled (cancellable, perror))
245 return;
246
247 ews_settings = CAMEL_EWS_SETTINGS (async_context->settings);
248 oab_url = camel_ews_settings_get_oaburl (ews_settings);
249
250 connection = e_ews_config_utils_open_connection_for (async_context->source, ews_settings, oab_url,
251 mail_config_ews_aol_combo_box_update_try_credentials_sync, async_context, cancellable, perror);
252
253 g_clear_object (&connection);
254 }
255
256 static void
257 mail_config_ews_aol_combo_box_update_idle_cb (GObject *with_object,
258 gpointer user_data,
259 GCancellable *cancellable,
260 GError **perror)
261 {
262 AsyncContext *async_context;
263 GError *error = NULL;
264
265 async_context = (AsyncContext *) user_data;
266
267 if (perror) {
268 error = *perror;
269 *perror = NULL;
270 }
271
272 if (error != NULL)
273 g_simple_async_result_take_error (async_context->simple, error);
274
275 g_simple_async_result_complete (async_context->simple);
276 }
277
278 void
279 e_mail_config_ews_oal_combo_box_update (EMailConfigEwsOalComboBox *combo_box,
280 GCancellable *cancellable,
281 GAsyncReadyCallback callback,
282 gpointer user_data)
283 {
284 GSimpleAsyncResult *simple;
285 EMailConfigServiceBackend *backend;
286 AsyncContext *async_context;
287 CamelSettings *settings;
288 ESource *source;
289 ESource *collection;
290
291 g_return_if_fail (E_IS_MAIL_CONFIG_EWS_OAL_COMBO_BOX (combo_box));
292
293 backend = e_mail_config_ews_oal_combo_box_get_backend (combo_box);
294 settings = e_mail_config_service_backend_get_settings (backend);
295 source = e_mail_config_service_backend_get_source (backend);
296 collection = e_mail_config_service_backend_get_collection (backend);
297
298 if (collection && e_source_has_extension (collection, E_SOURCE_EXTENSION_AUTHENTICATION))
299 source = collection;
300
301 simple = g_simple_async_result_new (
302 G_OBJECT (combo_box), callback, user_data,
303 e_mail_config_ews_oal_combo_box_update);
304
305 async_context = g_slice_new0 (AsyncContext);
306 async_context->combo_box = g_object_ref (combo_box);
307 async_context->simple = simple; /* takes ownership */
308 async_context->source = g_object_ref (source);
309 async_context->settings = G_OBJECT (g_object_ref (settings));
310
311 /* Property changes can cause update of the UI, but this runs in a thread,
312 thus freeze the notify till be back in UI thread */
313 g_object_freeze_notify (async_context->settings);
314
315 e_ews_config_utils_run_in_thread (G_OBJECT (combo_box),
316 mail_config_ews_aol_combo_box_update_thread_cb,
317 mail_config_ews_aol_combo_box_update_idle_cb,
318 async_context, async_context_free, cancellable);
319 }
320
321 gboolean
322 e_mail_config_ews_oal_combo_box_update_finish (EMailConfigEwsOalComboBox *combo_box,
323 GAsyncResult *result,
324 GError **error)
325 {
326 GSimpleAsyncResult *simple;
327 GtkComboBoxText *combo_box_text;
328 GSList *list, *link;
329 gchar *active_id;
330
331 g_return_val_if_fail (
332 g_simple_async_result_is_valid (
333 result, G_OBJECT (combo_box),
334 e_mail_config_ews_oal_combo_box_update), FALSE);
335
336 simple = G_SIMPLE_ASYNC_RESULT (result);
337
338 if (g_simple_async_result_propagate_error (simple, error))
339 return FALSE;
340
341 /* Re-populate the combo box using the cached results. */
342
343 g_mutex_lock (&combo_box->priv->oal_items_lock);
344 list = combo_box->priv->oal_items;
345 combo_box->priv->oal_items = NULL;
346 g_mutex_unlock (&combo_box->priv->oal_items_lock);
347
348 active_id = g_strdup (gtk_combo_box_get_active_id (GTK_COMBO_BOX (combo_box)));
349 combo_box_text = GTK_COMBO_BOX_TEXT (combo_box);
350 gtk_combo_box_text_remove_all (combo_box_text);
351
352 for (link = list; link != NULL; link = g_slist_next (link)) {
353 EwsOAL *oal = link->data;
354 const gchar *name = oal->name;
355
356 while (name && *name == '\\')
357 name++;
358
359 gtk_combo_box_text_append (
360 combo_box_text, oal->id, name);
361 }
362
363 g_slist_free_full (list, (GDestroyNotify) ews_oal_free);
364
365 if (active_id && *active_id)
366 gtk_combo_box_set_active_id (GTK_COMBO_BOX (combo_box), active_id);
367 else
368 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 0);
369 g_free (active_id);
370
371 return TRUE;
372 }
373