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];
35 gint ishidden(const gchar *name)
37 if (name[0] != '.') return FALSE;
38 if (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) return FALSE;
42 static FilterEntry *filter_entry_new(const gchar *key, const gchar *description,
43 const gchar *extensions, FileFormatClass file_class, gint enabled)
47 fe = g_new0(FilterEntry, 1);
48 fe->key = g_strdup(key);
49 fe->description = g_strdup(description);
50 fe->extensions = g_strdup(extensions);
51 fe->enabled = enabled;
52 fe->file_class = file_class;
57 static void filter_entry_free(FilterEntry *fe)
62 g_free(fe->description);
63 g_free(fe->extensions);
67 GList *filter_get_list(void)
72 void filter_remove_entry(FilterEntry *fe)
74 if (!g_list_find(filter_list, fe)) return;
76 filter_list = g_list_remove(filter_list, fe);
77 filter_entry_free(fe);
80 static gint filter_key_exists(const gchar *key)
84 if (!key) return FALSE;
89 FilterEntry *fe = work->data;
92 if (strcmp(fe->key, key) == 0) return TRUE;
98 void filter_add(const gchar *key, const gchar *description, const gchar *extensions, FileFormatClass file_class, gint enabled)
100 filter_list = g_list_append(filter_list, filter_entry_new(key, description, extensions, file_class, enabled));
103 void filter_add_unique(const gchar *description, const gchar *extensions, FileFormatClass file_class, gint enabled)
108 key = g_strdup("user0");
110 while (filter_key_exists(key))
114 key = g_strdup_printf("user%d", n);
118 filter_add(key, description, extensions, file_class, enabled);
122 static void filter_add_if_missing(const gchar *key, const gchar *description, const gchar *extensions, FileFormatClass file_class, gint enabled)
131 FilterEntry *fe = work->data;
133 if (fe->key && strcmp(fe->key, key) == 0)
135 if (fe->file_class == FORMAT_CLASS_UNKNOWN)
136 fe->file_class = file_class; /* for compatibility */
141 filter_add(key, description, extensions, file_class, enabled);
144 void filter_reset(void)
151 FilterEntry *fe = work->data;
153 filter_entry_free(fe);
156 g_list_free(filter_list);
160 void filter_add_defaults(void)
164 list = gdk_pixbuf_get_formats();
168 GdkPixbufFormat *format;
172 GString *filter = NULL;
178 name = gdk_pixbuf_format_get_name(format);
179 desc = gdk_pixbuf_format_get_description(format);
180 extensions = gdk_pixbuf_format_get_extensions(format);
183 while (extensions[i])
187 filter = g_string_new(".");
188 filter = g_string_append(filter, extensions[i]);
192 filter = g_string_append(filter, ";.");
193 filter = g_string_append(filter, extensions[i]);
198 if (debug) printf("loader reported [%s] [%s] [%s]\n", name, desc, filter->str);
200 filter_add_if_missing(name, desc, filter->str, FORMAT_CLASS_IMAGE, TRUE);
204 g_strfreev(extensions);
205 g_string_free(filter, TRUE);
209 /* add defaults even if gdk-pixbuf does not have them, but disabled */
210 filter_add_if_missing("jpeg", "JPEG group", ".jpg;.jpeg;.jpe", FORMAT_CLASS_IMAGE, FALSE);
211 filter_add_if_missing("png", "Portable Network Graphic", ".png", FORMAT_CLASS_IMAGE, FALSE);
212 filter_add_if_missing("tiff", "Tiff", ".tif;.tiff", FORMAT_CLASS_IMAGE, FALSE);
213 filter_add_if_missing("pnm", "Packed Pixel formats", ".pbm;.pgm;.pnm;.ppm", FORMAT_CLASS_IMAGE, FALSE);
214 filter_add_if_missing("gif", "Graphics Interchange Format", ".gif", FORMAT_CLASS_IMAGE, FALSE);
215 filter_add_if_missing("xbm", "X bitmap", ".xbm", FORMAT_CLASS_IMAGE, FALSE);
216 filter_add_if_missing("xpm", "X pixmap", ".xpm", FORMAT_CLASS_IMAGE, FALSE);
217 filter_add_if_missing("bmp", "Bitmap", ".bmp", FORMAT_CLASS_IMAGE, FALSE);
218 filter_add_if_missing("ico", "Icon file", ".ico;.cur", FORMAT_CLASS_IMAGE, FALSE);
219 filter_add_if_missing("ras", "Raster", ".ras", FORMAT_CLASS_IMAGE, FALSE);
220 filter_add_if_missing("svg", "Scalable Vector Graphics", ".svg", FORMAT_CLASS_IMAGE, FALSE);
222 /* non-image files that might be desirable to show */
223 filter_add_if_missing("xmp", "XMP sidecar", ".xmp", FORMAT_CLASS_META, TRUE);
225 /* These are the raw camera formats with embedded jpeg/exif.
226 * (see format_raw.c and/or exiv2.cc)
228 filter_add_if_missing("arw", "Sony raw format", ".arw;.srf;.sr2", FORMAT_CLASS_RAWIMAGE, TRUE);
229 filter_add_if_missing("crw", "Canon raw format", ".crw;.cr2", FORMAT_CLASS_RAWIMAGE, TRUE);
230 filter_add_if_missing("kdc", "Kodak raw format", ".kdc;.dcr", FORMAT_CLASS_RAWIMAGE, TRUE);
231 filter_add_if_missing("raf", "Fujifilm raw format", ".raf", FORMAT_CLASS_RAWIMAGE, TRUE);
232 filter_add_if_missing("mef", "Mamiya raw format", ".mef;.mos", FORMAT_CLASS_RAWIMAGE, TRUE);
233 filter_add_if_missing("mrw", "Minolta raw format", ".mrw", FORMAT_CLASS_RAWIMAGE, TRUE);
234 filter_add_if_missing("nef", "Nikon raw format", ".nef", FORMAT_CLASS_RAWIMAGE, TRUE);
235 filter_add_if_missing("orf", "Olympus raw format", ".orf", FORMAT_CLASS_RAWIMAGE, TRUE);
236 filter_add_if_missing("pef", "Pentax raw format", ".pef;.ptx", FORMAT_CLASS_RAWIMAGE, TRUE);
237 filter_add_if_missing("dng", "Adobe Digital Negative raw format", ".dng", FORMAT_CLASS_RAWIMAGE, TRUE);
238 filter_add_if_missing("x3f", "Sigma raw format", ".x3f", FORMAT_CLASS_RAWIMAGE, TRUE);
239 filter_add_if_missing("raw", "Panasonic raw format", ".raw", FORMAT_CLASS_RAWIMAGE, TRUE);
240 filter_add_if_missing("r3d", "Red raw format", ".r3d", FORMAT_CLASS_RAWIMAGE, TRUE);
241 filter_add_if_missing("3fr", "Hasselblad raw format", ".3fr", FORMAT_CLASS_RAWIMAGE, TRUE);
242 filter_add_if_missing("erf", "Epson raw format", ".erf", FORMAT_CLASS_RAWIMAGE, TRUE);
245 GList *filter_to_list(const gchar *extensions)
250 if (!extensions) return NULL;
259 while (*p != '\0' && *p != ';')
264 list = g_list_append(list, g_strndup(b, l));
271 void filter_rebuild(void)
276 string_list_free(extension_list);
277 extension_list = NULL;
279 for (i = 0; i < FILE_FORMAT_CLASSES; i++)
281 string_list_free(file_class_extension_list[i]);
282 file_class_extension_list[i] = NULL;
297 ext = filter_to_list(fe->extensions);
298 if (ext) extension_list = g_list_concat(extension_list, ext);
300 if (fe->file_class >= 0 && fe->file_class < FILE_FORMAT_CLASSES)
302 ext = filter_to_list(fe->extensions);
303 if (ext) file_class_extension_list[fe->file_class] = g_list_concat(file_class_extension_list[fe->file_class], ext);
307 printf("WARNING: invalid file class %d\n", fe->file_class);
313 gint filter_name_exists(const gchar *name)
318 if (!extension_list || file_filter_disable) return TRUE;
321 work = extension_list;
324 gchar *filter = work->data;
325 gint lf = strlen(filter);
329 if (strncasecmp(name + ln - lf, filter, lf) == 0) return TRUE;
337 gint filter_file_class(const gchar *name, FileFormatClass file_class)
342 if (file_class < 0 || file_class >= FILE_FORMAT_CLASSES)
344 printf("WARNING: invalid file class %d\n", file_class);
349 work = file_class_extension_list[file_class];
352 gchar *filter = work->data;
353 gint lf = strlen(filter);
357 if (strncasecmp(name + ln - lf, filter, lf) == 0) return TRUE;
365 void filter_write_list(FILE *f)
372 FilterEntry *fe = work->data;
375 gchar *extensions = escquote_value(fe->extensions);
376 gchar *description = escquote_value(fe->description);
378 fprintf(f, "filter_ext: \"%s%s\" %s %s\n", (fe->enabled) ? "" : "#",
379 fe->key, extensions, description);
385 void filter_parse(const gchar *text)
393 if (!text || text[0] != '"') return;
395 key = quoted_value(text, &p);
398 ext = quoted_value(p, &p);
399 desc = quoted_value(p, &p);
401 if (key && key[0] == '#')
404 tmp = g_strdup(key + 1);
411 if (key && strlen(key) > 0 && ext) filter_add(key, desc, ext, FORMAT_CLASS_UNKNOWN, enabled);
418 GList *path_list_filter(GList *list, gint is_dir_list)
422 if (!is_dir_list && file_filter_disable && show_dot_files) return list;
427 gchar *name = work->data;
430 base = filename_from_path(name);
432 if ((!show_dot_files && ishidden(base)) ||
433 (!is_dir_list && !filter_name_exists(base)) ||
434 (is_dir_list && base[0] == '.' && (strcmp(base, GQVIEW_CACHE_LOCAL_THUMB) == 0 ||
435 strcmp(base, GQVIEW_CACHE_LOCAL_METADATA) == 0)) )
439 list = g_list_remove_link(list, link);
454 *-----------------------------------------------------------------------------
455 * sidecar extension list
456 *-----------------------------------------------------------------------------
459 static GList *sidecar_ext_get_list(void)
461 return sidecar_ext_list;
464 void sidecar_ext_parse(const gchar *text, gint quoted)
469 work = sidecar_ext_list;
472 gchar *ext = work->data;
476 g_list_free(sidecar_ext_list);
477 sidecar_ext_list = NULL;
480 value = quoted_value(text, NULL);
482 value = g_strdup(text);
484 if (value == NULL) return;
486 sidecar_ext_list = filter_to_list(value);
491 void sidecar_ext_write(FILE *f)
494 fprintf(f, "\nsidecar_ext: \"%s\"\n", sidecar_ext_to_string());
497 char *sidecar_ext_to_string()
500 GString *str = g_string_new("");
502 work = sidecar_ext_list;
505 gchar *ext = work->data;
507 g_string_append(str, ext);
508 if (work) g_string_append(str, ";");
510 return g_string_free(str, FALSE);
513 void sidecar_ext_add_defaults()
515 sidecar_ext_parse(".jpg;.cr2;.nef;.crw;.xmp", FALSE);
519 *-----------------------------------------------------------------------------
520 * path list recursive
521 *-----------------------------------------------------------------------------
524 static gint path_list_sort_cb(gconstpointer a, gconstpointer b)
526 return CASE_SORT((gchar *)a, (gchar *)b);
529 GList *path_list_sort(GList *list)
531 return g_list_sort(list, path_list_sort_cb);
534 static void path_list_recursive_append(GList **list, GList *dirs)
541 const gchar *path = work->data;
545 if (path_list(path, &f, &d))
547 f = path_list_filter(f, FALSE);
548 f = path_list_sort(f);
549 *list = g_list_concat(*list, f);
551 d = path_list_filter(d, TRUE);
552 d = path_list_sort(d);
553 path_list_recursive_append(list, d);
561 GList *path_list_recursive(const gchar *path)
566 if (!path_list(path, &list, &d)) return NULL;
567 list = path_list_filter(list, FALSE);
568 list = path_list_sort(list);
570 d = path_list_filter(d, TRUE);
571 d = path_list_sort(d);
572 path_list_recursive_append(&list, d);
579 *-----------------------------------------------------------------------------
580 * text conversion utils
581 *-----------------------------------------------------------------------------
584 gchar *text_from_size(gint64 size)
590 /* what I would like to use is printf("%'d", size)
591 * BUT: not supported on every libc :(
593 if (size > G_MAXUINT)
595 /* the %lld conversion is not valid in all libcs, so use a simple work-around */
596 a = g_strdup_printf("%d%09d", (guint)(size / 1000000000), (guint)(size % 1000000000));
600 a = g_strdup_printf("%d", (guint)size);
606 b = g_new(gchar, l + n + 1);
631 gchar *text_from_size_abrev(gint64 size)
633 if (size < (gint64)1024)
635 return g_strdup_printf(_("%d bytes"), (gint)size);
637 if (size < (gint64)1048576)
639 return g_strdup_printf(_("%.1f K"), (double)size / 1024.0);
641 if (size < (gint64)1073741824)
643 return g_strdup_printf(_("%.1f MB"), (double)size / 1048576.0);
646 /* to avoid overflowing the double, do division in two steps */
648 return g_strdup_printf(_("%.1f GB"), (double)size / 1024.0);
651 /* note: returned string is valid until next call to text_from_time() */
652 const gchar *text_from_time(time_t t)
654 static gchar *ret = NULL;
658 GError *error = NULL;
660 btime = localtime(&t);
662 /* the %x warning about 2 digit years is not an error */
663 buflen = strftime(buf, sizeof(buf), "%x %H:%M", btime);
664 if (buflen < 1) return "";
667 ret = g_locale_to_utf8(buf, buflen, NULL, NULL, &error);
670 printf("Error converting locale strftime to UTF-8: %s\n", error->message);
679 *-----------------------------------------------------------------------------
681 *-----------------------------------------------------------------------------
684 FileData *file_data_merge_sidecar_files(FileData *target, FileData *source);
685 static void file_data_check_sidecars(FileData *fd);
686 FileData *file_data_disconnect_sidecar_file(FileData *target, FileData *sfd);
689 static void file_data_set_path(FileData *fd, const gchar *path)
692 if (strcmp(path, "/") == 0)
694 fd->path = g_strdup(path);
696 fd->extension = fd->name + 1;
700 fd->path = g_strdup(path);
701 fd->name = filename_from_path(fd->path);
703 if (strcmp(fd->name, "..") == 0)
705 gchar *dir = remove_level_from_path(path);
707 fd->path = remove_level_from_path(dir);
710 fd->extension = fd->name + 2;
713 else if (strcmp(fd->name, ".") == 0)
716 fd->path = remove_level_from_path(path);
718 fd->extension = fd->name + 1;
722 fd->extension = extension_from_path(fd->path);
723 if (fd->extension == NULL)
724 fd->extension = fd->name + strlen(fd->name);
727 static void file_data_check_changed_files(FileData *fd, struct stat *st)
730 if (fd->size != st->st_size ||
731 fd->date != st->st_mtime)
733 fd->size = st->st_size;
734 fd->date = st->st_mtime;
735 if (fd->pixbuf) g_object_unref(fd->pixbuf);
739 work = fd->sidecar_files;
742 FileData *sfd = work->data;
745 if (!stat_utf8(sfd->path, &st))
747 file_data_disconnect_sidecar_file(fd, sfd);
750 file_data_check_changed_files(sfd, &st);
755 static GHashTable *file_data_pool = NULL;
757 static FileData *file_data_new(const gchar *path_utf8, struct stat *st, gboolean check_sidecars)
761 if (debug) printf("file_data_new: '%s' %d\n", path_utf8, check_sidecars);
764 file_data_pool = g_hash_table_new (g_str_hash, g_str_equal);
766 fd = g_hash_table_lookup(file_data_pool, path_utf8);
769 file_data_check_changed_files(fd, st);
770 if (debug) printf("file_data_pool hit: '%s'\n", fd->path);
771 return file_data_ref(fd);
774 fd = g_new0(FileData, 1);
776 file_data_set_path(fd, path_utf8);
778 fd->original_path = g_strdup(path_utf8);
779 fd->size = st->st_size;
780 fd->date = st->st_mtime;
782 fd->sidecar_files = NULL;
784 fd->magick = 0x12345678;
786 g_hash_table_insert(file_data_pool, fd->original_path, fd);
788 if (check_sidecars && sidecar_file_priority(fd->extension))
789 file_data_check_sidecars(fd);
793 static void file_data_check_sidecars(FileData *fd)
796 int base_len = fd->extension - fd->path;
797 GString *fname = g_string_new_len(fd->path, base_len);
798 FileData *parent_fd = NULL;
799 GList *work = sidecar_ext_get_list();
802 /* check for possible sidecar files;
803 the sidecar files created here are referenced only via fd->sidecar_files or fd->parent,
804 they have fd->ref set to 0 and file_data unref must chack and free them all together
805 (using fd->ref would cause loops and leaks)
810 gchar *ext = work->data;
813 if (strcmp(ext, fd->extension) == 0)
815 new_fd = fd; /* processing the original file */
820 g_string_truncate(fname, base_len);
821 g_string_append(fname, ext);
823 if (!stat_utf8(fname->str, &nst))
826 new_fd = file_data_new(fname->str, &nst, FALSE);
827 new_fd->ref--; /* do not use ref here */
831 parent_fd = new_fd; /* parent is the one with the highest prio, found first */
833 file_data_merge_sidecar_files(parent_fd, new_fd);
835 g_string_free(fname, TRUE);
839 static FileData *file_data_new_local(const gchar *path, struct stat *st, gboolean check_sidecars)
841 gchar *path_utf8 = path_to_utf8(path);
842 FileData *ret = file_data_new(path_utf8, st, check_sidecars);
847 FileData *file_data_new_simple(const gchar *path_utf8)
851 if (!stat_utf8(path_utf8, &st))
857 return file_data_new(path_utf8, &st, TRUE);
860 FileData *file_data_add_sidecar_file(FileData *target, FileData *sfd)
862 sfd->parent = target;
863 if(!g_list_find(target->sidecar_files, sfd))
864 target->sidecar_files = g_list_prepend(target->sidecar_files, sfd);
869 FileData *file_data_merge_sidecar_files(FileData *target, FileData *source)
872 file_data_add_sidecar_file(target, source);
874 work = source->sidecar_files;
877 FileData *sfd = work->data;
878 file_data_add_sidecar_file(target, sfd);
882 g_list_free(source->sidecar_files);
883 source->sidecar_files = NULL;
885 target->sidecar_files = filelist_sort(target->sidecar_files, SORT_NAME, TRUE);
891 FileData *file_data_ref(FileData *fd)
893 if (fd == NULL) return NULL;
895 // return g_memdup(fd, sizeof(FileData));
896 g_assert(fd->magick == 0x12345678);
901 static void file_data_free(FileData *fd)
903 g_assert(fd->magick == 0x12345678);
904 g_assert(fd->ref == 0);
906 g_hash_table_remove(file_data_pool, fd->original_path);
909 g_free(fd->original_path);
910 if (fd->pixbuf) g_object_unref(fd->pixbuf);
913 g_assert(fd->sidecar_files == NULL); /* sidecar files must be freed before calling this */
915 file_data_change_info_free(NULL, fd);
919 void file_data_unref(FileData *fd)
921 if (fd == NULL) return;
922 g_assert(fd->magick == 0x12345678);
923 if (debug) printf("file_data_unref: '%s'\n", fd->path);
927 FileData *parent = fd->parent ? fd->parent : fd;
934 work = parent->sidecar_files;
937 FileData *sfd = work->data;
943 /* none of parent/children is referenced, we can free everything */
945 if (debug) printf("file_data_unref: deleting '%s', parent '%s'\n", fd->path, parent->path);
947 work = parent->sidecar_files;
950 FileData *sfd = work->data;
955 g_list_free(parent->sidecar_files);
956 parent->sidecar_files = NULL;
958 file_data_free(parent);
963 FileData *file_data_disconnect_sidecar_file(FileData *target, FileData *sfd)
965 sfd->parent = target;
966 g_assert(g_list_find(target->sidecar_files, sfd));
968 target->sidecar_files = g_list_remove(target->sidecar_files, sfd);
979 /* compare name without extension */
980 gint file_data_compare_name_without_ext(FileData *fd1, FileData *fd2)
982 size_t len1 = fd1->extension - fd1->name;
983 size_t len2 = fd2->extension - fd2->name;
985 if (len1 < len2) return -1;
986 if (len1 > len2) return 1;
988 return strncmp(fd1->name, fd2->name, len1);
991 FileData *file_data_do_change(FileData *fd)
994 g_assert(fd->change);
996 g_hash_table_remove(file_data_pool, fd->original_path);
997 g_free(fd->original_path);
998 file_data_set_path(fd, fd->change->dest);
999 fd->original_path = g_strdup(fd->change->dest);
1000 g_hash_table_insert(file_data_pool, fd->original_path, fd);
1004 gboolean file_data_add_change_info(FileData *fd, FileDataChangeType type, const gchar *src, const gchar *dest)
1007 FileDataChangeInfo *fdci;
1009 if (fd->change) return FALSE;
1011 fdci = g_new0(FileDataChangeInfo, 1);
1016 fdci->source = g_strdup(src);
1018 fdci->source = g_strdup(fd->path);
1021 fdci->dest = g_strdup(dest);
1027 void file_data_change_info_free(FileDataChangeInfo *fdci, FileData *fd)
1035 g_free(fdci->source);
1048 *-----------------------------------------------------------------------------
1049 * sidecar file info struct
1050 *-----------------------------------------------------------------------------
1055 gint sidecar_file_priority(const gchar *path)
1057 const char *extension = extension_from_path(path);
1060 if (extension == NULL)
1063 work = sidecar_ext_get_list();
1066 gchar *ext = work->data;
1068 if (strcmp(extension, ext) == 0) return i;
1074 gchar *sidecar_file_data_list_to_string(FileData *fd)
1077 GString *result = g_string_new("");
1079 work = fd->sidecar_files;
1082 FileData *sfd = work->data;
1083 result = g_string_append(result, "+ ");
1084 result = g_string_append(result, sfd->extension);
1086 if (work) result = g_string_append_c(result, ' ');
1089 return g_string_free(result, FALSE);
1093 *-----------------------------------------------------------------------------
1095 *-----------------------------------------------------------------------------
1098 static SortType filelist_sort_method = SORT_NONE;
1099 static gint filelist_sort_ascend = TRUE;
1102 gint filelist_sort_compare_filedata(FileData *fa, FileData *fb)
1104 if (!filelist_sort_ascend)
1111 switch (filelist_sort_method)
1114 if (fa->size < fb->size) return -1;
1115 if (fa->size > fb->size) return 1;
1116 return CASE_SORT(fa->name, fb->name); /* fall back to name */
1119 if (fa->date < fb->date) return -1;
1120 if (fa->date > fb->date) return 1;
1121 return CASE_SORT(fa->name, fb->name); /* fall back to name */
1123 #ifdef HAVE_STRVERSCMP
1125 return strverscmp(fa->name, fb->name);
1130 return CASE_SORT(fa->name, fb->name);
1135 gint filelist_sort_compare_filedata_full(FileData *fa, FileData *fb, SortType method, gint ascend)
1137 filelist_sort_method = method;
1138 filelist_sort_ascend = ascend;
1139 return filelist_sort_compare_filedata(fa, fb);
1142 static gint filelist_sort_file_cb(void *a, void *b)
1144 return filelist_sort_compare_filedata(a, b);
1147 GList *filelist_sort_full(GList *list, SortType method, gint ascend, GCompareFunc cb)
1149 filelist_sort_method = method;
1150 filelist_sort_ascend = ascend;
1151 return g_list_sort(list, cb);
1154 GList *filelist_insert_sort_full(GList *list, void *data, SortType method, gint ascend, GCompareFunc cb)
1156 filelist_sort_method = method;
1157 filelist_sort_ascend = ascend;
1158 return g_list_insert_sorted(list, data, cb);
1161 GList *filelist_sort(GList *list, SortType method, gint ascend)
1163 return filelist_sort_full(list, method, ascend, (GCompareFunc) filelist_sort_file_cb);
1166 GList *filelist_insert_sort(GList *list, FileData *fd, SortType method, gint ascend)
1168 return filelist_insert_sort_full(list, fd, method, ascend, (GCompareFunc) filelist_sort_file_cb);
1172 static GList *filelist_filter_out_sidecars(GList *flist)
1174 GList *work = flist;
1175 GList *flist_filtered = NULL;
1179 FileData *fd = work->data;
1181 if (fd->parent) /* remove fd's that are children */
1182 file_data_unref(fd);
1184 flist_filtered = g_list_prepend(flist_filtered, fd);
1187 return flist_filtered;
1190 static gint filelist_read_real(const gchar *path, GList **files, GList **dirs, gint follow_symlinks)
1194 struct stat ent_sbuf;
1202 pathl = path_from_utf8(path);
1203 if (!pathl || (dp = opendir(pathl)) == NULL)
1206 if (files) *files = NULL;
1207 if (dirs) *dirs = NULL;
1212 if (pathl[0] == '/' && pathl[1] == '\0')
1215 pathl = g_strdup("");
1218 while ((dir = readdir(dp)) != NULL)
1220 gchar *name = dir->d_name;
1221 if (show_dot_files || !ishidden(name))
1223 gchar *filepath = g_strconcat(pathl, "/", name, NULL);
1224 if ((follow_symlinks ?
1225 stat(filepath, &ent_sbuf) :
1226 lstat(filepath, &ent_sbuf)) >= 0)
1228 if (S_ISDIR(ent_sbuf.st_mode))
1230 /* we ignore the .thumbnails dir for cleanliness */
1232 !(name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) &&
1233 strcmp(name, GQVIEW_CACHE_LOCAL_THUMB) != 0 &&
1234 strcmp(name, GQVIEW_CACHE_LOCAL_METADATA) != 0 &&
1235 strcmp(name, THUMB_FOLDER_LOCAL) != 0)
1237 dlist = g_list_prepend(dlist, file_data_new_local(filepath, &ent_sbuf, FALSE));
1242 if ((files) && filter_name_exists(name))
1244 flist = g_list_prepend(flist, file_data_new_local(filepath, &ent_sbuf, TRUE));
1256 flist = filelist_filter_out_sidecars(flist);
1258 if (dirs) *dirs = dlist;
1259 if (files) *files = flist;
1264 gint filelist_read(const gchar *path, GList **files, GList **dirs)
1266 return filelist_read_real(path, files, dirs, TRUE);
1269 gint filelist_read_lstat(const gchar *path, GList **files, GList **dirs)
1271 return filelist_read_real(path, files, dirs, FALSE);
1274 void filelist_free(GList *list)
1281 file_data_unref((FileData *)work->data);
1289 GList *filelist_copy(GList *list)
1291 GList *new_list = NULL;
1302 new_list = g_list_prepend(new_list, file_data_ref(fd));
1305 return g_list_reverse(new_list);
1308 GList *filelist_from_path_list(GList *list)
1310 GList *new_list = NULL;
1321 new_list = g_list_prepend(new_list, file_data_new_simple(path));
1324 return g_list_reverse(new_list);
1327 GList *filelist_to_path_list(GList *list)
1329 GList *new_list = NULL;
1340 new_list = g_list_prepend(new_list, g_strdup(fd->path));
1343 return g_list_reverse(new_list);
1346 GList *filelist_filter(GList *list, gint is_dir_list)
1350 if (!is_dir_list && file_filter_disable && show_dot_files) return list;
1355 FileData *fd = (FileData *)(work->data);
1356 const gchar *name = fd->name;
1358 if ((!show_dot_files && ishidden(name)) ||
1359 (!is_dir_list && !filter_name_exists(name)) ||
1360 (is_dir_list && name[0] == '.' && (strcmp(name, GQVIEW_CACHE_LOCAL_THUMB) == 0 ||
1361 strcmp(name, GQVIEW_CACHE_LOCAL_METADATA) == 0)) )
1365 list = g_list_remove_link(list, link);
1366 file_data_unref(fd);
1379 *-----------------------------------------------------------------------------
1380 * filelist recursive
1381 *-----------------------------------------------------------------------------
1384 static gint filelist_sort_path_cb(gconstpointer a, gconstpointer b)
1386 return CASE_SORT(((FileData *)a)->path, ((FileData *)b)->path);
1389 GList *filelist_sort_path(GList *list)
1391 return g_list_sort(list, filelist_sort_path_cb);
1394 static void filelist_recursive_append(GList **list, GList *dirs)
1401 FileData *fd = (FileData *)(work->data);
1402 const gchar *path = fd->path;
1406 if (filelist_read(path, &f, &d))
1408 f = filelist_filter(f, FALSE);
1409 f = filelist_sort_path(f);
1410 *list = g_list_concat(*list, f);
1412 d = filelist_filter(d, TRUE);
1413 d = filelist_sort_path(d);
1414 filelist_recursive_append(list, d);
1422 GList *filelist_recursive(const gchar *path)
1427 if (!filelist_read(path, &list, &d)) return NULL;
1428 list = filelist_filter(list, FALSE);
1429 list = filelist_sort_path(list);
1431 d = filelist_filter(d, TRUE);
1432 d = filelist_sort_path(d);
1433 filelist_recursive_append(&list, d);