whitespace
[conkeror.git] / modules / mime.js
blobbd460e387ae1d756537f201c73e2c17cd8dc326f
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     constructor: mime_type_table,
31     table: {},
32     get: function (mime_type) {
33         var p = mime_type_parse(mime_type);
34         if (this.table[p[0]])
35             return this.table[p[0]][p[1]] ||
36                 this.table[p[0]]["*"] ||
37                 this.table["*"] || null;
38         else
39             return this.table["*"] || null;
40     },
41     set: function (mime_type, value) {
42         var p = mime_type_parse(mime_type);
43         if (p == "*")
44             return (this.table["*"] = value);
45         if (typeof p == "string") {
46             if (! this.table[p])
47                 this.table[p] = {};
48             return (this.table[p]["*"] = value);
49         }
50         if (! this.table[p[0]])
51             this.table[p[0]] = {};
52         return (this.table[p[0]][p[1]] = value);
53     }
57 /**
58  * define_mime_type_table makes a user variable of the given name that
59  * encapsulates the table given as the default value, having the methods
60  * `get' and `set' for maintaining an association of mime-type patterns
61  * with arbitrary values.
62  */
63 function define_mime_type_table (name, default_value, doc) {
64     var handlers = { table: default_value };
65     handlers.__proto__ = mime_type_table.prototype;
66     define_special_variable(name,
67         function () handlers,
68         function (table) handlers.table = table,
69         doc);
73 define_mime_type_table("external_content_handlers",
74     {
75         "*": getenv("EDITOR"),
76         text: { "*": getenv("EDITOR") },
77         image: { "*": "feh" },
78         video: { "*": "mplayer" },
79         audio: { "*": "mplayer" },
80         application: {
81             pdf: "evince",
82             postscript: "evince",
83             "x-dvi": "evince"
84         }
85     },
86     "Structure associating MIME types and MIME type patterns with "+
87     "the names of programs for handling those them.  The key \"*\" "+
88     "is a pattern-matching symbol which matches anything.");
91 /**
92  *
93  */
94 function mime_type_from_uri (uri) {
95     var type = "application/octet-stream";
96     try {
97         uri = make_uri(uri);
98         type = mime_service.getTypeFromURI(uri);
99     } catch (e) {}
100     return type;
105  */
106 function mime_info_from_mime_type (type) {
107     if (type == null)
108         type = "application/octet-stream";
109     try {
110         return mime_service.getFromTypeAndExtension(type, null);
111     } catch (e) {
112         return null;
113     }
116 provide("mime");