2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2015 Colin Leroy <colin@colino.net> and
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
27 #include <glib/gi18n.h>
31 #include <sys/types.h>
35 #include <curl/curl.h>
36 #include <curl/curlver.h>
41 #include "procheader.h"
43 #include "folderview.h"
44 #include "folder_item_prefs.h"
45 #include "vcalendar.h"
46 #include "vcal_folder.h"
47 #include "vcal_prefs.h"
48 #include "vcal_manager.h"
49 #include "vcal_meeting_gtk.h"
50 #include "vcal_interface.h"
51 #include "prefs_account.h"
52 #include "prefs_common.h"
55 #include "inputdialog.h"
58 #include "alertpanel.h"
60 #include "mainwindow.h"
61 #include "statusbar.h"
63 #include "passwordstore.h"
65 #include "messageview.h"
70 #define VCAL_FOLDERITEM(item) ((VCalFolderItem *) item)
76 typedef struct _thread_data {
83 typedef struct _IcalFeedData {
85 gchar *pseudoevent_id;
88 typedef struct _VCalFolder VCalFolder;
89 typedef struct _VCalFolderItem VCalFolderItem;
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);
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,
104 static gchar *vcal_fetch_msg(Folder * folder, FolderItem * item,
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,
110 static FolderItem *vcal_create_folder(Folder * folder,
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);
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);
127 static void add_menuitems(GtkUIManager *ui_manager, FolderItem *item);
128 static void set_sensitivity(GtkUIManager *ui_manager, FolderItem *item);
130 static void update_subscription(const gchar *uri, gboolean verbose);
131 static void vcal_folder_set_batch (Folder *folder,
135 gboolean vcal_subscribe_uri(Folder *folder, const gchar *uri);
137 FolderClass vcal_class;
139 static GSList *created_files = NULL;
140 static GHashTable *hash_uids = NULL;
147 struct _VCalFolderItem
163 static char *vcal_popup_labels[] =
165 N_("_New meeting..."),
166 N_("_Export calendar..."),
167 N_("_Subscribe to webCal..."),
168 N_("_Unsubscribe..."),
170 N_("U_pdate subscriptions"),
177 static GtkActionEntry vcal_popup_entries[] =
179 {"FolderViewPopup/NewMeeting", NULL, NULL, NULL, NULL, G_CALLBACK(new_meeting_cb) },
180 {"FolderViewPopup/ExportCal", NULL, NULL, NULL, NULL, G_CALLBACK(export_cal_cb) },
182 {"FolderViewPopup/SubscribeCal", NULL, NULL, NULL, NULL, G_CALLBACK(subscribe_cal_cb) },
183 {"FolderViewPopup/UnsubscribeCal", NULL, NULL, NULL, NULL, G_CALLBACK(unsubscribe_cal_cb) },
185 {"FolderViewPopup/RenameFolder", NULL, NULL, NULL, NULL, G_CALLBACK(rename_cb) },
187 {"FolderViewPopup/CheckSubs", NULL, NULL, NULL, NULL, G_CALLBACK(check_subs_cb) },
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 },
197 static IcalFeedData *icalfeeddata_new(icalcomponent *evt, gchar *str)
199 IcalFeedData *data = g_new0(IcalFeedData, 1);
201 data->pseudoevent_id = g_strdup(str);
206 static void icalfeeddata_free(IcalFeedData *data)
208 g_free(data->pseudoevent_id);
210 icalcomponent_free(data->event);
214 static void slist_free_icalfeeddata(GSList *list)
217 IcalFeedData *data = (IcalFeedData *)list->data;
218 icalfeeddata_free(data);
223 static void vcal_fill_popup_menu_labels(void)
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]);
236 static FolderViewPopup vcal_popup =
241 G_N_ELEMENTS(vcal_popup_entries),
243 vcal_popup_radio_entries,
244 G_N_ELEMENTS(vcal_popup_radio_entries), 1, set_view_cb,
249 static void vcal_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
252 folder_item_set_xml(folder, item, tag);
253 gboolean found_cal_view_setting = FALSE;
255 for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
256 XMLAttr *attr = (XMLAttr *) cur->data;
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);
264 if (!strcmp(attr->name, "use_cal_view")) {
265 found_cal_view_setting = TRUE;
266 ((VCalFolderItem *)item)->use_cal_view = atoi(attr->value);
269 if (((VCalFolderItem *)item)->uri == NULL) {
270 /* give a path to inbox */
272 item->path = g_strdup(".meetings");
274 if (!found_cal_view_setting)
275 ((VCalFolderItem *)item)->use_cal_view = 1; /*week view */
279 static XMLTag *vcal_item_get_xml(Folder *folder, FolderItem *item)
283 tag = folder_item_get_xml(folder, item);
285 if (((VCalFolderItem *)item)->uri)
286 xml_tag_add_attr(tag, xml_attr_new("uri", ((VCalFolderItem *)item)->uri));
288 xml_tag_add_attr(tag, xml_attr_new_int("use_cal_view", ((VCalFolderItem *)item)->use_cal_view));
293 static gint vcal_rename_folder(Folder *folder, FolderItem *item,
299 item->name = g_strdup(name);
303 static void vcal_get_sort_type(Folder *folder, FolderSortKey *sort_key,
304 FolderSortType *sort_type)
307 *sort_key = SORT_BY_DATE;
310 static void vcal_item_opened(FolderItem *item)
313 time_t t = time(NULL);
315 localtime_r(&t, &tmdate);
319 tmdate = *localtime(&t);
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);
331 void vcal_folder_refresh_cal(FolderItem *item)
333 Folder *folder = folder_find_from_name ("vCalendar", vcal_folder_get_class());
334 if (item->folder != folder)
336 if (((VCalFolderItem *)(item))->dw)
337 refresh_day_win(((VCalFolderItem *)(item))->dw);
338 if (((VCalFolderItem *)(item))->mw)
339 refresh_month_win(((VCalFolderItem *)(item))->mw);
342 static void vcal_item_closed(FolderItem *item)
344 if (((VCalFolderItem *)(item))->dw) {
345 dw_close_window(((VCalFolderItem *)(item))->dw);
346 ((VCalFolderItem *)(item))->dw = NULL;
348 if (((VCalFolderItem *)(item))->mw) {
349 mw_close_window(((VCalFolderItem *)(item))->mw);
350 ((VCalFolderItem *)(item))->mw = NULL;
354 FolderClass *vcal_folder_get_class()
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";
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;
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;
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;
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");
403 static void vcal_folder_set_batch (Folder *folder,
407 VCalFolderItem *item = (VCalFolderItem *)_item;
409 g_return_if_fail(item != NULL);
411 if (item->batching == batch)
415 item->batching = TRUE;
416 debug_print("vcal switching to batch mode\n");
418 debug_print("vcal switching away from batch mode\n");
420 item->batching = FALSE;
422 vcal_folder_export(folder);
427 static Folder *vcal_folder_new(const gchar * name,
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);
437 return FOLDER(folder);
440 static void vcal_folder_destroy(Folder *_folder)
444 static FolderItem *vcal_item_new(Folder *folder)
446 VCalFolderItem *item;
447 item = g_new0(VCalFolderItem, 1);
448 item->use_cal_view = 1;
449 return (FolderItem *) item;
453 static void vcal_item_destroy(Folder *folder, FolderItem *_item)
455 VCalFolderItem *item = (VCalFolderItem *)_item;
456 g_return_if_fail(item != NULL);
460 static gchar *vcal_item_get_path(Folder *folder, FolderItem *item)
462 VCalFolderItem *fitem = (VCalFolderItem *)item;
463 if (fitem->uri == NULL)
464 return g_strdup(vcal_manager_get_event_path());
466 gchar *path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
467 "vcalendar", G_DIR_SEPARATOR_S,
473 static gint vcal_scan_tree(Folder *folder)
475 g_return_val_if_fail(folder != NULL, -1);
477 folder->outbox = NULL;
478 folder->draft = NULL;
479 folder->queue = NULL;
480 folder->trash = NULL;
482 debug_print("scanning tree\n");
483 vcal_create_tree(folder);
488 gboolean manual_update = TRUE;
490 static gint feed_fetch(FolderItem *fitem, MsgNumberList ** list, gboolean *old_uids_valid)
492 VCalFolderItem *item = (VCalFolderItem *)fitem;
493 icalcomponent *evt = NULL;
494 icalcomponent_kind type = ICAL_VEVENT_COMPONENT;
496 gint past_msg = -1, today_msg = -1, tomorrow_msg = -1,
497 thisweek_msg = -1, later_msg = -1;
499 debug_print("fetching\n");
502 debug_print("no uri!\n");
506 update_subscription(item->uri, TRUE);
508 *old_uids_valid = FALSE;
512 evt = icalcomponent_get_first_component(
513 item->cal, ICAL_VEVENT_COMPONENT);
515 evt = icalcomponent_get_first_component(
516 item->cal, ICAL_VTODO_COMPONENT);
518 type = ICAL_VTODO_COMPONENT;
521 debug_print("no cal!\n");
524 debug_print("no event\n");
527 g_slist_free(item->numlist);
528 item->numlist = NULL;
532 slist_free_icalfeeddata(item->evtlist);
533 g_slist_free(item->evtlist);
534 item->evtlist = NULL;
539 icalproperty *rprop = icalcomponent_get_first_property(evt, ICAL_RRULE_PROPERTY);
540 struct icalrecurrencetype recur;
541 struct icaltimetype next;
542 icalrecur_iterator* ritr = NULL;
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);
551 ritr = icalrecur_iterator_new(recur, icalproperty_get_dtstart(prop));
552 next = icalrecur_iterator_next(ritr); /* skip first one */
558 prop = icalcomponent_get_first_property(evt, ICAL_UID_PROPERTY);
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);
565 orig_uid = g_strdup(uid);
568 item->numlist = g_slist_prepend(item->numlist, GINT_TO_POINTER(num));
569 item->evtlist = g_slist_prepend(item->evtlist, data);
571 debug_print("add %d : %s\n", num, uid);
574 prop = icalcomponent_get_first_property(evt, ICAL_DTSTART_PROPERTY);
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);
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);
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);
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);
607 item->evtlist = g_slist_prepend(item->evtlist, data);
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);
616 icalcomponent_remove_property(evt, prop);
617 icalproperty_free(prop);
619 prop = icalcomponent_get_first_property(evt, ICAL_DTSTART_PROPERTY);
621 dtstart = icalproperty_get_dtstart(prop);
622 prop = icalcomponent_get_first_property(evt, ICAL_DTEND_PROPERTY);
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);
632 prop = icalcomponent_get_first_property(evt, ICAL_DTEND_PROPERTY);
634 icalproperty_set_dtend(prop, icaltime_add(next, ical_dur));
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);
644 icalcomponent_free(evt);
650 debug_print("no uid!\n");
653 icalproperty_free(rprop);
656 icalrecur_iterator_free(ritr);
659 evt = icalcomponent_get_next_component(
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));
666 item->evtlist = g_slist_prepend(item->evtlist, data);
668 item->numlist = g_slist_reverse(item->numlist);
669 item->evtlist = g_slist_reverse(item->evtlist);
671 *list = item->numlist ? g_slist_copy(item->numlist) : NULL;
672 debug_print("return %d\n", num);
676 #define VCAL_FOLDER_ADD_EVENT(event) \
679 *list = g_slist_prepend(*list, GINT_TO_POINTER(n_msg)); \
680 debug_print("add %d %s\n", n_msg, event->uid); \
682 days = event_to_today(event, 0); \
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)); \
707 GSList *vcal_get_events_list(FolderItem *item)
711 GSList *events = NULL;
712 GError *error = NULL;
714 if (item != item->folder->inbox) {
715 GSList *subs = vcal_folder_get_webcal_events_for_folder(item);
717 for (cur = subs; cur; cur = cur->next) {
718 /* Don't free that, it's done when subscriptions are
720 icalcomponent *ical = (icalcomponent *)cur->data;
721 VCalEvent *event = vcal_get_event_from_ical(
722 icalcomponent_as_ical_string(ical), NULL);
723 events = g_slist_prepend(events, event);
729 dp = g_dir_open(vcal_manager_get_event_path(), 0, &error);
732 debug_print("couldn't open dir '%s': %s (%d)\n",
733 vcal_manager_get_event_path(), error->message, error->code);
738 while ((d = g_dir_read_name(dp)) != NULL) {
739 VCalEvent *event = NULL;
740 if (d[0] == '.' || strstr(d, ".bak")
741 || !strcmp(d, "internal.ics")
742 || !strcmp(d, "internal.ifb")
743 || !strcmp(d, "multisync"))
746 event = vcal_manager_load_event(d);
749 if (event->rec_occurence) {
750 vcal_manager_free_event(event);
755 if (event && event->method != ICAL_METHOD_CANCEL) {
756 PrefsAccount *account = vcal_manager_get_account_from_event(event);
757 enum icalparameter_partstat status =
758 account ? vcal_manager_get_reply_for_attendee(event, account->address): ICAL_PARTSTAT_NEEDSACTION;
759 if (status == ICAL_PARTSTAT_ACCEPTED
760 || status == ICAL_PARTSTAT_TENTATIVE) {
761 events = g_slist_prepend(events, event);
763 vcal_manager_free_event(event);
766 if ((status == ICAL_PARTSTAT_ACCEPTED
767 || status == ICAL_PARTSTAT_TENTATIVE)
768 && event->recur && *(event->recur)) {
769 struct icalrecurrencetype recur;
770 struct icaltimetype dtstart;
771 struct icaltimetype next;
772 icalrecur_iterator* ritr;
773 time_t duration = (time_t) NULL;
774 struct icaldurationtype ical_dur;
777 debug_print("dumping recurring events from main event %s\n", d);
778 recur = icalrecurrencetype_from_string(event->recur);
779 dtstart = icaltime_from_string(event->dtstart);
781 duration = icaltime_as_timet(icaltime_from_string(event->dtend))
782 - icaltime_as_timet(icaltime_from_string(event->dtstart));
784 ical_dur = icaldurationtype_from_int(duration);
786 ritr = icalrecur_iterator_new(recur, dtstart);
788 next = icalrecur_iterator_next(ritr); /* skip first one */
789 if (!icaltime_is_null_time(next))
790 next = icalrecur_iterator_next(ritr);
791 debug_print("next time is %snull\n", icaltime_is_null_time(next)?"":"not ");
792 while (!icaltime_is_null_time(next) && i < 100) {
793 gchar *new_start = NULL, *new_end = NULL;
794 VCalEvent *nevent = NULL;
795 gchar *uid = g_strdup_printf("%s-%d", event->uid, i);
796 new_start = icaltime_as_ical_string(next);
797 new_end = icaltime_as_ical_string(
798 icaltime_add(next, ical_dur));
799 debug_print("adding with start/end %s:%s\n", new_start, new_end);
800 nevent = vcal_manager_new_event(uid, event->organizer, event->orgname,
801 event->location, event->summary, event->description,
802 new_start, new_end, NULL,
803 event->tzid, event->url, event->method,
804 event->sequence, event->type);
806 vcal_manager_copy_attendees(event, nevent);
807 nevent->rec_occurence = TRUE;
808 vcal_manager_save_event(nevent, FALSE);
809 account = vcal_manager_get_account_from_event(event);
811 account ? vcal_manager_get_reply_for_attendee(event, account->address): ICAL_PARTSTAT_NEEDSACTION;
812 if (status == ICAL_PARTSTAT_ACCEPTED
813 || status == ICAL_PARTSTAT_TENTATIVE) {
814 events = g_slist_prepend(events, nevent);
816 vcal_manager_free_event(nevent);
818 next = icalrecur_iterator_next(ritr);
819 debug_print("next time is %snull\n", icaltime_is_null_time(next)?"":"not ");
822 icalrecur_iterator_free(ritr);
825 vcal_manager_free_event(event);
829 return g_slist_reverse(events);
832 static gint vcal_get_num_list(Folder *folder, FolderItem *item,
833 MsgNumberList ** list, gboolean *old_uids_valid)
836 gint past_msg = -1, today_msg = -1, tomorrow_msg = -1,
837 thisweek_msg = -1, later_msg = -1;
838 GSList *events = NULL, *cur;
840 g_return_val_if_fail (*list == NULL, 0); /* we expect a NULL list */
842 debug_print(" num for %s\n", ((VCalFolderItem *)item)->uri ? ((VCalFolderItem *)item)->uri:"(null)");
844 *old_uids_valid = FALSE;
846 if (((VCalFolderItem *)item)->uri)
847 return feed_fetch(item, list, old_uids_valid);
849 events = vcal_get_events_list(item);
851 debug_print("get_num_list\n");
853 if (hash_uids != NULL)
854 g_hash_table_destroy(hash_uids);
856 hash_uids = g_hash_table_new_full(g_direct_hash, g_direct_equal,
859 for (cur = events; cur; cur = cur->next) {
860 VCalEvent *event = (VCalEvent *)cur->data;
864 g_hash_table_insert(hash_uids, GINT_TO_POINTER(n_msg), g_strdup(event->uid));
866 if (event->rec_occurence) {
867 vcal_manager_free_event(event);
871 if (event->method != ICAL_METHOD_CANCEL) {
873 VCAL_FOLDER_ADD_EVENT(event);
876 vcal_manager_free_event(event);
881 if (today_msg == -1) {
882 *list = g_slist_prepend(*list, GINT_TO_POINTER(n_msg));
884 g_hash_table_insert(hash_uids, GINT_TO_POINTER(today_msg), g_strdup(EVENT_TODAY_ID));
887 g_slist_free(events);
888 vcal_folder_export(folder);
890 vcal_set_mtime(folder, item);
892 *list = g_slist_reverse(*list);
894 return g_slist_length(*list);
897 static MsgInfo *vcal_parse_msg(const gchar *file, FolderItem *item, int num)
899 MsgInfo *msginfo = NULL;
902 debug_print("parse_msg\n");
904 flags.perm_flags = 0;
906 msginfo = procheader_parse_file(file, flags, TRUE, TRUE);
908 msginfo->msgnum = num;
909 msginfo->folder = item;
913 static MsgInfo *vcal_get_msginfo(Folder * folder,
914 FolderItem * item, gint num)
916 MsgInfo *msginfo = NULL;
919 debug_print("get_msginfo\n");
921 g_return_val_if_fail(item != NULL, NULL);
922 g_return_val_if_fail(num > 0, NULL);
924 file = vcal_fetch_msg(folder, item, num);
930 msginfo = vcal_parse_msg(file, item, num);
933 msginfo->flags.perm_flags = 0;
934 msginfo->flags.tmp_flags = 0;
936 vcal_change_flags(NULL, NULL, msginfo, 0);
938 debug_print(" adding %d\n", num);
943 debug_print(" got msginfo %p\n", msginfo);
948 static gchar *feed_fetch_item(FolderItem * fitem, gint num)
950 gchar *filename = NULL;
951 VCalFolderItem *item = (VCalFolderItem *)fitem;
954 IcalFeedData *data = NULL;
956 if (!item->numlist) {
957 folder_item_scan_full(fitem, FALSE);
959 if (!item->numlist) {
960 debug_print("numlist null\n");
964 ncur = item->numlist;
965 ecur = item->evtlist;
968 if (!ncur || !ecur) {
969 debug_print("list short end (%d to %d) %d,%d\n", i, num, ncur!=NULL, ecur!=NULL);
977 data = (IcalFeedData *)ecur->data;
984 filename = vcal_manager_icalevent_dump(data->event, fitem->name, NULL);
985 else if (data->pseudoevent_id) {
986 filename = vcal_manager_dateevent_dump(data->pseudoevent_id, fitem);
987 created_files = g_slist_prepend(created_files, g_strdup(filename));
989 debug_print("no event\n");
993 debug_print("feed item dump to %s\n", filename);
997 static gchar *vcal_fetch_msg(Folder * folder, FolderItem * item,
1000 gchar *filename = NULL;
1001 const gchar *uid = NULL;
1003 debug_print(" fetch for %s %d\n", (((VCalFolderItem *)item)->uri ? ((VCalFolderItem *)item)->uri:"(null)"), num);
1004 if (((VCalFolderItem *)item)->uri)
1005 return feed_fetch_item(item, num);
1009 folder_item_scan_full(item, FALSE);
1010 uid = g_hash_table_lookup(hash_uids, GINT_TO_POINTER(num));
1013 (!strcmp(uid, EVENT_PAST_ID) ||
1014 !strcmp(uid, EVENT_TODAY_ID) ||
1015 !strcmp(uid, EVENT_TOMORROW_ID) ||
1016 !strcmp(uid, EVENT_THISWEEK_ID) ||
1017 !strcmp(uid, EVENT_LATER_ID))) {
1018 filename = vcal_manager_dateevent_dump(uid, item);
1020 VCalEvent *event = NULL;
1021 event = vcal_manager_load_event(uid);
1023 filename = vcal_manager_event_dump(event, FALSE, TRUE, NULL, FALSE);
1026 created_files = g_slist_prepend(created_files, g_strdup(filename));
1029 vcal_manager_free_event(event);
1035 static gint vcal_add_msg(Folder *folder, FolderItem *_dest, const gchar *file, MsgFlags *flags)
1037 gchar *contents = file_read_to_str(file);
1039 vcal_add_event(contents);
1045 static void vcal_remove_event (Folder *folder, MsgInfo *msginfo);
1047 static gint vcal_remove_msg(Folder *folder, FolderItem *_item, gint num)
1049 MsgInfo *msginfo = folder_item_get_msginfo(_item, num);
1054 if (_item == folder->inbox)
1055 vcal_remove_event(folder, msginfo);
1057 procmsg_msginfo_free(&msginfo);
1061 static FolderItem *vcal_create_folder(Folder * folder,
1062 FolderItem * parent,
1066 FolderItem *newitem = NULL;
1067 debug_print("creating new vcal folder\n");
1069 path = g_strconcat((parent->path != NULL) ? parent->path : "", ".", name, NULL);
1070 newitem = folder_item_new(folder, name, path);
1071 folder_item_append(parent, newitem);
1077 static gint vcal_create_tree(Folder *folder)
1079 FolderItem *rootitem, *inboxitem;
1080 GNode *rootnode, *inboxnode;
1082 if (!folder->node) {
1083 rootitem = folder_item_new(folder, folder->name, NULL);
1084 rootitem->folder = folder;
1085 rootnode = g_node_new(rootitem);
1086 folder->node = rootnode;
1087 rootitem->node = rootnode;
1089 rootitem = FOLDER_ITEM(folder->node->data);
1090 rootnode = folder->node;
1093 /* Add inbox folder */
1094 if (!folder->inbox) {
1095 inboxitem = folder_item_new(folder, _("Meetings"), ".meetings");
1096 inboxitem->folder = folder;
1097 inboxitem->stype = F_INBOX;
1098 inboxnode = g_node_new(inboxitem);
1099 inboxitem->node = inboxnode;
1100 folder->inbox = inboxitem;
1101 g_node_append(rootnode, inboxnode);
1103 g_free(folder->inbox->path);
1104 folder->inbox->path = g_strdup(".meetings");
1107 debug_print("created new vcal tree\n");
1111 static gint vcal_remove_folder(Folder *folder, FolderItem *fitem)
1113 VCalFolderItem *item = (VCalFolderItem *)fitem;
1123 folder_item_remove(fitem);
1128 static gboolean vcal_scan_required(Folder *folder, FolderItem *item)
1131 VCalFolderItem *vitem = (VCalFolderItem *)item;
1133 g_return_val_if_fail(item != NULL, FALSE);
1137 } else if (g_stat(vcal_manager_get_event_path(), &s) < 0) {
1139 } else if ((s.st_mtime > item->mtime) &&
1140 (s.st_mtime - 3600 != item->mtime)) {
1146 static gint vcal_folder_lock_count = 0;
1148 static void vcal_set_mtime(Folder *folder, FolderItem *item)
1151 gchar *path = folder_item_get_path(item);
1153 if (folder->inbox != item)
1156 g_return_if_fail(path != NULL);
1158 if (g_stat(path, &s) < 0) {
1159 FILE_OP_ERROR(path, "stat");
1164 item->mtime = s.st_mtime;
1165 debug_print("VCAL: forced mtime of %s to %lld\n",
1166 item->name?item->name:"(null)", (long long)item->mtime);
1170 void vcal_folder_export(Folder *folder)
1172 FolderItem *item = folder?folder->inbox:NULL;
1173 gboolean need_scan = folder?vcal_scan_required(folder, item):TRUE;
1174 gchar *export_pass = NULL;
1175 gchar *export_freebusy_pass = NULL;
1177 if (vcal_folder_lock_count) /* blocked */
1179 vcal_folder_lock_count++;
1181 export_pass = vcal_passwd_get("export");
1182 export_freebusy_pass = vcal_passwd_get("export_freebusy");
1184 if (vcal_meeting_export_calendar(vcalprefs.export_path,
1185 vcalprefs.export_user,
1188 debug_print("exporting calendar\n");
1189 if (vcalprefs.export_enable &&
1190 vcalprefs.export_command &&
1191 strlen(vcalprefs.export_command))
1192 execute_command_line(
1193 vcalprefs.export_command, TRUE, NULL);
1195 if (export_pass != NULL) {
1196 memset(export_pass, 0, strlen(export_pass));
1198 g_free(export_pass);
1199 if (vcal_meeting_export_freebusy(vcalprefs.export_freebusy_path,
1200 vcalprefs.export_freebusy_user,
1201 export_freebusy_pass)) {
1202 debug_print("exporting freebusy\n");
1203 if (vcalprefs.export_freebusy_enable &&
1204 vcalprefs.export_freebusy_command &&
1205 strlen(vcalprefs.export_freebusy_command))
1206 execute_command_line(
1207 vcalprefs.export_freebusy_command, TRUE, NULL);
1209 if (export_freebusy_pass != NULL) {
1210 memset(export_freebusy_pass, 0, strlen(export_freebusy_pass));
1212 g_free(export_freebusy_pass);
1213 vcal_folder_lock_count--;
1214 if (!need_scan && folder) {
1215 vcal_set_mtime(folder, folder->inbox);
1219 static void vcal_remove_event (Folder *folder, MsgInfo *msginfo)
1221 const gchar *uid = msginfo->msgid;
1222 VCalFolderItem *item = (VCalFolderItem *)msginfo->folder;
1225 gchar *file = vcal_manager_get_event_file(uid);
1230 if (!item || !item->batching)
1231 vcal_folder_export(folder);
1237 static void vcal_change_flags(Folder *folder, FolderItem *_item, MsgInfo *msginfo, MsgPermFlags newflags)
1241 if (newflags & MSG_DELETED) {
1242 /* delete the stuff */
1243 msginfo->flags.perm_flags |= MSG_DELETED;
1244 vcal_remove_event(folder, msginfo);
1248 /* accept the rest */
1249 msginfo->flags.perm_flags = newflags;
1252 msginfo->flags.perm_flags &= ~MSG_CLABEL_FLAG_MASK;
1254 date = event_to_today(NULL, msginfo->date_t);
1259 msginfo->flags.perm_flags |= MSG_COLORLABEL_TO_FLAGS(2); /* Red */
1261 case EVENT_TOMORROW:
1263 case EVENT_THISWEEK:
1268 if (msginfo->msgid) {
1269 if (!strcmp(msginfo->msgid, EVENT_TODAY_ID) ||
1270 !strcmp(msginfo->msgid, EVENT_TOMORROW_ID))
1271 msginfo->flags.perm_flags |= MSG_MARKED;
1275 void vcal_folder_gtk_init(void)
1277 vcal_fill_popup_menu_labels();
1279 folderview_register_popup(&vcal_popup);
1282 void vcal_folder_gtk_done(void)
1284 GSList *cur = created_files;
1286 gchar *file = (gchar *)cur->data;
1290 debug_print("removing %s\n", file);
1294 g_slist_free(created_files);
1295 folderview_unregister_popup(&vcal_popup);
1298 static void add_menuitems(GtkUIManager *ui_manager, FolderItem *item)
1300 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "NewMeeting", "FolderViewPopup/NewMeeting", GTK_UI_MANAGER_MENUITEM)
1301 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "ExportCal", "FolderViewPopup/ExportCal", GTK_UI_MANAGER_MENUITEM)
1302 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorVcal1", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
1303 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SubscribeCal", "FolderViewPopup/SubscribeCal", GTK_UI_MANAGER_MENUITEM)
1304 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "UnsubscribeCal", "FolderViewPopup/UnsubscribeCal", GTK_UI_MANAGER_MENUITEM)
1305 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorVcal2", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
1306 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RenameFolder", "FolderViewPopup/RenameFolder", GTK_UI_MANAGER_MENUITEM)
1307 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorVcal3", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
1308 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CheckSubs", "FolderViewPopup/CheckSubs", GTK_UI_MANAGER_MENUITEM)
1309 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorVcal4", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
1310 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "ListView", "FolderViewPopup/ListView", GTK_UI_MANAGER_MENUITEM)
1311 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "WeekView", "FolderViewPopup/WeekView", GTK_UI_MANAGER_MENUITEM)
1312 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "MonthView", "FolderViewPopup/MonthView", GTK_UI_MANAGER_MENUITEM)
1313 MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorVcal5", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
1316 static gboolean setting_sensitivity = FALSE;
1317 static void set_sensitivity(GtkUIManager *ui_manager, FolderItem *fitem)
1319 VCalFolderItem *item = (VCalFolderItem *)fitem;
1321 #define SET_SENS(name, sens) \
1322 cm_menu_set_sensitive_full(ui_manager, "Popup/"name, sens)
1324 setting_sensitivity = TRUE;
1326 cm_toggle_menu_set_active_full(ui_manager, "Popup/FolderViewPopup/ListView", (item->use_cal_view == 0));
1327 cm_toggle_menu_set_active_full(ui_manager, "Popup/FolderViewPopup/WeekView", (item->use_cal_view == 1));
1328 cm_toggle_menu_set_active_full(ui_manager, "Popup/FolderViewPopup/MonthView", (item->use_cal_view == 2));
1329 SET_SENS("FolderViewPopup/NewMeeting", item->uri == NULL);
1330 SET_SENS("FolderViewPopup/ExportCal", TRUE);
1331 SET_SENS("FolderViewPopup/SubscribeCal", item->uri == NULL);
1332 SET_SENS("FolderViewPopup/UnsubscribeCal", item->uri != NULL);
1333 SET_SENS("FolderViewPopup/RenameFolder", folder_item_parent(fitem) != NULL);
1334 SET_SENS("FolderViewPopup/CheckSubs", TRUE);
1335 SET_SENS("FolderViewPopup/ListView", folder_item_parent(fitem) != NULL);
1336 SET_SENS("FolderViewPopup/WeekView", folder_item_parent(fitem) != NULL);
1337 SET_SENS("FolderViewPopup/MonthView", folder_item_parent(fitem) != NULL);
1338 setting_sensitivity = FALSE;
1342 static void new_meeting_cb(GtkAction *action, gpointer data)
1344 debug_print("new_meeting_cb\n");
1345 vcal_meeting_create(NULL);
1348 GSList * vcal_folder_get_waiting_events(void)
1350 Folder *folder = folder_find_from_name ("vCalendar", vcal_folder_get_class());
1351 return vcal_get_events_list(folder->inbox);
1354 typedef struct _get_webcal_data {
1359 static gboolean get_webcal_events_func(GNode *node, gpointer user_data)
1361 FolderItem *item = node->data;
1362 GetWebcalData *data = user_data;
1363 gboolean dummy = FALSE;
1364 GSList *list = NULL, *cur = NULL;
1366 if (data->item && data->item != item)
1369 feed_fetch(item, &list, &dummy);
1373 for (cur = ((VCalFolderItem *)item)->evtlist; cur; cur = cur->next) {
1374 IcalFeedData *fdata = (IcalFeedData *)cur->data;
1376 data->list = g_slist_prepend(data->list, fdata->event);
1381 GSList * vcal_folder_get_webcal_events(void)
1383 GetWebcalData *data = g_new0(GetWebcalData, 1);
1384 Folder *folder = folder_find_from_name ("vCalendar", vcal_folder_get_class());
1385 GSList *list = NULL;
1387 g_node_traverse(folder->node, G_PRE_ORDER,
1388 G_TRAVERSE_ALL, -1, get_webcal_events_func, data);
1393 return g_slist_reverse(list);
1396 static gboolean vcal_free_data_func(GNode *node, gpointer user_data)
1398 VCalFolderItem *item = node->data;
1401 icalcomponent_free(item->cal);
1404 if (item->numlist) {
1405 g_slist_free(item->numlist);
1406 item->numlist = NULL;
1409 if (item->evtlist) {
1410 slist_free_icalfeeddata(item->evtlist);
1411 g_slist_free(item->evtlist);
1412 item->evtlist = NULL;
1418 void vcal_folder_free_data(void)
1420 Folder *folder = folder_find_from_name ("vCalendar", vcal_folder_get_class());
1422 g_node_traverse(folder->node, G_PRE_ORDER,
1423 G_TRAVERSE_ALL, -1, vcal_free_data_func, NULL);
1426 GSList * vcal_folder_get_webcal_events_for_folder(FolderItem *item)
1428 GetWebcalData *data = g_new0(GetWebcalData, 1);
1429 Folder *folder = folder_find_from_name ("vCalendar", vcal_folder_get_class());
1430 GSList *list = NULL;
1432 g_node_traverse(folder->node, G_PRE_ORDER,
1433 G_TRAVERSE_ALL, -1, get_webcal_events_func, data);
1438 return g_slist_reverse(list);
1441 gchar* get_item_event_list_for_date(FolderItem *item, EventTime date)
1443 GSList *strs = NULL;
1445 gchar *result = NULL;
1446 gchar *datestr = NULL;
1448 if (((VCalFolderItem *)item)->uri) {
1449 for (cur = ((VCalFolderItem *)item)->evtlist; cur; cur = cur->next) {
1450 IcalFeedData *fdata = (IcalFeedData *)cur->data;
1452 struct icaltimetype itt;
1453 gchar *summary = NULL;
1457 prop = icalcomponent_get_first_property((icalcomponent *)fdata->event, ICAL_DTSTART_PROPERTY);
1461 itt = icalproperty_get_dtstart(prop);
1462 days = event_to_today(NULL, icaltime_as_timet(itt));
1465 prop = icalcomponent_get_first_property((icalcomponent *)fdata->event, ICAL_SUMMARY_PROPERTY);
1467 if (!g_utf8_validate(icalproperty_get_summary(prop), -1, NULL))
1468 summary = conv_codeset_strdup(icalproperty_get_summary(prop),
1469 conv_get_locale_charset_str(), CS_UTF_8);
1471 summary = g_strdup(icalproperty_get_summary(prop));
1473 summary = g_strdup("-");
1475 strs = g_slist_prepend(strs, summary);
1478 GSList *evtlist = vcal_folder_get_waiting_events();
1479 for (cur = evtlist; cur; cur = cur->next) {
1480 VCalEvent *event = (VCalEvent *)cur->data;
1482 days = event_to_today(event, 0);
1483 gchar *summary = NULL;
1485 summary = g_strdup(event->summary);
1486 strs = g_slist_prepend(strs, summary);
1488 vcal_manager_free_event(event);
1494 datestr=_("in the past");
1499 case EVENT_TOMORROW:
1500 datestr=_("tomorrow");
1502 case EVENT_THISWEEK:
1503 datestr=_("this week");
1510 result = g_strdup_printf(_("\nThese are the events planned %s:\n"),
1511 datestr?datestr:"never");
1513 strs = g_slist_reverse(strs);
1514 for (cur = strs; cur; cur = cur->next) {
1515 int e_len = strlen(result);
1516 int n_len = strlen((gchar *)cur->data);
1518 result = g_realloc(result, e_len+n_len+4);
1519 *(result+e_len) = '\n';
1520 strcpy(result+e_len+1, "- ");
1521 strcpy(result+e_len+3, (gchar *)cur->data);
1523 result = g_realloc(result, e_len+n_len+3);
1524 strcpy(result+e_len, "- ");
1525 strcpy(result+e_len+2, (gchar *)cur->data);
1528 slist_free_strings(strs);
1533 static void export_cal_cb(GtkAction *action, gpointer data)
1535 vcal_meeting_export_calendar(NULL, NULL, NULL, FALSE);
1542 static size_t curl_recv(void *buf, size_t size, size_t nmemb, void *stream)
1544 struct CBuf *buffer = (struct CBuf *)stream;
1546 gchar tmpbuf[size*nmemb + 1];
1548 memcpy(tmpbuf, buf, size*nmemb);
1549 tmpbuf[size*nmemb] = '\0';
1552 tmp = g_strconcat(buffer->str, tmpbuf, NULL);
1553 g_free(buffer->str);
1556 buffer->str = g_strdup(tmpbuf);
1562 void *url_read_thread(void *data)
1564 thread_data *td = (thread_data *)data;
1566 CURL *curl_ctx = NULL;
1568 struct CBuf buffer = { NULL };
1569 gchar *t_url = (gchar *)td->url;
1571 while (*t_url == ' ')
1573 if (strchr(t_url, ' '))
1574 *(strchr(t_url, ' ')) = '\0';
1577 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
1578 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
1581 curl_ctx = curl_easy_init();
1583 curl_easy_setopt(curl_ctx, CURLOPT_URL, t_url);
1584 curl_easy_setopt(curl_ctx, CURLOPT_WRITEFUNCTION, curl_recv);
1585 curl_easy_setopt(curl_ctx, CURLOPT_WRITEDATA, &buffer);
1586 curl_easy_setopt(curl_ctx, CURLOPT_TIMEOUT, prefs_common_get_prefs()->io_timeout_secs);
1587 curl_easy_setopt(curl_ctx, CURLOPT_NOSIGNAL, 1);
1588 #if LIBCURL_VERSION_NUM >= 0x070a00
1589 if(vcalprefs.ssl_verify_peer == FALSE) {
1590 curl_easy_setopt(curl_ctx, CURLOPT_SSL_VERIFYPEER, 0);
1591 curl_easy_setopt(curl_ctx, CURLOPT_SSL_VERIFYHOST, 0);
1594 curl_easy_setopt(curl_ctx, CURLOPT_USERAGENT,
1595 "Claws Mail vCalendar plugin "
1596 "(" PLUGINS_URI ")");
1597 curl_easy_setopt(curl_ctx, CURLOPT_FOLLOWLOCATION, 1);
1598 res = curl_easy_perform(curl_ctx);
1601 debug_print("res %d %s\n", res, curl_easy_strerror(res));
1602 td->error = g_strdup(curl_easy_strerror(res));
1604 if(res == CURLE_OPERATION_TIMEOUTED)
1605 log_error(LOG_PROTOCOL, _("Timeout (%d seconds) connecting to %s\n"),
1606 prefs_common_get_prefs()->io_timeout_secs, t_url);
1609 curl_easy_getinfo(curl_ctx, CURLINFO_RESPONSE_CODE, &response_code);
1610 if( response_code >= 400 && response_code < 500 ) {
1611 debug_print("VCalendar: got %ld\n", response_code);
1612 switch(response_code) {
1614 td->error = g_strdup(_("401 (Authorisation required)"));
1617 td->error = g_strdup(_("403 (Unauthorised)"));
1620 td->error = g_strdup(_("404 (Not found)"));
1623 td->error = g_strdup_printf(_("Error %ld"), response_code);
1627 curl_easy_cleanup(curl_ctx);
1629 td->result = g_strdup(buffer.str);
1633 td->done = TRUE; /* let the caller thread join() */
1634 return GINT_TO_POINTER(0);
1637 gchar *vcal_curl_read(const char *url, const gchar *label, gboolean verbose,
1638 void (*callback)(const gchar *url, gchar *data, gboolean verbose, gchar *error))
1647 gchar *error = NULL;
1649 td = g_new0(thread_data, 1);
1656 STATUSBAR_PUSH(mainwindow_get_mainwindow(), label);
1659 if (pthread_attr_init(&pta) != 0 ||
1660 pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE) != 0 ||
1661 pthread_create(&pt, &pta,
1662 url_read_thread, td) != 0) {
1663 url_read_thread(td);
1669 pthread_join(pt, &res);
1671 url_read_thread(td);
1674 result = td->result;
1678 STATUSBAR_POP(mainwindow_get_mainwindow());
1681 callback(url, result, verbose, error);
1690 gboolean vcal_curl_put(gchar *url, FILE *fp, gint filesize, const gchar *user, const gchar *pass)
1692 gboolean res = TRUE;
1693 CURL *curl_ctx = curl_easy_init();
1694 long response_code = 0;
1696 gchar *userpwd = NULL;
1698 struct curl_slist * headers = curl_slist_append(NULL,
1699 "Content-Type: text/calendar; charset=\"utf-8\"" );
1701 while (*t_url == ' ')
1703 if (strchr(t_url, ' '))
1704 *(strchr(t_url, ' ')) = '\0';
1706 if (user && pass && *user && *pass) {
1707 userpwd = g_strdup_printf("%s:%s",user,pass);
1708 curl_easy_setopt(curl_ctx, CURLOPT_USERPWD, userpwd);
1710 curl_easy_setopt(curl_ctx, CURLOPT_URL, t_url);
1711 curl_easy_setopt(curl_ctx, CURLOPT_UPLOAD, 1);
1712 curl_easy_setopt(curl_ctx, CURLOPT_READFUNCTION, NULL);
1713 curl_easy_setopt(curl_ctx, CURLOPT_READDATA, fp);
1714 curl_easy_setopt(curl_ctx, CURLOPT_HTTPHEADER, headers);
1715 #if LIBCURL_VERSION_NUM >= 0x070a00
1716 if(vcalprefs.ssl_verify_peer == FALSE) {
1717 curl_easy_setopt(curl_ctx, CURLOPT_SSL_VERIFYPEER, 0);
1718 curl_easy_setopt(curl_ctx, CURLOPT_SSL_VERIFYHOST, 0);
1721 curl_easy_setopt(curl_ctx, CURLOPT_USERAGENT,
1722 "Claws Mail vCalendar plugin "
1723 "(" PLUGINS_URI ")");
1724 curl_easy_setopt(curl_ctx, CURLOPT_INFILESIZE, filesize);
1725 res = curl_easy_perform(curl_ctx);
1729 debug_print("res %d %s\n", res, curl_easy_strerror(res));
1734 curl_easy_getinfo(curl_ctx, CURLINFO_RESPONSE_CODE, &response_code);
1735 if (response_code < 200 || response_code >= 300) {
1736 g_warning("Can't export calendar, got code %ld", response_code);
1739 curl_easy_cleanup(curl_ctx);
1740 curl_slist_free_all(headers);
1744 static gboolean folder_item_find_func(GNode *node, gpointer data)
1746 FolderItem *item = node->data;
1748 const gchar *uri = d[0];
1750 if (!uri || !((VCalFolderItem *)item)->uri
1751 || strcmp(uri, ((VCalFolderItem *)item)->uri))
1759 static FolderItem *get_folder_item_for_uri(const gchar *uri)
1761 Folder *root = folder_find_from_name ("vCalendar", vcal_folder_get_class());
1767 d[0] = (gpointer)uri;
1769 g_node_traverse(root->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1770 folder_item_find_func, d);
1774 static gchar *feed_get_title(const gchar *str)
1776 gchar *title = NULL;
1777 if (strstr(str, "X-WR-CALNAME:")) {
1778 title = g_strdup(strstr(str, "X-WR-CALNAME:")+strlen("X-WR-CALNAME:"));
1779 if (strstr(title, "\n"))
1780 *(strstr(title, "\n")) = '\0';
1781 if (strstr(title, "\r"))
1782 *(strstr(title, "\r")) = '\0';
1783 } else if (strstr(str, "X-WR-CALDESC:")) {
1784 title = g_strdup(strstr(str, "X-WR-CALDESC:")+strlen("X-WR-CALDESC:"));
1785 if (strstr(title, "\n"))
1786 *(strstr(title, "\n")) = '\0';
1787 if (strstr(title, "\r"))
1788 *(strstr(title, "\r")) = '\0';
1794 static void update_subscription_finish(const gchar *uri, gchar *feed, gboolean verbose, gchar *error)
1796 Folder *root = folder_find_from_name ("vCalendar", vcal_folder_get_class());
1797 FolderItem *item = NULL;
1798 icalcomponent *cal = NULL;
1801 g_warning("can't get root folder");
1809 if (verbose && manual_update) {
1811 tmp = g_strdup(uri);
1812 if (strlen(uri) > 61) {
1820 alertpanel_error(_("Could not retrieve the Webcal URL:\n%s:\n\n%s"),
1821 tmp, error ? error:_("Unknown error"));
1824 log_error(LOG_PROTOCOL, _("Could not retrieve the Webcal URL:\n%s:\n\n%s\n"),
1825 uri, error ? error:_("Unknown error"));
1827 main_window_cursor_normal(mainwindow_get_mainwindow());
1833 if (strncmp(feed, "BEGIN:VCALENDAR", strlen("BEGIN:VCALENDAR"))) {
1834 if (verbose && manual_update) {
1835 alertpanel_error(_("This URL does not look like a WebCal URL:\n%s\n%s"),
1836 uri, error ? error:_("Unknown error"));
1838 log_error(LOG_PROTOCOL, _("This URL does not look like a WebCal URL:\n%s\n%s\n"),
1839 uri, error ? error:_("Unknown error"));
1842 main_window_cursor_normal(mainwindow_get_mainwindow());
1850 item = get_folder_item_for_uri(uri);
1852 gchar *title = feed_get_title(feed);
1853 if (title == NULL) {
1854 if (strstr(uri, "://"))
1855 title = g_strdup(strstr(uri,"://")+3);
1857 title = g_strdup(uri);
1858 subst_for_filename(title);
1859 if (strlen(title) > 32) {
1860 title[29]=title[30]=title[31]='.';
1864 item = folder_create_folder(root->node->data, title);
1866 if (verbose && manual_update) {
1867 alertpanel_error(_("Could not create directory %s"),
1870 log_error(LOG_PROTOCOL, _("Could not create directory %s"),
1875 main_window_cursor_normal(mainwindow_get_mainwindow());
1878 debug_print("item done %s\n", title);
1879 ((VCalFolderItem *)item)->uri = g_strdup(uri);
1880 ((VCalFolderItem *)item)->feed = feed;
1883 if (((VCalFolderItem *)item)->feed)
1884 g_free(((VCalFolderItem *)item)->feed);
1886 ((VCalFolderItem *)item)->feed = feed;
1887 /* if title differs, update it */
1889 cal = icalparser_parse_string(feed);
1891 if (((VCalFolderItem *)item)->cal)
1892 icalcomponent_free(((VCalFolderItem *)item)->cal);
1894 ((VCalFolderItem *)item)->cal = cal;
1896 main_window_cursor_normal(mainwindow_get_mainwindow());
1897 ((VCalFolderItem *)item)->last_fetch = time(NULL);
1900 static void update_subscription(const gchar *uri, gboolean verbose)
1902 FolderItem *item = get_folder_item_for_uri(uri);
1905 if (prefs_common_get_prefs()->work_offline) {
1907 !inc_offline_should_override(TRUE,
1908 _("Claws Mail needs network access in order "
1909 "to update the Webcal feed.")))
1913 if (time(NULL) - ((VCalFolderItem *)(item))->last_fetch < 60 &&
1914 ((VCalFolderItem *)(item))->cal)
1917 main_window_cursor_wait(mainwindow_get_mainwindow());
1919 label = g_strdup_printf(_("Fetching calendar for %s..."),
1920 item && item->name ? item->name : _("new subscription"));
1921 vcal_curl_read(uri, label, verbose, update_subscription_finish);
1925 static void check_subs_cb(GtkAction *action, gpointer data)
1927 Folder *root = folder_find_from_name ("vCalendar", vcal_folder_get_class());
1929 if (prefs_common_get_prefs()->work_offline &&
1930 !inc_offline_should_override(TRUE,
1931 _("Claws Mail needs network access in order "
1932 "to update the subscription.")))
1935 folderview_check_new(root);
1938 static void subscribe_cal_cb(GtkAction *action, gpointer data)
1943 tmp = input_dialog(_("Subscribe to WebCal"), _("Enter the WebCal URL:"), NULL);
1947 if (!strncmp(tmp, "http", 4)) {
1949 } else if (!strncmp(tmp, "file://", 7)) {
1951 } else if (!strncmp(tmp, "webcal", 6)) {
1952 uri = g_strconcat("http", tmp+6, NULL);
1955 alertpanel_error(_("Could not parse the URL."));
1959 debug_print("uri %s\n", uri);
1961 update_subscription(uri, TRUE);
1962 folder_write_list();
1966 static void unsubscribe_cal_cb(GtkAction *action, gpointer data)
1968 FolderView *folderview = (FolderView *)data;
1969 FolderItem *item, *opened;
1974 if (!folderview->selected) return;
1976 item = folderview_get_selected_item(folderview);
1977 g_return_if_fail(item != NULL);
1978 g_return_if_fail(item->path != NULL);
1979 g_return_if_fail(item->folder != NULL);
1980 opened = folderview_get_opened_item(folderview);
1982 message = g_strdup_printf
1983 (_("Do you really want to unsubscribe?"));
1984 avalue = alertpanel_full(_("Delete folder"), message,
1985 GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL,
1986 FALSE, NULL, ALERT_WARNING, G_ALERTDEFAULT);
1988 if (avalue != G_ALERTALTERNATE) return;
1990 old_id = folder_item_get_identifier(item);
1992 vcal_item_closed(item);
1994 if (item == opened ||
1995 folder_is_child_of(item, opened)) {
1996 summary_clear_all(folderview->summaryview);
1997 folderview_close_opened(folderview, TRUE);
2000 if (item->folder->klass->remove_folder(item->folder, item) < 0) {
2001 folder_item_scan(item);
2002 alertpanel_error(_("Can't remove the folder '%s'."), item->name);
2007 folder_write_list();
2009 prefs_filtering_delete_path(old_id);
2013 gboolean vcal_subscribe_uri(Folder *folder, const gchar *uri)
2016 if (folder->klass != vcal_folder_get_class())
2022 if (!strncmp(uri, "webcal", 6)) {
2023 tmp = g_strconcat("http", uri+6, NULL);
2027 debug_print("uri %s\n", tmp);
2029 update_subscription(tmp, FALSE);
2030 folder_write_list();
2034 static void rename_cb(GtkAction *action, gpointer data)
2036 FolderView *folderview = (FolderView *)data;
2042 item = folderview_get_selected_item(folderview);
2043 g_return_if_fail(item != NULL);
2044 g_return_if_fail(item->path != NULL);
2045 g_return_if_fail(item->folder != NULL);
2047 name = trim_string(item->name, 32);
2048 message = g_strdup_printf(_("Input new name for '%s':"), name);
2049 new_folder = input_dialog(_("Rename folder"), message, name);
2052 if (!new_folder) return;
2053 AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
2055 if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
2056 alertpanel_error(_("'%c' can't be included in folder name."),
2061 if (folder_find_child_item_by_name(folder_item_parent(item), new_folder)) {
2062 name = trim_string(new_folder, 32);
2063 alertpanel_error(_("The folder '%s' already exists."), name);
2068 if (folder_item_rename(item, new_folder) < 0) {
2069 alertpanel_error(_("The folder could not be renamed.\n"
2070 "The new folder name is not allowed."));
2074 folder_item_prefs_save_config_recursive(item);
2075 folder_write_list();
2078 static void set_view_cb(GtkAction *gaction, GtkRadioAction *current, gpointer data)
2080 FolderView *folderview = (FolderView *)data;
2081 gint action = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (current));
2082 FolderItem *item = NULL, *oitem = NULL;
2084 if (!folderview->selected) return;
2085 if (setting_sensitivity) return;
2087 oitem = folderview_get_opened_item(folderview);
2088 item = folderview_get_selected_item(folderview);
2093 if (((VCalFolderItem *)(item))->use_cal_view == action)
2095 debug_print("set view %d\n", action);
2096 if (oitem && item == oitem && oitem->folder->klass == vcal_folder_get_class())
2097 oitem->folder->klass->item_closed(oitem);
2098 ((VCalFolderItem *)(item))->use_cal_view = action;
2099 if (((VCalFolderItem *)(item))->use_cal_view) {
2100 if (oitem && item == oitem && oitem->folder->klass == vcal_folder_get_class())
2101 oitem->folder->klass->item_opened(oitem);
2105 gchar *vcal_get_event_as_ical_str(VCalEvent *event)
2108 icalcomponent *calendar = icalcomponent_vanew(
2109 ICAL_VCALENDAR_COMPONENT,
2110 icalproperty_new_version("2.0"),
2111 icalproperty_new_prodid(
2112 "-//Claws Mail//NONSGML Claws Mail Calendar//EN"),
2113 icalproperty_new_calscale("GREGORIAN"),
2115 vcal_manager_event_dump(event, FALSE, FALSE, calendar, FALSE);
2116 ical = g_strdup(icalcomponent_as_ical_string(calendar));
2117 icalcomponent_free(calendar);
2122 static gchar *get_name_from_property(icalproperty *p)
2126 if (p && icalproperty_get_parameter_as_string(p, "CN") != NULL)
2127 tmp = g_strdup(icalproperty_get_parameter_as_string(p, "CN"));
2132 static gchar *get_email_from_property(icalproperty *p)
2135 gchar *email = NULL;
2138 tmp = g_strdup(icalproperty_get_organizer(p));
2143 if (!strncasecmp(tmp, "MAILTO:", strlen("MAILTO:")))
2144 email = g_strdup(tmp+strlen("MAILTO:"));
2146 email = g_strdup(tmp);
2152 static void adjust_for_local_time_zone(icalproperty *eventtime, icalproperty *tzoffsetto, int dtstart)
2156 time_t loctime, gmttime, evttime;
2157 struct icaltimetype icaltime;
2159 /* calculate local UTC offset */
2160 loctime = time(NULL);
2161 loctime = mktime(localtime(&loctime));
2162 gmttime = mktime(gmtime(&loctime));
2163 loctzoffset = loctime - gmttime;
2165 if (eventtime && tzoffsetto) {
2166 tzoffset = icalproperty_get_tzoffsetto(tzoffsetto);
2168 evttime = icaltime_as_timet(icalproperty_get_dtstart(eventtime));
2171 evttime = icaltime_as_timet(icalproperty_get_dtend(eventtime));
2174 /* convert to UTC */
2175 evttime -= tzoffset;
2176 /* and adjust for local time zone */
2177 evttime += loctzoffset;
2178 icaltime = icaltime_from_timet(evttime, 0);
2181 icalproperty_set_dtstart(eventtime, icaltime);
2184 icalproperty_set_dtend(eventtime, icaltime);
2189 #define GET_PROP(comp,prop,kind) { \
2191 if (!(prop = icalcomponent_get_first_property(comp, kind))) { \
2193 ? icalcomponent_get_first_property(inner, kind) \
2198 #define GET_PROP_LIST(comp,list,kind) { \
2200 if (!(prop = icalcomponent_get_first_property(comp, kind))) { \
2202 ? icalcomponent_get_first_property(inner, kind) \
2205 list = g_slist_prepend(list, prop); \
2206 } while ((prop = icalcomponent_get_next_property(inner, kind)));\
2208 list = g_slist_prepend(list, prop); \
2209 } while ((prop = icalcomponent_get_next_property(comp, kind))); \
2212 #define TO_UTF8(string) { \
2213 if (string && !g_utf8_validate(string, -1, NULL)) { \
2214 gchar *tmp = conv_codeset_strdup(string, \
2215 charset ? charset:conv_get_locale_charset_str(),\
2222 VCalEvent *vcal_get_event_from_ical(const gchar *ical, const gchar *charset)
2224 VCalEvent *event = NULL;
2225 gchar *int_ical = g_strdup(ical);
2226 icalcomponent *comp = icalcomponent_new_from_string(int_ical);
2227 icalcomponent *inner = NULL;
2228 icalcomponent *tzcomp = NULL;
2229 icalproperty *prop = NULL;
2230 GSList *list = NULL, *cur = NULL;
2232 gchar *location = NULL;
2233 gchar *summary = NULL;
2234 gchar *dtstart = NULL;
2235 gchar *dtend = NULL;
2236 gchar *org_email = NULL, *org_name = NULL;
2237 gchar *description = NULL;
2240 gchar *recur = NULL;
2242 enum icalproperty_method method = ICAL_METHOD_REQUEST;
2243 enum icalcomponent_kind type = ICAL_VEVENT_COMPONENT;
2244 GSList *attendees = NULL;
2251 if ((inner = icalcomponent_get_inner(comp)) != NULL)
2252 type = icalcomponent_isa(inner);
2254 GET_PROP(comp, prop, ICAL_UID_PROPERTY);
2256 uid = g_strdup(icalproperty_get_uid(prop));
2258 icalproperty_free(prop);
2260 GET_PROP(comp, prop, ICAL_LOCATION_PROPERTY);
2262 location = g_strdup(icalproperty_get_location(prop));
2264 icalproperty_free(prop);
2266 GET_PROP(comp, prop, ICAL_SUMMARY_PROPERTY);
2268 summary = g_strdup(icalproperty_get_summary(prop));
2270 icalproperty_free(prop);
2272 tzcomp = icalcomponent_get_first_component(comp, ICAL_VTIMEZONE_COMPONENT);
2274 icalproperty *evtstart = NULL;
2275 icalproperty *evtend = NULL;
2276 icalproperty *tzoffsetto = NULL;
2277 icalcomponent *tzstd = NULL;
2279 tzstd = icalcomponent_get_first_component(tzcomp, ICAL_XSTANDARD_COMPONENT);
2280 tzoffsetto = icalcomponent_get_first_property(tzstd, ICAL_TZOFFSETTO_PROPERTY);
2282 GET_PROP(comp, evtstart, ICAL_DTSTART_PROPERTY);
2283 adjust_for_local_time_zone(evtstart, tzoffsetto, TRUE);
2285 GET_PROP(comp, evtend, ICAL_DTEND_PROPERTY);
2286 adjust_for_local_time_zone(evtend, tzoffsetto, FALSE);
2289 icalproperty_free(tzoffsetto);
2291 icalproperty_free(evtstart);
2293 icalproperty_free(evtend);
2295 icalcomponent_free(tzstd);
2297 GET_PROP(comp, prop, ICAL_DTSTART_PROPERTY);
2299 dtstart = g_strdup(icaltime_as_ical_string(icalproperty_get_dtstart(prop)));
2301 icalproperty_free(prop);
2303 GET_PROP(comp, prop, ICAL_DTEND_PROPERTY);
2305 dtend = g_strdup(icaltime_as_ical_string(icalproperty_get_dtend(prop)));
2307 icalproperty_free(prop);
2309 GET_PROP(comp, prop, ICAL_DURATION_PROPERTY);
2311 struct icaldurationtype duration = icalproperty_get_duration(prop);
2312 struct icaltimetype itt;
2313 icalproperty_free(prop);
2314 GET_PROP(comp, prop, ICAL_DTSTART_PROPERTY);
2316 itt = icalproperty_get_dtstart(prop);
2317 icalproperty_free(prop);
2318 dtend = g_strdup(icaltime_as_ical_string(icaltime_add(itt,duration)));
2323 GET_PROP(comp, prop, ICAL_SEQUENCE_PROPERTY);
2325 sequence = icalproperty_get_sequence(prop);
2326 icalproperty_free(prop);
2328 GET_PROP(comp, prop, ICAL_METHOD_PROPERTY);
2330 method = icalproperty_get_method(prop);
2331 icalproperty_free(prop);
2333 GET_PROP(comp, prop, ICAL_ORGANIZER_PROPERTY);
2335 org_email = get_email_from_property(prop);
2337 org_name = get_name_from_property(prop);
2339 icalproperty_free(prop);
2341 GET_PROP(comp, prop, ICAL_DESCRIPTION_PROPERTY);
2343 description = g_strdup(icalproperty_get_description(prop));
2344 TO_UTF8(description);
2345 icalproperty_free(prop);
2347 GET_PROP(comp, prop, ICAL_URL_PROPERTY);
2349 url = g_strdup(icalproperty_get_url(prop));
2351 icalproperty_free(prop);
2353 GET_PROP(comp, prop, ICAL_TZID_PROPERTY);
2355 tzid = g_strdup(icalproperty_get_tzid(prop));
2357 icalproperty_free(prop);
2359 GET_PROP(comp, prop, ICAL_RRULE_PROPERTY);
2361 struct icalrecurrencetype rrule = icalproperty_get_rrule(prop);
2362 recur = g_strdup(icalrecurrencetype_as_string(&rrule));
2364 icalproperty_free(prop);
2366 GET_PROP_LIST(comp, list, ICAL_ATTENDEE_PROPERTY);
2367 for (cur = list; cur; cur = cur->next) {
2368 enum icalparameter_partstat partstat = 0;
2369 enum icalparameter_cutype cutype = 0;
2370 icalparameter *param = NULL;
2371 gchar *email = NULL;
2373 Answer *answer = NULL;
2375 prop = (icalproperty *)(cur->data);
2377 email = get_email_from_property(prop);
2379 name = get_name_from_property(prop);
2382 param = icalproperty_get_first_parameter(prop, ICAL_PARTSTAT_PARAMETER);
2384 partstat = icalparameter_get_partstat(param);
2386 param = icalproperty_get_first_parameter(prop, ICAL_CUTYPE_PARAMETER);
2388 cutype= icalparameter_get_cutype(param);
2391 partstat = ICAL_PARTSTAT_NEEDSACTION;
2393 cutype = ICAL_CUTYPE_INDIVIDUAL;
2394 answer = answer_new(email, name, partstat, cutype);
2395 attendees = g_slist_prepend(attendees, answer);
2398 icalproperty_free(prop);
2402 event = vcal_manager_new_event (uid, org_email, org_name,
2403 location, summary, description,
2404 dtstart, dtend, recur,
2406 method, sequence, type);
2407 event->answers = attendees;
2415 g_free(description);
2420 icalcomponent_free(comp);
2424 gboolean vcal_event_exists(const gchar *id)
2426 MsgInfo *info = NULL;
2427 Folder *folder = folder_find_from_name ("vCalendar", vcal_folder_get_class());
2431 info = folder_item_get_msginfo_by_msgid(folder->inbox, id);
2433 procmsg_msginfo_free(&info);
2439 void vcal_foreach_event(gboolean (*cb_func)(const gchar *vevent))
2441 GSList *list = vcal_folder_get_waiting_events();
2445 debug_print("calling cb_func...\n");
2446 for (cur = list; cur; cur = cur->next) {
2447 VCalEvent *event = (VCalEvent *)cur->data;
2448 gchar *tmp = vcal_get_event_as_ical_str(event);
2450 debug_print(" ...for event %s\n", event->uid);
2453 vcal_manager_free_event(event);
2458 /* please call vcalendar_refresh_folder_contents() after one or more
2459 * calls to this function */
2460 gboolean vcal_delete_event(const gchar *id)
2462 MsgInfo *info = NULL;
2463 Folder *folder = folder_find_from_name ("vCalendar", vcal_folder_get_class());
2467 info = folder_item_get_msginfo_by_msgid(folder->inbox, id);
2469 debug_print("removing event %s\n", id);
2470 vcal_remove_event(folder, info);
2471 procmsg_msginfo_free(&info);
2472 folder_item_scan(folder->inbox);
2475 debug_print("not removing unexisting event %s\n", id);
2479 /* please call vcalendar_refresh_folder_contents() after one or more
2480 * calls to this function */
2481 gchar* vcal_add_event(const gchar *vevent)
2483 VCalEvent *event = vcal_get_event_from_ical(vevent, NULL);
2484 gchar *retVal = NULL;
2485 Folder *folder = folder_find_from_name ("vCalendar", vcal_folder_get_class());
2490 if (vcal_event_exists(event->uid)) {
2491 debug_print("event %s already exists\n", event->uid);
2492 vcal_manager_free_event(event);
2495 debug_print("adding event %s\n", event->uid);
2496 if (!account_find_from_address(event->organizer, FALSE) &&
2497 !vcal_manager_get_account_from_event(event)) {
2498 PrefsAccount *account = account_get_default();
2499 vcal_manager_update_answer(event, account->address,
2501 ICAL_PARTSTAT_ACCEPTED,
2502 ICAL_CUTYPE_INDIVIDUAL);
2503 debug_print("can't find our accounts in event, adding default\n");
2505 vcal_manager_save_event(event, TRUE);
2506 folder_item_scan(folder->inbox);
2507 retVal = vcal_get_event_as_ical_str(event);
2508 vcal_manager_free_event(event);
2514 /* please call vcalendar_refresh_folder_contents() after one or more
2515 * calls to this function */
2516 gchar* vcal_update_event(const gchar *vevent)
2518 VCalEvent *event = vcal_get_event_from_ical(vevent, NULL);
2521 r = vcal_delete_event(event->uid);
2522 vcal_manager_free_event(event);
2524 return vcal_add_event(vevent);