Mercurial > geeqie
annotate src/trash.c @ 777:088b71bf5715
Reduce indentation level.
author | zas_ |
---|---|
date | Tue, 03 Jun 2008 08:57:46 +0000 |
parents | 7148e125bf23 |
children | d6a7fb4b8e7c |
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 | |
766
7148e125bf23
Check for existing editor command using is_valid_editor_command().
zas_
parents:
730
diff
changeset
|
18 #include "editors.h" |
586 | 19 #include "filedata.h" |
9 | 20 #include "ui_fileops.h" |
21 #include "ui_misc.h" | |
22 | |
23 | |
24 /* | |
25 *-------------------------------------------------------------------------- | |
26 * Safe Delete | |
27 *-------------------------------------------------------------------------- | |
28 */ | |
29 | |
30 static gint file_util_safe_number(gint64 free_space) | |
31 { | |
32 gint n = 0; | |
33 gint64 total = 0; | |
34 GList *list; | |
35 GList *work; | |
36 gint sorted = FALSE; | |
37 gint warned = FALSE; | |
38 | |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
39 if (!filelist_read(options->file_ops.safe_delete_path, &list, NULL)) return 0; |
9 | 40 |
41 work = list; | |
42 while (work) | |
1 | 43 { |
9 | 44 FileData *fd; |
45 gint v; | |
442 | 46 |
9 | 47 fd = work->data; |
48 work = work->next; | |
49 | |
50 v = (gint)strtol(fd->name, NULL, 10); | |
51 if (v >= n) n = v + 1; | |
52 | |
53 total += fd->size; | |
1 | 54 } |
9 | 55 |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
56 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
|
57 (free_space < 0 || total + free_space > (gint64)options->file_ops.safe_delete_folder_maxsize * 1048576) ) |
9 | 58 { |
59 FileData *fd; | |
60 | |
61 if (!sorted) | |
62 { | |
63 list = filelist_sort(list, SORT_NAME, TRUE); | |
64 sorted = TRUE; | |
65 } | |
66 | |
67 fd = list->data; | |
68 list = g_list_remove(list, fd); | |
69 | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
497
diff
changeset
|
70 DEBUG_1("expunging from trash for space: %s", fd->name); |
9 | 71 if (!unlink_file(fd->path) && !warned) |
72 { | |
73 file_util_warning_dialog(_("Delete failed"), | |
74 _("Unable to remove old file from trash folder"), | |
75 GTK_STOCK_DIALOG_WARNING, NULL); | |
76 warned = TRUE; | |
77 } | |
78 total -= fd->size; | |
138 | 79 file_data_unref(fd); |
9 | 80 } |
81 | |
82 filelist_free(list); | |
83 | |
84 return n; | |
85 } | |
86 | |
87 void file_util_trash_clear(void) | |
88 { | |
89 file_util_safe_number(-1); | |
90 } | |
91 | |
92 static gchar *file_util_safe_dest(const gchar *path) | |
93 { | |
94 gint n; | |
727 | 95 gchar *name; |
96 gchar *dest; | |
9 | 97 |
98 n = file_util_safe_number(filesize(path)); | |
727 | 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); | |
101 g_free(name); | |
102 | |
103 return dest; | |
9 | 104 } |
105 | |
106 static void file_util_safe_del_toggle_cb(GtkWidget *button, gpointer data) | |
107 { | |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
108 options->file_ops.safe_delete_enable = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)); |
9 | 109 } |
110 | |
111 static void file_util_safe_del_close_cb(GtkWidget *dialog, gpointer data) | |
112 { | |
113 GenericDialog **gd = data; | |
114 | |
115 *gd = NULL; | |
1 | 116 } |
117 | |
600 | 118 gint file_util_safe_unlink(const gchar *path) |
1 | 119 { |
9 | 120 static GenericDialog *gd = NULL; |
121 gchar *result = NULL; | |
122 gint success = TRUE; | |
123 | |
597 | 124 if (!isfile(path)) return FALSE; |
9 | 125 |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
126 if (!isdir(options->file_ops.safe_delete_path)) |
9 | 127 { |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
497
diff
changeset
|
128 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
|
129 if (!options->file_ops.safe_delete_path || !mkdir_utf8(options->file_ops.safe_delete_path, 0755)) |
9 | 130 { |
131 result = _("Could not create folder"); | |
132 success = FALSE; | |
133 } | |
134 } | |
135 | |
136 if (success) | |
137 { | |
138 gchar *dest; | |
139 | |
597 | 140 dest = file_util_safe_dest(path); |
9 | 141 if (dest) |
142 { | |
597 | 143 DEBUG_1("safe deleting %s to %s", path, dest); |
144 success = move_file(path, dest); | |
9 | 145 } |
146 else | |
147 { | |
148 success = FALSE; | |
149 } | |
150 | |
597 | 151 if (!success && !access_file(path, W_OK)) |
9 | 152 { |
153 result = _("Permission denied"); | |
154 } | |
155 g_free(dest); | |
156 } | |
157 | |
158 if (result && !gd) | |
159 { | |
160 GtkWidget *button; | |
161 gchar *buf; | |
162 | |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
163 buf = g_strdup_printf(_("Unable to access or create the trash folder.\n\"%s\""), options->file_ops.safe_delete_path); |
9 | 164 gd = file_util_warning_dialog(result, buf, GTK_STOCK_DIALOG_WARNING, NULL); |
165 g_free(buf); | |
166 | |
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); | |
172 | |
173 g_signal_connect(G_OBJECT(gd->dialog), "destroy", | |
174 G_CALLBACK(file_util_safe_del_close_cb), &gd); | |
175 } | |
176 | |
177 return success; | |
1 | 178 } |
179 | |
600 | 180 gchar *file_util_safe_delete_status(void) |
1 | 181 { |
9 | 182 gchar *buf; |
183 | |
766
7148e125bf23
Check for existing editor command using is_valid_editor_command().
zas_
parents:
730
diff
changeset
|
184 if (is_valid_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
|
185 { |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
186 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
|
187 } |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
188 else |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
189 { |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
190 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
|
191 { |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
192 gchar *buf2; |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
193 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
|
194 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
|
195 else |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
196 buf2 = g_strdup(""); |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
197 |
341
15c6b94545a2
Move safe_delete* and in place rename options to file_ops
zas_
parents:
318
diff
changeset
|
198 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
|
199 g_free(buf2); |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
200 } |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
201 else |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
202 { |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
203 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
|
204 } |
73efc1ba150f
Setting no limit size to trash directory is now possible using zero as value.
zas_
parents:
196
diff
changeset
|
205 } |
597 | 206 |
207 return buf; | |
9 | 208 } |