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