4 * Copyright (C) 2008 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
21 #include "ui_fileops.h"
26 *--------------------------------------------------------------------------
28 *--------------------------------------------------------------------------
31 static gint file_util_safe_number(gint64 free_space)
40 if (!filelist_read(options->file_ops.safe_delete_path, &list, NULL)) return 0;
51 v = (gint)strtol(fd->name, NULL, 10);
52 if (v >= n) n = v + 1;
57 while (options->file_ops.safe_delete_folder_maxsize > 0 && list &&
58 (free_space < 0 || total + free_space > (gint64)options->file_ops.safe_delete_folder_maxsize * 1048576) )
64 list = filelist_sort(list, SORT_NAME, TRUE);
69 list = g_list_remove(list, fd);
71 DEBUG_1("expunging from trash for space: %s", fd->name);
72 if (!unlink_file(fd->path) && !warned)
74 file_util_warning_dialog(_("Delete failed"),
75 _("Unable to remove old file from trash folder"),
76 GTK_STOCK_DIALOG_WARNING, NULL);
88 void file_util_trash_clear(void)
90 file_util_safe_number(-1);
93 static gchar *file_util_safe_dest(const gchar *path)
97 n = file_util_safe_number(filesize(path));
98 return g_strdup_printf("%s/%06d_%s", options->file_ops.safe_delete_path, n, filename_from_path(path));
101 static void file_util_safe_del_toggle_cb(GtkWidget *button, gpointer data)
103 options->file_ops.safe_delete_enable = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
106 static void file_util_safe_del_close_cb(GtkWidget *dialog, gpointer data)
108 GenericDialog **gd = data;
113 gint file_util_safe_unlink(const char *path)
115 static GenericDialog *gd = NULL;
116 gchar *result = NULL;
119 if (!isfile(path)) return FALSE;
121 if (!isdir(options->file_ops.safe_delete_path))
123 DEBUG_1("creating trash: %s", options->file_ops.safe_delete_path);
124 if (!options->file_ops.safe_delete_path || !mkdir_utf8(options->file_ops.safe_delete_path, 0755))
126 result = _("Could not create folder");
135 dest = file_util_safe_dest(path);
138 DEBUG_1("safe deleting %s to %s", path, dest);
139 success = move_file(path, dest);
146 if (!success && !access_file(path, W_OK))
148 result = _("Permission denied");
158 buf = g_strdup_printf(_("Unable to access or create the trash folder.\n\"%s\""), options->file_ops.safe_delete_path);
159 gd = file_util_warning_dialog(result, buf, GTK_STOCK_DIALOG_WARNING, NULL);
162 button = gtk_check_button_new_with_label(_("Turn off safe delete"));
163 g_signal_connect(G_OBJECT(button), "toggled",
164 G_CALLBACK(file_util_safe_del_toggle_cb), NULL);
165 gtk_box_pack_start(GTK_BOX(gd->vbox), button, FALSE, FALSE, 0);
166 gtk_widget_show(button);
168 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
169 G_CALLBACK(file_util_safe_del_close_cb), &gd);
175 char *file_util_safe_delete_status(void)
179 if (options->editor_command[CMD_DELETE])
181 buf = g_strdup(_("Deletion by external command"));
185 if (options->file_ops.safe_delete_enable)
188 if (options->file_ops.safe_delete_folder_maxsize > 0)
189 buf2 = g_strdup_printf(_(" (max. %d MB)"), options->file_ops.safe_delete_folder_maxsize);
193 buf = g_strdup_printf(_("Safe delete: %s%s\nTrash: %s"), _("on"), buf2, options->file_ops.safe_delete_path);
198 buf = g_strdup_printf(_("Safe delete: %s"), _("off"));