From 281a1a206e6086c726544e07eba3c7cb667c4769 Mon Sep 17 00:00:00 2001 From: Dianne Skoll Date: Thu, 27 Feb 2020 15:18:23 -0500 Subject: [PATCH] Implement JSONQUEUE daemon command. --- scripts/tkremind | 19 +++++++------- src/calendar.c | 25 +++++++++++++++---- src/protos.h | 6 +++++ src/queue.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/test.cmp | 3 ++- tests/test.rem | 3 +++ 6 files changed, 106 insertions(+), 15 deletions(-) diff --git a/scripts/tkremind b/scripts/tkremind index 604c038e..463a950d 100755 --- a/scripts/tkremind +++ b/scripts/tkremind @@ -582,7 +582,7 @@ proc ConfigureCalFrame { w firstDay numDays } { proc DoQueue {} { global DaemonFile - puts $DaemonFile "QUEUE" + puts $DaemonFile "JSONQUEUE" flush $DaemonFile } @@ -2418,18 +2418,19 @@ proc ShowQueue { file } { grid columnconfigure $w 1 -weight 0 grid rowconfigure $w 0 -weight 1 grid rowconfigure $w 1 -weight 0 - set did 0 CenterWindow $w . while (1) { + # We should only get one line gets $file line - if {$line == "NOTE endqueue"} { + if {$line == "NOTE ENDJSONQUEUE"} { break } - set did 1 - $w.t insert end "$line\n" - } - if {!$did} { - $w.t insert end "*** Queue is empty ***\n" + if {[catch {set obj [::json::many-json2dict $line]}]} { + continue; + } + foreach q $obj { + $w.t insert end "$q\n" + } } $w.t configure -state disabled } @@ -2456,7 +2457,7 @@ proc DaemonReadable { file } { scan $line "NOTE reminder %s %s %s" time now tag IssueBackgroundReminder $file $time $now $tag } - "NOTE queue" { + "NOTE JSONQUEUE" { ShowQueue $file } "NOTE newdate" { diff --git a/src/calendar.c b/src/calendar.c index 6818aa8a..be450820 100644 --- a/src/calendar.c +++ b/src/calendar.c @@ -245,7 +245,7 @@ static void WriteBottomCalLine (void); static void WriteIntermediateCalLine (void); static void WriteCalDays (void); -static void PrintJSONString(char const *s) +void PrintJSONString(char const *s) { while (*s) { switch(*s) { @@ -262,14 +262,14 @@ static void PrintJSONString(char const *s) } } -static void PrintJSONKeyPairInt(char const *name, int val) +void PrintJSONKeyPairInt(char const *name, int val) { printf("\""); PrintJSONString(name); printf("\":%d, ", val); } -static void PrintJSONKeyPairString(char const *name, char const *val) +void PrintJSONKeyPairString(char const *name, char const *val) { /* If value is blank, skip it! */ if (!val || !*val) { @@ -283,7 +283,7 @@ static void PrintJSONKeyPairString(char const *name, char const *val) printf("\", "); } -static void PrintJSONKeyPairDate(char const *name, int jul) +void PrintJSONKeyPairDate(char const *name, int jul) { int y, m, d; if (jul == NO_DATE) { @@ -297,7 +297,7 @@ static void PrintJSONKeyPairDate(char const *name, int jul) } -static void PrintJSONKeyPairDateTime(char const *name, int dt) +void PrintJSONKeyPairDateTime(char const *name, int dt) { int y, m, d, h, i, k; if (dt == NO_TIME) { @@ -315,6 +315,21 @@ static void PrintJSONKeyPairDateTime(char const *name, int dt) } +void PrintJSONKeyPairTime(char const *name, int t) +{ + int h, i; + if (t == NO_TIME) { + /* Skip it! */ + return; + } + h = t / 60; + i = t % 60; + printf("\""); + PrintJSONString(name); + printf("\":\"%02d:%02d\", ", h, i); + +} + #ifdef REM_USE_WCHAR static void PutWideChar(wchar_t const wc) { diff --git a/src/protos.h b/src/protos.h index 0b0358b8..a87039c4 100644 --- a/src/protos.h +++ b/src/protos.h @@ -154,3 +154,9 @@ void SaveAllTriggerInfo(Trigger const *t, TimeTrig const *tt, int trigdate, int void PerIterationInit(void); char const *Decolorize(int r, int g, int b); char const *Colorize(int r, int g, int b); +void PrintJSONString(char const *s); +void PrintJSONKeyPairInt(char const *name, int val); +void PrintJSONKeyPairString(char const *name, char const *val); +void PrintJSONKeyPairDate(char const *name, int jul); +void PrintJSONKeyPairDateTime(char const *name, int dt); +void PrintJSONKeyPairTime(char const *name, int t); diff --git a/src/queue.c b/src/queue.c index 9d103158..ec60340a 100644 --- a/src/queue.c +++ b/src/queue.c @@ -448,6 +448,66 @@ static int CalculateNextTimeUsingSched(QueuedRem *q) } } +/* Dump the queue in JSON format */ +static void +json_queue(QueuedRem const *q) +{ + printf("["); + while(q) { + if (q->tt.nexttime == NO_TIME) { + q = q->next; + continue; + } + + printf("{"); + switch(q->typ) { + case NO_TYPE: PrintJSONKeyPairString("type", "NO_TYPE "); break; + case MSG_TYPE: PrintJSONKeyPairString("type", "MSG_TYPE "); break; + case RUN_TYPE: PrintJSONKeyPairString("type", "RUN_TYPE "); break; + case CAL_TYPE: PrintJSONKeyPairString("type", "CAL_TYPE "); break; + case SAT_TYPE: PrintJSONKeyPairString("type", "SAT_TYPE "); break; + case PS_TYPE: PrintJSONKeyPairString("type", "PS_TYPE "); break; + case PSF_TYPE: PrintJSONKeyPairString("type", "PSF_TYPE "); break; + case MSF_TYPE: PrintJSONKeyPairString("type", "MSF_TYPE "); break; + case PASSTHRU_TYPE: PrintJSONKeyPairString("type", "PASSTHRU_TYPE "); break; + default: PrintJSONKeyPairString("type", "? "); break; + } + PrintJSONKeyPairInt("rundisabled", q->RunDisabled); + PrintJSONKeyPairInt("ntrig", q->ntrig); + PrintJSONKeyPairTime("ttime", q->tt.ttime); + PrintJSONKeyPairTime("nextttime", q->tt.nexttime); + PrintJSONKeyPairInt("delta", q->tt.delta); + if (q->tt.rep != NO_TIME) { + PrintJSONKeyPairInt("rep", q->tt.rep); + } + if (q->tt.duration != NO_TIME) { + PrintJSONKeyPairInt("duration", q->tt.duration); + } + if (q->passthru[0]) { + PrintJSONKeyPairString("passthru", q->passthru); + } + if (q->sched[0]) { + PrintJSONKeyPairString("sched", q->sched); + } + if (DBufLen(&(q->tags))) { + PrintJSONKeyPairString("tags", DBufValue(&(q->tags))); + } + + /* Last one is a special case - no trailing comma */ + printf("\""); + PrintJSONString("body"); + printf("\":\""); + if (q->text) { + PrintJSONString(q->text); + } else { + PrintJSONString(""); + } + printf("\"}"); + q = q->next; + } + printf("]\n"); +} + /***************************************************************/ /* */ /* DaemonWait */ @@ -532,6 +592,11 @@ static void DaemonWait(unsigned int sleeptime) } printf("NOTE endqueue\n"); fflush(stdout); + } else if (!strcmp(cmdLine, "JSONQUEUE\n")) { + printf("NOTE JSONQUEUE\n"); + json_queue(QueueHead); + printf("NOTE ENDJSONQUEUE\n"); + fflush(stdout); } else if (!strcmp(cmdLine, "REREAD\n")) { printf("NOTE reread\n"); fflush(stdout); diff --git a/tests/test.cmp b/tests/test.cmp index 59ec05d4..01ded989 100644 --- a/tests/test.cmp +++ b/tests/test.cmp @@ -1833,7 +1833,8 @@ coerce("DATETIME", "2020-05-05@12:45") => 2020-05-05@12:45 set x coerce("DATETIME", "2020-05-05@1:45pm") coerce("DATETIME", "2020-05-05@1:45pm") => 2020-05-05@13:45 - +# Don't want Remind to queue reminders +EXIT Test 2 diff --git a/tests/test.rem b/tests/test.rem index 155add95..2659c6a6 100644 --- a/tests/test.rem +++ b/tests/test.rem @@ -525,6 +525,9 @@ set x coerce("DATETIME", "2020-05-05@12:45am") set x coerce("DATETIME", "2020-05-05@12:45") set x coerce("DATETIME", "2020-05-05@1:45pm") +# Don't want Remind to queue reminders +EXIT + __EOF__ REM This line should not even be seen And you can put whatever you like here.