2 * Copyright (C) 2006 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "ui_fileops.h"
33 *--------------------------------------------------------------------------
35 *--------------------------------------------------------------------------
38 static gint file_util_safe_number(gint64 free_space)
44 gboolean sorted = FALSE;
45 gboolean warned = FALSE;
48 dir_fd = file_data_new_dir(options->file_ops.safe_delete_path);
49 if (!filelist_read(dir_fd, &list, NULL))
51 file_data_unref(dir_fd);
54 file_data_unref(dir_fd);
65 v = (gint)strtol(fd->name, NULL, 10);
66 if (v >= n) n = v + 1;
71 while (options->file_ops.safe_delete_folder_maxsize > 0 && list &&
72 (free_space < 0 || total + free_space > (gint64)options->file_ops.safe_delete_folder_maxsize * 1048576) )
78 list = filelist_sort(list, SORT_NAME, TRUE);
83 list = g_list_remove(list, fd);
85 DEBUG_1("expunging from trash for space: %s", fd->name);
86 if (!unlink_file(fd->path) && !warned)
88 file_util_warning_dialog(_("Delete failed"),
89 _("Unable to remove old file from trash folder"),
90 GTK_STOCK_DIALOG_WARNING, NULL);
102 void file_util_trash_clear(void)
104 file_util_safe_number(-1);
107 static gchar *file_util_safe_dest(const gchar *path)
113 n = file_util_safe_number(filesize(path));
114 name = g_strdup_printf("%06d_%s", n, filename_from_path(path));
115 dest = g_build_filename(options->file_ops.safe_delete_path, name, NULL);
121 static void file_util_safe_del_toggle_cb(GtkWidget *button, gpointer data)
123 options->file_ops.safe_delete_enable = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
126 static void file_util_safe_del_close_cb(GtkWidget *dialog, gpointer data)
128 GenericDialog **gd = data;
133 gboolean file_util_safe_unlink(const gchar *path)
135 static GenericDialog *gd = NULL;
136 gchar *result = NULL;
137 gboolean success = TRUE;
139 if (!isfile(path)) return FALSE;
141 if (!isdir(options->file_ops.safe_delete_path))
143 DEBUG_1("creating trash: %s", options->file_ops.safe_delete_path);
144 if (!options->file_ops.safe_delete_path || !mkdir_utf8(options->file_ops.safe_delete_path, 0755))
146 result = _("Could not create folder");
155 dest = file_util_safe_dest(path);
158 DEBUG_1("safe deleting %s to %s", path, dest);
159 success = move_file(path, dest);
166 if (!success && !access_file(path, W_OK))
168 result = _("Permission denied");
178 buf = g_strdup_printf(_("Unable to access or create the trash folder.\n\"%s\""), options->file_ops.safe_delete_path);
179 gd = file_util_warning_dialog(result, buf, GTK_STOCK_DIALOG_WARNING, NULL);
182 button = gtk_check_button_new_with_label(_("Turn off safe delete"));
183 g_signal_connect(G_OBJECT(button), "toggled",
184 G_CALLBACK(file_util_safe_del_toggle_cb), NULL);
185 gtk_box_pack_start(GTK_BOX(gd->vbox), button, FALSE, FALSE, 0);
186 gtk_widget_show(button);
188 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
189 G_CALLBACK(file_util_safe_del_close_cb), &gd);
195 gchar *file_util_safe_delete_status(void)
199 if (is_valid_editor_command(CMD_DELETE))
201 buf = g_strdup(_("Deletion by external command"));
205 if (options->file_ops.safe_delete_enable)
208 if (options->file_ops.safe_delete_folder_maxsize > 0)
209 buf2 = g_strdup_printf(_(" (max. %d MB)"), options->file_ops.safe_delete_folder_maxsize);
213 buf = g_strdup_printf(_("Safe delete: %s%s\nTrash: %s"), _("on"), buf2, options->file_ops.safe_delete_path);
218 buf = g_strdup_printf(_("Safe delete: %s"), _("off"));
224 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */