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.
29 #include "ui-fileops.h"
32 *--------------------------------------------------------------------------
34 *--------------------------------------------------------------------------
37 static gint file_util_safe_number(gint64 free_space)
43 gboolean sorted = FALSE;
44 gboolean warned = FALSE;
47 dir_fd = file_data_new_dir(options->file_ops.safe_delete_path);
48 if (!filelist_read(dir_fd, &list, NULL))
50 file_data_unref(dir_fd);
53 file_data_unref(dir_fd);
64 v = (gint)strtol(fd->name, NULL, 10);
65 if (v >= n) n = v + 1;
70 while (options->file_ops.safe_delete_folder_maxsize > 0 && list &&
71 (free_space < 0 || total + free_space > (gint64)options->file_ops.safe_delete_folder_maxsize * 1048576) )
77 list = filelist_sort(list, SORT_NAME, TRUE);
82 list = g_list_remove(list, fd);
84 DEBUG_1("expunging from trash for space: %s", fd->name);
85 if (!unlink_file(fd->path) && !warned)
87 file_util_warning_dialog(_("Delete failed"),
88 _("Unable to remove old file from trash folder"),
89 GTK_STOCK_DIALOG_WARNING, NULL);
101 void file_util_trash_clear(void)
103 file_util_safe_number(-1);
106 static gchar *file_util_safe_dest(const gchar *path)
112 n = file_util_safe_number(filesize(path));
113 name = g_strdup_printf("%06d_%s", n, filename_from_path(path));
114 dest = g_build_filename(options->file_ops.safe_delete_path, name, NULL);
120 static void move_to_trash_failed_cb(GenericDialog *UNUSED(gd), gpointer UNUSED(data))
122 help_window_show("TrashFailed.html");
125 gboolean file_util_safe_unlink(const gchar *path)
127 static GenericDialog *gd = NULL;
128 gchar *result = NULL;
129 gboolean success = TRUE;
132 if (!isfile(path)) return FALSE;
134 if (options->file_ops.no_trash)
136 if (!unlink_file(path))
138 file_util_warning_dialog(_("Delete failed"),
139 _("Unable to remove file"),
140 GTK_STOCK_DIALOG_WARNING, NULL);
144 else if (!options->file_ops.use_system_trash)
146 if (!isdir(options->file_ops.safe_delete_path))
148 DEBUG_1("creating trash: %s", options->file_ops.safe_delete_path);
149 if (!options->file_ops.safe_delete_path || !mkdir_utf8(options->file_ops.safe_delete_path, 0755))
151 result = _("Could not create folder");
160 dest = file_util_safe_dest(path);
163 DEBUG_1("safe deleting %s to %s", path, dest);
164 success = move_file(path, dest);
171 if (!success && !access_file(path, W_OK))
173 result = _("Permission denied");
182 buf = g_strdup_printf(_("Unable to access or create the trash folder.\n\"%s\""), options->file_ops.safe_delete_path);
183 gd = file_util_warning_dialog(result, buf, GTK_STOCK_DIALOG_WARNING, NULL);
189 GFile *tmp = g_file_new_for_path(path);
190 GError *error = NULL;
192 if (!g_file_trash(tmp, FALSE, &error) )
194 message = g_strconcat("See the Help file for a possible workaround.\n\n", error->message, NULL);
195 gd = warning_dialog(_("Move to trash failed\n\n"), message, GTK_STOCK_DIALOG_ERROR, NULL);
196 generic_dialog_add_button(gd, GTK_STOCK_HELP, "Help", move_to_trash_failed_cb, FALSE);
201 /* A second warning dialog is not necessary */
209 gchar *file_util_safe_delete_status(void)
213 if (is_valid_editor_command(CMD_DELETE))
215 buf = g_strdup(_("Deletion by external command"));
217 else if (options->file_ops.no_trash)
219 buf = g_strdup(_("Deleting without trash"));
221 else if (options->file_ops.safe_delete_enable)
223 if (!options->file_ops.use_system_trash)
226 if (options->file_ops.safe_delete_folder_maxsize > 0)
227 buf2 = g_strdup_printf(_(" (max. %d MiB)"), options->file_ops.safe_delete_folder_maxsize);
231 buf = g_strdup_printf(_("Using Geeqie Trash bin\n%s"), buf2);
236 buf = g_strdup(_("Using system Trash bin"));
242 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */