Prepare new Debian package
[conkeror.git] / modules / mime.js
blob89db7b8ab347b201cc2f880d2aff53b5f4dc1e44
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 const mime_service = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
12 /**
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
16  * unchanged.
17  */
18 function mime_type_parse (mime_type) {
19     var slash_idx = mime_type.indexOf("/");
20     if (slash_idx != -1)
21         return [mime_type.substring(0, slash_idx),
22                 mime_type.substring(slash_idx + 1)];
23     return mime_type;
26 function mime_type_table () {}
27 mime_type_table.prototype = {
28     table: {},
29     get: function (mime_type) {
30         var p = mime_type_parse(mime_type);
31         if (this.table[p[0]])
32             return this.table[p[0]][p[1]] ||
33                 this.table[p[0]]["*"] ||
34                 this.table["*"];
35         else
36             return this.table["*"];
37     },
38     set: function (mime_type, value) {
39         var p = mime_type_parse(mime_type);
40         if (p == "*")
41             return (this.table["*"] = value);
42         if (typeof p == "string") {
43             if (! this.table[p])
44                 this.table[p] = {};
45             return (this.table[p]["*"] = value);
46         }
47         if (! this.table[p[0]])
48             this.table[p[0]] = {};
49         return (this.table[p[0]][p[1]] = value);
50     }
54 /**
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.
59  */
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,
64         function () handlers,
65         function (table) handlers.table = table,
66         doc);
70 define_mime_type_table("external_content_handlers",
71     {
72         "*": getenv("EDITOR"),
73         text: { "*": getenv("EDITOR") },
74         image: { "*": "feh" },
75         video: { "*": "mplayer" },
76         audio: { "*": "mplayer" },
77         application: {
78             pdf: "evince",
79             postscript: "evince",
80             "x-dvi": "evince"
81         }
82     },
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.");
88 /**
89  *
90  */
91 function mime_type_from_uri (uri) {
92     var type = "application/octet-stream";
93     try {
94         uri = make_uri(uri);
95         type = mime_service.getTypeFromURI(uri);
96     } catch (e) {}
97     return type;
102  */
103 function mime_info_from_mime_type (type) {
104     if (type == null)
105         type = "application/octet-stream";
106     try {
107         return mime_service.getFromTypeAndExtension(type, null);
108     } catch (e) {
109         return null;
110     }