Add initial OpenSearch search engine support
[conkeror.git] / modules / download-manager.js
blobccb8204c278b29f58612d1bdc90db104ea968a4f
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()
9 {}
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]]);
17     },
19     handle_show: function () {
20         var action_chosen = false;
21         try {
22             this.create_panel();
23             var action = yield this.window.minibuffer.read_single_character_option(
24                 $prompt = "Action to perform: (save or open)",
25                 $options = ["s", "o"]);
28             if (action == "s") {
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,
33                     $select);
34                 register_download(this.buffer, this.launcher.source);
35                 this.launcher.saveToDisk(file, false);
36                 action_chosen = true;
38             } else {
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,
44                     $cwd = cwd);
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);
50                 action_chosen = true;
51             }
52         } catch (e) {
53             handle_interactive_error(this.window, e);
54         } finally {
55             if (action_chosen)
56                 this.cleanup();
57             else
58                 this.abort();
59         }
60     },
62     show : function (launcher, context, reason) {
63         this.launcher = launcher;
65         // Get associated buffer; if that fails (hopefully not), just get any window
66         var buffer = null;
67         var window = null;
68         try {
69             var frame = context.QueryInterface(Ci.nsIWebProgress).DOMWindow.top;
70             window = get_window_from_frame(frame);
71             if (window)
72                 buffer = get_buffer_from_frame(window, frame);
73         } catch (e) {
74             window = get_recent_conkeror_window();
76             if (window == null) {
77                 // FIXME: need to handle this case perhaps where no windows exist
78                 this.abort(); // for now, just cancel the download
79                 return;
80             }
81         }
83         this.window = window;
84         this.buffer = buffer;
86         co_call(this.handle_show());
87     },
89     abort : function () {
90         const NS_BINDING_ABORTED = 0x804b0002;
91         this.launcher.cancel(NS_BINDING_ABORTED);
92         this.cleanup();
93     },
95     cleanup : function () {
96         if (this.panel)
97             this.panel.destroy();
98         this.panel = null;
99         this.launcher = null;
100         this.window = null;
101         this.buffer = null;
102     },
104     promptForSaveToFile : function(launcher, context, default_file, suggested_file_extension) {
105         return null;
106     }
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);
139     },
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);
154     },
156     /**
157      * None of the following members may be used until attach is called
158      */
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; },
165     get size () {
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 */)
172             return s;
173         return -1;
174     },
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; },
179     get MIME_type () {
180         if (this.MIME_info)
181             return this.MIME_info.MIMEType;
182         return null;
183     },
184     get id () { return this.mozilla_info.id; },
185     get referrer () { return this.mozilla_info.referrer; },
187     throw_if_removed : function () {
188         if (this.removed)
189             throw interactive_error("Download has already been removed from the download manager.");
190     },
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.");
206         default:
207             throw new Error("Download has unexpected state: " + this.state);
208         }
209     },
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:
218             try {
219                 download_manager_service.cancelDownload(this.id);
220             } catch (e) {
221                 throw interactive_error("Download cannot be canceled.");
222             }
223             break;
224         default:
225             this.throw_state_error();
226         }
227     },
229     retry : function () {
230         this.throw_if_removed();
231         switch (this.state) {
232         case DOWNLOAD_CANCELED:
233         case DOWNLOAD_FAILED:
234             try {
235                 download_manager_service.retryDownload(this.id);
236             } catch (e) {
237                 throw interactive_error("Download cannot be retried.");
238             }
239             break;
240         default:
241             this.throw_state_error();
242         }
243     },
245     resume : function () {
246         this.throw_if_removed();
247         switch (this.state) {
248         case DOWNLOAD_PAUSED:
249             try {
250                 download_manager_service.resumeDownload(this.id);
251             } catch (e) {
252                 throw interactive_error("Download cannot be resumed.");
253             }
254             break;
255         default:
256             this.throw_state_error();
257         }
258     },
260     pause : function () {
261         this.throw_if_removed();
262         switch (this.state) {
263         case DOWNLOAD_DOWNLOADING:
264         case DOWNLOAD_QUEUED:
265             try {
266                 download_manager_service.pauseDownload(this.id);
267             } catch (e) {
268                 throw interactive_error("Download cannot be paused.");
269             }
270             break;
271         default:
272             this.throw_state_error();
273         }
274     },
276     remove : function () {
277         this.throw_if_removed();
278         switch (this.state) {
279         case DOWNLOAD_FAILED:
280         case DOWNLOAD_CANCELED:
281         case DOWNLOAD_FINISHED:
282             try {
283                 download_manager_service.removeDownload(this.id);
284             } catch (e) {
285                 throw interactive_error("Download cannot be removed.");
286             }
287             break;
288         default:
289             throw interactive_error("Download is still in progress.");
290         }
291     },
293     delete_target : function () {
294         if (this.state != DOWNLOAD_FINISHED)
295             throw interactive_error("Download has not finished.");
296         try {
297             this.target_file.remove(false);
298         } catch (e) {
299             if (result in e) {
300                 switch (e) {
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.");
307                 }
308             }
309             throw e;
310         }
311     }
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);
322     return info;
325 function match_registered_download(mozilla_info) {
326     let list = unmanaged_download_info_list;
327     let t = Date.now();
328     for (let i = 0; i < list.length; ++i) {
329         let x = list[i];
330         if (x.registered_source_uri == mozilla_info.source) {
331             list.splice(i, 1);
332             return x;
333         }
334         if (t - x.registered_time_stamp > download_info_max_queue_delay) {
335             list.splice(i, 1);
336             --i;
337             continue;
338         }
339     }
340     return null;
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) {
356         var info = null;
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);
364             if (info == null) {
365                 info = new download_info(null, download);
366                 dumpln("error: encountered unknown new download");
367             } else {
368                 info.attach(download);
369             }
370         } else {
371             info = id_to_download_info[download.id];
372             if (info == null) {
373                 dumpln("Error: encountered unknown download");
375             } else {
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 () {
384                             try {
385                                 yield shell_command_with_argument(info.shell_command,
386                                                                   info.target_file.path,
387                                                                   $cwd = info.shell_command_cwd);
388                             } finally  {
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);
393                             }
394                         }());
395                         download_shell_command_change_hook.run(info);
396                     }
397                 }
398             }
399         }
400     },
402     onProgressChange : function (progress, request, cur_self_progress, max_self_progress,
403                                  cur_total_progress, max_total_progress,
404                                  download) {
405         var info = id_to_download_info[download.id];
406         if (info == null) {
407             dumpln("error: encountered unknown download in progress change");
408             return;
409         }
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);
414     },
416     onSecurityChange : function (progress, request, state, download) {
417     }
420 var download_observer = {
421     observe : function(subject, topic, data) {
422         switch(topic) {
423         case "download-manager-remove-download":
424             var ids = [];
425             if (!subject) {
426                 // Remove all downloads
427                 for (let i in id_to_download_info)
428                     ids.push(i);
429             } else {
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);
434                 } else
435                     ids.push(id);
436             }
437             for each (let i in ids) {
438                 dumpln("deleting download: " + i);
439                 let d = id_to_download_info[i];
440                 d.removed = true;
441                 download_removed_hook.run(d);
442                 delete id_to_download_info[i];
443             }
444             break;
445         }
446     }
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 */
456     var suffix, div;
457     if (val < KIBI) {
458         div = 1;
459         suffix = "B";
460     }
461     else if (val < MEBI) {
462         suffix = "KiB";
463         div = KIBI;
464     } else if (val < GIBI) {
465         suffix = "MiB";
466         div = MEBI;
467     } else {
468         suffix = "GiB";
469         div = GIBI;
470     }
471     val = val / div;
472     var precision = 2;
473     if (val > 10)
474         precision = 1;
475     if (val > 100)
476         precision = 0;
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);
489     
490     var parts = [];
492     if (hours > 1)
493         parts.push(hours + " hours");
494     else if (hours == 1)
495         parts.push("1 hour");
497     if (minutes > 1)
498         parts.push(minutes + " minutes");
499     else if (minutes == 1)
500         parts.push("1 minute");
502     if (minutes <= 1 && hours == 0) {
503         if (seconds != 1)
504             parts.push(seconds + " seconds");
505         else
506             parts.push("1 second");
507     }
509     return parts.join(", ");
512 define_variable(
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();
521     keywords(arguments);
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;
527     this.update_title();
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;
555     },
557     update_title : function () {
558         // FIXME: do this properly
559         var new_title;
560         var info = this.info;
561         var append_transfer_info = false;
562         var append_speed_info = true;
563         var label = null;
564         switch(info.state) {
565         case DOWNLOAD_DOWNLOADING:
566             label = "Downloading";
567             append_transfer_info = true;
568             break;
569         case DOWNLOAD_FINISHED:
570             label = "Download complete";
571             break;
572         case DOWNLOAD_FAILED:
573             label = "Download failed";
574             append_transfer_info = true;
575             append_speed_info = false;
576             break;
577         case DOWNLOAD_CANCELED:
578             label = "Download canceled";
579             append_transfer_info = true;
580             append_speed_info = false;
581             break;
582         case DOWNLOAD_PAUSED:
583             label = "Download paused";
584             append_transfer_info = true;
585             append_speed_info = false;
586             break;
587         case DOWNLOAD_QUEUED:
588         default:
589             label = "Download queued";
590             break;
591         }
592         
593         if (append_transfer_info) {
594             if (append_speed_info)
595                 new_title = label + " at " + pretty_print_file_size(info.speed).join(" ") + "/s: ";
596             else
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];
603                 else
604                     new_title += trans.join(" ") + "/" + total.join(" ");
605             } else
606                 new_title += trans.join(" ");
607             if (info.percent_complete >= 0)
608                 new_title += " (" + info.percent_complete + "%)";
609         } else
610             new_title = label;
611         if (new_title != this.title) {
612             this.title = new_title;
613             return true;
614         }
615         return false;
616     },
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) {
623             
624             if (this.update_title())
625                 buffer_title_change_hook.run(this);
627             if (this.generated) {
628                 this.update_fields();
629             }
630             this.previous_status = this.info.status;
631             this.last_update = cur_time;
632         }
633     },
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");
649         var div;
650         var label, value;
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");
660         var target_label;
661         if (info.temporary_status != DOWNLOAD_NOT_TEMPORARY)
662             target_label = "Temp. file:";
663         else
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);
699         }
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();
710     },
712     update_fields : function () {
713         if (!this.generated)
714             return;
715         var info = this.info;
716         var label = null;
717         switch(info.state) {
718         case DOWNLOAD_DOWNLOADING:
719             label = "Downloading";
720             break;
721         case DOWNLOAD_FINISHED:
722             label = "Completed";
723             break;
724         case DOWNLOAD_FAILED:
725             label = "Failed";
726             break;
727         case DOWNLOAD_CANCELED:
728             label = "Canceled";
729             break;
730         case DOWNLOAD_PAUSED:
731             label = "Paused";
732             break;
733         case DOWNLOAD_QUEUED:
734         default:
735             label = "Queued";
736             break;
737         }
738         this.status_textnode.nodeValue = label + ":";
739         this.update_time_field();
741         var tran_text = "";
742         if (info.state == DOWNLOAD_FINISHED)
743             tran_text = pretty_print_file_size(info.size).join(" ");
744         else {
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];
750                 else
751                     tran_text += trans.join(" ") + "/" + total.join(" ");
752             } else
753                 tran_text += trans.join(" ");
754         }
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 + "%";
760         } else {
761             this.percent_textnode.nodeValue = "";
762             this.progress_container_node.style.display = "none";
763         }
764         
765         this.update_command_field();
766     },
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";
771         var text = "";
772         if (info.state == DOWNLOAD_DOWNLOADING) {
773             text = pretty_print_file_size(info.speed).join(" ") + "/s, ";
774         }
775         if (info.state == DOWNLOAD_DOWNLOADING &&
776             info.size >= 0 &&
777             info.speed > 0) {
778             let remaining = (info.size - info.amount_transferred) / info.speed;
779             text += pretty_print_time(remaining) + " left (" + elapsed_text + ")";
780         } else {
781             text = elapsed_text;
782         }
783         this.time_textnode.nodeValue = text;
784     },
786     update_command_field : function () {
787         if (!this.generated)
788             return;
789         if (this.info.shell_command != null) {
790             this.command_div_node.style.display = "";
791             var label;
792             if (this.info.running_shell_command)
793                 label = "Running:";
794             else if (this.info.state == DOWNLOAD_FINISHED)
795                 label = "Ran command:";
796             else
797                 label = "Run command:";
798             this.command_label_textnode.nodeValue = label;
799             this.command_textnode.nodeValue = this.info.shell_command;
800         } else {
801             this.command_div_node.style.display = "none";
802         }
803     }
806 function download_cancel(buffer) {
807     check_buffer(buffer, download_buffer);
808     var info = buffer.info;
809     info.cancel();
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;
821     info.retry();
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);
832     buffer.info.pause();
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);
866     else
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);
880     else
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);
904         return;
905     }
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);
910     else
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.",
918             function (I) {
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(
922                     $cwd = cwd,
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);
926             });
928 function download_manager_ui()
930 download_manager_ui.prototype = {
931     QueryInterface : XPCOMUtils.generateQI([Ci.nsIDownloadManagerUI]),
933     getAttention : function () {},
934     show : function () {},
935     visible : false
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;
962     if (buf == null)
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);
967     else {
968         var timer = null;
969         function finish() {
970             timer.cancel();
971         }
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);
977     }
979 add_hook("download_added_hook", open_download_buffer_automatically);