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
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
24 #include "theme-manager.h"
27 /******************************************************************************
29 *****************************************************************************/
31 static GHashTable
*theme_table
= NULL
;
33 /*****************************************************************************
35 ****************************************************************************/
38 purple_theme_manager_get_type(void)
40 static GType type
= 0;
42 static const GTypeInfo info
= {
43 sizeof(PurpleThemeManagerClass
),
45 NULL
, /* base_finalize */
46 NULL
, /* class_init */
47 NULL
, /* class_finalize */
48 NULL
, /* class_data */
49 sizeof(PurpleThemeManager
),
51 NULL
, /* instance_init */
52 NULL
, /* Value Table */
54 type
= g_type_register_static(G_TYPE_OBJECT
,
55 "PurpleThemeManager", &info
, 0);
60 /******************************************************************************
62 *****************************************************************************/
64 /* makes a key of <type> + '/' + <name> */
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" */
75 purple_theme_manager_is_theme_type(gchar
*key
,
79 return g_str_has_prefix(key
, g_strconcat(user_data
, "/", NULL
));
83 purple_theme_manager_is_theme(gchar
*key
,
87 return PURPLE_IS_THEME(value
);
91 purple_theme_manager_function_wrapper(gchar
*key
,
95 if (PURPLE_IS_THEME(value
))
100 purple_theme_manager_build_dir(const gchar
*root
)
102 gchar
*purple_dir
, *theme_dir
;
103 const gchar
*name
= NULL
, *type
= NULL
;
105 PurpleThemeLoader
*loader
;
107 rdir
= g_dir_open(root
, 0, NULL
);
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
);
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
);
132 if (PURPLE_IS_THEME(theme
))
133 purple_theme_manager_add_theme(theme
);
144 /*****************************************************************************
145 * Public API functions
146 *****************************************************************************/
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
);
156 purple_theme_manager_refresh(void)
159 const gchar
*xdg
= NULL
;
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
);
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
);
174 path
= g_build_filename(purple_home_dir(), ".local", "themes", NULL
);
176 purple_theme_manager_build_dir(path
);
179 /* now dig through XDG_DATA_DIRS and add those too */
180 xdg
= g_getenv("XDG_DATA_DIRS");
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
);
190 g_strfreev(xdg_dirs
);
195 purple_theme_manager_uninit(void)
197 g_hash_table_destroy(theme_table
);
201 purple_theme_manager_register_type(PurpleThemeLoader
*loader
)
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
);
216 purple_theme_manager_unregister_type(PurpleThemeLoader
*loader
)
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 */
235 purple_theme_manager_find_theme(const gchar
*name
,
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
);
253 purple_theme_manager_add_theme(PurpleTheme
*theme
)
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
);
270 purple_theme_manager_remove_theme(PurpleTheme
*theme
)
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
);
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
);
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
);