2 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
3 * (C) Copyright 2008 Nicholas A. Zigarovich
5 * Use, modification, and distribution are subject to the terms specified in the
9 const mime_service = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
11 define_variable("mime_type_external_handlers",
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")]
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)
37 function _get_mime_type_regexp (mime_type) {
38 if (mime_type instanceof RegExp)
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)
48 .replace(/\.\*/, "*");
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;
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);
66 return mime_type_external_handlers.splice(mt_i, 1)[0][1];
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
76 * define_mime_type_external_handler("video/ogg-theora", "mplayer", "video/*");
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 == "^.*$" ?
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);
91 eh[mt_i] = [mt_r, handler];
93 let mt_m = /^([^\/]+)\/[^*]+$/.exec(mt_s);
95 before = mt_m[1] + "/*";
96 define_mime_type_external_handler(mime_type, handler, before);
98 eh.splice(eh_eol, 0, [mt_r, handler]);
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);
107 eh.splice(bf_i, 0, [mt_r, handler]);
111 function mime_type_from_uri (uri) {
112 var type = "application/octet-stream";
115 type = mime_service.getTypeFromURI(uri);
120 function mime_info_from_mime_type (type) {
122 type = "application/octet-stream";
124 return mime_service.getFromTypeAndExtension(type, null);