From e38c4bd56752b28f3fa75992555c3ac011f4d3c0 Mon Sep 17 00:00:00 2001 From: Eric Koegel Date: Thu, 9 Feb 2012 22:02:24 +0300 Subject: [PATCH] Keep a list of all images loaded Any images the user loads to the xfdesktop-settings app will be saved to a backdrop list which by default will be called images.list this list will be loaded instead of the default resource location while in Single image mode. Bug 4888 --- settings/main.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 134 insertions(+), 7 deletions(-) diff --git a/settings/main.c b/settings/main.c index 1f76be9..e8c54df 100644 --- a/settings/main.c +++ b/settings/main.c @@ -77,6 +77,8 @@ #define PER_SCREEN_PROP_FORMAT "/backdrop/screen%d/monitor%d" +#define DEFAULT_IMAGE_LOCATIONS_LIST "xfce4/desktop/images.list" + typedef struct { XfconfChannel *channel; @@ -658,6 +660,10 @@ xfdesktop_settings_dialog_populate_image_list(AppearancePanel *panel) } else if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(panel->radio_singleimage))) { GtkTreeIter *tmp; gchar **backdrop_dirs; + gint n_images; + gchar **images = NULL, *list_file; + gchar propname[1024]; + GError *error = NULL; gint i; g_snprintf(prop_last, sizeof(prop_last), @@ -677,13 +683,40 @@ xfdesktop_settings_dialog_populate_image_list(AppearancePanel *panel) xfconf_channel_set_string(panel->channel, prop_image, image_file); xfconf_channel_set_string(panel->channel, prop_last, image_file); - backdrop_dirs = xfce_resource_lookup_all(XFCE_RESOURCE_DATA, - "xfce4/backdrops/"); - for(i = 0; backdrop_dirs[i]; ++i) { - tmp = xfdesktop_image_list_add_dir(ls, backdrop_dirs[i], - image_file); - if(tmp) - image_file_iter = tmp; + g_snprintf(propname, sizeof(propname), "/backdrop/image-locations-list"); + list_file = xfconf_channel_get_string(panel->channel, propname, NULL); + if(!list_file) { + list_file = xfce_resource_save_location(XFCE_RESOURCE_CONFIG, + DEFAULT_IMAGE_LOCATIONS_LIST, + FALSE); + g_warning("Didn't find prop %s when loading image locations list; using default %s", + propname, list_file); + } + + images = xfdesktop_backdrop_list_load(list_file, &n_images, &error); + if(error == NULL) { + for(i = 0; i < n_images; i++) { + /* Add images that still exist on the user's drive and avoid + * adding the current wallpaper twice. + */ + if(xfdesktop_image_file_is_valid(images[i]) && + g_strcmp0(images[i], image_file)) { + xfdesktop_settings_image_treeview_add(GTK_TREE_MODEL(ls), + images[i]); + } + } + } else { + g_error_free(error); + error = NULL; + + backdrop_dirs = xfce_resource_lookup_all(XFCE_RESOURCE_DATA, + "xfce4/backdrops/"); + for(i = 0; backdrop_dirs[i]; ++i) { + tmp = xfdesktop_image_list_add_dir(ls, backdrop_dirs[i], + image_file); + if(tmp) + image_file_iter = tmp; + } } if(!image_file_iter) @@ -813,6 +846,95 @@ xfdesktop_settings_save_backdrop_list(AppearancePanel *panel, return ret; } +static gboolean +xfdesktop_settings_save_image_locations_list(AppearancePanel *panel, + GtkTreeModel *model) +{ + gboolean ret = TRUE; + gint n_images, max_images; + gchar **images = NULL, *list_file; + GtkTreeIter iter; + gchar propname[1024]; + GError *error = NULL; + + TRACE("entering"); + + g_snprintf(propname, sizeof(propname), "/backdrop/image-locations-list"); + list_file = xfconf_channel_get_string(panel->channel, propname, NULL); + if(!list_file) { + list_file = xfce_resource_save_location(XFCE_RESOURCE_CONFIG, + DEFAULT_IMAGE_LOCATIONS_LIST, + TRUE); + g_warning("Didn't find prop %s when saving image locations list; using default %s", + propname, list_file); + } + + images = xfdesktop_backdrop_list_load(list_file, &n_images, &error); + if(error != NULL) { + g_warning("Unable to load image locations list."); + n_images = 0; + g_error_free(error); + error = NULL; + } + + /* resize to the max possible, rather than resizing on every addition */ + max_images = n_images + gtk_tree_model_iter_n_children(model, NULL); + images = g_renew(gchar *, images, max_images + 1); + images[max_images] = NULL; + + if(gtk_tree_model_get_iter_first(model, &iter)) { + gboolean exists = FALSE; + gchar *image; + gint i = 0; + + do { + exists = FALSE; + gtk_tree_model_get(model, &iter, + COL_FILENAME, &image, + -1); + + /* find and add only unique entries to the list */ + for(i = 0; i < n_images; i++) { + if(g_strcmp0(image, images[i]) == 0) { + exists = TRUE; + break; + } + } + + if(!exists) { + DBG("Added %s to image locations list", image); + images[n_images] = image; + n_images++; + } + } while(gtk_tree_model_iter_next(model, &iter)); + } + + /* resize down to only what was used */ + images = g_renew(gchar *, images, n_images + 1); + images[n_images] = NULL; + + if(!xfdesktop_backdrop_list_save(list_file, images, &error)) { + gchar *primary = g_strdup_printf(_("Failed to write image locations list to \"%s\""), + list_file); + + xfce_message_dialog(GTK_WINDOW(gtk_widget_get_toplevel(panel->frame_image_list)), + _("Image locations List Error"), GTK_STOCK_DIALOG_ERROR, + primary, error->message, + GTK_STOCK_CLOSE, GTK_RESPONSE_ACCEPT, NULL); + + g_free(primary); + g_error_free(error); + ret = FALSE; + } else { + xfconf_channel_set_string(panel->channel, propname, list_file); + } + + g_free(list_file); + g_strfreev(images); + + return ret; +} + static void add_file_button_clicked(GtkWidget *button, gpointer user_data) @@ -885,6 +1007,8 @@ add_file_button_clicked(GtkWidget *button, if(gtk_tree_model_iter_n_children(model, NULL) == 1) g_spawn_command_line_async("xfdesktop --reload", NULL); } + + xfdesktop_settings_save_image_locations_list(panel, model); } gtk_widget_destroy(chooser); @@ -1185,6 +1309,9 @@ image_treeview_drag_data_received(GtkWidget *widget, if(file_added && panel->image_list_loaded) xfdesktop_settings_save_backdrop_list(panel, model); + + if(file_added) + xfdesktop_settings_save_image_locations_list(panel, model); } static void -- 1.7.5.4