]> git.proxmox.com Git - libxdgmime-perl.git/blob - xdgmime-source/src/xdgmime.c
90a0ed0d56707bb1ae694568a17df3aac3aec008
[libxdgmime-perl.git] / xdgmime-source / src / xdgmime.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* xdgmime.c: XDG Mime Spec mime resolver. Based on version 0.11 of the spec.
3 *
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>
8 *
9 * Licensed under the Academic Free License version 2.0
10 * Or under the following terms:
11 *
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.
16 *
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.
21 *
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.
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include "xdgmime.h"
33 #include "xdgmimeint.h"
34 #include "xdgmimeglob.h"
35 #include "xdgmimemagic.h"
36 #include "xdgmimealias.h"
37 #include "xdgmimeicon.h"
38 #include "xdgmimeparent.h"
39 #include "xdgmimecache.h"
40 #include <stdio.h>
41 #include <string.h>
42 #include <sys/stat.h>
43 #include <sys/types.h>
44 #include <sys/time.h>
45 #include <unistd.h>
46 #include <assert.h>
47
48 typedef struct XdgDirTimeList XdgDirTimeList;
49 typedef struct XdgCallbackList XdgCallbackList;
50
51 static int need_reread = TRUE;
52 static time_t last_stat_time = 0;
53
54 static XdgGlobHash *global_hash = NULL;
55 static XdgMimeMagic *global_magic = NULL;
56 static XdgAliasList *alias_list = NULL;
57 static XdgParentList *parent_list = NULL;
58 static XdgDirTimeList *dir_time_list = NULL;
59 static XdgCallbackList *callback_list = NULL;
60 static XdgIconList *icon_list = NULL;
61 static XdgIconList *generic_icon_list = NULL;
62
63 XdgMimeCache **_caches = NULL;
64 static int n_caches = 0;
65
66 const char xdg_mime_type_unknown[] = "application/octet-stream";
67 const char xdg_mime_type_empty[] = "application/x-zerosize";
68 const char xdg_mime_type_textplain[] = "text/plain";
69
70
71 enum
72 {
73 XDG_CHECKED_UNCHECKED,
74 XDG_CHECKED_VALID,
75 XDG_CHECKED_INVALID
76 };
77
78 struct XdgDirTimeList
79 {
80 time_t mtime;
81 char *directory_name;
82 int checked;
83 XdgDirTimeList *next;
84 };
85
86 struct XdgCallbackList
87 {
88 XdgCallbackList *next;
89 XdgCallbackList *prev;
90 int callback_id;
91 XdgMimeCallback callback;
92 void *data;
93 XdgMimeDestroy destroy;
94 };
95
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);
100
101 static void
102 xdg_dir_time_list_add (char *file_name,
103 time_t mtime)
104 {
105 XdgDirTimeList *list;
106
107 for (list = dir_time_list; list; list = list->next)
108 {
109 if (strcmp (list->directory_name, file_name) == 0)
110 {
111 free (file_name);
112 return;
113 }
114 }
115
116 list = calloc (1, sizeof (XdgDirTimeList));
117 list->checked = XDG_CHECKED_UNCHECKED;
118 list->directory_name = file_name;
119 list->mtime = mtime;
120 list->next = dir_time_list;
121 dir_time_list = list;
122 }
123
124 static void
125 xdg_dir_time_list_free (XdgDirTimeList *list)
126 {
127 XdgDirTimeList *next;
128
129 while (list)
130 {
131 next = list->next;
132 free (list->directory_name);
133 free (list);
134 list = next;
135 }
136 }
137
138 static int
139 xdg_mime_init_from_directory (const char *directory,
140 void *user_data)
141 {
142 char *file_name;
143 struct stat st;
144
145 assert (directory != NULL);
146
147 file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
148 strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
149 if (stat (file_name, &st) == 0)
150 {
151 XdgMimeCache *cache = _xdg_mime_cache_new_from_file (file_name);
152
153 if (cache != NULL)
154 {
155 xdg_dir_time_list_add (file_name, st.st_mtime);
156
157 _caches = realloc (_caches, sizeof (XdgMimeCache *) * (n_caches + 2));
158 _caches[n_caches] = cache;
159 _caches[n_caches + 1] = NULL;
160 n_caches++;
161
162 return FALSE;
163 }
164 }
165 free (file_name);
166
167 file_name = malloc (strlen (directory) + strlen ("/mime/globs2") + 1);
168 strcpy (file_name, directory); strcat (file_name, "/mime/globs2");
169 if (stat (file_name, &st) == 0)
170 {
171 _xdg_mime_glob_read_from_file (global_hash, file_name, TRUE);
172 xdg_dir_time_list_add (file_name, st.st_mtime);
173 }
174 else
175 {
176 free (file_name);
177 file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
178 strcpy (file_name, directory); strcat (file_name, "/mime/globs");
179 if (stat (file_name, &st) == 0)
180 {
181 _xdg_mime_glob_read_from_file (global_hash, file_name, FALSE);
182 xdg_dir_time_list_add (file_name, st.st_mtime);
183 }
184 else
185 {
186 free (file_name);
187 }
188 }
189
190 file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
191 strcpy (file_name, directory); strcat (file_name, "/mime/magic");
192 if (stat (file_name, &st) == 0)
193 {
194 _xdg_mime_magic_read_from_file (global_magic, file_name);
195 xdg_dir_time_list_add (file_name, st.st_mtime);
196 }
197 else
198 {
199 free (file_name);
200 }
201
202 file_name = malloc (strlen (directory) + strlen ("/mime/aliases") + 1);
203 strcpy (file_name, directory); strcat (file_name, "/mime/aliases");
204 _xdg_mime_alias_read_from_file (alias_list, file_name);
205 free (file_name);
206
207 file_name = malloc (strlen (directory) + strlen ("/mime/subclasses") + 1);
208 strcpy (file_name, directory); strcat (file_name, "/mime/subclasses");
209 _xdg_mime_parent_read_from_file (parent_list, file_name);
210 free (file_name);
211
212 file_name = malloc (strlen (directory) + strlen ("/mime/icons") + 1);
213 strcpy (file_name, directory); strcat (file_name, "/mime/icons");
214 _xdg_mime_icon_read_from_file (icon_list, file_name);
215 free (file_name);
216
217 file_name = malloc (strlen (directory) + strlen ("/mime/generic-icons") + 1);
218 strcpy (file_name, directory); strcat (file_name, "/mime/generic-icons");
219 _xdg_mime_icon_read_from_file (generic_icon_list, file_name);
220 free (file_name);
221
222 return FALSE; /* Keep processing */
223 }
224
225 /* Runs a command on all the directories in the search path */
226 static void
227 xdg_run_command_on_dirs (XdgDirectoryFunc func,
228 void *user_data)
229 {
230 const char *xdg_data_home;
231 const char *xdg_data_dirs;
232 const char *ptr;
233
234 xdg_data_home = getenv ("XDG_DATA_HOME");
235 if (xdg_data_home)
236 {
237 if ((func) (xdg_data_home, user_data))
238 return;
239 }
240 else
241 {
242 const char *home;
243
244 home = getenv ("HOME");
245 if (home != NULL)
246 {
247 char *guessed_xdg_home;
248 int stop_processing;
249
250 guessed_xdg_home = malloc (strlen (home) + strlen ("/.local/share/") + 1);
251 strcpy (guessed_xdg_home, home);
252 strcat (guessed_xdg_home, "/.local/share/");
253 stop_processing = (func) (guessed_xdg_home, user_data);
254 free (guessed_xdg_home);
255
256 if (stop_processing)
257 return;
258 }
259 }
260
261 xdg_data_dirs = getenv ("XDG_DATA_DIRS");
262 if (xdg_data_dirs == NULL)
263 xdg_data_dirs = "/usr/local/share/:/usr/share/";
264
265 ptr = xdg_data_dirs;
266
267 while (*ptr != '\000')
268 {
269 const char *end_ptr;
270 char *dir;
271 int len;
272 int stop_processing;
273
274 end_ptr = ptr;
275 while (*end_ptr != ':' && *end_ptr != '\000')
276 end_ptr ++;
277
278 if (end_ptr == ptr)
279 {
280 ptr++;
281 continue;
282 }
283
284 if (*end_ptr == ':')
285 len = end_ptr - ptr;
286 else
287 len = end_ptr - ptr + 1;
288 dir = malloc (len + 1);
289 strncpy (dir, ptr, len);
290 dir[len] = '\0';
291 stop_processing = (func) (dir, user_data);
292 free (dir);
293
294 if (stop_processing)
295 return;
296
297 ptr = end_ptr;
298 }
299 }
300
301 /* Checks file_path to make sure it has the same mtime as last time it was
302 * checked. If it has a different mtime, or if the file doesn't exist, it
303 * returns FALSE.
304 *
305 * FIXME: This doesn't protect against permission changes.
306 */
307 static int
308 xdg_check_file (const char *file_path,
309 int *exists)
310 {
311 struct stat st;
312
313 /* If the file exists */
314 if (stat (file_path, &st) == 0)
315 {
316 XdgDirTimeList *list;
317
318 if (exists)
319 *exists = TRUE;
320
321 for (list = dir_time_list; list; list = list->next)
322 {
323 if (! strcmp (list->directory_name, file_path))
324 {
325 if (st.st_mtime == list->mtime)
326 list->checked = XDG_CHECKED_VALID;
327 else
328 list->checked = XDG_CHECKED_INVALID;
329
330 return (list->checked != XDG_CHECKED_VALID);
331 }
332 }
333 return TRUE;
334 }
335
336 if (exists)
337 *exists = FALSE;
338
339 return FALSE;
340 }
341
342 static int
343 xdg_check_dir (const char *directory,
344 void *user_data)
345 {
346 int invalid, exists;
347 char *file_name;
348 int* invalid_dir_list = user_data;
349
350 assert (directory != NULL);
351
352 /* Check the mime.cache file */
353 file_name = malloc (strlen (directory) + strlen ("/mime/mime.cache") + 1);
354 strcpy (file_name, directory); strcat (file_name, "/mime/mime.cache");
355 invalid = xdg_check_file (file_name, &exists);
356 free (file_name);
357 if (invalid)
358 {
359 *invalid_dir_list = TRUE;
360 return TRUE;
361 }
362 else if (exists)
363 {
364 return FALSE;
365 }
366
367 /* Check the globs file */
368 file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
369 strcpy (file_name, directory); strcat (file_name, "/mime/globs");
370 invalid = xdg_check_file (file_name, NULL);
371 free (file_name);
372 if (invalid)
373 {
374 *invalid_dir_list = TRUE;
375 return TRUE;
376 }
377
378 /* Check the magic file */
379 file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
380 strcpy (file_name, directory); strcat (file_name, "/mime/magic");
381 invalid = xdg_check_file (file_name, NULL);
382 free (file_name);
383 if (invalid)
384 {
385 *invalid_dir_list = TRUE;
386 return TRUE;
387 }
388
389 return FALSE; /* Keep processing */
390 }
391
392 /* Walks through all the mime files stat()ing them to see if they've changed.
393 * Returns TRUE if they have. */
394 static int
395 xdg_check_dirs (void)
396 {
397 XdgDirTimeList *list;
398 int invalid_dir_list = FALSE;
399
400 for (list = dir_time_list; list; list = list->next)
401 list->checked = XDG_CHECKED_UNCHECKED;
402
403 xdg_run_command_on_dirs (xdg_check_dir, &invalid_dir_list);
404
405 if (invalid_dir_list)
406 return TRUE;
407
408 for (list = dir_time_list; list; list = list->next)
409 {
410 if (list->checked != XDG_CHECKED_VALID)
411 return TRUE;
412 }
413
414 return FALSE;
415 }
416
417 /* We want to avoid stat()ing on every single mime call, so we only look for
418 * newer files every 5 seconds. This will return TRUE if we need to reread the
419 * mime data from disk.
420 */
421 static int
422 xdg_check_time_and_dirs (void)
423 {
424 struct timeval tv;
425 time_t current_time;
426 int retval = FALSE;
427
428 gettimeofday (&tv, NULL);
429 current_time = tv.tv_sec;
430
431 if (current_time >= last_stat_time + 5)
432 {
433 retval = xdg_check_dirs ();
434 last_stat_time = current_time;
435 }
436
437 return retval;
438 }
439
440 /* Called in every public function. It reloads the hash function if need be.
441 */
442 static void
443 xdg_mime_init (void)
444 {
445 if (xdg_check_time_and_dirs ())
446 {
447 xdg_mime_shutdown ();
448 }
449
450 if (need_reread)
451 {
452 global_hash = _xdg_glob_hash_new ();
453 global_magic = _xdg_mime_magic_new ();
454 alias_list = _xdg_mime_alias_list_new ();
455 parent_list = _xdg_mime_parent_list_new ();
456 icon_list = _xdg_mime_icon_list_new ();
457 generic_icon_list = _xdg_mime_icon_list_new ();
458
459 xdg_run_command_on_dirs (xdg_mime_init_from_directory, NULL);
460
461 need_reread = FALSE;
462 }
463 }
464
465 const char *
466 xdg_mime_get_mime_type_for_data (const void *data,
467 size_t len,
468 int *result_prio)
469 {
470 const char *mime_type;
471
472 if (len == 0)
473 {
474 if (result_prio != NULL)
475 *result_prio = 100;
476 return XDG_MIME_TYPE_EMPTY;
477 }
478
479 xdg_mime_init ();
480
481 if (_caches)
482 mime_type = _xdg_mime_cache_get_mime_type_for_data (data, len, result_prio);
483 else
484 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len, result_prio, NULL, 0);
485
486 if (mime_type)
487 return mime_type;
488
489 return _xdg_binary_or_text_fallback(data, len);
490 }
491
492 const char *
493 xdg_mime_get_mime_type_for_file (const char *file_name,
494 struct stat *statbuf)
495 {
496 const char *mime_type;
497 /* currently, only a few globs occur twice, and none
498 * more often, so 5 seems plenty.
499 */
500 const char *mime_types[5];
501 FILE *file;
502 unsigned char *data;
503 int max_extent;
504 int bytes_read;
505 struct stat buf;
506 const char *base_name;
507 int n;
508
509 if (file_name == NULL)
510 return NULL;
511 if (! _xdg_utf8_validate (file_name))
512 return NULL;
513
514 xdg_mime_init ();
515
516 if (_caches)
517 return _xdg_mime_cache_get_mime_type_for_file (file_name, statbuf);
518
519 base_name = _xdg_get_base_name (file_name);
520 n = _xdg_glob_hash_lookup_file_name (global_hash, base_name, mime_types, 5);
521
522 if (n == 1)
523 return mime_types[0];
524
525 if (!statbuf)
526 {
527 if (stat (file_name, &buf) != 0)
528 return XDG_MIME_TYPE_UNKNOWN;
529
530 statbuf = &buf;
531 }
532
533 if (!S_ISREG (statbuf->st_mode))
534 return XDG_MIME_TYPE_UNKNOWN;
535
536 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
537 * be large and need getting from a stream instead of just reading it all
538 * in. */
539 max_extent = _xdg_mime_magic_get_buffer_extents (global_magic);
540 data = malloc (max_extent);
541 if (data == NULL)
542 return XDG_MIME_TYPE_UNKNOWN;
543
544 file = fopen (file_name, "r");
545 if (file == NULL)
546 {
547 free (data);
548 return XDG_MIME_TYPE_UNKNOWN;
549 }
550
551 bytes_read = fread (data, 1, max_extent, file);
552 if (ferror (file))
553 {
554 free (data);
555 fclose (file);
556 return XDG_MIME_TYPE_UNKNOWN;
557 }
558
559 mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read, NULL,
560 mime_types, n);
561
562 if (!mime_type)
563 mime_type = _xdg_binary_or_text_fallback (data, bytes_read);
564
565 free (data);
566 fclose (file);
567
568 return mime_type;
569 }
570
571 const char *
572 xdg_mime_get_mime_type_from_file_name (const char *file_name)
573 {
574 const char *mime_type;
575
576 xdg_mime_init ();
577
578 if (_caches)
579 return _xdg_mime_cache_get_mime_type_from_file_name (file_name);
580
581 if (_xdg_glob_hash_lookup_file_name (global_hash, file_name, &mime_type, 1))
582 return mime_type;
583 else
584 return XDG_MIME_TYPE_UNKNOWN;
585 }
586
587 int
588 xdg_mime_get_mime_types_from_file_name (const char *file_name,
589 const char *mime_types[],
590 int n_mime_types)
591 {
592 xdg_mime_init ();
593
594 if (_caches)
595 return _xdg_mime_cache_get_mime_types_from_file_name (file_name, mime_types, n_mime_types);
596
597 return _xdg_glob_hash_lookup_file_name (global_hash, file_name, mime_types, n_mime_types);
598 }
599
600 int
601 xdg_mime_is_valid_mime_type (const char *mime_type)
602 {
603 /* FIXME: We should make this a better test
604 */
605 return _xdg_utf8_validate (mime_type);
606 }
607
608 void
609 xdg_mime_shutdown (void)
610 {
611 XdgCallbackList *list;
612
613 /* FIXME: Need to make this (and the whole library) thread safe */
614 if (dir_time_list)
615 {
616 xdg_dir_time_list_free (dir_time_list);
617 dir_time_list = NULL;
618 }
619
620 if (global_hash)
621 {
622 _xdg_glob_hash_free (global_hash);
623 global_hash = NULL;
624 }
625 if (global_magic)
626 {
627 _xdg_mime_magic_free (global_magic);
628 global_magic = NULL;
629 }
630
631 if (alias_list)
632 {
633 _xdg_mime_alias_list_free (alias_list);
634 alias_list = NULL;
635 }
636
637 if (parent_list)
638 {
639 _xdg_mime_parent_list_free (parent_list);
640 parent_list = NULL;
641 }
642
643 if (icon_list)
644 {
645 _xdg_mime_icon_list_free (icon_list);
646 icon_list = NULL;
647 }
648
649 if (generic_icon_list)
650 {
651 _xdg_mime_icon_list_free (generic_icon_list);
652 generic_icon_list = NULL;
653 }
654
655 if (_caches)
656 {
657 int i;
658
659 for (i = 0; i < n_caches; i++)
660 _xdg_mime_cache_unref (_caches[i]);
661 free (_caches);
662 _caches = NULL;
663 n_caches = 0;
664 }
665
666 for (list = callback_list; list; list = list->next)
667 (list->callback) (list->data);
668
669 need_reread = TRUE;
670 }
671
672 int
673 xdg_mime_get_max_buffer_extents (void)
674 {
675 xdg_mime_init ();
676
677 if (_caches)
678 return _xdg_mime_cache_get_max_buffer_extents ();
679
680 return _xdg_mime_magic_get_buffer_extents (global_magic);
681 }
682
683 const char *
684 _xdg_mime_unalias_mime_type (const char *mime_type)
685 {
686 const char *lookup;
687
688 if (_caches)
689 return _xdg_mime_cache_unalias_mime_type (mime_type);
690
691 if ((lookup = _xdg_mime_alias_list_lookup (alias_list, mime_type)) != NULL)
692 return lookup;
693
694 return mime_type;
695 }
696
697 const char *
698 xdg_mime_unalias_mime_type (const char *mime_type)
699 {
700 xdg_mime_init ();
701
702 return _xdg_mime_unalias_mime_type (mime_type);
703 }
704
705 int
706 _xdg_mime_mime_type_equal (const char *mime_a,
707 const char *mime_b)
708 {
709 const char *unalias_a, *unalias_b;
710
711 unalias_a = _xdg_mime_unalias_mime_type (mime_a);
712 unalias_b = _xdg_mime_unalias_mime_type (mime_b);
713
714 if (strcmp (unalias_a, unalias_b) == 0)
715 return 1;
716
717 return 0;
718 }
719
720 int
721 xdg_mime_mime_type_equal (const char *mime_a,
722 const char *mime_b)
723 {
724 xdg_mime_init ();
725
726 return _xdg_mime_mime_type_equal (mime_a, mime_b);
727 }
728
729 int
730 xdg_mime_media_type_equal (const char *mime_a,
731 const char *mime_b)
732 {
733 char *sep;
734
735 sep = strchr (mime_a, '/');
736
737 if (sep && strncmp (mime_a, mime_b, sep - mime_a + 1) == 0)
738 return 1;
739
740 return 0;
741 }
742
743 #if 1
744 static int
745 ends_with (const char *str,
746 const char *suffix)
747 {
748 int length;
749 int suffix_length;
750
751 length = strlen (str);
752 suffix_length = strlen (suffix);
753 if (length < suffix_length)
754 return 0;
755
756 if (strcmp (str + length - suffix_length, suffix) == 0)
757 return 1;
758
759 return 0;
760 }
761
762 static int
763 xdg_mime_is_super_type (const char *mime)
764 {
765 return ends_with (mime, "/*");
766 }
767 #endif
768
769 int
770 _xdg_mime_mime_type_subclass (const char *mime,
771 const char *base)
772 {
773 const char *umime, *ubase;
774 const char **parents;
775
776 if (_caches)
777 return _xdg_mime_cache_mime_type_subclass (mime, base);
778
779 umime = _xdg_mime_unalias_mime_type (mime);
780 ubase = _xdg_mime_unalias_mime_type (base);
781
782 if (strcmp (umime, ubase) == 0)
783 return 1;
784
785 #if 1
786 /* Handle supertypes */
787 if (xdg_mime_is_super_type (ubase) &&
788 xdg_mime_media_type_equal (umime, ubase))
789 return 1;
790 #endif
791
792 /* Handle special cases text/plain and application/octet-stream */
793 if (strcmp (ubase, "text/plain") == 0 &&
794 strncmp (umime, "text/", 5) == 0)
795 return 1;
796
797 if (strcmp (ubase, "application/octet-stream") == 0 &&
798 strncmp (umime, "inode/", 6) != 0)
799 return 1;
800
801 parents = _xdg_mime_parent_list_lookup (parent_list, umime);
802 for (; parents && *parents; parents++)
803 {
804 if (_xdg_mime_mime_type_subclass (*parents, ubase))
805 return 1;
806 }
807
808 return 0;
809 }
810
811 int
812 xdg_mime_mime_type_subclass (const char *mime,
813 const char *base)
814 {
815 xdg_mime_init ();
816
817 return _xdg_mime_mime_type_subclass (mime, base);
818 }
819
820 char **
821 xdg_mime_list_mime_parents (const char *mime)
822 {
823 const char **parents;
824 char **result;
825 int i, n;
826
827 if (_caches)
828 return _xdg_mime_cache_list_mime_parents (mime);
829
830 parents = xdg_mime_get_mime_parents (mime);
831
832 if (!parents)
833 return NULL;
834
835 for (i = 0; parents[i]; i++) ;
836
837 n = (i + 1) * sizeof (char *);
838 result = (char **) malloc (n);
839 memcpy (result, parents, n);
840
841 return result;
842 }
843
844 const char **
845 xdg_mime_get_mime_parents (const char *mime)
846 {
847 const char *umime;
848
849 xdg_mime_init ();
850
851 umime = _xdg_mime_unalias_mime_type (mime);
852
853 return _xdg_mime_parent_list_lookup (parent_list, umime);
854 }
855
856 void
857 xdg_mime_dump (void)
858 {
859 xdg_mime_init();
860
861 printf ("*** ALIASES ***\n\n");
862 _xdg_mime_alias_list_dump (alias_list);
863 printf ("\n*** PARENTS ***\n\n");
864 _xdg_mime_parent_list_dump (parent_list);
865 printf ("\n*** CACHE ***\n\n");
866 _xdg_glob_hash_dump (global_hash);
867 printf ("\n*** GLOBS ***\n\n");
868 _xdg_glob_hash_dump (global_hash);
869 printf ("\n*** GLOBS REVERSE TREE ***\n\n");
870 _xdg_mime_cache_glob_dump ();
871 }
872
873
874 /* Registers a function to be called every time the mime database reloads its files
875 */
876 int
877 xdg_mime_register_reload_callback (XdgMimeCallback callback,
878 void *data,
879 XdgMimeDestroy destroy)
880 {
881 XdgCallbackList *list_el;
882 static int callback_id = 1;
883
884 /* Make a new list element */
885 list_el = calloc (1, sizeof (XdgCallbackList));
886 list_el->callback_id = callback_id;
887 list_el->callback = callback;
888 list_el->data = data;
889 list_el->destroy = destroy;
890 list_el->next = callback_list;
891 if (list_el->next)
892 list_el->next->prev = list_el;
893
894 callback_list = list_el;
895 callback_id ++;
896
897 return callback_id - 1;
898 }
899
900 void
901 xdg_mime_remove_callback (int callback_id)
902 {
903 XdgCallbackList *list;
904
905 for (list = callback_list; list; list = list->next)
906 {
907 if (list->callback_id == callback_id)
908 {
909 if (list->next)
910 list->next = list->prev;
911
912 if (list->prev)
913 list->prev->next = list->next;
914 else
915 callback_list = list->next;
916
917 /* invoke the destroy handler */
918 (list->destroy) (list->data);
919 free (list);
920 return;
921 }
922 }
923 }
924
925 const char *
926 xdg_mime_get_icon (const char *mime)
927 {
928 xdg_mime_init ();
929
930 if (_caches)
931 return _xdg_mime_cache_get_icon (mime);
932
933 return _xdg_mime_icon_list_lookup (icon_list, mime);
934 }
935
936 const char *
937 xdg_mime_get_generic_icon (const char *mime)
938 {
939 xdg_mime_init ();
940
941 if (_caches)
942 return _xdg_mime_cache_get_generic_icon (mime);
943
944 return _xdg_mime_icon_list_lookup (generic_icon_list, mime);
945 }