7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
18 #include "thumb_standard.h"
19 #include "ui_fileops.h"
23 *-----------------------------------------------------------------------------
25 *-----------------------------------------------------------------------------
28 static GList *filter_list = NULL;
29 static GList *extension_list = NULL;
30 static GList *sidecar_ext_list = NULL;
32 static GList *file_class_extension_list[FILE_FORMAT_CLASSES];
34 static gint sidecar_file_priority(const gchar *path);
37 gint ishidden(const gchar *name)
39 if (name[0] != '.') return FALSE;
40 if (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) return FALSE;
44 static FilterEntry *filter_entry_new(const gchar *key, const gchar *description,
45 const gchar *extensions, FileFormatClass file_class, gint enabled)
49 fe = g_new0(FilterEntry, 1);
50 fe->key = g_strdup(key);
51 fe->description = g_strdup(description);
52 fe->extensions = g_strdup(extensions);
53 fe->enabled = enabled;
54 fe->file_class = file_class;
59 static void filter_entry_free(FilterEntry *fe)
64 g_free(fe->description);
65 g_free(fe->extensions);
69 GList *filter_get_list(void)
74 void filter_remove_entry(FilterEntry *fe)
76 if (!g_list_find(filter_list, fe)) return;
78 filter_list = g_list_remove(filter_list, fe);
79 filter_entry_free(fe);
82 static gint filter_key_exists(const gchar *key)
86 if (!key) return FALSE;
91 FilterEntry *fe = work->data;
94 if (strcmp(fe->key, key) == 0) return TRUE;
100 void filter_add(const gchar *key, const gchar *description, const gchar *extensions, FileFormatClass file_class, gint enabled)
102 filter_list = g_list_append(filter_list, filter_entry_new(key, description, extensions, file_class, enabled));
105 void filter_add_unique(const gchar *description, const gchar *extensions, FileFormatClass file_class, gint enabled)
110 key = g_strdup("user0");
112 while (filter_key_exists(key))
116 key = g_strdup_printf("user%d", n);
120 filter_add(key, description, extensions, file_class, enabled);
124 static void filter_add_if_missing(const gchar *key, const gchar *description, const gchar *extensions, FileFormatClass file_class, gint enabled)
133 FilterEntry *fe = work->data;
135 if (fe->key && strcmp(fe->key, key) == 0)
137 if (fe->file_class == FORMAT_CLASS_UNKNOWN)
138 fe->file_class = file_class; /* for compatibility */
143 filter_add(key, description, extensions, file_class, enabled);
146 void filter_reset(void)
153 FilterEntry *fe = work->data;
155 filter_entry_free(fe);
158 g_list_free(filter_list);
162 void filter_add_defaults(void)
166 list = gdk_pixbuf_get_formats();
170 GdkPixbufFormat *format;
174 GString *filter = NULL;
180 name = gdk_pixbuf_format_get_name(format);
181 desc = gdk_pixbuf_format_get_description(format);
182 extensions = gdk_pixbuf_format_get_extensions(format);
185 while (extensions[i])
189 filter = g_string_new(".");
190 filter = g_string_append(filter, extensions[i]);
194 filter = g_string_append(filter, ";.");
195 filter = g_string_append(filter, extensions[i]);
200 if (debug) printf("loader reported [%s] [%s] [%s]\n", name, desc, filter->str);
202 filter_add_if_missing(name, desc, filter->str, FORMAT_CLASS_IMAGE, TRUE);
206 g_strfreev(extensions);
207 g_string_free(filter, TRUE);
211 /* add defaults even if gdk-pixbuf does not have them, but disabled */
212 filter_add_if_missing("jpeg", "JPEG group", ".jpg;.jpeg;.jpe", FORMAT_CLASS_IMAGE, FALSE);
213 filter_add_if_missing("png", "Portable Network Graphic", ".png", FORMAT_CLASS_IMAGE, FALSE);
214 filter_add_if_missing("tiff", "Tiff", ".tif;.tiff", FORMAT_CLASS_IMAGE, FALSE);
215 filter_add_if_missing("pnm", "Packed Pixel formats", ".pbm;.pgm;.pnm;.ppm", FORMAT_CLASS_IMAGE, FALSE);
216 filter_add_if_missing("gif", "Graphics Interchange Format", ".gif", FORMAT_CLASS_IMAGE, FALSE);
217 filter_add_if_missing("xbm", "X bitmap", ".xbm", FORMAT_CLASS_IMAGE, FALSE);
218 filter_add_if_missing("xpm", "X pixmap", ".xpm", FORMAT_CLASS_IMAGE, FALSE);
219 filter_add_if_missing("bmp", "Bitmap", ".bmp", FORMAT_CLASS_IMAGE, FALSE);
220 filter_add_if_missing("ico", "Icon file", ".ico;.cur", FORMAT_CLASS_IMAGE, FALSE);
221 filter_add_if_missing("ras", "Raster", ".ras", FORMAT_CLASS_IMAGE, FALSE);
222 filter_add_if_missing("svg", "Scalable Vector Graphics", ".svg", FORMAT_CLASS_IMAGE, FALSE);
224 /* non-image files that might be desirable to show */
225 filter_add_if_missing("xmp", "XMP sidecar", ".xmp", FORMAT_CLASS_META, TRUE);
227 /* These are the raw camera formats with embedded jpeg/exif.
228 * (see format_raw.c and/or exiv2.cc)
230 filter_add_if_missing("arw", "Sony raw format", ".arw;.srf;.sr2", FORMAT_CLASS_RAWIMAGE, TRUE);
231 filter_add_if_missing("crw", "Canon raw format", ".crw;.cr2", FORMAT_CLASS_RAWIMAGE, TRUE);
232 filter_add_if_missing("kdc", "Kodak raw format", ".kdc;.dcr", FORMAT_CLASS_RAWIMAGE, TRUE);
233 filter_add_if_missing("raf", "Fujifilm raw format", ".raf", FORMAT_CLASS_RAWIMAGE, TRUE);
234 filter_add_if_missing("mef", "Mamiya raw format", ".mef;.mos", FORMAT_CLASS_RAWIMAGE, TRUE);
235 filter_add_if_missing("mrw", "Minolta raw format", ".mrw", FORMAT_CLASS_RAWIMAGE, TRUE);
236 filter_add_if_missing("nef", "Nikon raw format", ".nef", FORMAT_CLASS_RAWIMAGE, TRUE);
237 filter_add_if_missing("orf", "Olympus raw format", ".orf", FORMAT_CLASS_RAWIMAGE, TRUE);
238 filter_add_if_missing("pef", "Pentax or Samsung raw format", ".pef;.ptx", FORMAT_CLASS_RAWIMAGE, TRUE);
239 filter_add_if_missing("dng", "Adobe Digital Negative raw format", ".dng", FORMAT_CLASS_RAWIMAGE, TRUE);
240 filter_add_if_missing("x3f", "Sigma raw format", ".x3f", FORMAT_CLASS_RAWIMAGE, TRUE);
241 filter_add_if_missing("raw", "Panasonic raw format", ".raw", FORMAT_CLASS_RAWIMAGE, TRUE);
242 filter_add_if_missing("r3d", "Red raw format", ".r3d", FORMAT_CLASS_RAWIMAGE, TRUE);
243 filter_add_if_missing("3fr", "Hasselblad raw format", ".3fr", FORMAT_CLASS_RAWIMAGE, TRUE);
244 filter_add_if_missing("erf", "Epson raw format", ".erf", FORMAT_CLASS_RAWIMAGE, TRUE);
247 GList *filter_to_list(const gchar *extensions)
252 if (!extensions) return NULL;
261 while (*p != '\0' && *p != ';')
266 list = g_list_append(list, g_strndup(b, l));
273 void filter_rebuild(void)
278 string_list_free(extension_list);
279 extension_list = NULL;
281 for (i = 0; i < FILE_FORMAT_CLASSES; i++)
283 string_list_free(file_class_extension_list[i]);
284 file_class_extension_list[i] = NULL;
299 ext = filter_to_list(fe->extensions);
300 if (ext) extension_list = g_list_concat(extension_list, ext);
302 if (fe->file_class >= 0 && fe->file_class < FILE_FORMAT_CLASSES)
304 ext = filter_to_list(fe->extensions);
305 if (ext) file_class_extension_list[fe->file_class] = g_list_concat(file_class_extension_list[fe->file_class], ext);
309 printf("WARNING: invalid file class %d\n", fe->file_class);
315 gint filter_name_exists(const gchar *name)
320 if (!extension_list || file_filter_disable) return TRUE;
323 work = extension_list;
326 gchar *filter = work->data;
327 gint lf = strlen(filter);
331 if (strncasecmp(name + ln - lf, filter, lf) == 0) return TRUE;
339 gint filter_file_class(const gchar *name, FileFormatClass file_class)
344 if (file_class < 0 || file_class >= FILE_FORMAT_CLASSES)
346 printf("WARNING: invalid file class %d\n", file_class);
351 work = file_class_extension_list[file_class];
354 gchar *filter = work->data;
355 gint lf = strlen(filter);
359 if (strncasecmp(name + ln - lf, filter, lf) == 0) return TRUE;
367 void filter_write_list(SecureSaveInfo *ssi)
374 FilterEntry *fe = work->data;
377 gchar *extensions = escquote_value(fe->extensions);
378 gchar *description = escquote_value(fe->description);
380 secure_fprintf(ssi, "filter_ext: \"%s%s\" %s %s\n",
381 (fe->enabled) ? "" : "#",
382 fe->key, extensions, description);
388 void filter_parse(const gchar *text)
396 if (!text || text[0] != '"') return;
398 key = quoted_value(text, &p);
401 ext = quoted_value(p, &p);
402 desc = quoted_value(p, &p);
404 if (key && key[0] == '#')
407 tmp = g_strdup(key + 1);
414 if (key && strlen(key) > 0 && ext) filter_add(key, desc, ext, FORMAT_CLASS_UNKNOWN, enabled);
421 GList *path_list_filter(GList *list, gint is_dir_list)
425 if (!is_dir_list && file_filter_disable && show_dot_files) return list;
430 gchar *name = work->data;
433 base = filename_from_path(name);
435 if ((!show_dot_files && ishidden(base)) ||
436 (!is_dir_list && !filter_name_exists(base)) ||
437 (is_dir_list && base[0] == '.' && (strcmp(base, GQVIEW_CACHE_LOCAL_THUMB) == 0 ||
438 strcmp(base, GQVIEW_CACHE_LOCAL_METADATA) == 0)) )
442 list = g_list_remove_link(list, link);
457 *-----------------------------------------------------------------------------
458 * sidecar extension list
459 *-----------------------------------------------------------------------------
462 static GList *sidecar_ext_get_list(void)
464 return sidecar_ext_list;
467 void sidecar_ext_parse(const gchar *text, gint quoted)
472 work = sidecar_ext_list;
475 gchar *ext = work->data;
479 g_list_free(sidecar_ext_list);
480 sidecar_ext_list = NULL;
483 value = quoted_value(text, NULL);
485 value = g_strdup(text);
487 if (value == NULL) return;
489 sidecar_ext_list = filter_to_list(value);
494 void sidecar_ext_write(SecureSaveInfo *ssi)
496 secure_fprintf(ssi, "\nsidecar_ext: \"%s\"\n", sidecar_ext_to_string());
499 char *sidecar_ext_to_string()
502 GString *str = g_string_new("");
504 work = sidecar_ext_list;
507 gchar *ext = work->data;
509 g_string_append(str, ext);
510 if (work) g_string_append(str, ";");
512 return g_string_free(str, FALSE);
515 void sidecar_ext_add_defaults()
517 sidecar_ext_parse(".jpg;.cr2;.nef;.crw;.xmp", FALSE);
521 *-----------------------------------------------------------------------------
522 * path list recursive
523 *-----------------------------------------------------------------------------
526 static gint path_list_sort_cb(gconstpointer a, gconstpointer b)
528 return CASE_SORT((gchar *)a, (gchar *)b);
531 GList *path_list_sort(GList *list)
533 return g_list_sort(list, path_list_sort_cb);
536 static void path_list_recursive_append(GList **list, GList *dirs)
543 const gchar *path = work->data;
547 if (path_list(path, &f, &d))
549 f = path_list_filter(f, FALSE);
550 f = path_list_sort(f);
551 *list = g_list_concat(*list, f);
553 d = path_list_filter(d, TRUE);
554 d = path_list_sort(d);
555 path_list_recursive_append(list, d);
563 GList *path_list_recursive(const gchar *path)
568 if (!path_list(path, &list, &d)) return NULL;
569 list = path_list_filter(list, FALSE);
570 list = path_list_sort(list);
572 d = path_list_filter(d, TRUE);
573 d = path_list_sort(d);
574 path_list_recursive_append(&list, d);
581 *-----------------------------------------------------------------------------
582 * text conversion utils
583 *-----------------------------------------------------------------------------
586 gchar *text_from_size(gint64 size)
592 /* what I would like to use is printf("%'d", size)
593 * BUT: not supported on every libc :(
595 if (size > G_MAXUINT)
597 /* the %lld conversion is not valid in all libcs, so use a simple work-around */
598 a = g_strdup_printf("%d%09d", (guint)(size / 1000000000), (guint)(size % 1000000000));
602 a = g_strdup_printf("%d", (guint)size);
608 b = g_new(gchar, l + n + 1);
633 gchar *text_from_size_abrev(gint64 size)
635 if (size < (gint64)1024)
637 return g_strdup_printf(_("%d bytes"), (gint)size);
639 if (size < (gint64)1048576)
641 return g_strdup_printf(_("%.1f K"), (double)size / 1024.0);
643 if (size < (gint64)1073741824)
645 return g_strdup_printf(_("%.1f MB"), (double)size / 1048576.0);
648 /* to avoid overflowing the double, do division in two steps */
650 return g_strdup_printf(_("%.1f GB"), (double)size / 1024.0);
653 /* note: returned string is valid until next call to text_from_time() */
654 const gchar *text_from_time(time_t t)
656 static gchar *ret = NULL;
660 GError *error = NULL;
662 btime = localtime(&t);
664 /* the %x warning about 2 digit years is not an error */
665 buflen = strftime(buf, sizeof(buf), "%x %H:%M", btime);
666 if (buflen < 1) return "";
669 ret = g_locale_to_utf8(buf, buflen, NULL, NULL, &error);
672 printf("Error converting locale strftime to UTF-8: %s\n", error->message);
681 *-----------------------------------------------------------------------------
683 *-----------------------------------------------------------------------------
686 FileData *file_data_merge_sidecar_files(FileData *target, FileData *source);
687 static void file_data_check_sidecars(FileData *fd);
688 FileData *file_data_disconnect_sidecar_file(FileData *target, FileData *sfd);
691 static void file_data_set_path(FileData *fd, const gchar *path)
694 if (strcmp(path, "/") == 0)
696 fd->path = g_strdup(path);
698 fd->extension = fd->name + 1;
702 fd->path = g_strdup(path);
703 fd->name = filename_from_path(fd->path);
705 if (strcmp(fd->name, "..") == 0)
707 gchar *dir = remove_level_from_path(path);
709 fd->path = remove_level_from_path(dir);
712 fd->extension = fd->name + 2;
715 else if (strcmp(fd->name, ".") == 0)
718 fd->path = remove_level_from_path(path);
720 fd->extension = fd->name + 1;
724 fd->extension = extension_from_path(fd->path);
725 if (fd->extension == NULL)
726 fd->extension = fd->name + strlen(fd->name);
729 static void file_data_check_changed_files(FileData *fd, struct stat *st)
732 if (fd->size != st->st_size ||
733 fd->date != st->st_mtime)
735 fd->size = st->st_size;
736 fd->date = st->st_mtime;
737 if (fd->pixbuf) g_object_unref(fd->pixbuf);
741 work = fd->sidecar_files;
744 FileData *sfd = work->data;
747 if (!stat_utf8(sfd->path, &st))
749 file_data_disconnect_sidecar_file(fd, sfd);
752 file_data_check_changed_files(sfd, &st);
757 static GHashTable *file_data_pool = NULL;
759 static FileData *file_data_new(const gchar *path_utf8, struct stat *st, gboolean check_sidecars)
763 if (debug) printf("file_data_new: '%s' %d\n", path_utf8, check_sidecars);
766 file_data_pool = g_hash_table_new (g_str_hash, g_str_equal);
768 fd = g_hash_table_lookup(file_data_pool, path_utf8);
771 file_data_check_changed_files(fd, st);
772 if (debug) printf("file_data_pool hit: '%s'\n", fd->path);
773 return file_data_ref(fd);
776 fd = g_new0(FileData, 1);
778 file_data_set_path(fd, path_utf8);
780 fd->original_path = g_strdup(path_utf8);
781 fd->size = st->st_size;
782 fd->date = st->st_mtime;
784 fd->sidecar_files = NULL;
786 fd->magick = 0x12345678;
788 g_hash_table_insert(file_data_pool, fd->original_path, fd);
790 if (check_sidecars && sidecar_file_priority(fd->extension))
791 file_data_check_sidecars(fd);
795 static void file_data_check_sidecars(FileData *fd)
797 int base_len = fd->extension - fd->path;
798 GString *fname = g_string_new_len(fd->path, base_len);
799 FileData *parent_fd = NULL;
800 GList *work = sidecar_ext_get_list();
803 /* check for possible sidecar files;
804 the sidecar files created here are referenced only via fd->sidecar_files or fd->parent,
805 they have fd->ref set to 0 and file_data unref must chack and free them all together
806 (using fd->ref would cause loops and leaks)
811 gchar *ext = work->data;
814 if (strcmp(ext, fd->extension) == 0)
816 new_fd = fd; /* processing the original file */
821 g_string_truncate(fname, base_len);
822 g_string_append(fname, ext);
824 if (!stat_utf8(fname->str, &nst))
827 new_fd = file_data_new(fname->str, &nst, FALSE);
828 new_fd->ref--; /* do not use ref here */
832 parent_fd = new_fd; /* parent is the one with the highest prio, found first */
834 file_data_merge_sidecar_files(parent_fd, new_fd);
836 g_string_free(fname, TRUE);
840 static FileData *file_data_new_local(const gchar *path, struct stat *st, gboolean check_sidecars)
842 gchar *path_utf8 = path_to_utf8(path);
843 FileData *ret = file_data_new(path_utf8, st, check_sidecars);
848 FileData *file_data_new_simple(const gchar *path_utf8)
852 if (!stat_utf8(path_utf8, &st))
858 return file_data_new(path_utf8, &st, TRUE);
861 FileData *file_data_add_sidecar_file(FileData *target, FileData *sfd)
863 sfd->parent = target;
864 if(!g_list_find(target->sidecar_files, sfd))
865 target->sidecar_files = g_list_prepend(target->sidecar_files, sfd);
870 FileData *file_data_merge_sidecar_files(FileData *target, FileData *source)
873 file_data_add_sidecar_file(target, source);
875 work = source->sidecar_files;
878 FileData *sfd = work->data;
879 file_data_add_sidecar_file(target, sfd);
883 g_list_free(source->sidecar_files);
884 source->sidecar_files = NULL;
886 target->sidecar_files = filelist_sort(target->sidecar_files, SORT_NAME, TRUE);
892 FileData *file_data_ref(FileData *fd)
894 if (fd == NULL) return NULL;
896 // return g_memdup(fd, sizeof(FileData));
897 g_assert(fd->magick == 0x12345678);
902 static void file_data_free(FileData *fd)
904 g_assert(fd->magick == 0x12345678);
905 g_assert(fd->ref == 0);
907 g_hash_table_remove(file_data_pool, fd->original_path);
910 g_free(fd->original_path);
911 if (fd->pixbuf) g_object_unref(fd->pixbuf);
914 g_assert(fd->sidecar_files == NULL); /* sidecar files must be freed before calling this */
916 file_data_change_info_free(NULL, fd);
920 void file_data_unref(FileData *fd)
922 if (fd == NULL) return;
923 g_assert(fd->magick == 0x12345678);
924 if (debug) printf("file_data_unref: '%s'\n", fd->path);
928 FileData *parent = fd->parent ? fd->parent : fd;
935 work = parent->sidecar_files;
938 FileData *sfd = work->data;
944 /* none of parent/children is referenced, we can free everything */
946 if (debug) printf("file_data_unref: deleting '%s', parent '%s'\n", fd->path, parent->path);
948 work = parent->sidecar_files;
951 FileData *sfd = work->data;
956 g_list_free(parent->sidecar_files);
957 parent->sidecar_files = NULL;
959 file_data_free(parent);
964 FileData *file_data_disconnect_sidecar_file(FileData *target, FileData *sfd)
966 sfd->parent = target;
967 g_assert(g_list_find(target->sidecar_files, sfd));
969 target->sidecar_files = g_list_remove(target->sidecar_files, sfd);
980 /* compare name without extension */
981 gint file_data_compare_name_without_ext(FileData *fd1, FileData *fd2)
983 size_t len1 = fd1->extension - fd1->name;
984 size_t len2 = fd2->extension - fd2->name;
986 if (len1 < len2) return -1;
987 if (len1 > len2) return 1;
989 return strncmp(fd1->name, fd2->name, len1);
992 void file_data_do_change(FileData *fd)
995 g_assert(fd->change);
997 g_hash_table_remove(file_data_pool, fd->original_path);
998 g_free(fd->original_path);
999 file_data_set_path(fd, fd->change->dest);
1000 fd->original_path = g_strdup(fd->change->dest);
1001 g_hash_table_insert(file_data_pool, fd->original_path, fd);
1005 gboolean file_data_add_change_info(FileData *fd, FileDataChangeType type, const gchar *src, const gchar *dest)
1008 FileDataChangeInfo *fdci;
1010 if (fd->change) return FALSE;
1012 fdci = g_new0(FileDataChangeInfo, 1);
1017 fdci->source = g_strdup(src);
1019 fdci->source = g_strdup(fd->path);
1022 fdci->dest = g_strdup(dest);
1028 void file_data_change_info_free(FileDataChangeInfo *fdci, FileData *fd)
1036 g_free(fdci->source);
1049 *-----------------------------------------------------------------------------
1050 * sidecar file info struct
1051 *-----------------------------------------------------------------------------
1056 static gint sidecar_file_priority(const gchar *path)
1058 const char *extension = extension_from_path(path);
1061 if (extension == NULL)
1064 work = sidecar_ext_get_list();
1067 gchar *ext = work->data;
1069 if (strcmp(extension, ext) == 0) return i;
1075 gchar *sidecar_file_data_list_to_string(FileData *fd)
1078 GString *result = g_string_new("");
1080 work = fd->sidecar_files;
1083 FileData *sfd = work->data;
1084 result = g_string_append(result, "+ ");
1085 result = g_string_append(result, sfd->extension);
1087 if (work) result = g_string_append_c(result, ' ');
1090 return g_string_free(result, FALSE);
1094 *-----------------------------------------------------------------------------
1096 *-----------------------------------------------------------------------------
1099 static SortType filelist_sort_method = SORT_NONE;
1100 static gint filelist_sort_ascend = TRUE;
1103 gint filelist_sort_compare_filedata(FileData *fa, FileData *fb)
1105 if (!filelist_sort_ascend)
1112 switch (filelist_sort_method)
1115 if (fa->size < fb->size) return -1;
1116 if (fa->size > fb->size) return 1;
1117 return CASE_SORT(fa->name, fb->name); /* fall back to name */
1120 if (fa->date < fb->date) return -1;
1121 if (fa->date > fb->date) return 1;
1122 return CASE_SORT(fa->name, fb->name); /* fall back to name */
1124 #ifdef HAVE_STRVERSCMP
1126 return strverscmp(fa->name, fb->name);
1131 return CASE_SORT(fa->name, fb->name);
1136 gint filelist_sort_compare_filedata_full(FileData *fa, FileData *fb, SortType method, gint ascend)
1138 filelist_sort_method = method;
1139 filelist_sort_ascend = ascend;
1140 return filelist_sort_compare_filedata(fa, fb);
1143 static gint filelist_sort_file_cb(void *a, void *b)
1145 return filelist_sort_compare_filedata(a, b);
1148 GList *filelist_sort_full(GList *list, SortType method, gint ascend, GCompareFunc cb)
1150 filelist_sort_method = method;
1151 filelist_sort_ascend = ascend;
1152 return g_list_sort(list, cb);
1155 GList *filelist_insert_sort_full(GList *list, void *data, SortType method, gint ascend, GCompareFunc cb)
1157 filelist_sort_method = method;
1158 filelist_sort_ascend = ascend;
1159 return g_list_insert_sorted(list, data, cb);
1162 GList *filelist_sort(GList *list, SortType method, gint ascend)
1164 return filelist_sort_full(list, method, ascend, (GCompareFunc) filelist_sort_file_cb);
1167 GList *filelist_insert_sort(GList *list, FileData *fd, SortType method, gint ascend)
1169 return filelist_insert_sort_full(list, fd, method, ascend, (GCompareFunc) filelist_sort_file_cb);
1173 static GList *filelist_filter_out_sidecars(GList *flist)
1175 GList *work = flist;
1176 GList *flist_filtered = NULL;
1180 FileData *fd = work->data;
1182 if (fd->parent) /* remove fd's that are children */
1183 file_data_unref(fd);
1185 flist_filtered = g_list_prepend(flist_filtered, fd);
1188 return flist_filtered;
1191 static gint filelist_read_real(const gchar *path, GList **files, GList **dirs, gint follow_symlinks)
1195 struct stat ent_sbuf;
1203 pathl = path_from_utf8(path);
1204 if (!pathl || (dp = opendir(pathl)) == NULL)
1207 if (files) *files = NULL;
1208 if (dirs) *dirs = NULL;
1213 if (pathl[0] == '/' && pathl[1] == '\0')
1216 pathl = g_strdup("");
1219 while ((dir = readdir(dp)) != NULL)
1221 gchar *name = dir->d_name;
1222 if (show_dot_files || !ishidden(name))
1224 gchar *filepath = g_strconcat(pathl, "/", name, NULL);
1225 if ((follow_symlinks ?
1226 stat(filepath, &ent_sbuf) :
1227 lstat(filepath, &ent_sbuf)) >= 0)
1229 if (S_ISDIR(ent_sbuf.st_mode))
1231 /* we ignore the .thumbnails dir for cleanliness */
1233 !(name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) &&
1234 strcmp(name, GQVIEW_CACHE_LOCAL_THUMB) != 0 &&
1235 strcmp(name, GQVIEW_CACHE_LOCAL_METADATA) != 0 &&
1236 strcmp(name, THUMB_FOLDER_LOCAL) != 0)
1238 dlist = g_list_prepend(dlist, file_data_new_local(filepath, &ent_sbuf, FALSE));
1243 if ((files) && filter_name_exists(name))
1245 flist = g_list_prepend(flist, file_data_new_local(filepath, &ent_sbuf, TRUE));
1257 flist = filelist_filter_out_sidecars(flist);
1259 if (dirs) *dirs = dlist;
1260 if (files) *files = flist;
1265 gint filelist_read(const gchar *path, GList **files, GList **dirs)
1267 return filelist_read_real(path, files, dirs, TRUE);
1270 gint filelist_read_lstat(const gchar *path, GList **files, GList **dirs)
1272 return filelist_read_real(path, files, dirs, FALSE);
1275 void filelist_free(GList *list)
1282 file_data_unref((FileData *)work->data);
1290 GList *filelist_copy(GList *list)
1292 GList *new_list = NULL;
1303 new_list = g_list_prepend(new_list, file_data_ref(fd));
1306 return g_list_reverse(new_list);
1309 GList *filelist_from_path_list(GList *list)
1311 GList *new_list = NULL;
1322 new_list = g_list_prepend(new_list, file_data_new_simple(path));
1325 return g_list_reverse(new_list);
1328 GList *filelist_to_path_list(GList *list)
1330 GList *new_list = NULL;
1341 new_list = g_list_prepend(new_list, g_strdup(fd->path));
1344 return g_list_reverse(new_list);
1347 GList *filelist_filter(GList *list, gint is_dir_list)
1351 if (!is_dir_list && file_filter_disable && show_dot_files) return list;
1356 FileData *fd = (FileData *)(work->data);
1357 const gchar *name = fd->name;
1359 if ((!show_dot_files && ishidden(name)) ||
1360 (!is_dir_list && !filter_name_exists(name)) ||
1361 (is_dir_list && name[0] == '.' && (strcmp(name, GQVIEW_CACHE_LOCAL_THUMB) == 0 ||
1362 strcmp(name, GQVIEW_CACHE_LOCAL_METADATA) == 0)) )
1366 list = g_list_remove_link(list, link);
1367 file_data_unref(fd);
1380 *-----------------------------------------------------------------------------
1381 * filelist recursive
1382 *-----------------------------------------------------------------------------
1385 static gint filelist_sort_path_cb(gconstpointer a, gconstpointer b)
1387 return CASE_SORT(((FileData *)a)->path, ((FileData *)b)->path);
1390 GList *filelist_sort_path(GList *list)
1392 return g_list_sort(list, filelist_sort_path_cb);
1395 static void filelist_recursive_append(GList **list, GList *dirs)
1402 FileData *fd = (FileData *)(work->data);
1403 const gchar *path = fd->path;
1407 if (filelist_read(path, &f, &d))
1409 f = filelist_filter(f, FALSE);
1410 f = filelist_sort_path(f);
1411 *list = g_list_concat(*list, f);
1413 d = filelist_filter(d, TRUE);
1414 d = filelist_sort_path(d);
1415 filelist_recursive_append(list, d);
1423 GList *filelist_recursive(const gchar *path)
1428 if (!filelist_read(path, &list, &d)) return NULL;
1429 list = filelist_filter(list, FALSE);
1430 list = filelist_sort_path(list);
1432 d = filelist_filter(d, TRUE);
1433 d = filelist_sort_path(d);
1434 filelist_recursive_append(&list, d);