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
12 const mime_service = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
15 * mime_type_parse splits a mime type or mime type pattern on
16 * a / character, and returns a two element array of the halves.
17 * If mime_type does not contain a /, then the argument is returned
20 function mime_type_parse (mime_type) {
21 var slash_idx = mime_type.indexOf("/");
23 return [mime_type.substring(0, slash_idx),
24 mime_type.substring(slash_idx + 1)];
28 function mime_type_table () {}
29 mime_type_table.prototype = {
31 get: function (mime_type) {
32 var p = mime_type_parse(mime_type);
34 return this.table[p[0]][p[1]] ||
35 this.table[p[0]]["*"] ||
38 return this.table["*"];
40 set: function (mime_type, value) {
41 var p = mime_type_parse(mime_type);
43 return (this.table["*"] = value);
44 if (typeof p == "string") {
47 return (this.table[p]["*"] = value);
49 if (! this.table[p[0]])
50 this.table[p[0]] = {};
51 return (this.table[p[0]][p[1]] = value);
57 * define_mime_type_table makes a user variable of the given name that
58 * encapsulates the table given as the default value, having the methods
59 * `get' and `set' for maintaining an association of mime-type patterns
60 * with arbitrary values.
62 function define_mime_type_table (name, default_value, doc) {
63 var handlers = { table: default_value };
64 handlers.__proto__ = mime_type_table.prototype;
65 define_special_variable(name,
67 function (table) handlers.table = table,
72 define_mime_type_table("external_content_handlers",
74 "*": getenv("EDITOR"),
75 text: { "*": getenv("EDITOR") },
76 image: { "*": "feh" },
77 video: { "*": "mplayer" },
78 audio: { "*": "mplayer" },
85 "Structure associating MIME types and MIME type patterns with "+
86 "the names of programs for handling those them. The key \"*\" "+
87 "is a pattern-matching symbol which matches anything.");
93 function mime_type_from_uri (uri) {
94 var type = "application/octet-stream";
97 type = mime_service.getTypeFromURI(uri);
105 function mime_info_from_mime_type (type) {
107 type = "application/octet-stream";
109 return mime_service.getFromTypeAndExtension(type, null);