2 * (C) Copyright 2008 Jeremy Maitin-Shepard
4 * Use, modification, and distribution are subject to the terms specified in the
8 require("special-buffer.js");
9 require("mime-type-override.js");
10 require("minibuffer-read-mime-type.js");
12 var download_manager_service = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
13 //var download_manager_ui = Cc["@mozilla.org/download-manager-ui;1"].getService(Ci.nsIDownloadManagerUI);
14 var download_manager_builtin_ui = Components.classesByID["{7dfdf0d1-aff6-4a34-bad1-d0fe74601642}"]
15 .getService(Ci.nsIDownloadManagerUI);
17 /* This implements nsIHelperAppLauncherDialog interface. */
18 function download_helper()
20 download_helper.prototype = {
21 QueryInterface: generate_QI(Ci.nsIHelperAppLauncherDialog, Ci.nsIWebProgressListener2),
23 handle_show: function () {
24 var action_chosen = false;
26 var can_view_internally = this.frame != null &&
27 can_override_mime_type_for_uri(this.launcher.source);
29 this.panel = create_info_panel(this.window, "download-panel",
30 [["downloading", "Downloading:", this.launcher.source.spec],
31 ["mime-type", "Mime type:", this.launcher.MIMEInfo.MIMEType]]);
32 var action = yield this.window.minibuffer.read_single_character_option(
33 $prompt = "Action to perform: (s: save; o: open; O: open URL; c: copy URL; " +
34 (can_view_internally ? "i: view internally; t: view as text)" : ")"),
35 $options = (can_view_internally ? ["s", "o", "O", "c", "i", "t"] : ["s", "o", "O", "c"]));
38 var suggested_path = suggest_save_path_from_file_name(this.launcher.suggestedFileName, this.buffer);
39 var file = yield this.window.minibuffer.read_file_check_overwrite(
40 $prompt = "Save to file:",
41 $initial_value = suggested_path,
43 register_download(this.buffer, this.launcher.source);
44 this.launcher.saveToDisk(file, false);
47 } else if (action == "o") {
48 var cwd = this.buffer ? this.buffer.cwd : default_directory.path;
49 var mime_type = this.launcher.MIMEInfo.MIMEType;
50 var suggested_action = get_external_handler_for_mime_type(mime_type);
51 var command = yield this.window.minibuffer.read_shell_command(
52 $initial_value = suggested_action,
54 var file = get_temporary_file(this.launcher.suggestedFileName);
55 var info = register_download(this.buffer, this.launcher.source);
56 info.temporary_status = DOWNLOAD_TEMPORARY_FOR_COMMAND;
57 info.set_shell_command(command, cwd);
58 this.launcher.saveToDisk(file, false);
60 } else if (action == "O") {
62 this.abort(); // abort download
63 let mime_type = this.launcher.MIMEInfo.MIMEType;
64 let cwd = this.buffer ? this.buffer.cwd : this.window.buffers.current.cwd;
65 let cmd = yield this.window.minibuffer.read_shell_command(
67 $initial_value = get_external_handler_for_mime_type(mime_type));
68 shell_command_with_argument_blind(cmd, this.launcher.source.spec, $cwd = cwd);
69 } else if (action == "c") {
71 this.abort(); // abort download
72 let uri = this.launcher.source.spec;
73 writeToClipboard(uri);
74 this.window.minibuffer.message("Copied: " + uri);
75 } else /* if (action == "i" || action == "t") */ {
78 mime_type = "text/plain";
80 let suggested_type = this.launcher.MIMEInfo.MIMEType;
81 if (gecko_viewable_mime_type_list.indexOf(suggested_type) == -1)
82 suggested_type = "text/plain";
83 mime_type = yield this.window.minibuffer.read_gecko_viewable_mime_type(
84 $prompt = "View internally as",
85 $initial_value = suggested_type,
89 this.abort(); // abort before reloading
91 override_mime_type_for_next_load(this.launcher.source, mime_type);
92 this.frame.location = this.launcher.source.spec; // reload
95 handle_interactive_error(this.window, e);
103 show : function (launcher, context, reason) {
104 this.launcher = launcher;
106 // Get associated buffer; if that fails (hopefully not), just get any window
111 frame = context.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal);
112 window = get_window_from_frame(frame.top);
114 buffer = get_buffer_from_frame(window, frame);
116 window = get_recent_conkeror_window();
118 if (window == null) {
119 // FIXME: need to handle this case perhaps where no windows exist
120 this.abort(); // for now, just cancel the download
126 this.window = window;
127 this.buffer = buffer;
129 co_call(this.handle_show());
132 abort : function () {
133 const NS_BINDING_ABORTED = 0x804b0002;
134 this.launcher.cancel(NS_BINDING_ABORTED);
137 cleanup : function () {
139 this.panel.destroy();
141 this.launcher = null;
147 promptForSaveToFile : function(launcher, context, default_file, suggested_file_extension) {
153 var unmanaged_download_info_list = [];
154 var id_to_download_info = {};
156 // Import these constants for convenience
157 const DOWNLOAD_NOTSTARTED = Ci.nsIDownloadManager.DOWNLOAD_NOTSTARTED;
158 const DOWNLOAD_DOWNLOADING = Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING;
159 const DOWNLOAD_FINISHED = Ci.nsIDownloadManager.DOWNLOAD_FINISHED;
160 const DOWNLOAD_FAILED = Ci.nsIDownloadManager.DOWNLOAD_FAILED;
161 const DOWNLOAD_CANCELED = Ci.nsIDownloadManager.DOWNLOAD_CANCELED;
162 const DOWNLOAD_PAUSED = Ci.nsIDownloadManager.DOWNLOAD_PAUSED;
163 const DOWNLOAD_QUEUED = Ci.nsIDownloadManager.DOWNLOAD_QUEUED;
164 const DOWNLOAD_BLOCKED = Ci.nsIDownloadManager.DOWNLOAD_BLOCKED;
165 const DOWNLOAD_SCANNING = Ci.nsIDownloadManager.DOWNLOAD_SCANNING;
168 const DOWNLOAD_NOT_TEMPORARY = 0;
169 const DOWNLOAD_TEMPORARY_FOR_ACTION = 1;
170 const DOWNLOAD_TEMPORARY_FOR_COMMAND = 2;
172 function download_info(source_buffer, mozilla_info) {
173 this.source_buffer = source_buffer;
174 if (mozilla_info != null)
175 this.attach(mozilla_info);
177 download_info.prototype = {
178 attach : function (mozilla_info) {
179 this.mozilla_info = mozilla_info;
180 id_to_download_info[mozilla_info.id] = this;
181 download_added_hook.run(this);
184 shell_command : null,
186 shell_command_cwd : null,
188 temporary_status : DOWNLOAD_NOT_TEMPORARY,
190 action_description : null,
192 set_shell_command : function (str, cwd) {
193 this.shell_command = str;
194 this.shell_command_cwd = cwd;
195 if (this.mozilla_info)
196 download_shell_command_change_hook.run(this);
200 * None of the following members may be used until attach is called
203 // Reflectors to properties of nsIDownload
204 get state () { return this.mozilla_info.state; },
205 get target_file () { return this.mozilla_info.targetFile; },
206 get amount_transferred () { return this.mozilla_info.amountTransferred; },
207 get percent_complete () { return this.mozilla_info.percentComplete; },
209 var s = this.mozilla_info.size;
210 /* nsIDownload.size is a PRUint64, and will have value
211 * LL_MAXUINT (2^64 - 1) to indicate an unknown size. Because
212 * JavaScript only has a double numerical type, this value
213 * cannot be represented exactly, so 2^36 is used instead as the cutoff. */
214 if (s < 68719476736 /* 2^36 */)
218 get source () { return this.mozilla_info.source; },
219 get start_time () { return this.mozilla_info.startTime; },
220 get speed () { return this.mozilla_info.speed; },
221 get MIME_info () { return this.mozilla_info.MIMEInfo; },
224 return this.MIME_info.MIMEType;
227 get id () { return this.mozilla_info.id; },
228 get referrer () { return this.mozilla_info.referrer; },
230 throw_if_removed : function () {
232 throw interactive_error("Download has already been removed from the download manager.");
235 throw_state_error : function () {
236 switch (this.state) {
237 case DOWNLOAD_DOWNLOADING:
238 throw interactive_error("Download is already in progress.");
239 case DOWNLOAD_FINISHED:
240 throw interactive_error("Download has already completed.");
241 case DOWNLOAD_FAILED:
242 throw interactive_error("Download has already failed.");
243 case DOWNLOAD_CANCELED:
244 throw interactive_error("Download has already been canceled.");
245 case DOWNLOAD_PAUSED:
246 throw interactive_error("Download has already been paused.");
247 case DOWNLOAD_QUEUED:
248 throw interactive_error("Download is queued.");
250 throw new Error("Download has unexpected state: " + this.state);
254 // Download manager operations
255 cancel : function () {
256 this.throw_if_removed();
257 switch (this.state) {
258 case DOWNLOAD_DOWNLOADING:
259 case DOWNLOAD_PAUSED:
260 case DOWNLOAD_QUEUED:
262 download_manager_service.cancelDownload(this.id);
264 throw interactive_error("Download cannot be canceled.");
268 this.throw_state_error();
272 retry : function () {
273 this.throw_if_removed();
274 switch (this.state) {
275 case DOWNLOAD_CANCELED:
276 case DOWNLOAD_FAILED:
278 download_manager_service.retryDownload(this.id);
280 throw interactive_error("Download cannot be retried.");
284 this.throw_state_error();
288 resume : function () {
289 this.throw_if_removed();
290 switch (this.state) {
291 case DOWNLOAD_PAUSED:
293 download_manager_service.resumeDownload(this.id);
295 throw interactive_error("Download cannot be resumed.");
299 this.throw_state_error();
303 pause : function () {
304 this.throw_if_removed();
305 switch (this.state) {
306 case DOWNLOAD_DOWNLOADING:
307 case DOWNLOAD_QUEUED:
309 download_manager_service.pauseDownload(this.id);
311 throw interactive_error("Download cannot be paused.");
315 this.throw_state_error();
319 remove : function () {
320 this.throw_if_removed();
321 switch (this.state) {
322 case DOWNLOAD_FAILED:
323 case DOWNLOAD_CANCELED:
324 case DOWNLOAD_FINISHED:
326 download_manager_service.removeDownload(this.id);
328 throw interactive_error("Download cannot be removed.");
332 throw interactive_error("Download is still in progress.");
336 delete_target : function () {
337 if (this.state != DOWNLOAD_FINISHED)
338 throw interactive_error("Download has not finished.");
340 this.target_file.remove(false);
344 case Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST:
345 throw interactive_error("File has already been deleted.");
346 case Cr.NS_ERROR_FILE_ACCESS_DENIED:
347 throw interactive_error("Access denied");
348 case Cr.NS_ERROR_FILE_DIR_NOT_EMPTY:
349 throw interactive_error("Failed to delete file.");
357 var define_download_local_hook = simple_local_hook_definer();
359 // FIXME: add more parameters
360 function register_download(buffer, source_uri) {
361 var info = new download_info(buffer);
362 info.registered_time_stamp = Date.now();
363 info.registered_source_uri = source_uri;
364 unmanaged_download_info_list.push(info);
368 function match_registered_download(mozilla_info) {
369 let list = unmanaged_download_info_list;
371 for (let i = 0; i < list.length; ++i) {
373 if (x.registered_source_uri == mozilla_info.source) {
377 if (t - x.registered_time_stamp > download_info_max_queue_delay) {
386 define_download_local_hook("download_added_hook");
387 define_download_local_hook("download_removed_hook");
388 define_download_local_hook("download_finished_hook");
389 define_download_local_hook("download_progress_change_hook");
390 define_download_local_hook("download_state_change_hook");
391 define_download_local_hook("download_shell_command_change_hook");
393 var download_info_max_queue_delay = 100;
395 var download_progress_listener = {
396 QueryInterface: generate_QI(Ci.nsIDownloadProgressListener),
398 onDownloadStateChange : function (state, download) {
400 /* FIXME: Determine if only new downloads will have this state
401 * as their previous state. */
403 dumpln("download state change: " + download.source.spec + ": " + state + ", " + download.state + ", " + download.id);
405 if (state == DOWNLOAD_NOTSTARTED) {
406 info = match_registered_download(download);
408 info = new download_info(null, download);
409 dumpln("error: encountered unknown new download");
411 info.attach(download);
414 info = id_to_download_info[download.id];
416 dumpln("Error: encountered unknown download");
419 info.mozilla_info = download;
420 download_state_change_hook.run(info);
421 if (info.state == DOWNLOAD_FINISHED) {
422 download_finished_hook.run(info);
424 if (info.shell_command != null) {
425 info.running_shell_command = true;
426 co_call(function () {
428 yield shell_command_with_argument(info.shell_command,
429 info.target_file.path,
430 $cwd = info.shell_command_cwd);
432 if (info.temporary_status == DOWNLOAD_TEMPORARY_FOR_COMMAND)
433 info.target_file.remove(false /* not recursive */);
434 info.running_shell_command = false;
435 download_shell_command_change_hook.run(info);
438 download_shell_command_change_hook.run(info);
445 onProgressChange : function (progress, request, cur_self_progress, max_self_progress,
446 cur_total_progress, max_total_progress,
448 var info = id_to_download_info[download.id];
450 dumpln("error: encountered unknown download in progress change");
453 info.mozilla_info = download;
454 download_progress_change_hook.run(info);
455 //dumpln("download progress change: " + download.source.spec + ": " + cur_self_progress + "/" + max_self_progress + " "
456 // + cur_total_progress + "/" + max_total_progress + ", " + download.state + ", " + download.id);
459 onSecurityChange : function (progress, request, state, download) {
462 onStateChange : function (progress, request, state_flags, status, download) {
466 var download_observer = {
467 observe : function(subject, topic, data) {
469 case "download-manager-remove-download":
472 // Remove all downloads
473 for (let i in id_to_download_info)
476 let id = subject.QueryInterface(Ci.nsISupportsPRUint32);
477 /* FIXME: determine if this should really be an error */
478 if (!(id in id_to_download_info)) {
479 dumpln("Error: download-manager-remove-download event received for unknown download: " + id);
483 for each (let i in ids) {
484 dumpln("deleting download: " + i);
485 let d = id_to_download_info[i];
487 download_removed_hook.run(d);
488 delete id_to_download_info[i];
494 observer_service.addObserver(download_observer, "download-manager-remove-download", false);
496 download_manager_service.addListener(download_progress_listener);
498 function pretty_print_file_size(val) {
499 const GIBI = 1073741824; /* 2^30 */
500 const MEBI = 1048576; /* 2^20 */
501 const KIBI = 1024; /* 2^10 */
507 else if (val < MEBI) {
510 } else if (val < GIBI) {
523 return [val.toFixed(precision), suffix];
526 function pretty_print_time(val) {
527 val = Math.round(val);
528 var seconds = val % 60;
530 val = Math.floor(val / 60);
532 var minutes = val % 60;
534 var hours = Math.floor(val / 60);
539 parts.push(hours + " hours");
541 parts.push("1 hour");
544 parts.push(minutes + " minutes");
545 else if (minutes == 1)
546 parts.push("1 minute");
548 if (minutes <= 1 && hours == 0) {
550 parts.push(seconds + " seconds");
552 parts.push("1 second");
555 return parts.join(", ");
559 "download_buffer_min_update_interval", 2000,
560 "Minimum interval (in milliseconds) between updates in download progress buffers.\n" +
561 "Lowering this interval will increase the promptness of the progress display at " +
562 "the cost of using additional processor time.");
564 define_keywords("$info");
565 function download_buffer(window, element) {
566 this.constructor_begin();
568 special_buffer.call(this, window, element, forward_keywords(arguments));
569 this.info = arguments.$info;
570 this.configuration.cwd = this.info.mozilla_info.targetFile.parent.path;
571 this.description = this.info.mozilla_info.source.spec;
572 this.keymap = download_buffer_keymap;
575 this.progress_change_handler_fn = method_caller(this, this.handle_progress_change);
576 add_hook.call(this.info, "download_progress_change_hook", this.progress_change_handler_fn);
577 add_hook.call(this.info, "download_state_change_hook", this.progress_change_handler_fn);
578 this.command_change_handler_fn = method_caller(this, this.update_command_field);
579 add_hook.call(this.info, "download_shell_command_change_hook", this.command_change_handler_fn);
580 this.constructor_end();
582 download_buffer.prototype = {
583 __proto__: special_buffer.prototype,
585 handle_kill : function () {
586 this.__proto__.handle_kill.call(this);
587 remove_hook.call(this.info, "download_progress_change_hook", this.progress_change_handler_fn);
588 remove_hook.call(this.info, "download_state_change_hook", this.progress_change_handler_fn);
589 remove_hook.call(this.info, "download_shell_command_change_hook", this.command_change_handler_fn);
591 // Remove all node references
592 delete this.status_textnode;
593 delete this.transferred_div_node;
594 delete this.transferred_textnode;
595 delete this.progress_container_node;
596 delete this.progress_bar_node;
597 delete this.time_textnode;
598 delete this.command_div_node;
599 delete this.command_label_textnode;
600 delete this.command_textnode;
603 update_title : function () {
604 // FIXME: do this properly
606 var info = this.info;
607 var append_transfer_info = false;
608 var append_speed_info = true;
611 case DOWNLOAD_DOWNLOADING:
612 label = "Downloading";
613 append_transfer_info = true;
615 case DOWNLOAD_FINISHED:
616 label = "Download complete";
618 case DOWNLOAD_FAILED:
619 label = "Download failed";
620 append_transfer_info = true;
621 append_speed_info = false;
623 case DOWNLOAD_CANCELED:
624 label = "Download canceled";
625 append_transfer_info = true;
626 append_speed_info = false;
628 case DOWNLOAD_PAUSED:
629 label = "Download paused";
630 append_transfer_info = true;
631 append_speed_info = false;
633 case DOWNLOAD_QUEUED:
635 label = "Download queued";
639 if (append_transfer_info) {
640 if (append_speed_info)
641 new_title = label + " at " + pretty_print_file_size(info.speed).join(" ") + "/s: ";
643 new_title = label + ": ";
644 var trans = pretty_print_file_size(info.amount_transferred);
645 if (info.size >= 0) {
646 var total = pretty_print_file_size(info.size);
647 if (trans[1] == total[1])
648 new_title += trans[0] + "/" + total[0] + " " + total[1];
650 new_title += trans.join(" ") + "/" + total.join(" ");
652 new_title += trans.join(" ");
653 if (info.percent_complete >= 0)
654 new_title += " (" + info.percent_complete + "%)";
657 if (new_title != this.title) {
658 this.title = new_title;
664 handle_progress_change : function () {
665 var cur_time = Date.now();
666 if (this.last_update == null ||
667 (cur_time - this.last_update) > download_buffer_min_update_interval ||
668 this.info.state != this.previous_state) {
670 if (this.update_title())
671 buffer_title_change_hook.run(this);
673 if (this.generated) {
674 this.update_fields();
676 this.previous_status = this.info.status;
677 this.last_update = cur_time;
681 generate: function() {
682 var d = this.document;
683 var g = new dom_generator(d, XHTML_NS);
685 /* Warning: If any additional node references are saved in
686 * this function, appropriate code to delete the saved
687 * properties must be added to handle_kill. */
689 var info = this.info;
691 d.body.setAttribute("class", "download-buffer");
693 g.add_stylesheet("chrome://conkeror/content/downloads.css");
698 div = g.element("div", d.body, "class", "download-info", "id", "download-source");
699 label = g.element("div", div, "class", "download-label");
700 this.status_textnode = g.text("", label);
701 value = g.element("div", div, "class", "download-value");
702 g.text(info.source.spec, value);
704 div = g.element("div", d.body, "class", "download-info", "id", "download-target");
705 label = g.element("div", div, "class", "download-label");
707 if (info.temporary_status != DOWNLOAD_NOT_TEMPORARY)
708 target_label = "Temp. file:";
710 target_label = "Target:";
711 g.text(target_label, label);
712 value = g.element("div", div, "class", "download-value");
713 g.text(info.target_file.path, value);
715 div = g.element("div", d.body, "class", "download-info", "id", "download-mime-type");
716 label = g.element("div", div, "class", "download-label");
717 g.text("MIME type:", label);
718 value = g.element("div", div, "class", "download-value");
719 g.text(info.MIME_type || "unknown", value);
721 this.transferred_div_node = div = g.element("div", d.body,
722 "class", "download-info",
723 "id", "download-transferred");
724 label = g.element("div", div, "class", "download-label");
725 g.text("Transferred:", label);
726 value = g.element("div", div, "class", "download-value");
727 this.transferred_textnode = g.text("", value);
728 this.progress_container_node = value = g.element("div", div, "id", "download-progress-container");
729 this.progress_bar_node = g.element("div", value, "id", "download-progress-bar");
730 value = g.element("div", div, "class", "download-value", "id", "download-percent");
731 this.percent_textnode = g.text("", value);
733 div = g.element("div", d.body, "class", "download-info", "id", "download-time");
734 label = g.element("div", div, "class", "download-label");
735 g.text("Time:", label);
736 value = g.element("div", div, "class", "download-value");
737 this.time_textnode = g.text("", value);
739 if (info.action_description != null) {
740 div = g.element("div", d.body, "class", "download-info", "id", "download-action");
741 label = g.element("div", div, "class", "download-label");
742 g.text("Action:", label);
743 value = g.element("div", div, "class", "download-value");
744 g.text(info.action_description, value);
747 this.command_div_node = div = g.element("div", d.body, "class", "download-info", "id", "download-command");
748 label = g.element("div", div, "class", "download-label");
749 this.command_label_textnode = g.text("Run command:", label);
750 value = g.element("div", div, "class", "download-value");
751 this.command_textnode = g.text("", value);
753 this.update_fields();
755 this.update_command_field();
758 update_fields : function () {
761 var info = this.info;
764 case DOWNLOAD_DOWNLOADING:
765 label = "Downloading";
767 case DOWNLOAD_FINISHED:
770 case DOWNLOAD_FAILED:
773 case DOWNLOAD_CANCELED:
776 case DOWNLOAD_PAUSED:
779 case DOWNLOAD_QUEUED:
784 this.status_textnode.nodeValue = label + ":";
785 this.update_time_field();
788 if (info.state == DOWNLOAD_FINISHED)
789 tran_text = pretty_print_file_size(info.size).join(" ");
791 var trans = pretty_print_file_size(info.amount_transferred);
792 if (info.size >= 0) {
793 var total = pretty_print_file_size(info.size);
794 if (trans[1] == total[1])
795 tran_text += trans[0] + "/" + total[0] + " " + total[1];
797 tran_text += trans.join(" ") + "/" + total.join(" ");
799 tran_text += trans.join(" ");
801 this.transferred_textnode.nodeValue = tran_text;
802 if (info.percent_complete >= 0) {
803 this.progress_container_node.style.display = "";
804 this.percent_textnode.nodeValue = info.percent_complete + "%";
805 this.progress_bar_node.style.width = info.percent_complete + "%";
807 this.percent_textnode.nodeValue = "";
808 this.progress_container_node.style.display = "none";
811 this.update_command_field();
814 update_time_field : function () {
815 var info = this.info;
816 var elapsed_text = pretty_print_time((Date.now() - info.start_time / 1000) / 1000) + " elapsed";
818 if (info.state == DOWNLOAD_DOWNLOADING) {
819 text = pretty_print_file_size(info.speed).join(" ") + "/s, ";
821 if (info.state == DOWNLOAD_DOWNLOADING &&
824 let remaining = (info.size - info.amount_transferred) / info.speed;
825 text += pretty_print_time(remaining) + " left (" + elapsed_text + ")";
829 this.time_textnode.nodeValue = text;
832 update_command_field : function () {
835 if (this.info.shell_command != null) {
836 this.command_div_node.style.display = "";
838 if (this.info.running_shell_command)
840 else if (this.info.state == DOWNLOAD_FINISHED)
841 label = "Ran command:";
843 label = "Run command:";
844 this.command_label_textnode.nodeValue = label;
845 this.command_textnode.nodeValue = this.info.shell_command;
847 this.command_div_node.style.display = "none";
852 function download_cancel(buffer) {
853 check_buffer(buffer, download_buffer);
854 var info = buffer.info;
856 buffer.window.minibuffer.message("Download canceled");
858 interactive("download-cancel",
859 "Cancel the current download.\n" +
860 "The download can later be retried using the `download-retry' command, but any " +
861 "data already transferred will be lost.",
862 function (I) {download_cancel(I.buffer);});
864 function download_retry(buffer) {
865 check_buffer(buffer, download_buffer);
866 var info = buffer.info;
868 buffer.window.minibuffer.message("Download retried");
870 interactive("download-retry",
871 "Retry a failed or canceled download.\n" +
872 "This command can be used to retry a download that failed or was cancled using " +
873 "the `download-cancel' command. The download will begin from the start again.",
874 function (I) {download_retry(I.buffer);});
876 function download_pause(buffer) {
877 check_buffer(buffer, download_buffer);
879 buffer.window.minibuffer.message("Download paused");
881 interactive("download-pause",
882 "Pause the current download.\n" +
883 "The download can later be resumed using the `download-resume' command. The " +
884 "data already transferred will not be lost.",
885 function (I) {download_pause(I.buffer);});
887 function download_resume(buffer) {
888 check_buffer(buffer, download_buffer);
889 buffer.info.resume();
890 buffer.window.minibuffer.message("Download resumed");
892 interactive("download-resume",
893 "Resume the current download.\n" +
894 "This command can be used to resume a download paused using the `download-pause' command.",
895 function (I) {download_resume(I.buffer);});
897 function download_remove(buffer) {
898 check_buffer(buffer, download_buffer);
899 buffer.info.remove();
900 buffer.window.minibuffer.message("Download removed");
902 interactive("download-remove",
903 "Remove the current download from the download manager.\n" +
904 "This command can only be used on inactive (paused, canceled, completed, or failed) downloads.",
905 function (I) {download_remove(I.buffer);});
907 function download_retry_or_resume(buffer) {
908 check_buffer(buffer, download_buffer);
909 var info = buffer.info;
910 if (info.state == DOWNLOAD_PAUSED)
911 download_resume(buffer);
913 download_retry(buffer);
915 interactive("download-retry-or-resume",
916 "Retry or resume the current download.\n" +
917 "This command can be used to resume a download paused using the `download-pause' " +
918 "command or canceled using the `download-cancel' command.",
919 function (I) {download_retry_or_resume(I.buffer);});
921 function download_pause_or_resume(buffer) {
922 check_buffer(buffer, download_buffer);
923 var info = buffer.info;
924 if (info.state == DOWNLOAD_PAUSED)
925 download_resume(buffer);
927 download_pause(buffer);
929 interactive("download-pause-or-resume",
930 "Pause or resume the current download.\n" +
931 "This command toggles the paused state of the current download.",
932 function (I) {download_pause_or_resume(I.buffer);});
934 function download_delete_target(buffer) {
935 check_buffer(buffer, download_buffer);
936 var info = buffer.info;
937 info.delete_target();
938 buffer.window.minibuffer.message("Deleted file: " + info.target_file.path);
940 interactive("download-delete-target",
941 "Delete the target file of the current download.\n" +
942 "This command can only be used if the download has finished successfully.",
943 function (I) {download_delete_target(I.buffer);});
945 function download_shell_command(buffer, cwd, cmd) {
946 check_buffer(buffer, download_buffer);
947 var info = buffer.info;
948 if (info.state == DOWNLOAD_FINISHED) {
949 shell_command_with_argument_blind(cmd, info.target_file.path, $cwd = cwd);
952 if (info.state != DOWNLOAD_DOWNLOADING && info.state != DOWNLOAD_PAUSED && info.state != DOWNLOAD_QUEUED)
953 info.throw_state_error();
954 if (cmd == null || cmd.length == 0)
955 info.set_shell_command(null, cwd);
957 info.set_shell_command(cmd, cwd);
958 buffer.window.minibuffer.message("Queued shell command: " + cmd);
960 interactive("download-shell-command",
961 "Run a shell command on the target file of the current download.\n" +
962 "If the download is still in progress, the shell command will be queued " +
963 "to run when the download finishes.",
965 var buffer = check_buffer(I.buffer, download_buffer);
966 var cwd = buffer.info.shell_command_cwd || buffer.cwd;
967 var cmd = yield I.minibuffer.read_shell_command(
969 $initial_value = buffer.info.shell_command ||
970 get_external_handler_for_mime_type(buffer.info.MIME_type));
971 download_shell_command(buffer, cwd, cmd);
974 function download_manager_ui()
976 download_manager_ui.prototype = {
977 QueryInterface : XPCOMUtils.generateQI([Ci.nsIDownloadManagerUI]),
979 getAttention : function () {},
980 show : function () {},
985 function download_manager_show_builtin_ui(window) {
986 download_manager_builtin_ui.show(window);
988 interactive("download-manager-show-builtin-ui",
989 "Show the built-in (Firefox-style) download manager user interface.",
990 function (I) {download_manager_show_builtin_ui(I.window);});
994 define_variable("download_temporary_file_open_buffer_delay", 500,
995 "Delay (in milliseconds) before a download buffer is opened for temporary downloads.\n" +
996 "This variable takes effect only if `open_download_buffer_automatically' is in " +
997 "`download_added_hook', as it is by default.");
1000 define_variable("download_buffer_automatic_open_target", OPEN_NEW_WINDOW,
1001 "Target for download buffers created by the `open_download_buffer_automatically' function.\n" +
1002 "This variable takes effect only if `open_download_buffer_auotmatically' is in " +
1003 "`download_added_hook', as it is by default.");
1005 function open_download_buffer_automatically(info) {
1006 var buf = info.source_buffer;
1007 var target = download_buffer_automatic_open_target;
1009 target = OPEN_NEW_WINDOW;
1010 if (info.temporary_status == DOWNLOAD_NOT_TEMPORARY ||
1011 !(download_temporary_file_open_buffer_delay > 0))
1012 create_buffer(buf, buffer_creator(download_buffer, $info = info), target);
1018 add_hook.call(info, "download_finished_hook", finish);
1019 timer = call_after_timeout(function () {
1020 remove_hook.call(info, "download_finished_hook", finish);
1021 create_buffer(buf, buffer_creator(download_buffer, $info = info), target);
1022 }, download_temporary_file_open_buffer_delay);
1025 add_hook("download_added_hook", open_download_buffer_automatically);