Mercurial > geeqie.yaz
annotate src/trash.c @ 600:9c28465c95d1
Tidy up.
author | zas_ |
---|---|
date | Tue, 06 May 2008 23:58:08 +0000 |
parents | 5da092a6a92a |
children | 8268cbe682f1 |
rev | line source |
---|---|
1 | 1 /* |
196 | 2 * Geeqie |
67
f63ecca6c087
Fri Oct 13 05:22:43 2006 John Ellis <johne@verizon.net>
gqview
parents:
66
diff
changeset
|
3 * (C) 2006 John Ellis |
475 | 4 * Copyright (C) 2008 The Geeqie Team |
1 | 5 * |
6 * Author: John Ellis | |
7 * | |
9 | 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! | |
1 | 11 */ |
12 | |
9 | 13 |
281 | 14 #include "main.h" |
597 | 15 #include "trash.h" |
9 | 16 #include "utilops.h" |
17 | |
18 | |
507 | 19 #include "debug.h" |
586 | 20 #include "filedata.h" |
9 | 21 #include "ui_fileops.h" |
22 #include "ui_misc.h" | |
23 | |
24 | |
25 /* | |
26 *-------------------------------------------------------------------------- | |
27 * Safe Delete | |
28 *-------------------------------------------------------------------------- | |
29 */ | |
30 | |
31 static gint file_util_safe_number(gint64 free_space) | |
32 { | |
33 gint n = 0; | |
34 gint64 total = 0; | |
35 GList *list; | |
36 GList *work; | |
37 gint sorted = FALSE; | |
38 gint warned = FALSE; | |
39 | |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
40 if (!filelist_read(options->file_ops.safe_delete_path, &list, NULL)) return 0; |
9 | 41 |
42 work = list; | |
43 while (work) | |
1 | 44 { |
9 | 45 FileData *fd; |
46 gint v; | |
442 | 47 |
9 | 48 fd = work->data; |
49 work = work->next; | |
50 | |
51 v = (gint)strtol(fd->name, NULL, 10); | |
52 if (v >= n) n = v + 1; | |
53 | |
54 total += fd->size; | |
1 | 55 } |
9 | 56 |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
57 while (options->file_ops.safe_delete_folder_maxsize > 0 && list && |
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
58 (free_space < 0 || total + free_space > (gint64)options->file_ops.safe_delete_folder_maxsize * 1048576) ) |
9 | 59 { |
60 FileData *fd; | |
61 | |
62 if (!sorted) | |
63 { | |
64 list = filelist_sort(list, SORT_NAME, TRUE); | |
65 sorted = TRUE; | |
66 } | |
67 | |
68 fd = list->data; | |
69 list = g_list_remove(list, fd); | |
70 | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
497
diff
changeset
|
71 DEBUG_1("expunging from trash for space: %s", fd->name); |
9 | 72 if (!unlink_file(fd->path) && !warned) |
73 { | |
74 file_util_warning_dialog(_("Delete failed"), | |
75 _("Unable to remove old file from trash folder"), | |
76 GTK_STOCK_DIALOG_WARNING, NULL); | |
77 warned = TRUE; | |
78 } | |
79 total -= fd->size; | |
138 | 80 file_data_unref(fd); |
9 | 81 } |
82 | |
83 filelist_free(list); | |
84 | |
85 return n; | |
86 } | |
87 | |
88 void file_util_trash_clear(void) | |
89 { | |
90 file_util_safe_number(-1); | |
91 } | |
92 | |
93 static gchar *file_util_safe_dest(const gchar *path) | |
94 { | |
95 gint n; | |
96 | |
97 n = file_util_safe_number(filesize(path)); | |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
98 return g_strdup_printf("%s/%06d_%s", options->file_ops.safe_delete_path, n, filename_from_path(path)); |
9 | 99 } |
100 | |
101 static void file_util_safe_del_toggle_cb(GtkWidget *button, gpointer data) | |
102 { | |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
103 options->file_ops.safe_delete_enable = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)); |
9 | 104 } |
105 | |
106 static void file_util_safe_del_close_cb(GtkWidget *dialog, gpointer data) | |
107 { | |
108 GenericDialog **gd = data; | |
109 | |
110 *gd = NULL; | |
1 | 111 } |
112 | |
600 | 113 gint file_util_safe_unlink(const gchar *path) |
1 | 114 { |
9 | 115 static GenericDialog *gd = NULL; |
116 gchar *result = NULL; | |
117 gint success = TRUE; | |
118 | |
597 | 119 if (!isfile(path)) return FALSE; |
9 | 120 |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
121 if (!isdir(options->file_ops.safe_delete_path)) |
9 | 122 { |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
497
diff
changeset
|
123 DEBUG_1("creating trash: %s", options->file_ops.safe_delete_path); |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
124 if (!options->file_ops.safe_delete_path || !mkdir_utf8(options->file_ops.safe_delete_path, 0755)) |
9 | 125 { |
126 result = _("Could not create folder"); | |
127 success = FALSE; | |
128 } | |
129 } | |
130 | |
131 if (success) | |
132 { | |
133 gchar *dest; | |
134 | |
597 | 135 dest = file_util_safe_dest(path); |
9 | 136 if (dest) |
137 { | |
597 | 138 DEBUG_1("safe deleting %s to %s", path, dest); |
139 success = move_file(path, dest); | |
9 | 140 } |
141 else | |
142 { | |
143 success = FALSE; | |
144 } | |
145 | |
597 | 146 if (!success && !access_file(path, W_OK)) |
9 | 147 { |
148 result = _("Permission denied"); | |
149 } | |
150 g_free(dest); | |
151 } | |
152 | |
153 if (result && !gd) | |
154 { | |
155 GtkWidget *button; | |
156 gchar *buf; | |
157 | |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
158 buf = g_strdup_printf(_("Unable to access or create the trash folder.\n\"%s\""), options->file_ops.safe_delete_path); |
9 | 159 gd = file_util_warning_dialog(result, buf, GTK_STOCK_DIALOG_WARNING, NULL); |
160 g_free(buf); | |
161 | |
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); | |
167 | |
168 g_signal_connect(G_OBJECT(gd->dialog), "destroy", | |
169 G_CALLBACK(file_util_safe_del_close_cb), &gd); | |
170 } | |
171 | |
172 return success; | |
1 | 173 } |
174 | |
600 | 175 gchar *file_util_safe_delete_status(void) |
1 | 176 { |
9 | 177 gchar *buf; |
178 | |
318 | 179 if (options->editor_command[CMD_DELETE]) |
223
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
180 { |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
181 buf = g_strdup(_("Deletion by external command")); |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
182 } |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
183 else |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
184 { |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
185 if (options->file_ops.safe_delete_enable) |
223
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
186 { |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
187 gchar *buf2; |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
188 if (options->file_ops.safe_delete_folder_maxsize > 0) |
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
189 buf2 = g_strdup_printf(_(" (max. %d MB)"), options->file_ops.safe_delete_folder_maxsize); |
223
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
190 else |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
191 buf2 = g_strdup(""); |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
192 |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
193 buf = g_strdup_printf(_("Safe delete: %s%s\nTrash: %s"), _("on"), buf2, options->file_ops.safe_delete_path); |
223
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
194 g_free(buf2); |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
195 } |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
196 else |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
197 { |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
198 buf = g_strdup_printf(_("Safe delete: %s"), _("off")); |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
199 } |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
200 } |
597 | 201 |
202 return buf; | |
9 | 203 } |