From 82daa8c28ea67a895b2703256733cba5dd889778 Mon Sep 17 00:00:00 2001 From: Eric Koegel Date: Tue, 1 Oct 2013 19:24:17 +0300 Subject: [PATCH] Migrate backdrop settings from previous versions (Bug 10380) This patch allows for migrating from previous versions (before 4.11). Continuing to use the older format should make it easier for distribution maintainers to apply their own backdrop settings since the newer format uses monitor names rather than numbers. Xfdesktop will search for backdrop settings in the new location, then the old location, and finally fall back to the DEFAULT_BACKDROP. --- settings/main.c | 75 ++++++++++++++++++++++++++------- src/xfce-backdrop.c | 4 +- src/xfce-workspace.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 179 insertions(+), 17 deletions(-) diff --git a/settings/main.c b/settings/main.c index e132506..33fb0b6 100644 --- a/settings/main.c +++ b/settings/main.c @@ -158,7 +158,7 @@ static void cb_xfdesktop_chk_apply_to_all(GtkCheckButton *button, gpointer user_data); static gchar *xfdesktop_settings_generate_per_workspace_binding_string(AppearancePanel *panel, const gchar* property); - +static gchar *xfdesktop_settings_get_backdrop_image(AppearancePanel *panel); static void xfdesktop_settings_do_single_preview(GtkTreeModel *model, @@ -615,7 +615,6 @@ xfdesktop_image_list_add_dir(GObject *source_object, gpointer user_data) { AppearancePanel *panel = user_data; - gchar *property; AddDirData *dir_data = g_new0(AddDirData, 1); TRACE("entering"); @@ -627,9 +626,7 @@ xfdesktop_image_list_add_dir(GObject *source_object, /* Get the last image/current image displayed so we can select it in the * icon view */ - property = xfdesktop_settings_generate_per_workspace_binding_string(panel, "last-image"); - - dir_data->last_image = xfconf_channel_get_string(panel->channel, property, DEFAULT_BACKDROP); + dir_data->last_image = xfdesktop_settings_get_backdrop_image(panel); dir_data->file_path = g_file_get_path(panel->selected_folder); @@ -643,8 +640,6 @@ xfdesktop_image_list_add_dir(GObject *source_object, xfdesktop_image_list_add_item, dir_data, cb_destroy_add_dir_enumeration); - - g_free(property); } static void @@ -723,6 +718,51 @@ xfdesktop_settings_generate_per_workspace_binding_string(AppearancePanel *panel, return buf; } +static gchar* +xfdesktop_settings_generate_old_binding_string(AppearancePanel *panel, + const gchar* property) +{ + gchar *buf = NULL; + + buf = g_strdup_printf("/backdrop/screen%d/monitor%d/%s", + panel->screen, panel->monitor, property); + + DBG("name %s", buf); + + return buf; +} + +/* Attempts to load the backdrop from the current location followed by using + * how previous versions of xfdesktop (before 4.11) did. This matches how + * xfdesktop searches for backdrops. + * Free the returned string when done using it. */ +static gchar * +xfdesktop_settings_get_backdrop_image(AppearancePanel *panel) +{ + gchar *last_image; + gchar *property, *old_property = NULL; + + /* Get the last image/current image displayed, if available */ + property = xfdesktop_settings_generate_per_workspace_binding_string(panel, "last-image"); + + last_image = xfconf_channel_get_string(panel->channel, property, NULL); + + /* Try the previous version or fall back to our provided default */ + if(last_image == NULL) { + old_property = xfdesktop_settings_generate_old_binding_string(panel, + "image-path"); + last_image = xfconf_channel_get_string(panel->channel, + old_property, + DEFAULT_BACKDROP); + } + + g_free(property); + if(old_property) + g_free(old_property); + + return last_image; +} + static void cb_image_selection_changed(GtkIconView *icon_view, gpointer user_data) @@ -732,7 +772,7 @@ cb_image_selection_changed(GtkIconView *icon_view, GtkTreeIter iter; GList *selected_items = NULL; gchar *filename = NULL, *current_filename = NULL; - gchar *buf; + gchar *buf = NULL; TRACE("entering"); @@ -751,9 +791,8 @@ cb_image_selection_changed(GtkIconView *icon_view, gtk_tree_model_get(model, &iter, COL_FILENAME, &filename, -1); - buf = xfdesktop_settings_generate_per_workspace_binding_string(panel, "last-image"); - - current_filename = xfconf_channel_get_string(panel->channel, buf, ""); + /* Get the current/last image, handles migrating from old versions */ + current_filename = xfdesktop_settings_get_backdrop_image(panel); /* check to see if the selection actually did change */ if(g_strcmp0(current_filename, filename) != 0) { @@ -765,13 +804,19 @@ cb_image_selection_changed(GtkIconView *icon_view, panel->screen, panel->monitor_name, panel->workspace); } + /* Get the property location to save our changes, always save to new + * location */ + buf = xfdesktop_settings_generate_per_workspace_binding_string(panel, + "last-image"); + xfconf_channel_set_string(panel->channel, buf, filename); } g_list_foreach (selected_items, (GFunc)gtk_tree_path_free, NULL); g_list_free(selected_items); g_free(current_filename); - g_free(buf); + if(buf) + g_free(buf); } static gint @@ -1033,12 +1078,11 @@ cb_xfdesktop_combo_color_changed(GtkComboBox *combo, static void xfdesktop_settings_update_iconview_folder(AppearancePanel *panel) { - gchar *current_folder, *prop_last, *dirname; + gchar *current_folder, *dirname; TRACE("entering"); - prop_last = xfdesktop_settings_generate_per_workspace_binding_string(panel, "last-image"); - current_folder = xfconf_channel_get_string(panel->channel, prop_last, DEFAULT_BACKDROP); + current_folder = xfdesktop_settings_get_backdrop_image(panel); dirname = g_path_get_dirname(current_folder); gtk_file_chooser_set_current_folder((GtkFileChooser*)panel->btn_folder, dirname); @@ -1047,7 +1091,6 @@ xfdesktop_settings_update_iconview_folder(AppearancePanel *panel) cb_folder_selection_changed(panel->btn_folder, panel); g_free(current_folder); - g_free(prop_last); g_free(dirname); } diff --git a/src/xfce-backdrop.c b/src/xfce-backdrop.c index c4827a9..757dc9f 100644 --- a/src/xfce-backdrop.c +++ b/src/xfce-backdrop.c @@ -297,11 +297,13 @@ xfce_backdrop_class_init(XfceBackdropClass *klass) XFCE_BACKDROP_IMAGE_SCALED, XFDESKTOP_PARAM_FLAGS)); + /* The DEFAULT_BACKDROP is provided in the function + * xfce_workspace_migrate_backdrop_settings instead of here */ g_object_class_install_property(gobject_class, PROP_IMAGE_FILENAME, g_param_spec_string("image-filename", "image filename", "image filename", - DEFAULT_BACKDROP, + NULL, XFDESKTOP_PARAM_FLAGS)); g_object_class_install_property(gobject_class, PROP_BACKDROP_CYCLE_ENABLE, diff --git a/src/xfce-workspace.c b/src/xfce-workspace.c index 70796d8..e134ef9 100644 --- a/src/xfce-workspace.c +++ b/src/xfce-workspace.c @@ -314,6 +314,119 @@ xfce_workspace_get_property(GObject *object, } } +/* The image styles changed from versions prior to 4.11. Convert Auto to + * stretched since auto isn't an option anymore */ +static XfceBackdropImageStyle +xfce_workspace_translate_image_styles(gint image_style) +{ + if(image_style == 0) + image_style = XFCE_BACKDROP_IMAGE_STRETCHED; + + return image_style; +} + +/* This function is for migrating from previous versions of xfdesktop (before + * 4.11) and should make it easier for distribution maintainers to apply their + * own backdrop settings for new installs */ +static void +xfce_workspace_migrate_backdrop_settings(XfceWorkspace *workspace, + XfceBackdrop *backdrop, + guint monitor) +{ + XfconfChannel *channel = workspace->priv->channel; + char buf[1024]; + gint pp_len; + GValue value = { 0, }; + const gchar *filename; + + /* Attempt to use the old single image if it's available */ + g_snprintf(buf, sizeof(buf), "%smonitor%d/", + workspace->priv->property_prefix, monitor); + pp_len = strlen(buf); + + /* Try the old backdrop first */ + buf[pp_len] = 0; + g_strlcat(buf, "image-path", sizeof(buf)); + xfconf_channel_get_property(channel, buf, &value); + + /* Either there was a backdrop to migrate from or we use the backdrop + * we provide as a default */ + if(G_VALUE_HOLDS_STRING(&value)) + filename = g_value_get_string(&value); + else + filename = DEFAULT_BACKDROP; + + xfce_backdrop_set_image_filename(backdrop, filename); + + /* If we used the DEFAULT_BACKDROP there's no reason to to pull any + * other settings */ + if(!G_VALUE_HOLDS_STRING(&value)) + return; + + g_value_unset(&value); + + /* Color style */ + buf[pp_len] = 0; + g_strlcat(buf, "color-style", sizeof(buf)); + xfconf_channel_get_property(channel, buf, &value); + + if(G_VALUE_HOLDS_ENUM(&value)) { + xfce_backdrop_set_color_style(backdrop, g_value_get_enum(&value)); + g_value_unset(&value); + } + + /* first color */ + buf[pp_len] = 0; + g_strlcat(buf, "color1", sizeof(buf)); + xfconf_channel_get_property(channel, buf, &value); + + if(G_VALUE_HOLDS_BOXED(&value)) { + xfce_backdrop_set_first_color(backdrop, g_value_get_boxed(&value)); + g_value_unset(&value); + } + + /* second color */ + buf[pp_len] = 0; + g_strlcat(buf, "color2", sizeof(buf)); + xfconf_channel_get_property(channel, buf, &value); + + if(G_VALUE_HOLDS_BOXED(&value)) { + xfce_backdrop_set_second_color(backdrop, g_value_get_boxed(&value)); + g_value_unset(&value); + } + + /* show image */ + buf[pp_len] = 0; + g_strlcat(buf, "image-show", sizeof(buf)); + xfconf_channel_get_property(channel, buf, &value); + + if(G_VALUE_HOLDS_BOOLEAN(&value)) { + gboolean show_image = g_value_get_boolean(&value); + + /* if we aren't showing the image, set the style and exit the function + * so we don't set the style to something else */ + if(!show_image) { + xfce_backdrop_set_image_style(backdrop, XFCE_BACKDROP_IMAGE_NONE); + g_value_unset(&value); + return; + } + + g_value_unset(&value); + } + + /* image style */ + buf[pp_len] = 0; + g_strlcat(buf, "image-style", sizeof(buf)); + xfconf_channel_get_property(channel, buf, &value); + + if(G_VALUE_HOLDS_ENUM(&value)) { + XfceBackdropImageStyle style; + style = xfce_workspace_translate_image_styles(g_value_get_enum(&value)); + xfce_backdrop_set_image_style(backdrop, style); + g_value_unset(&value); + } +} + static void xfce_workspace_connect_backdrop_settings(XfceWorkspace *workspace, XfceBackdrop *backdrop, @@ -378,6 +491,10 @@ xfce_workspace_connect_backdrop_settings(XfceWorkspace *workspace, xfconf_g_property_bind(channel, buf, G_TYPE_STRING, G_OBJECT(backdrop), "image-filename"); + /* If we didn't get a filename try to load one from a previous version */ + if(xfce_backdrop_get_image_filename(backdrop) == NULL) + xfce_workspace_migrate_backdrop_settings(workspace, backdrop, monitor); + g_free(monitor_name); } -- 1.8.3.2