Fix new Coverity defects
[claws.git] / src / plugins / vcalendar / vcal_manager.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Colin Leroy <colin@colino.net> and 
4  * the Claws Mail team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #include "claws-features.h"
24 #endif
25
26 #include <stddef.h>
27 #include <glib.h>
28 #include <glib/gi18n.h>
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #ifdef USE_PTHREAD
33 #include <pthread.h>
34 #endif
35 #include <ical.h>
36 #include "vcalendar.h"
37 #include "vcal_folder.h"
38 #include "vcal_manager.h"
39 #include "vcal_meeting_gtk.h"
40 #include "vcal_prefs.h"
41 #include "xml.h"
42 #include "xmlprops.h"
43 #include "prefs.h"
44 #include "prefs_common.h"
45 #include "prefs_account.h"
46 #include "alertpanel.h"
47 #include "account.h"
48 #include "codeconv.h"
49 #include <time.h>
50 #include "folder.h"
51 #include "quoted-printable.h"
52 #include "utils.h"
53
54 #ifdef G_OS_WIN32
55 #define getuid() 0
56 #endif
57
58 Answer *answer_new(const gchar *attendee, 
59                           const gchar *name,
60                           enum icalparameter_partstat ans,
61                           enum icalparameter_cutype cutype)
62 {
63         Answer *answer = g_new0(Answer, 1);
64         answer->attendee = g_strdup(attendee);
65         answer->name = g_strdup(name);
66         if (!answer->name)
67                 answer->name = g_strdup("");
68         if (!answer->attendee)
69                 answer->attendee = g_strdup("");
70         answer->answer = ans;
71         answer->cutype = cutype;
72         return answer;
73 }
74
75 static void answer_free(Answer *answer)
76 {
77         g_free(answer->attendee);
78         g_free(answer->name);
79         g_free(answer);
80 }
81
82 static GSList *answer_find(VCalEvent *event, Answer *answer)
83 {
84         GSList *cur = event->answers;
85         while (cur && cur->data) {
86                 Answer *b = (Answer *)cur->data;
87                 if (!strcasecmp(b->attendee, answer->attendee))
88                         return cur;
89                 cur = cur->next;
90         }
91         return NULL;
92 }
93
94 void vcal_manager_copy_attendees(VCalEvent *src, VCalEvent *dest)
95 {
96         GSList *cur = src->answers;
97         while (cur && cur->data) {
98                 Answer *a = (Answer *)cur->data;
99                 Answer *b = answer_new(a->attendee, a->name, a->answer, a->cutype);
100                 dest->answers = g_slist_prepend(dest->answers, b);
101                 cur = cur->next;
102         }
103         dest->answers = g_slist_reverse(dest->answers);
104 }
105
106 gchar *vcal_manager_answer_get_text(enum icalparameter_partstat ans) 
107 {
108         static gchar *replies[5]={
109                 N_("accepted"),
110                 N_("tentatively accepted"),
111                 N_("declined"),
112                 N_("did not answer"),
113                 N_("unknown")
114         };
115
116         switch (ans) {
117         case ICAL_PARTSTAT_ACCEPTED:
118                 return _(replies[0]);
119                 break;
120         case ICAL_PARTSTAT_DECLINED:
121                 return _(replies[2]);
122                 break;
123         case ICAL_PARTSTAT_TENTATIVE:
124                 return _(replies[1]);
125                 break;
126         case ICAL_PARTSTAT_NEEDSACTION:
127                 return _(replies[3]);
128         case ICAL_PARTSTAT_DELEGATED:
129         case ICAL_PARTSTAT_COMPLETED:
130         case ICAL_PARTSTAT_X:
131         case ICAL_PARTSTAT_INPROCESS:
132         case ICAL_PARTSTAT_NONE:
133                 return _(replies[4]);
134                 break;                  
135         }
136         return NULL;
137 }
138
139 gchar *vcal_manager_cutype_get_text(enum icalparameter_cutype type) 
140 {
141         static gchar *replies[5]={
142                 N_("individual"),
143                 N_("group"),
144                 N_("resource"),
145                 N_("room"),
146                 N_("unknown")
147         };
148
149         switch (type) {
150         case ICAL_CUTYPE_INDIVIDUAL:
151                 return _(replies[0]);
152                 break;
153         case ICAL_CUTYPE_GROUP:
154                 return _(replies[1]);
155                 break;
156         case ICAL_CUTYPE_RESOURCE:
157                 return _(replies[2]);
158                 break;
159         case ICAL_CUTYPE_ROOM:
160                 return _(replies[3]);
161         default:
162                 return _(replies[4]);
163                 break;                  
164         }
165         return NULL;
166 }
167
168 static GSList *answer_remove(VCalEvent *event, Answer *answer)
169 {
170         GSList *cur = answer_find(event, answer);
171         if (cur) {
172                 Answer *b = (Answer *)cur->data;
173                 event->answers = g_slist_remove(event->answers, b);
174                 answer_free(b);
175         }
176         return event->answers;
177 }
178
179 static GSList *answer_add(VCalEvent *event, Answer *answer)
180 {
181         event->answers = g_slist_append(event->answers, answer);
182         return event->answers;
183 }
184
185 GSList *vcal_manager_get_answers_emails(VCalEvent *event)
186 {
187         GSList *new = NULL;
188         GSList *cur = event->answers;
189         while (cur && cur->data) {
190                 Answer *b = (Answer *)cur->data;
191                 new = g_slist_prepend(new, b->attendee);
192                 cur = cur->next;
193         }
194         new = g_slist_reverse(new);
195         return new;     
196 }
197
198 enum icalparameter_partstat vcal_manager_get_reply_for_attendee(VCalEvent *event, const gchar *att)
199 {
200         Answer *a = answer_new(att, NULL, 0, 0);
201         GSList *ans = answer_find(event, a);
202         enum icalparameter_partstat res = 0;
203         if (ans) {
204                 Answer *b = (Answer *)ans->data;
205                 res = b->answer;
206         }
207         answer_free(a);
208         return res;
209 }
210
211 gchar *vcal_manager_get_cutype_text_for_attendee(VCalEvent *event, const gchar *att)
212 {
213         enum icalparameter_cutype status = vcal_manager_get_cutype_for_attendee(event, att);
214         gchar *res = NULL;
215         if (status != 0) 
216                 res = g_strdup(vcal_manager_cutype_get_text(status));
217
218         return res;
219 }
220
221 enum icalparameter_partstat vcal_manager_get_cutype_for_attendee(VCalEvent *event, const gchar *att)
222 {
223         Answer *a = answer_new(att, NULL, 0, 0);
224         GSList *ans = answer_find(event, a);
225         enum icalparameter_cutype res = 0;
226         if (ans) {
227                 Answer *b = (Answer *)ans->data;
228                 res = b->cutype;
229         }
230         answer_free(a);
231         return res;
232 }
233
234 gchar *vcal_manager_get_reply_text_for_attendee(VCalEvent *event, const gchar *att)
235 {
236         enum icalparameter_partstat status = vcal_manager_get_reply_for_attendee(event, att);
237         gchar *res = NULL;
238         if (status != 0) 
239                 res = g_strdup(vcal_manager_answer_get_text(status));
240
241         return res;
242 }
243
244 gchar *vcal_manager_get_attendee_name(VCalEvent *event, const gchar *att)
245 {
246         Answer *a = answer_new(att, NULL, 0, 0);
247         GSList *ans = answer_find(event, a);
248         gchar *res = NULL;
249         if (ans) {
250                 Answer *b = (Answer *)ans->data;
251                 if (b->name)
252                         res = g_strdup(b->name);
253         }
254         answer_free(a);
255         return res;
256 }
257
258 void vcal_manager_event_print(VCalEvent *event)
259 {
260         GSList *list = event->answers;
261         printf( "event->uid\t\t%s\n"
262                 "event->organizer\t\t%s\n"
263                 "event->start\t\t%s\n"
264                 "event->end\t\t%s\n"
265                 "event->location\t\t%s\n"
266                 "event->summary\t\t%s\n"
267                 "event->description\t%s\n"
268                 "event->url\t%s\n"
269                 "event->dtstart\t\t%s\n"
270                 "event->dtend\t\t%s\n"
271                 "event->recur\t\t%s\n"
272                 "event->tzid\t\t%s\n"
273                 "event->method\t\t%d\n"
274                 "event->sequence\t\t%d\n",
275                 event->uid,
276                 event->organizer,
277                 event->start,
278                 event->end,
279                 event->location,
280                 event->summary,
281                 event->description,
282                 event->url,
283                 event->dtstart,
284                 event->dtend,
285                 event->recur,
286                 event->tzid,
287                 event->method,
288                 event->sequence);
289         while (list && list->data) {
290                 Answer *a = (Answer *)list->data;
291                 printf(" ans: %s %s, %s\n", a->name, a->attendee, vcal_manager_answer_get_text(a->answer));
292                 list = list->next;
293         }
294         
295 }
296
297 static gchar *write_headers(PrefsAccount        *account, 
298                             VCalEvent           *event,
299                             gboolean             short_headers,
300                             gboolean             is_reply, 
301                             gboolean             is_pseudo_event);
302
303 gchar *vcal_manager_event_dump(VCalEvent *event, gboolean is_reply, gboolean is_pseudo_event,
304                                 icalcomponent *use_calendar, gboolean modif)
305 {
306         gchar *organizer = g_strdup_printf("MAILTO:%s", event->organizer);
307         PrefsAccount *account = vcal_manager_get_account_from_event(event);
308         gchar *attendee  = NULL;
309         gchar *body, *headers;
310         gchar *tmpfile = NULL;
311         icalcomponent *calendar, *ievent, *timezone, *tzc;
312         icalproperty *attprop;
313         icalproperty *orgprop;
314         enum icalparameter_partstat status = ICAL_PARTSTAT_NEEDSACTION;
315         gchar *sanitized_uid = g_strdup(event->uid);
316         
317         subst_for_filename(sanitized_uid);
318
319         tmpfile = g_strdup_printf("%s%cevt-%d-%s", get_tmp_dir(),
320                                       G_DIR_SEPARATOR, getuid(), sanitized_uid);
321         g_free(sanitized_uid);
322
323         if (!account) {
324                 g_free(organizer);
325                 g_free(tmpfile);
326                 debug_print("no account found\n");
327                 return NULL;
328         }
329
330         attendee = g_strdup_printf("MAILTO:%s", account->address);
331         
332         if (vcal_manager_get_reply_for_attendee(event, account->address) != 0)
333                 status = vcal_manager_get_reply_for_attendee(event, account->address);
334         
335         tzset();
336         
337         if (use_calendar != NULL) {
338                 calendar = use_calendar;
339                 g_free(tmpfile);
340                 tmpfile = NULL;
341         } else
342                 calendar = 
343                         icalcomponent_vanew(
344                             ICAL_VCALENDAR_COMPONENT,
345                             icalproperty_new_version("2.0"),
346                             icalproperty_new_prodid(
347                                  "-//Claws Mail//NONSGML Claws Mail Calendar//EN"),
348                             icalproperty_new_calscale("GREGORIAN"),
349                             icalproperty_new_method(is_reply ? ICAL_METHOD_REPLY:event->method),
350                             0
351                             );  
352
353         if (!calendar) {
354                 g_warning ("can't generate calendar");
355                 g_free(organizer);
356                 g_free(tmpfile);
357                 g_free(attendee);
358                 return NULL;
359         }
360
361         orgprop = icalproperty_new_organizer(organizer);
362         
363         timezone = icalcomponent_new(ICAL_VTIMEZONE_COMPONENT);
364         
365         icalcomponent_add_property(timezone,
366                 icalproperty_new_tzid("UTC")); /* free */
367         
368         tzc = icalcomponent_new(ICAL_XSTANDARD_COMPONENT);
369         icalcomponent_add_property(tzc,
370                 icalproperty_new_dtstart(
371                         icaltime_from_string("19700101T000000")));
372         icalcomponent_add_property(tzc,
373                 icalproperty_new_tzoffsetfrom(0.0));
374         icalcomponent_add_property(tzc,
375                 icalproperty_new_tzoffsetto(0.0));
376         icalcomponent_add_property(tzc,
377                 icalproperty_new_tzname("Greenwich meridian time"));
378
379         icalcomponent_add_component(timezone, tzc);
380
381         ievent = 
382             icalcomponent_vanew(
383                 ICAL_VEVENT_COMPONENT, 0);
384
385         if (!ievent) {
386                 g_warning ("can't generate event");
387                 g_free(organizer);
388                 g_free(tmpfile);
389                 g_free(attendee);
390                 return NULL;
391         }
392         
393         icalcomponent_add_property(ievent,
394                 icalproperty_new_uid(event->uid));
395         icalcomponent_add_property(ievent,
396                 icalproperty_vanew_dtstamp(icaltime_from_timet(time(NULL), TRUE), 0));
397         icalcomponent_add_property(ievent,
398                 icalproperty_vanew_dtstart((icaltime_from_string(event->dtstart)), 0));
399         icalcomponent_add_property(ievent,
400                 icalproperty_vanew_dtend((icaltime_from_string(event->dtend)), 0));
401         if (event->recur && *(event->recur)) {
402                 icalcomponent_add_property(ievent,
403                         icalproperty_vanew_rrule((icalrecurrencetype_from_string(event->recur)), 0));
404         }
405         icalcomponent_add_property(ievent,
406                 icalproperty_new_description(event->description));
407         icalcomponent_add_property(ievent,
408                 icalproperty_new_summary(event->summary));
409         icalcomponent_add_property(ievent,
410                 icalproperty_new_sequence(modif && !is_reply ? event->sequence + 1 : event->sequence));
411         icalcomponent_add_property(ievent,
412                 icalproperty_new_class("PUBLIC"));
413         icalcomponent_add_property(ievent,
414                 icalproperty_new_transp("OPAQUE"));
415         if (event->location && *event->location)
416                 icalcomponent_add_property(ievent,
417                         icalproperty_new_location(event->location));
418         else
419                 icalcomponent_add_property(ievent,
420                         icalproperty_new_location(""));
421         icalcomponent_add_property(ievent,
422                 icalproperty_new_status(ICAL_STATUS_CONFIRMED));
423         icalcomponent_add_property(ievent,
424                 icalproperty_vanew_created(icaltime_from_timet(time(NULL), TRUE), 0));
425         icalcomponent_add_property(ievent,
426                 icalproperty_vanew_lastmodified(icaltime_from_timet(time(NULL), TRUE), 0));
427         icalcomponent_add_property(ievent,              
428                 orgprop);
429
430         if (!icalcomponent_get_first_component(calendar, ICAL_VTIMEZONE_COMPONENT))
431                 icalcomponent_add_component(calendar, timezone);
432         else 
433                 icalcomponent_free(timezone);
434
435         icalcomponent_add_component(calendar, ievent);
436
437         if (event->url && *(event->url)) {
438                 attprop = icalproperty_new_url(event->url);
439                 icalcomponent_add_property(ievent, attprop);
440         }
441
442         if (is_reply) {
443                 /* dump only this attendee */
444                 attprop =
445                         icalproperty_vanew_attendee(
446                             attendee,
447                             icalparameter_new_role(
448                                 ICAL_ROLE_REQPARTICIPANT),
449                             icalparameter_new_rsvp(ICAL_RSVP_TRUE),
450                             icalparameter_new_partstat(status),
451                             0
452                             );
453                 icalcomponent_add_property(ievent, attprop);
454         } else {
455                 /* dump all attendees */
456                 GSList *cur = event->answers;
457                 while (cur && cur->data) {
458                         Answer *a = (Answer *)cur->data;
459
460                         if (a->cutype == 0)
461                                 a->cutype = ICAL_CUTYPE_INDIVIDUAL;
462                         if (a->answer == 0)
463                                 a->answer = ICAL_PARTSTAT_NEEDSACTION;
464
465                         attprop =
466                                 icalproperty_vanew_attendee(
467                                     a->attendee,
468                                     icalparameter_new_role(
469                                         ICAL_ROLE_REQPARTICIPANT),
470                                     icalparameter_new_rsvp(ICAL_RSVP_TRUE),
471                                     icalparameter_new_cutype(a->cutype),
472                                     icalparameter_new_partstat(a->answer),
473                                     0
474                                     );
475
476                         icalcomponent_add_property(ievent, attprop);
477                         cur = cur->next;
478                 }
479         }
480
481         if (use_calendar) {
482                 g_free(organizer);
483                 g_free(tmpfile);
484                 g_free(attendee);
485                 return NULL;
486         }
487
488         headers = write_headers(account, event, is_pseudo_event, is_reply, is_pseudo_event);
489
490         if (!headers) {
491                 g_warning("can't get headers");
492                 g_free(organizer);
493                 g_free(tmpfile);
494                 g_free(attendee);
495                 return NULL;
496         }
497
498         body = g_strdup_printf("%s"
499                                "\n"
500                                "%s", headers, icalcomponent_as_ical_string(calendar));
501         
502         if (str_write_to_file(body, tmpfile) < 0) {
503                 g_free(tmpfile);
504                 tmpfile = NULL;
505         }
506
507         chmod(tmpfile, S_IRUSR|S_IWUSR);
508
509         g_free(body);
510         g_free(headers);
511         icalcomponent_free(calendar);
512         g_free(attendee);
513         g_free(organizer);
514         
515         tzset();
516
517         return tmpfile; 
518 }
519
520 static void get_rfc822_date_from_time_t(gchar *buf, gint len, time_t t)
521 {
522         struct tm *lt;
523         gchar day[4], mon[4];
524         gint dd, hh, mm, ss, yyyy;
525         gchar buft1[512];
526         struct tm buft2;
527
528 #ifndef G_OS_WIN32
529         lt = localtime_r(&t, &buft2);
530 #else
531         if (t < 0)
532                 t = 1;
533         lt = localtime(&t);
534 #endif
535
536         sscanf(asctime_r(lt, buft1), "%3s %3s %d %d:%d:%d %d\n",
537                day, mon, &dd, &hh, &mm, &ss, &yyyy);
538         g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
539                    day, dd, mon, yyyy, hh, mm, ss, tzoffset(&t));
540 }
541
542 static gchar *write_headers_date(const gchar *uid)
543 {
544         gchar subject[512];
545         gchar *t_subject;
546         gchar date[128];
547         time_t t;
548         struct tm lt;
549         struct tm buft;
550
551         memset(subject, 0, sizeof(subject));
552         memset(date, 0, sizeof(date));
553         
554         if (!strcmp(uid, EVENT_PAST_ID)) {
555                 t = 1;
556                 t_subject = _("Past");
557         } else if (!strcmp(uid, EVENT_TODAY_ID)) {
558                 t = time(NULL);
559                 t_subject = _("Today");
560         }  else if (!strcmp(uid, EVENT_TOMORROW_ID)) {
561                 t = time(NULL) + 86400;
562                 t_subject = _("Tomorrow");
563         }  else if (!strcmp(uid, EVENT_THISWEEK_ID)) {
564                 t = time(NULL) + (86400*2);
565                 t_subject = _("This week");
566         }  else if (!strcmp(uid, EVENT_LATER_ID)) {
567                 t = time(NULL) + (86400*7);
568                 t_subject = _("Later");
569         }  else {
570                 g_warning("unknown spec date\n");
571                 return NULL;
572         } 
573         
574 #ifndef G_OS_WIN32
575         lt = *localtime_r(&t, &buft);
576 #else
577         if (t < 0)
578                 t = 1;
579         lt = *localtime(&t);
580 #endif
581         lt.tm_hour = lt.tm_min = lt.tm_sec = 0;
582         t = mktime(&lt);
583         get_rfc822_date_from_time_t(date, sizeof(date), t);
584         conv_encode_header(subject, 511, t_subject, strlen("Subject: "), FALSE);
585                                 
586         return g_strdup_printf("From: -\n"
587                                 "To: -\n"
588                                 "Subject: %s\n"
589                                 "Date: %s\n"
590                                 "MIME-Version: 1.0\n"
591                                 "Content-Type: text/plain; charset=\"UTF-8\";\n"
592                                 "Content-Transfer-Encoding: quoted-printable\n"
593                                 "Message-ID: <%s>\n",
594                                 subject,
595                                 date,
596                                 uid);
597 }
598
599 gchar *vcal_manager_dateevent_dump(const gchar *uid, FolderItem *item)
600 {
601         gchar *sanitized_uid = NULL;
602         gchar *headers = NULL;
603         gchar *lines, *body, *tmpfile;
604         EventTime date;
605         sanitized_uid = g_strdup(uid);
606         subst_for_filename(sanitized_uid);
607         
608         tmpfile = g_strdup_printf("%s%cevt-%d-%s", get_tmp_dir(),
609                                    G_DIR_SEPARATOR, getuid(), sanitized_uid);
610         g_free(sanitized_uid);
611
612         headers = write_headers_date(uid);
613
614         if (!headers) {
615                 g_warning("can't get headers");
616                 g_free(tmpfile);
617                 return NULL;
618         }
619
620         if (!strcmp(uid, EVENT_PAST_ID))
621                 date = EVENT_PAST;
622         else if (!strcmp(uid, EVENT_TODAY_ID))
623                 date = EVENT_TODAY;
624         else if (!strcmp(uid, EVENT_TOMORROW_ID))
625                 date = EVENT_TOMORROW;
626         else if (!strcmp(uid, EVENT_THISWEEK_ID))
627                 date = EVENT_THISWEEK;
628         else if (!strcmp(uid, EVENT_LATER_ID))
629                 date = EVENT_LATER;
630         else
631                 date = EVENT_PAST;
632
633         lines = get_item_event_list_for_date(item, date);
634         body = g_strdup_printf("%s"
635                                "\n"
636                                "%s", headers, lines);
637         g_free(lines);
638         if (str_write_to_file(body, tmpfile) < 0) {
639                 g_free(tmpfile);
640                 tmpfile = NULL;
641         } else
642                 chmod(tmpfile, S_IRUSR|S_IWUSR);
643
644         g_free(body);
645         g_free(headers);
646         
647         return tmpfile; 
648 }
649
650 static gchar *write_headers_ical(PrefsAccount   *account, 
651                             icalcomponent       *ievent,
652                             gchar               *orga);
653
654 gchar *vcal_manager_icalevent_dump(icalcomponent *event, gchar *orga, icalcomponent *use_calendar)
655 {
656         PrefsAccount *account = account_get_cur_account();
657         gchar *body, *headers, *qpbody;
658         gchar **lines = NULL;
659         gchar *tmpfile = NULL;
660         icalcomponent *calendar;
661         icalproperty *prop;
662         icalcomponent *ievent = NULL;
663         int i = 0;
664
665         ievent = icalcomponent_new_clone(event);
666
667         prop = icalcomponent_get_first_property(ievent, ICAL_UID_PROPERTY);
668         if (prop) {
669                 gchar *sanitized_uid = g_strdup(icalproperty_get_uid(prop));
670
671                 subst_for_filename(sanitized_uid);
672
673                 tmpfile = g_strdup_printf("%s%cevt-%d-%s", get_tmp_dir(),
674                                               G_DIR_SEPARATOR, getuid(), sanitized_uid);
675                 g_free(sanitized_uid);
676                 icalproperty_free(prop);
677         } else {
678                 tmpfile = g_strdup_printf("%s%cevt-%d-%p", get_tmp_dir(),
679                                       G_DIR_SEPARATOR, getuid(), ievent);
680         }
681
682         if (!account) {
683                 g_free(tmpfile);
684                 icalcomponent_free(ievent);
685                 return NULL;
686         }
687
688         tzset();
689         
690         if (use_calendar != NULL) {
691                 calendar = use_calendar;
692                 g_free(tmpfile);
693                 tmpfile = NULL;
694         } else
695                 calendar = 
696                         icalcomponent_vanew(
697                             ICAL_VCALENDAR_COMPONENT,
698                             icalproperty_new_version("2.0"),
699                             icalproperty_new_prodid(
700                                  "-//Claws Mail//NONSGML Claws Mail Calendar//EN"),
701                             icalproperty_new_calscale("GREGORIAN"),
702                             icalproperty_new_method(ICAL_METHOD_PUBLISH),
703                             0
704                             );  
705
706         if (!calendar) {
707                 g_warning ("can't generate calendar");
708                 g_free(tmpfile);
709                 icalcomponent_free(ievent);
710                 return NULL;
711         }
712         
713         icalcomponent_add_component(calendar, ievent);
714
715         if (use_calendar)
716                 return NULL;
717
718         headers = write_headers_ical(account, ievent, orga);
719
720         if (!headers) {
721                 g_warning("can't get headers");
722                 g_free(tmpfile);
723                 icalcomponent_free(calendar);
724                 return NULL;
725         }
726
727         lines = g_strsplit(icalcomponent_as_ical_string(calendar), "\n", 0);
728         qpbody = g_strdup("");
729         
730         /* encode to quoted-printable */
731         while (lines[i]) {
732                 gchar buf[256];
733                 gint e_len = strlen(qpbody), n_len = 0;
734                 gchar *outline = conv_codeset_strdup(lines[i], CS_UTF_8, conv_get_outgoing_charset_str());
735                 
736                 qp_encode_line(buf, (guchar *)outline);
737                 n_len = strlen(buf);
738                 
739                 qpbody = g_realloc(qpbody, e_len + n_len + 1);
740                 strcpy(qpbody+e_len, buf);
741                 *(qpbody+n_len+e_len) = '\0';
742                 
743                 g_free(outline);
744                 i++;
745         }
746         
747         body = g_strdup_printf("%s"
748                                "\n"
749                                "%s", headers, qpbody);
750         
751         if (str_write_to_file(body, tmpfile) < 0) {
752                 g_free(tmpfile);
753                 tmpfile = NULL;
754         } else
755                 chmod(tmpfile, S_IRUSR|S_IWUSR);
756
757         g_strfreev(lines);
758         g_free(body);
759         g_free(qpbody);
760         g_free(headers);
761         icalcomponent_free(calendar);
762         
763         return tmpfile; 
764 }
765
766 VCalEvent * vcal_manager_new_event      (const gchar    *uid, 
767                                          const gchar    *organizer,
768                                          const gchar    *orgname,
769                                          const gchar    *location,
770                                          const gchar    *summary,
771                                          const gchar    *description,
772                                          const gchar    *dtstart,
773                                          const gchar    *dtend,
774                                          const gchar    *recur,
775                                          const gchar    *tzid,
776                                          const gchar    *url,
777                                          enum icalproperty_method method,
778                                          gint            sequence,
779                                          enum icalcomponent_kind type)
780 {
781         VCalEvent *event = g_new0(VCalEvent, 1);
782
783         event->uid              = g_strdup(uid?uid:"");
784         event->organizer        = g_strdup(organizer?organizer:"");
785         event->orgname          = g_strdup(orgname?orgname:"");
786
787         if (dtend && *(dtend)) {
788                 time_t tmp = icaltime_as_timet((icaltime_from_string(dtend)));
789                 gchar buft[512];
790                 tzset();
791                 event->end      = g_strdup(ctime_r(&tmp, buft));
792         }
793         
794         if (dtstart && *(dtstart)) {
795                 time_t tmp = icaltime_as_timet((icaltime_from_string(dtstart)));
796                 gchar buft[512];
797                 tzset();
798                 event->start    = g_strdup(ctime_r(&tmp, buft));
799         }
800         event->dtstart          = g_strdup(dtstart?dtstart:"");
801         event->dtend            = g_strdup(dtend?dtend:"");
802         event->recur            = g_strdup(recur?recur:"");
803         event->location         = g_strdup(location?location:"");
804         event->summary          = g_strdup(summary?summary:"");
805         event->description      = g_strdup(description?description:"");
806         event->url              = g_strdup(url?url:"");
807         event->tzid             = g_strdup(tzid?tzid:"");
808         event->method           = method;
809         event->sequence         = sequence;
810         event->type             = type;
811         event->rec_occurence            = FALSE;
812         while (strchr(event->summary, '\n'))
813                 *(strchr(event->summary, '\n')) = ' ';
814
815         return event;
816 }
817                                          
818 void vcal_manager_free_event (VCalEvent *event)
819 {
820         GSList *cur;
821         if (!event)
822                 return;
823
824         g_free(event->uid);
825         g_free(event->organizer);
826         g_free(event->orgname);
827         g_free(event->start);
828         g_free(event->end);
829         g_free(event->location);
830         g_free(event->summary);
831         g_free(event->dtstart);
832         g_free(event->dtend);
833         g_free(event->recur);
834         g_free(event->tzid);
835         g_free(event->description);
836         g_free(event->url);
837         for (cur = event->answers; cur; cur = cur->next) {
838                 answer_free((Answer *)cur->data);
839         }
840         g_slist_free(event->answers);
841         g_free(event);
842 }
843
844 gchar *vcal_manager_get_event_path(void)
845 {
846         static gchar *event_path = NULL;
847         if (!event_path)
848                 event_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
849                                         "vcalendar", NULL);
850         
851         return event_path;
852 }
853
854 gchar *vcal_manager_get_event_file(const gchar *uid)
855 {
856         gchar *tmp = g_strdup(uid);
857         gchar *res = NULL;
858
859         subst_for_filename(tmp);
860         res = g_strconcat(vcal_manager_get_event_path(), G_DIR_SEPARATOR_S,
861                            tmp, NULL);
862         g_free(tmp);
863         return res;
864 }
865
866 PrefsAccount *vcal_manager_get_account_from_event(VCalEvent *event)
867 {
868         GSList *list = vcal_manager_get_answers_emails(event);
869         GSList *cur = list;
870         
871         /* find an attendee corresponding to one of our accounts */
872         while (cur && cur->data) {
873                 gchar *email = (gchar *)cur->data;
874                 if (account_find_from_address(email, FALSE)) {
875                         g_slist_free(list);
876                         return account_find_from_address(email, FALSE);
877                 }
878                 cur = cur->next;
879         }
880         g_slist_free(list);
881         return NULL;
882 }
883
884 void vcal_manager_save_event (VCalEvent *event, gboolean export_after)
885 {
886         XMLTag *tag = NULL;
887         XMLNode *xmlnode = NULL;
888         GNode *rootnode = NULL;
889         PrefFile *pfile;
890         gchar *path = NULL;
891         GSList *answers = event->answers;
892         gchar *tmp = NULL;
893         gint tmp_method = event->method;
894         
895         tag = xml_tag_new("event");
896         xml_tag_add_attr(tag, xml_attr_new("organizer", event->organizer));
897         xml_tag_add_attr(tag, xml_attr_new("orgname", event->orgname));
898         xml_tag_add_attr(tag, xml_attr_new("location", event->location));
899         xml_tag_add_attr(tag, xml_attr_new("summary", event->summary));
900         xml_tag_add_attr(tag, xml_attr_new("description", event->description));
901         xml_tag_add_attr(tag, xml_attr_new("url", event->url));
902         xml_tag_add_attr(tag, xml_attr_new("dtstart", event->dtstart));
903         xml_tag_add_attr(tag, xml_attr_new("dtend", event->dtend));
904         xml_tag_add_attr(tag, xml_attr_new("recur", event->recur));
905         xml_tag_add_attr(tag, xml_attr_new("tzid", event->tzid));
906         
907         /* updating answers saves events, don't save them with reply type */
908         if (tmp_method == ICAL_METHOD_REPLY)
909                 tmp_method = ICAL_METHOD_REQUEST;
910         
911         tmp = g_strdup_printf("%d", tmp_method);
912         xml_tag_add_attr(tag, xml_attr_new("method", tmp));
913         g_free(tmp);
914
915         tmp = g_strdup_printf("%d", event->sequence);
916         xml_tag_add_attr(tag, xml_attr_new("sequence", tmp));
917         g_free(tmp);
918         
919         tmp = g_strdup_printf("%d", event->type);
920         xml_tag_add_attr(tag, xml_attr_new("type", tmp));
921         g_free(tmp);
922         
923         tmp = g_strdup_printf("%lu", event->postponed);
924         xml_tag_add_attr(tag, xml_attr_new("postponed", tmp));
925         g_free(tmp);
926         
927         tmp = g_strdup_printf("%d", event->rec_occurence);
928         xml_tag_add_attr(tag, xml_attr_new("rec_occurence", tmp));
929         g_free(tmp);
930         
931         xmlnode = xml_node_new(tag, NULL);
932         rootnode = g_node_new(xmlnode);
933         
934         while (answers && answers->data) {
935                 XMLNode *ansxmlnode = NULL;
936                 GNode *ansnode = NULL;
937                 XMLTag *anstag = xml_tag_new("answer");
938                 Answer *a = (Answer *)answers->data;
939                 xml_tag_add_attr(anstag, xml_attr_new("attendee", a->attendee));
940                 xml_tag_add_attr(anstag, xml_attr_new("name", a->name?a->name:""));
941                 tmp = g_strdup_printf("%d", a->answer);
942                 xml_tag_add_attr(anstag, xml_attr_new("answer", tmp));
943                 g_free(tmp);
944                 tmp = g_strdup_printf("%d", a->cutype);
945                 xml_tag_add_attr(anstag, xml_attr_new("cutype", tmp));
946                 g_free(tmp);
947                 ansxmlnode = xml_node_new(anstag, NULL);
948                 ansnode = g_node_new(ansxmlnode);
949                 g_node_append(rootnode, ansnode);
950                 answers = answers->next;
951         }
952         
953         path = vcal_manager_get_event_file(event->uid);
954                                         
955         if ((pfile = prefs_write_open(path)) == NULL) {
956                 gchar *dir_path = vcal_manager_get_event_path();
957                 if (!is_dir_exist(dir_path) && make_dir(vcal_manager_get_event_path()) != 0) {
958                         g_free(dir_path);
959                         g_free(path);
960                         return;
961                 }
962                 g_free(dir_path);
963                 if ((pfile = prefs_write_open(path)) == NULL) {
964                         g_free(path);
965                         return;
966                 }
967         }
968         
969         g_free(path);
970         xml_file_put_xml_decl(pfile->fp);
971         xml_write_tree(rootnode, pfile->fp);
972         xml_free_tree(rootnode);
973
974         if (prefs_file_close(pfile) < 0) {
975                 g_warning("failed to write event.\n");
976                 return;
977         }
978  
979         if (export_after)
980                 vcal_folder_export(NULL);
981 }
982
983 static VCalEvent *event_get_from_xml (const gchar *uid, GNode *node)
984 {
985         XMLNode *xmlnode;
986         GList *list;
987         gchar *org = NULL, *location = NULL, *summary = NULL, *orgname = NULL;
988         gchar *dtstart = NULL, *dtend = NULL, *tzid = NULL;
989         gchar *description = NULL, *url = NULL, *recur = NULL;
990         VCalEvent *event = NULL;
991         enum icalproperty_method method = ICAL_METHOD_REQUEST;
992         enum icalcomponent_kind type = ICAL_VEVENT_COMPONENT;
993         gint sequence = 0, rec_occurence = 0;
994         time_t postponed = (time_t)0;
995         
996         g_return_val_if_fail(node->data != NULL, NULL);
997
998         xmlnode = node->data;
999         if (strcmp2(xmlnode->tag->tag, "event") != 0) {
1000                 g_warning("tag name != \"event\"\n");
1001                 return NULL;
1002         }
1003         
1004         list = xmlnode->tag->attr;
1005         for (; list != NULL; list = list->next) {
1006                 XMLAttr *attr = list->data;
1007
1008                 if (!attr || !attr->name || !attr->value) continue;
1009                 if (!strcmp(attr->name, "organizer"))
1010                         org = g_strdup(attr->value);
1011                 if (!strcmp(attr->name, "orgname"))
1012                         orgname = g_strdup(attr->value);
1013                 if (!strcmp(attr->name, "location"))
1014                         location = g_strdup(attr->value);
1015                 if (!strcmp(attr->name, "summary"))
1016                         summary = g_strdup(attr->value);
1017                 if (!strcmp(attr->name, "description"))
1018                         description = g_strdup(attr->value);
1019                 if (!strcmp(attr->name, "url"))
1020                         url = g_strdup(attr->value);
1021                 if (!strcmp(attr->name, "dtstart"))
1022                         dtstart = g_strdup(attr->value);
1023                 if (!strcmp(attr->name, "dtend"))
1024                         dtend = g_strdup(attr->value);
1025                 if (!strcmp(attr->name, "recur"))
1026                         recur = g_strdup(attr->value);
1027                 if (!strcmp(attr->name, "tzid"))
1028                         tzid = g_strdup(attr->value);
1029                 if (!strcmp(attr->name, "type"))
1030                         type = atoi(attr->value);
1031                 if (!strcmp(attr->name, "method"))
1032                         method = atoi(attr->value);
1033                 if (!strcmp(attr->name, "sequence"))
1034                         sequence = atoi(attr->value);
1035                 if (!strcmp(attr->name, "postponed"))
1036                         postponed = atoi(attr->value);
1037                 if (!strcmp(attr->name, "rec_occurence"))
1038                         rec_occurence = atoi(attr->value);
1039         }
1040
1041         event = vcal_manager_new_event(uid, org, orgname, location, summary, description, 
1042                                         dtstart, dtend, recur, tzid, url, method, 
1043                                         sequence, type);
1044
1045         event->postponed = postponed;
1046         event->rec_occurence = rec_occurence;
1047
1048         g_free(org); 
1049         g_free(orgname); 
1050         g_free(location);
1051         g_free(summary); 
1052         g_free(description); 
1053         g_free(url); 
1054         g_free(dtstart); 
1055         g_free(dtend);
1056         g_free(recur);
1057         g_free(tzid);
1058
1059         node = node->children;
1060         while (node != NULL) {
1061                 gchar *attendee = NULL;
1062                 gchar *name = NULL;
1063                 enum icalparameter_partstat answer = ICAL_PARTSTAT_NEEDSACTION;
1064                 enum icalparameter_cutype cutype   = ICAL_CUTYPE_INDIVIDUAL;
1065                 
1066                 xmlnode = node->data;
1067                 if (strcmp2(xmlnode->tag->tag, "answer") != 0) {
1068                         g_warning("tag name != \"answer\"\n");
1069                         return event;
1070                 }
1071                 list = xmlnode->tag->attr;
1072                 for (; list != NULL; list = list->next) {
1073                         XMLAttr *attr = list->data;
1074
1075                         if (!attr || !attr->name || !attr->value) continue;
1076                         if (!strcmp(attr->name, "attendee"))
1077                                 attendee = g_strdup(attr->value);
1078                         if (!strcmp(attr->name, "name"))
1079                                 name = g_strdup(attr->value);
1080                         if (!strcmp(attr->name, "answer"))
1081                                 answer = atoi(attr->value);
1082                         if (!strcmp(attr->name, "cutype"))
1083                                 cutype = atoi(attr->value);
1084                 }
1085
1086                 event->answers = g_slist_prepend(event->answers, answer_new(attendee, name, answer, cutype));
1087                 g_free(attendee);
1088                 g_free(name);
1089                 node = node->next;
1090         }
1091         event->answers = g_slist_reverse(event->answers);
1092         
1093         return event;
1094 }
1095
1096 VCalEvent *vcal_manager_load_event (const gchar *uid)
1097 {
1098         GNode *node;
1099         gchar *path = NULL;
1100         VCalEvent *event = NULL;
1101
1102         path = vcal_manager_get_event_file(uid);
1103         
1104         if (!is_file_exist(path)) {
1105                 g_free(path);
1106                 return NULL;
1107         }
1108         
1109         node = xml_parse_file(path);
1110         
1111         g_free(path);
1112         
1113         if (!node) {
1114                 g_warning("no node\n");
1115                 return NULL;
1116         }
1117         
1118         event = event_get_from_xml(uid, node);
1119         /* vcal_manager_event_print(event); */
1120         xml_free_tree(node);
1121
1122         if (!event)
1123                 return NULL;
1124
1125         while (strchr(event->summary, '\n'))
1126                 *(strchr(event->summary, '\n')) = ' ';
1127
1128         return event;
1129         
1130 }
1131
1132 void vcal_manager_update_answer (VCalEvent      *event, 
1133                                  const gchar    *attendee,
1134                                  const gchar    *name,
1135                                  enum icalparameter_partstat ans,
1136                                  enum icalparameter_cutype cutype)
1137 {
1138         Answer *answer = NULL;
1139         GSList *existing = NULL;
1140         Answer *existing_a = NULL;
1141         
1142         if (!ans)
1143                 return;
1144                 
1145         answer = answer_new(attendee, name, ans, cutype);
1146         existing = answer_find(event, answer);
1147                 
1148         if (existing) {
1149                 existing_a = (Answer *)existing->data;
1150         
1151                 if (!answer->name && existing_a->name)
1152                         answer->name = g_strdup(existing_a->name);
1153                 if (!answer->cutype && existing_a->cutype)
1154                         answer->cutype = existing_a->cutype;
1155
1156                 answer_remove(event, answer);
1157         }
1158
1159         answer_add(event, answer);
1160         
1161         vcal_manager_save_event(event, FALSE);
1162 }
1163
1164 static gchar *write_headers(PrefsAccount        *account, 
1165                             VCalEvent           *event,
1166                             gboolean             short_headers,
1167                             gboolean             is_reply, 
1168                             gboolean             is_pseudo_display)
1169 {
1170         gchar *subject = NULL;
1171         gchar date[128];
1172         gchar *save_folder = NULL;
1173         gchar *result = NULL;
1174         gchar *queue_headers = NULL;
1175         gchar *method_str = NULL;
1176         gchar *attendees = NULL;
1177         enum icalparameter_partstat status;
1178         gchar *prefix = NULL;
1179         gchar enc_subject[512], enc_from[512], *from = NULL;
1180         gchar msgid[128];       
1181         gchar *calmsgid = NULL;
1182
1183         cm_return_val_if_fail(account != NULL, NULL);
1184
1185         memset(date, 0, sizeof(date));
1186         
1187         if (is_pseudo_display) {
1188                 struct icaltimetype itt = (icaltime_from_string(event->dtstart));
1189                 time_t t = icaltime_as_timet(itt);
1190                 get_rfc822_date_from_time_t(date, sizeof(date), t);
1191         } else {
1192                 get_rfc822_date(date, sizeof(date));
1193         }
1194         
1195         if (account_get_special_folder(account, F_OUTBOX)) {
1196                 save_folder = folder_item_get_identifier(account_get_special_folder
1197                                   (account, F_OUTBOX));
1198         }
1199         
1200         if (!is_reply) {
1201                 GSList *cur = event->answers;
1202                 while (cur && cur->data) {
1203                         gchar *tmp = NULL;
1204                         Answer *a = (Answer *)cur->data;
1205                         
1206                         if (strcasecmp(a->attendee, event->organizer)) {
1207                                 if (attendees) {
1208                                         tmp = g_strdup_printf("%s>,\n <%s", attendees, a->attendee);
1209                                         g_free(attendees);
1210                                         attendees = tmp;
1211                                 } else {
1212                                         attendees = g_strdup_printf("%s", a->attendee);
1213                                 }
1214                         }
1215                         cur = cur->next;
1216                 }
1217         }
1218         
1219         if (!short_headers) {
1220                 queue_headers = g_strdup_printf("S:%s\n"
1221                                 "SSV:%s\n"
1222                                 "R:<%s>\n"
1223                                 "MAID:%d\n"
1224                                 "%s%s%s"
1225                                 "X-Claws-End-Special-Headers: 1\n",
1226                                 account->address,
1227                                 account->smtp_server,
1228                                 is_reply ? event->organizer:attendees,
1229                                 account->account_id,
1230                                 save_folder?"SCF:":"",
1231                                 save_folder?save_folder:"",
1232                                 save_folder?"\n":"");
1233         } else {
1234                 queue_headers = g_strdup("");
1235         }
1236         
1237         prefix = "";
1238         if (is_reply) {
1239                 method_str = "REPLY";
1240                 status = vcal_manager_get_reply_for_attendee(event, account->address);
1241                 if (status == ICAL_PARTSTAT_ACCEPTED)
1242                         prefix = _("Accepted: ");
1243                 else if (status == ICAL_PARTSTAT_DECLINED)
1244                         prefix = _("Declined: ");
1245                 else if (status == ICAL_PARTSTAT_TENTATIVE)
1246                         prefix = _("Tentatively Accepted: ");
1247                 else 
1248                         prefix = "Re: ";
1249         } else if (event->method == ICAL_METHOD_PUBLISH)
1250                 method_str = "PUBLISH";
1251         else
1252                 method_str = "REQUEST";         
1253         
1254         subject = g_strdup_printf("%s%s", prefix, event->summary);
1255
1256         conv_encode_header_full(enc_subject, sizeof(enc_subject), subject, strlen("Subject: "), 
1257                         FALSE, conv_get_outgoing_charset_str());
1258         from = is_reply?account->name:(event->orgname?event->orgname:"");
1259         conv_encode_header_full(enc_from, sizeof(enc_from), from, strlen("From: "), 
1260                         TRUE, conv_get_outgoing_charset_str());
1261
1262         if (is_pseudo_display && event->uid) {
1263                 calmsgid = g_strdup_printf("Message-ID: <%s>\n",event->uid);
1264         } else {
1265                 calmsgid = g_strdup("");
1266         }
1267
1268         if (account && account->set_domain && account->domain) {
1269                 g_snprintf(msgid, sizeof(msgid), "%s", account->domain); 
1270         } else if (!strncmp(get_domain_name(), "localhost", strlen("localhost"))) {
1271                 g_snprintf(msgid, sizeof(msgid), "%s", 
1272                         strchr(account->address, '@') ?
1273                                 strchr(account->address, '@')+1 :
1274                                 account->address);
1275         } else {
1276                 g_snprintf(msgid, sizeof(msgid), "%s", "");
1277         }
1278
1279         generate_msgid(msgid, sizeof(msgid), account->address);
1280
1281         result = g_strdup_printf("%s"
1282                                 "From: %s <%s>\n"
1283                                 "To: <%s>\n"
1284                                 "Subject: %s\n"
1285                                 "Date: %s\n"
1286                                 "MIME-Version: 1.0\n"
1287                                 "Content-Type: text/calendar; method=%s; charset=\"%s\"\n"
1288                                 "Content-Transfer-Encoding: 8bit\n"
1289                                 "%s"
1290                                 "%s: <%s>\n",
1291                                 queue_headers,
1292                                 enc_from,
1293                                 is_reply ? account->address:event->organizer,
1294                                 is_reply ? event->organizer:(attendees?attendees:event->organizer),
1295                                 enc_subject,
1296                                 date,
1297                                 method_str,
1298                                 CS_UTF_8,
1299                                 calmsgid,
1300                                 is_pseudo_display?
1301                                         "In-Reply-To":"Message-ID",
1302                                 is_pseudo_display?
1303                                         event_to_today_str(event, 0):msgid);
1304         g_free(calmsgid);
1305         g_free(subject);
1306         g_free(save_folder);
1307         g_free(queue_headers);
1308         g_free(attendees);
1309         return result;                  
1310                                                                                
1311
1312 }
1313
1314 static gchar *write_headers_ical(PrefsAccount   *account, 
1315                             icalcomponent       *ievent,
1316                             gchar               *orga)
1317 {
1318         gchar subject[512];
1319         gchar date[128];
1320         gchar *result = NULL;
1321         gchar *method_str = NULL;
1322         gchar *summary = NULL;
1323         gchar *organizer = NULL;
1324         gchar *orgname = NULL;
1325         icalproperty *prop = NULL;
1326         gchar *calmsgid = NULL;
1327
1328         time_t t = (time_t)0;
1329
1330         memset(subject, 0, sizeof(subject));
1331         memset(date, 0, sizeof(date));
1332         
1333         prop = icalcomponent_get_first_property(ievent, ICAL_SUMMARY_PROPERTY);
1334         if (prop) {
1335                 summary = g_strdup(icalproperty_get_summary(prop));
1336                 icalproperty_free(prop);
1337         } else {
1338                 summary = g_strdup("");
1339         }
1340         
1341         while (strchr(summary, '\n'))
1342                 *(strchr(summary, '\n')) = ' ';
1343
1344         prop = icalcomponent_get_first_property(ievent, ICAL_ORGANIZER_PROPERTY);
1345         if (prop) {
1346                 organizer = g_strdup(icalproperty_get_organizer(prop));
1347                 if (icalproperty_get_parameter_as_string(prop, "CN") != NULL)
1348                         orgname = g_strdup(icalproperty_get_parameter_as_string(prop, "CN"));
1349
1350                 icalproperty_free(prop);
1351         } else {
1352                 organizer = orga? g_strdup(orga):g_strdup("");
1353         }
1354
1355         prop = icalcomponent_get_first_property(ievent, ICAL_DTSTART_PROPERTY);
1356         if (prop) {
1357                 t = icaltime_as_timet(icalproperty_get_dtstart(prop));
1358                 get_rfc822_date_from_time_t(date, sizeof(date), t);
1359         } else {
1360                 get_rfc822_date(date, sizeof(date));
1361         }
1362
1363         conv_encode_header(subject, 511, summary, strlen("Subject: "), FALSE);
1364                         
1365         method_str = "PUBLISH";
1366
1367         prop = icalcomponent_get_first_property(ievent, ICAL_UID_PROPERTY);
1368         if (prop) {
1369                 calmsgid = g_strdup_printf("Message-ID: <%s>\n",icalproperty_get_uid(prop));
1370                 icalproperty_free(prop);
1371         } else {
1372                 calmsgid = g_strdup("");
1373         }
1374         
1375
1376         result = g_strdup_printf("From: %s <%s>\n"
1377                                 "To: <%s>\n"
1378                                 "Subject: %s%s\n"
1379                                 "Date: %s\n"
1380                                 "MIME-Version: 1.0\n"
1381                                 "Content-Type: text/calendar; method=%s; charset=\"%s\"; vcalsave=\"no\"\n"
1382                                 "Content-Transfer-Encoding: quoted-printable\n"
1383                                 "%s"
1384                                 "In-Reply-To: <%s>\n",
1385                                 orgname?orgname:"",
1386                                 !strncmp(organizer, "MAILTO:", 7) ? organizer+7 : organizer,
1387                                 account->address,
1388                                 "",
1389                                 subject,
1390                                 date,
1391                                 method_str,
1392                                 conv_get_outgoing_charset_str(),
1393                                 calmsgid,
1394                                 event_to_today_str(NULL, t));
1395         
1396         g_free(calmsgid);
1397         g_free(orgname);
1398         g_free(organizer);
1399         g_free(summary);
1400
1401         return result;                  
1402                                                                                
1403
1404 }
1405
1406 static gboolean vcal_manager_send (PrefsAccount         *account, 
1407                                      VCalEvent          *event,
1408                                      gboolean            is_reply)
1409 {
1410         gchar *tmpfile = NULL;
1411         gint msgnum;
1412         FolderItem *folderitem;
1413         gchar *msgpath = NULL;
1414         Folder *folder = NULL;
1415         
1416         tmpfile = vcal_manager_event_dump(event, is_reply, FALSE, NULL, TRUE);
1417
1418         if (!tmpfile)
1419                 return FALSE;
1420
1421         folderitem = account_get_special_folder(account, F_QUEUE);
1422         if (!folderitem) {
1423                 g_warning("can't find queue folder for %s\n", account->address);
1424                 g_unlink(tmpfile);
1425                 g_free(tmpfile);
1426                 return FALSE;
1427         }
1428         folder_item_scan(folderitem);
1429         
1430         if ((msgnum = folder_item_add_msg(folderitem, tmpfile, NULL, TRUE)) < 0) {
1431                 g_warning("can't queue the message\n");
1432                 g_unlink(tmpfile);
1433                 g_free(tmpfile);
1434                 return FALSE;
1435         }
1436
1437         msgpath = folder_item_fetch_msg(folderitem, msgnum);
1438         
1439         if (!prefs_common_get_prefs()->work_offline) {
1440                 gchar *err = NULL;
1441                 gboolean queued_removed = FALSE;
1442                 gint val = procmsg_send_message_queue_with_lock(msgpath, &err, folderitem, msgnum, &queued_removed);
1443                 if (val == 0) {
1444                         if (!queued_removed)
1445                                 folder_item_remove_msg(folderitem, msgnum);
1446                         folder_item_scan(folderitem);
1447                 } else if (err) {
1448                         alertpanel_error_log("%s", err);
1449                         g_free(err);
1450                 }
1451         }
1452         g_unlink(tmpfile);
1453         g_free(tmpfile);
1454         g_free(msgpath);
1455
1456         folder = folder_find_from_name ("vCalendar", vcal_folder_get_class());
1457         if (folder)
1458                 folder_item_scan(folder->inbox);
1459
1460         vcalviewer_reload(folder->inbox);
1461         return TRUE;
1462 }
1463
1464 gboolean vcal_manager_reply (PrefsAccount       *account, 
1465                              VCalEvent          *event)
1466 {
1467         return vcal_manager_send(account, event, TRUE);
1468 }
1469
1470 gboolean vcal_manager_request (PrefsAccount     *account, 
1471                                VCalEvent        *event)
1472 {
1473         return vcal_manager_send(account, event, FALSE);
1474 }
1475
1476 EventTime event_to_today(VCalEvent *event, time_t t)
1477 {
1478         struct tm evtstart, today;
1479         time_t evtstart_t, today_t;
1480         struct icaltimetype itt;
1481         struct tm buft;
1482
1483         tzset();
1484         
1485         today_t = time(NULL);
1486         if (event) {
1487                 itt = icaltime_from_string(event->dtstart);
1488                 evtstart_t = icaltime_as_timet(itt);
1489         } else {
1490                 evtstart_t = t;
1491         }
1492         
1493 #ifndef G_OS_WIN32
1494         today = *localtime_r(&today_t, &buft);
1495         localtime_r(&evtstart_t, &evtstart);
1496 #else
1497         if (today_t < 0)
1498                 today_t = 1;
1499         if (evtstart_t < 0)
1500                 evtstart_t = 1;
1501         today = *localtime(&today_t);
1502         evtstart = *localtime(&evtstart_t);
1503 #endif
1504         
1505         if (today.tm_year == evtstart.tm_year) {
1506                 int days = evtstart.tm_yday - today.tm_yday;
1507                 if (days < 0) {
1508                         return EVENT_PAST;
1509                 } else if (days == 0) {
1510                         return EVENT_TODAY;
1511                 } else if (days == 1) {
1512                         return EVENT_TOMORROW;
1513                 } else if (days > 1 && days < 7) {
1514                         return EVENT_THISWEEK;
1515                 } else {
1516                         return EVENT_LATER;
1517                 }
1518         } else if (today.tm_year > evtstart.tm_year) {
1519                 return EVENT_PAST;
1520         } else if (today.tm_year == evtstart.tm_year - 1) {
1521                 int days = ((365 - today.tm_yday) + evtstart.tm_yday);
1522                 if (days == 0) {
1523                         return EVENT_TODAY;
1524                 } else if (days == 1) {
1525                         return EVENT_TOMORROW;
1526                 } else if (days > 1 && days < 7) {
1527                         return EVENT_THISWEEK;
1528                 } else {
1529                         return EVENT_LATER;
1530                 }
1531         } else 
1532                 return EVENT_LATER;
1533 }
1534
1535 const gchar *event_to_today_str(VCalEvent *event, time_t t)
1536 {
1537         EventTime days = event_to_today(event, t);
1538         switch(days) {
1539         case EVENT_PAST:
1540                 return EVENT_PAST_ID;
1541         case EVENT_TODAY:
1542                 return EVENT_TODAY_ID;
1543         case EVENT_TOMORROW:
1544                 return EVENT_TOMORROW_ID;
1545         case EVENT_THISWEEK:
1546                 return EVENT_THISWEEK_ID;
1547         case EVENT_LATER:
1548                 return EVENT_LATER_ID;
1549         }
1550         return NULL;
1551 }