X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Fnote.c;h=be9d87a0e25db084b40348ee5dd98c742b0d5c45;hb=909b5e1edc01d95ebfaf39982b69831d8503c74c;hp=d9ce755d8b9cb0739b5a9ef7c9a1583c54a695a6;hpb=41ddd7f6aed1015af7c197f039966089d7105600;p=ptask.git diff --git a/src/note.c b/src/note.c index d9ce755..be9d87a 100644 --- a/src/note.c +++ b/src/note.c @@ -23,16 +23,21 @@ #include #include +#include #include +#include +#include -static char *get_path(const char *uuid) +static const char *NOTE_SUF = ".note"; + +static char *get_default_path() { - char *home, *dir, *path; + char *home, *dir; home = getenv("HOME"); if (!home) { - fprintf(stderr, "HOME environment variable not defined\n"); + log_err("HOME environment variable not defined"); return NULL; } @@ -40,8 +45,27 @@ static char *get_path(const char *uuid) sprintf(dir, "%s/%s", home, ".task"); mkdir(dir, 0777); - path = malloc(strlen(dir) + 1 + strlen(uuid) + strlen(".note") + 1); - sprintf(path, "%s/%s.note", dir, uuid); + return dir; +} + +static char *get_path(const char *uuid) +{ + const char *sdir; + char *path, *dir; + + sdir = settings_get_str(SETTINGS_KEY_NOTES_DIR); + + if (sdir == NULL || *sdir == '\0') { + dir = get_default_path(); + settings_set_str(SETTINGS_KEY_NOTES_DIR, dir); + } else { + dir = strdup(sdir); + } + + mkdirs(dir, 0777); + + path = malloc(strlen(dir) + 1 + strlen(uuid) + strlen(NOTE_SUF) + 1); + sprintf(path, "%s/%s%s", dir, uuid, NOTE_SUF); free(dir); @@ -58,7 +82,7 @@ void note_put(const char *uuid, const char *note) if (!path) return ; - printf("note_put %s %s %s\n", path, uuid, note); + log_debug("note_put %s %s %s", path, uuid, note); f = fopen(path, "w"); @@ -66,7 +90,7 @@ void note_put(const char *uuid, const char *note) fwrite(note, 1, strlen(note), f); fclose(f); } else { - fprintf(stderr, "Failed to open %s\n", path); + log_err("Failed to open %s", path); } free(path); @@ -99,7 +123,7 @@ char *note_get(const char *uuid) } fclose(f); } else { - fprintf(stderr, "Failed to open %s\n", path); + log_debug("%s does not exist or cannot be opened", path); } return str;