Debian Codegraph @

src/Microsoft365/addressbook/e-book-backend-m365.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 <string.h>
9 #include <time.h>
10
11 #include <glib.h>
12 #include <glib/gi18n-lib.h>
13
14 #include <libedata-book/libedata-book.h>
15
16 #include "common/camel-m365-settings.h"
17 #include "common/e-m365-connection.h"
18 #include "common/e-source-m365-folder.h"
19
20 #include "e-book-backend-m365.h"
21
22 #ifdef G_OS_WIN32
23 #ifdef gmtime_r
24 #undef gmtime_r
25 #endif
26
27 /* The gmtime() in Microsoft's C library is MT-safe */
28 #define gmtime_r(tp,tmp) (gmtime(tp)?(*(tmp)=*gmtime(tp),(tmp)):0)
29 #endif
30
31 #define EC_ERROR_EX(_code,_msg) e_client_error_create (_code, _msg)
32 #define EBC_ERROR_EX(_code,_msg) e_book_client_error_create (_code, _msg)
33
34 #define LOCK(_bb) g_rec_mutex_lock (&_bb->priv->property_lock)
35 #define UNLOCK(_bb) g_rec_mutex_unlock (&_bb->priv->property_lock)
36
37 struct _EBookBackendM365Private {
38 GRecMutex property_lock;
39 EM365Connection *cnc;
40 gchar *folder_id;
41 GHashTable *view_cancellables; /* gpointer view ~> GCancellable; shares property_lock */
42 EM365FolderKind folder_kind;
43 gboolean cached_for_offline;
44 guint max_people;
45 };
46
47 G_DEFINE_TYPE_WITH_PRIVATE (EBookBackendM365, e_book_backend_m365, E_TYPE_BOOK_META_BACKEND)
48
49 static void ebb_m365_check_source_properties (EBookBackendM365 *bbm365);
50
51 static void
52 ebb_m365_contact_get_string_attribute (EM365Contact *m365_contact,
53 EContact *inout_contact,
54 EContactField field_id,
55 const gchar * (*m365_get_func) (EM365Contact *contact))
56 {
57 e_contact_set (inout_contact, field_id, m365_get_func (m365_contact));
58 }
59
60 static void
61 ebb_m365_contact_add_string_attribute (EContact *new_contact,
62 EContact *old_contact,
63 EContactField field_id,
64 JsonBuilder *builder,
65 void (* m365_add_func) (JsonBuilder *builder,
66 const gchar *value))
67 {
68 const gchar *new_value, *old_value;
69
70 g_return_if_fail (m365_add_func != NULL);
71
72 new_value = e_contact_get_const (new_contact, field_id);
73 old_value = old_contact ? e_contact_get_const (old_contact, field_id) : NULL;
74
75 if (g_strcmp0 (new_value, old_value) != 0)
76 m365_add_func (builder, new_value);
77 }
78
79 static gboolean
80 ebb_m365_contact_get_rev (EBookBackendM365 *bbm365,
81 EM365Contact *m365_contact,
82 EContact *inout_contact,
83 EContactField field_id,
84 EM365Connection *cnc,
85 GCancellable *cancellable,
86 GError **error)
87 {
88 gchar time_string[100] = { 0 };
89 struct tm stm;
90 time_t value;
91
92 value = e_m365_contact_get_last_modified_date_time (m365_contact);
93
94 if (value <= (time_t) 0)
95 value = time (NULL);
96
97 gmtime_r (&value, &stm);
98 strftime (time_string, 100, "%Y-%m-%dT%H:%M:%SZ", &stm);
99
100 e_contact_set (inout_contact, field_id, time_string);
101
102 return TRUE;
103 }
104
105 static gboolean
106 ebb_m365_contact_get_birthday (EBookBackendM365 *bbm365,
107 EM365Contact *m365_contact,
108 EContact *inout_contact,
109 EContactField field_id,
110 EM365Connection *cnc,
111 GCancellable *cancellable,
112 GError **error)
113 {
114 time_t value;
115 gboolean exists = FALSE;
116
117 value = e_m365_contact_get_birthday (m365_contact, &exists);
118
119 if (exists) {
120 EContactDate dt;
121 struct tm stm;
122
123 gmtime_r (&value, &stm);
124
125 dt.year = stm.tm_year + 1900;
126 dt.month = stm.tm_mon + 1;
127 dt.day = stm.tm_mday;
128
129 e_contact_set (inout_contact, field_id, &dt);
130 }
131
132 return TRUE;
133 }
134
135 static gboolean
136 ebb_m365_contact_add_birthday (EBookBackendM365 *bbm365,
137 EContact *new_contact,
138 EContact *old_contact,
139 EContactField field_id,
140 const gchar *m365_id,
141 JsonBuilder *builder,
142 GCancellable *cancellable,
143 GError **error)
144 {
145 EContactDate *old_dt = NULL;
146 EContactDate *new_dt = NULL;
147
148 new_dt = e_contact_get (new_contact, field_id);
149 old_dt = old_contact ? e_contact_get (old_contact, field_id) : NULL;
150
151 if (!e_contact_date_equal (new_dt, old_dt)) {
152 if (new_dt) {
153 GDateTime *gdt;
154 time_t value = (time_t) 0;
155
156 gdt = g_date_time_new_local (new_dt->year, new_dt->month, new_dt->day, 11, 59, 0.0);
157
158 if (gdt) {
159 value = g_date_time_to_unix (gdt);
160 value = value - (value % (24 * 60 * 60));
161 value = value + (((12 * 60) - 1) * 60);
162
163 g_date_time_unref (gdt);
164 }
165
166 e_m365_contact_add_birthday (builder, value);
167 } else {
168 e_m365_contact_add_birthday (builder, (time_t) 0);
169 }
170 }
171
172 e_contact_date_free (new_dt);
173 e_contact_date_free (old_dt);
174
175 return TRUE;
176 }
177
178 static gboolean
179 ebb_m365_contact_get_home_page (EBookBackendM365 *bbm365,
180 EM365Contact *m365_contact,
181 EContact *inout_contact,
182 EContactField field_id,
183 EM365Connection *cnc,
184 GCancellable *cancellable,
185 GError **error)
186 {
187 if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_CONTACTS)
188 ebb_m365_contact_get_string_attribute (m365_contact, inout_contact, field_id, e_m365_contact_get_business_home_page);
189 else if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_USERS)
190 ebb_m365_contact_get_string_attribute (m365_contact, inout_contact, field_id, e_m365_contact_user_get_my_site);
191
192 return TRUE;
193 }
194
195 static gboolean
196 ebb_m365_contact_add_home_page (EBookBackendM365 *bbm365,
197 EContact *new_contact,
198 EContact *old_contact,
199 EContactField field_id,
200 const gchar *m365_id,
201 JsonBuilder *builder,
202 GCancellable *cancellable,
203 GError **error)
204 {
205 if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_CONTACTS)
206 ebb_m365_contact_add_string_attribute (new_contact, old_contact, field_id, builder, e_m365_contact_add_business_home_page);
207
208 return TRUE;
209 }
210
211 static gboolean
212 ebb_m365_contact_get_address (EBookBackendM365 *bbm365,
213 EM365Contact *m365_contact,
214 EContact *inout_contact,
215 EContactField field_id,
216 EM365Connection *cnc,
217 GCancellable *cancellable,
218 GError **error)
219 {
220 EContactAddress addr;
221
222 memset (&addr, 0, sizeof (EContactAddress));
223
224 if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_CONTACTS) {
225 EM365PhysicalAddress *phys_address = NULL;
226
227 if (field_id == E_CONTACT_ADDRESS_WORK)
228 phys_address = e_m365_contact_get_business_address (m365_contact);
229 else if (field_id == E_CONTACT_ADDRESS_HOME)
230 phys_address = e_m365_contact_get_home_address (m365_contact);
231 else if (field_id == E_CONTACT_ADDRESS_OTHER)
232 phys_address = e_m365_contact_get_other_address (m365_contact);
233 else
234 g_warning ("%s: Uncaught field '%s'", G_STRFUNC, e_contact_vcard_attribute (field_id));
235
236 if (phys_address) {
237 addr.locality = (gchar *) e_m365_physical_address_get_city (phys_address);
238 addr.country = (gchar *) e_m365_physical_address_get_country_or_region (phys_address);
239 addr.code = (gchar *) e_m365_physical_address_get_postal_code (phys_address);
240 addr.region = (gchar *) e_m365_physical_address_get_state (phys_address);
241 addr.street = (gchar *) e_m365_physical_address_get_street (phys_address);
242 }
243 } else if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_USERS && field_id == E_CONTACT_ADDRESS_WORK) {
244 addr.locality = (gchar *) e_m365_contact_user_get_city (m365_contact);
245 addr.country = (gchar *) e_m365_contact_user_get_country (m365_contact);
246 addr.code = (gchar *) e_m365_contact_user_get_postal_code (m365_contact);
247 addr.region = (gchar *) e_m365_contact_user_get_state (m365_contact);
248 addr.street = (gchar *) e_m365_contact_user_get_street_address (m365_contact);
249 } else if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_ORG_CONTACTS && field_id == E_CONTACT_ADDRESS_WORK) {
250 JsonArray *values = e_m365_contact_org_get_addresses (m365_contact);
251
252 if (values) {
253 guint ii, len;
254
255 len = json_array_get_length (values);
256
257 for (ii = 0; ii < len; ii++) {
258 EM365PhysicalAddress *phys_address = json_array_get_object_element (values, ii);
259
260 if (phys_address) {
261 addr.locality = (gchar *) e_m365_physical_address_get_city (phys_address);
262 addr.country = (gchar *) e_m365_physical_address_get_country_or_region (phys_address);
263 addr.code = (gchar *) e_m365_physical_address_get_postal_code (phys_address);
264 addr.region = (gchar *) e_m365_physical_address_get_state (phys_address);
265 addr.street = (gchar *) e_m365_physical_address_get_street (phys_address);
266 /* only one supported */
267 break;
268 }
269 }
270 }
271 } else if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_PEOPLE) {
272 JsonArray *values = e_m365_contact_person_get_postal_addresses (m365_contact);
273
274 if (values) {
275 guint ii, len;
276
277 len = json_array_get_length (values);
278
279 for (ii = 0; ii < len; ii++) {
280 EM365Location *location = json_array_get_object_element (values, ii);
281 EM365PhysicalAddress *phys_address;
282
283 if (!location)
284 continue;
285
286 phys_address = e_m365_location_get_address (location);
287 if (!phys_address)
288 continue;
289
290 if (field_id == E_CONTACT_ADDRESS_WORK) {
291 if (e_m365_location_get_type (location) != E_M365_LOCATION_BUSINESS_ADDRESS)
292 continue;
293 } else if (field_id == E_CONTACT_ADDRESS_HOME) {
294 if (e_m365_location_get_type (location) != E_M365_LOCATION_HOME_ADDRESS)
295 continue;
296 } else if (field_id == E_CONTACT_ADDRESS_OTHER) {
297 if (e_m365_location_get_type (location) != E_M365_LOCATION_STREET_ADDRESS &&
298 e_m365_location_get_type (location) != E_M365_LOCATION_POSTAL_ADDRESS)
299 continue;
300 }
301
302 addr.locality = (gchar *) e_m365_physical_address_get_city (phys_address);
303 addr.country = (gchar *) e_m365_physical_address_get_country_or_region (phys_address);
304 addr.code = (gchar *) e_m365_physical_address_get_postal_code (phys_address);
305 addr.region = (gchar *) e_m365_physical_address_get_state (phys_address);
306 addr.street = (gchar *) e_m365_physical_address_get_street (phys_address);
307 /* only one supported */
308 break;
309 }
310 }
311 }
312
313 if (addr.locality || addr.country || addr.code || addr.region || addr.street)
314 e_contact_set (inout_contact, field_id, &addr);
315 else
316 e_contact_set (inout_contact, field_id, NULL);
317
318 return TRUE;
319 }
320
321 static gboolean
322 ebb_m365_contact_address_equal (const EContactAddress *addr1,
323 const EContactAddress *addr2)
324 {
325 if (!addr1 && !addr2)
326 return TRUE;
327
328 if ((addr1 && !addr2) || (!addr1 && addr2))
329 return FALSE;
330
331 return /* g_strcmp0 (addr1->address_format, addr2->address_format) == 0 && */
332 g_strcmp0 (addr1->po, addr2->po) == 0 &&
333 g_strcmp0 (addr1->ext, addr2->ext) == 0 &&
334 g_strcmp0 (addr1->street, addr2->street) == 0 &&
335 g_strcmp0 (addr1->locality, addr2->locality) == 0 &&
336 g_strcmp0 (addr1->region, addr2->region) == 0 &&
337 g_strcmp0 (addr1->code, addr2->code) == 0 &&
338 g_strcmp0 (addr1->country, addr2->country) == 0;
339 }
340
341 static gboolean
342 ebb_m365_contact_add_address (EBookBackendM365 *bbm365,
343 EContact *new_contact,
344 EContact *old_contact,
345 EContactField field_id,
346 const gchar *m365_id,
347 JsonBuilder *builder,
348 GCancellable *cancellable,
349 GError **error)
350 {
351 EContactAddress *new_addr, *old_addr;
352
353 new_addr = e_contact_get (new_contact, field_id);
354 old_addr = old_contact ? e_contact_get (old_contact, field_id) : NULL;
355
356 if (!ebb_m365_contact_address_equal (new_addr, old_addr)) {
357 void (* add_func) (JsonBuilder *builder,
358 const gchar *city,
359 const gchar *country_or_region,
360 const gchar *postal_code,
361 const gchar *state,
362 const gchar *street) = NULL;
363
364 if (field_id == E_CONTACT_ADDRESS_WORK)
365 add_func = e_m365_contact_add_business_address;
366 else if (field_id == E_CONTACT_ADDRESS_HOME)
367 add_func = e_m365_contact_add_home_address;
368 else if (field_id == E_CONTACT_ADDRESS_OTHER)
369 add_func = e_m365_contact_add_other_address;
370 else
371 g_warning ("%s: Uncaught field '%s'", G_STRFUNC, e_contact_vcard_attribute (field_id));
372
373 if (add_func) {
374 if (new_addr) {
375 add_func (builder, new_addr->locality, new_addr->country, new_addr->code, new_addr->region, new_addr->street);
376 } else {
377 add_func (builder, NULL, NULL, NULL, NULL, NULL);
378 }
379 }
380 }
381
382 e_contact_address_free (new_addr);
383 e_contact_address_free (old_addr);
384
385 return TRUE;
386 }
387
388 static gboolean
389 ebb_m365_contact_get_nick_name (EBookBackendM365 *bbm365,
390 EM365Contact *m365_contact,
391 EContact *inout_contact,
392 EContactField field_id,
393 EM365Connection *cnc,
394 GCancellable *cancellable,
395 GError **error)
396 {
397 if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_CONTACTS)
398 ebb_m365_contact_get_string_attribute (m365_contact, inout_contact, field_id, e_m365_contact_get_nick_name);
399 else if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_ORG_CONTACTS)
400 ebb_m365_contact_get_string_attribute (m365_contact, inout_contact, field_id, e_m365_contact_org_get_mail_nickname);
401 else if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_USERS)
402 ebb_m365_contact_get_string_attribute (m365_contact, inout_contact, field_id, e_m365_contact_user_get_mail_nickname);
403
404 return TRUE;
405 }
406
407 static gboolean
408 ebb_m365_contact_add_nick_name (EBookBackendM365 *bbm365,
409 EContact *new_contact,
410 EContact *old_contact,
411 EContactField field_id,
412 const gchar *m365_id,
413 JsonBuilder *builder,
414 GCancellable *cancellable,
415 GError **error)
416 {
417 if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_CONTACTS)
418 ebb_m365_contact_add_string_attribute (new_contact, old_contact, field_id, builder, e_m365_contact_add_nick_name);
419
420 return TRUE;
421 }
422
423 static gboolean
424 ebb_m365_contact_get_note (EBookBackendM365 *bbm365,
425 EM365Contact *m365_contact,
426 EContact *inout_contact,
427 EContactField field_id,
428 EM365Connection *cnc,
429 GCancellable *cancellable,
430 GError **error)
431 {
432 if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_CONTACTS)
433 ebb_m365_contact_get_string_attribute (m365_contact, inout_contact, field_id, e_m365_contact_get_personal_notes);
434 else if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_USERS)
435 ebb_m365_contact_get_string_attribute (m365_contact, inout_contact, field_id, e_m365_contact_user_get_about_me);
436
437 return TRUE;
438 }
439
440 static gboolean
441 ebb_m365_contact_add_note (EBookBackendM365 *bbm365,
442 EContact *new_contact,
443 EContact *old_contact,
444 EContactField field_id,
445 const gchar *m365_id,
446 JsonBuilder *builder,
447 GCancellable *cancellable,
448 GError **error)
449 {
450 if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_CONTACTS)
451 ebb_m365_contact_add_string_attribute (new_contact, old_contact, field_id, builder, e_m365_contact_add_personal_notes);
452
453 return TRUE;
454 }
455
456 static gboolean
457 ebb_m365_string_values_equal (GSList *new_values, /* const gchar * */
458 GSList *old_values) /* const gchar * */
459 {
460 GHashTable *values;
461 GSList *link;
462 gboolean equal = TRUE;
463
464 if (g_slist_length (new_values) != g_slist_length (old_values))
465 return FALSE;
466
467 values = g_hash_table_new (g_str_hash, g_str_equal);
468
469 for (link = new_values; link; link = g_slist_next (link)) {
470 gchar *value = link->data;
471
472 if (value)
473 g_hash_table_add (values, value);
474 }
475
476 for (link = old_values; link && equal; link = g_slist_next (link)) {
477 const gchar *value = link->data;
478
479 if (value)
480 equal = g_hash_table_remove (values, value);
481 }
482
483 equal = equal && !g_hash_table_size (values);
484
485 g_hash_table_destroy (values);
486
487 return equal;
488 }
489
490 static gboolean
491 ebb_m365_string_list_values_equal (GList *new_values, /* const gchar * */
492 GList *old_values) /* const gchar * */
493 {
494 GHashTable *values;
495 GList *link;
496 gboolean equal = TRUE;
497
498 if (g_list_length (new_values) != g_list_length (old_values))
499 return FALSE;
500
501 values = g_hash_table_new (g_str_hash, g_str_equal);
502
503 for (link = new_values; link; link = g_list_next (link)) {
504 gchar *value = link->data;
505
506 if (value)
507 g_hash_table_add (values, value);
508 }
509
510 for (link = old_values; link && equal; link = g_list_next (link)) {
511 const gchar *value = link->data;
512
513 if (value)
514 equal = g_hash_table_remove (values, value);
515 }
516
517 equal = equal && !g_hash_table_size (values);
518
519 g_hash_table_destroy (values);
520
521 return equal;
522 }
523
524 static gboolean
525 ebb_m365_contact_get_phone (EBookBackendM365 *bbm365,
526 EM365Contact *m365_contact,
527 EContact *inout_contact,
528 EContactField field_id,
529 EM365Connection *cnc,
530 GCancellable *cancellable,
531 GError **error)
532 {
533 if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_CONTACTS ||
534 (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_USERS && field_id == E_CONTACT_PHONE_BUSINESS)) {
535 JsonArray *values = NULL;
536 const gchar *type_val = NULL;
537
538 if (field_id == E_CONTACT_PHONE_BUSINESS) {
539 values = e_m365_contact_get_business_phones (m365_contact);
540 type_val = "WORK";
541 } else if (field_id == E_CONTACT_PHONE_HOME) {
542 values = e_m365_contact_get_home_phones (m365_contact);
543 type_val = "HOME";
544 } else {
545 g_warning ("%s: Uncaught field '%s'", G_STRFUNC, e_contact_vcard_attribute (field_id));
546 }
547
548 if (values) {
549 EVCard *vcard = E_VCARD (inout_contact);
550 guint ii, len;
551
552 len = json_array_get_length (values);
553
554 for (ii = 0; ii < len; ii++) {
555 const gchar *str = json_array_get_string_element (values, len - ii - 1);
556
557 if (str && *str) {
558 EVCardAttributeParam *param;
559 EVCardAttribute *attr;
560
561 attr = e_vcard_attribute_new (NULL, EVC_TEL);
562 param = e_vcard_attribute_param_new (EVC_TYPE);
563
564 e_vcard_attribute_add_param_with_value (attr, param, type_val);
565 e_vcard_add_attribute_with_value (vcard, attr, str);
566 }
567 }
568 }
569 } else if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_USERS && field_id == E_CONTACT_PHONE_HOME) {
570 e_contact_set (inout_contact, E_CONTACT_PHONE_HOME_FAX, e_m365_contact_user_get_fax_number (m365_contact));
571 e_contact_set (inout_contact, E_CONTACT_PHONE_MOBILE, e_m365_contact_user_get_mobile_phone (m365_contact));
572 } else if ((bbm365->priv->folder_kind == E_M365_FOLDER_KIND_ORG_CONTACTS || bbm365->priv->folder_kind == E_M365_FOLDER_KIND_PEOPLE) &&
573 field_id == E_CONTACT_PHONE_BUSINESS) {
574 JsonArray *values;
575
576 if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_ORG_CONTACTS)
577 values = e_m365_contact_org_get_phones (m365_contact);
578 else
579 values = e_m365_contact_person_get_phones (m365_contact);
580
581 if (values) {
582 gboolean had_business = FALSE;
583 gboolean had_home = FALSE;
584 guint ii, len;
585
586 len = json_array_get_length (values);
587
588 for (ii = 0; ii < len; ii++) {
589 EM365Phone *phone = json_array_get_object_element (values, len - ii - 1);
590 const gchar *number = phone ? e_m365_phone_get_number (phone) : NULL;
591
592 if (number && *number) {
593 EContactField fld = E_CONTACT_FIELD_LAST;
594
595 switch (e_m365_phone_get_type (phone)) {
596 case E_M365_PHONE_HOME:
597 fld = had_home ? E_CONTACT_PHONE_HOME_2 : E_CONTACT_PHONE_HOME;
598 had_home = TRUE;
599 break;
600 case E_M365_PHONE_BUSINESS:
601 fld = had_business ? E_CONTACT_PHONE_BUSINESS_2 : E_CONTACT_PHONE_BUSINESS;
602 had_business = TRUE;
603 break;
604 case E_M365_PHONE_MOBILE:
605 fld = E_CONTACT_PHONE_MOBILE;
606 break;
607 case E_M365_PHONE_OTHER:
608 fld = E_CONTACT_PHONE_OTHER;
609 break;
610 case E_M365_PHONE_ASSISTANT:
611 fld = E_CONTACT_PHONE_ASSISTANT;
612 break;
613 case E_M365_PHONE_HOMEFAX:
614 fld = E_CONTACT_PHONE_HOME_FAX;
615 break;
616 case E_M365_PHONE_BUSINESSFAX:
617 fld = E_CONTACT_PHONE_BUSINESS_FAX;
618 break;
619 case E_M365_PHONE_OTHERFAX:
620 fld = E_CONTACT_PHONE_OTHER_FAX;
621 break;
622 case E_M365_PHONE_PAGER:
623 fld = E_CONTACT_PHONE_PAGER;
624 break;
625 case E_M365_PHONE_RADIO:
626 fld = E_CONTACT_PHONE_RADIO;
627 break;
628 case E_M365_PHONE_NOT_SET:
629 case E_M365_PHONE_UNKNOWN:
630 break;
631 }
632
633 if (fld != E_CONTACT_FIELD_LAST)
634 e_contact_set (inout_contact, fld, number);
635 }
636 }
637 }
638 }
639
640 return TRUE;
641 }
642
643 static GSList * /* gchar * */
644 ebb_m365_extract_phones (EContact *contact,
645 const gchar *only_type) /* NULL for anything but known types */
646 {
647 GSList *phones = NULL;
648 GList *attrs, *link;
649
650 if (!contact)
651 return NULL;
652
653 attrs = e_vcard_get_attributes (E_VCARD (contact));
654
655 for (link = attrs; link; link = g_list_next (link)) {
656 EVCardAttribute *attr = link->data;
657 gboolean use_it = FALSE;
658
659 if (!attr || !e_vcard_attribute_get_name (attr) ||
660 g_ascii_strcasecmp (e_vcard_attribute_get_name (attr), EVC_TEL) != 0)
661 continue;
662
663 if (only_type) {
664 use_it = e_vcard_attribute_has_type (attr, only_type);
665 } else {
666 use_it = !e_vcard_attribute_has_type (attr, "WORK") &&
667 !e_vcard_attribute_has_type (attr, "CELL");
668 }
669
670 if (use_it)
671 phones = g_slist_prepend (phones, e_vcard_attribute_get_value (attr));
672 }
673
674 return g_slist_reverse (phones);
675 }
676
677 static gboolean
678 ebb_m365_contact_add_phone (EBookBackendM365 *bbm365,
679 EContact *new_contact,
680 EContact *old_contact,
681 EContactField field_id,
682 const gchar *m365_id,
683 JsonBuilder *builder,
684 GCancellable *cancellable,
685 GError **error)
686 {
687 void (* begin_func) (JsonBuilder *builder) = NULL;
688 void (* end_func) (JsonBuilder *builder) = NULL;
689 void (* add_func) (JsonBuilder *builder, const gchar *value) = NULL;
690 const gchar *type_val = NULL;
691 GSList *new_values, *old_values;
692
693 if (field_id == E_CONTACT_PHONE_BUSINESS) {
694 begin_func = e_m365_contact_begin_business_phones;
695 end_func = e_m365_contact_end_business_phones;
696 add_func = e_m365_contact_add_business_phone;
697 type_val = "WORK";
698 } else if (field_id == E_CONTACT_PHONE_HOME) {
699 begin_func = e_m365_contact_begin_home_phones;
700 end_func = e_m365_contact_end_home_phones;
701 add_func = e_m365_contact_add_home_phone;
702 type_val = NULL; /* everything else is treated as "HOME" phone */
703 } else {
704 g_warning ("%s: Uncaught field '%s'", G_STRFUNC, e_contact_vcard_attribute (field_id));
705 }
706
707 new_values = ebb_m365_extract_phones (new_contact, type_val);
708 old_values = ebb_m365_extract_phones (old_contact, type_val);
709
710 if (!ebb_m365_string_values_equal (new_values, old_values)) {
711 GSList *link;
712
713 begin_func (builder);
714
715 for (link = new_values; link; link = g_slist_next (link)) {
716 const gchar *value = link->data;
717
718 add_func (builder, value);
719 }
720
721 end_func (builder);
722 }
723
724 g_slist_free_full (new_values, g_free);
725 g_slist_free_full (old_values, g_free);
726
727 return TRUE;
728 }
729
730 static gboolean
731 ebb_m365_contact_get_categories (EBookBackendM365 *bbm365,
732 EM365Contact *m365_contact,
733 EContact *inout_contact,
734 EContactField field_id,
735 EM365Connection *cnc,
736 GCancellable *cancellable,
737 GError **error)
738 {
739 JsonArray *values;
740
741 values = e_m365_contact_get_categories (m365_contact);
742
743 if (values) {
744 GString *categories_str = NULL;
745 guint ii, len;
746
747 len = json_array_get_length (values);
748
749 for (ii = 0; ii < len; ii++) {
750 const gchar *str = json_array_get_string_element (values, ii);
751
752 if (str && *str) {
753 if (!categories_str) {
754 categories_str = g_string_new (str);
755 } else {
756 g_string_append_c (categories_str, ',');
757 g_string_append (categories_str, str);
758 }
759 }
760 }
761
762 if (categories_str) {
763 e_contact_set (inout_contact, field_id, categories_str->str);
764 g_string_free (categories_str, TRUE);
765 }
766 }
767
768 return TRUE;
769 }
770
771 static GSList *
772 ebb_m365_extract_categories (EContact *contact,
773 EContactField field_id)
774 {
775 GSList *categories = NULL;
776 const gchar *str;
777
778 if (!contact)
779 return NULL;
780
781 str = e_contact_get_const (contact, field_id);
782
783 if (str && *str) {
784 gchar **split_str;
785 gint ii;
786
787 split_str = g_strsplit (str, ",", -1);
788
789 for (ii = 0; split_str && split_str[ii]; ii++) {
790 gchar *item = split_str[ii];
791
792 if (item && *item)
793 categories = g_slist_prepend (categories, item);
794 else
795 g_free (item);
796
797 split_str[ii] = NULL;
798 }
799
800 g_free (split_str);
801 }
802
803 return g_slist_reverse (categories);
804 }
805
806 static gboolean
807 ebb_m365_contact_add_categories (EBookBackendM365 *bbm365,
808 EContact *new_contact,
809 EContact *old_contact,
810 EContactField field_id,
811 const gchar *m365_id,
812 JsonBuilder *builder,
813 GCancellable *cancellable,
814 GError **error)
815 {
816 GSList *new_values, *old_values;
817
818 new_values = ebb_m365_extract_categories (new_contact, field_id);
819 old_values = ebb_m365_extract_categories (old_contact, field_id);
820
821 if (!ebb_m365_string_values_equal (new_values, old_values)) {
822 GSList *link;
823
824 e_m365_contact_begin_categories (builder);
825
826 for (link = new_values; link; link = g_slist_next (link)) {
827 const gchar *value = link->data;
828
829 e_m365_contact_add_category (builder, value);
830 }
831
832 e_m365_contact_end_categories (builder);
833 }
834
835 g_slist_free_full (new_values, g_free);
836 g_slist_free_full (old_values, g_free);
837
838 return TRUE;
839 }
840
841 static gboolean
842 ebb_m365_contact_get_emails (EBookBackendM365 *bbm365,
843 EM365Contact *m365_contact,
844 EContact *inout_contact,
845 EContactField field_id,
846 EM365Connection *cnc,
847 GCancellable *cancellable,
848 GError **error)
849 {
850 if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_CONTACTS) {
851 JsonArray *values;
852
853 values = e_m365_contact_get_email_addresses (m365_contact);
854
855 if (values) {
856 EVCard *vcard = E_VCARD (inout_contact);
857 guint ii, len;
858
859 len = json_array_get_length (values);
860
861 for (ii = 0; ii < len; ii++) {
862 EM365EmailAddress *address = json_array_get_object_element (values, len - ii - 1);
863
864 if (address) {
865 EVCardAttribute *attr;
866
867 attr = e_vcard_attribute_new (NULL, EVC_EMAIL);
868 e_vcard_attribute_add_param_with_value (attr, e_vcard_attribute_param_new (EVC_TYPE), "OTHER");
869
870 /* ignore the address name, it's not meant to be there, but the server sometimes sets it and sends it back */
871 e_vcard_add_attribute_with_value (vcard, attr, e_m365_email_address_get_address (address));
872 }
873 }
874 }
875 } else {
876 GHashTable *known_mails = g_hash_table_new (camel_strcase_hash, camel_strcase_equal);
877 EVCard *vcard = E_VCARD (inout_contact);
878 EVCardAttribute *attr;
879 JsonArray *proxy_addresses = NULL;
880 JsonArray *scored_addresses = NULL;
881 JsonArray *other_mails = NULL;
882 const gchar *mail = NULL;
883 guint ii, len;
884
885 if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_ORG_CONTACTS) {
886 proxy_addresses = e_m365_contact_org_get_proxy_addresses (m365_contact);
887 mail = e_m365_contact_org_get_mail (m365_contact);
888 } else if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_USERS) {
889 proxy_addresses = e_m365_contact_user_get_proxy_addresses (m365_contact);
890 other_mails = e_m365_contact_user_get_other_mails (m365_contact);
891 mail = e_m365_contact_user_get_mail (m365_contact);
892 } else if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_PEOPLE) {
893 scored_addresses = e_m365_contact_person_get_scored_email_addresses (m365_contact);
894 }
895
896 /* add it to the vCard as the last, because "add_attribute" prepends it */
897 if (mail && *mail)
898 g_hash_table_add (known_mails, (gpointer) mail);
899
900 len = proxy_addresses ? json_array_get_length (proxy_addresses) :
901 (scored_addresses ? json_array_get_length (scored_addresses) : 0);
902
903 for (ii = 0; ii < len; ii++) {
904 const gchar *address = NULL;
905
906 if (proxy_addresses) {
907 address = json_array_get_string_element (proxy_addresses, len - ii - 1);
908 } else if (scored_addresses) {
909 EM365ScoredEmailAddress *scored_address;
910
911 scored_address = json_array_get_object_element (scored_addresses, len - ii - 1);
912 if (scored_address)
913 address = e_m365_scored_email_address_get_address (scored_address);
914 }
915
916 if (address && *address) {
917 /* accept only SMTP: addresses and skip any other */
918 if (g_ascii_strncasecmp (address, "smtp:", 5) == 0)
919 address += 5;
920 else if (!scored_addresses)
921 continue;
922
923 if (g_hash_table_add (known_mails, (gpointer) address)) {
924 attr = e_vcard_attribute_new (NULL, EVC_EMAIL);
925 e_vcard_attribute_add_param_with_value (attr, e_vcard_attribute_param_new (EVC_TYPE), "OTHER");
926 e_vcard_add_attribute_with_value (vcard, attr, address);
927 }
928 }
929 }
930
931 len = other_mails ? json_array_get_length (other_mails) : 0;
932
933 for (ii = 0; ii < len; ii++) {
934 const gchar *address = json_array_get_string_element (other_mails, len - ii - 1);
935
936 if (address && *address &&
937 g_hash_table_add (known_mails, (gpointer) address)) {
938 attr = e_vcard_attribute_new (NULL, EVC_EMAIL);
939 e_vcard_attribute_add_param_with_value (attr, e_vcard_attribute_param_new (EVC_TYPE), "OTHER");
940 e_vcard_add_attribute_with_value (vcard, attr, address);
941 }
942 }
943
944 if (mail && *mail) {
945 attr = e_vcard_attribute_new (NULL, EVC_EMAIL);
946 e_vcard_attribute_add_param_with_value (attr, e_vcard_attribute_param_new (EVC_TYPE), "OTHER");
947 e_vcard_add_attribute_with_value (vcard, attr, mail);
948 }
949
950 g_hash_table_unref (known_mails);
951 }
952
953 return TRUE;
954 }
955
956 static gboolean
957 ebb_m365_parse_qp_email (const gchar *string,
958 gchar **email)
959 {
960 struct _camel_header_address *address;
961 gboolean res = FALSE;
962
963 address = camel_header_address_decode (string, "UTF-8");
964
965 if (address) {
966 /* report success only when we have filled both name and email address */
967 if (address->type == CAMEL_HEADER_ADDRESS_NAME && address->name && *address->name && address->v.addr && *address->v.addr) {
968 *email = g_strdup (address->v.addr);
969 res = TRUE;
970 }
971
972 camel_header_address_unref (address);
973 }
974
975 if (!res) {
976 CamelInternetAddress *addr = camel_internet_address_new ();
977 const gchar *const_name = NULL, *const_email = NULL;
978
979 if (camel_address_unformat (CAMEL_ADDRESS (addr), string) == 1 &&
980 camel_internet_address_get (addr, 0, &const_name, &const_email) &&
981 const_name && *const_name && const_email && *const_email) {
982 *email = g_strdup (const_email);
983 res = TRUE;
984 }
985
986 g_clear_object (&addr);
987 }
988
989 return res;
990 }
991
992 static gboolean
993 ebb_m365_contact_add_emails (EBookBackendM365 *bbm365,
994 EContact *new_contact,
995 EContact *old_contact,
996 EContactField field_id,
997 const gchar *m365_id,
998 JsonBuilder *builder,
999 GCancellable *cancellable,
1000 GError **error)
1001 {
1002 GList *new_values, *old_values;
1003
1004 new_values = e_contact_get (new_contact, field_id);
1005 old_values = old_contact ? e_contact_get (old_contact, field_id) : NULL;
1006
1007 if (!ebb_m365_string_list_values_equal (new_values, old_values)) {
1008 GList *link;
1009
1010 e_m365_contact_begin_email_addresses (builder);
1011
1012 for (link = new_values; link; link = g_list_next (link)) {
1013 const gchar *value = link->data;
1014 gchar *address = NULL;
1015
1016 if (ebb_m365_parse_qp_email (value, &address))
1017 e_m365_add_email_address (builder, NULL, NULL, address);
1018 else
1019 e_m365_add_email_address (builder, NULL, NULL, value);
1020
1021 g_free (address);
1022 }
1023
1024 e_m365_contact_end_email_addresses (builder);
1025 }
1026
1027 g_list_free_full (new_values, g_free);
1028 g_list_free_full (old_values, g_free);
1029
1030 return TRUE;
1031 }
1032
1033 static gboolean
1034 ebb_m365_contact_add_file_as (EBookBackendM365 *bbm365,
1035 EContact *new_contact,
1036 EContact *old_contact,
1037 EContactField field_id,
1038 const gchar *m365_id,
1039 JsonBuilder *builder,
1040 GCancellable *cancellable,
1041 GError **error)
1042 {
1043 const gchar *new_value;
1044
1045 ebb_m365_contact_add_string_attribute (new_contact, old_contact, field_id, builder, e_m365_contact_add_file_as);
1046
1047 new_value = e_contact_get_const (new_contact, E_CONTACT_FILE_AS);
1048
1049 /* Set it always, to not be overwritten by server re-calculations on other property changes */
1050 e_m365_contact_add_display_name (builder, new_value);
1051
1052 return TRUE;
1053 }
1054
1055 static gboolean
1056 ebb_m365_contact_get_generation (EBookBackendM365 *bbm365,
1057 EM365Contact *m365_contact,
1058 EContact *inout_contact,
1059 EContactField field_id,
1060 EM365Connection *cnc,
1061 GCancellable *cancellable,
1062 GError **error)
1063 {
1064 const gchar *value;
1065
1066 value = e_m365_contact_get_generation (m365_contact);
1067
1068 if (value && *value) {
1069 EContactName *name = e_contact_get (inout_contact, field_id);
1070 gchar *prev;
1071
1072 if (!name)
1073 name = e_contact_name_new ();
1074
1075 prev = name->suffixes;
1076 name->suffixes = (gchar *) value;
1077
1078 e_contact_set (inout_contact, field_id, name);
1079
1080 name->suffixes = prev;
1081 e_contact_name_free (name);
1082 }
1083
1084 return TRUE;
1085 }
1086
1087 static gboolean
1088 ebb_m365_contact_add_generation (EBookBackendM365 *bbm365,
1089 EContact *new_contact,
1090 EContact *old_contact,
1091 EContactField field_id,
1092 const gchar *m365_id,
1093 JsonBuilder *builder,
1094 GCancellable *cancellable,
1095 GError **error)
1096 {
1097 EContactName *new_value, *old_value;
1098
1099 new_value = e_contact_get (new_contact, field_id);
1100 old_value = old_contact ? e_contact_get (old_contact, field_id) : NULL;
1101
1102 if (!(new_value && old_value && g_strcmp0 (new_value->suffixes, old_value->suffixes) == 0))
1103 e_m365_contact_add_generation (builder, new_value ? new_value->suffixes : NULL);
1104
1105 e_contact_name_free (new_value);
1106 e_contact_name_free (old_value);
1107
1108 return TRUE;
1109 }
1110
1111 static gboolean
1112 ebb_m365_contact_get_im_addresses (EBookBackendM365 *bbm365,
1113 EM365Contact *m365_contact,
1114 EContact *inout_contact,
1115 EContactField field_id,
1116 EM365Connection *cnc,
1117 GCancellable *cancellable,
1118 GError **error)
1119 {
1120 JsonArray *values;
1121
1122 values = e_m365_contact_get_im_addresses (m365_contact);
1123
1124 if (values) {
1125 EVCard *vcard = E_VCARD (inout_contact);
1126 const gchar *field_name = e_contact_vcard_attribute (field_id);
1127 guint ii, len;
1128
1129 len = json_array_get_length (values);
1130
1131 for (ii = 0; ii < len; ii++) {
1132 const gchar *str = json_array_get_string_element (values, len - ii - 1);
1133
1134 if (str && *str) {
1135 EVCardAttribute *attr;
1136
1137 attr = e_vcard_attribute_new (NULL, field_name);
1138
1139 e_vcard_add_attribute_with_value (vcard, attr, str);
1140 }
1141 }
1142 }
1143
1144 return TRUE;
1145 }
1146
1147 static GSList * /* gchar * */
1148 ebb_m365_extract_im_addresses (EContact *contact)
1149 {
1150 GSList *ims = NULL;
1151 GList *attrs, *link;
1152
1153 if (!contact)
1154 return NULL;
1155
1156 attrs = e_vcard_get_attributes (E_VCARD (contact));
1157
1158 for (link = attrs; link; link = g_list_next (link)) {
1159 EVCardAttribute *attr = link->data;
1160 const gchar *name;
1161
1162 if (!attr)
1163 continue;
1164
1165 name = e_vcard_attribute_get_name (attr);
1166
1167 if (!name || (
1168 g_ascii_strcasecmp (name, EVC_X_GOOGLE_TALK) != 0 &&
1169 g_ascii_strcasecmp (name, EVC_X_SKYPE) != 0 &&
1170 g_ascii_strcasecmp (name, EVC_X_GADUGADU) != 0 &&
1171 g_ascii_strcasecmp (name, EVC_X_AIM) != 0 &&
1172 g_ascii_strcasecmp (name, EVC_X_GROUPWISE) != 0 &&
1173 g_ascii_strcasecmp (name, EVC_X_JABBER) != 0 &&
1174 g_ascii_strcasecmp (name, EVC_X_YAHOO) != 0 &&
1175 g_ascii_strcasecmp (name, EVC_X_MSN) != 0 &&
1176 g_ascii_strcasecmp (name, EVC_X_ICQ) != 0))
1177 continue;
1178
1179 ims = g_slist_prepend (ims, e_vcard_attribute_get_value (attr));
1180 }
1181
1182 return g_slist_reverse (ims);
1183 }
1184
1185 static gboolean
1186 ebb_m365_contact_add_im_addresses (EBookBackendM365 *bbm365,
1187 EContact *new_contact,
1188 EContact *old_contact,
1189 EContactField field_id,
1190 const gchar *m365_id,
1191 JsonBuilder *builder,
1192 GCancellable *cancellable,
1193 GError **error)
1194 {
1195 GSList *new_values, *old_values;
1196
1197 new_values = ebb_m365_extract_im_addresses (new_contact);
1198 old_values = ebb_m365_extract_im_addresses (old_contact);
1199
1200 if (!ebb_m365_string_values_equal (new_values, old_values)) {
1201 GSList *link;
1202
1203 e_m365_contact_begin_im_addresses (builder);
1204
1205 for (link = new_values; link; link = g_slist_next (link)) {
1206 const gchar *value = link->data;
1207
1208 if (value && *value)
1209 e_m365_contact_add_im_address (builder, value);
1210 }
1211
1212 e_m365_contact_end_im_addresses (builder);
1213 }
1214
1215 g_slist_free_full (new_values, g_free);
1216 g_slist_free_full (old_values, g_free);
1217
1218 return TRUE;
1219 }
1220
1221 static gboolean
1222 ebb_m365_contact_get_middle_name (EBookBackendM365 *bbm365,
1223 EM365Contact *m365_contact,
1224 EContact *inout_contact,
1225 EContactField field_id,
1226 EM365Connection *cnc,
1227 GCancellable *cancellable,
1228 GError **error)
1229 {
1230 const gchar *value;
1231
1232 value = e_m365_contact_get_middle_name (m365_contact);
1233
1234 if (value && *value) {
1235 EContactName *name = e_contact_get (inout_contact, field_id);
1236 gchar *prev;
1237
1238 if (!name)
1239 name = e_contact_name_new ();
1240
1241 prev = name->additional;
1242 name->additional = (gchar *) value;
1243
1244 e_contact_set (inout_contact, field_id, name);
1245
1246 name->additional = prev;
1247 e_contact_name_free (name);
1248 }
1249
1250 return TRUE;
1251 }
1252
1253 static gboolean
1254 ebb_m365_contact_add_middle_name (EBookBackendM365 *bbm365,
1255 EContact *new_contact,
1256 EContact *old_contact,
1257 EContactField field_id,
1258 const gchar *m365_id,
1259 JsonBuilder *builder,
1260 GCancellable *cancellable,
1261 GError **error)
1262 {
1263 EContactName *new_value, *old_value;
1264
1265 new_value = e_contact_get (new_contact, field_id);
1266 old_value = old_contact ? e_contact_get (old_contact, field_id) : NULL;
1267
1268 if (!(new_value && old_value && g_strcmp0 (new_value->additional, old_value->additional) == 0))
1269 e_m365_contact_add_middle_name (builder, new_value ? new_value->additional : NULL);
1270
1271 e_contact_name_free (new_value);
1272 e_contact_name_free (old_value);
1273
1274 return TRUE;
1275 }
1276
1277 static gboolean
1278 ebb_m365_contact_get_title (EBookBackendM365 *bbm365,
1279 EM365Contact *m365_contact,
1280 EContact *inout_contact,
1281 EContactField field_id,
1282 EM365Connection *cnc,
1283 GCancellable *cancellable,
1284 GError **error)
1285 {
1286 const gchar *value;
1287
1288 value = e_m365_contact_get_title (m365_contact);
1289
1290 if (value && *value) {
1291 EContactName *name = e_contact_get (inout_contact, field_id);
1292 gchar *prev;
1293
1294 if (!name)
1295 name = e_contact_name_new ();
1296
1297 prev = name->prefixes;
1298 name->prefixes = (gchar *) value;
1299
1300 e_contact_set (inout_contact, field_id, name);
1301
1302 name->prefixes = prev;
1303 e_contact_name_free (name);
1304 }
1305
1306 return TRUE;
1307 }
1308
1309 static gboolean
1310 ebb_m365_contact_add_title (EBookBackendM365 *bbm365,
1311 EContact *new_contact,
1312 EContact *old_contact,
1313 EContactField field_id,
1314 const gchar *m365_id,
1315 JsonBuilder *builder,
1316 GCancellable *cancellable,
1317 GError **error)
1318 {
1319 EContactName *new_value, *old_value;
1320
1321 new_value = e_contact_get (new_contact, field_id);
1322 old_value = old_contact ? e_contact_get (old_contact, field_id) : NULL;
1323
1324 if (!(new_value && old_value && g_strcmp0 (new_value->prefixes, old_value->prefixes) == 0))
1325 e_m365_contact_add_title (builder, new_value ? new_value->prefixes : NULL);
1326
1327 e_contact_name_free (new_value);
1328 e_contact_name_free (old_value);
1329
1330 return TRUE;
1331 }
1332
1333 static gboolean
1334 ebb_m365_contact_get_photo (EBookBackendM365 *bbm365,
1335 EM365Contact *m365_contact,
1336 EContact *inout_contact,
1337 EContactField field_id,
1338 EM365Connection *cnc,
1339 GCancellable *cancellable,
1340 GError **error)
1341 {
1342 GByteArray *photo_data = NULL;
1343 GError *local_error = NULL;
1344
1345 LOCK (bbm365);
1346
1347 if (bbm365->priv->folder_id) {
1348 if (e_m365_connection_get_contact_photo_sync (cnc, NULL, bbm365->priv->folder_id,
1349 e_m365_contact_get_id (m365_contact), &photo_data, cancellable, &local_error) &&
1350 photo_data && photo_data->len) {
1351 EContactPhoto *photo;
1352
1353 photo = e_contact_photo_new ();
1354 e_contact_photo_set_inlined (photo, photo_data->data, photo_data->len);
1355 e_contact_photo_set_mime_type (photo, "image/jpeg");
1356 e_contact_set (inout_contact, field_id, photo);
1357 e_contact_photo_free (photo);
1358 }
1359 }
1360
1361 UNLOCK (bbm365);
1362
1363 if (photo_data)
1364 g_byte_array_unref (photo_data);
1365 g_clear_error (&local_error);
1366
1367 /* Even it could fail, ignore it and read as many contacts as possible, rather than stop on the first error */
1368 return TRUE;
1369 }
1370
1371 static gboolean
1372 ebb_m365_contact_photo_equal (EContactPhoto *photo1,
1373 EContactPhoto *photo2)
1374 {
1375 const guchar *data1, *data2;
1376 gsize len1 = 0, len2 = 0;
1377
1378 if (!photo1 && !photo2)
1379 return TRUE;
1380
1381 if ((photo1 && !photo2) || (!photo1 && photo2))
1382 return FALSE;
1383
1384 data1 = e_contact_photo_get_inlined (photo1, &len1);
1385 data2 = e_contact_photo_get_inlined (photo2, &len2);
1386
1387 if (!data1 && !data2)
1388 return TRUE;
1389
1390 return len1 == len2 && data1 && data2 &&
1391 memcmp (data1, data2, len1) == 0;
1392 }
1393
1394 static gboolean
1395 ebb_m365_contact_add_photo (EBookBackendM365 *bbm365,
1396 EContact *new_contact,
1397 EContact *old_contact,
1398 EContactField field_id,
1399 const gchar *m365_id,
1400 JsonBuilder *builder,
1401 GCancellable *cancellable,
1402 GError **error)
1403 {
1404 EContactPhoto *new_value, *old_value;
1405
1406 new_value = e_contact_get (new_contact, field_id);
1407 old_value = old_contact ? e_contact_get (old_contact, field_id) : NULL;
1408
1409 if (!ebb_m365_contact_photo_equal (new_value, old_value)) {
1410 GByteArray *jpeg_photo = NULL, tmp;
1411 GError *local_error = NULL;
1412
1413 if (new_value) {
1414 gsize len = 0;
1415
1416 tmp.data = (guchar *) e_contact_photo_get_inlined (new_value, &len);
1417
1418 if (len && tmp.data) {
1419 tmp.len = len;
1420 jpeg_photo = &tmp;
1421 }
1422 }
1423
1424 LOCK (bbm365);
1425
1426 if (bbm365->priv->folder_id &&
1427 !e_m365_connection_update_contact_photo_sync (bbm365->priv->cnc, NULL, bbm365->priv->folder_id,
1428 m365_id ? m365_id : e_contact_get_const (new_contact, E_CONTACT_UID), jpeg_photo, cancellable, &local_error)) {
1429 if (local_error) {
1430 g_propagate_error (error, local_error);
1431 local_error = NULL;
1432 }
1433 }
1434
1435 UNLOCK (bbm365);
1436
1437 g_clear_error (&local_error);
1438 }
1439
1440 e_contact_photo_free (new_value);
1441 e_contact_photo_free (old_value);
1442
1443 return TRUE;
1444 }
1445
1446 #define STRING_FIELD(fldid, getfn, addfn) { fldid, FALSE, getfn, NULL, addfn, NULL }
1447 #define COMPLEX_FIELD(fldid, getfn, addfn) { fldid, FALSE, NULL, getfn, NULL, addfn }
1448 #define COMPLEX_FIELD_2(fldid, getfn, addfn) { fldid, TRUE, NULL, getfn, NULL, addfn }
1449 #define COMPLEX_ADDFN(fldid, getfn, addfn) { fldid, FALSE, getfn, NULL, NULL, addfn }
1450
1451 struct _mappings {
1452 EContactField field_id;
1453 gboolean add_in_second_go;
1454 const gchar * (* m365_get_func) (EM365Contact *m365_contact);
1455 gboolean (* get_func) (EBookBackendM365 *bbm365,
1456 EM365Contact *m365_contact,
1457 EContact *inout_contact,
1458 EContactField field_id,
1459 EM365Connection *cnc,
1460 GCancellable *cancellable,
1461 GError **error);
1462 void (* m365_add_func) (JsonBuilder *builder,
1463 const gchar *value);
1464 gboolean (* add_func) (EBookBackendM365 *bbm365,
1465 EContact *new_contact,
1466 EContact *old_contact, /* nullable */
1467 EContactField field_id,
1468 const gchar *m365_id,
1469 JsonBuilder *builder,
1470 GCancellable *cancellable,
1471 GError **error);
1472 } mappings[] = {
1473 STRING_FIELD (E_CONTACT_UID, e_m365_contact_get_id, NULL),
1474 COMPLEX_FIELD (E_CONTACT_REV, ebb_m365_contact_get_rev, NULL),
1475 STRING_FIELD (E_CONTACT_ASSISTANT, e_m365_contact_get_assistant_name, e_m365_contact_add_assistant_name),
1476 COMPLEX_FIELD (E_CONTACT_BIRTH_DATE, ebb_m365_contact_get_birthday, ebb_m365_contact_add_birthday),
1477 COMPLEX_FIELD (E_CONTACT_ADDRESS_WORK, ebb_m365_contact_get_address, ebb_m365_contact_add_address),
1478 COMPLEX_FIELD (E_CONTACT_HOMEPAGE_URL, ebb_m365_contact_get_home_page, ebb_m365_contact_add_home_page),
1479 COMPLEX_FIELD (E_CONTACT_PHONE_BUSINESS, ebb_m365_contact_get_phone, ebb_m365_contact_add_phone),
1480 COMPLEX_FIELD (E_CONTACT_CATEGORIES, ebb_m365_contact_get_categories, ebb_m365_contact_add_categories),
1481 STRING_FIELD (E_CONTACT_ORG, e_m365_contact_get_company_name, e_m365_contact_add_company_name),
1482 STRING_FIELD (E_CONTACT_ORG_UNIT, e_m365_contact_get_department, e_m365_contact_add_department),
1483 COMPLEX_FIELD (E_CONTACT_EMAIL, ebb_m365_contact_get_emails, ebb_m365_contact_add_emails),
1484 COMPLEX_FIELD (E_CONTACT_NAME, ebb_m365_contact_get_generation, ebb_m365_contact_add_generation),
1485 STRING_FIELD (E_CONTACT_GIVEN_NAME, e_m365_contact_get_given_name, e_m365_contact_add_given_name),
1486 COMPLEX_FIELD (E_CONTACT_ADDRESS_HOME, ebb_m365_contact_get_address, ebb_m365_contact_add_address),
1487 COMPLEX_FIELD (E_CONTACT_PHONE_HOME, ebb_m365_contact_get_phone, ebb_m365_contact_add_phone),
1488 COMPLEX_FIELD (E_CONTACT_IM_MSN, ebb_m365_contact_get_im_addresses, ebb_m365_contact_add_im_addresses),
1489 /* STRING_FIELD (???, e_m365_contact_get_initials, e_m365_contact_add_initials), */
1490 STRING_FIELD (E_CONTACT_TITLE, e_m365_contact_get_job_title, e_m365_contact_add_job_title),
1491 STRING_FIELD (E_CONTACT_MANAGER, e_m365_contact_get_manager, e_m365_contact_add_manager),
1492 COMPLEX_FIELD (E_CONTACT_NAME, ebb_m365_contact_get_middle_name, ebb_m365_contact_add_middle_name),
1493 STRING_FIELD (E_CONTACT_PHONE_MOBILE, e_m365_contact_get_mobile_phone, e_m365_contact_add_mobile_phone),
1494 COMPLEX_FIELD (E_CONTACT_NICKNAME, ebb_m365_contact_get_nick_name, ebb_m365_contact_add_nick_name),
1495 STRING_FIELD (E_CONTACT_OFFICE, e_m365_contact_get_office_location, e_m365_contact_add_office_location),
1496 COMPLEX_FIELD (E_CONTACT_ADDRESS_OTHER, ebb_m365_contact_get_address, ebb_m365_contact_add_address),
1497 COMPLEX_FIELD (E_CONTACT_NOTE, ebb_m365_contact_get_note, ebb_m365_contact_add_note),
1498 STRING_FIELD (E_CONTACT_ROLE, e_m365_contact_get_profession, e_m365_contact_add_profession),
1499 STRING_FIELD (E_CONTACT_SPOUSE, e_m365_contact_get_spouse_name, e_m365_contact_add_spouse_name),
1500 STRING_FIELD (E_CONTACT_FAMILY_NAME, e_m365_contact_get_surname, e_m365_contact_add_surname),
1501 COMPLEX_FIELD (E_CONTACT_NAME, ebb_m365_contact_get_title, ebb_m365_contact_add_title),
1502 /* STRING_FIELD (???, e_m365_contact_get_yomi_company_name, e_m365_contact_add_yomi_company_name), */
1503 /* STRING_FIELD (???, e_m365_contact_get_yomi_given_name, e_m365_contact_add_yomi_given_name), */
1504 /* STRING_FIELD (???, e_m365_contact_get_yomi_surname, e_m365_contact_add_yomi_surname), */
1505 COMPLEX_ADDFN (E_CONTACT_FILE_AS, e_m365_contact_get_file_as, ebb_m365_contact_add_file_as),
1506 COMPLEX_FIELD_2 (E_CONTACT_PHOTO, ebb_m365_contact_get_photo, ebb_m365_contact_add_photo)
1507 };
1508
1509 static EContact *
1510 ebb_m365_json_contact_to_vcard (EBookBackendM365 *bbm365,
1511 EM365Contact *m365_contact,
1512 EM365Connection *cnc,
1513 gchar **out_object,
1514 GCancellable *cancellable,
1515 GError **error)
1516 {
1517 EContact *contact;
1518 gint ii;
1519 gboolean success = TRUE;
1520
1521 g_return_val_if_fail (m365_contact != NULL, NULL);
1522 g_return_val_if_fail (out_object != NULL, NULL);
1523
1524 *out_object = NULL;
1525
1526 contact = e_contact_new ();
1527
1528 for (ii = 0; success && ii < G_N_ELEMENTS (mappings); ii++) {
1529 if (mappings[ii].m365_get_func) {
1530 ebb_m365_contact_get_string_attribute (m365_contact, contact, mappings[ii].field_id, mappings[ii].m365_get_func);
1531 } else if (mappings[ii].get_func) {
1532 success = mappings[ii].get_func (bbm365, m365_contact, contact, mappings[ii].field_id, cnc, cancellable, error);
1533 }
1534 }
1535
1536 if (success)
1537 *out_object = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_30);
1538 else
1539 g_clear_object (&contact);
1540
1541 return contact;
1542 }
1543
1544 static JsonBuilder *
1545 ebb_m365_contact_to_json_locked (EBookBackendM365 *bbm365,
1546 EContact *new_contact,
1547 EContact *old_contact, /* nullable */
1548 GCancellable *cancellable,
1549 GError **error)
1550 {
1551 JsonBuilder *builder;
1552 gint ii;
1553 gboolean success = TRUE;
1554
1555 g_return_val_if_fail (new_contact != NULL, NULL);
1556
1557 builder = json_builder_new_immutable ();
1558 e_m365_json_begin_object_member (builder, NULL);
1559
1560 for (ii = 0; success && ii < G_N_ELEMENTS (mappings); ii++) {
1561 if (mappings[ii].m365_add_func) {
1562 ebb_m365_contact_add_string_attribute (new_contact, old_contact, mappings[ii].field_id, builder, mappings[ii].m365_add_func);
1563 } else if (!mappings[ii].add_in_second_go && mappings[ii].add_func) {
1564 success = mappings[ii].add_func (bbm365, new_contact, old_contact, mappings[ii].field_id, NULL, builder, cancellable, error);
1565 }
1566 }
1567
1568 e_m365_json_end_object_member (builder);
1569
1570 if (!success)
1571 g_clear_object (&builder);
1572
1573 return builder;
1574 }
1575
1576 static gboolean
1577 ebb_m365_contact_to_json_2nd_go_locked (EBookBackendM365 *bbm365,
1578 EContact *new_contact,
1579 EContact *old_contact, /* nullable */
1580 const gchar *m365_id,
1581 GCancellable *cancellable,
1582 GError **error)
1583 {
1584 gint ii;
1585 gboolean success = TRUE;
1586
1587 g_return_val_if_fail (new_contact != NULL, FALSE);
1588
1589 for (ii = 0; success && ii < G_N_ELEMENTS (mappings); ii++) {
1590 if (mappings[ii].add_in_second_go && mappings[ii].add_func) {
1591 success = mappings[ii].add_func (bbm365, new_contact, old_contact, mappings[ii].field_id, m365_id, NULL, cancellable, error);
1592 }
1593 }
1594
1595 return success;
1596 }
1597
1598 typedef struct {
1599 gboolean is_autocompletion;
1600 gchar *auto_comp_str;
1601 } EM365SExpData;
1602
1603 static ESExpResult *
1604 ebb_m365_func_not (ESExp *f,
1605 gint argc,
1606 ESExpResult **argv,
1607 gpointer data)
1608 {
1609 ESExpResult *r;
1610
1611 if (argc != 1 || argv[0]->type != ESEXP_RES_UNDEFINED) {
1612 e_sexp_fatal_error (f, "parse error");
1613 return NULL;
1614 }
1615
1616 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1617 r->value.boolean = FALSE;
1618
1619 return r;
1620 }
1621
1622 static ESExpResult *
1623 ebb_m365_func_and_or (ESExp *f,
1624 gint argc,
1625 ESExpResult **argv,
1626 gpointer data)
1627 {
1628 ESExpResult *r;
1629
1630 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1631 r->value.boolean = FALSE;
1632
1633 return r;
1634 }
1635
1636 static ESExpResult *
1637 ebb_m365_func_is (struct _ESExp *f,
1638 gint argc,
1639 struct _ESExpResult **argv,
1640 gpointer data)
1641 {
1642 ESExpResult *r;
1643
1644 if (argc != 2
1645 && argv[0]->type != ESEXP_RES_STRING
1646 && argv[1]->type != ESEXP_RES_STRING) {
1647 e_sexp_fatal_error (f, "parse error");
1648 return NULL;
1649 }
1650
1651 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1652 r->value.boolean = FALSE;
1653
1654 return r;
1655 }
1656
1657 static ESExpResult *
1658 ebb_m365_func_endswith (struct _ESExp *f,
1659 gint argc,
1660 struct _ESExpResult **argv,
1661 gpointer data)
1662 {
1663 ESExpResult *r;
1664
1665 if (argc != 2
1666 && argv[0]->type != ESEXP_RES_STRING
1667 && argv[1]->type != ESEXP_RES_STRING) {
1668 e_sexp_fatal_error (f, "parse error");
1669 return NULL;
1670 }
1671
1672 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1673 r->value.boolean = FALSE;
1674
1675 return r;
1676
1677 }
1678
1679 static ESExpResult *
1680 ebb_m365_func_contains (struct _ESExp *f,
1681 gint argc,
1682 struct _ESExpResult **argv,
1683 gpointer data)
1684 {
1685 ESExpResult *r;
1686 EM365SExpData *sdata = data;
1687 const gchar *propname, *str;
1688
1689 if (argc != 2
1690 && argv[0]->type != ESEXP_RES_STRING
1691 && argv[1]->type != ESEXP_RES_STRING) {
1692 e_sexp_fatal_error (f, "parse error");
1693 return NULL;
1694 }
1695
1696 propname = argv[0]->value.string;
1697 str = argv[1]->value.string;
1698
1699 if (!g_ascii_strcasecmp (propname, "full_name") || !g_ascii_strcasecmp (propname, "email") ||
1700 (str && *str && !g_ascii_strcasecmp (propname, "x-evolution-any-field"))) {
1701 if (!sdata->auto_comp_str) {
1702 sdata->auto_comp_str = g_strdup (str);
1703 sdata->is_autocompletion = TRUE;
1704 }
1705 }
1706
1707 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1708 r->value.boolean = FALSE;
1709
1710 return r;
1711
1712 }
1713
1714 /* We are just handling for autocompletion now. */
1715 static ESExpResult *
1716 ebb_m365_func_beginswith (struct _ESExp *f,
1717 gint argc,
1718 struct _ESExpResult **argv,
1719 gpointer data)
1720 {
1721 ESExpResult *r;
1722 const gchar *propname, *str;
1723 EM365SExpData *sdata = data;
1724
1725 if (argc != 2 ||
1726 argv[0]->type != ESEXP_RES_STRING ||
1727 argv[1]->type != ESEXP_RES_STRING) {
1728 e_sexp_fatal_error (f, "parse error");
1729 return NULL;
1730 }
1731
1732 propname = argv[0]->value.string;
1733 str = argv[1]->value.string;
1734
1735 if (!g_ascii_strcasecmp (propname, "full_name") || !g_ascii_strcasecmp (propname, "email") ||
1736 (str && *str && !g_ascii_strcasecmp (propname, "x-evolution-any-field"))) {
1737 if (!sdata->auto_comp_str) {
1738 sdata->auto_comp_str = g_strdup (str);
1739 sdata->is_autocompletion = TRUE;
1740 }
1741 }
1742
1743 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1744 r->value.boolean = FALSE;
1745 return r;
1746 }
1747
1748 static ESExpResult *
1749 ebb_m365_func_exists (struct _ESExp *f,
1750 gint argc,
1751 struct _ESExpResult **argv,
1752 gpointer data)
1753 {
1754 ESExpResult *r;
1755
1756 r = e_sexp_result_new (f, ESEXP_RES_BOOL);
1757 r->value.boolean = FALSE;
1758
1759 return r;
1760 }
1761
1762 static struct {
1763 const gchar *name;
1764 ESExpFunc *func;
1765 guint flags;
1766 } symbols[] = {
1767 { "and", ebb_m365_func_and_or, 0 },
1768 { "or", ebb_m365_func_and_or, 0},
1769 { "not", ebb_m365_func_not, 0 },
1770 { "contains", ebb_m365_func_contains, 0},
1771 { "is", ebb_m365_func_is, 0},
1772 { "beginswith", ebb_m365_func_beginswith, 0},
1773 { "endswith", ebb_m365_func_endswith, 0},
1774 { "exists", ebb_m365_func_exists, 0}
1775 };
1776
1777 static gchar *
1778 ebb_m365_expr_to_search_text (const gchar *query)
1779 {
1780 ESExp *sexp;
1781 gchar *text = NULL;
1782 EM365SExpData sdata;
1783 guint ii;
1784
1785 sexp = e_sexp_new ();
1786 memset (&sdata, 0, sizeof (EM365SExpData));
1787 sdata.is_autocompletion = FALSE;
1788
1789 for (ii = 0; ii < G_N_ELEMENTS (symbols); ii++) {
1790 e_sexp_add_function (
1791 sexp, 0, (gchar *) symbols[ii].name,
1792 symbols[ii].func,
1793 &sdata);
1794 }
1795
1796 e_sexp_input_text (sexp, query, strlen (query));
1797
1798 if (e_sexp_parse (sexp) == -1) {
1799 const gchar *errstr = e_sexp_get_error (sexp);
1800
1801 g_printerr ("%s: Failed to parse query '%s': %s\n", G_STRFUNC, query, errstr ? errstr : "Unknown error");
1802 } else {
1803 ESExpResult *r;
1804
1805 r = e_sexp_eval (sexp);
1806 if (r) {
1807 if (sdata.is_autocompletion && sdata.auto_comp_str && *sdata.auto_comp_str)
1808 text = sdata.auto_comp_str;
1809 else
1810 g_free (sdata.auto_comp_str);
1811 }
1812
1813 e_sexp_result_free (sexp, r);
1814 }
1815
1816 g_object_unref (sexp);
1817
1818 return text;
1819 }
1820
1821 static void
1822 ebb_m365_convert_error_to_client_error (GError **perror)
1823 {
1824 GError *error = NULL;
1825
1826 if (!perror || !*perror || (*perror)->domain == E_CLIENT_ERROR || (*perror)->domain == E_BOOK_CLIENT_ERROR)
1827 return;
1828
1829 /*if ((*perror)->domain == EWS_CONNECTION_ERROR) {
1830 switch ((*perror)->code) {
1831 case EWS_CONNECTION_ERROR_AUTHENTICATION_FAILED:
1832 error = EC_ERROR_EX (E_CLIENT_ERROR_AUTHENTICATION_FAILED, (*perror)->message);
1833 break;
1834 case EWS_CONNECTION_ERROR_FOLDERNOTFOUND:
1835 case EWS_CONNECTION_ERROR_MANAGEDFOLDERNOTFOUND:
1836 case EWS_CONNECTION_ERROR_PARENTFOLDERNOTFOUND:
1837 case EWS_CONNECTION_ERROR_PUBLICFOLDERSERVERNOTFOUND:
1838 error = EBC_ERROR_EX (E_BOOK_CLIENT_ERROR_NO_SUCH_BOOK, (*perror)->message);
1839 break;
1840 case EWS_CONNECTION_ERROR_EVENTNOTFOUND:
1841 case EWS_CONNECTION_ERROR_ITEMNOTFOUND:
1842 error = EBC_ERROR_EX (E_BOOK_CLIENT_ERROR_CONTACT_NOT_FOUND, (*perror)->message);
1843 break;
1844 case EWS_CONNECTION_ERROR_UNAVAILABLE:
1845 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_HOST_NOT_FOUND, (*perror)->message);
1846 break;
1847 }
1848
1849 if (!error)
1850 error = EC_ERROR_EX (E_CLIENT_ERROR_OTHER_ERROR, (*perror)->message);
1851 }*/
1852
1853 if (error) {
1854 g_error_free (*perror);
1855 *perror = error;
1856 }
1857 }
1858
1859 static gboolean
1860 ebb_m365_enum_cached_for_offline (EBookBackendM365 *bbm365)
1861 {
1862 if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_ORG_CONTACTS ||
1863 bbm365->priv->folder_kind == E_M365_FOLDER_KIND_USERS) {
1864 ESourceOffline *offline_ext;
1865
1866 offline_ext = e_source_get_extension (e_backend_get_source (E_BACKEND (bbm365)), E_SOURCE_EXTENSION_OFFLINE);
1867 return e_source_offline_get_stay_synchronized (offline_ext);
1868 }
1869
1870 return TRUE;
1871 }
1872
1873 static gboolean
1874 ebb_m365_update_cache_for_expression_sync (EBookBackendM365 *bbm365,
1875 const gchar *expr,
1876 GCancellable *cancellable,
1877 GError **error)
1878 {
1879 EBookCache *book_cache;
1880 GSList *contacts = NULL; /* EM365Contact * */
1881 gchar *text;
1882 gboolean success = TRUE;
1883
1884 if (bbm365->priv->cached_for_offline ||
1885 bbm365->priv->folder_kind == E_M365_FOLDER_KIND_CONTACTS ||
1886 bbm365->priv->folder_kind == E_M365_FOLDER_KIND_PEOPLE)
1887 return success;
1888
1889 if (!expr || !*expr || g_ascii_strcasecmp (expr, "(contains \"x-evolution-any-field\" \"\")") == 0)
1890 return success;
1891
1892 text = ebb_m365_expr_to_search_text (expr);
1893 if (!text || !*text) {
1894 g_free (text);
1895 return success;
1896 }
1897
1898 book_cache = e_book_meta_backend_ref_cache (E_BOOK_META_BACKEND (bbm365));
1899 if (!E_IS_BOOK_CACHE (book_cache)) {
1900 g_warn_if_fail (E_IS_BOOK_CACHE (book_cache));
1901 g_free (text);
1902 return FALSE;
1903 }
1904
1905 LOCK (bbm365);
1906
1907 success = e_book_meta_backend_ensure_connected_sync (E_BOOK_META_BACKEND (bbm365), cancellable, error) &&
1908 e_m365_connection_search_contacts_sync (bbm365->priv->cnc, NULL, bbm365->priv->folder_kind, bbm365->priv->folder_id, text, &contacts, cancellable, error);
1909
1910 if (success && contacts) {
1911 EBookBackend *book_backend = E_BOOK_BACKEND (bbm365);
1912 GSList *link;
1913
1914 for (link = contacts; link && success; link = g_slist_next (link)) {
1915 EM365Contact *contact = link->data;
1916 EContact *vcard;
1917 gchar *object;
1918 const gchar *id;
1919
1920 if (!contact)
1921 continue;
1922
1923 id = e_m365_contact_get_id (contact);
1924 if (!id)
1925 continue;
1926
1927 vcard = ebb_m365_json_contact_to_vcard (bbm365, contact, bbm365->priv->cnc, &object, cancellable, error);
1928
1929 if (!g_cancellable_is_cancelled (cancellable))
1930 g_warn_if_fail (object != NULL);
1931
1932 if (object && vcard) {
1933 success = e_book_cache_put_contact (book_cache, vcard, object, 0, E_CACHE_IS_ONLINE, cancellable, error);
1934
1935 if (success)
1936 e_book_backend_notify_update (book_backend, vcard);
1937 }
1938
1939 g_clear_object (&vcard);
1940 g_free (object);
1941 }
1942 }
1943
1944 UNLOCK (bbm365);
1945
1946 g_slist_free_full (contacts, (GDestroyNotify) json_object_unref);
1947 g_clear_object (&book_cache);
1948 g_free (text);
1949
1950 return success;
1951 }
1952
1953 static void
1954 ebb_m365_maybe_disconnect_sync (EBookBackendM365 *bbm365,
1955 GError **in_perror,
1956 GCancellable *cancellable)
1957 {
1958 g_return_if_fail (E_IS_BOOK_BACKEND_M365 (bbm365));
1959
1960 if (in_perror && g_error_matches (*in_perror, E_CLIENT_ERROR, E_CLIENT_ERROR_AUTHENTICATION_FAILED)) {
1961 e_book_meta_backend_disconnect_sync (E_BOOK_META_BACKEND (bbm365), cancellable, NULL);
1962 e_backend_schedule_credentials_required (E_BACKEND (bbm365), E_SOURCE_CREDENTIALS_REASON_REJECTED, NULL, 0, NULL, NULL, G_STRFUNC);
1963 }
1964 }
1965
1966 static gboolean
1967 ebb_m365_unset_connection_sync (EBookBackendM365 *bbm365,
1968 gboolean is_disconnect,
1969 GCancellable *cancellable,
1970 GError **error)
1971 {
1972 gboolean success = TRUE;
1973
1974 g_return_val_if_fail (E_IS_BOOK_BACKEND_M365 (bbm365), FALSE);
1975
1976 LOCK (bbm365);
1977
1978 if (bbm365->priv->cnc) {
1979 if (is_disconnect)
1980 success = e_m365_connection_disconnect_sync (bbm365->priv->cnc, cancellable, error);
1981 }
1982
1983 g_clear_object (&bbm365->priv->cnc);
1984 g_clear_pointer (&bbm365->priv->folder_id, g_free);
1985
1986 UNLOCK (bbm365);
1987
1988 return success;
1989 }
1990
1991 static gboolean
1992 ebb_m365_connect_sync (EBookMetaBackend *meta_backend,
1993 const ENamedParameters *credentials,
1994 ESourceAuthenticationResult *out_auth_result,
1995 gchar **out_certificate_pem,
1996 GTlsCertificateFlags *out_certificate_errors,
1997 GCancellable *cancellable,
1998 GError **error)
1999 {
2000 EBookBackendM365 *bbm365;
2001 gboolean success = FALSE;
2002
2003 g_return_val_if_fail (E_IS_BOOK_BACKEND_M365 (meta_backend), FALSE);
2004 g_return_val_if_fail (out_auth_result != NULL, FALSE);
2005
2006 bbm365 = E_BOOK_BACKEND_M365 (meta_backend);
2007
2008 LOCK (bbm365);
2009
2010 if (bbm365->priv->cnc) {
2011 UNLOCK (bbm365);
2012
2013 *out_auth_result = E_SOURCE_AUTHENTICATION_ACCEPTED;
2014
2015 return TRUE;
2016 } else {
2017 EBackend *backend;
2018 ESourceRegistry *registry;
2019 ESource *source;
2020 EM365Connection *cnc = NULL;
2021 ESourceM365Folder *m365_folder_extension;
2022 CamelM365Settings *m365_settings;
2023 gchar *folder_id;
2024
2025 backend = E_BACKEND (bbm365);
2026 source = e_backend_get_source (backend);
2027 registry = e_book_backend_get_registry (E_BOOK_BACKEND (bbm365));
2028 m365_settings = camel_m365_settings_get_from_backend (backend, registry);
2029 g_warn_if_fail (m365_settings != NULL);
2030
2031 m365_folder_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_M365_FOLDER);
2032 folder_id = e_source_m365_folder_dup_id (m365_folder_extension);
2033
2034 if (folder_id) {
2035 cnc = e_m365_connection_new_for_backend (backend, registry, source, m365_settings);
2036
2037 if (g_ascii_strcasecmp (folder_id, E_M365_ARTIFICIAL_FOLDER_ID_ORG_CONTACTS) == 0) {
2038 *out_auth_result = e_m365_connection_authenticate_sync (cnc, NULL, E_M365_FOLDER_KIND_ORG_CONTACTS, NULL, NULL,
2039 out_certificate_pem, out_certificate_errors, cancellable, error);
2040
2041 bbm365->priv->folder_kind = E_M365_FOLDER_KIND_ORG_CONTACTS;
2042
2043 /* do not store artificial folder id */
2044 g_clear_pointer (&folder_id, g_free);
2045 } else if (g_ascii_strcasecmp (folder_id, E_M365_ARTIFICIAL_FOLDER_ID_USERS) == 0) {
2046 *out_auth_result = e_m365_connection_authenticate_sync (cnc, NULL, E_M365_FOLDER_KIND_USERS, NULL, NULL,
2047 out_certificate_pem, out_certificate_errors, cancellable, error);
2048
2049 bbm365->priv->folder_kind = E_M365_FOLDER_KIND_USERS;
2050
2051 /* do not store artificial folder id */
2052 g_clear_pointer (&folder_id, g_free);
2053 } else if (g_ascii_strcasecmp (folder_id, E_M365_ARTIFICIAL_FOLDER_ID_PEOPLE) == 0) {
2054 *out_auth_result = e_m365_connection_authenticate_sync (cnc, NULL, E_M365_FOLDER_KIND_PEOPLE, NULL, NULL,
2055 out_certificate_pem, out_certificate_errors, cancellable, error);
2056
2057 bbm365->priv->folder_kind = E_M365_FOLDER_KIND_PEOPLE;
2058
2059 /* do not store artificial folder id */
2060 g_clear_pointer (&folder_id, g_free);
2061 } else {
2062 *out_auth_result = e_m365_connection_authenticate_sync (cnc, NULL, E_M365_FOLDER_KIND_CONTACTS, NULL, folder_id,
2063 out_certificate_pem, out_certificate_errors, cancellable, error);
2064
2065 bbm365->priv->folder_kind = E_M365_FOLDER_KIND_CONTACTS;
2066 }
2067
2068 if (*out_auth_result == E_SOURCE_AUTHENTICATION_ACCEPTED) {
2069 bbm365->priv->cnc = g_object_ref (cnc);
2070
2071 g_warn_if_fail (bbm365->priv->folder_id == NULL);
2072
2073 g_free (bbm365->priv->folder_id);
2074 bbm365->priv->folder_id = folder_id;
2075
2076 folder_id = NULL;
2077 success = TRUE;
2078
2079 ebb_m365_check_source_properties (bbm365);
2080 }
2081 } else {
2082 *out_auth_result = E_SOURCE_AUTHENTICATION_ERROR;
2083 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_OTHER_ERROR, _("Folder ID is not set")));
2084 }
2085
2086 g_clear_object (&cnc);
2087 g_free (folder_id);
2088
2089 e_book_backend_set_writable (E_BOOK_BACKEND (bbm365), bbm365->priv->folder_id != NULL);
2090 }
2091
2092 UNLOCK (bbm365);
2093
2094 ebb_m365_convert_error_to_client_error (error);
2095
2096 return success;
2097 }
2098
2099 static gboolean
2100 ebb_m365_disconnect_sync (EBookMetaBackend *meta_backend,
2101 GCancellable *cancellable,
2102 GError **error)
2103 {
2104 g_return_val_if_fail (E_IS_BOOK_BACKEND_M365 (meta_backend), FALSE);
2105
2106 return ebb_m365_unset_connection_sync (E_BOOK_BACKEND_M365 (meta_backend), TRUE, cancellable, error);
2107 }
2108
2109 typedef struct _ObjectsDeltaData {
2110 EBookBackendM365 *bbm365;
2111 ECache *cache;
2112 GPtrArray *ids;
2113 GSList **out_created_objects;
2114 GSList **out_modified_objects;
2115 GSList **out_removed_objects;
2116 } ObjectsDeltaData;
2117
2118 static gboolean
2119 ebb_m365_get_objects_delta_cb (EM365Connection *cnc,
2120 const GSList *results, /* JsonObject * - the returned objects from the server */
2121 gpointer user_data,
2122 GCancellable *cancellable,
2123 GError **error)
2124 {
2125 ObjectsDeltaData *odd = user_data;
2126 GSList *link;
2127
2128 g_return_val_if_fail (odd != NULL, FALSE);
2129
2130 for (link = (GSList *) results; link && !g_cancellable_is_cancelled (cancellable); link = g_slist_next (link)) {
2131 EM365Contact *contact = link->data;
2132 const gchar *id;
2133
2134 if (!contact)
2135 continue;
2136
2137 id = e_m365_contact_get_id (contact);
2138
2139 if (!id)
2140 continue;
2141
2142 if (e_m365_delta_is_removed_object (contact)) {
2143 *(odd->out_removed_objects) = g_slist_prepend (*(odd->out_removed_objects),
2144 e_book_meta_backend_info_new (id, NULL, NULL, NULL));
2145 } else {
2146 g_ptr_array_add (odd->ids, g_strdup (id));
2147 }
2148 }
2149
2150 return TRUE;
2151 }
2152
2153 static gboolean
2154 ebb_m365_get_changes_sync (EBookMetaBackend *meta_backend,
2155 const gchar *last_sync_tag,
2156 gboolean is_repeat,
2157 gchar **out_new_sync_tag,
2158 gboolean *out_repeat,
2159 GSList **out_created_objects,
2160 GSList **out_modified_objects,
2161 GSList **out_removed_objects,
2162 GCancellable *cancellable,
2163 GError **error)
2164 {
2165 EBookBackendM365 *bbm365;
2166 EBookCache *book_cache;
2167 ObjectsDeltaData odd;
2168 gboolean success;
2169 GError *local_error = NULL;
2170
2171 g_return_val_if_fail (E_IS_BOOK_BACKEND_M365 (meta_backend), FALSE);
2172 g_return_val_if_fail (out_new_sync_tag != NULL, FALSE);
2173 g_return_val_if_fail (out_repeat != NULL, FALSE);
2174 g_return_val_if_fail (out_created_objects != NULL, FALSE);
2175 g_return_val_if_fail (out_modified_objects != NULL, FALSE);
2176 g_return_val_if_fail (out_removed_objects != NULL, FALSE);
2177
2178 *out_created_objects = NULL;
2179 *out_modified_objects = NULL;
2180 *out_removed_objects = NULL;
2181
2182 bbm365 = E_BOOK_BACKEND_M365 (meta_backend);
2183
2184 book_cache = e_book_meta_backend_ref_cache (meta_backend);
2185 g_return_val_if_fail (E_IS_BOOK_CACHE (book_cache), FALSE);
2186
2187 odd.bbm365 = bbm365;
2188 odd.cache = E_CACHE (book_cache);
2189 odd.ids = g_ptr_array_new_with_free_func (g_free);
2190 odd.out_created_objects = out_created_objects;
2191 odd.out_modified_objects = out_modified_objects;
2192 odd.out_removed_objects = out_removed_objects;
2193
2194 LOCK (bbm365);
2195
2196 if (bbm365->priv->cached_for_offline && bbm365->priv->folder_kind != E_M365_FOLDER_KIND_PEOPLE) {
2197 success = e_m365_connection_get_objects_delta_sync (bbm365->priv->cnc, NULL,
2198 bbm365->priv->folder_kind, bbm365->priv->folder_id, "id", last_sync_tag, 0,
2199 ebb_m365_get_objects_delta_cb, &odd,
2200 out_new_sync_tag, cancellable, &local_error);
2201 } else {
2202 success = TRUE;
2203 }
2204
2205 if (e_m365_connection_util_delta_token_failed (local_error)) {
2206 GSList *known_uids = NULL, *link;
2207
2208 g_clear_error (&local_error);
2209
2210 if (e_book_cache_search_uids (book_cache, NULL, &known_uids, cancellable, error)) {
2211 for (link = known_uids; link; link = g_slist_next (link)) {
2212 const gchar *uid = link->data;
2213
2214 if (uid) {
2215 *out_removed_objects = g_slist_prepend (*out_removed_objects,
2216 e_book_meta_backend_info_new (uid, NULL, NULL, NULL));
2217 }
2218 }
2219 }
2220
2221 e_cache_remove_all (E_CACHE (book_cache), cancellable, NULL);
2222
2223 g_slist_free_full (known_uids, g_free);
2224
2225 success = e_m365_connection_get_objects_delta_sync (bbm365->priv->cnc, NULL,
2226 bbm365->priv->folder_kind, bbm365->priv->folder_id, "id", NULL, 0,
2227 ebb_m365_get_objects_delta_cb, &odd,
2228 out_new_sync_tag, cancellable, &local_error);
2229 } else if (local_error) {
2230 g_propagate_error (error, local_error);
2231 }
2232
2233 if (success && (odd.ids->len || bbm365->priv->folder_kind == E_M365_FOLDER_KIND_PEOPLE)) {
2234 GPtrArray *contacts = NULL;
2235
2236 switch (bbm365->priv->folder_kind) {
2237 case E_M365_FOLDER_KIND_CONTACTS:
2238 success = e_m365_connection_get_contacts_sync (bbm365->priv->cnc, NULL,
2239 bbm365->priv->folder_id, odd.ids, &contacts, cancellable, error);
2240 break;
2241 case E_M365_FOLDER_KIND_ORG_CONTACTS:
2242 success = e_m365_connection_get_org_contacts_sync (bbm365->priv->cnc, NULL,
2243 odd.ids, &contacts, cancellable, error);
2244 break;
2245 case E_M365_FOLDER_KIND_USERS:
2246 success = e_m365_connection_get_users_sync (bbm365->priv->cnc, NULL,
2247 odd.ids, &contacts, cancellable, error);
2248 break;
2249 case E_M365_FOLDER_KIND_PEOPLE:
2250 success = e_m365_connection_get_people_sync (bbm365->priv->cnc, NULL,
2251 bbm365->priv->max_people, &contacts, cancellable, error);
2252 break;
2253 default:
2254 break;
2255 }
2256
2257 /* process them also on failure, because it could fail in following batch requests */
2258 if (contacts != NULL) {
2259 guint ii;
2260
2261 for (ii = 0; ii < contacts->len; ii++) {
2262 EM365Contact *contact = g_ptr_array_index (contacts, ii);
2263 GSList **out_slist;
2264 EContact *vcard;
2265 gchar *object;
2266 const gchar *id = e_m365_contact_get_id (contact);
2267
2268 if (!id)
2269 continue;
2270
2271 if (e_cache_contains (odd.cache, id, E_CACHE_INCLUDE_DELETED))
2272 out_slist = odd.out_modified_objects;
2273 else
2274 out_slist = odd.out_created_objects;
2275
2276 vcard = ebb_m365_json_contact_to_vcard (bbm365, contact, bbm365->priv->cnc, &object, cancellable, error);
2277
2278 g_clear_object (&vcard);
2279
2280 if (!g_cancellable_is_cancelled (cancellable))
2281 g_warn_if_fail (object != NULL);
2282
2283 if (object) {
2284 EBookMetaBackendInfo *nfo;
2285
2286 nfo = e_book_meta_backend_info_new (id,
2287 e_m365_contact_get_change_key (contact),
2288 object, NULL);
2289
2290 nfo->extra = object; /* assumes ownership, to avoid unnecessary re-allocation */
2291
2292 *out_slist = g_slist_prepend (*out_slist, nfo);
2293 }
2294 }
2295
2296 g_ptr_array_unref (contacts);
2297 }
2298 }
2299
2300 UNLOCK (bbm365);
2301
2302 ebb_m365_convert_error_to_client_error (error);
2303 ebb_m365_maybe_disconnect_sync (bbm365, error, cancellable);
2304
2305 g_clear_pointer (&odd.ids, g_ptr_array_unref);
2306 g_clear_object (&book_cache);
2307
2308 return success;
2309 }
2310
2311 static gboolean
2312 ebb_m365_load_contact_sync (EBookMetaBackend *meta_backend,
2313 const gchar *uid,
2314 const gchar *extra,
2315 EContact **out_contact,
2316 gchar **out_extra,
2317 GCancellable *cancellable,
2318 GError **error)
2319 {
2320 EBookBackendM365 *bbm365;
2321 EM365Contact *contact = NULL;
2322 gboolean success = FALSE;
2323
2324 g_return_val_if_fail (E_IS_BOOK_BACKEND_M365 (meta_backend), FALSE);
2325 g_return_val_if_fail (uid != NULL, FALSE);
2326 g_return_val_if_fail (out_contact, FALSE);
2327 g_return_val_if_fail (out_extra != NULL, FALSE);
2328
2329 bbm365 = E_BOOK_BACKEND_M365 (meta_backend);
2330
2331 LOCK (bbm365);
2332
2333 if (bbm365->priv->folder_id) {
2334 success = e_m365_connection_get_contact_sync (bbm365->priv->cnc, NULL,
2335 bbm365->priv->folder_id, uid, &contact, cancellable, error);
2336 } else if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_ORG_CONTACTS) {
2337 success = e_m365_connection_get_org_contact_sync (bbm365->priv->cnc, NULL,
2338 uid, &contact, cancellable, error);
2339 } else if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_USERS) {
2340 success = e_m365_connection_get_user_sync (bbm365->priv->cnc, NULL,
2341 uid, &contact, cancellable, error);
2342 } else {
2343 g_warn_if_reached ();
2344 }
2345
2346 if (success) {
2347 *out_contact = ebb_m365_json_contact_to_vcard (bbm365, contact, bbm365->priv->cnc, out_extra, cancellable, error);
2348
2349 if (contact)
2350 json_object_unref (contact);
2351 }
2352
2353 UNLOCK (bbm365);
2354
2355 ebb_m365_convert_error_to_client_error (error);
2356 ebb_m365_maybe_disconnect_sync (bbm365, error, cancellable);
2357
2358 return success;
2359 }
2360
2361 static gboolean
2362 ebb_m365_save_contact_sync (EBookMetaBackend *meta_backend,
2363 gboolean overwrite_existing,
2364 EConflictResolution conflict_resolution,
2365 /* const */ EContact *contact,
2366 const gchar *extra,
2367 guint32 opflags,
2368 gchar **out_new_uid,
2369 gchar **out_new_extra,
2370 GCancellable *cancellable,
2371 GError **error)
2372 {
2373 EBookBackendM365 *bbm365;
2374 EContact *tmp_contact = NULL, *old_contact = NULL;
2375 JsonBuilder *builder;
2376 gboolean success = FALSE;
2377
2378 g_return_val_if_fail (E_IS_BOOK_BACKEND_M365 (meta_backend), FALSE);
2379 g_return_val_if_fail (E_IS_CONTACT (contact), FALSE);
2380 g_return_val_if_fail (out_new_uid != NULL, FALSE);
2381 g_return_val_if_fail (out_new_extra != NULL, FALSE);
2382
2383 if (GPOINTER_TO_INT (e_contact_get (contact, E_CONTACT_IS_LIST))) {
2384 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_NOT_SUPPORTED, _("Cannot save contact list into a Microsoft 365 address book")));
2385 return FALSE;
2386 }
2387
2388 bbm365 = E_BOOK_BACKEND_M365 (meta_backend);
2389
2390 LOCK (bbm365);
2391
2392 if (!bbm365->priv->folder_id) {
2393 const gchar *text = "Cannot modify contact"; /* this should not happen, thus not localized */
2394
2395 if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_ORG_CONTACTS)
2396 text = _("Cannot modify organizational contact");
2397 else if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_USERS)
2398 text = _("Cannot modify user contact");
2399 else if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_PEOPLE)
2400 text = _("Cannot modify recent contact");
2401
2402 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_PERMISSION_DENIED, text));
2403
2404 UNLOCK (bbm365);
2405
2406 return FALSE;
2407 }
2408
2409 if (e_vcard_get_attribute (E_VCARD (contact), EVC_PHOTO)) {
2410 tmp_contact = e_contact_duplicate (contact);
2411 contact = tmp_contact;
2412
2413 e_contact_inline_local_photos (contact, NULL);
2414 }
2415
2416 if (extra && *extra)
2417 old_contact = e_contact_new_from_vcard (extra);
2418
2419 builder = ebb_m365_contact_to_json_locked (bbm365, contact, old_contact, cancellable, error);
2420
2421 if (builder) {
2422 if (overwrite_existing) {
2423 const gchar *uid = e_contact_get_const (contact, E_CONTACT_UID);
2424
2425 success = e_m365_connection_update_contact_sync (bbm365->priv->cnc, NULL, bbm365->priv->folder_id,
2426 uid, builder, cancellable, error);
2427
2428 if (success)
2429 success = ebb_m365_contact_to_json_2nd_go_locked (bbm365, contact, old_contact, uid, cancellable, error);
2430
2431 if (success)
2432 *out_new_extra = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_30);
2433 } else {
2434 EM365Contact *created_contact = NULL;
2435
2436 success = e_m365_connection_create_contact_sync (bbm365->priv->cnc, NULL, bbm365->priv->folder_id,
2437 builder, &created_contact, cancellable, error);
2438
2439 if (success && created_contact) {
2440 const gchar *m365_id = e_m365_contact_get_id (created_contact);
2441
2442 success = ebb_m365_contact_to_json_2nd_go_locked (bbm365, contact, old_contact, m365_id, cancellable, error);
2443 }
2444
2445 if (success && created_contact) {
2446 EContact *vcard;
2447
2448 *out_new_uid = g_strdup (e_m365_contact_get_id (created_contact));
2449
2450 vcard = ebb_m365_json_contact_to_vcard (bbm365, created_contact, bbm365->priv->cnc, out_new_extra, cancellable, error);
2451
2452 if (!vcard)
2453 success = FALSE;
2454
2455 g_clear_object (&vcard);
2456 }
2457
2458 if (created_contact)
2459 json_object_unref (created_contact);
2460 }
2461
2462 g_clear_object (&builder);
2463 }
2464
2465 UNLOCK (bbm365);
2466
2467 ebb_m365_convert_error_to_client_error (error);
2468 ebb_m365_maybe_disconnect_sync (bbm365, error, cancellable);
2469
2470 g_clear_object (&old_contact);
2471 g_clear_object (&tmp_contact);
2472
2473 return success;
2474 }
2475
2476 static gboolean
2477 ebb_m365_remove_contact_sync (EBookMetaBackend *meta_backend,
2478 EConflictResolution conflict_resolution,
2479 const gchar *uid,
2480 const gchar *extra,
2481 const gchar *object,
2482 guint32 opflags,
2483 GCancellable *cancellable,
2484 GError **error)
2485 {
2486 EBookBackendM365 *bbm365;
2487 gboolean success;
2488
2489 g_return_val_if_fail (E_IS_BOOK_BACKEND_M365 (meta_backend), FALSE);
2490
2491 bbm365 = E_BOOK_BACKEND_M365 (meta_backend);
2492
2493 LOCK (bbm365);
2494
2495 if (bbm365->priv->folder_id) {
2496 GError *local_error = NULL;
2497
2498 success = e_m365_connection_delete_contact_sync (bbm365->priv->cnc, NULL,
2499 bbm365->priv->folder_id, uid, cancellable, &local_error);
2500
2501 if (g_error_matches (local_error, E_M365_ERROR, E_M365_ERROR_ITEM_NOT_FOUND)) {
2502 g_clear_error (&local_error);
2503 success = TRUE;
2504 } else if (local_error) {
2505 g_propagate_error (error, local_error);
2506 }
2507 } else {
2508 const gchar *text = "Cannot remove contact"; /* this should not happen, thus not localized */
2509
2510 if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_ORG_CONTACTS)
2511 text = _("Cannot remove organizational contact");
2512 else if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_USERS)
2513 text = _("Cannot remove user contact");
2514 else if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_PEOPLE)
2515 text = _("Cannot remove recent contact");
2516
2517 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_PERMISSION_DENIED, text));
2518 success = FALSE;
2519 }
2520
2521 UNLOCK (bbm365);
2522
2523 ebb_m365_convert_error_to_client_error (error);
2524 ebb_m365_maybe_disconnect_sync (bbm365, error, cancellable);
2525
2526 return success;
2527 }
2528
2529 static gboolean
2530 ebb_m365_search_sync (EBookMetaBackend *meta_backend,
2531 const gchar *expr,
2532 gboolean meta_contact,
2533 GSList **out_contacts,
2534 GCancellable *cancellable,
2535 GError **error)
2536 {
2537 g_return_val_if_fail (E_IS_BOOK_BACKEND_M365 (meta_backend), FALSE);
2538
2539 /* Ignore errors, just try its best */
2540 ebb_m365_update_cache_for_expression_sync (E_BOOK_BACKEND_M365 (meta_backend), expr, cancellable, NULL);
2541
2542 /* Chain up to parent's method */
2543 return E_BOOK_META_BACKEND_CLASS (e_book_backend_m365_parent_class)->search_sync (meta_backend, expr, meta_contact, out_contacts, cancellable, error);
2544 }
2545
2546 static gboolean
2547 ebb_m365_search_uids_sync (EBookMetaBackend *meta_backend,
2548 const gchar *expr,
2549 GSList **out_uids,
2550 GCancellable *cancellable,
2551 GError **error)
2552 {
2553 g_return_val_if_fail (E_IS_BOOK_BACKEND_M365 (meta_backend), FALSE);
2554
2555 /* Ignore errors, just try its best */
2556 ebb_m365_update_cache_for_expression_sync (E_BOOK_BACKEND_M365 (meta_backend), expr, cancellable, NULL);
2557
2558 /* Chain up to parent's method */
2559 return E_BOOK_META_BACKEND_CLASS (e_book_backend_m365_parent_class)->search_uids_sync (meta_backend, expr,
2560 out_uids, cancellable, error);
2561 }
2562
2563 static gchar *
2564 ebb_m365_get_backend_property (EBookBackend *book_backend,
2565 const gchar *prop_name)
2566 {
2567 g_return_val_if_fail (E_IS_BOOK_BACKEND_M365 (book_backend), NULL);
2568 g_return_val_if_fail (prop_name != NULL, NULL);
2569
2570 if (g_str_equal (prop_name, CLIENT_BACKEND_PROPERTY_CAPABILITIES)) {
2571 gboolean cached_for_offline = ebb_m365_enum_cached_for_offline (E_BOOK_BACKEND_M365 (book_backend));
2572
2573 return g_strjoin (",",
2574 "net",
2575 "contact-lists",
2576 cached_for_offline ? "do-initial-query" : "",
2577 e_book_meta_backend_get_capabilities (E_BOOK_META_BACKEND (book_backend)),
2578 NULL);
2579 } else if (g_str_equal (prop_name, E_BOOK_BACKEND_PROPERTY_REQUIRED_FIELDS)) {
2580 return g_strdup (e_contact_field_name (E_CONTACT_FILE_AS));
2581 } else if (g_str_equal (prop_name, E_BOOK_BACKEND_PROPERTY_SUPPORTED_FIELDS)) {
2582 GString *buffer;
2583 gchar *fields;
2584 gint ii;
2585
2586 buffer = g_string_sized_new (1024);
2587
2588 for (ii = 0; ii < G_N_ELEMENTS (mappings); ii++) {
2589 if (buffer->len > 0)
2590 g_string_append_c (buffer, ',');
2591
2592 g_string_append (buffer, e_contact_field_name (mappings[ii].field_id));
2593 }
2594
2595 fields = g_strjoin (
2596 ",",
2597 buffer->str,
2598 e_contact_field_name (E_CONTACT_FULL_NAME),
2599 e_contact_field_name (E_CONTACT_EMAIL_1),
2600 e_contact_field_name (E_CONTACT_EMAIL_2),
2601 e_contact_field_name (E_CONTACT_EMAIL_3),
2602 e_contact_field_name (E_CONTACT_EMAIL_4),
2603 NULL);
2604
2605 g_string_free (buffer, TRUE);
2606
2607 return fields;
2608 }
2609
2610 /* Chain up to parent's method. */
2611 return E_BOOK_BACKEND_CLASS (e_book_backend_m365_parent_class)->impl_get_backend_property (book_backend, prop_name);
2612 }
2613
2614 static void
2615 ebb_m365_start_view_search_thread_func (EBookBackend *book_backend,
2616 gpointer user_data,
2617 GCancellable *cancellable,
2618 GError **error)
2619 {
2620 EDataBookView *view = user_data;
2621 EBookBackendM365 *bbm365;
2622
2623 g_return_if_fail (E_IS_BOOK_BACKEND_M365 (book_backend));
2624 g_return_if_fail (E_IS_DATA_BOOK_VIEW (view));
2625
2626 bbm365 = E_BOOK_BACKEND_M365 (book_backend);
2627
2628 if (!g_cancellable_set_error_if_cancelled (cancellable, error)) {
2629 EBookBackendSExp *sexp;
2630 GSList *contacts = NULL;
2631 const gchar *expr = NULL;
2632
2633 sexp = e_data_book_view_get_sexp (view);
2634 if (sexp)
2635 expr = e_book_backend_sexp_text (sexp);
2636
2637 /* this is called only for not-cached-for-offline books and it's enough to search for the contacts,
2638 the rest is done by the notifications when added/discovered a new contact */
2639 if (e_book_meta_backend_search_sync (E_BOOK_META_BACKEND (book_backend), expr, TRUE, &contacts, cancellable, NULL))
2640 g_slist_free_full (contacts, g_object_unref);
2641 }
2642
2643 LOCK (bbm365);
2644 g_hash_table_remove (bbm365->priv->view_cancellables, view);
2645 UNLOCK (bbm365);
2646 }
2647
2648 static void
2649 ebb_m365_start_view (EBookBackend *book_backend,
2650 EDataBookView *view)
2651 {
2652 if ((e_data_book_view_get_flags (view) & E_BOOK_CLIENT_VIEW_FLAGS_MANUAL_QUERY) != 0) {
2653 EBookBackendM365 *bbm365 = E_BOOK_BACKEND_M365 (book_backend);
2654
2655 if (!bbm365->priv->cached_for_offline) {
2656 GCancellable *cancellable;
2657
2658 cancellable = g_cancellable_new ();
2659
2660 LOCK (bbm365);
2661 g_hash_table_insert (bbm365->priv->view_cancellables, view, g_object_ref (cancellable));
2662 UNLOCK (bbm365);
2663
2664 e_book_backend_schedule_custom_operation (book_backend, cancellable,
2665 ebb_m365_start_view_search_thread_func, g_object_ref (view), g_object_unref);
2666
2667 g_clear_object (&cancellable);
2668 }
2669 }
2670
2671 /* Chain up to parent's method. */
2672 E_BOOK_BACKEND_CLASS (e_book_backend_m365_parent_class)->impl_start_view (book_backend, view);
2673 }
2674
2675 static void
2676 ebb_m365_stop_view (EBookBackend *book_backend,
2677 EDataBookView *view)
2678 {
2679 if ((e_data_book_view_get_flags (view) & E_BOOK_CLIENT_VIEW_FLAGS_MANUAL_QUERY) != 0) {
2680 EBookBackendM365 *bbm365 = E_BOOK_BACKEND_M365 (book_backend);
2681 GCancellable *cancellable;
2682 gpointer value = NULL;
2683
2684 LOCK (bbm365);
2685 if (!g_hash_table_steal_extended (bbm365->priv->view_cancellables, view, NULL, &value))
2686 value = NULL;
2687 UNLOCK (bbm365);
2688
2689 cancellable = value;
2690
2691 if (cancellable) {
2692 g_cancellable_cancel (cancellable);
2693 g_clear_object (&cancellable);
2694 }
2695 }
2696
2697 /* Chain up to parent's method. */
2698 E_BOOK_BACKEND_CLASS (e_book_backend_m365_parent_class)->impl_stop_view (book_backend, view);
2699 }
2700
2701 static gboolean
2702 ebb_m365_get_destination_address (EBackend *backend,
2703 gchar **host,
2704 guint16 *port)
2705 {
2706 g_return_val_if_fail (port != NULL, FALSE);
2707 g_return_val_if_fail (host != NULL, FALSE);
2708
2709 /* Sanity checking */
2710 if (!e_book_backend_get_registry (E_BOOK_BACKEND (backend)) ||
2711 !e_backend_get_source (backend))
2712 return FALSE;
2713
2714 *host = g_strdup ("graph.microsoft.com");
2715 *port = 443;
2716
2717 return TRUE;
2718 }
2719
2720 static void
2721 ebb_m365_check_source_properties (EBookBackendM365 *bbm365)
2722 {
2723 ESourceM365Folder *m365_folder_ext;
2724 guint max_people;
2725 gboolean cached_for_offline;
2726
2727 if (!e_backend_get_source (E_BACKEND (bbm365)))
2728 return;
2729
2730 cached_for_offline = ebb_m365_enum_cached_for_offline (bbm365);
2731
2732 if ((cached_for_offline ? 1 : 0) != (bbm365->priv->cached_for_offline ? 1 : 0)) {
2733 EBookBackend *book_backend = E_BOOK_BACKEND (bbm365);
2734 gchar *value;
2735
2736 bbm365->priv->cached_for_offline = cached_for_offline;
2737
2738 value = ebb_m365_get_backend_property (book_backend, CLIENT_BACKEND_PROPERTY_CAPABILITIES);
2739
2740 e_book_backend_notify_property_changed (book_backend, CLIENT_BACKEND_PROPERTY_CAPABILITIES, value);
2741
2742 g_free (value);
2743 }
2744
2745 m365_folder_ext = e_source_get_extension (e_backend_get_source (E_BACKEND (bbm365)), E_SOURCE_EXTENSION_M365_FOLDER);
2746 max_people = e_source_m365_folder_get_max_people (m365_folder_ext);
2747
2748 if (max_people != bbm365->priv->max_people) {
2749 bbm365->priv->max_people = max_people;
2750
2751 if (bbm365->priv->folder_kind == E_M365_FOLDER_KIND_PEOPLE &&
2752 e_backend_get_online (E_BACKEND (bbm365)))
2753 e_book_meta_backend_schedule_refresh (E_BOOK_META_BACKEND (bbm365));
2754 }
2755 }
2756
2757 static void
2758 e_book_backend_m365_dispose (GObject *object)
2759 {
2760 EBookBackendM365 *bbm365 = E_BOOK_BACKEND_M365 (object);
2761
2762 ebb_m365_unset_connection_sync (bbm365, FALSE, NULL, NULL);
2763
2764 /* Chain up to parent's method. */
2765 G_OBJECT_CLASS (e_book_backend_m365_parent_class)->dispose (object);
2766 }
2767
2768 static void
2769 e_book_backend_m365_finalize (GObject *object)
2770 {
2771 EBookBackendM365 *bbm365 = E_BOOK_BACKEND_M365 (object);
2772
2773 g_rec_mutex_clear (&bbm365->priv->property_lock);
2774 g_hash_table_destroy (bbm365->priv->view_cancellables);
2775
2776 /* Chain up to parent's method. */
2777 G_OBJECT_CLASS (e_book_backend_m365_parent_class)->finalize (object);
2778 }
2779
2780 static void
2781 e_book_backend_m365_init (EBookBackendM365 *bbm365)
2782 {
2783 bbm365->priv = e_book_backend_m365_get_instance_private (bbm365);
2784
2785 g_rec_mutex_init (&bbm365->priv->property_lock);
2786 bbm365->priv->view_cancellables = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref);
2787
2788 g_signal_connect (bbm365, "source-changed",
2789 G_CALLBACK (ebb_m365_check_source_properties), NULL);
2790 }
2791
2792 static void
2793 e_book_backend_m365_class_init (EBookBackendM365Class *klass)
2794 {
2795 GObjectClass *object_class;
2796 EBackendClass *backend_class;
2797 EBookBackendClass *book_backend_class;
2798 EBookMetaBackendClass *book_meta_backend_class;
2799
2800 book_meta_backend_class = E_BOOK_META_BACKEND_CLASS (klass);
2801 book_meta_backend_class->backend_module_filename = "libebookbackendmicrosoft365.so";
2802 book_meta_backend_class->backend_factory_type_name = "EBookBackendM365Factory";
2803 book_meta_backend_class->connect_sync = ebb_m365_connect_sync;
2804 book_meta_backend_class->disconnect_sync = ebb_m365_disconnect_sync;
2805 book_meta_backend_class->get_changes_sync = ebb_m365_get_changes_sync;
2806 book_meta_backend_class->load_contact_sync = ebb_m365_load_contact_sync;
2807 book_meta_backend_class->save_contact_sync = ebb_m365_save_contact_sync;
2808 book_meta_backend_class->remove_contact_sync = ebb_m365_remove_contact_sync;
2809 book_meta_backend_class->search_sync = ebb_m365_search_sync;
2810 book_meta_backend_class->search_uids_sync = ebb_m365_search_uids_sync;
2811
2812 book_backend_class = E_BOOK_BACKEND_CLASS (klass);
2813 book_backend_class->impl_get_backend_property = ebb_m365_get_backend_property;
2814 book_backend_class->impl_start_view = ebb_m365_start_view;
2815 book_backend_class->impl_stop_view = ebb_m365_stop_view;
2816
2817 backend_class = E_BACKEND_CLASS (klass);
2818 backend_class->get_destination_address = ebb_m365_get_destination_address;
2819
2820 object_class = G_OBJECT_CLASS (klass);
2821 object_class->dispose = e_book_backend_m365_dispose;
2822 object_class->finalize = e_book_backend_m365_finalize;
2823 }