Handle timezones correctly in vCalendar.
[claws.git] / src / plugins / vcalendar / vcal_folder.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2015 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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <stddef.h>
26 #include <glib.h>
27 #include <glib/gi18n.h>
28
29 #include "defs.h"
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <curl/curl.h>
36 #include <curl/curlver.h>
37
38 #include "account.h"
39 #include "utils.h"
40 #include "procmsg.h"
41 #include "procheader.h"
42 #include "folder.h"
43 #include "folderview.h"
44 #include "folder_item_prefs.h"
45 #include "vcalendar.h"
46 #include "vcal_folder.h"
47 #include "vcal_prefs.h"
48 #include "vcal_manager.h"
49 #include "vcal_meeting_gtk.h"
50 #include "vcal_interface.h"
51 #include "prefs_account.h"
52 #include "prefs_common.h"
53 #include "claws.h"
54 #include "menu.h"
55 #include "inputdialog.h"
56 #include "inc.h"
57 #include "xml.h"
58 #include "alertpanel.h"
59 #include "log.h"
60 #include "mainwindow.h"
61 #include "statusbar.h"
62 #include "msgcache.h"
63 #include "passwordstore.h"
64 #include "timing.h"
65 #include "messageview.h"
66
67 #include <gtk/gtk.h>
68 #include <dirent.h>
69
70 #define VCAL_FOLDERITEM(item) ((VCalFolderItem *) item)
71
72 #ifdef USE_PTHREAD
73 #include <pthread.h>
74 #endif
75
76 typedef struct _thread_data {
77         const gchar *url;
78         gchar *result;
79         gchar *error;
80         gboolean done;
81 } thread_data;
82
83 typedef struct _IcalFeedData {
84         icalcomponent *event;
85         gchar *pseudoevent_id;
86 } IcalFeedData;
87
88 typedef struct _VCalFolder VCalFolder;
89 typedef struct _VCalFolderItem VCalFolderItem;
90
91 static Folder *vcal_folder_new(const gchar * name,
92                                   const gchar * folder);
93 static void vcal_folder_destroy(Folder * folder);
94 static void vcal_item_destroy(Folder *folder, FolderItem *_item);
95 static gchar *vcal_item_get_path(Folder *folder, FolderItem *item);
96
97 static gint vcal_scan_tree(Folder * folder);
98 static FolderItem *vcal_item_new(Folder * folder);
99 static gint vcal_get_num_list(Folder * folder, FolderItem * item,
100                                  MsgNumberList ** list,
101                                  gboolean * old_uids_valid);
102 static MsgInfo *vcal_get_msginfo(Folder * folder, FolderItem * item,
103                                     gint num);
104 static gchar *vcal_fetch_msg(Folder * folder, FolderItem * item,
105                                 gint num);
106 static gint vcal_add_msg(Folder * folder, FolderItem * _dest,
107                             const gchar * file, MsgFlags * flags);
108 static gint vcal_remove_msg(Folder * folder, FolderItem * _item,
109                                gint num);
110 static FolderItem *vcal_create_folder(Folder * folder,
111                                          FolderItem * parent,
112                                          const gchar * name);
113 static gint vcal_create_tree(Folder *folder);
114 static gint vcal_remove_folder(Folder *folder, FolderItem *item);
115 static gboolean vcal_scan_required(Folder *folder, FolderItem *item);
116 static void vcal_set_mtime(Folder *folder, FolderItem *item);
117 static void vcal_change_flags(Folder *folder, FolderItem *_item, MsgInfo *msginfo, MsgPermFlags newflags);
118
119 static void new_meeting_cb(GtkAction *action, gpointer data);
120 static void export_cal_cb(GtkAction *action, gpointer data);
121 static void subscribe_cal_cb(GtkAction *action, gpointer data);
122 static void check_subs_cb(GtkAction *action, gpointer data);
123 static void unsubscribe_cal_cb(GtkAction *action, gpointer data);
124 static void rename_cb(GtkAction *action, gpointer data);
125 static void set_view_cb(GtkAction *action, GtkRadioAction *current, gpointer data);
126
127 static void add_menuitems(GtkUIManager *ui_manager, FolderItem *item);
128 static void set_sensitivity(GtkUIManager *ui_manager, FolderItem *item);
129
130 static void update_subscription(const gchar *uri, gboolean verbose);
131 static void vcal_folder_set_batch       (Folder         *folder,
132                                          FolderItem     *item,
133                                          gboolean        batch);
134 static void convert_to_utc(icalcomponent *calendar);
135
136 gboolean vcal_subscribe_uri(Folder *folder, const gchar *uri);
137
138 FolderClass vcal_class;
139
140 static GSList *created_files = NULL;
141 static GHashTable *hash_uids = NULL;
142
143 struct _VCalFolder
144 {
145         Folder folder;
146 };
147
148 struct _VCalFolderItem
149 {
150         FolderItem item;
151         gchar *uri;
152         gchar *feed;
153         icalcomponent *cal;
154         GSList *numlist;
155         GSList *evtlist;
156         gboolean batching;
157         gboolean dirty;
158         day_win *dw;
159         month_win *mw;
160         time_t last_fetch;
161         int use_cal_view;
162 };
163
164 static char *vcal_popup_labels[] =
165 {
166         N_("_New meeting..."),
167         N_("_Export calendar..."),
168         N_("_Subscribe to Webcal..."),
169         N_("_Unsubscribe..."),
170         N_("_Rename..."),
171         N_("U_pdate subscriptions"),
172         N_("_List view"),
173         N_("_Week view"),
174         N_("_Month view"),
175         NULL
176 };
177
178 static GtkActionEntry vcal_popup_entries[] = 
179 {
180         {"FolderViewPopup/NewMeeting",          NULL, NULL, NULL, NULL, G_CALLBACK(new_meeting_cb) },
181         {"FolderViewPopup/ExportCal",           NULL, NULL, NULL, NULL, G_CALLBACK(export_cal_cb) },
182
183         {"FolderViewPopup/SubscribeCal",        NULL, NULL, NULL, NULL, G_CALLBACK(subscribe_cal_cb) },
184         {"FolderViewPopup/UnsubscribeCal",      NULL, NULL, NULL, NULL, G_CALLBACK(unsubscribe_cal_cb) },
185
186         {"FolderViewPopup/RenameFolder",        NULL, NULL, NULL, NULL, G_CALLBACK(rename_cb) },
187
188         {"FolderViewPopup/CheckSubs",           NULL, NULL, NULL, NULL, G_CALLBACK(check_subs_cb) },
189
190 };                      
191
192 static GtkRadioActionEntry vcal_popup_radio_entries[] = { /* set_view_cb */
193         {"FolderViewPopup/ListView",            NULL, NULL, NULL, NULL, 0 }, 
194         {"FolderViewPopup/WeekView",            NULL, NULL, NULL, NULL, 1 }, 
195         {"FolderViewPopup/MonthView",           NULL, NULL, NULL, NULL, 2 }, 
196 };
197                 
198 static IcalFeedData *icalfeeddata_new(icalcomponent *evt, gchar *str)
199 {
200         IcalFeedData *data = g_new0(IcalFeedData, 1);
201         if (str)
202                 data->pseudoevent_id = g_strdup(str);
203         data->event = evt;
204         return data;
205 }
206
207 static void icalfeeddata_free(IcalFeedData *data)
208 {
209         g_free(data->pseudoevent_id);
210         if (data->event)
211                 icalcomponent_free(data->event);
212         g_free(data);
213 }
214
215 static void slist_free_icalfeeddata(GSList *list)
216 {
217         while (list) {
218                 IcalFeedData *data = (IcalFeedData *)list->data;
219                 icalfeeddata_free(data);
220                 list = list->next;
221         }
222 }
223
224 static void vcal_fill_popup_menu_labels(void)
225 {
226         vcal_popup_entries[0].label = _(vcal_popup_labels[0]);
227         vcal_popup_entries[1].label = _(vcal_popup_labels[1]);
228         vcal_popup_entries[2].label = _(vcal_popup_labels[2]);
229         vcal_popup_entries[3].label = _(vcal_popup_labels[3]);
230         vcal_popup_entries[4].label = _(vcal_popup_labels[4]);
231         vcal_popup_entries[5].label = _(vcal_popup_labels[5]);
232         vcal_popup_radio_entries[0].label = _(vcal_popup_labels[6]);
233         vcal_popup_radio_entries[1].label = _(vcal_popup_labels[7]);
234         vcal_popup_radio_entries[2].label = _(vcal_popup_labels[8]);
235 }
236
237 static FolderViewPopup vcal_popup =
238 {
239         PLUGIN_NAME,
240         "<vCalendar>",
241         vcal_popup_entries,
242         G_N_ELEMENTS(vcal_popup_entries),
243         NULL, 0,
244         vcal_popup_radio_entries, 
245         G_N_ELEMENTS(vcal_popup_radio_entries), 1, set_view_cb,
246         add_menuitems,
247         set_sensitivity
248 };
249
250 static void vcal_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
251 {
252         GList *cur;
253         folder_item_set_xml(folder, item, tag);
254         gboolean found_cal_view_setting = FALSE;
255
256         for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
257                 XMLAttr *attr = (XMLAttr *) cur->data;
258
259                 if (!attr || !attr->name || !attr->value) continue;
260                 if (!strcmp(attr->name, "uri")) {
261                         if (((VCalFolderItem *)item)->uri != NULL)
262                                 g_free(((VCalFolderItem *)item)->uri);
263                         ((VCalFolderItem *)item)->uri = g_strdup(attr->value);
264                 } 
265                 if (!strcmp(attr->name, "use_cal_view")) {
266                         found_cal_view_setting = TRUE;
267                         ((VCalFolderItem *)item)->use_cal_view = atoi(attr->value);
268                 } 
269         }
270         if (((VCalFolderItem *)item)->uri == NULL) {
271                 /* give a path to inbox */
272                 g_free(item->path);
273                 item->path = g_strdup(".meetings");
274         }
275         if (!found_cal_view_setting)
276                 ((VCalFolderItem *)item)->use_cal_view = 1; /*week view */
277         
278 }
279
280 static XMLTag *vcal_item_get_xml(Folder *folder, FolderItem *item)
281 {
282         XMLTag *tag;
283
284         tag = folder_item_get_xml(folder, item);
285
286         if (((VCalFolderItem *)item)->uri)
287                 xml_tag_add_attr(tag, xml_attr_new("uri", ((VCalFolderItem *)item)->uri));
288
289         xml_tag_add_attr(tag, xml_attr_new_int("use_cal_view", ((VCalFolderItem *)item)->use_cal_view));
290
291         return tag;
292 }
293
294 static gint vcal_rename_folder(Folder *folder, FolderItem *item,
295                              const gchar *name)
296 {
297         if (!name)
298                 return -1;
299         g_free(item->name);
300         item->name = g_strdup(name);
301         return 0;
302 }
303
304 static void vcal_get_sort_type(Folder *folder, FolderSortKey *sort_key,
305                                FolderSortType *sort_type)
306 {
307         if (sort_key)
308                 *sort_key = SORT_BY_DATE;
309 }
310
311 static void vcal_item_opened(FolderItem *item)
312 {
313         struct tm tmdate;
314         time_t t = time(NULL);
315 #ifndef G_OS_WIN32
316         localtime_r(&t, &tmdate);
317 #else
318         if (t < 0)
319                 t = 1;
320         tmdate = *localtime(&t);
321 #endif
322         if (!((VCalFolderItem *)(item))->dw 
323             && ((VCalFolderItem *)(item))->use_cal_view == 1)
324                 ((VCalFolderItem *)(item))->dw = create_day_win(item, tmdate);          
325         else if (!((VCalFolderItem *)(item))->mw 
326             && ((VCalFolderItem *)(item))->use_cal_view == 2)
327                 ((VCalFolderItem *)(item))->mw = create_month_win(item, tmdate);
328         else if (((VCalFolderItem *)(item))->use_cal_view != 0)
329                 vcal_folder_refresh_cal(item);
330 }
331
332 void vcal_folder_refresh_cal(FolderItem *item)
333 {
334         Folder *folder = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
335         if (item->folder != folder)
336                 return;
337         if (((VCalFolderItem *)(item))->dw)
338                 refresh_day_win(((VCalFolderItem *)(item))->dw);
339         if (((VCalFolderItem *)(item))->mw)
340                 refresh_month_win(((VCalFolderItem *)(item))->mw);
341 }
342
343 static void vcal_item_closed(FolderItem *item)
344 {
345         if (((VCalFolderItem *)(item))->dw) {
346                 dw_close_window(((VCalFolderItem *)(item))->dw);
347                 ((VCalFolderItem *)(item))->dw = NULL;
348         }
349         if (((VCalFolderItem *)(item))->mw) {
350                 mw_close_window(((VCalFolderItem *)(item))->mw);
351                 ((VCalFolderItem *)(item))->mw = NULL;
352         }
353 }
354
355 FolderClass *vcal_folder_get_class()
356 {
357         if (vcal_class.idstr == NULL) {
358                 debug_print("register class\n");
359                 vcal_class.type = F_UNKNOWN;
360                 vcal_class.idstr = PLUGIN_NAME;
361                 vcal_class.uistr = PLUGIN_NAME;
362
363                 /* Folder functions */
364                 vcal_class.new_folder = vcal_folder_new;
365                 vcal_class.destroy_folder = vcal_folder_destroy;
366                 vcal_class.set_xml = folder_set_xml;
367                 vcal_class.get_xml = folder_get_xml;
368                 vcal_class.item_set_xml = vcal_item_set_xml;
369                 vcal_class.item_get_xml = vcal_item_get_xml;
370                 vcal_class.scan_tree = vcal_scan_tree;
371                 vcal_class.create_tree = vcal_create_tree;
372                 vcal_class.get_sort_type = vcal_get_sort_type;
373
374                 /* FolderItem functions */
375                 vcal_class.item_new = vcal_item_new;
376                 vcal_class.item_destroy = vcal_item_destroy;
377                 vcal_class.item_get_path = vcal_item_get_path;
378                 vcal_class.create_folder = vcal_create_folder;
379                 vcal_class.remove_folder = vcal_remove_folder;
380                 vcal_class.rename_folder = vcal_rename_folder;
381                 vcal_class.scan_required = vcal_scan_required;
382                 vcal_class.set_mtime = vcal_set_mtime;
383                 vcal_class.get_num_list = vcal_get_num_list;
384                 vcal_class.set_batch = vcal_folder_set_batch;
385
386                 /* Message functions */
387                 vcal_class.get_msginfo = vcal_get_msginfo;
388                 vcal_class.fetch_msg = vcal_fetch_msg;
389                 vcal_class.add_msg = vcal_add_msg;
390                 vcal_class.copy_msg = NULL;
391                 vcal_class.remove_msg = vcal_remove_msg;
392                 vcal_class.change_flags = vcal_change_flags;
393                 vcal_class.subscribe = vcal_subscribe_uri;
394
395                 /* FolderView functions */
396                 vcal_class.item_opened = vcal_item_opened;
397                 vcal_class.item_closed = vcal_item_closed;
398                 debug_print("registered class for real\n");
399         }
400
401         return &vcal_class;
402 }
403
404 static void vcal_folder_set_batch       (Folder         *folder,
405                                          FolderItem     *_item,
406                                          gboolean        batch)
407 {
408         VCalFolderItem *item = (VCalFolderItem *)_item;
409
410         g_return_if_fail(item != NULL);
411         
412         if (item->batching == batch)
413                 return;
414         
415         if (batch) {
416                 item->batching = TRUE;
417                 debug_print("vcal switching to batch mode\n");
418         } else {
419                 debug_print("vcal switching away from batch mode\n");
420                 /* ici */
421                 item->batching = FALSE;
422                 if (item->dirty)
423                         vcal_folder_export(folder);
424                 item->dirty = FALSE;
425         }
426 }
427
428 static Folder *vcal_folder_new(const gchar * name,
429                                   const gchar * path)
430 {
431         VCalFolder *folder;                
432         
433         debug_print("vcal_folder_new\n");
434         folder = g_new0(VCalFolder, 1);
435         FOLDER(folder)->klass = &vcal_class;
436         folder_init(FOLDER(folder), name);
437
438         return FOLDER(folder);
439 }
440
441 static void vcal_folder_destroy(Folder *_folder)
442 {
443 }
444
445 static FolderItem *vcal_item_new(Folder *folder)
446 {
447         VCalFolderItem *item;
448         item = g_new0(VCalFolderItem, 1);
449         item->use_cal_view = 1;
450         return (FolderItem *) item;
451
452 }
453
454 static void vcal_item_destroy(Folder *folder, FolderItem *_item)
455 {
456         VCalFolderItem *item = (VCalFolderItem *)_item;
457         g_return_if_fail(item != NULL);
458         g_free(item);
459 }
460
461 static gchar *vcal_item_get_path(Folder *folder, FolderItem *item)
462 {
463         VCalFolderItem *fitem = (VCalFolderItem *)item;
464         if (fitem->uri == NULL)
465                 return g_strdup(vcal_manager_get_event_path());
466         else {
467                 gchar *path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
468                                         "vcalendar", G_DIR_SEPARATOR_S,
469                                         item->path, NULL);
470                 return path;
471         }
472 }
473
474 static gint vcal_scan_tree(Folder *folder)
475 {
476         g_return_val_if_fail(folder != NULL, -1);
477
478         folder->outbox = NULL;
479         folder->draft = NULL;
480         folder->queue = NULL;
481         folder->trash = NULL;
482
483         debug_print("scanning tree\n");
484         vcal_create_tree(folder);
485
486         return 0;
487 }
488
489 gboolean manual_update = TRUE;
490
491 static gint feed_fetch(FolderItem *fitem, MsgNumberList ** list, gboolean *old_uids_valid)
492 {
493         VCalFolderItem *item = (VCalFolderItem *)fitem;
494         icalcomponent *evt = NULL;
495         icalcomponent_kind type = ICAL_VEVENT_COMPONENT;
496         gint num = 1;
497         gint past_msg = -1, today_msg = -1, tomorrow_msg = -1, 
498                 thisweek_msg = -1, later_msg = -1;
499
500         debug_print("fetching\n");
501
502         if (!item->uri) {
503                 debug_print("no uri!\n");
504                 return -1;
505         }
506
507         update_subscription(item->uri, TRUE);
508
509         *old_uids_valid = FALSE;
510         *list = NULL;
511
512         if (item->cal) {
513                 evt = icalcomponent_get_first_component(
514                         item->cal, ICAL_VEVENT_COMPONENT);
515                 if (evt == NULL) {
516                         evt = icalcomponent_get_first_component(
517                                 item->cal, ICAL_VTODO_COMPONENT);
518                         if (evt != NULL)
519                                 type = ICAL_VTODO_COMPONENT;
520                 }
521         } else
522                 debug_print("no cal!\n");
523
524         if (evt == NULL)
525                 debug_print("no event\n");
526
527         if (item->numlist) {
528                 g_slist_free(item->numlist);
529                 item->numlist = NULL;
530         }
531
532         if (item->evtlist) {
533                 slist_free_icalfeeddata(item->evtlist);
534                 g_slist_free(item->evtlist);
535                 item->evtlist = NULL;
536         }
537
538         while (evt) {
539                 icalproperty *prop;
540                 icalproperty *rprop = icalcomponent_get_first_property(evt, ICAL_RRULE_PROPERTY);
541                 struct icalrecurrencetype recur;
542                 struct icaltimetype next;
543                 icalrecur_iterator* ritr = NULL;
544                 EventTime days;
545
546                 if (rprop) {
547                         icalproperty *rprop2;
548                         recur = icalproperty_get_rrule(rprop);
549                         rprop2 = icalproperty_new_rrule(recur);
550                         prop = icalcomponent_get_first_property(evt, ICAL_DTSTART_PROPERTY);
551                         if (prop) {
552                                 ritr = icalrecur_iterator_new(recur, icalproperty_get_dtstart(prop));
553                                 next = icalrecur_iterator_next(ritr); /* skip first one */
554                         }
555                         
556                         rprop = rprop2;
557                 } 
558
559                 prop = icalcomponent_get_first_property(evt, ICAL_UID_PROPERTY);
560                 if (prop) {
561                         gchar *orig_uid = NULL;
562                         gchar *uid = g_strdup(icalproperty_get_uid(prop));
563                         IcalFeedData *data = icalfeeddata_new(
564                                                 icalcomponent_new_clone(evt), NULL);
565                         int i = 0;
566                         orig_uid = g_strdup(uid);
567
568 add_new:                        
569                         item->numlist = g_slist_prepend(item->numlist, GINT_TO_POINTER(num));
570                         item->evtlist = g_slist_prepend(item->evtlist, data);
571                         data = NULL;
572                         debug_print("add %d : %s\n", num, uid);
573                         g_free(uid);
574                         num++;
575                         prop = icalcomponent_get_first_property(evt, ICAL_DTSTART_PROPERTY);
576                         if (prop) {
577                                 struct icaltimetype itt = icalproperty_get_dtstart(prop);
578                                 days = event_to_today(NULL, icaltime_as_timet(itt));
579                                 if (days == EVENT_PAST && past_msg == -1) {
580                                         item->numlist = g_slist_prepend(item->numlist, GINT_TO_POINTER(num));
581                                         data = icalfeeddata_new(NULL, EVENT_PAST_ID);
582                                         past_msg = num++;
583                                 } else if (days == EVENT_TODAY && today_msg == -1) {
584                                         item->numlist = g_slist_prepend(item->numlist, GINT_TO_POINTER(num));
585                                         data = icalfeeddata_new(NULL, EVENT_TODAY_ID);
586                                         today_msg = num++;
587                                 } else if (days == EVENT_TOMORROW && tomorrow_msg == -1) {
588                                         item->numlist = g_slist_prepend(item->numlist, GINT_TO_POINTER(num));
589                                         data = icalfeeddata_new(NULL, EVENT_TOMORROW_ID);
590                                         tomorrow_msg = num++;
591                                 } else if (days == EVENT_THISWEEK && thisweek_msg == -1) {
592                                         item->numlist = g_slist_prepend(item->numlist, GINT_TO_POINTER(num));
593                                         data = icalfeeddata_new(NULL, EVENT_THISWEEK_ID);
594                                         thisweek_msg = num++;
595                                 } else if (days == EVENT_LATER && later_msg == -1) {
596                                         item->numlist = g_slist_prepend(item->numlist, GINT_TO_POINTER(num));
597                                         data = icalfeeddata_new(NULL, EVENT_LATER_ID);
598                                         later_msg = num++;
599                                 }
600                         } else {
601                                 if (past_msg == -1) {
602                                         item->numlist = g_slist_prepend(item->numlist, GINT_TO_POINTER(num));
603                                         data = icalfeeddata_new(NULL, EVENT_PAST_ID);
604                                         past_msg = num++;
605                                 }
606                         }
607                         if (data) {
608                                 item->evtlist = g_slist_prepend(item->evtlist, data);
609                                 data = NULL;
610                         }
611                         if (rprop && ritr) {
612                                 struct icaldurationtype ical_dur;
613                                 struct icaltimetype dtstart, dtend;
614                                 evt = icalcomponent_new_clone(evt);
615                                 prop = icalcomponent_get_first_property(evt, ICAL_RRULE_PROPERTY);
616                                 if (prop) {
617                                         icalcomponent_remove_property(evt, prop);
618                                         icalproperty_free(prop);
619                                 }
620                                 prop = icalcomponent_get_first_property(evt, ICAL_DTSTART_PROPERTY);
621                                 if (prop)
622                                         dtstart = icalproperty_get_dtstart(prop);
623                                 prop = icalcomponent_get_first_property(evt, ICAL_DTEND_PROPERTY);
624                                 if (prop)
625                                         dtend = icalproperty_get_dtend(prop);
626                                 ical_dur = icaltime_subtract(dtend, dtstart);
627                                 next = icalrecur_iterator_next(ritr);
628                                 if (!icaltime_is_null_time(next) &&
629                                     !icaltime_is_null_time(dtstart) && i < 100) {
630                                         prop = icalcomponent_get_first_property(evt, ICAL_DTSTART_PROPERTY);
631                                         icalproperty_set_dtstart(prop, next);
632
633                                         prop = icalcomponent_get_first_property(evt, ICAL_DTEND_PROPERTY);
634                                         if (prop)
635                                                 icalproperty_set_dtend(prop, icaltime_add(next, ical_dur));
636
637                                         prop = icalcomponent_get_first_property(evt, ICAL_UID_PROPERTY);
638                                         uid = g_strdup_printf("%s-%d", orig_uid, i);
639                                         icalproperty_set_uid(prop, uid);
640                                         /* dont free uid, used after (add_new) */
641                                         data = icalfeeddata_new(evt, NULL);
642                                         i++;
643                                         goto add_new;
644                                 } else {
645                                         icalcomponent_free(evt);
646                                         evt = NULL;
647                                 }
648                         }
649                         g_free(orig_uid);
650                 } else {
651                         debug_print("no uid!\n");
652                 }
653                 if (rprop) {
654                         icalproperty_free(rprop);
655                 }
656                 if (ritr) {
657                         icalrecur_iterator_free(ritr);
658                         ritr = NULL;
659                 }
660                 evt = icalcomponent_get_next_component(
661                         item->cal, type);
662         }
663         if (today_msg == -1) {
664                 IcalFeedData *data = icalfeeddata_new(NULL, EVENT_TODAY_ID);
665                 item->numlist = g_slist_prepend(item->numlist, GINT_TO_POINTER(num));
666                 today_msg = num++;
667                 item->evtlist = g_slist_prepend(item->evtlist, data);
668         }
669         item->numlist = g_slist_reverse(item->numlist);
670         item->evtlist = g_slist_reverse(item->evtlist);
671         
672         *list = item->numlist ? g_slist_copy(item->numlist) : NULL;
673         debug_print("return %d\n", num);
674         return num;
675 }
676
677 #define VCAL_FOLDER_ADD_EVENT(event) \
678 { \
679  \
680         *list = g_slist_prepend(*list, GINT_TO_POINTER(n_msg)); \
681         debug_print("add %d %s\n", n_msg, event->uid); \
682         n_msg++; \
683         days = event_to_today(event, 0); \
684  \
685         if (days == EVENT_PAST && past_msg == -1) { \
686                 *list = g_slist_prepend(*list, GINT_TO_POINTER(n_msg)); \
687                 past_msg = n_msg++; \
688                 g_hash_table_insert(hash_uids, GINT_TO_POINTER(past_msg), g_strdup(EVENT_PAST_ID)); \
689         } else if (days == EVENT_TODAY && today_msg == -1) { \
690                 *list = g_slist_prepend(*list, GINT_TO_POINTER(n_msg)); \
691                 today_msg = n_msg++; \
692                 g_hash_table_insert(hash_uids, GINT_TO_POINTER(today_msg), g_strdup(EVENT_TODAY_ID)); \
693         } else if (days == EVENT_TOMORROW && tomorrow_msg == -1) { \
694                 *list = g_slist_prepend(*list, GINT_TO_POINTER(n_msg)); \
695                 tomorrow_msg = n_msg++; \
696                 g_hash_table_insert(hash_uids, GINT_TO_POINTER(tomorrow_msg), g_strdup(EVENT_TOMORROW_ID)); \
697         } else if (days == EVENT_THISWEEK && thisweek_msg == -1) { \
698                 *list = g_slist_prepend(*list, GINT_TO_POINTER(n_msg)); \
699                 thisweek_msg = n_msg++; \
700                 g_hash_table_insert(hash_uids, GINT_TO_POINTER(thisweek_msg), g_strdup(EVENT_THISWEEK_ID)); \
701         } else if (days == EVENT_LATER && later_msg == -1) { \
702                 *list = g_slist_prepend(*list, GINT_TO_POINTER(n_msg)); \
703                 later_msg = n_msg++; \
704                 g_hash_table_insert(hash_uids, GINT_TO_POINTER(later_msg), g_strdup(EVENT_LATER_ID)); \
705         } \
706 }
707
708 GSList *vcal_get_events_list(FolderItem *item)
709 {
710         GDir *dp;
711         const gchar *d;
712         GSList *events = NULL;
713         GError *error = NULL;
714
715         if (item != item->folder->inbox) {
716                 GSList *subs = vcal_folder_get_webcal_events_for_folder(item);
717                 GSList *cur = NULL;
718                 for (cur = subs; cur; cur = cur->next) {
719                         /* Don't free that, it's done when subscriptions are
720                          * fetched */
721                         icalcomponent *ical = (icalcomponent *)cur->data;
722                         VCalEvent *event = vcal_get_event_from_ical(
723                                 icalcomponent_as_ical_string(ical), NULL);
724                         events = g_slist_prepend(events, event);
725                 }
726                 g_slist_free(subs);
727                 return events;
728         }
729
730         dp = g_dir_open(vcal_manager_get_event_path(), 0, &error);
731         
732         if (!dp) {
733                 debug_print("couldn't open dir '%s': %s (%d)\n",
734                                 vcal_manager_get_event_path(), error->message, error->code);
735                 g_error_free(error);
736                 return 0;
737         }
738
739         while ((d = g_dir_read_name(dp)) != NULL) {
740                 VCalEvent *event = NULL;
741                 if (d[0] == '.' || strstr(d, ".bak")
742                 ||  !strcmp(d, "internal.ics")
743                 ||  !strcmp(d, "internal.ifb")
744                 ||  !strcmp(d, "multisync")) 
745                         continue;
746
747                 event = vcal_manager_load_event(d);
748                 if (!event)
749                         continue;
750                 if (event->rec_occurrence) {
751                         vcal_manager_free_event(event);
752                         claws_unlink(d);
753                         continue;
754                 }
755
756                 if (event && event->method != ICAL_METHOD_CANCEL) {
757                         PrefsAccount *account = vcal_manager_get_account_from_event(event);
758                         enum icalparameter_partstat status =
759                                 account ? vcal_manager_get_reply_for_attendee(event, account->address): ICAL_PARTSTAT_NEEDSACTION;
760                         if (status == ICAL_PARTSTAT_ACCEPTED
761                         ||  status == ICAL_PARTSTAT_TENTATIVE) {
762                                 events = g_slist_prepend(events, event);
763                         } else {
764                                 vcal_manager_free_event(event);
765                                 continue;
766                         }
767                         if ((status == ICAL_PARTSTAT_ACCEPTED
768                              || status == ICAL_PARTSTAT_TENTATIVE) 
769                             && event->recur && *(event->recur)) {
770                                 struct icalrecurrencetype recur;
771                                 struct icaltimetype dtstart;
772                                 struct icaltimetype next;
773                                 icalrecur_iterator* ritr;
774                                 time_t duration = (time_t) NULL;
775                                 struct icaldurationtype ical_dur;
776                                 int i = 0;
777
778                                 debug_print("dumping recurring events from main event %s\n", d);
779                                 recur = icalrecurrencetype_from_string(event->recur);
780                                 dtstart = icaltime_from_string(event->dtstart);
781
782                                 duration = icaltime_as_timet(icaltime_from_string(event->dtend))
783                                                             - icaltime_as_timet(icaltime_from_string(event->dtstart));
784
785                                 ical_dur = icaldurationtype_from_int(duration);
786
787                                 ritr = icalrecur_iterator_new(recur, dtstart);
788
789                                 next = icalrecur_iterator_next(ritr); /* skip first one */
790                                 if (!icaltime_is_null_time(next))
791                                         next = icalrecur_iterator_next(ritr);
792                                 debug_print("next time is %snull\n", icaltime_is_null_time(next)?"":"not ");
793                                 while (!icaltime_is_null_time(next) && i < 100) {
794                                         const gchar *new_start = NULL, *new_end = NULL;
795                                         VCalEvent *nevent = NULL;
796                                         gchar *uid = g_strdup_printf("%s-%d", event->uid, i);
797                                         new_start = icaltime_as_ical_string(next);
798                                         new_end = icaltime_as_ical_string(
799                                                         icaltime_add(next, ical_dur));
800                                         debug_print("adding with start/end %s:%s\n", new_start, new_end);
801                                         nevent = vcal_manager_new_event(uid, event->organizer, event->orgname, 
802                                                                 event->location, event->summary, event->description, 
803                                                                 new_start, new_end, NULL, 
804                                                                 event->tzid, event->url, event->method, 
805                                                                 event->sequence, event->type);
806                                         g_free(uid);
807                                         vcal_manager_copy_attendees(event, nevent);
808                                         nevent->rec_occurrence = TRUE;
809                                         vcal_manager_save_event(nevent, FALSE);
810                                         account = vcal_manager_get_account_from_event(event);
811                                         status =
812                                                 account ? vcal_manager_get_reply_for_attendee(event, account->address): ICAL_PARTSTAT_NEEDSACTION;
813                                         if (status == ICAL_PARTSTAT_ACCEPTED
814                                         ||  status == ICAL_PARTSTAT_TENTATIVE) {
815                                                 events = g_slist_prepend(events, nevent);
816                                         } else {
817                                                 vcal_manager_free_event(nevent);
818                                         }
819                                         next = icalrecur_iterator_next(ritr);
820                                         debug_print("next time is %snull\n", icaltime_is_null_time(next)?"":"not ");
821                                         i++;
822                                 }
823                                 icalrecur_iterator_free(ritr);
824                         }
825                 } else {
826                         vcal_manager_free_event(event);
827                 }
828         }
829         g_dir_close(dp);
830         return g_slist_reverse(events);
831 }
832
833 static gint vcal_get_num_list(Folder *folder, FolderItem *item,
834                                  MsgNumberList ** list, gboolean *old_uids_valid)
835 {
836         int n_msg = 1;
837         gint past_msg = -1, today_msg = -1, tomorrow_msg = -1, 
838                 thisweek_msg = -1, later_msg = -1;
839         GSList *events = NULL, *cur;
840         START_TIMING("");
841         g_return_val_if_fail (*list == NULL, 0); /* we expect a NULL list */
842
843         debug_print(" num for %s\n", ((VCalFolderItem *)item)->uri ? ((VCalFolderItem *)item)->uri:"(null)");
844         
845         *old_uids_valid = FALSE;
846         
847         if (((VCalFolderItem *)item)->uri) 
848                 return feed_fetch(item, list, old_uids_valid);
849         
850         events = vcal_get_events_list(item);
851         
852         debug_print("get_num_list\n");
853
854         if (hash_uids != NULL)
855                 g_hash_table_destroy(hash_uids);
856                 
857         hash_uids = g_hash_table_new_full(g_direct_hash, g_direct_equal,
858                                           NULL, g_free);
859         
860         for (cur = events; cur; cur = cur->next) {
861                 VCalEvent *event = (VCalEvent *)cur->data;
862
863                 if (!event)
864                         continue;
865                 g_hash_table_insert(hash_uids, GINT_TO_POINTER(n_msg), g_strdup(event->uid));
866                 
867                 if (event->rec_occurrence) {
868                         vcal_manager_free_event(event);
869                         continue;
870                 }
871
872                 if (event->method != ICAL_METHOD_CANCEL) {
873                         EventTime days;
874                         VCAL_FOLDER_ADD_EVENT(event);
875                 }
876                 if (event)
877                         vcal_manager_free_event(event);
878
879
880         }
881
882         if (today_msg == -1) { 
883                 *list = g_slist_prepend(*list, GINT_TO_POINTER(n_msg)); 
884                 today_msg = n_msg++; 
885                 g_hash_table_insert(hash_uids, GINT_TO_POINTER(today_msg), g_strdup(EVENT_TODAY_ID)); 
886         }
887
888         g_slist_free(events);
889         vcal_folder_export(folder);
890
891         vcal_set_mtime(folder, item);
892         
893         *list = g_slist_reverse(*list);
894         END_TIMING();
895         return g_slist_length(*list);
896 }
897
898 static MsgInfo *vcal_parse_msg(const gchar *file, FolderItem *item, int num)
899 {
900         MsgInfo *msginfo = NULL;
901         MsgFlags flags;
902
903         debug_print("parse_msg\n");
904         
905         flags.perm_flags = 0;
906         flags.tmp_flags = 0;
907         msginfo = procheader_parse_file(file, flags, TRUE, TRUE);
908         
909         msginfo->msgnum = num;
910         msginfo->folder = item;
911         return msginfo;
912 }
913
914 static MsgInfo *vcal_get_msginfo(Folder * folder,
915                                     FolderItem * item, gint num)
916 {
917         MsgInfo *msginfo = NULL;
918         gchar *file;
919
920         debug_print("get_msginfo\n");
921         
922         g_return_val_if_fail(item != NULL, NULL);
923         g_return_val_if_fail(num > 0, NULL);
924
925         file = vcal_fetch_msg(folder, item, num);
926
927         if (!file) {
928                 return NULL;
929         }
930
931         msginfo = vcal_parse_msg(file, item, num);
932
933         if (msginfo) {
934                 msginfo->flags.perm_flags = 0;
935                 msginfo->flags.tmp_flags = 0;
936
937                 vcal_change_flags(NULL, NULL, msginfo, 0);
938
939                 debug_print("  adding %d\n", num);
940         }
941         g_unlink(file);
942         g_free(file);
943
944         debug_print("  got msginfo %p\n", msginfo);
945
946         return msginfo;
947 }
948
949 static gchar *feed_fetch_item(FolderItem * fitem, gint num)
950 {
951         gchar *filename = NULL;
952         VCalFolderItem *item = (VCalFolderItem *)fitem;
953         GSList *ncur, *ecur;
954         int i = 1;
955         IcalFeedData *data = NULL;
956
957         if (!item->numlist) {
958                 folder_item_scan_full(fitem, FALSE);
959         }       
960         if (!item->numlist) {
961                 debug_print("numlist null\n");
962                 return NULL;
963         }
964
965         ncur = item->numlist;
966         ecur = item->evtlist;
967         
968         while (i < num) {
969                 if (!ncur || !ecur) {
970                         debug_print("list short end (%d to %d) %d,%d\n", i, num, ncur!=NULL, ecur!=NULL);
971                         return NULL;
972                 }
973                 ncur = ncur->next;
974                 ecur = ecur->next;
975                 i++;
976         }
977         
978         data = (IcalFeedData *)ecur->data;
979         
980         if (!data) {
981                 return NULL;
982         }
983
984         if (data->event)
985                 filename = vcal_manager_icalevent_dump(data->event, fitem->name, NULL);
986         else if (data->pseudoevent_id) {
987                 filename = vcal_manager_dateevent_dump(data->pseudoevent_id, fitem);
988                 created_files = g_slist_prepend(created_files, g_strdup(filename));
989         } else {
990                 debug_print("no event\n");
991                 return NULL;
992         }
993
994         debug_print("feed item dump to %s\n", filename);
995         return filename;
996 }
997
998 static gchar *vcal_fetch_msg(Folder * folder, FolderItem * item,
999                                 gint num)
1000 {
1001         gchar *filename = NULL;
1002         const gchar *uid = NULL;
1003
1004         debug_print(" fetch for %s %d\n", (((VCalFolderItem *)item)->uri ? ((VCalFolderItem *)item)->uri:"(null)"), num);
1005         if (((VCalFolderItem *)item)->uri) 
1006                 return feed_fetch_item(item, num);
1007
1008         if (!uid) {
1009                 if (!hash_uids)
1010                         folder_item_scan_full(item, FALSE);
1011                 uid = g_hash_table_lookup(hash_uids, GINT_TO_POINTER(num));
1012         }
1013         if (uid && 
1014             (!strcmp(uid, EVENT_PAST_ID) ||
1015              !strcmp(uid, EVENT_TODAY_ID) ||
1016              !strcmp(uid, EVENT_TOMORROW_ID) ||
1017              !strcmp(uid, EVENT_THISWEEK_ID) ||
1018              !strcmp(uid, EVENT_LATER_ID))) {
1019                 filename = vcal_manager_dateevent_dump(uid, item);
1020         } else if (uid) {
1021                 VCalEvent *event = NULL;
1022                 event = vcal_manager_load_event(uid);
1023                 if (event)
1024                         filename = vcal_manager_event_dump(event, FALSE, TRUE, NULL, FALSE);
1025
1026                 if (filename) {
1027                         created_files = g_slist_prepend(created_files, g_strdup(filename));
1028                 }
1029
1030                 vcal_manager_free_event(event);
1031         } 
1032                 
1033         return filename;
1034 }
1035
1036 static gint vcal_add_msg(Folder *folder, FolderItem *_dest, const gchar *file, MsgFlags *flags)
1037 {
1038         gchar *contents = file_read_to_str(file);
1039         if (contents) {
1040                 vcal_add_event(contents);               
1041         }
1042         g_free(contents);
1043         return 0;
1044 }
1045
1046 static void vcal_remove_event (Folder *folder, MsgInfo *msginfo);
1047
1048 static gint vcal_remove_msg(Folder *folder, FolderItem *_item, gint num)
1049 {
1050         MsgInfo *msginfo = folder_item_get_msginfo(_item, num);
1051
1052         if (!msginfo)
1053                 return 0;
1054
1055         if (_item == folder->inbox)
1056                 vcal_remove_event(folder, msginfo);
1057
1058         procmsg_msginfo_free(&msginfo);
1059         return 0;
1060 }
1061
1062 static FolderItem *vcal_create_folder(Folder * folder,
1063                                          FolderItem * parent,
1064                                          const gchar * name)
1065 {
1066         gchar *path = NULL;
1067         FolderItem *newitem = NULL;
1068         debug_print("creating new vcal folder\n");
1069
1070         path = g_strconcat((parent->path != NULL) ? parent->path : "", ".", name, NULL);
1071         newitem = folder_item_new(folder, name, path);
1072         folder_item_append(parent, newitem);
1073         g_free(path);
1074
1075         return newitem;
1076 }
1077
1078 static gint vcal_create_tree(Folder *folder)
1079 {
1080         FolderItem *rootitem, *inboxitem;
1081         GNode *rootnode, *inboxnode;
1082
1083         if (!folder->node) {
1084                 rootitem = folder_item_new(folder, folder->name, NULL);
1085                 rootitem->folder = folder;
1086                 rootnode = g_node_new(rootitem);
1087                 folder->node = rootnode;
1088                 rootitem->node = rootnode;
1089         } else {
1090                 rootitem = FOLDER_ITEM(folder->node->data);
1091                 rootnode = folder->node;
1092         }
1093
1094         /* Add inbox folder */
1095         if (!folder->inbox) {
1096                 inboxitem = folder_item_new(folder, _("Meetings"), ".meetings");
1097                 inboxitem->folder = folder;
1098                 inboxitem->stype = F_INBOX;
1099                 inboxnode = g_node_new(inboxitem);
1100                 inboxitem->node = inboxnode;
1101                 folder->inbox = inboxitem;
1102                 g_node_append(rootnode, inboxnode);     
1103         } else {
1104                 g_free(folder->inbox->path);
1105                 folder->inbox->path = g_strdup(".meetings");
1106         }
1107
1108         debug_print("created new vcal tree\n");
1109         return 0;
1110 }
1111
1112 static gint vcal_remove_folder(Folder *folder, FolderItem *fitem)
1113 {
1114         VCalFolderItem *item = (VCalFolderItem *)fitem;
1115         if (!item->uri)
1116                 return -1;
1117         else {
1118                 if (item->feed)
1119                         g_free(item->feed);
1120                 if (item->uri)
1121                         g_free(item->uri);
1122                 item->feed = NULL;
1123                 item->uri = NULL;
1124                 folder_item_remove(fitem);
1125                 return 0;
1126         }
1127 }
1128
1129 static gboolean vcal_scan_required(Folder *folder, FolderItem *item)
1130 {
1131         GStatBuf s;
1132         VCalFolderItem *vitem = (VCalFolderItem *)item;
1133
1134         g_return_val_if_fail(item != NULL, FALSE);
1135
1136         if (vitem->uri) {
1137                 return TRUE;
1138         } else if (g_stat(vcal_manager_get_event_path(), &s) < 0) {
1139                 return TRUE;
1140         } else if ((s.st_mtime > item->mtime) &&
1141                 (s.st_mtime - 3600 != item->mtime)) {
1142                 return TRUE;
1143         }
1144         return FALSE;
1145 }
1146
1147 static gint vcal_folder_lock_count = 0;
1148
1149 static void vcal_set_mtime(Folder *folder, FolderItem *item)
1150 {
1151         GStatBuf s;
1152         gchar *path = folder_item_get_path(item);
1153
1154         if (folder->inbox != item)
1155                 return;
1156
1157         g_return_if_fail(path != NULL);
1158
1159         if (g_stat(path, &s) < 0) {
1160                 FILE_OP_ERROR(path, "stat");
1161                 g_free(path);
1162                 return;
1163         }
1164
1165         item->mtime = s.st_mtime;
1166         debug_print("VCAL: forced mtime of %s to %lld\n",
1167                         item->name?item->name:"(null)", (long long)item->mtime);
1168         g_free(path);
1169 }
1170
1171 void vcal_folder_export(Folder *folder)
1172 {       
1173         FolderItem *item = folder?folder->inbox:NULL;
1174         gboolean need_scan = folder?vcal_scan_required(folder, item):TRUE;
1175         gchar *export_pass = NULL;
1176         gchar *export_freebusy_pass = NULL;
1177
1178         if (vcal_folder_lock_count) /* blocked */
1179                 return;
1180         vcal_folder_lock_count++;
1181         
1182         export_pass = vcal_passwd_get("export");
1183         export_freebusy_pass = vcal_passwd_get("export_freebusy");
1184
1185         if (vcal_meeting_export_calendar(vcalprefs.export_path, 
1186                         vcalprefs.export_user, 
1187                         export_pass,
1188                         TRUE)) {
1189                 debug_print("exporting calendar\n");
1190                 if (vcalprefs.export_enable &&
1191                     vcalprefs.export_command &&
1192                     strlen(vcalprefs.export_command))
1193                         execute_command_line(
1194                                 vcalprefs.export_command, TRUE, NULL);
1195         }
1196         if (export_pass != NULL) {
1197                 memset(export_pass, 0, strlen(export_pass));
1198         }
1199         g_free(export_pass);
1200         if (vcal_meeting_export_freebusy(vcalprefs.export_freebusy_path,
1201                         vcalprefs.export_freebusy_user,
1202                         export_freebusy_pass)) {
1203                 debug_print("exporting freebusy\n");
1204                 if (vcalprefs.export_freebusy_enable &&
1205                     vcalprefs.export_freebusy_command &&
1206                     strlen(vcalprefs.export_freebusy_command))
1207                         execute_command_line(
1208                                 vcalprefs.export_freebusy_command, TRUE, NULL);
1209         }
1210         if (export_freebusy_pass != NULL) {
1211                 memset(export_freebusy_pass, 0, strlen(export_freebusy_pass));
1212         }
1213         g_free(export_freebusy_pass);
1214         vcal_folder_lock_count--;
1215         if (!need_scan && folder) {
1216                 vcal_set_mtime(folder, folder->inbox);
1217         }
1218 }
1219
1220 static void vcal_remove_event (Folder *folder, MsgInfo *msginfo)
1221 {
1222         const gchar *uid = msginfo->msgid;
1223         VCalFolderItem *item = (VCalFolderItem *)msginfo->folder;
1224
1225         if (uid) {
1226                 gchar *file = vcal_manager_get_event_file(uid);
1227                 g_unlink(file);
1228                 g_free(file);
1229         }
1230         
1231         if (!item || !item->batching)
1232                 vcal_folder_export(folder);
1233         else if (item) {
1234                 item->dirty = TRUE;
1235         }
1236 }
1237
1238 static void vcal_change_flags(Folder *folder, FolderItem *_item, MsgInfo *msginfo, MsgPermFlags newflags)
1239 {
1240         EventTime date;
1241
1242         if (newflags & MSG_DELETED) {
1243                 /* delete the stuff */
1244                 msginfo->flags.perm_flags |= MSG_DELETED;
1245                 vcal_remove_event(folder, msginfo);
1246                 return;
1247         }
1248
1249         /* accept the rest */
1250         msginfo->flags.perm_flags = newflags;
1251
1252         /* but not color */
1253         msginfo->flags.perm_flags &= ~MSG_CLABEL_FLAG_MASK;
1254         
1255         date = event_to_today(NULL, msginfo->date_t);
1256         switch(date) {
1257         case EVENT_PAST:
1258                 break;
1259         case EVENT_TODAY:
1260                 msginfo->flags.perm_flags |= MSG_COLORLABEL_TO_FLAGS(2); /* Red */
1261                 break;
1262         case EVENT_TOMORROW:
1263                 break;
1264         case EVENT_THISWEEK:
1265                 break;
1266         case EVENT_LATER:
1267                 break;
1268         }
1269         if (msginfo->msgid) {
1270                 if (!strcmp(msginfo->msgid, EVENT_TODAY_ID) ||
1271                     !strcmp(msginfo->msgid, EVENT_TOMORROW_ID))
1272                 msginfo->flags.perm_flags |= MSG_MARKED;
1273         }
1274 }
1275
1276 void vcal_folder_gtk_init(void)
1277 {
1278         vcal_fill_popup_menu_labels();
1279
1280         folderview_register_popup(&vcal_popup);
1281 }
1282
1283 void vcal_folder_gtk_done(void)
1284 {
1285         GSList *cur = created_files;
1286         while (cur) {
1287                 gchar *file = (gchar *)cur->data;
1288                 cur = cur->next;
1289                 if (!file)                      
1290                         continue;
1291                 debug_print("removing %s\n", file);
1292                 g_unlink(file);
1293                 g_free(file);
1294         }
1295         g_slist_free(created_files);
1296         folderview_unregister_popup(&vcal_popup);
1297 }
1298
1299 static void add_menuitems(GtkUIManager *ui_manager, FolderItem *item)
1300 {
1301         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "NewMeeting", "FolderViewPopup/NewMeeting", GTK_UI_MANAGER_MENUITEM)
1302         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "ExportCal", "FolderViewPopup/ExportCal", GTK_UI_MANAGER_MENUITEM)
1303         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorVcal1", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
1304         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SubscribeCal", "FolderViewPopup/SubscribeCal", GTK_UI_MANAGER_MENUITEM)
1305         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "UnsubscribeCal", "FolderViewPopup/UnsubscribeCal", GTK_UI_MANAGER_MENUITEM)
1306         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorVcal2", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
1307         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RenameFolder", "FolderViewPopup/RenameFolder", GTK_UI_MANAGER_MENUITEM)
1308         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorVcal3", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
1309         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CheckSubs", "FolderViewPopup/CheckSubs", GTK_UI_MANAGER_MENUITEM)
1310         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorVcal4", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
1311         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "ListView", "FolderViewPopup/ListView", GTK_UI_MANAGER_MENUITEM)
1312         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "WeekView", "FolderViewPopup/WeekView", GTK_UI_MANAGER_MENUITEM)
1313         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "MonthView", "FolderViewPopup/MonthView", GTK_UI_MANAGER_MENUITEM)
1314         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorVcal5", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
1315 }
1316
1317 static gboolean setting_sensitivity = FALSE;
1318 static void set_sensitivity(GtkUIManager *ui_manager, FolderItem *fitem)
1319 {
1320         VCalFolderItem *item = (VCalFolderItem *)fitem;
1321
1322 #define SET_SENS(name, sens) \
1323         cm_menu_set_sensitive_full(ui_manager, "Popup/"name, sens)
1324
1325         setting_sensitivity = TRUE;
1326
1327         cm_toggle_menu_set_active_full(ui_manager, "Popup/FolderViewPopup/ListView", (item->use_cal_view == 0));
1328         cm_toggle_menu_set_active_full(ui_manager, "Popup/FolderViewPopup/WeekView", (item->use_cal_view == 1));
1329         cm_toggle_menu_set_active_full(ui_manager, "Popup/FolderViewPopup/MonthView", (item->use_cal_view == 2));
1330         SET_SENS("FolderViewPopup/NewMeeting",   item->uri == NULL);
1331         SET_SENS("FolderViewPopup/ExportCal", TRUE);
1332         SET_SENS("FolderViewPopup/SubscribeCal", item->uri == NULL);
1333         SET_SENS("FolderViewPopup/UnsubscribeCal", item->uri != NULL);
1334         SET_SENS("FolderViewPopup/RenameFolder", folder_item_parent(fitem) != NULL);
1335         SET_SENS("FolderViewPopup/CheckSubs", TRUE);
1336         SET_SENS("FolderViewPopup/ListView", folder_item_parent(fitem) != NULL);
1337         SET_SENS("FolderViewPopup/WeekView", folder_item_parent(fitem) != NULL);
1338         SET_SENS("FolderViewPopup/MonthView", folder_item_parent(fitem) != NULL);
1339         setting_sensitivity = FALSE;
1340 #undef SET_SENS
1341 }
1342
1343 static void new_meeting_cb(GtkAction *action, gpointer data)
1344 {
1345         debug_print("new_meeting_cb\n");
1346         vcal_meeting_create(NULL);
1347 }
1348
1349 GSList * vcal_folder_get_waiting_events(void)
1350 {
1351         Folder *folder = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
1352         return vcal_get_events_list(folder->inbox);
1353 }
1354
1355 typedef struct _get_webcal_data {
1356         GSList *list;
1357         FolderItem *item;
1358 } GetWebcalData;
1359
1360 static gboolean get_webcal_events_func(GNode *node, gpointer user_data)
1361 {
1362         FolderItem *item = node->data;
1363         GetWebcalData *data = user_data;
1364         gboolean dummy = FALSE;
1365         GSList *list = NULL, *cur = NULL;
1366
1367         if (data->item && data->item != item)
1368                 return FALSE;
1369
1370         feed_fetch(item, &list, &dummy);
1371
1372         g_slist_free(list);
1373
1374         for (cur = ((VCalFolderItem *)item)->evtlist; cur; cur = cur->next) {
1375                 IcalFeedData *fdata = (IcalFeedData *)cur->data;
1376                 if (fdata->event)
1377                         data->list = g_slist_prepend(data->list, fdata->event);
1378         }
1379         return FALSE;
1380 }
1381
1382 GSList * vcal_folder_get_webcal_events(void)
1383 {
1384         GetWebcalData *data = g_new0(GetWebcalData, 1);
1385         Folder *folder = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
1386         GSList *list = NULL;
1387         data->item = NULL;
1388         g_node_traverse(folder->node, G_PRE_ORDER,
1389                         G_TRAVERSE_ALL, -1, get_webcal_events_func, data);
1390
1391         list = data->list;
1392         g_free(data);
1393
1394         return g_slist_reverse(list);
1395 }
1396
1397 static gboolean vcal_free_data_func(GNode *node, gpointer user_data)
1398 {
1399         VCalFolderItem *item = node->data;
1400
1401         if (item->cal) {
1402                 icalcomponent_free(item->cal);
1403                 item->cal = NULL;
1404         }
1405         if (item->numlist) {
1406                 g_slist_free(item->numlist);
1407                 item->numlist = NULL;
1408         }
1409
1410         if (item->evtlist) {
1411                 slist_free_icalfeeddata(item->evtlist);
1412                 g_slist_free(item->evtlist);
1413                 item->evtlist = NULL;
1414         }
1415
1416         return FALSE;
1417 }
1418
1419 void vcal_folder_free_data(void)
1420 {
1421         Folder *folder = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
1422
1423         g_node_traverse(folder->node, G_PRE_ORDER,
1424                         G_TRAVERSE_ALL, -1, vcal_free_data_func, NULL);
1425 }
1426
1427 GSList * vcal_folder_get_webcal_events_for_folder(FolderItem *item)
1428 {
1429         GetWebcalData *data = g_new0(GetWebcalData, 1);
1430         Folder *folder = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
1431         GSList *list = NULL;
1432         data->item = item;
1433         g_node_traverse(folder->node, G_PRE_ORDER,
1434                         G_TRAVERSE_ALL, -1, get_webcal_events_func, data);
1435
1436         list = data->list;
1437         g_free(data);
1438
1439         return g_slist_reverse(list);
1440 }
1441
1442 gchar* get_item_event_list_for_date(FolderItem *item, EventTime date)
1443 {
1444         GSList *strs = NULL;
1445         GSList *cur;
1446         gchar *result = NULL;
1447         gchar *datestr = NULL;
1448
1449         if (((VCalFolderItem *)item)->uri) {
1450                 for (cur = ((VCalFolderItem *)item)->evtlist; cur; cur = cur->next) {
1451                         IcalFeedData *fdata = (IcalFeedData *)cur->data;
1452                         icalproperty *prop;
1453                         struct icaltimetype itt;
1454                         gchar *summary = NULL;
1455                         EventTime days;
1456                         if (!fdata->event)
1457                                 continue;
1458                         prop = icalcomponent_get_first_property((icalcomponent *)fdata->event, ICAL_DTSTART_PROPERTY);
1459                         
1460                         if (!prop)
1461                                 continue;
1462                         itt = icalproperty_get_dtstart(prop);
1463                         days = event_to_today(NULL, icaltime_as_timet(itt));
1464                         if (days != date)
1465                                 continue;
1466                         prop = icalcomponent_get_first_property((icalcomponent *)fdata->event, ICAL_SUMMARY_PROPERTY);
1467                         if (prop) {
1468                                 if (!g_utf8_validate(icalproperty_get_summary(prop), -1, NULL))
1469                                         summary = conv_codeset_strdup(icalproperty_get_summary(prop), 
1470                                                 conv_get_locale_charset_str(), CS_UTF_8);
1471                                 else
1472                                         summary = g_strdup(icalproperty_get_summary(prop));
1473                         } else
1474                                 summary = g_strdup("-");
1475
1476                         strs = g_slist_prepend(strs, summary);
1477                 }
1478         } else {
1479                 GSList *evtlist = vcal_folder_get_waiting_events();
1480                 for (cur = evtlist; cur; cur = cur->next) {
1481                         VCalEvent *event = (VCalEvent *)cur->data;
1482                         EventTime days;
1483                         days = event_to_today(event, 0);
1484                         gchar *summary = NULL;
1485                         if (days == date) {
1486                                 summary = g_strdup(event->summary);
1487                                 strs = g_slist_prepend(strs, summary);
1488                         }
1489                         vcal_manager_free_event(event);
1490                 }
1491         }
1492         
1493         switch(date) {
1494         case EVENT_PAST:
1495                 datestr=_("in the past");
1496                 break;
1497         case EVENT_TODAY:
1498                 datestr=_("today");
1499                 break;
1500         case EVENT_TOMORROW:
1501                 datestr=_("tomorrow");
1502                 break;
1503         case EVENT_THISWEEK:
1504                 datestr=_("this week");
1505                 break;
1506         case EVENT_LATER:
1507                 datestr=_("later");
1508                 break;
1509         }
1510         
1511         result = g_strdup_printf(_("\nThese are the events planned %s:\n"),
1512                         datestr?datestr:"never");
1513         
1514         strs = g_slist_reverse(strs);
1515         for (cur = strs; cur; cur = cur->next) {
1516                 int e_len = strlen(result);
1517                 int n_len = strlen((gchar *)cur->data);
1518                 if (e_len) {
1519                         result = g_realloc(result, e_len+n_len+4);
1520                         *(result+e_len) = '\n';
1521                         strcpy(result+e_len+1, "- ");
1522                         strcpy(result+e_len+3, (gchar *)cur->data);
1523                 } else {
1524                         result = g_realloc(result, e_len+n_len+3);
1525                         strcpy(result+e_len, "- ");
1526                         strcpy(result+e_len+2, (gchar *)cur->data);
1527                 }
1528         }
1529         slist_free_strings(strs);
1530         g_slist_free(strs);
1531         return result;
1532 }
1533
1534 static void export_cal_cb(GtkAction *action, gpointer data)
1535 {
1536         vcal_meeting_export_calendar(NULL, NULL, NULL, FALSE);
1537 }
1538
1539 struct CBuf {
1540         gchar *str;
1541 };
1542
1543 static size_t curl_recv(void *buf, size_t size, size_t nmemb, void *stream)
1544 {
1545         struct CBuf *buffer = (struct CBuf *)stream;
1546         gchar *tmp = NULL;
1547         gchar tmpbuf[size*nmemb + 1];
1548
1549         memcpy(tmpbuf, buf, size*nmemb);
1550         tmpbuf[size*nmemb] = '\0';
1551
1552         if (buffer->str) {
1553                 tmp = g_strconcat(buffer->str, tmpbuf, NULL);
1554                 g_free(buffer->str);
1555                 buffer->str = tmp;
1556         } else {
1557                 buffer->str = g_strdup(tmpbuf);
1558         }
1559
1560         return size*nmemb;
1561 }
1562
1563 void *url_read_thread(void *data)
1564 {
1565         thread_data *td = (thread_data *)data;
1566         CURLcode res;
1567         CURL *curl_ctx = NULL;
1568         long response_code;
1569         struct CBuf buffer = { NULL };
1570         gchar *t_url = (gchar *)td->url;
1571
1572         while (*t_url == ' ')
1573                 t_url++;
1574         if (strchr(t_url, ' '))
1575                 *(strchr(t_url, ' ')) = '\0';
1576
1577 #ifdef USE_PTHREAD
1578         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
1579         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
1580 #endif
1581         
1582         curl_ctx = curl_easy_init();
1583         
1584         curl_easy_setopt(curl_ctx, CURLOPT_URL, t_url);
1585         curl_easy_setopt(curl_ctx, CURLOPT_WRITEFUNCTION, curl_recv);
1586         curl_easy_setopt(curl_ctx, CURLOPT_WRITEDATA, &buffer);
1587         curl_easy_setopt(curl_ctx, CURLOPT_TIMEOUT, prefs_common_get_prefs()->io_timeout_secs);
1588         curl_easy_setopt(curl_ctx, CURLOPT_NOSIGNAL, 1);
1589 #if LIBCURL_VERSION_NUM >= 0x070a00
1590         if(vcalprefs.ssl_verify_peer == FALSE) {
1591                 curl_easy_setopt(curl_ctx, CURLOPT_SSL_VERIFYPEER, 0);
1592                 curl_easy_setopt(curl_ctx, CURLOPT_SSL_VERIFYHOST, 0);
1593         }
1594 #endif
1595         curl_easy_setopt(curl_ctx, CURLOPT_USERAGENT, 
1596                 "Claws Mail vCalendar plugin "
1597                 "(" PLUGINS_URI ")");
1598         curl_easy_setopt(curl_ctx, CURLOPT_FOLLOWLOCATION, 1);
1599         res = curl_easy_perform(curl_ctx);
1600         
1601         if (res != 0) {
1602                 debug_print("res %d %s\n", res, curl_easy_strerror(res));
1603                 td->error = g_strdup(curl_easy_strerror(res));
1604                 
1605                 if(res == CURLE_OPERATION_TIMEOUTED)
1606                         log_error(LOG_PROTOCOL, _("Timeout (%d seconds) connecting to %s\n"),
1607                                 prefs_common_get_prefs()->io_timeout_secs, t_url);
1608         }
1609
1610         curl_easy_getinfo(curl_ctx, CURLINFO_RESPONSE_CODE, &response_code);
1611         if( response_code >= 400 && response_code < 500 ) {
1612                 debug_print("VCalendar: got %ld\n", response_code);
1613                 switch(response_code) {
1614                         case 401: 
1615                                 td->error = g_strdup(_("401 (Authorisation required)"));
1616                                 break;
1617                         case 403:
1618                                 td->error = g_strdup(_("403 (Unauthorised)"));
1619                                 break;
1620                         case 404:
1621                                 td->error = g_strdup(_("404 (Not found)"));
1622                                 break;
1623                         default:
1624                                 td->error = g_strdup_printf(_("Error %ld"), response_code);
1625                                 break;
1626                 }
1627         }
1628         curl_easy_cleanup(curl_ctx);
1629         if (buffer.str) {
1630                 td->result = g_strdup(buffer.str);
1631                 g_free(buffer.str);
1632         }
1633
1634         td->done = TRUE; /* let the caller thread join() */
1635         return GINT_TO_POINTER(0);
1636 }
1637
1638 gchar *vcal_curl_read(const char *url, const gchar *label, gboolean verbose, 
1639         void (*callback)(const gchar *url, gchar *data, gboolean verbose, gchar *error))
1640 {
1641         gchar *result;
1642         thread_data *td;
1643 #ifdef USE_PTHREAD
1644         pthread_t pt;
1645         pthread_attr_t pta;
1646 #endif
1647         void *res;
1648         gchar *error = NULL;
1649         result = NULL;
1650         td = g_new0(thread_data, 1);
1651         res = NULL;
1652
1653         td->url  = url;
1654         td->result  = NULL;
1655         td->done = FALSE;
1656
1657         STATUSBAR_PUSH(mainwindow_get_mainwindow(), label);
1658
1659 #ifdef USE_PTHREAD
1660         if (pthread_attr_init(&pta) != 0 ||
1661             pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE) != 0 ||
1662             pthread_create(&pt, &pta, 
1663                         url_read_thread, td) != 0) {
1664                 url_read_thread(td);    
1665         }
1666         while (!td->done)  {
1667                 claws_do_idle();
1668         }
1669  
1670         pthread_join(pt, &res);
1671 #else
1672         url_read_thread(td);
1673 #endif
1674         
1675         result = td->result;
1676         error = td->error;
1677         g_free(td);
1678         
1679         STATUSBAR_POP(mainwindow_get_mainwindow());
1680
1681         if (callback) {
1682                 callback(url, result, verbose, error);
1683                 return NULL;
1684         } else {
1685                 if (error)
1686                         g_free(error);
1687                 return result;
1688         }
1689 }
1690
1691 gboolean vcal_curl_put(gchar *url, FILE *fp, gint filesize, const gchar *user, const gchar *pass)
1692 {
1693         gboolean res = TRUE;
1694         CURL *curl_ctx = curl_easy_init();
1695         long response_code = 0;
1696         gchar *t_url = url;
1697         gchar *userpwd = NULL;
1698
1699         struct curl_slist * headers = curl_slist_append(NULL, 
1700                 "Content-Type: text/calendar; charset=\"utf-8\"" );
1701
1702         while (*t_url == ' ')
1703                 t_url++;
1704         if (strchr(t_url, ' '))
1705                 *(strchr(t_url, ' ')) = '\0';
1706
1707         if (user && pass && *user && *pass) {
1708                 userpwd = g_strdup_printf("%s:%s",user,pass);
1709                 curl_easy_setopt(curl_ctx, CURLOPT_USERPWD, userpwd);
1710         }
1711         curl_easy_setopt(curl_ctx, CURLOPT_URL, t_url);
1712         curl_easy_setopt(curl_ctx, CURLOPT_UPLOAD, 1);
1713         curl_easy_setopt(curl_ctx, CURLOPT_READFUNCTION, NULL);
1714         curl_easy_setopt(curl_ctx, CURLOPT_READDATA, fp);
1715         curl_easy_setopt(curl_ctx, CURLOPT_HTTPHEADER, headers);
1716 #if LIBCURL_VERSION_NUM >= 0x070a00
1717         if(vcalprefs.ssl_verify_peer == FALSE) {
1718                 curl_easy_setopt(curl_ctx, CURLOPT_SSL_VERIFYPEER, 0);
1719                 curl_easy_setopt(curl_ctx, CURLOPT_SSL_VERIFYHOST, 0);
1720         }
1721 #endif
1722         curl_easy_setopt(curl_ctx, CURLOPT_USERAGENT, 
1723                 "Claws Mail vCalendar plugin "
1724                 "(" PLUGINS_URI ")");
1725         curl_easy_setopt(curl_ctx, CURLOPT_INFILESIZE, filesize);
1726         res = curl_easy_perform(curl_ctx);
1727         g_free(userpwd);
1728
1729         if (res != 0) {
1730                 debug_print("res %d %s\n", res, curl_easy_strerror(res));
1731         } else {
1732                 res = TRUE;
1733         }
1734
1735         curl_easy_getinfo(curl_ctx, CURLINFO_RESPONSE_CODE, &response_code);
1736         if (response_code < 200 || response_code >= 300) {
1737                 g_warning("Can't export calendar, got code %ld", response_code);
1738                 res = FALSE;
1739         }
1740         curl_easy_cleanup(curl_ctx);
1741         curl_slist_free_all(headers);
1742         return res;
1743 }
1744
1745 static gboolean folder_item_find_func(GNode *node, gpointer data)
1746 {
1747         FolderItem *item = node->data;
1748         gpointer *d = data;
1749         const gchar *uri = d[0];
1750
1751         if (!uri || !((VCalFolderItem *)item)->uri
1752         ||  strcmp(uri, ((VCalFolderItem *)item)->uri))
1753                 return FALSE;
1754
1755         d[1] = item;
1756
1757         return TRUE;
1758 }
1759
1760 static FolderItem *get_folder_item_for_uri(const gchar *uri)
1761 {
1762         Folder *root = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
1763         gpointer d[2];
1764         
1765         if (root == NULL)
1766                 return NULL;
1767         
1768         d[0] = (gpointer)uri;
1769         d[1] = NULL;
1770         g_node_traverse(root->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1771                         folder_item_find_func, d);
1772         return d[1];
1773 }
1774
1775 static gchar *feed_get_title(const gchar *str)
1776 {
1777         gchar *title = NULL;
1778         if (strstr(str, "X-WR-CALNAME:")) {
1779                 title = g_strdup(strstr(str, "X-WR-CALNAME:")+strlen("X-WR-CALNAME:"));
1780                 if (strstr(title, "\n"))
1781                         *(strstr(title, "\n")) = '\0';
1782                 if (strstr(title, "\r"))
1783                         *(strstr(title, "\r")) = '\0';          
1784         } else if (strstr(str, "X-WR-CALDESC:")) {
1785                 title = g_strdup(strstr(str, "X-WR-CALDESC:")+strlen("X-WR-CALDESC:"));
1786                 if (strstr(title, "\n"))
1787                         *(strstr(title, "\n")) = '\0';
1788                 if (strstr(title, "\r"))
1789                         *(strstr(title, "\r")) = '\0';          
1790         }
1791         
1792         return title;
1793 }
1794
1795 static void update_subscription_finish(const gchar *uri, gchar *feed, gboolean verbose, gchar *error)
1796 {
1797         Folder *root = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
1798         FolderItem *item = NULL;
1799         icalcomponent *cal = NULL;
1800         
1801         if (root == NULL) {
1802                 g_warning("can't get root folder");
1803                 g_free(feed);
1804                 if (error)
1805                         g_free(error);
1806                 return;
1807         }
1808
1809         if (feed == NULL) {
1810                 if (verbose && manual_update) {
1811                         gchar *tmp; 
1812                         tmp = g_strdup(uri);
1813                         if (strlen(uri) > 61) {
1814                                 tmp[55]='[';
1815                                 tmp[56]='.';
1816                                 tmp[57]='.';
1817                                 tmp[58]='.';
1818                                 tmp[59]=']';
1819                                 tmp[60]='\0';
1820                         } 
1821                         alertpanel_error(_("Could not retrieve the Webcal URL:\n%s:\n\n%s"),
1822                                         tmp, error ? error:_("Unknown error"));
1823                         g_free(tmp);
1824                 } else  {
1825                         log_error(LOG_PROTOCOL, _("Could not retrieve the Webcal URL:\n%s:\n\n%s\n"),
1826                                         uri, error ? error:_("Unknown error"));
1827                 }
1828                 main_window_cursor_normal(mainwindow_get_mainwindow());
1829                 g_free(feed);
1830                 if (error)
1831                         g_free(error);
1832                 return;
1833         }
1834         if (strncmp(feed, "BEGIN:VCALENDAR", strlen("BEGIN:VCALENDAR"))) {
1835                 if (verbose && manual_update) {
1836                         alertpanel_error(_("This URL does not look like a Webcal URL:\n%s\n%s"),
1837                                         uri, error ? error:_("Unknown error"));
1838                 } else  {
1839                         log_error(LOG_PROTOCOL, _("This URL does not look like a Webcal URL:\n%s\n%s\n"),
1840                                         uri, error ? error:_("Unknown error"));
1841                 }
1842                 g_free(feed);
1843                 main_window_cursor_normal(mainwindow_get_mainwindow());
1844                 if (error)
1845                         g_free(error);
1846                 return;
1847         }
1848         
1849         if (error)
1850                 g_free(error);
1851         item = get_folder_item_for_uri(uri);
1852         if (item == NULL) {
1853                 gchar *title = feed_get_title(feed);
1854                 if (title == NULL) {
1855                         if (strstr(uri, "://"))
1856                                 title = g_path_get_basename(strstr(uri,"://")+3);
1857                         else
1858                                 title = g_strdup(uri);
1859                         subst_for_filename(title);
1860                 }
1861                 item = folder_create_folder(root->node->data, title);
1862                 if (!item) {
1863                         if (verbose && manual_update) {
1864                                 alertpanel_error(_("Could not create directory %s"),
1865                                         title);
1866                         } else  {
1867                                 log_error(LOG_PROTOCOL, _("Could not create directory %s"),
1868                                         title);
1869                         }
1870                         g_free(feed);
1871                         g_free(title);
1872                         main_window_cursor_normal(mainwindow_get_mainwindow());
1873                         return;
1874                 }
1875                 debug_print("item done %s\n", title);
1876                 ((VCalFolderItem *)item)->uri = g_strdup(uri);
1877                 ((VCalFolderItem *)item)->feed = feed;
1878                 g_free(title);
1879         } else {
1880                 if (((VCalFolderItem *)item)->feed)
1881                         g_free(((VCalFolderItem *)item)->feed);
1882
1883                 ((VCalFolderItem *)item)->feed = feed;
1884                 /* if title differs, update it */
1885         }
1886         cal = icalparser_parse_string(feed);
1887
1888         convert_to_utc(cal);
1889         
1890         if (((VCalFolderItem *)item)->cal)
1891                 icalcomponent_free(((VCalFolderItem *)item)->cal);
1892
1893         ((VCalFolderItem *)item)->cal = cal;
1894         
1895         main_window_cursor_normal(mainwindow_get_mainwindow());
1896         ((VCalFolderItem *)item)->last_fetch = time(NULL);
1897 }
1898
1899 static void update_subscription(const gchar *uri, gboolean verbose)
1900 {
1901         FolderItem *item = get_folder_item_for_uri(uri);
1902         gchar *label;
1903
1904         if (prefs_common_get_prefs()->work_offline) {
1905                 if (!verbose || 
1906                 !inc_offline_should_override(TRUE,
1907                    _("Claws Mail needs network access in order "
1908                      "to update the Webcal feed.")))
1909                         return;
1910         }
1911         if (item) {
1912                 if (time(NULL) - ((VCalFolderItem *)(item))->last_fetch < 60 && 
1913                     ((VCalFolderItem *)(item))->cal)
1914                         return;
1915         }
1916         main_window_cursor_wait(mainwindow_get_mainwindow());
1917
1918         label = g_strdup_printf(_("Fetching calendar for %s..."), 
1919                         item && item->name ? item->name : _("new subscription"));
1920         vcal_curl_read(uri, label, verbose, update_subscription_finish);
1921         g_free(label);
1922 }
1923
1924 static void check_subs_cb(GtkAction *action, gpointer data)
1925 {
1926         Folder *root = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
1927
1928         if (prefs_common_get_prefs()->work_offline && 
1929             !inc_offline_should_override(TRUE,
1930                      _("Claws Mail needs network access in order "
1931                      "to update the subscription.")))
1932                 return;
1933
1934         folderview_check_new(root);
1935 }
1936
1937 static void subscribe_cal_cb(GtkAction *action, gpointer data)
1938 {
1939         gchar *uri = NULL;
1940         gchar *tmp = NULL;
1941
1942         tmp = input_dialog(_("Subscribe to Webcal"), _("Enter the WebCal URL:"), NULL);
1943         if (tmp == NULL)
1944                 return;
1945         
1946         if (!strncmp(tmp, "http", 4)) {
1947                 uri = tmp;
1948         } else if (!strncmp(tmp, "file://", 7)) {
1949                 uri = tmp;
1950         } else if (!strncmp(tmp, "webcal", 6)) {
1951                 uri = g_strconcat("http", tmp+6, NULL);
1952                 g_free(tmp);
1953         } else {
1954                 alertpanel_error(_("Could not parse the URL."));
1955                 g_free(tmp);
1956                 return;
1957         }
1958         debug_print("uri %s\n", uri);
1959         
1960         update_subscription(uri, TRUE); 
1961         folder_write_list();
1962         g_free(uri);
1963 }
1964
1965 static void unsubscribe_cal_cb(GtkAction *action, gpointer data)
1966 {
1967         FolderView *folderview = (FolderView *)data;
1968         FolderItem *item, *opened;
1969         gchar *message;
1970         AlertValue avalue;
1971         gchar *old_id;
1972
1973         if (!folderview->selected) return;
1974
1975         item = folderview_get_selected_item(folderview);
1976         g_return_if_fail(item != NULL);
1977         g_return_if_fail(item->path != NULL);
1978         g_return_if_fail(item->folder != NULL);
1979         opened = folderview_get_opened_item(folderview);
1980
1981         message = g_strdup_printf
1982                 (_("Do you really want to unsubscribe?"));
1983         avalue = alertpanel_full(_("Delete subscription"), message,
1984                                  GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, 
1985                                  FALSE, NULL, ALERT_WARNING, G_ALERTDEFAULT);
1986         g_free(message);
1987         if (avalue != G_ALERTALTERNATE) return;
1988
1989         old_id = folder_item_get_identifier(item);
1990
1991         vcal_item_closed(item);
1992
1993         if (item == opened ||
1994                         folder_is_child_of(item, opened)) {
1995                 summary_clear_all(folderview->summaryview);
1996                 folderview_close_opened(folderview, TRUE);
1997         }
1998
1999         if (item->folder->klass->remove_folder(item->folder, item) < 0) {
2000                 folder_item_scan(item);
2001                 alertpanel_error(_("Can't remove the folder '%s'."), item->name);
2002                 g_free(old_id);
2003                 return;
2004         }
2005
2006         folder_write_list();
2007
2008         prefs_filtering_delete_path(old_id);
2009         g_free(old_id);
2010 }
2011
2012 gboolean vcal_subscribe_uri(Folder *folder, const gchar *uri)
2013 {
2014         gchar *tmp = NULL;
2015         if (folder->klass != vcal_folder_get_class())
2016                 return FALSE;
2017
2018         if (uri == NULL)
2019                 return FALSE;
2020
2021         if (!strncmp(uri, "webcal", 6)) {
2022                 tmp = g_strconcat("http", uri+6, NULL);
2023         } else {
2024                 return FALSE;
2025         }
2026         debug_print("uri %s\n", tmp);
2027         
2028         update_subscription(tmp, FALSE);
2029         folder_write_list();
2030         return TRUE;
2031 }
2032
2033 static void rename_cb(GtkAction *action, gpointer data)
2034 {
2035         FolderView *folderview = (FolderView *)data;
2036         FolderItem *item;
2037         gchar *new_folder;
2038         gchar *name;
2039         gchar *message;
2040
2041         item = folderview_get_selected_item(folderview);
2042         g_return_if_fail(item != NULL);
2043         g_return_if_fail(item->path != NULL);
2044         g_return_if_fail(item->folder != NULL);
2045
2046         name = trim_string(item->name, 32);
2047         message = g_strdup_printf(_("Input new name for '%s':"), name);
2048         new_folder = input_dialog(_("Rename folder"), message, name);
2049         g_free(message);
2050         g_free(name);
2051         if (!new_folder) return;
2052         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
2053
2054         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
2055                 alertpanel_error(_("'%c' can't be included in folder name."),
2056                                  G_DIR_SEPARATOR);
2057                 return;
2058         }
2059
2060         if (folder_find_child_item_by_name(folder_item_parent(item), new_folder)) {
2061                 name = trim_string(new_folder, 32);
2062                 alertpanel_error(_("The folder '%s' already exists."), name);
2063                 g_free(name);
2064                 return;
2065         }
2066
2067         if (folder_item_rename(item, new_folder) < 0) {
2068                 alertpanel_error(_("The folder could not be renamed.\n"
2069                                    "The new folder name is not allowed."));
2070                 return;
2071         }
2072
2073         folder_item_prefs_save_config_recursive(item);
2074         folder_write_list();
2075 }
2076
2077 static void set_view_cb(GtkAction *gaction, GtkRadioAction *current, gpointer data)
2078 {
2079         FolderView *folderview = (FolderView *)data;
2080         gint action = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (current));
2081         FolderItem *item = NULL, *oitem = NULL;
2082
2083         if (!folderview->selected) return;
2084         if (setting_sensitivity) return;
2085
2086         oitem = folderview_get_opened_item(folderview);
2087         item = folderview_get_selected_item(folderview);
2088
2089         if (!item)
2090                 return;
2091
2092         if (((VCalFolderItem *)(item))->use_cal_view == action)
2093                 return;
2094         debug_print("set view %d\n", action);
2095         if (oitem && item == oitem && oitem->folder->klass == vcal_folder_get_class())
2096                 oitem->folder->klass->item_closed(oitem);
2097         ((VCalFolderItem *)(item))->use_cal_view = action;
2098         if (((VCalFolderItem *)(item))->use_cal_view) {
2099                 if (oitem && item == oitem && oitem->folder->klass == vcal_folder_get_class())
2100                         oitem->folder->klass->item_opened(oitem);
2101         }
2102 }
2103
2104 gchar *vcal_get_event_as_ical_str(VCalEvent *event)
2105 {
2106         gchar *ical;
2107         icalcomponent *calendar = icalcomponent_vanew(
2108             ICAL_VCALENDAR_COMPONENT,
2109             icalproperty_new_version("2.0"),
2110             icalproperty_new_prodid(
2111                  "-//Claws Mail//NONSGML Claws Mail Calendar//EN"),
2112             icalproperty_new_calscale("GREGORIAN"),
2113             (void*)0);
2114         vcal_manager_event_dump(event, FALSE, FALSE, calendar, FALSE);
2115         ical = g_strdup(icalcomponent_as_ical_string(calendar));
2116         icalcomponent_free(calendar);
2117         
2118         return ical;
2119 }
2120
2121 static gchar *get_name_from_property(icalproperty *p)
2122 {
2123         gchar *tmp = NULL;
2124         
2125         if (p && icalproperty_get_parameter_as_string(p, "CN") != NULL)
2126                 tmp = g_strdup(icalproperty_get_parameter_as_string(p, "CN"));
2127
2128         return tmp;
2129 }
2130
2131 static gchar *get_email_from_property(icalproperty *p)
2132 {
2133         gchar *tmp = NULL;
2134         gchar *email = NULL;
2135         
2136         if (p)
2137                 tmp = g_strdup(icalproperty_get_organizer(p));
2138
2139         if (!tmp) 
2140                 return NULL;
2141
2142         if (!strncasecmp(tmp, "MAILTO:", strlen("MAILTO:")))
2143                 email = g_strdup(tmp+strlen("MAILTO:"));
2144         else
2145                 email = g_strdup(tmp);
2146         g_free(tmp);
2147         
2148         return email;
2149 }
2150
2151 static void convert_to_utc(icalcomponent *calendar)
2152 {
2153         icalcomponent *event;
2154         icaltimezone *tz, *tzutc = icaltimezone_get_utc_timezone();
2155         icalproperty *prop;
2156         icalparameter *tzid;
2157
2158         cm_return_if_fail(calendar != NULL);
2159
2160         for (
2161                         event = icalcomponent_get_first_component(calendar,
2162                                 ICAL_VEVENT_COMPONENT);
2163                         event != NULL;
2164                         event = icalcomponent_get_next_component(calendar,
2165                                 ICAL_VEVENT_COMPONENT)) {
2166
2167                 /* DTSTART */
2168                 if ((prop = icalcomponent_get_first_property(event, ICAL_DTSTART_PROPERTY)) != NULL
2169                                 && (tzid = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER)) != NULL) {
2170                         /* Event has its DTSTART with a timezone specification, let's convert
2171                          * to UTC and remove the TZID parameter. */
2172
2173                         tz = icalcomponent_get_timezone(calendar, icalparameter_get_iana_value(tzid));
2174                         if (tz != NULL) {
2175                                 debug_print("Converting DTSTART to UTC.\n");
2176                                 icaltimetype t = icalproperty_get_dtstart(prop);
2177                                 icaltimezone_convert_time(&t, tz, tzutc);
2178                                 icalproperty_set_dtstart(prop, t);
2179                                 icalproperty_remove_parameter_by_ref(prop, tzid);
2180                         }
2181                 }
2182
2183                 /* DTEND */
2184                 if ((prop = icalcomponent_get_first_property(event, ICAL_DTEND_PROPERTY)) != NULL
2185                                 && (tzid = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER)) != NULL) {
2186                         /* Event has its DTEND with a timezone specification, let's convert
2187                          * to UTC and remove the TZID parameter. */
2188
2189                         tz = icalcomponent_get_timezone(calendar, icalparameter_get_iana_value(tzid));
2190                         if (tz != NULL) {
2191                                 debug_print("Converting DTEND to UTC.\n");
2192                                 icaltimetype t = icalproperty_get_dtend(prop);
2193                                 icaltimezone_convert_time(&t, tz, tzutc);
2194                                 icalproperty_set_dtend(prop, t);
2195                                 icalproperty_remove_parameter_by_ref(prop, tzid);
2196                         }
2197                 }
2198         }
2199 }
2200
2201 #define GET_PROP(comp,prop,kind) {                                              \
2202         prop = NULL;                                                            \
2203         if (!(prop = icalcomponent_get_first_property(comp, kind))) {           \
2204                 prop = inner                                                    \
2205                         ? icalcomponent_get_first_property(inner, kind)         \
2206                         : NULL;                                                 \
2207         }                                                                       \
2208 }
2209
2210 #define GET_PROP_LIST(comp,list,kind) {                                         \
2211         list = NULL;                                                            \
2212         if (!(prop = icalcomponent_get_first_property(comp, kind))) {           \
2213                 prop = inner                                                    \
2214                         ? icalcomponent_get_first_property(inner, kind)         \
2215                         : NULL;                                                 \
2216                 if (prop) do {                                                  \
2217                         list = g_slist_prepend(list, prop);                     \
2218                 } while ((prop = icalcomponent_get_next_property(inner, kind)));\
2219         }else do {                                                              \
2220                 list = g_slist_prepend(list, prop);                             \
2221         } while ((prop = icalcomponent_get_next_property(comp, kind)));         \
2222 }
2223
2224 #define TO_UTF8(string) {                                                       \
2225         if (string && !g_utf8_validate(string, -1, NULL)) {                     \
2226                 gchar *tmp = conv_codeset_strdup(string,                        \
2227                                 charset ? charset:conv_get_locale_charset_str(),\
2228                                 CS_UTF_8);                                      \
2229                 g_free(string);                                                 \
2230                 string = tmp;                                                   \
2231         }                                                                       \
2232 }
2233
2234 VCalEvent *vcal_get_event_from_ical(const gchar *ical, const gchar *charset)
2235 {
2236         VCalEvent *event = NULL;
2237         gchar *int_ical = g_strdup(ical);
2238         icalcomponent *comp = icalcomponent_new_from_string(int_ical);
2239         icalcomponent *inner = NULL;
2240         icalcomponent *tzcomp = NULL;
2241         icalproperty *prop = NULL;
2242         GSList *list = NULL, *cur = NULL;
2243         gchar *uid = NULL;
2244         gchar *location = NULL;
2245         gchar *summary = NULL;
2246         gchar *dtstart = NULL;
2247         gchar *dtend = NULL;
2248         gchar *org_email = NULL, *org_name = NULL;
2249         gchar *description = NULL;
2250         gchar *url = NULL;
2251         gchar *tzid = NULL;
2252         gchar *recur = NULL;
2253         int sequence = 0;
2254         enum icalproperty_method method = ICAL_METHOD_REQUEST;
2255         enum icalcomponent_kind type = ICAL_VEVENT_COMPONENT;
2256         GSList *attendees = NULL;
2257         
2258         if (comp == NULL) {
2259                 g_free(int_ical);
2260                 return NULL;
2261         }
2262
2263         if ((inner = icalcomponent_get_inner(comp)) != NULL)
2264             type = icalcomponent_isa(inner);
2265
2266         GET_PROP(comp, prop, ICAL_UID_PROPERTY);
2267         if (prop) {
2268                 uid = g_strdup(icalproperty_get_uid(prop));
2269                 TO_UTF8(uid);
2270                 icalproperty_free(prop);
2271         }
2272         GET_PROP(comp, prop, ICAL_LOCATION_PROPERTY);
2273         if (prop) {
2274                 location = g_strdup(icalproperty_get_location(prop));
2275                 TO_UTF8(location);
2276                 icalproperty_free(prop);
2277         }
2278         GET_PROP(comp, prop, ICAL_SUMMARY_PROPERTY);
2279         if (prop) {
2280                 summary = g_strdup(icalproperty_get_summary(prop));
2281                 TO_UTF8(summary);
2282                 icalproperty_free(prop);
2283         }
2284
2285         convert_to_utc(comp);
2286
2287         GET_PROP(comp, prop, ICAL_DTSTART_PROPERTY);
2288         if (prop) {
2289                 dtstart = g_strdup(icaltime_as_ical_string(icalproperty_get_dtstart(prop)));
2290                 TO_UTF8(dtstart);
2291                 icalproperty_free(prop);
2292         }
2293         GET_PROP(comp, prop, ICAL_DTEND_PROPERTY);
2294         if (prop) {
2295                 dtend = g_strdup(icaltime_as_ical_string(icalproperty_get_dtend(prop)));
2296                 TO_UTF8(dtend);
2297                 icalproperty_free(prop);
2298         } else {
2299                 GET_PROP(comp, prop, ICAL_DURATION_PROPERTY);
2300                 if (prop) {
2301                         struct icaldurationtype duration = icalproperty_get_duration(prop);
2302                         struct icaltimetype itt;
2303                         icalproperty_free(prop);
2304                         GET_PROP(comp, prop, ICAL_DTSTART_PROPERTY);
2305                         if (prop) {
2306                                 itt = icalproperty_get_dtstart(prop);
2307                                 icalproperty_free(prop);
2308                                 dtend = g_strdup(icaltime_as_ical_string(icaltime_add(itt,duration)));
2309                                 TO_UTF8(dtend);
2310                         }
2311                 }
2312         }
2313         GET_PROP(comp, prop, ICAL_SEQUENCE_PROPERTY);
2314         if (prop) {
2315                 sequence = icalproperty_get_sequence(prop);
2316                 icalproperty_free(prop);
2317         }
2318         GET_PROP(comp, prop, ICAL_METHOD_PROPERTY);
2319         if (prop) {
2320                 method = icalproperty_get_method(prop);
2321                 icalproperty_free(prop);
2322         }
2323         GET_PROP(comp, prop, ICAL_ORGANIZER_PROPERTY);
2324         if (prop) {
2325                 org_email = get_email_from_property(prop);
2326                 TO_UTF8(org_email);
2327                 org_name = get_name_from_property(prop);
2328                 TO_UTF8(org_name);
2329                 icalproperty_free(prop);
2330         }
2331         GET_PROP(comp, prop, ICAL_DESCRIPTION_PROPERTY);
2332         if (prop) {
2333                 description = g_strdup(icalproperty_get_description(prop));
2334                 TO_UTF8(description);
2335                 icalproperty_free(prop);
2336         }
2337         GET_PROP(comp, prop, ICAL_URL_PROPERTY);
2338         if (prop) {
2339                 url = g_strdup(icalproperty_get_url(prop));
2340                 TO_UTF8(url);
2341                 icalproperty_free(prop);
2342         }
2343         GET_PROP(comp, prop, ICAL_TZID_PROPERTY);
2344         if (prop) {
2345                 tzid = g_strdup(icalproperty_get_tzid(prop));
2346                 TO_UTF8(tzid);
2347                 icalproperty_free(prop);
2348         }
2349         GET_PROP(comp, prop, ICAL_RRULE_PROPERTY);
2350         if (prop) {
2351                 struct icalrecurrencetype rrule = icalproperty_get_rrule(prop);
2352                 recur = g_strdup(icalrecurrencetype_as_string(&rrule));
2353                 TO_UTF8(recur);
2354                 icalproperty_free(prop);
2355         }
2356         GET_PROP_LIST(comp, list, ICAL_ATTENDEE_PROPERTY);
2357         for (cur = list; cur; cur = cur->next) {
2358                 enum icalparameter_partstat partstat = 0;
2359                 enum icalparameter_cutype cutype = 0;
2360                 icalparameter *param = NULL;
2361                 gchar *email = NULL;
2362                 gchar *name = NULL;
2363                 Answer *answer = NULL;
2364
2365                 prop = (icalproperty *)(cur->data);
2366
2367                 email = get_email_from_property(prop);
2368                 TO_UTF8(email);
2369                 name = get_name_from_property(prop);
2370                 TO_UTF8(name);
2371
2372                 param = icalproperty_get_first_parameter(prop, ICAL_PARTSTAT_PARAMETER);
2373                 if (param)
2374                         partstat = icalparameter_get_partstat(param);
2375
2376                 param = icalproperty_get_first_parameter(prop, ICAL_CUTYPE_PARAMETER);
2377                 if (param)
2378                         cutype= icalparameter_get_cutype(param);
2379                 
2380                 if (!partstat)
2381                         partstat = ICAL_PARTSTAT_NEEDSACTION;
2382                 if (!cutype)
2383                         cutype = ICAL_CUTYPE_INDIVIDUAL;
2384                 answer = answer_new(email, name, partstat, cutype);
2385                 attendees = g_slist_prepend(attendees, answer);
2386                 g_free(email);
2387                 g_free(name);
2388                 icalproperty_free(prop);
2389         }
2390         g_slist_free(list);
2391         
2392         event = vcal_manager_new_event  (uid, org_email, org_name,
2393                                          location, summary, description,
2394                                          dtstart, dtend, recur,
2395                                          tzid, url,
2396                                          method, sequence, type);
2397         event->answers = attendees;
2398         g_free(uid);
2399         g_free(location);
2400         g_free(summary);
2401         g_free(dtstart);
2402         g_free(dtend);
2403         g_free(org_email);
2404         g_free(org_name);
2405         g_free(description);
2406         g_free(url);
2407         g_free(tzid);
2408         g_free(recur);
2409         g_free(int_ical);
2410         icalcomponent_free(comp);
2411         return event;
2412 }
2413
2414 gboolean vcal_event_exists(const gchar *id)
2415 {
2416         MsgInfo *info = NULL;
2417         Folder *folder = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
2418         if (!folder)
2419                 return FALSE;
2420
2421         info = folder_item_get_msginfo_by_msgid(folder->inbox, id);
2422         if (info != NULL) {
2423                 procmsg_msginfo_free(&info);
2424                 return TRUE;
2425         }
2426         return FALSE;
2427 }
2428
2429 void vcal_foreach_event(gboolean (*cb_func)(const gchar *vevent))
2430 {
2431         GSList *list = vcal_folder_get_waiting_events();
2432         GSList *cur = NULL;
2433         if (!cb_func)
2434                 return;
2435         debug_print("calling cb_func...\n");
2436         for (cur = list; cur; cur = cur->next) {
2437                 VCalEvent *event = (VCalEvent *)cur->data;
2438                 gchar *tmp = vcal_get_event_as_ical_str(event);
2439                 if (tmp) {
2440                         debug_print(" ...for event %s\n", event->uid);
2441                         cb_func(tmp);
2442                 }
2443                 vcal_manager_free_event(event);
2444                 g_free(tmp);
2445         }
2446 }
2447
2448 /* please call vcalendar_refresh_folder_contents() after one or more 
2449  * calls to this function */
2450 gboolean vcal_delete_event(const gchar *id)
2451 {
2452         MsgInfo *info = NULL;
2453         Folder *folder = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
2454         if (!folder)
2455                 return FALSE;
2456
2457         info = folder_item_get_msginfo_by_msgid(folder->inbox, id);
2458         if (info != NULL) {
2459                 debug_print("removing event %s\n", id);
2460                 vcal_remove_event(folder, info);
2461                 procmsg_msginfo_free(&info);
2462                 folder_item_scan(folder->inbox);
2463                 return TRUE;
2464         }
2465         debug_print("not removing unexisting event %s\n", id);
2466         return FALSE;
2467 }
2468
2469 /* please call vcalendar_refresh_folder_contents() after one or more 
2470  * calls to this function */
2471 gchar* vcal_add_event(const gchar *vevent)
2472 {
2473         VCalEvent *event = vcal_get_event_from_ical(vevent, NULL);
2474         gchar *retVal = NULL;
2475         Folder *folder = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
2476         if (!folder)
2477                 return NULL;
2478
2479         if (event) {
2480                 if (vcal_event_exists(event->uid)) {
2481                         debug_print("event %s already exists\n", event->uid);
2482                         vcal_manager_free_event(event);
2483                         return retVal;
2484                 }
2485                 debug_print("adding event %s\n", event->uid);
2486                 if (!account_find_from_address(event->organizer, FALSE) &&
2487                     !vcal_manager_get_account_from_event(event)) {
2488                         PrefsAccount *account = account_get_default();
2489                         vcal_manager_update_answer(event, account->address, 
2490                                         account->name,
2491                                         ICAL_PARTSTAT_ACCEPTED, 
2492                                         ICAL_CUTYPE_INDIVIDUAL);
2493                         debug_print("can't find our accounts in event, adding default\n");
2494                 }
2495                 vcal_manager_save_event(event, TRUE);
2496                 folder_item_scan(folder->inbox);
2497                 retVal = vcal_get_event_as_ical_str(event);
2498                 vcal_manager_free_event(event);
2499         }
2500
2501         return retVal;
2502 }
2503
2504 /* please call vcalendar_refresh_folder_contents() after one or more 
2505  * calls to this function */
2506 gchar* vcal_update_event(const gchar *vevent)
2507 {
2508         VCalEvent *event = vcal_get_event_from_ical(vevent, NULL);
2509         gboolean r = FALSE;
2510         if (event) {
2511                 r = vcal_delete_event(event->uid);
2512                 vcal_manager_free_event(event);
2513                 if (r)
2514                         return vcal_add_event(vevent);
2515         }
2516         return NULL;
2517 }