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);
212 static GList *filter_to_list(const gchar *extensions)
217 if (!extensions) return NULL;
226 while (*p != '\0' && *p != ';')
231 list = g_list_append(list, g_strndup(b, l));
238 void filter_rebuild(void)
242 path_list_free(extension_list);
243 extension_list = NULL;
257 ext = filter_to_list(fe->extensions);
258 if (ext) extension_list = g_list_concat(extension_list, ext);
263 gint filter_name_exists(const gchar *name)
266 if (!extension_list || file_filter_disable) return TRUE;
268 work = extension_list;
271 gchar *filter = work->data;
272 gint lf = strlen(filter);
273 gint ln = strlen(name);
276 if (strncasecmp(name + ln - lf, filter, lf) == 0) return TRUE;
284 void filter_write_list(FILE *f)
291 FilterEntry *fe = work->data;
294 fprintf(f, "filter_ext: \"%s%s\" \"%s\" \"%s\"\n", (fe->enabled) ? "" : "#",
295 fe->key, fe->extensions,
296 (fe->description) ? fe->description : "");
300 void filter_parse(const gchar *text)
308 if (!text || text[0] != '"') return;
310 key = quoted_value(text);
315 while (*p != '"' && *p != '\0') p++;
322 while (*p != '"' && *p != '\0') p++;
329 ext = quoted_value(p);
332 while (*p != '"' && *p != '\0') p++;
334 while (*p != '"' && *p != '\0') p++;
338 desc = quoted_value(p);
345 if (key && key[0] == '#')
348 tmp = g_strdup(key + 1);
355 if (key && strlen(key) > 0 && ext) filter_add(key, desc, ext, enabled);
362 GList *path_list_filter(GList *list, gint is_dir_list)
366 if (!is_dir_list && file_filter_disable && show_dot_files) return list;
371 gchar *name = work->data;
374 base = filename_from_path(name);
376 if ((!show_dot_files && ishidden(base)) ||
377 (!is_dir_list && !filter_name_exists(base)) ||
378 (is_dir_list && base[0] == '.' && (strcmp(base, GQVIEW_CACHE_LOCAL_THUMB) == 0 ||
379 strcmp(base, GQVIEW_CACHE_LOCAL_METADATA) == 0)) )
383 list = g_list_remove_link(list, link);
397 *-----------------------------------------------------------------------------
398 * path list recursive
399 *-----------------------------------------------------------------------------
402 static gint path_list_sort_cb(gconstpointer a, gconstpointer b)
404 return CASE_SORT((gchar *)a, (gchar *)b);
407 GList *path_list_sort(GList *list)
409 return g_list_sort(list, path_list_sort_cb);
412 static void path_list_recursive_append(GList **list, GList *dirs)
419 const gchar *path = work->data;
423 if (path_list(path, &f, &d))
425 f = path_list_filter(f, FALSE);
426 f = path_list_sort(f);
427 *list = g_list_concat(*list, f);
429 d = path_list_filter(d, TRUE);
430 d = path_list_sort(d);
431 path_list_recursive_append(list, d);
439 GList *path_list_recursive(const gchar *path)
444 if (!path_list(path, &list, &d)) return NULL;
445 list = path_list_filter(list, FALSE);
446 list = path_list_sort(list);
448 d = path_list_filter(d, TRUE);
449 d = path_list_sort(d);
450 path_list_recursive_append(&list, d);
457 *-----------------------------------------------------------------------------
458 * text conversion utils
459 *-----------------------------------------------------------------------------
462 gchar *text_from_size(gint64 size)
468 /* what I would like to use is printf("%'d", size)
469 * BUT: not supported on every libc :(
471 if (size > G_MAXUINT)
473 /* the %lld conversion is not valid in all libcs, so use a simple work-around */
474 a = g_strdup_printf("%d%09d", (guint)(size / 1000000000), (guint)(size % 1000000000));
478 a = g_strdup_printf("%d", (guint)size);
484 b = g_new(gchar, l + n + 1);
509 gchar *text_from_size_abrev(gint64 size)
511 if (size < (gint64)1024)
513 return g_strdup_printf(_("%d bytes"), (gint)size);
515 if (size < (gint64)1048576)
517 return g_strdup_printf(_("%.1f K"), (double)size / 1024.0);
519 if (size < (gint64)1073741824)
521 return g_strdup_printf(_("%.1f MB"), (double)size / 1048576.0);
524 /* to avoid overflowing the double, do division in two steps */
526 return g_strdup_printf(_("%.1f GB"), (double)size / 1024.0);
529 /* note: returned string is valid until next call to text_from_time() */
530 const gchar *text_from_time(time_t t)
532 static gchar *ret = NULL;
536 GError *error = NULL;
538 btime = localtime(&t);
540 /* the %x warning about 2 digit years is not an error */
541 buflen = strftime(buf, sizeof(buf), "%x %H:%M", btime);
542 if (buflen < 1) return "";
545 ret = g_locale_to_utf8(buf, buflen, NULL, NULL, &error);
548 printf("Error converting locale strftime to UTF-8: %s\n", error->message);
557 *-----------------------------------------------------------------------------
559 *-----------------------------------------------------------------------------
562 FileData *file_data_new(const gchar *path, struct stat *st)
566 fd = g_new0(FileData, 1);
567 fd->path = path_to_utf8(path);
568 fd->name = filename_from_path(fd->path);
569 fd->size = st->st_size;
570 fd->date = st->st_mtime;
576 FileData *file_data_new_simple(const gchar *path)
581 fd = g_new0(FileData, 1);
582 fd->path = g_strdup(path);
583 fd->name = filename_from_path(fd->path);
585 if (stat_utf8(fd->path, &st))
587 fd->size = st.st_size;
588 fd->date = st.st_mtime;
596 void file_data_free(FileData *fd)
599 if (fd->pixbuf) g_object_unref(fd->pixbuf);
604 *-----------------------------------------------------------------------------
606 *-----------------------------------------------------------------------------
609 static SortType filelist_sort_method = SORT_NONE;
610 static gint filelist_sort_ascend = TRUE;
612 static gint sort_file_cb(void *a, void *b)
617 if (!filelist_sort_ascend)
623 switch (filelist_sort_method)
626 if (fa->size < fb->size) return -1;
627 if (fa->size > fb->size) return 1;
631 if (fa->date < fb->date) return -1;
632 if (fa->date > fb->date) return 1;
635 #ifdef HAVE_STRVERSCMP
637 return strverscmp(fa->name, fb->name);
642 return CASE_SORT(fa->name, fb->name);
647 GList *filelist_sort(GList *list, SortType method, gint ascend)
649 filelist_sort_method = method;
650 filelist_sort_ascend = ascend;
651 return g_list_sort(list, (GCompareFunc) sort_file_cb);
654 GList *filelist_insert_sort(GList *list, FileData *fd, SortType method, gint ascend)
656 filelist_sort_method = method;
657 filelist_sort_ascend = ascend;
658 return g_list_insert_sorted(list, fd, (GCompareFunc) sort_file_cb);
661 gint filelist_read(const gchar *path, GList **files, GList **dirs)
665 struct stat ent_sbuf;
673 pathl = path_from_utf8(path);
674 if (!pathl || (dp = opendir(pathl)) == NULL)
677 if (files) *files = NULL;
678 if (dirs) *dirs = NULL;
683 if (pathl[0] == '/' && pathl[1] == '\0')
686 pathl = g_strdup("");
689 while ((dir = readdir(dp)) != NULL)
691 gchar *name = dir->d_name;
692 if (show_dot_files || !ishidden(name))
694 gchar *filepath = g_strconcat(pathl, "/", name, NULL);
695 if (stat(filepath, &ent_sbuf) >= 0)
697 if (S_ISDIR(ent_sbuf.st_mode))
699 /* we ignore the .thumbnails dir for cleanliness */
701 !(name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) &&
702 strcmp(name, GQVIEW_CACHE_LOCAL_THUMB) != 0 &&
703 strcmp(name, GQVIEW_CACHE_LOCAL_METADATA) != 0)
705 dlist = g_list_prepend(dlist, file_data_new(filepath, &ent_sbuf));
710 if ((files) && filter_name_exists(name))
712 flist = g_list_prepend(flist, file_data_new(filepath, &ent_sbuf));
724 if (dirs) *dirs = dlist;
725 if (files) *files = flist;
730 void filelist_free(GList *list)
737 file_data_free((FileData *)work->data);