Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / servers / groupwise / e-gw-item.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* 
3  * Authors : 
4  *  JP Rosevear <jpr@ximian.com>
5  *  Rodrigo Moya <rodrigo@ximian.com>
6  *  Harish Krishnaswamy <kharish@novell.com>
7  * Copyright 2003, Novell, Inc.
8  *
9  * This program is free software; you can redistribute it and/or 
10  * modify it under the terms of version 2 of the GNU Lesser General Public 
11  * License as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21  * USA
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 #include <string.h>
28 #include <glib.h>
29 #include <glib/gprintf.h>
30 #include <libsoup/soup-misc.h>
31 #include "e-gw-item.h"
32 #include "e-gw-connection.h"
33 #include "e-gw-message.h"
34
35 struct _EGwItemPrivate {
36         EGwItemType item_type;
37         char *container;
38         GList *category_list; /*list of category ids*/
39
40         /* properties */
41         char *id;
42         char *creation_date;
43         char *delivered_date;
44         char *start_date;
45         char *end_date;
46         char *due_date;
47         char *completed_date;
48         gboolean completed;
49         gboolean is_allday_event;
50         char *subject;
51         char *message;
52         char *classification;
53         char *accept_level;
54         char *priority;
55         char *task_priority;
56         char *place;
57         char *source ;
58         char *security;
59         GSList *recipient_list;
60         GSList *recurrence_dates;
61         GSList *exdate_list;
62         EGwItemRecurrenceRule *rrule;
63         int recurrence_key;
64         int trigger; /* alarm */
65         /*message size*/
66         int size;    
67         EGwItemOrganizer *organizer;
68
69         /*properties for mail*/
70         char *from ;
71         char *to ;
72         char *content_type ;
73         char *msg_body_id;
74         int item_status;
75         /*Attachments*/
76         gboolean has_attachment;
77         GSList *attach_list ; 
78         /*linkInfo for replies*/
79         EGwItemLinkInfo *link_info; 
80
81         /* properties for tasks/calendars */
82         char *icalid;
83         /* if the self is not the organizer of the item, the 
84          * status is not reflected in the recipientStatus.
85          * Hence it should be gleaned from the 'status' element
86          * of the Mail, the parent item.*/
87         guint32 self_status;
88         /* properties for category items*/
89         char *category_name;
90
91         /* properties for contacts */
92         FullName *full_name;
93         GList *email_list;
94         GList *im_list;
95         GHashTable *simple_fields;
96         GList *member_list;
97         GHashTable *addresses;
98
99         /***** Send Options *****/
100         
101         gboolean set_sendoptions;
102         /* Reply Request */
103         char *reply_within;
104         gboolean reply_request_set;
105         
106         /* Status Tracking through sent Item */
107         EGwItemTrack track_info;
108         gboolean autodelete;
109
110         /* Return Notification */
111         EGwItemReturnNotify notify_completed;
112         EGwItemReturnNotify notify_accepted;
113         EGwItemReturnNotify notify_declined;
114         EGwItemReturnNotify notify_opened;
115         EGwItemReturnNotify notify_deleted;
116
117         /* Expiration Date */
118         char *expires;
119
120         /* Delay delivery */
121         char *delay_until;
122
123         /* changes */
124         GHashTable *additions;
125         GHashTable *updates;
126         GHashTable *deletions;  
127         /*padding*/
128         unsigned int padding[10];
129 };
130
131 static GObjectClass *parent_class = NULL;
132
133 static void
134 free_recipient (EGwItemRecipient *recipient, gpointer data)
135 {
136         g_free (recipient->email);
137         g_free (recipient->display_name);
138         g_free (recipient->delivered_date);
139         g_free (recipient->opened_date);
140         g_free (recipient->accepted_date);
141         g_free (recipient->deleted_date);
142         g_free (recipient->declined_date);
143         g_free (recipient->completed_date);
144         g_free (recipient->undelivered_date);
145         g_free (recipient);
146 }
147
148 static void 
149 free_postal_address (gpointer  postal_address)
150 {
151         PostalAddress *address;
152         address = (PostalAddress *) postal_address;
153         if (address) {
154                 g_free (address->street_address);
155                 g_free (address->location);
156                 g_free(address->city);
157                 g_free(address->country);
158                 g_free(address->state);
159                 g_free(address->postal_code);
160                 g_free(address);
161         }
162 }
163
164 static void 
165 free_full_name (gpointer full_name)
166 {
167         FullName *name = (FullName *) full_name;
168         g_free (name->name_prefix);
169         g_free (name->first_name);
170         g_free (name->middle_name);
171         g_free (name->last_name);
172         g_free (name->name_suffix);
173         g_free (name);
174
175 }
176
177 static void 
178 free_string (gpointer s, gpointer data)
179 {
180         if (s)
181                 free (s);
182 }
183
184 static void
185 free_attach (gpointer s, gpointer data) 
186 {
187         EGwItemAttachment *attach = (EGwItemAttachment *) s ;
188         if (attach) {
189                 if (attach->id) 
190                         g_free (attach->id), attach->id = NULL ;
191                 if (attach->name)
192                         g_free (attach->name), attach->name = NULL ;
193                 if (attach->contentid)
194                         g_free (attach->contentid), attach->contentid= NULL ;
195                 if (attach->contentType)
196                         g_free (attach->contentType), attach->contentType = NULL ;
197                 if (attach->date)
198                         g_free (attach->date), attach->date = NULL ;
199                 if (attach->data)
200                         g_free (attach->data), attach->data = NULL ;
201         
202                 g_free(attach) ;
203         }
204         
205 }
206 static void 
207 free_member (gpointer member, gpointer data)
208 {
209         EGroupMember *group_member = (EGroupMember *) member;
210         if (group_member->id)
211                 g_free (group_member->id);
212         if (group_member->email)
213                 g_free (group_member->email);
214         if (group_member->name)
215                 g_free (group_member->name);
216         g_free (group_member);
217 }
218
219 static void 
220 free_im_address ( gpointer address, gpointer data)
221 {
222         IMAddress *im_address;
223         im_address = (IMAddress *) address;
224         
225         if (im_address) {
226                 if (im_address->service)
227                         g_free (im_address->service);
228                 if (im_address->address)
229                         g_free (im_address->address);
230                 g_free (im_address);
231         }
232 }
233
234 static void
235 free_link_info (EGwItemLinkInfo *info)
236 {
237         if (info) {
238                 if (info->id )
239                         g_free (info->id), info->id = NULL;
240                 if (info->type)
241                         g_free (info->type), info->type = NULL;
242                 if (info->thread)
243                         g_free (info->thread), info->thread = NULL;
244                 g_free (info);
245                 info = NULL;
246         }
247 }
248
249 static void 
250 free_changes ( GHashTable *changes)
251 {
252         gpointer value;
253         if (!changes)
254                 return;
255         value = g_hash_table_lookup (changes, "full_name");
256         if (value)
257                 free_full_name (value);
258         value = g_hash_table_lookup (changes, "email");
259         if (value)
260                 g_list_free ((GList*) value);
261         value = g_hash_table_lookup (changes, "ims");
262         if (value)
263                 g_list_free ((GList*) value);
264         value = g_hash_table_lookup (changes, "Home");
265         if (value)
266                 free_postal_address (value);
267         value = g_hash_table_lookup (changes, "Office");
268         if (value)
269                 free_postal_address (value);
270         g_hash_table_destroy (changes);
271 }
272 static void
273 e_gw_item_dispose (GObject *object)
274 {
275         EGwItem *item = (EGwItem *) object;
276         EGwItemPrivate *priv;
277
278         g_return_if_fail (E_IS_GW_ITEM (item));
279
280         priv = item->priv;
281         if (priv) {
282                 if (priv->container) {
283                         g_free (priv->container);
284                         priv->container = NULL;
285                 }
286
287                 if (priv->id) {
288                         g_free (priv->id);
289                         priv->id = NULL;
290                 }
291
292                 if (priv->subject) {
293                         g_free (priv->subject);
294                         priv->subject = NULL;
295                 }
296
297                 if (priv->message) {
298                         g_free (priv->message);
299                         priv->message = NULL;
300                 }
301
302                 if (priv->classification) {
303                         g_free (priv->classification);
304                         priv->classification = NULL;
305                 }
306
307                 if (priv->accept_level) {
308                         g_free (priv->accept_level);
309                         priv->accept_level = NULL;
310                 }
311
312                 if (priv->priority) {
313                         g_free (priv->priority);
314                         priv->priority = NULL;
315                 }
316                         
317                 if (priv->task_priority) {
318                         g_free (priv->task_priority);
319                         priv->task_priority = NULL;
320                 }
321                 
322                 if (priv->place) {
323                         g_free (priv->place);
324                         priv->place = NULL;
325                 }
326
327                 if (priv->from) {
328                         g_free (priv->from) ;
329                         priv->from = NULL ;
330                 }
331
332                 if (priv->to) {
333                         g_free (priv->to) ;
334                         priv->to = NULL ;
335                 }
336                 
337                 if (priv->content_type) {
338                         g_free (priv->content_type) ;
339                         priv->content_type = NULL ;
340                 }
341                 if (priv->msg_body_id) {
342                         g_free (priv->msg_body_id);
343                         priv->msg_body_id = NULL;
344                 }
345
346                 if (priv->icalid) {
347                         g_free (priv->icalid);
348                         priv->icalid = NULL;
349                 }
350
351                 if (priv->reply_within) {
352                         g_free (priv->reply_within);
353                         priv->reply_within = NULL;
354                 }
355
356                 if (priv->expires) {
357                         g_free (priv->expires);
358                         priv->expires = NULL;
359                 }
360
361                 if (priv->delay_until) {
362                         g_free (priv->delay_until);
363                         priv->delay_until = NULL;
364                 }
365
366                 if (priv->recipient_list) {
367                         g_slist_foreach (priv->recipient_list, (GFunc) free_recipient, NULL);
368                         g_slist_free (priv->recipient_list);
369                         priv->recipient_list = NULL;
370                 }       
371                 
372                 if (priv->organizer) {
373                         g_free (priv->organizer->display_name);
374                         g_free (priv->organizer->email);
375                         priv->organizer = NULL;
376                 }
377                 
378                 if (priv->recurrence_dates) {
379                         g_slist_foreach (priv->recurrence_dates, free_string, NULL);
380                         g_slist_free (priv->recurrence_dates);
381                         priv->recurrence_dates = NULL;
382                 }
383
384                 if (priv->exdate_list) {
385                         g_slist_foreach (priv->exdate_list, free_string, NULL);
386                         g_slist_free (priv->exdate_list);
387                         priv->exdate_list = NULL;
388                 }
389
390                 if (priv->rrule) {
391                         /*TODO free all the strings */
392                         priv->rrule = NULL;
393                 }
394
395                 if (priv->full_name) {
396                         free_full_name (priv->full_name);
397                         priv->full_name = NULL;
398                         }
399                 
400                 if (priv->simple_fields)
401                         g_hash_table_destroy (priv->simple_fields);
402                 
403                 if (priv->addresses)
404                         g_hash_table_destroy (priv->addresses);
405                 
406                 if (priv->email_list) {
407                         g_list_foreach (priv->email_list,  free_string , NULL);
408                         g_list_free (priv->email_list);
409                         priv->email_list = NULL;
410                 }
411                 
412                 if (priv->member_list) {
413                         g_list_foreach (priv->member_list,  free_member, NULL);
414                         g_list_free (priv->member_list);
415                         priv->member_list = NULL;
416                 }
417
418                 if (priv->im_list) {
419                         g_list_foreach (priv->im_list, free_im_address, NULL);
420                         g_list_free (priv->im_list);
421                         priv->im_list = NULL;
422                 }
423                 
424                 if (priv->category_list) {
425                         g_list_foreach (priv->category_list,  free_string, NULL);
426                         g_list_free (priv->category_list);
427                         priv->category_list = NULL;
428                 }
429                 
430                 if(priv->attach_list) {
431                         g_slist_foreach (priv->attach_list, free_attach, NULL); 
432                         g_slist_free (priv->attach_list) ;
433                         priv->attach_list = NULL ;
434                 }
435                 
436                 if (priv->category_name) {
437                         g_free (priv->category_name);
438                         priv->category_name = NULL;
439                 }
440                 
441                 if (priv->source) {
442                         g_free (priv->source);
443                         priv->source = NULL;
444                 }
445                 
446                 if (priv->link_info) {
447                         free_link_info (priv->link_info);
448                         priv->link_info = NULL;
449                 }
450
451                 if (priv->end_date) {
452                         g_free (priv->end_date);
453                         priv->end_date = NULL;
454                 }       
455                 
456                 free_changes (priv->additions);
457                 free_changes (priv->deletions);
458                 free_changes (priv->updates);
459                 
460         }
461
462         if (parent_class->dispose)
463                 (* parent_class->dispose) (object);
464 }
465
466 static void
467 e_gw_item_finalize (GObject *object)
468 {
469         EGwItem *item = (EGwItem *) object;
470         EGwItemPrivate *priv;
471
472         g_return_if_fail (E_IS_GW_ITEM (item));
473
474         priv = item->priv;
475
476         /* clean up */
477         g_free (priv);
478         item->priv = NULL;
479
480         if (parent_class->finalize)
481                 (* parent_class->finalize) (object);
482 }
483
484 static void
485 e_gw_item_class_init (EGwItemClass *klass)
486 {
487         GObjectClass *object_class = G_OBJECT_CLASS (klass);
488
489         parent_class = g_type_class_peek_parent (klass);
490
491         object_class->dispose = e_gw_item_dispose;
492         object_class->finalize = e_gw_item_finalize;
493 }
494
495 static void
496 e_gw_item_init (EGwItem *item, EGwItemClass *klass)
497 {
498         EGwItemPrivate *priv;
499
500         /* allocate internal structure */
501         priv = g_new0 (EGwItemPrivate, 1);
502         priv->item_type = E_GW_ITEM_TYPE_UNKNOWN;
503         priv->creation_date = NULL;
504         priv->delivered_date = NULL;
505         priv->start_date = NULL;
506         priv->end_date = NULL; 
507         priv->due_date = NULL; 
508         priv->completed_date = NULL;
509         priv->trigger = 0;
510         priv->recipient_list = NULL;
511         priv->organizer = NULL;
512         priv->recurrence_dates = NULL;
513         priv->completed = FALSE;
514         priv->is_allday_event = FALSE;
515         priv->im_list = NULL;
516         priv->email_list = NULL;
517         priv->member_list = NULL;
518         priv->category_list = NULL;
519         priv->reply_within = NULL;
520         priv->reply_request_set = FALSE;
521         priv->autodelete = FALSE;
522         priv->set_sendoptions = FALSE;
523         priv->expires = NULL;
524         priv->delay_until = NULL;
525         priv->attach_list = NULL ;
526         priv->simple_fields = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_free);
527         priv->full_name = g_new0(FullName, 1);
528         priv->addresses = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, free_postal_address);
529         priv->additions = g_hash_table_new(g_str_hash, g_str_equal);
530         priv->updates =   g_hash_table_new (g_str_hash, g_str_equal);
531         priv->deletions = g_hash_table_new (g_str_hash, g_str_equal);
532         priv->self_status = 0;
533         priv->link_info = NULL;
534         priv->msg_body_id = NULL;
535         priv->has_attachment = FALSE;
536         item->priv = priv;
537         
538         
539 }
540
541 GType
542 e_gw_item_get_type (void)
543 {
544         static GType type = 0;
545
546         if (!type) {
547                 static GTypeInfo info = {
548                         sizeof (EGwItemClass),
549                         (GBaseInitFunc) NULL,
550                         (GBaseFinalizeFunc) NULL,
551                         (GClassInitFunc) e_gw_item_class_init,
552                         NULL, NULL,
553                         sizeof (EGwItem),
554                         0,
555                         (GInstanceInitFunc) e_gw_item_init
556                 };
557                 type = g_type_register_static (G_TYPE_OBJECT, "EGwItem", &info, 0);
558         }
559
560         return type;
561 }
562
563 void 
564 e_gw_item_free_cal_id (EGwItemCalId *calid)
565 {
566         if (calid->item_id) {
567                 g_free (calid->item_id);
568                 calid->item_id = NULL;
569         }
570         
571         if (calid->ical_id) {
572                 g_free (calid->ical_id);
573                 calid->ical_id = NULL;
574         }
575         
576         if (calid->recur_key) {
577                 g_free (calid->recur_key);
578                 calid->recur_key = NULL;
579         }
580
581         g_free (calid);
582 }
583
584 EGwItem *
585 e_gw_item_new_empty (void)
586 {
587         return g_object_new (E_TYPE_GW_ITEM, NULL);
588 }
589
590 static void 
591 set_recipient_list_from_soap_parameter (EGwItem *item, SoupSoapParameter *param)
592 {
593         SoupSoapParameter *param_recipient;
594         char *email, *cn;
595         EGwItemRecipient *recipient;
596         GList *email_list;
597
598         email_list = e_gw_item_get_email_list (item);
599         
600         for (param_recipient = soup_soap_parameter_get_first_child_by_name (param, "recipient");
601              param_recipient != NULL;
602              param_recipient = soup_soap_parameter_get_next_child_by_name (param_recipient, "recipient")) {
603                 SoupSoapParameter *subparam;
604
605                 recipient = g_new0 (EGwItemRecipient, 1);       
606                 subparam = soup_soap_parameter_get_first_child_by_name (param_recipient, "email");
607                 if (subparam) {
608                         email = soup_soap_parameter_get_string_value (subparam);
609                         if (email)
610                                 recipient->email = email;
611                 }        
612                 subparam = soup_soap_parameter_get_first_child_by_name (param_recipient, "displayName");
613                 if (subparam) {
614                         cn = soup_soap_parameter_get_string_value (subparam);
615                         if (cn)
616                                 recipient->display_name = cn;
617                 }
618                 
619                 subparam = soup_soap_parameter_get_first_child_by_name (param_recipient, "distType");
620                 if (subparam) {
621                         const char *dist_type;
622                         dist_type = soup_soap_parameter_get_string_value (subparam);
623                         if (!strcmp (dist_type, "TO")) 
624                                 recipient->type = E_GW_ITEM_RECIPIENT_TO;
625                         else if (!strcmp (dist_type, "CC"))
626                                 recipient->type = E_GW_ITEM_RECIPIENT_CC;
627                         else if (!strcmp (dist_type, "BC"))
628                                 recipient->type = E_GW_ITEM_RECIPIENT_BC;
629                         else
630                                 recipient->type = E_GW_ITEM_RECIPIENT_NONE;
631                 }
632                 /*FIXME  gw recipientTypes need to be added after the server is fixed. */
633
634                 
635                 /* update Recipient Status
636                  look for accepted/declined and update the item else set it
637                  to none. */
638                 subparam = soup_soap_parameter_get_first_child_by_name (param_recipient, "recipientStatus");
639                 if (subparam) {
640                         char *formatted_date, *value; 
641                         SoupSoapParameter *temp_param ;
642
643                         recipient->status_enabled = TRUE;
644                         if ( (temp_param = soup_soap_parameter_get_first_child_by_name (subparam, "deleted")) ) {
645                                 recipient->status = E_GW_ITEM_STAT_DELETED;
646                                 value = soup_soap_parameter_get_string_value (temp_param);
647                                 formatted_date = e_gw_connection_format_date_string (value);
648                                 recipient->deleted_date = g_strdup (formatted_date);
649                                 g_free (value), value = NULL;
650                                 g_free (formatted_date), formatted_date = NULL;
651                         } 
652                         if ( (temp_param = soup_soap_parameter_get_first_child_by_name (subparam, "declined")) ) {
653                                 recipient->status = E_GW_ITEM_STAT_DECLINED;
654                                 value = soup_soap_parameter_get_string_value (temp_param);
655                                 formatted_date = e_gw_connection_format_date_string (value);
656                                 recipient->declined_date = g_strdup (formatted_date);
657                                 g_free (value), value = NULL;
658                                 g_free (formatted_date), formatted_date = NULL;
659
660                         } if ( (temp_param = soup_soap_parameter_get_first_child_by_name (subparam, "accepted"))) {
661                                 recipient->status = E_GW_ITEM_STAT_ACCEPTED;
662                                 value = soup_soap_parameter_get_string_value (temp_param);
663                                 formatted_date = e_gw_connection_format_date_string (value);
664                                 recipient->accepted_date = g_strdup (formatted_date);
665                                 g_free (value), value = NULL;
666                                 g_free (formatted_date), formatted_date = NULL;
667                         } else  
668                                 recipient->status = E_GW_ITEM_STAT_NONE;
669                         temp_param = soup_soap_parameter_get_first_child_by_name (subparam, "completed");
670                         if (temp_param) {
671                                 value = soup_soap_parameter_get_string_value (temp_param);
672                                 formatted_date = e_gw_connection_format_date_string (value);
673                                 e_gw_item_set_completed_date (item, formatted_date);
674                                 recipient->completed_date = g_strdup (formatted_date);
675                                 g_free (value), value = NULL;
676                                 g_free (formatted_date), formatted_date = NULL;
677                         }
678                         if ( (temp_param = soup_soap_parameter_get_first_child_by_name (subparam, "opened"))) {
679                                 value = soup_soap_parameter_get_string_value (temp_param);
680                                 formatted_date = e_gw_connection_format_date_string (value);
681                                 recipient->opened_date = g_strdup (formatted_date);
682                                 g_free (value), value = NULL;
683                                 g_free (formatted_date), formatted_date = NULL;
684                         }
685                         if ( ( temp_param = soup_soap_parameter_get_first_child_by_name (subparam, "delivered"))) {
686                                 value = soup_soap_parameter_get_string_value (temp_param);
687                                 formatted_date = e_gw_connection_format_date_string (value);
688                                 recipient->delivered_date = g_strdup (formatted_date);
689                                 g_free (value), value = NULL;
690                                 g_free (formatted_date), formatted_date = NULL;
691                         }
692                         if ( (temp_param = soup_soap_parameter_get_first_child_by_name (subparam, "undeliverable_date"))) {
693                                 value = soup_soap_parameter_get_string_value (temp_param);
694                                 formatted_date = e_gw_connection_format_date_string (value);
695                                 recipient->undelivered_date = g_strdup (formatted_date);
696                                 g_free (value), value = NULL;
697                                 g_free (formatted_date), formatted_date = NULL;
698                         }
699                 }
700                 else {
701                         /* if recipientStatus is not provided, use the
702                          * self_status, obtained from the mail properties. */
703                         if (!strcmp ((const char *)email_list->data, recipient->email)) 
704                                 recipient->status = item->priv->self_status & (E_GW_ITEM_STAT_DECLINED | 
705                                                                                 E_GW_ITEM_STAT_ACCEPTED);
706                         else
707                                 recipient->status = E_GW_ITEM_STAT_NONE;
708                 }
709                 
710                 item->priv->recipient_list = g_slist_append (item->priv->recipient_list, recipient);
711         }        
712 }
713 static EGwItemReturnNotify
714 get_notification_value (SoupSoapParameter *param, const char *param_name)
715 {
716         SoupSoapParameter *subparam;
717         
718         if ((subparam = soup_soap_parameter_get_first_child_by_name (param, param_name))) {
719                 char *value = NULL;
720                 
721                 subparam = soup_soap_parameter_get_first_child_by_name (subparam, "mail");
722                 if (subparam)
723                         value = soup_soap_parameter_get_string_value (subparam);
724                 if (value && !g_ascii_strcasecmp (value, "1")) {
725                         g_free (value), value = NULL;
726                         return E_GW_ITEM_NOTIFY_MAIL;
727                 }
728                 g_free (value), value = NULL;
729         }
730         return E_GW_ITEM_NOTIFY_NONE;
731 }
732
733 static void
734 set_sendoptions_from_soap_parameter (EGwItem *item, SoupSoapParameter *param)
735 {
736         EGwItemPrivate *priv;
737         SoupSoapParameter *subparam, *child;
738         char *value = NULL;
739
740         priv = item->priv;
741
742         if ( (subparam = soup_soap_parameter_get_first_child_by_name (param, "requestReply")) ) {               
743                 child = soup_soap_parameter_get_first_child_by_name (subparam, "whenConvenient");
744                 if (child) {
745                         value = soup_soap_parameter_get_string_value (child);
746                         if (value && !g_ascii_strcasecmp (value, "1")) 
747                                 priv->reply_request_set = TRUE;
748                 }       
749
750                 if (!priv->reply_request_set) {
751                         child = soup_soap_parameter_get_first_child_by_name (subparam, "byDate");
752                         
753                         if (child)
754                                 value = soup_soap_parameter_get_string_value (child);
755                         if (value) {
756                                 char *date;
757                                 date = e_gw_connection_format_date_string (value);
758                                 priv->reply_request_set = TRUE;
759                                 priv->reply_within = date;
760                         }
761
762                 }
763                 g_free (value), value = NULL;
764         }       
765
766         if ( (subparam = soup_soap_parameter_get_first_child_by_name (param, "statusTracking"))) {
767                 value = soup_soap_parameter_get_string_value (subparam);
768                if (value) {
769                        if (!g_ascii_strcasecmp (value, "Delivered")) 
770                                priv->track_info = E_GW_ITEM_DELIVERED;
771                        else if (!g_ascii_strcasecmp (value, "DeliveredAndOpened")) 
772                                 priv->track_info = E_GW_ITEM_DELIVERED_OPENED;
773                        else if (!g_ascii_strcasecmp (value, "All")) 
774                                 priv->track_info = E_GW_ITEM_ALL;
775                        
776                        g_free (value), value = NULL;
777
778                        value = soup_soap_parameter_get_property (subparam, "autoDelete");
779                        if (value && !g_ascii_strcasecmp (value, "1"))
780                                 priv->autodelete = TRUE; 
781                        g_free (value), value = NULL;
782                }               
783         }
784
785         if ( (subparam = soup_soap_parameter_get_first_child_by_name (param, "notification"))) {
786                 priv->notify_opened = get_notification_value (subparam, "opened");      
787                 priv->notify_deleted = get_notification_value (subparam, "deleted");    
788                 priv->notify_accepted = get_notification_value (subparam, "accepted");  
789                 priv->notify_declined = get_notification_value (subparam, "declined");  
790                 priv->notify_completed = get_notification_value (subparam, "completed");        
791         }
792 }
793
794 char*
795 e_gw_item_get_field_value (EGwItem *item, char *field_name)
796 {
797         gpointer value;
798
799         g_return_val_if_fail (field_name != NULL, NULL);
800         g_return_val_if_fail (E_IS_GW_ITEM(item), NULL);
801         
802         if (item->priv->simple_fields == NULL)
803                 return NULL;
804        
805         value =  (char *) g_hash_table_lookup (item->priv->simple_fields, field_name);
806         if (value)
807                 return value;
808                         
809         return NULL;
810 }
811
812 void 
813 e_gw_item_set_field_value (EGwItem *item, char *field_name, char* field_value)
814 {
815         g_return_if_fail (field_name != NULL);
816         g_return_if_fail (field_name != NULL);
817         g_return_if_fail (E_IS_GW_ITEM(item));
818         
819         if (item->priv->simple_fields != NULL)
820                 g_hash_table_insert (item->priv->simple_fields, field_name, g_strdup (field_value));
821
822 }
823 guint32 
824 e_gw_item_get_item_status (EGwItem *item)
825 {
826
827         return item->priv->self_status;
828 }
829 GList * 
830 e_gw_item_get_email_list (EGwItem *item)
831 {
832         return item->priv->email_list;
833
834
835 }
836
837 void 
838 e_gw_item_set_email_list (EGwItem *item, GList* email_list)     
839 {
840         item->priv->email_list = email_list;
841 }
842
843 GList * 
844 e_gw_item_get_im_list (EGwItem *item)
845
846 {
847         return item->priv->im_list;
848 }
849
850 void 
851 e_gw_item_set_im_list (EGwItem *item, GList *im_list)
852 {
853         item->priv->im_list = im_list;
854 }
855 FullName*
856 e_gw_item_get_full_name (EGwItem *item)
857 {
858         return item->priv->full_name;
859 }
860
861 void 
862 e_gw_item_set_full_name (EGwItem *item, FullName *full_name)
863 {       
864         item->priv->full_name = full_name;
865 }
866
867 GList *
868 e_gw_item_get_member_list (EGwItem *item)
869 {
870         return item->priv->member_list;
871 }
872
873 void 
874 e_gw_item_set_member_list (EGwItem *item, GList *list)
875 {
876         item->priv->member_list = list;
877
878 }
879
880 void 
881 e_gw_item_set_address (EGwItem *item, char *address_type, PostalAddress *address)
882 {
883         if (address_type && address)
884                 g_hash_table_insert (item->priv->addresses, address_type, address);
885
886 }
887
888 PostalAddress *e_gw_item_get_address (EGwItem *item, char *address_type)
889 {
890         return (PostalAddress *) g_hash_table_lookup (item->priv->addresses, address_type);
891 }
892
893 void 
894 e_gw_item_set_categories (EGwItem *item, GList *category_list)
895 {
896         item->priv->category_list = category_list;
897
898 }
899
900 GList*
901 e_gw_item_get_categories (EGwItem *item)
902 {
903         return item->priv->category_list;
904 }
905
906 void 
907 e_gw_item_set_category_name (EGwItem *item, char *category_name)
908 {
909         item->priv->category_name = category_name;
910 }
911
912 char*
913 e_gw_item_get_category_name (EGwItem *item)
914 {
915         return item->priv->category_name;
916 }
917
918 void e_gw_item_set_change (EGwItem *item, EGwItemChangeType change_type, char *field_name, gpointer field_value)
919 {
920         GHashTable *hash_table;
921         EGwItemPrivate *priv;
922
923         priv = item->priv;
924         hash_table = NULL;
925         switch (change_type) {
926         case E_GW_ITEM_CHANGE_TYPE_ADD :
927                 hash_table = priv->additions;
928                 break;
929         case E_GW_ITEM_CHANGE_TYPE_UPDATE :
930                 hash_table = priv->updates;
931                 break;
932         case E_GW_ITEM_CHANGE_TYPE_DELETE :
933                 hash_table = priv->deletions;
934                 break;
935         case E_GW_ITEM_CHNAGE_TYPE_UNKNOWN :
936                 hash_table = NULL;
937                 break;
938         
939         }
940
941         if (hash_table)
942                 g_hash_table_insert (hash_table, field_name, field_value);
943         
944 }
945
946 static void 
947 set_common_addressbook_item_fields_from_soap_parameter (EGwItem *item, SoupSoapParameter *param)
948 {
949         SoupSoapParameter *subparam, *category_param;
950         GHashTable *simple_fields;
951         char *value;
952         EGwItemPrivate *priv;
953         if (strcmp (soup_soap_parameter_get_name (param), "item") != 0) {
954                 g_warning (G_STRLOC ": Invalid SOAP parameter %s", soup_soap_parameter_get_name (param));
955                 return;
956         }
957         priv = item->priv;
958         simple_fields = priv->simple_fields;
959
960         subparam = soup_soap_parameter_get_first_child_by_name(param, "id");
961         if(subparam) {
962                 value = soup_soap_parameter_get_string_value (subparam);
963                 g_hash_table_insert (simple_fields, "id", value);
964                 item->priv->id = g_strdup (value);
965         }
966         value = NULL;
967         subparam = soup_soap_parameter_get_first_child_by_name (param, "modified");
968         if (subparam) {
969                 value = soup_soap_parameter_get_string_value (subparam);
970                 g_hash_table_insert (simple_fields, "modified_time", value);
971         }
972         value = NULL;
973         subparam = soup_soap_parameter_get_first_child_by_name (param, "comment");
974         if(subparam) {
975                 value = soup_soap_parameter_get_string_value (subparam);
976                 if (value)
977                         g_hash_table_insert (simple_fields , "comment", value);
978         }
979         value = NULL;
980         subparam = soup_soap_parameter_get_first_child_by_name(param, "name");
981         if(subparam) {
982                 value = soup_soap_parameter_get_string_value (subparam);
983                 if (value)
984                         g_hash_table_insert (simple_fields, "name", value);
985         }
986         value = NULL;
987         subparam = soup_soap_parameter_get_first_child_by_name (param, "categories");
988         if (subparam) {
989                 for (category_param = soup_soap_parameter_get_first_child_by_name (subparam, "category");
990                      category_param != NULL;
991                      category_param = soup_soap_parameter_get_next_child_by_name (category_param, "category")) {
992
993                         value = soup_soap_parameter_get_string_value (category_param);
994                         if (value) {
995                                 char **components = g_strsplit (value, "@", -1);
996                                 g_free (value);
997                                 value = components[0];
998                                 priv->category_list = g_list_append (priv->category_list, g_strdup (value));
999                                 g_strfreev(components);
1000                                 
1001                         }
1002                                 
1003                 }
1004         }
1005
1006
1007 }
1008
1009 static void 
1010 set_postal_address_from_soap_parameter (PostalAddress *address, SoupSoapParameter *param)
1011 {
1012         SoupSoapParameter *subparam;
1013         char *value;
1014
1015         subparam= soup_soap_parameter_get_first_child_by_name (param, "streetAddress");
1016         if (subparam) {
1017                 value = soup_soap_parameter_get_string_value (subparam);
1018                 if (value)
1019                         address->street_address = value;
1020         }
1021         
1022         subparam = soup_soap_parameter_get_first_child_by_name (param, "location");
1023         if (subparam) {
1024                 value = soup_soap_parameter_get_string_value (subparam);
1025                 
1026                 if (value)
1027                         address->location = value;
1028         }
1029         
1030         subparam = soup_soap_parameter_get_first_child_by_name (param, "city");
1031         if (subparam) {
1032                 value = soup_soap_parameter_get_string_value (subparam);
1033                 if (value)
1034                         address->city = value;
1035         }
1036         
1037         subparam = soup_soap_parameter_get_first_child_by_name (param, "state");
1038         if (subparam) {
1039                 value = soup_soap_parameter_get_string_value (subparam);
1040                 if (value)
1041                         address->state = value;
1042         }
1043         
1044         subparam = soup_soap_parameter_get_first_child_by_name (param, "postalCode");
1045         if (subparam) {
1046                 value = soup_soap_parameter_get_string_value (subparam);
1047                 if (value)
1048                         address->postal_code = value;
1049         }
1050         
1051         subparam = soup_soap_parameter_get_first_child_by_name (param, "country");
1052         if (subparam) {
1053                 value = soup_soap_parameter_get_string_value (subparam);
1054                 if (value)
1055                         address->country = value;
1056         }
1057         
1058 }
1059
1060 static void 
1061 set_contact_fields_from_soap_parameter (EGwItem *item, SoupSoapParameter *param)
1062 {
1063         char *value;
1064         char *type;
1065         char *primary_email;
1066         SoupSoapParameter *subparam;
1067         SoupSoapParameter *temp;
1068         SoupSoapParameter *second_level_child;
1069         GHashTable *simple_fields;
1070         FullName *full_name ;
1071         PostalAddress *address;
1072
1073         value = NULL;
1074         if (strcmp (soup_soap_parameter_get_name (param), "item") != 0) {
1075                 g_warning (G_STRLOC ": Invalid SOAP parameter %s", soup_soap_parameter_get_name (param));
1076                 return;
1077         }
1078         set_common_addressbook_item_fields_from_soap_parameter (item, param);
1079         simple_fields = item->priv->simple_fields;
1080         full_name = item->priv->full_name;
1081         if (full_name) {
1082                 subparam = soup_soap_parameter_get_first_child_by_name (param, "fullName");
1083                 if (subparam) {
1084                         temp = soup_soap_parameter_get_first_child_by_name(subparam, "namePrefix"); 
1085                         if (temp) {
1086                                 value = soup_soap_parameter_get_string_value (temp);
1087                                 if (value)
1088                                         full_name->name_prefix = value;
1089                         }
1090                         temp = soup_soap_parameter_get_first_child_by_name(subparam, "firstName"); 
1091                         if (temp) {
1092                                 value = soup_soap_parameter_get_string_value (temp);
1093                                 if (value)
1094                                         full_name->first_name = value;
1095                         }
1096                         temp = soup_soap_parameter_get_first_child_by_name(subparam, "middleName"); 
1097                         if (temp) {
1098                                 value = soup_soap_parameter_get_string_value (temp);
1099                                 if (value)
1100                                         full_name->middle_name = value;
1101                         }
1102                         temp = soup_soap_parameter_get_first_child_by_name(subparam, "lastName"); 
1103                         if (temp) {
1104                                 value = soup_soap_parameter_get_string_value (temp);
1105                                 if (value)
1106                                         full_name->last_name = value;
1107                         }
1108                         temp = soup_soap_parameter_get_first_child_by_name(subparam, "nameSuffix"); 
1109                         if (temp) {
1110                                 value = soup_soap_parameter_get_string_value (temp);
1111                                 if (value)
1112                                         full_name->name_suffix = value;
1113                         }
1114                 }
1115         }
1116         subparam = soup_soap_parameter_get_first_child_by_name(param, "emailList"); 
1117         if (subparam) {
1118                 primary_email = NULL;
1119                 value = soup_soap_parameter_get_property(subparam, "primary");
1120                 if (value) {     
1121                         primary_email = value;
1122                         item->priv->email_list = g_list_append (item->priv->email_list, g_strdup (primary_email));
1123                 }
1124                 for ( temp = soup_soap_parameter_get_first_child (subparam); temp != NULL; temp = soup_soap_parameter_get_next_child (temp)) {
1125                         value = soup_soap_parameter_get_string_value (temp);
1126                         if (value) {
1127                                 if (!primary_email || !g_str_equal (primary_email, value))
1128                                         item->priv->email_list = g_list_append (item->priv->email_list, value);
1129                                 else
1130                                         g_free (value);
1131                         }
1132                 }
1133                 g_free (primary_email);
1134         }
1135
1136         subparam =  soup_soap_parameter_get_first_child_by_name(param, "imList");
1137         if(subparam) {
1138                 for ( temp = soup_soap_parameter_get_first_child (subparam); temp != NULL; temp = soup_soap_parameter_get_next_child (temp))
1139                         {
1140                                 IMAddress *im_address = g_new0(IMAddress, 1);
1141                                 im_address->address = im_address->service = NULL;
1142                                 second_level_child = soup_soap_parameter_get_first_child_by_name (temp, "service");
1143                                 if (second_level_child) {
1144                                         value = soup_soap_parameter_get_string_value (second_level_child);
1145                                         if (value )
1146                                                 im_address->service = value;
1147                                 }
1148                                 second_level_child = soup_soap_parameter_get_first_child_by_name (temp, "address");
1149                                 if (second_level_child) {
1150                                         value = soup_soap_parameter_get_string_value (second_level_child);
1151                                         if (value)
1152                                                 im_address->address = value;
1153                                 }
1154                                 if (im_address->service && im_address->address)
1155                                         item->priv->im_list = g_list_append (item->priv->im_list, im_address);
1156                                 else 
1157                                         free_im_address (im_address, NULL);                                     
1158                                 
1159                         }
1160         }
1161         
1162         
1163         subparam =  soup_soap_parameter_get_first_child_by_name(param, "phoneList");
1164         if(subparam) {
1165                 g_hash_table_insert (simple_fields, "default_phone", soup_soap_parameter_get_property(subparam, "default"));
1166                 for ( temp = soup_soap_parameter_get_first_child (subparam); temp != NULL; temp = soup_soap_parameter_get_next_child (temp))
1167                         {
1168                                 const char *key = NULL;
1169
1170                                 type =  soup_soap_parameter_get_property (temp, "type");
1171                                 value = soup_soap_parameter_get_string_value (temp);
1172                                 switch (*type) {
1173                                         case 'O' :      
1174                                                 key = "phone_Office";
1175                                                 break;
1176                                         case 'H' :
1177                                                 key = "phone_Home";
1178                                                 break;
1179                                         case 'P' :
1180                                                 key = "phone_Pager";
1181                                                 break;
1182                                         case 'M' :
1183                                                 key = "phone_Mobile";
1184                                                 break;
1185                                         case 'F' :
1186                                                 key = "phone_Fax";
1187                                                 break;
1188                                         default:
1189                                                 /* It should never come here. For the worst */
1190                                                 key = "phone_";
1191                                                 break;
1192                                 };
1193
1194                                 if (type) {
1195                                         g_hash_table_insert (item->priv->simple_fields, (char*)key, value);
1196                                         g_free (type);
1197                                 }
1198                         }
1199         }
1200         subparam =  soup_soap_parameter_get_first_child_by_name(param, "personalInfo");
1201         if(subparam) {
1202                 temp = soup_soap_parameter_get_first_child_by_name (subparam, "birthday");
1203                 if(temp) {
1204                         value = soup_soap_parameter_get_string_value (temp);
1205                         if (value)
1206                                 g_hash_table_insert (simple_fields, "birthday", value);
1207                         
1208                 }
1209                 temp = soup_soap_parameter_get_first_child_by_name (subparam, "website");
1210                 if(temp) {
1211                         value = soup_soap_parameter_get_string_value (temp);
1212                         if (value)
1213                                 g_hash_table_insert (simple_fields, "website", value);
1214                 }
1215         }
1216
1217         subparam =  soup_soap_parameter_get_first_child_by_name(param, "officeInfo");
1218         if (subparam) {
1219                 temp = soup_soap_parameter_get_first_child_by_name (subparam, "organization");
1220                 if(temp) {
1221                         value = soup_soap_parameter_get_string_value (temp);
1222                         if(value)
1223                                 g_hash_table_insert (simple_fields, "organization", value);
1224                         value = soup_soap_parameter_get_property(temp, "uid");
1225                         if (value)
1226                                 g_hash_table_insert (simple_fields, "organization_id", value);
1227                 }
1228                 temp = soup_soap_parameter_get_first_child_by_name (subparam, "department");
1229                 if(temp) {
1230                         value = soup_soap_parameter_get_string_value (temp);
1231                         if(value)
1232                                 g_hash_table_insert (simple_fields, "department", value);
1233                 }
1234                 temp = soup_soap_parameter_get_first_child_by_name (subparam, "title");
1235                 if(temp) {
1236                         value = soup_soap_parameter_get_string_value (temp);
1237                         if(value)
1238                                 g_hash_table_insert (simple_fields, "title", value);
1239                 }
1240                         
1241         }
1242
1243         subparam = soup_soap_parameter_get_first_child_by_name (param, "addressList");
1244         if (subparam) {
1245                 for ( temp = soup_soap_parameter_get_first_child (subparam); temp != NULL; temp = soup_soap_parameter_get_next_child (temp)) {
1246                         const char *add = NULL;
1247                         address = g_new0 (PostalAddress, 1);
1248                         set_postal_address_from_soap_parameter (address, temp);
1249                         value = soup_soap_parameter_get_property(temp, "type");
1250                         
1251                         if (value && value[0] == 'H')
1252                                 add = "Home";
1253                         else if (value && value[0] == 'O')
1254                                 add = "Office";
1255                         else 
1256                                 add = "Other";
1257
1258                         if (value) 
1259                                 g_hash_table_insert (item->priv->addresses, (char*)add, address);
1260                         else
1261                                 free_postal_address (address);
1262                         g_free (value);
1263                 }
1264                 
1265         }
1266         
1267 }
1268 static void 
1269 set_group_fields_from_soap_parameter (EGwItem *item, SoupSoapParameter *param)
1270 {
1271
1272         char *value;
1273         SoupSoapParameter *subparam, *temp, *second_level_child;
1274         GHashTable *simple_fields;
1275         
1276         if (strcmp (soup_soap_parameter_get_name (param), "item") != 0) {
1277                 g_warning (G_STRLOC ": Invalid SOAP parameter %s", soup_soap_parameter_get_name (param));
1278                 return;
1279         }
1280
1281         set_common_addressbook_item_fields_from_soap_parameter (item, param);
1282         simple_fields = item->priv->simple_fields;
1283
1284         /* set name as the ful name also , as it is needed for searching*/
1285         value = g_hash_table_lookup (simple_fields, "name");
1286         if (value) 
1287                 item->priv->full_name->first_name = g_strdup (value);
1288
1289         subparam = soup_soap_parameter_get_first_child_by_name (param, "members");
1290         if (subparam) {
1291                 char *id, *email;
1292                 for ( temp = soup_soap_parameter_get_first_child (subparam); temp != NULL; temp = soup_soap_parameter_get_next_child (temp)) {
1293                         id = email = NULL;
1294                         second_level_child = soup_soap_parameter_get_first_child_by_name (temp, "email"); 
1295                         if (second_level_child)
1296                                 email = soup_soap_parameter_get_string_value (second_level_child);
1297                         second_level_child = soup_soap_parameter_get_first_child_by_name (temp, "id");
1298                         if (second_level_child)
1299                                 id = soup_soap_parameter_get_string_value (second_level_child);
1300                         
1301                         if (id && email) {
1302                                 EGroupMember *member = g_new0 (EGroupMember, 1);
1303                                 member->id = id;
1304                                 member->email = email;
1305                                 second_level_child = soup_soap_parameter_get_first_child_by_name (temp, "name");
1306                                 member->name =  soup_soap_parameter_get_string_value (second_level_child); 
1307                                 item->priv->member_list = g_list_append (item->priv->member_list, member);
1308                         }
1309                               
1310                         
1311                 }
1312         }
1313         
1314         
1315 }
1316
1317 static void 
1318 set_resource_fields_from_soap_parameter (EGwItem *item, SoupSoapParameter *param)
1319 {
1320
1321         char *value;
1322         SoupSoapParameter *subparam;
1323         GHashTable *simple_fields;
1324         
1325         if (strcmp (soup_soap_parameter_get_name (param), "item") != 0) {
1326                 g_warning (G_STRLOC ": Invalid SOAP parameter %s", soup_soap_parameter_get_name (param));
1327                 return;
1328         }
1329
1330         set_common_addressbook_item_fields_from_soap_parameter (item, param);
1331         simple_fields = item->priv->simple_fields;
1332
1333         /* set name as the ful name also , as it is needed for searching*/
1334         value = g_hash_table_lookup (simple_fields, "name");
1335         if (value) 
1336                 item->priv->full_name->first_name = g_strdup (value);
1337                 
1338         subparam = soup_soap_parameter_get_first_child_by_name (param, "phone");
1339         if(subparam) {
1340                 value = soup_soap_parameter_get_string_value (subparam);
1341                 if(value)
1342                         g_hash_table_insert (simple_fields, "default_phone", value);
1343         }
1344         subparam = soup_soap_parameter_get_first_child_by_name (param, "email");
1345         if(subparam) {
1346                 value = soup_soap_parameter_get_string_value (subparam);
1347                 if(value)
1348                         item->priv->email_list = g_list_append (item->priv->email_list, value);
1349         }
1350         
1351 }
1352
1353 static void 
1354 set_organization_fields_from_soap_parameter (EGwItem *item, SoupSoapParameter *param)
1355 {
1356
1357         char *value;
1358         SoupSoapParameter *subparam;
1359         PostalAddress *address;
1360         GHashTable *simple_fields;
1361         
1362         if (strcmp (soup_soap_parameter_get_name (param), "item") != 0) {
1363                 g_warning (G_STRLOC ": Invalid SOAP parameter %s", soup_soap_parameter_get_name (param));
1364                 return;
1365         }
1366         set_common_addressbook_item_fields_from_soap_parameter (item, param);
1367         simple_fields = item->priv->simple_fields;
1368
1369         /* set name as the ful name also , as it is needed for searching*/
1370         value = g_hash_table_lookup (simple_fields, "name");
1371         if (value) 
1372                 item->priv->full_name->first_name = g_strdup (value);
1373
1374         subparam = soup_soap_parameter_get_first_child_by_name (param, "phone");
1375         if(subparam) {
1376                 value = soup_soap_parameter_get_string_value (subparam);
1377                 if(value)
1378                         g_hash_table_insert (simple_fields, "default_phone", value);
1379         }
1380
1381         subparam = soup_soap_parameter_get_first_child_by_name (param, "fax");
1382         if(subparam) {
1383                 value = soup_soap_parameter_get_string_value (subparam);
1384                 if(value)
1385                         g_hash_table_insert (simple_fields, "phone_Fax", value);
1386         }
1387
1388         subparam = soup_soap_parameter_get_first_child_by_name (param, "address");
1389         if (subparam) {
1390                 address = g_new0 (PostalAddress, 1);
1391                 set_postal_address_from_soap_parameter (address, subparam);
1392                 g_hash_table_insert (item->priv->addresses, "Office", address);
1393                 
1394         }
1395
1396         subparam = soup_soap_parameter_get_first_child_by_name (param, "website");
1397         if(subparam) {
1398                 value = soup_soap_parameter_get_string_value (subparam);
1399                 if(value)
1400                         g_hash_table_insert (simple_fields, "website", value);
1401         }
1402
1403
1404 }
1405
1406 static void 
1407 append_postal_address_to_soap_message (SoupSoapMessage *msg, PostalAddress *address, char *address_type)
1408 {
1409         soup_soap_message_start_element (msg, "address", NULL, NULL);
1410         soup_soap_message_add_attribute (msg, "type", address_type, NULL, NULL);
1411         if (address->street_address)
1412                 e_gw_message_write_string_parameter (msg, "streetAddress", NULL, address->street_address);
1413         if (address->location)
1414                 e_gw_message_write_string_parameter (msg, "location", NULL, address->location);
1415         if (address->city)
1416                 e_gw_message_write_string_parameter (msg, "city", NULL, address->city);
1417         if (address->state)
1418                 e_gw_message_write_string_parameter (msg, "state", NULL, address->state);
1419         if (address->postal_code)
1420                 e_gw_message_write_string_parameter (msg, "postalCode", NULL, address->postal_code);
1421         if (address->country)
1422                 e_gw_message_write_string_parameter (msg, "country", NULL, address->country);
1423         soup_soap_message_end_element(msg);
1424
1425 }
1426
1427 static void
1428 append_common_addressbook_item_fields_to_soap_message (GHashTable *simple_fields, GList *category_list,  SoupSoapMessage *msg)
1429 {
1430         char * value;
1431         
1432         value =  g_hash_table_lookup (simple_fields, "name");
1433         if (value)
1434                 e_gw_message_write_string_parameter (msg, "name", NULL, value);
1435
1436         soup_soap_message_start_element (msg, "categories", NULL, NULL);
1437         if (category_list && category_list->data) 
1438                 soup_soap_message_add_attribute (msg, "types:primary", category_list->data, NULL, NULL);
1439         for (; category_list != NULL; category_list = g_list_next (category_list)) 
1440                 if (category_list->data) {
1441                         e_gw_message_write_string_parameter (msg, "category", NULL, category_list->data);
1442                 }
1443         soup_soap_message_end_element (msg);
1444
1445         value = g_hash_table_lookup (simple_fields, "comment");
1446         if(value) 
1447                 e_gw_message_write_string_parameter (msg, "comment", NULL, value);
1448
1449 }
1450
1451 static void 
1452 append_full_name_to_soap_message (FullName *full_name, char *display_name, SoupSoapMessage *msg)
1453 {
1454         g_return_if_fail (full_name != NULL);
1455         soup_soap_message_start_element (msg, "fullName", NULL, NULL);
1456         if (display_name)
1457                 e_gw_message_write_string_parameter (msg, "displayName", NULL, display_name);
1458         if (full_name->name_prefix)
1459                 e_gw_message_write_string_parameter (msg, "namePrefix", NULL, full_name->name_prefix);
1460         if (full_name->first_name)
1461                 e_gw_message_write_string_parameter (msg, "firstName", NULL, full_name->first_name);
1462         if (full_name->middle_name)
1463                 e_gw_message_write_string_parameter (msg, "middleName", NULL, full_name->middle_name);
1464         if (full_name->last_name)
1465                 e_gw_message_write_string_parameter (msg, "lastName", NULL, full_name->last_name) ;
1466         if (full_name->name_suffix)
1467                 e_gw_message_write_string_parameter (msg, "nameSuffix", NULL, full_name->name_suffix);
1468         soup_soap_message_end_element (msg);
1469
1470 }
1471
1472 static void 
1473 append_email_list_soap_message (GList *email_list, SoupSoapMessage *msg)
1474 {
1475         g_return_if_fail (email_list != NULL);
1476
1477         soup_soap_message_start_element (msg, "emailList", NULL, NULL);
1478         soup_soap_message_add_attribute (msg, "primary", email_list->data, NULL, NULL);
1479         for (; email_list != NULL; email_list = g_list_next (email_list)) 
1480                 if(email_list->data) 
1481                         e_gw_message_write_string_parameter (msg, "email", NULL, email_list->data);
1482         soup_soap_message_end_element (msg);
1483
1484 }
1485
1486 static void 
1487 append_im_list_to_soap_message (GList *ims, SoupSoapMessage *msg)
1488 {
1489         IMAddress *address;
1490         g_return_if_fail (ims != NULL);
1491
1492         soup_soap_message_start_element (msg, "imList", NULL, NULL);
1493         for (; ims != NULL; ims = g_list_next (ims)) {
1494                 soup_soap_message_start_element (msg, "im", NULL, NULL);
1495                 address = (IMAddress *) ims->data;
1496                 e_gw_message_write_string_parameter (msg, "service", NULL, address->service);
1497                 e_gw_message_write_string_parameter (msg, "address", NULL, address->address);
1498                 soup_soap_message_end_element (msg);
1499         }
1500         soup_soap_message_end_element (msg);
1501
1502 }
1503 static void
1504 append_phone_list_to_soap_message (GHashTable *simple_fields, SoupSoapMessage *msg)
1505 {
1506         char *value;
1507
1508         g_return_if_fail (simple_fields != NULL);
1509
1510         soup_soap_message_start_element (msg, "phoneList", NULL, NULL);
1511         value = g_hash_table_lookup (simple_fields, "default_phone");
1512         if (value) 
1513                 soup_soap_message_add_attribute (msg, "default", value, NULL, NULL);
1514         value = g_hash_table_lookup (simple_fields, "phone_Office");
1515         if (value) 
1516                 e_gw_message_write_string_parameter_with_attribute (msg, "phone", NULL, value, "type", "Office");
1517         value = g_hash_table_lookup (simple_fields, "phone_Home");
1518         if (value) 
1519                 e_gw_message_write_string_parameter_with_attribute (msg, "phone", NULL, value, "type", "Home");
1520         value = g_hash_table_lookup (simple_fields, "phone_Pager");
1521         if (value) 
1522                 e_gw_message_write_string_parameter_with_attribute (msg, "phone", NULL, value, "type", "Pager");
1523         value = g_hash_table_lookup (simple_fields, "phone_Mobile");
1524         if (value) 
1525                 e_gw_message_write_string_parameter_with_attribute (msg, "phone", NULL, value, "type", "Mobile");
1526         value = g_hash_table_lookup (simple_fields, "phone_Fax");
1527         if (value) 
1528                 e_gw_message_write_string_parameter_with_attribute (msg, "phone", NULL, value, "type", "Fax");
1529
1530         soup_soap_message_end_element (msg);
1531
1532 }
1533
1534 static void 
1535 append_office_info_to_soap_message (GHashTable *simple_fields, SoupSoapMessage *msg)
1536 {
1537         char *value;
1538         char *org_name;
1539         g_return_if_fail (simple_fields != NULL);
1540
1541         soup_soap_message_start_element (msg, "officeInfo", NULL, NULL);
1542         value = g_hash_table_lookup (simple_fields, "organization_id");
1543         org_name = g_hash_table_lookup (simple_fields, "organization");
1544         if (value && org_name) 
1545                 e_gw_message_write_string_parameter_with_attribute (msg, "organization", NULL, org_name, "uid", value);
1546         else if(org_name)
1547                 e_gw_message_write_string_parameter (msg, "organization", NULL, org_name);
1548         value = g_hash_table_lookup (simple_fields, "department");
1549         if (value)
1550                 e_gw_message_write_string_parameter (msg, "department", NULL, value);
1551         
1552                 value = g_hash_table_lookup (simple_fields, "title");
1553         if (value)
1554                 e_gw_message_write_string_parameter (msg, "title", NULL, value);
1555
1556         value = g_hash_table_lookup (simple_fields, "website");
1557         if (value)
1558                 e_gw_message_write_string_parameter (msg, "website", NULL, value);
1559         soup_soap_message_end_element (msg);
1560
1561 }
1562
1563 static void 
1564 append_personal_info_to_soap_message (GHashTable *simple_fields, SoupSoapMessage *msg)
1565 {
1566         char *value;
1567
1568         g_return_if_fail (simple_fields != NULL);
1569         
1570         soup_soap_message_start_element (msg, "personalInfo", NULL, NULL);
1571         value =  g_hash_table_lookup (simple_fields, "birthday");
1572         if(value)
1573                 e_gw_message_write_string_parameter (msg, "birthday", NULL, value);
1574         value =  g_hash_table_lookup (simple_fields, "website");
1575         if(value)
1576                 e_gw_message_write_string_parameter (msg, "website",NULL,  value);
1577         
1578         soup_soap_message_end_element (msg);
1579
1580 }
1581
1582 static void
1583 append_contact_fields_to_soap_message (EGwItem *item, SoupSoapMessage *msg)
1584 {
1585         char * value;
1586         GHashTable *simple_fields;
1587         FullName *full_name;
1588         PostalAddress *postal_address;
1589         
1590         simple_fields = item->priv->simple_fields;
1591         value = g_hash_table_lookup (simple_fields, "id");
1592         if (value)
1593                 e_gw_message_write_string_parameter (msg, "id", NULL, value);
1594         
1595         if (item->priv->container)
1596                 e_gw_message_write_string_parameter (msg, "container", NULL, item->priv->container);
1597
1598         append_common_addressbook_item_fields_to_soap_message (simple_fields, item->priv->category_list, msg);
1599         value =  g_hash_table_lookup (simple_fields, "name");
1600         
1601         full_name = item->priv->full_name;
1602        
1603         if (full_name)
1604                 append_full_name_to_soap_message (full_name, value, msg); 
1605         
1606         if (item->priv->email_list)
1607                 append_email_list_soap_message (item->priv->email_list, msg);
1608         
1609         if (item->priv->im_list)
1610                 append_im_list_to_soap_message (item->priv->im_list, msg);
1611         
1612         if (simple_fields)
1613                 append_phone_list_to_soap_message (simple_fields, msg);
1614                 
1615         soup_soap_message_start_element (msg, "addressList", NULL, NULL);
1616         postal_address = g_hash_table_lookup (item->priv->addresses, "Home");
1617         if (postal_address)
1618                 append_postal_address_to_soap_message (msg, postal_address, "Home");
1619         postal_address = g_hash_table_lookup (item->priv->addresses, "Office");
1620         if (postal_address)
1621                 append_postal_address_to_soap_message (msg, postal_address, "Office");
1622         soup_soap_message_end_element (msg);
1623         append_office_info_to_soap_message (simple_fields, msg);
1624         append_personal_info_to_soap_message (simple_fields, msg);
1625
1626 }
1627
1628 static void 
1629 append_group_fields_to_soap_message (EGwItem *item, SoupSoapMessage *msg)
1630 {
1631         GHashTable *simple_fields;
1632         GList *members;
1633
1634         simple_fields = item->priv->simple_fields;
1635         append_common_addressbook_item_fields_to_soap_message (simple_fields, item->priv->category_list, msg);
1636         if (item->priv->container)
1637                 e_gw_message_write_string_parameter (msg, "container", NULL, item->priv->container);
1638         soup_soap_message_start_element (msg, "members", NULL, NULL);
1639         members = g_list_copy (item->priv->member_list);
1640         for (; members != NULL; members = g_list_next (members)) {
1641                 EGroupMember *member = (EGroupMember *) members->data;
1642                 soup_soap_message_start_element (msg, "member", NULL, NULL);
1643                 e_gw_message_write_string_parameter (msg, "id", NULL, member->id);
1644                 e_gw_message_write_string_parameter (msg, "distType", NULL, "TO");
1645                 e_gw_message_write_string_parameter (msg, "itemType", NULL, "Contact");
1646                 soup_soap_message_end_element(msg);
1647
1648         }
1649         soup_soap_message_end_element (msg);
1650         
1651 }
1652
1653 EGwItem *
1654 e_gw_item_new_from_soap_parameter (const char *email, const char *container, SoupSoapParameter *param)
1655 {
1656         EGwItem *item;
1657         char *item_type;
1658         SoupSoapParameter *subparameter, *child, *category_param, *attachment_param;
1659         gboolean is_group_item = TRUE;
1660         GList *user_email = NULL;
1661         
1662         g_return_val_if_fail (param != NULL, NULL);
1663
1664         if (strcmp (soup_soap_parameter_get_name (param), "item") != 0) {
1665                 g_warning (G_STRLOC ": Invalid SOAP parameter %s", soup_soap_parameter_get_name (param));
1666                 return NULL;
1667         }
1668
1669         item = g_object_new (E_TYPE_GW_ITEM, NULL);
1670         item_type = soup_soap_parameter_get_property (param, "type");
1671         if (!g_ascii_strcasecmp (item_type, "Mail"))
1672                 item->priv->item_type = E_GW_ITEM_TYPE_MAIL ;
1673         else if (!g_ascii_strcasecmp (item_type, "Appointment"))
1674                 item->priv->item_type = E_GW_ITEM_TYPE_APPOINTMENT;
1675         else if (!g_ascii_strcasecmp (item_type, "Task"))
1676                 item->priv->item_type = E_GW_ITEM_TYPE_TASK;
1677         else if (!g_ascii_strcasecmp (item_type, "Note"))
1678                 item->priv->item_type = E_GW_ITEM_TYPE_NOTE;
1679         else if (!g_ascii_strcasecmp (item_type, "Contact") ) {
1680                 item->priv->item_type = E_GW_ITEM_TYPE_CONTACT;
1681                 set_contact_fields_from_soap_parameter (item, param);
1682                 g_free (item_type);
1683                 return item;
1684         } 
1685         else if (!g_ascii_strcasecmp (item_type,"SharedNotification"))
1686                 item->priv->item_type = E_GW_ITEM_TYPE_NOTIFICATION ;
1687
1688         else if (!g_ascii_strcasecmp (item_type, "Organization")) {
1689
1690                 item->priv->item_type =  E_GW_ITEM_TYPE_ORGANISATION;
1691                 set_organization_fields_from_soap_parameter (item, param);
1692                 g_free (item_type);
1693                 return item;
1694         }
1695                 
1696         else if (!g_ascii_strcasecmp (item_type, "Resource")) {
1697                 
1698                 item->priv->item_type = E_GW_ITEM_TYPE_CONTACT;
1699                 set_resource_fields_from_soap_parameter (item, param);
1700                 g_free (item_type);
1701                 return item;
1702         }
1703                  
1704         else if (!g_ascii_strcasecmp (item_type, "Group")) {
1705                 item->priv->item_type = E_GW_ITEM_TYPE_GROUP;
1706                 set_group_fields_from_soap_parameter (item, param);
1707                 g_free (item_type);
1708                 return item;
1709         }
1710         
1711         else {
1712                 g_free (item_type);
1713                 g_object_unref (item);
1714                 return NULL;
1715         }
1716
1717         g_free (item_type);
1718
1719         item->priv->container = g_strdup (container);
1720         /* set the email of the user */
1721         user_email = g_list_append (user_email, g_strdup (email));
1722         e_gw_item_set_email_list (item, user_email);
1723
1724         /* If the parameter consists of changes - populate deltas */
1725         subparameter = soup_soap_parameter_get_first_child_by_name (param, "changes");
1726         if (subparameter) {
1727                 SoupSoapParameter *changes = subparameter;
1728                 subparameter = soup_soap_parameter_get_first_child_by_name (changes, "add");
1729                 if (!subparameter)
1730                         subparameter = soup_soap_parameter_get_first_child_by_name (changes, "delete");
1731                 if (!subparameter)
1732                         subparameter = soup_soap_parameter_get_first_child_by_name (changes, "update");
1733         }
1734         else subparameter = param; /* The item is a complete one, not a delta  */
1735         
1736         /* now add all properties to the private structure */
1737         for (child = soup_soap_parameter_get_first_child (subparameter);
1738                         child != NULL;
1739                         child = soup_soap_parameter_get_next_child (child)) {
1740                 const char *name;
1741                 char *value = NULL;
1742
1743                 name = soup_soap_parameter_get_name (child);
1744
1745                 if (!g_ascii_strcasecmp (name, "security")) {
1746                         item->priv->security = soup_soap_parameter_get_string_value (child);
1747                         item->priv->classification = soup_soap_parameter_get_string_value (child);
1748                         continue;
1749                 }
1750                 if (!g_ascii_strcasecmp (name, "acceptLevel"))
1751                         item->priv->accept_level = soup_soap_parameter_get_string_value (child);
1752
1753                 else if (!g_ascii_strcasecmp (name, "class")) {
1754                         item->priv->classification = soup_soap_parameter_get_string_value (child);
1755
1756                 } else if (!g_ascii_strcasecmp (name, "completed")) {
1757                         value = soup_soap_parameter_get_string_value (child);
1758                         if (!g_ascii_strcasecmp (value, "1"))
1759                                 item->priv->completed = TRUE;
1760                         else
1761                                 item->priv->completed = FALSE;
1762                         g_free (value);
1763
1764                 } else if (!g_ascii_strcasecmp (name, "allDayEvent")) {
1765                         value = soup_soap_parameter_get_string_value (child);
1766                         if (!g_ascii_strcasecmp (value, "1"))
1767                                 item->priv->is_allday_event = TRUE;
1768                         else
1769                                 item->priv->is_allday_event = FALSE;
1770                         g_free (value); 
1771
1772                 } else if (!g_ascii_strcasecmp (name, "status")) {
1773                         SoupSoapParameter *status_param;
1774                         const char *status_name;
1775                         item->priv->self_status = 0;
1776                         for (status_param = soup_soap_parameter_get_first_child (child); status_param != NULL;
1777                                         status_param = soup_soap_parameter_get_next_child (status_param)) {
1778                                 status_name = soup_soap_parameter_get_name (status_param);
1779
1780                                 if (!strcmp (status_name, "accepted"))
1781                                         item->priv->self_status |= E_GW_ITEM_STAT_ACCEPTED;
1782                                 else if (!strcmp (status_name, "declined"))
1783                                         item->priv->self_status |= E_GW_ITEM_STAT_DECLINED;
1784                                 else if (!strcmp (status_name, "deleted"))
1785                                         item->priv->self_status |= E_GW_ITEM_STAT_DELETED;
1786                                 else if (!strcmp (status_name, "read")) 
1787                                         item->priv->self_status |= E_GW_ITEM_STAT_READ;
1788                                 else if (!strcmp (status_name, "opened"))
1789                                         item->priv->self_status |= E_GW_ITEM_STAT_OPENED;
1790                                 else if (!strcmp (status_name, "completed"))
1791                                         item->priv->self_status |= E_GW_ITEM_STAT_COMPLETED;
1792                                 else if (!strcmp (status_name, "forwarded"))
1793                                         item->priv->self_status |= E_GW_ITEM_STAT_FORWARDED;
1794                                 else if (!strcmp (status_name, "replied"))
1795                                         item->priv->self_status |= E_GW_ITEM_STAT_REPLIED;
1796                                 else if (!strcmp (status_name, "delegated"))
1797                                         item->priv->self_status |= E_GW_ITEM_STAT_DELEGATED;
1798                         }
1799
1800                 } else if (!g_ascii_strcasecmp (name, "created")) {
1801                         char *formatted_date;
1802                         value = soup_soap_parameter_get_string_value (child);
1803                         formatted_date = e_gw_connection_format_date_string (value);
1804                         e_gw_item_set_creation_date (item, formatted_date);
1805                         g_free (value);
1806                         g_free (formatted_date);
1807
1808                 } else if (!g_ascii_strcasecmp (name, "delivered")) {
1809                         char *formatted_date;
1810                         value = soup_soap_parameter_get_string_value (child);
1811                         formatted_date = e_gw_connection_format_date_string (value);
1812                         e_gw_item_set_delivered_date (item, formatted_date);
1813                         g_free (value);
1814                         g_free (formatted_date);
1815
1816                 } else if (!g_ascii_strcasecmp (name, "distribution")) {
1817                         SoupSoapParameter *tp;
1818
1819                         tp = soup_soap_parameter_get_first_child_by_name (child, "recipients");
1820                         if (tp) {
1821                                 g_slist_foreach (item->priv->recipient_list, (GFunc) free_recipient, NULL);
1822                                 item->priv->recipient_list = NULL;
1823                                 set_recipient_list_from_soap_parameter (item, tp);
1824                         }
1825
1826                         tp = soup_soap_parameter_get_first_child_by_name (child, "sendoptions");
1827                         if (tp) 
1828                                 set_sendoptions_from_soap_parameter (item, tp);         
1829
1830                         tp = soup_soap_parameter_get_first_child_by_name (child, "from");
1831                         if (tp && is_group_item) {
1832                                 SoupSoapParameter *subparam;
1833                                 EGwItemOrganizer *organizer = g_new0 (EGwItemOrganizer, 1);
1834
1835                                 subparam = soup_soap_parameter_get_first_child_by_name (tp, "displayName");
1836                                 if (subparam) 
1837                                         organizer->display_name = soup_soap_parameter_get_string_value (subparam);
1838                                 subparam = soup_soap_parameter_get_first_child_by_name (tp, "email");
1839                                 if (subparam) 
1840                                         organizer->email = soup_soap_parameter_get_string_value (subparam);
1841                                 e_gw_item_set_organizer (item, organizer);
1842                         }
1843                 } else if (!g_ascii_strcasecmp (name, "size")) {
1844                         item->priv->size = soup_soap_parameter_get_int_value (child);
1845
1846                 } else if (!g_ascii_strcasecmp (name, "hasAttachment")) {
1847                         item->priv->has_attachment = soup_soap_parameter_get_int_value (child);
1848
1849                 } else if (!g_ascii_strcasecmp (name, "options")) {
1850                         SoupSoapParameter *subparam;
1851
1852                         subparam = soup_soap_parameter_get_first_child_by_name (child, "priority");
1853                         if (subparam) {
1854                                 item->priv->priority = soup_soap_parameter_get_string_value (subparam);
1855                         }
1856
1857                         subparam = soup_soap_parameter_get_first_child_by_name (child, "expires");
1858                         if (subparam)
1859                                 value = soup_soap_parameter_get_string_value (subparam);
1860                         if (value)
1861                                 item->priv->expires = g_strdup (value); 
1862                         g_free (value), value = NULL;
1863
1864                         subparam = soup_soap_parameter_get_first_child_by_name (child, "delayDeliveryUntil");
1865                         if (subparam)
1866                                 value = soup_soap_parameter_get_string_value (subparam);
1867                         if (value)
1868                                 item->priv->delay_until = g_strdup (value);
1869                         g_free (value), value = NULL;
1870
1871                 } else if (!g_ascii_strcasecmp (name, "dueDate")) {
1872                         char *formatted_date; 
1873                         value = soup_soap_parameter_get_string_value (child);
1874                         formatted_date = e_gw_connection_format_date_string (value);
1875                         e_gw_item_set_due_date (item, formatted_date);
1876                         g_free (value);
1877                         g_free (formatted_date);
1878
1879                 } else if (!g_ascii_strcasecmp (name, "endDate") || !g_ascii_strcasecmp (name, "endDay")) {
1880                         char *formatted_date; 
1881                         value = soup_soap_parameter_get_string_value (child);
1882                         formatted_date = e_gw_connection_format_date_string (value);
1883                         e_gw_item_set_end_date (item, formatted_date);
1884                         g_free (value);
1885                         g_free (formatted_date); 
1886
1887                 } else if (!g_ascii_strcasecmp (name, "to")) {
1888                         char *to ;
1889                         to = soup_soap_parameter_get_string_value (child) ;
1890                         e_gw_item_set_to (item, to);
1891                         g_free (to);
1892                 } else if (!g_ascii_strcasecmp (name, "iCalId"))
1893                         item->priv->icalid = soup_soap_parameter_get_string_value (child);
1894
1895                 else if (!g_ascii_strcasecmp (name, "categories")) { 
1896                         for (category_param = soup_soap_parameter_get_first_child_by_name (child, "category");
1897                                         category_param != NULL;
1898                                         category_param = soup_soap_parameter_get_next_child_by_name (category_param, "category")) {
1899
1900                                 value = soup_soap_parameter_get_string_value (category_param);
1901                                 if (value) {
1902                                         char **components = g_strsplit (value, "@", -1);
1903                                         g_free (value);
1904                                         value = components[0];
1905                                         item->priv->category_list = g_list_append (item->priv->category_list, g_strdup (value));
1906                                         g_strfreev(components);
1907                                 }
1908                         }
1909
1910                 } else if (!g_ascii_strcasecmp (name, "id")) 
1911                         item->priv->id = soup_soap_parameter_get_string_value (child);
1912                 else if (!g_ascii_strcasecmp (name, "recurrenceKey"))
1913                         item->priv->recurrence_key = soup_soap_parameter_get_int_value (child);
1914                 else if (!g_ascii_strcasecmp (name, "message")) {
1915                         SoupSoapParameter *part;
1916                         int len;
1917                         char *msg;
1918                         char *length;
1919
1920                         part = soup_soap_parameter_get_first_child_by_name (child, "part");
1921                         msg = soup_soap_parameter_get_string_value (part);
1922                         length = soup_soap_parameter_get_property (part, "length"); 
1923
1924                         if (msg && length) {
1925                                 len = atoi (length);
1926                                 item->priv->message = soup_base64_decode  (msg, &len);
1927                                 if (!(item->priv->message)) {
1928                                         item->priv->msg_body_id = soup_soap_parameter_get_property (part, "id") ;
1929                                 } 
1930                                 item->priv->content_type = soup_soap_parameter_get_property (part, "contentType") ;
1931                         } 
1932
1933                         g_free (length);
1934                         g_free (msg);
1935
1936                 } else if (!g_ascii_strcasecmp (name, "attachments")) {
1937                         for (attachment_param = soup_soap_parameter_get_first_child_by_name(child,"attachment") ;
1938                                         attachment_param != NULL ;
1939                                         attachment_param = soup_soap_parameter_get_next_child_by_name (attachment_param, "attachment")) {
1940
1941                                 SoupSoapParameter *temp ;
1942                                 EGwItemAttachment *attach = g_new0 (EGwItemAttachment, 1) ;
1943
1944                                 temp = soup_soap_parameter_get_first_child_by_name (attachment_param, "id") ;
1945                                 if (temp) {
1946                                         attach->id = soup_soap_parameter_get_string_value (temp) ;
1947                                         attach->item_reference =  soup_soap_parameter_get_property (temp, "itemReference");
1948                                 }
1949
1950                                 temp = soup_soap_parameter_get_first_child_by_name (attachment_param, "contentId") ;
1951                                 if (temp)
1952                                         attach->contentid = soup_soap_parameter_get_string_value (temp) ;
1953
1954                                 temp = soup_soap_parameter_get_first_child_by_name (attachment_param, "name") ;
1955                                 if (temp)
1956                                         attach->name = soup_soap_parameter_get_string_value (temp) ;
1957
1958                                 temp = soup_soap_parameter_get_first_child_by_name (attachment_param, "contentType") ;
1959                                 if (temp)
1960                                         attach->contentType = soup_soap_parameter_get_string_value (temp) ;
1961
1962                                 temp = soup_soap_parameter_get_first_child_by_name (attachment_param, "size") ;
1963                                 if (temp)
1964                                         attach->size = atoi (soup_soap_parameter_get_string_value (temp)) ;
1965
1966                                 temp = soup_soap_parameter_get_first_child_by_name (attachment_param, "date") ;
1967                                 if (temp)
1968                                         attach->date = soup_soap_parameter_get_string_value (temp) ;
1969
1970
1971                                 item->priv->attach_list = g_slist_append (item->priv->attach_list, attach) ;
1972                         }
1973
1974                 } else if (!g_ascii_strcasecmp (name, "place"))
1975                         item->priv->place = soup_soap_parameter_get_string_value (child);
1976
1977                 else if (!g_ascii_strcasecmp (name, "taskPriority")) {
1978                         char *priority;
1979
1980                         priority = soup_soap_parameter_get_string_value (child);
1981                         e_gw_item_set_task_priority (item, priority);
1982                         g_free (priority);
1983                 }
1984
1985                 else if (!g_ascii_strcasecmp (name, "startDate") || !g_ascii_strcasecmp (name, "startDay")) {
1986                         char *formatted_date;
1987                         value = soup_soap_parameter_get_string_value (child);
1988                         formatted_date = e_gw_connection_format_date_string (value);
1989                         e_gw_item_set_start_date (item, formatted_date);
1990                         g_free (value);
1991                         g_free (formatted_date);
1992
1993                 } else if (!g_ascii_strcasecmp (name, "subject"))
1994                         item->priv->subject = soup_soap_parameter_get_string_value (child);
1995                 else if (!g_ascii_strcasecmp (name, "source")) {
1996                         value = soup_soap_parameter_get_string_value (child);
1997                         if (!strcmp (value, "personal")) {
1998                                 if (item->priv->item_type == E_GW_ITEM_TYPE_TASK || 
1999                                                 item->priv->item_type == E_GW_ITEM_TYPE_APPOINTMENT)
2000                                         is_group_item = FALSE;
2001                         }
2002                         g_free (value);
2003                 } else if (!g_ascii_strcasecmp (name, "link")){
2004                         SoupSoapParameter *subparam;
2005                         EGwItemLinkInfo *info = g_new0 (EGwItemLinkInfo, 1);
2006
2007                         subparam = soup_soap_parameter_get_first_child_by_name (child, "id") ;
2008                         if (subparam)
2009                                 info->id = soup_soap_parameter_get_string_value (subparam);
2010
2011                         subparam = soup_soap_parameter_get_first_child_by_name (child, "type") ;
2012                         if (subparam)
2013                                 info->type =soup_soap_parameter_get_string_value (subparam);
2014
2015                         subparam = soup_soap_parameter_get_first_child_by_name (child, "thread") ;
2016                         if (subparam)
2017                                 info->thread =soup_soap_parameter_get_string_value (subparam);
2018
2019                         e_gw_item_set_link_info (item, info);
2020
2021                 } else if (!g_ascii_strcasecmp (name, "alarm")) {
2022                         char *enabled;
2023                         enabled = soup_soap_parameter_get_property (child, "enabled");
2024                         if (!g_ascii_strcasecmp (enabled, "1") ) {
2025                                 value = soup_soap_parameter_get_string_value (child);
2026                                 /* convert it into integer */
2027                                 item->priv->trigger = atoi (value);
2028                                 g_free (value);
2029                                 g_free (enabled);
2030                         }
2031                 }
2032
2033         }
2034
2035 return item;
2036 }
2037
2038 EGwItemType
2039 e_gw_item_get_item_type (EGwItem *item)
2040 {
2041         g_return_val_if_fail (E_IS_GW_ITEM (item), E_GW_ITEM_TYPE_UNKNOWN);
2042
2043         return item->priv->item_type;
2044 }
2045
2046 void
2047 e_gw_item_set_item_type (EGwItem *item, EGwItemType new_type)
2048 {
2049         g_return_if_fail (E_IS_GW_ITEM (item));
2050
2051         item->priv->item_type = new_type;
2052 }
2053
2054 const char *
2055 e_gw_item_get_container_id (EGwItem *item)
2056 {
2057         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2058
2059         return (const char *) item->priv->container;
2060 }
2061
2062 void
2063 e_gw_item_set_container_id (EGwItem *item, const char *new_id)
2064 {
2065         g_return_if_fail (E_IS_GW_ITEM (item));
2066
2067         if (item->priv->container)
2068                 g_free (item->priv->container);
2069         item->priv->container = g_strdup (new_id);
2070 }
2071
2072 const char *
2073 e_gw_item_get_icalid (EGwItem *item)
2074 {
2075         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2076
2077         return (const char *) item->priv->icalid;
2078 }
2079
2080 void
2081 e_gw_item_set_icalid (EGwItem *item, const char *new_icalid)
2082 {
2083         g_return_if_fail (E_IS_GW_ITEM (item));
2084
2085         if (item->priv->icalid)
2086                 g_free (item->priv->icalid);
2087         item->priv->icalid = g_strdup (new_icalid);
2088 }
2089
2090 const char *
2091 e_gw_item_get_id (EGwItem *item)
2092 {
2093         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2094
2095         return (const char *) item->priv->id;
2096 }
2097
2098 void
2099 e_gw_item_set_id (EGwItem *item, const char *new_id)
2100 {
2101         g_return_if_fail (E_IS_GW_ITEM (item));
2102
2103         if (item->priv->id)
2104                 g_free (item->priv->id);
2105         item->priv->id = g_strdup (new_id);
2106 }
2107
2108 int
2109 e_gw_item_get_mail_size (EGwItem *item)
2110 {
2111         g_return_val_if_fail (E_IS_GW_ITEM (item), 0);
2112
2113         return item->priv->size;
2114 }
2115
2116 gboolean
2117 e_gw_item_has_attachment (EGwItem *item)
2118 {
2119         g_return_val_if_fail (E_IS_GW_ITEM (item), 0);
2120
2121         return item->priv->has_attachment;
2122 }
2123
2124 char *
2125 e_gw_item_get_creation_date (EGwItem *item)
2126 {
2127         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2128
2129         return item->priv->creation_date;
2130 }
2131
2132 void
2133 e_gw_item_set_creation_date (EGwItem *item, const char *new_date)
2134 {
2135         g_return_if_fail (E_IS_GW_ITEM (item));
2136
2137         if (item->priv->creation_date)
2138                 g_free (item->priv->creation_date);
2139         item->priv->creation_date = g_strdup (new_date);
2140 }
2141
2142 char *
2143 e_gw_item_get_delivered_date (EGwItem *item)
2144 {
2145         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2146
2147         return item->priv->delivered_date;
2148 }
2149
2150 void
2151 e_gw_item_set_delivered_date (EGwItem *item, const char *new_date)
2152 {
2153         g_return_if_fail (E_IS_GW_ITEM (item));
2154
2155         if (item->priv->delivered_date)
2156                 g_free (item->priv->delivered_date);
2157         item->priv->delivered_date = g_strdup (new_date);
2158 }
2159
2160 char *
2161 e_gw_item_get_start_date (EGwItem *item)
2162 {
2163         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2164
2165         return item->priv->start_date;
2166 }
2167
2168 void
2169 e_gw_item_set_start_date (EGwItem *item, const char *new_date)
2170 {
2171         g_return_if_fail (E_IS_GW_ITEM (item));
2172
2173         if (item->priv->start_date)
2174                 g_free (item->priv->start_date);
2175         item->priv->start_date = g_strdup (new_date);
2176 }
2177
2178 char *
2179 e_gw_item_get_end_date (EGwItem *item)
2180 {
2181         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2182
2183         return item->priv->end_date;
2184 }
2185
2186 void
2187 e_gw_item_set_end_date (EGwItem *item, const char *new_date)
2188 {
2189         g_return_if_fail (E_IS_GW_ITEM (item));
2190
2191         if (item->priv->end_date)
2192                 g_free (item->priv->end_date);
2193         item->priv->end_date = g_strdup (new_date);
2194 }
2195
2196 char *
2197 e_gw_item_get_due_date (EGwItem *item)
2198 {
2199         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2200
2201         return item->priv->due_date;
2202 }
2203
2204 void
2205 e_gw_item_set_due_date (EGwItem *item, const char *new_date)
2206 {
2207         g_return_if_fail (E_IS_GW_ITEM (item));
2208
2209         if (item->priv->due_date)
2210                 g_free (item->priv->due_date);
2211         item->priv->due_date = g_strdup (new_date);
2212 }
2213
2214 char *
2215 e_gw_item_get_completed_date (EGwItem *item)
2216 {
2217         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2218
2219         return item->priv->completed_date;
2220 }
2221
2222 void 
2223 e_gw_item_set_completed_date (EGwItem *item, const char *new_date)
2224 {
2225         g_return_if_fail (E_IS_GW_ITEM (item));
2226
2227         if (item->priv->completed_date)
2228                 g_free (item->priv->completed_date);
2229         item->priv->completed_date = g_strdup (new_date);
2230 }
2231
2232 const char *
2233 e_gw_item_get_subject (EGwItem *item)
2234 {
2235         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2236
2237         return (const char *) item->priv->subject;
2238 }
2239
2240 void
2241 e_gw_item_set_subject (EGwItem *item, const char *new_subject)
2242 {
2243         g_return_if_fail (E_IS_GW_ITEM (item));
2244
2245         if (item->priv->subject)
2246                 g_free (item->priv->subject);
2247         item->priv->subject = g_strdup (new_subject);
2248 }
2249
2250 const char *
2251 e_gw_item_get_message (EGwItem *item)
2252 {
2253         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2254
2255         return (const char *) item->priv->message;
2256 }
2257
2258 void
2259 e_gw_item_set_message (EGwItem *item, const char *new_message)
2260 {
2261         g_return_if_fail (E_IS_GW_ITEM (item));
2262
2263         if (item->priv->message)
2264                 g_free (item->priv->message);
2265         item->priv->message = g_strdup (new_message);
2266 }
2267
2268 const char *
2269 e_gw_item_get_place (EGwItem *item)
2270 {
2271         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2272
2273         return item->priv->place;
2274 }
2275
2276 void
2277 e_gw_item_set_place (EGwItem *item, const char *new_place)
2278 {
2279         g_return_if_fail (E_IS_GW_ITEM (item));
2280
2281         if (item->priv->place)
2282                 g_free (item->priv->place);
2283         item->priv->place = g_strdup (new_place);
2284 }
2285
2286 const char *
2287 e_gw_item_get_classification (EGwItem *item)
2288 {
2289         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2290
2291         return (const char *) item->priv->classification;
2292 }
2293
2294 void
2295 e_gw_item_set_classification (EGwItem *item, const char *new_class)
2296 {
2297         g_return_if_fail (E_IS_GW_ITEM (item));
2298
2299         if (item->priv->classification)
2300                 g_free (item->priv->classification);
2301         item->priv->classification = g_strdup (new_class);
2302 }
2303
2304 void
2305 e_gw_item_set_security (EGwItem *item, const char *new_class)
2306 {
2307         g_return_if_fail (E_IS_GW_ITEM (item));
2308
2309         if (item->priv->security)
2310                 g_free (item->priv->security);
2311         item->priv->security = g_strdup (new_class);
2312 }
2313
2314 gboolean
2315 e_gw_item_get_completed (EGwItem *item)
2316 {
2317         g_return_val_if_fail (E_IS_GW_ITEM (item), FALSE);
2318
2319         return item->priv->completed;
2320 }
2321
2322 void
2323 e_gw_item_set_completed (EGwItem *item, gboolean new_completed)
2324 {
2325         g_return_if_fail (E_IS_GW_ITEM (item));
2326
2327         item->priv->completed = new_completed;
2328 }
2329
2330 gboolean 
2331 e_gw_item_get_is_allday_event (EGwItem *item)
2332 {
2333         g_return_val_if_fail (E_IS_GW_ITEM (item), FALSE);
2334
2335         return item->priv->is_allday_event;
2336 }
2337
2338 void 
2339 e_gw_item_set_is_allday_event (EGwItem *item, gboolean allday_event)
2340 {
2341         g_return_if_fail (E_IS_GW_ITEM (item));
2342
2343         item->priv->is_allday_event = allday_event;
2344 }
2345
2346 const char *
2347 e_gw_item_get_accept_level (EGwItem *item)
2348 {
2349         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2350
2351         return (const char *) item->priv->accept_level;
2352 }
2353
2354 void
2355 e_gw_item_set_accept_level (EGwItem *item, const char *new_level)
2356 {
2357         g_return_if_fail (E_IS_GW_ITEM (item));
2358
2359         if (item->priv->accept_level)
2360                 g_free (item->priv->accept_level);
2361         item->priv->accept_level = g_strdup (new_level);
2362 }
2363
2364 const char *
2365 e_gw_item_get_priority (EGwItem *item)
2366 {
2367         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2368         return (const char *) item->priv->priority;
2369 }
2370
2371 void
2372 e_gw_item_set_priority (EGwItem *item, const char *new_priority)
2373 {
2374         g_return_if_fail (E_IS_GW_ITEM (item));
2375
2376         if (item->priv->priority)
2377                 g_free (item->priv->priority);
2378         item->priv->priority = g_strdup (new_priority);
2379 }
2380
2381 const char *
2382 e_gw_item_get_task_priority (EGwItem *item)
2383 {
2384         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2385
2386         return (const char *) item->priv->task_priority;
2387 }
2388
2389 void
2390 e_gw_item_set_task_priority (EGwItem *item, const char *new_priority)
2391 {
2392         g_return_if_fail (E_IS_GW_ITEM (item));
2393
2394         if (item->priv->task_priority)
2395                 g_free (item->priv->task_priority);
2396         item->priv->task_priority = g_strdup (new_priority);
2397 }
2398 GSList *
2399 e_gw_item_get_recipient_list (EGwItem *item)
2400 {
2401         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2402         return item->priv->recipient_list;
2403 }
2404
2405 void
2406 e_gw_item_set_recipient_list (EGwItem  *item, GSList *new_recipient_list)
2407 {
2408         /* free old list and set a new one*/
2409         g_slist_foreach (item->priv->recipient_list, (GFunc) free_recipient, NULL);
2410         g_slist_free (item->priv->recipient_list);
2411         item->priv->recipient_list = new_recipient_list;
2412 }
2413
2414 EGwItemOrganizer *
2415 e_gw_item_get_organizer (EGwItem *item)
2416 {
2417         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2418         return item->priv->organizer;
2419 }
2420
2421 void
2422 e_gw_item_set_attach_id_list (EGwItem *item, GSList *attach_list)
2423 {
2424         g_return_if_fail (E_IS_GW_ITEM (item)) ;
2425         if (attach_list) {
2426                 g_slist_foreach (item->priv->attach_list, (GFunc)free_attach, NULL) ;
2427                 g_slist_free (item->priv->attach_list) ;
2428         }
2429         item->priv->attach_list = attach_list ;
2430 }
2431
2432 GSList *
2433 e_gw_item_get_attach_id_list (EGwItem *item)
2434 {
2435         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL) ;
2436         return item->priv->attach_list ;
2437 }
2438
2439 void
2440 e_gw_item_set_organizer (EGwItem  *item, EGwItemOrganizer *organizer)
2441 {
2442         /* free organizer */ 
2443         g_free (item->priv->organizer);
2444         item->priv->organizer = organizer;
2445 }
2446
2447 GSList *
2448 e_gw_item_get_recurrence_dates (EGwItem *item)
2449 {
2450         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2451         return item->priv->recurrence_dates;
2452 }
2453
2454 void
2455 e_gw_item_set_recurrence_dates (EGwItem  *item, GSList *new_recurrence_dates)
2456 {
2457         /* free old list and set a new one*/
2458         g_slist_foreach (item->priv->recurrence_dates, free_string, NULL);
2459         /*free the list */
2460         g_slist_free (item->priv->recurrence_dates);
2461         item->priv->recurrence_dates = new_recurrence_dates;
2462 }
2463
2464 GSList *
2465 e_gw_item_get_exdate_list (EGwItem *item)
2466 {
2467         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2468         return item->priv->exdate_list;
2469 }
2470
2471 void
2472 e_gw_item_set_exdate_list (EGwItem  *item, GSList *new_exdate_list)
2473 {
2474         /* free old list and set a new one*/
2475         g_slist_foreach (item->priv->exdate_list, free_string, NULL);
2476         /*free the list */
2477         g_slist_free (item->priv->exdate_list);
2478         item->priv->exdate_list = new_exdate_list;
2479 }
2480
2481 EGwItemRecurrenceRule *
2482 e_gw_item_get_rrule (EGwItem *item)
2483 {
2484         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2485         return item->priv->rrule;
2486 }
2487
2488 void
2489 e_gw_item_set_rrule (EGwItem  *item, EGwItemRecurrenceRule *new_rrule)
2490 {
2491         if (item->priv->rrule) {
2492         /* TODO free old list and set a new one*/
2493         }
2494         item->priv->rrule = new_rrule;
2495 }
2496
2497 int
2498 e_gw_item_get_recurrence_key (EGwItem *item)
2499 {
2500         g_return_val_if_fail (E_IS_GW_ITEM (item), 0);
2501         return item->priv->recurrence_key;
2502 }
2503
2504 void
2505 e_gw_item_set_recurrence_key (EGwItem *item, int recur_key)
2506 {
2507         item->priv->recurrence_key = recur_key;
2508 }
2509
2510 int
2511 e_gw_item_get_trigger (EGwItem *item)
2512 {
2513         g_return_val_if_fail (E_IS_GW_ITEM (item), 0);
2514
2515         return item->priv->trigger;
2516 }
2517
2518 void
2519 e_gw_item_set_trigger (EGwItem *item, int trigger)
2520 {
2521         g_return_if_fail (E_IS_GW_ITEM (item));
2522
2523         item->priv->trigger = trigger;
2524 }
2525
2526
2527 void 
2528 e_gw_item_set_to (EGwItem *item, const char *to)
2529 {
2530         g_return_if_fail (E_IS_GW_ITEM (item));
2531         item->priv->to = g_strdup (to) ; 
2532 }
2533
2534 const char *
2535 e_gw_item_get_to (EGwItem *item)
2536 {
2537         g_return_val_if_fail (E_IS_GW_ITEM(item), NULL) ;
2538         return item->priv->to ;
2539 }
2540
2541 const char *
2542 e_gw_item_get_msg_content_type (EGwItem *item)
2543 {
2544         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL) ;
2545         return item->priv->content_type ;
2546 }
2547
2548 const char *
2549 e_gw_item_get_msg_body_id (EGwItem *item)
2550 {
2551         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2552
2553         return item->priv->msg_body_id;
2554 }
2555
2556 void
2557 e_gw_item_set_sendoptions (EGwItem *item, gboolean set)
2558 {
2559         g_return_if_fail (E_IS_GW_ITEM (item));
2560
2561         item->priv->set_sendoptions = set;
2562 }
2563
2564 void
2565 e_gw_item_set_reply_request (EGwItem *item, gboolean set)
2566 {
2567         g_return_if_fail (E_IS_GW_ITEM (item));
2568
2569         item->priv->reply_request_set = set;
2570 }
2571
2572 gboolean
2573 e_gw_item_get_reply_request (EGwItem *item)
2574 {
2575         g_return_val_if_fail (E_IS_GW_ITEM (item), FALSE);
2576
2577         return item->priv->reply_request_set;
2578 }
2579
2580 void
2581 e_gw_item_set_reply_within (EGwItem *item, char *reply_within)
2582 {
2583         g_return_if_fail (E_IS_GW_ITEM (item));
2584
2585         item->priv->reply_within = g_strdup (reply_within);
2586 }
2587
2588 char *
2589 e_gw_item_get_reply_within (EGwItem *item)
2590 {
2591         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2592
2593         return item->priv->reply_within;
2594 }
2595
2596 void
2597 e_gw_item_set_track_info (EGwItem *item, EGwItemTrack track_info)
2598 {
2599         g_return_if_fail (E_IS_GW_ITEM (item));
2600
2601         item->priv->track_info = track_info;
2602 }
2603
2604 EGwItemTrack
2605 e_gw_item_get_track_info (EGwItem *item)
2606 {
2607         g_return_val_if_fail (E_IS_GW_ITEM (item), E_GW_ITEM_NONE);
2608
2609         return item->priv->track_info;
2610 }
2611
2612
2613 void
2614 e_gw_item_set_autodelete (EGwItem *item, gboolean set)
2615 {
2616         g_return_if_fail (E_IS_GW_ITEM (item));
2617
2618         item->priv->autodelete = set;
2619 }
2620
2621 gboolean
2622 e_gw_item_get_autodelete (EGwItem *item)
2623 {
2624         g_return_val_if_fail (E_IS_GW_ITEM (item), FALSE);
2625
2626         return item->priv->autodelete;
2627 }
2628
2629 void 
2630 e_gw_item_set_notify_completed (EGwItem *item, EGwItemReturnNotify notify)
2631 {
2632         g_return_if_fail (E_IS_GW_ITEM (item));
2633
2634         item->priv->notify_completed = notify;
2635 }
2636
2637 EGwItemReturnNotify 
2638 e_gw_item_get_notify_completed (EGwItem *item)
2639 {
2640         g_return_val_if_fail (E_IS_GW_ITEM (item), FALSE);
2641
2642         return item->priv->notify_completed;
2643 }
2644
2645 void 
2646 e_gw_item_set_notify_accepted (EGwItem *item, EGwItemReturnNotify notify)
2647 {
2648         g_return_if_fail (E_IS_GW_ITEM (item));
2649
2650         item->priv->notify_accepted = notify;
2651 }
2652
2653 EGwItemReturnNotify 
2654 e_gw_item_get_notify_accepted (EGwItem *item)
2655 {
2656         g_return_val_if_fail (E_IS_GW_ITEM (item), FALSE);
2657
2658         return item->priv->notify_accepted;
2659 }
2660
2661 void 
2662 e_gw_item_set_notify_declined (EGwItem *item, EGwItemReturnNotify notify)
2663 {
2664         g_return_if_fail (E_IS_GW_ITEM (item));
2665
2666         item->priv->notify_declined = notify;
2667 }
2668
2669 EGwItemReturnNotify 
2670 e_gw_item_get_notify_declined (EGwItem *item)
2671 {
2672         g_return_val_if_fail (E_IS_GW_ITEM (item), FALSE);
2673
2674         return item->priv->notify_declined;
2675 }
2676
2677 void 
2678 e_gw_item_set_notify_opened (EGwItem *item, EGwItemReturnNotify notify)
2679 {
2680         g_return_if_fail (E_IS_GW_ITEM (item));
2681
2682         item->priv->notify_opened = notify;
2683 }
2684
2685 EGwItemReturnNotify 
2686 e_gw_item_get_notify_opened (EGwItem *item)
2687 {
2688         g_return_val_if_fail (E_IS_GW_ITEM (item), FALSE);
2689
2690         return item->priv->notify_opened;
2691 }
2692
2693 void 
2694 e_gw_item_set_notify_deleted (EGwItem *item, EGwItemReturnNotify notify)
2695 {
2696         g_return_if_fail (E_IS_GW_ITEM (item));
2697
2698         item->priv->notify_deleted = notify;
2699 }
2700
2701 EGwItemReturnNotify 
2702 e_gw_item_get_notify_deleted (EGwItem *item)
2703 {
2704         g_return_val_if_fail (E_IS_GW_ITEM (item), FALSE);
2705
2706         return item->priv->notify_deleted;
2707 }
2708
2709 void
2710 e_gw_item_set_expires (EGwItem *item, char *expires)
2711 {
2712         g_return_if_fail (E_IS_GW_ITEM (item));
2713
2714         item->priv->expires = g_strdup (expires);
2715 }
2716
2717 char *
2718 e_gw_item_get_expires (EGwItem *item)
2719 {
2720         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2721
2722         return item->priv->expires;
2723 }
2724
2725 void
2726 e_gw_item_set_delay_until (EGwItem *item, char *delay_until)
2727 {
2728         g_return_if_fail (E_IS_GW_ITEM (item));
2729
2730         item->priv->delay_until = g_strdup (delay_until);
2731 }
2732
2733 char *
2734 e_gw_item_get_delay_until (EGwItem *item)
2735 {
2736         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2737
2738         return item->priv->delay_until;
2739 }
2740
2741 void
2742 e_gw_item_set_source (EGwItem *item, char *source)
2743 {
2744         g_return_if_fail (E_IS_GW_ITEM (item));
2745         item->priv->source = g_strdup (source);
2746 }
2747
2748 void
2749 e_gw_item_set_content_type (EGwItem *item, const char *content_type)
2750 {
2751         g_return_if_fail (E_IS_GW_ITEM (item));
2752
2753         if (item->priv->content_type)
2754                 g_free (item->priv->content_type);
2755         item->priv->content_type= g_strdup (content_type);
2756 }
2757
2758 char *
2759 e_gw_item_get_content_type (EGwItem *item)
2760 {
2761         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2762
2763         return item->priv->content_type ;
2764 }
2765
2766 void
2767 e_gw_item_set_link_info (EGwItem *item, EGwItemLinkInfo *info)
2768 {
2769         g_return_if_fail (E_IS_GW_ITEM (item));
2770         item->priv->link_info = info;
2771 }
2772
2773 EGwItemLinkInfo *
2774 e_gw_item_get_link_info (EGwItem *item)
2775 {
2776         g_return_val_if_fail (E_IS_GW_ITEM (item), NULL);
2777         return item->priv->link_info;
2778 }
2779
2780 static void
2781 add_return_notification (SoupSoapMessage *msg, char *option, EGwItemReturnNotify value)
2782 {
2783         soup_soap_message_start_element (msg, option, NULL, NULL);
2784
2785         switch (value) {
2786                 case E_GW_ITEM_NOTIFY_MAIL:
2787                         e_gw_message_write_string_parameter (msg, "mail", NULL, "1");
2788                         break;
2789                 case E_GW_ITEM_NOTIFY_NONE:
2790                         e_gw_message_write_string_parameter (msg, "mail", NULL, "0");
2791         }
2792         
2793         soup_soap_message_end_element (msg);    
2794 }
2795
2796 static void 
2797 append_gw_item_options (SoupSoapMessage *msg, EGwItem *item) 
2798 {
2799         EGwItemPrivate *priv;
2800
2801         priv = item->priv;
2802         
2803         soup_soap_message_start_element (msg, "options", NULL, NULL);
2804         
2805         /* Priority */
2806         e_gw_message_write_string_parameter (msg, "priority", NULL, priv->priority ? priv->priority : "0");
2807
2808         /* Expiration date */
2809         e_gw_message_write_string_parameter (msg, "expires", NULL, priv->expires ? priv->expires : "0");
2810         
2811         /* Delay delivery */
2812         e_gw_message_write_string_parameter (msg, "delayDeliveryUntil", NULL, priv->delay_until ? priv->delay_until : "0");
2813
2814         soup_soap_message_end_element (msg);
2815 }
2816
2817 void
2818 e_gw_item_add_distribution_to_soap_message (EGwItem *item, SoupSoapMessage *msg)
2819 {
2820         GSList *rl;
2821         EGwItemPrivate *priv;
2822         EGwItemOrganizer *organizer;
2823         GSList *recipient_list;
2824
2825         priv = item->priv;
2826         organizer = priv->organizer;
2827         recipient_list = priv->recipient_list;
2828         
2829         /* start distribution element */
2830         soup_soap_message_start_element (msg, "distribution", NULL, NULL);
2831         if (organizer) {
2832                 soup_soap_message_start_element (msg, "from", NULL, NULL);
2833                 e_gw_message_write_string_parameter (msg, "displayName", NULL,
2834                                 organizer->display_name ? organizer->display_name : "");
2835                 e_gw_message_write_string_parameter (msg, "email", NULL, 
2836                                 organizer->email ? organizer->email : "");
2837                 
2838                 soup_soap_message_end_element (msg);
2839         }
2840         /* start recipients */
2841         soup_soap_message_start_element (msg, "recipients", NULL, NULL);
2842         /* add each recipient */
2843         for (rl = recipient_list; rl != NULL; rl = rl->next) {
2844                 char *dist_type;
2845                 char *status = NULL;
2846
2847                 EGwItemRecipient *recipient = (EGwItemRecipient *) rl->data;
2848                 
2849                 soup_soap_message_start_element (msg, "recipient", NULL, NULL);
2850                 e_gw_message_write_string_parameter (msg, "displayName", NULL, recipient->display_name ? recipient->display_name : "");
2851                 e_gw_message_write_string_parameter (msg, "email", NULL, recipient->email ? recipient->email : "");
2852                 if (recipient->type == E_GW_ITEM_RECIPIENT_TO)
2853                         dist_type = "TO";
2854                 else if (recipient->type == E_GW_ITEM_RECIPIENT_CC)
2855                         dist_type = "CC";
2856                 else if (recipient->type == E_GW_ITEM_RECIPIENT_BC)
2857                         dist_type = "BC";
2858                 else 
2859                         dist_type ="";
2860                 e_gw_message_write_string_parameter (msg, "distType", NULL, dist_type);
2861                 /* add recip_status */
2862                 if (recipient->status == E_GW_ITEM_STAT_ACCEPTED)
2863                         status = "accepted";
2864                 else if (recipient->status == E_GW_ITEM_STAT_DECLINED)
2865                         status = "declined";
2866                 else
2867                         status = "";
2868                 if (status && *status)
2869                         e_gw_message_write_string_parameter (msg, "recipientStatus", NULL, status);
2870                 
2871                 soup_soap_message_end_element (msg);            
2872         }
2873         
2874         soup_soap_message_end_element (msg);
2875         
2876         if (priv->set_sendoptions) {    
2877                 soup_soap_message_start_element (msg, "sendoptions", NULL, NULL);
2878                         
2879                 soup_soap_message_start_element (msg, "requestReply", NULL, NULL);
2880                 if (priv->reply_request_set) {
2881                         
2882                         if (priv->reply_within)  
2883                                 e_gw_message_write_string_parameter (msg, "byDate", NULL, priv->reply_within);
2884                         else 
2885                                 e_gw_message_write_string_parameter (msg, "whenConvenient", NULL, "1");
2886                         
2887                 } else
2888                         soup_soap_message_write_string (msg, "0");
2889                 soup_soap_message_end_element (msg);
2890
2891                 soup_soap_message_start_element (msg, "statusTracking", NULL, NULL);
2892                 
2893                 soup_soap_message_add_attribute (msg, "autoDelete", priv->autodelete ? "1" : "0", NULL, NULL);
2894         
2895                 switch (priv->track_info) {
2896                         case E_GW_ITEM_DELIVERED : soup_soap_message_write_string (msg, "Delivered");
2897                                  break;
2898                         case E_GW_ITEM_DELIVERED_OPENED : soup_soap_message_write_string (msg, "DeliveredAndOpened");
2899                                  break;
2900                         case E_GW_ITEM_ALL : soup_soap_message_write_string (msg, "All");
2901                                  break;
2902                         default: soup_soap_message_write_string (msg, "None");
2903                 }
2904                 
2905                 soup_soap_message_end_element (msg);
2906         
2907                 soup_soap_message_start_element (msg, "notification", NULL, NULL);
2908                 switch (priv->item_type) {
2909                 
2910                 case E_GW_ITEM_TYPE_TASK :
2911                         add_return_notification (msg, "completed", priv->notify_completed);
2912                         
2913                 case E_GW_ITEM_TYPE_APPOINTMENT:
2914                         add_return_notification (msg, "accepted", priv->notify_accepted);
2915                         add_return_notification (msg, "declined", priv->notify_declined);
2916                         add_return_notification (msg, "opened", priv->notify_opened);
2917                         break;
2918                 
2919                 default:
2920                         add_return_notification (msg, "opened", priv->notify_opened);
2921                         add_return_notification (msg, "deleted", priv->notify_deleted);
2922                 }
2923                 soup_soap_message_end_element (msg);
2924
2925                 soup_soap_message_end_element (msg);
2926         }
2927
2928         soup_soap_message_end_element (msg);
2929 }
2930
2931 static void
2932 add_attachment_to_soap_message(EGwItemAttachment *attachment, SoupSoapMessage *msg)
2933 {
2934         char *size = NULL;
2935
2936         soup_soap_message_start_element (msg, "attachment", NULL, NULL) ;
2937
2938         /*id*/
2939         if (attachment->id && attachment->item_reference)
2940                 e_gw_message_write_string_parameter_with_attribute (msg,"id", NULL, attachment->id, "itemReference", attachment->item_reference);
2941         else if (attachment->id)
2942                 e_gw_message_write_string_parameter (msg, "id", NULL, attachment->id);
2943         else
2944                 e_gw_message_write_string_parameter (msg, "id", NULL, "");
2945         /*name*/
2946         e_gw_message_write_string_parameter (msg, "name", NULL, attachment->name) ;
2947         /*content id*/
2948         e_gw_message_write_string_parameter (msg, "contentId", NULL, attachment->contentid);
2949         /*content type*/
2950         e_gw_message_write_string_parameter (msg, "contentType", NULL, attachment->contentType) ;
2951         /*size*/
2952         if (attachment->size > 0) {
2953                 size = g_strdup_printf ("%d", attachment->size) ;
2954                 e_gw_message_write_string_parameter (msg, "size", NULL, size) ;
2955         } 
2956         /*date*/
2957         if (attachment->date) 
2958                 e_gw_message_write_string_parameter (msg, "date", NULL, attachment->date) ;     
2959
2960         /*data*/
2961         if (attachment->size > 0) {
2962                 soup_soap_message_start_element (msg, "data", NULL, NULL) ;
2963                 soup_soap_message_add_attribute (msg, "contentId", attachment->id, NULL, NULL);
2964                 soup_soap_message_add_attribute (msg, "contentType", attachment->contentType, NULL, NULL) ;
2965                 soup_soap_message_add_attribute (msg, "length", size, NULL, NULL) ;
2966                 soup_soap_message_write_string (msg, attachment->data) ;
2967                 soup_soap_message_end_element (msg) ;
2968         }
2969         
2970         soup_soap_message_end_element (msg) ;
2971         g_free (size) ;
2972 }
2973
2974 static void 
2975 e_gw_item_set_calendar_item_elements (EGwItem *item, SoupSoapMessage *msg)
2976 {
2977         EGwItemPrivate *priv = item->priv;
2978         char *dtstring;
2979
2980         if (priv->id)
2981                 e_gw_message_write_string_parameter (msg, "id", NULL, priv->id);
2982         if (priv->container)
2983                 e_gw_message_write_string_parameter (msg, "container", NULL, priv->container);
2984
2985         if (priv->icalid)
2986                 e_gw_message_write_string_parameter (msg, "iCalId", NULL, priv->icalid);
2987
2988         if (item->priv->source)
2989                 e_gw_message_write_string_parameter (msg, "source", NULL, item->priv->source );
2990
2991         if (priv->classification)
2992                 e_gw_message_write_string_parameter (msg, "class", NULL, priv->classification);
2993         else
2994                 e_gw_message_write_string_parameter (msg, "class", NULL, "");
2995
2996         e_gw_message_write_string_parameter (msg, "subject", NULL, priv->subject ? priv->subject : "");
2997
2998         if (priv->recipient_list != NULL) {
2999                 e_gw_item_add_distribution_to_soap_message (item, msg);
3000                 if (priv->set_sendoptions)
3001                         append_gw_item_options (msg, item);
3002         }
3003
3004         soup_soap_message_start_element (msg, "message", NULL, NULL);
3005         if (priv->message) {
3006                 char *str;
3007                 
3008                 str = soup_base64_encode (priv->message, strlen (priv->message));
3009                 dtstring = g_strdup_printf ("%d", (int)strlen (str));
3010                 soup_soap_message_add_attribute (msg, "length", dtstring, NULL, NULL);
3011                 g_free (dtstring);
3012                 soup_soap_message_write_string (msg, str);
3013                 g_free (str);
3014         } else {
3015                 soup_soap_message_add_attribute (msg, "length", "0", NULL, NULL);
3016                 soup_soap_message_write_string (msg, "");
3017         }
3018
3019         soup_soap_message_end_element (msg);
3020
3021         if (priv->start_date) {
3022                 e_gw_message_write_string_parameter (msg, "startDate", NULL, 
3023                                 priv->start_date);
3024         }
3025
3026         if (priv->category_list) {
3027                         soup_soap_message_start_element (msg, "categories", NULL, NULL);
3028                         
3029                         if (priv->category_list && priv->category_list->data) 
3030                                 soup_soap_message_add_attribute (msg, "types:primary", priv->category_list->data, NULL, NULL);
3031                         
3032                         for (; priv->category_list != NULL; priv->category_list = g_list_next (priv->category_list)) 
3033                                 if (priv->category_list->data) {
3034                                         e_gw_message_write_string_parameter (msg, "category", NULL, priv->category_list->data);
3035                                 }
3036                         soup_soap_message_end_element (msg);
3037                 }
3038         
3039         /* handle recurrences */
3040          if (item->priv->rrule) {
3041                 EGwItemRecurrenceRule *rrule = item->priv->rrule;
3042
3043                 soup_soap_message_start_element (msg, "rrule", NULL, NULL);
3044                 
3045                 e_gw_message_write_string_parameter (msg, "frequency", NULL, rrule->frequency); 
3046                 e_gw_message_write_int_parameter (msg, "interval", NULL, rrule->interval); 
3047
3048                 if (rrule->until)
3049                         e_gw_message_write_string_parameter (msg, "until", NULL, rrule->until);
3050                 else
3051                         e_gw_message_write_int_parameter (msg, "count", NULL, rrule->count);
3052
3053                 /* byDay */
3054                 if (rrule->by_day[0]) {
3055                         int i, max_elements;
3056                         soup_soap_message_start_element (msg, "byDay", NULL, NULL);
3057                         max_elements = sizeof (rrule->by_day) / sizeof (rrule->by_day[0]);
3058                         /* expand into  a sequence of 'day' here  */
3059                         for (i = 0; i < max_elements && rrule->by_day [i] != E_GW_ITEM_RECUR_END_MARKER; i++) {
3060                                 /*TODO occurence attribute */
3061                                 e_gw_message_write_string_parameter (msg, "day", NULL, 
3062                                                 e_gw_recur_get_day_of_week (rrule->by_day [i]));
3063                         }
3064                         soup_soap_message_end_element (msg);
3065                 }
3066                 /* byMonthDay*/
3067                 if (rrule->by_month_day) {
3068                         int i, max_elements;
3069                         char month_day[3];
3070                         
3071                         soup_soap_message_start_element (msg, "byMonthDay", NULL, NULL);
3072                         max_elements = sizeof (rrule->by_month_day)  / sizeof (rrule->by_month_day [i]);
3073                         /* expand into  a sequence of 'day' here  */
3074                         for (i = 0; i < max_elements && rrule->by_month_day [i] != E_GW_ITEM_RECUR_END_MARKER; i++) {
3075                                 /*TODO occurence attribute */
3076                                 g_sprintf (month_day, "%d", rrule->by_month_day [i]);
3077                                 e_gw_message_write_string_parameter (msg, "day", NULL, month_day);
3078                                                                                 
3079                         }
3080                         soup_soap_message_end_element (msg);
3081                 }
3082                 /* byYearDay */
3083         if (rrule->by_year_day) {
3084                         int i, max_elements;
3085                         char year_day[4];
3086                         soup_soap_message_start_element (msg, "byYearDay", NULL, NULL);
3087                         max_elements = sizeof (rrule->by_year_day)  / sizeof (rrule->by_year_day [i]);
3088                         /* expand into  a sequence of 'day' here  */
3089                         for (i = 0; i < max_elements && rrule->by_year_day [i] != E_GW_ITEM_RECUR_END_MARKER; i++) {
3090                                 /*TODO occurence attribute */
3091                                 g_sprintf (year_day, "%d", rrule->by_year_day [i]);
3092                                 e_gw_message_write_string_parameter (msg, "day", NULL, year_day);
3093                                                                                 
3094                         }
3095                 soup_soap_message_end_element (msg);
3096                 }
3097                 /* byMonth */
3098                 if (rrule->by_month) {
3099                         int i, max_elements;
3100                         char month[3];
3101                         soup_soap_message_start_element (msg, "byMonth", NULL, NULL);
3102                         max_elements = sizeof (rrule->by_month)  / sizeof (rrule->by_month [i]);
3103                         /* expand into  a sequence of 'month' here  */
3104                         for (i = 0; i < max_elements && rrule->by_month [i] != E_GW_ITEM_RECUR_END_MARKER; i++) {
3105                                 /*TODO occurence attribute */
3106                                 g_sprintf (month, "%d", rrule->by_month [i]);
3107                                 e_gw_message_write_string_parameter (msg, "month", NULL, month);
3108                                                                                 
3109                         }
3110                         soup_soap_message_end_element (msg);
3111                 }
3112                 soup_soap_message_end_element (msg);
3113                 
3114         } else if (item->priv->recurrence_dates) {
3115                 GSList *date;
3116                 soup_soap_message_start_element (msg, "rdate", NULL, NULL);
3117                 for (date = item->priv->recurrence_dates; date != NULL; date = g_slist_next (date)) {
3118                         e_gw_message_write_string_parameter (msg, "date", NULL, (char *) date->data);
3119                 }
3120                 soup_soap_message_end_element (msg);
3121         }
3122         
3123         if (item->priv->exdate_list) {
3124                 GSList *date;
3125                 soup_soap_message_start_element (msg, "exdate", NULL, NULL);
3126                 for (date = item->priv->exdate_list; date != NULL; date = g_slist_next (date)) {
3127                         e_gw_message_write_string_parameter (msg, "date", NULL, (char *) date->data);
3128                 }
3129                 soup_soap_message_end_element (msg);
3130         }
3131         /*xxx - byday, bymonthday and byyearday not handled - FIXME */  
3132
3133         /*attachments*/
3134         if (priv->attach_list) {
3135                 GSList *al ;
3136                 soup_soap_message_start_element (msg, "attachments", NULL, NULL) ;
3137                 for (al = priv->attach_list ; al != NULL ;  al = al->next) {
3138                         EGwItemAttachment *attachment = (EGwItemAttachment *)al->data ;
3139                         add_attachment_to_soap_message (attachment, msg) ;
3140                 
3141                 }
3142                 soup_soap_message_end_element (msg) ;
3143         }
3144 }
3145
3146 gboolean
3147 e_gw_item_append_to_soap_message (EGwItem *item, SoupSoapMessage *msg)
3148 {
3149         EGwItemPrivate *priv;
3150         char *alarm;
3151
3152         g_return_val_if_fail (E_IS_GW_ITEM (item), FALSE);
3153         g_return_val_if_fail (SOUP_IS_SOAP_MESSAGE (msg), FALSE);
3154
3155         priv = item->priv;
3156
3157         soup_soap_message_start_element (msg, "item", "types", NULL);
3158
3159         switch (priv->item_type) {
3160         case E_GW_ITEM_TYPE_MAIL :
3161                 soup_soap_message_add_attribute (msg, "type", "Mail", "xsi", NULL);
3162
3163                 if (priv->security)
3164                         e_gw_message_write_string_parameter (msg, "security", NULL, priv->security);
3165                 
3166                 /*The subject*/
3167                 if (priv->subject)
3168                         e_gw_message_write_string_parameter (msg, "subject", NULL, priv->subject) ;
3169                 /*distribution*/
3170                 e_gw_item_add_distribution_to_soap_message(item, msg) ;
3171                 
3172                 if (priv->set_sendoptions) {
3173                         /* item options */
3174                         append_gw_item_options (msg, item);
3175                 }
3176                 /*container*/
3177                 if (item->priv->container)
3178                         e_gw_message_write_string_parameter (msg, "container", NULL, item->priv->container);
3179                 if (item->priv->source)
3180                         e_gw_message_write_string_parameter (msg, "source", NULL, item->priv->source);
3181                 
3182                 /*message*/
3183                 soup_soap_message_start_element (msg, "message", NULL, NULL);
3184                 if (priv->message) {
3185                         char *str ;
3186                         char *str_len ;
3187
3188                         str = soup_base64_encode (priv->message, strlen (priv->message));
3189                         //str = g_strdup (priv->message);
3190                         str_len = g_strdup_printf ("%d", str ? (int)strlen (str) : 0);
3191                         soup_soap_message_start_element (msg, "part", NULL, NULL);
3192                         soup_soap_message_add_attribute (msg, "length", str_len, NULL, NULL);
3193                         soup_soap_message_add_attribute (msg, "contentType", priv->content_type, NULL, NULL);
3194                         g_free (str_len);
3195                         soup_soap_message_write_string (msg, str);
3196                         soup_soap_message_end_element (msg);
3197                         g_free (str);
3198                 } else {
3199                         soup_soap_message_add_attribute (msg, "length", "0", NULL, NULL);
3200                         soup_soap_message_write_string (msg, "");
3201                 }
3202
3203                 soup_soap_message_end_element (msg);
3204
3205                 /*attachments*/
3206                 if (priv->attach_list) {
3207                         GSList *al;
3208                         soup_soap_message_start_element (msg, "attachments", NULL, NULL);
3209                         for (al = priv->attach_list ; al != NULL ;  al = al->next) {
3210                                 EGwItemAttachment *attachment = (EGwItemAttachment *)al->data;
3211                                 add_attachment_to_soap_message (attachment, msg);
3212                         }
3213                         soup_soap_message_end_element (msg);
3214                 }
3215
3216                 /*Link info - used when a mail is being replied to or forwarded*/
3217                 if (priv->link_info) {
3218                         soup_soap_message_start_element (msg, "link", NULL, NULL);
3219                         e_gw_message_write_string_parameter (msg, "id", NULL, priv->link_info->id) ;
3220                         e_gw_message_write_string_parameter (msg, "type", NULL, priv->link_info->type) ;
3221                         e_gw_message_write_string_parameter (msg, "thread", NULL, priv->link_info->thread) ;
3222                         soup_soap_message_end_element (msg);
3223                 }
3224
3225                 break;
3226
3227         case E_GW_ITEM_TYPE_APPOINTMENT :
3228                 soup_soap_message_add_attribute (msg, "type", "Appointment", "xsi", NULL);
3229
3230                 /* Calendar Item properties. */
3231                 e_gw_item_set_calendar_item_elements (item, msg);
3232
3233                 /* Appointment specific properties */
3234                 if (priv->end_date) {
3235                         e_gw_message_write_string_parameter (msg, "endDate", NULL, priv->end_date);
3236                 } else
3237                         e_gw_message_write_string_parameter (msg, "endDate", NULL, "");
3238
3239                 e_gw_message_write_string_parameter (msg, "acceptLevel", NULL, priv->accept_level ? priv->accept_level : "");
3240                 if (priv->is_allday_event)
3241                         e_gw_message_write_string_parameter (msg, "allDayEvent", NULL, "1");
3242                 else
3243                         e_gw_message_write_string_parameter (msg, "allDayEvent", NULL ,"0");
3244
3245
3246                 if (priv->trigger != 0) {
3247                         alarm = g_strdup_printf ("%d", priv->trigger);
3248                         e_gw_message_write_string_parameter_with_attribute (msg, "alarm", NULL, alarm, "enabled", "1");
3249                         g_free (alarm);
3250                 }
3251                 e_gw_message_write_string_parameter (msg, "place", NULL, priv->place ? priv->place : "");
3252                 
3253                 break;
3254
3255         case E_GW_ITEM_TYPE_TASK :
3256                 soup_soap_message_add_attribute (msg, "type", "Task", "xsi", NULL);
3257
3258                 /* Calendar Item properties. */
3259                 e_gw_item_set_calendar_item_elements (item, msg);
3260
3261                 /* Task specific properties */
3262                 if (priv->due_date) {
3263                         e_gw_message_write_string_parameter (msg, "dueDate", NULL, priv->due_date);
3264                 } else
3265                         e_gw_message_write_string_parameter (msg, "dueDate", NULL, "");
3266
3267                 e_gw_message_write_string_parameter (msg, "taskPriority", NULL, priv->task_priority ? priv->task_priority : "");
3268                 if (priv->completed)
3269                         e_gw_message_write_string_parameter (msg, "completed", NULL, "1");
3270                 else
3271                         e_gw_message_write_string_parameter (msg, "completed", NULL, "0");
3272
3273                 break;
3274         case E_GW_ITEM_TYPE_NOTE:
3275                 soup_soap_message_add_attribute (msg, "type", "Note", "xsi", NULL);
3276                 
3277                 e_gw_item_set_calendar_item_elements (item, msg);
3278                 break;
3279         case E_GW_ITEM_TYPE_CONTACT :
3280                 soup_soap_message_add_attribute (msg, "type", "Contact", "xsi", NULL);
3281                 append_contact_fields_to_soap_message (item, msg);
3282                 soup_soap_message_end_element(msg);
3283                 return TRUE;
3284         case E_GW_ITEM_TYPE_GROUP :
3285                 soup_soap_message_add_attribute (msg, "type", "Group", "xsi", NULL);
3286                 append_group_fields_to_soap_message (item, msg);
3287                 soup_soap_message_end_element(msg); 
3288                 return TRUE;
3289         case E_GW_ITEM_TYPE_ORGANISATION :
3290                 soup_soap_message_add_attribute (msg, "type", "Organization", "xsi", NULL); 
3291                 append_contact_fields_to_soap_message (item, msg);
3292                 soup_soap_message_end_element(msg); 
3293                 return TRUE;
3294         case E_GW_ITEM_TYPE_CATEGORY :
3295                 soup_soap_message_add_attribute (msg, "type", "Category", "xsi", NULL);
3296                 e_gw_message_write_string_parameter (msg, "name", NULL, item->priv->category_name);
3297                 soup_soap_message_end_element(msg); 
3298                 return TRUE;
3299         default :
3300                 g_warning (G_STRLOC ": Unknown type for item");
3301                 return FALSE;
3302         }
3303
3304         soup_soap_message_end_element (msg);
3305
3306         return TRUE;
3307 }
3308
3309
3310 static void
3311 append_contact_changes_to_soap_message (EGwItem *item, SoupSoapMessage *msg, int change_type)
3312 {
3313         GHashTable *changes;
3314         EGwItemPrivate *priv;
3315         FullName *full_name;
3316         char *value;
3317         GList *list;
3318         PostalAddress *postal_address;
3319
3320         priv = item->priv;
3321         changes = NULL;
3322         switch (change_type) {
3323         case E_GW_ITEM_CHANGE_TYPE_ADD :
3324                 changes = priv->additions;
3325                 soup_soap_message_start_element (msg, "add", NULL, NULL);
3326                 break;
3327         case E_GW_ITEM_CHANGE_TYPE_UPDATE :
3328                 changes = priv->updates;
3329                 soup_soap_message_start_element (msg, "update", NULL, NULL);
3330                 break;
3331         case E_GW_ITEM_CHANGE_TYPE_DELETE :
3332                 soup_soap_message_start_element (msg, "delete", NULL, NULL);
3333                 changes = priv->deletions;
3334                 break;
3335         
3336         }
3337         if (!changes)
3338                 return;
3339         list = g_hash_table_lookup (changes, "categories");
3340         append_common_addressbook_item_fields_to_soap_message (changes, list, msg);
3341         full_name = g_hash_table_lookup (changes, "full_name");
3342         value = g_hash_table_lookup (changes, "name");
3343         if (full_name) 
3344                 append_full_name_to_soap_message (full_name, value, msg);
3345         list = g_hash_table_lookup (changes, "email");
3346         if (list)
3347                 append_email_list_soap_message (list, msg);
3348         list = g_hash_table_lookup (changes, "ims");
3349         if (list)
3350                 append_im_list_to_soap_message (list, msg);
3351         append_phone_list_to_soap_message (changes, msg);
3352
3353         soup_soap_message_start_element (msg, "addressList", NULL, NULL);
3354         postal_address = g_hash_table_lookup (changes, "Home");
3355         if (postal_address)
3356                 append_postal_address_to_soap_message (msg, postal_address, "Home");
3357         postal_address = g_hash_table_lookup (changes, "Office");
3358         if (postal_address)
3359                 append_postal_address_to_soap_message (msg, postal_address, "Office");
3360         soup_soap_message_end_element (msg);
3361         
3362         append_office_info_to_soap_message (changes, msg);
3363         append_personal_info_to_soap_message (changes, msg);
3364
3365         soup_soap_message_end_element (msg);
3366
3367 }
3368
3369 static void
3370 append_event_changes_to_soap_message (EGwItem *item, SoupSoapMessage *msg, int change_type)
3371 {
3372         GHashTable *changes;
3373         EGwItemPrivate *priv;
3374
3375         priv = item->priv;
3376         changes = NULL;
3377         switch (change_type) {
3378         case E_GW_ITEM_CHANGE_TYPE_ADD :
3379                 changes = priv->additions;
3380                 if (!changes)
3381                         return;
3382                 soup_soap_message_start_element (msg, "add", NULL, NULL);
3383                 break;
3384         case E_GW_ITEM_CHANGE_TYPE_UPDATE :
3385                 changes = priv->updates;
3386                 if (!changes)
3387                         return;
3388                 soup_soap_message_start_element (msg, "update", NULL, NULL);
3389                 break;
3390         case E_GW_ITEM_CHANGE_TYPE_DELETE :
3391                 changes = priv->deletions;
3392                 if (!changes)
3393                         return;
3394                 soup_soap_message_start_element (msg, "delete", NULL, NULL);
3395                 break;
3396         }
3397         if (g_hash_table_lookup (changes, "categories")){
3398                 GList *list;    
3399                 list = g_hash_table_lookup (changes, "categories");
3400
3401                 soup_soap_message_start_element (msg, "categories", NULL, NULL);
3402                 if (list != NULL && list->data) 
3403                         soup_soap_message_add_attribute (msg, "types:primary",list->data, NULL, NULL);
3404                         for (; list != NULL; list = g_list_next (list)) 
3405                                 if (list->data) {
3406                                         e_gw_message_write_string_parameter (msg, "category", NULL, list->data);
3407                         }
3408                 soup_soap_message_end_element (msg);
3409                 g_list_free (list);
3410         }
3411         if (g_hash_table_lookup (changes, "subject"))
3412                 e_gw_message_write_string_parameter (msg, "subject", NULL, priv->subject ? priv->subject : "");
3413         if (g_hash_table_lookup (changes, "start_date")) {
3414                 if (priv->start_date) {
3415                         e_gw_message_write_string_parameter (msg, "startDate", NULL, priv->start_date); 
3416                 }
3417         }
3418         if (g_hash_table_lookup (changes, "end_date")) {
3419                 if (priv->end_date) {
3420                         e_gw_message_write_string_parameter (msg, "endDate", NULL, priv->end_date);
3421                 }
3422         }
3423         if (g_hash_table_lookup (changes, "allDayEvent"))
3424                 e_gw_message_write_string_parameter (msg, "allDayEvent", NULL, priv->is_allday_event ? "1" : "0");
3425         if (g_hash_table_lookup (changes, "message")) {
3426                 soup_soap_message_start_element (msg, "message", NULL, NULL);
3427                 if (priv->message) {
3428                         char *str, *message;
3429
3430                         str = soup_base64_encode (priv->message, strlen (priv->message));
3431                         message = g_strdup_printf ("%d", (int)strlen (str));
3432                         soup_soap_message_add_attribute (msg, "length", message, NULL, NULL);
3433                         g_free (message);
3434                         soup_soap_message_write_string (msg, str);
3435                         g_free (str);
3436                 } else {
3437                         soup_soap_message_add_attribute (msg, "length", "0", NULL, NULL);
3438                         soup_soap_message_write_string (msg, "");
3439                 }
3440
3441                 soup_soap_message_end_element (msg);
3442         }
3443         if (g_hash_table_lookup (changes, "classification"))
3444                 e_gw_message_write_string_parameter (msg, "class", NULL, priv->classification);
3445         if (g_hash_table_lookup (changes, "task_priority")) {
3446                 e_gw_message_write_string_parameter (msg, "taskPriority", NULL, priv->task_priority);
3447         }
3448         if (g_hash_table_lookup (changes, "accept_level"))
3449                 e_gw_message_write_string_parameter (msg, "acceptLevel", NULL, priv->accept_level ? priv->accept_level : "");
3450         if (g_hash_table_lookup (changes, "place"))
3451                 e_gw_message_write_string_parameter (msg, "place", NULL, priv->place ? priv->place : "");
3452         if (g_hash_table_lookup (changes, "alarm")) {
3453                 if (priv->trigger != 0) {
3454                         char *alarm = g_strdup_printf ("%d", priv->trigger);
3455                         e_gw_message_write_string_parameter_with_attribute (msg, "alarm", NULL, alarm, "enabled", "1");
3456                         g_free (alarm);
3457                 }
3458                 else
3459                         e_gw_message_write_string_parameter_with_attribute (msg, "alarm", NULL, "0", "enabled", "0");
3460         }
3461         if (g_hash_table_lookup (changes, "completed"))
3462                 e_gw_message_write_string_parameter (msg, "completed", NULL, priv->completed ? "1" : "0");
3463         if (g_hash_table_lookup (changes, "due_date"))
3464                 e_gw_message_write_string_parameter (msg, "dueDate", NULL, priv->due_date);
3465         soup_soap_message_end_element (msg);
3466
3467 }
3468
3469 gboolean 
3470 e_gw_item_append_changes_to_soap_message (EGwItem *item, SoupSoapMessage *msg)
3471 {
3472         EGwItemPrivate *priv;
3473         char *value;
3474         g_return_val_if_fail (E_IS_GW_ITEM (item), FALSE);
3475         g_return_val_if_fail (SOUP_IS_SOAP_MESSAGE (msg), FALSE);
3476
3477         priv = item->priv;
3478
3479         soup_soap_message_start_element (msg, "updates", NULL, NULL);
3480
3481         switch (priv->item_type) {
3482         case E_GW_ITEM_TYPE_CONTACT :
3483         case E_GW_ITEM_TYPE_ORGANISATION :
3484                 append_contact_changes_to_soap_message (item, msg, E_GW_ITEM_CHANGE_TYPE_ADD);
3485                 append_contact_changes_to_soap_message (item, msg, E_GW_ITEM_CHANGE_TYPE_UPDATE);
3486                 append_contact_changes_to_soap_message (item, msg, E_GW_ITEM_CHANGE_TYPE_DELETE);
3487                 soup_soap_message_end_element(msg); 
3488                 return TRUE;
3489         case E_GW_ITEM_TYPE_GROUP :
3490                 soup_soap_message_start_element (msg, "update", NULL, NULL);
3491                 value = g_hash_table_lookup (item->priv->simple_fields, "name");
3492                 if (value)
3493                         e_gw_message_write_string_parameter (msg, "name", NULL, value);
3494                 soup_soap_message_end_element (msg);
3495                 soup_soap_message_end_element(msg); 
3496                 return TRUE;
3497         case E_GW_ITEM_TYPE_APPOINTMENT:
3498         case E_GW_ITEM_TYPE_TASK :
3499         case E_GW_ITEM_TYPE_NOTE:
3500                 append_event_changes_to_soap_message (item, msg, E_GW_ITEM_CHANGE_TYPE_ADD);
3501                 append_event_changes_to_soap_message (item, msg, E_GW_ITEM_CHANGE_TYPE_UPDATE);
3502                 append_event_changes_to_soap_message (item, msg, E_GW_ITEM_CHANGE_TYPE_DELETE);
3503                 soup_soap_message_end_element(msg); 
3504                 return TRUE;
3505         default :
3506                 g_warning (G_STRLOC ": Unknown type for item");
3507                 return FALSE;
3508         }
3509
3510 }