Index: xdgmime/Makefile.am =================================================================== RCS file: /cvs/gnome/gtk+/gtk/xdgmime/Makefile.am,v retrieving revision 1.3 diff -u -p -r1.3 Makefile.am --- xdgmime/Makefile.am 24 Oct 2003 22:15:35 -0000 1.3 +++ xdgmime/Makefile.am 13 Sep 2004 03:15:06 -0000 @@ -10,7 +10,11 @@ libxdgmime_la_SOURCES = \ xdgmimeint.c \ xdgmimeint.h \ xdgmimemagic.c \ - xdgmimemagic.h + xdgmimemagic.h \ + xdgmimealias.c \ + xdgmimealias.h \ + xdgmimeparent.c \ + xdgmimeparent.h noinst_PROGRAMS = test-mime Index: xdgmime/test-mime.c =================================================================== RCS file: /cvs/gnome/gtk+/gtk/xdgmime/test-mime.c,v retrieving revision 1.3 diff -u -p -r1.3 test-mime.c --- xdgmime/test-mime.c 6 Mar 2004 03:38:49 -0000 1.3 +++ xdgmime/test-mime.c 13 Sep 2004 03:15:06 -0000 @@ -61,6 +61,67 @@ test_glob_type (void) test_individual_glob ("tree.[ch]", XDG_GLOB_FULL); } +static void +test_alias (const char *mime_a, + const char *mime_b, + int expected) +{ + int actual; + + actual = xdg_mime_mime_type_equal (mime_a, mime_b); + + if (actual != expected) + { + printf ("Test Failed: %s is %s to %s\n", + mime_a, actual ? "equal" : "not equal", mime_b); + } +} + +static void +test_aliasing (void) +{ + test_alias ("application/wordperfect", "application/vnd.wordperfect", 1); + test_alias ("application/x-gnome-app-info", "application/x-desktop", 1); + test_alias ("application/x-wordperfect", "application/vnd.wordperfect", 1); + test_alias ("application/x-wordperfect", "audio/x-midi", 0); + test_alias ("/", "vnd/vnd", 0); + test_alias ("application/octet-stream", "text/plain", 0); + test_alias ("text/plain", "text/*", 0); +} + +static void +test_subclass (const char *mime_a, + const char *mime_b, + int expected) +{ + int actual; + + actual = xdg_mime_mime_type_subclass (mime_a, mime_b); + + if (actual != expected) + { + printf ("Test Failed: %s is %s of %s\n", + mime_a, actual ? "subclass" : "not subclass", mime_b); + } +} + +static void +test_subclassing (void) +{ + test_subclass ("application/rtf", "text/plain", 1); + test_subclass ("message/news", "text/plain", 1); + test_subclass ("message/news", "message/*", 1); + test_subclass ("message/news", "text/*", 1); + test_subclass ("message/news", "application/octet-stream", 1); + test_subclass ("application/rtf", "application/octet-stream", 1); + test_subclass ("application/x-gnome-app-info", "text/plain", 1); + test_subclass ("image/x-djvu", "image/vnd.djvu", 1); + test_subclass ("image/vnd.djvu", "image/x-djvu", 1); + test_subclass ("image/vnd.djvu", "text/plain", 0); + test_subclass ("image/vnd.djvu", "text/*", 0); + test_subclass ("text/*", "text/plain", 0); +} + int main (int argc, char *argv[]) { @@ -69,6 +130,8 @@ main (int argc, char *argv[]) int i; test_glob_type (); + test_aliasing (); + test_subclassing (); for (i = 1; i < argc; i++) { file_name = argv[i]; @@ -76,6 +139,9 @@ main (int argc, char *argv[]) printf ("File \"%s\" has a mime-type of %s\n", file_name, result); } +#if 0 + xdg_mime_dump (); +#endif return 0; } Index: xdgmime/xdgmime.c =================================================================== RCS file: /cvs/gnome/gtk+/gtk/xdgmime/xdgmime.c,v retrieving revision 1.7 diff -u -p -r1.7 xdgmime.c --- xdgmime/xdgmime.c 12 Mar 2004 18:25:01 -0000 1.7 +++ xdgmime/xdgmime.c 13 Sep 2004 03:15:06 -0000 @@ -30,6 +30,8 @@ #include "xdgmimeint.h" #include "xdgmimeglob.h" #include "xdgmimemagic.h" +#include "xdgmimealias.h" +#include "xdgmimeparent.h" #include #include #include @@ -39,6 +41,8 @@ static int initted = 0; static XdgGlobHash *global_hash = NULL; static XdgMimeMagic *global_magic = NULL; +static XdgAliasList *alias_list = NULL; +static XdgParentList *parent_list = NULL; const char *xdg_mime_type_unknown = "application/octet-stream"; static void @@ -57,6 +61,18 @@ _xdg_mime_init_from_directory (const cha strcat (file_name, "/mime/magic"); _xdg_mime_magic_read_from_file (global_magic, file_name); free (file_name); + + file_name = malloc (strlen (directory) + strlen ("/mime/aliases") + 1); + strcpy (file_name, directory); + strcat (file_name, "/mime/aliases"); + _xdg_mime_alias_read_from_file (alias_list, file_name); + free (file_name); + + file_name = malloc (strlen (directory) + strlen ("/mime/subclasses") + 1); + strcpy (file_name, directory); + strcat (file_name, "/mime/subclasses"); + _xdg_mime_parent_read_from_file (parent_list, file_name); + free (file_name); } static void @@ -70,6 +86,8 @@ xdg_mime_init (void) global_hash = _xdg_glob_hash_new (); global_magic = _xdg_mime_magic_new (); + alias_list = _xdg_mime_alias_list_new (); + parent_list = _xdg_mime_parent_list_new (); /* We look for globs and magic files based upon the XDG Base Directory * Specification @@ -248,6 +266,9 @@ xdg_mime_shutdown (void) _xdg_mime_magic_free (global_magic); global_magic = NULL; + _xdg_mime_alias_list_free (alias_list); + alias_list = NULL; + initted = 0; } } @@ -258,4 +279,114 @@ xdg_mime_get_max_buffer_extents (void) xdg_mime_init (); return _xdg_mime_magic_get_buffer_extents (global_magic); +} + +static const char * +unalias_mime_type (const char *mime_type) +{ + const char *lookup; + + if ((lookup = _xdg_mime_alias_list_lookup (alias_list, mime_type)) != NULL) + return lookup; + + return mime_type; +} + +int +xdg_mime_mime_type_equal (const char *mime_a, + const char *mime_b) +{ + const char *unalias_a, *unalias_b; + + xdg_mime_init (); + + unalias_a = unalias_mime_type (mime_a); + unalias_b = unalias_mime_type (mime_b); + + if (strcmp (unalias_a, unalias_b) == 0) + return 1; + + return 0; +} + +int +xdg_mime_media_type_equal (const char *mime_a, + const char *mime_b) +{ + char *sep; + + xdg_mime_init (); + + sep = strchr (mime_a, '/'); + + if (sep && strncmp (mime_a, mime_b, sep - mime_a + 1) == 0) + return 1; + + return 0; +} + +#if 0 +static int +xdg_mime_is_super_type (const char *mime) +{ + int length; + const char *type; + + length = strlen (mime); + type = &(mime[length - 2]); + + if (strcmp (type, "/*") == 0) + return 1; + + return 0; +} +#endif + +int +xdg_mime_mime_type_subclass (const char *mime, + const char *base) +{ + const char *umime, *ubase; + const char **parents; + + xdg_mime_init (); + + umime = unalias_mime_type (mime); + ubase = unalias_mime_type (base); + + if (strcmp (umime, ubase) == 0) + return 1; + +#if 0 + /* Handle supertypes */ + if (xdg_mime_is_super_type (ubase) && + xdg_mime_media_type_equal (umime, ubase)) + return 1; +#endif + + /* Handle special cases text/plain and application/octet-stream */ + if (strcmp (ubase, "text/plain") == 0 && + strncmp (umime, "text/", 5) == 0) + return 1; + + if (strcmp (ubase, "application/octet-stream") == 0) + return 1; + + parents = _xdg_mime_parent_list_lookup (parent_list, umime); + for (; parents && *parents; parents++) + { + if (xdg_mime_mime_type_subclass (*parents, ubase)) + return 1; + } + + return 0; +} + +void +xdg_mime_dump (void) +{ + printf ("*** ALIASES ***\n\n"); + _xdg_mime_alias_list_dump (alias_list); + printf ("\n*** PARENTS ***\n\n"); + _xdg_mime_parent_list_dump (parent_list); } Index: xdgmime/xdgmime.h =================================================================== RCS file: /cvs/gnome/gtk+/gtk/xdgmime/xdgmime.h,v retrieving revision 1.5 diff -u -p -r1.5 xdgmime.h --- xdgmime/xdgmime.h 12 Aug 2004 00:46:43 -0000 1.5 +++ xdgmime/xdgmime.h 13 Sep 2004 03:15:06 -0000 @@ -47,6 +47,8 @@ extern "C" { #define xdg_mime_get_mime_type_for_file XDG_ENTRY(get_mime_type_for_file) #define xdg_mime_get_mime_type_from_file_name XDG_ENTRY(get_mime_type_from_file_name) #define xdg_mime_is_valid_mime_type XDG_ENTRY(is_valid_mime_type) +#define xdg_mime_mime_type_equal XDG_ENTRY(mime_type_equal) +#define xdg_mime_mime_type_subclass XDG_ENTRY(mime_type_subclass) #define xdg_mime_get_max_buffer_extents XDG_ENTRY(get_max_buffer_extents) #define xdg_mime_type_unknown XDG_ENTRY(type_unknown) #define xdg_mime_shutdown XDG_ENTRY(shutdown) @@ -60,8 +62,15 @@ const char *xdg_mime_get_mime_type_for_d const char *xdg_mime_get_mime_type_for_file (const char *file_name); const char *xdg_mime_get_mime_type_from_file_name (const char *file_name); int xdg_mime_is_valid_mime_type (const char *mime_type); +int xdg_mime_mime_type_equal (const char *mime_a, + const char *mime_b); +int xdg_mime_media_type_equal (const char *mime_a, + const char *mime_b); +int xdg_mime_mime_type_subclass (const char *mime_a, + const char *mime_b); int xdg_mime_get_max_buffer_extents (void); void xdg_mime_shutdown (void); +void xdg_mime_dump (void); #ifdef __cplusplus --- /dev/null 2004-07-29 15:44:00.000000000 -0400 +++ xdgmime/xdgmimealias.h 2004-09-12 02:23:31.000000000 -0400 @@ -0,0 +1,50 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +/* xdgmimealias.h: Private file. Datastructure for storing the aliases. + * + * More info can be found at http://www.freedesktop.org/standards/ + * + * Copyright (C) 2004 Red Hat, Inc. + * Copyright (C) 200 Matthias Clasen + * + * Licensed under the Academic Free License version 2.0 + * Or under the following terms: + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __XDG_MIME_ALIAS_H__ +#define __XDG_MIME_ALIAS_H__ + +#include "xdgmime.h" + +typedef struct XdgAliasList XdgAliasList; + +#ifdef XDG_PREFIX +#define _xdg_mime_alias_read_from_file XDG_ENTRY(alias_read_from_file) +#define _xdg_mime_alias_list_new XDG_ENTRY(alias_list_new) +#define _xdg_mime_alias_list_free XDG_ENTRY(alias_list_free) +#define _xdg_mime_alias_list_lookup XDG_ENTRY(alias_list_lookup) +#endif + +void _xdg_mime_alias_read_from_file (XdgAliasList *list, + const char *file_name); +XdgAliasList *_xdg_mime_alias_list_new (void); +void _xdg_mime_alias_list_free (XdgAliasList *list); +const char *_xdg_mime_alias_list_lookup (XdgAliasList *list, + const char *alias); +void _xdg_mime_alias_list_dump (XdgAliasList *list); + +#endif /* __XDG_MIME_ALIAS_H__ */ --- /dev/null 2004-07-29 15:44:00.000000000 -0400 +++ xdgmime/xdgmimealias.c 2004-09-12 02:29:28.000000000 -0400 @@ -0,0 +1,177 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +/* xdgmimealias.c: Private file. Datastructure for storing the aliases. + * + * More info can be found at http://www.freedesktop.org/standards/ + * + * Copyright (C) 2004 Red Hat, Inc. + * Copyright (C) 2004 Matthias Clasen + * + * Licensed under the Academic Free License version 2.0 + * Or under the following terms: + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include +#include "xdgmimealias.h" +#include "xdgmimeint.h" +#include +#include +#include +#include +#include + +#ifndef FALSE +#define FALSE (0) +#endif + +#ifndef TRUE +#define TRUE (!FALSE) +#endif + +typedef struct XdgAlias XdgAlias; + +struct XdgAlias +{ + char *alias; + char *mime_type; +}; + +struct XdgAliasList +{ + struct XdgAlias *aliases; + int n_aliases; +}; + +XdgAliasList * +_xdg_mime_alias_list_new (void) +{ + XdgAliasList *list; + + list = malloc (sizeof (XdgAliasList)); + + list->aliases = NULL; + list->n_aliases = 0; + + return list; +} + +void +_xdg_mime_alias_list_free (XdgAliasList *list) +{ + int i; + + if (list->aliases) + { + for (i = 0; i < list->n_aliases; i++) + { + free (list->aliases[i].alias); + free (list->aliases[i].mime_type); + } + free (list->aliases); + } + free (list); +} + +static int +alias_entry_cmp (const void *v1, const void *v2) +{ + return strcmp (((XdgAlias *)v1)->alias, ((XdgAlias *)v2)->alias); +} + +const char * +_xdg_mime_alias_list_lookup (XdgAliasList *list, + const char *alias) +{ + XdgAlias *entry; + XdgAlias key; + + key.alias = (char *)alias; + key.mime_type = 0; + + entry = bsearch (&key, list->aliases, list->n_aliases, + sizeof (XdgAlias), alias_entry_cmp); + if (entry) + return entry->mime_type; + + return NULL; +} + +void +_xdg_mime_alias_read_from_file (XdgAliasList *list, + const char *file_name) +{ + FILE *file; + char line[255]; + int alloc; + + file = fopen (file_name, "r"); + + if (file == NULL) + return; + + /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars. + * Blah */ + alloc = 16; + list->aliases = malloc (alloc * sizeof (XdgAlias)); + while (fgets (line, 255, file) != NULL) + { + char *sep; + if (line[0] == '#') + continue; + + sep = strchr (line, ' '); + if (sep == NULL) + continue; + *(sep++) = '\000'; + sep[strlen (sep) -1] = '\000'; + if (list->n_aliases == alloc) + { + alloc <<= 1; + list->aliases = realloc (list->aliases, + alloc * sizeof (XdgAlias)); + } + list->aliases[list->n_aliases].alias = strdup (line); + list->aliases[list->n_aliases].mime_type = strdup (sep); + list->n_aliases++; + } + list->aliases = realloc (list->aliases, + list->n_aliases * sizeof (XdgAlias)); + + fclose (file); + + qsort (list->aliases, list->n_aliases, + sizeof (XdgAlias), alias_entry_cmp); +} + + +void +_xdg_mime_alias_list_dump (XdgAliasList *list) +{ + int i; + + if (list->aliases) + { + for (i = 0; i < list->n_aliases; i++) + { + printf ("%s %s\n", + list->aliases[i].alias, + list->aliases[i].mime_type); + } + } +} + + --- /dev/null 2004-07-29 15:44:00.000000000 -0400 +++ xdgmime/xdgmimeparent.h 2004-09-12 02:25:11.000000000 -0400 @@ -0,0 +1,50 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +/* xdgmimeparent.h: Private file. Datastructure for storing the hierarchy. + * + * More info can be found at http://www.freedesktop.org/standards/ + * + * Copyright (C) 2004 Red Hat, Inc. + * Copyright (C) 200 Matthias Clasen + * + * Licensed under the Academic Free License version 2.0 + * Or under the following terms: + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __XDG_MIME_PARENT_H__ +#define __XDG_MIME_PARENT_H__ + +#include "xdgmime.h" + +typedef struct XdgParentList XdgParentList; + +#ifdef XDG_PREFIX +#define _xdg_mime_parent_read_from_file XDG_ENTRY(parent_read_from_file) +#define _xdg_mime_parent_list_new XDG_ENTRY(parent_list_new) +#define _xdg_mime_parent_list_free XDG_ENTRY(parent_list_free) +#define _xdg_mime_parent_list_lookup XDG_ENTRY(parent_list_lookup) +#endif + +void _xdg_mime_parent_read_from_file (XdgParentList *list, + const char *file_name); +XdgParentList *_xdg_mime_parent_list_new (void); +void _xdg_mime_parent_list_free (XdgParentList *list); +const char **_xdg_mime_parent_list_lookup (XdgParentList *list, + const char *mime); +void _xdg_mime_parent_list_dump (XdgParentList *list); + +#endif /* __XDG_MIME_PARENT_H__ */ --- /dev/null 2004-07-29 15:44:00.000000000 -0400 +++ xdgmime/xdgmimeparent.c 2004-09-12 02:26:17.000000000 -0400 @@ -0,0 +1,211 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +/* xdgmimealias.c: Private file. Datastructure for storing the hierarchy. + * + * More info can be found at http://www.freedesktop.org/standards/ + * + * Copyright (C) 2004 Red Hat, Inc. + * Copyright (C) 2004 Matthias Clasen + * + * Licensed under the Academic Free License version 2.0 + * Or under the following terms: + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include +#include "xdgmimeparent.h" +#include "xdgmimeint.h" +#include +#include +#include +#include +#include + +#ifndef FALSE +#define FALSE (0) +#endif + +#ifndef TRUE +#define TRUE (!FALSE) +#endif + +typedef struct XdgMimeParents XdgMimeParents; + +struct XdgMimeParents +{ + char *mime; + char **parents; + int n_parents; +}; + +struct XdgParentList +{ + struct XdgMimeParents *parents; + int n_mimes; +}; + +XdgParentList * +_xdg_mime_parent_list_new (void) +{ + XdgParentList *list; + + list = malloc (sizeof (XdgParentList)); + + list->parents = NULL; + list->n_mimes = 0; + + return list; +} + +void +_xdg_mime_parent_list_free (XdgParentList *list) +{ + int i; + char **p; + + if (list->parents) + { + for (i = 0; i < list->n_mimes; i++) + { + for (p = list->parents[i].parents; *p; p++) + free (*p); + + free (list->parents[i].parents); + free (list->parents[i].mime); + } + free (list->parents); + } + free (list); +} + +static int +parent_entry_cmp (const void *v1, const void *v2) +{ + return strcmp (((XdgMimeParents *)v1)->mime, ((XdgMimeParents *)v2)->mime); +} + +const char ** +_xdg_mime_parent_list_lookup (XdgParentList *list, + const char *mime) +{ + XdgMimeParents *entry; + XdgMimeParents key; + + key.mime = (char *)mime; + key.parents = NULL; + + entry = bsearch (&key, list->parents, list->n_mimes, + sizeof (XdgMimeParents), &parent_entry_cmp); + if (entry) + return (const char **)entry->parents; + + return NULL; +} + +void +_xdg_mime_parent_read_from_file (XdgParentList *list, + const char *file_name) +{ + FILE *file; + char line[255]; + int i, alloc; + XdgMimeParents *entry; + + file = fopen (file_name, "r"); + + if (file == NULL) + return; + + /* FIXME: Not UTF-8 safe. Doesn't work if lines are greater than 255 chars. + * Blah */ + alloc = 16; + list->parents = malloc (alloc * sizeof (XdgMimeParents)); + while (fgets (line, 255, file) != NULL) + { + char *sep; + if (line[0] == '#') + continue; + + sep = strchr (line, ' '); + if (sep == NULL) + continue; + *(sep++) = '\000'; + sep[strlen (sep) -1] = '\000'; + entry = NULL; + for (i = 0; i < list->n_mimes; i++) + { + if (strcmp (list->parents[i].mime, line) == 0) + { + entry = &(list->parents[i]); + break; + } + } + + if (!entry) + { + if (list->n_mimes == alloc) + { + alloc <<= 1; + list->parents = realloc (list->parents, + alloc * sizeof (XdgMimeParents)); + } + list->parents[list->n_mimes].mime = strdup (line); + entry = &(list->parents[list->n_mimes]); + list->n_mimes++; + } + + if (!entry->parents) + { + entry->n_parents = 1; + entry->parents = malloc ((entry->n_parents + 1) * sizeof (char *)); + } + else + { + entry->n_parents += 1; + entry->parents = realloc (entry->parents, + (entry->n_parents + 2) * sizeof (char *)); + } + entry->parents[entry->n_parents - 1] = strdup (sep); + entry->parents[entry->n_parents] = NULL; + } + + list->parents = realloc (list->parents, + list->n_mimes * sizeof (XdgMimeParents)); + + fclose (file); + + qsort (list->parents, list->n_mimes, + sizeof (XdgMimeParents), &parent_entry_cmp); +} + + +void +_xdg_mime_parent_list_dump (XdgParentList *list) +{ + int i; + char **p; + + if (list->parents) + { + for (i = 0; i < list->n_mimes; i++) + { + for (p = list->parents[i].parents; *p; p++) + printf ("%s %s\n", list->parents[i].mime, *p); + } + } +} + +