Fix a crash when remote users have certain characters in their
[pidgin-git.git] / libpurple / theme-manager.c
blob7b8e3506f8264a6abc46003d7edda8eca8cda291
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 purple_theme_manager_is_theme(gchar *key,
84 gpointer value,
85 gchar *user_data)
87 return PURPLE_IS_THEME(value);
90 static void
91 purple_theme_manager_function_wrapper(gchar *key,
92 gpointer value,
93 PTFunc user_data)
95 if (PURPLE_IS_THEME(value))
96 (* user_data)(value);
99 static void
100 purple_theme_manager_build_dir(const gchar *root)
102 gchar *purple_dir, *theme_dir;
103 const gchar *name = NULL, *type = NULL;
104 GDir *rdir, *tdir;
105 PurpleThemeLoader *loader;
107 rdir = g_dir_open(root, 0, NULL);
109 if (!rdir)
110 return;
112 /* Parses directory by root/name/purple/type */
113 while ((name = g_dir_read_name(rdir))) {
114 purple_dir = g_build_filename(root, name, "purple", NULL);
115 tdir = g_dir_open(purple_dir, 0, NULL);
117 if (!tdir) {
118 g_free(purple_dir);
120 continue;
123 while ((type = g_dir_read_name(tdir))) {
124 if ((loader = g_hash_table_lookup(theme_table, type))) {
125 PurpleTheme *theme = NULL;
127 theme_dir = g_build_filename(purple_dir, type, NULL);
129 theme = purple_theme_loader_build(loader, theme_dir);
130 g_free(theme_dir);
132 if (PURPLE_IS_THEME(theme))
133 purple_theme_manager_add_theme(theme);
137 g_dir_close(tdir);
138 g_free(purple_dir);
141 g_dir_close(rdir);
144 /*****************************************************************************
145 * Public API functions
146 *****************************************************************************/
148 void
149 purple_theme_manager_init(void)
151 theme_table = g_hash_table_new_full(g_str_hash,
152 g_str_equal, g_free, g_object_unref);
155 void
156 purple_theme_manager_refresh(void)
158 gchar *path = NULL;
159 const gchar *xdg = NULL;
160 gint i = 0;
162 g_hash_table_foreach_remove(theme_table,
163 (GHRFunc) purple_theme_manager_is_theme, NULL);
165 /* Add themes from ~/.purple */
166 path = g_build_filename(purple_user_dir(), "themes", NULL);
167 purple_theme_manager_build_dir(path);
168 g_free(path);
170 /* look for XDG_DATA_HOME. If we don't have it use ~/.local, and add it */
171 if ((xdg = g_getenv("XDG_DATA_HOME")) != NULL)
172 path = g_build_filename(xdg, "themes", NULL);
173 else
174 path = g_build_filename(purple_home_dir(), ".local", "themes", NULL);
176 purple_theme_manager_build_dir(path);
177 g_free(path);
179 /* now dig through XDG_DATA_DIRS and add those too */
180 xdg = g_getenv("XDG_DATA_DIRS");
181 if (xdg) {
182 gchar **xdg_dirs = g_strsplit(xdg, G_SEARCHPATH_SEPARATOR_S, 0);
184 for (i = 0; xdg_dirs[i]; i++) {
185 path = g_build_filename(xdg_dirs[i], "themes", NULL);
186 purple_theme_manager_build_dir(path);
187 g_free(path);
190 g_strfreev(xdg_dirs);
194 void
195 purple_theme_manager_uninit(void)
197 g_hash_table_destroy(theme_table);
200 void
201 purple_theme_manager_register_type(PurpleThemeLoader *loader)
203 gchar *type;
205 g_return_if_fail(PURPLE_IS_THEME_LOADER(loader));
207 type = g_strdup(purple_theme_loader_get_type_string(loader));
208 g_return_if_fail(type);
210 /* if something is already there do nothing */
211 if (!g_hash_table_lookup(theme_table, type))
212 g_hash_table_insert(theme_table, type, loader);
215 void
216 purple_theme_manager_unregister_type(PurpleThemeLoader *loader)
218 const gchar *type;
220 g_return_if_fail(PURPLE_IS_THEME_LOADER(loader));
222 type = purple_theme_loader_get_type_string(loader);
223 g_return_if_fail(type);
225 if (g_hash_table_lookup(theme_table, type) == loader)
227 g_hash_table_remove(theme_table, type);
229 g_hash_table_foreach_remove(theme_table,
230 (GHRFunc)purple_theme_manager_is_theme_type, (gpointer)type);
231 } /* only free if given registered loader */
234 PurpleTheme *
235 purple_theme_manager_find_theme(const gchar *name,
236 const gchar *type)
238 gchar *key;
239 PurpleTheme *theme;
241 key = purple_theme_manager_make_key(name, type);
243 g_return_val_if_fail(key, NULL);
245 theme = g_hash_table_lookup(theme_table, key);
247 g_free(key);
249 return theme;
252 void
253 purple_theme_manager_add_theme(PurpleTheme *theme)
255 gchar *key;
257 g_return_if_fail(PURPLE_IS_THEME(theme));
259 key = purple_theme_manager_make_key(purple_theme_get_name(theme),
260 purple_theme_get_type_string(theme));
262 g_return_if_fail(key);
264 /* if something is already there do nothing */
265 if (g_hash_table_lookup(theme_table, key) == NULL)
266 g_hash_table_insert(theme_table, key, theme);
269 void
270 purple_theme_manager_remove_theme(PurpleTheme *theme)
272 gchar *key;
274 g_return_if_fail(PURPLE_IS_THEME(theme));
276 key = purple_theme_manager_make_key(purple_theme_get_name(theme),
277 purple_theme_get_type_string(theme));
279 g_return_if_fail(key);
281 g_hash_table_remove(theme_table, key);
283 g_free(key);
286 void
287 purple_theme_manager_for_each_theme(PTFunc func)
289 g_return_if_fail(func);
291 g_hash_table_foreach(theme_table,
292 (GHFunc) purple_theme_manager_function_wrapper, func);
295 PurpleTheme *
296 purple_theme_manager_load_theme(const gchar *theme_dir, const gchar *type)
298 PurpleThemeLoader *loader;
300 g_return_val_if_fail(theme_dir != NULL && type != NULL, NULL);
302 loader = g_hash_table_lookup(theme_table, type);
303 g_return_val_if_fail(PURPLE_IS_THEME_LOADER(loader), NULL);
305 return purple_theme_loader_build(loader, theme_dir);