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!
20 #include "ui_fileops.h"
25 *--------------------------------------------------------------------------
27 *--------------------------------------------------------------------------
30 static gint file_util_safe_number(gint64 free_space)
39 if (!filelist_read(options->file_ops.safe_delete_path, &list, NULL)) return 0;
50 v = (gint)strtol(fd->name, NULL, 10);
51 if (v >= n) n = v + 1;
56 while (options->file_ops.safe_delete_folder_maxsize > 0 && list &&
57 (free_space < 0 || total + free_space > (gint64)options->file_ops.safe_delete_folder_maxsize * 1048576) )
63 list = filelist_sort(list, SORT_NAME, TRUE);
68 list = g_list_remove(list, fd);
70 DEBUG_1("expunging from trash for space: %s", fd->name);
71 if (!unlink_file(fd->path) && !warned)
73 file_util_warning_dialog(_("Delete failed"),
74 _("Unable to remove old file from trash folder"),
75 GTK_STOCK_DIALOG_WARNING, NULL);
87 void file_util_trash_clear(void)
89 file_util_safe_number(-1);
92 static gchar *file_util_safe_dest(const gchar *path)
98 n = file_util_safe_number(filesize(path));
99 name = g_strdup_printf("%06d_%s", n, filename_from_path(path));
100 dest = g_build_filename(options->file_ops.safe_delete_path, name, NULL);
106 static void file_util_safe_del_toggle_cb(GtkWidget *button, gpointer data)
108 options->file_ops.safe_delete_enable = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
111 static void file_util_safe_del_close_cb(GtkWidget *dialog, gpointer data)
113 GenericDialog **gd = data;
118 gint file_util_safe_unlink(const gchar *path)
120 static GenericDialog *gd = NULL;
121 gchar *result = NULL;
124 if (!isfile(path)) return FALSE;
126 if (!isdir(options->file_ops.safe_delete_path))
128 DEBUG_1("creating trash: %s", options->file_ops.safe_delete_path);
129 if (!options->file_ops.safe_delete_path || !mkdir_utf8(options->file_ops.safe_delete_path, 0755))
131 result = _("Could not create folder");
140 dest = file_util_safe_dest(path);
143 DEBUG_1("safe deleting %s to %s", path, dest);
144 success = move_file(path, dest);
151 if (!success && !access_file(path, W_OK))
153 result = _("Permission denied");
163 buf = g_strdup_printf(_("Unable to access or create the trash folder.\n\"%s\""), options->file_ops.safe_delete_path);
164 gd = file_util_warning_dialog(result, buf, GTK_STOCK_DIALOG_WARNING, NULL);
167 button = gtk_check_button_new_with_label(_("Turn off safe delete"));
168 g_signal_connect(G_OBJECT(button), "toggled",
169 G_CALLBACK(file_util_safe_del_toggle_cb), NULL);
170 gtk_box_pack_start(GTK_BOX(gd->vbox), button, FALSE, FALSE, 0);
171 gtk_widget_show(button);
173 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
174 G_CALLBACK(file_util_safe_del_close_cb), &gd);
180 gchar *file_util_safe_delete_status(void)
184 if (options->editor_command[CMD_DELETE])
186 buf = g_strdup(_("Deletion by external command"));
190 if (options->file_ops.safe_delete_enable)
193 if (options->file_ops.safe_delete_folder_maxsize > 0)
194 buf2 = g_strdup_printf(_(" (max. %d MB)"), options->file_ops.safe_delete_folder_maxsize);
198 buf = g_strdup_printf(_("Safe delete: %s%s\nTrash: %s"), _("on"), buf2, options->file_ops.safe_delete_path);
203 buf = g_strdup_printf(_("Safe delete: %s"), _("off"));