minibuffer.pop_all: js efficiency change
[conkeror.git] / modules / mime.js
blob4e67c5dd03889e6690859c875e5a8ec38af89ef9
1 /**
2  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
3  * (C) Copyright 2008 Nicholas A. Zigarovich
4  * (C) Copyright 2009 John J. Foerch
5  *
6  * Use, modification, and distribution are subject to the terms specified in the
7  * COPYING file.
8 **/
10 in_module(null);
12 const mime_service = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
14 /**
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
18  * unchanged.
19  */
20 function mime_type_parse (mime_type) {
21     var slash_idx = mime_type.indexOf("/");
22     if (slash_idx != -1)
23         return [mime_type.substring(0, slash_idx),
24                 mime_type.substring(slash_idx + 1)];
25     return mime_type;
28 function mime_type_table () {}
29 mime_type_table.prototype = {
30     table: {},
31     get: function (mime_type) {
32         var p = mime_type_parse(mime_type);
33         if (this.table[p[0]])
34             return this.table[p[0]][p[1]] ||
35                 this.table[p[0]]["*"] ||
36                 this.table["*"];
37         else
38             return this.table["*"];
39     },
40     set: function (mime_type, value) {
41         var p = mime_type_parse(mime_type);
42         if (p == "*")
43             return (this.table["*"] = value);
44         if (typeof p == "string") {
45             if (! this.table[p])
46                 this.table[p] = {};
47             return (this.table[p]["*"] = value);
48         }
49         if (! this.table[p[0]])
50             this.table[p[0]] = {};
51         return (this.table[p[0]][p[1]] = value);
52     }
56 /**
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.
61  */
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,
66         function () handlers,
67         function (table) handlers.table = table,
68         doc);
72 define_mime_type_table("external_content_handlers",
73     {
74         "*": getenv("EDITOR"),
75         text: { "*": getenv("EDITOR") },
76         image: { "*": "feh" },
77         video: { "*": "mplayer" },
78         audio: { "*": "mplayer" },
79         application: {
80             pdf: "evince",
81             postscript: "evince",
82             "x-dvi": "evince"
83         }
84     },
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.");
90 /**
91  *
92  */
93 function mime_type_from_uri (uri) {
94     var type = "application/octet-stream";
95     try {
96         uri = make_uri(uri);
97         type = mime_service.getTypeFromURI(uri);
98     } catch (e) {}
99     return type;
104  */
105 function mime_info_from_mime_type (type) {
106     if (type == null)
107         type = "application/octet-stream";
108     try {
109         return mime_service.getFromTypeAndExtension(type, null);
110     } catch (e) {
111         return null;
112     }
115 provide("mime");