Debian Codegraph @

src/EWS/evolution/e-mail-config-ews-backend.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 "e-mail-config-ews-backend.h"
9
10 #include <glib/gi18n-lib.h>
11
12 #include <camel/camel.h>
13 #include <libebackend/libebackend.h>
14
15 #include <mail/e-mail-config-auth-check.h>
16 #include <mail/e-mail-config-receiving-page.h>
17
18 #include "common/camel-ews-settings.h"
19 #include "common/e-oauth2-service-office365.h"
20
21 #include "e-mail-config-ews-autodiscover.h"
22 #include "e-ews-config-utils.h"
23 #include "e-ews-search-user.h"
24
25 struct _EMailConfigEwsBackendPrivate {
26 GtkWidget *user_entry;
27 GtkWidget *host_entry;
28 GtkWidget *url_button;
29 GtkWidget *oab_entry;
30 GtkWidget *auth_check;
31 GtkWidget *impersonate_user_entry;
32 GtkGrid *oauth2_settings_grid;
33 GtkWidget *oauth2_override_check;
34 GtkWidget *oauth2_v2_check;
35 GtkWidget *oauth2_tenant_entry;
36 GtkWidget *oauth2_client_id_entry;
37 GtkWidget *oauth2_redirect_uri_entry;
38 GtkWidget *oauth2_resource_uri_entry;
39 GtkWidget *oauth2_endpoint_host_entry;
40 };
41
42 G_DEFINE_DYNAMIC_TYPE_EXTENDED (EMailConfigEwsBackend, e_mail_config_ews_backend, E_TYPE_MAIL_CONFIG_SERVICE_BACKEND, 0,
43 G_ADD_PRIVATE_DYNAMIC (EMailConfigEwsBackend))
44
45 static ESource *
46 mail_config_ews_backend_new_collection (EMailConfigServiceBackend *backend)
47 {
48 EMailConfigServiceBackendClass *class;
49 ESourceBackend *extension;
50 ESource *source;
51 const gchar *extension_name;
52
53 /* This backend serves double duty. One instance holds the
54 * mail account source, another holds the mail transport source.
55 * We can differentiate by examining the EMailConfigServicePage
56 * the backend is associated with. We return a new collection
57 * for both the Receiving Page and Sending Page. Although the
58 * Sending Page instance ultimately gets discarded, it's still
59 * needed to avoid creating an [Ews Backend] extension in the
60 * mail transport source. */
61
62 class = E_MAIL_CONFIG_SERVICE_BACKEND_GET_CLASS (backend);
63
64 source = e_source_new (NULL, NULL, NULL);
65 extension_name = E_SOURCE_EXTENSION_COLLECTION;
66 extension = e_source_get_extension (source, extension_name);
67 e_source_backend_set_backend_name (extension, class->backend_name);
68
69 return source;
70 }
71
72 static void
73 search_for_impersonate_user_clicked_cb (GtkButton *button,
74 EMailConfigServiceBackend *backend)
75 {
76 EMailConfigEwsBackend *ews_backend;
77 ESource *source;
78 CamelSettings *settings;
79 EEwsConnection *conn;
80 GtkWindow *parent;
81 gchar *email = NULL;
82
83 g_return_if_fail (E_IS_MAIL_CONFIG_SERVICE_BACKEND (backend));
84
85 source = e_mail_config_service_backend_get_collection (backend);
86 if (!source)
87 source = e_mail_config_service_backend_get_source (backend);
88
89 ews_backend = E_MAIL_CONFIG_EWS_BACKEND (backend);
90 settings = e_mail_config_service_backend_get_settings (backend);
91 conn = e_ews_connection_new (source, gtk_entry_get_text (GTK_ENTRY (ews_backend->priv->host_entry)), CAMEL_EWS_SETTINGS (settings));
92 parent = e_ews_config_utils_get_widget_toplevel_window (GTK_WIDGET (button));
93
94 if (e_ews_search_user_modal (parent, conn, NULL, NULL, &email)) {
95 gtk_entry_set_text (GTK_ENTRY (ews_backend->priv->impersonate_user_entry), email);
96 }
97
98 g_object_unref (conn);
99 g_free (email);
100 }
101
102 static gboolean
103 mail_config_ews_backend_auth_mech_is_oauth2 (GBinding *binding,
104 const GValue *from_value,
105 GValue *to_value,
106 gpointer user_data)
107 {
108 gboolean is_office365;
109 const gchar *active_mechanism;
110
111 active_mechanism = g_value_get_string (from_value);
112 is_office365 = g_strcmp0 (active_mechanism, "Office365") == 0;
113
114 g_value_set_boolean (to_value, is_office365);
115
116 return TRUE;
117 }
118
119 static gboolean
120 mail_config_ews_active_mech_to_auth_mech (GBinding *binding,
121 const GValue *from_value,
122 GValue *to_value,
123 gpointer user_data)
124 {
125 const gchar *active_mechanism, *use_mechanism;
126
127 active_mechanism = g_value_get_string (from_value);
128
129 if (!active_mechanism || (
130 g_ascii_strcasecmp (active_mechanism, "NTLM") != 0 &&
131 g_ascii_strcasecmp (active_mechanism, "PLAIN") != 0 &&
132 g_ascii_strcasecmp (active_mechanism, "GSSAPI") != 0 &&
133 g_ascii_strcasecmp (active_mechanism, "Office365") != 0))
134 use_mechanism = "NTLM";
135 else
136 use_mechanism = active_mechanism;
137
138 g_value_set_string (to_value, use_mechanism);
139
140 return TRUE;
141 }
142
143 static void
144 mail_config_ews_backend_set_oauth2_tooltip (GtkWidget *widget,
145 const gchar *value,
146 const gchar *when_value_empty,
147 gchar *when_value_filled) /* takes ownership */
148 {
149 g_return_if_fail (GTK_IS_WIDGET (widget));
150
151 gtk_widget_set_tooltip_text (widget, value && *value ? when_value_filled : when_value_empty);
152
153 g_free (when_value_filled);
154 }
155
156 static void
157 mail_config_ews_backend_insert_widgets (EMailConfigServiceBackend *backend,
158 GtkBox *parent)
159 {
160 EMailConfigEwsBackend *ews_backend;
161 EMailConfigServicePage *page;
162 ESource *source;
163 ESourceExtension *extension;
164 CamelSettings *settings;
165 CamelEwsSettings *ews_settings;
166 GtkLabel *label;
167 GtkWidget *widget;
168 GtkWidget *container;
169 GtkWidget *expander;
170 GtkWidget *advanced_help;
171 GtkWidget *endpoint_host_label;
172 GtkWidget *redirect_uri_label;
173 GtkWidget *resource_uri_label;
174 const gchar *extension_name;
175 const gchar *text;
176 gchar *markup;
177
178 ews_backend = E_MAIL_CONFIG_EWS_BACKEND (backend);
179 page = e_mail_config_service_backend_get_page (backend);
180
181 /* This backend serves double duty. One instance holds the
182 * mail account source, another holds the mail transport source.
183 * We can differentiate by examining the EMailConfigServicePage
184 * the backend is associated with. This method only applies to
185 * the Receiving Page. */
186 if (!E_IS_MAIL_CONFIG_RECEIVING_PAGE (page))
187 return;
188
189 /* This needs to come _after_ the page type check so we don't
190 * introduce a backend extension in the mail transport source. */
191 settings = e_mail_config_service_backend_get_settings (backend);
192
193 text = _("Configuration");
194 markup = g_markup_printf_escaped ("<b>%s</b>", text);
195 widget = gtk_label_new (markup);
196 gtk_label_set_use_markup (GTK_LABEL (widget), TRUE);
197 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
198 gtk_box_pack_start (GTK_BOX (parent), widget, FALSE, FALSE, 0);
199 gtk_widget_show (widget);
200 g_free (markup);
201
202 widget = gtk_grid_new ();
203 gtk_widget_set_margin_left (widget, 12);
204 gtk_grid_set_row_spacing (GTK_GRID (widget), 6);
205 gtk_grid_set_column_spacing (GTK_GRID (widget), 6);
206 gtk_box_pack_start (GTK_BOX (parent), widget, FALSE, FALSE, 0);
207 gtk_widget_show (widget);
208
209 container = widget;
210
211 widget = gtk_label_new_with_mnemonic (_("User_name:"));
212 gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
213 gtk_grid_attach (GTK_GRID (container), widget, 0, 0, 1, 1);
214 gtk_widget_show (widget);
215
216 label = GTK_LABEL (widget);
217
218 widget = gtk_entry_new ();
219 gtk_widget_set_hexpand (widget, TRUE);
220 gtk_label_set_mnemonic_widget (label, widget);
221 gtk_grid_attach (GTK_GRID (container), widget, 1, 0, 2, 1);
222 ews_backend->priv->user_entry = widget; /* do not reference */
223 gtk_widget_show (widget);
224
225 widget = gtk_label_new_with_mnemonic (_("_Host URL:"));
226 gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
227 gtk_grid_attach (GTK_GRID (container), widget, 0, 1, 1, 1);
228 gtk_widget_show (widget);
229
230 label = GTK_LABEL (widget);
231
232 widget = gtk_entry_new ();
233 gtk_widget_set_hexpand (widget, TRUE);
234 gtk_label_set_mnemonic_widget (label, widget);
235 gtk_grid_attach (GTK_GRID (container), widget, 1, 1, 1, 1);
236 ews_backend->priv->host_entry = widget; /* do not reference */
237 gtk_widget_show (widget);
238
239 widget = e_mail_config_ews_autodiscover_new (backend);
240 gtk_grid_attach (GTK_GRID (container), widget, 2, 1, 1, 1);
241 ews_backend->priv->url_button = widget; /* do not reference */
242 gtk_widget_show (widget);
243
244 widget = gtk_label_new_with_mnemonic (_("OAB U_RL:"));
245 gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
246 gtk_grid_attach (GTK_GRID (container), widget, 0, 2, 1, 1);
247 gtk_widget_show (widget);
248
249 label = GTK_LABEL (widget);
250
251 widget = gtk_entry_new ();
252 gtk_widget_set_hexpand (widget, TRUE);
253 gtk_label_set_mnemonic_widget (label, widget);
254 gtk_grid_attach (GTK_GRID (container), widget, 1, 2, 2, 1);
255 ews_backend->priv->oab_entry = widget; /* do not reference */
256 gtk_widget_show (widget);
257
258 widget = gtk_check_button_new_with_mnemonic (_("Open _Mailbox of other user"));
259 gtk_grid_attach (GTK_GRID (container), widget, 1, 3, 1, 1);
260 gtk_widget_show (widget);
261
262 ews_settings = CAMEL_EWS_SETTINGS (settings);
263
264 camel_ews_settings_lock (ews_settings);
265
266 if (camel_ews_settings_get_use_impersonation (ews_settings)) {
267 const gchar *impersonate_user = camel_ews_settings_get_impersonate_user (ews_settings);
268
269 if (impersonate_user && !*impersonate_user) {
270 camel_ews_settings_unlock (ews_settings);
271
272 camel_ews_settings_set_impersonate_user (ews_settings, NULL);
273 camel_ews_settings_set_use_impersonation (ews_settings, FALSE);
274
275 camel_ews_settings_lock (ews_settings);
276 }
277 }
278
279 camel_ews_settings_unlock (ews_settings);
280
281 e_binding_bind_property (
282 settings, "use-impersonation",
283 widget, "active",
284 G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
285
286 widget = gtk_entry_new ();
287 gtk_widget_set_hexpand (widget, TRUE);
288 gtk_grid_attach (GTK_GRID (container), widget, 1, 4, 1, 1);
289 gtk_widget_show (widget);
290 ews_backend->priv->impersonate_user_entry = widget; /* do not reference */
291
292 e_binding_bind_object_text_property (
293 settings, "impersonate-user",
294 widget, "text",
295 G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
296
297 e_binding_bind_property (
298 settings, "use-impersonation",
299 widget, "sensitive",
300 G_BINDING_SYNC_CREATE);
301
302 widget = gtk_button_new_with_mnemonic (_("S_earch…"));
303 gtk_grid_attach (GTK_GRID (container), widget, 2, 4, 1, 1);
304 gtk_widget_show (widget);
305
306 e_binding_bind_property (
307 ews_backend->priv->impersonate_user_entry, "sensitive",
308 widget, "sensitive",
309 G_BINDING_SYNC_CREATE);
310
311 g_signal_connect (widget, "clicked", G_CALLBACK (search_for_impersonate_user_clicked_cb), backend);
312
313 text = _("Authentication");
314 markup = g_markup_printf_escaped ("<b>%s</b>", text);
315 widget = gtk_label_new (markup);
316 gtk_widget_set_margin_top (widget, 6);
317 gtk_label_set_use_markup (GTK_LABEL (widget), TRUE);
318 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
319 gtk_box_pack_start (GTK_BOX (parent), widget, FALSE, FALSE, 0);
320 gtk_widget_show (widget);
321 g_free (markup);
322
323 widget = e_mail_config_auth_check_new (backend);
324 gtk_widget_set_margin_left (widget, 12);
325 gtk_box_pack_start (GTK_BOX (parent), widget, FALSE, FALSE, 0);
326 ews_backend->priv->auth_check = widget; /* do not reference */
327 gtk_widget_show (widget);
328
329 widget = gtk_grid_new ();
330 gtk_widget_set_margin_left (widget, 12);
331 gtk_box_pack_start (GTK_BOX (parent), widget, FALSE, FALSE, 0);
332 ews_backend->priv->oauth2_settings_grid = GTK_GRID (widget);
333
334 gtk_grid_set_column_spacing (ews_backend->priv->oauth2_settings_grid, 4);
335 gtk_grid_set_row_spacing (ews_backend->priv->oauth2_settings_grid, 4);
336
337 widget = gtk_check_button_new_with_mnemonic (_("Use _protocol version 2.0 (for school and organization accounts)"));
338 ews_backend->priv->oauth2_v2_check = widget;
339 gtk_grid_attach (ews_backend->priv->oauth2_settings_grid, widget, 0, 0, 2, 1);
340
341 container = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
342 gtk_grid_attach (ews_backend->priv->oauth2_settings_grid, container, 0, 1, 2, 1);
343
344 widget = gtk_check_button_new_with_mnemonic (_("_Override Office365 OAuth2 settings"));
345 gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
346 ews_backend->priv->oauth2_override_check = widget;
347
348 markup = g_markup_printf_escaped ("(<a href=\"https://gitlab.gnome.org/GNOME/evolution/-/wikis/EWS-OAuth2\">%s</a>)", _("Help…"));
349 widget = gtk_label_new (markup);
350 gtk_label_set_use_markup (GTK_LABEL (widget), TRUE);
351 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
352 gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
353 g_free (markup);
354
355 widget = gtk_label_new_with_mnemonic (_("Application I_D:"));
356 gtk_widget_set_margin_left (widget, 12);
357 gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
358 gtk_grid_attach (ews_backend->priv->oauth2_settings_grid, widget, 0, 2, 1, 1);
359 label = GTK_LABEL (widget);
360
361 e_binding_bind_property (
362 ews_backend->priv->oauth2_override_check, "active",
363 widget, "sensitive",
364 G_BINDING_SYNC_CREATE);
365
366 widget = gtk_entry_new ();
367 gtk_widget_set_hexpand (widget, TRUE);
368 gtk_label_set_mnemonic_widget (label, widget);
369 gtk_grid_attach (ews_backend->priv->oauth2_settings_grid, widget, 1, 2, 1, 1);
370 ews_backend->priv->oauth2_client_id_entry = widget;
371
372 e_binding_bind_property (
373 ews_backend->priv->oauth2_override_check, "active",
374 widget, "sensitive",
375 G_BINDING_SYNC_CREATE);
376
377 mail_config_ews_backend_set_oauth2_tooltip (widget, OFFICE365_CLIENT_ID,
378 _("There is not set any default application ID"),
379 g_strdup_printf (_("Default application ID is “%s”"), OFFICE365_CLIENT_ID));
380
381 /* Translators: 'Tenant ID' here means a term used by Microsoft to identify a company or organization in an Office 365 world.
382 You probably do not want to translate it. More for example here: https://powerbi.microsoft.com/en-us/blog/what-is-a-tenant/ */
383 widget = gtk_label_new_with_mnemonic (_("_Tenant ID:"));
384 gtk_widget_set_margin_left (widget, 12);
385 gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
386 gtk_grid_attach (ews_backend->priv->oauth2_settings_grid, widget, 0, 3, 1, 1);
387 label = GTK_LABEL (widget);
388
389 e_binding_bind_property (
390 ews_backend->priv->oauth2_override_check, "active",
391 widget, "sensitive",
392 G_BINDING_SYNC_CREATE);
393
394 widget = gtk_entry_new ();
395 gtk_widget_set_hexpand (widget, TRUE);
396 gtk_label_set_mnemonic_widget (label, widget);
397 gtk_grid_attach (ews_backend->priv->oauth2_settings_grid, widget, 1, 3, 1, 1);
398 ews_backend->priv->oauth2_tenant_entry = widget;
399
400 e_binding_bind_property (
401 ews_backend->priv->oauth2_override_check, "active",
402 widget, "sensitive",
403 G_BINDING_SYNC_CREATE);
404
405 mail_config_ews_backend_set_oauth2_tooltip (widget, OFFICE365_TENANT,
406 /* Translators: 'Tenant' here means a term used by Microsoft to identify a company or organization in an Office 365 world. Same for 'common', it's a default URL path.
407 You probably do not want to translate it. More for example here: https://powerbi.microsoft.com/en-us/blog/what-is-a-tenant/ */
408 _("Default tenant ID is “common“"),
409 /* Translators: 'Tenant' here means a term used by Microsoft to identify a company or organization in an Office 365 world.
410 You probably do not want to translate it. More for example here: https://powerbi.microsoft.com/en-us/blog/what-is-a-tenant/ */
411 g_strdup_printf (_("Default tenant ID is “%s”"), OFFICE365_TENANT));
412
413 container = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
414 gtk_grid_attach (ews_backend->priv->oauth2_settings_grid, container, 0, 4, 2, 1);
415
416 widget = gtk_expander_new_with_mnemonic (_("_Advanced Settings"));
417 gtk_widget_set_margin_left (widget, 12);
418 gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
419 expander = widget;
420
421 e_binding_bind_property (
422 ews_backend->priv->oauth2_override_check, "active",
423 widget, "sensitive",
424 G_BINDING_SYNC_CREATE);
425
426 markup = g_markup_printf_escaped ("(<a href=\"https://gitlab.gnome.org/GNOME/evolution/-/wikis/EWS-OAuth2#alternative-endpoints\">%s</a>)", _("Help…"));
427 widget = gtk_label_new (markup);
428 gtk_label_set_use_markup (GTK_LABEL (widget), TRUE);
429 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
430 gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
431 g_free (markup);
432 advanced_help = widget;
433
434 widget = gtk_label_new_with_mnemonic (_("_Endpoint host:"));
435 gtk_widget_set_margin_left (widget, 12);
436 gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
437 gtk_grid_attach (ews_backend->priv->oauth2_settings_grid, widget, 0, 5, 1, 1);
438 label = GTK_LABEL (widget);
439 endpoint_host_label = widget;
440
441 e_binding_bind_property (
442 ews_backend->priv->oauth2_override_check, "active",
443 widget, "sensitive",
444 G_BINDING_SYNC_CREATE);
445
446 widget = gtk_entry_new ();
447 gtk_widget_set_hexpand (widget, TRUE);
448 gtk_label_set_mnemonic_widget (label, widget);
449 gtk_grid_attach (ews_backend->priv->oauth2_settings_grid, widget, 1, 5, 1, 1);
450 ews_backend->priv->oauth2_endpoint_host_entry = widget;
451
452 e_binding_bind_property (
453 ews_backend->priv->oauth2_override_check, "active",
454 widget, "sensitive",
455 G_BINDING_SYNC_CREATE);
456
457 markup = g_strdup_printf (_("Default endpoint host is “%s”"), "login.microsoftonline.com");
458 mail_config_ews_backend_set_oauth2_tooltip (widget, OFFICE365_ENDPOINT_HOST,
459 markup,
460 g_strdup_printf (_("Default endpoint host is “%s”"), OFFICE365_ENDPOINT_HOST));
461 g_free (markup);
462
463 widget = gtk_label_new_with_mnemonic (_("Red_irect URI:"));
464 gtk_widget_set_margin_left (widget, 12);
465 gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
466 gtk_grid_attach (ews_backend->priv->oauth2_settings_grid, widget, 0, 6, 1, 1);
467 label = GTK_LABEL (widget);
468 redirect_uri_label = widget;
469
470 e_binding_bind_property (
471 ews_backend->priv->oauth2_override_check, "active",
472 widget, "sensitive",
473 G_BINDING_SYNC_CREATE);
474
475 widget = gtk_entry_new ();
476 gtk_widget_set_hexpand (widget, TRUE);
477 gtk_label_set_mnemonic_widget (label, widget);
478 gtk_grid_attach (ews_backend->priv->oauth2_settings_grid, widget, 1, 6, 1, 1);
479 ews_backend->priv->oauth2_redirect_uri_entry = widget;
480
481 e_binding_bind_property (
482 ews_backend->priv->oauth2_override_check, "active",
483 widget, "sensitive",
484 G_BINDING_SYNC_CREATE);
485
486 markup = g_strdup_printf (_("Default redirect URI is “%s”"), "https://login.microsoftonline.com/common/oauth2/nativeclient");
487 mail_config_ews_backend_set_oauth2_tooltip (widget, OFFICE365_REDIRECT_URI,
488 markup,
489 g_strdup_printf (_("Default redirect URI is “%s”"), OFFICE365_REDIRECT_URI));
490 g_free (markup);
491
492 widget = gtk_label_new_with_mnemonic (_("Re_source URI:"));
493 gtk_widget_set_margin_left (widget, 12);
494 gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
495 gtk_grid_attach (ews_backend->priv->oauth2_settings_grid, widget, 0, 7, 1, 1);
496 label = GTK_LABEL (widget);
497 resource_uri_label = widget;
498
499 e_binding_bind_property (
500 ews_backend->priv->oauth2_override_check, "active",
501 widget, "sensitive",
502 G_BINDING_SYNC_CREATE);
503
504 widget = gtk_entry_new ();
505 gtk_widget_set_hexpand (widget, TRUE);
506 gtk_label_set_mnemonic_widget (label, widget);
507 gtk_grid_attach (ews_backend->priv->oauth2_settings_grid, widget, 1, 7, 1, 1);
508 ews_backend->priv->oauth2_resource_uri_entry = widget;
509
510 e_binding_bind_property (
511 ews_backend->priv->oauth2_override_check, "active",
512 widget, "sensitive",
513 G_BINDING_SYNC_CREATE);
514
515 markup = g_strdup_printf (_("Default resource URI is derived from the Host URL, or it can eventually fall back to “%s”."), OFFICE365_FALLBACK_RESOURCE_URI);
516 mail_config_ews_backend_set_oauth2_tooltip (widget, NULL,
517 markup,
518 NULL);
519 g_free (markup);
520
521 gtk_widget_show_all (GTK_WIDGET (ews_backend->priv->oauth2_settings_grid));
522
523 camel_ews_settings_lock (ews_settings);
524
525 gtk_expander_set_expanded (GTK_EXPANDER (expander),
526 (e_util_strcmp0 (camel_ews_settings_get_oauth2_endpoint_host (ews_settings), NULL) != 0 &&
527 e_util_strcmp0 (camel_ews_settings_get_oauth2_endpoint_host (ews_settings), OFFICE365_ENDPOINT_HOST) != 0) ||
528 (e_util_strcmp0 (camel_ews_settings_get_oauth2_redirect_uri (ews_settings), NULL) != 0 &&
529 e_util_strcmp0 (camel_ews_settings_get_oauth2_redirect_uri (ews_settings), OFFICE365_REDIRECT_URI) != 0) ||
530 e_util_strcmp0 (camel_ews_settings_get_oauth2_resource_uri (ews_settings), NULL) != 0);
531
532 camel_ews_settings_unlock (ews_settings);
533
534 e_binding_bind_property (
535 expander, "expanded",
536 advanced_help, "visible",
537 G_BINDING_SYNC_CREATE);
538
539 e_binding_bind_property (
540 expander, "expanded",
541 endpoint_host_label, "visible",
542 G_BINDING_SYNC_CREATE);
543
544 e_binding_bind_property (
545 expander, "expanded",
546 ews_backend->priv->oauth2_endpoint_host_entry, "visible",
547 G_BINDING_SYNC_CREATE);
548
549 e_binding_bind_property (
550 expander, "expanded",
551 redirect_uri_label, "visible",
552 G_BINDING_SYNC_CREATE);
553
554 e_binding_bind_property (
555 expander, "expanded",
556 ews_backend->priv->oauth2_redirect_uri_entry, "visible",
557 G_BINDING_SYNC_CREATE);
558
559 e_binding_bind_property (
560 expander, "expanded",
561 resource_uri_label, "visible",
562 G_BINDING_SYNC_CREATE);
563
564 e_binding_bind_property (
565 expander, "expanded",
566 ews_backend->priv->oauth2_resource_uri_entry, "visible",
567 G_BINDING_SYNC_CREATE);
568
569 e_binding_bind_property_full (
570 ews_backend->priv->auth_check, "active-mechanism",
571 ews_backend->priv->oauth2_settings_grid, "visible",
572 G_BINDING_SYNC_CREATE,
573 mail_config_ews_backend_auth_mech_is_oauth2,
574 NULL, NULL, NULL);
575
576 e_binding_bind_object_text_property (
577 settings, "user",
578 ews_backend->priv->user_entry, "text",
579 G_BINDING_BIDIRECTIONAL |
580 G_BINDING_SYNC_CREATE);
581
582 e_binding_bind_object_text_property (
583 settings, "hosturl",
584 ews_backend->priv->host_entry, "text",
585 G_BINDING_BIDIRECTIONAL |
586 G_BINDING_SYNC_CREATE);
587
588 e_binding_bind_object_text_property (
589 settings, "oaburl",
590 ews_backend->priv->oab_entry, "text",
591 G_BINDING_BIDIRECTIONAL |
592 G_BINDING_SYNC_CREATE);
593
594 /* Don't use G_BINDING_SYNC_CREATE here since the widget
595 * chooses its initial mechanism more intelligently than
596 * a simple property binding would. */
597 e_binding_bind_property_full (
598 settings, "auth-mechanism",
599 ews_backend->priv->auth_check, "active-mechanism",
600 G_BINDING_BIDIRECTIONAL,
601 NULL,
602 mail_config_ews_active_mech_to_auth_mech,
603 NULL, NULL);
604
605 e_binding_bind_property (
606 settings, "use-oauth2-v2",
607 ews_backend->priv->oauth2_v2_check, "active",
608 G_BINDING_BIDIRECTIONAL |
609 G_BINDING_SYNC_CREATE);
610
611 e_binding_bind_property (
612 settings, "override-oauth2",
613 ews_backend->priv->oauth2_override_check, "active",
614 G_BINDING_BIDIRECTIONAL |
615 G_BINDING_SYNC_CREATE);
616
617 e_binding_bind_object_text_property (
618 settings, "oauth2-tenant",
619 ews_backend->priv->oauth2_tenant_entry, "text",
620 G_BINDING_BIDIRECTIONAL |
621 G_BINDING_SYNC_CREATE);
622
623 e_binding_bind_object_text_property (
624 settings, "oauth2-client-id",
625 ews_backend->priv->oauth2_client_id_entry, "text",
626 G_BINDING_BIDIRECTIONAL |
627 G_BINDING_SYNC_CREATE);
628
629 e_binding_bind_object_text_property (
630 settings, "oauth2-redirect-uri",
631 ews_backend->priv->oauth2_redirect_uri_entry, "text",
632 G_BINDING_BIDIRECTIONAL |
633 G_BINDING_SYNC_CREATE);
634
635 e_binding_bind_object_text_property (
636 settings, "oauth2-resource-uri",
637 ews_backend->priv->oauth2_resource_uri_entry, "text",
638 G_BINDING_BIDIRECTIONAL |
639 G_BINDING_SYNC_CREATE);
640
641 e_binding_bind_object_text_property (
642 settings, "oauth2-endpoint-host",
643 ews_backend->priv->oauth2_endpoint_host_entry, "text",
644 G_BINDING_BIDIRECTIONAL |
645 G_BINDING_SYNC_CREATE);
646
647 extension_name = E_SOURCE_EXTENSION_COLLECTION;
648 source = e_mail_config_service_backend_get_collection (backend);
649 extension = e_source_get_extension (source, extension_name);
650
651 /* The collection identity is the user name. */
652 e_binding_bind_property (
653 settings, "user",
654 extension, "identity",
655 G_BINDING_BIDIRECTIONAL |
656 G_BINDING_SYNC_CREATE);
657 }
658
659 static void
660 mail_config_ews_backend_setup_defaults (EMailConfigServiceBackend *backend)
661 {
662 CamelSettings *settings;
663 EMailConfigServicePage *page;
664 const gchar *email_address;
665 gchar **parts = NULL;
666
667 page = e_mail_config_service_backend_get_page (backend);
668
669 /* This backend serves double duty. One instance holds the
670 * mail account source, another holds the mail transport source.
671 * We can differentiate by examining the EMailConfigServicePage
672 * the backend is associated with. This method only applies to
673 * the Receiving Page. */
674 if (!E_IS_MAIL_CONFIG_RECEIVING_PAGE (page))
675 return;
676
677 /* This needs to come _after_ the page type check so we don't
678 * introduce a backend extension in the mail transport source. */
679 settings = e_mail_config_service_backend_get_settings (backend);
680
681 email_address = e_mail_config_service_page_get_email_address (page);
682 if (email_address != NULL)
683 parts = g_strsplit (email_address, "@", 2);
684
685 if (parts != NULL && g_strv_length (parts) >= 2) {
686 CamelEwsSettings *ews_settings;
687 CamelNetworkSettings *network_settings;
688 gchar *hosturl;
689
690 g_strstrip (parts[0]); /* user name */
691 g_strstrip (parts[1]); /* domain name */
692
693 hosturl = g_strdup_printf (
694 "https://exchange.%s/EWS/Exchange.asmx", parts[1]);
695
696 ews_settings = CAMEL_EWS_SETTINGS (settings);
697 camel_ews_settings_set_hosturl (ews_settings, hosturl);
698 camel_ews_settings_set_email (ews_settings, email_address);
699
700 /* Prefill email address as the user name, it's needed for office365.com
701 server, but also on-premise servers support it. */
702 network_settings = CAMEL_NETWORK_SETTINGS (settings);
703 camel_network_settings_set_user (network_settings, email_address);
704
705 g_free (hosturl);
706 }
707
708 g_strfreev (parts);
709 }
710
711 static gboolean
712 mail_config_ews_backend_auto_configure (EMailConfigServiceBackend *backend,
713 EConfigLookup *config_lookup,
714 gint *out_priority,
715 gboolean *out_is_complete)
716 {
717 return e_mail_config_service_backend_auto_configure_for_kind (backend, config_lookup,
718 E_CONFIG_LOOKUP_RESULT_COLLECTION, NULL,
719 e_mail_config_service_backend_get_collection (backend),
720 out_priority, out_is_complete);
721 }
722
723 static gboolean
724 mail_config_ews_backend_check_complete (EMailConfigServiceBackend *backend)
725 {
726 EMailConfigServicePage *page;
727 EMailConfigEwsBackend *ews_backend;
728 CamelSettings *settings;
729 CamelEwsSettings *ews_settings;
730 CamelNetworkSettings *network_settings;
731 const gchar *hosturl, *oaburl;
732 const gchar *user;
733 gboolean correct, complete = TRUE;
734
735 ews_backend = E_MAIL_CONFIG_EWS_BACKEND (backend);
736 page = e_mail_config_service_backend_get_page (backend);
737
738 /* This backend serves double duty. One instance holds the
739 * mail account source, another holds the mail transport source.
740 * We can differentiate by examining the EMailConfigServicePage
741 * the backend is associated with. This method only applies to
742 * the Receiving Page. */
743 if (!E_IS_MAIL_CONFIG_RECEIVING_PAGE (page))
744 return TRUE;
745
746 /* This needs to come _after_ the page type check so we don't
747 * introduce a backend extension in the mail transport source. */
748 settings = e_mail_config_service_backend_get_settings (backend);
749
750 ews_settings = CAMEL_EWS_SETTINGS (settings);
751
752 camel_ews_settings_lock (ews_settings);
753
754 hosturl = camel_ews_settings_get_hosturl (ews_settings);
755 oaburl = camel_ews_settings_get_oaburl (ews_settings);
756
757 network_settings = CAMEL_NETWORK_SETTINGS (settings);
758 user = camel_network_settings_get_user (network_settings);
759
760 correct = hosturl != NULL && *hosturl != '\0';
761 complete = complete && correct;
762
763 if (correct) {
764 GUri *uri;
765
766 uri = g_uri_parse (hosturl, SOUP_HTTP_URI_FLAGS | G_URI_FLAGS_PARSE_RELAXED, NULL);
767 if (uri) {
768 g_uri_unref (uri);
769 e_util_set_entry_issue_hint (ews_backend->priv->host_entry, NULL);
770 } else {
771 e_util_set_entry_issue_hint (ews_backend->priv->host_entry, _("Host URL is not valid"));
772 complete = FALSE;
773 }
774 } else {
775 e_util_set_entry_issue_hint (ews_backend->priv->host_entry, _("Host URL cannot be empty"));
776 }
777
778 if (oaburl && *oaburl) {
779 GUri *uri;
780
781 uri = g_uri_parse (oaburl, SOUP_HTTP_URI_FLAGS | G_URI_FLAGS_PARSE_RELAXED, NULL);
782 if (uri) {
783 g_uri_unref (uri);
784 e_util_set_entry_issue_hint (ews_backend->priv->oab_entry, NULL);
785 } else {
786 e_util_set_entry_issue_hint (ews_backend->priv->oab_entry, _("OAB URL is not valid"));
787 complete = FALSE;
788 }
789 } else {
790 e_util_set_entry_issue_hint (ews_backend->priv->oab_entry, NULL);
791 }
792
793 correct = user != NULL && *user != '\0';
794 complete = complete && correct;
795
796 e_util_set_entry_issue_hint (ews_backend->priv->user_entry, correct ? NULL : _("User name cannot be empty"));
797
798 if (correct && camel_ews_settings_get_auth_mechanism (ews_settings) == EWS_AUTH_TYPE_OAUTH2) {
799 const gchar *client_id;
800
801 if (camel_ews_settings_get_override_oauth2 (ews_settings)) {
802 client_id = camel_ews_settings_get_oauth2_client_id (ews_settings);
803 } else {
804 client_id = OFFICE365_CLIENT_ID;
805 }
806
807 correct = e_util_strcmp0 (client_id, NULL) != 0;
808 complete = complete && correct;
809
810 e_util_set_entry_issue_hint (ews_backend->priv->oauth2_client_id_entry, correct ? NULL : _("Application ID cannot be empty"));
811 }
812
813 camel_ews_settings_unlock (ews_settings);
814
815 return complete;
816 }
817
818 static void
819 mail_config_ews_backend_commit_changes (EMailConfigServiceBackend *backend)
820 {
821 CamelSettings *settings;
822 CamelEwsSettings *ews_settings;
823 EMailConfigServicePage *page;
824 const gchar *email_address;
825
826 page = e_mail_config_service_backend_get_page (backend);
827
828 /* This backend serves double duty. One instance holds the
829 * mail account source, another holds the mail transport source.
830 * We can differentiate by examining the EMailConfigServicePage
831 * the backend is associated with. This method only applies to
832 * the Receiving Page. */
833 if (!E_IS_MAIL_CONFIG_RECEIVING_PAGE (page))
834 return;
835
836 /* This needs to come _after_ the page type check so we don't
837 * introduce a backend extension in the mail transport source. */
838 settings = e_mail_config_service_backend_get_settings (backend);
839
840 email_address = e_mail_config_service_page_get_email_address (page);
841 if (email_address != NULL) {
842 ews_settings = CAMEL_EWS_SETTINGS (settings);
843 camel_ews_settings_set_email (ews_settings, email_address);
844 }
845 }
846
847 static void
848 e_mail_config_ews_backend_class_init (EMailConfigEwsBackendClass *class)
849 {
850 EMailConfigServiceBackendClass *backend_class;
851
852 backend_class = E_MAIL_CONFIG_SERVICE_BACKEND_CLASS (class);
853 backend_class->backend_name = "ews";
854 backend_class->new_collection = mail_config_ews_backend_new_collection;
855 backend_class->insert_widgets = mail_config_ews_backend_insert_widgets;
856 backend_class->setup_defaults = mail_config_ews_backend_setup_defaults;
857 backend_class->auto_configure = mail_config_ews_backend_auto_configure;
858 backend_class->check_complete = mail_config_ews_backend_check_complete;
859 backend_class->commit_changes = mail_config_ews_backend_commit_changes;
860 }
861
862 static void
863 e_mail_config_ews_backend_class_finalize (EMailConfigEwsBackendClass *class)
864 {
865 }
866
867 static void
868 e_mail_config_ews_backend_init (EMailConfigEwsBackend *backend)
869 {
870 backend->priv = e_mail_config_ews_backend_get_instance_private (backend);
871 }
872
873 void
874 e_mail_config_ews_backend_type_register (GTypeModule *type_module)
875 {
876 /* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
877 * function, so we have to wrap it with a public function in
878 * order to register types from a separate compilation unit. */
879 e_mail_config_ews_backend_register_type (type_module);
880 }
881