r4034: Use application_x_shellscript for scripts.
[rox-filer/translations.git] / ROX-Filer / src / xdgmime.c
blobbe7398e4157762ec96ccab5df375d8af185c7c61
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* xdgmime.c: XDG Mime Spec mime resolver. Based on version 0.11 of the spec.
4 * More info can be found at http://www.freedesktop.org/standards/
5 *
6 * Copyright (C) 2003,2004 Red Hat, Inc.
7 * Copyright (C) 2003,2004 Jonathan Blandford <jrb@alum.mit.edu>
9 * Licensed under the Academic Free License version 2.0
10 * Or under the following terms:
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the
24 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 * Boston, MA 02111-1307, USA.
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
32 #include "xdgmime.h"
33 #include "xdgmimeint.h"
34 #include "xdgmimeglob.h"
35 #include "xdgmimemagic.h"
36 #include "xdgmimealias.h"
37 #include "xdgmimeparent.h"
38 #include "xdgmimecache.h"
39 #include <stdio.h>
40 #include <string.h>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 #include <sys/time.h>
44 #include <unistd.h>
45 #include <assert.h>
47 typedef struct XdgDirTimeList XdgDirTimeList;
48 typedef struct XdgCallbackList XdgCallbackList;
50 static int need_reread = TRUE;
51 static time_t last_stat_time = 0;
53 static XdgGlobHash *global_hash = NULL;
54 static XdgMimeMagic *global_magic = NULL;
55 static XdgAliasList *alias_list = NULL;
56 static XdgParentList *parent_list = NULL;
57 static XdgDirTimeList *dir_time_list = NULL;
58 static XdgCallbackList *callback_list = NULL;
59 XdgMimeCache **caches = NULL;
60 int n_caches = 0;
62 const char *xdg_mime_type_unknown = "application/octet-stream";
63 const char *xdg_mime_type_unknown_text = "text/plain";
66 enum
68 XDG_CHECKED_UNCHECKED,
69 XDG_CHECKED_VALID,
70 XDG_CHECKED_INVALID
73 struct XdgDirTimeList
75 time_t mtime;
76 char *directory_name;
77 int checked;
78 XdgDirTimeList *next;
81 struct XdgCallbackList
83 XdgCallbackList *next;
84 XdgCallbackList *prev;
85 int callback_id;
86 XdgMimeCallback callback;
87 void *data;
88 XdgMimeDestroy destroy;
91 /* Function called by xdg_run_command_on_dirs. If it returns TRUE, further
92 * directories aren't looked at */
93 typedef int (*XdgDirectoryFunc) (const char *directory,
94 void *user_data);
96 static XdgDirTimeList *
97 xdg_dir_time_list_new (void)
99 XdgDirTimeList *retval;
101 retval = calloc (1, sizeof (XdgDirTimeList));
102 retval->checked = XDG_CHECKED_UNCHECKED;
104 return retval;
107 static void
108 xdg_dir_time_list_free (XdgDirTimeList *list)
110 XdgDirTimeList *next;
112 while (list)
114 next = list->next;
115 free (list->directory_name);
116 free (list);
117 list = next;
121 static int
122 xdg_mime_init_from_directory (const char *directory)
124 char *file_name;
125 struct stat st;
126 XdgDirTimeList *list;
128 assert (directory != NULL);
130 file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
131 strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
132 if (stat (file_name, &st) == 0)
134 XdgMimeCache *cache = _xdg_mime_cache_new_from_file (file_name);
136 if (cache != NULL)
138 list = xdg_dir_time_list_new ();
139 list->directory_name = file_name;
140 list->mtime = st.st_mtime;
141 list->next = dir_time_list;
142 dir_time_list = list;
144 caches = realloc (caches, sizeof (XdgMimeCache *) * (n_caches + 1));
145 caches[n_caches] = cache;
146 n_caches++;
148 return FALSE;
151 free (file_name);
153 file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
154 strcpy (file_name, directory); strcat (file_name, "/mime/globs");
155 if (stat (file_name, &st) == 0)
157 _xdg_mime_glob_read_from_file (global_hash, file_name);
159 list = xdg_dir_time_list_new ();
160 list->directory_name = file_name;
161 list->mtime = st.st_mtime;
162 list->next = dir_time_list;
163 dir_time_list = list;
165 else
167 free (file_name);
170 file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
171 strcpy (file_name, directory); strcat (file_name, "/mime/magic");
172 if (stat (file_name, &st) == 0)
174 _xdg_mime_magic_read_from_file (global_magic, file_name);
176 list = xdg_dir_time_list_new ();
177 list->directory_name = file_name;
178 list->mtime = st.st_mtime;
179 list->next = dir_time_list;
180 dir_time_list = list;
182 else
184 free (file_name);
187 file_name = malloc (strlen (directory) + strlen ("/mime/aliases") + 1);
188 strcpy (file_name, directory); strcat (file_name, "/mime/aliases");
189 _xdg_mime_alias_read_from_file (alias_list, file_name);
190 free (file_name);
192 file_name = malloc (strlen (directory) + strlen ("/mime/subclasses") + 1);
193 strcpy (file_name, directory); strcat (file_name, "/mime/subclasses");
194 _xdg_mime_parent_read_from_file (parent_list, file_name);
195 free (file_name);
197 return FALSE; /* Keep processing */
200 /* Runs a command on all the directories in the search path */
201 static void
202 xdg_run_command_on_dirs (XdgDirectoryFunc func,
203 void *user_data)
205 const char *xdg_data_home;
206 const char *xdg_data_dirs;
207 const char *ptr;
209 xdg_data_home = getenv ("XDG_DATA_HOME");
210 if (xdg_data_home)
212 if ((func) (xdg_data_home, user_data))
213 return;
215 else
217 const char *home;
219 home = getenv ("HOME");
220 if (home != NULL)
222 char *guessed_xdg_home;
223 int stop_processing;
225 guessed_xdg_home = malloc (strlen (home) + strlen ("/.local/share/") + 1);
226 strcpy (guessed_xdg_home, home);
227 strcat (guessed_xdg_home, "/.local/share/");
228 stop_processing = (func) (guessed_xdg_home, user_data);
229 free (guessed_xdg_home);
231 if (stop_processing)
232 return;
236 xdg_data_dirs = getenv ("XDG_DATA_DIRS");
237 if (xdg_data_dirs == NULL)
238 xdg_data_dirs = "/usr/local/share/:/usr/share/";
240 ptr = xdg_data_dirs;
242 while (*ptr != '\000')
244 const char *end_ptr;
245 char *dir;
246 int len;
247 int stop_processing;
249 end_ptr = ptr;
250 while (*end_ptr != ':' && *end_ptr != '\000')
251 end_ptr ++;
253 if (end_ptr == ptr)
255 ptr++;
256 continue;
259 if (*end_ptr == ':')
260 len = end_ptr - ptr;
261 else
262 len = end_ptr - ptr + 1;
263 dir = malloc (len + 1);
264 strncpy (dir, ptr, len);
265 dir[len] = '\0';
266 stop_processing = (func) (dir, user_data);
267 free (dir);
269 if (stop_processing)
270 return;
272 ptr = end_ptr;
276 /* Checks file_path to make sure it has the same mtime as last time it was
277 * checked. If it has a different mtime, or if the file doesn't exist, it
278 * returns FALSE.
280 * FIXME: This doesn't protect against permission changes.
282 static int
283 xdg_check_file (const char *file_path)
285 struct stat st;
287 /* If the file exists */
288 if (stat (file_path, &st) == 0)
290 XdgDirTimeList *list;
292 for (list = dir_time_list; list; list = list->next)
294 if (! strcmp (list->directory_name, file_path) &&
295 st.st_mtime == list->mtime)
297 if (list->checked == XDG_CHECKED_UNCHECKED)
298 list->checked = XDG_CHECKED_VALID;
299 else if (list->checked == XDG_CHECKED_VALID)
300 list->checked = XDG_CHECKED_INVALID;
302 return (list->checked != XDG_CHECKED_VALID);
305 return TRUE;
308 return FALSE;
311 static int
312 xdg_check_dir (const char *directory,
313 int *invalid_dir_list)
315 int invalid;
316 char *file_name;
318 assert (directory != NULL);
320 /* Check the globs file */
321 file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
322 strcpy (file_name, directory); strcat (file_name, "/mime/globs");
323 invalid = xdg_check_file (file_name);
324 free (file_name);
325 if (invalid)
327 *invalid_dir_list = TRUE;
328 return TRUE;
331 /* Check the magic file */
332 file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
333 strcpy (file_name, directory); strcat (file_name, "/mime/magic");
334 invalid = xdg_check_file (file_name);
335 free (file_name);
336 if (invalid)
338 *invalid_dir_list = TRUE;
339 return TRUE;
342 /* Check the mime.cache file */
343 file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
344 strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
345 invalid = xdg_check_file (file_name);
346 free (file_name);
347 if (invalid)
349 *invalid_dir_list = TRUE;
350 return TRUE;
353 return FALSE; /* Keep processing */
356 /* Walks through all the mime files stat()ing them to see if they've changed.
357 * Returns TRUE if they have. */
358 static int
359 xdg_check_dirs (void)
361 XdgDirTimeList *list;
362 int invalid_dir_list = FALSE;
364 for (list = dir_time_list; list; list = list->next)
365 list->checked = XDG_CHECKED_UNCHECKED;
367 xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_check_dir,
368 &invalid_dir_list);
370 if (invalid_dir_list)
371 return TRUE;
373 for (list = dir_time_list; list; list = list->next)
375 if (list->checked != XDG_CHECKED_VALID)
376 return TRUE;
379 return FALSE;
382 /* We want to avoid stat()ing on every single mime call, so we only look for
383 * newer files every 5 seconds. This will return TRUE if we need to reread the
384 * mime data from disk.
386 static int
387 xdg_check_time_and_dirs (void)
389 struct timeval tv;
390 time_t current_time;
391 int retval = FALSE;
393 gettimeofday (&tv, NULL);
394 current_time = tv.tv_sec;
396 if (current_time >= last_stat_time + 5)
398 retval = xdg_check_dirs ();
399 last_stat_time = current_time;
402 return retval;
405 /* Called in every public function. It reloads the hash function if need be.
407 static void
408 xdg_mime_init (void)
410 if (xdg_check_time_and_dirs ())
412 xdg_mime_shutdown ();
415 if (need_reread)
417 global_hash = _xdg_glob_hash_new ();
418 global_magic = _xdg_mime_magic_new ();
419 alias_list = _xdg_mime_alias_list_new ();
420 parent_list = _xdg_mime_parent_list_new ();
422 xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_mime_init_from_directory,
423 NULL);
425 need_reread = FALSE;
429 const char *
430 xdg_mime_get_mime_type_for_data (const void *data,
431 size_t len)
433 const char *mime_type;
435 xdg_mime_init ();
437 if (caches)
438 return _xdg_mime_cache_get_mime_type_for_data (data, len);
440 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len);
442 if (mime_type)
443 return mime_type;
445 return XDG_MIME_TYPE_UNKNOWN;
448 const char *
449 xdg_mime_get_mime_type_for_file (const char *file_name)
451 const char *mime_type;
452 FILE *file;
453 unsigned char *data;
454 int max_extent;
455 int bytes_read;
456 struct stat statbuf;
457 const char *base_name;
459 if (file_name == NULL)
460 return NULL;
461 if (! _xdg_utf8_validate (file_name))
462 return NULL;
464 xdg_mime_init ();
466 if (caches)
467 return _xdg_mime_cache_get_mime_type_for_file (file_name);
469 base_name = _xdg_get_base_name (file_name);
470 mime_type = xdg_mime_get_mime_type_from_file_name (base_name);
472 if (mime_type != XDG_MIME_TYPE_UNKNOWN)
473 return mime_type;
475 if (stat (file_name, &statbuf) != 0)
476 return XDG_MIME_TYPE_UNKNOWN;
478 if (!S_ISREG (statbuf.st_mode))
479 return XDG_MIME_TYPE_UNKNOWN;
481 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
482 * be large and need getting from a stream instead of just reading it all
483 * in. */
484 max_extent = _xdg_mime_magic_get_buffer_extents (global_magic);
485 data = malloc (max_extent);
486 if (data == NULL)
487 return XDG_MIME_TYPE_UNKNOWN;
489 file = fopen (file_name, "r");
490 if (file == NULL)
492 free (data);
493 return XDG_MIME_TYPE_UNKNOWN;
496 bytes_read = fread (data, 1, max_extent, file);
497 if (ferror (file))
499 free (data);
500 fclose (file);
501 return XDG_MIME_TYPE_UNKNOWN;
504 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read);
506 free (data);
507 fclose (file);
509 if (mime_type)
510 return mime_type;
512 return XDG_MIME_TYPE_UNKNOWN;
515 const char *
516 xdg_mime_get_mime_type_from_file_name (const char *file_name)
518 const char *mime_type;
520 xdg_mime_init ();
522 if (caches)
523 return _xdg_mime_cache_get_mime_type_from_file_name (file_name);
525 mime_type = _xdg_glob_hash_lookup_file_name (global_hash, file_name);
526 if (mime_type)
527 return mime_type;
528 else
529 return XDG_MIME_TYPE_UNKNOWN;
533 xdg_mime_is_valid_mime_type (const char *mime_type)
535 /* FIXME: We should make this a better test
537 return _xdg_utf8_validate (mime_type);
540 void
541 xdg_mime_shutdown (void)
543 XdgCallbackList *list;
545 /* FIXME: Need to make this (and the whole library) thread safe */
546 if (dir_time_list)
548 xdg_dir_time_list_free (dir_time_list);
549 dir_time_list = NULL;
552 if (global_hash)
554 _xdg_glob_hash_free (global_hash);
555 global_hash = NULL;
557 if (global_magic)
559 _xdg_mime_magic_free (global_magic);
560 global_magic = NULL;
563 if (alias_list)
565 _xdg_mime_alias_list_free (alias_list);
566 alias_list = NULL;
569 if (parent_list)
571 _xdg_mime_parent_list_free (parent_list);
572 parent_list = NULL;
575 for (list = callback_list; list; list = list->next)
576 (list->callback) (list->data);
578 need_reread = TRUE;
582 xdg_mime_get_max_buffer_extents (void)
584 xdg_mime_init ();
586 if (caches)
587 return _xdg_mime_cache_get_max_buffer_extents ();
589 return _xdg_mime_magic_get_buffer_extents (global_magic);
592 const char *
593 xdg_mime_unalias_mime_type (const char *mime_type)
595 const char *lookup;
597 xdg_mime_init ();
599 if (caches)
600 return _xdg_mime_cache_unalias_mime_type (mime_type);
602 if ((lookup = _xdg_mime_alias_list_lookup (alias_list, mime_type)) != NULL)
603 return lookup;
605 return mime_type;
609 xdg_mime_mime_type_equal (const char *mime_a,
610 const char *mime_b)
612 const char *unalias_a, *unalias_b;
614 xdg_mime_init ();
616 unalias_a = xdg_mime_unalias_mime_type (mime_a);
617 unalias_b = xdg_mime_unalias_mime_type (mime_b);
619 if (strcmp (unalias_a, unalias_b) == 0)
620 return 1;
622 return 0;
626 xdg_mime_media_type_equal (const char *mime_a,
627 const char *mime_b)
629 char *sep;
631 xdg_mime_init ();
633 sep = strchr (mime_a, '/');
635 if (sep && strncmp (mime_a, mime_b, sep - mime_a + 1) == 0)
636 return 1;
638 return 0;
641 #if 0
642 static int
643 xdg_mime_is_super_type (const char *mime)
645 int length;
646 const char *type;
648 length = strlen (mime);
649 type = &(mime[length - 2]);
651 if (strcmp (type, "/*") == 0)
652 return 1;
654 return 0;
656 #endif
659 xdg_mime_mime_type_subclass (const char *mime,
660 const char *base)
662 const char *umime, *ubase;
663 const char **parents;
665 xdg_mime_init ();
667 if (caches)
668 return _xdg_mime_cache_mime_type_subclass (mime, base);
670 umime = xdg_mime_unalias_mime_type (mime);
671 ubase = xdg_mime_unalias_mime_type (base);
673 if (strcmp (umime, ubase) == 0)
674 return 1;
676 #if 0
677 /* Handle supertypes */
678 if (xdg_mime_is_super_type (ubase) &&
679 xdg_mime_media_type_equal (umime, ubase))
680 return 1;
681 #endif
683 /* Handle special cases text/plain and application/octet-stream */
684 if (strcmp (ubase, "text/plain") == 0 &&
685 strncmp (umime, "text/", 5) == 0)
686 return 1;
688 if (strcmp (ubase, "application/octet-stream") == 0)
689 return 1;
691 parents = _xdg_mime_parent_list_lookup (parent_list, umime);
692 for (; parents && *parents; parents++)
694 if (xdg_mime_mime_type_subclass (*parents, ubase))
695 return 1;
698 return 0;
701 char **
702 xdg_mime_list_mime_parents (const char *mime)
704 const char **parents;
705 char **result;
706 int i, n;
708 if (caches)
709 return _xdg_mime_cache_list_mime_parents (mime);
711 parents = xdg_mime_get_mime_parents (mime);
712 for (i = 0; parents[i]; i++) ;
714 n = (i + 1) * sizeof (char *);
715 result = (char **) malloc (n);
716 memcpy (result, parents, n);
718 return result;
721 const char **
722 xdg_mime_get_mime_parents (const char *mime)
724 const char *umime;
726 xdg_mime_init ();
728 umime = xdg_mime_unalias_mime_type (mime);
730 return _xdg_mime_parent_list_lookup (parent_list, umime);
733 void
734 xdg_mime_dump (void)
736 printf ("*** ALIASES ***\n\n");
737 _xdg_mime_alias_list_dump (alias_list);
738 printf ("\n*** PARENTS ***\n\n");
739 _xdg_mime_parent_list_dump (parent_list);
743 /* Registers a function to be called every time the mime database reloads its files
746 xdg_mime_register_reload_callback (XdgMimeCallback callback,
747 void *data,
748 XdgMimeDestroy destroy)
750 XdgCallbackList *list_el;
751 static int callback_id = 1;
753 /* Make a new list element */
754 list_el = calloc (1, sizeof (XdgCallbackList));
755 list_el->callback_id = callback_id;
756 list_el->callback = callback;
757 list_el->data = data;
758 list_el->destroy = destroy;
759 list_el->next = callback_list;
760 if (list_el->next)
761 list_el->next->prev = list_el;
763 callback_list = list_el;
764 callback_id ++;
766 return callback_id - 1;
769 void
770 xdg_mime_remove_callback (int callback_id)
772 XdgCallbackList *list;
774 for (list = callback_list; list; list = list->next)
776 if (list->callback_id == callback_id)
778 if (list->next)
779 list->next = list->prev;
781 if (list->prev)
782 list->prev->next = list->next;
783 else
784 callback_list = list->next;
786 /* invoke the destroy handler */
787 (list->destroy) (list->data);
788 free (list);
789 return;