1 /* vim: set sw=4 ts=4 sts=4 et: */
3 #include "efreet_private.h"
5 static const char *efreet_home_dir = NULL;
6 static const char *xdg_data_home = NULL;
7 static const char *xdg_config_home = NULL;
8 static const char *xdg_cache_home = NULL;
9 static Eina_List *xdg_data_dirs = NULL;
10 static Eina_List *xdg_config_dirs = NULL;
12 static const char *efreet_dir_get(const char *key, const char *fallback);
13 static Eina_List *efreet_dirs_get(const char *key,
14 const char *fallback);
18 * @return Returns 1 on success or 0 on failure
19 * @brief Initializes the efreet base settings
22 efreet_base_init(void)
24 if (!eina_stringshare_init()) return 0;
31 * @return Returns no value
32 * @brief Cleans up the efreet base settings system
35 efreet_base_shutdown(void)
37 IF_RELEASE(efreet_home_dir);
38 IF_RELEASE(xdg_data_home);
39 IF_RELEASE(xdg_config_home);
40 IF_RELEASE(xdg_cache_home);
42 IF_FREE_LIST(xdg_data_dirs);
43 IF_FREE_LIST(xdg_config_dirs);
45 eina_stringshare_shutdown();
50 * @return Returns the users home directory
51 * @brief Gets the users home directory and returns it.
54 efreet_home_dir_get(void)
56 if (efreet_home_dir) return efreet_home_dir;
58 efreet_home_dir = getenv("HOME");
59 if (!efreet_home_dir || efreet_home_dir[0] == '\0')
60 efreet_home_dir = "/tmp";
62 efreet_home_dir = eina_stringshare_add(efreet_home_dir);
64 return efreet_home_dir;
68 * @return Returns the XDG Data Home directory
69 * @brief Retrieves the XDG Data Home directory
72 efreet_data_home_get(void)
74 if (xdg_data_home) return xdg_data_home;
75 xdg_data_home = efreet_dir_get("XDG_DATA_HOME", "/.local/share");
80 * @return Returns the Eina_List of preference ordered extra data directories
81 * @brief Returns the Eina_List of prefernece oredred extra data
84 * @note The returned list is static inside Efreet. If you add/remove from the
85 * list then the next call to efreet_data_dirs_get() will return your
86 * modified values. DO NOT free this list.
89 efreet_data_dirs_get(void)
91 if (xdg_data_dirs) return xdg_data_dirs;
92 xdg_data_dirs = efreet_dirs_get("XDG_DATA_DIRS",
93 "/usr/local/share:/usr/share");
98 * @return Returns the XDG Config Home directory
99 * @brief Retrieves the XDG Config Home directory
102 efreet_config_home_get(void)
104 if (xdg_config_home) return xdg_config_home;
105 xdg_config_home = efreet_dir_get("XDG_CONFIG_HOME", "/.config");
106 return xdg_config_home;
110 * @return Returns the Eina_List of preference ordered extra config directories
111 * @brief Returns the Eina_List of prefernece oredred extra config
114 * @note The returned list is static inside Efreet. If you add/remove from the
115 * list then the next call to efreet_config_dirs_get() will return your
116 * modified values. DO NOT free this list.
119 efreet_config_dirs_get(void)
121 if (xdg_config_dirs) return xdg_config_dirs;
122 xdg_config_dirs = efreet_dirs_get("XDG_CONFIG_DIRS", "/etc/xdg");
123 return xdg_config_dirs;
127 * @return Returns the XDG Cache Home directory
128 * @brief Retrieves the XDG Cache Home directory
131 efreet_cache_home_get(void)
133 if (xdg_cache_home) return xdg_cache_home;
134 xdg_cache_home = efreet_dir_get("XDG_CACHE_HOME", "/.cache");
135 return xdg_cache_home;
140 * @param key: The environemnt key to lookup
141 * @param fallback: The fallback value to use
142 * @return Returns the directory related to the given key or the fallback
143 * @brief This trys to determine the correct directory name given the
144 * environment key @a key and fallbacks @a fallback.
147 efreet_dir_get(const char *key, const char *fallback)
153 if (!dir || dir[0] == '\0')
158 user = efreet_home_dir_get();
159 len = strlen(user) + strlen(fallback) + 1;
160 dir = malloc(sizeof(char) * len);
161 snprintf(dir, len, "%s%s", user, fallback);
163 t = eina_stringshare_add(dir);
166 else t = eina_stringshare_add(dir);
173 * @param key: The environment key to lookup
174 * @param fallback: The fallback value to use
175 * @return Returns a list of directories specified by the given key @a key
176 * or from the list of fallbacks in @a fallback.
177 * @brief Creates a list of directories as given in the environment key @a
178 * key or from the fallbacks in @a fallback
181 efreet_dirs_get(const char *key, const char *fallback)
183 Eina_List *dirs = NULL;
188 if (!path || (path[0] == '\0')) path = fallback;
190 if (!path) return dirs;
198 if (!eina_list_search_unsorted(dirs, (Eina_Compare_Cb)strcmp, s))
199 dirs = eina_list_append(dirs, (void *)eina_stringshare_add(s));
204 if (!eina_list_search_unsorted(dirs, ECORE_COMPARE_CB(strcmp), s))
205 dirs = eina_list_append(dirs, (void *)eina_stringshare_add(s));