1 require("special-buffer.js");
2 var download_manager_service = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
3 //var download_manager_ui = Cc["@mozilla.org/download-manager-ui;1"].getService(Ci.nsIDownloadManagerUI);
4 var download_manager_builtin_ui = Components.classesByID["{7dfdf0d1-aff6-4a34-bad1-d0fe74601642}"]
5 .getService(Ci.nsIDownloadManagerUI);
7 /* This implements nsIHelperAppLauncherDialog interface. */
8 function download_helper()
10 download_helper.prototype = {
11 QueryInterface: generate_QI(Ci.nsIHelperAppLauncherDialog, Ci.nsIWebProgressListener2),
13 create_panel : function() {
14 this.panel = create_info_panel(this.window, "download-panel",
15 [["downloading", "Downloading:", this.launcher.source.spec],
16 ["mime-type", "Mime type:", this.launcher.MIMEInfo.MIMEType]]);
19 handle_show: function () {
20 var action_chosen = false;
23 var action = yield this.window.minibuffer.read_single_character_option(
24 $prompt = "Action to perform: (save or open)",
25 $options = ["s", "o"]);
29 var suggested_path = suggest_save_path_from_file_name(this.launcher.suggestedFileName, this.buffer);
30 var file = yield this.window.minibuffer.read_file_check_overwrite(
31 $prompt = "Save to file:",
32 $initial_value = suggested_path,
34 register_download(this.buffer, this.launcher.source);
35 this.launcher.saveToDisk(file, false);
39 var cwd = this.buffer ? this.buffer.cwd : default_directory.path;
40 var mime_type = this.launcher.MIMEInfo.MIMEType;
41 var suggested_action = get_external_handler_for_mime_type(mime_type);
42 var command = yield this.window.minibuffer.read_shell_command(
43 $initial_value = suggested_action,
45 var file = get_temporary_file(this.launcher.suggestedFileName);
46 var info = register_download(this.buffer, this.launcher.source);
47 info.temporary_status = DOWNLOAD_TEMPORARY_FOR_COMMAND;
48 info.set_shell_command(command, cwd);
49 this.launcher.saveToDisk(file, false);
53 handle_interactive_error(this.window, e);
62 show : function (launcher, context, reason) {
63 this.launcher = launcher;
65 // Get associated buffer; if that fails (hopefully not), just get any window
69 var frame = context.QueryInterface(Ci.nsIWebProgress).DOMWindow.top;
70 window = get_window_from_frame(frame);
72 buffer = get_buffer_from_frame(window, frame);
74 window = get_recent_conkeror_window();
77 // FIXME: need to handle this case perhaps where no windows exist
78 this.abort(); // for now, just cancel the download
86 co_call(this.handle_show());
90 const NS_BINDING_ABORTED = 0x804b0002;
91 this.launcher.cancel(NS_BINDING_ABORTED);
95 cleanup : function () {
104 promptForSaveToFile : function(launcher, context, default_file, suggested_file_extension) {
110 var unmanaged_download_info_list = [];
111 var id_to_download_info = {};
113 // Import these constants for convenience
114 const DOWNLOAD_NOTSTARTED = Ci.nsIDownloadManager.DOWNLOAD_NOTSTARTED;
115 const DOWNLOAD_DOWNLOADING = Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING;
116 const DOWNLOAD_FINISHED = Ci.nsIDownloadManager.DOWNLOAD_FINISHED;
117 const DOWNLOAD_FAILED = Ci.nsIDownloadManager.DOWNLOAD_FAILED;
118 const DOWNLOAD_CANCELED = Ci.nsIDownloadManager.DOWNLOAD_CANCELED;
119 const DOWNLOAD_PAUSED = Ci.nsIDownloadManager.DOWNLOAD_PAUSED;
120 const DOWNLOAD_QUEUED = Ci.nsIDownloadManager.DOWNLOAD_QUEUED;
121 const DOWNLOAD_BLOCKED = Ci.nsIDownloadManager.DOWNLOAD_BLOCKED;
122 const DOWNLOAD_SCANNING = Ci.nsIDownloadManager.DOWNLOAD_SCANNING;
125 const DOWNLOAD_NOT_TEMPORARY = 0;
126 const DOWNLOAD_TEMPORARY_FOR_ACTION = 1;
127 const DOWNLOAD_TEMPORARY_FOR_COMMAND = 2;
129 function download_info(source_buffer, mozilla_info) {
130 this.source_buffer = source_buffer;
131 if (mozilla_info != null)
132 this.attach(mozilla_info);
134 download_info.prototype = {
135 attach : function (mozilla_info) {
136 this.mozilla_info = mozilla_info;
137 id_to_download_info[mozilla_info.id] = this;
138 download_added_hook.run(this);
141 shell_command : null,
143 shell_command_cwd : null,
145 temporary_status : DOWNLOAD_NOT_TEMPORARY,
147 action_description : null,
149 set_shell_command : function (str, cwd) {
150 this.shell_command = str;
151 this.shell_command_cwd = cwd;
152 if (this.mozilla_info)
153 download_shell_command_change_hook.run(this);
157 * None of the following members may be used until attach is called
160 // Reflectors to properties of nsIDownload
161 get state () { return this.mozilla_info.state; },
162 get target_file () { return this.mozilla_info.targetFile; },
163 get amount_transferred () { return this.mozilla_info.amountTransferred; },
164 get percent_complete () { return this.mozilla_info.percentComplete; },
166 var s = this.mozilla_info.size;
167 /* nsIDownload.size is a PRUint64, and will have value
168 * LL_MAXUINT (2^64 - 1) to indicate an unknown size. Because
169 * JavaScript only has a double numerical type, this value
170 * cannot be represented exactly, so 2^36 is used instead as the cutoff. */
171 if (s < 68719476736 /* 2^36 */)
175 get source () { return this.mozilla_info.source; },
176 get start_time () { return this.mozilla_info.startTime; },
177 get speed () { return this.mozilla_info.speed; },
178 get MIME_info () { return this.mozilla_info.MIMEInfo; },
181 return this.MIME_info.MIMEType;
184 get id () { return this.mozilla_info.id; },
185 get referrer () { return this.mozilla_info.referrer; },
187 throw_if_removed : function () {
189 throw interactive_error("Download has already been removed from the download manager.");
192 throw_state_error : function () {
193 switch (this.state) {
194 case DOWNLOAD_DOWNLOADING:
195 throw interactive_error("Download is already in progress.");
196 case DOWNLOAD_FINISHED:
197 throw interactive_error("Download has already completed.");
198 case DOWNLOAD_FAILED:
199 throw interactive_error("Download has already failed.");
200 case DOWNLOAD_CANCELED:
201 throw interactive_error("Download has already been canceled.");
202 case DOWNLOAD_PAUSED:
203 throw interactive_error("Download has already been paused.");
204 case DOWNLOAD_QUEUED:
205 throw interactive_error("Download is queued.");
207 throw new Error("Download has unexpected state: " + this.state);
211 // Download manager operations
212 cancel : function () {
213 this.throw_if_removed();
214 switch (this.state) {
215 case DOWNLOAD_DOWNLOADING:
216 case DOWNLOAD_PAUSED:
217 case DOWNLOAD_QUEUED:
219 download_manager_service.cancelDownload(this.id);
221 throw interactive_error("Download cannot be canceled.");
225 this.throw_state_error();
229 retry : function () {
230 this.throw_if_removed();
231 switch (this.state) {
232 case DOWNLOAD_CANCELED:
233 case DOWNLOAD_FAILED:
235 download_manager_service.retryDownload(this.id);
237 throw interactive_error("Download cannot be retried.");
241 this.throw_state_error();
245 resume : function () {
246 this.throw_if_removed();
247 switch (this.state) {
248 case DOWNLOAD_PAUSED:
250 download_manager_service.resumeDownload(this.id);
252 throw interactive_error("Download cannot be resumed.");
256 this.throw_state_error();
260 pause : function () {
261 this.throw_if_removed();
262 switch (this.state) {
263 case DOWNLOAD_DOWNLOADING:
264 case DOWNLOAD_QUEUED:
266 download_manager_service.pauseDownload(this.id);
268 throw interactive_error("Download cannot be paused.");
272 this.throw_state_error();
276 remove : function () {
277 this.throw_if_removed();
278 switch (this.state) {
279 case DOWNLOAD_FAILED:
280 case DOWNLOAD_CANCELED:
281 case DOWNLOAD_FINISHED:
283 download_manager_service.removeDownload(this.id);
285 throw interactive_error("Download cannot be removed.");
289 throw interactive_error("Download is still in progress.");
293 delete_target : function () {
294 if (this.state != DOWNLOAD_FINISHED)
295 throw interactive_error("Download has not finished.");
297 this.target_file.remove(false);
301 case Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST:
302 throw interactive_error("File has already been deleted.");
303 case Cr.NS_ERROR_FILE_ACCESS_DENIED:
304 throw interactive_error("Access denied");
305 case Cr.NS_ERROR_FILE_DIR_NOT_EMPTY:
306 throw interactive_error("Failed to delete file.");
314 var define_download_local_hook = simple_local_hook_definer();
316 // FIXME: add more parameters
317 function register_download(buffer, source_uri) {
318 var info = new download_info(buffer);
319 info.registered_time_stamp = Date.now();
320 info.registered_source_uri = source_uri;
321 unmanaged_download_info_list.push(info);
325 function match_registered_download(mozilla_info) {
326 let list = unmanaged_download_info_list;
328 for (let i = 0; i < list.length; ++i) {
330 if (x.registered_source_uri == mozilla_info.source) {
334 if (t - x.registered_time_stamp > download_info_max_queue_delay) {
343 define_download_local_hook("download_added_hook");
344 define_download_local_hook("download_removed_hook");
345 define_download_local_hook("download_finished_hook");
346 define_download_local_hook("download_progress_change_hook");
347 define_download_local_hook("download_state_change_hook");
348 define_download_local_hook("download_shell_command_change_hook");
350 var download_info_max_queue_delay = 100;
352 var download_progress_listener = {
353 QueryInterface: generate_QI(Ci.nsIDownloadProgressListener),
355 onDownloadStateChange : function (state, download) {
357 /* FIXME: Determine if only new downloads will have this state
358 * as their previous state. */
360 dumpln("download state change: " + download.source.spec + ": " + state + ", " + download.state + ", " + download.id);
362 if (state == DOWNLOAD_NOTSTARTED) {
363 info = match_registered_download(download);
365 info = new download_info(null, download);
366 dumpln("error: encountered unknown new download");
368 info.attach(download);
371 info = id_to_download_info[download.id];
373 dumpln("Error: encountered unknown download");
376 info.mozilla_info = download;
377 download_state_change_hook.run(info);
378 if (info.state == DOWNLOAD_FINISHED) {
379 download_finished_hook.run(info);
381 if (info.shell_command != null) {
382 info.running_shell_command = true;
383 co_call(function () {
385 yield shell_command_with_argument(info.shell_command,
386 info.target_file.path,
387 $cwd = info.shell_command_cwd);
389 if (info.temporary_status == DOWNLOAD_TEMPORARY_FOR_COMMAND)
390 info.target_file.remove(false /* not recursive */);
391 info.running_shell_command = false;
392 download_shell_command_change_hook.run(info);
395 download_shell_command_change_hook.run(info);
402 onProgressChange : function (progress, request, cur_self_progress, max_self_progress,
403 cur_total_progress, max_total_progress,
405 var info = id_to_download_info[download.id];
407 dumpln("error: encountered unknown download in progress change");
410 info.mozilla_info = download;
411 download_progress_change_hook.run(info);
412 //dumpln("download progress change: " + download.source.spec + ": " + cur_self_progress + "/" + max_self_progress + " "
413 // + cur_total_progress + "/" + max_total_progress + ", " + download.state + ", " + download.id);
416 onSecurityChange : function (progress, request, state, download) {
420 var download_observer = {
421 observe : function(subject, topic, data) {
423 case "download-manager-remove-download":
426 // Remove all downloads
427 for (let i in id_to_download_info)
430 let id = subject.QueryInterface(Ci.nsISupportsPRUint32);
431 /* FIXME: determine if this should really be an error */
432 if (!(id in id_to_download_info)) {
433 dumpln("Error: download-manager-remove-download event received for unknown download: " + id);
437 for each (let i in ids) {
438 dumpln("deleting download: " + i);
439 let d = id_to_download_info[i];
441 download_removed_hook.run(d);
442 delete id_to_download_info[i];
448 observer_service.addObserver(download_observer, "download-manager-remove-download", false);
450 download_manager_service.addListener(download_progress_listener);
452 function pretty_print_file_size(val) {
453 const GIBI = 1073741824; /* 2^30 */
454 const MEBI = 1048576; /* 2^20 */
455 const KIBI = 1024; /* 2^10 */
461 else if (val < MEBI) {
464 } else if (val < GIBI) {
477 return [val.toFixed(precision), suffix];
480 function pretty_print_time(val) {
481 val = Math.round(val);
482 var seconds = val % 60;
484 val = Math.floor(val / 60);
486 var minutes = val % 60;
488 var hours = Math.floor(val / 60);
493 parts.push(hours + " hours");
495 parts.push("1 hour");
498 parts.push(minutes + " minutes");
499 else if (minutes == 1)
500 parts.push("1 minute");
502 if (minutes <= 1 && hours == 0) {
504 parts.push(seconds + " seconds");
506 parts.push("1 second");
509 return parts.join(", ");
513 "download_buffer_min_update_interval", 2000,
514 "Minimum interval (in milliseconds) between updates in download progress buffers.\n" +
515 "Lowering this interval will increase the promptness of the progress display at " +
516 "the cost of using additional processor time.");
518 define_keywords("$info");
519 function download_buffer(window, element) {
520 this.constructor_begin();
522 special_buffer.call(this, window, element, forward_keywords(arguments));
523 this.info = arguments.$info;
524 this.configuration.cwd = this.info.mozilla_info.targetFile.parent.path;
525 this.description = this.info.mozilla_info.source.spec;
526 this.keymap = download_buffer_keymap;
529 this.progress_change_handler_fn = method_caller(this, this.handle_progress_change);
530 add_hook.call(this.info, "download_progress_change_hook", this.progress_change_handler_fn);
531 add_hook.call(this.info, "download_state_change_hook", this.progress_change_handler_fn);
532 this.command_change_handler_fn = method_caller(this, this.update_command_field);
533 add_hook.call(this.info, "download_shell_command_change_hook", this.command_change_handler_fn);
534 this.constructor_end();
536 download_buffer.prototype = {
537 __proto__: special_buffer.prototype,
539 handle_kill : function () {
540 this.__proto__.handle_kill();
541 remove_hook.call(this.info, "download_progress_change_hook", this.progress_change_handler_fn);
542 remove_hook.call(this.info, "download_state_change_hook", this.progress_change_handler_fn);
543 remove_hook.call(this.info, "download_shell_command_change_hook", this.command_change_handler_fn);
545 // Remove all node references
546 delete this.status_textnode;
547 delete this.transferred_div_node;
548 delete this.transferred_textnode;
549 delete this.progress_container_node;
550 delete this.progress_bar_node;
551 delete this.time_textnode;
552 delete this.command_div_node;
553 delete this.command_label_textnode;
554 delete this.command_textnode;
557 update_title : function () {
558 // FIXME: do this properly
560 var info = this.info;
561 var append_transfer_info = false;
562 var append_speed_info = true;
565 case DOWNLOAD_DOWNLOADING:
566 label = "Downloading";
567 append_transfer_info = true;
569 case DOWNLOAD_FINISHED:
570 label = "Download complete";
572 case DOWNLOAD_FAILED:
573 label = "Download failed";
574 append_transfer_info = true;
575 append_speed_info = false;
577 case DOWNLOAD_CANCELED:
578 label = "Download canceled";
579 append_transfer_info = true;
580 append_speed_info = false;
582 case DOWNLOAD_PAUSED:
583 label = "Download paused";
584 append_transfer_info = true;
585 append_speed_info = false;
587 case DOWNLOAD_QUEUED:
589 label = "Download queued";
593 if (append_transfer_info) {
594 if (append_speed_info)
595 new_title = label + " at " + pretty_print_file_size(info.speed).join(" ") + "/s: ";
597 new_title = label + ": ";
598 var trans = pretty_print_file_size(info.amount_transferred);
599 if (info.size >= 0) {
600 var total = pretty_print_file_size(info.size);
601 if (trans[1] == total[1])
602 new_title += trans[0] + "/" + total[0] + " " + total[1];
604 new_title += trans.join(" ") + "/" + total.join(" ");
606 new_title += trans.join(" ");
607 if (info.percent_complete >= 0)
608 new_title += " (" + info.percent_complete + "%)";
611 if (new_title != this.title) {
612 this.title = new_title;
618 handle_progress_change : function () {
619 var cur_time = Date.now();
620 if (this.last_update == null ||
621 (cur_time - this.last_update) > download_buffer_min_update_interval ||
622 this.info.state != this.previous_state) {
624 if (this.update_title())
625 buffer_title_change_hook.run(this);
627 if (this.generated) {
628 this.update_fields();
630 this.previous_status = this.info.status;
631 this.last_update = cur_time;
635 generate: function() {
636 var d = this.document;
637 var g = new dom_generator(d, XHTML_NS);
639 /* Warning: If any additional node references are saved in
640 * this function, appropriate code to delete the saved
641 * properties must be added to handle_kill. */
643 var info = this.info;
645 d.body.setAttribute("class", "download-buffer");
647 g.add_stylesheet("chrome://conkeror/content/downloads.css");
652 div = g.element("div", d.body, "class", "download-info", "id", "download-source");
653 label = g.element("div", div, "class", "download-label");
654 this.status_textnode = g.text("", label);
655 value = g.element("div", div, "class", "download-value");
656 g.text(info.source.spec, value);
658 div = g.element("div", d.body, "class", "download-info", "id", "download-target");
659 label = g.element("div", div, "class", "download-label");
661 if (info.temporary_status != DOWNLOAD_NOT_TEMPORARY)
662 target_label = "Temp. file:";
664 target_label = "Target:";
665 g.text(target_label, label);
666 value = g.element("div", div, "class", "download-value");
667 g.text(info.target_file.path, value);
669 div = g.element("div", d.body, "class", "download-info", "id", "download-mime-type");
670 label = g.element("div", div, "class", "download-label");
671 g.text("MIME type:", label);
672 value = g.element("div", div, "class", "download-value");
673 g.text(info.MIME_type || "unknown", value);
675 this.transferred_div_node = div = g.element("div", d.body,
676 "class", "download-info",
677 "id", "download-transferred");
678 label = g.element("div", div, "class", "download-label");
679 g.text("Transferred:", label);
680 value = g.element("div", div, "class", "download-value");
681 this.transferred_textnode = g.text("", value);
682 this.progress_container_node = value = g.element("div", div, "id", "download-progress-container");
683 this.progress_bar_node = g.element("div", value, "id", "download-progress-bar");
684 value = g.element("div", div, "class", "download-value", "id", "download-percent");
685 this.percent_textnode = g.text("", value);
687 div = g.element("div", d.body, "class", "download-info", "id", "download-time");
688 label = g.element("div", div, "class", "download-label");
689 g.text("Time:", label);
690 value = g.element("div", div, "class", "download-value");
691 this.time_textnode = g.text("", value);
693 if (info.action_description != null) {
694 div = g.element("div", d.body, "class", "download-info", "id", "download-action");
695 label = g.element("div", div, "class", "download-label");
696 g.text("Action:", label);
697 value = g.element("div", div, "class", "download-value");
698 g.text(info.action_description, value);
701 this.command_div_node = div = g.element("div", d.body, "class", "download-info", "id", "download-command");
702 label = g.element("div", div, "class", "download-label");
703 this.command_label_textnode = g.text("Run command:", label);
704 value = g.element("div", div, "class", "download-value");
705 this.command_textnode = g.text("", value);
707 this.update_fields();
709 this.update_command_field();
712 update_fields : function () {
715 var info = this.info;
718 case DOWNLOAD_DOWNLOADING:
719 label = "Downloading";
721 case DOWNLOAD_FINISHED:
724 case DOWNLOAD_FAILED:
727 case DOWNLOAD_CANCELED:
730 case DOWNLOAD_PAUSED:
733 case DOWNLOAD_QUEUED:
738 this.status_textnode.nodeValue = label + ":";
739 this.update_time_field();
742 if (info.state == DOWNLOAD_FINISHED)
743 tran_text = pretty_print_file_size(info.size).join(" ");
745 var trans = pretty_print_file_size(info.amount_transferred);
746 if (info.size >= 0) {
747 var total = pretty_print_file_size(info.size);
748 if (trans[1] == total[1])
749 tran_text += trans[0] + "/" + total[0] + " " + total[1];
751 tran_text += trans.join(" ") + "/" + total.join(" ");
753 tran_text += trans.join(" ");
755 this.transferred_textnode.nodeValue = tran_text;
756 if (info.percent_complete >= 0) {
757 this.progress_container_node.style.display = "";
758 this.percent_textnode.nodeValue = info.percent_complete + "%";
759 this.progress_bar_node.style.width = info.percent_complete + "%";
761 this.percent_textnode.nodeValue = "";
762 this.progress_container_node.style.display = "none";
765 this.update_command_field();
768 update_time_field : function () {
769 var info = this.info;
770 var elapsed_text = pretty_print_time((Date.now() - info.start_time / 1000) / 1000) + " elapsed";
772 if (info.state == DOWNLOAD_DOWNLOADING) {
773 text = pretty_print_file_size(info.speed).join(" ") + "/s, ";
775 if (info.state == DOWNLOAD_DOWNLOADING &&
778 let remaining = (info.size - info.amount_transferred) / info.speed;
779 text += pretty_print_time(remaining) + " left (" + elapsed_text + ")";
783 this.time_textnode.nodeValue = text;
786 update_command_field : function () {
789 if (this.info.shell_command != null) {
790 this.command_div_node.style.display = "";
792 if (this.info.running_shell_command)
794 else if (this.info.state == DOWNLOAD_FINISHED)
795 label = "Ran command:";
797 label = "Run command:";
798 this.command_label_textnode.nodeValue = label;
799 this.command_textnode.nodeValue = this.info.shell_command;
801 this.command_div_node.style.display = "none";
806 function download_cancel(buffer) {
807 check_buffer(buffer, download_buffer);
808 var info = buffer.info;
810 buffer.window.minibuffer.message("Download canceled");
812 interactive("download-cancel",
813 "Cancel the current download.\n" +
814 "The download can later be retried using the `download-retry' command, but any " +
815 "data already transferred will be lost.",
816 function (I) {download_cancel(I.buffer);});
818 function download_retry(buffer) {
819 check_buffer(buffer, download_buffer);
820 var info = buffer.info;
822 buffer.window.minibuffer.message("Download retried");
824 interactive("download-retry",
825 "Retry a failed or canceled download.\n" +
826 "This command can be used to retry a download that failed or was cancled using " +
827 "the `download-cancel' command. The download will begin from the start again.",
828 function (I) {download_retry(I.buffer);});
830 function download_pause(buffer) {
831 check_buffer(buffer, download_buffer);
833 buffer.window.minibuffer.message("Download paused");
835 interactive("download-pause",
836 "Pause the current download.\n" +
837 "The download can later be resumed using the `download-resume' command. The " +
838 "data already transferred will not be lost.",
839 function (I) {download_pause(I.buffer);});
841 function download_resume(buffer) {
842 check_buffer(buffer, download_buffer);
843 buffer.info.resume();
844 buffer.window.minibuffer.message("Download resumed");
846 interactive("download-resume",
847 "Resume the current download.\n" +
848 "This command can be used to resume a download paused using the `download-pause' command.",
849 function (I) {download_resume(I.buffer);});
851 function download_remove(buffer) {
852 check_buffer(buffer, download_buffer);
853 buffer.info.remove();
854 buffer.window.minibuffer.message("Download removed");
856 interactive("download-remove",
857 "Remove the current download from the download manager.\n" +
858 "This command can only be used on inactive (paused, canceled, completed, or failed) downloads.",
859 function (I) {download_remove(I.buffer);});
861 function download_retry_or_resume(buffer) {
862 check_buffer(buffer, download_buffer);
863 var info = buffer.info;
864 if (info.state == DOWNLOAD_PAUSED)
865 download_resume(buffer);
867 download_retry(buffer);
869 interactive("download-retry-or-resume",
870 "Retry or resume the current download.\n" +
871 "This command can be used to resume a download paused using the `download-pause' " +
872 "command or canceled using the `download-cancel' command.",
873 function (I) {download_retry_or_resume(I.buffer);});
875 function download_pause_or_resume(buffer) {
876 check_buffer(buffer, download_buffer);
877 var info = buffer.info;
878 if (info.state == DOWNLOAD_PAUSED)
879 download_resume(buffer);
881 download_pause(buffer);
883 interactive("download-pause-or-resume",
884 "Pause or resume the current download.\n" +
885 "This command toggles the paused state of the current download.",
886 function (I) {download_pause_or_resume(I.buffer);});
888 function download_delete_target(buffer) {
889 check_buffer(buffer, download_buffer);
890 var info = buffer.info;
891 info.delete_target();
892 buffer.window.minibuffer.message("Deleted file: " + info.target_file.path);
894 interactive("download-delete-target",
895 "Delete the target file of the current download.\n" +
896 "This command can only be used if the download has finished successfully.",
897 function (I) {download_delete_target(I.buffer);});
899 function download_shell_command(buffer, cwd, cmd) {
900 check_buffer(buffer, download_buffer);
901 var info = buffer.info;
902 if (info.state == DOWNLOAD_FINISHED) {
903 shell_command_with_argument_blind(cmd, info.target_file.path, $cwd = cwd);
906 if (info.state != DOWNLOAD_DOWNLOADING && info.state != DOWNLOAD_PAUSED && info.state != DOWNLOAD_QUEUED)
907 info.throw_state_error();
908 if (cmd == null || cmd.length == 0)
909 info.set_shell_command(null, cwd);
911 info.set_shell_command(cmd, cwd);
912 buffer.window.minibuffer.message("Queued shell command: " + cmd);
914 interactive("download-shell-command",
915 "Run a shell command on the target file of the current download.\n" +
916 "If the download is still in progress, the shell command will be queued " +
917 "to run when the download finishes.",
919 var buffer = check_buffer(I.buffer, download_buffer);
920 var cwd = buffer.info.shell_command_cwd || buffer.cwd;
921 var cmd = yield I.minibuffer.read_shell_command(
923 $initial_value = buffer.info.shell_command ||
924 get_external_handler_for_mime_type(buffer.info.MIME_type));
925 download_shell_command(buffer, cwd, cmd);
928 function download_manager_ui()
930 download_manager_ui.prototype = {
931 QueryInterface : XPCOMUtils.generateQI([Ci.nsIDownloadManagerUI]),
933 getAttention : function () {},
934 show : function () {},
939 function download_manager_show_builtin_ui(window) {
940 download_manager_builtin_ui.show(window);
942 interactive("download-manager-show-builtin-ui",
943 "Show the built-in (Firefox-style) download manager user interface.",
944 function (I) {download_manager_show_builtin_ui(I.window);});
948 define_variable("download_temporary_file_open_buffer_delay", 500,
949 "Delay (in milliseconds) before a download buffer is opened for temporary downloads.\n" +
950 "This variable takes effect only if `open_download_buffer_automatically' is in " +
951 "`download_added_hook', as it is by default.");
954 define_variable("download_buffer_automatic_open_target", OPEN_NEW_WINDOW,
955 "Target for download buffers created by the `open_download_buffer_automatically' function.\n" +
956 "This variable takes effect only if `open_download_buffer_auotmatically' is in " +
957 "`download_added_hook', as it is by default.");
959 function open_download_buffer_automatically(info) {
960 var buf = info.source_buffer;
961 var target = download_buffer_automatic_open_target;
963 target = OPEN_NEW_WINDOW;
964 if (info.temporary_status == DOWNLOAD_NOT_TEMPORARY ||
965 !(download_temporary_file_open_buffer_delay > 0))
966 create_buffer(buf, buffer_creator(download_buffer, $info = info), target);
972 add_hook.call(info, "download_finished_hook", finish);
973 timer = call_after_timeout(function () {
974 remove_hook.call(info, "download_finished_hook", finish);
975 create_buffer(buf, buffer_creator(download_buffer, $info = info), target);
976 }, download_temporary_file_open_buffer_delay);
979 add_hook("download_added_hook", open_download_buffer_automatically);