Debian Codegraph @

src/Microsoft365/registry/e-m365-backend.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 2020 Red Hat (www.redhat.com)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "evolution-ews-config.h"
7
8 #include <glib/gi18n-lib.h>
9
10 #include "common/e-m365-connection.h"
11 #include "common/e-source-m365-folder.h"
12 #include "common/camel-m365-settings.h"
13
14 #include "e-source-m365-deltas.h"
15
16 #include "e-m365-backend.h"
17
18 #define LOCK(_backend) g_mutex_lock (&_backend->priv->property_lock)
19 #define UNLOCK(_backend) g_mutex_unlock (&_backend->priv->property_lock)
20
21 struct _EM365BackendPrivate {
22 GMutex property_lock;
23
24 GHashTable *folder_sources; /* gchar *folder_id ~> ESource * */
25
26 gboolean need_update_folders;
27
28 gulong source_changed_id;
29 };
30
31 G_DEFINE_DYNAMIC_TYPE_EXTENDED (EM365Backend, e_m365_backend, E_TYPE_COLLECTION_BACKEND, 0,
32 G_ADD_PRIVATE_DYNAMIC (EM365Backend))
33
34 static void
35 m365_backend_claim_old_resources (ECollectionBackend *backend)
36 {
37 ESourceRegistryServer *registry;
38 GList *old_resources, *iter;
39
40 g_return_if_fail (E_IS_COLLECTION_BACKEND (backend));
41
42 registry = e_collection_backend_ref_server (backend);
43 old_resources = e_collection_backend_claim_all_resources (backend);
44
45 for (iter = old_resources; iter; iter = g_list_next (iter)) {
46 ESource *source = iter->data;
47
48 e_source_registry_server_add_source (registry, source);
49 }
50
51 g_list_free_full (old_resources, g_object_unref);
52 g_clear_object (&registry);
53 }
54
55 static void m365_backend_populate (ECollectionBackend *backend);
56
57 static void
58 m365_backend_source_changed_cb (ESource *source,
59 EM365Backend *backend)
60 {
61 if (!e_collection_backend_get_part_enabled (E_COLLECTION_BACKEND (backend), E_COLLECTION_BACKEND_PART_ANY)) {
62 backend->priv->need_update_folders = TRUE;
63 return;
64 }
65
66 if (!backend->priv->need_update_folders)
67 return;
68
69 m365_backend_populate (E_COLLECTION_BACKEND (backend));
70 }
71
72 static void
73 m365_backend_populate (ECollectionBackend *collection_backend)
74 {
75 ESource *source;
76 EM365Backend *m365_backend = E_M365_BACKEND (collection_backend);
77 EBackend *backend = E_BACKEND (m365_backend);
78
79 source = e_backend_get_source (E_BACKEND (backend));
80
81 m365_backend->priv->need_update_folders = TRUE;
82
83 if (!m365_backend->priv->source_changed_id) {
84 m365_backend->priv->source_changed_id = g_signal_connect (source, "changed",
85 G_CALLBACK (m365_backend_source_changed_cb), m365_backend);
86 }
87
88 /* do not do anything, if account is disabled */
89 if (!e_collection_backend_get_part_enabled (collection_backend, E_COLLECTION_BACKEND_PART_ANY))
90 return;
91
92 if (!e_collection_backend_freeze_populate (collection_backend)) {
93 e_collection_backend_thaw_populate (collection_backend);
94 return;
95 }
96
97 m365_backend_claim_old_resources (collection_backend);
98
99 if (e_backend_get_online (backend))
100 e_backend_schedule_authenticate (backend, NULL);
101
102 e_collection_backend_thaw_populate (collection_backend);
103 }
104
105 static void
106 m365_backend_update_resource (EM365Backend *m365_backend,
107 const gchar *extension_name,
108 const gchar *id,
109 const gchar *group_id,
110 const gchar *display_name,
111 gboolean is_default,
112 const gchar *calendar_color)
113 {
114 ESource *source;
115 gboolean is_new;
116
117 LOCK (m365_backend);
118 source = g_hash_table_lookup (m365_backend->priv->folder_sources, id);
119 if (source)
120 g_object_ref (source);
121 UNLOCK (m365_backend);
122
123 is_new = !source;
124
125 if (is_new)
126 source = e_collection_backend_new_child (E_COLLECTION_BACKEND (m365_backend), id);
127
128 if (source) {
129 ESourceM365Folder *folder_ext;
130
131 folder_ext = e_source_get_extension (source, E_SOURCE_EXTENSION_M365_FOLDER);
132
133 if (is_new || !e_source_m365_folder_get_display_name (folder_ext) ||
134 g_strcmp0 (e_source_m365_folder_get_display_name (folder_ext), e_source_get_display_name (source)) == 0)
135 e_source_set_display_name (source, display_name);
136
137 e_source_m365_folder_set_display_name (folder_ext, display_name);
138
139 if (calendar_color && g_ascii_strcasecmp (calendar_color, "auto") != 0 && (
140 g_strcmp0 (extension_name, E_SOURCE_EXTENSION_CALENDAR) == 0 ||
141 g_strcmp0 (extension_name, E_SOURCE_EXTENSION_TASK_LIST) == 0 ||
142 g_strcmp0 (extension_name, E_SOURCE_EXTENSION_MEMO_LIST) == 0)) {
143 ESourceSelectable *selectable;
144
145 selectable = e_source_get_extension (source, extension_name);
146
147 if (is_new || !e_source_m365_folder_get_color (folder_ext) ||
148 g_strcmp0 (e_source_m365_folder_get_color (folder_ext), e_source_selectable_get_color (selectable)) == 0) {
149 e_source_selectable_set_color (selectable, calendar_color);
150 }
151
152 e_source_m365_folder_set_color (folder_ext, calendar_color);
153 }
154
155 if (is_new) {
156 ESourceRegistryServer *server;
157 gpointer extension;
158
159 extension = e_source_get_extension (source, extension_name);
160 e_source_backend_set_backend_name (E_SOURCE_BACKEND (extension), "microsoft365");
161
162 /* Do not notify with too old reminders */
163 if (g_strcmp0 (extension_name, E_SOURCE_EXTENSION_CALENDAR) == 0 ||
164 g_strcmp0 (extension_name, E_SOURCE_EXTENSION_TASK_LIST) == 0) {
165 ESourceAlarms *alarms;
166 gchar *today;
167 GTimeVal today_tv;
168 GDate dt;
169
170 g_date_clear (&dt, 1);
171 g_get_current_time (&today_tv);
172 g_date_set_time_val (&dt, &today_tv);
173
174 /* midnight UTC */
175 today = g_strdup_printf ("%04d-%02d-%02dT00:00:00Z", g_date_get_year (&dt), g_date_get_month (&dt), g_date_get_day (&dt));
176
177 alarms = e_source_get_extension (source, E_SOURCE_EXTENSION_ALARMS);
178 e_source_alarms_set_last_notified (alarms, today);
179
180 g_free (today);
181 }
182
183 if (g_strcmp0 (id, E_M365_ARTIFICIAL_FOLDER_ID_ORG_CONTACTS) == 0 ||
184 g_strcmp0 (id, E_M365_ARTIFICIAL_FOLDER_ID_USERS) == 0) {
185 ESourceOffline *offline_ext;
186
187 offline_ext = e_source_get_extension (source, E_SOURCE_EXTENSION_OFFLINE);
188 /* look up by default, these can be very large */
189 e_source_offline_set_stay_synchronized (offline_ext, FALSE);
190 }
191
192 e_source_m365_folder_set_id (folder_ext, id);
193 e_source_m365_folder_set_group_id (folder_ext, group_id);
194 e_source_m365_folder_set_is_default (folder_ext, is_default);
195
196 server = e_collection_backend_ref_server (E_COLLECTION_BACKEND (m365_backend));
197
198 e_source_registry_server_add_source (server, source);
199
200 g_clear_object (&server);
201 } else if (!e_source_has_extension (source, E_SOURCE_EXTENSION_OFFLINE) && (
202 g_strcmp0 (id, E_M365_ARTIFICIAL_FOLDER_ID_ORG_CONTACTS) == 0 ||
203 g_strcmp0 (id, E_M365_ARTIFICIAL_FOLDER_ID_USERS) == 0)) {
204 ESourceOffline *offline_ext;
205
206 offline_ext = e_source_get_extension (source, E_SOURCE_EXTENSION_OFFLINE);
207 /* existing books without the extension mean they had been synchronized for offline use */
208 e_source_offline_set_stay_synchronized (offline_ext, TRUE);
209 }
210 }
211
212 g_clear_object (&source);
213 }
214
215 static void
216 m365_backend_remove_resource (EM365Backend *m365_backend,
217 const gchar *extension_name,
218 const gchar *id) /* NULL to remove the "is-default" resource for the extension_name */
219 {
220 ESource *existing_source = NULL;
221
222 LOCK (m365_backend);
223
224 if (id) {
225 existing_source = g_hash_table_lookup (m365_backend->priv->folder_sources, id);
226 } else {
227 GHashTableIter iter;
228 gpointer value;
229
230 g_hash_table_iter_init (&iter, m365_backend->priv->folder_sources);
231 while (g_hash_table_iter_next (&iter, NULL, &value)) {
232 ESource *source = value;
233
234 if (value && e_source_has_extension (source, extension_name) &&
235 e_source_m365_folder_get_is_default (e_source_get_extension (source, E_SOURCE_EXTENSION_M365_FOLDER))) {
236 existing_source = source;
237 break;
238 }
239 }
240 }
241
242 if (existing_source)
243 g_object_ref (existing_source);
244
245 UNLOCK (m365_backend);
246
247 if (existing_source)
248 e_source_remove_sync (existing_source, NULL, NULL);
249
250 g_clear_object (&existing_source);
251 }
252
253 static GHashTable * /* gchar *uid ~> NULL */
254 m365_backend_get_known_folder_ids (EM365Backend *m365_backend,
255 const gchar *extension_name,
256 gboolean with_the_default)
257 {
258 GHashTable *ids;
259 GHashTableIter iter;
260 gpointer value;
261
262 ids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
263
264 LOCK (m365_backend);
265
266 g_hash_table_iter_init (&iter, m365_backend->priv->folder_sources);
267
268 while (g_hash_table_iter_next (&iter, NULL, &value)) {
269 ESource *source = value;
270
271 if (source && e_source_has_extension (source, extension_name)) {
272 ESourceM365Folder *m365_folder;
273
274 m365_folder = e_source_get_extension (source, E_SOURCE_EXTENSION_M365_FOLDER);
275
276 if (with_the_default || !e_source_m365_folder_get_is_default (m365_folder))
277 g_hash_table_insert (ids, e_source_m365_folder_dup_id (m365_folder), NULL);
278 }
279 }
280
281 UNLOCK (m365_backend);
282
283 return ids;
284 }
285
286 static void
287 m365_backend_forget_folders_hash (EM365Backend *m365_backend,
288 const gchar *extension_name,
289 GHashTable *ids) /* gchar *id ~> NULL */
290 {
291 GHashTableIter iter;
292 gpointer key;
293
294 g_hash_table_iter_init (&iter, ids);
295
296 while (g_hash_table_iter_next (&iter, &key, NULL)) {
297 const gchar *id = key;
298
299 if (id)
300 m365_backend_remove_resource (m365_backend, extension_name, id);
301 }
302 }
303
304 static void
305 m365_backend_forget_book_folders (EM365Backend *m365_backend,
306 const gchar *extension_name)
307 {
308 GHashTable *ids;
309
310 ids = m365_backend_get_known_folder_ids (m365_backend, extension_name, FALSE);
311
312 g_hash_table_remove (ids, E_M365_ARTIFICIAL_FOLDER_ID_ORG_CONTACTS);
313 g_hash_table_remove (ids, E_M365_ARTIFICIAL_FOLDER_ID_USERS);
314 g_hash_table_remove (ids, E_M365_ARTIFICIAL_FOLDER_ID_PEOPLE);
315
316 m365_backend_forget_folders_hash (m365_backend, extension_name, ids);
317
318 g_hash_table_destroy (ids);
319 }
320
321 static gboolean
322 m365_backend_got_contact_folders_delta_cb (EM365Connection *cnc,
323 const GSList *results, /* JsonObject * - the returned objects from the server */
324 gpointer user_data,
325 GCancellable *cancellable,
326 GError **error)
327 {
328 EM365Backend *m365_backend = user_data;
329 GSList *link;
330
331 g_return_val_if_fail (E_IS_M365_BACKEND (m365_backend), FALSE);
332
333 for (link = (GSList *) results; link; link = g_slist_next (link)) {
334 JsonObject *object = link->data;
335 const gchar *id = e_m365_folder_get_id (object);
336
337 if (!id)
338 continue;
339
340 if (e_m365_delta_is_removed_object (object)) {
341 m365_backend_remove_resource (m365_backend, E_SOURCE_EXTENSION_ADDRESS_BOOK, id);
342 } else {
343 m365_backend_update_resource (m365_backend, E_SOURCE_EXTENSION_ADDRESS_BOOK,
344 id, NULL, e_m365_folder_get_display_name (object),
345 FALSE, NULL);
346 }
347 }
348
349 return TRUE;
350 }
351
352 static void
353 m365_backend_sync_contact_folders_sync (EM365Backend *m365_backend,
354 EM365Connection *cnc,
355 GCancellable *cancellable)
356 {
357 EM365Folder *user_contacts = NULL;
358 ESourceM365Deltas *m365_deltas;
359 gchar *old_delta_link, *new_delta_link;
360 gboolean success;
361 GError *error = NULL;
362
363 m365_deltas = e_source_get_extension (e_backend_get_source (E_BACKEND (m365_backend)), E_SOURCE_EXTENSION_M365_DELTAS);
364
365 if (e_m365_connection_get_contacts_folder_sync (cnc, NULL, NULL, NULL, &user_contacts, cancellable, &error)) {
366 const gchar *id, *display_name;
367
368 id = e_m365_folder_get_id (user_contacts);
369 display_name = e_m365_folder_get_display_name (user_contacts);
370
371 g_warn_if_fail (id != NULL);
372 g_warn_if_fail (display_name != NULL);
373
374 m365_backend_update_resource (m365_backend,
375 E_SOURCE_EXTENSION_ADDRESS_BOOK,
376 id, NULL, display_name, TRUE, NULL);
377
378 json_object_unref (user_contacts);
379 } else if (g_error_matches (error, E_SOUP_SESSION_ERROR, SOUP_STATUS_NOT_FOUND) ||
380 g_error_matches (error, E_SOUP_SESSION_ERROR, SOUP_STATUS_UNAUTHORIZED)) {
381 m365_backend_remove_resource (m365_backend, E_SOURCE_EXTENSION_ADDRESS_BOOK, NULL);
382 }
383
384 g_clear_error (&error);
385
386 new_delta_link = NULL;
387 old_delta_link = e_source_m365_deltas_dup_contacts_link (m365_deltas);
388
389 success = e_m365_connection_get_folders_delta_sync (cnc, NULL, E_M365_FOLDER_KIND_CONTACTS, NULL, old_delta_link, 0,
390 m365_backend_got_contact_folders_delta_cb, m365_backend, &new_delta_link, cancellable, &error);
391
392 if (old_delta_link && *old_delta_link && e_m365_connection_util_delta_token_failed (error)) {
393 g_clear_pointer (&old_delta_link, g_free);
394 g_clear_error (&error);
395
396 m365_backend_forget_book_folders (m365_backend, E_SOURCE_EXTENSION_ADDRESS_BOOK);
397
398 success = e_m365_connection_get_folders_delta_sync (cnc, NULL, E_M365_FOLDER_KIND_CONTACTS, NULL, NULL, 0,
399 m365_backend_got_contact_folders_delta_cb, m365_backend, &new_delta_link, cancellable, &error);
400 }
401
402 if (success)
403 e_source_m365_deltas_set_contacts_link (m365_deltas, new_delta_link);
404
405 g_clear_pointer (&old_delta_link, g_free);
406 g_clear_pointer (&new_delta_link, g_free);
407 g_clear_error (&error);
408
409 if (e_m365_connection_get_org_contacts_accessible_sync (cnc, NULL, cancellable, NULL)) {
410 m365_backend_update_resource (m365_backend, E_SOURCE_EXTENSION_ADDRESS_BOOK,
411 E_M365_ARTIFICIAL_FOLDER_ID_ORG_CONTACTS, NULL, _("Organizational Contacts"), TRUE, NULL);
412 } else {
413 m365_backend_remove_resource (m365_backend, E_SOURCE_EXTENSION_ADDRESS_BOOK, E_M365_ARTIFICIAL_FOLDER_ID_ORG_CONTACTS);
414 }
415
416 if (e_m365_connection_get_users_accessible_sync (cnc, NULL, cancellable, NULL)) {
417 m365_backend_update_resource (m365_backend, E_SOURCE_EXTENSION_ADDRESS_BOOK,
418 E_M365_ARTIFICIAL_FOLDER_ID_USERS, NULL, _("Organizational Users"), TRUE, NULL);
419 } else {
420 m365_backend_remove_resource (m365_backend, E_SOURCE_EXTENSION_ADDRESS_BOOK, E_M365_ARTIFICIAL_FOLDER_ID_USERS);
421 }
422
423 if (e_m365_connection_get_people_accessible_sync (cnc, NULL, cancellable, NULL)) {
424 m365_backend_update_resource (m365_backend, E_SOURCE_EXTENSION_ADDRESS_BOOK,
425 E_M365_ARTIFICIAL_FOLDER_ID_PEOPLE, NULL, _("Recent Contacts"), TRUE, NULL);
426 } else {
427 m365_backend_remove_resource (m365_backend, E_SOURCE_EXTENSION_ADDRESS_BOOK, E_M365_ARTIFICIAL_FOLDER_ID_PEOPLE);
428 }
429 }
430
431 static void
432 m365_backend_sync_calendar_folders_sync (EM365Backend *m365_backend,
433 EM365Connection *cnc,
434 GCancellable *cancellable)
435 {
436 const gchar *extension_name = E_SOURCE_EXTENSION_CALENDAR;
437 GHashTable *known_ids; /* gchar *id ~> NULL */
438 gboolean success = FALSE;
439 GSList *groups = NULL, *link;
440 GError *error = NULL;
441
442 known_ids = m365_backend_get_known_folder_ids (m365_backend, extension_name, FALSE);
443
444 if (e_m365_connection_list_calendar_groups_sync (cnc, NULL, &groups, cancellable, &error) && groups) {
445 success = TRUE;
446
447 for (link = groups; link && success; link = g_slist_next (link)) {
448 EM365CalendarGroup *group = link->data;
449 GSList *calendars = NULL;
450
451 if (!group)
452 continue;
453
454 if (e_m365_connection_list_calendars_sync (cnc, NULL, e_m365_calendar_group_get_id (group), NULL, &calendars, cancellable, &error)) {
455 GSList *clink;
456
457 for (clink = calendars; clink; clink = g_slist_next (clink)) {
458 EM365Calendar *calendar = clink->data;
459 const gchar *color;
460
461 if (!calendar || !e_m365_calendar_get_id (calendar))
462 continue;
463
464 color = e_m365_calendar_get_hex_color (calendar);
465 if (!color || !*color)
466 color = e_m365_calendar_color_to_rgb (e_m365_calendar_get_color (calendar));
467
468 m365_backend_update_resource (m365_backend, extension_name,
469 e_m365_calendar_get_id (calendar),
470 e_m365_calendar_group_get_id (group),
471 e_m365_calendar_get_name (calendar),
472 FALSE,
473 color);
474
475 g_hash_table_remove (known_ids, e_m365_calendar_get_id (calendar));
476 }
477
478 g_slist_free_full (calendars, (GDestroyNotify) json_object_unref);
479 } else {
480 success = FALSE;
481 }
482 }
483
484 g_slist_free_full (groups, (GDestroyNotify) json_object_unref);
485 }
486
487 if (success)
488 m365_backend_forget_folders_hash (m365_backend, extension_name, known_ids);
489
490 g_hash_table_destroy (known_ids);
491 g_clear_error (&error);
492 }
493
494 static void
495 m365_backend_sync_task_folders_sync (EM365Backend *m365_backend,
496 EM365Connection *cnc,
497 GCancellable *cancellable)
498 {
499 const gchar *extension_name = E_SOURCE_EXTENSION_TASK_LIST;
500 GHashTable *known_ids; /* gchar *id ~> NULL */
501 gboolean success = FALSE;
502 GSList *task_lists = NULL, *link;
503 GError *error = NULL;
504
505 known_ids = m365_backend_get_known_folder_ids (m365_backend, extension_name, FALSE);
506
507 if (e_m365_connection_list_task_lists_sync (cnc, NULL, &task_lists, cancellable, &error)) {
508 for (link = task_lists; link; link = g_slist_next (link)) {
509 EM365TaskList *task_list = link->data;
510
511 if (!task_list || !e_m365_task_list_get_id (task_list))
512 continue;
513
514 m365_backend_update_resource (m365_backend, extension_name,
515 e_m365_task_list_get_id (task_list),
516 NULL,
517 e_m365_task_list_get_display_name (task_list),
518 e_m365_task_list_get_kind (task_list) == E_M365_TASK_LIST_KIND_DEFAULT_LIST,
519 NULL);
520
521 g_hash_table_remove (known_ids, e_m365_task_list_get_id (task_list));
522 }
523
524 g_slist_free_full (task_lists, (GDestroyNotify) json_object_unref);
525 }
526
527 if (success)
528 m365_backend_forget_folders_hash (m365_backend, extension_name, known_ids);
529
530 g_hash_table_destroy (known_ids);
531 g_clear_error (&error);
532 }
533
534 static void
535 m365_backend_sync_folders_thread (GTask *task,
536 gpointer source_object,
537 gpointer task_data,
538 GCancellable *cancellable)
539 {
540 EM365Backend *m365_backend = source_object;
541 EM365Connection *cnc = task_data;
542 ESourceCollection *collection_extension = NULL;
543 ESource *source;
544
545 g_return_if_fail (E_IS_M365_BACKEND (m365_backend));
546 g_return_if_fail (E_IS_M365_CONNECTION (cnc));
547
548 source = e_backend_get_source (E_BACKEND (m365_backend));
549 collection_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_COLLECTION);
550
551 if (e_source_collection_get_contacts_enabled (collection_extension)) {
552 m365_backend_sync_contact_folders_sync (m365_backend, cnc, cancellable);
553 }
554
555 if (e_source_collection_get_calendar_enabled (collection_extension)) {
556 m365_backend_sync_calendar_folders_sync (m365_backend, cnc, cancellable);
557 m365_backend_sync_task_folders_sync (m365_backend, cnc, cancellable);
558 }
559
560 e_collection_backend_thaw_populate (E_COLLECTION_BACKEND (m365_backend));
561 }
562
563 static void
564 m365_backend_sync_folders (EM365Backend *m365_backend,
565 EM365Connection *cnc,
566 GCancellable *cancellable,
567 GAsyncReadyCallback callback,
568 gpointer user_data)
569 {
570 GTask *task;
571
572 m365_backend->priv->need_update_folders = FALSE;
573
574 task = g_task_new (m365_backend, cancellable, callback, user_data);
575
576 g_task_set_check_cancellable (task, TRUE);
577 g_task_set_task_data (task, g_object_ref (cnc), g_object_unref);
578 g_task_run_in_thread (task, m365_backend_sync_folders_thread);
579
580 g_object_unref (task);
581 }
582
583 static gchar *
584 m365_backend_dup_resource_id (ECollectionBackend *backend,
585 ESource *child_source)
586 {
587 ESourceM365Folder *extension;
588 const gchar *extension_name;
589
590 extension_name = E_SOURCE_EXTENSION_M365_FOLDER;
591 extension = e_source_get_extension (child_source, extension_name);
592
593 return e_source_m365_folder_dup_id (extension);
594 }
595
596 static void
597 m365_backend_child_added (ECollectionBackend *backend,
598 ESource *child_source)
599 {
600 ESource *collection_source;
601
602 collection_source = e_backend_get_source (E_BACKEND (backend));
603
604 if (e_source_has_extension (child_source, E_SOURCE_EXTENSION_AUTHENTICATION)) {
605 ESourceAuthentication *auth_child_extension;
606 ESourceAuthentication *auth_collection_extension;
607
608 auth_child_extension = e_source_get_extension (child_source, E_SOURCE_EXTENSION_AUTHENTICATION);
609 auth_collection_extension = e_source_get_extension (collection_source, E_SOURCE_EXTENSION_AUTHENTICATION);
610
611 e_binding_bind_property (
612 auth_collection_extension, "host",
613 auth_child_extension, "host",
614 G_BINDING_SYNC_CREATE);
615
616 e_binding_bind_property (
617 auth_collection_extension, "user",
618 auth_child_extension, "user",
619 G_BINDING_SYNC_CREATE);
620
621 e_binding_bind_property (
622 auth_collection_extension, "method",
623 auth_child_extension, "method",
624 G_BINDING_SYNC_CREATE);
625 }
626
627 /* We track M365 folders in a hash table by folder ID. */
628 if (e_source_has_extension (child_source, E_SOURCE_EXTENSION_M365_FOLDER)) {
629 ESourceM365Folder *extension;
630 gchar *folder_id;
631
632 extension = e_source_get_extension (child_source, E_SOURCE_EXTENSION_M365_FOLDER);
633 folder_id = e_source_m365_folder_dup_id (extension);
634
635 if (folder_id) {
636 EM365Backend *m365_backend = E_M365_BACKEND (backend);
637
638 LOCK (m365_backend);
639 g_hash_table_insert (m365_backend->priv->folder_sources, folder_id, g_object_ref (child_source));
640 UNLOCK (m365_backend);
641 }
642 }
643
644 /* Chain up to parent's method. */
645 E_COLLECTION_BACKEND_CLASS (e_m365_backend_parent_class)->child_added (backend, child_source);
646 }
647
648 static void
649 m365_backend_child_removed (ECollectionBackend *backend,
650 ESource *child_source)
651 {
652 /* We track M365 folders in a hash table by folder ID. */
653 if (e_source_has_extension (child_source, E_SOURCE_EXTENSION_M365_FOLDER)) {
654 ESourceM365Folder *extension;
655 const gchar *folder_id;
656
657 extension = e_source_get_extension (child_source, E_SOURCE_EXTENSION_M365_FOLDER);
658 folder_id = e_source_m365_folder_get_id (extension);
659
660 if (folder_id) {
661 EM365Backend *m365_backend = E_M365_BACKEND (backend);
662
663 LOCK (m365_backend);
664 g_hash_table_remove (m365_backend->priv->folder_sources, folder_id);
665 UNLOCK (m365_backend);
666 }
667 }
668
669 /* Chain up to parent's method. */
670 E_COLLECTION_BACKEND_CLASS (e_m365_backend_parent_class)->child_removed (backend, child_source);
671 }
672
673 static gboolean
674 m365_backend_create_resource_sync (ECollectionBackend *backend,
675 ESource *source,
676 GCancellable *cancellable,
677 GError **error)
678 {
679 #if 0
680 EM365Connection *connection = NULL;
681 M365FolderId *out_folder_id = NULL;
682 EM365FolderType folder_type = E_M365_FOLDER_TYPE_UNKNOWN;
683 const gchar *extension_name;
684 const gchar *parent_folder_id = NULL;
685 gchar *folder_name;
686 gboolean success = FALSE;
687
688 extension_name = E_SOURCE_EXTENSION_M365_FOLDER;
689 if (e_source_has_extension (source, extension_name)) {
690 ESourceM365Folder *extension;
691
692 /* foreign and public folders are just added */
693 extension = e_source_get_extension (source, extension_name);
694 if (e_source_m365_folder_get_foreign (extension) ||
695 e_source_m365_folder_get_public (extension))
696 success = TRUE;
697 }
698
699 if (!success) {
700 connection = e_m365_backend_ref_connection_sync (E_M365_BACKEND (backend), NULL, NULL, NULL, cancellable, error);
701 if (connection == NULL)
702 return FALSE;
703
704 extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
705 if (e_source_has_extension (source, extension_name)) {
706 folder_type = E_M365_FOLDER_TYPE_CONTACTS;
707 parent_folder_id = "contacts";
708 }
709
710 extension_name = E_SOURCE_EXTENSION_CALENDAR;
711 if (e_source_has_extension (source, extension_name)) {
712 folder_type = E_M365_FOLDER_TYPE_CALENDAR;
713 parent_folder_id = "calendar";
714 }
715
716 extension_name = E_SOURCE_EXTENSION_TASK_LIST;
717 if (e_source_has_extension (source, extension_name)) {
718 folder_type = E_M365_FOLDER_TYPE_TASKS;
719 parent_folder_id = "tasks";
720 }
721
722 /* FIXME No support for memo lists. */
723
724 if (parent_folder_id == NULL) {
725 g_set_error (
726 error, G_IO_ERROR,
727 G_IO_ERROR_INVALID_ARGUMENT,
728 _("Could not determine a suitable folder "
729 "class for a new folder named “%s”"),
730 e_source_get_display_name (source));
731 goto exit;
732 }
733
734 folder_name = e_source_dup_display_name (source);
735
736 success = e_m365_connection_create_folder_sync (
737 connection, M365_PRIORITY_MEDIUM,
738 parent_folder_id, TRUE,
739 folder_name, folder_type,
740 &out_folder_id, cancellable, error);
741
742 g_free (folder_name);
743
744 /* Sanity check */
745 g_warn_if_fail (
746 (success && out_folder_id != NULL) ||
747 (!success && out_folder_id == NULL));
748
749 if (out_folder_id != NULL) {
750 ESourceM365Folder *extension;
751 const gchar *extension_name;
752
753 extension_name = E_SOURCE_EXTENSION_M365_FOLDER;
754 extension = e_source_get_extension (source, extension_name);
755 e_source_m365_folder_set_id (
756 extension, out_folder_id->id);
757 e_source_m365_folder_set_change_key (
758 extension, out_folder_id->change_key);
759
760 e_m365_folder_id_free (out_folder_id);
761 }
762 }
763
764 if (success) {
765 ESourceRegistryServer *server;
766 ESource *parent_source;
767 const gchar *cache_dir;
768 const gchar *parent_uid;
769
770 /* Configure the source as a collection member. */
771 parent_source = e_backend_get_source (E_BACKEND (backend));
772 parent_uid = e_source_get_uid (parent_source);
773 e_source_set_parent (source, parent_uid);
774
775 /* Changes should be written back to the cache directory. */
776 cache_dir = e_collection_backend_get_cache_dir (backend);
777 e_server_side_source_set_write_directory (
778 E_SERVER_SIDE_SOURCE (source), cache_dir);
779
780 /* Set permissions for clients. */
781 e_server_side_source_set_writable (E_SERVER_SIDE_SOURCE (source), TRUE);
782 e_server_side_source_set_remote_deletable (E_SERVER_SIDE_SOURCE (source), TRUE);
783
784 server = e_collection_backend_ref_server (backend);
785 e_source_registry_server_add_source (server, source);
786 g_object_unref (server);
787 }
788
789 exit:
790 if (connection)
791 g_object_unref (connection);
792
793 return success;
794 #endif
795 return FALSE;
796 }
797
798 static gboolean
799 m365_backend_delete_resource_sync (ECollectionBackend *backend,
800 ESource *source,
801 GCancellable *cancellable,
802 GError **error)
803 {
804 #if 0
805 EM365Connection *connection;
806 ESourceM365Folder *extension;
807 const gchar *extension_name;
808 gboolean success = FALSE;
809
810 connection = e_m365_backend_ref_connection_sync (E_M365_BACKEND (backend), NULL, NULL, NULL, cancellable, error);
811 if (connection == NULL)
812 return FALSE;
813
814 extension_name = E_SOURCE_EXTENSION_M365_FOLDER;
815 if (!e_source_has_extension (source, extension_name)) {
816 g_set_error (
817 error, G_IO_ERROR,
818 G_IO_ERROR_INVALID_ARGUMENT,
819 _("Data source “%s” does not represent a Microsoft 365 folder"),
820 e_source_get_display_name (source));
821 goto exit;
822 }
823 extension = e_source_get_extension (source, extension_name);
824
825 if (e_source_m365_folder_get_foreign (extension) ||
826 e_source_m365_folder_get_public (extension)) {
827 /* do not delete foreign or public folders,
828 * just remove them from local store */
829 success = TRUE;
830 } else {
831 gchar *folder_id;
832
833 folder_id = e_source_m365_folder_dup_id (extension);
834
835 success = e_m365_connection_delete_folder_sync (
836 connection, M365_PRIORITY_MEDIUM, folder_id,
837 FALSE, "HardDelete", cancellable, error);
838
839 g_free (folder_id);
840 }
841
842 if (success)
843 success = e_source_remove_sync (source, cancellable, error);
844
845 exit:
846 g_object_unref (connection);
847
848 return success;
849 #endif
850 return FALSE;
851 }
852
853 static gboolean
854 m365_backend_get_destination_address (EBackend *backend,
855 gchar **host,
856 guint16 *port)
857 {
858 g_return_val_if_fail (port != NULL, FALSE);
859 g_return_val_if_fail (host != NULL, FALSE);
860
861 *host = g_strdup ("graph.microsoft.com");
862 *port = 443;
863
864 return TRUE;
865 }
866
867 static ESourceAuthenticationResult
868 m365_backend_authenticate_sync (EBackend *backend,
869 const ENamedParameters *credentials,
870 gchar **out_certificate_pem,
871 GTlsCertificateFlags *out_certificate_errors,
872 GCancellable *cancellable,
873 GError **error)
874 {
875 CamelM365Settings *m365_settings;
876 EM365Connection *cnc;
877 ESourceAuthenticationResult result;
878 gboolean in_sync_folders = FALSE;
879
880 g_return_val_if_fail (E_IS_M365_BACKEND (backend), E_SOURCE_AUTHENTICATION_ERROR);
881
882 e_collection_backend_freeze_populate (E_COLLECTION_BACKEND (backend));
883
884 m365_settings = camel_m365_settings_get_from_backend (backend, NULL);
885 g_return_val_if_fail (m365_settings != NULL, E_SOURCE_AUTHENTICATION_ERROR);
886
887 cnc = e_m365_connection_new (e_backend_get_source (backend), m365_settings);
888
889 result = e_m365_connection_authenticate_sync (cnc, NULL, E_M365_FOLDER_KIND_UNKNOWN, NULL, NULL, out_certificate_pem, out_certificate_errors, cancellable, error);
890
891 if (result == E_SOURCE_AUTHENTICATION_ACCEPTED) {
892 e_collection_backend_authenticate_children (E_COLLECTION_BACKEND (backend), credentials);
893 m365_backend_sync_folders (E_M365_BACKEND (backend), cnc, NULL, NULL, NULL);
894
895 in_sync_folders = TRUE;
896 } else if (result == E_SOURCE_AUTHENTICATION_REJECTED &&
897 !e_named_parameters_exists (credentials, E_SOURCE_CREDENTIAL_PASSWORD)) {
898 result = E_SOURCE_AUTHENTICATION_REQUIRED;
899 }
900
901 g_clear_object (&cnc);
902
903 if (!in_sync_folders)
904 e_collection_backend_thaw_populate (E_COLLECTION_BACKEND (backend));
905
906 return result;
907 }
908
909 static void
910 m365_backend_constructed (GObject *object)
911 {
912 EBackend *backend;
913 ESource *source;
914
915 /* Chain up to parent's method. */
916 G_OBJECT_CLASS (e_m365_backend_parent_class)->constructed (object);
917
918 backend = E_BACKEND (object);
919 source = e_backend_get_source (backend);
920
921 e_server_side_source_set_remote_creatable (E_SERVER_SIDE_SOURCE (source), TRUE);
922
923 /* Reset the connectable, it steals data from Authentication extension,
924 where is written incorrect address */
925 e_backend_set_connectable (backend, NULL);
926
927 if (e_source_has_extension (source, E_SOURCE_EXTENSION_COLLECTION)) {
928 ESourceCollection *collection_extension;
929
930 collection_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_COLLECTION);
931 e_source_collection_set_allow_sources_rename (collection_extension, TRUE);
932 }
933 }
934
935 static void
936 m365_backend_dispose (GObject *object)
937 {
938 EM365Backend *m365_backend = E_M365_BACKEND (object);
939 ESource *source;
940
941 source = e_backend_get_source (E_BACKEND (object));
942 if (source && m365_backend->priv->source_changed_id) {
943 g_signal_handler_disconnect (source, m365_backend->priv->source_changed_id);
944 m365_backend->priv->source_changed_id = 0;
945 }
946
947 /* Chain up to parent's method. */
948 G_OBJECT_CLASS (e_m365_backend_parent_class)->dispose (object);
949 }
950
951 static void
952 m365_backend_finalize (GObject *object)
953 {
954 EM365Backend *m365_backend = E_M365_BACKEND (object);
955
956 g_hash_table_destroy (m365_backend->priv->folder_sources);
957 g_mutex_clear (&m365_backend->priv->property_lock);
958
959 /* Chain up to parent's method. */
960 G_OBJECT_CLASS (e_m365_backend_parent_class)->finalize (object);
961 }
962
963 static void
964 e_m365_backend_class_init (EM365BackendClass *class)
965 {
966 GObjectClass *object_class;
967 EBackendClass *backend_class;
968 ECollectionBackendClass *collection_backend_class;
969
970 object_class = G_OBJECT_CLASS (class);
971 object_class->dispose = m365_backend_dispose;
972 object_class->finalize = m365_backend_finalize;
973 object_class->constructed = m365_backend_constructed;
974
975 collection_backend_class = E_COLLECTION_BACKEND_CLASS (class);
976 collection_backend_class->populate = m365_backend_populate;
977 collection_backend_class->dup_resource_id = m365_backend_dup_resource_id;
978 collection_backend_class->child_added = m365_backend_child_added;
979 collection_backend_class->child_removed = m365_backend_child_removed;
980 collection_backend_class->create_resource_sync = m365_backend_create_resource_sync;
981 collection_backend_class->delete_resource_sync = m365_backend_delete_resource_sync;
982
983 backend_class = E_BACKEND_CLASS (class);
984 backend_class->get_destination_address = m365_backend_get_destination_address;
985 backend_class->authenticate_sync = m365_backend_authenticate_sync;
986
987 /* This generates an ESourceCamel subtype for CamelM365Settings. */
988 e_source_camel_generate_subtype ("microsoft365", CAMEL_TYPE_M365_SETTINGS);
989 }
990
991 static void
992 e_m365_backend_class_finalize (EM365BackendClass *class)
993 {
994 }
995
996 static void
997 e_m365_backend_init (EM365Backend *backend)
998 {
999 backend->priv = e_m365_backend_get_instance_private (backend);
1000 backend->priv->folder_sources = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
1001
1002 g_mutex_init (&backend->priv->property_lock);
1003 }
1004
1005 void
1006 e_m365_backend_type_register (GTypeModule *type_module)
1007 {
1008 /* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
1009 * function, so we have to wrap it with a public function in
1010 * order to register types from a separate compilation unit. */
1011 e_m365_backend_register_type (type_module);
1012 }