home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / phoenx05.zip / phoenix / components / nsProgressDialog.js < prev    next >
Text File  |  2002-12-10  |  34KB  |  856 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Mozilla Progress Dialog.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Netscape Communications Corp.
  18.  * Portions created by the Initial Developer are Copyright (C) 2002
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Bill Law       <law@netscape.com>
  23.  *   Aaron Kaluszka <ask@swva.net>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. /* This file implements the nsIProgressDialog interface.  See nsIProgressDialog.idl
  40.  *
  41.  * The implementation consists of a JavaScript "class" named nsProgressDialog,
  42.  * comprised of:
  43.  *   - a JS constructor function
  44.  *   - a prototype providing all the interface methods and implementation stuff
  45.  *
  46.  * In addition, this file implements an nsIModule object that registers the
  47.  * nsProgressDialog component.
  48.  */
  49.  
  50. /* ctor
  51.  */
  52. function nsProgressDialog() {
  53.     // Initialize data properties.
  54.     this.mParent      = null;
  55.     this.mOperation   = null;
  56.     this.mStartTime   = ( new Date() ).getTime();
  57.     this.observer     = null;
  58.     this.mLastUpdate  = Number.MIN_VALUE; // To ensure first onProgress causes update.
  59.     this.mInterval    = 750; // Default to .75 seconds.
  60.     this.mElapsed     = 0;
  61.     this.mLoaded      = false;
  62.     this.fields       = new Array;
  63.     this.strings      = new Array;
  64.     this.mSource      = null;
  65.     this.mTarget      = null;
  66.     this.mMIMEInfo    = null;
  67.     this.mDialog      = null;
  68.     this.mDisplayName = null;
  69.     this.mPaused      = null;
  70.     this.mRequest     = null;
  71.     this.mCompleted   = false;
  72.     this.mMode        = "normal";
  73.     this.mPercent     = 0;
  74.     this.mRate        = 0;
  75.     this.mBundle      = null;
  76.     this.mCancelDownloadOnClose = true;
  77. }
  78.  
  79. const nsIProgressDialog = Components.interfaces.nsIProgressDialog;
  80.  
  81. nsProgressDialog.prototype = {
  82.     // Turn this on to get debugging messages.
  83.     debug: false,
  84.  
  85.     // Chrome-related constants.
  86.     dialogChrome:   "chrome://global/content/nsProgressDialog.xul",
  87.     dialogFeatures: "chrome,titlebar,minimizable=yes",
  88.  
  89.     // getters/setters
  90.     get saving()            { return this.MIMEInfo == null ||
  91.                   this.MIMEInfo.preferredAction == Components.interfaces.nsIMIMEInfo.saveToDisk; },
  92.     get parent()            { return this.mParent; },
  93.     set parent(newval)      { return this.mParent = newval; },
  94.     get operation()         { return this.mOperation; },
  95.     set operation(newval)   { return this.mOperation = newval; },
  96.     get observer()          { return this.mObserver; },
  97.     set observer(newval)    { return this.mObserver = newval; },
  98.     get startTime()         { return this.mStartTime; },
  99.     set startTime(newval)   { return this.mStartTime = newval/1000; }, // PR_Now() is in microseconds, so we convert.
  100.     get lastUpdate()        { return this.mLastUpdate; },
  101.     set lastUpdate(newval)  { return this.mLastUpdate = newval; },
  102.     get interval()          { return this.mInterval; },
  103.     set interval(newval)    { return this.mInterval = newval; },
  104.     get elapsed()           { return this.mElapsed; },
  105.     set elapsed(newval)     { return this.mElapsed = newval; },
  106.     get loaded()            { return this.mLoaded; },
  107.     set loaded(newval)      { return this.mLoaded = newval; },
  108.     get source()            { return this.mSource; },
  109.     set source(newval)      { return this.mSource = newval; },
  110.     get target()            { return this.mTarget; },
  111.     set target(newval)      { return this.mTarget = newval; },
  112.     get MIMEInfo()          { return this.mMIMEInfo; },
  113.     set MIMEInfo(newval)    { return this.mMIMEInfo = newval; },
  114.     get dialog()            { return this.mDialog; },
  115.     set dialog(newval)      { return this.mDialog = newval; },
  116.     get displayName()       { return this.mDisplayName; },
  117.     set displayName(newval) { return this.mDisplayName = newval; },
  118.     get paused()            { return this.mPaused; },
  119.     get request()           { return this.mRequest; },
  120.     get completed()         { return this.mCompleted; },
  121.     get mode()              { return this.mMode; },
  122.     get percent()           { return this.mPercent; },
  123.     get rate()              { return this.mRate; },
  124.     get kRate()             { return this.mRate / 1024; },
  125.     get cancelDownloadOnClose() { return this.mCancelDownloadOnClose; },
  126.     set cancelDownloadOnClose(newval) { return this.mCancelDownloadOnClose = newval; },
  127.  
  128.     // These setters use functions that update the dialog.
  129.     set paused(newval)      { return this.setPaused(newval); },
  130.     set request(newval)     { return this.setRequest(newval); },
  131.     set completed(newval)   { return this.setCompleted(newval); },
  132.     set mode(newval)        { return this.setMode(newval); },
  133.     set percent(newval)     { return this.setPercent(newval); },
  134.     set rate(newval)        { return this.setRate(newval); },
  135.  
  136.     // ---------- nsIProgressDialog methods ----------
  137.  
  138.     // open: Store aParentWindow and open the dialog.
  139.     open: function( aParentWindow ) {
  140.         // Save parent and "persist" operation.
  141.         this.parent    = aParentWindow;
  142.  
  143.         // Open dialog using the WindowWatcher service.
  144.         var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  145.                    .getService( Components.interfaces.nsIWindowWatcher );
  146.         this.dialog = ww.openWindow( this.parent,
  147.                                      this.dialogChrome,
  148.                                      null,
  149.                                      this.dialogFeatures,
  150.                                      this );
  151.     },
  152.     
  153.     init: function( aSource, aTarget, aDisplayName, aMIMEInfo, aStartTime, aOperation ) {
  154.       this.source = aSource;
  155.       this.target = aTarget;
  156.       this.displayName = aDisplayName;
  157.       this.MIMEInfo = aMIMEInfo;
  158.       if ( aStartTime ) {
  159.           this.startTime = aStartTime;
  160.       }
  161.       this.operation = aOperation;
  162.     },
  163.  
  164.     // ---------- nsIWebProgressListener methods ----------
  165.  
  166.     // Look for STATE_STOP and update dialog to indicate completion when it happens.
  167.     onStateChange: function( aWebProgress, aRequest, aStateFlags, aStatus ) {
  168.         if ( aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP ) {
  169.             // we are done downloading...
  170.             this.completed = true;
  171.         }
  172.     },
  173.  
  174.     // Handle progress notifications.
  175.     onProgressChange: function( aWebProgress,
  176.                                 aRequest,
  177.                                 aCurSelfProgress,
  178.                                 aMaxSelfProgress,
  179.                                 aCurTotalProgress,
  180.                                 aMaxTotalProgress ) {
  181.         // Remember the request; this will also initialize the pause/resume stuff.
  182.         this.request = aRequest;
  183.  
  184.         var overallProgress = aCurTotalProgress;
  185.  
  186.         // Get current time.
  187.         var now = ( new Date() ).getTime();
  188.  
  189.         // If interval hasn't elapsed, ignore it.
  190.         if ( now - this.lastUpdate < this.interval &&
  191.              aMaxTotalProgress != "-1" && 
  192.              parseInt( aCurTotalProgress ) < parseInt( aMaxTotalProgress ) ) {
  193.             return;
  194.         }
  195.  
  196.         // Update this time.
  197.         this.lastUpdate = now;
  198.  
  199.         // Update elapsed time.
  200.         this.elapsed = now - this.startTime;
  201.  
  202.         // Calculate percentage.
  203.         if ( aMaxTotalProgress > 0) {
  204.             this.percent = Math.floor( ( overallProgress * 100.0 ) / aMaxTotalProgress );
  205.         } else {
  206.             this.percent = -1;
  207.         }
  208.  
  209.         // If dialog not loaded, then don't bother trying to update display.
  210.         if ( !this.loaded ) {
  211.             return;
  212.         }
  213.  
  214.         // Update dialog's display of elapsed time.
  215.         this.setValue( "timeElapsed", this.formatSeconds( this.elapsed / 1000 ) );
  216.  
  217.         // Now that we've set the progress and the time, update # bytes downloaded...
  218.         // Update status (nnK of mmK bytes at xx.xK aCurTotalProgress/sec)
  219.         var status = this.getString( "progressMsg" );
  220.  
  221.         // Insert 1 is the number of kilobytes downloaded so far.
  222.         status = this.replaceInsert( status, 1, parseInt( overallProgress/1024 + .5 ) );
  223.  
  224.         // Insert 2 is the total number of kilobytes to be downloaded (if known).
  225.         if ( aMaxTotalProgress != "-1" ) {
  226.             status = this.replaceInsert( status, 2, parseInt( aMaxTotalProgress/1024 + .5 ) );
  227.         } else {
  228.             status = replaceInsert( status, 2, "??" );
  229.         }
  230.     
  231.         // Insert 3 is the download rate.
  232.         if ( this.elapsed ) {
  233.             this.rate = ( aCurTotalProgress * 1000 ) / this.elapsed;
  234.             status = this.replaceInsert( status, 3, this.rateToKRate( this.rate ) );
  235.         } else {
  236.             // Rate not established, yet.
  237.             status = this.replaceInsert( status, 3, "??.?" );
  238.         }
  239.     
  240.         // All 3 inserts are taken care of, now update status msg.
  241.         this.setValue( "status", status );
  242.     
  243.         // Update time remaining.
  244.         if ( this.rate && ( aMaxTotalProgress > 0 ) ) {
  245.             // Calculate how much time to download remaining at this rate.
  246.             var rem = Math.round( ( aMaxTotalProgress - aCurTotalProgress ) / this.rate );
  247.             this.setValue( "timeLeft", this.formatSeconds( rem ) );
  248.         } else {
  249.             // We don't know how much time remains.
  250.             this.setValue( "timeLeft", this.getString( "unknownTime" ) );
  251.         }
  252.     },
  253.  
  254.     // Look for error notifications and display alert to user.
  255.     onStatusChange: function( aWebProgress, aRequest, aStatus, aMessage ) {
  256.         // Check for error condition (only if dialog is still open).
  257.         if ( aStatus != Components.results.NS_OK ) {
  258.             if ( this.loaded ) {
  259.                 // Get prompt service.
  260.                 var prompter = Components.classes[ "@mozilla.org/embedcomp/prompt-service;1" ]
  261.                                    .getService( Components.interfaces.nsIPromptService );
  262.                 // Display error alert (using text supplied by back-end).
  263.                 var title = this.getProperty( this.saving ? "savingAlertTitle" : "openingAlertTitle",
  264.                                               [ this.fileName() ], 
  265.                                               1 );
  266.                 prompter.alert( this.dialog, title, aMessage );
  267.     
  268.                 // Close the dialog.
  269.                 if ( !this.completed ) {
  270.                     this.onCancel();
  271.                 }
  272.             } else {
  273.                 // Error occurred prior to onload even firing.
  274.                 // We can't handle this error until we're done loading, so
  275.                 // defer the handling of this call.
  276.                 this.dialog.setTimeout( function(obj,wp,req,stat,msg){obj.onStatusChange(wp,req,stat,msg)},
  277.                                         100, this, aWebProgress, aRequest, aStatus, aMessage );
  278.             }
  279.         }
  280.     },
  281.  
  282.     // Ignore onLocationChange and onSecurityChange notifications.
  283.     onLocationChange: function( aWebProgress, aRequest, aLocation ) {
  284.     },
  285.  
  286.     onSecurityChange: function( aWebProgress, aRequest, state ) {
  287.     },
  288.  
  289.     // ---------- nsIObserver methods ----------
  290.     observe: function( anObject, aTopic, aData ) {
  291.         // Something of interest occured on the dialog.
  292.         // Dispatch to corresponding implementation method.
  293.         switch ( aTopic ) {
  294.         case "onload":
  295.             this.onLoad();
  296.             break;
  297.         case "oncancel":
  298.             this.onCancel();
  299.             break;
  300.         case "onpause":
  301.             this.onPause();
  302.             break;
  303.         case "onlaunch":
  304.             this.onLaunch();
  305.             break;
  306.         case "onreveal":
  307.             this.onReveal();
  308.             break;
  309.         case "onunload":
  310.             this.onUnload();
  311.             break;
  312.         case "oncompleted":
  313.             // This event comes in when setCompleted needs to be deferred because
  314.             // the dialog isn't loaded yet.
  315.             this.completed = true;
  316.             break;
  317.         default:
  318.             break;
  319.         }
  320.     },
  321.  
  322.     // ---------- nsISupports methods ----------
  323.  
  324.     // This "class" supports nsIProgressDialog, nsIWebProgressListener (by virtue
  325.     // of interface inheritance), nsIObserver, and nsISupports.
  326.     QueryInterface: function (iid) {
  327.         if (!iid.equals(Components.interfaces.nsIProgressDialog) &&
  328.             !iid.equals(Components.interfaces.nsIDownload) && 
  329.             !iid.equals(Components.interfaces.nsIWebProgressListener) &&
  330.             !iid.equals(Components.interfaces.nsIObserver) &&
  331.             !iid.equals(Components.interfaces.nsISupports)) {
  332.             throw Components.results.NS_ERROR_NO_INTERFACE;
  333.         }
  334.         return this;
  335.     },
  336.  
  337.     // ---------- implementation methods ----------
  338.  
  339.     // Initialize the dialog.
  340.     onLoad: function() {
  341.         // Note that onLoad has finished.
  342.         this.loaded = true;
  343.  
  344.         // Fill dialog.
  345.         this.loadDialog();
  346.  
  347.         // Position dialog.
  348.         if ( this.dialog.opener ) {
  349.             this.dialog.moveToAlertPosition();
  350.         } else {
  351.             this.dialog.centerWindowOnScreen();
  352.         }
  353.  
  354.         // Set initial focus on "keep open" box.  If that box is hidden, or, if
  355.         // the download is already complete, then focus is on the cancel/close
  356.         // button.  The download may be complete if it was really short and the
  357.         // dialog took longer to open than to download the data.
  358.         if ( !this.completed && !this.saving ) {
  359.             this.dialogElement( "keep" ).focus();
  360.         } else {
  361.             this.dialogElement( "cancel" ).focus();
  362.         }
  363.     },
  364.  
  365.     // load dialog with initial contents
  366.     loadDialog: function() {
  367.         // Check whether we're saving versus opening with a helper app.
  368.         if ( !this.saving ) {
  369.             // Put proper label on source field.
  370.             this.setValue( "sourceLabel", this.getString( "openingSource" ) );
  371.  
  372.             // Target is the "preferred" application.  Hide if empty.
  373.         if ( this.MIMEInfo && this.MIMEInfo.preferredApplicationHandler ) {
  374.                 var appName = this.MIMEInfo.preferredApplicationHandler.leafName;
  375.                 if ( appName == null || appName.length == 0 ) {
  376.                     this.hide( "targetRow" );
  377.                 } else {
  378.                     // Use the "with:" label.
  379.                     this.setValue( "targetLabel", this.getString( "openingTarget" ) );
  380.                     // Name of application.
  381.                     this.setValue( "target", appName );
  382.                 }
  383.            } else {
  384.                this.hide( "targetRow" );
  385.            }
  386.         } else {
  387.             // Target is the destination file.
  388.             this.setValue( "target", this.target.path );
  389.         }
  390.  
  391.         // Set source field.
  392.         this.setValue( "source", this.source.spec );
  393.  
  394.         var now = ( new Date() ).getTime();
  395.  
  396.         // Intialize the elapsed time.
  397.         if ( !this.elapsed ) {
  398.             this.elapsed = now - this.startTime;
  399.         }
  400.  
  401.         // Update elapsed time display.
  402.         this.setValue( "timeElapsed", this.formatSeconds( this.elapsed / 1000 ) );
  403.         this.setValue( "timeLeft", this.getString( "unknownTime" ) );
  404.  
  405.         // Initialize the "keep open" box.  Hide this if we're opening a helper app.
  406.         if ( !this.saving ) {
  407.             // Hide this in this case.
  408.             this.hide( "keep" );
  409.         } else {
  410.             // Initialize using last-set value from prefs.
  411.             var prefs = Components.classes[ "@mozilla.org/preferences-service;1" ]
  412.                             .getService( Components.interfaces.nsIPrefBranch );
  413.             if ( prefs ) {
  414.                 this.dialogElement( "keep" ).checked = prefs.getBoolPref( "browser.download.progressDnldDialog.keepAlive" );
  415.             }
  416.         }
  417.  
  418.         // Initialize title.
  419.         this.setTitle();
  420.     },
  421.  
  422.     // Cancel button stops the download (if not completed),
  423.     // and closes the dialog.
  424.     onCancel: function() {
  425.          // Cancel the download, if not completed.
  426.          if ( !this.completed ) {
  427.              if ( this.operation ) {
  428.                  this.operation.cancelSave();
  429.                  // XXX We're supposed to clean up files/directories.
  430.              }
  431.              if ( this.observer ) {
  432.                  this.observer.observe( this, "oncancel", "" );
  433.              }
  434.              this.paused = false;
  435.          }
  436.         // Test whether the dialog is already closed.
  437.         // This will be the case if we've come through onUnload.
  438.         if ( this.dialog ) {
  439.             // Close the dialog.
  440.             this.dialog.close();
  441.         }
  442.     },
  443.  
  444.     // onunload event means the dialog has closed.
  445.     // We go through our onCancel logic to stop the download if still in progress.
  446.     onUnload: function() {
  447.         // Remember "keep dialog open" setting, if visible.
  448.         if ( this.saving ) {
  449.             var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  450.                           .getService( Components.interfaces.nsIPrefBranch );
  451.             if ( prefs ) {
  452.                 prefs.setBoolPref( "browser.download.progressDnldDialog.keepAlive", this.dialogElement( "keep" ).checked );
  453.             }
  454.          }
  455.          this.dialog = null; // The dialog is history.
  456.          if ( this.mCancelDownloadOnClose ) {
  457.              this.onCancel();
  458.          }
  459.     },
  460.  
  461.     // onpause event means the user pressed the pause/resume button
  462.     // Toggle the pause/resume state (see the function setPause(), below).i
  463.     onPause: function() {
  464.          this.paused = !this.paused;
  465.     },
  466.  
  467.     // onlaunch event means the user pressed the launch button
  468.     // Invoke the launch method of the target file.
  469.     onLaunch: function() {
  470.          try {
  471.              this.target.launch();
  472.              this.dialog.close();
  473.          } catch ( exception ) {
  474.              // XXX Need code here to tell user the launch failed!
  475.              dump( "nsProgressDialog::onLaunch failed: " + exception + "\n" );
  476.          }
  477.     },
  478.  
  479.     // onreveal event means the user pressed the "reveal location" button
  480.     // Invoke the reveal method of the target file.
  481.     onReveal: function() {
  482.          try {
  483.              this.target.reveal();
  484.              this.dialog.close();
  485.          } catch ( exception ) {
  486.          }
  487.     },
  488.  
  489.     // Get filename from target file.
  490.     fileName: function() {
  491.         return this.target ? this.target.leafName : "";
  492.     },
  493.  
  494.     // Set the dialog title.
  495.     setTitle: function() {
  496.         // Start with saving/opening template.
  497.         // If percentage is not known (-1), use alternate template
  498.         var title = this.saving 
  499.             ? ( this.percent != -1 ? this.getString( "savingTitle" ) : this.getString( "unknownSavingTitle" ) )
  500.             : ( this.percent != -1 ? this.getString( "openingTitle" ) : this.getString( "unknownOpeningTitle" ) );
  501.  
  502.  
  503.         // Use file name as insert 1.
  504.         title = this.replaceInsert( title, 1, this.fileName() );
  505.  
  506.         // Use percentage as insert 2 (if known).
  507.         if ( this.percent != -1 ) {
  508.             title = this.replaceInsert( title, 2, this.percent );
  509.         }
  510.  
  511.         // Set <window>'s title attribute.
  512.         if ( this.dialog ) {
  513.             this.dialog.title = title;
  514.         }
  515.     },
  516.  
  517.     // Update the dialog to indicate specified percent complete.
  518.     setPercent: function( percent ) {
  519.         // Maximum percentage is 100.
  520.         if ( percent > 100 ) {
  521.             percent = 100;
  522.         }
  523.         // Test if percentage is changing.
  524.         if ( this.percent != percent ) {
  525.             this.mPercent = percent;
  526.  
  527.             // If dialog not opened yet, bail early.
  528.             if ( !this.loaded ) {
  529.                 return this.mPercent;
  530.             }
  531.  
  532.             if ( percent == -1 ) {
  533.                 // Progress meter needs to be in "undetermined" mode.
  534.                 this.mode = "undetermined";
  535.  
  536.                 // Update progress meter percentage text.
  537.                 this.setValue( "progressText", "" );
  538.             } else {
  539.                 // Progress meter needs to be in normal mode.
  540.                 this.mode = "normal";
  541.  
  542.                 // Set progress meter thermometer.
  543.                 this.setValue( "progress", percent );
  544.  
  545.                 // Update progress meter percentage text.
  546.                 this.setValue( "progressText", this.replaceInsert( this.getString( "percentMsg" ), 1, percent ) );
  547.             }
  548.     
  549.             // Update title.
  550.             this.setTitle();
  551.         }
  552.         return this.mPercent;
  553.     },
  554.  
  555.     // Update download rate and dialog display.
  556.     // Note that we don't want the displayed value to quiver
  557.     // between essentially identical values (e.g., 99.9Kb and
  558.     // 100.0Kb) so we only update if we see a big change.
  559.     setRate: function( rate ) {
  560.         if ( rate ) {
  561.             // rate is bytes/sec
  562.             var change = Math.abs( this.rate - rate );
  563.             // Don't update too often!
  564.             if ( change > this.rate / 10 ) {
  565.                 // Displayed rate changes.
  566.                 this.mRate = rate;
  567.             }
  568.         }
  569.         return this.mRate;
  570.     },
  571.  
  572.     // Handle download completion.
  573.     setCompleted: function() {
  574.         // If dialog hasn't loaded yet, defer this.
  575.         if ( !this.loaded ) {
  576.             this.dialog.setTimeout( function(obj){obj.setCompleted()}, 100, this );
  577.             return false;
  578.         }
  579.         if ( !this.mCompleted ) {
  580.             this.mCompleted = true;
  581.  
  582.             // If the "keep dialog open" box is checked, then update dialog.
  583.             if ( this.dialog && this.dialogElement( "keep" ).checked ) {
  584.                 // Indicate completion in status area.
  585.                 this.setValue( "status", this.replaceInsert( this.getString( "completeMsg" ),
  586.                                                              1,
  587.                                                              this.formatSeconds( this.elapsed/1000 ) ) );
  588.  
  589.                 // Put progress meter at 100%.
  590.                 this.percent = 100;
  591.  
  592.                 // Set time remaining to 00:00.
  593.                 this.setValue( "timeLeft", this.formatSeconds( 0 ) );
  594.  
  595.                 // Change Cancel button to Close, and give it focus.
  596.                 var cancelButton = this.dialogElement( "cancel" );
  597.                 cancelButton.label = this.getString( "close" );
  598.                 cancelButton.focus();
  599.  
  600.                 // Activate reveal/launch buttons if we enable them.
  601.                 var enableButtons = true;
  602.                 try {
  603.                   var prefs = Components.classes[ "@mozilla.org/preferences-service;1" ]
  604.                                   .getService( Components.interfaces.nsIPrefBranch );
  605.                   enableButtons = prefs.getBoolPref( "browser.download.progressDnldDialog.enable_launch_reveal_buttons" );
  606.                 } catch ( e ) {
  607.                 }
  608.  
  609.                 if ( enableButtons ) {
  610.                     this.enable( "reveal" );
  611.                     try {
  612.                         if ( this.target && !this.target.isExecutable() ) {
  613.                             this.enable( "launch" );
  614.                         }
  615.                     } catch(e) {
  616.                     }
  617.                 }
  618.  
  619.                 // Disable the Pause/Resume buttons.
  620.                 this.dialogElement( "pauseResume" ).disabled = true;
  621.  
  622.                 // Fix up dialog layout (which gets messed up sometimes).
  623.                 this.dialog.sizeToContent();
  624.             } else if ( this.dialog ) {
  625.                 this.dialog.close();
  626.             }
  627.         }
  628.         return this.mCompleted;
  629.     },
  630.  
  631.     // Set progress meter to given mode ("normal" or "undetermined").
  632.     setMode: function( newMode ) {
  633.         if ( this.mode != newMode ) {
  634.             // Need to update progress meter.
  635.             this.dialogElement( "progress" ).setAttribute( "mode", newMode );
  636.         }
  637.         return this.mMode = newMode;
  638.     },
  639.  
  640.     // Set pause/resume state.
  641.     setPaused: function( pausing ) {
  642.         // If state changing, then update stuff.
  643.         if ( this.paused != pausing ) {
  644.             var string = pausing ? "resume" : "pause";
  645.             this.dialogElement( "pauseResume" ).label = this.getString(string);
  646.  
  647.             // If we have a request, suspend/resume it.
  648.             if ( this.request ) {
  649.                 if ( pausing ) {
  650.                     this.request.suspend();
  651.                 } else {
  652.                     this.request.resume();
  653.                 }
  654.             }
  655.         }
  656.         return this.mPaused = pausing;
  657.     },
  658.  
  659.     // Set the saved nsIRequest.  The first time, we test it for
  660.     // ftp and initialize the pause/resume stuff.
  661.     // XXX This is broken, I think, because if we're doing something
  662.     //     like saving a web-page-complete that has multiple images
  663.     //     accessed via ftp: urls, then it seems like the pause/resume
  664.     //     button should come and go, depending on what's being downloaded
  665.     //     at a given point in time.  The old dialog didn't handle that case
  666.     //     either, though, so I'm not sweating it for now.
  667.     setRequest: function( aRequest ) {
  668.         if ( this.request == null && this.loaded && aRequest ) {
  669.             // Right now, all that supports restarting downloads is ftp (rfc959).
  670.             try {
  671.                 ftpChannel = aRequest.QueryInterface( Components.interfaces.nsIFTPChannel );
  672.                 if ( ftpChannel ) {
  673.                     this.dialogElement("pauseResume").label = this.getString("pause");
  674.                     this.paused = false;
  675.                 }
  676.             } catch ( e ) {
  677.             }
  678.             // This *must* come after the "this.paused = false" above, so that we
  679.             // don't suspend or resume the first time we call that function!
  680.             this.mRequest = aRequest;
  681.         }
  682.         return this.mRequest;
  683.     },
  684.  
  685.     // Convert raw rate (bytes/sec) to Kbytes/sec (to nearest tenth).
  686.     rateToKRate: function( rate ) {
  687.          var kRate = rate / 1024; // KBytes/sec
  688.          var fraction = parseInt( ( kRate - ( kRate = parseInt( kRate ) ) ) * 10 + 0.5 );
  689.          return kRate + "." + fraction;
  690.     },
  691.  
  692.     // Format number of seconds in hh:mm:ss form.
  693.     formatSeconds: function( secs ) {
  694.         // Round the number of seconds to remove fractions.
  695.         secs = parseInt( secs + .5 );
  696.         var hours = parseInt( secs/3600 );
  697.         secs -= hours*3600;
  698.         var mins = parseInt( secs/60 );
  699.         secs -= mins*60;
  700.         var result;
  701.         if ( hours )
  702.             result = this.getString( "longTimeFormat" );
  703.         else
  704.             result = this.getString( "shortTimeFormat" );
  705.     
  706.         if ( hours < 10 )
  707.             hours = "0" + hours;
  708.         if ( mins < 10 )
  709.             mins = "0" + mins;
  710.         if ( secs < 10 )
  711.             secs = "0" + secs;
  712.     
  713.         // Insert hours, minutes, and seconds into result string.
  714.         result = this.replaceInsert( result, 1, hours );
  715.         result = this.replaceInsert( result, 2, mins );
  716.         result = this.replaceInsert( result, 3, secs );
  717.     
  718.         return result;
  719.     },
  720.  
  721.     // Get dialog element using argument as id.
  722.     dialogElement: function( id ) {
  723.         // Check if we've already fetched it.
  724.         if ( !( id in this.fields ) ) {
  725.             // No, then get it from dialog.
  726.             try {
  727.                 this.fields[ id ] = this.dialog.document.getElementById( id );
  728.             } catch(e) {
  729.                 this.fields[ id ] = { 
  730.                     value: "",
  731.                     setAttribute: function(id,val) {},
  732.                     removeAttribute: function(id) {}
  733.                     }
  734.             }
  735.         }
  736.         return this.fields[ id ];
  737.     },
  738.  
  739.     // Set dialog element value for given dialog element.
  740.     setValue: function( id, val ) {
  741.         this.dialogElement( id ).value = val;
  742.     },
  743.  
  744.     // Enable dialgo element.
  745.     enable: function( field ) {
  746.         this.dialogElement( field ).removeAttribute( "disabled" );
  747.     },
  748.  
  749.     // Get localizable string from properties file.
  750.     getProperty: function( propertyId, strings, len ) {
  751.         if ( !this.mBundle ) {
  752.             this.mBundle = Components.classes[ "@mozilla.org/intl/stringbundle;1" ]
  753.                              .getService( Components.interfaces.nsIStringBundleService )
  754.                                .createBundle( "chrome://global/locale/nsProgressDialog.properties");
  755.         }
  756.         return this.mBundle.formatStringFromName( propertyId, strings, len );
  757.     },
  758.  
  759.     // Get localizable string (from dialog <data> elements).
  760.     getString: function ( stringId ) {
  761.         // Check if we've fetched this string already.
  762.         if ( !( this.strings && stringId in this.strings ) ) {
  763.             // Presume the string is empty if we can't get it.
  764.             this.strings[ stringId ] = "";
  765.             // Try to get it.
  766.             try {
  767.                 this.strings[ stringId ] = this.dialog.document.getElementById( "string."+stringId ).childNodes[0].nodeValue;
  768.             } catch (e) {}
  769.        }
  770.        return this.strings[ stringId ];
  771.     },
  772.     
  773.     // Replaces insert ("#n") with input text.
  774.     replaceInsert: function( text, index, value ) {
  775.         var result = text;
  776.         var regExp = new RegExp( "#"+index );
  777.         result = result.replace( regExp, value );
  778.         return result;
  779.     },
  780.  
  781.     // Hide a given dialog field.
  782.     hide: function( field ) {
  783.         this.dialogElement( field ).setAttribute( "style", "display: none;" );
  784.         // Hide the associated separator, too.
  785.         this.dialogElement( field+"Separator" ).setAttribute( "style", "display: none;" );
  786.     },
  787.  
  788.     // Return input in hex, prepended with "0x" and leading zeros (to 8 digits).
  789.     hex: function( x ) {
  790.          var hex = Number(x).toString(16);
  791.          return "0x" + ( "00000000" + hex ).substring( hex.length );
  792.     },
  793.  
  794.     // Dump text (if debug is on).
  795.     dump: function( text ) {
  796.         if ( this.debug ) {
  797.             dump( text );
  798.         }
  799.     }
  800. }
  801.  
  802. // This Component's module implementation.  All the code below is used to get this
  803. // component registered and accessible via XPCOM.
  804. var module = {
  805.     // registerSelf: Register this component.
  806.     registerSelf: function (compMgr, fileSpec, location, type) {
  807.         compMgr = compMgr.QueryInterface( Components.interfaces.nsIComponentManagerObsolete );
  808.         compMgr.registerComponentWithType( this.cid,
  809.                                            "Mozilla Download Progress Dialog",
  810.                                            this.contractId,
  811.                                            fileSpec,
  812.                                            location,
  813.                                            true,
  814.                                            true,
  815.                                            type );
  816.     },
  817.  
  818.     // getClassObject: Return this component's factory object.
  819.     getClassObject: function (compMgr, cid, iid) {
  820.         if (!cid.equals(this.cid))
  821.             throw Components.results.NS_ERROR_NO_INTERFACE;
  822.  
  823.         if (!iid.equals(Components.interfaces.nsIFactory))
  824.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  825.  
  826.         return this.factory;
  827.     },
  828.  
  829.     /* CID for this class */
  830.     cid: Components.ID("{F5D248FD-024C-4f30-B208-F3003B85BC92}"),
  831.  
  832.     /* Contract ID for this class */
  833.     contractId: "@mozilla.org/progressdialog;1",
  834.  
  835.     /* factory object */
  836.     factory: {
  837.         // createInstance: Return a new nsProgressDialog object.
  838.         createInstance: function (outer, iid) {
  839.             if (outer != null)
  840.                 throw Components.results.NS_ERROR_NO_AGGREGATION;
  841.  
  842.             return (new nsProgressDialog()).QueryInterface(iid);
  843.         }
  844.     },
  845.  
  846.     // canUnload: n/a (returns true)
  847.     canUnload: function(compMgr) {
  848.         return true;
  849.     }
  850. };
  851.  
  852. // NSGetModule: Return the nsIModule object.
  853. function NSGetModule(compMgr, fileSpec) {
  854.     return module;
  855. }
  856.