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>
This commit is contained in:
Lukas Fleischer 2016-01-12 18:40:40 +01:00
parent c58087d591
commit 3eae7ce828
5 changed files with 66 additions and 26 deletions

View File

@ -70,6 +70,7 @@ enum {
OPT_FMT_EV, OPT_FMT_EV,
OPT_FMT_REV, OPT_FMT_REV,
OPT_FMT_TODO, OPT_FMT_TODO,
OPT_LIST_IMPORTED,
OPT_READ_ONLY, OPT_READ_ONLY,
OPT_STATUS OPT_STATUS
}; };
@ -452,6 +453,7 @@ int parse_args(int argc, char **argv)
const char *fmt_todo = "%p. %m\n"; const char *fmt_todo = "%p. %m\n";
/* Import and export parameters */ /* Import and export parameters */
int xfmt = IO_EXPORT_ICAL; int xfmt = IO_EXPORT_ICAL;
int list_imported = 0;
/* Data file locations */ /* Data file locations */
const char *cfile = NULL, *datadir = NULL, *ifile = NULL; 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-event", required_argument, NULL, OPT_FMT_EV},
{"format-recur-event", required_argument, NULL, OPT_FMT_REV}, {"format-recur-event", required_argument, NULL, OPT_FMT_REV},
{"format-todo", required_argument, NULL, OPT_FMT_TODO}, {"format-todo", required_argument, NULL, OPT_FMT_TODO},
{"list-imported", no_argument, NULL, OPT_LIST_IMPORTED},
{"read-only", no_argument, NULL, OPT_READ_ONLY}, {"read-only", no_argument, NULL, OPT_READ_ONLY},
{"status", no_argument, NULL, OPT_STATUS}, {"status", no_argument, NULL, OPT_STATUS},
{NULL, no_argument, NULL, 0} {NULL, no_argument, NULL, 0}
@ -713,6 +716,9 @@ int parse_args(int argc, char **argv)
case OPT_FMT_TODO: case OPT_FMT_TODO:
fmt_todo = optarg; fmt_todo = optarg;
break; break;
case OPT_LIST_IMPORTED:
list_imported = 1;
break;
case OPT_READ_ONLY: case OPT_READ_ONLY:
read_only = 1; read_only = 1;
break; break;
@ -789,7 +795,7 @@ int parse_args(int argc, char **argv)
/* Get default pager in case we need to show a log file. */ /* Get default pager in case we need to show a log file. */
vars_init(); vars_init();
io_load_data(NULL); 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_apts(path_apts);
io_save_todo(path_todo); io_save_todo(path_todo);
} else if (export) { } else if (export) {

View File

@ -276,7 +276,7 @@ static inline void key_generic_reload(void)
static inline void key_generic_import(void) static inline void key_generic_import(void)
{ {
wins_erase_status_bar(); 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(); ui_calendar_monthly_view_cache_set_invalid();
do_storage(0); do_storage(0);
wins_update(FLAG_ALL); wins_update(FLAG_ALL);

View File

@ -797,7 +797,7 @@ int display_help(const char *);
int run_hook(const char *); int run_hook(const char *);
/* ical.c */ /* ical.c */
void ical_import_data(FILE *, FILE *, unsigned *, unsigned *, unsigned *, void ical_import_data(FILE *, FILE *, int, unsigned *, unsigned *, unsigned *,
unsigned *, unsigned *); unsigned *, unsigned *);
void ical_export_data(FILE *); void ical_export_data(FILE *);
@ -823,7 +823,7 @@ int io_check_file(const char *);
int io_check_data_files(void); int io_check_data_files(void);
void io_startup_screen(int); void io_startup_screen(int);
void io_export_data(enum export_type); 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); struct io_file *io_log_init(void);
void io_log_print(struct io_file *, int, const char *); void io_log_print(struct io_file *, int, const char *);
void io_log_display(struct io_file *, const char *, const char *); void io_log_display(struct io_file *, const char *, const char *);

View File

@ -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); 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); mem_free(mesg);
erase_note(&note); erase_note(&note);
} }
static void static void
ical_store_event(char *mesg, char *note, long day, long end, 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; const int EVENTID = 1;
struct event *ev;
struct recur_event *rev;
if (rpt) { if (rpt) {
recur_event_new(mesg, note, day, EVENTID, rpt->type, rev = recur_event_new(mesg, note, day, EVENTID, rpt->type,
rpt->freq, rpt->until, exc); rpt->freq, rpt->until, exc);
mem_free(rpt); mem_free(rpt);
if (list) {
char *hash = recur_event_hash(rev);
printf("%s\n", hash);
mem_free(hash);
}
goto cleanup; goto cleanup;
} }
if (end == 0 || end - day <= DAYINSEC) { 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; goto cleanup;
} }
@ -319,9 +336,14 @@ ical_store_event(char *mesg, char *note, long day, long end,
rpt->freq = 1; rpt->freq = 1;
rpt->count = 0; rpt->count = 0;
rpt->until = end; rpt->until = end;
recur_event_new(mesg, note, day, EVENTID, rpt->type, rev = recur_event_new(mesg, note, day, EVENTID, rpt->type,
rpt->freq, rpt->until, exc); rpt->freq, rpt->until, exc);
mem_free(rpt); mem_free(rpt);
if (list) {
char *hash = recur_event_hash(rev);
printf("%s\n", hash);
mem_free(hash);
}
cleanup: cleanup:
mem_free(mesg); mem_free(mesg);
@ -330,18 +352,30 @@ cleanup:
static void static void
ical_store_apoint(char *mesg, char *note, long start, long dur, 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; char state = 0L;
struct apoint *apt;
struct recur_apoint *rapt;
if (has_alarm) if (has_alarm)
state |= APOINT_NOTIFY; state |= APOINT_NOTIFY;
if (rpt) { if (rpt) {
recur_apoint_new(mesg, note, start, dur, state, rpt->type, rapt = recur_apoint_new(mesg, note, start, dur, state,
rpt->freq, rpt->until, exc); rpt->type, rpt->freq, rpt->until, exc);
mem_free(rpt); mem_free(rpt);
if (list) {
char *hash = recur_apoint_hash(rapt);
printf("%s\n", hash);
mem_free(hash);
}
} else { } 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); mem_free(mesg);
erase_note(&note); erase_note(&note);
@ -833,7 +867,7 @@ static char *ical_read_summary(char *line)
} }
static void 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, unsigned *noapoints, unsigned *noskipped, char *buf,
char *lstore, unsigned *lineno) char *lstore, unsigned *lineno)
{ {
@ -906,13 +940,13 @@ ical_read_event(FILE * fdi, FILE * log, unsigned *noevents,
ical_store_apoint(vevent.mesg, vevent.note, ical_store_apoint(vevent.mesg, vevent.note,
vevent.start, vevent.dur, vevent.start, vevent.dur,
vevent.rpt, &vevent.exc, vevent.rpt, &vevent.exc,
vevent.has_alarm); vevent.has_alarm, list);
(*noapoints)++; (*noapoints)++;
break; break;
case EVENT: case EVENT:
ical_store_event(vevent.mesg, vevent.note, ical_store_event(vevent.mesg, vevent.note,
vevent.start, vevent.end, vevent.start, vevent.end,
vevent.rpt, &vevent.exc); vevent.rpt, &vevent.exc, list);
(*noevents)++; (*noevents)++;
break; break;
case UNDEFINED: case UNDEFINED:
@ -993,7 +1027,7 @@ cleanup:
} }
static void 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 *noskipped, char *buf, char *lstore,
unsigned *lineno) unsigned *lineno)
{ {
@ -1028,7 +1062,7 @@ ical_read_todo(FILE * fdi, FILE * log, unsigned *notodos,
} }
ical_store_todo(vtodo.priority, vtodo.mesg, ical_store_todo(vtodo.priority, vtodo.mesg,
vtodo.note); vtodo.note, list);
(*notodos)++; (*notodos)++;
return; return;
} }
@ -1066,7 +1100,7 @@ cleanup:
/* Import calcurse data. */ /* Import calcurse data. */
void 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 *apoints, unsigned *todos, unsigned *lines,
unsigned *skipped) unsigned *skipped)
{ {
@ -1084,10 +1118,10 @@ ical_import_data(FILE * stream, FILE * log, unsigned *events,
while (ical_readline(stream, buf, lstore, lines)) { while (ical_readline(stream, buf, lstore, lines)) {
(*lines)++; (*lines)++;
if (starts_with_ci(buf, "BEGIN:VEVENT")) { 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); skipped, buf, lstore, lines);
} else if (starts_with_ci(buf, "BEGIN:VTODO")) { } 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); lstore, lines);
} }
} }

View File

@ -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, * A temporary log file is created in /tmp to store the import process report,
* and is cleared at the end. * 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 = const char *proc_report =
_("Import process report: %04d lines read"); _("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) 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.apoints, &stats.todos,
&stats.lines, &stats.skipped); &stats.lines, &stats.skipped);