changeset 32184:a7a6770f26e2

Load the list of variants when building the theme.
author Elliott Sales de Andrade <qulogic@pidgin.im>
date Mon, 19 Sep 2011 18:48:00 +0000
parents 504a62af2dd0
children f857a6db3bd9
files pidgin/gtkconv-theme-loader.c pidgin/gtkconv-theme.c pidgin/gtkconv-theme.h pidgin/gtkprefs.c
diffstat 4 files changed, 61 insertions(+), 96 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin/gtkconv-theme-loader.c	Mon Sep 19 03:10:41 2011 +0000
+++ b/pidgin/gtkconv-theme-loader.c	Mon Sep 19 18:48:00 2011 +0000
@@ -109,6 +109,8 @@
 	int MessageViewVersion;
 	const char *CFBundleName;
 	const char *CFBundleIdentifier;
+	GDir *variants;
+	char *variant_dir;
 
 	g_return_val_if_fail(dir != NULL, NULL);
 
@@ -168,6 +170,29 @@
 	                     "directory", dir,
 	                     "info", info, NULL);
 
+	/* Read list of variants */
+	variant_dir = g_build_filename(dir, "Contents", "Resources", "Variants", NULL);
+	variants = g_dir_open(variant_dir, 0, NULL);
+	g_free(variant_dir);
+
+	if (variants) {
+		const char *file;
+		char *name;
+
+		while ((file = g_dir_read_name(variants)) != NULL) {
+			const char *end = g_strrstr(file, ".css");
+			char *name;
+
+			if ((end == NULL) || (*(end + 4) != '\0'))
+				continue;
+
+			name = g_strndup(file, end - file);
+			pidgin_conversation_theme_add_variant(theme, name);
+		}
+
+		g_dir_close(variants);
+	}
+
 	return PURPLE_THEME(theme);
 }
 
--- a/pidgin/gtkconv-theme.c	Mon Sep 19 03:10:41 2011 +0000
+++ b/pidgin/gtkconv-theme.c	Mon Sep 19 18:48:00 2011 +0000
@@ -47,6 +47,7 @@
 typedef struct {
 	/* current config options */
 	char     *variant; /* allowed to be NULL if there are no variants */
+	GList    *variants;
 
 	/* Info.plist keys/values */
 	GHashTable *info;
@@ -552,49 +553,48 @@
 }
 
 void
-pidgin_conversation_theme_save_state(const PidginConvTheme *theme)
+pidgin_conversation_theme_add_variant(PidginConvTheme *theme, const char *variant)
+{
+	PidginConvThemePrivate *priv;
+	priv = PIDGIN_CONV_THEME_GET_PRIVATE(theme);
+
+	priv->variants = g_list_prepend(priv->variants, g_strdup(variant));
+}
+
+const char *
+pidgin_conversation_theme_get_variant(PidginConvTheme *theme)
+{
+	PidginConvThemePrivate *priv;
+	priv = PIDGIN_CONV_THEME_GET_PRIVATE(theme);
+
+	return g_strdup(priv->variant);
+}
+
+void
+pidgin_conversation_theme_set_variant(PidginConvTheme *theme, const char *variant)
 {
 	PidginConvThemePrivate *priv;
 	const GValue *val;
 	char *prefname;
-	char *variant;
+	priv = PIDGIN_CONV_THEME_GET_PRIVATE(theme);
 
-	priv = PIDGIN_CONV_THEME_GET_PRIVATE(theme);
+	g_free(priv->variant);
+	priv->variant = g_strdup(variant);
 
 	val = get_key(priv, "CFBundleIdentifier", FALSE);
-	prefname = g_strdup_printf(PIDGIN_PREFS_ROOT "/conversation/themes/%s", g_value_get_string(val));
-	variant = g_strdup_printf("%s/variant", prefname);
-
-	purple_debug_info("webkit", "saving state with variant %s\n", priv->variant);
-	purple_prefs_add_none(prefname);
-	purple_prefs_add_string(variant, "");
-	purple_prefs_set_string(variant, priv->variant);
-
+	prefname = g_strdup_printf(PIDGIN_PREFS_ROOT "/conversation/themes/%s/variant",
+	                           g_value_get_string(val));
+	purple_prefs_set_string(prefname, variant);
 	g_free(prefname);
-	g_free(variant);
 }
 
-static void
-pidgin_conversation_theme_load_state(PidginConvTheme *theme)
+const GList *
+pidgin_conversation_theme_get_variants(PidginConvTheme *theme)
 {
 	PidginConvThemePrivate *priv;
-	const GValue *val;
-	char *prefname;
-	const char* value;
-	gboolean changed;
-
 	priv = PIDGIN_CONV_THEME_GET_PRIVATE(theme);
 
-	val = get_key(priv, "CFBundleIdentifier", FALSE);
-	prefname = g_strdup_printf(PIDGIN_PREFS_ROOT "/conversation/themes/%s/variant", g_value_get_string(val));
-
-	value = purple_prefs_get_string(prefname);
-	changed = !priv->variant || !g_str_equal(priv->variant, value);
-
-	g_free(priv->variant);
-	priv->variant = g_strdup(value);
-
-	g_free(prefname);
+	return priv->variants;
 }
 
 PidginConvTheme *
