Backport upstream libical 4 support, retaining compatibility with older libical.

Bug: https://bugs.gentoo.org/987844
Upstream: https://git.claws-mail.org/?p=claws.git;a=commit;h=1ad8482186cdcd056047aa5f9e2d495e6296a1bb
Upstream: https://git.claws-mail.org/?p=claws.git;a=commit;h=9b6b82879c75ceb52d93eaa137eafe04b258136e

--- a/src/plugins/vcalendar/vcal_folder.c
+++ b/src/plugins/vcalendar/vcal_folder.c
@@ -536,7 +536,11 @@
 	while (evt) {
 		icalproperty *prop;
 		icalproperty *rprop = icalcomponent_get_first_property(evt, ICAL_RRULE_PROPERTY);
+#if ICAL_CHECK_VERSION(4, 0, 0)
+		struct icalrecurrencetype* recur;
+#else
 		struct icalrecurrencetype recur;
+#endif
         	struct icaltimetype next;
         	icalrecur_iterator* ritr = NULL;
 		EventTime days;
@@ -559,7 +563,12 @@
 			gchar *orig_uid = NULL;
 			gchar *uid = g_strdup(icalproperty_get_uid(prop));
 			IcalFeedData *data = icalfeeddata_new(
-						icalcomponent_new_clone(evt), NULL);
+#if ICAL_CHECK_VERSION(4, 0, 0)
+						icalcomponent_clone(evt),
+#else
+						icalcomponent_new_clone(evt),
+#endif
+						NULL);
 			int i = 0;
 			orig_uid = g_strdup(uid);
 
@@ -610,7 +619,11 @@
 				struct icaldurationtype ical_dur;
 				struct icaltimetype dtstart = icaltime_null_time();
 				struct icaltimetype dtend = icaltime_null_time();
+#if ICAL_CHECK_VERSION(4, 0, 0)
+				evt = icalcomponent_clone(evt);
+#else
 				evt = icalcomponent_new_clone(evt);
+#endif
 				prop = icalcomponent_get_first_property(evt, ICAL_RRULE_PROPERTY);
 				if (prop) {
 					icalcomponent_remove_property(evt, prop);
@@ -626,7 +639,11 @@
 					dtend = icalproperty_get_dtend(prop);
 				else
 					debug_print("event has no DTEND!\n");
+#if ICAL_CHECK_VERSION(4, 0, 0)
+				ical_dur = icalduration_from_times(dtend, dtstart);
+#else
 				ical_dur = icaltime_subtract(dtend, dtstart);
+#endif
 				next = icalrecur_iterator_next(ritr);
 				if (!icaltime_is_null_time(next) &&
 				    !icaltime_is_null_time(dtstart) && i < 100) {
@@ -635,8 +652,11 @@
 
 					prop = icalcomponent_get_first_property(evt, ICAL_DTEND_PROPERTY);
 					if (prop)
+#if ICAL_CHECK_VERSION(4, 0, 0)
+						icalproperty_set_dtend(prop, icalduration_extend(next, ical_dur));
+#else
 						icalproperty_set_dtend(prop, icaltime_add(next, ical_dur));
-
+#endif
 					prop = icalcomponent_get_first_property(evt, ICAL_UID_PROPERTY);
 					uid = g_strdup_printf("%s-%d", orig_uid, i);
 					icalproperty_set_uid(prop, uid);
@@ -770,7 +790,11 @@
 			if ((status == ICAL_PARTSTAT_ACCEPTED
 			     || status == ICAL_PARTSTAT_TENTATIVE) 
 			    && event->recur && *(event->recur)) {
+#if ICAL_CHECK_VERSION(4, 0, 0)
+				struct icalrecurrencetype* recur;
+#else
         			struct icalrecurrencetype recur;
+#endif
         			struct icaltimetype dtstart;
         			struct icaltimetype next;
         			icalrecur_iterator* ritr;
@@ -779,14 +803,21 @@
 				int i = 0;
 
 				debug_print("dumping recurring events from main event %s\n", d);
+#if ICAL_CHECK_VERSION(4, 0, 0)
+				recur = icalrecurrencetype_new_from_string(event->recur);
+#else
         			recur = icalrecurrencetype_from_string(event->recur);
+#endif
 				dtstart = icaltime_from_string(event->dtstart);
 
 				duration = icaltime_as_timet(icaltime_from_string(event->dtend))
 							    - icaltime_as_timet(icaltime_from_string(event->dtstart));
 
+#if ICAL_CHECK_VERSION(4, 0, 0)
+				ical_dur = icaldurationtype_from_seconds(duration);
+#else
 				ical_dur = icaldurationtype_from_int(duration);
-
+#endif
         			ritr = icalrecur_iterator_new(recur, dtstart);
 
 				next = icalrecur_iterator_next(ritr); /* skip first one */
@@ -799,7 +830,12 @@
 					gchar *uid = g_strdup_printf("%s-%d", event->uid, i);
 					new_start = icaltime_as_ical_string(next);
 					new_end = icaltime_as_ical_string(
-							icaltime_add(next, ical_dur));
+#if ICAL_CHECK_VERSION(4, 0, 0)
+							icalduration_extend(next, ical_dur)
+#else
+							icaltime_add(next, ical_dur)
+#endif
+							);
 					debug_print("adding with start/end %s:%s\n", new_start, new_end);
 					nevent = vcal_manager_new_event(uid, event->organizer, event->orgname, 
 								event->location, event->summary, event->description, 
@@ -825,6 +861,9 @@
 					i++;
 				}
 				icalrecur_iterator_free(ritr);
+#if ICAL_CHECK_VERSION(4, 0, 0)
+				icalrecurrencetype_unref(recur);
+#endif
 			}
 		} else {
 			vcal_manager_free_event(event);
@@ -2379,7 +2418,11 @@
 			if (prop) {
 				itt = icalproperty_get_dtstart(prop);
 				icalproperty_free(prop);
+#if ICAL_CHECK_VERSION(4, 0, 0)
+				dtend = g_strdup(icaltime_as_ical_string(icalduration_extend(itt,duration)));
+#else
 				dtend = g_strdup(icaltime_as_ical_string(icaltime_add(itt,duration)));
+#endif
 				TO_UTF8(dtend);
 			}
 		}
