new module system
[conkeror.git] / modules / content-handler.js
blobca4f85e70cceca159bba7e0ebe55788ca44952d9
1 /**
2  * (C) Copyright 2008 Jeremy Maitin-Shepard
3  * (C) Copyright 2009 John Foerch
4  *
5  * Use, modification, and distribution are subject to the terms specified in the
6  * COPYING file.
7 **/
9 in_module(null);
11 require("mime-type-override.js");
13 define_mime_type_table("content_handlers", {},
14     "A mime type table mapping mime types to content handlers. This table "+
15     "will be checked for a match when a navigation event causes Conkeror "+
16     "to encounter a mime type which is not supported by Mozilla.  If no "+
17     "appropriate content handler is found in this table, the user will "+
18     "be prompted to choose from among common actions.");
20 /**
21  * content_handler_context is a datatype for storing contextual information
22  * that can be used by content handlers, such as window, buffer, frame, and
23  * the download launcher object.  It will also contain an abort method to
24  * abort the download.  Download helper actions are passed an object of this
25  * type.
26  */
27 function content_handler_context () {}
30 /**
31  * The content_handler_* functions below are all things that conkeror
32  * is able to do when Mozilla has invoked download_helper because of an
33  * attempt to navigate to a document of an unsupported mime type.
34  */
35 function content_handler_save (ctx) {
36     var suggested_path = suggest_save_path_from_file_name(
37         ctx.launcher.suggestedFileName, ctx.buffer);
38     var file = yield ctx.window.minibuffer.read_file_check_overwrite(
39         $prompt = "Save to file:",
40         $initial_value = suggested_path,
41         $select);
42     register_download(ctx.buffer, ctx.launcher.source);
43     ctx.launcher.saveToDisk(file, false);
46 function content_handler_open (ctx) {
47     var cwd = with_current_buffer(ctx.buffer, function (I) I.local.cwd);
48     var mime_type = ctx.launcher.MIMEInfo.MIMEType;
49     var suggested_action = external_content_handlers.get(mime_type);
50     var command = yield ctx.window.minibuffer.read_shell_command(
51         $initial_value = suggested_action,
52         $cwd = cwd);
53     var file = get_temporary_file(ctx.launcher.suggestedFileName);
54     var info = register_download(ctx.buffer, ctx.launcher.source);
55     info.temporary_status = DOWNLOAD_TEMPORARY_FOR_COMMAND;
56     info.set_shell_command(command, cwd);
57     ctx.launcher.saveToDisk(file, false);
60 function content_handler_open_default_viewer (ctx) {
61     var cwd = with_current_buffer(ctx.buffer, function (I) I.local.cwd);
62     var mime_type = ctx.launcher.MIMEInfo.MIMEType;
63     var command = external_content_handlers.get(mime_type);
64     if (command == null)
65         command = yield ctx.window.minibuffer.read_shell_command(
66             $initial_value = command,
67             $cwd = cwd);
68     var file = get_temporary_file(ctx.launcher.suggestedFileName);
69     var info = register_download(ctx.buffer, ctx.launcher.source);
70     info.temporary_status = DOWNLOAD_TEMPORARY_FOR_COMMAND;
71     info.set_shell_command(command, cwd);
72     ctx.launcher.saveToDisk(file, false);
75 function content_handler_open_url (ctx) {
76     ctx.abort(); // abort download
77     let mime_type = ctx.launcher.MIMEInfo.MIMEType;
78     let cwd = with_current_buffer(ctx.buffer, function (I) I.local.cwd);
79     let cmd = yield ctx.window.minibuffer.read_shell_command(
80         $cwd = cwd,
81         $initial_value = external_content_handlers.get(mime_type));
82     shell_command_with_argument_blind(cmd, ctx.launcher.source.spec, $cwd = cwd);
85 function content_handler_copy_url (ctx) {
86     ctx.abort(); // abort download
87     let uri = ctx.launcher.source.spec;
88     writeToClipboard(uri);
89     ctx.window.minibuffer.message("Copied: " + uri);
92 function content_handler_view_internally (ctx) {
93     var suggested_type = ctx.launcher.MIMEInfo.MIMEType;
94     if (viewable_mime_type_list.indexOf(suggested_type) == -1)
95         suggested_type = "text/plain";
96     var mime_type = yield ctx.window.minibuffer.read_viewable_mime_type(
97         $prompt = "View internally as",
98         $initial_value = suggested_type,
99         $select);
100     ctx.abort(); // abort before reloading
101     override_mime_type_for_next_load(ctx.launcher.source, mime_type);
102     ctx.frame.location = ctx.launcher.source.spec; // reload
105 function content_handler_view_as_text (ctx) {
106     ctx.abort(); // abort before reloading
107     override_mime_type_for_next_load(ctx.launcher.source, "text/plain");
108     ctx.frame.location = ctx.launcher.source.spec; // reload
111 function content_handler_prompt (ctx) {
112     var action_chosen = false;
113     var can_view_internally = ctx.frame != null &&
114         can_override_mime_type_for_uri(ctx.launcher.source);
115     var panel;
116     try {
117         panel = create_info_panel(ctx.window, "download-panel",
118                                   [["downloading", "Downloading:", ctx.launcher.source.spec],
119                                    ["mime-type", "Mime type:", ctx.launcher.MIMEInfo.MIMEType]]);
120         var action = yield ctx.window.minibuffer.read_single_character_option(
121             $prompt = "Action to perform: (s: save; o: open; O: open URL; c: copy URL; " +
122                 (can_view_internally ? "i: view internally; t: view as text)" : ")"),
123             $options = (can_view_internally ? ["s", "o", "O", "c", "i", "t"] : ["s", "o", "O", "c"]));
124         switch (action) {
125         case "s":
126             yield content_handler_save(ctx);
127             action_chosen = true;
128             break;
129         case "o":
130             yield content_handler_open(ctx);
131             action_chosen = true;
132             break;
133         case "O":
134             yield content_handler_open_url(ctx);
135             action_chosen = true;
136             break;
137         case "c":
138             yield content_handler_copy_url(ctx);
139             action_chosen = true;
140             break;
141         case "i":
142             yield content_handler_view_internally(ctx);
143             action_chosen = true;
144             break;
145         case "t":
146             yield content_handler_view_as_text(ctx);
147             action_chosen = true;
148             break;
149         }
150     } catch (e) {
151         handle_interactive_error(ctx.window, e);
152     } finally {
153         if (! action_chosen)
154             ctx.abort();
155         if (panel)
156             panel.destroy();
157     }
162  * download_helper implements nsIHelperAppLauncherDialog.
164  * Sometimes, like when following a link, the content type of the document
165  * cannot be displayed by Mozilla.  When this happens, Mozilla calls the
166  * `show' method of our download_helper with a handle for the started
167  * download and other contextual information.
168  */
169 function download_helper () {}
170 download_helper.prototype = {
171     QueryInterface: generate_QI(Ci.nsIHelperAppLauncherDialog,
172                                 Ci.nsIWebProgressListener2),
174     show: function (launcher, context, reason) {
175         // content handlers will be passed a context containing references
176         // to the window, buffer, frame, launcher, and an abort method.
177         var ctx = new content_handler_context();
178         ctx.launcher = launcher;
179         ctx.abort = function () {
180             const NS_BINDING_ABORTED = 0x804b0002;
181             launcher.cancel(NS_BINDING_ABORTED);
182         };
183         // then set frame, window, and buffer in the context, so that
184         // action handlers can use them if needed.
185         try {
186             ctx.frame = context.QueryInterface(Ci.nsIInterfaceRequestor)
187                 .getInterface(Ci.nsIDOMWindowInternal);
188             ctx.window = get_window_from_frame(ctx.frame);
189             ctx.buffer = get_buffer_from_frame(ctx.window, ctx.frame);
190         } catch (e) {
191             ctx.window = get_recent_conkeror_window();
192             if (! ctx.window) {
193                 // FIXME: need to handle this case perhaps where no windows exist
194                 ctx.abort(); // for now, just cancel the download
195                 return;
196             }
197             ctx.buffer = ctx.window.buffers.current;
198             ctx.frame = ctx.buffer.focused_frame;
199         }
200         try {
201             // is there anything in content_handlers for this object?
202             var mime_type = launcher.MIMEInfo.MIMEType;
203             var action = content_handlers.get(mime_type) ||
204                 content_handler_prompt;
205             co_call(action(ctx));
206         } catch (e) {
207             handle_interactive_error(ctx.window, e);
208         }
209     },
211     promptForSaveToFile: function (launcher, context, default_file, suggested_file_extension) {
212         return null;
213     }
216 provide("content-handler");