Support Debian's upcoming xulrunner-1.9.1 packages
[conkeror.git] / modules / mime.js
blob21436ba05f25c209de893e304dfd135f45bddb32
1 /**
2  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
3  * (C) Copyright 2008 Nicholas A. Zigarovich
4  *
5  * Use, modification, and distribution are subject to the terms specified in the
6  * COPYING file.
7 **/
9 const mime_service = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
11 define_variable("mime_type_external_handlers",
12                 [
13                   [/^text\/.*$/, getenv("EDITOR")],
14                   [/^image\/.*$/, "feh"],
15                   [/^video\/.*$/, "mplayer"],
16                   [/^audio\/.*$/, "mplayer"],
17                   [/^application\/pdf$/, "evince"],
18                   [/^application\/postscript$/, "evince"],
19                   [/^application\/x-dvi$/, "evince"],
20                   [/^.*$/, getenv("EDITOR")]
21                 ],
22                 "Array of MIME types and external handlers. Each element is " +
23                 "an array of two elements, the first being the RegExp for " +
24                 "the target MIME type, the second being handler as a " +
25                 "string. Order matters. The handler of the first RegExp to " +
26                 "match a specified MIME type will be used.");
28 function _get_mime_type_index (mime_type) {
29     for (let i = 0; i < mime_type_external_handlers.length; ++i) {
30         let r = mime_type_external_handlers[i][0];
31         if (mime_type.source == r.source)
32             return i;
33     }
34     return -1;
37 function _get_mime_type_regexp (mime_type) {
38     if (mime_type instanceof RegExp)
39         return mime_type;
40     let mt_esc = mime_type.replace(/\*/, ".*").replace(/\//, "\\/");
41     return new RegExp("^" + mt_esc + "$");
44 function _get_mime_type_string (mime_type) {
45     if (mime_type instanceof RegExp) {
46         return mime_type.source.slice(1,-1)
47                                .replace(/\\\//, "/")
48                                .replace(/\.\*/, "*");
49     }
50     return mime_type;
53 function get_mime_type_external_handler (mime_type) {
54     if (mime_type instanceof RegExp) {
55         let mt_i = _get_mime_type_index(mime_type);
56         return mt_i != -1 ? mime_type_external_handlers[mt_i][1] : undefined;
57     }
58     else
59         return predicate_alist_match(mime_type_external_handlers, mime_type);
62 function remove_mime_type_external_handler (mime_type) {
63     let mt_r = _get_mime_type_regexp(mime_type);
64     let mt_i = _get_mime_type_index(mt_r);
65     if (mt_i != -1)
66         return mime_type_external_handlers.splice(mt_i, 1)[0][1];
67     return undefined;
70 /* Define a new external MIME type handler, or replace an existing handler.
71  * 'before' is optional, and must be a MIME type. If 'before' is specified,
72  * then the 'mime_type' will be added to the list immediately before the MIME
73  * type noted by 'before', or at the end of the list if 'before' is not on the
74  * list. For example:
75  *
76  * define_mime_type_external_handler("video/ogg-theora", "mplayer", "video/*");
77  */
78 function define_mime_type_external_handler (mime_type, handler, before) {
79     let eh = mime_type_external_handlers;
80     let eh_len = eh.length;
81     let eh_eol = eh_len > 0 && eh[eh_len-1][0].source == "^.*$" ?
82         eh_len - 1 : eh_len;
83     let mt_r = _get_mime_type_regexp(mime_type);
84     let mt_s = _get_mime_type_string(mime_type);
85     if (mt_s == "*" && ! before) {
86         remove_mime_type_external_handler(mt_r);
87         eh.push([mt_r, handler]);
88     } else if (! before) {
89         let mt_i = _get_mime_type_index(mt_r);
90         if (mt_i != -1)
91             eh[mt_i] = [mt_r, handler];
92         else {
93             let mt_m = /^([^\/]+)\/[^*]+$/.exec(mt_s);
94             if (mt_m != null) {
95                 before = mt_m[1] + "/*";
96                 define_mime_type_external_handler(mime_type, handler, before);
97             } else {
98                 eh.splice(eh_eol, 0, [mt_r, handler]);
99             }
100         }
101     } else {
102         remove_mime_type_external_handler(mt_r);
103         let bf_r = _get_mime_type_regexp(before);
104         let bf_i = _get_mime_type_index(bf_r);
105         if (bf_i == -1)
106             bf_i = eh_eol;
107         eh.splice(bf_i, 0, [mt_r, handler]);
108     }
111 function mime_type_from_uri (uri) {
112     var type = "application/octet-stream";
113     try {
114         uri = make_uri(uri);
115         type = mime_service.getTypeFromURI(uri);
116     } catch (e) {}
117     return type;
120 function mime_info_from_mime_type (type) {
121     if (type == null)
122         type = "application/octet-stream";
123     try {
124         return mime_service.getFromTypeAndExtension(type, null);
125     } catch (e) {
126         return null;
127     }