2 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
3 * (C) Copyright 2008 Nicholas A. Zigarovich
4 * (C) Copyright 2009 John J. Foerch
6 * Use, modification, and distribution are subject to the terms specified in the
10 const mime_service = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
13 * mime_type_parse splits a mime type or mime type pattern on
14 * a / character, and returns a two element array of the halves.
15 * If mime_type does not contain a /, then the argument is returned
18 function mime_type_parse (mime_type) {
19 var slash_idx = mime_type.indexOf("/");
21 return [mime_type.substring(0, slash_idx),
22 mime_type.substring(slash_idx + 1)];
26 function mime_type_table () {}
27 mime_type_table.prototype = {
29 get: function (mime_type) {
30 var p = mime_type_parse(mime_type);
32 return this.table[p[0]][p[1]] ||
33 this.table[p[0]]["*"] ||
36 return this.table["*"];
38 set: function (mime_type, value) {
39 var p = mime_type_parse(mime_type);
41 return (this.table["*"] = value);
42 if (typeof p == "string") {
45 return (this.table[p]["*"] = value);
47 if (! this.table[p[0]])
48 this.table[p[0]] = {};
49 return (this.table[p[0]][p[1]] = value);
55 * define_mime_type_table makes a user variable of the given name that
56 * encapsulates the table given as the default value, having the methods
57 * `get' and `set' for maintaining an association of mime-type patterns
58 * with arbitrary values.
60 function define_mime_type_table (name, default_value, doc) {
61 var handlers = { table: default_value };
62 handlers.__proto__ = mime_type_table.prototype;
63 define_special_variable(name,
65 function (table) handlers.table = table,
70 define_mime_type_table("external_content_handlers",
72 "*": getenv("EDITOR"),
73 text: { "*": getenv("EDITOR") },
74 image: { "*": "feh" },
75 video: { "*": "mplayer" },
76 audio: { "*": "mplayer" },
83 "Structure associating MIME types and MIME type patterns with "+
84 "the names of programs for handling those them. The key \"*\" "+
85 "is a pattern-matching symbol which matches anything.");
91 function mime_type_from_uri (uri) {
92 var type = "application/octet-stream";
95 type = mime_service.getTypeFromURI(uri);
103 function mime_info_from_mime_type (type) {
105 type = "application/octet-stream";
107 return mime_service.getFromTypeAndExtension(type, null);