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;
31 gint ishidden(const gchar *name)
33 if (name[0] != '.') return FALSE;
34 if (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) return FALSE;
38 static FilterEntry *filter_entry_new(const gchar *key, const gchar *description,
39 const gchar *extensions, gint enabled)
43 fe = g_new0(FilterEntry, 1);
44 fe->key = g_strdup(key);
45 fe->description = g_strdup(description);
46 fe->extensions = g_strdup(extensions);
47 fe->enabled = enabled;
52 static void filter_entry_free(FilterEntry *fe)
57 g_free(fe->description);
58 g_free(fe->extensions);
62 GList *filter_get_list(void)
67 void filter_remove_entry(FilterEntry *fe)
69 if (!g_list_find(filter_list, fe)) return;
71 filter_list = g_list_remove(filter_list, fe);
72 filter_entry_free(fe);
75 static gint filter_key_exists(const gchar *key)
79 if (!key) return FALSE;
84 FilterEntry *fe = work->data;
87 if (strcmp(fe->key, key) == 0) return TRUE;
93 void filter_add(const gchar *key, const gchar *description, const gchar *extensions, gint enabled)
95 filter_list = g_list_append(filter_list, filter_entry_new(key, description, extensions, enabled));
98 void filter_add_unique(const gchar *description, const gchar *extensions, gint enabled)
103 key = g_strdup("user0");
105 while (filter_key_exists(key))
109 key = g_strdup_printf("user%d", n);
113 filter_add(key, description, extensions, enabled);
117 static void filter_add_if_missing(const gchar *key, const gchar *description, const gchar *extensions, gint enabled)
126 FilterEntry *fe = work->data;
128 if (fe->key && strcmp(fe->key, key) == 0) return;
131 filter_add(key, description, extensions, enabled);
134 void filter_reset(void)
141 FilterEntry *fe = work->data;
143 filter_entry_free(fe);
146 g_list_free(filter_list);
150 void filter_add_defaults(void)
154 list = gdk_pixbuf_get_formats();
158 GdkPixbufFormat *format;
162 GString *filter = NULL;
168 name = gdk_pixbuf_format_get_name(format);
169 desc = gdk_pixbuf_format_get_description(format);
170 extensions = gdk_pixbuf_format_get_extensions(format);
173 while (extensions[i])
177 filter = g_string_new(".");
178 filter = g_string_append(filter, extensions[i]);
182 filter = g_string_append(filter, ";.");
183 filter = g_string_append(filter, extensions[i]);
188 if (debug) printf("loader reported [%s] [%s] [%s]\n", name, desc, filter->str);
190 filter_add_if_missing(name, desc, filter->str, TRUE);
194 g_strfreev(extensions);
195 g_string_free(filter, TRUE);
199 /* add defaults even if gdk-pixbuf does not have them, but disabled */
200 filter_add_if_missing("jpeg", "JPEG group", ".jpg;.jpeg;.jpe", FALSE);
201 filter_add_if_missing("png", "Portable Network Graphic", ".png", FALSE);
202 filter_add_if_missing("tiff", "Tiff", ".tif;.tiff", FALSE);
203 filter_add_if_missing("pnm", "Packed Pixel formats", ".pbm;.pgm;.pnm;.ppm", FALSE);
204 filter_add_if_missing("gif", "Graphics Interchange Format", ".gif", FALSE);
205 filter_add_if_missing("xbm", "X bitmap", ".xbm", FALSE);
206 filter_add_if_missing("xpm", "X pixmap", ".xpm", FALSE);
207 filter_add_if_missing("bmp", "Bitmap", ".bmp", FALSE);
208 filter_add_if_missing("ico", "Icon file", ".ico;.cur", FALSE);
209 filter_add_if_missing("ras", "Raster", ".ras", FALSE);
210 filter_add_if_missing("svg", "Scalable Vector Graphics", ".svg", FALSE);
212 /* These are the raw camera formats with embedded jpeg/exif.
215 filter_add_if_missing("crw", "Canon raw format", ".crw;.cr2", TRUE);
216 filter_add_if_missing("raf", "Fujifilm raw format", ".raf", TRUE);
217 filter_add_if_missing("nef", "Nikon raw format", ".nef", TRUE);
218 filter_add_if_missing("orf", "Olympus raw format", ".orf", TRUE);
219 filter_add_if_missing("pef", "Pentax raw format", ".pef", TRUE);
222 static GList *filter_to_list(const gchar *extensions)
227 if (!extensions) return NULL;
236 while (*p != '\0' && *p != ';')
241 list = g_list_append(list, g_strndup(b, l));
248 void filter_rebuild(void)
252 path_list_free(extension_list);
253 extension_list = NULL;
267 ext = filter_to_list(fe->extensions);
268 if (ext) extension_list = g_list_concat(extension_list, ext);
273 gint filter_name_exists(const gchar *name)
276 if (!extension_list || file_filter_disable) return TRUE;
278 work = extension_list;
281 gchar *filter = work->data;
282 gint lf = strlen(filter);
283 gint ln = strlen(name);
286 if (strncasecmp(name + ln - lf, filter, lf) == 0) return TRUE;
294 void filter_write_list(FILE *f)
301 FilterEntry *fe = work->data;
304 fprintf(f, "filter_ext: \"%s%s\" \"%s\" \"%s\"\n", (fe->enabled) ? "" : "#",
305 fe->key, fe->extensions,
306 (fe->description) ? fe->description : "");
310 void filter_parse(const gchar *text)
318 if (!text || text[0] != '"') return;
320 key = quoted_value(text);
325 while (*p != '"' && *p != '\0') p++;
332 while (*p != '"' && *p != '\0') p++;
339 ext = quoted_value(p);
342 while (*p != '"' && *p != '\0') p++;
344 while (*p != '"' && *p != '\0') p++;
348 desc = quoted_value(p);
355 if (key && key[0] == '#')
358 tmp = g_strdup(key + 1);
365 if (key && strlen(key) > 0 && ext) filter_add(key, desc, ext, enabled);
372 GList *path_list_filter(GList *list, gint is_dir_list)
376 if (!is_dir_list && file_filter_disable && show_dot_files) return list;
381 gchar *name = work->data;
384 base = filename_from_path(name);
386 if ((!show_dot_files && ishidden(base)) ||
387 (!is_dir_list && !filter_name_exists(base)) ||
388 (is_dir_list && base[0] == '.' && (strcmp(base, GQVIEW_CACHE_LOCAL_THUMB) == 0 ||
389 strcmp(base, GQVIEW_CACHE_LOCAL_METADATA) == 0)) )
393 list = g_list_remove_link(list, link);
407 *-----------------------------------------------------------------------------
408 * path list recursive
409 *-----------------------------------------------------------------------------
412 static gint path_list_sort_cb(gconstpointer a, gconstpointer b)
414 return CASE_SORT((gchar *)a, (gchar *)b);
417 GList *path_list_sort(GList *list)
419 return g_list_sort(list, path_list_sort_cb);
422 static void path_list_recursive_append(GList **list, GList *dirs)
429 const gchar *path = work->data;
433 if (path_list(path, &f, &d))
435 f = path_list_filter(f, FALSE);
436 f = path_list_sort(f);
437 *list = g_list_concat(*list, f);
439 d = path_list_filter(d, TRUE);
440 d = path_list_sort(d);
441 path_list_recursive_append(list, d);
449 GList *path_list_recursive(const gchar *path)
454 if (!path_list(path, &list, &d)) return NULL;
455 list = path_list_filter(list, FALSE);
456 list = path_list_sort(list);
458 d = path_list_filter(d, TRUE);
459 d = path_list_sort(d);
460 path_list_recursive_append(&list, d);
467 *-----------------------------------------------------------------------------
468 * text conversion utils
469 *-----------------------------------------------------------------------------
472 gchar *text_from_size(gint64 size)
478 /* what I would like to use is printf("%'d", size)
479 * BUT: not supported on every libc :(
481 if (size > G_MAXUINT)
483 /* the %lld conversion is not valid in all libcs, so use a simple work-around */
484 a = g_strdup_printf("%d%09d", (guint)(size / 1000000000), (guint)(size % 1000000000));
488 a = g_strdup_printf("%d", (guint)size);
494 b = g_new(gchar, l + n + 1);
519 gchar *text_from_size_abrev(gint64 size)
521 if (size < (gint64)1024)
523 return g_strdup_printf(_("%d bytes"), (gint)size);
525 if (size < (gint64)1048576)
527 return g_strdup_printf(_("%.1f K"), (double)size / 1024.0);
529 if (size < (gint64)1073741824)
531 return g_strdup_printf(_("%.1f MB"), (double)size / 1048576.0);
534 /* to avoid overflowing the double, do division in two steps */
536 return g_strdup_printf(_("%.1f GB"), (double)size / 1024.0);
539 /* note: returned string is valid until next call to text_from_time() */
540 const gchar *text_from_time(time_t t)
542 static gchar *ret = NULL;
546 GError *error = NULL;
548 btime = localtime(&t);
550 /* the %x warning about 2 digit years is not an error */
551 buflen = strftime(buf, sizeof(buf), "%x %H:%M", btime);
552 if (buflen < 1) return "";
555 ret = g_locale_to_utf8(buf, buflen, NULL, NULL, &error);
558 printf("Error converting locale strftime to UTF-8: %s\n", error->message);
567 *-----------------------------------------------------------------------------
569 *-----------------------------------------------------------------------------
572 FileData *file_data_new(const gchar *path, struct stat *st)
576 fd = g_new0(FileData, 1);
577 fd->path = path_to_utf8(path);
578 fd->name = filename_from_path(fd->path);
579 fd->size = st->st_size;
580 fd->date = st->st_mtime;
586 FileData *file_data_new_simple(const gchar *path)
591 fd = g_new0(FileData, 1);
592 fd->path = g_strdup(path);
593 fd->name = filename_from_path(fd->path);
595 if (stat_utf8(fd->path, &st))
597 fd->size = st.st_size;
598 fd->date = st.st_mtime;
606 void file_data_free(FileData *fd)
609 if (fd->pixbuf) g_object_unref(fd->pixbuf);
614 *-----------------------------------------------------------------------------
616 *-----------------------------------------------------------------------------
619 static SortType filelist_sort_method = SORT_NONE;
620 static gint filelist_sort_ascend = TRUE;
622 static gint sort_file_cb(void *a, void *b)
627 if (!filelist_sort_ascend)
633 switch (filelist_sort_method)
636 if (fa->size < fb->size) return -1;
637 if (fa->size > fb->size) return 1;
641 if (fa->date < fb->date) return -1;
642 if (fa->date > fb->date) return 1;
645 #ifdef HAVE_STRVERSCMP
647 return strverscmp(fa->name, fb->name);
652 return CASE_SORT(fa->name, fb->name);
657 GList *filelist_sort(GList *list, SortType method, gint ascend)
659 filelist_sort_method = method;
660 filelist_sort_ascend = ascend;
661 return g_list_sort(list, (GCompareFunc) sort_file_cb);
664 GList *filelist_insert_sort(GList *list, FileData *fd, SortType method, gint ascend)
666 filelist_sort_method = method;
667 filelist_sort_ascend = ascend;
668 return g_list_insert_sorted(list, fd, (GCompareFunc) sort_file_cb);
671 gint filelist_read(const gchar *path, GList **files, GList **dirs)
675 struct stat ent_sbuf;
683 pathl = path_from_utf8(path);
684 if (!pathl || (dp = opendir(pathl)) == NULL)
687 if (files) *files = NULL;
688 if (dirs) *dirs = NULL;
693 if (pathl[0] == '/' && pathl[1] == '\0')
696 pathl = g_strdup("");
699 while ((dir = readdir(dp)) != NULL)
701 gchar *name = dir->d_name;
702 if (show_dot_files || !ishidden(name))
704 gchar *filepath = g_strconcat(pathl, "/", name, NULL);
705 if (stat(filepath, &ent_sbuf) >= 0)
707 if (S_ISDIR(ent_sbuf.st_mode))
709 /* we ignore the .thumbnails dir for cleanliness */
711 !(name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) &&
712 strcmp(name, GQVIEW_CACHE_LOCAL_THUMB) != 0 &&
713 strcmp(name, GQVIEW_CACHE_LOCAL_METADATA) != 0 &&
714 strcmp(name, THUMB_FOLDER_LOCAL) != 0)
716 dlist = g_list_prepend(dlist, file_data_new(filepath, &ent_sbuf));
721 if ((files) && filter_name_exists(name))
723 flist = g_list_prepend(flist, file_data_new(filepath, &ent_sbuf));
735 if (dirs) *dirs = dlist;
736 if (files) *files = flist;
741 void filelist_free(GList *list)
748 file_data_free((FileData *)work->data);