@@ -2434,8 +2477,13 @@
 	}
 	GET_PROP(comp, prop, ICAL_RRULE_PROPERTY);
 	if (prop) {
+#if ICAL_CHECK_VERSION(4, 0, 0)
+		struct icalrecurrencetype* rrule = icalproperty_get_rrule(prop);
+		recur = g_strdup(icalrecurrencetype_as_string(rrule));
+#else
 		struct icalrecurrencetype rrule = icalproperty_get_rrule(prop);
 		recur = g_strdup(icalrecurrencetype_as_string(&rrule));
+#endif
 		TO_UTF8(recur);
 		icalproperty_free(prop);
 	}
--- a/src/plugins/vcalendar/vcal_manager.c
+++ b/src/plugins/vcalendar/vcal_manager.c
@@ -402,7 +402,11 @@
 		icalproperty_vanew_dtend((icaltime_from_string(event->dtend)), (void*)0));
 	if (event->recur && *(event->recur)) {
 		icalcomponent_add_property(ievent,
+#if ICAL_CHECK_VERSION(4, 0, 0)
+			icalproperty_vanew_rrule((icalrecurrencetype_new_from_string(event->recur)), (void*)0));
+#else
 			icalproperty_vanew_rrule((icalrecurrencetype_from_string(event->recur)), (void*)0));
+#endif
 	}
 	icalcomponent_add_property(ievent,
 		icalproperty_new_description(event->description));
@@ -689,9 +693,11 @@
 	icalproperty *prop;
 	icalcomponent *ievent = NULL;
 	int i = 0;
-
+#if ICAL_CHECK_VERSION(4, 0, 0)
+	ievent = icalcomponent_clone(event);
+#else
 	ievent = icalcomponent_new_clone(event);
-
+#endif
 	prop = icalcomponent_get_first_property(ievent, ICAL_UID_PROPERTY);
 	if (prop) {
 		gchar *sanitized_uid = g_strdup(icalproperty_get_uid(prop));
--- a/src/plugins/vcalendar/vcal_meeting_gtk.c
+++ b/src/plugins/vcalendar/vcal_meeting_gtk.c
@@ -2110,7 +2110,11 @@
 	
 		ipt.start = icaltime_from_string(event->dtstart);
 		ipt.end = icaltime_from_string(event->dtend);
+#if ICAL_CHECK_VERSION(4, 0, 0)
+		ipt.duration = icalduration_from_times(ipt.end, ipt.start);
+#else
 		ipt.duration = icaltime_subtract(ipt.end, ipt.start);
+#endif
 		if (icaltime_as_timet(ipt.start) <= icaltime_as_timet(itt_end) 
 		 && icaltime_as_timet(ipt.end) >= icaltime_as_timet(itt_start)) {
 			prop = icalproperty_new_freebusy(ipt);
