1aa1ecd0cdad869da32f8af4b0a4c7596d68cc55
[geeqie.git] / src / filelist.c
1 /*
2  * GQview
3  * (C) 2006 John Ellis
4  *
5  * Author: John Ellis
6  *
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!
10  */
11
12
13 #include "gqview.h"
14 #include "filelist.h"
15
16 #include "cache.h"
17 #include "rcfile.h"
18 #include "thumb_standard.h"
19 #include "ui_fileops.h"
20
21
22 /*
23  *-----------------------------------------------------------------------------
24  * file filtering
25  *-----------------------------------------------------------------------------
26  */
27
28 static GList *filter_list = NULL;
29 static GList *extension_list = NULL;
30
31 gint ishidden(const gchar *name)
32 {
33         if (name[0] != '.') return FALSE;
34         if (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) return FALSE;
35         return TRUE;
36 }
37
38 static FilterEntry *filter_entry_new(const gchar *key, const gchar *description,
39                                      const gchar *extensions, gint enabled)
40 {
41         FilterEntry *fe;
42
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;
48         
49         return fe;
50 }
51
52 static void filter_entry_free(FilterEntry *fe)
53 {
54         if (!fe) return;
55
56         g_free(fe->key);
57         g_free(fe->description);
58         g_free(fe->extensions);
59         g_free(fe);
60 }
61
62 GList *filter_get_list(void)
63 {
64         return filter_list;
65 }
66
67 void filter_remove_entry(FilterEntry *fe)
68 {
69         if (!g_list_find(filter_list, fe)) return;
70
71         filter_list = g_list_remove(filter_list, fe);
72         filter_entry_free(fe);
73 }
74
75 static gint filter_key_exists(const gchar *key)
76 {
77         GList *work;
78
79         if (!key) return FALSE;
80
81         work = filter_list;
82         while (work)
83                 {
84                 FilterEntry *fe = work->data;
85                 work = work->next;
86
87                 if (strcmp(fe->key, key) == 0) return TRUE;
88                 }
89
90         return FALSE;
91 }
92
93 void filter_add(const gchar *key, const gchar *description, const gchar *extensions, gint enabled)
94 {
95         filter_list = g_list_append(filter_list, filter_entry_new(key, description, extensions, enabled));
96 }
97
98 void filter_add_unique(const gchar *description, const gchar *extensions, gint enabled)
99 {
100         gchar *key;
101         gint n;
102
103         key = g_strdup("user0");
104         n = 1;
105         while (filter_key_exists(key))
106                 {
107                 g_free(key);
108                 if (n > 999) return;
109                 key = g_strdup_printf("user%d", n);
110                 n++;
111                 }
112
113         filter_add(key, description, extensions, enabled);
114         g_free(key);
115 }
116
117 static void filter_add_if_missing(const gchar *key, const gchar *description, const gchar *extensions, gint enabled)
118 {
119         GList *work;
120
121         if (!key) return;
122
123         work = filter_list;
124         while (work)
125                 {
126                 FilterEntry *fe = work->data;
127                 work = work->next;
128                 if (fe->key && strcmp(fe->key, key) == 0) return;
129                 }
130
131         filter_add(key, description, extensions, enabled);
132 }
133
134 void filter_reset(void)
135 {
136         GList *work;
137
138         work = filter_list;
139         while (work)
140                 {
141                 FilterEntry *fe = work->data;
142                 work = work->next;
143                 filter_entry_free(fe);
144                 }
145
146         g_list_free(filter_list);
147         filter_list = NULL;
148 }
149
150 void filter_add_defaults(void)
151 {
152         GSList *list, *work;
153
154         list = gdk_pixbuf_get_formats();
155         work = list;
156         while (work)
157                 {
158                 GdkPixbufFormat *format;
159                 gchar *name;
160                 gchar *desc;
161                 gchar **extensions;
162                 GString *filter = NULL;
163                 gint i;
164                 
165                 format = work->data;
166                 work = work->next;
167
168                 name = gdk_pixbuf_format_get_name(format);
169                 desc = gdk_pixbuf_format_get_description(format);
170                 extensions = gdk_pixbuf_format_get_extensions(format);
171
172                 i = 0;
173                 while (extensions[i])
174                         {
175                         if (!filter)
176                                 {
177                                 filter = g_string_new(".");
178                                 filter = g_string_append(filter, extensions[i]);
179                                 }
180                         else
181                                 {
182                                 filter = g_string_append(filter, ";.");
183                                 filter = g_string_append(filter, extensions[i]);
184                                 }
185                         i++;
186                         }
187
188                 if (debug) printf("loader reported [%s] [%s] [%s]\n", name, desc, filter->str);
189
190                 filter_add_if_missing(name, desc, filter->str, TRUE);
191
192                 g_free(name);
193                 g_free(desc);
194                 g_strfreev(extensions);
195                 g_string_free(filter, TRUE);
196                 }
197         g_slist_free(list);
198
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);
211
212         /* These are the raw camera formats with embedded jpeg/exif.
213          * (see format_raw.c)
214          */
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 }
219
220 static GList *filter_to_list(const gchar *extensions)
221 {
222         GList *list = NULL;
223         const gchar *p;
224
225         if (!extensions) return NULL;
226
227         p = extensions;
228         while (*p != '\0')
229                 {
230                 const gchar *b;
231                 gint l = 0;
232
233                 b = p;
234                 while (*p != '\0' && *p != ';')
235                         {
236                         p++;
237                         l++;
238                         }
239                 list = g_list_append(list, g_strndup(b, l));
240                 if (*p == ';') p++;
241                 }
242
243         return list;
244 }
245
246 void filter_rebuild(void)
247 {
248         GList *work;
249
250         path_list_free(extension_list);
251         extension_list = NULL;
252
253         work = filter_list;
254         while (work)
255                 {
256                 FilterEntry *fe;
257
258                 fe = work->data;
259                 work = work->next;
260
261                 if (fe->enabled)
262                         {
263                         GList *ext;
264
265                         ext = filter_to_list(fe->extensions);
266                         if (ext) extension_list = g_list_concat(extension_list, ext);
267                         }
268                 }
269 }
270
271 gint filter_name_exists(const gchar *name)
272 {
273         GList *work;
274         if (!extension_list || file_filter_disable) return TRUE;
275
276         work = extension_list;
277         while (work)
278                 {
279                 gchar *filter = work->data;
280                 gint lf = strlen(filter);
281                 gint ln = strlen(name);
282                 if (ln >= lf)
283                         {
284                         if (strncasecmp(name + ln - lf, filter, lf) == 0) return TRUE;
285                         }
286                 work = work->next;
287                 }
288
289         return FALSE;
290 }
291
292 void filter_write_list(FILE *f)
293 {
294         GList *work;
295
296         work = filter_list;
297         while (work)
298                 {
299                 FilterEntry *fe = work->data;
300                 work = work->next;
301
302                 fprintf(f, "filter_ext: \"%s%s\" \"%s\" \"%s\"\n", (fe->enabled) ? "" : "#",
303                         fe->key, fe->extensions,
304                         (fe->description) ? fe->description : "");
305                 }
306 }
307
308 void filter_parse(const gchar *text)
309 {
310         const gchar *p;
311         gchar *key;
312         gchar *ext;
313         gchar *desc;
314         gint enabled = TRUE;
315
316         if (!text || text[0] != '"') return;
317
318         key = quoted_value(text);
319         if (!key) return;
320
321         p = text;
322         p++;
323         while (*p != '"' && *p != '\0') p++;
324         if (*p != '"')
325                 {
326                 g_free(key);
327                 return;
328                 }
329         p++;
330         while (*p != '"' && *p != '\0') p++;
331         if (*p != '"')
332                 {
333                 g_free(key);
334                 return;
335                 }
336
337         ext = quoted_value(p);
338
339         p++;
340         while (*p != '"' && *p != '\0') p++;
341         if (*p == '"') p++;
342         while (*p != '"' && *p != '\0') p++;
343
344         if (*p == '"')
345                 {
346                 desc = quoted_value(p);
347                 }
348         else
349                 {
350                 desc = NULL;
351                 }
352
353         if (key && key[0] == '#')
354                 {
355                 gchar *tmp;
356                 tmp = g_strdup(key + 1);
357                 g_free(key);
358                 key = tmp;
359
360                 enabled = FALSE;
361                 }
362
363         if (key && strlen(key) > 0 && ext) filter_add(key, desc, ext, enabled);
364
365         g_free(key);
366         g_free(ext);
367         g_free(desc);
368 }
369
370 GList *path_list_filter(GList *list, gint is_dir_list)
371 {
372         GList *work;
373
374         if (!is_dir_list && file_filter_disable && show_dot_files) return list;
375
376         work = list;
377         while (work)
378                 {
379                 gchar *name = work->data;
380                 const gchar *base;
381
382                 base = filename_from_path(name);
383
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)) )
388                         {
389                         GList *link = work;
390                         work = work->next;
391                         list = g_list_remove_link(list, link);
392                         g_free(name);
393                         g_list_free(link);
394                         }
395                 else
396                         {
397                         work = work->next;
398                         }
399                 }
400
401         return list;
402 }
403
404 /*
405  *-----------------------------------------------------------------------------
406  * path list recursive
407  *-----------------------------------------------------------------------------
408  */
409
410 static gint path_list_sort_cb(gconstpointer a, gconstpointer b)
411 {
412         return CASE_SORT((gchar *)a, (gchar *)b);
413 }
414
415 GList *path_list_sort(GList *list)
416 {
417         return g_list_sort(list, path_list_sort_cb);
418 }
419
420 static void path_list_recursive_append(GList **list, GList *dirs)
421 {
422         GList *work;
423
424         work = dirs;
425         while (work)
426                 {
427                 const gchar *path = work->data;
428                 GList *f = NULL;
429                 GList *d = NULL;
430
431                 if (path_list(path, &f, &d))
432                         {
433                         f = path_list_filter(f, FALSE);
434                         f = path_list_sort(f);
435                         *list = g_list_concat(*list, f);
436
437                         d = path_list_filter(d, TRUE);
438                         d = path_list_sort(d);
439                         path_list_recursive_append(list, d);
440                         path_list_free(d);
441                         }
442
443                 work = work->next;
444                 }
445 }
446
447 GList *path_list_recursive(const gchar *path)
448 {
449         GList *list = NULL;
450         GList *d = NULL;
451
452         if (!path_list(path, &list, &d)) return NULL;
453         list = path_list_filter(list, FALSE);
454         list = path_list_sort(list);
455
456         d = path_list_filter(d, TRUE);
457         d = path_list_sort(d);
458         path_list_recursive_append(&list, d);
459         path_list_free(d);
460
461         return list;
462 }
463
464 /*
465  *-----------------------------------------------------------------------------
466  * text conversion utils
467  *-----------------------------------------------------------------------------
468  */
469
470 gchar *text_from_size(gint64 size)
471 {
472         gchar *a, *b;
473         gchar *s, *d;
474         gint l, n, i;
475
476         /* what I would like to use is printf("%'d", size)
477          * BUT: not supported on every libc :(
478          */
479         if (size > G_MAXUINT)
480                 {
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));
483                 }
484         else
485                 {
486                 a = g_strdup_printf("%d", (guint)size);
487                 }
488         l = strlen(a);
489         n = (l - 1)/ 3;
490         if (n < 1) return a;
491
492         b = g_new(gchar, l + n + 1);
493
494         s = a;
495         d = b;
496         i = l - n * 3;
497         while (*s != '\0')
498                 {
499                 if (i < 1)
500                         {
501                         i = 3;
502                         *d = ',';
503                         d++;
504                         }
505
506                 *d = *s;
507                 s++;
508                 d++;
509                 i--;
510                 }
511         *d = '\0';
512
513         g_free(a);
514         return b;
515 }
516
517 gchar *text_from_size_abrev(gint64 size)
518 {
519         if (size < (gint64)1024)
520                 {
521                 return g_strdup_printf(_("%d bytes"), (gint)size);
522                 }
523         if (size < (gint64)1048576)
524                 {
525                 return g_strdup_printf(_("%.1f K"), (double)size / 1024.0);
526                 }
527         if (size < (gint64)1073741824)
528                 {
529                 return g_strdup_printf(_("%.1f MB"), (double)size / 1048576.0);
530                 }
531
532         /* to avoid overflowing the double, do division in two steps */
533         size /= 1048576;
534         return g_strdup_printf(_("%.1f GB"), (double)size / 1024.0);
535 }
536
537 /* note: returned string is valid until next call to text_from_time() */
538 const gchar *text_from_time(time_t t)
539 {
540         static gchar *ret = NULL;
541         gchar buf[128];
542         gint buflen;
543         struct tm *btime;
544         GError *error = NULL;
545
546         btime = localtime(&t);
547
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 "";
551
552         g_free(ret);
553         ret = g_locale_to_utf8(buf, buflen, NULL, NULL, &error);
554         if (error)
555                 {
556                 printf("Error converting locale strftime to UTF-8: %s\n", error->message);
557                 g_error_free(error);
558                 return "";
559                 }
560
561         return ret;
562 }
563
564 /*
565  *-----------------------------------------------------------------------------
566  * file info struct
567  *-----------------------------------------------------------------------------
568  */
569
570 FileData *file_data_new(const gchar *path, struct stat *st)
571 {
572         FileData *fd;
573
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;
579         fd->pixbuf = NULL;
580
581         return fd;
582 }
583
584 FileData *file_data_new_simple(const gchar *path)
585 {
586         FileData *fd;
587         struct stat st;
588
589         fd = g_new0(FileData, 1);
590         fd->path = g_strdup(path);
591         fd->name = filename_from_path(fd->path);
592
593         if (stat_utf8(fd->path, &st))
594                 {
595                 fd->size = st.st_size;
596                 fd->date = st.st_mtime;
597                 }
598
599         fd->pixbuf = NULL;
600
601         return fd;
602 }
603
604 void file_data_free(FileData *fd)
605 {
606         g_free(fd->path);
607         if (fd->pixbuf) g_object_unref(fd->pixbuf);
608         g_free(fd);
609 }
610
611 /*
612  *-----------------------------------------------------------------------------
613  * load file list
614  *-----------------------------------------------------------------------------
615  */
616
617 static SortType filelist_sort_method = SORT_NONE;
618 static gint filelist_sort_ascend = TRUE;
619
620 static gint sort_file_cb(void *a, void *b)
621 {
622         FileData *fa = a;
623         FileData *fb = b;
624
625         if (!filelist_sort_ascend)
626                 {
627                 fa = b;
628                 fb = a;
629                 }
630
631         switch (filelist_sort_method)
632                 {
633                 case SORT_SIZE:
634                         if (fa->size < fb->size) return -1;
635                         if (fa->size > fb->size) return 1;
636                         return 0;
637                         break;
638                 case SORT_TIME:
639                         if (fa->date < fb->date) return -1;
640                         if (fa->date > fb->date) return 1;
641                         return 0;
642                         break;
643 #ifdef HAVE_STRVERSCMP
644                 case SORT_NUMBER:
645                         return strverscmp(fa->name, fb->name);
646                         break;
647 #endif
648                 case SORT_NAME:
649                 default:
650                         return CASE_SORT(fa->name, fb->name);
651                         break;
652                 }
653 }
654
655 GList *filelist_sort(GList *list, SortType method, gint ascend)
656 {
657         filelist_sort_method = method;
658         filelist_sort_ascend = ascend;
659         return g_list_sort(list, (GCompareFunc) sort_file_cb);
660 }
661
662 GList *filelist_insert_sort(GList *list, FileData *fd, SortType method, gint ascend)
663 {
664         filelist_sort_method = method;
665         filelist_sort_ascend = ascend;
666         return g_list_insert_sorted(list, fd, (GCompareFunc) sort_file_cb);
667 }
668
669 gint filelist_read(const gchar *path, GList **files, GList **dirs)
670 {
671         DIR *dp;
672         struct dirent *dir;
673         struct stat ent_sbuf;
674         gchar *pathl;
675         GList *dlist;
676         GList *flist;
677
678         dlist = NULL;
679         flist = NULL;
680
681         pathl = path_from_utf8(path);
682         if (!pathl || (dp = opendir(pathl)) == NULL)
683                 {
684                 g_free(pathl);
685                 if (files) *files = NULL;
686                 if (dirs) *dirs = NULL;
687                 return FALSE;
688                 }
689
690         /* root dir fix */
691         if (pathl[0] == '/' && pathl[1] == '\0')
692                 {
693                 g_free(pathl);
694                 pathl = g_strdup("");
695                 }
696
697         while ((dir = readdir(dp)) != NULL)
698                 {
699                 gchar *name = dir->d_name;
700                 if (show_dot_files || !ishidden(name))
701                         {
702                         gchar *filepath = g_strconcat(pathl, "/", name, NULL);
703                         if (stat(filepath, &ent_sbuf) >= 0)
704                                 {
705                                 if (S_ISDIR(ent_sbuf.st_mode))
706                                         {
707                                         /* we ignore the .thumbnails dir for cleanliness */
708                                         if ((dirs) &&
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)
713                                                 {
714                                                 dlist = g_list_prepend(dlist, file_data_new(filepath, &ent_sbuf));
715                                                 }
716                                         }
717                                 else
718                                         {
719                                         if ((files) && filter_name_exists(name))
720                                                 {
721                                                 flist = g_list_prepend(flist, file_data_new(filepath, &ent_sbuf));
722                                                 }
723                                         }
724                                 }
725                         g_free(filepath);
726                         }
727                 }
728
729         closedir(dp);
730
731         g_free(pathl);
732
733         if (dirs) *dirs = dlist;
734         if (files) *files = flist;
735
736         return TRUE;
737 }
738
739 void filelist_free(GList *list)
740 {
741         GList *work;
742
743         work = list;
744         while (work)
745                 {
746                 file_data_free((FileData *)work->data);
747                 work = work->next;
748                 }
749
750         g_list_free(list);
751 }
752
753