@@ -622,65 +622,6 @@
 	return ret;
 }
 
-void
-pidgin_conversation_theme_set_variant(PidginConvTheme *theme, const char *variant)
-{
-	PidginConvThemePrivate *priv;
-
-	priv = PIDGIN_CONV_THEME_GET_PRIVATE(theme);
-
-	/* I'm not going to test whether this variant is valid! */
-	g_free(priv->variant);
-	priv->variant = g_strdup(variant);
-
-	/* todo, the style has "changed". Ideally, I would like to use signals at this point. */
-}
-
-char *
-pidgin_conversation_theme_get_variant(PidginConvTheme *theme)
-{
-	PidginConvThemePrivate *priv;
-	priv = PIDGIN_CONV_THEME_GET_PRIVATE(theme);
-
-	return g_strdup(priv->variant);
-}
-
-/**
- * Get a list of variants supported by the style.
- */
-GList *
-pidgin_conversation_theme_get_variants(PidginConvTheme *theme)
-{
-	PidginConvThemePrivate *priv;
-	GList *ret = NULL;
-	GDir *variants;
-	const char *css_file;
-	char *css;
-	char *variant_dir;
-
-	priv = PIDGIN_CONV_THEME_GET_PRIVATE(theme);
-
-	variant_dir = g_build_filename(purple_theme_get_dir(PURPLE_THEME(theme)), "Contents", "Resources", "Variants", NULL);
-
-	variants = g_dir_open(variant_dir, 0, NULL);
-	if (!variants)
-		return NULL;
-
-	while ((css_file = g_dir_read_name(variants)) != NULL) {
-		if (!g_str_has_suffix(css_file, ".css"))
-			continue;
-
-		css = g_strndup(css_file, strlen(css_file) - 4);
-		ret = g_list_append(ret, css);
-	}
-
-	g_dir_close(variants);
-	g_free(variant_dir);
-
-	ret = g_list_sort(ret, (GCompareFunc)g_strcmp0);
-	return ret;
-}
-
 char *
 pidgin_conversation_theme_get_css(PidginConvTheme *theme)
 {
--- a/pidgin/gtkconv-theme.h	Mon Sep 19 03:10:41 2011 +0000
+++ b/pidgin/gtkconv-theme.h	Mon Sep 19 18:48:00 2011 +0000
@@ -72,13 +72,14 @@
 const GHashTable *pidgin_conversation_theme_get_info(const PidginConvTheme *theme);
 void pidgin_conversation_theme_set_info(PidginConvTheme *theme, GHashTable *info);
 
+void pidgin_conversation_theme_add_variant(PidginConvTheme *theme, const char *variant);
+const char *pidgin_conversation_theme_get_variant(PidginConvTheme *theme);
+void pidgin_conversation_theme_set_variant(PidginConvTheme *theme, const char *variant);
+const GList *pidgin_conversation_theme_get_variants(PidginConvTheme *theme);
+
 PidginConvTheme *pidgin_conversation_theme_load(const char *styledir);
 PidginConvTheme *pidgin_conversation_theme_copy(const PidginConvTheme *theme);
 void pidgin_conversation_theme_save_state(const PidginConvTheme *theme);
-void pidgin_conversation_theme_read_info_plist(PidginConvTheme *theme, const char *variant);
-char *pidgin_conversation_theme_get_variant(PidginConvTheme *theme);
-GList *pidgin_conversation_theme_get_variants(PidginConvTheme *theme);
-void pidgin_conversation_theme_set_variant(PidginConvTheme *theme, const char *variant);
 
 char *pidgin_conversation_theme_get_css(PidginConvTheme *theme);
 
--- a/pidgin/gtkprefs.c	Mon Sep 19 03:10:41 2011 +0000
+++ b/pidgin/gtkprefs.c	Mon Sep 19 18:48:00 2011 +0000
@@ -496,7 +496,7 @@
 			g_object_unref(G_OBJECT(pixbuf));
 
 	} else if (PIDGIN_IS_CONV_THEME(theme)) {
-		GList *variants;
+		const GList *variants;
 
 		/* No image available? */
 
@@ -509,14 +509,12 @@
 		 */
 		for (variants = pidgin_conversation_theme_get_variants(PIDGIN_CONV_THEME(theme));
 		     variants;
-		     variants = g_list_delete_link(variants, variants)) {
+		     variants = g_list_next(variants)) {
 
 			markup = get_theme_markup(name, FALSE, NULL, variants->data);
 
 			gtk_list_store_append(prefs_conv_themes, &iter);
 			gtk_list_store_set(prefs_conv_themes, &iter, 1, markup, 2, name, -1);
-
-			g_free(variants->data);
 		}
 	}
 }