Mercurial > geeqie
annotate src/filedata.c @ 1745:7a6f3ffa036a
fixed reloading of editors
author | nadvornik |
---|---|
date | Sat, 12 Sep 2009 15:31:24 +0000 |
parents | 77415a49cd99 |
children | 886594f72b04 |
rev | line source |
---|---|
586 | 1 /* |
2 * Geeqie | |
3 * (C) 2006 John Ellis | |
1284 | 4 * Copyright (C) 2008 - 2009 The Geeqie Team |
586 | 5 * |
6 * Author: John Ellis | |
7 * | |
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! | |
11 */ | |
12 | |
13 | |
14 #include "main.h" | |
15 #include "filedata.h" | |
16 | |
17 #include "filefilter.h" | |
18 #include "cache.h" | |
19 #include "thumb_standard.h" | |
20 #include "ui_fileops.h" | |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
21 #include "metadata.h" |
1212 | 22 #include "trash.h" |
1439 | 23 #include "histogram.h" |
586 | 24 |
25 | |
785 | 26 static GHashTable *file_data_pool = NULL; |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
27 static GHashTable *file_data_planned_change_hash = NULL; |
785 | 28 |
586 | 29 static gint sidecar_file_priority(const gchar *path); |
1728 | 30 static FileData *file_data_new_local(const gchar *path, struct stat *st, gboolean check_sidecars, GHashTable *basename_hash); |
586 | 31 |
32 | |
33 /* | |
34 *----------------------------------------------------------------------------- | |
35 * text conversion utils | |
36 *----------------------------------------------------------------------------- | |
37 */ | |
38 | |
39 gchar *text_from_size(gint64 size) | |
40 { | |
41 gchar *a, *b; | |
42 gchar *s, *d; | |
43 gint l, n, i; | |
44 | |
45 /* what I would like to use is printf("%'d", size) | |
46 * BUT: not supported on every libc :( | |
47 */ | |
48 if (size > G_MAXUINT) | |
49 { | |
50 /* the %lld conversion is not valid in all libcs, so use a simple work-around */ | |
51 a = g_strdup_printf("%d%09d", (guint)(size / 1000000000), (guint)(size % 1000000000)); | |
52 } | |
53 else | |
54 { | |
55 a = g_strdup_printf("%d", (guint)size); | |
56 } | |
57 l = strlen(a); | |
58 n = (l - 1)/ 3; | |
59 if (n < 1) return a; | |
60 | |
61 b = g_new(gchar, l + n + 1); | |
62 | |
63 s = a; | |
64 d = b; | |
65 i = l - n * 3; | |
66 while (*s != '\0') | |
67 { | |
68 if (i < 1) | |
69 { | |
70 i = 3; | |
71 *d = ','; | |
72 d++; | |
73 } | |
74 | |
75 *d = *s; | |
76 s++; | |
77 d++; | |
78 i--; | |
79 } | |
80 *d = '\0'; | |
81 | |
82 g_free(a); | |
83 return b; | |
84 } | |
85 | |
86 gchar *text_from_size_abrev(gint64 size) | |
87 { | |
88 if (size < (gint64)1024) | |
89 { | |
90 return g_strdup_printf(_("%d bytes"), (gint)size); | |
91 } | |
92 if (size < (gint64)1048576) | |
93 { | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
94 return g_strdup_printf(_("%.1f K"), (gdouble)size / 1024.0); |
586 | 95 } |
96 if (size < (gint64)1073741824) | |
97 { | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
98 return g_strdup_printf(_("%.1f MB"), (gdouble)size / 1048576.0); |
586 | 99 } |
100 | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
101 /* to avoid overflowing the gdouble, do division in two steps */ |
586 | 102 size /= 1048576; |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
103 return g_strdup_printf(_("%.1f GB"), (gdouble)size / 1024.0); |
586 | 104 } |
105 | |
106 /* note: returned string is valid until next call to text_from_time() */ | |
107 const gchar *text_from_time(time_t t) | |
108 { | |
109 static gchar *ret = NULL; | |
110 gchar buf[128]; | |
111 gint buflen; | |
112 struct tm *btime; | |
113 GError *error = NULL; | |
114 | |
115 btime = localtime(&t); | |
116 | |
117 /* the %x warning about 2 digit years is not an error */ | |
118 buflen = strftime(buf, sizeof(buf), "%x %H:%M", btime); | |
119 if (buflen < 1) return ""; | |
120 | |
121 g_free(ret); | |
122 ret = g_locale_to_utf8(buf, buflen, NULL, NULL, &error); | |
123 if (error) | |
124 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
125 log_printf("Error converting locale strftime to UTF-8: %s\n", error->message); |
586 | 126 g_error_free(error); |
127 return ""; | |
128 } | |
129 | |
130 return ret; | |
131 } | |
132 | |
133 /* | |
134 *----------------------------------------------------------------------------- | |
135 * file info struct | |
136 *----------------------------------------------------------------------------- | |
137 */ | |
138 | |
139 FileData *file_data_merge_sidecar_files(FileData *target, FileData *source); | |
1728 | 140 static void file_data_check_sidecars(FileData *fd, GHashTable *basename_hash); |
586 | 141 FileData *file_data_disconnect_sidecar_file(FileData *target, FileData *sfd); |
142 | |
143 | |
763
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
144 void file_data_increment_version(FileData *fd) |
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
145 { |
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
146 fd->version++; |
1227 | 147 fd->valid_marks = 0; |
148 if (fd->parent) | |
149 { | |
150 fd->parent->version++; | |
151 fd->parent->valid_marks = 0; | |
152 } | |
763
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
153 } |
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
154 |
1719 | 155 static gint file_data_sort_by_ext(gconstpointer a, gconstpointer b) |
156 { | |
157 const FileData *fda = a; | |
158 const FileData *fdb = b; | |
159 | |
160 return strcmp(fdb->extension, fda->extension); | |
161 } | |
162 | |
1728 | 163 static GHashTable *file_data_basename_hash_new(void) |
164 { | |
165 return g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); | |
166 } | |
167 | |
168 static void file_data_basename_hash_insert(GHashTable *basename_hash, FileData *fd) | |
1518 | 169 { |
170 GList *list; | |
171 const gchar *ext = extension_from_path(fd->path); | |
172 gchar *basename = ext ? g_strndup(fd->path, ext - fd->path) : g_strdup(fd->path); | |
1728 | 173 |
174 list = g_hash_table_lookup(basename_hash, basename); | |
1518 | 175 |
176 if (!g_list_find(list, fd)) | |
177 { | |
1728 | 178 list = g_list_insert_sorted(list, file_data_ref(fd), file_data_sort_by_ext); |
179 g_hash_table_insert(basename_hash, basename, list); | |
1518 | 180 } |
181 else | |
182 { | |
183 g_free(basename); | |
184 } | |
185 } | |
186 | |
1728 | 187 static void file_data_basename_hash_remove(GHashTable *basename_hash, FileData *fd) |
1518 | 188 { |
189 GList *list; | |
190 const gchar *ext = extension_from_path(fd->path); | |
191 gchar *basename = ext ? g_strndup(fd->path, ext - fd->path) : g_strdup(fd->path); | |
192 | |
1728 | 193 list = g_hash_table_lookup(basename_hash, basename); |
194 | |
195 if (!g_list_find(list, fd)) return; | |
1518 | 196 |
197 list = g_list_remove(list, fd); | |
1728 | 198 file_data_unref(fd); |
1518 | 199 |
200 if (list) | |
201 { | |
1728 | 202 g_hash_table_insert(basename_hash, basename, list); |
1518 | 203 } |
204 else | |
205 { | |
1728 | 206 g_hash_table_remove(basename_hash, basename); |
1518 | 207 g_free(basename); |
208 } | |
209 } | |
210 | |
1728 | 211 static void file_data_basename_hash_remove_list(gpointer key, gpointer value, gpointer data) |
212 { | |
213 filelist_free((GList *)value); | |
214 } | |
215 | |
216 static void file_data_basename_hash_free(GHashTable *basename_hash) | |
217 { | |
218 g_hash_table_foreach(basename_hash, file_data_basename_hash_remove_list, NULL); | |
219 g_hash_table_destroy(basename_hash); | |
220 } | |
221 | |
785 | 222 static void file_data_set_collate_keys(FileData *fd) |
223 { | |
224 gchar *caseless_name; | |
1007
4303ee1e88ec
Removed converting fd->name to utf8 from file_data_set_collate_keys(), because fd->name is utf8.
bruclik
parents:
1002
diff
changeset
|
225 |
4303ee1e88ec
Removed converting fd->name to utf8 from file_data_set_collate_keys(), because fd->name is utf8.
bruclik
parents:
1002
diff
changeset
|
226 caseless_name = g_utf8_casefold(fd->name, -1); |
785 | 227 |
228 g_free(fd->collate_key_name); | |
229 g_free(fd->collate_key_name_nocase); | |
230 | |
231 #if GLIB_CHECK_VERSION(2, 8, 0) | |
1007
4303ee1e88ec
Removed converting fd->name to utf8 from file_data_set_collate_keys(), because fd->name is utf8.
bruclik
parents:
1002
diff
changeset
|
232 fd->collate_key_name = g_utf8_collate_key_for_filename(fd->name, -1); |
785 | 233 fd->collate_key_name_nocase = g_utf8_collate_key_for_filename(caseless_name, -1); |
234 #else | |
1007
4303ee1e88ec
Removed converting fd->name to utf8 from file_data_set_collate_keys(), because fd->name is utf8.
bruclik
parents:
1002
diff
changeset
|
235 fd->collate_key_name = g_utf8_collate_key(fd->name, -1); |
785 | 236 fd->collate_key_name_nocase = g_utf8_collate_key(caseless_name, -1); |
237 #endif | |
238 g_free(caseless_name); | |
239 } | |
763
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
240 |
1728 | 241 static void file_data_set_path(FileData *fd, const gchar *path, GHashTable *basename_hash) |
586 | 242 { |
790 | 243 g_assert(path /* && *path*/); /* view_dir_tree uses FileData with zero length path */ |
785 | 244 g_assert(file_data_pool); |
245 | |
1728 | 246 if (basename_hash && fd->path) file_data_basename_hash_remove(basename_hash, fd); |
1518 | 247 |
785 | 248 g_free(fd->path); |
249 | |
250 if (fd->original_path) | |
251 { | |
252 g_hash_table_remove(file_data_pool, fd->original_path); | |
253 g_free(fd->original_path); | |
254 } | |
915 | 255 |
256 g_assert(!g_hash_table_lookup(file_data_pool, path)); | |
257 | |
785 | 258 fd->original_path = g_strdup(path); |
259 g_hash_table_insert(file_data_pool, fd->original_path, fd); | |
586 | 260 |
725 | 261 if (strcmp(path, G_DIR_SEPARATOR_S) == 0) |
586 | 262 { |
263 fd->path = g_strdup(path); | |
264 fd->name = fd->path; | |
265 fd->extension = fd->name + 1; | |
785 | 266 file_data_set_collate_keys(fd); |
586 | 267 return; |
268 } | |
269 | |
270 fd->path = g_strdup(path); | |
271 fd->name = filename_from_path(fd->path); | |
272 | |
273 if (strcmp(fd->name, "..") == 0) | |
274 { | |
275 gchar *dir = remove_level_from_path(path); | |
276 g_free(fd->path); | |
277 fd->path = remove_level_from_path(dir); | |
278 g_free(dir); | |
279 fd->name = ".."; | |
280 fd->extension = fd->name + 2; | |
785 | 281 file_data_set_collate_keys(fd); |
586 | 282 return; |
283 } | |
284 else if (strcmp(fd->name, ".") == 0) | |
285 { | |
286 g_free(fd->path); | |
287 fd->path = remove_level_from_path(path); | |
288 fd->name = "."; | |
289 fd->extension = fd->name + 1; | |
785 | 290 file_data_set_collate_keys(fd); |
586 | 291 return; |
292 } | |
293 | |
294 fd->extension = extension_from_path(fd->path); | |
295 if (fd->extension == NULL) | |
1422 | 296 { |
586 | 297 fd->extension = fd->name + strlen(fd->name); |
1422 | 298 } |
785 | 299 |
1728 | 300 if (basename_hash) file_data_basename_hash_insert(basename_hash, fd); /* we can ignore the special cases above - they don't have extensions */ |
1518 | 301 |
785 | 302 file_data_set_collate_keys(fd); |
586 | 303 } |
304 | |
801 | 305 static gboolean file_data_check_changed_files_recursive(FileData *fd, struct stat *st) |
586 | 306 { |
801 | 307 gboolean ret = FALSE; |
586 | 308 GList *work; |
806 | 309 |
586 | 310 if (fd->size != st->st_size || |
311 fd->date != st->st_mtime) | |
312 { | |
313 fd->size = st->st_size; | |
314 fd->date = st->st_mtime; | |
945
fd84847c8231
speed-up of directory notification on deleting large number of files
nadvornik
parents:
942
diff
changeset
|
315 fd->mode = st->st_mode; |
845 | 316 if (fd->thumb_pixbuf) g_object_unref(fd->thumb_pixbuf); |
317 fd->thumb_pixbuf = NULL; | |
763
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
318 file_data_increment_version(fd); |
1432 | 319 file_data_send_notification(fd, NOTIFY_REREAD); |
801 | 320 ret = TRUE; |
586 | 321 } |
322 | |
323 work = fd->sidecar_files; | |
324 while (work) | |
325 { | |
326 FileData *sfd = work->data; | |
327 struct stat st; | |
801 | 328 work = work->next; |
586 | 329 |
330 if (!stat_utf8(sfd->path, &st)) | |
331 { | |
801 | 332 fd->size = 0; |
333 fd->date = 0; | |
586 | 334 file_data_disconnect_sidecar_file(fd, sfd); |
801 | 335 ret = TRUE; |
336 continue; | |
586 | 337 } |
338 | |
801 | 339 ret |= file_data_check_changed_files_recursive(sfd, &st); |
586 | 340 } |
801 | 341 return ret; |
342 } | |
343 | |
344 | |
888 | 345 gboolean file_data_check_changed_files(FileData *fd) |
801 | 346 { |
347 gboolean ret = FALSE; | |
348 struct stat st; | |
806 | 349 |
801 | 350 if (fd->parent) fd = fd->parent; |
351 | |
352 if (!stat_utf8(fd->path, &st)) | |
353 { | |
806 | 354 GList *work; |
355 FileData *sfd = NULL; | |
356 | |
801 | 357 /* parent is missing, we have to rebuild whole group */ |
358 ret = TRUE; | |
359 fd->size = 0; | |
360 fd->date = 0; | |
806 | 361 |
362 work = fd->sidecar_files; | |
801 | 363 while (work) |
364 { | |
365 sfd = work->data; | |
366 work = work->next; | |
367 | |
368 file_data_disconnect_sidecar_file(fd, sfd); | |
369 } | |
1741 | 370 if (sfd) file_data_check_sidecars(sfd, NULL); /* this will group the sidecars back together */ |
1432 | 371 file_data_send_notification(fd, NOTIFY_REREAD); |
801 | 372 } |
806 | 373 else |
374 { | |
801 | 375 ret |= file_data_check_changed_files_recursive(fd, &st); |
806 | 376 } |
377 | |
801 | 378 return ret; |
586 | 379 } |
380 | |
1728 | 381 static FileData *file_data_new(const gchar *path_utf8, struct stat *st, gboolean check_sidecars, GHashTable *basename_hash) |
586 | 382 { |
383 FileData *fd; | |
384 | |
1728 | 385 DEBUG_2("file_data_new: '%s' %d %d", path_utf8, check_sidecars, !!basename_hash); |
586 | 386 |
387 if (!file_data_pool) | |
388 file_data_pool = g_hash_table_new(g_str_hash, g_str_equal); | |
389 | |
918 | 390 fd = g_hash_table_lookup(file_data_pool, path_utf8); |
391 if (fd) | |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
392 { |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
393 file_data_ref(fd); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
394 } |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
395 |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
396 if (!fd && file_data_planned_change_hash) |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
397 { |
918 | 398 fd = g_hash_table_lookup(file_data_planned_change_hash, path_utf8); |
399 if (fd) | |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
400 { |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
401 DEBUG_1("planned change: using %s -> %s", path_utf8, fd->path); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
402 file_data_ref(fd); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
403 file_data_apply_ci(fd); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
404 } |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
405 } |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
406 |
586 | 407 if (fd) |
408 { | |
801 | 409 gboolean changed; |
892
2022112583d0
increase reference count before sending notification in file_data_new
nadvornik
parents:
888
diff
changeset
|
410 |
801 | 411 if (fd->parent) |
412 changed = file_data_check_changed_files(fd); | |
413 else | |
414 changed = file_data_check_changed_files_recursive(fd, st); | |
415 if (changed && check_sidecars && sidecar_file_priority(fd->extension)) | |
1728 | 416 file_data_check_sidecars(fd, basename_hash); |
801 | 417 DEBUG_2("file_data_pool hit: '%s' %s", fd->path, changed ? "(changed)" : ""); |
806 | 418 |
892
2022112583d0
increase reference count before sending notification in file_data_new
nadvornik
parents:
888
diff
changeset
|
419 return fd; |
586 | 420 } |
421 | |
422 fd = g_new0(FileData, 1); | |
785 | 423 |
586 | 424 fd->size = st->st_size; |
425 fd->date = st->st_mtime; | |
945
fd84847c8231
speed-up of directory notification on deleting large number of files
nadvornik
parents:
942
diff
changeset
|
426 fd->mode = st->st_mode; |
586 | 427 fd->ref = 1; |
428 fd->magick = 0x12345678; | |
429 | |
1728 | 430 file_data_set_path(fd, path_utf8, basename_hash); /* set path, name, collate_key_*, original_path */ |
586 | 431 |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
432 if (check_sidecars) |
1728 | 433 file_data_check_sidecars(fd, basename_hash); |
785 | 434 |
586 | 435 return fd; |
436 } | |
437 | |
1719 | 438 /* extension must contain only ASCII characters */ |
439 static GList *check_case_insensitive_ext(gchar *path) | |
440 { | |
441 gchar *sl; | |
442 gchar *extl; | |
443 gint ext_len; | |
444 GList *list = NULL; | |
445 | |
446 sl = path_from_utf8(path); | |
1741 | 447 |
1719 | 448 extl = strrchr(sl, '.'); |
449 if (extl) | |
450 { | |
451 gint i, j; | |
452 extl++; /* the first char after . */ | |
453 ext_len = strlen(extl); | |
454 | |
455 for (i = 0; i < (1 << ext_len); i++) | |
456 { | |
457 struct stat st; | |
1741 | 458 gboolean skip = FALSE; |
1719 | 459 for (j = 0; j < ext_len; j++) |
460 { | |
461 if (i & (1 << (ext_len - 1 - j))) | |
1741 | 462 { |
1719 | 463 extl[j] = g_ascii_tolower(extl[j]); |
1741 | 464 /* make sure the result does not contain duplicates */ |
465 if (extl[j] == g_ascii_toupper(extl[j])) | |
466 { | |
467 /* no change, probably a number, we have already tested this combination */ | |
468 skip = TRUE; | |
469 break; | |
470 } | |
471 } | |
1719 | 472 else |
473 extl[j] = g_ascii_toupper(extl[j]); | |
474 } | |
1741 | 475 if (skip) continue; |
476 | |
1719 | 477 if (stat(sl, &st) == 0) |
478 { | |
479 list = g_list_prepend(list, file_data_new_local(sl, &st, FALSE, FALSE)); | |
480 } | |
481 } | |
482 } | |
483 g_free(sl); | |
484 | |
485 return list; | |
486 } | |
487 | |
1728 | 488 static void file_data_check_sidecars(FileData *fd, GHashTable *basename_hash) |
586 | 489 { |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
490 gint base_len; |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
491 GString *fname; |
586 | 492 FileData *parent_fd = NULL; |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
493 GList *work; |
1719 | 494 const GList *basename_list = NULL; |
495 GList *group_list = NULL; | |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
496 if (fd->disable_grouping || !sidecar_file_priority(fd->extension)) |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
497 return; |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
498 |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
499 base_len = fd->extension - fd->path; |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
500 fname = g_string_new_len(fd->path, base_len); |
1518 | 501 |
1728 | 502 if (basename_hash) |
1518 | 503 { |
1728 | 504 basename_list = g_hash_table_lookup(basename_hash, fname->str); |
1518 | 505 } |
506 | |
1719 | 507 |
508 /* check for possible sidecar files; | |
509 the sidecar files created here are referenced only via fd->sidecar_files or fd->parent, | |
510 they have fd->ref set to 0 and file_data unref must chack and free them all together | |
511 (using fd->ref would cause loops and leaks) | |
512 */ | |
513 | |
514 /* find all possible sidecar files and order them according to sidecar_ext_get_list, | |
515 for case-only differences put lowercase first, | |
516 put the result to group_list | |
517 */ | |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
518 work = sidecar_ext_get_list(); |
586 | 519 while (work) |
520 { | |
806 | 521 gchar *ext = work->data; |
586 | 522 work = work->next; |
523 | |
1728 | 524 if (!basename_hash) |
586 | 525 { |
1719 | 526 GList *new_list; |
527 g_string_truncate(fname, base_len); | |
528 g_string_append(fname, ext); | |
529 new_list = check_case_insensitive_ext(fname->str); | |
530 group_list = g_list_concat(group_list, new_list); | |
586 | 531 } |
532 else | |
533 { | |
1719 | 534 const GList *work2 = basename_list; |
535 | |
536 while (work2) | |
1518 | 537 { |
1719 | 538 FileData *sfd = work2->data; |
1518 | 539 |
1728 | 540 if (g_ascii_strcasecmp(ext, sfd->extension) == 0) |
1518 | 541 { |
1719 | 542 group_list = g_list_append(group_list, file_data_ref(sfd)); |
1518 | 543 } |
1719 | 544 work2 = work2->next; |
1518 | 545 } |
586 | 546 } |
1719 | 547 } |
548 g_string_free(fname, TRUE); | |
549 | |
550 /* process the group list - the first one is the parent file, others are sidecars */ | |
551 work = group_list; | |
552 while (work) | |
553 { | |
554 FileData *new_fd = work->data; | |
555 work = work->next; | |
556 | |
557 if (new_fd->disable_grouping) | |
558 { | |
559 file_data_unref(new_fd); | |
560 continue; | |
561 } | |
562 | |
563 new_fd->ref--; /* do not use ref here */ | |
586 | 564 |
565 if (!parent_fd) | |
566 parent_fd = new_fd; /* parent is the one with the highest prio, found first */ | |
567 else | |
568 file_data_merge_sidecar_files(parent_fd, new_fd); | |
569 } | |
1719 | 570 g_list_free(group_list); |
586 | 571 } |
572 | |
573 | |
1728 | 574 static FileData *file_data_new_local(const gchar *path, struct stat *st, gboolean check_sidecars, GHashTable *basename_hash) |
586 | 575 { |
576 gchar *path_utf8 = path_to_utf8(path); | |
1728 | 577 FileData *ret = file_data_new(path_utf8, st, check_sidecars, basename_hash); |
806 | 578 |
586 | 579 g_free(path_utf8); |
580 return ret; | |
581 } | |
582 | |
583 FileData *file_data_new_simple(const gchar *path_utf8) | |
584 { | |
585 struct stat st; | |
586 | |
587 if (!stat_utf8(path_utf8, &st)) | |
588 { | |
589 st.st_size = 0; | |
590 st.st_mtime = 0; | |
591 } | |
592 | |
1728 | 593 return file_data_new(path_utf8, &st, TRUE, NULL); |
586 | 594 } |
595 | |
596 FileData *file_data_add_sidecar_file(FileData *target, FileData *sfd) | |
597 { | |
598 sfd->parent = target; | |
855 | 599 if (!g_list_find(target->sidecar_files, sfd)) |
586 | 600 target->sidecar_files = g_list_prepend(target->sidecar_files, sfd); |
801 | 601 file_data_increment_version(sfd); /* increments both sfd and target */ |
586 | 602 return target; |
603 } | |
604 | |
605 | |
606 FileData *file_data_merge_sidecar_files(FileData *target, FileData *source) | |
607 { | |
608 GList *work; | |
806 | 609 |
586 | 610 file_data_add_sidecar_file(target, source); |
611 | |
612 work = source->sidecar_files; | |
613 while (work) | |
614 { | |
615 FileData *sfd = work->data; | |
616 file_data_add_sidecar_file(target, sfd); | |
617 work = work->next; | |
618 } | |
619 | |
620 g_list_free(source->sidecar_files); | |
621 source->sidecar_files = NULL; | |
622 | |
623 target->sidecar_files = filelist_sort(target->sidecar_files, SORT_NAME, TRUE); | |
806 | 624 |
586 | 625 return target; |
626 } | |
627 | |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
628 #ifdef DEBUG_FILEDATA |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
629 FileData *file_data_ref_debug(const gchar *file, gint line, FileData *fd) |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
630 #else |
586 | 631 FileData *file_data_ref(FileData *fd) |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
632 #endif |
586 | 633 { |
634 if (fd == NULL) return NULL; | |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
635 #ifdef DEBUG_FILEDATA |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
636 if (fd->magick != 0x12345678) |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
637 DEBUG_0("fd magick mismatch at %s:%d", file, line); |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
638 #endif |
586 | 639 g_assert(fd->magick == 0x12345678); |
640 fd->ref++; | |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
641 |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
642 #ifdef DEBUG_FILEDATA |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
643 DEBUG_2("file_data_ref (%d): '%s' @ %s:%d", fd->ref, fd->path, file, line); |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
644 #else |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
645 DEBUG_2("file_data_ref (%d): '%s'", fd->ref, fd->path); |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
646 #endif |
586 | 647 return fd; |
648 } | |
649 | |
650 static void file_data_free(FileData *fd) | |
651 { | |
652 g_assert(fd->magick == 0x12345678); | |
653 g_assert(fd->ref == 0); | |
654 | |
655 g_hash_table_remove(file_data_pool, fd->original_path); | |
656 | |
657 g_free(fd->path); | |
658 g_free(fd->original_path); | |
785 | 659 g_free(fd->collate_key_name); |
660 g_free(fd->collate_key_name_nocase); | |
845 | 661 if (fd->thumb_pixbuf) g_object_unref(fd->thumb_pixbuf); |
1439 | 662 histmap_free(fd->histmap); |
1294 | 663 |
586 | 664 g_assert(fd->sidecar_files == NULL); /* sidecar files must be freed before calling this */ |
665 | |
666 file_data_change_info_free(NULL, fd); | |
667 g_free(fd); | |
668 } | |
669 | |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
670 #ifdef DEBUG_FILEDATA |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
671 void file_data_unref_debug(const gchar *file, gint line, FileData *fd) |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
672 #else |
586 | 673 void file_data_unref(FileData *fd) |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
674 #endif |
586 | 675 { |
676 if (fd == NULL) return; | |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
677 #ifdef DEBUG_FILEDATA |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
678 if (fd->magick != 0x12345678) |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
679 DEBUG_0("fd magick mismatch @ %s:%d", file, line); |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
680 #endif |
586 | 681 g_assert(fd->magick == 0x12345678); |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
682 |
586 | 683 fd->ref--; |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
684 #ifdef DEBUG_FILEDATA |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
685 DEBUG_2("file_data_unref (%d): '%s' @ %s:%d", fd->ref, fd->path, file, line); |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
686 #else |
586 | 687 DEBUG_2("file_data_unref (%d): '%s'", fd->ref, fd->path); |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
688 #endif |
586 | 689 if (fd->ref == 0) |
690 { | |
806 | 691 GList *work; |
586 | 692 FileData *parent = fd->parent ? fd->parent : fd; |
806 | 693 |
1422 | 694 if (parent->ref > 0) return; |
586 | 695 |
696 work = parent->sidecar_files; | |
697 while (work) | |
698 { | |
699 FileData *sfd = work->data; | |
1422 | 700 if (sfd->ref > 0) return; |
586 | 701 work = work->next; |
702 } | |
703 | |
704 /* none of parent/children is referenced, we can free everything */ | |
705 | |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
706 DEBUG_2("file_data_unref: deleting '%s', parent '%s'", fd->path, fd->parent ? parent->path : "-"); |
586 | 707 |
708 work = parent->sidecar_files; | |
709 while (work) | |
710 { | |
711 FileData *sfd = work->data; | |
712 file_data_free(sfd); | |
713 work = work->next; | |
714 } | |
715 | |
716 g_list_free(parent->sidecar_files); | |
717 parent->sidecar_files = NULL; | |
718 | |
719 file_data_free(parent); | |
720 } | |
721 } | |
722 | |
723 FileData *file_data_disconnect_sidecar_file(FileData *target, FileData *sfd) | |
724 { | |
725 sfd->parent = target; | |
726 g_assert(g_list_find(target->sidecar_files, sfd)); | |
801 | 727 |
728 file_data_increment_version(sfd); /* increments both sfd and target */ | |
586 | 729 |
730 target->sidecar_files = g_list_remove(target->sidecar_files, sfd); | |
731 sfd->parent = NULL; | |
732 | |
806 | 733 if (sfd->ref == 0) |
734 { | |
586 | 735 file_data_free(sfd); |
736 return NULL; | |
806 | 737 } |
586 | 738 |
739 return sfd; | |
740 } | |
741 | |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
742 /* disables / enables grouping for particular file, sends UPDATE notification */ |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
743 void file_data_disable_grouping(FileData *fd, gboolean disable) |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
744 { |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
745 if (!fd->disable_grouping == !disable) return; |
1598 | 746 |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
747 fd->disable_grouping = !!disable; |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
748 |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
749 if (disable) |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
750 { |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
751 if (fd->parent) |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
752 { |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
753 FileData *parent = file_data_ref(fd->parent); |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
754 file_data_disconnect_sidecar_file(parent, fd); |
1432 | 755 file_data_send_notification(parent, NOTIFY_GROUPING); |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
756 file_data_unref(parent); |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
757 } |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
758 else if (fd->sidecar_files) |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
759 { |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
760 GList *sidecar_files = filelist_copy(fd->sidecar_files); |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
761 GList *work = sidecar_files; |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
762 while (work) |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
763 { |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
764 FileData *sfd = work->data; |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
765 work = work->next; |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
766 file_data_disconnect_sidecar_file(fd, sfd); |
1432 | 767 file_data_send_notification(sfd, NOTIFY_GROUPING); |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
768 } |
1518 | 769 file_data_check_sidecars((FileData *)sidecar_files->data, FALSE); /* this will group the sidecars back together */ |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
770 filelist_free(sidecar_files); |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
771 } |
1598 | 772 else |
773 { | |
774 file_data_increment_version(fd); /* the functions called in the cases above increments the version too */ | |
775 } | |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
776 } |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
777 else |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
778 { |
1598 | 779 file_data_increment_version(fd); |
1518 | 780 file_data_check_sidecars(fd, FALSE); |
1598 | 781 } |
782 file_data_send_notification(fd, NOTIFY_GROUPING); | |
783 } | |
784 | |
785 void file_data_disable_grouping_list(GList *fd_list, gboolean disable) | |
786 { | |
787 GList *work; | |
788 | |
789 work = fd_list; | |
790 while (work) | |
791 { | |
792 FileData *fd = work->data; | |
793 | |
794 file_data_disable_grouping(fd, disable); | |
795 work = work->next; | |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
796 } |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
797 } |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
798 |
1598 | 799 |
586 | 800 /* compare name without extension */ |
801 gint file_data_compare_name_without_ext(FileData *fd1, FileData *fd2) | |
802 { | |
803 size_t len1 = fd1->extension - fd1->name; | |
804 size_t len2 = fd2->extension - fd2->name; | |
805 | |
806 if (len1 < len2) return -1; | |
807 if (len1 > len2) return 1; | |
808 | |
785 | 809 return strncmp(fd1->name, fd2->name, len1); /* FIXME: utf8 */ |
586 | 810 } |
811 | |
812 void file_data_change_info_free(FileDataChangeInfo *fdci, FileData *fd) | |
813 { | |
1422 | 814 if (!fdci && fd) fdci = fd->change; |
586 | 815 |
1422 | 816 if (!fdci) return; |
586 | 817 |
818 g_free(fdci->source); | |
819 g_free(fdci->dest); | |
820 | |
821 g_free(fdci); | |
822 | |
1422 | 823 if (fd) fd->change = NULL; |
586 | 824 } |
825 | |
1224 | 826 static gboolean file_data_can_write_directly(FileData *fd) |
827 { | |
1239
254b09942e68
metadata write mode (direct or sidecar) made configurable for each file
nadvornik
parents:
1229
diff
changeset
|
828 return filter_name_is_writable(fd->extension); |
1224 | 829 } |
586 | 830 |
1224 | 831 static gboolean file_data_can_write_sidecar(FileData *fd) |
832 { | |
1239
254b09942e68
metadata write mode (direct or sidecar) made configurable for each file
nadvornik
parents:
1229
diff
changeset
|
833 return filter_name_allow_sidecar(fd->extension) && !filter_name_is_writable(fd->extension); |
1224 | 834 } |
835 | |
836 gchar *file_data_get_sidecar_path(FileData *fd, gboolean existing_only) | |
837 { | |
838 gchar *sidecar_path = NULL; | |
839 GList *work; | |
1422 | 840 |
1224 | 841 if (!file_data_can_write_sidecar(fd)) return NULL; |
842 | |
843 work = fd->parent ? fd->parent->sidecar_files : fd->sidecar_files; | |
844 while (work) | |
845 { | |
846 FileData *sfd = work->data; | |
847 work = work->next; | |
1307 | 848 if (g_ascii_strcasecmp(sfd->extension, ".xmp") == 0) |
1224 | 849 { |
850 sidecar_path = g_strdup(sfd->path); | |
851 break; | |
852 } | |
853 } | |
854 | |
855 if (!existing_only && !sidecar_path) | |
856 { | |
857 gchar *base = remove_extension_from_path(fd->path); | |
858 sidecar_path = g_strconcat(base, ".xmp", NULL); | |
859 g_free(base); | |
860 } | |
861 | |
862 return sidecar_path; | |
863 } | |
586 | 864 |
865 | |
866 /* | |
867 *----------------------------------------------------------------------------- | |
868 * sidecar file info struct | |
869 *----------------------------------------------------------------------------- | |
870 */ | |
871 | |
872 | |
873 | |
874 static gint sidecar_file_priority(const gchar *path) | |
875 { | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
876 const gchar *extension = extension_from_path(path); |
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
877 gint i = 1; |
586 | 878 GList *work; |
806 | 879 |
586 | 880 if (extension == NULL) |
881 return 0; | |
882 | |
883 work = sidecar_ext_get_list(); | |
884 | |
885 while (work) { | |
886 gchar *ext = work->data; | |
806 | 887 |
586 | 888 work = work->next; |
1307 | 889 if (g_ascii_strcasecmp(extension, ext) == 0) return i; |
586 | 890 i++; |
891 } | |
892 return 0; | |
893 } | |
894 | |
895 | |
896 /* | |
897 *----------------------------------------------------------------------------- | |
898 * load file list | |
899 *----------------------------------------------------------------------------- | |
900 */ | |
901 | |
902 static SortType filelist_sort_method = SORT_NONE; | |
1422 | 903 static gboolean filelist_sort_ascend = TRUE; |
586 | 904 |
905 | |
906 gint filelist_sort_compare_filedata(FileData *fa, FileData *fb) | |
907 { | |
908 if (!filelist_sort_ascend) | |
909 { | |
910 FileData *tmp = fa; | |
911 fa = fb; | |
912 fb = tmp; | |
913 } | |
914 | |
915 switch (filelist_sort_method) | |
916 { | |
785 | 917 case SORT_NAME: |
918 break; | |
586 | 919 case SORT_SIZE: |
920 if (fa->size < fb->size) return -1; | |
921 if (fa->size > fb->size) return 1; | |
785 | 922 /* fall back to name */ |
586 | 923 break; |
924 case SORT_TIME: | |
925 if (fa->date < fb->date) return -1; | |
926 if (fa->date > fb->date) return 1; | |
785 | 927 /* fall back to name */ |
586 | 928 break; |
929 #ifdef HAVE_STRVERSCMP | |
930 case SORT_NUMBER: | |
931 return strverscmp(fa->name, fb->name); | |
932 break; | |
933 #endif | |
934 default: | |
935 break; | |
936 } | |
785 | 937 |
938 if (options->file_sort.case_sensitive) | |
939 return strcmp(fa->collate_key_name, fb->collate_key_name); | |
940 else | |
941 return strcmp(fa->collate_key_name_nocase, fb->collate_key_name_nocase); | |
586 | 942 } |
943 | |
1422 | 944 gint filelist_sort_compare_filedata_full(FileData *fa, FileData *fb, SortType method, gboolean ascend) |
586 | 945 { |
946 filelist_sort_method = method; | |
947 filelist_sort_ascend = ascend; | |
948 return filelist_sort_compare_filedata(fa, fb); | |
949 } | |
950 | |
1002 | 951 static gint filelist_sort_file_cb(gpointer a, gpointer b) |
586 | 952 { |
953 return filelist_sort_compare_filedata(a, b); | |
954 } | |
955 | |
1422 | 956 GList *filelist_sort_full(GList *list, SortType method, gboolean ascend, GCompareFunc cb) |
586 | 957 { |
958 filelist_sort_method = method; | |
959 filelist_sort_ascend = ascend; | |
960 return g_list_sort(list, cb); | |
961 } | |
962 | |
1422 | 963 GList *filelist_insert_sort_full(GList *list, gpointer data, SortType method, gboolean ascend, GCompareFunc cb) |
586 | 964 { |
965 filelist_sort_method = method; | |
966 filelist_sort_ascend = ascend; | |
967 return g_list_insert_sorted(list, data, cb); | |
968 } | |
969 | |
1422 | 970 GList *filelist_sort(GList *list, SortType method, gboolean ascend) |
586 | 971 { |
972 return filelist_sort_full(list, method, ascend, (GCompareFunc) filelist_sort_file_cb); | |
973 } | |
974 | |
1422 | 975 GList *filelist_insert_sort(GList *list, FileData *fd, SortType method, gboolean ascend) |
586 | 976 { |
977 return filelist_insert_sort_full(list, fd, method, ascend, (GCompareFunc) filelist_sort_file_cb); | |
978 } | |
979 | |
980 | |
981 static GList *filelist_filter_out_sidecars(GList *flist) | |
982 { | |
983 GList *work = flist; | |
984 GList *flist_filtered = NULL; | |
985 | |
986 while (work) | |
987 { | |
988 FileData *fd = work->data; | |
806 | 989 |
586 | 990 work = work->next; |
991 if (fd->parent) /* remove fd's that are children */ | |
992 file_data_unref(fd); | |
993 else | |
994 flist_filtered = g_list_prepend(flist_filtered, fd); | |
995 } | |
996 g_list_free(flist); | |
806 | 997 |
586 | 998 return flist_filtered; |
999 } | |
1000 | |
1423
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
1001 static gboolean is_hidden_file(const gchar *name) |
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
1002 { |
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
1003 if (name[0] != '.') return FALSE; |
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
1004 if (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) return FALSE; |
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
1005 return TRUE; |
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
1006 } |
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
1007 |
1422 | 1008 static gboolean filelist_read_real(FileData *dir_fd, GList **files, GList **dirs, gboolean follow_symlinks) |
586 | 1009 { |
1010 DIR *dp; | |
1011 struct dirent *dir; | |
1012 gchar *pathl; | |
779 | 1013 GList *dlist = NULL; |
1014 GList *flist = NULL; | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
1015 gint (*stat_func)(const gchar *path, struct stat *buf); |
1728 | 1016 GHashTable *basename_hash = NULL; |
586 | 1017 |
779 | 1018 g_assert(files || dirs); |
1019 | |
1020 if (files) *files = NULL; | |
1021 if (dirs) *dirs = NULL; | |
586 | 1022 |
783 | 1023 pathl = path_from_utf8(dir_fd->path); |
779 | 1024 if (!pathl) return FALSE; |
1025 | |
1026 dp = opendir(pathl); | |
1027 if (dp == NULL) | |
586 | 1028 { |
1029 g_free(pathl); | |
1030 return FALSE; | |
1031 } | |
1032 | |
1728 | 1033 if (files) basename_hash = file_data_basename_hash_new(); |
1034 | |
779 | 1035 if (follow_symlinks) |
1036 stat_func = stat; | |
1037 else | |
1038 stat_func = lstat; | |
1039 | |
586 | 1040 while ((dir = readdir(dp)) != NULL) |
1041 { | |
779 | 1042 struct stat ent_sbuf; |
1043 const gchar *name = dir->d_name; | |
1044 gchar *filepath; | |
1045 | |
1423
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
1046 if (!options->file_filter.show_hidden_files && is_hidden_file(name)) |
779 | 1047 continue; |
1048 | |
1049 filepath = g_build_filename(pathl, name, NULL); | |
1050 if (stat_func(filepath, &ent_sbuf) >= 0) | |
586 | 1051 { |
779 | 1052 if (S_ISDIR(ent_sbuf.st_mode)) |
586 | 1053 { |
779 | 1054 /* we ignore the .thumbnails dir for cleanliness */ |
1055 if (dirs && | |
1056 !(name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) && | |
1057 strcmp(name, GQ_CACHE_LOCAL_THUMB) != 0 && | |
1058 strcmp(name, GQ_CACHE_LOCAL_METADATA) != 0 && | |
1059 strcmp(name, THUMB_FOLDER_LOCAL) != 0) | |
586 | 1060 { |
1728 | 1061 dlist = g_list_prepend(dlist, file_data_new_local(filepath, &ent_sbuf, FALSE, NULL)); |
586 | 1062 } |
1063 } | |
779 | 1064 else |
1065 { | |
1066 if (files && filter_name_exists(name)) | |
1067 { | |
1728 | 1068 flist = g_list_prepend(flist, file_data_new_local(filepath, &ent_sbuf, TRUE, basename_hash)); |
779 | 1069 } |
1070 } | |
586 | 1071 } |
779 | 1072 g_free(filepath); |
586 | 1073 } |
1074 | |
1075 closedir(dp); | |
779 | 1076 |
586 | 1077 g_free(pathl); |
1728 | 1078 if (basename_hash) file_data_basename_hash_free(basename_hash); |
586 | 1079 |
1080 if (dirs) *dirs = dlist; | |
779 | 1081 if (files) *files = filelist_filter_out_sidecars(flist); |
586 | 1082 |
1083 return TRUE; | |
1084 } | |
1085 | |
1422 | 1086 gboolean filelist_read(FileData *dir_fd, GList **files, GList **dirs) |
586 | 1087 { |
783 | 1088 return filelist_read_real(dir_fd, files, dirs, TRUE); |
586 | 1089 } |
1090 | |
1422 | 1091 gboolean filelist_read_lstat(FileData *dir_fd, GList **files, GList **dirs) |
586 | 1092 { |
783 | 1093 return filelist_read_real(dir_fd, files, dirs, FALSE); |
586 | 1094 } |
1095 | |
1096 void filelist_free(GList *list) | |
1097 { | |
1098 GList *work; | |
1099 | |
1100 work = list; | |
1101 while (work) | |
1102 { | |
1103 file_data_unref((FileData *)work->data); | |
1104 work = work->next; | |
1105 } | |
1106 | |
1107 g_list_free(list); | |
1108 } | |
1109 | |
1110 | |
1111 GList *filelist_copy(GList *list) | |
1112 { | |
1113 GList *new_list = NULL; | |
1114 GList *work; | |
1115 | |
1116 work = list; | |
1117 while (work) | |
1118 { | |
1119 FileData *fd; | |
1120 | |
1121 fd = work->data; | |
1122 work = work->next; | |
1123 | |
1124 new_list = g_list_prepend(new_list, file_data_ref(fd)); | |
1125 } | |
1126 | |
1127 return g_list_reverse(new_list); | |
1128 } | |
1129 | |
1130 GList *filelist_from_path_list(GList *list) | |
1131 { | |
1132 GList *new_list = NULL; | |
1133 GList *work; | |
1134 | |
1135 work = list; | |
1136 while (work) | |
1137 { | |
1138 gchar *path; | |
1139 | |
1140 path = work->data; | |
1141 work = work->next; | |
1142 | |
1143 new_list = g_list_prepend(new_list, file_data_new_simple(path)); | |
1144 } | |
1145 | |
1146 return g_list_reverse(new_list); | |
1147 } | |
1148 | |
1149 GList *filelist_to_path_list(GList *list) | |
1150 { | |
1151 GList *new_list = NULL; | |
1152 GList *work; | |
1153 | |
1154 work = list; | |
1155 while (work) | |
1156 { | |
1157 FileData *fd; | |
1158 | |
1159 fd = work->data; | |
1160 work = work->next; | |
1161 | |
1162 new_list = g_list_prepend(new_list, g_strdup(fd->path)); | |
1163 } | |
1164 | |
1165 return g_list_reverse(new_list); | |
1166 } | |
1167 | |
1422 | 1168 GList *filelist_filter(GList *list, gboolean is_dir_list) |
586 | 1169 { |
1170 GList *work; | |
1171 | |
1172 if (!is_dir_list && options->file_filter.disable && options->file_filter.show_hidden_files) return list; | |
1173 | |
1174 work = list; | |
1175 while (work) | |
1176 { | |
1177 FileData *fd = (FileData *)(work->data); | |
1178 const gchar *name = fd->name; | |
1179 | |
1423
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
1180 if ((!options->file_filter.show_hidden_files && is_hidden_file(name)) || |
586 | 1181 (!is_dir_list && !filter_name_exists(name)) || |
1182 (is_dir_list && name[0] == '.' && (strcmp(name, GQ_CACHE_LOCAL_THUMB) == 0 || | |
1183 strcmp(name, GQ_CACHE_LOCAL_METADATA) == 0)) ) | |
1184 { | |
1185 GList *link = work; | |
806 | 1186 |
586 | 1187 list = g_list_remove_link(list, link); |
1188 file_data_unref(fd); | |
1189 g_list_free(link); | |
1190 } | |
806 | 1191 |
1192 work = work->next; | |
586 | 1193 } |
1194 | |
1195 return list; | |
1196 } | |
1197 | |
1198 /* | |
1199 *----------------------------------------------------------------------------- | |
1200 * filelist recursive | |
1201 *----------------------------------------------------------------------------- | |
1202 */ | |
1203 | |
1204 static gint filelist_sort_path_cb(gconstpointer a, gconstpointer b) | |
1205 { | |
1206 return CASE_SORT(((FileData *)a)->path, ((FileData *)b)->path); | |
1207 } | |
1208 | |
1209 GList *filelist_sort_path(GList *list) | |
1210 { | |
1211 return g_list_sort(list, filelist_sort_path_cb); | |
1212 } | |
1213 | |
1214 static void filelist_recursive_append(GList **list, GList *dirs) | |
1215 { | |
1216 GList *work; | |
1217 | |
1218 work = dirs; | |
1219 while (work) | |
1220 { | |
1221 FileData *fd = (FileData *)(work->data); | |
780
44128da39e13
Drop initialization to NULL since filelist_read() will take care of it.
zas_
parents:
779
diff
changeset
|
1222 GList *f; |
44128da39e13
Drop initialization to NULL since filelist_read() will take care of it.
zas_
parents:
779
diff
changeset
|
1223 GList *d; |
586 | 1224 |
783 | 1225 if (filelist_read(fd, &f, &d)) |
586 | 1226 { |
1227 f = filelist_filter(f, FALSE); | |
1228 f = filelist_sort_path(f); | |
1229 *list = g_list_concat(*list, f); | |
1230 | |
1231 d = filelist_filter(d, TRUE); | |
1232 d = filelist_sort_path(d); | |
1233 filelist_recursive_append(list, d); | |
1234 filelist_free(d); | |
1235 } | |
1236 | |
1237 work = work->next; | |
1238 } | |
1239 } | |
1240 | |
783 | 1241 GList *filelist_recursive(FileData *dir_fd) |
586 | 1242 { |
780
44128da39e13
Drop initialization to NULL since filelist_read() will take care of it.
zas_
parents:
779
diff
changeset
|
1243 GList *list; |
44128da39e13
Drop initialization to NULL since filelist_read() will take care of it.
zas_
parents:
779
diff
changeset
|
1244 GList *d; |
586 | 1245 |
783 | 1246 if (!filelist_read(dir_fd, &list, &d)) return NULL; |
586 | 1247 list = filelist_filter(list, FALSE); |
1248 list = filelist_sort_path(list); | |
1249 | |
1250 d = filelist_filter(d, TRUE); | |
1251 d = filelist_sort_path(d); | |
1252 filelist_recursive_append(&list, d); | |
1253 filelist_free(d); | |
1254 | |
1255 return list; | |
1256 } | |
590 | 1257 |
1258 | |
800 | 1259 /* |
1260 * marks and orientation | |
1261 */ | |
995 | 1262 |
1222 | 1263 static FileDataGetMarkFunc file_data_get_mark_func[FILEDATA_MARKS_SIZE]; |
1264 static FileDataSetMarkFunc file_data_set_mark_func[FILEDATA_MARKS_SIZE]; | |
1265 static gpointer file_data_mark_func_data[FILEDATA_MARKS_SIZE]; | |
1425 | 1266 static GDestroyNotify file_data_destroy_mark_func[FILEDATA_MARKS_SIZE]; |
995 | 1267 |
800 | 1268 gboolean file_data_get_mark(FileData *fd, gint n) |
1269 { | |
1227 | 1270 gboolean valid = (fd->valid_marks & (1 << n)); |
1422 | 1271 |
1227 | 1272 if (file_data_get_mark_func[n] && !valid) |
1222 | 1273 { |
1227 | 1274 guint old = fd->marks; |
1222 | 1275 gboolean value = (file_data_get_mark_func[n])(fd, n, file_data_mark_func_data[n]); |
1422 | 1276 |
1227 | 1277 if (!value != !(fd->marks & (1 << n))) |
1278 { | |
1279 fd->marks = fd->marks ^ (1 << n); | |
1280 } | |
1422 | 1281 |
1227 | 1282 fd->valid_marks |= (1 << n); |
1283 if (old && !fd->marks) /* keep files with non-zero marks in memory */ | |
1284 { | |
1285 file_data_unref(fd); | |
1286 } | |
1287 else if (!old && fd->marks) | |
1288 { | |
1289 file_data_ref(fd); | |
1290 } | |
1222 | 1291 } |
1292 | |
800 | 1293 return !!(fd->marks & (1 << n)); |
1294 } | |
1295 | |
966 | 1296 guint file_data_get_marks(FileData *fd) |
1297 { | |
1222 | 1298 gint i; |
1299 for (i = 0; i < FILEDATA_MARKS_SIZE; i++) file_data_get_mark(fd, i); | |
966 | 1300 return fd->marks; |
1301 } | |
1302 | |
800 | 1303 void file_data_set_mark(FileData *fd, gint n, gboolean value) |
1304 { | |
1227 | 1305 guint old; |
1306 if (!value == !file_data_get_mark(fd, n)) return; | |
1307 | |
1222 | 1308 if (file_data_set_mark_func[n]) |
1309 { | |
1310 (file_data_set_mark_func[n])(fd, n, value, file_data_mark_func_data[n]); | |
1311 } | |
1227 | 1312 |
1313 old = fd->marks; | |
1222 | 1314 |
800 | 1315 fd->marks = fd->marks ^ (1 << n); |
965 | 1316 |
1317 if (old && !fd->marks) /* keep files with non-zero marks in memory */ | |
1318 { | |
1319 file_data_unref(fd); | |
1320 } | |
1321 else if (!old && fd->marks) | |
1322 { | |
1323 file_data_ref(fd); | |
1324 } | |
1325 | |
800 | 1326 file_data_increment_version(fd); |
1432 | 1327 file_data_send_notification(fd, NOTIFY_MARKS); |
800 | 1328 } |
1329 | |
964 | 1330 gboolean file_data_filter_marks(FileData *fd, guint filter) |
1331 { | |
1222 | 1332 gint i; |
1333 for (i = 0; i < FILEDATA_MARKS_SIZE; i++) if (filter & (1 << i)) file_data_get_mark(fd, i); | |
964 | 1334 return ((fd->marks & filter) == filter); |
1335 } | |
1336 | |
1337 GList *file_data_filter_marks_list(GList *list, guint filter) | |
1338 { | |
1339 GList *work; | |
1340 | |
1341 work = list; | |
1342 while (work) | |
1343 { | |
1344 FileData *fd = work->data; | |
1345 GList *link = work; | |
1346 work = work->next; | |
1347 | |
1348 if (!file_data_filter_marks(fd, filter)) | |
1349 { | |
1350 list = g_list_remove_link(list, link); | |
1351 file_data_unref(fd); | |
1352 g_list_free(link); | |
1353 } | |
1354 } | |
1355 | |
1356 return list; | |
1357 } | |
1358 | |
1222 | 1359 static void file_data_notify_mark_func(gpointer key, gpointer value, gpointer user_data) |
1360 { | |
1361 FileData *fd = value; | |
1362 file_data_increment_version(fd); | |
1432 | 1363 file_data_send_notification(fd, NOTIFY_MARKS); |
1222 | 1364 } |
1365 | |
1425 | 1366 gboolean file_data_register_mark_func(gint n, FileDataGetMarkFunc get_mark_func, FileDataSetMarkFunc set_mark_func, gpointer data, GDestroyNotify notify) |
1222 | 1367 { |
1368 if (n < 0 || n >= FILEDATA_MARKS_SIZE) return FALSE; | |
1425 | 1369 |
1370 if (file_data_destroy_mark_func[n]) (file_data_destroy_mark_func[n])(file_data_mark_func_data[n]); | |
1222 | 1371 |
1372 file_data_get_mark_func[n] = get_mark_func; | |
1373 file_data_set_mark_func[n] = set_mark_func; | |
1374 file_data_mark_func_data[n] = data; | |
1425 | 1375 file_data_destroy_mark_func[n] = notify; |
1376 | |
1222 | 1377 if (get_mark_func) |
1425 | 1378 { |
1379 /* this effectively changes all known files */ | |
1380 g_hash_table_foreach(file_data_pool, file_data_notify_mark_func, NULL); | |
1381 } | |
1382 | |
1222 | 1383 return TRUE; |
1384 } | |
1385 | |
1386 void file_data_get_registered_mark_func(gint n, FileDataGetMarkFunc *get_mark_func, FileDataSetMarkFunc *set_mark_func, gpointer *data) | |
1387 { | |
1388 if (get_mark_func) *get_mark_func = file_data_get_mark_func[n]; | |
1389 if (set_mark_func) *set_mark_func = file_data_set_mark_func[n]; | |
1390 if (data) *data = file_data_mark_func_data[n]; | |
1391 } | |
1392 | |
800 | 1393 gint file_data_get_user_orientation(FileData *fd) |
1394 { | |
1395 return fd->user_orientation; | |
1396 } | |
1397 | |
1398 void file_data_set_user_orientation(FileData *fd, gint value) | |
1399 { | |
1400 if (fd->user_orientation == value) return; | |
1401 | |
1402 fd->user_orientation = value; | |
1403 file_data_increment_version(fd); | |
1432 | 1404 file_data_send_notification(fd, NOTIFY_ORIENTATION); |
800 | 1405 } |
1406 | |
1407 | |
590 | 1408 /* |
1409 * file_data - operates on the given fd | |
1410 * file_data_sc - operates on the given fd + sidecars - all fds linked via fd->sidecar_files or fd->parent | |
1411 */ | |
1412 | |
1413 | |
1414 /* return list of sidecar file extensions in a string */ | |
596 | 1415 gchar *file_data_sc_list_to_string(FileData *fd) |
1416 { | |
1417 GList *work; | |
1418 GString *result = g_string_new(""); | |
1419 | |
1420 work = fd->sidecar_files; | |
1421 while (work) | |
1422 { | |
1423 FileData *sfd = work->data; | |
806 | 1424 |
596 | 1425 result = g_string_append(result, "+ "); |
1426 result = g_string_append(result, sfd->extension); | |
1427 work = work->next; | |
1428 if (work) result = g_string_append_c(result, ' '); | |
1429 } | |
1430 | |
1431 return g_string_free(result, FALSE); | |
1432 } | |
590 | 1433 |
1434 | |
995 | 1435 |
1436 /* | |
1437 * add FileDataChangeInfo (see typedefs.h) for the given operation | |
590 | 1438 * uses file_data_add_change_info |
1439 * | |
1440 * fails if the fd->change already exists - change operations can't run in parallel | |
1441 * fd->change_info works as a lock | |
1442 * | |
1443 * dest can be NULL - in this case the current name is used for now, it will | |
995 | 1444 * be changed later |
590 | 1445 */ |
1446 | |
1447 /* | |
1448 FileDataChangeInfo types: | |
1449 COPY | |
950 | 1450 MOVE - path is changed, name may be changed too |
590 | 1451 RENAME - path remains unchanged, name is changed |
1452 extension should remain (FIXME should we allow editing extension? it will make problems wth grouping) | |
1453 sidecar names are changed too, extensions are not changed | |
1454 DELETE | |
995 | 1455 UPDATE - file size, date or grouping has been changed |
590 | 1456 */ |
1457 | |
1458 gboolean file_data_add_ci(FileData *fd, FileDataChangeType type, const gchar *src, const gchar *dest) | |
1459 { | |
1460 FileDataChangeInfo *fdci; | |
1461 | |
1462 if (fd->change) return FALSE; | |
1463 | |
1464 fdci = g_new0(FileDataChangeInfo, 1); | |
1465 | |
1466 fdci->type = type; | |
1467 | |
1468 if (src) | |
1469 fdci->source = g_strdup(src); | |
1470 else | |
1471 fdci->source = g_strdup(fd->path); | |
1472 | |
1473 if (dest) | |
1474 fdci->dest = g_strdup(dest); | |
1475 | |
1476 fd->change = fdci; | |
1477 | |
1478 return TRUE; | |
1479 } | |
1480 | |
912
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1481 static void file_data_planned_change_remove(FileData *fd) |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1482 { |
995 | 1483 if (file_data_planned_change_hash && |
912
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1484 (fd->change->type == FILEDATA_CHANGE_MOVE || fd->change->type == FILEDATA_CHANGE_RENAME)) |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1485 { |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1486 if (g_hash_table_lookup(file_data_planned_change_hash, fd->change->dest) == fd) |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1487 { |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1488 DEBUG_1("planned change: removing %s -> %s", fd->change->dest, fd->path); |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1489 g_hash_table_remove(file_data_planned_change_hash, fd->change->dest); |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1490 file_data_unref(fd); |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1491 if (g_hash_table_size(file_data_planned_change_hash) == 0) |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1492 { |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1493 g_hash_table_destroy(file_data_planned_change_hash); |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1494 file_data_planned_change_hash = NULL; |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1495 DEBUG_1("planned change: empty"); |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1496 } |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1497 } |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1498 } |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1499 } |
995 | 1500 |
912
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1501 |
590 | 1502 void file_data_free_ci(FileData *fd) |
1503 { | |
1504 FileDataChangeInfo *fdci = fd->change; | |
1505 | |
1422 | 1506 if (!fdci) return; |
590 | 1507 |
912
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1508 file_data_planned_change_remove(fd); |
1623
2842a051c870
regroup sidecar files when an operation on partial group is finished
nadvornik
parents:
1622
diff
changeset
|
1509 |
2842a051c870
regroup sidecar files when an operation on partial group is finished
nadvornik
parents:
1622
diff
changeset
|
1510 if (fdci->regroup_when_finished) file_data_disable_grouping(fd, FALSE); |
912
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1511 |
590 | 1512 g_free(fdci->source); |
1513 g_free(fdci->dest); | |
1514 | |
1515 g_free(fdci); | |
1516 | |
1517 fd->change = NULL; | |
1518 } | |
1519 | |
1623
2842a051c870
regroup sidecar files when an operation on partial group is finished
nadvornik
parents:
1622
diff
changeset
|
1520 void file_data_set_regroup_when_finished(FileData *fd, gboolean enable) |
2842a051c870
regroup sidecar files when an operation on partial group is finished
nadvornik
parents:
1622
diff
changeset
|
1521 { |
2842a051c870
regroup sidecar files when an operation on partial group is finished
nadvornik
parents:
1622
diff
changeset
|
1522 FileDataChangeInfo *fdci = fd->change; |
2842a051c870
regroup sidecar files when an operation on partial group is finished
nadvornik
parents:
1622
diff
changeset
|
1523 if (!fdci) return; |
2842a051c870
regroup sidecar files when an operation on partial group is finished
nadvornik
parents:
1622
diff
changeset
|
1524 fdci->regroup_when_finished = enable; |
2842a051c870
regroup sidecar files when an operation on partial group is finished
nadvornik
parents:
1622
diff
changeset
|
1525 } |
995 | 1526 |
590 | 1527 static gboolean file_data_sc_add_ci(FileData *fd, FileDataChangeType type) |
1528 { | |
1529 GList *work; | |
806 | 1530 |
590 | 1531 if (fd->parent) fd = fd->parent; |
1532 | |
1533 if (fd->change) return FALSE; | |
806 | 1534 |
590 | 1535 work = fd->sidecar_files; |
1536 while (work) | |
1537 { | |
1538 FileData *sfd = work->data; | |
806 | 1539 |
590 | 1540 if (sfd->change) return FALSE; |
1541 work = work->next; | |
1542 } | |
1543 | |
1544 file_data_add_ci(fd, type, NULL, NULL); | |
1545 | |
1546 work = fd->sidecar_files; | |
1547 while (work) | |
1548 { | |
1549 FileData *sfd = work->data; | |
806 | 1550 |
590 | 1551 file_data_add_ci(sfd, type, NULL, NULL); |
1552 work = work->next; | |
1553 } | |
1554 | |
995 | 1555 return TRUE; |
590 | 1556 } |
1557 | |
1558 static gboolean file_data_sc_check_ci(FileData *fd, FileDataChangeType type) | |
1559 { | |
1560 GList *work; | |
806 | 1561 |
590 | 1562 if (fd->parent) fd = fd->parent; |
1563 | |
806 | 1564 if (!fd->change || fd->change->type != type) return FALSE; |
1565 | |
590 | 1566 work = fd->sidecar_files; |
1567 while (work) | |
1568 { | |
1569 FileData *sfd = work->data; | |
806 | 1570 |
1571 if (!sfd->change || sfd->change->type != type) return FALSE; | |
590 | 1572 work = work->next; |
1573 } | |
806 | 1574 |
590 | 1575 return TRUE; |
1576 } | |
1577 | |
1578 | |
751 | 1579 gboolean file_data_sc_add_ci_copy(FileData *fd, const gchar *dest_path) |
590 | 1580 { |
1581 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_COPY)) return FALSE; | |
1582 file_data_sc_update_ci_copy(fd, dest_path); | |
1583 return TRUE; | |
1584 } | |
1585 | |
751 | 1586 gboolean file_data_sc_add_ci_move(FileData *fd, const gchar *dest_path) |
590 | 1587 { |
1588 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_MOVE)) return FALSE; | |
1589 file_data_sc_update_ci_move(fd, dest_path); | |
1590 return TRUE; | |
1591 } | |
1592 | |
751 | 1593 gboolean file_data_sc_add_ci_rename(FileData *fd, const gchar *dest_path) |
590 | 1594 { |
1595 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_RENAME)) return FALSE; | |
1596 file_data_sc_update_ci_rename(fd, dest_path); | |
1597 return TRUE; | |
1598 } | |
1599 | |
1600 gboolean file_data_sc_add_ci_delete(FileData *fd) | |
1601 { | |
1602 return file_data_sc_add_ci(fd, FILEDATA_CHANGE_DELETE); | |
1603 } | |
1604 | |
753 | 1605 gboolean file_data_sc_add_ci_unspecified(FileData *fd, const gchar *dest_path) |
590 | 1606 { |
753 | 1607 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_UNSPECIFIED)) return FALSE; |
1608 file_data_sc_update_ci_unspecified(fd, dest_path); | |
1609 return TRUE; | |
590 | 1610 } |
1611 | |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1612 gboolean file_data_add_ci_write_metadata(FileData *fd) |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1613 { |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1614 return file_data_add_ci(fd, FILEDATA_CHANGE_WRITE_METADATA, NULL, NULL); |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1615 } |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1616 |
590 | 1617 void file_data_sc_free_ci(FileData *fd) |
1618 { | |
1619 GList *work; | |
806 | 1620 |
590 | 1621 if (fd->parent) fd = fd->parent; |
1622 | |
1623 file_data_free_ci(fd); | |
1624 | |
1625 work = fd->sidecar_files; | |
1626 while (work) | |
1627 { | |
1628 FileData *sfd = work->data; | |
806 | 1629 |
590 | 1630 file_data_free_ci(sfd); |
1631 work = work->next; | |
1632 } | |
1633 } | |
1634 | |
751 | 1635 gboolean file_data_sc_add_ci_delete_list(GList *fd_list) |
1636 { | |
1637 GList *work; | |
1638 gboolean ret = TRUE; | |
806 | 1639 |
751 | 1640 work = fd_list; |
1641 while (work) | |
1642 { | |
1643 FileData *fd = work->data; | |
806 | 1644 |
751 | 1645 if (!file_data_sc_add_ci_delete(fd)) ret = FALSE; |
1646 work = work->next; | |
1647 } | |
806 | 1648 |
751 | 1649 return ret; |
1650 } | |
1651 | |
913 | 1652 static void file_data_sc_revert_ci_list(GList *fd_list) |
751 | 1653 { |
1654 GList *work; | |
806 | 1655 |
751 | 1656 work = fd_list; |
1657 while (work) | |
1658 { | |
1659 FileData *fd = work->data; | |
806 | 1660 |
913 | 1661 file_data_sc_free_ci(fd); |
1662 work = work->prev; | |
1663 } | |
1664 } | |
1665 | |
923 | 1666 static gboolean file_data_sc_add_ci_list_call_func(GList *fd_list, const gchar *dest, gboolean (*func)(FileData *, const gchar *)) |
913 | 1667 { |
1668 GList *work; | |
1669 | |
1670 work = fd_list; | |
1671 while (work) | |
1672 { | |
1673 FileData *fd = work->data; | |
1674 | |
995 | 1675 if (!func(fd, dest)) |
913 | 1676 { |
1677 file_data_sc_revert_ci_list(work->prev); | |
1678 return FALSE; | |
1679 } | |
751 | 1680 work = work->next; |
1681 } | |
806 | 1682 |
913 | 1683 return TRUE; |
751 | 1684 } |
1685 | |
923 | 1686 gboolean file_data_sc_add_ci_copy_list(GList *fd_list, const gchar *dest) |
1687 { | |
1688 return file_data_sc_add_ci_list_call_func(fd_list, dest, file_data_sc_add_ci_copy); | |
1689 } | |
1690 | |
751 | 1691 gboolean file_data_sc_add_ci_move_list(GList *fd_list, const gchar *dest) |
1692 { | |
923 | 1693 return file_data_sc_add_ci_list_call_func(fd_list, dest, file_data_sc_add_ci_move); |
751 | 1694 } |
1695 | |
1696 gboolean file_data_sc_add_ci_rename_list(GList *fd_list, const gchar *dest) | |
1697 { | |
923 | 1698 return file_data_sc_add_ci_list_call_func(fd_list, dest, file_data_sc_add_ci_rename); |
751 | 1699 } |
1700 | |
753 | 1701 gboolean file_data_sc_add_ci_unspecified_list(GList *fd_list, const gchar *dest) |
1702 { | |
923 | 1703 return file_data_sc_add_ci_list_call_func(fd_list, dest, file_data_sc_add_ci_unspecified); |
753 | 1704 } |
1705 | |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1706 gboolean file_data_add_ci_write_metadata_list(GList *fd_list) |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1707 { |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1708 GList *work; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1709 gboolean ret = TRUE; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1710 |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1711 work = fd_list; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1712 while (work) |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1713 { |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1714 FileData *fd = work->data; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1715 |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1716 if (!file_data_add_ci_write_metadata(fd)) ret = FALSE; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1717 work = work->next; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1718 } |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1719 |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1720 return ret; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1721 } |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1722 |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1723 void file_data_free_ci_list(GList *fd_list) |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1724 { |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1725 GList *work; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1726 |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1727 work = fd_list; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1728 while (work) |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1729 { |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1730 FileData *fd = work->data; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1731 |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1732 file_data_free_ci(fd); |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1733 work = work->next; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1734 } |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1735 } |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1736 |
751 | 1737 void file_data_sc_free_ci_list(GList *fd_list) |
1738 { | |
1739 GList *work; | |
806 | 1740 |
751 | 1741 work = fd_list; |
1742 while (work) | |
1743 { | |
1744 FileData *fd = work->data; | |
806 | 1745 |
751 | 1746 file_data_sc_free_ci(fd); |
1747 work = work->next; | |
1748 } | |
1749 } | |
590 | 1750 |
995 | 1751 /* |
590 | 1752 * update existing fd->change, it will be used from dialog callbacks for interactive editing |
1753 * fails if fd->change does not exist or the change type does not match | |
1754 */ | |
1755 | |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1756 static void file_data_update_planned_change_hash(FileData *fd, const gchar *old_path, gchar *new_path) |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1757 { |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1758 FileDataChangeType type = fd->change->type; |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1759 |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1760 if (type == FILEDATA_CHANGE_MOVE || type == FILEDATA_CHANGE_RENAME) |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1761 { |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1762 FileData *ofd; |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1763 |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1764 if (!file_data_planned_change_hash) |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1765 file_data_planned_change_hash = g_hash_table_new(g_str_hash, g_str_equal); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1766 |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1767 if (old_path && g_hash_table_lookup(file_data_planned_change_hash, old_path) == fd) |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1768 { |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1769 DEBUG_1("planned change: removing %s -> %s", old_path, fd->path); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1770 g_hash_table_remove(file_data_planned_change_hash, old_path); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1771 file_data_unref(fd); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1772 } |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1773 |
918 | 1774 ofd = g_hash_table_lookup(file_data_planned_change_hash, new_path); |
1775 if (ofd != fd) | |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1776 { |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1777 if (ofd) |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1778 { |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1779 DEBUG_1("planned change: replacing %s -> %s", new_path, ofd->path); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1780 g_hash_table_remove(file_data_planned_change_hash, new_path); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1781 file_data_unref(ofd); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1782 } |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1783 |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1784 DEBUG_1("planned change: inserting %s -> %s", new_path, fd->path); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1785 file_data_ref(fd); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1786 g_hash_table_insert(file_data_planned_change_hash, new_path, fd); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1787 } |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1788 } |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1789 } |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1790 |
751 | 1791 static void file_data_update_ci_dest(FileData *fd, const gchar *dest_path) |
590 | 1792 { |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1793 gchar *old_path = fd->change->dest; |
918 | 1794 |
590 | 1795 fd->change->dest = g_strdup(dest_path); |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1796 file_data_update_planned_change_hash(fd, old_path, fd->change->dest); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1797 g_free(old_path); |
590 | 1798 } |
1799 | |
751 | 1800 static void file_data_update_ci_dest_preserve_ext(FileData *fd, const gchar *dest_path) |
590 | 1801 { |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
1802 const gchar *extension = extension_from_path(fd->change->source); |
751 | 1803 gchar *base = remove_extension_from_path(dest_path); |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1804 gchar *old_path = fd->change->dest; |
806 | 1805 |
920
408879d2a660
Use g_strconcat() instead of g_strdup_printf("%s%s", ...).
zas_
parents:
918
diff
changeset
|
1806 fd->change->dest = g_strconcat(base, extension, NULL); |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1807 file_data_update_planned_change_hash(fd, old_path, fd->change->dest); |
806 | 1808 |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1809 g_free(old_path); |
751 | 1810 g_free(base); |
590 | 1811 } |
1812 | |
751 | 1813 static void file_data_sc_update_ci(FileData *fd, const gchar *dest_path) |
590 | 1814 { |
1815 GList *work; | |
751 | 1816 gchar *dest_path_full = NULL; |
806 | 1817 |
590 | 1818 if (fd->parent) fd = fd->parent; |
1819 | |
995 | 1820 if (!dest_path) |
934 | 1821 { |
1822 dest_path = fd->path; | |
1823 } | |
1824 else if (!strchr(dest_path, G_DIR_SEPARATOR)) /* we got only filename, not a full path */ | |
751 | 1825 { |
1826 gchar *dir = remove_level_from_path(fd->path); | |
806 | 1827 |
751 | 1828 dest_path_full = g_build_filename(dir, dest_path, NULL); |
1829 g_free(dir); | |
1830 dest_path = dest_path_full; | |
1831 } | |
934 | 1832 else if (fd->change->type != FILEDATA_CHANGE_RENAME && isdir(dest_path)) /* rename should not move files between directories */ |
751 | 1833 { |
1834 dest_path_full = g_build_filename(dest_path, fd->name, NULL); | |
1835 dest_path = dest_path_full; | |
1836 } | |
806 | 1837 |
1838 file_data_update_ci_dest(fd, dest_path); | |
751 | 1839 |
590 | 1840 work = fd->sidecar_files; |
1841 while (work) | |
1842 { | |
1843 FileData *sfd = work->data; | |
806 | 1844 |
590 | 1845 file_data_update_ci_dest_preserve_ext(sfd, dest_path); |
1846 work = work->next; | |
1847 } | |
806 | 1848 |
751 | 1849 g_free(dest_path_full); |
590 | 1850 } |
1851 | |
1422 | 1852 static gboolean file_data_sc_check_update_ci(FileData *fd, const gchar *dest_path, FileDataChangeType type) |
590 | 1853 { |
950 | 1854 if (!file_data_sc_check_ci(fd, type)) return FALSE; |
590 | 1855 file_data_sc_update_ci(fd, dest_path); |
1856 return TRUE; | |
1857 } | |
1858 | |
1422 | 1859 gboolean file_data_sc_update_ci_copy(FileData *fd, const gchar *dest_path) |
950 | 1860 { |
1861 return file_data_sc_check_update_ci(fd, dest_path, FILEDATA_CHANGE_COPY); | |
1862 } | |
1863 | |
1422 | 1864 gboolean file_data_sc_update_ci_move(FileData *fd, const gchar *dest_path) |
950 | 1865 { |
1866 return file_data_sc_check_update_ci(fd, dest_path, FILEDATA_CHANGE_MOVE); | |
1867 } | |
1868 | |
1422 | 1869 gboolean file_data_sc_update_ci_rename(FileData *fd, const gchar *dest_path) |
590 | 1870 { |
950 | 1871 return file_data_sc_check_update_ci(fd, dest_path, FILEDATA_CHANGE_RENAME); |
590 | 1872 } |
1873 | |
1422 | 1874 gboolean file_data_sc_update_ci_unspecified(FileData *fd, const gchar *dest_path) |
753 | 1875 { |
950 | 1876 return file_data_sc_check_update_ci(fd, dest_path, FILEDATA_CHANGE_UNSPECIFIED); |
753 | 1877 } |
1878 | |
950 | 1879 static gboolean file_data_sc_update_ci_list_call_func(GList *fd_list, |
1880 const gchar *dest, | |
1881 gboolean (*func)(FileData *, const gchar *)) | |
751 | 1882 { |
1883 GList *work; | |
1884 gboolean ret = TRUE; | |
806 | 1885 |
751 | 1886 work = fd_list; |
1887 while (work) | |
1888 { | |
1889 FileData *fd = work->data; | |
806 | 1890 |
950 | 1891 if (!func(fd, dest)) ret = FALSE; |
751 | 1892 work = work->next; |
1893 } | |
806 | 1894 |
751 | 1895 return ret; |
1896 } | |
1897 | |
950 | 1898 gboolean file_data_sc_update_ci_move_list(GList *fd_list, const gchar *dest) |
1899 { | |
1900 return file_data_sc_update_ci_list_call_func(fd_list, dest, file_data_sc_update_ci_move); | |
1901 } | |
1902 | |
751 | 1903 gboolean file_data_sc_update_ci_copy_list(GList *fd_list, const gchar *dest) |
1904 { | |
950 | 1905 return file_data_sc_update_ci_list_call_func(fd_list, dest, file_data_sc_update_ci_copy); |
751 | 1906 } |
1907 | |
753 | 1908 gboolean file_data_sc_update_ci_unspecified_list(GList *fd_list, const gchar *dest) |
1909 { | |
950 | 1910 return file_data_sc_update_ci_list_call_func(fd_list, dest, file_data_sc_update_ci_unspecified); |
753 | 1911 } |
1912 | |
590 | 1913 |
1914 /* | |
929 | 1915 * verify source and dest paths - dest image exists, etc. |
590 | 1916 * it should detect all possible problems with the planned operation |
1917 */ | |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
1918 |
928 | 1919 gint file_data_verify_ci(FileData *fd) |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
1920 { |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
1921 gint ret = CHANGE_OK; |
929 | 1922 gchar *dir; |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
1923 |
942 | 1924 if (!fd->change) |
1925 { | |
1926 DEBUG_1("Change checked: no change info: %s", fd->path); | |
995 | 1927 return ret; |
942 | 1928 } |
929 | 1929 |
1224 | 1930 if (!isname(fd->path)) |
929 | 1931 { |
1932 /* this probably should not happen */ | |
1933 ret |= CHANGE_NO_SRC; | |
1934 DEBUG_1("Change checked: file does not exist: %s", fd->path); | |
995 | 1935 return ret; |
929 | 1936 } |
1937 | |
1938 dir = remove_level_from_path(fd->path); | |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
1939 |
995 | 1940 if (fd->change->type != FILEDATA_CHANGE_DELETE && |
1677
c5c7e19fbb23
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1654
diff
changeset
|
1941 fd->change->type != FILEDATA_CHANGE_MOVE && /* the unsaved metadata should survive move and rename operations */ |
c5c7e19fbb23
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1654
diff
changeset
|
1942 fd->change->type != FILEDATA_CHANGE_RENAME && |
c5c7e19fbb23
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1654
diff
changeset
|
1943 fd->change->type != FILEDATA_CHANGE_WRITE_METADATA && |
c5c7e19fbb23
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1654
diff
changeset
|
1944 fd->modified_xmp) |
c5c7e19fbb23
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1654
diff
changeset
|
1945 { |
c5c7e19fbb23
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1654
diff
changeset
|
1946 ret |= CHANGE_WARN_UNSAVED_META; |
c5c7e19fbb23
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1654
diff
changeset
|
1947 DEBUG_1("Change checked: unsaved metadata: %s", fd->path); |
c5c7e19fbb23
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1654
diff
changeset
|
1948 } |
c5c7e19fbb23
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1654
diff
changeset
|
1949 |
c5c7e19fbb23
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1654
diff
changeset
|
1950 if (fd->change->type != FILEDATA_CHANGE_DELETE && |
1211 | 1951 fd->change->type != FILEDATA_CHANGE_WRITE_METADATA && |
929 | 1952 !access_file(fd->path, R_OK)) |
1953 { | |
1954 ret |= CHANGE_NO_READ_PERM; | |
1955 DEBUG_1("Change checked: no read permission: %s", fd->path); | |
1956 } | |
1957 else if ((fd->change->type == FILEDATA_CHANGE_DELETE || fd->change->type == FILEDATA_CHANGE_MOVE) && | |
995 | 1958 !access_file(dir, W_OK)) |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
1959 { |
929 | 1960 ret |= CHANGE_NO_WRITE_PERM_DIR; |
1961 DEBUG_1("Change checked: source dir is readonly: %s", fd->path); | |
1962 } | |
995 | 1963 else if (fd->change->type != FILEDATA_CHANGE_COPY && |
1964 fd->change->type != FILEDATA_CHANGE_UNSPECIFIED && | |
1211 | 1965 fd->change->type != FILEDATA_CHANGE_WRITE_METADATA && |
995 | 1966 !access_file(fd->path, W_OK)) |
929 | 1967 { |
1968 ret |= CHANGE_WARN_NO_WRITE_PERM; | |
1969 DEBUG_1("Change checked: no write permission: %s", fd->path); | |
1970 } | |
1211 | 1971 /* WRITE_METADATA is special because it can be configured to silently write to ~/.geeqie/... |
1972 - that means that there are no hard errors and warnings can be disabled | |
1224 | 1973 - the destination is determined during the check |
1211 | 1974 */ |
1224 | 1975 else if (fd->change->type == FILEDATA_CHANGE_WRITE_METADATA) |
1211 | 1976 { |
1224 | 1977 /* determine destination file */ |
1978 gboolean have_dest = FALSE; | |
1979 gchar *dest_dir = NULL; | |
1980 | |
1981 if (options->metadata.save_in_image_file) | |
1211 | 1982 { |
1224 | 1983 if (file_data_can_write_directly(fd)) |
1984 { | |
1985 /* we can write the file directly */ | |
1986 if (access_file(fd->path, W_OK)) | |
1987 { | |
1988 have_dest = TRUE; | |
1989 } | |
1990 else | |
1991 { | |
1992 if (options->metadata.warn_on_write_problems) | |
1993 { | |
1994 ret |= CHANGE_WARN_NO_WRITE_PERM; | |
1995 DEBUG_1("Change checked: file is not writable: %s", fd->path); | |
1996 } | |
1997 } | |
1998 } | |
1999 else if (file_data_can_write_sidecar(fd)) | |
2000 { | |
2001 /* we can write sidecar */ | |
2002 gchar *sidecar = file_data_get_sidecar_path(fd, FALSE); | |
2003 if (access_file(sidecar, W_OK) || (!isname(sidecar) && access_file(dir, W_OK))) | |
2004 { | |
2005 file_data_update_ci_dest(fd, sidecar); | |
2006 have_dest = TRUE; | |
2007 } | |
2008 else | |
2009 { | |
2010 if (options->metadata.warn_on_write_problems) | |
2011 { | |
2012 ret |= CHANGE_WARN_NO_WRITE_PERM; | |
2013 DEBUG_1("Change checked: file is not writable: %s", sidecar); | |
2014 } | |
2015 } | |
2016 g_free(sidecar); | |
2017 } | |
1211 | 2018 } |
2019 | |
1224 | 2020 if (!have_dest) |
1211 | 2021 { |
1224 | 2022 /* write private metadata file under ~/.geeqie */ |
2023 | |
2024 /* If an existing metadata file exists, we will try writing to | |
2025 * it's location regardless of the user's preference. | |
2026 */ | |
1686 | 2027 gchar *metadata_path = NULL; |
2028 #ifdef HAVE_EXIV2 | |
2029 /* but ignore XMP if we are not able to write it */ | |
2030 metadata_path = cache_find_location(CACHE_TYPE_XMP_METADATA, fd->path); | |
2031 #endif | |
1224 | 2032 if (!metadata_path) metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path); |
2033 | |
2034 if (metadata_path && !access_file(metadata_path, W_OK)) | |
2035 { | |
2036 g_free(metadata_path); | |
2037 metadata_path = NULL; | |
2038 } | |
2039 | |
2040 if (!metadata_path) | |
2041 { | |
2042 mode_t mode = 0755; | |
2043 | |
2044 dest_dir = cache_get_location(CACHE_TYPE_METADATA, fd->path, FALSE, &mode); | |
2045 if (recursive_mkdir_if_not_exists(dest_dir, mode)) | |
2046 { | |
2047 gchar *filename = g_strconcat(fd->name, options->metadata.save_legacy_format ? GQ_CACHE_EXT_METADATA : GQ_CACHE_EXT_XMP_METADATA, NULL); | |
2048 | |
2049 metadata_path = g_build_filename(dest_dir, filename, NULL); | |
2050 g_free(filename); | |
2051 } | |
2052 } | |
2053 if (access_file(metadata_path, W_OK) || (!isname(metadata_path) && access_file(dest_dir, W_OK))) | |
2054 { | |
2055 file_data_update_ci_dest(fd, metadata_path); | |
2056 have_dest = TRUE; | |
2057 } | |
2058 else | |
2059 { | |
2060 ret |= CHANGE_NO_WRITE_PERM_DEST; | |
2061 DEBUG_1("Change checked: file is not writable: %s", metadata_path); | |
2062 } | |
2063 g_free(metadata_path); | |
1211 | 2064 } |
1224 | 2065 g_free(dest_dir); |
1211 | 2066 } |
2067 | |
1224 | 2068 if (fd->change->dest && fd->change->type != FILEDATA_CHANGE_WRITE_METADATA) |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2069 { |
953 | 2070 gboolean same; |
951 | 2071 gchar *dest_dir; |
955 | 2072 |
953 | 2073 same = (strcmp(fd->path, fd->change->dest) == 0); |
2074 | |
955 | 2075 if (!same) |
933 | 2076 { |
955 | 2077 const gchar *dest_ext = extension_from_path(fd->change->dest); |
2078 if (!dest_ext) dest_ext = ""; | |
933 | 2079 |
1307 | 2080 if (g_ascii_strcasecmp(fd->extension, dest_ext) != 0) |
955 | 2081 { |
2082 ret |= CHANGE_WARN_CHANGED_EXT; | |
2083 DEBUG_1("Change checked: source and destination have different extensions: %s -> %s", fd->path, fd->change->dest); | |
2084 } | |
2085 } | |
2086 else | |
933 | 2087 { |
955 | 2088 if (fd->change->type != FILEDATA_CHANGE_UNSPECIFIED) /* FIXME this is now needed for running editors */ |
2089 { | |
2090 ret |= CHANGE_WARN_SAME; | |
2091 DEBUG_1("Change checked: source and destination are the same: %s -> %s", fd->path, fd->change->dest); | |
2092 } | |
2093 } | |
933 | 2094 |
951 | 2095 dest_dir = remove_level_from_path(fd->change->dest); |
2096 | |
995 | 2097 if (!isdir(dest_dir)) |
929 | 2098 { |
2099 ret |= CHANGE_NO_DEST_DIR; | |
2100 DEBUG_1("Change checked: destination dir does not exist: %s -> %s", fd->path, fd->change->dest); | |
2101 } | |
2102 else if (!access_file(dest_dir, W_OK)) | |
2103 { | |
2104 ret |= CHANGE_NO_WRITE_PERM_DEST_DIR; | |
2105 DEBUG_1("Change checked: destination dir is readonly: %s -> %s", fd->path, fd->change->dest); | |
2106 } | |
955 | 2107 else if (!same) |
929 | 2108 { |
955 | 2109 if (isfile(fd->change->dest)) |
952 | 2110 { |
955 | 2111 if (!access_file(fd->change->dest, W_OK)) |
2112 { | |
2113 ret |= CHANGE_NO_WRITE_PERM_DEST; | |
2114 DEBUG_1("Change checked: destination file exists and is readonly: %s -> %s", fd->path, fd->change->dest); | |
2115 } | |
2116 else | |
2117 { | |
2118 ret |= CHANGE_WARN_DEST_EXISTS; | |
2119 DEBUG_1("Change checked: destination exists: %s -> %s", fd->path, fd->change->dest); | |
2120 } | |
952 | 2121 } |
955 | 2122 else if (isdir(fd->change->dest)) |
952 | 2123 { |
955 | 2124 ret |= CHANGE_DEST_EXISTS; |
952 | 2125 DEBUG_1("Change checked: destination exists: %s -> %s", fd->path, fd->change->dest); |
2126 } | |
929 | 2127 } |
951 | 2128 |
2129 g_free(dest_dir); | |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2130 } |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2131 |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2132 fd->change->error = ret; |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2133 if (ret == 0) DEBUG_1("Change checked: OK: %s", fd->path); |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2134 |
929 | 2135 g_free(dir); |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2136 return ret; |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2137 } |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2138 |
995 | 2139 |
928 | 2140 gint file_data_sc_verify_ci(FileData *fd) |
590 | 2141 { |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2142 GList *work; |
928 | 2143 gint ret; |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2144 |
928 | 2145 ret = file_data_verify_ci(fd); |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2146 |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2147 work = fd->sidecar_files; |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2148 while (work) |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2149 { |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2150 FileData *sfd = work->data; |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2151 |
928 | 2152 ret |= file_data_verify_ci(sfd); |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2153 work = work->next; |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2154 } |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2155 |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2156 return ret; |
590 | 2157 } |
2158 | |
928 | 2159 gchar *file_data_get_error_string(gint error) |
2160 { | |
2161 GString *result = g_string_new(""); | |
590 | 2162 |
929 | 2163 if (error & CHANGE_NO_SRC) |
2164 { | |
2165 if (result->len > 0) g_string_append(result, ", "); | |
2166 g_string_append(result, _("file or directory does not exist")); | |
2167 } | |
2168 | |
2169 if (error & CHANGE_DEST_EXISTS) | |
2170 { | |
2171 if (result->len > 0) g_string_append(result, ", "); | |
2172 g_string_append(result, _("destination already exists")); | |
2173 } | |
2174 | |
2175 if (error & CHANGE_NO_WRITE_PERM_DEST) | |
2176 { | |
2177 if (result->len > 0) g_string_append(result, ", "); | |
2178 g_string_append(result, _("destination can't be overwritten")); | |
2179 } | |
2180 | |
2181 if (error & CHANGE_NO_WRITE_PERM_DEST_DIR) | |
2182 { | |
2183 if (result->len > 0) g_string_append(result, ", "); | |
2184 g_string_append(result, _("destination directory is not writable")); | |
2185 } | |
2186 | |
2187 if (error & CHANGE_NO_DEST_DIR) | |
2188 { | |
2189 if (result->len > 0) g_string_append(result, ", "); | |
2190 g_string_append(result, _("destination directory does not exist")); | |
2191 } | |
2192 | |
2193 if (error & CHANGE_NO_WRITE_PERM_DIR) | |
2194 { | |
2195 if (result->len > 0) g_string_append(result, ", "); | |
2196 g_string_append(result, _("source directory is not writable")); | |
2197 } | |
2198 | |
2199 if (error & CHANGE_NO_READ_PERM) | |
928 | 2200 { |
2201 if (result->len > 0) g_string_append(result, ", "); | |
2202 g_string_append(result, _("no read permission")); | |
2203 } | |
2204 | |
929 | 2205 if (error & CHANGE_WARN_NO_WRITE_PERM) |
928 | 2206 { |
2207 if (result->len > 0) g_string_append(result, ", "); | |
929 | 2208 g_string_append(result, _("file is readonly")); |
2209 } | |
2210 | |
2211 if (error & CHANGE_WARN_DEST_EXISTS) | |
2212 { | |
2213 if (result->len > 0) g_string_append(result, ", "); | |
2214 g_string_append(result, _("destination already exists and will be overwritten")); | |
2215 } | |
933 | 2216 |
929 | 2217 if (error & CHANGE_WARN_SAME) |
2218 { | |
2219 if (result->len > 0) g_string_append(result, ", "); | |
948 | 2220 g_string_append(result, _("source and destination are the same")); |
928 | 2221 } |
2222 | |
933 | 2223 if (error & CHANGE_WARN_CHANGED_EXT) |
2224 { | |
2225 if (result->len > 0) g_string_append(result, ", "); | |
2226 g_string_append(result, _("source and destination have different extension")); | |
2227 } | |
2228 | |
1677
c5c7e19fbb23
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1654
diff
changeset
|
2229 if (error & CHANGE_WARN_UNSAVED_META) |
c5c7e19fbb23
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1654
diff
changeset
|
2230 { |
c5c7e19fbb23
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1654
diff
changeset
|
2231 if (result->len > 0) g_string_append(result, ", "); |
c5c7e19fbb23
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1654
diff
changeset
|
2232 g_string_append(result, _("there are unsaved metadata changes for the file")); |
c5c7e19fbb23
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1654
diff
changeset
|
2233 } |
c5c7e19fbb23
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1654
diff
changeset
|
2234 |
928 | 2235 return g_string_free(result, FALSE); |
2236 } | |
2237 | |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
2238 gint file_data_verify_ci_list(GList *list, gchar **desc, gboolean with_sidecars) |
928 | 2239 { |
956 | 2240 GList *work; |
928 | 2241 gint all_errors = 0; |
995 | 2242 gint common_errors = ~0; |
928 | 2243 gint num; |
2244 gint *errors; | |
2245 gint i; | |
2246 | |
2247 if (!list) return 0; | |
2248 | |
2249 num = g_list_length(list); | |
2250 errors = g_new(int, num); | |
956 | 2251 work = list; |
928 | 2252 i = 0; |
2253 while (work) | |
2254 { | |
2255 FileData *fd; | |
2256 gint error; | |
2257 | |
2258 fd = work->data; | |
2259 work = work->next; | |
2260 | |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
2261 error = with_sidecars ? file_data_sc_verify_ci(fd) : file_data_verify_ci(fd); |
928 | 2262 all_errors |= error; |
2263 common_errors &= error; | |
2264 | |
2265 errors[i] = error; | |
2266 | |
2267 i++; | |
2268 } | |
2269 | |
2270 if (desc && all_errors) | |
2271 { | |
956 | 2272 GList *work; |
928 | 2273 GString *result = g_string_new(""); |
2274 | |
2275 if (common_errors) | |
2276 { | |
2277 gchar *str = file_data_get_error_string(common_errors); | |
2278 g_string_append(result, str); | |
2279 g_string_append(result, "\n"); | |
2280 g_free(str); | |
2281 } | |
2282 | |
956 | 2283 work = list; |
928 | 2284 i = 0; |
2285 while (work) | |
2286 { | |
2287 FileData *fd; | |
2288 gint error; | |
2289 | |
2290 fd = work->data; | |
2291 work = work->next; | |
2292 | |
2293 error = errors[i] & ~common_errors; | |
2294 | |
2295 if (error) | |
2296 { | |
2297 gchar *str = file_data_get_error_string(error); | |
2298 g_string_append_printf(result, "%s: %s\n", fd->name, str); | |
2299 g_free(str); | |
2300 } | |
2301 i++; | |
2302 } | |
2303 *desc = g_string_free(result, FALSE); | |
2304 } | |
2305 | |
2306 g_free(errors); | |
2307 return all_errors; | |
2308 } | |
590 | 2309 |
2310 | |
2311 /* | |
2312 * perform the change described by FileFataChangeInfo | |
995 | 2313 * it is used for internal operations, |
590 | 2314 * this function actually operates with files on the filesystem |
2315 * it should implement safe delete | |
2316 */ | |
995 | 2317 |
590 | 2318 static gboolean file_data_perform_move(FileData *fd) |
2319 { | |
2320 g_assert(!strcmp(fd->change->source, fd->path)); | |
2321 return move_file(fd->change->source, fd->change->dest); | |
2322 } | |
2323 | |
2324 static gboolean file_data_perform_copy(FileData *fd) | |
2325 { | |
2326 g_assert(!strcmp(fd->change->source, fd->path)); | |
2327 return copy_file(fd->change->source, fd->change->dest); | |
2328 } | |
2329 | |
2330 static gboolean file_data_perform_delete(FileData *fd) | |
2331 { | |
896
cf21dc928122
implemented directory rename and delete operations
nadvornik
parents:
892
diff
changeset
|
2332 if (isdir(fd->path) && !islink(fd->path)) |
cf21dc928122
implemented directory rename and delete operations
nadvornik
parents:
892
diff
changeset
|
2333 return rmdir_utf8(fd->path); |
cf21dc928122
implemented directory rename and delete operations
nadvornik
parents:
892
diff
changeset
|
2334 else |
1212 | 2335 if (options->file_ops.safe_delete_enable) |
2336 return file_util_safe_unlink(fd->path); | |
2337 else | |
2338 return unlink_file(fd->path); | |
590 | 2339 } |
2340 | |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
2341 gboolean file_data_perform_ci(FileData *fd) |
590 | 2342 { |
2343 FileDataChangeType type = fd->change->type; | |
1422 | 2344 |
590 | 2345 switch (type) |
2346 { | |
2347 case FILEDATA_CHANGE_MOVE: | |
2348 return file_data_perform_move(fd); | |
2349 case FILEDATA_CHANGE_COPY: | |
2350 return file_data_perform_copy(fd); | |
2351 case FILEDATA_CHANGE_RENAME: | |
2352 return file_data_perform_move(fd); /* the same as move */ | |
2353 case FILEDATA_CHANGE_DELETE: | |
2354 return file_data_perform_delete(fd); | |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
2355 case FILEDATA_CHANGE_WRITE_METADATA: |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
2356 return metadata_write_perform(fd); |
753 | 2357 case FILEDATA_CHANGE_UNSPECIFIED: |
596 | 2358 /* nothing to do here */ |
590 | 2359 break; |
2360 } | |
2361 return TRUE; | |
2362 } | |
2363 | |
2364 | |
2365 | |
2366 gboolean file_data_sc_perform_ci(FileData *fd) | |
2367 { | |
2368 GList *work; | |
2369 gboolean ret = TRUE; | |
2370 FileDataChangeType type = fd->change->type; | |
806 | 2371 |
590 | 2372 if (!file_data_sc_check_ci(fd, type)) return FALSE; |
2373 | |
2374 work = fd->sidecar_files; | |
2375 while (work) | |
2376 { | |
2377 FileData *sfd = work->data; | |
806 | 2378 |
590 | 2379 if (!file_data_perform_ci(sfd)) ret = FALSE; |
2380 work = work->next; | |
2381 } | |
806 | 2382 |
590 | 2383 if (!file_data_perform_ci(fd)) ret = FALSE; |
806 | 2384 |
590 | 2385 return ret; |
2386 } | |
2387 | |
2388 /* | |
2389 * updates FileData structure according to FileDataChangeInfo | |
2390 */ | |
912
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
2391 |
1422 | 2392 gboolean file_data_apply_ci(FileData *fd) |
590 | 2393 { |
2394 FileDataChangeType type = fd->change->type; | |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
2395 |
590 | 2396 /* FIXME delete ?*/ |
773 | 2397 if (type == FILEDATA_CHANGE_MOVE || type == FILEDATA_CHANGE_RENAME) |
590 | 2398 { |
912
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
2399 DEBUG_1("planned change: applying %s -> %s", fd->change->dest, fd->path); |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
2400 file_data_planned_change_remove(fd); |
915 | 2401 |
2402 if (g_hash_table_lookup(file_data_pool, fd->change->dest)) | |
2403 { | |
2404 /* this change overwrites another file which is already known to other modules | |
2405 renaming fd would create duplicate FileData structure | |
2406 the best thing we can do is nothing | |
2407 FIXME: maybe we could copy stuff like marks | |
2408 */ | |
2409 DEBUG_1("can't rename fd, target exists %s -> %s", fd->change->dest, fd->path); | |
2410 } | |
2411 else | |
2412 { | |
1728 | 2413 file_data_set_path(fd, fd->change->dest, NULL); |
915 | 2414 } |
590 | 2415 } |
763
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
2416 file_data_increment_version(fd); |
1432 | 2417 file_data_send_notification(fd, NOTIFY_CHANGE); |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
2418 |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
2419 return TRUE; |
590 | 2420 } |
2421 | |
1422 | 2422 gboolean file_data_sc_apply_ci(FileData *fd) |
590 | 2423 { |
2424 GList *work; | |
2425 FileDataChangeType type = fd->change->type; | |
806 | 2426 |
590 | 2427 if (!file_data_sc_check_ci(fd, type)) return FALSE; |
2428 | |
2429 work = fd->sidecar_files; | |
2430 while (work) | |
2431 { | |
2432 FileData *sfd = work->data; | |
806 | 2433 |
590 | 2434 file_data_apply_ci(sfd); |
2435 work = work->next; | |
2436 } | |
806 | 2437 |
590 | 2438 file_data_apply_ci(fd); |
806 | 2439 |
590 | 2440 return TRUE; |
2441 } | |
2442 | |
1622 | 2443 static gboolean file_data_list_contains_whole_group(GList *list, FileData *fd) |
2444 { | |
2445 GList *work; | |
2446 if (fd->parent) fd = fd->parent; | |
2447 if (!g_list_find(list, fd)) return FALSE; | |
2448 | |
2449 work = fd->sidecar_files; | |
2450 while (work) | |
2451 { | |
2452 if (!g_list_find(list, work->data)) return FALSE; | |
2453 work = work->next; | |
2454 } | |
2455 return TRUE; | |
2456 } | |
2457 | |
2458 #if 0 | |
2459 static gboolean file_data_list_dump(GList *list) | |
2460 { | |
2461 GList *work, *work2; | |
2462 | |
2463 work = list; | |
2464 while (work) | |
2465 { | |
2466 FileData *fd = work->data; | |
2467 printf("%s\n", fd->name); | |
2468 work2 = fd->sidecar_files; | |
2469 while (work2) | |
2470 { | |
2471 FileData *fd = work2->data; | |
2472 printf(" %s\n", fd->name); | |
2473 work2 = work2->next; | |
2474 } | |
2475 work = work->next; | |
2476 } | |
2477 return TRUE; | |
2478 } | |
2479 #endif | |
2480 | |
1729 | 2481 GList *file_data_process_groups_in_selection(GList *list, gboolean ungroup, GList **ungrouped_list) |
1622 | 2482 { |
2483 GList *out = NULL; | |
2484 GList *work = list; | |
2485 | |
2486 /* change partial groups to independent files */ | |
1729 | 2487 if (ungroup) |
1622 | 2488 { |
1729 | 2489 while (work) |
2490 { | |
2491 FileData *fd = work->data; | |
2492 work = work->next; | |
1622 | 2493 |
1729 | 2494 if (!file_data_list_contains_whole_group(list, fd)) |
1623
2842a051c870
regroup sidecar files when an operation on partial group is finished
nadvornik
parents:
1622
diff
changeset
|
2495 { |
1729 | 2496 file_data_disable_grouping(fd, TRUE); |
2497 if (ungrouped_list) | |
2498 { | |
2499 *ungrouped_list = g_list_prepend(*ungrouped_list, file_data_ref(fd)); | |
2500 } | |
1623
2842a051c870
regroup sidecar files when an operation on partial group is finished
nadvornik
parents:
1622
diff
changeset
|
2501 } |
2842a051c870
regroup sidecar files when an operation on partial group is finished
nadvornik
parents:
1622
diff
changeset
|
2502 } |
1622 | 2503 } |
2504 | |
2505 /* remove sidecars from the list, | |
2506 they can be still acessed via main_fd->sidecar_files */ | |
2507 work = list; | |
2508 while (work) | |
2509 { | |
2510 FileData *fd = work->data; | |
2511 work = work->next; | |
2512 | |
1729 | 2513 if (!fd->parent || |
2514 (!ungroup && !file_data_list_contains_whole_group(list, fd))) | |
1622 | 2515 { |
1729 | 2516 out = g_list_prepend(out, file_data_ref(fd)); |
1622 | 2517 } |
2518 } | |
2519 | |
1729 | 2520 filelist_free(list); |
1622 | 2521 out = g_list_reverse(out); |
2522 | |
2523 return out; | |
2524 } | |
2525 | |
2526 | |
2527 | |
2528 | |
2529 | |
590 | 2530 /* |
1622 | 2531 * notify other modules about the change described by FileDataChangeInfo |
590 | 2532 */ |
995 | 2533 |
590 | 2534 /* might use file_maint_ functions for now, later it should be changed to a system of callbacks |
2535 FIXME do we need the ignore_list? It looks like a workaround for ineffective | |
2536 implementation in view_file_list.c */ | |
2537 | |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2538 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2539 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2540 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2541 typedef struct _NotifyData NotifyData; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2542 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2543 struct _NotifyData { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2544 FileDataNotifyFunc func; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2545 gpointer data; |
791 | 2546 NotifyPriority priority; |
1422 | 2547 }; |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2548 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2549 static GList *notify_func_list = NULL; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2550 |
791 | 2551 static gint file_data_notify_sort(gconstpointer a, gconstpointer b) |
2552 { | |
2553 NotifyData *nda = (NotifyData *)a; | |
2554 NotifyData *ndb = (NotifyData *)b; | |
806 | 2555 |
791 | 2556 if (nda->priority < ndb->priority) return -1; |
2557 if (nda->priority > ndb->priority) return 1; | |
2558 return 0; | |
2559 } | |
2560 | |
1422 | 2561 gboolean file_data_register_notify_func(FileDataNotifyFunc func, gpointer data, NotifyPriority priority) |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2562 { |
806 | 2563 NotifyData *nd; |
1730 | 2564 GList *work = notify_func_list; |
2565 | |
2566 while (work) | |
2567 { | |
2568 NotifyData *nd = (NotifyData *)work->data; | |
2569 | |
2570 if (nd->func == func && nd->data == data) | |
2571 { | |
2572 g_warning("Notify func already registered"); | |
2573 return FALSE; | |
2574 } | |
2575 work = work->next; | |
2576 } | |
806 | 2577 |
2578 nd = g_new(NotifyData, 1); | |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2579 nd->func = func; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2580 nd->data = data; |
791 | 2581 nd->priority = priority; |
806 | 2582 |
791 | 2583 notify_func_list = g_list_insert_sorted(notify_func_list, nd, file_data_notify_sort); |
1498 | 2584 DEBUG_2("Notify func registered: %p", nd); |
806 | 2585 |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2586 return TRUE; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2587 } |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2588 |
1422 | 2589 gboolean file_data_unregister_notify_func(FileDataNotifyFunc func, gpointer data) |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2590 { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2591 GList *work = notify_func_list; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2592 |
806 | 2593 while (work) |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2594 { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2595 NotifyData *nd = (NotifyData *)work->data; |
806 | 2596 |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2597 if (nd->func == func && nd->data == data) |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2598 { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2599 notify_func_list = g_list_delete_link(notify_func_list, work); |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2600 g_free(nd); |
1498 | 2601 DEBUG_2("Notify func unregistered: %p", nd); |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2602 return TRUE; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2603 } |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2604 work = work->next; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2605 } |
806 | 2606 |
1730 | 2607 g_warning("Notify func not found"); |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2608 return FALSE; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2609 } |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2610 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2611 |
792 | 2612 void file_data_send_notification(FileData *fd, NotifyType type) |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2613 { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2614 GList *work = notify_func_list; |
806 | 2615 |
2616 while (work) | |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2617 { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2618 NotifyData *nd = (NotifyData *)work->data; |
806 | 2619 |
792 | 2620 nd->func(fd, type, nd->data); |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2621 work = work->next; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2622 } |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2623 } |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2624 |
791 | 2625 static GHashTable *file_data_monitor_pool = NULL; |
1523 | 2626 static guint realtime_monitor_id = 0; /* event source id */ |
791 | 2627 |
2628 static void realtime_monitor_check_cb(gpointer key, gpointer value, gpointer data) | |
2629 { | |
2630 FileData *fd = key; | |
2631 | |
801 | 2632 file_data_check_changed_files(fd); |
791 | 2633 |
2634 DEBUG_1("monitor %s", fd->path); | |
2635 } | |
2636 | |
2637 static gboolean realtime_monitor_cb(gpointer data) | |
2638 { | |
903
c93823609f15
periodic testing of changed files can be now disabled
nadvornik
parents:
899
diff
changeset
|
2639 if (!options->update_on_time_change) return TRUE; |
791 | 2640 g_hash_table_foreach(file_data_monitor_pool, realtime_monitor_check_cb, NULL); |
2641 return TRUE; | |
2642 } | |
2643 | |
1422 | 2644 gboolean file_data_register_real_time_monitor(FileData *fd) |
791 | 2645 { |
950 | 2646 gint count; |
791 | 2647 |
2648 file_data_ref(fd); | |
2649 | |
2650 if (!file_data_monitor_pool) | |
2651 file_data_monitor_pool = g_hash_table_new(g_direct_hash, g_direct_equal); | |
2652 | |
2653 count = GPOINTER_TO_INT(g_hash_table_lookup(file_data_monitor_pool, fd)); | |
2654 | |
2655 DEBUG_1("Register realtime %d %s", count, fd->path); | |
2656 | |
2657 count++; | |
2658 g_hash_table_insert(file_data_monitor_pool, fd, GINT_TO_POINTER(count)); | |
2659 | |
1523 | 2660 if (!realtime_monitor_id) |
791 | 2661 { |
2662 realtime_monitor_id = g_timeout_add(5000, realtime_monitor_cb, NULL); | |
2663 } | |
806 | 2664 |
791 | 2665 return TRUE; |
2666 } | |
2667 | |
1422 | 2668 gboolean file_data_unregister_real_time_monitor(FileData *fd) |
791 | 2669 { |
2670 gint count; | |
806 | 2671 |
791 | 2672 g_assert(file_data_monitor_pool); |
2673 | |
2674 count = GPOINTER_TO_INT(g_hash_table_lookup(file_data_monitor_pool, fd)); | |
2675 | |
2676 DEBUG_1("Unregister realtime %d %s", count, fd->path); | |
2677 | |
2678 g_assert(count > 0); | |
2679 | |
2680 count--; | |
2681 | |
2682 if (count == 0) | |
2683 g_hash_table_remove(file_data_monitor_pool, fd); | |
2684 else | |
2685 g_hash_table_insert(file_data_monitor_pool, fd, GINT_TO_POINTER(count)); | |
2686 | |
2687 file_data_unref(fd); | |
2688 | |
2689 if (g_hash_table_size(file_data_monitor_pool) == 0) | |
2690 { | |
2691 g_source_remove(realtime_monitor_id); | |
1523 | 2692 realtime_monitor_id = 0; |
791 | 2693 return FALSE; |
2694 } | |
806 | 2695 |
791 | 2696 return TRUE; |
2697 } | |
1055
1646720364cf
Adding a vim modeline to all files - patch by Klaus Ethgen
nadvornik
parents:
1023
diff
changeset
|
2698 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ |