r4370: Added Vietnamese translation (Xomhau Newnick).
[rox-filer/ma.git] / ROX-Filer / src / xdgmime.c
blob6fd1b04530117fb353228dda6064fd5bffcf1ac5
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 #include "global.h"
48 #include "main.h"
50 typedef struct XdgDirTimeList XdgDirTimeList;
51 typedef struct XdgCallbackList XdgCallbackList;
53 static int need_reread = TRUE;
54 static time_t last_stat_time = 0;
56 static XdgGlobHash *global_hash = NULL;
57 static XdgMimeMagic *global_magic = NULL;
58 static XdgAliasList *alias_list = NULL;
59 static XdgParentList *parent_list = NULL;
60 static XdgDirTimeList *dir_time_list = NULL;
61 static XdgCallbackList *callback_list = NULL;
62 XdgMimeCache **caches = NULL;
63 int n_caches = 0;
65 const char *xdg_mime_type_unknown = "application/octet-stream";
66 const char *xdg_mime_type_unknown_text = "text/plain";
69 enum
71 XDG_CHECKED_UNCHECKED,
72 XDG_CHECKED_VALID,
73 XDG_CHECKED_INVALID
76 struct XdgDirTimeList
78 time_t mtime;
79 char *directory_name;
80 int checked;
81 XdgDirTimeList *next;
84 struct XdgCallbackList
86 XdgCallbackList *next;
87 XdgCallbackList *prev;
88 int callback_id;
89 XdgMimeCallback callback;
90 void *data;
91 XdgMimeDestroy destroy;
94 /* Function called by xdg_run_command_on_dirs. If it returns TRUE, further
95 * directories aren't looked at */
96 typedef int (*XdgDirectoryFunc) (const char *directory,
97 void *user_data);
99 static XdgDirTimeList *
100 xdg_dir_time_list_new (void)
102 XdgDirTimeList *retval;
104 retval = calloc (1, sizeof (XdgDirTimeList));
105 retval->checked = XDG_CHECKED_UNCHECKED;
107 return retval;
110 static void
111 xdg_dir_time_list_free (XdgDirTimeList *list)
113 XdgDirTimeList *next;
115 while (list)
117 next = list->next;
118 free (list->directory_name);
119 free (list);
120 list = next;
124 static int
125 xdg_mime_init_from_directory (const char *directory)
127 char *file_name;
128 struct stat st;
129 XdgDirTimeList *list;
131 assert (directory != NULL);
133 file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
134 strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
135 if (stat (file_name, &st) == 0)
137 XdgMimeCache *cache = _xdg_mime_cache_new_from_file (file_name);
139 if (cache != NULL)
141 list = xdg_dir_time_list_new ();
142 list->directory_name = file_name;
143 list->mtime = st.st_mtime;
144 list->next = dir_time_list;
145 dir_time_list = list;
147 caches = realloc (caches, sizeof (XdgMimeCache *) * (n_caches + 1));
148 caches[n_caches] = cache;
149 n_caches++;
151 return FALSE;
154 free (file_name);
156 file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
157 strcpy (file_name, directory); strcat (file_name, "/mime/globs");
158 if (stat (file_name, &st) == 0)
160 _xdg_mime_glob_read_from_file (global_hash, file_name);
162 list = xdg_dir_time_list_new ();
163 list->directory_name = file_name;
164 list->mtime = st.st_mtime;
165 list->next = dir_time_list;
166 dir_time_list = list;
168 else
170 free (file_name);
173 file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
174 strcpy (file_name, directory); strcat (file_name, "/mime/magic");
175 if (stat (file_name, &st) == 0)
177 _xdg_mime_magic_read_from_file (global_magic, file_name);
179 list = xdg_dir_time_list_new ();
180 list->directory_name = file_name;
181 list->mtime = st.st_mtime;
182 list->next = dir_time_list;
183 dir_time_list = list;
185 else
187 free (file_name);
190 file_name = malloc (strlen (directory) + strlen ("/mime/aliases") + 1);
191 strcpy (file_name, directory); strcat (file_name, "/mime/aliases");
192 _xdg_mime_alias_read_from_file (alias_list, file_name);
193 free (file_name);
195 file_name = malloc (strlen (directory) + strlen ("/mime/subclasses") + 1);
196 strcpy (file_name, directory); strcat (file_name, "/mime/subclasses");
197 _xdg_mime_parent_read_from_file (parent_list, file_name);
198 free (file_name);
200 return FALSE; /* Keep processing */
203 /* Runs a command on all the directories in the search path */
204 static void
205 xdg_run_command_on_dirs (XdgDirectoryFunc func,
206 void *user_data)
208 const char *xdg_data_home;
209 const char *xdg_data_dirs;
210 const char *ptr;
212 xdg_data_home = getenv ("XDG_DATA_HOME");
213 if (xdg_data_home)
215 if ((func) (xdg_data_home, user_data))
216 return;
218 else
220 const char *home;
222 home = getenv ("HOME");
223 if (home != NULL)
225 char *guessed_xdg_home;
226 int stop_processing;
228 guessed_xdg_home = malloc (strlen (home) + strlen ("/.local/share/") + 1);
229 strcpy (guessed_xdg_home, home);
230 strcat (guessed_xdg_home, "/.local/share/");
231 stop_processing = (func) (guessed_xdg_home, user_data);
232 free (guessed_xdg_home);
234 if (stop_processing)
235 return;
239 xdg_data_dirs = getenv ("XDG_DATA_DIRS");
240 if (xdg_data_dirs == NULL)
241 xdg_data_dirs = "/usr/local/share/:/usr/share/";
243 ptr = xdg_data_dirs;
245 while (*ptr != '\000')
247 const char *end_ptr;
248 char *dir;
249 int len;
250 int stop_processing;
252 end_ptr = ptr;
253 while (*end_ptr != ':' && *end_ptr != '\000')
254 end_ptr ++;
256 if (end_ptr == ptr)
258 ptr++;
259 continue;
262 if (*end_ptr == ':')
263 len = end_ptr - ptr;
264 else
265 len = end_ptr - ptr + 1;
266 dir = malloc (len + 1);
267 strncpy (dir, ptr, len);
268 dir[len] = '\0';
269 stop_processing = (func) (dir, user_data);
270 free (dir);
272 if (stop_processing)
273 return;
275 ptr = end_ptr;
279 /* Checks file_path to make sure it has the same mtime as last time it was
280 * checked. If it has a different mtime, or if the file doesn't exist, it
281 * returns FALSE.
283 * FIXME: This doesn't protect against permission changes.
285 static int
286 xdg_check_file (const char *file_path)
288 struct stat st;
290 /* If the file exists */
291 if (stat (file_path, &st) == 0)
293 XdgDirTimeList *list;
295 for (list = dir_time_list; list; list = list->next)
297 if (! strcmp (list->directory_name, file_path) &&
298 st.st_mtime == list->mtime)
300 if (list->checked == XDG_CHECKED_UNCHECKED)
301 list->checked = XDG_CHECKED_VALID;
302 else if (list->checked == XDG_CHECKED_VALID)
303 list->checked = XDG_CHECKED_INVALID;
305 return (list->checked != XDG_CHECKED_VALID);
308 return TRUE;
311 return FALSE;
314 static int
315 xdg_check_dir (const char *directory,
316 int *invalid_dir_list)
318 int invalid;
319 char *file_name;
321 assert (directory != NULL);
323 /* Check the globs file */
324 file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
325 strcpy (file_name, directory); strcat (file_name, "/mime/globs");
326 invalid = xdg_check_file (file_name);
327 free (file_name);
328 if (invalid)
330 *invalid_dir_list = TRUE;
331 return TRUE;
334 /* Check the magic file */
335 file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
336 strcpy (file_name, directory); strcat (file_name, "/mime/magic");
337 invalid = xdg_check_file (file_name);
338 free (file_name);
339 if (invalid)
341 *invalid_dir_list = TRUE;
342 return TRUE;
345 /* Check the mime.cache file */
346 file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
347 strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
348 invalid = xdg_check_file (file_name);
349 free (file_name);
350 if (invalid)
352 *invalid_dir_list = TRUE;
353 return TRUE;
356 return FALSE; /* Keep processing */
359 /* Walks through all the mime files stat()ing them to see if they've changed.
360 * Returns TRUE if they have. */
361 static int
362 xdg_check_dirs (void)
364 XdgDirTimeList *list;
365 int invalid_dir_list = FALSE;
367 for (list = dir_time_list; list; list = list->next)
368 list->checked = XDG_CHECKED_UNCHECKED;
370 xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_check_dir,
371 &invalid_dir_list);
373 if (invalid_dir_list)
374 return TRUE;
376 for (list = dir_time_list; list; list = list->next)
378 if (list->checked != XDG_CHECKED_VALID)
379 return TRUE;
382 return FALSE;
385 /* We want to avoid stat()ing on every single mime call, so we only look for
386 * newer files every 5 seconds. This will return TRUE if we need to reread the
387 * mime data from disk.
389 static int
390 xdg_check_time_and_dirs (void)
392 struct timeval tv;
393 time_t current_time;
394 int retval = FALSE;
396 gettimeofday (&tv, NULL);
397 current_time = tv.tv_sec;
399 if (current_time >= last_stat_time + 5)
401 retval = xdg_check_dirs ();
402 last_stat_time = current_time;
405 return retval;
408 /* Called in every public function. It reloads the hash function if need be.
410 static void
411 xdg_mime_init (void)
413 if (xdg_check_time_and_dirs ())
415 xdg_mime_shutdown ();
418 if (need_reread)
420 char *mime_parents;
422 global_hash = _xdg_glob_hash_new ();
423 global_magic = _xdg_mime_magic_new ();
424 alias_list = _xdg_mime_alias_list_new ();
425 parent_list = _xdg_mime_parent_list_new ();
427 xdg_run_command_on_dirs ((XdgDirectoryFunc) xdg_mime_init_from_directory,
428 NULL);
430 /* We want to support shared-mime-database < 0.16, where we can't
431 * do this with the rox.xml file.
433 mime_parents = g_build_filename(app_dir, "subclasses", NULL);
434 _xdg_mime_parent_read_from_file(parent_list, mime_parents);
435 g_free(mime_parents);
437 need_reread = FALSE;
441 const char *
442 xdg_mime_get_mime_type_for_data (const void *data,
443 size_t len)
445 const char *mime_type;
447 xdg_mime_init ();
449 if (caches)
450 return _xdg_mime_cache_get_mime_type_for_data (data, len);
452 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len);
454 if (mime_type)
455 return mime_type;
457 return XDG_MIME_TYPE_UNKNOWN;
460 const char *
461 xdg_mime_get_mime_type_for_file (const char *file_name)
463 const char *mime_type;
464 FILE *file;
465 unsigned char *data;
466 int max_extent;
467 int bytes_read;
468 struct stat statbuf;
469 const char *base_name;
471 if (file_name == NULL)
472 return NULL;
473 if (! _xdg_utf8_validate (file_name))
474 return NULL;
476 xdg_mime_init ();
478 if (caches)
479 return _xdg_mime_cache_get_mime_type_for_file (file_name);
481 base_name = _xdg_get_base_name (file_name);
482 mime_type = xdg_mime_get_mime_type_from_file_name (base_name);
484 if (mime_type != XDG_MIME_TYPE_UNKNOWN)
485 return mime_type;
487 if (stat (file_name, &statbuf) != 0)
488 return XDG_MIME_TYPE_UNKNOWN;
490 if (!S_ISREG (statbuf.st_mode))
491 return XDG_MIME_TYPE_UNKNOWN;
493 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
494 * be large and need getting from a stream instead of just reading it all
495 * in. */
496 max_extent = _xdg_mime_magic_get_buffer_extents (global_magic);
497 data = malloc (max_extent);
498 if (data == NULL)
499 return XDG_MIME_TYPE_UNKNOWN;
501 file = fopen (file_name, "r");
502 if (file == NULL)
504 free (data);
505 return XDG_MIME_TYPE_UNKNOWN;
508 bytes_read = fread (data, 1, max_extent, file);
509 if (ferror (file))
511 free (data);
512 fclose (file);
513 return XDG_MIME_TYPE_UNKNOWN;
516 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read);
518 free (data);
519 fclose (file);
521 if (mime_type)
522 return mime_type;
524 return XDG_MIME_TYPE_UNKNOWN;
527 const char *
528 xdg_mime_get_mime_type_from_file_name (const char *file_name)
530 const char *mime_type;
532 xdg_mime_init ();
534 if (caches)
535 return _xdg_mime_cache_get_mime_type_from_file_name (file_name);
537 mime_type = _xdg_glob_hash_lookup_file_name (global_hash, file_name);
538 if (mime_type)
539 return mime_type;
540 else
541 return XDG_MIME_TYPE_UNKNOWN;
545 xdg_mime_is_valid_mime_type (const char *mime_type)
547 /* FIXME: We should make this a better test
549 return _xdg_utf8_validate (mime_type);
552 void
553 xdg_mime_shutdown (void)
555 XdgCallbackList *list;
557 /* FIXME: Need to make this (and the whole library) thread safe */
558 if (dir_time_list)
560 xdg_dir_time_list_free (dir_time_list);
561 dir_time_list = NULL;
564 if (global_hash)
566 _xdg_glob_hash_free (global_hash);
567 global_hash = NULL;
569 if (global_magic)
571 _xdg_mime_magic_free (global_magic);
572 global_magic = NULL;
575 if (alias_list)
577 _xdg_mime_alias_list_free (alias_list);
578 alias_list = NULL;
581 if (parent_list)
583 _xdg_mime_parent_list_free (parent_list);
584 parent_list = NULL;
587 for (list = callback_list; list; list = list->next)
588 (list->callback) (list->data);
590 need_reread = TRUE;
594 xdg_mime_get_max_buffer_extents (void)
596 xdg_mime_init ();
598 if (caches)
599 return _xdg_mime_cache_get_max_buffer_extents ();
601 return _xdg_mime_magic_get_buffer_extents (global_magic);
604 const char *
605 xdg_mime_unalias_mime_type (const char *mime_type)
607 const char *lookup;
609 xdg_mime_init ();
611 if (caches)
612 return _xdg_mime_cache_unalias_mime_type (mime_type);
614 if ((lookup = _xdg_mime_alias_list_lookup (alias_list, mime_type)) != NULL)
615 return lookup;
617 return mime_type;
621 xdg_mime_mime_type_equal (const char *mime_a,
622 const char *mime_b)
624 const char *unalias_a, *unalias_b;
626 xdg_mime_init ();
628 unalias_a = xdg_mime_unalias_mime_type (mime_a);
629 unalias_b = xdg_mime_unalias_mime_type (mime_b);
631 if (strcmp (unalias_a, unalias_b) == 0)
632 return 1;
634 return 0;
638 xdg_mime_media_type_equal (const char *mime_a,
639 const char *mime_b)
641 char *sep;
643 xdg_mime_init ();
645 sep = strchr (mime_a, '/');
647 if (sep && strncmp (mime_a, mime_b, sep - mime_a + 1) == 0)
648 return 1;
650 return 0;
653 #if 0
654 static int
655 xdg_mime_is_super_type (const char *mime)
657 int length;
658 const char *type;
660 length = strlen (mime);
661 type = &(mime[length - 2]);
663 if (strcmp (type, "/*") == 0)
664 return 1;
666 return 0;
668 #endif
671 xdg_mime_mime_type_subclass (const char *mime,
672 const char *base)
674 const char *umime, *ubase;
675 const char **parents;
677 xdg_mime_init ();
679 if (caches)
680 return _xdg_mime_cache_mime_type_subclass (mime, base);
682 umime = xdg_mime_unalias_mime_type (mime);
683 ubase = xdg_mime_unalias_mime_type (base);
685 if (strcmp (umime, ubase) == 0)
686 return 1;
688 #if 0
689 /* Handle supertypes */
690 if (xdg_mime_is_super_type (ubase) &&
691 xdg_mime_media_type_equal (umime, ubase))
692 return 1;
693 #endif
695 /* Handle special cases text/plain and application/octet-stream */
696 if (strcmp (ubase, "text/plain") == 0 &&
697 strncmp (umime, "text/", 5) == 0)
698 return 1;
700 if (strcmp (ubase, "application/octet-stream") == 0)
701 return 1;
703 parents = _xdg_mime_parent_list_lookup (parent_list, umime);
704 for (; parents && *parents; parents++)
706 if (xdg_mime_mime_type_subclass (*parents, ubase))
707 return 1;
710 return 0;
713 char **
714 xdg_mime_list_mime_parents (const char *mime)
716 const char **parents;
717 char **result;
718 int i, n;
720 if (caches)
721 return _xdg_mime_cache_list_mime_parents (mime);
723 parents = xdg_mime_get_mime_parents (mime);
724 for (i = 0; parents[i]; i++) ;
726 n = (i + 1) * sizeof (char *);
727 result = (char **) malloc (n);
728 memcpy (result, parents, n);
730 return result;
733 const char **
734 xdg_mime_get_mime_parents (const char *mime)
736 const char *umime;
738 xdg_mime_init ();
740 umime = xdg_mime_unalias_mime_type (mime);
742 return _xdg_mime_parent_list_lookup (parent_list, umime);
745 void
746 xdg_mime_dump (void)
748 printf ("*** ALIASES ***\n\n");
749 _xdg_mime_alias_list_dump (alias_list);
750 printf ("\n*** PARENTS ***\n\n");
751 _xdg_mime_parent_list_dump (parent_list);
755 /* Registers a function to be called every time the mime database reloads its files
758 xdg_mime_register_reload_callback (XdgMimeCallback callback,
759 void *data,
760 XdgMimeDestroy destroy)
762 XdgCallbackList *list_el;
763 static int callback_id = 1;
765 /* Make a new list element */
766 list_el = calloc (1, sizeof (XdgCallbackList));
767 list_el->callback_id = callback_id;
768 list_el->callback = callback;
769 list_el->data = data;
770 list_el->destroy = destroy;
771 list_el->next = callback_list;
772 if (list_el->next)
773 list_el->next->prev = list_el;
775 callback_list = list_el;
776 callback_id ++;
778 return callback_id - 1;
781 void
782 xdg_mime_remove_callback (int callback_id)
784 XdgCallbackList *list;
786 for (list = callback_list; list; list = list->next)
788 if (list->callback_id == callback_id)
790 if (list->next)
791 list->next = list->prev;
793 if (list->prev)
794 list->prev->next = list->next;
795 else
796 callback_list = list->next;
798 /* invoke the destroy handler */
799 (list->destroy) (list->data);
800 free (list);
801 return;