Source: abook
Source-Version: 0.6.1-1
Severity: wishlist
Tags: patch upstream
Forwarded: https://sourceforge.net/p/abook/git/merge-requests/1/

Hi!

To be able to use calcurse(1), it would be nice if abook supported
exporting the birthdays in iCalendar format. I've implemented this
and sent a merge request upstream.

As I'm not sure what's the upstream state, I'm sending it here too
in the hope that it might be mergeable in Debian regardless of the
upstream situation. Otherwise, it is just a nicer way to track the
progress. :)

Thanks,
Guillem
From bfb6f9af85be71ef4ee18105521a74520591bed5 Mon Sep 17 00:00:00 2001
From: Guillem Jover <guil...@hadrons.org>
Date: Tue, 13 Nov 2018 03:29:07 +0100
Subject: [PATCH] Add support for iCalendar export format

This will export the birthday data into an iCalendar formatted file,
as aspecified in RFC 5545.
---
 abook.1  |  2 ++
 filter.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/abook.1 b/abook.1
index 96d665d..17cdcc2 100644
--- a/abook.1
+++ b/abook.1
@@ -78,6 +78,8 @@ The following \fIoutputformats\fR are supported:
 .br
 \- \fBwl\fP Wanderlust address book
 .br
+\- \fBical\fP iCalendar
+.br
 \- \fBbsdcal\fP BSD calendar
 .br
 \- \fBcustom\fP Custom output format, see below
diff --git a/filter.c b/filter.c
index 5ae7bd6..aff6bcb 100644
--- a/filter.c
+++ b/filter.c
@@ -71,6 +71,7 @@ static int	elm_alias_export(FILE *out, struct db_enumerator e);
 static int	text_export_database(FILE *out, struct db_enumerator e);
 static int	spruce_export_database(FILE *out, struct db_enumerator e);
 static int	wl_export_database(FILE *out, struct db_enumerator e);
+static int	ical_export_database(FILE *out, struct db_enumerator e);
 static int	bsdcal_export_database(FILE *out, struct db_enumerator e);
 static int	custom_export_database(FILE *out, struct db_enumerator e);
 
@@ -113,6 +114,7 @@ struct abook_output_filter e_filters[] = {
 	{ "text", N_("plain text"), text_export_database },
 	{ "wl", N_("Wanderlust address book"), wl_export_database },
 	{ "spruce", N_("Spruce address book"), spruce_export_database },
+	{ "ical", N_("iCalendar"), ical_export_database },
 	{ "bsdcal", N_("BSD calendar"), bsdcal_export_database },
 	{ "custom", N_("Custom format"), custom_export_database },
 	{ "\0", NULL, NULL }
@@ -2537,6 +2539,62 @@ wl_export_database(FILE *out, struct db_enumerator e)
  * end of wanderlust addressbook export filter
  */
 
+/*
+ * iCalendar export filter
+ */
+
+static int
+ical_export_database(FILE *out, struct db_enumerator e)
+{
+	fprintf(out, "BEGIN:VCALENDAR\n");
+	fprintf(out, "PRODID:-//%s//NONSGML %s//EN\n", PACKAGE, VERSION);
+	fprintf(out, "VERSION:2.0\n");
+
+	db_enumerate_items(e) {
+		int year, month = 0, day = 0;
+		char *anniversary = db_fget(e.item, ANNIVERSARY);
+		char *summary;
+
+		if (anniversary) {
+			if(!parse_date_string(anniversary, &day, &month, &year))
+				continue;
+
+			/*
+			 * If there is no year, or an unrepresentable one,
+			 * use the lowest leap year that can fit on a system
+			 * using a 32-bit time_t.
+			 *
+			 * Unfortunately we cannot use the --MMDD reduced form
+			 * which was allowed in ISO 8601:2000, as that is not
+			 * allowed anymore in ISO 8601:2004, which is what
+			 * iCalendar is based on.
+			 */
+			if(year < 1904)
+				year = 1904;
+
+			summary = strdup_printf(_("Anniversary of %s"),
+				safe_str(db_name_get(e.item))
+			);
+
+			fprintf(out, "BEGIN:VEVENT\n");
+			fprintf(out, "DTSTART:%d%02d%02d\n", year, month, day);
+			fprintf(out, "RRULE:FREQ=YEARLY\n");
+			fprintf(out, "SUMMARY:%s\n", summary);
+			fprintf(out, "END:VEVENT\n");
+
+			free(summary);
+		}
+	}
+
+	fprintf(out, "END:VCALENDAR\n");
+
+	return 0;
+}
+
+/*
+ * end of iCalendar export filter
+ */
+
 /*
  * BSD calendar export filter
  */
-- 
2.19.1.1182.g4ecb1133ce

Reply via email to