Use correct memory functions for GG oauth.
[pidgin-git.git] / libpurple / theme-manager.c
blobde5fce9243977541a1e63bdf6ba649d58b5b1d96
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 struct _PurpleThemeManager {
28 GObject parent;
31 /******************************************************************************
32 * Globals
33 *****************************************************************************/
35 static GHashTable *theme_table = NULL;
37 /*****************************************************************************
38 * GObject Stuff
39 ****************************************************************************/
41 GType
42 purple_theme_manager_get_type(void)
44 static GType type = 0;
45 if (type == 0) {
46 static const GTypeInfo info = {
47 sizeof(PurpleThemeManagerClass),
48 NULL, /* base_init */
49 NULL, /* base_finalize */
50 NULL, /* class_init */
51 NULL, /* class_finalize */
52 NULL, /* class_data */
53 sizeof(PurpleThemeManager),
54 0, /* n_preallocs */
55 NULL, /* instance_init */
56 NULL, /* Value Table */
58 type = g_type_register_static(G_TYPE_OBJECT,
59 "PurpleThemeManager", &info, 0);
61 return type;
64 /******************************************************************************
65 * Helpers
66 *****************************************************************************/
68 /* makes a key of <type> + '/' + <name> */
69 static gchar *
70 purple_theme_manager_make_key(const gchar *name, const gchar *type)
72 g_return_val_if_fail(name && *name, NULL);
73 g_return_val_if_fail(type && *type, NULL);
74 return g_strconcat(type, "/", name, NULL);
77 /* returns TRUE if theme is of type "user_data" */
78 static gboolean
79 purple_theme_manager_is_theme_type(gchar *key,
80 gpointer value,
81 gchar *user_data)
83 return g_str_has_prefix(key, g_strconcat(user_data, "/", NULL));
86 static gboolean
87 check_if_theme_or_loader(gchar *key, gpointer value, GSList **loaders)
89 if (PURPLE_IS_THEME(value))
90 return TRUE;
91 else if (PURPLE_IS_THEME_LOADER(value))
92 *loaders = g_slist_prepend(*loaders, value);
94 return FALSE;
97 static void
98 purple_theme_manager_function_wrapper(gchar *key,
99 gpointer value,
100 PurpleThemeFunc user_data)
102 if (PURPLE_IS_THEME(value))
103 (* user_data)(value);
106 static void
107 purple_theme_manager_build_dir(GSList *loaders, const gchar *root)
109 gchar *theme_dir;
110 const gchar *name;
111 GDir *rdir;
112 GSList *tmp;
113 PurpleThemeLoader *loader;
115 rdir = g_dir_open(root, 0, NULL);
117 if (!rdir)
118 return;
120 while ((name = g_dir_read_name(rdir))) {
121 theme_dir = g_build_filename(root, name, NULL);
123 for (tmp = loaders; tmp; tmp = g_slist_next(tmp)) {
124 loader = PURPLE_THEME_LOADER(tmp->data);
126 if (purple_theme_loader_probe(loader, theme_dir)) {
127 PurpleTheme *theme = purple_theme_loader_build(loader, theme_dir);
128 if (PURPLE_IS_THEME(theme))
129 purple_theme_manager_add_theme(theme);
133 g_free(theme_dir);
136 g_dir_close(rdir);
139 /*****************************************************************************
140 * Public API functions
141 *****************************************************************************/
143 void
144 purple_theme_manager_init(void)
146 theme_table = g_hash_table_new_full(g_str_hash,
147 g_str_equal, g_free, g_object_unref);
150 void
151 purple_theme_manager_refresh(void)
153 gchar *path;
154 const gchar *const *xdg_dirs;
155 gint i;
156 GSList *loaders = NULL;
158 g_hash_table_foreach_remove(theme_table, (GHRFunc)check_if_theme_or_loader,
159 &loaders);
161 /* Add themes from ~/.purple */
162 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
163 path = g_build_filename(purple_user_dir(), "themes", NULL);
164 G_GNUC_END_IGNORE_DEPRECATIONS
165 purple_theme_manager_build_dir(loaders, path);
166 g_free(path);
168 /* look for XDG_DATA_HOME */
169 /* NOTE: will work on Windows, see g_get_user_data_dir() documentation */
170 path = g_build_filename(g_get_user_data_dir(), "themes", NULL);
171 purple_theme_manager_build_dir(loaders, path);
172 g_free(path);
174 /* now dig through XDG_DATA_DIRS and add those too */
175 /* NOTE: will work on Windows, see g_get_system_data_dirs() documentation */
176 xdg_dirs = g_get_system_data_dirs();
177 for (i = 0; xdg_dirs[i] != NULL; i++) {
178 path = g_build_filename(xdg_dirs[i], "themes", NULL);
179 purple_theme_manager_build_dir(loaders, path);
180 g_free(path);
183 g_slist_free(loaders);
186 void
187 purple_theme_manager_uninit(void)
189 g_hash_table_destroy(theme_table);
192 void
193 purple_theme_manager_register_type(PurpleThemeLoader *loader)
195 gchar *type;
197 g_return_if_fail(PURPLE_IS_THEME_LOADER(loader));
199 type = g_strdup(purple_theme_loader_get_type_string(loader));
200 g_return_if_fail(type);
202 /* if something is already there do nothing */
203 if (!g_hash_table_lookup(theme_table, type))
204 g_hash_table_insert(theme_table, type, loader);
207 void
208 purple_theme_manager_unregister_type(PurpleThemeLoader *loader)
210 const gchar *type;
212 g_return_if_fail(PURPLE_IS_THEME_LOADER(loader));
214 type = purple_theme_loader_get_type_string(loader);
215 g_return_if_fail(type);
217 if (g_hash_table_lookup(theme_table, type) == loader)
219 g_hash_table_remove(theme_table, type);
221 g_hash_table_foreach_remove(theme_table,
222 (GHRFunc)purple_theme_manager_is_theme_type, (gpointer)type);
223 } /* only free if given registered loader */
226 PurpleTheme *
227 purple_theme_manager_find_theme(const gchar *name,
228 const gchar *type)
230 gchar *key;
231 PurpleTheme *theme;
233 key = purple_theme_manager_make_key(name, type);
235 g_return_val_if_fail(key, NULL);
237 theme = g_hash_table_lookup(theme_table, key);
239 g_free(key);
241 return theme;
244 void
245 purple_theme_manager_add_theme(PurpleTheme *theme)
247 gchar *key;
249 g_return_if_fail(PURPLE_IS_THEME(theme));
251 key = purple_theme_manager_make_key(purple_theme_get_name(theme),
252 purple_theme_get_type_string(theme));
254 g_return_if_fail(key);
256 /* if something is already there do nothing */
257 if (g_hash_table_lookup(theme_table, key) == NULL)
258 g_hash_table_insert(theme_table, key, theme);
261 void
262 purple_theme_manager_remove_theme(PurpleTheme *theme)
264 gchar *key;
266 g_return_if_fail(PURPLE_IS_THEME(theme));
268 key = purple_theme_manager_make_key(purple_theme_get_name(theme),
269 purple_theme_get_type_string(theme));
271 g_return_if_fail(key);
273 g_hash_table_remove(theme_table, key);
275 g_free(key);
278 void
279 purple_theme_manager_for_each_theme(PurpleThemeFunc func)
281 g_return_if_fail(func);
283 g_hash_table_foreach(theme_table,
284 (GHFunc) purple_theme_manager_function_wrapper, func);
287 PurpleTheme *
288 purple_theme_manager_load_theme(const gchar *theme_dir, const gchar *type)
290 PurpleThemeLoader *loader;
292 g_return_val_if_fail(theme_dir != NULL && type != NULL, NULL);
294 loader = g_hash_table_lookup(theme_table, type);
295 g_return_val_if_fail(PURPLE_IS_THEME_LOADER(loader), NULL);
297 return purple_theme_loader_build(loader, theme_dir);