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 "ui_fileops.h"
22 *-----------------------------------------------------------------------------
24 *-----------------------------------------------------------------------------
27 static GList *filter_list = NULL;
28 static GList *extension_list = NULL;
30 gint ishidden(const gchar *name)
32 if (name[0] != '.') return FALSE;
33 if (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) return FALSE;
37 static FilterEntry *filter_entry_new(const gchar *key, const gchar *description,
38 const gchar *extensions, gint enabled)
42 fe = g_new0(FilterEntry, 1);
43 fe->key = g_strdup(key);
44 fe->description = g_strdup(description);
45 fe->extensions = g_strdup(extensions);
46 fe->enabled = enabled;
51 static void filter_entry_free(FilterEntry *fe)
56 g_free(fe->description);
57 g_free(fe->extensions);
61 GList *filter_get_list(void)
66 void filter_remove_entry(FilterEntry *fe)
68 if (!g_list_find(filter_list, fe)) return;
70 filter_list = g_list_remove(filter_list, fe);
71 filter_entry_free(fe);
74 static gint filter_key_exists(const gchar *key)
78 if (!key) return FALSE;
83 FilterEntry *fe = work->data;
86 if (strcmp(fe->key, key) == 0) return TRUE;
92 void filter_add(const gchar *key, const gchar *description, const gchar *extensions, gint enabled)
94 filter_list = g_list_append(filter_list, filter_entry_new(key, description, extensions, enabled));
97 void filter_add_unique(const gchar *description, const gchar *extensions, gint enabled)
102 key = g_strdup("user0");
104 while (filter_key_exists(key))
108 key = g_strdup_printf("user%d", n);
112 filter_add(key, description, extensions, enabled);
116 static void filter_add_if_missing(const gchar *key, const gchar *description, const gchar *extensions, gint enabled)
125 FilterEntry *fe = work->data;
127 if (fe->key && strcmp(fe->key, key) == 0) return;
130 filter_add(key, description, extensions, enabled);
133 void filter_reset(void)
140 FilterEntry *fe = work->data;
142 filter_entry_free(fe);
145 g_list_free(filter_list);
149 void filter_add_defaults(void)
153 list = gdk_pixbuf_get_formats();
157 GdkPixbufFormat *format;
161 GString *filter = NULL;
167 name = gdk_pixbuf_format_get_name(format);
168 desc = gdk_pixbuf_format_get_description(format);
169 extensions = gdk_pixbuf_format_get_extensions(format);
172 while (extensions[i])
176 filter = g_string_new(".");
177 filter = g_string_append(filter, extensions[i]);
181 filter = g_string_append(filter, ";.");
182 filter = g_string_append(filter, extensions[i]);
187 if (debug) printf("loader reported [%s] [%s] [%s]\n", name, desc, filter->str);
189 filter_add_if_missing(name, desc, filter->str, TRUE);
193 g_strfreev(extensions);
194 g_string_free(filter, TRUE);
198 /* add defaults even if gdk-pixbuf does not have them, but disabled */
199 filter_add_if_missing("jpeg", "JPEG group", ".jpg;.jpeg;.jpe", FALSE);
200 filter_add_if_missing("png", "Portable Network Graphic", ".png", FALSE);
201 filter_add_if_missing("tiff", "Tiff", ".tif;.tiff", FALSE);
202 filter_add_if_missing("pnm", "Packed Pixel formats", ".pbm;.pgm;.pnm;.ppm", FALSE);
203 filter_add_if_missing("gif", "Graphics Interchange Format", ".gif", FALSE);
204 filter_add_if_missing("xbm", "X bitmap", ".xbm", FALSE);
205 filter_add_if_missing("xpm", "X pixmap", ".xpm", FALSE);
206 filter_add_if_missing("bmp", "Bitmap", ".bmp", FALSE);
207 filter_add_if_missing("ico", "Icon file", ".ico;.cur", FALSE);
208 filter_add_if_missing("ras", "Raster", ".ras", FALSE);
209 filter_add_if_missing("svg", "Scalable Vector Graphics", ".svg", FALSE);
211 /* These are the raw camera formats with embedded jpeg/exif.
214 filter_add_if_missing("raf", "Fujifilm raw format", ".raf", TRUE);
215 filter_add_if_missing("crw", "Canon raw format", ".crw;.cr2", TRUE);
218 static GList *filter_to_list(const gchar *extensions)
223 if (!extensions) return NULL;
232 while (*p != '\0' && *p != ';')
237 list = g_list_append(list, g_strndup(b, l));
244 void filter_rebuild(void)
248 path_list_free(extension_list);
249 extension_list = NULL;
263 ext = filter_to_list(fe->extensions);
264 if (ext) extension_list = g_list_concat(extension_list, ext);
269 gint filter_name_exists(const gchar *name)
272 if (!extension_list || file_filter_disable) return TRUE;
274 work = extension_list;
277 gchar *filter = work->data;
278 gint lf = strlen(filter);
279 gint ln = strlen(name);
282 if (strncasecmp(name + ln - lf, filter, lf) == 0) return TRUE;
290 void filter_write_list(FILE *f)
297 FilterEntry *fe = work->data;
300 fprintf(f, "filter_ext: \"%s%s\" \"%s\" \"%s\"\n", (fe->enabled) ? "" : "#",
301 fe->key, fe->extensions,
302 (fe->description) ? fe->description : "");
306 void filter_parse(const gchar *text)
314 if (!text || text[0] != '"') return;
316 key = quoted_value(text);
321 while (*p != '"' && *p != '\0') p++;
328 while (*p != '"' && *p != '\0') p++;
335 ext = quoted_value(p);
338 while (*p != '"' && *p != '\0') p++;
340 while (*p != '"' && *p != '\0') p++;
344 desc = quoted_value(p);
351 if (key && key[0] == '#')
354 tmp = g_strdup(key + 1);
361 if (key && strlen(key) > 0 && ext) filter_add(key, desc, ext, enabled);
368 GList *path_list_filter(GList *list, gint is_dir_list)
372 if (!is_dir_list && file_filter_disable && show_dot_files) return list;
377 gchar *name = work->data;
380 base = filename_from_path(name);
382 if ((!show_dot_files && ishidden(base)) ||
383 (!is_dir_list && !filter_name_exists(base)) ||
384 (is_dir_list && base[0] == '.' && (strcmp(base, GQVIEW_CACHE_LOCAL_THUMB) == 0 ||
385 strcmp(base, GQVIEW_CACHE_LOCAL_METADATA) == 0)) )
389 list = g_list_remove_link(list, link);
403 *-----------------------------------------------------------------------------
404 * path list recursive
405 *-----------------------------------------------------------------------------
408 static gint path_list_sort_cb(gconstpointer a, gconstpointer b)
410 return CASE_SORT((gchar *)a, (gchar *)b);
413 GList *path_list_sort(GList *list)
415 return g_list_sort(list, path_list_sort_cb);
418 static void path_list_recursive_append(GList **list, GList *dirs)
425 const gchar *path = work->data;
429 if (path_list(path, &f, &d))
431 f = path_list_filter(f, FALSE);
432 f = path_list_sort(f);
433 *list = g_list_concat(*list, f);
435 d = path_list_filter(d, TRUE);
436 d = path_list_sort(d);
437 path_list_recursive_append(list, d);
445 GList *path_list_recursive(const gchar *path)
450 if (!path_list(path, &list, &d)) return NULL;
451 list = path_list_filter(list, FALSE);
452 list = path_list_sort(list);
454 d = path_list_filter(d, TRUE);
455 d = path_list_sort(d);
456 path_list_recursive_append(&list, d);
463 *-----------------------------------------------------------------------------
464 * text conversion utils
465 *-----------------------------------------------------------------------------
468 gchar *text_from_size(gint64 size)
474 /* what I would like to use is printf("%'d", size)
475 * BUT: not supported on every libc :(
477 if (size > G_MAXUINT)
479 /* the %lld conversion is not valid in all libcs, so use a simple work-around */
480 a = g_strdup_printf("%d%09d", (guint)(size / 1000000000), (guint)(size % 1000000000));
484 a = g_strdup_printf("%d", (guint)size);
490 b = g_new(gchar, l + n + 1);
515 gchar *text_from_size_abrev(gint64 size)
517 if (size < (gint64)1024)
519 return g_strdup_printf(_("%d bytes"), (gint)size);
521 if (size < (gint64)1048576)
523 return g_strdup_printf(_("%.1f K"), (double)size / 1024.0);
525 if (size < (gint64)1073741824)
527 return g_strdup_printf(_("%.1f MB"), (double)size / 1048576.0);
530 /* to avoid overflowing the double, do division in two steps */
532 return g_strdup_printf(_("%.1f GB"), (double)size / 1024.0);
535 /* note: returned string is valid until next call to text_from_time() */
536 const gchar *text_from_time(time_t t)
538 static gchar *ret = NULL;
542 GError *error = NULL;
544 btime = localtime(&t);
546 /* the %x warning about 2 digit years is not an error */
547 buflen = strftime(buf, sizeof(buf), "%x %H:%M", btime);
548 if (buflen < 1) return "";
551 ret = g_locale_to_utf8(buf, buflen, NULL, NULL, &error);
554 printf("Error converting locale strftime to UTF-8: %s\n", error->message);
563 *-----------------------------------------------------------------------------
565 *-----------------------------------------------------------------------------
568 FileData *file_data_new(const gchar *path, struct stat *st)
572 fd = g_new0(FileData, 1);
573 fd->path = path_to_utf8(path);
574 fd->name = filename_from_path(fd->path);
575 fd->size = st->st_size;
576 fd->date = st->st_mtime;
582 FileData *file_data_new_simple(const gchar *path)
587 fd = g_new0(FileData, 1);
588 fd->path = g_strdup(path);
589 fd->name = filename_from_path(fd->path);
591 if (stat_utf8(fd->path, &st))
593 fd->size = st.st_size;
594 fd->date = st.st_mtime;
602 void file_data_free(FileData *fd)
605 if (fd->pixbuf) g_object_unref(fd->pixbuf);
610 *-----------------------------------------------------------------------------
612 *-----------------------------------------------------------------------------
615 static SortType filelist_sort_method = SORT_NONE;
616 static gint filelist_sort_ascend = TRUE;
618 static gint sort_file_cb(void *a, void *b)
623 if (!filelist_sort_ascend)
629 switch (filelist_sort_method)
632 if (fa->size < fb->size) return -1;
633 if (fa->size > fb->size) return 1;
637 if (fa->date < fb->date) return -1;
638 if (fa->date > fb->date) return 1;
641 #ifdef HAVE_STRVERSCMP
643 return strverscmp(fa->name, fb->name);
648 return CASE_SORT(fa->name, fb->name);
653 GList *filelist_sort(GList *list, SortType method, gint ascend)
655 filelist_sort_method = method;
656 filelist_sort_ascend = ascend;
657 return g_list_sort(list, (GCompareFunc) sort_file_cb);
660 GList *filelist_insert_sort(GList *list, FileData *fd, SortType method, gint ascend)
662 filelist_sort_method = method;
663 filelist_sort_ascend = ascend;
664 return g_list_insert_sorted(list, fd, (GCompareFunc) sort_file_cb);
667 gint filelist_read(const gchar *path, GList **files, GList **dirs)
671 struct stat ent_sbuf;
679 pathl = path_from_utf8(path);
680 if (!pathl || (dp = opendir(pathl)) == NULL)
683 if (files) *files = NULL;
684 if (dirs) *dirs = NULL;
689 if (pathl[0] == '/' && pathl[1] == '\0')
692 pathl = g_strdup("");
695 while ((dir = readdir(dp)) != NULL)
697 gchar *name = dir->d_name;
698 if (show_dot_files || !ishidden(name))
700 gchar *filepath = g_strconcat(pathl, "/", name, NULL);
701 if (stat(filepath, &ent_sbuf) >= 0)
703 if (S_ISDIR(ent_sbuf.st_mode))
705 /* we ignore the .thumbnails dir for cleanliness */
707 !(name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) &&
708 strcmp(name, GQVIEW_CACHE_LOCAL_THUMB) != 0 &&
709 strcmp(name, GQVIEW_CACHE_LOCAL_METADATA) != 0)
711 dlist = g_list_prepend(dlist, file_data_new(filepath, &ent_sbuf));
716 if ((files) && filter_name_exists(name))
718 flist = g_list_prepend(flist, file_data_new(filepath, &ent_sbuf));
730 if (dirs) *dirs = dlist;
731 if (files) *files = flist;
736 void filelist_free(GList *list)
743 file_data_free((FileData *)work->data);