Use GLib XDG dirs functions instead of g_getenv
[pidgin-git.git] / libpurple / theme-manager.c
blobd3485a87def168128a8e57ea9c4a800c74e5fc45
1 /*
2 * Themes for libpurple
4 * Pidgin is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include "internal.h"
24 #include "theme-manager.h"
25 #include "util.h"
27 /******************************************************************************
28 * Globals
29 *****************************************************************************/
31 static GHashTable *theme_table = NULL;
33 /*****************************************************************************
34 * GObject Stuff
35 ****************************************************************************/
37 GType
38 purple_theme_manager_get_type(void)
40 static GType type = 0;
41 if (type == 0) {
42 static const GTypeInfo info = {
43 sizeof(PurpleThemeManagerClass),
44 NULL, /* base_init */
45 NULL, /* base_finalize */
46 NULL, /* class_init */
47 NULL, /* class_finalize */
48 NULL, /* class_data */
49 sizeof(PurpleThemeManager),
50 0, /* n_preallocs */
51 NULL, /* instance_init */
52 NULL, /* Value Table */
54 type = g_type_register_static(G_TYPE_OBJECT,
55 "PurpleThemeManager", &info, 0);
57 return type;
60 /******************************************************************************
61 * Helpers
62 *****************************************************************************/
64 /* makes a key of <type> + '/' + <name> */
65 static gchar *
66 purple_theme_manager_make_key(const gchar *name, const gchar *type)
68 g_return_val_if_fail(name && *name, NULL);
69 g_return_val_if_fail(type && *type, NULL);
70 return g_strconcat(type, "/", name, NULL);
73 /* returns TRUE if theme is of type "user_data" */
74 static gboolean
75 purple_theme_manager_is_theme_type(gchar *key,
76 gpointer value,
77 gchar *user_data)
79 return g_str_has_prefix(key, g_strconcat(user_data, "/", NULL));
82 static gboolean
83 check_if_theme_or_loader(gchar *key, gpointer value, GSList **loaders)
85 if (PURPLE_IS_THEME(value))
86 return TRUE;
87 else if (PURPLE_IS_THEME_LOADER(value))
88 *loaders = g_slist_prepend(*loaders, value);
90 return FALSE;
93 static void
94 purple_theme_manager_function_wrapper(gchar *key,
95 gpointer value,
96 PurpleThemeFunc user_data)
98 if (PURPLE_IS_THEME(value))
99 (* user_data)(value);
102 static void
103 purple_theme_manager_build_dir(GSList *loaders, const gchar *root)
105 gchar *theme_dir;
106 const gchar *name;
107 GDir *rdir;
108 GSList *tmp;
109 PurpleThemeLoader *loader;
111 rdir = g_dir_open(root, 0, NULL);
113 if (!rdir)
114 return;
116 while ((name = g_dir_read_name(rdir))) {
117 theme_dir = g_build_filename(root, name, NULL);
119 for (tmp = loaders; tmp; tmp = g_slist_next(tmp)) {
120 loader = PURPLE_THEME_LOADER(tmp->data);
122 if (purple_theme_loader_probe(loader, theme_dir)) {
123 PurpleTheme *theme = purple_theme_loader_build(loader, theme_dir);
124 if (PURPLE_IS_THEME(theme))
125 purple_theme_manager_add_theme(theme);
129 g_free(theme_dir);
132 g_dir_close(rdir);
135 /*****************************************************************************
136 * Public API functions
137 *****************************************************************************/
139 void
140 purple_theme_manager_init(void)
142 theme_table = g_hash_table_new_full(g_str_hash,
143 g_str_equal, g_free, g_object_unref);
146 void
147 purple_theme_manager_refresh(void)
149 gchar *path;
150 const gchar *const *xdg_dirs;
151 gint i;
152 GSList *loaders = NULL;
154 g_hash_table_foreach_remove(theme_table, (GHRFunc)check_if_theme_or_loader,
155 &loaders);
157 /* Add themes from ~/.purple */
158 path = g_build_filename(purple_user_dir(), "themes", NULL);
159 purple_theme_manager_build_dir(loaders, path);
160 g_free(path);
162 /* look for XDG_DATA_HOME */
163 /* NOTE: will work on Windows, see g_get_user_data_dir() documentation */
164 path = g_build_filename(g_get_user_data_dir(), "themes", NULL);
165 purple_theme_manager_build_dir(loaders, path);
166 g_free(path);
168 /* now dig through XDG_DATA_DIRS and add those too */
169 /* NOTE: will work on Windows, see g_get_system_data_dirs() documentation */
170 xdg_dirs = g_get_system_data_dirs();
171 for (i = 0; xdg_dirs[i] != NULL; i++) {
172 path = g_build_filename(xdg_dirs[i], "themes", NULL);
173 purple_theme_manager_build_dir(loaders, path);
174 g_free(path);
177 g_slist_free(loaders);
180 void
181 purple_theme_manager_uninit(void)
183 g_hash_table_destroy(theme_table);
186 void
187 purple_theme_manager_register_type(PurpleThemeLoader *loader)
189 gchar *type;
191 g_return_if_fail(PURPLE_IS_THEME_LOADER(loader));
193 type = g_strdup(purple_theme_loader_get_type_string(loader));
194 g_return_if_fail(type);
196 /* if something is already there do nothing */
197 if (!g_hash_table_lookup(theme_table, type))
198 g_hash_table_insert(theme_table, type, loader);
201 void
202 purple_theme_manager_unregister_type(PurpleThemeLoader *loader)
204 const gchar *type;
206 g_return_if_fail(PURPLE_IS_THEME_LOADER(loader));
208 type = purple_theme_loader_get_type_string(loader);
209 g_return_if_fail(type);
211 if (g_hash_table_lookup(theme_table, type) == loader)
213 g_hash_table_remove(theme_table, type);
215 g_hash_table_foreach_remove(theme_table,
216 (GHRFunc)purple_theme_manager_is_theme_type, (gpointer)type);
217 } /* only free if given registered loader */
220 PurpleTheme *
221 purple_theme_manager_find_theme(const gchar *name,
222 const gchar *type)
224 gchar *key;
225 PurpleTheme *theme;
227 key = purple_theme_manager_make_key(name, type);
229 g_return_val_if_fail(key, NULL);
231 theme = g_hash_table_lookup(theme_table, key);
233 g_free(key);
235 return theme;
238 void
239 purple_theme_manager_add_theme(PurpleTheme *theme)
241 gchar *key;
243 g_return_if_fail(PURPLE_IS_THEME(theme));
245 key = purple_theme_manager_make_key(purple_theme_get_name(theme),
246 purple_theme_get_type_string(theme));
248 g_return_if_fail(key);
250 /* if something is already there do nothing */
251 if (g_hash_table_lookup(theme_table, key) == NULL)
252 g_hash_table_insert(theme_table, key, theme);
255 void
256 purple_theme_manager_remove_theme(PurpleTheme *theme)
258 gchar *key;
260 g_return_if_fail(PURPLE_IS_THEME(theme));
262 key = purple_theme_manager_make_key(purple_theme_get_name(theme),
263 purple_theme_get_type_string(theme));
265 g_return_if_fail(key);
267 g_hash_table_remove(theme_table, key);
269 g_free(key);
272 void
273 purple_theme_manager_for_each_theme(PurpleThemeFunc func)
275 g_return_if_fail(func);
277 g_hash_table_foreach(theme_table,
278 (GHFunc) purple_theme_manager_function_wrapper, func);
281 PurpleTheme *
282 purple_theme_manager_load_theme(const gchar *theme_dir, const gchar *type)
284 PurpleThemeLoader *loader;
286 g_return_val_if_fail(theme_dir != NULL && type != NULL, NULL);
288 loader = g_hash_table_lookup(theme_table, type);
289 g_return_val_if_fail(PURPLE_IS_THEME_LOADER(loader), NULL);
291 return purple_theme_loader_build(loader, theme_dir);