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