From 3eae7ce82869dd2c313496913bb94438dd0177a9 Mon Sep 17 00:00:00 2001
From: Lukas Fleischer <lfleischer@calcurse.org>
Date: Tue, 12 Jan 2016 18:40:40 +0100
Subject: Add --list-imported command line option

When this option is used together with -i/--import, the object
identifiers of imported objects are printed to stdout.

Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
---
 src/args.c     |  8 ++++++-
 src/calcurse.c |  2 +-
 src/calcurse.h |  4 ++--
 src/ical.c     | 74 ++++++++++++++++++++++++++++++++++++++++++----------------
 src/io.c       |  4 ++--
 5 files changed, 66 insertions(+), 26 deletions(-)

(limited to 'src')

diff --git a/src/args.c b/src/args.c
index 78a7058..0e6a5a2 100644
--- a/src/args.c
+++ b/src/args.c
@@ -70,6 +70,7 @@ enum {
 	OPT_FMT_EV,
 	OPT_FMT_REV,
 	OPT_FMT_TODO,
+	OPT_LIST_IMPORTED,
 	OPT_READ_ONLY,
 	OPT_STATUS
 };
@@ -452,6 +453,7 @@ int parse_args(int argc, char **argv)
 	const char *fmt_todo = "%p. %m\n";
 	/* Import and export parameters */
 	int xfmt = IO_EXPORT_ICAL;
+	int list_imported = 0;
 	/* Data file locations */
 	const char *cfile = NULL, *datadir = NULL, *ifile = NULL;
 
@@ -507,6 +509,7 @@ int parse_args(int argc, char **argv)
 		{"format-event", required_argument, NULL, OPT_FMT_EV},
 		{"format-recur-event", required_argument, NULL, OPT_FMT_REV},
 		{"format-todo", required_argument, NULL, OPT_FMT_TODO},
+		{"list-imported", no_argument, NULL, OPT_LIST_IMPORTED},
 		{"read-only", no_argument, NULL, OPT_READ_ONLY},
 		{"status", no_argument, NULL, OPT_STATUS},
 		{NULL, no_argument, NULL, 0}
@@ -713,6 +716,9 @@ int parse_args(int argc, char **argv)
 		case OPT_FMT_TODO:
 			fmt_todo = optarg;
 			break;
+		case OPT_LIST_IMPORTED:
+			list_imported = 1;
+			break;
 		case OPT_READ_ONLY:
 			read_only = 1;
 			break;
@@ -789,7 +795,7 @@ int parse_args(int argc, char **argv)
 		/* Get default pager in case we need to show a log file. */
 		vars_init();
 		io_load_data(NULL);
-		io_import_data(IO_IMPORT_ICAL, ifile);
+		io_import_data(IO_IMPORT_ICAL, ifile, list_imported);
 		io_save_apts(path_apts);
 		io_save_todo(path_todo);
 	} else if (export) {
diff --git a/src/calcurse.c b/src/calcurse.c
index 72c8fe3..9f85501 100644
--- a/src/calcurse.c
+++ b/src/calcurse.c
@@ -276,7 +276,7 @@ static inline void key_generic_reload(void)
 static inline void key_generic_import(void)
 {
 	wins_erase_status_bar();
-	io_import_data(IO_IMPORT_ICAL, NULL);
+	io_import_data(IO_IMPORT_ICAL, NULL, 0);
 	ui_calendar_monthly_view_cache_set_invalid();
 	do_storage(0);
 	wins_update(FLAG_ALL);
diff --git a/src/calcurse.h b/src/calcurse.h
index 7693d79..380384f 100644
--- a/src/calcurse.h
+++ b/src/calcurse.h
@@ -797,7 +797,7 @@ int display_help(const char *);
 int run_hook(const char *);
 
 /* ical.c */
-void ical_import_data(FILE *, FILE *, unsigned *, unsigned *, unsigned *,
+void ical_import_data(FILE *, FILE *, int, unsigned *, unsigned *, unsigned *,
 		      unsigned *, unsigned *);
 void ical_export_data(FILE *);
 
@@ -823,7 +823,7 @@ int io_check_file(const char *);
 int io_check_data_files(void);
 void io_startup_screen(int);
 void io_export_data(enum export_type);
-void io_import_data(enum import_type, const char *);
+void io_import_data(enum import_type, const char *, int);
 struct io_file *io_log_init(void);
 void io_log_print(struct io_file *, int, const char *);
 void io_log_display(struct io_file *, const char *, const char *);
diff --git a/src/ical.c b/src/ical.c
index 46bf9f2..c8ae023 100644
--- a/src/ical.c
+++ b/src/ical.c
@@ -281,28 +281,45 @@ static void ical_log(FILE * log, ical_types_e type, unsigned lineno,
 	fprintf(log, "%s [%d]: %s\n", typestr[type], lineno, msg);
 }
 
-static void ical_store_todo(int priority, char *mesg, char *note)
+static void ical_store_todo(int priority, char *mesg, char *note, int list)
 {
-	todo_add(mesg, priority, note);
+	struct todo *todo = todo_add(mesg, priority, note);
+	if (list) {
+		char *hash = todo_hash(todo);
+		printf("%s\n", hash);
+		mem_free(hash);
+	}
 	mem_free(mesg);
 	erase_note(&note);
 }
 
 static void
 ical_store_event(char *mesg, char *note, long day, long end,
-		 ical_rpt_t * rpt, llist_t * exc)
+		 ical_rpt_t * rpt, llist_t * exc, int list)
 {
 	const int EVENTID = 1;
+	struct event *ev;
+	struct recur_event *rev;
 
 	if (rpt) {
-		recur_event_new(mesg, note, day, EVENTID, rpt->type,
-				rpt->freq, rpt->until, exc);
+		rev = recur_event_new(mesg, note, day, EVENTID, rpt->type,
+				      rpt->freq, rpt->until, exc);
 		mem_free(rpt);
+		if (list) {
+			char *hash = recur_event_hash(rev);
+			printf("%s\n", hash);
+			mem_free(hash);
+		}
 		goto cleanup;
 	}
 
 	if (end == 0 || end - day <= DAYINSEC) {
-		event_new(mesg, note, day, EVENTID);
+		ev = event_new(mesg, note, day, EVENTID);
+		if (list) {
+			char *hash = event_hash(ev);
+			printf("%s\n", hash);
+			mem_free(hash);
+		}
 		goto cleanup;
 	}
 
@@ -319,9 +336,14 @@ ical_store_event(char *mesg, char *note, long day, long end,
 	rpt->freq = 1;
 	rpt->count = 0;
 	rpt->until = end;
-	recur_event_new(mesg, note, day, EVENTID, rpt->type,
-			rpt->freq, rpt->until, exc);
+	rev = recur_event_new(mesg, note, day, EVENTID, rpt->type,
+			      rpt->freq, rpt->until, exc);
 	mem_free(rpt);
+	if (list) {
+		char *hash = recur_event_hash(rev);
+		printf("%s\n", hash);
+		mem_free(hash);
+	}
 
 cleanup:
 	mem_free(mesg);
@@ -330,18 +352,30 @@ cleanup:
 
 static void
 ical_store_apoint(char *mesg, char *note, long start, long dur,
-		  ical_rpt_t * rpt, llist_t * exc, int has_alarm)
+		  ical_rpt_t * rpt, llist_t * exc, int has_alarm, int list)
 {
 	char state = 0L;
+	struct apoint *apt;
+	struct recur_apoint *rapt;
 
 	if (has_alarm)
 		state |= APOINT_NOTIFY;
 	if (rpt) {
-		recur_apoint_new(mesg, note, start, dur, state, rpt->type,
-				 rpt->freq, rpt->until, exc);
+		rapt = recur_apoint_new(mesg, note, start, dur, state,
+					rpt->type, rpt->freq, rpt->until, exc);
 		mem_free(rpt);
+		if (list) {
+			char *hash = recur_apoint_hash(rapt);
+			printf("%s\n", hash);
+			mem_free(hash);
+		}
 	} else {
-		apoint_new(mesg, note, start, dur, state);
+		apt = apoint_new(mesg, note, start, dur, state);
+		if (list) {
+			char *hash = apoint_hash(apt);
+			printf("%s\n", hash);
+			mem_free(hash);
+		}
 	}
 	mem_free(mesg);
 	erase_note(&note);
@@ -833,7 +867,7 @@ static char *ical_read_summary(char *line)
 }
 
 static void
-ical_read_event(FILE * fdi, FILE * log, unsigned *noevents,
+ical_read_event(FILE * fdi, FILE * log, int list, unsigned *noevents,
 		unsigned *noapoints, unsigned *noskipped, char *buf,
 		char *lstore, unsigned *lineno)
 {
@@ -906,13 +940,13 @@ ical_read_event(FILE * fdi, FILE * log, unsigned *noevents,
 				ical_store_apoint(vevent.mesg, vevent.note,
 						vevent.start, vevent.dur,
 						vevent.rpt, &vevent.exc,
-						vevent.has_alarm);
+						vevent.has_alarm, list);
 				(*noapoints)++;
 				break;
 			case EVENT:
 				ical_store_event(vevent.mesg, vevent.note,
 						vevent.start, vevent.end,
-						vevent.rpt, &vevent.exc);
+						vevent.rpt, &vevent.exc, list);
 				(*noevents)++;
 				break;
 			case UNDEFINED:
@@ -993,7 +1027,7 @@ cleanup:
 }
 
 static void
-ical_read_todo(FILE * fdi, FILE * log, unsigned *notodos,
+ical_read_todo(FILE * fdi, FILE * log, int list, unsigned *notodos,
 	       unsigned *noskipped, char *buf, char *lstore,
 	       unsigned *lineno)
 {
@@ -1028,7 +1062,7 @@ ical_read_todo(FILE * fdi, FILE * log, unsigned *notodos,
 			}
 
 			ical_store_todo(vtodo.priority, vtodo.mesg,
-					vtodo.note);
+					vtodo.note, list);
 			(*notodos)++;
 			return;
 		}
@@ -1066,7 +1100,7 @@ cleanup:
 
 /* Import calcurse data. */
 void
-ical_import_data(FILE * stream, FILE * log, unsigned *events,
+ical_import_data(FILE * stream, FILE * log, int list, unsigned *events,
 		 unsigned *apoints, unsigned *todos, unsigned *lines,
 		 unsigned *skipped)
 {
@@ -1084,10 +1118,10 @@ ical_import_data(FILE * stream, FILE * log, unsigned *events,
 	while (ical_readline(stream, buf, lstore, lines)) {
 		(*lines)++;
 		if (starts_with_ci(buf, "BEGIN:VEVENT")) {
-			ical_read_event(stream, log, events, apoints,
+			ical_read_event(stream, log, list, events, apoints,
 					skipped, buf, lstore, lines);
 		} else if (starts_with_ci(buf, "BEGIN:VTODO")) {
-			ical_read_todo(stream, log, todos, skipped, buf,
+			ical_read_todo(stream, log, list, todos, skipped, buf,
 				       lstore, lines);
 		}
 	}
diff --git a/src/io.c b/src/io.c
index 2639da5..e72a29d 100644
--- a/src/io.c
+++ b/src/io.c
@@ -1154,7 +1154,7 @@ static FILE *get_import_stream(enum import_type type)
  * A temporary log file is created in /tmp to store the import process report,
  * and is cleared at the end.
  */
-void io_import_data(enum import_type type, const char *stream_name)
+void io_import_data(enum import_type type, const char *stream_name, int list)
 {
 	const char *proc_report =
 	    _("Import process report: %04d lines read");
@@ -1195,7 +1195,7 @@ void io_import_data(enum import_type type, const char *stream_name)
 	}
 
 	if (type == IO_IMPORT_ICAL)
-		ical_import_data(stream, log->fd, &stats.events,
+		ical_import_data(stream, log->fd, list, &stats.events,
 				 &stats.apoints, &stats.todos,
 				 &stats.lines, &stats.skipped);
 
-- 
cgit v1.2.3-70-g09d2