Mercurial > geeqie.yaz
annotate src/filedata.c @ 708:444705451f3a
Use G_DIR_SEPARATOR_S instead of hardcoded "/".
author | zas_ |
---|---|
date | Tue, 20 May 2008 23:11:47 +0000 |
parents | fbebf5cf4a55 |
children | d4a68dfa6819 |
rev | line source |
---|---|
586 | 1 /* |
2 * Geeqie | |
3 * (C) 2006 John Ellis | |
4 * Copyright (C) 2008 The Geeqie Team | |
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 "rcfile.h" | |
20 #include "secure_save.h" | |
21 #include "thumb_standard.h" | |
22 #include "ui_fileops.h" | |
23 | |
24 | |
25 static gint sidecar_file_priority(const gchar *path); | |
26 | |
27 | |
28 /* | |
29 *----------------------------------------------------------------------------- | |
30 * text conversion utils | |
31 *----------------------------------------------------------------------------- | |
32 */ | |
33 | |
34 gchar *text_from_size(gint64 size) | |
35 { | |
36 gchar *a, *b; | |
37 gchar *s, *d; | |
38 gint l, n, i; | |
39 | |
40 /* what I would like to use is printf("%'d", size) | |
41 * BUT: not supported on every libc :( | |
42 */ | |
43 if (size > G_MAXUINT) | |
44 { | |
45 /* the %lld conversion is not valid in all libcs, so use a simple work-around */ | |
46 a = g_strdup_printf("%d%09d", (guint)(size / 1000000000), (guint)(size % 1000000000)); | |
47 } | |
48 else | |
49 { | |
50 a = g_strdup_printf("%d", (guint)size); | |
51 } | |
52 l = strlen(a); | |
53 n = (l - 1)/ 3; | |
54 if (n < 1) return a; | |
55 | |
56 b = g_new(gchar, l + n + 1); | |
57 | |
58 s = a; | |
59 d = b; | |
60 i = l - n * 3; | |
61 while (*s != '\0') | |
62 { | |
63 if (i < 1) | |
64 { | |
65 i = 3; | |
66 *d = ','; | |
67 d++; | |
68 } | |
69 | |
70 *d = *s; | |
71 s++; | |
72 d++; | |
73 i--; | |
74 } | |
75 *d = '\0'; | |
76 | |
77 g_free(a); | |
78 return b; | |
79 } | |
80 | |
81 gchar *text_from_size_abrev(gint64 size) | |
82 { | |
83 if (size < (gint64)1024) | |
84 { | |
85 return g_strdup_printf(_("%d bytes"), (gint)size); | |
86 } | |
87 if (size < (gint64)1048576) | |
88 { | |
89 return g_strdup_printf(_("%.1f K"), (double)size / 1024.0); | |
90 } | |
91 if (size < (gint64)1073741824) | |
92 { | |
93 return g_strdup_printf(_("%.1f MB"), (double)size / 1048576.0); | |
94 } | |
95 | |
96 /* to avoid overflowing the double, do division in two steps */ | |
97 size /= 1048576; | |
98 return g_strdup_printf(_("%.1f GB"), (double)size / 1024.0); | |
99 } | |
100 | |
101 /* note: returned string is valid until next call to text_from_time() */ | |
102 const gchar *text_from_time(time_t t) | |
103 { | |
104 static gchar *ret = NULL; | |
105 gchar buf[128]; | |
106 gint buflen; | |
107 struct tm *btime; | |
108 GError *error = NULL; | |
109 | |
110 btime = localtime(&t); | |
111 | |
112 /* the %x warning about 2 digit years is not an error */ | |
113 buflen = strftime(buf, sizeof(buf), "%x %H:%M", btime); | |
114 if (buflen < 1) return ""; | |
115 | |
116 g_free(ret); | |
117 ret = g_locale_to_utf8(buf, buflen, NULL, NULL, &error); | |
118 if (error) | |
119 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
120 log_printf("Error converting locale strftime to UTF-8: %s\n", error->message); |
586 | 121 g_error_free(error); |
122 return ""; | |
123 } | |
124 | |
125 return ret; | |
126 } | |
127 | |
128 /* | |
129 *----------------------------------------------------------------------------- | |
130 * file info struct | |
131 *----------------------------------------------------------------------------- | |
132 */ | |
133 | |
134 FileData *file_data_merge_sidecar_files(FileData *target, FileData *source); | |
135 static void file_data_check_sidecars(FileData *fd); | |
136 FileData *file_data_disconnect_sidecar_file(FileData *target, FileData *sfd); | |
137 | |
138 | |
139 static void file_data_set_path(FileData *fd, const gchar *path) | |
140 { | |
141 | |
142 if (strcmp(path, "/") == 0) | |
143 { | |
144 fd->path = g_strdup(path); | |
145 fd->name = fd->path; | |
146 fd->extension = fd->name + 1; | |
147 return; | |
148 } | |
149 | |
150 fd->path = g_strdup(path); | |
151 fd->name = filename_from_path(fd->path); | |
152 | |
153 if (strcmp(fd->name, "..") == 0) | |
154 { | |
155 gchar *dir = remove_level_from_path(path); | |
156 g_free(fd->path); | |
157 fd->path = remove_level_from_path(dir); | |
158 g_free(dir); | |
159 fd->name = ".."; | |
160 fd->extension = fd->name + 2; | |
161 return; | |
162 } | |
163 else if (strcmp(fd->name, ".") == 0) | |
164 { | |
165 g_free(fd->path); | |
166 fd->path = remove_level_from_path(path); | |
167 fd->name = "."; | |
168 fd->extension = fd->name + 1; | |
169 return; | |
170 } | |
171 | |
172 fd->extension = extension_from_path(fd->path); | |
173 if (fd->extension == NULL) | |
174 fd->extension = fd->name + strlen(fd->name); | |
175 } | |
176 | |
177 static void file_data_check_changed_files(FileData *fd, struct stat *st) | |
178 { | |
179 GList *work; | |
180 if (fd->size != st->st_size || | |
181 fd->date != st->st_mtime) | |
182 { | |
183 fd->size = st->st_size; | |
184 fd->date = st->st_mtime; | |
185 if (fd->pixbuf) g_object_unref(fd->pixbuf); | |
186 fd->pixbuf = NULL; | |
187 } | |
188 | |
189 work = fd->sidecar_files; | |
190 while (work) | |
191 { | |
192 FileData *sfd = work->data; | |
193 struct stat st; | |
194 | |
195 if (!stat_utf8(sfd->path, &st)) | |
196 { | |
197 file_data_disconnect_sidecar_file(fd, sfd); | |
198 } | |
199 | |
200 file_data_check_changed_files(sfd, &st); | |
201 work = work->next; | |
202 } | |
203 } | |
204 | |
205 static GHashTable *file_data_pool = NULL; | |
206 | |
207 static FileData *file_data_new(const gchar *path_utf8, struct stat *st, gboolean check_sidecars) | |
208 { | |
209 FileData *fd; | |
210 | |
211 DEBUG_2("file_data_new: '%s' %d", path_utf8, check_sidecars); | |
212 | |
213 if (!file_data_pool) | |
214 file_data_pool = g_hash_table_new(g_str_hash, g_str_equal); | |
215 | |
216 fd = g_hash_table_lookup(file_data_pool, path_utf8); | |
217 if (fd) | |
218 { | |
219 file_data_check_changed_files(fd, st); | |
220 DEBUG_2("file_data_pool hit: '%s'", fd->path); | |
221 return file_data_ref(fd); | |
222 } | |
223 | |
224 fd = g_new0(FileData, 1); | |
225 | |
226 file_data_set_path(fd, path_utf8); | |
227 | |
228 fd->original_path = g_strdup(path_utf8); | |
229 fd->size = st->st_size; | |
230 fd->date = st->st_mtime; | |
231 fd->pixbuf = NULL; | |
232 fd->sidecar_files = NULL; | |
233 fd->ref = 1; | |
234 fd->magick = 0x12345678; | |
235 | |
236 g_hash_table_insert(file_data_pool, fd->original_path, fd); | |
237 | |
238 if (check_sidecars && sidecar_file_priority(fd->extension)) | |
239 file_data_check_sidecars(fd); | |
240 return fd; | |
241 } | |
242 | |
243 static void file_data_check_sidecars(FileData *fd) | |
244 { | |
245 int base_len = fd->extension - fd->path; | |
246 GString *fname = g_string_new_len(fd->path, base_len); | |
247 FileData *parent_fd = NULL; | |
248 GList *work = sidecar_ext_get_list(); | |
249 while (work) | |
250 { | |
251 /* check for possible sidecar files; | |
252 the sidecar files created here are referenced only via fd->sidecar_files or fd->parent, | |
253 they have fd->ref set to 0 and file_data unref must chack and free them all together | |
254 (using fd->ref would cause loops and leaks) | |
255 */ | |
256 | |
257 FileData *new_fd; | |
258 | |
259 gchar *ext = work->data; | |
260 work = work->next; | |
261 | |
262 if (strcmp(ext, fd->extension) == 0) | |
263 { | |
264 new_fd = fd; /* processing the original file */ | |
265 } | |
266 else | |
267 { | |
268 struct stat nst; | |
269 g_string_truncate(fname, base_len); | |
270 g_string_append(fname, ext); | |
271 | |
272 if (!stat_utf8(fname->str, &nst)) | |
273 continue; | |
274 | |
275 new_fd = file_data_new(fname->str, &nst, FALSE); | |
276 new_fd->ref--; /* do not use ref here */ | |
277 } | |
278 | |
279 if (!parent_fd) | |
280 parent_fd = new_fd; /* parent is the one with the highest prio, found first */ | |
281 else | |
282 file_data_merge_sidecar_files(parent_fd, new_fd); | |
283 } | |
284 g_string_free(fname, TRUE); | |
285 } | |
286 | |
287 | |
288 static FileData *file_data_new_local(const gchar *path, struct stat *st, gboolean check_sidecars) | |
289 { | |
290 gchar *path_utf8 = path_to_utf8(path); | |
291 FileData *ret = file_data_new(path_utf8, st, check_sidecars); | |
292 g_free(path_utf8); | |
293 return ret; | |
294 } | |
295 | |
296 FileData *file_data_new_simple(const gchar *path_utf8) | |
297 { | |
298 struct stat st; | |
299 | |
300 if (!stat_utf8(path_utf8, &st)) | |
301 { | |
302 st.st_size = 0; | |
303 st.st_mtime = 0; | |
304 } | |
305 | |
306 return file_data_new(path_utf8, &st, TRUE); | |
307 } | |
308 | |
309 FileData *file_data_add_sidecar_file(FileData *target, FileData *sfd) | |
310 { | |
311 sfd->parent = target; | |
312 if(!g_list_find(target->sidecar_files, sfd)) | |
313 target->sidecar_files = g_list_prepend(target->sidecar_files, sfd); | |
314 return target; | |
315 } | |
316 | |
317 | |
318 FileData *file_data_merge_sidecar_files(FileData *target, FileData *source) | |
319 { | |
320 GList *work; | |
321 file_data_add_sidecar_file(target, source); | |
322 | |
323 work = source->sidecar_files; | |
324 while (work) | |
325 { | |
326 FileData *sfd = work->data; | |
327 file_data_add_sidecar_file(target, sfd); | |
328 work = work->next; | |
329 } | |
330 | |
331 g_list_free(source->sidecar_files); | |
332 source->sidecar_files = NULL; | |
333 | |
334 target->sidecar_files = filelist_sort(target->sidecar_files, SORT_NAME, TRUE); | |
335 return target; | |
336 } | |
337 | |
338 | |
339 | |
340 FileData *file_data_ref(FileData *fd) | |
341 { | |
342 if (fd == NULL) return NULL; | |
343 | |
344 // return g_memdup(fd, sizeof(FileData)); | |
345 g_assert(fd->magick == 0x12345678); | |
346 fd->ref++; | |
347 return fd; | |
348 } | |
349 | |
350 static void file_data_free(FileData *fd) | |
351 { | |
352 g_assert(fd->magick == 0x12345678); | |
353 g_assert(fd->ref == 0); | |
354 | |
355 g_hash_table_remove(file_data_pool, fd->original_path); | |
356 | |
357 g_free(fd->path); | |
358 g_free(fd->original_path); | |
359 if (fd->pixbuf) g_object_unref(fd->pixbuf); | |
360 | |
361 | |
362 g_assert(fd->sidecar_files == NULL); /* sidecar files must be freed before calling this */ | |
363 | |
364 file_data_change_info_free(NULL, fd); | |
365 g_free(fd); | |
366 } | |
367 | |
368 void file_data_unref(FileData *fd) | |
369 { | |
370 if (fd == NULL) return; | |
371 g_assert(fd->magick == 0x12345678); | |
372 | |
373 fd->ref--; | |
374 DEBUG_2("file_data_unref (%d): '%s'", fd->ref, fd->path); | |
375 | |
376 if (fd->ref == 0) | |
377 { | |
378 FileData *parent = fd->parent ? fd->parent : fd; | |
379 | |
380 GList *work; | |
381 | |
382 if (parent->ref > 0) | |
383 return; | |
384 | |
385 work = parent->sidecar_files; | |
386 while (work) | |
387 { | |
388 FileData *sfd = work->data; | |
389 if (sfd->ref > 0) | |
390 return; | |
391 work = work->next; | |
392 } | |
393 | |
394 /* none of parent/children is referenced, we can free everything */ | |
395 | |
396 DEBUG_2("file_data_unref: deleting '%s', parent '%s'", fd->path, parent->path); | |
397 | |
398 work = parent->sidecar_files; | |
399 while (work) | |
400 { | |
401 FileData *sfd = work->data; | |
402 file_data_free(sfd); | |
403 work = work->next; | |
404 } | |
405 | |
406 g_list_free(parent->sidecar_files); | |
407 parent->sidecar_files = NULL; | |
408 | |
409 file_data_free(parent); | |
410 | |
411 } | |
412 } | |
413 | |
414 FileData *file_data_disconnect_sidecar_file(FileData *target, FileData *sfd) | |
415 { | |
416 sfd->parent = target; | |
417 g_assert(g_list_find(target->sidecar_files, sfd)); | |
418 | |
419 target->sidecar_files = g_list_remove(target->sidecar_files, sfd); | |
420 sfd->parent = NULL; | |
421 | |
422 if (sfd->ref == 0) { | |
423 file_data_free(sfd); | |
424 return NULL; | |
425 } | |
426 | |
427 return sfd; | |
428 } | |
429 | |
430 /* compare name without extension */ | |
431 gint file_data_compare_name_without_ext(FileData *fd1, FileData *fd2) | |
432 { | |
433 size_t len1 = fd1->extension - fd1->name; | |
434 size_t len2 = fd2->extension - fd2->name; | |
435 | |
436 if (len1 < len2) return -1; | |
437 if (len1 > len2) return 1; | |
438 | |
439 return strncmp(fd1->name, fd2->name, len1); | |
440 } | |
441 | |
442 gboolean file_data_add_change_info(FileData *fd, FileDataChangeType type, const gchar *src, const gchar *dest) | |
443 { | |
444 | |
445 FileDataChangeInfo *fdci; | |
446 | |
447 if (fd->change) return FALSE; | |
448 | |
449 fdci = g_new0(FileDataChangeInfo, 1); | |
450 | |
451 fdci->type = type; | |
452 | |
453 if (src) | |
454 fdci->source = g_strdup(src); | |
455 else | |
456 fdci->source = g_strdup(fd->path); | |
457 | |
458 if (dest) | |
459 fdci->dest = g_strdup(dest); | |
460 | |
461 fd->change = fdci; | |
462 return TRUE; | |
463 } | |
464 | |
465 void file_data_change_info_free(FileDataChangeInfo *fdci, FileData *fd) | |
466 { | |
467 if (!fdci && fd) | |
468 fdci = fd->change; | |
469 | |
470 if (!fdci) | |
471 return; | |
472 | |
473 g_free(fdci->source); | |
474 g_free(fdci->dest); | |
475 | |
476 g_free(fdci); | |
477 | |
478 if (fd) | |
479 fd->change = NULL; | |
480 } | |
481 | |
482 | |
483 | |
484 | |
485 /* | |
486 *----------------------------------------------------------------------------- | |
487 * sidecar file info struct | |
488 *----------------------------------------------------------------------------- | |
489 */ | |
490 | |
491 | |
492 | |
493 static gint sidecar_file_priority(const gchar *path) | |
494 { | |
495 const char *extension = extension_from_path(path); | |
496 int i = 1; | |
497 GList *work; | |
498 if (extension == NULL) | |
499 return 0; | |
500 | |
501 work = sidecar_ext_get_list(); | |
502 | |
503 while (work) { | |
504 gchar *ext = work->data; | |
505 work = work->next; | |
506 if (strcmp(extension, ext) == 0) return i; | |
507 i++; | |
508 } | |
509 return 0; | |
510 } | |
511 | |
512 | |
513 /* | |
514 *----------------------------------------------------------------------------- | |
515 * load file list | |
516 *----------------------------------------------------------------------------- | |
517 */ | |
518 | |
519 static SortType filelist_sort_method = SORT_NONE; | |
520 static gint filelist_sort_ascend = TRUE; | |
521 | |
522 | |
523 gint filelist_sort_compare_filedata(FileData *fa, FileData *fb) | |
524 { | |
525 if (!filelist_sort_ascend) | |
526 { | |
527 FileData *tmp = fa; | |
528 fa = fb; | |
529 fb = tmp; | |
530 } | |
531 | |
532 switch (filelist_sort_method) | |
533 { | |
534 case SORT_SIZE: | |
535 if (fa->size < fb->size) return -1; | |
536 if (fa->size > fb->size) return 1; | |
537 return CASE_SORT(fa->name, fb->name); /* fall back to name */ | |
538 break; | |
539 case SORT_TIME: | |
540 if (fa->date < fb->date) return -1; | |
541 if (fa->date > fb->date) return 1; | |
542 return CASE_SORT(fa->name, fb->name); /* fall back to name */ | |
543 break; | |
544 #ifdef HAVE_STRVERSCMP | |
545 case SORT_NUMBER: | |
546 return strverscmp(fa->name, fb->name); | |
547 break; | |
548 #endif | |
549 case SORT_NAME: | |
550 default: | |
551 return CASE_SORT(fa->name, fb->name); | |
552 break; | |
553 } | |
554 } | |
555 | |
556 gint filelist_sort_compare_filedata_full(FileData *fa, FileData *fb, SortType method, gint ascend) | |
557 { | |
558 filelist_sort_method = method; | |
559 filelist_sort_ascend = ascend; | |
560 return filelist_sort_compare_filedata(fa, fb); | |
561 } | |
562 | |
563 static gint filelist_sort_file_cb(void *a, void *b) | |
564 { | |
565 return filelist_sort_compare_filedata(a, b); | |
566 } | |
567 | |
568 GList *filelist_sort_full(GList *list, SortType method, gint ascend, GCompareFunc cb) | |
569 { | |
570 filelist_sort_method = method; | |
571 filelist_sort_ascend = ascend; | |
572 return g_list_sort(list, cb); | |
573 } | |
574 | |
575 GList *filelist_insert_sort_full(GList *list, void *data, SortType method, gint ascend, GCompareFunc cb) | |
576 { | |
577 filelist_sort_method = method; | |
578 filelist_sort_ascend = ascend; | |
579 return g_list_insert_sorted(list, data, cb); | |
580 } | |
581 | |
582 GList *filelist_sort(GList *list, SortType method, gint ascend) | |
583 { | |
584 return filelist_sort_full(list, method, ascend, (GCompareFunc) filelist_sort_file_cb); | |
585 } | |
586 | |
587 GList *filelist_insert_sort(GList *list, FileData *fd, SortType method, gint ascend) | |
588 { | |
589 return filelist_insert_sort_full(list, fd, method, ascend, (GCompareFunc) filelist_sort_file_cb); | |
590 } | |
591 | |
592 | |
593 static GList *filelist_filter_out_sidecars(GList *flist) | |
594 { | |
595 GList *work = flist; | |
596 GList *flist_filtered = NULL; | |
597 | |
598 while (work) | |
599 { | |
600 FileData *fd = work->data; | |
601 work = work->next; | |
602 if (fd->parent) /* remove fd's that are children */ | |
603 file_data_unref(fd); | |
604 else | |
605 flist_filtered = g_list_prepend(flist_filtered, fd); | |
606 } | |
607 g_list_free(flist); | |
608 return flist_filtered; | |
609 } | |
610 | |
611 static gint filelist_read_real(const gchar *path, GList **files, GList **dirs, gint follow_symlinks) | |
612 { | |
613 DIR *dp; | |
614 struct dirent *dir; | |
615 struct stat ent_sbuf; | |
616 gchar *pathl; | |
617 GList *dlist; | |
618 GList *flist; | |
619 | |
620 dlist = NULL; | |
621 flist = NULL; | |
622 | |
623 pathl = path_from_utf8(path); | |
624 if (!pathl || (dp = opendir(pathl)) == NULL) | |
625 { | |
626 g_free(pathl); | |
627 if (files) *files = NULL; | |
628 if (dirs) *dirs = NULL; | |
629 return FALSE; | |
630 } | |
631 | |
632 /* root dir fix */ | |
633 if (pathl[0] == '/' && pathl[1] == '\0') | |
634 { | |
635 g_free(pathl); | |
636 pathl = g_strdup(""); | |
637 } | |
638 | |
639 while ((dir = readdir(dp)) != NULL) | |
640 { | |
641 gchar *name = dir->d_name; | |
642 if (options->file_filter.show_hidden_files || !ishidden(name)) | |
643 { | |
644 gchar *filepath = g_strconcat(pathl, "/", name, NULL); | |
645 if ((follow_symlinks ? | |
646 stat(filepath, &ent_sbuf) : | |
647 lstat(filepath, &ent_sbuf)) >= 0) | |
648 { | |
649 if (S_ISDIR(ent_sbuf.st_mode)) | |
650 { | |
651 /* we ignore the .thumbnails dir for cleanliness */ | |
652 if ((dirs) && | |
653 !(name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) && | |
654 strcmp(name, GQ_CACHE_LOCAL_THUMB) != 0 && | |
655 strcmp(name, GQ_CACHE_LOCAL_METADATA) != 0 && | |
656 strcmp(name, THUMB_FOLDER_LOCAL) != 0) | |
657 { | |
658 dlist = g_list_prepend(dlist, file_data_new_local(filepath, &ent_sbuf, FALSE)); | |
659 } | |
660 } | |
661 else | |
662 { | |
663 if ((files) && filter_name_exists(name)) | |
664 { | |
665 flist = g_list_prepend(flist, file_data_new_local(filepath, &ent_sbuf, TRUE)); | |
666 } | |
667 } | |
668 } | |
669 g_free(filepath); | |
670 } | |
671 } | |
672 | |
673 closedir(dp); | |
674 | |
675 g_free(pathl); | |
676 | |
677 flist = filelist_filter_out_sidecars(flist); | |
678 | |
679 if (dirs) *dirs = dlist; | |
680 if (files) *files = flist; | |
681 | |
682 return TRUE; | |
683 } | |
684 | |
685 gint filelist_read(const gchar *path, GList **files, GList **dirs) | |
686 { | |
687 return filelist_read_real(path, files, dirs, TRUE); | |
688 } | |
689 | |
690 gint filelist_read_lstat(const gchar *path, GList **files, GList **dirs) | |
691 { | |
692 return filelist_read_real(path, files, dirs, FALSE); | |
693 } | |
694 | |
695 void filelist_free(GList *list) | |
696 { | |
697 GList *work; | |
698 | |
699 work = list; | |
700 while (work) | |
701 { | |
702 file_data_unref((FileData *)work->data); | |
703 work = work->next; | |
704 } | |
705 | |
706 g_list_free(list); | |
707 } | |
708 | |
709 | |
710 GList *filelist_copy(GList *list) | |
711 { | |
712 GList *new_list = NULL; | |
713 GList *work; | |
714 | |
715 work = list; | |
716 while (work) | |
717 { | |
718 FileData *fd; | |
719 | |
720 fd = work->data; | |
721 work = work->next; | |
722 | |
723 new_list = g_list_prepend(new_list, file_data_ref(fd)); | |
724 } | |
725 | |
726 return g_list_reverse(new_list); | |
727 } | |
728 | |
729 GList *filelist_from_path_list(GList *list) | |
730 { | |
731 GList *new_list = NULL; | |
732 GList *work; | |
733 | |
734 work = list; | |
735 while (work) | |
736 { | |
737 gchar *path; | |
738 | |
739 path = work->data; | |
740 work = work->next; | |
741 | |
742 new_list = g_list_prepend(new_list, file_data_new_simple(path)); | |
743 } | |
744 | |
745 return g_list_reverse(new_list); | |
746 } | |
747 | |
748 GList *filelist_to_path_list(GList *list) | |
749 { | |
750 GList *new_list = NULL; | |
751 GList *work; | |
752 | |
753 work = list; | |
754 while (work) | |
755 { | |
756 FileData *fd; | |
757 | |
758 fd = work->data; | |
759 work = work->next; | |
760 | |
761 new_list = g_list_prepend(new_list, g_strdup(fd->path)); | |
762 } | |
763 | |
764 return g_list_reverse(new_list); | |
765 } | |
766 | |
767 GList *filelist_filter(GList *list, gint is_dir_list) | |
768 { | |
769 GList *work; | |
770 | |
771 if (!is_dir_list && options->file_filter.disable && options->file_filter.show_hidden_files) return list; | |
772 | |
773 work = list; | |
774 while (work) | |
775 { | |
776 FileData *fd = (FileData *)(work->data); | |
777 const gchar *name = fd->name; | |
778 | |
779 if ((!options->file_filter.show_hidden_files && ishidden(name)) || | |
780 (!is_dir_list && !filter_name_exists(name)) || | |
781 (is_dir_list && name[0] == '.' && (strcmp(name, GQ_CACHE_LOCAL_THUMB) == 0 || | |
782 strcmp(name, GQ_CACHE_LOCAL_METADATA) == 0)) ) | |
783 { | |
784 GList *link = work; | |
785 work = work->next; | |
786 list = g_list_remove_link(list, link); | |
787 file_data_unref(fd); | |
788 g_list_free(link); | |
789 } | |
790 else | |
791 { | |
792 work = work->next; | |
793 } | |
794 } | |
795 | |
796 return list; | |
797 } | |
798 | |
799 /* | |
800 *----------------------------------------------------------------------------- | |
801 * filelist recursive | |
802 *----------------------------------------------------------------------------- | |
803 */ | |
804 | |
805 static gint filelist_sort_path_cb(gconstpointer a, gconstpointer b) | |
806 { | |
807 return CASE_SORT(((FileData *)a)->path, ((FileData *)b)->path); | |
808 } | |
809 | |
810 GList *filelist_sort_path(GList *list) | |
811 { | |
812 return g_list_sort(list, filelist_sort_path_cb); | |
813 } | |
814 | |
815 static void filelist_recursive_append(GList **list, GList *dirs) | |
816 { | |
817 GList *work; | |
818 | |
819 work = dirs; | |
820 while (work) | |
821 { | |
822 FileData *fd = (FileData *)(work->data); | |
823 const gchar *path = fd->path; | |
824 GList *f = NULL; | |
825 GList *d = NULL; | |
826 | |
827 if (filelist_read(path, &f, &d)) | |
828 { | |
829 f = filelist_filter(f, FALSE); | |
830 f = filelist_sort_path(f); | |
831 *list = g_list_concat(*list, f); | |
832 | |
833 d = filelist_filter(d, TRUE); | |
834 d = filelist_sort_path(d); | |
835 filelist_recursive_append(list, d); | |
836 filelist_free(d); | |
837 } | |
838 | |
839 work = work->next; | |
840 } | |
841 } | |
842 | |
843 GList *filelist_recursive(const gchar *path) | |
844 { | |
845 GList *list = NULL; | |
846 GList *d = NULL; | |
847 | |
848 if (!filelist_read(path, &list, &d)) return NULL; | |
849 list = filelist_filter(list, FALSE); | |
850 list = filelist_sort_path(list); | |
851 | |
852 d = filelist_filter(d, TRUE); | |
853 d = filelist_sort_path(d); | |
854 filelist_recursive_append(&list, d); | |
855 filelist_free(d); | |
856 | |
857 return list; | |
858 } | |
590 | 859 |
860 | |
861 | |
862 /* | |
863 * file_data - operates on the given fd | |
864 * file_data_sc - operates on the given fd + sidecars - all fds linked via fd->sidecar_files or fd->parent | |
865 */ | |
866 | |
867 | |
868 /* return list of sidecar file extensions in a string */ | |
596 | 869 gchar *file_data_sc_list_to_string(FileData *fd) |
870 { | |
871 GList *work; | |
872 GString *result = g_string_new(""); | |
873 | |
874 work = fd->sidecar_files; | |
875 while (work) | |
876 { | |
877 FileData *sfd = work->data; | |
878 result = g_string_append(result, "+ "); | |
879 result = g_string_append(result, sfd->extension); | |
880 work = work->next; | |
881 if (work) result = g_string_append_c(result, ' '); | |
882 } | |
883 | |
884 return g_string_free(result, FALSE); | |
885 } | |
590 | 886 |
887 | |
888 /* disables / enables grouping for particular file, sends UPDATE notification */ | |
889 void file_data_disable_grouping(FileData *fd); // now file_data_disconnect_sidecar_file, broken | |
890 void file_data_disable_grouping(FileData *fd); | |
891 | |
892 /* runs stat on a file and sends UPDATE notification if it has been changed */ | |
893 void file_data_sc_update(FileData *fd); | |
894 | |
895 | |
896 | |
897 | |
898 /* | |
899 * add FileDataChangeInfo (see typedefs.h) for the given operation | |
900 * uses file_data_add_change_info | |
901 * | |
902 * fails if the fd->change already exists - change operations can't run in parallel | |
903 * fd->change_info works as a lock | |
904 * | |
905 * dest can be NULL - in this case the current name is used for now, it will | |
906 * be changed later | |
907 */ | |
908 | |
909 /* | |
910 FileDataChangeInfo types: | |
911 COPY | |
912 MOVE - patch is changed, name may be changed too | |
913 RENAME - path remains unchanged, name is changed | |
914 extension should remain (FIXME should we allow editing extension? it will make problems wth grouping) | |
915 sidecar names are changed too, extensions are not changed | |
916 DELETE | |
917 UPDATE - file size, date or grouping has been changed | |
918 */ | |
919 | |
920 gboolean file_data_add_ci(FileData *fd, FileDataChangeType type, const gchar *src, const gchar *dest) | |
921 { | |
922 | |
923 FileDataChangeInfo *fdci; | |
924 | |
925 if (fd->change) return FALSE; | |
926 | |
927 fdci = g_new0(FileDataChangeInfo, 1); | |
928 | |
929 fdci->type = type; | |
930 | |
931 if (src) | |
932 fdci->source = g_strdup(src); | |
933 else | |
934 fdci->source = g_strdup(fd->path); | |
935 | |
936 if (dest) | |
937 fdci->dest = g_strdup(dest); | |
938 | |
939 fd->change = fdci; | |
940 | |
941 return TRUE; | |
942 } | |
943 | |
944 void file_data_free_ci(FileData *fd) | |
945 { | |
946 FileDataChangeInfo *fdci = fd->change; | |
947 | |
948 if (!fdci) | |
949 return; | |
950 | |
951 g_free(fdci->source); | |
952 g_free(fdci->dest); | |
953 | |
954 g_free(fdci); | |
955 | |
956 fd->change = NULL; | |
957 } | |
958 | |
959 | |
960 static gboolean file_data_sc_add_ci(FileData *fd, FileDataChangeType type) | |
961 { | |
962 GList *work; | |
963 if (fd->parent) fd = fd->parent; | |
964 | |
965 if (fd->change) return FALSE; | |
966 work = fd->sidecar_files; | |
967 while (work) | |
968 { | |
969 FileData *sfd = work->data; | |
970 if (sfd->change) return FALSE; | |
971 work = work->next; | |
972 } | |
973 | |
974 file_data_add_ci(fd, type, NULL, NULL); | |
975 | |
976 work = fd->sidecar_files; | |
977 while (work) | |
978 { | |
979 FileData *sfd = work->data; | |
980 file_data_add_ci(sfd, type, NULL, NULL); | |
981 work = work->next; | |
982 } | |
983 | |
984 return TRUE; | |
985 } | |
986 | |
987 static gboolean file_data_sc_check_ci(FileData *fd, FileDataChangeType type) | |
988 { | |
989 GList *work; | |
990 if (fd->parent) fd = fd->parent; | |
991 | |
992 if (!fd->change) return FALSE; | |
993 if (fd->change->type != type) return FALSE; | |
994 work = fd->sidecar_files; | |
995 while (work) | |
996 { | |
997 FileData *sfd = work->data; | |
998 if (!sfd->change) return FALSE; | |
999 if (sfd->change->type != type) return FALSE; | |
1000 work = work->next; | |
1001 } | |
1002 return TRUE; | |
1003 } | |
1004 | |
1005 | |
1006 gboolean file_data_sc_add_ci_copy(FileData *fd, gchar *dest_path) | |
1007 { | |
1008 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_COPY)) return FALSE; | |
1009 file_data_sc_update_ci_copy(fd, dest_path); | |
1010 return TRUE; | |
1011 } | |
1012 | |
1013 gboolean file_data_sc_add_ci_move(FileData *fd, gchar *dest_path) | |
1014 { | |
1015 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_MOVE)) return FALSE; | |
1016 file_data_sc_update_ci_move(fd, dest_path); | |
1017 return TRUE; | |
1018 } | |
1019 | |
1020 gboolean file_data_sc_add_ci_rename(FileData *fd, gchar *dest_path) | |
1021 { | |
1022 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_RENAME)) return FALSE; | |
1023 file_data_sc_update_ci_rename(fd, dest_path); | |
1024 return TRUE; | |
1025 } | |
1026 | |
1027 gboolean file_data_sc_add_ci_delete(FileData *fd) | |
1028 { | |
1029 return file_data_sc_add_ci(fd, FILEDATA_CHANGE_DELETE); | |
1030 } | |
1031 | |
1032 gboolean file_data_sc_add_ci_update(FileData *fd) | |
1033 { | |
1034 return file_data_sc_add_ci(fd, FILEDATA_CHANGE_UPDATE); | |
1035 } | |
1036 | |
1037 void file_data_sc_free_ci(FileData *fd) | |
1038 { | |
1039 GList *work; | |
1040 if (fd->parent) fd = fd->parent; | |
1041 | |
1042 file_data_free_ci(fd); | |
1043 | |
1044 work = fd->sidecar_files; | |
1045 while (work) | |
1046 { | |
1047 FileData *sfd = work->data; | |
1048 file_data_free_ci(sfd); | |
1049 work = work->next; | |
1050 } | |
1051 } | |
1052 | |
1053 | |
1054 /* | |
1055 * update existing fd->change, it will be used from dialog callbacks for interactive editing | |
1056 * fails if fd->change does not exist or the change type does not match | |
1057 */ | |
1058 | |
1059 static void file_data_update_ci_dest(FileData *fd, gchar *dest_path) | |
1060 { | |
1061 g_free(fd->change->dest); | |
1062 fd->change->dest = g_strdup(dest_path); | |
1063 } | |
1064 | |
1065 static void file_data_update_ci_dest_preserve_ext(FileData *fd, gchar *dest_path) | |
1066 { | |
1067 const char *extension = extension_from_path(fd->change->source); | |
1068 g_free(fd->change->dest); | |
1069 fd->change->dest = g_strdup_printf("%*s%s", (int)(extension_from_path(dest_path) - dest_path), dest_path, extension); | |
1070 } | |
1071 | |
1072 static void file_data_sc_update_ci(FileData *fd, gchar *dest_path) | |
1073 { | |
1074 GList *work; | |
1075 if (fd->parent) fd = fd->parent; | |
1076 | |
1077 file_data_update_ci_dest(fd, dest_path); | |
1078 work = fd->sidecar_files; | |
1079 while (work) | |
1080 { | |
1081 FileData *sfd = work->data; | |
1082 file_data_update_ci_dest_preserve_ext(sfd, dest_path); | |
1083 work = work->next; | |
1084 } | |
1085 } | |
1086 | |
1087 gint file_data_sc_update_ci_copy(FileData *fd, gchar *dest_path) | |
1088 { | |
1089 if (!file_data_sc_check_ci(fd, FILEDATA_CHANGE_COPY)) return FALSE; | |
1090 file_data_sc_update_ci(fd, dest_path); | |
1091 return TRUE; | |
1092 } | |
1093 | |
1094 gint file_data_sc_update_ci_move(FileData *fd, gchar *dest_path) | |
1095 { | |
1096 if (!file_data_sc_check_ci(fd, FILEDATA_CHANGE_MOVE)) return FALSE; | |
1097 file_data_sc_update_ci(fd, dest_path); | |
1098 return TRUE; | |
1099 } | |
1100 | |
1101 gint file_data_sc_update_ci_rename(FileData *fd, gchar *dest_path) | |
1102 { | |
1103 if (!file_data_sc_check_ci(fd, FILEDATA_CHANGE_RENAME)) return FALSE; | |
1104 file_data_sc_update_ci(fd, dest_path); | |
1105 return TRUE; | |
1106 } | |
1107 | |
1108 | |
1109 | |
1110 /* | |
1111 * check dest paths - dest image exists, etc. | |
1112 * returns FIXME | |
1113 * it should detect all possible problems with the planned operation | |
1114 */ | |
1115 | |
1116 gint file_data_sc_check_ci_dest(FileData *fd) | |
1117 { | |
1118 } | |
1119 | |
1120 | |
1121 | |
1122 | |
1123 /* | |
1124 * perform the change described by FileFataChangeInfo | |
1125 * it is used for internal operations, | |
1126 * this function actually operates with files on the filesystem | |
1127 * it should implement safe delete | |
1128 */ | |
1129 | |
1130 static gboolean file_data_perform_move(FileData *fd) | |
1131 { | |
1132 g_assert(!strcmp(fd->change->source, fd->path)); | |
1133 return move_file(fd->change->source, fd->change->dest); | |
1134 } | |
1135 | |
1136 static gboolean file_data_perform_copy(FileData *fd) | |
1137 { | |
1138 g_assert(!strcmp(fd->change->source, fd->path)); | |
1139 return copy_file(fd->change->source, fd->change->dest); | |
1140 } | |
1141 | |
1142 static gboolean file_data_perform_delete(FileData *fd) | |
1143 { | |
1144 return unlink_file(fd->path); | |
1145 } | |
1146 | |
1147 static gboolean file_data_perform_ci(FileData *fd) | |
1148 { | |
1149 FileDataChangeType type = fd->change->type; | |
1150 switch (type) | |
1151 { | |
1152 case FILEDATA_CHANGE_MOVE: | |
1153 return file_data_perform_move(fd); | |
1154 case FILEDATA_CHANGE_COPY: | |
1155 return file_data_perform_copy(fd); | |
1156 case FILEDATA_CHANGE_RENAME: | |
1157 return file_data_perform_move(fd); /* the same as move */ | |
1158 case FILEDATA_CHANGE_DELETE: | |
1159 return file_data_perform_delete(fd); | |
1160 case FILEDATA_CHANGE_UPDATE: | |
596 | 1161 /* nothing to do here */ |
590 | 1162 break; |
1163 } | |
1164 return TRUE; | |
1165 } | |
1166 | |
1167 | |
1168 | |
1169 gboolean file_data_sc_perform_ci(FileData *fd) | |
1170 { | |
1171 GList *work; | |
1172 gboolean ret = TRUE; | |
1173 FileDataChangeType type = fd->change->type; | |
1174 if (!file_data_sc_check_ci(fd, type)) return FALSE; | |
1175 | |
1176 work = fd->sidecar_files; | |
1177 while (work) | |
1178 { | |
1179 FileData *sfd = work->data; | |
1180 if (!file_data_perform_ci(sfd)) ret = FALSE; | |
1181 work = work->next; | |
1182 } | |
1183 if (!file_data_perform_ci(fd)) ret = FALSE; | |
1184 return ret; | |
1185 } | |
1186 | |
1187 /* | |
1188 * updates FileData structure according to FileDataChangeInfo | |
1189 */ | |
1190 | |
1191 static void file_data_apply_ci(FileData *fd) | |
1192 { | |
1193 FileDataChangeType type = fd->change->type; | |
1194 /* FIXME delete ?*/ | |
1195 if (type == FILEDATA_CHANGE_MOVE || type == FILEDATA_CHANGE_COPY || type == FILEDATA_CHANGE_RENAME) | |
1196 { | |
1197 g_free(fd->path); | |
1198 g_hash_table_remove(file_data_pool, fd->original_path); | |
1199 g_free(fd->original_path); | |
1200 file_data_set_path(fd, fd->change->dest); | |
1201 fd->original_path = g_strdup(fd->change->dest); | |
1202 g_hash_table_insert(file_data_pool, fd->original_path, fd); | |
1203 } | |
1204 } | |
1205 | |
596 | 1206 gint file_data_sc_apply_ci(FileData *fd) |
590 | 1207 { |
1208 GList *work; | |
1209 FileDataChangeType type = fd->change->type; | |
1210 if (!file_data_sc_check_ci(fd, type)) return FALSE; | |
1211 | |
1212 work = fd->sidecar_files; | |
1213 while (work) | |
1214 { | |
1215 FileData *sfd = work->data; | |
1216 file_data_apply_ci(sfd); | |
1217 work = work->next; | |
1218 } | |
1219 file_data_apply_ci(fd); | |
1220 return TRUE; | |
1221 } | |
1222 | |
1223 | |
1224 /* | |
1225 * notify other modules about the change described by FileFataChangeInfo | |
1226 */ | |
1227 | |
1228 /* might use file_maint_ functions for now, later it should be changed to a system of callbacks | |
1229 FIXME do we need the ignore_list? It looks like a workaround for ineffective | |
1230 implementation in view_file_list.c */ | |
1231 | |
1232 void file_data_sc_send_notification(FileData *fd) | |
1233 { | |
1234 } | |
1235 | |
1236 |