Mercurial > geeqie
comparison src/filefilter.c @ 586:905688aa2317
split filelist.c to filefilter.c and filedata.c
author | nadvornik |
---|---|
date | Mon, 05 May 2008 19:11:12 +0000 |
parents | src/filelist.c@9dc0513837b5 |
children | 5963c394ba53 |
comparison
equal
deleted
inserted
replaced
585:4875d67823b4 | 586:905688aa2317 |
---|---|
1 /* | |
2 * Geeqie | |
3 * (C) 2006 John Ellis | |
4 * Copyright (C) 2008 The Geeqie Team | |
5 * | |
6 * Author: John Ellis | |
7 * | |
8 * This software is released under the GNU General Public License (GNU GPL). | |
9 * Please read the included file COPYING for more information. | |
10 * This software comes with no warranty of any kind, use at your own risk! | |
11 */ | |
12 | |
13 | |
14 #include "main.h" | |
15 #include "filefilter.h" | |
16 | |
17 #include "cache.h" | |
18 #include "debug.h" | |
19 #include "rcfile.h" | |
20 #include "secure_save.h" | |
21 #include "thumb_standard.h" | |
22 #include "ui_fileops.h" | |
23 | |
24 | |
25 /* | |
26 *----------------------------------------------------------------------------- | |
27 * file filtering | |
28 *----------------------------------------------------------------------------- | |
29 */ | |
30 | |
31 static GList *filter_list = NULL; | |
32 static GList *extension_list = NULL; | |
33 static GList *sidecar_ext_list = NULL; | |
34 | |
35 static GList *file_class_extension_list[FILE_FORMAT_CLASSES]; | |
36 | |
37 | |
38 gint ishidden(const gchar *name) | |
39 { | |
40 if (name[0] != '.') return FALSE; | |
41 if (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) return FALSE; | |
42 return TRUE; | |
43 } | |
44 | |
45 static FilterEntry *filter_entry_new(const gchar *key, const gchar *description, | |
46 const gchar *extensions, FileFormatClass file_class, gint enabled) | |
47 { | |
48 FilterEntry *fe; | |
49 | |
50 fe = g_new0(FilterEntry, 1); | |
51 fe->key = g_strdup(key); | |
52 fe->description = g_strdup(description); | |
53 fe->extensions = g_strdup(extensions); | |
54 fe->enabled = enabled; | |
55 fe->file_class = file_class; | |
56 | |
57 return fe; | |
58 } | |
59 | |
60 static void filter_entry_free(FilterEntry *fe) | |
61 { | |
62 if (!fe) return; | |
63 | |
64 g_free(fe->key); | |
65 g_free(fe->description); | |
66 g_free(fe->extensions); | |
67 g_free(fe); | |
68 } | |
69 | |
70 GList *filter_get_list(void) | |
71 { | |
72 return filter_list; | |
73 } | |
74 | |
75 void filter_remove_entry(FilterEntry *fe) | |
76 { | |
77 if (!g_list_find(filter_list, fe)) return; | |
78 | |
79 filter_list = g_list_remove(filter_list, fe); | |
80 filter_entry_free(fe); | |
81 } | |
82 | |
83 static gint filter_key_exists(const gchar *key) | |
84 { | |
85 GList *work; | |
86 | |
87 if (!key) return FALSE; | |
88 | |
89 work = filter_list; | |
90 while (work) | |
91 { | |
92 FilterEntry *fe = work->data; | |
93 work = work->next; | |
94 | |
95 if (strcmp(fe->key, key) == 0) return TRUE; | |
96 } | |
97 | |
98 return FALSE; | |
99 } | |
100 | |
101 void filter_add(const gchar *key, const gchar *description, const gchar *extensions, FileFormatClass file_class, gint enabled) | |
102 { | |
103 filter_list = g_list_append(filter_list, filter_entry_new(key, description, extensions, file_class, enabled)); | |
104 } | |
105 | |
106 void filter_add_unique(const gchar *description, const gchar *extensions, FileFormatClass file_class, gint enabled) | |
107 { | |
108 gchar *key; | |
109 gint n; | |
110 | |
111 key = g_strdup("user0"); | |
112 n = 1; | |
113 while (filter_key_exists(key)) | |
114 { | |
115 g_free(key); | |
116 if (n > 999) return; | |
117 key = g_strdup_printf("user%d", n); | |
118 n++; | |
119 } | |
120 | |
121 filter_add(key, description, extensions, file_class, enabled); | |
122 g_free(key); | |
123 } | |
124 | |
125 static void filter_add_if_missing(const gchar *key, const gchar *description, const gchar *extensions, FileFormatClass file_class, gint enabled) | |
126 { | |
127 GList *work; | |
128 | |
129 if (!key) return; | |
130 | |
131 work = filter_list; | |
132 while (work) | |
133 { | |
134 FilterEntry *fe = work->data; | |
135 work = work->next; | |
136 if (fe->key && strcmp(fe->key, key) == 0) | |
137 { | |
138 if (fe->file_class == FORMAT_CLASS_UNKNOWN) | |
139 fe->file_class = file_class; /* for compatibility */ | |
140 return; | |
141 } | |
142 } | |
143 | |
144 filter_add(key, description, extensions, file_class, enabled); | |
145 } | |
146 | |
147 void filter_reset(void) | |
148 { | |
149 GList *work; | |
150 | |
151 work = filter_list; | |
152 while (work) | |
153 { | |
154 FilterEntry *fe = work->data; | |
155 work = work->next; | |
156 filter_entry_free(fe); | |
157 } | |
158 | |
159 g_list_free(filter_list); | |
160 filter_list = NULL; | |
161 } | |
162 | |
163 void filter_add_defaults(void) | |
164 { | |
165 GSList *list, *work; | |
166 | |
167 list = gdk_pixbuf_get_formats(); | |
168 work = list; | |
169 while (work) | |
170 { | |
171 GdkPixbufFormat *format; | |
172 gchar *name; | |
173 gchar *desc; | |
174 gchar **extensions; | |
175 GString *filter = NULL; | |
176 gint i; | |
177 | |
178 format = work->data; | |
179 work = work->next; | |
180 | |
181 name = gdk_pixbuf_format_get_name(format); | |
182 desc = gdk_pixbuf_format_get_description(format); | |
183 extensions = gdk_pixbuf_format_get_extensions(format); | |
184 | |
185 i = 0; | |
186 while (extensions[i]) | |
187 { | |
188 if (!filter) | |
189 { | |
190 filter = g_string_new("."); | |
191 filter = g_string_append(filter, extensions[i]); | |
192 } | |
193 else | |
194 { | |
195 filter = g_string_append(filter, ";."); | |
196 filter = g_string_append(filter, extensions[i]); | |
197 } | |
198 i++; | |
199 } | |
200 | |
201 DEBUG_1("loader reported [%s] [%s] [%s]", name, desc, filter->str); | |
202 | |
203 filter_add_if_missing(name, desc, filter->str, FORMAT_CLASS_IMAGE, TRUE); | |
204 | |
205 g_free(name); | |
206 g_free(desc); | |
207 g_strfreev(extensions); | |
208 g_string_free(filter, TRUE); | |
209 } | |
210 g_slist_free(list); | |
211 | |
212 /* add defaults even if gdk-pixbuf does not have them, but disabled */ | |
213 filter_add_if_missing("jpeg", "JPEG group", ".jpg;.jpeg;.jpe", FORMAT_CLASS_IMAGE, FALSE); | |
214 filter_add_if_missing("png", "Portable Network Graphic", ".png", FORMAT_CLASS_IMAGE, FALSE); | |
215 filter_add_if_missing("tiff", "Tiff", ".tif;.tiff", FORMAT_CLASS_IMAGE, FALSE); | |
216 filter_add_if_missing("pnm", "Packed Pixel formats", ".pbm;.pgm;.pnm;.ppm", FORMAT_CLASS_IMAGE, FALSE); | |
217 filter_add_if_missing("gif", "Graphics Interchange Format", ".gif", FORMAT_CLASS_IMAGE, FALSE); | |
218 filter_add_if_missing("xbm", "X bitmap", ".xbm", FORMAT_CLASS_IMAGE, FALSE); | |
219 filter_add_if_missing("xpm", "X pixmap", ".xpm", FORMAT_CLASS_IMAGE, FALSE); | |
220 filter_add_if_missing("bmp", "Bitmap", ".bmp", FORMAT_CLASS_IMAGE, FALSE); | |
221 filter_add_if_missing("ico", "Icon file", ".ico;.cur", FORMAT_CLASS_IMAGE, FALSE); | |
222 filter_add_if_missing("ras", "Raster", ".ras", FORMAT_CLASS_IMAGE, FALSE); | |
223 filter_add_if_missing("svg", "Scalable Vector Graphics", ".svg", FORMAT_CLASS_IMAGE, FALSE); | |
224 | |
225 /* non-image files that might be desirable to show */ | |
226 filter_add_if_missing("xmp", "XMP sidecar", ".xmp", FORMAT_CLASS_META, TRUE); | |
227 | |
228 /* These are the raw camera formats with embedded jpeg/exif. | |
229 * (see format_raw.c and/or exiv2.cc) | |
230 */ | |
231 filter_add_if_missing("arw", "Sony raw format", ".arw;.srf;.sr2", FORMAT_CLASS_RAWIMAGE, TRUE); | |
232 filter_add_if_missing("crw", "Canon raw format", ".crw;.cr2", FORMAT_CLASS_RAWIMAGE, TRUE); | |
233 filter_add_if_missing("kdc", "Kodak raw format", ".kdc;.dcr", FORMAT_CLASS_RAWIMAGE, TRUE); | |
234 filter_add_if_missing("raf", "Fujifilm raw format", ".raf", FORMAT_CLASS_RAWIMAGE, TRUE); | |
235 filter_add_if_missing("mef", "Mamiya raw format", ".mef;.mos", FORMAT_CLASS_RAWIMAGE, TRUE); | |
236 filter_add_if_missing("mrw", "Minolta raw format", ".mrw", FORMAT_CLASS_RAWIMAGE, TRUE); | |
237 filter_add_if_missing("nef", "Nikon raw format", ".nef", FORMAT_CLASS_RAWIMAGE, TRUE); | |
238 filter_add_if_missing("orf", "Olympus raw format", ".orf", FORMAT_CLASS_RAWIMAGE, TRUE); | |
239 filter_add_if_missing("pef", "Pentax or Samsung raw format", ".pef;.ptx", FORMAT_CLASS_RAWIMAGE, TRUE); | |
240 filter_add_if_missing("dng", "Adobe Digital Negative raw format", ".dng", FORMAT_CLASS_RAWIMAGE, TRUE); | |
241 filter_add_if_missing("x3f", "Sigma raw format", ".x3f", FORMAT_CLASS_RAWIMAGE, TRUE); | |
242 filter_add_if_missing("raw", "Panasonic raw format", ".raw", FORMAT_CLASS_RAWIMAGE, TRUE); | |
243 filter_add_if_missing("r3d", "Red raw format", ".r3d", FORMAT_CLASS_RAWIMAGE, TRUE); | |
244 filter_add_if_missing("3fr", "Hasselblad raw format", ".3fr", FORMAT_CLASS_RAWIMAGE, TRUE); | |
245 filter_add_if_missing("erf", "Epson raw format", ".erf", FORMAT_CLASS_RAWIMAGE, TRUE); | |
246 } | |
247 | |
248 GList *filter_to_list(const gchar *extensions) | |
249 { | |
250 GList *list = NULL; | |
251 const gchar *p; | |
252 | |
253 if (!extensions) return NULL; | |
254 | |
255 p = extensions; | |
256 while (*p != '\0') | |
257 { | |
258 const gchar *b; | |
259 gint l = 0; | |
260 | |
261 b = p; | |
262 while (*p != '\0' && *p != ';') | |
263 { | |
264 p++; | |
265 l++; | |
266 } | |
267 list = g_list_append(list, g_strndup(b, l)); | |
268 if (*p == ';') p++; | |
269 } | |
270 | |
271 return list; | |
272 } | |
273 | |
274 void filter_rebuild(void) | |
275 { | |
276 GList *work; | |
277 gint i; | |
278 | |
279 string_list_free(extension_list); | |
280 extension_list = NULL; | |
281 | |
282 for (i = 0; i < FILE_FORMAT_CLASSES; i++) | |
283 { | |
284 string_list_free(file_class_extension_list[i]); | |
285 file_class_extension_list[i] = NULL; | |
286 } | |
287 | |
288 work = filter_list; | |
289 while (work) | |
290 { | |
291 FilterEntry *fe; | |
292 | |
293 fe = work->data; | |
294 work = work->next; | |
295 | |
296 if (fe->enabled) | |
297 { | |
298 GList *ext; | |
299 | |
300 ext = filter_to_list(fe->extensions); | |
301 if (ext) extension_list = g_list_concat(extension_list, ext); | |
302 | |
303 if (fe->file_class >= 0 && fe->file_class < FILE_FORMAT_CLASSES) | |
304 { | |
305 ext = filter_to_list(fe->extensions); | |
306 if (ext) file_class_extension_list[fe->file_class] = g_list_concat(file_class_extension_list[fe->file_class], ext); | |
307 } | |
308 else | |
309 { | |
310 printf("WARNING: invalid file class %d\n", fe->file_class); | |
311 } | |
312 } | |
313 } | |
314 } | |
315 | |
316 gint filter_name_exists(const gchar *name) | |
317 { | |
318 GList *work; | |
319 gint ln; | |
320 | |
321 if (!extension_list || options->file_filter.disable) return TRUE; | |
322 | |
323 ln = strlen(name); | |
324 work = extension_list; | |
325 while (work) | |
326 { | |
327 gchar *filter = work->data; | |
328 gint lf = strlen(filter); | |
329 | |
330 if (ln >= lf) | |
331 { | |
332 if (strncasecmp(name + ln - lf, filter, lf) == 0) return TRUE; | |
333 } | |
334 work = work->next; | |
335 } | |
336 | |
337 return FALSE; | |
338 } | |
339 | |
340 gint filter_file_class(const gchar *name, FileFormatClass file_class) | |
341 { | |
342 GList *work; | |
343 gint ln; | |
344 | |
345 if (file_class < 0 || file_class >= FILE_FORMAT_CLASSES) | |
346 { | |
347 printf("WARNING: invalid file class %d\n", file_class); | |
348 return FALSE; | |
349 } | |
350 | |
351 ln = strlen(name); | |
352 work = file_class_extension_list[file_class]; | |
353 while (work) | |
354 { | |
355 gchar *filter = work->data; | |
356 gint lf = strlen(filter); | |
357 | |
358 if (ln >= lf) | |
359 { | |
360 if (strncasecmp(name + ln - lf, filter, lf) == 0) return TRUE; | |
361 } | |
362 work = work->next; | |
363 } | |
364 | |
365 return FALSE; | |
366 } | |
367 | |
368 void filter_write_list(SecureSaveInfo *ssi) | |
369 { | |
370 GList *work; | |
371 | |
372 work = filter_list; | |
373 while (work) | |
374 { | |
375 FilterEntry *fe = work->data; | |
376 work = work->next; | |
377 | |
378 gchar *extensions = escquote_value(fe->extensions); | |
379 gchar *description = escquote_value(fe->description); | |
380 | |
381 secure_fprintf(ssi, "file_filter.ext: \"%s%s\" %s %s %d\n", | |
382 (fe->enabled) ? "" : "#", | |
383 fe->key, extensions, description, fe->file_class); | |
384 g_free(extensions); | |
385 g_free(description); | |
386 } | |
387 } | |
388 | |
389 void filter_parse(const gchar *text) | |
390 { | |
391 const gchar *p; | |
392 gchar *key; | |
393 gchar *ext; | |
394 gchar *desc; | |
395 gint enabled = TRUE; | |
396 gint file_class; | |
397 | |
398 if (!text || text[0] != '"') return; | |
399 | |
400 key = quoted_value(text, &p); | |
401 if (!key) return; | |
402 | |
403 ext = quoted_value(p, &p); | |
404 desc = quoted_value(p, &p); | |
405 | |
406 file_class = strtol(p, NULL, 10); | |
407 | |
408 if (file_class < 0 || file_class >= FILE_FORMAT_CLASSES) file_class = FORMAT_CLASS_UNKNOWN; | |
409 | |
410 if (key && key[0] == '#') | |
411 { | |
412 gchar *tmp; | |
413 tmp = g_strdup(key + 1); | |
414 g_free(key); | |
415 key = tmp; | |
416 | |
417 enabled = FALSE; | |
418 } | |
419 | |
420 if (key && strlen(key) > 0 && ext) filter_add(key, desc, ext, file_class, enabled); | |
421 | |
422 g_free(key); | |
423 g_free(ext); | |
424 g_free(desc); | |
425 } | |
426 | |
427 /* | |
428 *----------------------------------------------------------------------------- | |
429 * sidecar extension list | |
430 *----------------------------------------------------------------------------- | |
431 */ | |
432 | |
433 GList *sidecar_ext_get_list(void) | |
434 { | |
435 return sidecar_ext_list; | |
436 } | |
437 | |
438 void sidecar_ext_parse(const gchar *text, gint quoted) | |
439 { | |
440 GList *work; | |
441 gchar *value; | |
442 | |
443 work = sidecar_ext_list; | |
444 while (work) | |
445 { | |
446 gchar *ext = work->data; | |
447 work = work->next; | |
448 g_free(ext); | |
449 } | |
450 g_list_free(sidecar_ext_list); | |
451 sidecar_ext_list = NULL; | |
452 | |
453 if (quoted) | |
454 value = quoted_value(text, NULL); | |
455 else | |
456 value = g_strdup(text); | |
457 | |
458 if (value == NULL) return; | |
459 | |
460 sidecar_ext_list = filter_to_list(value); | |
461 | |
462 g_free(value); | |
463 } | |
464 | |
465 void sidecar_ext_write(SecureSaveInfo *ssi) | |
466 { | |
467 secure_fprintf(ssi, "sidecar.ext: \"%s\"\n", sidecar_ext_to_string()); | |
468 } | |
469 | |
470 char *sidecar_ext_to_string() | |
471 { | |
472 GList *work; | |
473 GString *str = g_string_new(""); | |
474 | |
475 work = sidecar_ext_list; | |
476 while (work) | |
477 { | |
478 gchar *ext = work->data; | |
479 work = work->next; | |
480 g_string_append(str, ext); | |
481 if (work) g_string_append(str, ";"); | |
482 } | |
483 return g_string_free(str, FALSE); | |
484 } | |
485 | |
486 void sidecar_ext_add_defaults() | |
487 { | |
488 sidecar_ext_parse(".jpg;.cr2;.nef;.crw;.xmp", FALSE); | |
489 } |