Italian translation update.
[rox-filer.git] / ROX-Filer / src / xdgmime.c
blob133282de3738d0fcd62a846c835bc176e3a71b93
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 /* ROX: */
48 #include "global.h"
49 #include "main.h"
51 typedef struct XdgDirTimeList XdgDirTimeList;
52 typedef struct XdgCallbackList XdgCallbackList;
54 static int need_reread = TRUE;
55 static time_t last_stat_time = 0;
57 static XdgGlobHash *global_hash = NULL;
58 static XdgMimeMagic *global_magic = NULL;
59 static XdgAliasList *alias_list = NULL;
60 static XdgParentList *parent_list = NULL;
61 static XdgDirTimeList *dir_time_list = NULL;
62 static XdgCallbackList *callback_list = NULL;
64 XdgMimeCache **_xdg_mime_caches = NULL;
65 static int n_caches = 0;
67 const char xdg_mime_type_unknown[] = "application/octet-stream";
70 enum
72 XDG_CHECKED_UNCHECKED,
73 XDG_CHECKED_VALID,
74 XDG_CHECKED_INVALID
77 struct XdgDirTimeList
79 time_t mtime;
80 char *directory_name;
81 int checked;
82 XdgDirTimeList *next;
83 XdgMimeCache *cache;
86 struct XdgCallbackList
88 XdgCallbackList *next;
89 XdgCallbackList *prev;
90 int callback_id;
91 XdgMimeCallback callback;
92 void *data;
93 XdgMimeDestroy destroy;
96 /* Function called by xdg_run_command_on_dirs. If it returns TRUE, further
97 * directories aren't looked at */
98 typedef int (*XdgDirectoryFunc) (const char *directory,
99 void *user_data);
101 static XdgDirTimeList *
102 xdg_dir_time_list_new (void)
104 XdgDirTimeList *retval;
106 retval = calloc (1, sizeof (XdgDirTimeList));
107 retval->checked = XDG_CHECKED_UNCHECKED;
109 return retval;
112 static void
113 xdg_dir_time_list_free (XdgDirTimeList *list)
115 XdgDirTimeList *next;
117 while (list)
119 next = list->next;
120 free (list->directory_name);
121 free (list);
122 list = next;
126 static int
127 xdg_mime_init_from_directory (const char *directory)
129 char *file_name;
130 struct stat st;
131 XdgDirTimeList *list;
133 assert (directory != NULL);
135 file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
136 strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
137 if (stat (file_name, &st) == 0)
139 XdgMimeCache *cache = _xdg_mime_cache_new_from_file (file_name);
141 if (cache != NULL)
143 list = xdg_dir_time_list_new ();
144 list->directory_name = file_name;
145 list->mtime = st.st_mtime;
146 list->next = dir_time_list;
147 list->cache = cache;
148 dir_time_list = list;
150 _xdg_mime_caches = realloc (_xdg_mime_caches, sizeof (XdgMimeCache *) * (n_caches + 2));
151 _xdg_mime_caches[n_caches] = cache;
152 _xdg_mime_caches[n_caches + 1] = NULL;
153 n_caches++;
155 return FALSE;
158 free (file_name);
160 file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
161 strcpy (file_name, directory); strcat (file_name, "/mime/globs");
162 if (stat (file_name, &st) == 0)
164 _xdg_mime_glob_read_from_file (global_hash, file_name);
166 list = xdg_dir_time_list_new ();
167 list->directory_name = file_name;
168 list->mtime = st.st_mtime;
169 list->next = dir_time_list;
170 dir_time_list = list;
172 else
174 free (file_name);
177 file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
178 strcpy (file_name, directory); strcat (file_name, "/mime/magic");
179 if (stat (file_name, &st) == 0)
181 _xdg_mime_magic_read_from_file (global_magic, file_name);
183 list = xdg_dir_time_list_new ();
184 list->directory_name = file_name;
185 list->mtime = st.st_mtime;
186 list->next = dir_time_list;
187 dir_time_list = list;
189 else
191 free (file_name);
194 file_name = malloc (strlen (directory) + strlen ("/mime/aliases") + 1);
195 strcpy (file_name, directory); strcat (file_name, "/mime/aliases");
196 _xdg_mime_alias_read_from_file (alias_list, file_name);
197 free (file_name);
199 file_name = malloc (strlen (directory) + strlen ("/mime/subclasses") + 1);
200 strcpy (file_name, directory); strcat (file_name, "/mime/subclasses");
201 _xdg_mime_parent_read_from_file (parent_list, file_name);
202 free (file_name);
204 return FALSE; /* Keep processing */
207 /* Runs a command on all the directories in the search path */
208 static void
209 xdg_run_command_on_dirs (XdgDirectoryFunc func,
210 void *user_data)
212 const char *xdg_data_home;
213 const char *xdg_data_dirs;
214 const char *ptr;
216 xdg_data_home = getenv ("XDG_DATA_HOME");
217 if (xdg_data_home)
219 if ((func) (xdg_data_home, user_data))
220 return;
222 else
224 const char *home;
226 home = getenv ("HOME");
227 if (home != NULL)
229 char *guessed_xdg_home;
230 int stop_processing;
232 guessed_xdg_home = malloc (strlen (home) + strlen ("/.local/share/") + 1);
233 strcpy (guessed_xdg_home, home);
234 strcat (guessed_xdg_home, "/.local/share/");
235 stop_processing = (func) (guessed_xdg_home, user_data);
236 free (guessed_xdg_home);
238 if (stop_processing)
239 return;
243 xdg_data_dirs = getenv ("XDG_DATA_DIRS");
244 if (xdg_data_dirs == NULL)
245 xdg_data_dirs = "/usr/local/share/:/usr/share/";
247 ptr = xdg_data_dirs;
249 while (*ptr != '\000')
251 const char *end_ptr;
252 char *dir;
253 int len;
254 int stop_processing;
256 end_ptr = ptr;
257 while (*end_ptr != ':' && *end_ptr != '\000')
258 end_ptr ++;
260 if (end_ptr == ptr)
262 ptr++;
263 continue;
266 if (*end_ptr == ':')
267 len = end_ptr - ptr;
268 else
269 len = end_ptr - ptr + 1;
270 dir = malloc (len + 1);
271 strncpy (dir, ptr, len);
272 dir[len] = '\0';
273 stop_processing = (func) (dir, user_data);
274 free (dir);
276 if (stop_processing)
277 return;
279 ptr = end_ptr;
283 static XdgMimeCache *
284 xdg_lookup_cache_for_file (const char *file_path)
286 XdgDirTimeList *list;
288 for (list = dir_time_list; list; list = list->next)
289 if (! strcmp (list->directory_name, file_path))
290 return list->cache;
292 return NULL;
295 /* Checks file_path to make sure it has the same mtime as last time it was
296 * checked. If it has a different mtime, or if the file doesn't exist, it
297 * returns FALSE.
299 * FIXME: This doesn't protect against permission changes.
301 static int
302 xdg_check_file (const char *file_path)
304 struct stat st;
306 /* If the file exists */
307 if (stat (file_path, &st) == 0)
309 XdgDirTimeList *list;
311 for (list = dir_time_list; list; list = list->next)
313 if (! strcmp (list->directory_name, file_path) &&
314 st.st_mtime == list->mtime)
316 if (list->checked == XDG_CHECKED_UNCHECKED)
317 list->checked = XDG_CHECKED_VALID;
318 else if (list->checked == XDG_CHECKED_VALID)
319 list->checked = XDG_CHECKED_INVALID;
321 return (list->checked != XDG_CHECKED_VALID);
324 return TRUE;
327 return FALSE;
330 static int
331 xdg_check_dir (const char *directory,
332 int *invalid_dir_list)
334 int invalid, has_cache;
335 char *file_name;
337 assert (directory != NULL);
339 /* Check the mime.cache file */
340 file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
341 strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
342 invalid = xdg_check_file (file_name);
343 has_cache = xdg_lookup_cache_for_file (file_name) != NULL;
344 free (file_name);
346 if (has_cache)
348 if (invalid)
350 *invalid_dir_list = TRUE;
351 return TRUE;
354 return FALSE;
357 /* Check the globs file */
358 file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
359 strcpy (file_name, directory); strcat (file_name, "/mime/globs");
360 invalid = xdg_check_file (file_name);
361 free (file_name);
362 if (invalid)
364 *invalid_dir_list = TRUE;
365 return TRUE;
368 /* Check the magic file */
369 file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
370 strcpy (file_name, directory); strcat (file_name, "/mime/magic");
371 invalid = xdg_check_file (file_name);
372 free (file_name);
373 if (invalid)
375 *invalid_dir_list = TRUE;
376 return TRUE;
379 return FALSE; /* Keep processing */
382 /* Walks through all the mime files stat()ing them to see if they've changed.
383 * Returns TRUE if they have. */
384 static int
385 xdg_check_dirs (void)
387 XdgDirTimeList *list;
388 int invalid_dir_list = FALSE;
390 for (list = dir_time_list; list; list = list->next)
391 list->checked = XDG_CHECKED_UNCHECKED;
393 xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_check_dir,
394 &invalid_dir_list);
396 if (invalid_dir_list)
397 return TRUE;
399 for (list = dir_time_list; list; list = list->next)
401 if (list->checked != XDG_CHECKED_VALID)
402 return TRUE;
405 return FALSE;
408 /* We want to avoid stat()ing on every single mime call, so we only look for
409 * newer files every 5 seconds. This will return TRUE if we need to reread the
410 * mime data from disk.
412 static int
413 xdg_check_time_and_dirs (void)
415 struct timeval tv;
416 time_t current_time;
417 int retval = FALSE;
419 gettimeofday (&tv, NULL);
420 current_time = tv.tv_sec;
422 if (current_time >= last_stat_time + 5)
424 retval = xdg_check_dirs ();
425 last_stat_time = current_time;
428 return retval;
431 /* Called in every public function. It reloads the hash function if need be.
433 static void
434 xdg_mime_init (void)
436 if (xdg_check_time_and_dirs ())
438 xdg_mime_shutdown ();
441 if (need_reread)
443 char *mime_parents;
444 int l;
446 global_hash = _xdg_glob_hash_new ();
447 global_magic = _xdg_mime_magic_new ();
448 alias_list = _xdg_mime_alias_list_new ();
449 parent_list = _xdg_mime_parent_list_new ();
451 xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_mime_init_from_directory,
452 NULL);
454 /* ROX: We want to support shared-mime-database < 0.16, where we can't
455 * do this with the rox.xml file.
457 mime_parents = g_build_filename(app_dir, "subclasses", NULL);
458 _xdg_mime_parent_read_from_file(parent_list, mime_parents);
459 g_free(mime_parents);
461 need_reread = FALSE;
465 const char *
466 xdg_mime_get_mime_type_for_data (const void *data,
467 size_t len)
469 const char *mime_type;
471 xdg_mime_init ();
473 if (_xdg_mime_caches)
474 return _xdg_mime_cache_get_mime_type_for_data (data, len);
476 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len, NULL, 0);
478 if (mime_type)
479 return mime_type;
481 return XDG_MIME_TYPE_UNKNOWN;
484 const char *
485 xdg_mime_get_mime_type_for_file (const char *file_name,
486 struct stat *statbuf)
488 const char *mime_type;
489 /* Used to detect whether multiple MIME types match file_name */
490 const char *mime_types[2];
491 FILE *file;
492 unsigned char *data;
493 int max_extent;
494 int bytes_read;
495 struct stat buf;
496 const char *base_name;
497 int n;
499 if (file_name == NULL)
500 return NULL;
501 if (! _xdg_utf8_validate (file_name))
502 return NULL;
504 xdg_mime_init ();
506 if (_xdg_mime_caches)
507 return _xdg_mime_cache_get_mime_type_for_file (file_name, statbuf);
509 base_name = _xdg_get_base_name (file_name);
510 n = _xdg_glob_hash_lookup_file_name (global_hash, base_name, mime_types, 2);
512 if (n == 1)
513 return mime_types[0];
515 if (!statbuf)
517 if (stat (file_name, &buf) != 0)
518 return XDG_MIME_TYPE_UNKNOWN;
520 statbuf = &buf;
523 if (!S_ISREG (statbuf->st_mode))
524 return XDG_MIME_TYPE_UNKNOWN;
526 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
527 * be large and need getting from a stream instead of just reading it all
528 * in. */
529 max_extent = _xdg_mime_magic_get_buffer_extents (global_magic);
530 data = malloc (max_extent);
531 if (data == NULL)
532 return XDG_MIME_TYPE_UNKNOWN;
534 file = fopen (file_name, "r");
535 if (file == NULL)
537 free (data);
538 return XDG_MIME_TYPE_UNKNOWN;
541 bytes_read = fread (data, 1, max_extent, file);
542 if (ferror (file))
544 free (data);
545 fclose (file);
546 return XDG_MIME_TYPE_UNKNOWN;
549 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read,
550 mime_types, n);
552 free (data);
553 fclose (file);
555 if (mime_type)
556 return mime_type;
558 return XDG_MIME_TYPE_UNKNOWN;
561 const char *
562 xdg_mime_get_mime_type_from_file_name (const char *file_name)
564 const char *mime_types[2];
566 xdg_mime_init ();
568 if (_xdg_mime_caches)
569 return _xdg_mime_cache_get_mime_type_from_file_name (file_name);
571 if (_xdg_glob_hash_lookup_file_name (global_hash, file_name, mime_types, 2) == 1)
572 return mime_types[0];
573 else
574 return XDG_MIME_TYPE_UNKNOWN;
578 xdg_mime_is_valid_mime_type (const char *mime_type)
580 /* FIXME: We should make this a better test
582 return _xdg_utf8_validate (mime_type);
585 void
586 xdg_mime_shutdown (void)
588 XdgCallbackList *list;
590 /* FIXME: Need to make this (and the whole library) thread safe */
591 if (dir_time_list)
593 xdg_dir_time_list_free (dir_time_list);
594 dir_time_list = NULL;
597 if (global_hash)
599 _xdg_glob_hash_free (global_hash);
600 global_hash = NULL;
602 if (global_magic)
604 _xdg_mime_magic_free (global_magic);
605 global_magic = NULL;
608 if (alias_list)
610 _xdg_mime_alias_list_free (alias_list);
611 alias_list = NULL;
614 if (parent_list)
616 _xdg_mime_parent_list_free (parent_list);
617 parent_list = NULL;
620 if (_xdg_mime_caches)
622 int i;
623 for (i = 0; i < n_caches; i++)
624 _xdg_mime_cache_unref (_xdg_mime_caches[i]);
625 free (_xdg_mime_caches);
626 _xdg_mime_caches = NULL;
627 n_caches = 0;
630 for (list = callback_list; list; list = list->next)
631 (list->callback) (list->data);
633 need_reread = TRUE;
637 xdg_mime_get_max_buffer_extents (void)
639 xdg_mime_init ();
641 if (_xdg_mime_caches)
642 return _xdg_mime_cache_get_max_buffer_extents ();
644 return _xdg_mime_magic_get_buffer_extents (global_magic);
647 static const char *
648 _xdg_mime_unalias_mime_type (const char *mime_type)
650 const char *lookup;
652 if (_xdg_mime_caches)
653 return _xdg_mime_cache_unalias_mime_type (mime_type);
655 if ((lookup = _xdg_mime_alias_list_lookup (alias_list, mime_type)) != NULL)
656 return lookup;
658 return mime_type;
661 const char *
662 xdg_mime_unalias_mime_type (const char *mime_type)
664 xdg_mime_init ();
666 return _xdg_mime_unalias_mime_type (mime_type);
670 _xdg_mime_mime_type_equal (const char *mime_a,
671 const char *mime_b)
673 const char *unalias_a, *unalias_b;
675 unalias_a = _xdg_mime_unalias_mime_type (mime_a);
676 unalias_b = _xdg_mime_unalias_mime_type (mime_b);
678 if (strcmp (unalias_a, unalias_b) == 0)
679 return 1;
681 return 0;
685 xdg_mime_mime_type_equal (const char *mime_a,
686 const char *mime_b)
688 xdg_mime_init ();
690 return _xdg_mime_mime_type_equal (mime_a, mime_b);
694 _xdg_mime_media_type_equal (const char *mime_a,
695 const char *mime_b)
697 char *sep;
699 xdg_mime_init ();
701 sep = strchr (mime_a, '/');
703 if (sep && strncmp (mime_a, mime_b, sep - mime_a + 1) == 0)
704 return 1;
706 return 0;
710 xdg_mime_media_type_equal (const char *mime_a,
711 const char *mime_b)
713 xdg_mime_init ();
715 return _xdg_mime_media_type_equal (mime_a, mime_b);
718 #if 0
719 static int
720 xdg_mime_is_super_type (const char *mime)
722 int length;
723 const char *type;
725 length = strlen (mime);
726 type = &(mime[length - 2]);
728 if (strcmp (type, "/*") == 0)
729 return 1;
731 return 0;
733 #endif
736 _xdg_mime_mime_type_subclass (const char *mime,
737 const char *base)
739 const char *umime, *ubase;
740 const char **parents;
742 if (_xdg_mime_caches)
743 return _xdg_mime_cache_mime_type_subclass (mime, base);
745 umime = _xdg_mime_unalias_mime_type (mime);
746 ubase = _xdg_mime_unalias_mime_type (base);
748 if (strcmp (umime, ubase) == 0)
749 return 1;
751 #if 0
752 /* Handle supertypes */
753 if (xdg_mime_is_super_type (ubase) &&
754 _xdg_mime_media_type_equal (umime, ubase))
755 return 1;
756 #endif
758 /* Handle special cases text/plain and application/octet-stream */
759 if (strcmp (ubase, "text/plain") == 0 &&
760 strncmp (umime, "text/", 5) == 0)
761 return 1;
763 if (strcmp (ubase, "application/octet-stream") == 0)
764 return 1;
766 parents = _xdg_mime_parent_list_lookup (parent_list, umime);
767 for (; parents && *parents; parents++)
769 if (_xdg_mime_mime_type_subclass (*parents, ubase))
770 return 1;
773 return 0;
777 xdg_mime_mime_type_subclass (const char *mime,
778 const char *base)
780 xdg_mime_init ();
782 return _xdg_mime_mime_type_subclass (mime, base);
785 char **
786 xdg_mime_list_mime_parents (const char *mime)
788 const char **parents;
789 char **result;
790 int i, n;
792 if (_xdg_mime_caches)
793 return _xdg_mime_cache_list_mime_parents (mime);
795 parents = xdg_mime_get_mime_parents (mime);
797 if (!parents)
798 return NULL;
800 for (i = 0; parents[i]; i++) ;
802 n = (i + 1) * sizeof (char *);
803 result = (char **) malloc (n);
804 memcpy (result, parents, n);
806 return result;
809 const char **
810 xdg_mime_get_mime_parents (const char *mime)
812 const char *umime;
814 xdg_mime_init ();
816 umime = _xdg_mime_unalias_mime_type (mime);
818 return _xdg_mime_parent_list_lookup (parent_list, umime);
821 void
822 xdg_mime_dump (void)
824 printf ("*** ALIASES ***\n\n");
825 _xdg_mime_alias_list_dump (alias_list);
826 printf ("\n*** PARENTS ***\n\n");
827 _xdg_mime_parent_list_dump (parent_list);
828 printf ("\n*** CACHE ***\n\n");
829 _xdg_glob_hash_dump (global_hash);
833 /* Registers a function to be called every time the mime database reloads its files
836 xdg_mime_register_reload_callback (XdgMimeCallback callback,
837 void *data,
838 XdgMimeDestroy destroy)
840 XdgCallbackList *list_el;
841 static int callback_id = 1;
843 /* Make a new list element */
844 list_el = calloc (1, sizeof (XdgCallbackList));
845 list_el->callback_id = callback_id;
846 list_el->callback = callback;
847 list_el->data = data;
848 list_el->destroy = destroy;
849 list_el->next = callback_list;
850 if (list_el->next)
851 list_el->next->prev = list_el;
853 callback_list = list_el;
854 callback_id ++;
856 return callback_id - 1;
859 void
860 xdg_mime_remove_callback (int callback_id)
862 XdgCallbackList *list;
864 for (list = callback_list; list; list = list->next)
866 if (list->callback_id == callback_id)
868 if (list->next)
869 list->next = list->prev;
871 if (list->prev)
872 list->prev->next = list->next;
873 else
874 callback_list = list->next;
876 /* invoke the destroy handler */
877 (list->destroy) (list->data);
878 free (list);
879 return;