From 305e8c86d91c9261943bf9cbeb402822f2ee5c43 Mon Sep 17 00:00:00 2001 From: Ray Strode Date: Wed, 11 Aug 2004 17:53:05 +0000 Subject: [PATCH] Add new XDG basedir API (bug 139973). 2004-08-11 Ray Strode * docs/reference/glib/glib-sections.txt: * glib/gutils.[ch] (g_get_user_data_dir), (g_get_user_config_dir), (g_get_user_cache_dir), (g_get_system_data_dirs), (g_get_system_config_dirs): Add new XDG basedir API (bug 139973). --- ChangeLog | 8 ++ ChangeLog.pre-2-10 | 8 ++ ChangeLog.pre-2-12 | 8 ++ ChangeLog.pre-2-6 | 8 ++ ChangeLog.pre-2-8 | 8 ++ docs/reference/glib/glib-sections.txt | 5 + glib/gutils.c | 205 ++++++++++++++++++++++++++++++++++ glib/gutils.h | 6 +- 8 files changed, 255 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 5fdb078..58582a4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2004-08-11 Ray Strode + + * docs/reference/glib/glib-sections.txt: + * glib/gutils.[ch] (g_get_user_data_dir), + (g_get_user_config_dir), (g_get_user_cache_dir), + (g_get_system_data_dirs), (g_get_system_config_dirs): + Add new XDG basedir API (bug 139973). + 2004-08-10 Matthias Clasen * glib/gmacros.h: Remove G_GNUC_INTERNAL from here. diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 5fdb078..58582a4 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,11 @@ +2004-08-11 Ray Strode + + * docs/reference/glib/glib-sections.txt: + * glib/gutils.[ch] (g_get_user_data_dir), + (g_get_user_config_dir), (g_get_user_cache_dir), + (g_get_system_data_dirs), (g_get_system_config_dirs): + Add new XDG basedir API (bug 139973). + 2004-08-10 Matthias Clasen * glib/gmacros.h: Remove G_GNUC_INTERNAL from here. diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 5fdb078..58582a4 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,11 @@ +2004-08-11 Ray Strode + + * docs/reference/glib/glib-sections.txt: + * glib/gutils.[ch] (g_get_user_data_dir), + (g_get_user_config_dir), (g_get_user_cache_dir), + (g_get_system_data_dirs), (g_get_system_config_dirs): + Add new XDG basedir API (bug 139973). + 2004-08-10 Matthias Clasen * glib/gmacros.h: Remove G_GNUC_INTERNAL from here. diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 5fdb078..58582a4 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,11 @@ +2004-08-11 Ray Strode + + * docs/reference/glib/glib-sections.txt: + * glib/gutils.[ch] (g_get_user_data_dir), + (g_get_user_config_dir), (g_get_user_cache_dir), + (g_get_system_data_dirs), (g_get_system_config_dirs): + Add new XDG basedir API (bug 139973). + 2004-08-10 Matthias Clasen * glib/gmacros.h: Remove G_GNUC_INTERNAL from here. diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 5fdb078..58582a4 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,11 @@ +2004-08-11 Ray Strode + + * docs/reference/glib/glib-sections.txt: + * glib/gutils.[ch] (g_get_user_data_dir), + (g_get_user_config_dir), (g_get_user_cache_dir), + (g_get_system_data_dirs), (g_get_system_config_dirs): + Add new XDG basedir API (bug 139973). + 2004-08-10 Matthias Clasen * glib/gmacros.h: Remove G_GNUC_INTERNAL from here. diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 37e2134..14c6601 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -1261,6 +1261,11 @@ g_setenv g_unsetenv g_get_user_name g_get_real_name +g_get_user_cache_dir +g_get_user_data_dir +g_get_user_config_dir +g_get_system_data_dirs +g_get_system_config_dirs g_get_home_dir diff --git a/glib/gutils.c b/glib/gutils.c index 366c044..9512b74 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -30,6 +30,8 @@ #include "config.h" +#include + #ifdef HAVE_UNISTD_H #include #endif @@ -884,6 +886,12 @@ static gchar *g_user_name = NULL; static gchar *g_real_name = NULL; static gchar *g_home_dir = NULL; +static gchar *g_user_data_dir = NULL; +static gchar **g_system_data_dirs = NULL; +static gchar *g_user_cache_dir = NULL; +static gchar *g_user_config_dir = NULL; +static gchar **g_system_config_dirs = NULL; + /* HOLDS: g_utils_global_lock */ static void g_get_any_init (void) @@ -1238,6 +1246,203 @@ g_set_application_name (const gchar *application_name) g_warning ("g_set_application() name called multiple times"); } +/** + * g_get_user_data_dir: + * + * Returns a base directory in which to access application data such + * as icons that is customized for a particular user. + * + * On Unix platforms this is determined using the mechanisms described in + * the + * XDG Base Directory Specification + * + * Return value: a string owned by GLib that must not be modified + * or freed. + * Since: 2.6 + **/ +G_CONST_RETURN gchar* +g_get_user_data_dir (void) +{ + gchar *data_dir; + + G_LOCK (g_utils_global); + + if (!g_user_data_dir) + { + data_dir = (gchar *) g_getenv ("XDG_DATA_HOME"); + + if (data_dir && data_dir[0]) + data_dir = g_strdup (data_dir); + else + data_dir = g_build_filename (g_get_home_dir (), ".local", + "share", NULL); + + g_user_data_dir = data_dir; + } + else + data_dir = g_user_data_dir; + + G_UNLOCK (g_utils_global); + + return data_dir; +} + +/** + * g_get_user_config_dir: + * + * Returns a base directory in which to store user-specific application + * configuration information such as user preferences and settings. + * + * On Unix platforms this is determined using the mechanisms described in + * the + * XDG Base Directory Specification + * + * Return value: a string owned by GLib that must not be modified + * or freed. + * Since: 2.6 + **/ +G_CONST_RETURN gchar* +g_get_user_config_dir (void) +{ + gchar *config_dir; + + G_LOCK (g_utils_global); + + if (!g_user_config_dir) + { + config_dir = (gchar *) g_getenv ("XDG_CONFIG_HOME"); + + if (config_dir && config_dir[0]) + config_dir = g_strdup (config_dir); + else + config_dir = g_build_filename (g_get_home_dir (), ".config", NULL); + + g_user_config_dir = config_dir; + } + else + config_dir = g_user_config_dir; + + G_UNLOCK (g_utils_global); + + return config_dir; +} + +/** + * g_get_user_cache_dir: + * + * Returns a base directory in which to store non-essential, cached + * data specific to particular user. + * + * On Unix platforms this is determined using the mechanisms described in + * the + * XDG Base Directory Specification + * + * Return value: a string owned by GLib that must not be modified + * or freed. + * Since: 2.6 + **/ +G_CONST_RETURN gchar* +g_get_user_cache_dir (void) +{ + gchar *cache_dir; + + G_LOCK (g_utils_global); + + if (!g_user_cache_dir) + { + cache_dir = (gchar *) g_getenv ("XDG_CACHE_HOME"); + + if (cache_dir && cache_dir[0]) + cache_dir = g_strdup (cache_dir); + else + cache_dir = g_build_filename (g_get_home_dir (), ".cache", NULL); + g_user_cache_dir = cache_dir; + } + else + cache_dir = g_user_cache_dir; + + G_UNLOCK (g_utils_global); + + return cache_dir; +} + +/** + * g_get_system_data_dirs: + * + * Returns an ordered list of base directories in which to access + * system-wide application data. + * + * On Unix platforms this is determined using the mechanisms described in + * the + * XDG Base Directory Specification + * + * Return value: a %NULL-terminated array of strings owned by GLib that must + * not be modified or freed. + * Since: 2.6 + **/ +G_CONST_RETURN gchar * G_CONST_RETURN * +g_get_system_data_dirs (void) +{ + gchar *data_dirs, **data_dir_vector; + + G_LOCK (g_utils_global); + + if (!g_system_data_dirs) + { + data_dirs = (gchar *) g_getenv ("XDG_DATA_DIRS"); + + if (!data_dirs || !data_dirs[0]) + data_dirs = "/usr/local/share/:/usr/share/"; + + data_dir_vector = g_strsplit (data_dirs, ":", 0); + + g_system_data_dirs = data_dir_vector; + } + else + data_dir_vector = g_system_data_dirs; + + G_UNLOCK (g_utils_global); + + return (G_CONST_RETURN gchar * G_CONST_RETURN *) data_dir_vector; +} + +/** + * g_get_system_config_dirs: + * + * Returns an ordered list of base directories in which to access + * system-wide configuration information. + * + * On Unix platforms this is determined using the mechanisms described in + * the + * XDG Base Directory Specification + * + * Return value: a %NULL-terminated array of strings owned by GLib that must + * not be modified or freed. + * Since: 2.6 + **/ +G_CONST_RETURN gchar * G_CONST_RETURN * +g_get_system_config_dirs (void) +{ + gchar *conf_dirs, **conf_dir_vector; + + G_LOCK (g_utils_global); + + if (!g_system_config_dirs) + { + conf_dirs = (gchar *) g_getenv ("XDG_CONFIG_DIRS"); + + if (!conf_dirs || !conf_dirs[0]) + conf_dirs = "/etc/xdg"; + + conf_dir_vector = g_strsplit (conf_dirs, ":", 0); + } + else + conf_dir_vector = g_system_config_dirs; + G_UNLOCK (g_utils_global); + + return (G_CONST_RETURN gchar * G_CONST_RETURN *) conf_dir_vector; +} + guint g_direct_hash (gconstpointer v) { diff --git a/glib/gutils.h b/glib/gutils.h index 3307e8a..73084af 100644 --- a/glib/gutils.h +++ b/glib/gutils.h @@ -122,6 +122,11 @@ void g_set_prgname (const gchar *prgname); G_CONST_RETURN gchar* g_get_application_name (void); void g_set_application_name (const gchar *application_name); +G_CONST_RETURN gchar* g_get_user_data_dir (void); +G_CONST_RETURN gchar* g_get_user_config_dir (void); +G_CONST_RETURN gchar* g_get_user_cache_dir (void); +G_CONST_RETURN gchar * G_CONST_RETURN * g_get_system_data_dirs (void); +G_CONST_RETURN gchar * G_CONST_RETURN * g_get_system_config_dirs (void); typedef struct _GDebugKey GDebugKey; struct _GDebugKey @@ -167,7 +172,6 @@ gchar* g_get_current_dir (void); gchar* g_path_get_basename (const gchar *file_name); gchar* g_path_get_dirname (const gchar *file_name); - /* Set the pointer at the specified location to NULL */ void g_nullify_pointer (gpointer *nullify_location); -- 2.7.4