/*
- * Copyright (C) 2010-2012 jeanfi@gmail.com
+ * Copyright (C) 2012-2016 jeanfi@gmail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
#include <sys/stat.h>
#include <sys/types.h>
+#include <log.h>
#include <note.h>
+#include <pio.h>
+#include <settings.h>
-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;
}
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_notes_dir();
+
+ if (sdir == NULL || *sdir == '\0') {
+ dir = get_default_path();
+ settings_set_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);
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");
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);
}
fclose(f);
} else {
- fprintf(stderr, "Failed to open %s\n", path);
+ log_debug("%s does not exist or cannot be opened", path);
}
return str;