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 = {
28 constructor: mime_type_table,
30 get: function (mime_type) {
31 var p = mime_type_parse(mime_type);
33 return this.table[p[0]][p[1]] ||
34 this.table[p[0]]["*"] ||
35 this.table["*"] || null;
37 return this.table["*"] || null;
39 set: function (mime_type, value) {
40 var p = mime_type_parse(mime_type);
42 return (this.table["*"] = value);
43 if (typeof p == "string") {
46 return (this.table[p]["*"] = value);
48 if (! this.table[p[0]])
49 this.table[p[0]] = {};
50 return (this.table[p[0]][p[1]] = value);
56 * define_mime_type_table makes a user variable of the given name that
57 * encapsulates the table given as the default value, having the methods
58 * `get' and `set' for maintaining an association of mime-type patterns
59 * with arbitrary values.
61 function define_mime_type_table (name, default_value, doc) {
62 var handlers = { table: default_value };
63 handlers.__proto__ = mime_type_table.prototype;
64 define_special_variable(name,
66 function (table) handlers.table = table,
71 define_mime_type_table("external_content_handlers",
73 "*": getenv("EDITOR"),
74 text: { "*": getenv("EDITOR") },
75 image: { "*": "feh" },
76 video: { "*": "mplayer" },
77 audio: { "*": "mplayer" },
84 "Structure associating MIME types and MIME type patterns with "+
85 "the names of programs for handling those them. The key \"*\" "+
86 "is a pattern-matching symbol which matches anything.");
92 function mime_type_from_uri (uri) {
93 var type = "application/octet-stream";
96 type = mime_service.getTypeFromURI(uri);
104 function mime_info_from_mime_type (type) {
106 type = "application/octet-stream";
108 return mime_service.getFromTypeAndExtension(type, null);