commit 3f582f78e7fcd5169b6243d5951581a0d9f2015a
parent 8a69311205582739c117b2f78a3b8c7e9f7414a8
Author: Andreas Gruhler <agruhl@gmx.ch>
Date: Sat, 22 May 2021 12:38:11 +0200
add extract_ical_field()
Diffstat:
6 files changed, 49 insertions(+), 19 deletions(-)
diff --git a/caldav.c b/caldav.c
@@ -484,7 +484,7 @@ void put_event(struct tm* date) {
}
-void caldav_sync(struct tm* date, WINDOW* header) {
+void caldav_sync(struct tm* date, WINDOW* header, WINDOW* cal, int pad_pos) {
// fetch existing API tokens
read_tokenfile();
@@ -559,7 +559,7 @@ void caldav_sync(struct tm* date, WINDOW* header) {
char dstr_cursor[30];
char dstr_next_day[30];
- char* format = "%Y%m%dT%H%M%SZ";
+ char* format = "%Y%m%dT000000Z";
strftime(dstr_cursor, sizeof dstr_cursor, format, date);
strftime(dstr_next_day, sizeof dstr_next_day, format, next_day);
@@ -574,6 +574,8 @@ void caldav_sync(struct tm* date, WINDOW* header) {
fprintf(stderr, "\nCalendar URI: %s\n", uri);
char* event = caldav_req(date, uri, "REPORT", caldata_postfields, 0);
fprintf(stderr, "Event:\n%s", event);
+ // todo: warn if multiple events,
+ // multistatus has more than just one caldav:calendar-data elements
// get path of entry
char path[100];
@@ -595,14 +597,13 @@ void caldav_sync(struct tm* date, WINDOW* header) {
struct tm remote_datetime;
time_t remote_date;
- // check remote LAST-MODIFIED:20210521T212441Z
- char* remote_last_mod = strstr(event, "LAST-MODIFIED:");
+ // check remote LAST-MODIFIED:20210521T212441Z of remote event
+ char* remote_last_mod = extract_ical_field(event, "LAST-MODIFIED");
+ fprintf(stderr, "Remote last modified: %s\n", remote_last_mod);
if (remote_last_mod == NULL) {
remote_file_exists = false;
} else {
- remote_last_mod = strtok(remote_last_mod, ":");
- remote_last_mod = strtok(NULL, "\n");
- strptime(remote_last_mod, format, &remote_datetime);
+ strptime(remote_last_mod, "%Y%m%dT%H%M%SZ", &remote_datetime);
remote_date = mktime(&remote_datetime);
fprintf(stderr, "Remote last modified: %s\n", ctime(&remote_date));
}
@@ -628,15 +629,19 @@ void caldav_sync(struct tm* date, WINDOW* header) {
//put_event(date);
}
+ char* remote_desc;
if (timediff < 0 && remote_file_exists) {
//todo: Warn - really sync? remote is more recent and will overwrite
- fprintf(stderr, "Remote file is newer, extracting description from remote...\n");
- char* remote_desc = strstr(event, "DESCRIPTION:");
- remote_desc = strtok(remote_desc, ":");
- remote_desc = strtok(NULL, "\n");
+ //fprintf(stderr, "Remote file is newer, extracting description from remote...\n");
+
+ remote_desc = extract_ical_field(event, "DESCRIPTION");
fprintf(stderr, "Remote event description:%s\n", remote_desc);
+ if (remote_desc == NULL) {
+ fprintf(stderr, "Failed to fetch description of remote event.\n");
+ return;
+ }
- // else persist downloaded buffer to local file
+ // persist downloaded buffer to local file
FILE* cursordate_file = fopen(path, "wb");
if (cursordate_file == NULL) {
perror("Failed to open cursor date file");
@@ -644,7 +649,10 @@ void caldav_sync(struct tm* date, WINDOW* header) {
fprintf(cursordate_file, remote_desc);
}
fclose(cursordate_file);
- }
- // todo: update bold face cursor date
+ // add new entry highlight
+ chtype atrs = winch(cal) & A_ATTRIBUTES;
+ wchgat(cal, 2, atrs | A_BOLD, 0, NULL);
+ prefresh(cal, pad_pos, 0, 1, ASIDE_WIDTH, LINES - 1, ASIDE_WIDTH + CAL_WIDTH);
+ }
}
diff --git a/caldav.h b/caldav.h
@@ -37,7 +37,7 @@
#define GOOGLE_API_URI "https://apidata.googleusercontent.com"
#define GOOGLE_CALDAV_URI GOOGLE_API_URI "/caldav/v2"
-void caldav_sync(struct tm* date, WINDOW* header);
+void caldav_sync(struct tm* date, WINDOW* header, WINDOW* cal, int pad_pos);
struct curl_mem_chunk {
char* memory;
diff --git a/diary.c b/diary.c
@@ -679,7 +679,7 @@ int main(int argc, char** argv) {
case 's':
//strftime(dstr, sizeof dstr, CONFIG.fmt, &curs_date);
mktime(&curs_date);
- caldav_sync(&curs_date, header);
+ caldav_sync(&curs_date, header, cal, pad_pos);
break;
}
diff --git a/diary.h b/diary.h
@@ -24,9 +24,6 @@
#define XDG_CONFIG_HOME_FALLBACK "~/.config"
#define CONFIG_FILE_PATH "diary/diary.cfg"
#define DIARY_VERSION "0.6-unstable"
-#define CAL_WIDTH 21
-#define ASIDE_WIDTH 4
-#define MAX_MONTH_HEIGHT 6
static const char* WEEKDAYS[] = {"Su","Mo","Tu","We","Th","Fr","Sa"};
diff --git a/utils.c b/utils.c
@@ -24,6 +24,26 @@ char* extract_json_value(char* json, char* key, bool quoted) {
return tok;
}
+char* extract_ical_field(char* ical, char* key) {
+ // work on a copy of the ical xml response
+ char* field = (char *) malloc(strlen(ical) * sizeof(char));
+ strcpy(field, ical);
+
+ field = strtok(field, "\n");
+ while (field != NULL) {
+ if (strstr(field, key) != NULL) {
+ fprintf(stderr, "field: %s\n", field);
+ field = strstr(field, ":"); // value
+ field++; // strip the ":"
+ break;
+ }
+ // key was not in this field, advance field
+ field = strtok(NULL, "\n");
+ }
+
+ return field;
+}
+
// Return expanded file path
char* expand_path(char* str) {
char* res;
diff --git a/utils.h b/utils.h
@@ -16,7 +16,12 @@
#define GOOGLE_OAUTH_CLIENT_SECRET ""
#endif
+#define CAL_WIDTH 21
+#define ASIDE_WIDTH 4
+#define MAX_MONTH_HEIGHT 6
+
char* extract_json_value(char* json, char* key, bool quoted);
+char* extract_ical_field(char* ical, char* key);
char* expand_path(char* str);
char* strrstr(char *haystack, char *needle);
void fpath(const char* dir, size_t dir_size, const struct tm* date, char** rpath, size_t rpath_size);