Mercurial > geeqie.yaz
comparison src/filedata.c @ 586:905688aa2317
split filelist.c to filefilter.c and filedata.c
author | nadvornik |
---|---|
date | Mon, 05 May 2008 19:11:12 +0000 |
parents | |
children | 2b7b966f61cf |
comparison
equal
deleted
inserted
replaced
585:4875d67823b4 | 586:905688aa2317 |
---|---|
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 "debug.h" | |
20 #include "rcfile.h" | |
21 #include "secure_save.h" | |
22 #include "thumb_standard.h" | |
23 #include "ui_fileops.h" | |
24 | |
25 | |
26 static gint sidecar_file_priority(const gchar *path); | |
27 | |
28 | |
29 /* | |
30 *----------------------------------------------------------------------------- | |
31 * text conversion utils | |
32 *----------------------------------------------------------------------------- | |
33 */ | |
34 | |
35 gchar *text_from_size(gint64 size) | |
36 { | |
37 gchar *a, *b; | |
38 gchar *s, *d; | |
39 gint l, n, i; | |
40 | |
41 /* what I would like to use is printf("%'d", size) | |
42 * BUT: not supported on every libc :( | |
43 */ | |
44 if (size > G_MAXUINT) | |
45 { | |
46 /* the %lld conversion is not valid in all libcs, so use a simple work-around */ | |
47 a = g_strdup_printf("%d%09d", (guint)(size / 1000000000), (guint)(size % 1000000000)); | |
48 } | |
49 else | |
50 { | |
51 a = g_strdup_printf("%d", (guint)size); | |
52 } | |
53 l = strlen(a); | |
54 n = (l - 1)/ 3; | |
55 if (n < 1) return a; | |
56 | |
57 b = g_new(gchar, l + n + 1); | |
58 | |
59 s = a; | |
60 d = b; | |
61 i = l - n * 3; | |
62 while (*s != '\0') | |
63 { | |
64 if (i < 1) | |
65 { | |
66 i = 3; | |
67 *d = ','; | |
68 d++; | |
69 } | |
70 | |
71 *d = *s; | |
72 s++; | |
73 d++; | |
74 i--; | |
75 } | |
76 *d = '\0'; | |
77 | |
78 g_free(a); | |
79 return b; | |
80 } | |
81 | |
82 gchar *text_from_size_abrev(gint64 size) | |
83 { | |
84 if (size < (gint64)1024) | |
85 { | |
86 return g_strdup_printf(_("%d bytes"), (gint)size); | |
87 } | |
88 if (size < (gint64)1048576) | |
89 { | |
90 return g_strdup_printf(_("%.1f K"), (double)size / 1024.0); | |
91 } | |
92 if (size < (gint64)1073741824) | |
93 { | |
94 return g_strdup_printf(_("%.1f MB"), (double)size / 1048576.0); | |
95 } | |
96 | |
97 /* to avoid overflowing the double, do division in two steps */ | |
98 size /= 1048576; | |
99 return g_strdup_printf(_("%.1f GB"), (double)size / 1024.0); | |
100 } | |
101 | |
102 /* note: returned string is valid until next call to text_from_time() */ | |
103 const gchar *text_from_time(time_t t) | |
104 { | |
105 static gchar *ret = NULL; | |
106 gchar buf[128]; | |
107 gint buflen; | |
108 struct tm *btime; | |
109 GError *error = NULL; | |
110 | |
111 btime = localtime(&t); | |
112 | |
113 /* the %x warning about 2 digit years is not an error */ | |
114 buflen = strftime(buf, sizeof(buf), "%x %H:%M", btime); | |
115 if (buflen < 1) return ""; | |
116 | |
117 g_free(ret); | |
118 ret = g_locale_to_utf8(buf, buflen, NULL, NULL, &error); | |
119 if (error) | |
120 { | |
121 printf("Error converting locale strftime to UTF-8: %s\n", error->message); | |
122 g_error_free(error); | |
123 return ""; | |
124 } | |
125 | |
126 return ret; | |
127 } | |
128 | |
129 /* | |
130 *----------------------------------------------------------------------------- | |
131 * file info struct | |
132 *----------------------------------------------------------------------------- | |
133 */ | |
134 | |
135 FileData *file_data_merge_sidecar_files(FileData *target, FileData *source); | |
136 static void file_data_check_sidecars(FileData *fd); | |
137 FileData *file_data_disconnect_sidecar_file(FileData *target, FileData *sfd); | |
138 | |
139 | |
140 static void file_data_set_path(FileData *fd, const gchar *path) | |
141 { | |
142 | |
143 if (strcmp(path, "/") == 0) | |
144 { | |
145 fd->path = g_strdup(path); | |
146 fd->name = fd->path; | |
147 fd->extension = fd->name + 1; | |
148 return; | |
149 } | |
150 | |
151 fd->path = g_strdup(path); | |
152 fd->name = filename_from_path(fd->path); | |
153 | |
154 if (strcmp(fd->name, "..") == 0) | |
155 { | |
156 gchar *dir = remove_level_from_path(path); | |
157 g_free(fd->path); | |
158 fd->path = remove_level_from_path(dir); | |
159 g_free(dir); | |
160 fd->name = ".."; | |
161 fd->extension = fd->name + 2; | |
162 return; | |
163 } | |
164 else if (strcmp(fd->name, ".") == 0) | |
165 { | |
166 g_free(fd->path); | |
167 fd->path = remove_level_from_path(path); | |
168 fd->name = "."; | |
169 fd->extension = fd->name + 1; | |
170 return; | |
171 } | |
172 | |
173 fd->extension = extension_from_path(fd->path); | |
174 if (fd->extension == NULL) | |
175 fd->extension = fd->name + strlen(fd->name); | |
176 } | |
177 | |
178 static void file_data_check_changed_files(FileData *fd, struct stat *st) | |
179 { | |
180 GList *work; | |
181 if (fd->size != st->st_size || | |
182 fd->date != st->st_mtime) | |
183 { | |
184 fd->size = st->st_size; | |
185 fd->date = st->st_mtime; | |
186 if (fd->pixbuf) g_object_unref(fd->pixbuf); | |
187 fd->pixbuf = NULL; | |
188 } | |
189 | |
190 work = fd->sidecar_files; | |
191 while (work) | |
192 { | |
193 FileData *sfd = work->data; | |
194 struct stat st; | |
195 | |
196 if (!stat_utf8(sfd->path, &st)) | |
197 { | |
198 file_data_disconnect_sidecar_file(fd, sfd); | |
199 } | |
200 | |
201 file_data_check_changed_files(sfd, &st); | |
202 work = work->next; | |
203 } | |
204 } | |
205 | |
206 static GHashTable *file_data_pool = NULL; | |
207 | |
208 static FileData *file_data_new(const gchar *path_utf8, struct stat *st, gboolean check_sidecars) | |
209 { | |
210 FileData *fd; | |
211 | |
212 DEBUG_2("file_data_new: '%s' %d", path_utf8, check_sidecars); | |
213 | |
214 if (!file_data_pool) | |
215 file_data_pool = g_hash_table_new(g_str_hash, g_str_equal); | |
216 | |
217 fd = g_hash_table_lookup(file_data_pool, path_utf8); | |
218 if (fd) | |
219 { | |
220 file_data_check_changed_files(fd, st); | |
221 DEBUG_2("file_data_pool hit: '%s'", fd->path); | |
222 return file_data_ref(fd); | |
223 } | |
224 | |
225 fd = g_new0(FileData, 1); | |
226 | |
227 file_data_set_path(fd, path_utf8); | |
228 | |
229 fd->original_path = g_strdup(path_utf8); | |
230 fd->size = st->st_size; | |
231 fd->date = st->st_mtime; | |
232 fd->pixbuf = NULL; | |
233 fd->sidecar_files = NULL; | |
234 fd->ref = 1; | |
235 fd->magick = 0x12345678; | |
236 | |
237 g_hash_table_insert(file_data_pool, fd->original_path, fd); | |
238 | |
239 if (check_sidecars && sidecar_file_priority(fd->extension)) | |
240 file_data_check_sidecars(fd); | |
241 return fd; | |
242 } | |
243 | |
244 static void file_data_check_sidecars(FileData *fd) | |
245 { | |
246 int base_len = fd->extension - fd->path; | |
247 GString *fname = g_string_new_len(fd->path, base_len); | |
248 FileData *parent_fd = NULL; | |
249 GList *work = sidecar_ext_get_list(); | |
250 while (work) | |
251 { | |
252 /* check for possible sidecar files; | |
253 the sidecar files created here are referenced only via fd->sidecar_files or fd->parent, | |
254 they have fd->ref set to 0 and file_data unref must chack and free them all together | |
255 (using fd->ref would cause loops and leaks) | |
256 */ | |
257 | |
258 FileData *new_fd; | |
259 | |
260 gchar *ext = work->data; | |
261 work = work->next; | |
262 | |
263 if (strcmp(ext, fd->extension) == 0) | |
264 { | |
265 new_fd = fd; /* processing the original file */ | |
266 } | |
267 else | |
268 { | |
269 struct stat nst; | |
270 g_string_truncate(fname, base_len); | |
271 g_string_append(fname, ext); | |
272 | |
273 if (!stat_utf8(fname->str, &nst)) | |
274 continue; | |
275 | |
276 new_fd = file_data_new(fname->str, &nst, FALSE); | |
277 new_fd->ref--; /* do not use ref here */ | |
278 } | |
279 | |
280 if (!parent_fd) | |
281 parent_fd = new_fd; /* parent is the one with the highest prio, found first */ | |
282 else | |
283 file_data_merge_sidecar_files(parent_fd, new_fd); | |
284 } | |
285 g_string_free(fname, TRUE); | |
286 } | |
287 | |
288 | |
289 static FileData *file_data_new_local(const gchar *path, struct stat *st, gboolean check_sidecars) | |
290 { | |
291 gchar *path_utf8 = path_to_utf8(path); | |
292 FileData *ret = file_data_new(path_utf8, st, check_sidecars); | |
293 g_free(path_utf8); | |
294 return ret; | |
295 } | |
296 | |
297 FileData *file_data_new_simple(const gchar *path_utf8) | |
298 { | |
299 struct stat st; | |
300 | |
301 if (!stat_utf8(path_utf8, &st)) | |
302 { | |
303 st.st_size = 0; | |
304 st.st_mtime = 0; | |
305 } | |
306 | |
307 return file_data_new(path_utf8, &st, TRUE); | |
308 } | |
309 | |
310 FileData *file_data_add_sidecar_file(FileData *target, FileData *sfd) | |
311 { | |
312 sfd->parent = target; | |
313 if(!g_list_find(target->sidecar_files, sfd)) | |
314 target->sidecar_files = g_list_prepend(target->sidecar_files, sfd); | |
315 return target; | |
316 } | |
317 | |
318 | |
319 FileData *file_data_merge_sidecar_files(FileData *target, FileData *source) | |
320 { | |
321 GList *work; | |
322 file_data_add_sidecar_file(target, source); | |
323 | |
324 work = source->sidecar_files; | |
325 while (work) | |
326 { | |
327 FileData *sfd = work->data; | |
328 file_data_add_sidecar_file(target, sfd); | |
329 work = work->next; | |
330 } | |
331 | |
332 g_list_free(source->sidecar_files); | |
333 source->sidecar_files = NULL; | |
334 | |
335 target->sidecar_files = filelist_sort(target->sidecar_files, SORT_NAME, TRUE); | |
336 return target; | |
337 } | |
338 | |
339 | |
340 | |
341 FileData *file_data_ref(FileData *fd) | |
342 { | |
343 if (fd == NULL) return NULL; | |
344 | |
345 // return g_memdup(fd, sizeof(FileData)); | |
346 g_assert(fd->magick == 0x12345678); | |
347 fd->ref++; | |
348 return fd; | |
349 } | |
350 | |
351 static void file_data_free(FileData *fd) | |
352 { | |
353 g_assert(fd->magick == 0x12345678); | |
354 g_assert(fd->ref == 0); | |
355 | |
356 g_hash_table_remove(file_data_pool, fd->original_path); | |
357 | |
358 g_free(fd->path); | |
359 g_free(fd->original_path); | |
360 if (fd->pixbuf) g_object_unref(fd->pixbuf); | |
361 | |
362 | |
363 g_assert(fd->sidecar_files == NULL); /* sidecar files must be freed before calling this */ | |
364 | |
365 file_data_change_info_free(NULL, fd); | |
366 g_free(fd); | |
367 } | |
368 | |
369 void file_data_unref(FileData *fd) | |
370 { | |
371 if (fd == NULL) return; | |
372 g_assert(fd->magick == 0x12345678); | |
373 | |
374 fd->ref--; | |
375 DEBUG_2("file_data_unref (%d): '%s'", fd->ref, fd->path); | |
376 | |
377 if (fd->ref == 0) | |
378 { | |
379 FileData *parent = fd->parent ? fd->parent : fd; | |
380 | |
381 GList *work; | |
382 | |
383 if (parent->ref > 0) | |
384 return; | |
385 | |
386 work = parent->sidecar_files; | |
387 while (work) | |
388 { | |
389 FileData *sfd = work->data; | |
390 if (sfd->ref > 0) | |
391 return; | |
392 work = work->next; | |
393 } | |
394 | |
395 /* none of parent/children is referenced, we can free everything */ | |
396 | |
397 DEBUG_2("file_data_unref: deleting '%s', parent '%s'", fd->path, parent->path); | |
398 | |
399 work = parent->sidecar_files; | |
400 while (work) | |
401 { | |
402 FileData *sfd = work->data; | |
403 file_data_free(sfd); | |
404 work = work->next; | |
405 } | |
406 | |
407 g_list_free(parent->sidecar_files); | |
408 parent->sidecar_files = NULL; | |
409 | |
410 file_data_free(parent); | |
411 | |
412 } | |
413 } | |
414 | |
415 FileData *file_data_disconnect_sidecar_file(FileData *target, FileData *sfd) | |
416 { | |
417 sfd->parent = target; | |
418 g_assert(g_list_find(target->sidecar_files, sfd)); | |
419 | |
420 target->sidecar_files = g_list_remove(target->sidecar_files, sfd); | |
421 sfd->parent = NULL; | |
422 | |
423 if (sfd->ref == 0) { | |
424 file_data_free(sfd); | |
425 return NULL; | |
426 } | |
427 | |
428 return sfd; | |
429 } | |
430 | |
431 /* compare name without extension */ | |
432 gint file_data_compare_name_without_ext(FileData *fd1, FileData *fd2) | |
433 { | |
434 size_t len1 = fd1->extension - fd1->name; | |
435 size_t len2 = fd2->extension - fd2->name; | |
436 | |
437 if (len1 < len2) return -1; | |
438 if (len1 > len2) return 1; | |
439 | |
440 return strncmp(fd1->name, fd2->name, len1); | |
441 } | |
442 | |
443 void file_data_do_change(FileData *fd) | |
444 { | |
445 //FIXME sidecars | |
446 g_assert(fd->change); | |
447 g_free(fd->path); | |
448 g_hash_table_remove(file_data_pool, fd->original_path); | |
449 g_free(fd->original_path); | |
450 file_data_set_path(fd, fd->change->dest); | |
451 fd->original_path = g_strdup(fd->change->dest); | |
452 g_hash_table_insert(file_data_pool, fd->original_path, fd); | |
453 | |
454 } | |
455 | |
456 gboolean file_data_add_change_info(FileData *fd, FileDataChangeType type, const gchar *src, const gchar *dest) | |
457 { | |
458 | |
459 FileDataChangeInfo *fdci; | |
460 | |
461 if (fd->change) return FALSE; | |
462 | |
463 fdci = g_new0(FileDataChangeInfo, 1); | |
464 | |
465 fdci->type = type; | |
466 | |
467 if (src) | |
468 fdci->source = g_strdup(src); | |
469 else | |
470 fdci->source = g_strdup(fd->path); | |
471 | |
472 if (dest) | |
473 fdci->dest = g_strdup(dest); | |
474 | |
475 fd->change = fdci; | |
476 return TRUE; | |
477 } | |
478 | |
479 void file_data_change_info_free(FileDataChangeInfo *fdci, FileData *fd) | |
480 { | |
481 if (!fdci && fd) | |
482 fdci = fd->change; | |
483 | |
484 if (!fdci) | |
485 return; | |
486 | |
487 g_free(fdci->source); | |
488 g_free(fdci->dest); | |
489 | |
490 g_free(fdci); | |
491 | |
492 if (fd) | |
493 fd->change = NULL; | |
494 } | |
495 | |
496 | |
497 | |
498 | |
499 /* | |
500 *----------------------------------------------------------------------------- | |
501 * sidecar file info struct | |
502 *----------------------------------------------------------------------------- | |
503 */ | |
504 | |
505 | |
506 | |
507 static gint sidecar_file_priority(const gchar *path) | |
508 { | |
509 const char *extension = extension_from_path(path); | |
510 int i = 1; | |
511 GList *work; | |
512 if (extension == NULL) | |
513 return 0; | |
514 | |
515 work = sidecar_ext_get_list(); | |
516 | |
517 while (work) { | |
518 gchar *ext = work->data; | |
519 work = work->next; | |
520 if (strcmp(extension, ext) == 0) return i; | |
521 i++; | |
522 } | |
523 return 0; | |
524 } | |
525 | |
526 gchar *sidecar_file_data_list_to_string(FileData *fd) | |
527 { | |
528 GList *work; | |
529 GString *result = g_string_new(""); | |
530 | |
531 work = fd->sidecar_files; | |
532 while (work) | |
533 { | |
534 FileData *sfd = work->data; | |
535 result = g_string_append(result, "+ "); | |
536 result = g_string_append(result, sfd->extension); | |
537 work = work->next; | |
538 if (work) result = g_string_append_c(result, ' '); | |
539 } | |
540 | |
541 return g_string_free(result, FALSE); | |
542 } | |
543 | |
544 /* | |
545 *----------------------------------------------------------------------------- | |
546 * load file list | |
547 *----------------------------------------------------------------------------- | |
548 */ | |
549 | |
550 static SortType filelist_sort_method = SORT_NONE; | |
551 static gint filelist_sort_ascend = TRUE; | |
552 | |
553 | |
554 gint filelist_sort_compare_filedata(FileData *fa, FileData *fb) | |
555 { | |
556 if (!filelist_sort_ascend) | |
557 { | |
558 FileData *tmp = fa; | |
559 fa = fb; | |
560 fb = tmp; | |
561 } | |
562 | |
563 switch (filelist_sort_method) | |
564 { | |
565 case SORT_SIZE: | |
566 if (fa->size < fb->size) return -1; | |
567 if (fa->size > fb->size) return 1; | |
568 return CASE_SORT(fa->name, fb->name); /* fall back to name */ | |
569 break; | |
570 case SORT_TIME: | |
571 if (fa->date < fb->date) return -1; | |
572 if (fa->date > fb->date) return 1; | |
573 return CASE_SORT(fa->name, fb->name); /* fall back to name */ | |
574 break; | |
575 #ifdef HAVE_STRVERSCMP | |
576 case SORT_NUMBER: | |
577 return strverscmp(fa->name, fb->name); | |
578 break; | |
579 #endif | |
580 case SORT_NAME: | |
581 default: | |
582 return CASE_SORT(fa->name, fb->name); | |
583 break; | |
584 } | |
585 } | |
586 | |
587 gint filelist_sort_compare_filedata_full(FileData *fa, FileData *fb, SortType method, gint ascend) | |
588 { | |
589 filelist_sort_method = method; | |
590 filelist_sort_ascend = ascend; | |
591 return filelist_sort_compare_filedata(fa, fb); | |
592 } | |
593 | |
594 static gint filelist_sort_file_cb(void *a, void *b) | |
595 { | |
596 return filelist_sort_compare_filedata(a, b); | |
597 } | |
598 | |
599 GList *filelist_sort_full(GList *list, SortType method, gint ascend, GCompareFunc cb) | |
600 { | |
601 filelist_sort_method = method; | |
602 filelist_sort_ascend = ascend; | |
603 return g_list_sort(list, cb); | |
604 } | |
605 | |
606 GList *filelist_insert_sort_full(GList *list, void *data, SortType method, gint ascend, GCompareFunc cb) | |
607 { | |
608 filelist_sort_method = method; | |
609 filelist_sort_ascend = ascend; | |
610 return g_list_insert_sorted(list, data, cb); | |
611 } | |
612 | |
613 GList *filelist_sort(GList *list, SortType method, gint ascend) | |
614 { | |
615 return filelist_sort_full(list, method, ascend, (GCompareFunc) filelist_sort_file_cb); | |
616 } | |
617 | |
618 GList *filelist_insert_sort(GList *list, FileData *fd, SortType method, gint ascend) | |
619 { | |
620 return filelist_insert_sort_full(list, fd, method, ascend, (GCompareFunc) filelist_sort_file_cb); | |
621 } | |
622 | |
623 | |
624 static GList *filelist_filter_out_sidecars(GList *flist) | |
625 { | |
626 GList *work = flist; | |
627 GList *flist_filtered = NULL; | |
628 | |
629 while (work) | |
630 { | |
631 FileData *fd = work->data; | |
632 work = work->next; | |
633 if (fd->parent) /* remove fd's that are children */ | |
634 file_data_unref(fd); | |
635 else | |
636 flist_filtered = g_list_prepend(flist_filtered, fd); | |
637 } | |
638 g_list_free(flist); | |
639 return flist_filtered; | |
640 } | |
641 | |
642 static gint filelist_read_real(const gchar *path, GList **files, GList **dirs, gint follow_symlinks) | |
643 { | |
644 DIR *dp; | |
645 struct dirent *dir; | |
646 struct stat ent_sbuf; | |
647 gchar *pathl; | |
648 GList *dlist; | |
649 GList *flist; | |
650 | |
651 dlist = NULL; | |
652 flist = NULL; | |
653 | |
654 pathl = path_from_utf8(path); | |
655 if (!pathl || (dp = opendir(pathl)) == NULL) | |
656 { | |
657 g_free(pathl); | |
658 if (files) *files = NULL; | |
659 if (dirs) *dirs = NULL; | |
660 return FALSE; | |
661 } | |
662 | |
663 /* root dir fix */ | |
664 if (pathl[0] == '/' && pathl[1] == '\0') | |
665 { | |
666 g_free(pathl); | |
667 pathl = g_strdup(""); | |
668 } | |
669 | |
670 while ((dir = readdir(dp)) != NULL) | |
671 { | |
672 gchar *name = dir->d_name; | |
673 if (options->file_filter.show_hidden_files || !ishidden(name)) | |
674 { | |
675 gchar *filepath = g_strconcat(pathl, "/", name, NULL); | |
676 if ((follow_symlinks ? | |
677 stat(filepath, &ent_sbuf) : | |
678 lstat(filepath, &ent_sbuf)) >= 0) | |
679 { | |
680 if (S_ISDIR(ent_sbuf.st_mode)) | |
681 { | |
682 /* we ignore the .thumbnails dir for cleanliness */ | |
683 if ((dirs) && | |
684 !(name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) && | |
685 strcmp(name, GQ_CACHE_LOCAL_THUMB) != 0 && | |
686 strcmp(name, GQ_CACHE_LOCAL_METADATA) != 0 && | |
687 strcmp(name, THUMB_FOLDER_LOCAL) != 0) | |
688 { | |
689 dlist = g_list_prepend(dlist, file_data_new_local(filepath, &ent_sbuf, FALSE)); | |
690 } | |
691 } | |
692 else | |
693 { | |
694 if ((files) && filter_name_exists(name)) | |
695 { | |
696 flist = g_list_prepend(flist, file_data_new_local(filepath, &ent_sbuf, TRUE)); | |
697 } | |
698 } | |
699 } | |
700 g_free(filepath); | |
701 } | |
702 } | |
703 | |
704 closedir(dp); | |
705 | |
706 g_free(pathl); | |
707 | |
708 flist = filelist_filter_out_sidecars(flist); | |
709 | |
710 if (dirs) *dirs = dlist; | |
711 if (files) *files = flist; | |
712 | |
713 return TRUE; | |
714 } | |
715 | |
716 gint filelist_read(const gchar *path, GList **files, GList **dirs) | |
717 { | |
718 return filelist_read_real(path, files, dirs, TRUE); | |
719 } | |
720 | |
721 gint filelist_read_lstat(const gchar *path, GList **files, GList **dirs) | |
722 { | |
723 return filelist_read_real(path, files, dirs, FALSE); | |
724 } | |
725 | |
726 void filelist_free(GList *list) | |
727 { | |
728 GList *work; | |
729 | |
730 work = list; | |
731 while (work) | |
732 { | |
733 file_data_unref((FileData *)work->data); | |
734 work = work->next; | |
735 } | |
736 | |
737 g_list_free(list); | |
738 } | |
739 | |
740 | |
741 GList *filelist_copy(GList *list) | |
742 { | |
743 GList *new_list = NULL; | |
744 GList *work; | |
745 | |
746 work = list; | |
747 while (work) | |
748 { | |
749 FileData *fd; | |
750 | |
751 fd = work->data; | |
752 work = work->next; | |
753 | |
754 new_list = g_list_prepend(new_list, file_data_ref(fd)); | |
755 } | |
756 | |
757 return g_list_reverse(new_list); | |
758 } | |
759 | |
760 GList *filelist_from_path_list(GList *list) | |
761 { | |
762 GList *new_list = NULL; | |
763 GList *work; | |
764 | |
765 work = list; | |
766 while (work) | |
767 { | |
768 gchar *path; | |
769 | |
770 path = work->data; | |
771 work = work->next; | |
772 | |
773 new_list = g_list_prepend(new_list, file_data_new_simple(path)); | |
774 } | |
775 | |
776 return g_list_reverse(new_list); | |
777 } | |
778 | |
779 GList *filelist_to_path_list(GList *list) | |
780 { | |
781 GList *new_list = NULL; | |
782 GList *work; | |
783 | |
784 work = list; | |
785 while (work) | |
786 { | |
787 FileData *fd; | |
788 | |
789 fd = work->data; | |
790 work = work->next; | |
791 | |
792 new_list = g_list_prepend(new_list, g_strdup(fd->path)); | |
793 } | |
794 | |
795 return g_list_reverse(new_list); | |
796 } | |
797 | |
798 GList *filelist_filter(GList *list, gint is_dir_list) | |
799 { | |
800 GList *work; | |
801 | |
802 if (!is_dir_list && options->file_filter.disable && options->file_filter.show_hidden_files) return list; | |
803 | |
804 work = list; | |
805 while (work) | |
806 { | |
807 FileData *fd = (FileData *)(work->data); | |
808 const gchar *name = fd->name; | |
809 | |
810 if ((!options->file_filter.show_hidden_files && ishidden(name)) || | |
811 (!is_dir_list && !filter_name_exists(name)) || | |
812 (is_dir_list && name[0] == '.' && (strcmp(name, GQ_CACHE_LOCAL_THUMB) == 0 || | |
813 strcmp(name, GQ_CACHE_LOCAL_METADATA) == 0)) ) | |
814 { | |
815 GList *link = work; | |
816 work = work->next; | |
817 list = g_list_remove_link(list, link); | |
818 file_data_unref(fd); | |
819 g_list_free(link); | |
820 } | |
821 else | |
822 { | |
823 work = work->next; | |
824 } | |
825 } | |
826 | |
827 return list; | |
828 } | |
829 | |
830 /* | |
831 *----------------------------------------------------------------------------- | |
832 * filelist recursive | |
833 *----------------------------------------------------------------------------- | |
834 */ | |
835 | |
836 static gint filelist_sort_path_cb(gconstpointer a, gconstpointer b) | |
837 { | |
838 return CASE_SORT(((FileData *)a)->path, ((FileData *)b)->path); | |
839 } | |
840 | |
841 GList *filelist_sort_path(GList *list) | |
842 { | |
843 return g_list_sort(list, filelist_sort_path_cb); | |
844 } | |
845 | |
846 static void filelist_recursive_append(GList **list, GList *dirs) | |
847 { | |
848 GList *work; | |
849 | |
850 work = dirs; | |
851 while (work) | |
852 { | |
853 FileData *fd = (FileData *)(work->data); | |
854 const gchar *path = fd->path; | |
855 GList *f = NULL; | |
856 GList *d = NULL; | |
857 | |
858 if (filelist_read(path, &f, &d)) | |
859 { | |
860 f = filelist_filter(f, FALSE); | |
861 f = filelist_sort_path(f); | |
862 *list = g_list_concat(*list, f); | |
863 | |
864 d = filelist_filter(d, TRUE); | |
865 d = filelist_sort_path(d); | |
866 filelist_recursive_append(list, d); | |
867 filelist_free(d); | |
868 } | |
869 | |
870 work = work->next; | |
871 } | |
872 } | |
873 | |
874 GList *filelist_recursive(const gchar *path) | |
875 { | |
876 GList *list = NULL; | |
877 GList *d = NULL; | |
878 | |
879 if (!filelist_read(path, &list, &d)) return NULL; | |
880 list = filelist_filter(list, FALSE); | |
881 list = filelist_sort_path(list); | |
882 | |
883 d = filelist_filter(d, TRUE); | |
884 d = filelist_sort_path(d); | |
885 filelist_recursive_append(&list, d); | |
886 filelist_free(d); | |
887 | |
888 return list; | |
889 } |