void
pidgin_stock_load_status_icon_theme(PidginStatusIconTheme *theme)
{
	GtkIconFactory *icon_factory;
	gint i;
	GtkIconSet *normal;
	GtkIconSet *translucent = NULL;
	GtkWidget *win;

	if (theme != NULL) {
		purple_prefs_set_string(PIDGIN_PREFS_ROOT "/status/icon-theme",
		                        purple_theme_get_name(PURPLE_THEME(theme)));
		purple_prefs_set_path(PIDGIN_PREFS_ROOT "/status/icon-theme-dir",
		                      purple_theme_get_dir(PURPLE_THEME(theme)));
	}
	else {
		purple_prefs_set_string(PIDGIN_PREFS_ROOT "/status/icon-theme", "");
		purple_prefs_set_path(PIDGIN_PREFS_ROOT "/status/icon-theme-dir", "");
	}

	icon_factory = gtk_icon_factory_new();

	gtk_icon_factory_add_default(icon_factory);

	win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_widget_realize(win);

	for (i = 0; i < G_N_ELEMENTS(sized_status_icons); i++)
	{
		normal = gtk_icon_set_new();
		if (sized_status_icons[i].translucent_name)
			translucent = gtk_icon_set_new();

#define ADD_SIZED_ICON(name, size) \
		if (sized_status_icons[i].name) { \
			add_sized_icon(normal, name, PIDGIN_ICON_THEME(theme), size, sized_status_icons[i], FALSE); \
			if (sized_status_icons[i].translucent_name) \
				add_sized_icon(translucent, name, PIDGIN_ICON_THEME(theme), size, sized_status_icons[i], TRUE); \
		}
		ADD_SIZED_ICON(microscopic, "11");
		ADD_SIZED_ICON(extra_small, "16");
		ADD_SIZED_ICON(small, "22");
		ADD_SIZED_ICON(medium, "32");
		ADD_SIZED_ICON(large, "48");
		ADD_SIZED_ICON(huge, "64");
#undef ADD_SIZED_ICON

		gtk_icon_factory_add(icon_factory, sized_status_icons[i].name, normal);
		gtk_icon_set_unref(normal);

		if (sized_status_icons[i].translucent_name) {
			gtk_icon_factory_add(icon_factory, sized_status_icons[i].translucent_name, translucent);
			gtk_icon_set_unref(translucent);
		}
	}

	gtk_widget_destroy(win);
	g_object_unref(G_OBJECT(icon_factory));
	reload_settings();
}
static gchar *
find_icon_file(PidginIconTheme *theme, const gchar *size, SizedStockIcon sized_icon, gboolean rtl)
{
	const gchar *file, *dir;
	gchar *file_full = NULL;
	gchar *tmp;

	if (theme != NULL) {
		file = pidgin_icon_theme_get_icon(PIDGIN_ICON_THEME(theme), sized_icon.name);
		dir = purple_theme_get_dir(PURPLE_THEME(theme));

		if (rtl)
			file_full = g_build_filename(dir, size, "rtl", file, NULL);
		else
			file_full = g_build_filename(dir, size, file, NULL);

		if (g_file_test(file_full, G_FILE_TEST_IS_REGULAR))
			return file_full;

		g_free(file_full);
	}

	if (rtl)
		tmp = g_build_filename("pixmaps", "pidgin", sized_icon.dir, size, "rtl", sized_icon.filename, NULL);
	else
		tmp = g_build_filename("pixmaps", "pidgin", sized_icon.dir, size, sized_icon.filename, NULL);

	file_full = find_file_common(tmp);
	g_free(tmp);
	return file_full;
}
static PurpleTheme *
pidgin_icon_loader_build(const gchar *theme_dir)
{
	PurpleXmlNode *root_node = NULL, *sub_node;
	gchar *dir, *filename_full, *data = NULL;
	PidginIconTheme *theme = NULL;
	const gchar *name;

	/* Find the theme file */
	g_return_val_if_fail(theme_dir != NULL, NULL);
	dir = g_build_filename(theme_dir, "purple", "status-icon", NULL);
	filename_full = g_build_filename(dir, "theme.xml", NULL);

	if (g_file_test(filename_full, G_FILE_TEST_IS_REGULAR))
		root_node = purple_xmlnode_from_file(dir, "theme.xml", "icon themes", "icon-theme-loader");

	g_free(filename_full);
	if (root_node == NULL) {
		g_free(dir);
		return NULL;
	}

	name = purple_xmlnode_get_attrib(root_node, "name");

	if (name) {
		/* Parse the tree */
		sub_node = purple_xmlnode_get_child(root_node, "description");
		data = purple_xmlnode_get_data(sub_node);

		if (purple_xmlnode_get_attrib(root_node, "name") != NULL) {
			theme = g_object_new(PIDGIN_TYPE_STATUS_ICON_THEME,
					"type", "status-icon",
					"name", name,
					"author", purple_xmlnode_get_attrib(root_node, "author"),
					"image", purple_xmlnode_get_attrib(root_node, "image"),
					"directory", dir,
					"description", data, NULL);

			sub_node = purple_xmlnode_get_child(root_node, "icon");

			while (sub_node) {
				pidgin_icon_theme_set_icon(theme,
						purple_xmlnode_get_attrib(sub_node, "id"),
						purple_xmlnode_get_attrib(sub_node, "file"));
				sub_node = purple_xmlnode_get_next_twin(sub_node);
			}
		}
	}

	purple_xmlnode_free(root_node);
	g_free(data);
	return PURPLE_THEME(theme);
}
gchar *
purple_sound_theme_get_file_full(PurpleSoundTheme *theme,
		const gchar *event)
{
	const gchar *filename;

	g_return_val_if_fail(PURPLE_IS_SOUND_THEME(theme), NULL);

	filename = purple_sound_theme_get_file(theme, event);

	g_return_val_if_fail(filename, NULL);

	return g_build_filename(purple_theme_get_dir(PURPLE_THEME(theme)), filename, NULL);
}
void
pidgin_stock_load_stock_icon_theme(PidginStockIconTheme *theme)
{
	GtkIconFactory *icon_factory;
	gint i;
	GtkWidget *win;

	if (theme != NULL) {
		purple_prefs_set_string(PIDGIN_PREFS_ROOT "/stock/icon-theme",
		                        purple_theme_get_name(PURPLE_THEME(theme)));
		purple_prefs_set_path(PIDGIN_PREFS_ROOT "/stock/icon-theme-dir",
		                      purple_theme_get_dir(PURPLE_THEME(theme)));
	}
	else {
		purple_prefs_set_string(PIDGIN_PREFS_ROOT "/stock/icon-theme", "");
		purple_prefs_set_path(PIDGIN_PREFS_ROOT "/stock/icon-theme-dir", "");
	}

	icon_factory = gtk_icon_factory_new();

	gtk_icon_factory_add_default(icon_factory);

	win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_widget_realize(win);

	/* All non-sized icons */
	for (i = 0; i < G_N_ELEMENTS(stock_icons); i++) {
		GtkIconSource *source;
		GtkIconSet *iconset;
		gchar *filename;

		if (stock_icons[i].dir == NULL) {
			/* GTK+ Stock icon */
			iconset = gtk_style_lookup_icon_set(gtk_widget_get_style(win),
					stock_icons[i].filename);
		} else {
			filename = find_file(stock_icons[i].dir, stock_icons[i].filename);

			if (filename == NULL)
				continue;

			source = gtk_icon_source_new();
			gtk_icon_source_set_filename(source, filename);
			gtk_icon_source_set_direction_wildcarded(source, TRUE);
			gtk_icon_source_set_size_wildcarded(source, TRUE);
			gtk_icon_source_set_state_wildcarded(source, TRUE);

			iconset = gtk_icon_set_new();
			gtk_icon_set_add_source(iconset, source);

			gtk_icon_source_free(source);
			g_free(filename);
		}

		gtk_icon_factory_add(icon_factory, stock_icons[i].name, iconset);

		gtk_icon_set_unref(iconset);
	}

	/* All non-status sized icons */
	for (i = 0; i < G_N_ELEMENTS(sized_stock_icons); i++)
	{
		GtkIconSet *iconset = gtk_icon_set_new();

#define ADD_SIZED_ICON(name, size) \
		if (sized_stock_icons[i].name) \
			add_sized_icon(iconset, name, PIDGIN_ICON_THEME(theme), size, sized_stock_icons[i], FALSE);
		ADD_SIZED_ICON(microscopic, "11");
		ADD_SIZED_ICON(extra_small, "16");
		ADD_SIZED_ICON(small, "22");
		ADD_SIZED_ICON(medium, "32");
		ADD_SIZED_ICON(large, "48");
		ADD_SIZED_ICON(huge, "64");
#undef ADD_SIZED_ICON

		gtk_icon_factory_add(icon_factory, sized_stock_icons[i].name, iconset);
		gtk_icon_set_unref(iconset);
	}

	gtk_widget_destroy(win);
	g_object_unref(G_OBJECT(icon_factory));
	reload_settings();
}
static PurpleTheme *
pidgin_blist_loader_build(const gchar *dir)
{
	xmlnode *root_node = NULL, *sub_node, *sub_sub_node;
	gchar *filename_full, *data = NULL;
	const gchar *temp, *name;
	gboolean success = TRUE;
	GdkColor *bgcolor, *expanded_bgcolor, *collapsed_bgcolor, *contact_color;
	PidginThemeFont *expanded, *collapsed, *contact, *online, *away, *offline, *idle, *message, *message_nick_said, *status;
	PidginBlistLayout layout;
	PidginBlistTheme *theme;
	int i;
	struct {
		const char *tag;
		PidginThemeFont **font;
	} lookups[] = {
		{"contact_text", &contact},
		{"online_text", &online},
		{"away_text", &away},
		{"offline_text", &offline},
		{"idle_text", &idle},
		{"message_text", &message},
		{"message_nick_said_text", &message_nick_said},
		{"status_text", &status},
		{NULL, NULL}
	};

	bgcolor = expanded_bgcolor = collapsed_bgcolor = contact_color = NULL;
	expanded          = NULL;
	collapsed         = NULL;
	contact           = NULL;
	online            = NULL;
	away              = NULL;
	offline           = NULL;
	idle              = NULL;
	message           = NULL;
	message_nick_said = NULL;
	status            = NULL;

	/* Find the theme file */
	g_return_val_if_fail(dir != NULL, NULL);
	filename_full = g_build_filename(dir, "theme.xml", NULL);

	if (g_file_test(filename_full, G_FILE_TEST_IS_REGULAR))
		root_node = xmlnode_from_file(dir, "theme.xml", "buddy list themes", "blist-loader");

	g_free(filename_full);
	if (root_node == NULL)
		return NULL;

	sub_node = xmlnode_get_child(root_node, "description");
	data = xmlnode_get_data(sub_node);

	name = xmlnode_get_attrib(root_node, "name");

	/* <blist> */
	success = name && purple_strequal(xmlnode_get_attrib(root_node, "type"), "pidgin buddy list");

	if (!success)
		purple_debug_warning("gtkblist-theme-loader", "Missing attribute or problem with the root element\n");

	if (success) {
		if ((success = (sub_node = xmlnode_get_child(root_node, "blist")) != NULL))
			bgcolor = parse_color(sub_node, "color");
		else
			purple_debug_warning("gtkblist-theme-loader", "Missing or problem with tags: <blist>.\n");
	}

	/* <groups> */
	if (success) {
		if ((success = (sub_node = xmlnode_get_child(root_node, "groups")) != NULL
			     && (sub_sub_node = xmlnode_get_child(sub_node, "expanded")) != NULL)) {
			expanded = pidgin_theme_font_parse(sub_sub_node);
			expanded_bgcolor = parse_color(sub_sub_node, "background");
		} else
			purple_debug_warning("gtkblist-theme-loader", "Missing or problem with tags: <groups> <expanded>.\n");
	}

	if (success) {
		if ((success = sub_node != NULL && (sub_sub_node = xmlnode_get_child(sub_node, "collapsed")) != NULL)) {
			collapsed = pidgin_theme_font_parse(sub_sub_node);
			collapsed_bgcolor = parse_color(sub_sub_node, "background");
		} else
			purple_debug_warning("gtkblist-theme-loader", "Missing or problem with tags: <groups> <collapsed>.\n");
	}

	/* <buddys> */
	if (success) {
		if ((success = (sub_node = xmlnode_get_child(root_node, "buddys")) != NULL &&
			     (sub_sub_node = xmlnode_get_child(sub_node, "placement")) != NULL)) {

			layout.status_icon = (temp = xmlnode_get_attrib(sub_sub_node, "status_icon")) != NULL ? atoi(temp) : 0;
			layout.text = (temp = xmlnode_get_attrib(sub_sub_node, "name")) != NULL ? atoi(temp) : 1;
			layout.emblem = (temp = xmlnode_get_attrib(sub_sub_node, "emblem")) != NULL ? atoi(temp) : 2;
			layout.protocol_icon = (temp = xmlnode_get_attrib(sub_sub_node, "protocol_icon")) != NULL ? atoi(temp) : 3;
			layout.buddy_icon = (temp = xmlnode_get_attrib(sub_sub_node, "buddy_icon")) != NULL ? atoi(temp) : 4;
			layout.show_status = (temp = xmlnode_get_attrib(sub_sub_node, "status_icon")) != NULL ? atoi(temp) != 0 : 1;

		} else purple_debug_warning("gtkblist-theme-loader", "Missing or problem with tags: <buddys> <placement>.\n");
	}

	if (success) {
		if ((success = (sub_node != NULL && (sub_sub_node = xmlnode_get_child(sub_node, "background")) != NULL)))
			contact_color = parse_color(sub_sub_node, "color");
		else
			purple_debug_warning("gtkblist-theme-loader", "Missing or problem with tags: <buddys> <background>.\n");
	}

	for (i = 0; success && lookups[i].tag; i++) {
		if ((success = (sub_node != NULL &&
						(sub_sub_node = xmlnode_get_child(sub_node, lookups[i].tag)) != NULL))) {
			*(lookups[i].font) = pidgin_theme_font_parse(sub_sub_node);
		} else {
			*(lookups[i].font) = NULL;
		}
	}

	/* name is required for theme manager */
	success = (success && xmlnode_get_attrib(root_node, "name") != NULL);

	/* the new theme */
	theme = g_object_new(PIDGIN_TYPE_BLIST_THEME,
			"type", "blist",
			"name", name,
			"author", xmlnode_get_attrib(root_node, "author"),
			"image", xmlnode_get_attrib(root_node, "image"),
			"directory", dir,
			"description", data,
			"background-color", bgcolor,
			"layout", &layout,
			"expanded-color", expanded_bgcolor,
			"expanded-text", expanded,
			"collapsed-color", collapsed_bgcolor,
			"collapsed-text", collapsed,
			"contact-color", contact_color,
			"contact", contact,
			"online", online,
			"away", away,
			"offline", offline,
			"idle", idle,
			"message", message,
			"message_nick_said", message_nick_said,
			"status", status, NULL);

	for (i = 0; lookups[i].tag; i++) {
		if (*lookups[i].font) {
			pidgin_theme_font_free(*lookups[i].font);
		}
	}

	pidgin_theme_font_free(expanded);
	pidgin_theme_font_free(collapsed);

	xmlnode_free(root_node);
	g_free(data);

	/* malformed xml file - also frees all partial data*/
	if (!success) {
		g_object_unref(theme);
		theme = NULL;
	}

	if (bgcolor)
		gdk_color_free(bgcolor);
	if (expanded_bgcolor)
		gdk_color_free(expanded_bgcolor);
	if (collapsed_bgcolor)
		gdk_color_free(collapsed_bgcolor);
	if (contact_color)
		gdk_color_free(contact_color);

	return PURPLE_THEME(theme);
}