Add XDG Base Directory Support

* Use "$XDG_DATA_HOME/calcurse" for data files
* Use "$XDG_CONFIG_HOME/calcurse" for config files
* "$XDG_DATA_HOME" defaults to "$HOME/.local/share"
* "$XDG_CONFIG_HOME" defaults to "$HOME/.config"
* If "$HOME/.calcurse" exists, then it will be used instead for backward
compatibility.

Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
Nitroretro 2019-12-17 02:34:42 +02:00 committed by Lukas Fleischer
parent 250a233ff7
commit 04162de6dd
2 changed files with 37 additions and 9 deletions

View File

@ -84,7 +84,8 @@
#endif /* ENABLE_NLS */ #endif /* ENABLE_NLS */
/* Paths configuration. */ /* Paths configuration. */
#define DIR_NAME ".calcurse/" #define DIR_NAME "calcurse/"
#define DIR_NAME_LEGACY ".calcurse/"
#define TODO_PATH_NAME "todo" #define TODO_PATH_NAME "todo"
#define APTS_PATH_NAME "apts" #define APTS_PATH_NAME "apts"
#define CONF_PATH_NAME "conf" #define CONF_PATH_NAME "conf"

View File

@ -143,17 +143,44 @@ unsigned io_fprintln(const char *fname, const char *fmt, ...)
*/ */
void io_init(const char *cfile, const char *datadir, const char *confdir) void io_init(const char *cfile, const char *datadir, const char *confdir)
{ {
if (!datadir) { char* home_dir = getenv("HOME");
if (!(datadir = getenv("HOME"))) char* legacy_dir = NULL;
datadir = ".";
asprintf(&path_ddir, "%s%s", datadir, "/" DIR_NAME);
} else
asprintf(&path_ddir, "%s%s", datadir, "/");
if (!confdir) if (home_dir) {
asprintf(&path_cdir, "%s%s", path_ddir, "/"); asprintf(&legacy_dir, "%s%s", home_dir, "/" DIR_NAME_LEGACY);
if (!io_dir_exists(legacy_dir)) {
mem_free(legacy_dir);
legacy_dir = NULL;
}
}
if (datadir)
asprintf(&path_ddir, "%s%s", datadir, "/");
else if (legacy_dir)
path_ddir = mem_strdup(legacy_dir);
else if ((path_ddir = getenv("XDG_DATA_HOME")))
asprintf(&path_ddir, "%s%s", path_ddir, "/" DIR_NAME);
else if (home_dir)
asprintf(&path_ddir, "%s%s", home_dir, "/.local/share/" DIR_NAME);
else else
path_ddir = mem_strdup("./." DIR_NAME);
if (confdir)
asprintf(&path_cdir, "%s%s", confdir, "/"); asprintf(&path_cdir, "%s%s", confdir, "/");
else if (datadir)
path_cdir = mem_strdup(path_ddir);
else if (legacy_dir)
path_cdir = mem_strdup(legacy_dir);
else if ((path_cdir = getenv("XDG_CONFIG_HOME")))
asprintf(&path_cdir, "%s%s", path_cdir, "/" DIR_NAME);
else if (home_dir)
asprintf(&path_cdir, "%s%s", home_dir, "/.config/" DIR_NAME);
else
path_cdir = mem_strdup("./." DIR_NAME);
if (legacy_dir)
mem_free(legacy_dir);
/* Data files */ /* Data files */
if (cfile) { if (cfile) {