Debian Codegraph @

src/EWS/evolution/e-mail-config-ews-ooo-page.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 <string.h>
9 #include <unistd.h>
10
11 #include <gtk/gtk.h>
12 #include <glib/gi18n-lib.h>
13
14 #include <e-util/e-util.h>
15
16 #include "common/camel-ews-settings.h"
17 #include "common/e-ews-connection.h"
18 #include "common/e-ews-connection-utils.h"
19 #include "common/e-ews-oof-settings.h"
20
21 #include "e-ews-config-utils.h"
22
23 #include "e-mail-config-ews-ooo-page.h"
24
25 typedef struct _AsyncContext AsyncContext;
26
27 struct _EMailConfigEwsOooPagePrivate {
28 ESourceRegistry *registry;
29 ESource *account_source;
30 ESource *identity_source;
31 ESource *collection_source;
32
33 /* The try_password() method deposits results here.
34 * This avoids calling GTK+ functions from multiple threads. */
35 EEwsOofSettings *oof_settings;
36 GMutex oof_settings_lock;
37
38 GCancellable *refresh_cancellable;
39
40 /* to not save unchanged state */
41 gboolean changed;
42
43 GtkWidget *enabled_radio_button; /* not referenced */
44 GtkWidget *disabled_radio_button; /* not referenced */
45 GtkWidget *scheduled_radio_button; /* not referenced */
46 GtkWidget *start_time; /* not referenced */
47 GtkWidget *end_time; /* not referenced */
48 GtkWidget *external_audience; /* not referenced */
49 GtkTextBuffer *internal_reply; /* not referenced */
50 GtkTextBuffer *external_reply; /* not referenced */
51 };
52
53 struct _AsyncContext {
54 EMailConfigEwsOooPage *page;
55 EActivity *activity;
56 ESource *source;
57 GObject *settings;
58 };
59
60 enum {
61 PROP_0,
62 PROP_ACCOUNT_SOURCE,
63 PROP_COLLECTION_SOURCE,
64 PROP_IDENTITY_SOURCE,
65 PROP_REGISTRY
66 };
67
68 /* Forward Declarations */
69 static void e_mail_config_ews_ooo_page_interface_init
70 (EMailConfigPageInterface *iface);
71
72 G_DEFINE_DYNAMIC_TYPE_EXTENDED (EMailConfigEwsOooPage, e_mail_config_ews_ooo_page, E_TYPE_MAIL_CONFIG_ACTIVITY_PAGE, 0,
73 G_ADD_PRIVATE_DYNAMIC (EMailConfigEwsOooPage)
74 G_IMPLEMENT_INTERFACE_DYNAMIC (E_TYPE_MAIL_CONFIG_PAGE, e_mail_config_ews_ooo_page_interface_init))
75
76 static void
77 async_context_free (gpointer ptr)
78 {
79 AsyncContext *async_context = ptr;
80
81 if (!async_context)
82 return;
83
84 if (async_context->settings)
85 g_object_thaw_notify (async_context->settings);
86
87 g_clear_object (&async_context->page);
88 g_clear_object (&async_context->activity);
89 g_clear_object (&async_context->source);
90 g_clear_object (&async_context->settings);
91
92 g_slice_free (AsyncContext, async_context);
93 }
94
95 static CamelSettings *
96 mail_config_ews_ooo_page_get_settings (EMailConfigEwsOooPage *page)
97 {
98 ESource *source;
99 ESourceCamel *extension;
100 const gchar *extension_name;
101
102 source = e_mail_config_ews_ooo_page_get_collection_source (page);
103
104 extension_name = e_source_camel_get_extension_name ("ews");
105 extension = e_source_get_extension (source, extension_name);
106
107 return e_source_camel_get_settings (extension);
108 }
109
110 static const gchar *
111 mail_config_ews_ooo_page_get_mailbox (EMailConfigEwsOooPage *page)
112 {
113 ESourceRegistry *registry;
114 ESource *collection_source;
115 GList *list, *link;
116 const gchar *collection_uid;
117 const gchar *extension_name;
118 const gchar *mailbox = NULL;
119
120 /* Find the mail identity source that belongs to
121 * our collection and return its email address. */
122
123 collection_source =
124 e_mail_config_ews_ooo_page_get_collection_source (page);
125 collection_uid = e_source_get_uid (collection_source);
126
127 extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
128 registry = e_mail_config_ews_ooo_page_get_registry (page);
129 list = e_source_registry_list_sources (registry, extension_name);
130
131 for (link = list; link != NULL; link = g_list_next (link)) {
132 ESource *source = E_SOURCE (link->data);
133 ESourceMailIdentity *extension;
134 const gchar *parent_uid;
135
136 parent_uid = e_source_get_parent (source);
137 if (g_strcmp0 (parent_uid, collection_uid) != 0)
138 continue;
139
140 extension = e_source_get_extension (source, extension_name);
141 mailbox = e_source_mail_identity_get_address (extension);
142 break;
143 }
144
145 g_list_free_full (list, g_object_unref);
146
147 return mailbox;
148 }
149
150 static void
151 mail_config_ews_ooo_page_display_settings (EMailConfigEwsOooPage *page,
152 EEwsOofSettings *oof_settings)
153 {
154 GtkWidget *button;
155 GDateTime *date_time;
156
157 switch (e_ews_oof_settings_get_state (oof_settings)) {
158 default:
159 /* fall through */
160 case E_EWS_OOF_STATE_DISABLED:
161 button = page->priv->disabled_radio_button;
162 break;
163 case E_EWS_OOF_STATE_ENABLED:
164 button = page->priv->enabled_radio_button;
165 break;
166 case E_EWS_OOF_STATE_SCHEDULED:
167 button = page->priv->scheduled_radio_button;
168 break;
169 }
170
171 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
172
173 gtk_combo_box_set_active (
174 GTK_COMBO_BOX (page->priv->external_audience),
175 e_ews_oof_settings_get_external_audience (oof_settings));
176
177 date_time = e_ews_oof_settings_ref_start_time (oof_settings);
178 e_date_edit_set_time (
179 E_DATE_EDIT (page->priv->start_time),
180 (time_t) g_date_time_to_unix (date_time));
181 g_date_time_unref (date_time);
182
183 date_time = e_ews_oof_settings_ref_end_time (oof_settings);
184 e_date_edit_set_time (
185 E_DATE_EDIT (page->priv->end_time),
186 (time_t) g_date_time_to_unix (date_time));
187 g_date_time_unref (date_time);
188
189 gtk_text_buffer_set_text (
190 page->priv->internal_reply,
191 e_ews_oof_settings_get_internal_reply (oof_settings), -1);
192
193 gtk_text_buffer_set_text (
194 page->priv->external_reply,
195 e_ews_oof_settings_get_external_reply (oof_settings), -1);
196 }
197
198 static void
199 mail_config_ews_ooo_page_set_account_source (EMailConfigEwsOooPage *page,
200 ESource *account_source)
201 {
202 g_return_if_fail (E_IS_SOURCE (account_source));
203 g_return_if_fail (page->priv->account_source == NULL);
204
205 page->priv->account_source = g_object_ref (account_source);
206 }
207
208 static void
209 mail_config_ews_ooo_page_set_collection_source (EMailConfigEwsOooPage *page,
210 ESource *collection_source)
211 {
212 g_return_if_fail (E_IS_SOURCE (collection_source));
213 g_return_if_fail (page->priv->collection_source == NULL);
214
215 page->priv->collection_source = g_object_ref (collection_source);
216 }
217
218 static void
219 mail_config_ews_ooo_page_set_identity_source (EMailConfigEwsOooPage *page,
220 ESource *identity_source)
221 {
222 g_return_if_fail (E_IS_SOURCE (identity_source));
223 g_return_if_fail (page->priv->identity_source == NULL);
224
225 page->priv->identity_source = g_object_ref (identity_source);
226 }
227
228 static void
229 mail_config_ews_ooo_page_set_registry (EMailConfigEwsOooPage *page,
230 ESourceRegistry *registry)
231 {
232 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
233 g_return_if_fail (page->priv->registry == NULL);
234
235 page->priv->registry = g_object_ref (registry);
236 }
237
238 static void
239 mail_config_ews_ooo_page_set_property (GObject *object,
240 guint property_id,
241 const GValue *value,
242 GParamSpec *pspec)
243 {
244 switch (property_id) {
245 case PROP_ACCOUNT_SOURCE:
246 mail_config_ews_ooo_page_set_account_source (
247 E_MAIL_CONFIG_EWS_OOO_PAGE (object),
248 g_value_get_object (value));
249 return;
250
251 case PROP_COLLECTION_SOURCE:
252 mail_config_ews_ooo_page_set_collection_source (
253 E_MAIL_CONFIG_EWS_OOO_PAGE (object),
254 g_value_get_object (value));
255 return;
256
257 case PROP_IDENTITY_SOURCE:
258 mail_config_ews_ooo_page_set_identity_source (
259 E_MAIL_CONFIG_EWS_OOO_PAGE (object),
260 g_value_get_object (value));
261 return;
262
263 case PROP_REGISTRY:
264 mail_config_ews_ooo_page_set_registry (
265 E_MAIL_CONFIG_EWS_OOO_PAGE (object),
266 g_value_get_object (value));
267 return;
268 }
269
270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
271 }
272
273 static void
274 mail_config_ews_ooo_page_get_property (GObject *object,
275 guint property_id,
276 GValue *value,
277 GParamSpec *pspec)
278 {
279 switch (property_id) {
280 case PROP_ACCOUNT_SOURCE:
281 g_value_set_object (
282 value,
283 e_mail_config_ews_ooo_page_get_account_source (
284 E_MAIL_CONFIG_EWS_OOO_PAGE (object)));
285 return;
286
287 case PROP_COLLECTION_SOURCE:
288 g_value_set_object (
289 value,
290 e_mail_config_ews_ooo_page_get_collection_source (
291 E_MAIL_CONFIG_EWS_OOO_PAGE (object)));
292 return;
293
294 case PROP_IDENTITY_SOURCE:
295 g_value_set_object (
296 value,
297 e_mail_config_ews_ooo_page_get_identity_source (
298 E_MAIL_CONFIG_EWS_OOO_PAGE (object)));
299 return;
300
301 case PROP_REGISTRY:
302 g_value_set_object (
303 value,
304 e_mail_config_ews_ooo_page_get_registry (
305 E_MAIL_CONFIG_EWS_OOO_PAGE (object)));
306 return;
307 }
308
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
310 }
311
312 static void
313 mail_config_ews_ooo_page_dispose (GObject *object)
314 {
315 EMailConfigEwsOooPage *ooo_page = E_MAIL_CONFIG_EWS_OOO_PAGE (object);
316
317 if (ooo_page->priv->refresh_cancellable) {
318 g_cancellable_cancel (ooo_page->priv->refresh_cancellable);
319 g_clear_object (&ooo_page->priv->refresh_cancellable);
320 }
321
322 g_clear_object (&ooo_page->priv->registry);
323 g_clear_object (&ooo_page->priv->account_source);
324 g_clear_object (&ooo_page->priv->collection_source);
325 g_clear_object (&ooo_page->priv->identity_source);
326 g_clear_object (&ooo_page->priv->oof_settings);
327
328 /* Chain up to parent's dispose() method. */
329 G_OBJECT_CLASS (e_mail_config_ews_ooo_page_parent_class)->dispose (object);
330 }
331
332 static void
333 mail_config_ews_ooo_page_finalize (GObject *object)
334 {
335 EMailConfigEwsOooPage *ooo_page = E_MAIL_CONFIG_EWS_OOO_PAGE (object);
336
337 g_mutex_clear (&ooo_page->priv->oof_settings_lock);
338
339 /* Chain up to parent's finalize() method. */
340 G_OBJECT_CLASS (e_mail_config_ews_ooo_page_parent_class)->finalize (object);
341 }
342
343 static void
344 mail_config_ews_ooo_page_constructed (GObject *object)
345 {
346 EMailConfigEwsOooPage *page;
347 GtkLabel *label;
348 GtkWidget *grid;
349 GtkWidget *widget;
350 GtkWidget *container;
351 GtkWidget *main_box;
352 GtkSizeGroup *size_group;
353 GtkTextBuffer *text_buffer;
354 GSList *group = NULL;
355 const gchar *text;
356 gchar *markup;
357
358 page = E_MAIL_CONFIG_EWS_OOO_PAGE (object);
359
360 /* Chain up to parent's constructed() method. */
361 G_OBJECT_CLASS (e_mail_config_ews_ooo_page_parent_class)->constructed (object);
362
363 main_box = e_mail_config_activity_page_get_internal_box (E_MAIL_CONFIG_ACTIVITY_PAGE (page));
364 gtk_box_set_spacing (GTK_BOX (main_box), 12);
365
366 size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
367
368 text = _("Out of Office");
369 markup = g_markup_printf_escaped ("<b>%s</b>", text);
370 widget = gtk_label_new (markup);
371 gtk_label_set_use_markup (GTK_LABEL (widget), TRUE);
372 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
373 gtk_box_pack_start (GTK_BOX (main_box), widget, FALSE, FALSE, 0);
374 gtk_widget_show (widget);
375 g_free (markup);
376
377 widget = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
378 gtk_widget_set_margin_left (widget, 12);
379 gtk_box_pack_start (GTK_BOX (main_box), widget, FALSE, FALSE, 0);
380 gtk_widget_show (widget);
381
382 container = widget;
383
384 text = _("The messages specified below will be automatically sent to "
385 "each internal and external person who sends a mail to you.");
386 widget = gtk_label_new (text);
387 gtk_label_set_width_chars (GTK_LABEL (widget), 20);
388 gtk_label_set_max_width_chars (GTK_LABEL (widget), 80);
389 gtk_label_set_line_wrap (GTK_LABEL (widget), TRUE);
390 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
391 gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
392 gtk_widget_show (widget);
393
394 text = _("Do _not send Out of Office replies");
395 widget = gtk_radio_button_new_with_mnemonic (group, text);
396 gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
397 page->priv->disabled_radio_button = widget; /* do not reference */
398 gtk_widget_show (widget);
399
400 group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget));
401
402 text = _("_Send Out of Office replies");
403 widget = gtk_radio_button_new_with_mnemonic (group, text);
404 gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
405 page->priv->enabled_radio_button = widget; /* do not reference */
406 gtk_widget_show (widget);
407
408 group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget));
409
410 text = _("Send Out of Office replies only _during this time period:");
411 widget = gtk_radio_button_new_with_mnemonic (group, text);
412 gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
413 page->priv->scheduled_radio_button = widget; /* do not reference */
414 gtk_widget_show (widget);
415
416 widget = gtk_grid_new ();
417 gtk_grid_set_row_spacing (GTK_GRID (widget), 6);
418 gtk_grid_set_column_spacing (GTK_GRID (widget), 6);
419 gtk_box_pack_start (GTK_BOX (main_box), widget, TRUE, TRUE, 0);
420 gtk_widget_show (widget);
421
422 e_binding_bind_property (
423 page->priv->disabled_radio_button, "active",
424 widget, "sensitive",
425 G_BINDING_SYNC_CREATE |
426 G_BINDING_INVERT_BOOLEAN);
427
428 grid = widget;
429
430 widget = gtk_label_new_with_mnemonic (_("_From:"));
431 gtk_widget_set_margin_left (widget, 12);
432 gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
433 gtk_grid_attach (GTK_GRID (grid), widget, 0, 1, 1, 1);
434 gtk_size_group_add_widget (size_group, widget);
435 gtk_widget_show (widget);
436
437 e_binding_bind_property (
438 page->priv->scheduled_radio_button, "active",
439 widget, "sensitive",
440 G_BINDING_SYNC_CREATE);
441
442 label = GTK_LABEL (widget);
443
444 widget = e_date_edit_new ();
445 gtk_label_set_mnemonic_widget (label, widget);
446 gtk_grid_attach (GTK_GRID (grid), widget, 1, 1, 1, 1);
447 page->priv->start_time = widget; /* do not reference */
448 gtk_widget_show (widget);
449
450 e_binding_bind_property (
451 page->priv->scheduled_radio_button, "active",
452 widget, "sensitive",
453 G_BINDING_SYNC_CREATE);
454
455 widget = gtk_label_new_with_mnemonic (_("_To:"));
456 gtk_widget_set_margin_left (widget, 12);
457 gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
458 gtk_grid_attach (GTK_GRID (grid), widget, 0, 2, 1, 1);
459 gtk_size_group_add_widget (size_group, widget);
460 gtk_widget_show (widget);
461
462 e_binding_bind_property (
463 page->priv->scheduled_radio_button, "active",
464 widget, "sensitive",
465 G_BINDING_SYNC_CREATE);
466
467 label = GTK_LABEL (widget);
468
469 widget = e_date_edit_new ();
470 gtk_label_set_mnemonic_widget (label, widget);
471 gtk_grid_attach (GTK_GRID (grid), widget, 1, 2, 1, 1);
472 page->priv->end_time = widget; /* do not reference */
473 gtk_widget_show (widget);
474
475 e_binding_bind_property (
476 page->priv->scheduled_radio_button, "active",
477 widget, "sensitive",
478 G_BINDING_SYNC_CREATE);
479
480 widget = gtk_label_new_with_mnemonic (_("I_nternal:"));
481 gtk_widget_set_margin_top (widget, 12);
482 gtk_widget_set_margin_left (widget, 12);
483 gtk_widget_set_valign (widget, GTK_ALIGN_START);
484 gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
485 gtk_grid_attach (GTK_GRID (grid), widget, 0, 3, 1, 1);
486 gtk_size_group_add_widget (size_group, widget);
487 gtk_widget_show (widget);
488
489 text = _("Message to be sent within the organization");
490 gtk_widget_set_tooltip_text (widget, text);
491
492 label = GTK_LABEL (widget);
493
494 widget = gtk_scrolled_window_new (NULL, NULL);
495 gtk_scrolled_window_set_policy (
496 GTK_SCROLLED_WINDOW (widget),
497 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
498 gtk_scrolled_window_set_shadow_type (
499 GTK_SCROLLED_WINDOW (widget), GTK_SHADOW_IN);
500 gtk_widget_set_margin_top (widget, 12);
501 gtk_widget_set_hexpand (widget, TRUE);
502 gtk_widget_set_vexpand (widget, TRUE);
503 gtk_grid_attach (GTK_GRID (grid), widget, 1, 3, 1, 1);
504 gtk_widget_show (widget);
505
506 container = widget;
507
508 widget = gtk_text_view_new ();
509 gtk_label_set_mnemonic_widget (label, widget);
510 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (widget), GTK_WRAP_WORD);
511 gtk_container_add (GTK_CONTAINER (container), widget);
512 text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
513 page->priv->internal_reply = text_buffer; /* do not reference */
514 e_spell_text_view_attach (GTK_TEXT_VIEW (widget));
515 gtk_widget_show (widget);
516
517 widget = gtk_label_new_with_mnemonic (_("E_xternal:"));
518 gtk_widget_set_margin_top (widget, 12);
519 gtk_widget_set_margin_left (widget, 12);
520 gtk_misc_set_alignment (GTK_MISC (widget), 1.0, 0.5);
521 gtk_grid_attach (GTK_GRID (grid), widget, 0, 4, 1, 1);
522 gtk_size_group_add_widget (size_group, widget);
523 gtk_widget_show (widget);
524
525 text = _("Message to be sent outside the organization");
526 gtk_widget_set_tooltip_text (widget, text);
527
528 label = GTK_LABEL (widget);
529
530 /* The order of the combo box items needs to stay
531 * syncrhonized with the EEwsExternalAudience enum. */
532 widget = gtk_combo_box_text_new ();
533 gtk_combo_box_text_append_text (
534 GTK_COMBO_BOX_TEXT (widget),
535 _("Do not reply to senders outside the organization"));
536 gtk_combo_box_text_append_text (
537 GTK_COMBO_BOX_TEXT (widget),
538 _("Reply only to known senders outside the organization"));
539 gtk_combo_box_text_append_text (
540 GTK_COMBO_BOX_TEXT (widget),
541 _("Reply to any sender outside the organization"));
542 gtk_widget_set_margin_top (widget, 12);
543 gtk_grid_attach (GTK_GRID (grid), widget, 1, 4, 1, 1);
544 page->priv->external_audience = widget; /* do not reference */
545 gtk_widget_show (widget);
546
547 widget = gtk_scrolled_window_new (NULL, NULL);
548 gtk_scrolled_window_set_policy (
549 GTK_SCROLLED_WINDOW (widget),
550 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
551 gtk_scrolled_window_set_shadow_type (
552 GTK_SCROLLED_WINDOW (widget), GTK_SHADOW_IN);
553 gtk_widget_set_hexpand (widget, TRUE);
554 gtk_widget_set_vexpand (widget, TRUE);
555 gtk_grid_attach (GTK_GRID (grid), widget, 1, 5, 1, 1);
556 gtk_widget_show (widget);
557
558 container = widget;
559
560 widget = gtk_text_view_new ();
561 gtk_label_set_mnemonic_widget (label, widget);
562 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (widget), GTK_WRAP_WORD);
563 gtk_container_add (GTK_CONTAINER (container), widget);
564 text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
565 page->priv->external_reply = text_buffer; /* do not reference */
566 e_spell_text_view_attach (GTK_TEXT_VIEW (widget));
567 gtk_widget_show (widget);
568
569 /* XXX Bit of a hack. Since the enum value for "none" is zero,
570 * sensitize the text view if the combo box has a non-zero
571 * "active" value (in other words, anything but "none"). */
572 e_binding_bind_property (
573 page->priv->external_audience, "active",
574 widget, "sensitive",
575 G_BINDING_SYNC_CREATE);
576
577 g_object_unref (size_group);
578
579 e_mail_config_page_set_content (E_MAIL_CONFIG_PAGE (page), main_box);
580
581 e_mail_config_ews_ooo_page_refresh (page);
582 }
583
584 /* Helper for mail_config_ews_ooo_page_submit() */
585 static void
586 mail_config_ews_ooo_page_submit_cb (GObject *source_object,
587 GAsyncResult *result,
588 gpointer user_data)
589 {
590 GSimpleAsyncResult *simple;
591 GError *error = NULL;
592
593 simple = G_SIMPLE_ASYNC_RESULT (user_data);
594
595 e_ews_oof_settings_submit_finish (
596 E_EWS_OOF_SETTINGS (source_object), result, &error);
597
598 if (error != NULL)
599 g_simple_async_result_take_error (simple, error);
600
601 g_simple_async_result_complete (simple);
602
603 g_object_unref (simple);
604 }
605
606 static void
607 ews_oof_settings_changed (gboolean *pchanged)
608 {
609 g_return_if_fail (pchanged != NULL);
610
611 *pchanged = TRUE;
612 }
613
614 static void
615 mail_config_ews_ooo_page_submit (EMailConfigPage *page,
616 GCancellable *cancellable,
617 GAsyncReadyCallback callback,
618 gpointer user_data)
619 {
620 EMailConfigEwsOooPage *ooo_page;
621 GSimpleAsyncResult *simple;
622 GtkToggleButton *toggle_button;
623 GtkTextBuffer *text_buffer;
624 GtkTextIter start, end;
625 GDateTime *date_time;
626 gchar *text;
627 time_t tm;
628 gulong signal_id;
629
630 ooo_page = E_MAIL_CONFIG_EWS_OOO_PAGE (page);
631
632 g_mutex_lock (&ooo_page->priv->oof_settings_lock);
633
634 /* It may be that the Out of Office settings are still
635 * loading or have failed to load, in which case there
636 * are obviously no changes to submit. */
637 if (!ooo_page->priv->oof_settings) {
638 g_mutex_unlock (&ooo_page->priv->oof_settings_lock);
639
640 simple = g_simple_async_result_new (
641 G_OBJECT (page), callback, user_data,
642 mail_config_ews_ooo_page_submit);
643 g_simple_async_result_complete (simple);
644 g_object_unref (simple);
645
646 return;
647 }
648
649 signal_id = g_signal_connect_swapped (
650 ooo_page->priv->oof_settings, "notify",
651 G_CALLBACK (ews_oof_settings_changed), &ooo_page->priv->changed);
652
653 toggle_button = GTK_TOGGLE_BUTTON (ooo_page->priv->enabled_radio_button);
654 if (gtk_toggle_button_get_active (toggle_button))
655 e_ews_oof_settings_set_state (
656 ooo_page->priv->oof_settings,
657 E_EWS_OOF_STATE_ENABLED);
658
659 toggle_button = GTK_TOGGLE_BUTTON (ooo_page->priv->disabled_radio_button);
660 if (gtk_toggle_button_get_active (toggle_button))
661 e_ews_oof_settings_set_state (
662 ooo_page->priv->oof_settings,
663 E_EWS_OOF_STATE_DISABLED);
664
665 toggle_button = GTK_TOGGLE_BUTTON (ooo_page->priv->scheduled_radio_button);
666 if (gtk_toggle_button_get_active (toggle_button))
667 e_ews_oof_settings_set_state (
668 ooo_page->priv->oof_settings,
669 E_EWS_OOF_STATE_SCHEDULED);
670
671 tm = e_date_edit_get_time (E_DATE_EDIT (ooo_page->priv->start_time));
672 date_time = g_date_time_new_from_unix_utc ((gint64) tm);
673 e_ews_oof_settings_set_start_time (ooo_page->priv->oof_settings, date_time);
674 g_date_time_unref (date_time);
675
676 tm = e_date_edit_get_time (E_DATE_EDIT (ooo_page->priv->end_time));
677 date_time = g_date_time_new_from_unix_utc ((gint64) tm);
678 e_ews_oof_settings_set_end_time (ooo_page->priv->oof_settings, date_time);
679 g_date_time_unref (date_time);
680
681 text_buffer = ooo_page->priv->internal_reply;
682 gtk_text_buffer_get_bounds (text_buffer, &start, &end);
683 text = gtk_text_buffer_get_text (text_buffer, &start, &end, FALSE);
684 e_ews_oof_settings_set_internal_reply (ooo_page->priv->oof_settings, text);
685 g_free (text);
686
687 e_ews_oof_settings_set_external_audience (ooo_page->priv->oof_settings,
688 gtk_combo_box_get_active (GTK_COMBO_BOX (ooo_page->priv->external_audience)));
689
690 text_buffer = ooo_page->priv->external_reply;
691 gtk_text_buffer_get_bounds (text_buffer, &start, &end);
692 text = gtk_text_buffer_get_text (text_buffer, &start, &end, FALSE);
693 e_ews_oof_settings_set_external_reply (ooo_page->priv->oof_settings, text);
694 g_free (text);
695
696 simple = g_simple_async_result_new (
697 G_OBJECT (page), callback, user_data,
698 mail_config_ews_ooo_page_submit);
699
700 g_signal_handler_disconnect (ooo_page->priv->oof_settings, signal_id);
701
702 if (ooo_page->priv->changed) {
703 g_simple_async_result_set_check_cancellable (simple, cancellable);
704
705 e_ews_oof_settings_submit (
706 ooo_page->priv->oof_settings, cancellable,
707 mail_config_ews_ooo_page_submit_cb,
708 g_object_ref (simple));
709 } else {
710 g_simple_async_result_complete (simple);
711 }
712
713 g_object_unref (simple);
714
715 g_mutex_unlock (&ooo_page->priv->oof_settings_lock);
716 }
717
718 static gboolean
719 mail_config_ews_ooo_page_submit_finish (EMailConfigPage *page,
720 GAsyncResult *result,
721 GError **error)
722 {
723 GSimpleAsyncResult *simple;
724
725 g_return_val_if_fail (
726 g_simple_async_result_is_valid (
727 result, G_OBJECT (page),
728 mail_config_ews_ooo_page_submit), FALSE);
729
730 simple = G_SIMPLE_ASYNC_RESULT (result);
731
732 /* Assume success unless a GError is set. */
733 return !g_simple_async_result_propagate_error (simple, error);
734 }
735
736 static ESourceAuthenticationResult
737 mail_config_ews_ooo_page_try_credentials_sync (EEwsConnection *connection,
738 const ENamedParameters *credentials,
739 gpointer user_data,
740 GCancellable *cancellable,
741 GError **error)
742 {
743 AsyncContext *async_context = user_data;
744 EMailConfigEwsOooPage *page;
745 ESourceAuthenticationResult result;
746 EEwsOofSettings *oof_settings;
747 const gchar *mailbox;
748 GError *local_error = NULL;
749
750 page = async_context->page;
751
752 mailbox = mail_config_ews_ooo_page_get_mailbox (page);
753 e_ews_connection_set_mailbox (connection, mailbox);
754
755 oof_settings = e_ews_oof_settings_new_sync (connection, cancellable, &local_error);
756
757 if (oof_settings != NULL) {
758 result = E_SOURCE_AUTHENTICATION_ACCEPTED;
759 g_warn_if_fail (local_error == NULL);
760
761 /* The page takes ownership of the settings. */
762 g_mutex_lock (&page->priv->oof_settings_lock);
763 if (page->priv->oof_settings != NULL)
764 g_object_unref (oof_settings);
765 page->priv->oof_settings = oof_settings;
766 page->priv->changed = FALSE;
767 g_mutex_unlock (&page->priv->oof_settings_lock);
768
769 } else if (g_error_matches (local_error, E_SOUP_SESSION_ERROR, SOUP_STATUS_UNAUTHORIZED)) {
770 result = E_SOURCE_AUTHENTICATION_REJECTED;
771 g_error_free (local_error);
772
773 } else {
774 result = E_SOURCE_AUTHENTICATION_ERROR;
775 g_propagate_error (error, local_error);
776 }
777
778 return result;
779 }
780
781 static void
782 mail_config_ews_ooo_page_refresh_thread_cb (GObject *with_object,
783 gpointer user_data,
784 GCancellable *cancellable,
785 GError **perror)
786 {
787 AsyncContext *async_context = user_data;
788 CamelEwsSettings *ews_settings;
789 EEwsConnection *connection;
790
791 if (g_cancellable_set_error_if_cancelled (cancellable, perror))
792 return;
793
794 ews_settings = CAMEL_EWS_SETTINGS (async_context->settings);
795 connection = e_ews_config_utils_open_connection_for (async_context->source, ews_settings, NULL,
796 mail_config_ews_ooo_page_try_credentials_sync, async_context, cancellable, perror);
797
798 g_clear_object (&connection);
799 }
800
801 static void
802 mail_config_ews_ooo_page_refresh_idle_cb (GObject *with_object,
803 gpointer user_data,
804 GCancellable *cancellable,
805 GError **perror)
806 {
807 AsyncContext *async_context;
808 EAlertSink *alert_sink;
809 GError *error = NULL;
810
811 async_context = (AsyncContext *) user_data;
812
813 if (perror) {
814 error = *perror;
815 *perror = NULL;
816 }
817
818 alert_sink = e_activity_get_alert_sink (async_context->activity);
819
820 if (e_activity_handle_cancellation (async_context->activity, error)) {
821 g_error_free (error);
822
823 } else if (error != NULL) {
824 e_alert_submit (
825 alert_sink,
826 "ews:query-ooo-error",
827 error->message, NULL);
828 g_error_free (error);
829
830 } else {
831 EMailConfigEwsOooPage *page = async_context->page;
832
833 g_mutex_lock (&page->priv->oof_settings_lock);
834
835 if (page->priv->oof_settings != NULL)
836 mail_config_ews_ooo_page_display_settings (
837 page, page->priv->oof_settings);
838
839 g_mutex_unlock (&page->priv->oof_settings_lock);
840 }
841 }
842
843 static void
844 e_mail_config_ews_ooo_page_class_init (EMailConfigEwsOooPageClass *class)
845 {
846 GObjectClass *object_class;
847
848 object_class = G_OBJECT_CLASS (class);
849 object_class->set_property = mail_config_ews_ooo_page_set_property;
850 object_class->get_property = mail_config_ews_ooo_page_get_property;
851 object_class->dispose = mail_config_ews_ooo_page_dispose;
852 object_class->finalize = mail_config_ews_ooo_page_finalize;
853 object_class->constructed = mail_config_ews_ooo_page_constructed;
854
855 g_object_class_install_property (
856 object_class,
857 PROP_ACCOUNT_SOURCE,
858 g_param_spec_object (
859 "account-source",
860 "Account Source",
861 "Mail account source being edited",
862 E_TYPE_SOURCE,
863 G_PARAM_READWRITE |
864 G_PARAM_CONSTRUCT_ONLY));
865
866 g_object_class_install_property (
867 object_class,
868 PROP_COLLECTION_SOURCE,
869 g_param_spec_object (
870 "collection-source",
871 "Collection Source",
872 "Collection source being edited",
873 E_TYPE_SOURCE,
874 G_PARAM_READWRITE |
875 G_PARAM_CONSTRUCT_ONLY));
876
877 g_object_class_install_property (
878 object_class,
879 PROP_IDENTITY_SOURCE,
880 g_param_spec_object (
881 "identity-source",
882 "Identity Source",
883 "Mail identity source being edited",
884 E_TYPE_SOURCE,
885 G_PARAM_READWRITE |
886 G_PARAM_CONSTRUCT_ONLY));
887
888 g_object_class_install_property (
889 object_class,
890 PROP_REGISTRY,
891 g_param_spec_object (
892 "registry",
893 "Registry",
894 "Data source registry",
895 E_TYPE_SOURCE_REGISTRY,
896 G_PARAM_READWRITE |
897 G_PARAM_CONSTRUCT_ONLY));
898 }
899
900 static void
901 e_mail_config_ews_ooo_page_interface_init (EMailConfigPageInterface *iface)
902 {
903 iface->title = _("Out of Office");
904 iface->sort_order = E_MAIL_CONFIG_EWS_OOO_PAGE_SORT_ORDER;
905 iface->submit = mail_config_ews_ooo_page_submit;
906 iface->submit_finish = mail_config_ews_ooo_page_submit_finish;
907 }
908
909 static void
910 e_mail_config_ews_ooo_page_class_finalize (EMailConfigEwsOooPageClass *class)
911 {
912 }
913
914 static void
915 e_mail_config_ews_ooo_page_init (EMailConfigEwsOooPage *page)
916 {
917 page->priv = e_mail_config_ews_ooo_page_get_instance_private (page);
918
919 g_mutex_init (&page->priv->oof_settings_lock);
920 }
921
922 void
923 e_mail_config_ews_ooo_page_type_register (GTypeModule *type_module)
924 {
925 /* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
926 * function, so we have to wrap it with a public function in
927 * order to register types from a separate compilation unit. */
928 e_mail_config_ews_ooo_page_register_type (type_module);
929 }
930
931 EMailConfigPage *
932 e_mail_config_ews_ooo_page_new (ESourceRegistry *registry,
933 ESource *account_source,
934 ESource *identity_source,
935 ESource *collection_source)
936 {
937 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
938 g_return_val_if_fail (E_IS_SOURCE (account_source), NULL);
939 g_return_val_if_fail (E_IS_SOURCE (identity_source), NULL);
940 g_return_val_if_fail (E_IS_SOURCE (collection_source), NULL);
941
942 return g_object_new (
943 E_TYPE_MAIL_CONFIG_EWS_OOO_PAGE,
944 "registry", registry,
945 "account-source", account_source,
946 "identity-source", identity_source,
947 "collection-source", collection_source,
948 NULL);
949 }
950
951 void
952 e_mail_config_ews_ooo_page_refresh (EMailConfigEwsOooPage *page)
953 {
954 ESource *source;
955 EActivity *activity;
956 GCancellable *cancellable;
957 CamelSettings *settings;
958 AsyncContext *async_context;
959
960 g_return_if_fail (E_IS_MAIL_CONFIG_EWS_OOO_PAGE (page));
961
962 source = e_mail_config_ews_ooo_page_get_collection_source (page);
963
964 if (page->priv->refresh_cancellable) {
965 g_cancellable_cancel (page->priv->refresh_cancellable);
966 g_clear_object (&page->priv->refresh_cancellable);
967 }
968
969 activity = e_mail_config_activity_page_new_activity (
970 E_MAIL_CONFIG_ACTIVITY_PAGE (page));
971 cancellable = e_activity_get_cancellable (activity);
972 page->priv->refresh_cancellable = g_object_ref (cancellable);
973
974 e_activity_set_text (
975 activity, _("Retrieving “Out of Office” settings"));
976
977 settings = mail_config_ews_ooo_page_get_settings (page);
978
979 async_context = g_slice_new0 (AsyncContext);
980 async_context->page = g_object_ref (page);
981 async_context->activity = activity; /* takes ownership */
982 async_context->source = g_object_ref (source);
983 async_context->settings = G_OBJECT (g_object_ref (settings));
984
985 /* Property changes can cause update of the UI, but this runs in a thread,
986 thus freeze the notify till be back in UI thread */
987 g_object_freeze_notify (async_context->settings);
988
989 e_ews_config_utils_run_in_thread (G_OBJECT (page),
990 mail_config_ews_ooo_page_refresh_thread_cb,
991 mail_config_ews_ooo_page_refresh_idle_cb,
992 async_context, async_context_free, cancellable);
993 }
994
995 ESourceRegistry *
996 e_mail_config_ews_ooo_page_get_registry (EMailConfigEwsOooPage *page)
997 {
998 g_return_val_if_fail (E_IS_MAIL_CONFIG_EWS_OOO_PAGE (page), NULL);
999
1000 return page->priv->registry;
1001 }
1002
1003 ESource *
1004 e_mail_config_ews_ooo_page_get_account_source (EMailConfigEwsOooPage *page)
1005 {
1006 g_return_val_if_fail (E_IS_MAIL_CONFIG_EWS_OOO_PAGE (page), NULL);
1007
1008 return page->priv->account_source;
1009 }
1010
1011 ESource *
1012 e_mail_config_ews_ooo_page_get_identity_source (EMailConfigEwsOooPage *page)
1013 {
1014 g_return_val_if_fail (E_IS_MAIL_CONFIG_EWS_OOO_PAGE (page), NULL);
1015
1016 return page->priv->identity_source;
1017 }
1018
1019 ESource *
1020 e_mail_config_ews_ooo_page_get_collection_source (EMailConfigEwsOooPage *page)
1021 {
1022 g_return_val_if_fail (E_IS_MAIL_CONFIG_EWS_OOO_PAGE (page), NULL);
1023
1024 return page->priv->collection_source;
1025 }
1026