Add support for XDG_DATA_HOME

This commit is contained in:
Pierre Pronchery 2015-06-18 00:15:59 -04:00
parent df42b7894d
commit cdafa7ad6b

View File

@ -74,31 +74,41 @@ static int _bookmark_do(char const * title, char const * url, char const * icon,
int ret = 0; int ret = 0;
const char section[] = "Desktop Entry"; const char section[] = "Desktop Entry";
char const * homedir; char const * homedir;
String * filename = NULL; String * datahome;
String * pathname = NULL;
String * filename;
Config * config; Config * config;
String * p;
if((homedir = getenv("HOME")) == NULL) if((homedir = getenv("XDG_DATA_HOME")) != NULL)
homedir = g_get_home_dir(); datahome = string_new(homedir);
if((p = string_new_append(homedir, "/.local/share/applications", NULL)) else
== NULL) {
if((homedir = getenv("HOME")) == NULL)
homedir = g_get_home_dir();
datahome = string_new_append(homedir,
"/.local/share/applications", NULL);
}
if(datahome == NULL)
return -1; return -1;
if(mkdir(p, 0755) != 0 && errno != EEXIST) if(mkdir(datahome, 0755) != 0 && errno != EEXIST)
ret = error_set_code(1, "%s: %s", p, strerror(errno)); ret = error_set_code(1, "%s: %s", datahome, strerror(errno));
string_delete(p);
if(ret != 0) if(ret != 0)
{
string_delete(datahome);
return -1; return -1;
}
if(title == NULL) if(title == NULL)
title = url; title = url;
if((p = string_new(title)) == NULL) if((filename = string_new(title)) == NULL)
return -1; return -1;
string_replace(&p, "/", "_"); if(string_replace(&filename, "/", "_") != 0
if((filename = string_new_append(homedir, "/.local/share/applications/", || (pathname = string_new_append(datahome, "/",
p, ".desktop", NULL)) == NULL filename, ".desktop", NULL)) == NULL
|| (config = config_new()) == NULL) || (config = config_new()) == NULL)
{ {
string_delete(pathname);
string_delete(filename); string_delete(filename);
string_delete(p); string_delete(datahome);
return -1; return -1;
} }
if((ret = config_set(config, section, "Type", "URL")) != 0 if((ret = config_set(config, section, "Type", "URL")) != 0
@ -107,11 +117,12 @@ static int _bookmark_do(char const * title, char const * url, char const * icon,
|| (ret = config_set(config, section, "Icon", icon)) || (ret = config_set(config, section, "Icon", icon))
|| (ret = config_set(config, section, "Comment", || (ret = config_set(config, section, "Comment",
comment)) != 0 comment)) != 0
|| (ret = config_save(config, filename)) != 0) || (ret = config_save(config, pathname)) != 0)
ret = -1; ret = -1;
config_delete(config); config_delete(config);
string_delete(filename); string_delete(filename);
string_delete(p); string_delete(pathname);
string_delete(datahome);
return ret; return ret;
} }