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);
220 static GList *filter_to_list(const gchar *extensions)
225 if (!extensions) return NULL;
234 while (*p != '\0' && *p != ';')
239 list = g_list_append(list, g_strndup(b, l));
246 void filter_rebuild(void)
250 path_list_free(extension_list);
251 extension_list = NULL;
265 ext = filter_to_list(fe->extensions);
266 if (ext) extension_list = g_list_concat(extension_list, ext);
271 gint filter_name_exists(const gchar *name)
274 if (!extension_list || file_filter_disable) return TRUE;
276 work = extension_list;
279 gchar *filter = work->data;
280 gint lf = strlen(filter);
281 gint ln = strlen(name);
284 if (strncasecmp(name + ln - lf, filter, lf) == 0) return TRUE;
292 void filter_write_list(FILE *f)
299 FilterEntry *fe = work->data;
302 fprintf(f, "filter_ext: \"%s%s\" \"%s\" \"%s\"\n", (fe->enabled) ? "" : "#",
303 fe->key, fe->extensions,
304 (fe->description) ? fe->description : "");
308 void filter_parse(const gchar *text)
316 if (!text || text[0] != '"') return;
318 key = quoted_value(text);
323 while (*p != '"' && *p != '\0') p++;
330 while (*p != '"' && *p != '\0') p++;
337 ext = quoted_value(p);
340 while (*p != '"' && *p != '\0') p++;
342 while (*p != '"' && *p != '\0') p++;
346 desc = quoted_value(p);
353 if (key && key[0] == '#')
356 tmp = g_strdup(key + 1);
363 if (key && strlen(key) > 0 && ext) filter_add(key, desc, ext, enabled);
370 GList *path_list_filter(GList *list, gint is_dir_list)
374 if (!is_dir_list && file_filter_disable && show_dot_files) return list;
379 gchar *name = work->data;
382 base = filename_from_path(name);
384 if ((!show_dot_files && ishidden(base)) ||
385 (!is_dir_list && !filter_name_exists(base)) ||
386 (is_dir_list && base[0] == '.' && (strcmp(base, GQVIEW_CACHE_LOCAL_THUMB) == 0 ||
387 strcmp(base, GQVIEW_CACHE_LOCAL_METADATA) == 0)) )
391 list = g_list_remove_link(list, link);
405 *-----------------------------------------------------------------------------
406 * path list recursive
407 *-----------------------------------------------------------------------------
410 static gint path_list_sort_cb(gconstpointer a, gconstpointer b)
412 return CASE_SORT((gchar *)a, (gchar *)b);
415 GList *path_list_sort(GList *list)
417 return g_list_sort(list, path_list_sort_cb);
420 static void path_list_recursive_append(GList **list, GList *dirs)
427 const gchar *path = work->data;
431 if (path_list(path, &f, &d))
433 f = path_list_filter(f, FALSE);
434 f = path_list_sort(f);
435 *list = g_list_concat(*list, f);
437 d = path_list_filter(d, TRUE);
438 d = path_list_sort(d);
439 path_list_recursive_append(list, d);
447 GList *path_list_recursive(const gchar *path)
452 if (!path_list(path, &list, &d)) return NULL;
453 list = path_list_filter(list, FALSE);
454 list = path_list_sort(list);
456 d = path_list_filter(d, TRUE);
457 d = path_list_sort(d);
458 path_list_recursive_append(&list, d);
465 *-----------------------------------------------------------------------------
466 * text conversion utils
467 *-----------------------------------------------------------------------------
470 gchar *text_from_size(gint64 size)
476 /* what I would like to use is printf("%'d", size)
477 * BUT: not supported on every libc :(
479 if (size > G_MAXUINT)
481 /* the %lld conversion is not valid in all libcs, so use a simple work-around */
482 a = g_strdup_printf("%d%09d", (guint)(size / 1000000000), (guint)(size % 1000000000));
486 a = g_strdup_printf("%d", (guint)size);
492 b = g_new(gchar, l + n + 1);
517 gchar *text_from_size_abrev(gint64 size)
519 if (size < (gint64)1024)
521 return g_strdup_printf(_("%d bytes"), (gint)size);
523 if (size < (gint64)1048576)
525 return g_strdup_printf(_("%.1f K"), (double)size / 1024.0);
527 if (size < (gint64)1073741824)
529 return g_strdup_printf(_("%.1f MB"), (double)size / 1048576.0);
532 /* to avoid overflowing the double, do division in two steps */
534 return g_strdup_printf(_("%.1f GB"), (double)size / 1024.0);
537 /* note: returned string is valid until next call to text_from_time() */
538 const gchar *text_from_time(time_t t)
540 static gchar *ret = NULL;
544 GError *error = NULL;
546 btime = localtime(&t);
548 /* the %x warning about 2 digit years is not an error */
549 buflen = strftime(buf, sizeof(buf), "%x %H:%M", btime);
550 if (buflen < 1) return "";
553 ret = g_locale_to_utf8(buf, buflen, NULL, NULL, &error);
556 printf("Error converting locale strftime to UTF-8: %s\n", error->message);
565 *-----------------------------------------------------------------------------
567 *-----------------------------------------------------------------------------
570 FileData *file_data_new(const gchar *path, struct stat *st)
574 fd = g_new0(FileData, 1);
575 fd->path = path_to_utf8(path);
576 fd->name = filename_from_path(fd->path);
577 fd->size = st->st_size;
578 fd->date = st->st_mtime;
584 FileData *file_data_new_simple(const gchar *path)
589 fd = g_new0(FileData, 1);
590 fd->path = g_strdup(path);
591 fd->name = filename_from_path(fd->path);
593 if (stat_utf8(fd->path, &st))
595 fd->size = st.st_size;
596 fd->date = st.st_mtime;
604 void file_data_free(FileData *fd)
607 if (fd->pixbuf) g_object_unref(fd->pixbuf);
612 *-----------------------------------------------------------------------------
614 *-----------------------------------------------------------------------------
617 static SortType filelist_sort_method = SORT_NONE;
618 static gint filelist_sort_ascend = TRUE;
620 static gint sort_file_cb(void *a, void *b)
625 if (!filelist_sort_ascend)
631 switch (filelist_sort_method)
634 if (fa->size < fb->size) return -1;
635 if (fa->size > fb->size) return 1;
639 if (fa->date < fb->date) return -1;
640 if (fa->date > fb->date) return 1;
643 #ifdef HAVE_STRVERSCMP
645 return strverscmp(fa->name, fb->name);
650 return CASE_SORT(fa->name, fb->name);
655 GList *filelist_sort(GList *list, SortType method, gint ascend)
657 filelist_sort_method = method;
658 filelist_sort_ascend = ascend;
659 return g_list_sort(list, (GCompareFunc) sort_file_cb);
662 GList *filelist_insert_sort(GList *list, FileData *fd, SortType method, gint ascend)
664 filelist_sort_method = method;
665 filelist_sort_ascend = ascend;
666 return g_list_insert_sorted(list, fd, (GCompareFunc) sort_file_cb);
669 gint filelist_read(const gchar *path, GList **files, GList **dirs)
673 struct stat ent_sbuf;
681 pathl = path_from_utf8(path);
682 if (!pathl || (dp = opendir(pathl)) == NULL)
685 if (files) *files = NULL;
686 if (dirs) *dirs = NULL;
691 if (pathl[0] == '/' && pathl[1] == '\0')
694 pathl = g_strdup("");
697 while ((dir = readdir(dp)) != NULL)
699 gchar *name = dir->d_name;
700 if (show_dot_files || !ishidden(name))
702 gchar *filepath = g_strconcat(pathl, "/", name, NULL);
703 if (stat(filepath, &ent_sbuf) >= 0)
705 if (S_ISDIR(ent_sbuf.st_mode))
707 /* we ignore the .thumbnails dir for cleanliness */
709 !(name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) &&
710 strcmp(name, GQVIEW_CACHE_LOCAL_THUMB) != 0 &&
711 strcmp(name, GQVIEW_CACHE_LOCAL_METADATA) != 0 &&
712 strcmp(name, THUMB_FOLDER_LOCAL) != 0)
714 dlist = g_list_prepend(dlist, file_data_new(filepath, &ent_sbuf));
719 if ((files) && filter_name_exists(name))
721 flist = g_list_prepend(flist, file_data_new(filepath, &ent_sbuf));
733 if (dirs) *dirs = dlist;
734 if (files) *files = flist;
739 void filelist_free(GList *list)
746 file_data_free((FileData *)work->data);