Debian Codegraph @

src/EWS/common/e-ews-item.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 1999-2008 Novell, Inc. (www.novell.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 <errno.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <glib.h>
14 #include <glib/gstdio.h>
15 #include <glib/gprintf.h>
16 #include <libsoup/soup.h>
17 #include <libedataserver/libedataserver.h>
18 #include <libecal/libecal.h>
19
20 #include "e-ews-item.h"
21 #include "e-ews-item-change.h"
22
23 struct _EEwsContactFields {
24 gchar *fileas;
25 gchar *display_name;
26 EwsCompleteName *complete_name;
27
28 GHashTable *email_addresses;
29 GHashTable *physical_addresses;
30 GHashTable *phone_numbers;
31 GHashTable *im_addresses;
32
33 gchar *company_name;
34 gchar *department;
35 gchar *job_title;
36 gchar *assistant_name;
37 gchar *manager;
38 gchar *office_location;
39
40 gchar *business_homepage;
41
42 gboolean has_birthday;
43 time_t birthday;
44 gboolean has_wedding_anniversary;
45 time_t wedding_anniversary;
46
47 gchar *profession;
48 gchar *spouse_name;
49 gchar *culture;
50 gchar *surname;
51 gchar *givenname;
52 gchar *middlename;
53 gchar *notes;
54
55 gsize msexchange_cert_len;
56 guchar *msexchange_cert;
57
58 gsize user_cert_len;
59 guchar *user_cert;
60 };
61
62 struct _EEwsTaskFields {
63 gchar *percent_complete;
64 gchar *status;
65 gchar *body;
66 gchar *sensitivity;
67 gchar *owner;
68 gchar *delegator;
69 time_t due_date;
70 time_t start_date;
71 time_t complete_date;
72 gboolean has_due_date;
73 gboolean has_start_date;
74 gboolean has_complete_date;
75 };
76
77 struct _EEwsItemPrivate {
78 EwsId *attachment_id;
79 EEwsItemType item_type;
80 GError *error;
81
82 /* MAPI properties */
83 /* The Exchange server is so fundamentally misdesigned that it doesn't expose
84 * certain information in a coherent way; the \Answered, and \Deleted message
85 * flags don't even seem to work properly. It looks like the only way to work
86 * it out is from the PidTagIconIndex field. Quite what the hell an *icon*
87 * selector is doing in the database, I have absolutely no fucking idea; a
88 * database is supposed to represent the *data*, not do bizarre things that
89 * live in the client. But that's typical Exchange brain damage for you... */
90 guint32 mapi_icon_index; /* http://msdn.microsoft.com/en-us/library/cc815472.aspx */
91 guint32 mapi_last_verb_executed; /* http://msdn.microsoft.com/en-us/library/cc841968.aspx */
92 guint32 mapi_message_status; /* http://msdn.microsoft.com/en-us/library/cc839915.aspx */
93 guint32 mapi_message_flags; /* http://msdn.microsoft.com/en-us/library/cc839733.aspx */
94
95 GHashTable *mapi_extended_tags; /* simple tag->string_value */
96 GHashTable *mapi_extended_sets; /* setid-> [ tag->string_value ] */
97
98 /* properties */
99 EwsId *item_id;
100 gchar *subject;
101 gchar *mime_content;
102 gchar *body;
103 EEwsBodyType body_type;
104
105 gchar *date_header;
106 time_t date_received;
107 time_t date_sent;
108 time_t date_created;
109 time_t last_modified_time;
110
111 gsize size;
112 gchar *msg_id;
113 gchar *in_replyto;
114 gchar *references;
115 gchar *preview;
116 gboolean has_attachments;
117 gboolean is_read;
118 EwsImportance importance;
119
120 gchar *uid;
121 gchar *timezone;
122 time_t calendar_start;
123 time_t calendar_end;
124 gchar *start_timezone;
125 gchar *end_timezone;
126 gchar *contact_photo_id;
127 gchar *iana_start_time_zone;
128 gchar *iana_end_time_zone;
129 gchar *event_url;
130
131 GSList *to_recipients;
132 GSList *cc_recipients;
133 GSList *bcc_recipients;
134
135 EwsMailbox *from;
136 EwsMailbox *sender;
137
138 gboolean is_meeting;
139 gboolean is_response_requested;
140 GSList *modified_occurrences;
141 GSList *attachments_ids;
142 gchar *my_response_type;
143 GSList *attendees;
144
145 EwsId *calendar_item_accept_id;
146
147 /* the evolution labels are implemented as exchange
148 * Categories. These appear in the message headers as
149 * Keywords: and are set and extracted from the EWS server as
150 * <Categories> which is a string array valued XML element */
151 GSList *categories;
152
153 gboolean reminder_is_set;
154 time_t reminder_due_by;
155 gint reminder_minutes_before_start;
156 EEwsRecurrence recurrence;
157
158 struct _EEwsContactFields *contact_fields;
159 struct _EEwsTaskFields *task_fields;
160 };
161
162 G_DEFINE_TYPE_WITH_PRIVATE (EEwsItem, e_ews_item, G_TYPE_OBJECT)
163
164 static void ews_item_free_attendee (EwsAttendee *attendee);
165 static void ews_free_contact_fields (struct _EEwsContactFields *con_fields);
166
167 typedef gpointer (* EwsGetValFunc) (ESoapParameter *param);
168
169 static void
170 e_ews_item_dispose (GObject *object)
171 {
172 EEwsItem *item = (EEwsItem *) object;
173 EEwsItemPrivate *priv;
174
175 g_return_if_fail (E_IS_EWS_ITEM (item));
176
177 priv = item->priv;
178
179 g_clear_error (&priv->error);
180
181 if (priv->mapi_extended_sets) {
182 g_hash_table_destroy (priv->mapi_extended_sets);
183 priv->mapi_extended_sets = NULL;
184 }
185
186 if (priv->mapi_extended_tags) {
187 g_hash_table_destroy (priv->mapi_extended_tags);
188 priv->mapi_extended_tags = NULL;
189 }
190
191 if (priv->item_id) {
192 g_free (priv->item_id->id);
193 g_free (priv->item_id->change_key);
194 g_free (priv->item_id);
195 priv->item_id = NULL;
196 }
197
198 if (priv->attachment_id) {
199 g_free (priv->attachment_id->id);
200 g_free (priv->attachment_id->change_key);
201 g_free (priv->attachment_id);
202 priv->attachment_id = NULL;
203 }
204
205 g_clear_pointer (&priv->mime_content, g_free);
206 g_clear_pointer (&priv->body, g_free);
207 g_clear_pointer (&priv->subject, g_free);
208 g_clear_pointer (&priv->msg_id, g_free);
209 g_clear_pointer (&priv->uid, g_free);
210 g_clear_pointer (&priv->in_replyto, g_free);
211 g_clear_pointer (&priv->references, g_free);
212 g_clear_pointer (&priv->preview, g_free);
213 g_clear_pointer (&priv->date_header, g_free);
214 g_clear_pointer (&priv->timezone, g_free);
215 g_clear_pointer (&priv->start_timezone, g_free);
216 g_clear_pointer (&priv->end_timezone, g_free);
217 g_clear_pointer (&priv->contact_photo_id, g_free);
218 g_clear_pointer (&priv->iana_start_time_zone, g_free);
219 g_clear_pointer (&priv->iana_end_time_zone, g_free);
220 g_clear_pointer (&priv->event_url, g_free);
221
222 g_slist_free_full (priv->to_recipients, (GDestroyNotify) e_ews_mailbox_free);
223 priv->to_recipients = NULL;
224
225 g_slist_free_full (priv->cc_recipients, (GDestroyNotify) e_ews_mailbox_free);
226 priv->cc_recipients = NULL;
227
228 g_slist_free_full (priv->bcc_recipients, (GDestroyNotify) e_ews_mailbox_free);
229 priv->bcc_recipients = NULL;
230
231 g_slist_free_full (priv->modified_occurrences, g_free);
232 priv->modified_occurrences = NULL;
233
234 g_slist_free_full (priv->attachments_ids, g_free);
235 priv->attachments_ids = NULL;
236
237 g_clear_pointer (&priv->my_response_type, g_free);
238
239 g_slist_free_full (priv->attendees, (GDestroyNotify) ews_item_free_attendee);
240 priv->attendees = NULL;
241
242 if (priv->calendar_item_accept_id) {
243 g_free (priv->calendar_item_accept_id->id);
244 g_free (priv->calendar_item_accept_id->change_key);
245 g_free (priv->calendar_item_accept_id);
246 priv->calendar_item_accept_id = NULL;
247 }
248
249 e_ews_mailbox_free (priv->sender);
250 e_ews_mailbox_free (priv->from);
251
252 if (priv->item_type == E_EWS_ITEM_TYPE_CONTACT)
253 ews_free_contact_fields (priv->contact_fields);
254
255 if (priv->task_fields) {
256 g_free (priv->task_fields->percent_complete);
257 priv->task_fields->percent_complete = NULL;
258 g_free (priv->task_fields->status);
259 priv->task_fields->status = NULL;
260 g_free (priv->task_fields->body);
261 priv->task_fields->body = NULL;
262 g_free (priv->task_fields->sensitivity);
263 priv->task_fields->sensitivity = NULL;
264 g_free (priv->task_fields->owner);
265 priv->task_fields->owner = NULL;
266 g_free (priv->task_fields);
267 }
268
269 g_slist_free_full (priv->categories, g_free);
270 priv->categories = NULL;
271
272 G_OBJECT_CLASS (e_ews_item_parent_class)->dispose (object);
273 }
274
275 static void
276 e_ews_item_class_init (EEwsItemClass *klass)
277 {
278 GObjectClass *object_class = G_OBJECT_CLASS (klass);
279
280 object_class->dispose = e_ews_item_dispose;
281 }
282
283 static void
284 e_ews_item_init (EEwsItem *item)
285 {
286 item->priv = e_ews_item_get_instance_private (item);
287
288 item->priv->item_type = E_EWS_ITEM_TYPE_UNKNOWN;
289 item->priv->body_type = E_EWS_BODY_TYPE_ANY;
290 item->priv->is_meeting = FALSE;
291 item->priv->is_response_requested = FALSE;
292
293 item->priv->mapi_extended_tags = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
294 item->priv->mapi_extended_sets = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_destroy);
295
296 item->priv->reminder_is_set = FALSE;
297 item->priv->reminder_minutes_before_start = -1;
298 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
299 item->priv->recurrence.end_type = E_EWS_RECURRENCE_END_UNKNOWN;
300 }
301
302 static void
303 ews_free_contact_fields (struct _EEwsContactFields *con_fields)
304 {
305 if (con_fields) {
306 if (con_fields->complete_name) {
307 EwsCompleteName *cn = con_fields->complete_name;
308
309 g_free (cn->title);
310 g_free (cn->first_name);
311 g_free (cn->middle_name);
312 g_free (cn->last_name);
313 g_free (cn->suffix);
314 g_free (cn->initials);
315 g_free (cn->full_name);
316 g_free (cn->nick_name);
317 g_free (cn->yomi_first_name);
318 g_free (cn->yomi_last_name);
319 g_free (cn);
320 }
321
322 if (con_fields->email_addresses)
323 g_hash_table_destroy (con_fields->email_addresses);
324
325 if (con_fields->physical_addresses)
326 g_hash_table_destroy (con_fields->physical_addresses);
327
328 if (con_fields->phone_numbers)
329 g_hash_table_destroy (con_fields->phone_numbers);
330
331 if (con_fields->im_addresses)
332 g_hash_table_destroy (con_fields->im_addresses);
333
334 g_free (con_fields->display_name);
335 g_free (con_fields->fileas);
336 g_free (con_fields->company_name);
337 g_free (con_fields->department);
338 g_free (con_fields->job_title);
339 g_free (con_fields->assistant_name);
340 g_free (con_fields->manager);
341 g_free (con_fields->office_location);
342 g_free (con_fields->business_homepage);
343 g_free (con_fields->profession);
344 g_free (con_fields->spouse_name);
345 g_free (con_fields->culture);
346 g_free (con_fields->surname);
347 g_free (con_fields->givenname);
348 g_free (con_fields->middlename);
349 g_free (con_fields->notes);
350 g_free (con_fields->msexchange_cert);
351 g_free (con_fields->user_cert);
352 g_free (con_fields);
353 }
354 }
355
356 static void
357 ews_item_free_attendee (EwsAttendee *attendee)
358 {
359 if (attendee) {
360 e_ews_mailbox_free (attendee->mailbox);
361 g_free (attendee->responsetype);
362 g_free (attendee);
363 }
364 }
365
366 static time_t
367 ews_item_parse_date (ESoapParameter *param)
368 {
369 time_t t = 0;
370 GTimeVal t_val;
371 gchar *dtstring;
372 gint len;
373
374 dtstring = e_soap_parameter_get_string_value (param);
375
376 g_return_val_if_fail (dtstring != NULL, 0);
377
378 len = strlen (dtstring);
379 if (g_time_val_from_iso8601 (dtstring, &t_val)) {
380 t = (time_t) t_val.tv_sec;
381 } else if (len == 8 || (len == 11 && dtstring[4] == '-' && dtstring[7] == '-' && dtstring[10] == 'Z')) {
382 /* It might be a date value */
383 guint16 year;
384 guint month;
385 guint8 day;
386
387 if (len == 11) {
388 dtstring[4] = dtstring[5];
389 dtstring[5] = dtstring[6];
390 dtstring[6] = dtstring[8];
391 dtstring[7] = dtstring[9];
392 dtstring[8] = dtstring[10];
393 dtstring[9] = '\0';
394 }
395
396 #define digit_at(x,y) (x[y] - '0')
397 year = digit_at (dtstring, 0) * 1000
398 + digit_at (dtstring, 1) * 100
399 + digit_at (dtstring, 2) * 10
400 + digit_at (dtstring, 3);
401 month = digit_at (dtstring, 4) * 10 + digit_at (dtstring, 5);
402 day = digit_at (dtstring, 6) * 10 + digit_at (dtstring, 7);
403
404 if (len == 11) {
405 ICalTime *itt;
406
407 itt = i_cal_time_new_null_time ();
408 i_cal_time_set_date (itt, year, month, day);
409 i_cal_time_set_timezone (itt, i_cal_timezone_get_utc_timezone ());
410 i_cal_time_set_is_date (itt, TRUE);
411
412 t = i_cal_time_as_timet_with_zone (itt, i_cal_timezone_get_utc_timezone ());
413
414 g_object_unref (itt);
415 } else {
416 GDate date;
417 struct tm tt;
418
419 g_date_clear (&date, 1);
420 g_date_set_year (&date, year);
421 g_date_set_month (&date, month);
422 g_date_set_day (&date, day);
423
424 g_date_to_struct_tm (&date, &tt);
425 t = mktime (&tt);
426 }
427 } else
428 g_warning ("%s: Could not parse the string '%s'", G_STRFUNC, dtstring ? dtstring : "[null]");
429
430 g_free (dtstring);
431
432 return t;
433 }
434
435 static void
436 parse_extended_property (EEwsItemPrivate *priv,
437 ESoapParameter *param)
438 {
439 EEwsMessageDataType data_type;
440 ESoapParameter *subparam;
441 gchar *str, *setid, *name, *value;
442 guint32 tag = 0;
443
444 subparam = e_soap_parameter_get_first_child_by_name (param, "ExtendedFieldURI");
445 if (!subparam)
446 return;
447
448 str = e_soap_parameter_get_property (subparam, "PropertyType");
449 if (!str)
450 return;
451
452 /* We only handle some MAPI properties for now... */
453 if (g_ascii_strcasecmp (str, "Boolean") == 0) {
454 data_type = E_EWS_MESSAGE_DATA_TYPE_BOOLEAN;
455 } else if (g_ascii_strcasecmp (str, "Integer") == 0) {
456 data_type = E_EWS_MESSAGE_DATA_TYPE_INT;
457 } else if (g_ascii_strcasecmp (str, "Double") == 0) {
458 data_type = E_EWS_MESSAGE_DATA_TYPE_DOUBLE;
459 } else if (g_ascii_strcasecmp (str, "String") == 0) {
460 data_type = E_EWS_MESSAGE_DATA_TYPE_STRING;
461 } else if (g_ascii_strcasecmp (str, "SystemTime") == 0) {
462 data_type = E_EWS_MESSAGE_DATA_TYPE_TIME;
463 } else {
464 g_free (str);
465 return;
466 }
467 g_free (str);
468
469 name = e_soap_parameter_get_property (subparam, "PropertyName");
470 if (!name) {
471 str = e_soap_parameter_get_property (subparam, "PropertyTag");
472 if (!str) {
473 str = e_soap_parameter_get_property (subparam, "PropertyId");
474 if (!str)
475 return;
476 }
477
478 tag = strtol (str, NULL, 0);
479 g_free (str);
480 }
481
482 setid = e_soap_parameter_get_property (subparam, "DistinguishedPropertySetId");
483
484 subparam = e_soap_parameter_get_first_child_by_name (param, "Value");
485 if (!subparam) {
486 g_free (setid);
487 g_free (name);
488 return;
489 }
490
491 value = e_soap_parameter_get_string_value (subparam);
492 if (!value) {
493 g_free (setid);
494 g_free (name);
495 return;
496 }
497
498 if (data_type == E_EWS_MESSAGE_DATA_TYPE_INT) {
499 guint32 num_value;
500
501 num_value = strtol (value, NULL, 0);
502
503 switch (tag) {
504 case 0x01080: /* PidTagIconIndex */
505 priv->mapi_icon_index = num_value;
506 break;
507
508 case 0x1081: /* PidTagLastVerbExecuted */
509 priv->mapi_last_verb_executed = num_value;
510 break;
511
512 case 0x0e07: /* PidTagMessageFlags */
513 priv->mapi_message_flags = num_value;
514 break;
515
516 case 0x0e17: /* PidTagMessageStatus */
517 priv->mapi_message_status = num_value;
518 break;
519 }
520 }
521
522 if (setid) {
523 if (g_strcmp0 (name, "EvolutionEWSStartTimeZone") == 0) {
524 priv->iana_start_time_zone = g_strdup (value);
525 } else if (g_strcmp0 (name, "EvolutionEWSEndTimeZone") == 0) {
526 priv->iana_end_time_zone = g_strdup (value);
527 } else if (g_strcmp0 (name, "EvolutionEWSURL") == 0) {
528 priv->event_url = value && *value ? g_strdup (value) : NULL;
529 } else {
530 GHashTable *set_hash = g_hash_table_lookup (priv->mapi_extended_sets, setid);
531
532 if (!set_hash) {
533 set_hash = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
534 g_hash_table_insert (priv->mapi_extended_sets, setid, set_hash);
535 setid = NULL;
536 }
537
538 g_hash_table_insert (set_hash, GUINT_TO_POINTER (tag), g_strdup (value));
539 }
540 } else if (tag != 0) {
541 g_hash_table_insert (priv->mapi_extended_tags, GUINT_TO_POINTER (tag), g_strdup (value));
542 }
543
544 g_free (setid);
545 g_free (value);
546 g_free (name);
547 }
548
549 static void
550 parse_categories (EEwsItemPrivate *priv,
551 ESoapParameter *param)
552 {
553 gchar *value;
554 ESoapParameter *subparam;
555
556 /* release all the old data (if any) */
557 g_slist_free_full (priv->categories, g_free);
558 priv->categories = NULL;
559
560 /* categories are an array of <string> */
561 for (subparam = e_soap_parameter_get_first_child (param);
562 subparam != NULL;
563 subparam = e_soap_parameter_get_next_child (subparam)) {
564 value = e_soap_parameter_get_string_value (subparam);
565
566 priv->categories = g_slist_append (priv->categories, value);
567 }
568
569 }
570
571 static EwsImportance
572 parse_importance (ESoapParameter *param)
573 {
574 gchar *value;
575 EwsImportance importance = EWS_ITEM_LOW;
576
577 value = e_soap_parameter_get_string_value (param);
578
579 if (!g_ascii_strcasecmp (value, "Normal"))
580 importance = EWS_ITEM_NORMAL;
581 else if (!g_ascii_strcasecmp (value, "High") )
582 importance = EWS_ITEM_HIGH;
583
584 g_free (value);
585 return importance;
586 }
587
588 static void
589 process_modified_occurrences (EEwsItemPrivate *priv,
590 ESoapParameter *param)
591 {
592 ESoapParameter *subparam, *subparam1;
593 gchar *modified_occurrence_id;
594
595 for (subparam = e_soap_parameter_get_first_child (param); subparam != NULL; subparam = e_soap_parameter_get_next_child (subparam)) {
596
597 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "ItemId");
598 modified_occurrence_id = e_soap_parameter_get_property (subparam1, "Id");
599 priv->modified_occurrences = g_slist_append (priv->modified_occurrences, modified_occurrence_id);
600 }
601
602 return;
603 }
604
605 static void
606 process_attachments_list (EEwsItemPrivate *priv,
607 ESoapParameter *param)
608 {
609 ESoapParameter *subparam, *subparam1;
610
611 GSList *ids = NULL;
612
613 for (subparam = e_soap_parameter_get_first_child (param); subparam != NULL; subparam = e_soap_parameter_get_next_child (subparam)) {
614 gchar *id;
615
616 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "AttachmentId");
617 id = e_soap_parameter_get_property (subparam1, "Id");
618
619 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "IsContactPhoto");
620 if (subparam1) {
621 gchar *value = e_soap_parameter_get_string_value (subparam1);
622 if (g_strcmp0 (value, "true") == 0) {
623 priv->contact_photo_id = id;
624 g_free (value);
625 continue;
626 }
627 g_free (value);
628 }
629
630 ids = g_slist_append (ids, id);
631 }
632
633 priv->attachments_ids = ids;
634 return;
635 }
636
637 static void
638 process_attendees (EEwsItemPrivate *priv,
639 ESoapParameter *param,
640 const gchar *type)
641 {
642 ESoapParameter *subparam, *subparam1;
643 EwsAttendee *attendee;
644
645 for (subparam = e_soap_parameter_get_first_child (param); subparam != NULL; subparam = e_soap_parameter_get_next_child (subparam)) {
646 EwsMailbox *mailbox = NULL;
647
648 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "Mailbox");
649 mailbox = e_ews_item_mailbox_from_soap_param (subparam1);
650 /* Ignore attendee if mailbox is not valid,
651 * for instance, ppl that does not exists any more */
652 if (!mailbox)
653 continue;
654
655 attendee = g_new0 (EwsAttendee, 1);
656
657 attendee->mailbox = mailbox;
658
659 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "ResponseType");
660 attendee->responsetype = subparam1 ? e_soap_parameter_get_string_value (subparam1) : NULL;
661
662 attendee->attendeetype = (gchar *) type;
663
664 priv->attendees = g_slist_append (priv->attendees, attendee);
665 }
666
667 return;
668 }
669
670 static void
671 parse_complete_name (struct _EEwsContactFields *con_fields,
672 ESoapParameter *param)
673 {
674 ESoapParameter *subparam;
675 EwsCompleteName *cn;
676
677 cn = g_new0 (EwsCompleteName, 1);
678
679 subparam = e_soap_parameter_get_first_child_by_name (param, "Title");
680 if (subparam)
681 cn->title = e_soap_parameter_get_string_value (subparam);
682 subparam = e_soap_parameter_get_first_child_by_name (param, "FirstName");
683 if (subparam)
684 cn->first_name = e_soap_parameter_get_string_value (subparam);
685 subparam = e_soap_parameter_get_first_child_by_name (param, "MiddleName");
686 if (subparam)
687 cn->middle_name = e_soap_parameter_get_string_value (subparam);
688 subparam = e_soap_parameter_get_first_child_by_name (param, "LastName");
689 if (subparam)
690 cn->last_name = e_soap_parameter_get_string_value (subparam);
691 subparam = e_soap_parameter_get_first_child_by_name (param, "Suffix");
692 if (subparam)
693 cn->suffix = e_soap_parameter_get_string_value (subparam);
694 subparam = e_soap_parameter_get_first_child_by_name (param, "Initials");
695 if (subparam)
696 cn->initials = e_soap_parameter_get_string_value (subparam);
697 subparam = e_soap_parameter_get_first_child_by_name (param, "FullName");
698 if (subparam)
699 cn->full_name = e_soap_parameter_get_string_value (subparam);
700 subparam = e_soap_parameter_get_first_child_by_name (param, "Nickname");
701 if (subparam)
702 cn->nick_name = e_soap_parameter_get_string_value (subparam);
703 subparam = e_soap_parameter_get_first_child_by_name (param, "YomiFirstName");
704 if (subparam)
705 cn->yomi_first_name = e_soap_parameter_get_string_value (subparam);
706 subparam = e_soap_parameter_get_first_child_by_name (param, "YomiLastName");
707 if (subparam)
708 cn->yomi_last_name = e_soap_parameter_get_string_value (subparam);
709
710 con_fields->complete_name = cn;
711 }
712
713 static gpointer
714 ews_get_physical_address (ESoapParameter *param)
715 {
716 ESoapParameter *subparam;
717 EwsAddress *address;
718
719 address = g_new0 (EwsAddress, 1);
720
721 subparam = e_soap_parameter_get_first_child_by_name (param, "Street");
722 if (subparam)
723 address->street = e_soap_parameter_get_string_value (subparam);
724
725 subparam = e_soap_parameter_get_first_child_by_name (param, "City");
726 if (subparam)
727 address->city = e_soap_parameter_get_string_value (subparam);
728
729 subparam = e_soap_parameter_get_first_child_by_name (param, "State");
730 if (subparam)
731 address->state = e_soap_parameter_get_string_value (subparam);
732
733 subparam = e_soap_parameter_get_first_child_by_name (param, "CountryOrRegion");
734 if (subparam)
735 address->country = e_soap_parameter_get_string_value (subparam);
736
737 subparam = e_soap_parameter_get_first_child_by_name (param, "PostalCode");
738 if (subparam)
739 address->postal_code = e_soap_parameter_get_string_value (subparam);
740
741 return address;
742 }
743
744 static void
745 parse_entries (GHashTable *hash_table,
746 ESoapParameter *param,
747 EwsGetValFunc get_val_func)
748 {
749 ESoapParameter *subparam;
750
751 for (subparam = e_soap_parameter_get_first_child_by_name (param, "Entry");
752 subparam != NULL;
753 subparam = e_soap_parameter_get_next_child_by_name (subparam, "Entry")) {
754 gchar *key;
755 gpointer value;
756
757 key = e_soap_parameter_get_property (subparam, "Key");
758 value = get_val_func (subparam);
759
760 if (value)
761 g_hash_table_insert (hash_table, key, value);
762 else
763 g_free (key);
764 }
765 }
766
767 static void
768 ews_free_physical_address (gpointer value)
769 {
770 EwsAddress *address = (EwsAddress *) value;
771
772 if (address) {
773 g_free (address->street);
774 g_free (address->city);
775 g_free (address->state);
776 g_free (address->country);
777 g_free (address->postal_code);
778 g_free (address);
779 }
780 }
781
782 static void
783 parse_contact_field (EEwsItem *item,
784 const gchar *name,
785 ESoapParameter *subparam)
786 {
787 EEwsItemPrivate *priv = item->priv;
788
789 if (!g_ascii_strcasecmp (name, "Culture")) {
790 priv->contact_fields->culture = e_soap_parameter_get_string_value (subparam);
791 } else if (!g_ascii_strcasecmp (name, "DisplayName")) {
792 priv->contact_fields->display_name = e_soap_parameter_get_string_value (subparam);
793 } else if (!g_ascii_strcasecmp (name, "FileAs")) {
794 priv->contact_fields->fileas = e_soap_parameter_get_string_value (subparam);
795 } else if (!g_ascii_strcasecmp (name, "CompleteName")) {
796 parse_complete_name (priv->contact_fields, subparam);
797 } else if (!g_ascii_strcasecmp (name, "CompanyName")) {
798 priv->contact_fields->company_name = e_soap_parameter_get_string_value (subparam);
799 } else if (!g_ascii_strcasecmp (name, "EmailAddresses")) {
800 priv->contact_fields->email_addresses = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
801 parse_entries (priv->contact_fields->email_addresses, subparam, (EwsGetValFunc) e_soap_parameter_get_string_value);
802 } else if (!g_ascii_strcasecmp (name, "PhysicalAddresses")) {
803 priv->contact_fields->physical_addresses = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, ews_free_physical_address);
804 parse_entries (priv->contact_fields->physical_addresses, subparam, ews_get_physical_address);
805 } else if (!g_ascii_strcasecmp (name, "PhoneNumbers")) {
806 priv->contact_fields->phone_numbers = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
807 parse_entries (priv->contact_fields->phone_numbers, subparam, (EwsGetValFunc) e_soap_parameter_get_string_value);
808 } else if (!g_ascii_strcasecmp (name, "AssistantName")) {
809 priv->contact_fields->assistant_name = e_soap_parameter_get_string_value (subparam);
810 } else if (!g_ascii_strcasecmp (name, "Birthday")) {
811 priv->contact_fields->has_birthday = TRUE;
812 priv->contact_fields->birthday = ews_item_parse_date (subparam);
813 } else if (!g_ascii_strcasecmp (name, "BusinessHomePage")) {
814 priv->contact_fields->business_homepage = e_soap_parameter_get_string_value (subparam);
815 } else if (!g_ascii_strcasecmp (name, "Department")) {
816 priv->contact_fields->department = e_soap_parameter_get_string_value (subparam);
817 } else if (!g_ascii_strcasecmp (name, "ImAddresses")) {
818 priv->contact_fields->im_addresses = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
819 parse_entries (priv->contact_fields->im_addresses, subparam, (EwsGetValFunc) e_soap_parameter_get_string_value);
820 } else if (!g_ascii_strcasecmp (name, "JobTitle")) {
821 priv->contact_fields->job_title = e_soap_parameter_get_string_value (subparam);
822 } else if (!g_ascii_strcasecmp (name, "Manager")) {
823 priv->contact_fields->manager = e_soap_parameter_get_string_value (subparam);
824 } else if (!g_ascii_strcasecmp (name, "OfficeLocation")) {
825 priv->contact_fields->office_location = e_soap_parameter_get_string_value (subparam);
826 } else if (!g_ascii_strcasecmp (name, "Profession")) {
827 priv->contact_fields->profession = e_soap_parameter_get_string_value (subparam);
828 } else if (!g_ascii_strcasecmp (name, "SpouseName")) {
829 priv->contact_fields->spouse_name = e_soap_parameter_get_string_value (subparam);
830 } else if (!g_ascii_strcasecmp (name, "Surname")) {
831 priv->contact_fields->surname = e_soap_parameter_get_string_value (subparam);
832 } else if (!g_ascii_strcasecmp (name, "GivenName")) {
833 priv->contact_fields->givenname = e_soap_parameter_get_string_value (subparam);
834 } else if (!g_ascii_strcasecmp (name, "MiddleName")) {
835 priv->contact_fields->middlename = e_soap_parameter_get_string_value (subparam);
836 } else if (!g_ascii_strcasecmp (name, "WeddingAnniversary")) {
837 priv->contact_fields->has_wedding_anniversary = TRUE;
838 priv->contact_fields->wedding_anniversary = ews_item_parse_date (subparam);
839 } else if (!g_ascii_strcasecmp (name, "Body")) {
840 /*
841 * For Exchange versions >= 2010_SP2 Notes property can be get
842 * directly from contacts:Notes. But for backward compatibility
843 * with old servers (< 2010_SP2) we prefer use item:Body.
844 */
845 priv->contact_fields->notes = e_soap_parameter_get_string_value (subparam);
846 } else if (!g_ascii_strcasecmp (name, "UserSMIMECertificate") ||
847 !g_ascii_strcasecmp (name, "MSExchangeCertificate")) {
848 ESoapParameter *data_param;
849 guchar **out_bytes;
850 gsize *out_len;
851
852 if (!g_ascii_strcasecmp (name, "UserSMIMECertificate")) {
853 out_bytes = &priv->contact_fields->user_cert;
854 out_len = &priv->contact_fields->user_cert_len;
855 } else {
856 out_bytes = &priv->contact_fields->msexchange_cert;
857 out_len = &priv->contact_fields->msexchange_cert_len;
858 }
859
860 data_param = e_soap_parameter_get_first_child_by_name (subparam, "Base64Binary");
861 if (data_param) {
862 gchar *base64_data;
863
864 base64_data = e_soap_parameter_get_string_value (data_param);
865 if (base64_data && *base64_data) {
866 *out_bytes = g_base64_decode_inplace (base64_data, out_len);
867 if (!*out_len) {
868 g_free (*out_bytes);
869
870 *out_len = 0;
871 *out_bytes = NULL;
872 }
873 } else {
874 g_free (base64_data);
875 }
876 }
877 }
878 }
879
880 static guint32 /* bit-or of EEwsRecurrenceDaysOfWeek */
881 parse_recur_days_of_week (ESoapParameter *param)
882 {
883 struct _keys {
884 const gchar *str_value;
885 EEwsRecurrenceDaysOfWeek bit_value;
886 } keys[] = {
887 /* Do not localize, these are values used in XML */
888 { "Sunday", E_EWS_RECURRENCE_DAYS_OF_WEEK_SUNDAY },
889 { "Monday", E_EWS_RECURRENCE_DAYS_OF_WEEK_MONDAY },
890 { "Tuesday", E_EWS_RECURRENCE_DAYS_OF_WEEK_TUESDAY },
891 { "Wednesday", E_EWS_RECURRENCE_DAYS_OF_WEEK_WEDNESDAY },
892 { "Thursday", E_EWS_RECURRENCE_DAYS_OF_WEEK_THURSDAY },
893 { "Friday", E_EWS_RECURRENCE_DAYS_OF_WEEK_FRIDAY },
894 { "Saturday", E_EWS_RECURRENCE_DAYS_OF_WEEK_SATURDAY },
895 { "Day", E_EWS_RECURRENCE_DAYS_OF_WEEK_DAY },
896 { "Weekday", E_EWS_RECURRENCE_DAYS_OF_WEEK_WEEKDAY },
897 { "WeekendDay", E_EWS_RECURRENCE_DAYS_OF_WEEK_WEEKENDDAY }
898 };
899 gchar *value, **split_value;
900 guint32 days_of_week = 0;
901 gint ii, jj;
902
903 g_return_val_if_fail (param != NULL, E_EWS_RECURRENCE_DAYS_OF_WEEK_UNKNOWN);
904
905 value = e_soap_parameter_get_string_value (param);
906 if (!value || !*value) {
907 g_free (value);
908 return E_EWS_RECURRENCE_DAYS_OF_WEEK_UNKNOWN;
909 }
910
911 split_value = g_strsplit (value, " ", -1);
912
913 for (ii = 0; split_value && split_value[ii]; ii++) {
914 const gchar *str = split_value[ii];
915
916 if (!str || !*str)
917 continue;
918
919 for (jj = 0; jj < G_N_ELEMENTS (keys); jj++) {
920 if (g_strcmp0 (str, keys[jj].str_value) == 0) {
921 days_of_week |= keys[jj].bit_value;
922 break;
923 }
924 }
925 }
926
927 g_strfreev (split_value);
928 g_free (value);
929
930 return days_of_week;
931 }
932
933 static EEwsRecurrenceDayOfWeekIndex
934 parse_recur_day_of_week_index (ESoapParameter *param)
935 {
936 EEwsRecurrenceDayOfWeekIndex day_of_week_index = E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_UNKNOWN;
937 gchar *value;
938
939 g_return_val_if_fail (param != NULL, E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_UNKNOWN);
940
941 value = e_soap_parameter_get_string_value (param);
942 if (!value || !*value) {
943 g_free (value);
944 return E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_UNKNOWN;
945 }
946
947 /* Do not localize, these are values used in XML */
948 if (g_strcmp0 (value, "First") == 0)
949 day_of_week_index = E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_FIRST;
950 else if (g_strcmp0 (value, "Second") == 0)
951 day_of_week_index = E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_SECOND;
952 else if (g_strcmp0 (value, "Third") == 0)
953 day_of_week_index = E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_THIRD;
954 else if (g_strcmp0 (value, "Fourth") == 0)
955 day_of_week_index = E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_FOURTH;
956 else if (g_strcmp0 (value, "Last") == 0)
957 day_of_week_index = E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_LAST;
958
959 g_free (value);
960
961 return day_of_week_index;
962 }
963
964 static GDateMonth
965 parse_recur_month (ESoapParameter *param)
966 {
967 GDateMonth month = G_DATE_BAD_MONTH;
968 gchar *value;
969
970 g_return_val_if_fail (param != NULL, G_DATE_BAD_MONTH);
971
972 value = e_soap_parameter_get_string_value (param);
973 if (!value || !*value) {
974 g_free (value);
975 return G_DATE_BAD_MONTH;
976 }
977
978 /* Do not localize, these are values used in XML */
979 if (g_strcmp0 (value, "January") == 0)
980 month = G_DATE_JANUARY;
981 else if (g_strcmp0 (value, "February") == 0)
982 month = G_DATE_FEBRUARY;
983 else if (g_strcmp0 (value, "March") == 0)
984 month = G_DATE_MARCH;
985 else if (g_strcmp0 (value, "April") == 0)
986 month = G_DATE_APRIL;
987 else if (g_strcmp0 (value, "May") == 0)
988 month = G_DATE_MAY;
989 else if (g_strcmp0 (value, "June") == 0)
990 month = G_DATE_JUNE;
991 else if (g_strcmp0 (value, "July") == 0)
992 month = G_DATE_JULY;
993 else if (g_strcmp0 (value, "August") == 0)
994 month = G_DATE_AUGUST;
995 else if (g_strcmp0 (value, "September") == 0)
996 month = G_DATE_SEPTEMBER;
997 else if (g_strcmp0 (value, "October") == 0)
998 month = G_DATE_OCTOBER;
999 else if (g_strcmp0 (value, "November") == 0)
1000 month = G_DATE_NOVEMBER;
1001 else if (g_strcmp0 (value, "December") == 0)
1002 month = G_DATE_DECEMBER;
1003
1004 g_free (value);
1005
1006 return month;
1007 }
1008
1009 static GDateWeekday
1010 parse_recur_first_day_of_week (ESoapParameter *param)
1011 {
1012 GDateWeekday first_day_of_week = G_DATE_BAD_WEEKDAY;
1013 gchar *value;
1014
1015 g_return_val_if_fail (param != NULL, G_DATE_BAD_WEEKDAY);
1016
1017 value = e_soap_parameter_get_string_value (param);
1018 if (!value || !*value) {
1019 g_free (value);
1020 return G_DATE_BAD_WEEKDAY;
1021 }
1022
1023 /* Do not localize, these are values used in XML */
1024 if (g_strcmp0 (value, "Sunday") == 0)
1025 first_day_of_week = G_DATE_SUNDAY;
1026 else if (g_strcmp0 (value, "Monday") == 0)
1027 first_day_of_week = G_DATE_MONDAY;
1028 else if (g_strcmp0 (value, "Tuesday") == 0)
1029 first_day_of_week = G_DATE_TUESDAY;
1030 else if (g_strcmp0 (value, "Wednesday") == 0)
1031 first_day_of_week = G_DATE_WEDNESDAY;
1032 else if (g_strcmp0 (value, "Thursday") == 0)
1033 first_day_of_week = G_DATE_THURSDAY;
1034 else if (g_strcmp0 (value, "Friday") == 0)
1035 first_day_of_week = G_DATE_FRIDAY;
1036 else if (g_strcmp0 (value, "Saturday") == 0)
1037 first_day_of_week = G_DATE_SATURDAY;
1038
1039 g_free (value);
1040
1041 return first_day_of_week;
1042 }
1043
1044 static void
1045 parse_recurrence_field (EEwsItem *item,
1046 ESoapParameter *param)
1047 {
1048 ESoapParameter *subparam, *subparam1;
1049
1050 g_return_if_fail (item != NULL);
1051 g_return_if_fail (param != NULL);
1052
1053 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1054 item->priv->recurrence.end_type = E_EWS_RECURRENCE_END_UNKNOWN;
1055
1056 if ((subparam = e_soap_parameter_get_first_child_by_name (param, "RelativeYearlyRecurrence")) != NULL) {
1057 item->priv->recurrence.type = E_EWS_RECURRENCE_RELATIVE_YEARLY;
1058 item->priv->recurrence.recur.relative_yearly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_UNKNOWN;
1059 item->priv->recurrence.recur.relative_yearly.day_of_week_index = E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_UNKNOWN;
1060 item->priv->recurrence.recur.relative_yearly.month = G_DATE_BAD_MONTH;
1061
1062 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "DaysOfWeek");
1063 if (subparam1) {
1064 item->priv->recurrence.recur.relative_yearly.days_of_week = parse_recur_days_of_week (subparam1);
1065
1066 if (item->priv->recurrence.recur.relative_yearly.days_of_week == E_EWS_RECURRENCE_DAYS_OF_WEEK_UNKNOWN)
1067 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1068 }
1069
1070 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "DayOfWeekIndex");
1071 if (subparam1) {
1072 item->priv->recurrence.recur.relative_yearly.day_of_week_index = parse_recur_day_of_week_index (subparam1);
1073
1074 if (item->priv->recurrence.recur.relative_yearly.day_of_week_index == E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_UNKNOWN)
1075 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1076 }
1077
1078 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "Month");
1079 if (subparam1) {
1080 item->priv->recurrence.recur.relative_yearly.month = parse_recur_month (subparam1);
1081
1082 if (item->priv->recurrence.recur.relative_yearly.month == G_DATE_BAD_MONTH)
1083 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1084 }
1085 } else if ((subparam = e_soap_parameter_get_first_child_by_name (param, "AbsoluteYearlyRecurrence")) != NULL) {
1086 item->priv->recurrence.type = E_EWS_RECURRENCE_ABSOLUTE_YEARLY;
1087 item->priv->recurrence.recur.absolute_yearly.day_of_month = 0;
1088 item->priv->recurrence.recur.absolute_yearly.month = G_DATE_BAD_MONTH;
1089
1090 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "DayOfMonth");
1091 if (subparam1) {
1092 item->priv->recurrence.recur.absolute_yearly.day_of_month = e_soap_parameter_get_int_value (subparam1);
1093
1094 if (item->priv->recurrence.recur.absolute_yearly.day_of_month < 1 ||
1095 item->priv->recurrence.recur.absolute_yearly.day_of_month > 31)
1096 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1097 } else {
1098 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1099 }
1100
1101 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "Month");
1102 if (subparam1) {
1103 item->priv->recurrence.recur.absolute_yearly.month = parse_recur_month (subparam1);
1104
1105 if (item->priv->recurrence.recur.absolute_yearly.month == G_DATE_BAD_MONTH)
1106 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1107 } else {
1108 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1109 }
1110 } else if ((subparam = e_soap_parameter_get_first_child_by_name (param, "RelativeMonthlyRecurrence")) != NULL) {
1111 item->priv->recurrence.type = E_EWS_RECURRENCE_RELATIVE_MONTHLY;
1112 item->priv->recurrence.recur.relative_monthly.interval = 0;
1113 item->priv->recurrence.recur.relative_monthly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_UNKNOWN;
1114 item->priv->recurrence.recur.relative_monthly.day_of_week_index = E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_UNKNOWN;
1115
1116 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "Interval");
1117 if (subparam1) {
1118 item->priv->recurrence.recur.relative_monthly.interval = e_soap_parameter_get_int_value (subparam1);
1119
1120 if (item->priv->recurrence.recur.relative_monthly.interval < 1)
1121 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1122 } else {
1123 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1124 }
1125
1126 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "DaysOfWeek");
1127 if (subparam1) {
1128 item->priv->recurrence.recur.relative_monthly.days_of_week = parse_recur_days_of_week (subparam1);
1129
1130 if (item->priv->recurrence.recur.relative_monthly.days_of_week == E_EWS_RECURRENCE_DAYS_OF_WEEK_UNKNOWN)
1131 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1132 } else {
1133 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1134 }
1135
1136 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "DayOfWeekIndex");
1137 if (subparam1) {
1138 item->priv->recurrence.recur.relative_monthly.day_of_week_index = parse_recur_day_of_week_index (subparam1);
1139
1140 if (item->priv->recurrence.recur.relative_monthly.day_of_week_index == E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_UNKNOWN)
1141 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1142 } else {
1143 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1144 }
1145 } else if ((subparam = e_soap_parameter_get_first_child_by_name (param, "AbsoluteMonthlyRecurrence")) != NULL) {
1146 item->priv->recurrence.type = E_EWS_RECURRENCE_ABSOLUTE_MONTHLY;
1147 item->priv->recurrence.recur.absolute_monthly.interval = 0;
1148 item->priv->recurrence.recur.absolute_monthly.day_of_month = 0;
1149
1150 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "Interval");
1151 if (subparam1) {
1152 item->priv->recurrence.recur.absolute_monthly.interval = e_soap_parameter_get_int_value (subparam1);
1153
1154 if (item->priv->recurrence.recur.absolute_monthly.interval < 1)
1155 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1156 } else {
1157 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1158 }
1159
1160 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "DayOfMonth");
1161 if (subparam1) {
1162 item->priv->recurrence.recur.absolute_monthly.day_of_month = e_soap_parameter_get_int_value (subparam1);
1163
1164 if (item->priv->recurrence.recur.absolute_monthly.day_of_month < 1 ||
1165 item->priv->recurrence.recur.absolute_monthly.day_of_month > 31)
1166 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1167 } else {
1168 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1169 }
1170 } else if ((subparam = e_soap_parameter_get_first_child_by_name (param, "WeeklyRecurrence")) != NULL) {
1171 item->priv->recurrence.type = E_EWS_RECURRENCE_WEEKLY;
1172 item->priv->recurrence.recur.weekly.interval = 0;
1173 item->priv->recurrence.recur.weekly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_UNKNOWN;
1174 item->priv->recurrence.recur.weekly.first_day_of_week = G_DATE_BAD_WEEKDAY;
1175
1176 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "Interval");
1177 if (subparam1) {
1178 item->priv->recurrence.recur.weekly.interval = e_soap_parameter_get_int_value (subparam1);
1179
1180 if (item->priv->recurrence.recur.weekly.interval < 1)
1181 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1182 } else {
1183 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1184 }
1185
1186 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "DaysOfWeek");
1187 if (subparam1) {
1188 item->priv->recurrence.recur.weekly.days_of_week = parse_recur_days_of_week (subparam1);
1189
1190 if (item->priv->recurrence.recur.weekly.days_of_week == E_EWS_RECURRENCE_DAYS_OF_WEEK_UNKNOWN)
1191 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1192 } else {
1193 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1194 }
1195
1196 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "FirstDayOfWeek");
1197 if (subparam1) {
1198 item->priv->recurrence.recur.weekly.first_day_of_week = parse_recur_first_day_of_week (subparam1);
1199
1200 if (item->priv->recurrence.recur.weekly.first_day_of_week == G_DATE_BAD_WEEKDAY)
1201 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1202 } else {
1203 /* It's okay, because 2007 doesn't support it */
1204 }
1205 } else if ((subparam = e_soap_parameter_get_first_child_by_name (param, "DailyRecurrence")) != NULL) {
1206 item->priv->recurrence.type = E_EWS_RECURRENCE_DAILY;
1207 item->priv->recurrence.recur.interval = 0;
1208
1209 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "Interval");
1210 if (subparam1) {
1211 item->priv->recurrence.recur.interval = e_soap_parameter_get_int_value (subparam1);
1212
1213 if (item->priv->recurrence.recur.interval < 1)
1214 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1215 } else {
1216 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1217 }
1218 } else if ((subparam = e_soap_parameter_get_first_child_by_name (param, "DailyRegeneration")) != NULL) {
1219 item->priv->recurrence.type = E_EWS_RECURRENCE_DAILY_REGENERATION;
1220 item->priv->recurrence.recur.interval = 0;
1221
1222 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "Interval");
1223 if (subparam1) {
1224 item->priv->recurrence.recur.interval = e_soap_parameter_get_int_value (subparam1);
1225
1226 if (item->priv->recurrence.recur.interval < 1)
1227 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1228 } else {
1229 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1230 }
1231 } else if ((subparam = e_soap_parameter_get_first_child_by_name (param, "WeeklyRegeneration")) != NULL) {
1232 item->priv->recurrence.type = E_EWS_RECURRENCE_WEEKLY_REGENERATION;
1233 item->priv->recurrence.recur.interval = 0;
1234
1235 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "Interval");
1236 if (subparam1) {
1237 item->priv->recurrence.recur.interval = e_soap_parameter_get_int_value (subparam1);
1238
1239 if (item->priv->recurrence.recur.interval < 1)
1240 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1241 } else {
1242 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1243 }
1244 } else if ((subparam = e_soap_parameter_get_first_child_by_name (param, "MonthlyRegeneration")) != NULL) {
1245 item->priv->recurrence.type = E_EWS_RECURRENCE_MONTHLY_REGENERATION;
1246 item->priv->recurrence.recur.interval = 0;
1247
1248 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "Interval");
1249 if (subparam1) {
1250 item->priv->recurrence.recur.interval = e_soap_parameter_get_int_value (subparam1);
1251
1252 if (item->priv->recurrence.recur.interval < 1)
1253 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1254 } else {
1255 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1256 }
1257 } else if ((subparam = e_soap_parameter_get_first_child_by_name (param, "YearlyRegeneration")) != NULL) {
1258 item->priv->recurrence.type = E_EWS_RECURRENCE_YEARLY_REGENERATION;
1259 item->priv->recurrence.recur.interval = 0;
1260
1261 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "Interval");
1262 if (subparam1) {
1263 item->priv->recurrence.recur.interval = e_soap_parameter_get_int_value (subparam1);
1264
1265 if (item->priv->recurrence.recur.interval < 1)
1266 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1267 } else {
1268 item->priv->recurrence.type = E_EWS_RECURRENCE_UNKNOWN;
1269 }
1270 }
1271
1272 if (item->priv->recurrence.type != E_EWS_RECURRENCE_UNKNOWN) {
1273 if ((subparam = e_soap_parameter_get_first_child_by_name (param, "NoEndRecurrence")) != NULL) {
1274 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "StartDate");
1275 if (subparam1) {
1276 item->priv->recurrence.end_type = E_EWS_RECURRENCE_END_NO_END;
1277 item->priv->recurrence.utc_start_date = ews_item_parse_date (subparam1);
1278
1279 if (item->priv->recurrence.utc_start_date == (time_t) -1)
1280 item->priv->recurrence.end_type = E_EWS_RECURRENCE_END_UNKNOWN;
1281 }
1282 } else if ((subparam = e_soap_parameter_get_first_child_by_name (param, "EndDateRecurrence")) != NULL) {
1283 item->priv->recurrence.end_type = E_EWS_RECURRENCE_END_DATE;
1284
1285 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "StartDate");
1286 if (subparam1) {
1287 item->priv->recurrence.utc_start_date = ews_item_parse_date (subparam1);
1288
1289 if (item->priv->recurrence.utc_start_date == (time_t) -1)
1290 item->priv->recurrence.end_type = E_EWS_RECURRENCE_END_UNKNOWN;
1291 } else {
1292 item->priv->recurrence.end_type = E_EWS_RECURRENCE_END_UNKNOWN;
1293 }
1294
1295 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "EndDate");
1296 if (subparam1) {
1297 item->priv->recurrence.end.utc_end_date = ews_item_parse_date (subparam1);
1298
1299 if (item->priv->recurrence.end.utc_end_date == (time_t) -1)
1300 item->priv->recurrence.end_type = E_EWS_RECURRENCE_END_UNKNOWN;
1301 } else {
1302 item->priv->recurrence.end_type = E_EWS_RECURRENCE_END_UNKNOWN;
1303 }
1304 } else if ((subparam = e_soap_parameter_get_first_child_by_name (param, "NumberedRecurrence")) != NULL) {
1305 item->priv->recurrence.end_type = E_EWS_RECURRENCE_END_NUMBERED;
1306
1307 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "StartDate");
1308 if (subparam1) {
1309 item->priv->recurrence.utc_start_date = ews_item_parse_date (subparam1);
1310
1311 if (item->priv->recurrence.utc_start_date == (time_t) -1)
1312 item->priv->recurrence.end_type = E_EWS_RECURRENCE_END_UNKNOWN;
1313 } else {
1314 item->priv->recurrence.end_type = E_EWS_RECURRENCE_END_UNKNOWN;
1315 }
1316
1317 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "NumberOfOccurrences");
1318 if (subparam1) {
1319 item->priv->recurrence.end.number_of_occurrences = e_soap_parameter_get_int_value (subparam1);
1320
1321 if (item->priv->recurrence.end.number_of_occurrences < 1)
1322 item->priv->recurrence.end_type = E_EWS_RECURRENCE_END_UNKNOWN;
1323 } else {
1324 item->priv->recurrence.end_type = E_EWS_RECURRENCE_END_UNKNOWN;
1325 }
1326 }
1327 }
1328
1329 g_warn_if_fail (
1330 (item->priv->recurrence.type == E_EWS_RECURRENCE_UNKNOWN && item->priv->recurrence.end_type == E_EWS_RECURRENCE_END_UNKNOWN) ||
1331 (item->priv->recurrence.type != E_EWS_RECURRENCE_UNKNOWN && item->priv->recurrence.end_type != E_EWS_RECURRENCE_END_UNKNOWN));
1332 }
1333
1334 static gchar *
1335 strip_html_tags (const gchar *html_text)
1336 {
1337 gssize haystack_len = strlen (html_text);
1338 gchar *plain_text = g_malloc (haystack_len + 1);
1339 gchar *start = g_strstr_len (html_text, haystack_len, "<body"),
1340 *end = g_strstr_len (html_text, haystack_len, "</body"),
1341 *i, *j;
1342
1343 if (!start || !end) {
1344 g_free (plain_text);
1345 return g_strdup (html_text);
1346 }
1347
1348 i = start;
1349 while (i < end && *i != '>') {
1350 i++;
1351 }
1352
1353 for (j = plain_text; i < end; i++) {
1354 if (*i == '&') {
1355 gchar *from = i;
1356
1357 while (i < end && *i != ';' && *i != '<' && *i != '>')
1358 i++;
1359
1360 if (i >= end)
1361 break;
1362
1363 if (*i != ';')
1364 i = from;
1365 else
1366 continue;
1367 }
1368
1369 if (*i == '<') {
1370 while (i < end && *i != '>')
1371 i++;
1372
1373 if (i >= end)
1374 break;
1375 } else {
1376 *j = *i;
1377 j++;
1378 }
1379 }
1380
1381 *j = '\0';
1382
1383 return plain_text;
1384 }
1385
1386 static void
1387 parse_task_field (EEwsItem *item,
1388 const gchar *name,
1389 ESoapParameter *subparam)
1390 {
1391 EEwsItemPrivate *priv = item->priv;
1392 gchar *value = NULL;
1393
1394 if (!g_ascii_strcasecmp (name, "Status")) {
1395 priv->task_fields->status = e_soap_parameter_get_string_value (subparam);
1396 } else if (!g_ascii_strcasecmp (name, "PercentComplete")) {
1397 priv->task_fields->percent_complete = e_soap_parameter_get_string_value (subparam);
1398 } else if (!g_ascii_strcasecmp (name, "DueDate")) {
1399 priv->task_fields->due_date = ews_item_parse_date (subparam);
1400 priv->task_fields->has_due_date = TRUE;
1401 } else if (!g_ascii_strcasecmp (name, "StartDate")) {
1402 priv->task_fields->start_date = ews_item_parse_date (subparam);
1403 priv->task_fields->has_start_date = TRUE;
1404 } else if (!g_ascii_strcasecmp (name, "CompleteDate")) {
1405 priv->task_fields->complete_date = ews_item_parse_date (subparam);
1406 priv->task_fields->has_complete_date = TRUE;
1407 } else if (!g_ascii_strcasecmp (name, "Sensitivity")) {
1408 priv->task_fields->sensitivity = e_soap_parameter_get_string_value (subparam);
1409 } else if (!g_ascii_strcasecmp (name, "Body")) {
1410 if (!g_ascii_strcasecmp (e_soap_parameter_get_property (subparam, "BodyType"),"HTML")) {
1411 value = e_soap_parameter_get_string_value (subparam);
1412 priv->task_fields->body = strip_html_tags (value);
1413 g_free (value);
1414 } else
1415 priv->task_fields->body = e_soap_parameter_get_string_value (subparam);
1416 } else if (!g_ascii_strcasecmp (name, "Owner")) {
1417 priv->task_fields->owner = e_soap_parameter_get_string_value (subparam);
1418 } else if (!g_ascii_strcasecmp (name, "Delegator")) {
1419 priv->task_fields->delegator = e_soap_parameter_get_string_value (subparam);
1420 if (!g_ascii_strcasecmp (priv->task_fields->delegator, "")) {
1421 g_free (priv->task_fields->delegator);
1422 priv->task_fields->delegator = NULL;
1423 }
1424 } else if (!g_ascii_strcasecmp (name, "Recurrence")) {
1425 parse_recurrence_field (item, subparam);
1426 }
1427 }
1428
1429 static gboolean
1430 e_ews_item_set_from_soap_parameter (EEwsItem *item,
1431 ESoapParameter *param)
1432 {
1433 EEwsItemPrivate *priv = item->priv;
1434 ESoapParameter *subparam, *node = NULL, *attach_id;
1435 const gchar *name;
1436
1437 g_return_val_if_fail (param != NULL, FALSE);
1438
1439 name = e_soap_parameter_get_name (param);
1440
1441 /*We get two types of response for items from server like below from two apis
1442 * Syncfolderitems and Finditem
1443 * <m:Changes> <t:Items>
1444 * <t:Create> <t:Contact>
1445 * <t:Contact> <t:ItemId Id="AS4AUn=" ChangeKey="fsVU4==" />
1446 * <t:ItemId Id="AAA=" ChangeKey="NAgws"/> </t:Contact>
1447 * </t:Contact> <t:Contact>
1448 * </t:Create> <t:ItemId Id="AS4BS=" ChangeKey="fjidU4==" />
1449 * <t:Contact> </t:Contact>
1450 * <t:ItemId Id="ABB=" ChangeKey="GCDab"/> ...
1451 * </t:Contact> </t:Items>
1452 * </t:Create>
1453 * ...
1454 * </m:Changes>
1455 * So check param is the node we want to use, by comparing name or is it child of the param */
1456
1457 if (!g_ascii_strcasecmp (name, "Message") || (node = e_soap_parameter_get_first_child_by_name (param, "Message"))) {
1458 priv->item_type = E_EWS_ITEM_TYPE_MESSAGE;
1459 subparam = e_soap_parameter_get_first_child_by_name (node ? node : param, "ItemClass");
1460 if (subparam) {
1461 gchar *folder_class = e_soap_parameter_get_string_value (subparam);
1462
1463 if (g_strcmp0 (folder_class, "IPM.StickyNote") == 0) {
1464 priv->item_type = E_EWS_ITEM_TYPE_MEMO;
1465 priv->task_fields = g_new0 (struct _EEwsTaskFields, 1);
1466 priv->task_fields->has_due_date = FALSE;
1467 priv->task_fields->has_start_date = FALSE;
1468 priv->task_fields->has_complete_date = FALSE;
1469 }
1470
1471 g_free (folder_class);
1472 }
1473 } else if (!g_ascii_strcasecmp (name, "PostItem") || (node = e_soap_parameter_get_first_child_by_name (param, "PostItem")))
1474 priv->item_type = E_EWS_ITEM_TYPE_POST_ITEM;
1475 else if (!g_ascii_strcasecmp (name, "CalendarItem") || (node = e_soap_parameter_get_first_child_by_name (param, "CalendarItem")))
1476 priv->item_type = E_EWS_ITEM_TYPE_EVENT;
1477 else if (!g_ascii_strcasecmp (name, "Contact") || (node = e_soap_parameter_get_first_child_by_name (param, "Contact"))) {
1478 priv->item_type = E_EWS_ITEM_TYPE_CONTACT;
1479 priv->contact_fields = g_new0 (struct _EEwsContactFields, 1);
1480 } else if (!g_ascii_strcasecmp (name, "DistributionList") || (node = e_soap_parameter_get_first_child_by_name (param, "DistributionList")))
1481 priv->item_type = E_EWS_ITEM_TYPE_GROUP;
1482 else if (!g_ascii_strcasecmp (name, "MeetingMessage") || (node = e_soap_parameter_get_first_child_by_name (param, "MeetingMessage")))
1483 priv->item_type = E_EWS_ITEM_TYPE_MEETING_MESSAGE;
1484 else if (!g_ascii_strcasecmp (name, "MeetingRequest") || (node = e_soap_parameter_get_first_child_by_name (param, "MeetingRequest")))
1485 priv->item_type = E_EWS_ITEM_TYPE_MEETING_REQUEST;
1486 else if (!g_ascii_strcasecmp (name, "MeetingResponse") || (node = e_soap_parameter_get_first_child_by_name (param, "MeetingResponse")))
1487 priv->item_type = E_EWS_ITEM_TYPE_MEETING_RESPONSE;
1488 else if (!g_ascii_strcasecmp (name, "MeetingCancellation") || (node = e_soap_parameter_get_first_child_by_name (param, "MeetingCancellation")))
1489 priv->item_type = E_EWS_ITEM_TYPE_MEETING_CANCELLATION;
1490 else if (!g_ascii_strcasecmp (name, "Task") || (node = e_soap_parameter_get_first_child_by_name (param, "Task"))) {
1491 priv->item_type = E_EWS_ITEM_TYPE_TASK;
1492 priv->task_fields = g_new0 (struct _EEwsTaskFields, 1);
1493 priv->task_fields->has_due_date = FALSE;
1494 priv->task_fields->has_start_date = FALSE;
1495 priv->task_fields->has_complete_date = FALSE;
1496 } else if (!g_ascii_strcasecmp (name, "Item") || (node = e_soap_parameter_get_first_child_by_name (param, "Item")))
1497 priv->item_type = E_EWS_ITEM_TYPE_GENERIC_ITEM;
1498 else if ((node = e_soap_parameter_get_first_child_by_name (param, "AttachmentId"))) {
1499 priv->attachment_id = g_new0 (EwsId, 1);
1500 priv->attachment_id->id = e_soap_parameter_get_property (node, "Id");
1501 priv->attachment_id->change_key = e_soap_parameter_get_property (node, "ChangeKey");
1502 } else if ((node = e_soap_parameter_get_first_child_by_name (param, "ItemId"))) {
1503 /*Spesial case when we are facing <ReadFlagChange> during sync folders*/
1504 priv->item_id = g_new0 (EwsId, 1);
1505 priv->item_id->id = e_soap_parameter_get_property (node, "Id");
1506 priv->item_id->change_key = e_soap_parameter_get_property (node, "ChangeKey");
1507 return TRUE;
1508 } else {
1509 g_warning ("Unable to find the Item type \n");
1510 return FALSE;
1511 }
1512
1513 attach_id = e_soap_parameter_get_first_child_by_name (param, "AttachmentId");
1514 if (attach_id) {
1515 priv->attachment_id = g_new0 (EwsId, 1);
1516 priv->attachment_id->id = e_soap_parameter_get_property (attach_id, "Id");
1517 priv->attachment_id->change_key = e_soap_parameter_get_property (attach_id, "ChangeKey");
1518 }
1519
1520 if (!node)
1521 node = param;
1522
1523 for (subparam = e_soap_parameter_get_first_child (node);
1524 subparam != NULL;
1525 subparam = e_soap_parameter_get_next_child (subparam)) {
1526 ESoapParameter *subparam1;
1527 gchar *value = NULL;
1528
1529 name = e_soap_parameter_get_name (subparam);
1530
1531 /* The order is maintained according to the order in soap response */
1532 if (!g_ascii_strcasecmp (name, "MimeContent")) {
1533 gchar *charset;
1534 guchar *data;
1535 gsize data_len = 0;
1536
1537 value = e_soap_parameter_get_string_value (subparam);
1538 data = g_base64_decode (value, &data_len);
1539 if (!data || !data_len) {
1540 g_free (value);
1541 g_free (data);
1542 return FALSE;
1543 }
1544
1545 charset = e_soap_parameter_get_property (subparam, "CharacterSet");
1546 if (g_strcmp0 (charset, "UTF-8") == 0 &&
1547 !g_utf8_validate ((const gchar *) data, data_len, NULL)) {
1548 gchar *tmp;
1549
1550 tmp = e_util_utf8_data_make_valid ((const gchar *) data, data_len);
1551 if (tmp) {
1552 g_free (data);
1553 data = (guchar *) tmp;
1554 }
1555 }
1556 g_free (charset);
1557
1558 priv->mime_content = (gchar *) data;
1559
1560 g_free (value);
1561 } else if (!g_ascii_strcasecmp (name, "ItemId")) {
1562 priv->item_id = g_new0 (EwsId, 1);
1563 priv->item_id->id = e_soap_parameter_get_property (subparam, "Id");
1564 priv->item_id->change_key = e_soap_parameter_get_property (subparam, "ChangeKey");
1565 } else if (!g_ascii_strcasecmp (name, "Subject")) {
1566 priv->subject = e_soap_parameter_get_string_value (subparam);
1567 } else if (!g_ascii_strcasecmp (name, "InternetMessageHeaders")) {
1568 for (subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "InternetMessageHeader");
1569 subparam1;
1570 subparam1 = e_soap_parameter_get_next_child (subparam1)) {
1571 gchar *str = e_soap_parameter_get_property (subparam1, "HeaderName");
1572
1573 if (g_strcmp0 (str, "Date") == 0) {
1574 priv->date_header = e_soap_parameter_get_string_value (subparam1);
1575 g_free (str);
1576 break;
1577 }
1578
1579 g_free (str);
1580 }
1581 } else if (!g_ascii_strcasecmp (name, "DateTimeReceived")) {
1582 priv->date_received = ews_item_parse_date (subparam);
1583 } else if (!g_ascii_strcasecmp (name, "Size")) {
1584 priv->size = e_soap_parameter_get_int_value (subparam);
1585 } else if (!g_ascii_strcasecmp (name, "Categories")) {
1586 parse_categories (priv, subparam);
1587 } else if (!g_ascii_strcasecmp (name, "Importance")) {
1588 priv->importance = parse_importance (subparam);
1589 } else if (!g_ascii_strcasecmp (name, "InReplyTo")) {
1590 priv->in_replyto = e_soap_parameter_get_string_value (subparam);
1591 } else if (!g_ascii_strcasecmp (name, "DateTimeSent")) {
1592 priv->date_sent = ews_item_parse_date (subparam);
1593 } else if (!g_ascii_strcasecmp (name, "DateTimeCreated")) {
1594 priv->date_created = ews_item_parse_date (subparam);
1595 } else if (!g_ascii_strcasecmp (name, "LastModifiedTime")) {
1596 priv->last_modified_time = ews_item_parse_date (subparam);
1597 } else if (!g_ascii_strcasecmp (name, "HasAttachments")) {
1598 value = e_soap_parameter_get_string_value (subparam);
1599 priv->has_attachments = (!g_ascii_strcasecmp (value, "true"));
1600 g_free (value);
1601 } else if (!g_ascii_strcasecmp (name, "Attachments")) {
1602 process_attachments_list (priv, subparam);
1603 } else if (priv->item_type == E_EWS_ITEM_TYPE_CONTACT)
1604 parse_contact_field (item, name, subparam);
1605 /* fields below are not relevant for contacts, so skip them */
1606 else if (!g_ascii_strcasecmp (name, "Sender")) {
1607 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "Mailbox");
1608 priv->sender = e_ews_item_mailbox_from_soap_param (subparam1);
1609 } else if (!g_ascii_strcasecmp (name, "ToRecipients")) {
1610 GSList *list = NULL;
1611 for (subparam1 = e_soap_parameter_get_first_child (subparam);
1612 subparam1 != NULL;
1613 subparam1 = e_soap_parameter_get_next_child (subparam1)) {
1614 EwsMailbox *mb = e_ews_item_mailbox_from_soap_param (subparam1);
1615 list = g_slist_append (list, mb);
1616 }
1617 priv->to_recipients = list;
1618 } else if (!g_ascii_strcasecmp (name, "CcRecipients")) {
1619 GSList *list = NULL;
1620 for (subparam1 = e_soap_parameter_get_first_child (subparam);
1621 subparam1 != NULL;
1622 subparam1 = e_soap_parameter_get_next_child (subparam1)) {
1623 EwsMailbox *mb = e_ews_item_mailbox_from_soap_param (subparam1);
1624 list = g_slist_append (list, mb);
1625 }
1626 priv->cc_recipients = list;
1627 } else if (!g_ascii_strcasecmp (name, "BccRecipients")) {
1628 GSList *list = NULL;
1629 for (subparam1 = e_soap_parameter_get_first_child (subparam);
1630 subparam1 != NULL;
1631 subparam1 = e_soap_parameter_get_next_child (subparam1)) {
1632 EwsMailbox *mb = e_ews_item_mailbox_from_soap_param (subparam1);
1633 list = g_slist_append (list, mb);
1634 }
1635 priv->bcc_recipients = list;
1636 } else if (!g_ascii_strcasecmp (name, "From")) {
1637 subparam1 = e_soap_parameter_get_first_child_by_name (subparam, "Mailbox");
1638 priv->from = e_ews_item_mailbox_from_soap_param (subparam1);
1639 } else if (!g_ascii_strcasecmp (name, "InternetMessageId")) {
1640 priv->msg_id = e_soap_parameter_get_string_value (subparam);
1641 } else if (!g_ascii_strcasecmp (name, "Preview")) {
1642 priv->preview = e_soap_parameter_get_string_value (subparam);
1643 } else if (!g_ascii_strcasecmp (name, "UID")) {
1644 priv->uid = e_soap_parameter_get_string_value (subparam);
1645 } else if (!g_ascii_strcasecmp (name, "IsRead")) {
1646 value = e_soap_parameter_get_string_value (subparam);
1647 priv->is_read = (!g_ascii_strcasecmp (value, "true"));
1648 g_free (value);
1649 } else if (!g_ascii_strcasecmp (name, "TimeZone")) {
1650 priv->timezone = e_soap_parameter_get_string_value (subparam);
1651 } else if (!g_ascii_strcasecmp (name, "ReminderIsSet")) {
1652 value = e_soap_parameter_get_string_value (subparam);
1653 priv->reminder_is_set = (!g_ascii_strcasecmp (value, "true"));
1654 g_free (value);
1655 } else if (!g_ascii_strcasecmp (name, "ReminderDueBy")) {
1656 priv->reminder_due_by = ews_item_parse_date (subparam);
1657 } else if (!g_ascii_strcasecmp (name, "ReminderMinutesBeforeStart")) {
1658 priv->reminder_minutes_before_start = e_soap_parameter_get_int_value (subparam);
1659 } else if (priv->item_type == E_EWS_ITEM_TYPE_TASK || priv->item_type == E_EWS_ITEM_TYPE_MEMO) {
1660 parse_task_field (item, name, subparam);
1661 /* fields below are not relevant for task, so skip them */
1662 } else if (!g_ascii_strcasecmp (name, "References")) {
1663 priv->references = e_soap_parameter_get_string_value (subparam);
1664 } else if (!g_ascii_strcasecmp (name, "ExtendedProperty")) {
1665 parse_extended_property (priv, subparam);
1666 } else if (!g_ascii_strcasecmp (name, "ModifiedOccurrences")) {
1667 process_modified_occurrences (priv, subparam);
1668 } else if (!g_ascii_strcasecmp (name, "IsMeeting")) {
1669 value = e_soap_parameter_get_string_value (subparam);
1670 priv->is_meeting = (!g_ascii_strcasecmp (value, "true"));
1671 g_free (value);
1672 } else if (!g_ascii_strcasecmp (name, "IsResponseRequested")) {
1673 value = e_soap_parameter_get_string_value (subparam);
1674 priv->is_response_requested = (!g_ascii_strcasecmp (value, "true"));
1675 g_free (value);
1676 } else if (!g_ascii_strcasecmp (name, "MyResponseType")) {
1677 g_free (priv->my_response_type);
1678 priv->my_response_type = e_soap_parameter_get_string_value (subparam);
1679 } else if (!g_ascii_strcasecmp (name, "RequiredAttendees")) {
1680 process_attendees (priv, subparam, "Required");
1681 } else if (!g_ascii_strcasecmp (name, "OptionalAttendees")) {
1682 process_attendees (priv, subparam, "Optional");
1683 } else if (!g_ascii_strcasecmp (name, "Resources")) {
1684 process_attendees (priv, subparam, "Resource");
1685 } else if (!g_ascii_strcasecmp (name, "AssociatedCalendarItemId")) {
1686 priv->calendar_item_accept_id = g_new0 (EwsId, 1);
1687 priv->calendar_item_accept_id->id = e_soap_parameter_get_property (subparam, "Id");
1688 priv->calendar_item_accept_id->change_key = e_soap_parameter_get_property (subparam, "ChangeKey");
1689 } else if (!g_ascii_strcasecmp (name, "Start")) {
1690 priv->calendar_start = ews_item_parse_date (subparam);
1691 } else if (!g_ascii_strcasecmp (name, "End")) {
1692 priv->calendar_end = ews_item_parse_date (subparam);
1693 } else if (!g_ascii_strcasecmp (name, "StartTimeZone")) {
1694 priv->start_timezone = e_soap_parameter_get_property (subparam, "Id");
1695 } else if (!g_ascii_strcasecmp (name, "EndTimeZone")) {
1696 priv->end_timezone = e_soap_parameter_get_property (subparam, "Id");
1697 } else if (!g_ascii_strcasecmp (name, "Body")) {
1698 const gchar *body_type;
1699
1700 priv->body = e_soap_parameter_get_string_value (subparam);
1701
1702 body_type = e_soap_parameter_get_property (subparam, "BodyType");
1703
1704 if (g_strcmp0 (body_type, "HTML") == 0)
1705 priv->body_type = E_EWS_BODY_TYPE_HTML;
1706 else if (g_strcmp0 (body_type, "Text") == 0)
1707 priv->body_type = E_EWS_BODY_TYPE_TEXT;
1708 else
1709 priv->body_type = E_EWS_BODY_TYPE_ANY;
1710 }
1711 }
1712
1713 return TRUE;
1714 }
1715
1716 EEwsItem *
1717 e_ews_item_new_from_soap_parameter (ESoapParameter *param)
1718 {
1719 EEwsItem *item;
1720
1721 g_return_val_if_fail (param != NULL, NULL);
1722
1723 item = g_object_new (E_TYPE_EWS_ITEM, NULL);
1724 if (!e_ews_item_set_from_soap_parameter (item, param)) {
1725 g_object_unref (item);
1726 return NULL;
1727 }
1728
1729 return item;
1730 }
1731
1732 EEwsItem *
1733 e_ews_item_new_from_error (const GError *error)
1734 {
1735 EEwsItem *item;
1736
1737 g_return_val_if_fail (error != NULL, NULL);
1738
1739 item = g_object_new (E_TYPE_EWS_ITEM, NULL);
1740 e_ews_item_set_error (item, error);
1741
1742 return item;
1743 }
1744
1745 EEwsItemType
1746 e_ews_item_get_item_type (EEwsItem *item)
1747 {
1748 g_return_val_if_fail (E_IS_EWS_ITEM (item), E_EWS_ITEM_TYPE_UNKNOWN);
1749
1750 return item->priv->item_type;
1751 }
1752
1753 void
1754 e_ews_item_set_item_type (EEwsItem *item,
1755 EEwsItemType new_type)
1756 {
1757 g_return_if_fail (E_IS_EWS_ITEM (item));
1758
1759 /* once the type is set to error type it stays as error type */
1760 if (item->priv->item_type != E_EWS_ITEM_TYPE_ERROR)
1761 item->priv->item_type = new_type;
1762 }
1763
1764 const GError *
1765 e_ews_item_get_error (EEwsItem *item)
1766 {
1767 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
1768
1769 return item->priv->error;
1770 }
1771
1772 void
1773 e_ews_item_set_error (EEwsItem *item,
1774 const GError *error)
1775 {
1776 GError *new_error;
1777
1778 g_return_if_fail (E_IS_EWS_ITEM (item));
1779
1780 if (error)
1781 new_error = g_error_copy (error);
1782 else
1783 new_error = NULL;
1784
1785 g_clear_error (&item->priv->error);
1786 item->priv->error = new_error;
1787
1788 if (item->priv->error)
1789 e_ews_item_set_item_type (item, E_EWS_ITEM_TYPE_ERROR);
1790 }
1791
1792 const gchar *
1793 e_ews_item_get_subject (EEwsItem *item)
1794 {
1795 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
1796
1797 return (const gchar *) item->priv->subject;
1798 }
1799
1800 void
1801 e_ews_item_set_subject (EEwsItem *item,
1802 const gchar *new_subject)
1803 {
1804 g_return_if_fail (E_IS_EWS_ITEM (item));
1805
1806 if (item->priv->subject)
1807 g_free (item->priv->subject);
1808 item->priv->subject = g_strdup (new_subject);
1809 }
1810
1811 const gchar *
1812 e_ews_item_get_mime_content (EEwsItem *item)
1813 {
1814 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
1815
1816 return (const gchar *) item->priv->mime_content;
1817 }
1818
1819 void
1820 e_ews_item_set_mime_content (EEwsItem *item,
1821 const gchar *new_mime_content)
1822 {
1823 g_return_if_fail (E_IS_EWS_ITEM (item));
1824
1825 if (item->priv->mime_content)
1826 g_free (item->priv->mime_content);
1827 item->priv->mime_content = g_strdup (new_mime_content);
1828 }
1829
1830 const EwsId *
1831 e_ews_item_get_id (EEwsItem *item)
1832 {
1833 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
1834
1835 return (const EwsId *) item->priv->item_id;
1836 }
1837
1838 const EwsId *
1839 e_ews_item_get_attachment_id (EEwsItem *item)
1840 {
1841 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
1842
1843 return (const EwsId *) item->priv->attachment_id;
1844 }
1845
1846 gsize
1847 e_ews_item_get_size (EEwsItem *item)
1848 {
1849 g_return_val_if_fail (E_IS_EWS_ITEM (item), -1);
1850
1851 return item->priv->size;
1852 }
1853
1854 const gchar *
1855 e_ews_item_get_msg_id (EEwsItem *item)
1856 {
1857 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
1858
1859 return (const gchar *) item->priv->msg_id;
1860 }
1861
1862 const gchar *
1863 e_ews_item_get_preview (EEwsItem *item)
1864 {
1865 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
1866
1867 return item->priv->preview;
1868 }
1869
1870 const gchar *
1871 e_ews_item_get_uid (EEwsItem *item)
1872 {
1873 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
1874
1875 return (const gchar *) item->priv->uid;
1876 }
1877
1878 const gchar *
1879 e_ews_item_get_in_replyto (EEwsItem *item)
1880 {
1881 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
1882
1883 return (const gchar *) item->priv->in_replyto;
1884 }
1885
1886 const gchar *
1887 e_ews_item_get_references (EEwsItem *item)
1888 {
1889 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
1890
1891 return (const gchar *) item->priv->references;
1892 }
1893
1894 const gchar *
1895 e_ews_item_get_date_header (EEwsItem *item)
1896 {
1897 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
1898
1899 return item->priv->date_header;
1900 }
1901
1902 time_t
1903 e_ews_item_get_date_received (EEwsItem *item)
1904 {
1905 g_return_val_if_fail (E_IS_EWS_ITEM (item), -1);
1906
1907 return item->priv->date_received;
1908 }
1909
1910 time_t
1911 e_ews_item_get_date_sent (EEwsItem *item)
1912 {
1913 g_return_val_if_fail (E_IS_EWS_ITEM (item), -1);
1914
1915 return item->priv->date_sent;
1916 }
1917
1918 time_t
1919 e_ews_item_get_date_created (EEwsItem *item)
1920 {
1921 g_return_val_if_fail (E_IS_EWS_ITEM (item), -1);
1922
1923 return item->priv->date_created;
1924 }
1925
1926 time_t
1927 e_ews_item_get_last_modified_time (EEwsItem *item)
1928 {
1929 g_return_val_if_fail (E_IS_EWS_ITEM (item), -1);
1930
1931 return item->priv->last_modified_time;
1932 }
1933
1934 gboolean
1935 e_ews_item_has_attachments (EEwsItem *item,
1936 gboolean *has_attachments)
1937 {
1938 g_return_val_if_fail (E_IS_EWS_ITEM (item), FALSE);
1939
1940 *has_attachments = item->priv->has_attachments;
1941
1942 return TRUE;
1943 }
1944
1945 gboolean
1946 e_ews_item_is_read (EEwsItem *item,
1947 gboolean *read)
1948 {
1949 g_return_val_if_fail (E_IS_EWS_ITEM (item), FALSE);
1950
1951 *read = item->priv->is_read;
1952
1953 return TRUE;
1954 }
1955
1956 gboolean
1957 e_ews_item_get_is_meeting (EEwsItem *item)
1958 {
1959 g_return_val_if_fail (E_IS_EWS_ITEM (item), FALSE);
1960
1961 return item->priv->is_meeting;
1962 }
1963
1964 gboolean
1965 e_ews_item_get_is_response_requested (EEwsItem *item)
1966 {
1967 g_return_val_if_fail (E_IS_EWS_ITEM (item), FALSE);
1968
1969 return item->priv->is_response_requested;
1970 }
1971
1972 gboolean
1973 e_ews_item_is_forwarded (EEwsItem *item,
1974 gboolean *forwarded)
1975 {
1976 g_return_val_if_fail (E_IS_EWS_ITEM (item), FALSE);
1977
1978 *forwarded = (item->priv->mapi_icon_index == 0x106);
1979
1980 return TRUE;
1981 }
1982
1983 gboolean
1984 e_ews_item_is_answered (EEwsItem *item,
1985 gboolean *answered)
1986 {
1987 g_return_val_if_fail (E_IS_EWS_ITEM (item), FALSE);
1988
1989 *answered = (item->priv->mapi_icon_index == 0x105);
1990
1991 return TRUE;
1992 }
1993
1994 guint32
1995 e_ews_item_get_message_flags (EEwsItem *item)
1996 {
1997 g_return_val_if_fail (E_IS_EWS_ITEM (item), 0);
1998
1999 return item->priv->mapi_message_flags;
2000 }
2001
2002 const GSList *
2003 e_ews_item_get_to_recipients (EEwsItem *item)
2004 {
2005 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2006
2007 return (const GSList *) item->priv->to_recipients;
2008 }
2009
2010 const GSList *
2011 e_ews_item_get_cc_recipients (EEwsItem *item)
2012 {
2013 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2014
2015 return (const GSList *) item->priv->cc_recipients;
2016 }
2017
2018 const GSList *
2019 e_ews_item_get_bcc_recipients (EEwsItem *item)
2020 {
2021 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2022
2023 return (const GSList *) item->priv->bcc_recipients;
2024 }
2025
2026 const EwsMailbox *
2027 e_ews_item_get_sender (EEwsItem *item)
2028 {
2029 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2030
2031 return (const EwsMailbox *) item->priv->sender;
2032 }
2033
2034 const EwsMailbox *
2035 e_ews_item_get_from (EEwsItem *item)
2036 {
2037 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2038
2039 return (const EwsMailbox *) item->priv->from;
2040 }
2041
2042 const GSList *
2043 e_ews_item_get_categories (EEwsItem *item)
2044 {
2045 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2046
2047 return item->priv->categories;
2048 }
2049
2050 EwsImportance
2051 e_ews_item_get_importance (EEwsItem *item)
2052 {
2053 g_return_val_if_fail (E_IS_EWS_ITEM (item), EWS_ITEM_LOW);
2054
2055 return item->priv->importance;
2056 }
2057
2058 const gchar *
2059 e_ews_item_get_iana_start_time_zone (EEwsItem *item)
2060 {
2061 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2062
2063 return item->priv->iana_start_time_zone;
2064 }
2065
2066 const gchar *
2067 e_ews_item_get_iana_end_time_zone (EEwsItem *item)
2068 {
2069 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2070
2071 return item->priv->iana_end_time_zone;
2072 }
2073
2074 const gchar *
2075 e_ews_item_get_event_url (EEwsItem *item)
2076 {
2077 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2078
2079 return item->priv->event_url;
2080 }
2081
2082 EwsMailbox *
2083 e_ews_item_mailbox_from_soap_param (ESoapParameter *param)
2084 {
2085 EwsMailbox *mb;
2086 ESoapParameter *subparam;
2087
2088 mb = g_new0 (EwsMailbox, 1);
2089
2090 subparam = e_soap_parameter_get_first_child_by_name (param, "Name");
2091 if (subparam)
2092 mb->name = e_soap_parameter_get_string_value (subparam);
2093
2094 subparam = e_soap_parameter_get_first_child_by_name (param, "EmailAddress");
2095 if (subparam)
2096 mb->email = e_soap_parameter_get_string_value (subparam);
2097
2098 subparam = e_soap_parameter_get_first_child_by_name (param, "RoutingType");
2099 if (subparam)
2100 mb->routing_type = e_soap_parameter_get_string_value (subparam);
2101
2102 subparam = e_soap_parameter_get_first_child_by_name (param, "MailboxType");
2103 if (subparam)
2104 mb->mailbox_type = e_soap_parameter_get_string_value (subparam);
2105
2106 subparam = e_soap_parameter_get_first_child_by_name (param, "ItemId");
2107 if (subparam) {
2108 EwsId *id = g_new0 (EwsId, 1);
2109 id->id = e_soap_parameter_get_property (subparam, "Id");
2110 id->change_key = e_soap_parameter_get_property (subparam, "ChangeKey");
2111 mb->item_id = id;
2112 }
2113
2114 if (!mb->email && !mb->name) {
2115 e_ews_mailbox_free (mb);
2116 mb = NULL;
2117 }
2118
2119 return mb;
2120 }
2121
2122 void
2123 e_ews_mailbox_free (EwsMailbox *mailbox)
2124 {
2125 if (!mailbox)
2126 return;
2127
2128 g_free (mailbox->name);
2129 g_free (mailbox->email);
2130 g_free (mailbox->routing_type);
2131 g_free (mailbox->mailbox_type);
2132
2133 if (mailbox->item_id) {
2134 g_free (mailbox->item_id->id);
2135 g_free (mailbox->item_id->change_key);
2136 g_free (mailbox->item_id);
2137 }
2138
2139 g_free (mailbox);
2140 }
2141
2142 const GSList *
2143 e_ews_item_get_modified_occurrences (EEwsItem *item)
2144 {
2145 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2146
2147 return item->priv->modified_occurrences;
2148 }
2149
2150 GSList *
2151 e_ews_item_get_attachments_ids (EEwsItem *item)
2152 {
2153 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2154
2155 return item->priv->attachments_ids;
2156 }
2157
2158 const gchar *
2159 e_ews_item_get_extended_tag (EEwsItem *item,
2160 guint32 prop_tag)
2161 {
2162 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2163
2164 if (!item->priv->mapi_extended_tags)
2165 return NULL;
2166
2167 return g_hash_table_lookup (item->priv->mapi_extended_tags, GUINT_TO_POINTER (prop_tag));
2168 }
2169
2170 const gchar *
2171 e_ews_item_get_extended_distinguished_tag (EEwsItem *item,
2172 const gchar *set_id,
2173 guint32 prop_id)
2174 {
2175 GHashTable *set_tags;
2176
2177 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2178
2179 if (!item->priv->mapi_extended_sets)
2180 return NULL;
2181
2182 set_tags = g_hash_table_lookup (item->priv->mapi_extended_sets, set_id);
2183 if (!set_tags)
2184 return NULL;
2185
2186 return g_hash_table_lookup (set_tags, GUINT_TO_POINTER (prop_id));
2187 }
2188
2189 gboolean
2190 e_ews_item_get_extended_property_as_boolean (EEwsItem *item,
2191 const gchar *set_id,
2192 guint32 prop_id_or_tag,
2193 gboolean *found)
2194 {
2195 const gchar *value;
2196
2197 value = e_ews_item_get_extended_property_as_string (item, set_id, prop_id_or_tag, found);
2198 if (!value)
2199 return FALSE;
2200
2201 if (g_str_equal (value, "true"))
2202 return TRUE;
2203
2204 if (g_str_equal (value, "false"))
2205 return FALSE;
2206
2207 if (found)
2208 *found = FALSE;
2209
2210 return FALSE;
2211 }
2212
2213 gint
2214 e_ews_item_get_extended_property_as_int (EEwsItem *item,
2215 const gchar *set_id,
2216 guint32 prop_id_or_tag,
2217 gboolean *found)
2218 {
2219 const gchar *value;
2220
2221 value = e_ews_item_get_extended_property_as_string (item, set_id, prop_id_or_tag, found);
2222 if (!value)
2223 return 0;
2224
2225 return strtol (value, NULL, 0);
2226 }
2227
2228 gdouble
2229 e_ews_item_get_extended_property_as_double (EEwsItem *item,
2230 const gchar *set_id,
2231 guint32 prop_id_or_tag,
2232 gboolean *found)
2233 {
2234 const gchar *value;
2235
2236 value = e_ews_item_get_extended_property_as_string (item, set_id, prop_id_or_tag, found);
2237 if (!value)
2238 return 0.0;
2239
2240 return g_ascii_strtod (value, NULL);
2241 }
2242
2243 const gchar *
2244 e_ews_item_get_extended_property_as_string (EEwsItem *item,
2245 const gchar *set_id,
2246 guint32 prop_id_or_tag,
2247 gboolean *found)
2248 {
2249 const gchar *value;
2250
2251 if (set_id)
2252 value = e_ews_item_get_extended_distinguished_tag (item, set_id, prop_id_or_tag);
2253 else
2254 value = e_ews_item_get_extended_tag (item, prop_id_or_tag);
2255
2256 if (found)
2257 *found = value != NULL;
2258
2259 return value;
2260 }
2261
2262 time_t
2263 e_ews_item_get_extended_property_as_time (EEwsItem *item,
2264 const gchar *set_id,
2265 guint32 prop_id_or_tag,
2266 gboolean *found)
2267 {
2268 const gchar *value;
2269 GTimeVal tv;
2270
2271 value = e_ews_item_get_extended_property_as_string (item, set_id, prop_id_or_tag, found);
2272 if (!value)
2273 return (time_t) 0;
2274
2275 if (g_time_val_from_iso8601 (value, &tv))
2276 return tv.tv_sec;
2277
2278 if (found)
2279 *found = FALSE;
2280
2281 return (time_t) 0;
2282 }
2283
2284 gchar *
2285 e_ews_embed_attachment_id_in_uri (const gchar *olduri,
2286 const gchar *attach_id)
2287 {
2288 gchar *tmpdir, *tmpfilename, *filename, *dirname, *name;
2289
2290 tmpfilename = g_filename_from_uri (olduri, NULL, NULL);
2291 g_return_val_if_fail (tmpfilename != NULL, NULL);
2292
2293 name = g_path_get_basename (tmpfilename);
2294 tmpdir = g_path_get_dirname (tmpfilename);
2295
2296 dirname = g_build_filename (tmpdir, attach_id, NULL);
2297 if (g_mkdir (dirname, 0775) == -1) {
2298 g_warning ("Failed create directory to place file in [%s]: %s\n", dirname, g_strerror (errno));
2299 }
2300
2301 filename = g_build_filename (dirname, name, NULL);
2302 if (g_rename (tmpfilename, filename) != 0) {
2303 g_warning ("Failed to move attachment cache file [%s -> %s]: %s\n", tmpfilename, filename, g_strerror (errno));
2304 }
2305
2306 g_free (tmpfilename);
2307 g_free (tmpdir);
2308 g_free (dirname);
2309 g_free (name);
2310
2311 tmpfilename = g_filename_to_uri (filename, NULL, NULL);
2312
2313 g_free (filename);
2314
2315 return tmpfilename;
2316 }
2317
2318 EEwsAttachmentInfo *
2319 e_ews_dump_file_attachment_from_soap_parameter (ESoapParameter *param,
2320 const gchar *cache,
2321 const gchar *comp_uid)
2322 {
2323 ESoapParameter *subparam;
2324 const gchar *param_name, *tmpfilename;
2325 gchar *name = NULL, *value, *filename, *dirname;
2326 guchar *content = NULL;
2327 gsize data_len = 0;
2328 gchar *tmpdir;
2329 EEwsAttachmentInfo *info;
2330
2331 g_return_val_if_fail (param != NULL, NULL);
2332
2333 /* Parse element, look for filename and content */
2334 for (subparam = e_soap_parameter_get_first_child (param); subparam != NULL; subparam = e_soap_parameter_get_next_child (subparam)) {
2335 param_name = e_soap_parameter_get_name (subparam);
2336
2337 if (g_ascii_strcasecmp (param_name, "Name") == 0) {
2338 g_free (name);
2339 name = e_soap_parameter_get_string_value (subparam);
2340 } else if (g_ascii_strcasecmp (param_name, "Content") == 0) {
2341 g_free (content);
2342 value = e_soap_parameter_get_string_value (subparam);
2343 content = g_base64_decode (value, &data_len);
2344 g_free (value);
2345 }
2346 }
2347
2348 /* Make sure we have needed data */
2349 if (!content || !name) {
2350 g_free (name);
2351 g_free (content);
2352 return NULL;
2353 }
2354
2355 if (cache && content && g_file_test ((const gchar *) content, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_EXISTS)) {
2356 gchar *uri;
2357
2358 info = e_ews_attachment_info_new (E_EWS_ATTACHMENT_INFO_TYPE_URI);
2359
2360 tmpfilename = (gchar *) content;
2361 tmpdir = g_path_get_dirname (tmpfilename);
2362
2363 dirname = g_build_filename (tmpdir, comp_uid, NULL);
2364 if (g_mkdir_with_parents (dirname, 0775) == -1) {
2365 g_warning ("Failed create directory to place file in [%s]: %s\n", dirname, g_strerror (errno));
2366 }
2367
2368 filename = g_build_filename (dirname, name, NULL);
2369 if (g_rename (tmpfilename, filename) != 0) {
2370 g_warning ("Failed to move attachment cache file [%s -> %s]: %s\n",
2371 tmpfilename, filename, g_strerror (errno));
2372 }
2373
2374 g_free (dirname);
2375 g_free (tmpdir);
2376 g_free (name);
2377 g_free (content);
2378
2379 /* Return URI to saved file */
2380 uri = g_filename_to_uri (filename, NULL, NULL);
2381 e_ews_attachment_info_set_uri (info, uri);
2382 g_free (filename);
2383 g_free (uri);
2384 } else {
2385 info = e_ews_attachment_info_new (E_EWS_ATTACHMENT_INFO_TYPE_INLINED);
2386 e_ews_attachment_info_set_inlined_data (info, content, data_len);
2387 e_ews_attachment_info_set_prefer_filename (info, name);
2388 }
2389 return info;
2390 }
2391
2392 EEwsAttachmentInfo *
2393 e_ews_item_dump_mime_content (EEwsItem *item,
2394 const gchar *cache)
2395 {
2396 EEwsAttachmentInfo *info;
2397 gchar *filename, *surename, *dirname;
2398 gchar *tmpdir, *uri;
2399 const gchar *tmpfilename;
2400
2401 g_return_val_if_fail (item->priv->mime_content != NULL, NULL);
2402 g_return_val_if_fail (g_file_test ((const gchar *) item->priv->mime_content, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_EXISTS), NULL);
2403
2404 tmpfilename = (gchar *) item->priv->mime_content;
2405 tmpdir = g_path_get_dirname (tmpfilename);
2406
2407 dirname = g_build_filename (tmpdir, "XXXXXX", NULL);
2408 if (!g_mkdtemp (dirname)) {
2409 g_warning ("Failed to create directory for attachment cache '%s': %s", dirname, g_strerror (errno));
2410
2411 g_free (tmpdir);
2412 g_free (dirname);
2413
2414 return NULL;
2415 }
2416
2417 surename = g_uri_escape_string (item->priv->subject, "", TRUE);
2418 filename = g_build_filename (dirname, surename, NULL);
2419
2420 if (g_rename ((const gchar *) item->priv->mime_content, filename) != 0) {
2421 g_warning ("Failed to move attachment cache file '%s': %s", filename, g_strerror (errno));
2422
2423 g_free (tmpdir);
2424 g_free (dirname);
2425 g_free (filename);
2426 g_free (surename);
2427
2428 return NULL;
2429 }
2430
2431 uri = g_filename_to_uri (filename, NULL, NULL);
2432
2433 info = e_ews_attachment_info_new (E_EWS_ATTACHMENT_INFO_TYPE_URI);
2434 e_ews_attachment_info_set_uri (info, uri);
2435
2436 g_free (uri);
2437 g_free (filename);
2438 g_free (dirname);
2439 g_free (tmpdir);
2440 g_free (surename);
2441
2442 /* Return URI to saved file */
2443 return info;
2444 }
2445
2446 const gchar *
2447 e_ews_item_get_my_response_type (EEwsItem *item)
2448 {
2449 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2450
2451 return item->priv->my_response_type;
2452 }
2453
2454 const GSList *
2455 e_ews_item_get_attendees (EEwsItem *item)
2456 {
2457 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2458
2459 return item->priv->attendees;
2460 }
2461
2462 const EwsId *
2463 e_ews_item_get_calendar_item_accept_id (EEwsItem *item)
2464 {
2465 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2466
2467 return (const EwsId *) item->priv->calendar_item_accept_id;
2468 }
2469
2470 gboolean
2471 e_ews_item_get_reminder_is_set (EEwsItem *item)
2472 {
2473 g_return_val_if_fail (E_IS_EWS_ITEM (item), FALSE);
2474
2475 return item->priv->reminder_is_set;
2476 }
2477
2478 time_t
2479 e_ews_item_get_reminder_due_by (EEwsItem *item)
2480 {
2481 g_return_val_if_fail (E_IS_EWS_ITEM (item), -1);
2482
2483 return item->priv->reminder_due_by;
2484 }
2485
2486 /* -1 when not set, but should really check also
2487 e_ews_item_get_reminder_is_set() before using the value */
2488 gint
2489 e_ews_item_get_reminder_minutes_before_start (EEwsItem *item)
2490 {
2491 g_return_val_if_fail (E_IS_EWS_ITEM (item), -1);
2492
2493 return item->priv->reminder_minutes_before_start;
2494 }
2495
2496 /* the out_recurrence is filled only if the function returns TRUE */
2497 gboolean
2498 e_ews_item_get_recurrence (EEwsItem *item,
2499 EEwsRecurrence *out_recurrence)
2500 {
2501 g_return_val_if_fail (E_IS_EWS_ITEM (item), FALSE);
2502 g_return_val_if_fail (out_recurrence != NULL, -1);
2503
2504 if (item->priv->recurrence.type == E_EWS_RECURRENCE_UNKNOWN ||
2505 item->priv->recurrence.end_type == E_EWS_RECURRENCE_END_UNKNOWN)
2506 return FALSE;
2507
2508 *out_recurrence = item->priv->recurrence;
2509
2510 return TRUE;
2511 }
2512
2513 const gchar *
2514 e_ews_item_get_fileas (EEwsItem *item)
2515 {
2516 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2517 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2518
2519 return (const gchar *) item->priv->contact_fields->fileas;
2520 }
2521
2522 const EwsCompleteName *
2523 e_ews_item_get_complete_name (EEwsItem *item)
2524 {
2525 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2526 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2527
2528 if (!item->priv->contact_fields->complete_name && (
2529 item->priv->contact_fields->surname ||
2530 item->priv->contact_fields->middlename ||
2531 item->priv->contact_fields->givenname)) {
2532 EwsCompleteName *cn;
2533
2534 cn = g_new0 (EwsCompleteName, 1);
2535
2536 cn->first_name = g_strdup (item->priv->contact_fields->givenname);
2537 cn->middle_name = g_strdup (item->priv->contact_fields->middlename);
2538 cn->last_name = g_strdup (item->priv->contact_fields->surname);
2539
2540 item->priv->contact_fields->complete_name = cn;
2541 }
2542
2543 return (const EwsCompleteName *) item->priv->contact_fields->complete_name;
2544 }
2545
2546 const gchar *
2547 e_ews_item_get_display_name (EEwsItem *item)
2548 {
2549 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2550 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2551
2552 return item->priv->contact_fields->display_name;
2553 }
2554
2555 GHashTable *
2556 e_ews_item_get_email_addresses (EEwsItem *item)
2557 {
2558 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2559 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2560
2561 return item->priv->contact_fields->email_addresses;
2562 }
2563
2564 /**
2565 * e_ews_item_get_email_address
2566 * @item:
2567 * @field: "EmailAddress1", "EmailAddress2", "EmailAddress3"
2568 *
2569 *
2570 * Returns:
2571 **/
2572 const gchar *
2573 e_ews_item_get_email_address (EEwsItem *item,
2574 const gchar *field)
2575 {
2576 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2577 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2578
2579 if (item->priv->contact_fields->email_addresses)
2580 return g_hash_table_lookup (item->priv->contact_fields->email_addresses, field);
2581
2582 return NULL;
2583 }
2584
2585 /**
2586 * e_ews_item_get_physical_address
2587 * @item:
2588 * @field: "Business", "Home", "Other"
2589 *
2590 *
2591 * Returns:
2592 **/
2593 const EwsAddress *
2594 e_ews_item_get_physical_address (EEwsItem *item,
2595 const gchar *field)
2596 {
2597 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2598 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2599
2600 if (item->priv->contact_fields->physical_addresses)
2601 return g_hash_table_lookup (item->priv->contact_fields->physical_addresses, field);
2602
2603 return NULL;
2604 }
2605
2606 /**
2607 * e_ews_item_get_phone_number
2608 * @item:
2609 * @field: "AssistantPhone", "BusinessFax", "BusinessPhone", "BusinessPhone2", "Callback"
2610 * "CarPhone", "CompanyMainPhone", "HomeFax", "HomePhone", "HomePhone2", "Isdn", "MobilePhone"
2611 * "OtherFax", "OtherTelephone", "Pager", "PrimaryPhone", "RadioPhone", "Telex", "TtyTddPhone"
2612 *
2613 *
2614 * Returns:
2615 **/
2616 const gchar *
2617 e_ews_item_get_phone_number (EEwsItem *item,
2618 const gchar *field)
2619 {
2620 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2621 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2622
2623 if (item->priv->contact_fields->phone_numbers)
2624 return g_hash_table_lookup (item->priv->contact_fields->phone_numbers, field);
2625
2626 return NULL;
2627 }
2628
2629 /**
2630 * e_ews_item_get_im_address
2631 * @item:
2632 * @field: "ImAddress1", "ImAddress2", "ImAddress3"
2633 *
2634 *
2635 * Returns:
2636 **/
2637 const gchar *
2638 e_ews_item_get_im_address (EEwsItem *item,
2639 const gchar *field)
2640 {
2641 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2642 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2643
2644 if (item->priv->contact_fields->im_addresses)
2645 return g_hash_table_lookup (item->priv->contact_fields->im_addresses, field);
2646
2647 return NULL;
2648 }
2649
2650 const gchar *
2651 e_ews_item_get_company_name (EEwsItem *item)
2652 {
2653 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2654 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2655
2656 return (const gchar *) item->priv->contact_fields->company_name;
2657 }
2658
2659 const gchar *
2660 e_ews_item_get_department (EEwsItem *item)
2661 {
2662 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2663 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2664
2665 return (const gchar *) item->priv->contact_fields->department;
2666 }
2667
2668 const gchar *
2669 e_ews_item_get_job_title (EEwsItem *item)
2670 {
2671 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2672 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2673
2674 return (const gchar *) item->priv->contact_fields->job_title;
2675 }
2676
2677 const gchar *
2678 e_ews_item_get_assistant_name (EEwsItem *item)
2679 {
2680 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2681 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2682
2683 return (const gchar *) item->priv->contact_fields->assistant_name;
2684 }
2685
2686 const gchar *
2687 e_ews_item_get_manager (EEwsItem *item)
2688 {
2689 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2690 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2691
2692 return (const gchar *) item->priv->contact_fields->manager;
2693 }
2694
2695 const gchar *
2696 e_ews_item_get_office_location (EEwsItem *item)
2697 {
2698 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2699 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2700
2701 return (const gchar *) item->priv->contact_fields->office_location;
2702 }
2703
2704 const gchar *
2705 e_ews_item_get_business_homepage (EEwsItem *item)
2706 {
2707 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2708 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2709
2710 return (const gchar *) item->priv->contact_fields->business_homepage;
2711 }
2712
2713 const gchar *
2714 e_ews_item_get_profession (EEwsItem *item)
2715 {
2716 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2717 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2718
2719 return (const gchar *) item->priv->contact_fields->profession;
2720 }
2721
2722 const gchar *
2723 e_ews_item_get_spouse_name (EEwsItem *item)
2724 {
2725 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2726 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2727
2728 return (const gchar *) item->priv->contact_fields->spouse_name;
2729 }
2730
2731 const gchar *
2732 e_ews_item_get_surname (EEwsItem *item)
2733 {
2734 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2735 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2736
2737 return (const gchar *) item->priv->contact_fields->surname;
2738 }
2739
2740 const gchar *
2741 e_ews_item_get_givenname (EEwsItem *item)
2742 {
2743 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2744 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2745
2746 return item->priv->contact_fields->givenname;
2747 }
2748
2749 const gchar *
2750 e_ews_item_get_middlename (EEwsItem *item)
2751 {
2752 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2753 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2754
2755 return item->priv->contact_fields->middlename;
2756 }
2757
2758 const gchar *
2759 e_ews_item_get_notes (EEwsItem *item)
2760 {
2761 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2762 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2763
2764 return item->priv->contact_fields->notes;
2765 }
2766
2767 const guchar *
2768 e_ews_item_get_user_certificate (EEwsItem *item,
2769 gsize *out_len)
2770 {
2771 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2772 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2773 g_return_val_if_fail (out_len != NULL, NULL);
2774
2775 *out_len = item->priv->contact_fields->user_cert_len;
2776
2777 return item->priv->contact_fields->user_cert;
2778 }
2779
2780 const guchar *
2781 e_ews_item_get_msexchange_certificate (EEwsItem *item,
2782 gsize *out_len)
2783 {
2784 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2785 g_return_val_if_fail (item->priv->contact_fields != NULL, NULL);
2786 g_return_val_if_fail (out_len != NULL, NULL);
2787
2788 *out_len = item->priv->contact_fields->msexchange_cert_len;
2789
2790 return item->priv->contact_fields->msexchange_cert;
2791 }
2792
2793 time_t
2794 e_ews_item_get_birthday (EEwsItem *item,
2795 gboolean *out_exists)
2796 {
2797 g_return_val_if_fail (E_IS_EWS_ITEM (item), -1);
2798 g_return_val_if_fail (item->priv->contact_fields != NULL, -1);
2799
2800 if (out_exists)
2801 *out_exists = item->priv->contact_fields->has_birthday;
2802
2803 return item->priv->contact_fields->birthday;
2804 }
2805
2806 time_t
2807 e_ews_item_get_wedding_anniversary (EEwsItem *item,
2808 gboolean *out_exists)
2809 {
2810 g_return_val_if_fail (E_IS_EWS_ITEM (item), -1);
2811 g_return_val_if_fail (item->priv->contact_fields != NULL, -1);
2812
2813 if (out_exists)
2814 *out_exists = item->priv->contact_fields->has_wedding_anniversary;
2815
2816 return item->priv->contact_fields->wedding_anniversary;
2817 }
2818
2819 const gchar *
2820 e_ews_item_get_status (EEwsItem *item)
2821 {
2822 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2823 g_return_val_if_fail (item->priv->task_fields != NULL, NULL);
2824
2825 return item->priv->task_fields->status;
2826 }
2827
2828 const gchar *
2829 e_ews_item_get_percent_complete (EEwsItem *item)
2830 {
2831 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2832 g_return_val_if_fail (item->priv->task_fields != NULL, NULL);
2833
2834 return item->priv->task_fields->percent_complete;
2835 }
2836
2837 time_t
2838 e_ews_item_get_due_date (EEwsItem *item)
2839 {
2840 g_return_val_if_fail (E_IS_EWS_ITEM (item), -1);
2841 g_return_val_if_fail (item->priv->task_fields != NULL, -1);
2842
2843 return item->priv->task_fields->due_date;
2844 }
2845
2846 time_t
2847 e_ews_item_get_start_date (EEwsItem *item)
2848 {
2849 g_return_val_if_fail (E_IS_EWS_ITEM (item), -1);
2850 g_return_val_if_fail (item->priv->task_fields != NULL, -1);
2851
2852 return item->priv->task_fields->start_date;
2853 }
2854
2855 time_t
2856 e_ews_item_get_complete_date (EEwsItem *item)
2857 {
2858 g_return_val_if_fail (E_IS_EWS_ITEM (item), -1);
2859 g_return_val_if_fail (item->priv->task_fields != NULL, -1);
2860
2861 return item->priv->task_fields->complete_date;
2862 }
2863
2864 const gchar *
2865 e_ews_item_get_sensitivity (EEwsItem *item)
2866 {
2867 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2868 g_return_val_if_fail (item->priv->task_fields != NULL, NULL);
2869
2870 return item->priv->task_fields->sensitivity;
2871 }
2872
2873 const gchar *
2874 e_ews_item_get_body (EEwsItem *item)
2875 {
2876 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2877
2878 if (item->priv->body)
2879 return item->priv->body;
2880
2881 return item->priv->task_fields ? item->priv->task_fields->body : NULL;
2882 }
2883
2884 EEwsBodyType
2885 e_ews_item_get_body_type (EEwsItem *item)
2886 {
2887 g_return_val_if_fail (E_IS_EWS_ITEM (item), E_EWS_BODY_TYPE_ANY);
2888
2889 if (item->priv->body)
2890 return item->priv->body_type;
2891
2892 return E_EWS_BODY_TYPE_ANY;
2893 }
2894
2895 const gchar *
2896 e_ews_item_get_owner (EEwsItem *item)
2897 {
2898 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2899 g_return_val_if_fail (item->priv->task_fields != NULL, NULL);
2900
2901 return item->priv->task_fields->owner;
2902 }
2903
2904 const gchar *
2905 e_ews_item_get_delegator (EEwsItem *item)
2906 {
2907 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2908 g_return_val_if_fail (item->priv->task_fields != NULL, NULL);
2909
2910 return item->priv->task_fields->delegator;
2911 }
2912
2913 gboolean
2914 e_ews_item_task_has_start_date (EEwsItem *item,
2915 gboolean *has_date)
2916 {
2917 g_return_val_if_fail (E_IS_EWS_ITEM (item), FALSE);
2918 g_return_val_if_fail (item->priv->task_fields != NULL, FALSE);
2919
2920 *has_date = item->priv->task_fields->has_start_date;
2921
2922 return TRUE;
2923 }
2924
2925 gboolean
2926 e_ews_item_task_has_due_date (EEwsItem *item,
2927 gboolean *has_date)
2928 {
2929 g_return_val_if_fail (E_IS_EWS_ITEM (item), FALSE);
2930 g_return_val_if_fail (item->priv->task_fields != NULL, FALSE);
2931
2932 *has_date = item->priv->task_fields->has_due_date;
2933
2934 return TRUE;
2935 }
2936
2937 gboolean
2938 e_ews_item_task_has_complete_date (EEwsItem *item,
2939 gboolean *has_date)
2940 {
2941 g_return_val_if_fail (E_IS_EWS_ITEM (item), FALSE);
2942 g_return_val_if_fail (item->priv->task_fields != NULL, FALSE);
2943
2944 *has_date = item->priv->task_fields->has_complete_date;
2945
2946 return TRUE;
2947 }
2948
2949 const gchar *
2950 e_ews_item_get_tzid (EEwsItem *item)
2951 {
2952 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2953
2954 /* can be NULL */
2955 return item->priv->timezone;
2956 }
2957
2958 const gchar *
2959 e_ews_item_get_start_tzid (EEwsItem *item)
2960 {
2961 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2962
2963 /* can be NULL */
2964 return item->priv->start_timezone;
2965 }
2966
2967 const gchar *
2968 e_ews_item_get_end_tzid (EEwsItem *item)
2969 {
2970 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2971
2972 /* can be NULL */
2973 return item->priv->end_timezone;
2974 }
2975
2976 time_t
2977 e_ews_item_get_start (EEwsItem *item)
2978 {
2979 g_return_val_if_fail (E_IS_EWS_ITEM (item), -1);
2980
2981 return item->priv->calendar_start;
2982 }
2983
2984 time_t
2985 e_ews_item_get_end (EEwsItem *item)
2986 {
2987 g_return_val_if_fail (E_IS_EWS_ITEM (item), -1);
2988
2989 return item->priv->calendar_end;
2990 }
2991
2992 const gchar *
2993 e_ews_item_get_contact_photo_id (EEwsItem *item)
2994 {
2995 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
2996
2997 return item->priv->contact_photo_id;
2998 }
2999
3000 /* free returned pointer with e_ews_permission_free() */
3001 EEwsPermission *
3002 e_ews_permission_new (EEwsPermissionUserType user_type,
3003 const gchar *display_name,
3004 const gchar *primary_smtp,
3005 const gchar *sid,
3006 guint32 rights)
3007 {
3008 EEwsPermission *perm;
3009
3010 perm = g_new0 (EEwsPermission, 1);
3011 perm->user_type = user_type;
3012 perm->display_name = g_strdup (display_name);
3013 perm->primary_smtp = g_strdup (primary_smtp);
3014 perm->sid = g_strdup (sid);
3015 perm->rights = rights;
3016
3017 return perm;
3018 }
3019
3020 void
3021 e_ews_permission_free (EEwsPermission *perm)
3022 {
3023 if (!perm)
3024 return;
3025
3026 g_free (perm->display_name);
3027 g_free (perm->primary_smtp);
3028 g_free (perm->sid);
3029 g_free (perm);
3030 }
3031
3032 static void
3033 ews_level_rights_converter (const gchar **plevel_name,
3034 guint32 *prights,
3035 gboolean level_to_rights)
3036 {
3037 struct _known {
3038 const gchar *level_name;
3039 guint32 rights;
3040 } known[] = {
3041 { "None", 0 },
3042 { "Owner", E_EWS_PERMISSION_BIT_READ_ANY |
3043 E_EWS_PERMISSION_BIT_CREATE |
3044 E_EWS_PERMISSION_BIT_CREATE_SUBFOLDER |
3045 E_EWS_PERMISSION_BIT_EDIT_OWNED |
3046 E_EWS_PERMISSION_BIT_EDIT_ANY |
3047 E_EWS_PERMISSION_BIT_DELETE_OWNED |
3048 E_EWS_PERMISSION_BIT_DELETE_ANY |
3049 E_EWS_PERMISSION_BIT_FOLDER_OWNER |
3050 E_EWS_PERMISSION_BIT_FOLDER_CONTACT |
3051 E_EWS_PERMISSION_BIT_FOLDER_VISIBLE },
3052 { "PublishingEditor",
3053 E_EWS_PERMISSION_BIT_READ_ANY |
3054 E_EWS_PERMISSION_BIT_CREATE |
3055 E_EWS_PERMISSION_BIT_CREATE_SUBFOLDER |
3056 E_EWS_PERMISSION_BIT_EDIT_OWNED |
3057 E_EWS_PERMISSION_BIT_EDIT_ANY |
3058 E_EWS_PERMISSION_BIT_DELETE_OWNED |
3059 E_EWS_PERMISSION_BIT_DELETE_ANY |
3060 E_EWS_PERMISSION_BIT_FOLDER_VISIBLE },
3061 { "Editor",
3062 E_EWS_PERMISSION_BIT_READ_ANY |
3063 E_EWS_PERMISSION_BIT_CREATE |
3064 E_EWS_PERMISSION_BIT_EDIT_OWNED |
3065 E_EWS_PERMISSION_BIT_EDIT_ANY |
3066 E_EWS_PERMISSION_BIT_DELETE_OWNED |
3067 E_EWS_PERMISSION_BIT_DELETE_ANY |
3068 E_EWS_PERMISSION_BIT_FOLDER_VISIBLE },
3069 { "PublishingAuthor",
3070 E_EWS_PERMISSION_BIT_READ_ANY |
3071 E_EWS_PERMISSION_BIT_CREATE |
3072 E_EWS_PERMISSION_BIT_CREATE_SUBFOLDER |
3073 E_EWS_PERMISSION_BIT_EDIT_OWNED |
3074 E_EWS_PERMISSION_BIT_DELETE_OWNED |
3075 E_EWS_PERMISSION_BIT_FOLDER_VISIBLE },
3076 { "Author",
3077 E_EWS_PERMISSION_BIT_READ_ANY |
3078 E_EWS_PERMISSION_BIT_CREATE |
3079 E_EWS_PERMISSION_BIT_EDIT_OWNED |
3080 E_EWS_PERMISSION_BIT_DELETE_OWNED |
3081 E_EWS_PERMISSION_BIT_FOLDER_VISIBLE },
3082 { "NoneditingAuthor",
3083 E_EWS_PERMISSION_BIT_READ_ANY |
3084 E_EWS_PERMISSION_BIT_CREATE |
3085 E_EWS_PERMISSION_BIT_DELETE_OWNED |
3086 E_EWS_PERMISSION_BIT_FOLDER_VISIBLE },
3087 { "Reviewer",
3088 E_EWS_PERMISSION_BIT_READ_ANY |
3089 E_EWS_PERMISSION_BIT_FOLDER_VISIBLE },
3090 { "Contributor",
3091 E_EWS_PERMISSION_BIT_CREATE |
3092 E_EWS_PERMISSION_BIT_FOLDER_VISIBLE },
3093 { "FreeBusyTimeOnly",
3094 E_EWS_PERMISSION_BIT_FREE_BUSY_SIMPLE },
3095 { "FreeBusyTimeAndSubjectAndLocation",
3096 E_EWS_PERMISSION_BIT_FREE_BUSY_DETAILED }
3097
3098 };
3099 gint ii;
3100 guint32 rights;
3101
3102 g_return_if_fail (plevel_name != NULL);
3103 g_return_if_fail (prights != NULL);
3104
3105 rights = (*prights) & ~(E_EWS_PERMISSION_BIT_FREE_BUSY_SIMPLE | E_EWS_PERMISSION_BIT_FREE_BUSY_DETAILED);
3106
3107 for (ii = 0; ii < G_N_ELEMENTS (known); ii++) {
3108 if (level_to_rights) {
3109 if (g_strcmp0 (*plevel_name, known[ii].level_name) == 0) {
3110 *prights = known[ii].rights;
3111 return;
3112 }
3113 } else {
3114 if (*prights == known[ii].rights || (rights && rights == known[ii].rights)) {
3115 *plevel_name = known[ii].level_name;
3116 return;
3117 }
3118 }
3119 }
3120
3121 /* here lefts only "Custom" */
3122 if (level_to_rights)
3123 *prights = 0;
3124 else
3125 *plevel_name = "Custom";
3126 }
3127
3128 /* converts user rights to level name suitable for PermissionLevel/CalendarPermissionLevel */
3129 const gchar *
3130 e_ews_permission_rights_to_level_name (guint32 rights)
3131 {
3132 const gchar *level_name = NULL;
3133
3134 ews_level_rights_converter (&level_name, &rights, FALSE);
3135
3136 return level_name;
3137 }
3138
3139 /* converts PermissionLevel/CalendarPermissionLevel name to user rights */
3140 guint32
3141 e_ews_permission_level_name_to_rights (const gchar *level_name)
3142 {
3143 guint32 rights = 0;
3144
3145 ews_level_rights_converter (&level_name, &rights, TRUE);
3146
3147 return rights;
3148 }
3149
3150 static EEwsPermission *
3151 ews_permissions_parse (ESoapParameter *param)
3152 {
3153 EEwsPermission *res;
3154 ESoapParameter *node, *subnode;
3155 EEwsPermissionUserType user_type;
3156 gchar *value, *display_name = NULL, *primary_smtp = NULL, *sid = NULL;
3157 guint32 rights = 0;
3158
3159 g_return_val_if_fail (param != NULL, NULL);
3160
3161 node = e_soap_parameter_get_first_child_by_name (param, "UserId");
3162 if (!node)
3163 return NULL;
3164
3165 subnode = e_soap_parameter_get_first_child_by_name (node, "DistinguishedUser");
3166 if (subnode) {
3167 value = e_soap_parameter_get_string_value (subnode);
3168 if (g_strcmp0 (value, "Anonymous") == 0) {
3169 user_type = E_EWS_PERMISSION_USER_TYPE_ANONYMOUS;
3170 } else if (g_strcmp0 (value, "Default") == 0) {
3171 user_type = E_EWS_PERMISSION_USER_TYPE_DEFAULT;
3172 } else {
3173 g_free (value);
3174 return NULL;
3175 }
3176
3177 g_free (value);
3178 } else {
3179 user_type = E_EWS_PERMISSION_USER_TYPE_REGULAR;
3180 }
3181
3182 subnode = e_soap_parameter_get_first_child_by_name (node, "SID");
3183 if (subnode)
3184 sid = e_soap_parameter_get_string_value (subnode);
3185
3186 subnode = e_soap_parameter_get_first_child_by_name (node, "PrimarySmtpAddress");
3187 if (subnode)
3188 primary_smtp = e_soap_parameter_get_string_value (subnode);
3189
3190 subnode = e_soap_parameter_get_first_child_by_name (node, "DisplayName");
3191 if (subnode)
3192 display_name = e_soap_parameter_get_string_value (subnode);
3193
3194 node = e_soap_parameter_get_first_child_by_name (param, "PermissionLevel");
3195 if (!node)
3196 node = e_soap_parameter_get_first_child_by_name (param, "CalendarPermissionLevel");
3197
3198 if (node) {
3199 value = e_soap_parameter_get_string_value (node);
3200 rights = e_ews_permission_level_name_to_rights (value);
3201 g_free (value);
3202 }
3203
3204 node = e_soap_parameter_get_first_child_by_name (param, "CanCreateItems");
3205 if (node) {
3206 value = e_soap_parameter_get_string_value (node);
3207 if (g_strcmp0 (value, "true") == 0)
3208 rights |= E_EWS_PERMISSION_BIT_CREATE;
3209 g_free (value);
3210 }
3211
3212 node = e_soap_parameter_get_first_child_by_name (param, "CanCreateSubFolders");
3213 if (node) {
3214 value = e_soap_parameter_get_string_value (node);
3215 if (g_strcmp0 (value, "true") == 0)
3216 rights |= E_EWS_PERMISSION_BIT_CREATE_SUBFOLDER;
3217 g_free (value);
3218 }
3219
3220 node = e_soap_parameter_get_first_child_by_name (param, "IsFolderOwner");
3221 if (node) {
3222 value = e_soap_parameter_get_string_value (node);
3223 if (g_strcmp0 (value, "true") == 0)
3224 rights |= E_EWS_PERMISSION_BIT_FOLDER_OWNER;
3225 g_free (value);
3226 }
3227
3228 node = e_soap_parameter_get_first_child_by_name (param, "IsFolderVisible");
3229 if (node) {
3230 value = e_soap_parameter_get_string_value (node);
3231 if (g_strcmp0 (value, "true") == 0)
3232 rights |= E_EWS_PERMISSION_BIT_FOLDER_VISIBLE;
3233 g_free (value);
3234 }
3235
3236 node = e_soap_parameter_get_first_child_by_name (param, "IsFolderContact");
3237 if (node) {
3238 value = e_soap_parameter_get_string_value (node);
3239 if (g_strcmp0 (value, "true") == 0)
3240 rights |= E_EWS_PERMISSION_BIT_FOLDER_CONTACT;
3241 g_free (value);
3242 }
3243
3244 node = e_soap_parameter_get_first_child_by_name (param, "EditItems");
3245 if (node) {
3246 value = e_soap_parameter_get_string_value (node);
3247 if (g_strcmp0 (value, "None") == 0)
3248 rights |= 0;
3249 else if (g_strcmp0 (value, "Owned") == 0)
3250 rights |= E_EWS_PERMISSION_BIT_EDIT_OWNED;
3251 else if (g_strcmp0 (value, "All") == 0)
3252 rights |= E_EWS_PERMISSION_BIT_EDIT_ANY;
3253 g_free (value);
3254 }
3255
3256 node = e_soap_parameter_get_first_child_by_name (param, "DeleteItems");
3257 if (node) {
3258 value = e_soap_parameter_get_string_value (node);
3259 if (g_strcmp0 (value, "None") == 0)
3260 rights |= 0;
3261 else if (g_strcmp0 (value, "Owned") == 0)
3262 rights |= E_EWS_PERMISSION_BIT_DELETE_OWNED;
3263 else if (g_strcmp0 (value, "All") == 0)
3264 rights |= E_EWS_PERMISSION_BIT_DELETE_ANY;
3265 g_free (value);
3266 }
3267
3268 node = e_soap_parameter_get_first_child_by_name (param, "ReadItems");
3269 if (node) {
3270 value = e_soap_parameter_get_string_value (node);
3271 if (g_strcmp0 (value, "None") == 0)
3272 rights |= 0;
3273 else if (g_strcmp0 (value, "TimeOnly") == 0)
3274 rights |= E_EWS_PERMISSION_BIT_FREE_BUSY_SIMPLE;
3275 else if (g_strcmp0 (value, "TimeAndSubjectAndLocation") == 0)
3276 rights |= E_EWS_PERMISSION_BIT_FREE_BUSY_DETAILED;
3277 else if (g_strcmp0 (value, "FullDetails") == 0)
3278 rights |= E_EWS_PERMISSION_BIT_READ_ANY;
3279 g_free (value);
3280 }
3281
3282 res = e_ews_permission_new (user_type, display_name, primary_smtp, sid, rights);
3283
3284 g_free (display_name);
3285 g_free (primary_smtp);
3286 g_free (sid);
3287
3288 return res;
3289 }
3290
3291 /* Returns GSList of EEwsPermission * objects, as read from @param.
3292 * Returned GSList should be freed with e_ews_permissions_free()
3293 * when done with it. Returns NULL when no permissions recognized
3294 * from @param.
3295 */
3296 GSList *
3297 e_ews_permissions_from_soap_param (ESoapParameter *param)
3298 {
3299 GSList *perms = NULL;
3300 ESoapParameter *node;
3301 const gchar *name;
3302
3303 g_return_val_if_fail (param != NULL, NULL);
3304
3305 name = e_soap_parameter_get_name (param);
3306 if (g_ascii_strcasecmp (name, "Permissions") == 0 ||
3307 g_ascii_strcasecmp (name, "CalendarPermissions") == 0) {
3308 node = param;
3309 } else {
3310 node = e_soap_parameter_get_first_child_by_name (param, "Permissions");
3311 if (!node)
3312 node = e_soap_parameter_get_first_child_by_name (param, "CalendarPermissions");
3313 if (!node)
3314 return NULL;
3315 }
3316
3317 for (node = e_soap_parameter_get_first_child (node);
3318 node;
3319 node = e_soap_parameter_get_next_child (node)) {
3320 name = e_soap_parameter_get_name (node);
3321 if (g_ascii_strcasecmp (name, "Permission") == 0 ||
3322 g_ascii_strcasecmp (name, "CalendarPermission") == 0) {
3323 EEwsPermission *perm;
3324
3325 perm = ews_permissions_parse (node);
3326 if (perm) {
3327 perms = g_slist_prepend (perms, perm);
3328 }
3329 }
3330 }
3331
3332 return perms ? g_slist_reverse (perms) : NULL;
3333 }
3334
3335 void
3336 e_ews_permissions_free (GSList *permissions)
3337 {
3338 g_slist_free_full (permissions, (GDestroyNotify) e_ews_permission_free);
3339 }
3340
3341 /* strips ex_address by its LDAP-like part, returning position in ex_address where
3342 common name begins; returns whole ex_address, if not found */
3343 const gchar *
3344 e_ews_item_util_strip_ex_address (const gchar *ex_address)
3345 {
3346 const gchar *ptr;
3347
3348 if (!ex_address)
3349 return ex_address;
3350
3351 ptr = strrchr (ex_address, '/');
3352 if (ptr && g_ascii_strncasecmp (ptr, "/cn=", 4) == 0) {
3353 return ptr + 4;
3354 }
3355
3356 return ex_address;
3357 }
3358
3359 EwsId *
3360 e_ews_id_copy (const EwsId *ews_id)
3361 {
3362 EwsId *copy;
3363
3364 if (!ews_id)
3365 return NULL;
3366
3367 copy = g_new0 (EwsId, 1);
3368 copy->id = g_strdup (ews_id->id);
3369 copy->change_key = g_strdup (ews_id->change_key);
3370
3371 return copy;
3372 }
3373
3374 void
3375 e_ews_id_free (EwsId *ews_id)
3376 {
3377 if (ews_id) {
3378 g_free (ews_id->id);
3379 g_free (ews_id->change_key);
3380 g_free (ews_id);
3381 }
3382 }