From c09e997e57ce05d4917a01015cc7b377259107be Mon Sep 17 00:00:00 2001
From: Federico Mena Quintero <federico@novell.com>
Date: Tue, 8 Dec 2009 13:06:14 -0600
Subject: [PATCH 1/2] gnome-bg: Use the schema defaults if the user-configured background is not available

Previously we only used the default filename from the schema, instead of
using all the other values as well (color type, etc.).  This change
fully resets the GnomeBG to the schema defaults if the background file
is not available.

Signed-off-by: Federico Mena Quintero <federico@novell.com>
---
 libgnome-desktop/gnome-bg.c |   68 ++++++++++++++++++++++++++++++++++--------
 1 files changed, 55 insertions(+), 13 deletions(-)

diff --git a/libgnome-desktop/gnome-bg.c b/libgnome-desktop/gnome-bg.c
index 0f1d564..f126e57 100644
--- a/libgnome-desktop/gnome-bg.c
+++ b/libgnome-desktop/gnome-bg.c
@@ -275,6 +275,55 @@ queue_changed (GnomeBG *bg)
 					     NULL);
 }
 
+static char *
+get_default_string_from_schema (GConfClient *client, const char *key)
+{
+	GConfValue *value;
+	char *str;
+
+	value = gconf_client_get_default_from_schema (client, key, NULL);
+	if (!value)
+		return NULL;
+
+	str = g_strdup (gconf_value_get_string (value));
+	gconf_value_free (value);
+	return str;
+}
+
+static void
+reset_to_schema_defaults (GnomeBG *bg, GConfClient *client)
+{
+	char *filename;
+	char *str;
+	GdkColor c1, c2;
+	GnomeBGColorType ctype;
+	GnomeBGPlacement placement;
+
+	filename = get_default_string_from_schema (client, BG_KEY_PICTURE_FILENAME);
+
+	str = get_default_string_from_schema (client, BG_KEY_PRIMARY_COLOR);
+	color_from_string (str, &c1);
+	g_free (str);
+
+	str = get_default_string_from_schema (client, BG_KEY_SECONDARY_COLOR);
+	color_from_string (str, &c2);
+	g_free (str);
+
+	str = get_default_string_from_schema (client, BG_KEY_COLOR_TYPE);
+	color_type_from_string (str, &ctype);
+	g_free (str);
+
+	str = get_default_string_from_schema (client, BG_KEY_PICTURE_PLACEMENT);
+	placement_from_string (str, &placement);
+	g_free (str);
+
+	gnome_bg_set_color (bg, ctype, &c1, &c2);
+	gnome_bg_set_placement (bg, placement);
+	gnome_bg_set_filename (bg, filename);
+
+	g_free (filename);
+}
+
 void
 gnome_bg_load_from_preferences (GnomeBG     *bg,
 				GConfClient *client)
@@ -300,23 +349,16 @@ gnome_bg_load_from_preferences (GnomeBG     *bg,
 		}
 
 		/* Fall back to default background if filename was set
-		   but no longer exists */
+		 * but no longer exists.
+		 */
 		if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
-			GConfValue *default_value;
-
 			g_free (filename);
-			filename = NULL;
-
-			default_value =
-				gconf_client_get_default_from_schema (client,
-								      BG_KEY_PICTURE_FILENAME,
-								      NULL);
-			if (default_value != NULL) {
-				filename = g_strdup (gconf_value_get_string (default_value));
-				gconf_value_free (default_value);
-			}
+			g_free (tmp);
+			reset_to_schema_defaults (bg, client);
+			return;
 		}
 	}
+
 	g_free (tmp);
 
 	/* Colors */
-- 
1.6.4.2


From 2a5de399399eea78a1eaaa6106db9c2274f840d7 Mon Sep 17 00:00:00 2001
From: Federico Mena Quintero <federico@novell.com>
Date: Tue, 8 Dec 2009 14:59:31 -0600
Subject: [PATCH 2/2] gnome-bg: Use the schema defaults if the user changes to a nonexistent filename

Previously we would only use the colors that were configured.  With this new
scheme, we use the schema defaults to revert to the system-default background.

Signed-off-by: Federico Mena Quintero <federico@novell.com>
---
 libgnome-desktop/gnome-bg.c |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/libgnome-desktop/gnome-bg.c b/libgnome-desktop/gnome-bg.c
index f126e57..1d03790 100644
--- a/libgnome-desktop/gnome-bg.c
+++ b/libgnome-desktop/gnome-bg.c
@@ -1334,6 +1334,27 @@ get_as_pixbuf (GnomeBG *bg, const char *filename)
 
 		if (pixbuf)
 			file_cache_add_pixbuf (bg, filename, pixbuf);
+		else {
+			GConfClient *client;
+
+			/* We revert to the schema defaults as we couldn't load the image.
+			 * For bonus points, we should overlay a "broken image" icon
+			 * on top of the default one.  This is left as an exercise for the reader.
+			 */
+
+			/* FIXME: elsewhere we use a GConfClient supplied by the caller, but here
+			 * we don't have one!  We will have to use the default one.
+			 */
+			client = gconf_client_get_default ();
+			reset_to_schema_defaults (bg, client);
+			g_object_unref (client);
+
+			if (bg->filename) {
+				pixbuf = gdk_pixbuf_new_from_file (bg->filename, NULL);
+				if (pixbuf)
+					file_cache_add_pixbuf (bg, bg->filename, pixbuf);
+			}
+		}
 
 		return pixbuf;
 	}
-- 
1.6.4.2

