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