home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / 01_03.iso / software / ghostzilla_hit / files / ghostzilla-1.0-plus-install.exe / chrome / toolkit.jar / content / global / printProgress.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  7.3 KB  |  248 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "License"); you may not use this file except in
  5.  * compliance with the License.  You may obtain a copy of the License at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  10.  * for the specific language governing rights and limitations under the
  11.  * License.
  12.  *
  13.  * The Original Code is Mozilla Communicator client code, 
  14.  * released March 31, 1998. 
  15.  *
  16.  * The Initial Developer of the Original Code is Netscape Communications 
  17.  * Corporation.  Portions created by Netscape are 
  18.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  19.  * Reserved.
  20.  *
  21.  * Contributors:
  22.  *     William A. ("PowerGUI") Law <law@netscape.com>
  23.  *     Scott MacGregor <mscott@netscape.com>
  24.  *     jean-Francois Ducarroz <ducarroz@netscape.com>
  25.  *     Rod Spears <rods@netscape.com>
  26.  */
  27.  
  28. // dialog is just an array we'll use to store various properties from the dialog document...
  29. var dialog;
  30.  
  31. // the printProgress is a nsIPrintProgress object
  32. var printProgress = null; 
  33.  
  34. // random global variables...
  35. var targetFile;
  36.  
  37. var docTitle = "";
  38. var docURL   = "";
  39. var progressParams = null;
  40.  
  41. // all progress notifications are done through the nsIWebProgressListener implementation...
  42. var progressListener = {
  43.     onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus)
  44.     {
  45.       if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_START)
  46.       {
  47.         // Put progress meter in undetermined mode.
  48.         // dialog.progress.setAttribute( "value", 0 );
  49.         dialog.progress.setAttribute( "mode", "undetermined" );
  50.       }
  51.       
  52.       if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP)
  53.       {
  54.         // we are done sending/saving the message...
  55.         // Indicate completion in status area.
  56.         var msg = getString( "printComplete" );
  57.         dialog.status.setAttribute("value", msg);
  58.  
  59.         // Put progress meter at 100%.
  60.         dialog.progress.setAttribute( "value", 100 );
  61.         dialog.progress.setAttribute( "mode", "normal" );
  62.         var percentPrint = getString( "progressText" );
  63.         percentPrint = replaceInsert( percentPrint, 1, 100 );
  64.         dialog.progressText.setAttribute("value", percentPrint);
  65.  
  66.         window.close();
  67.       }
  68.     },
  69.     
  70.     onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
  71.     {
  72.       if (progressParams)
  73.       {
  74.         var docTitleStr = progressParams.docTitle;
  75.         if (docTitleStr != docTitle) {
  76.           SetTitle(docTitleStr);
  77.           docTitle = docTitleStr;
  78.         }
  79.         var docURLStr = progressParams.docURL;
  80.         if (docURLStr != docURL && dialog.status != null) {
  81.           dialog.status.value = docURLStr;
  82.           docURL = docURLStr;
  83.         }
  84.       }
  85.  
  86.       // Calculate percentage.
  87.       var percent;
  88.       if ( aMaxTotalProgress > 0 ) 
  89.       {
  90.         percent = parseInt( (aCurTotalProgress*100)/aMaxTotalProgress + .5 );
  91.         if ( percent > 100 )
  92.           percent = 100;
  93.         
  94.         dialog.progress.removeAttribute( "mode");
  95.         
  96.         // Advance progress meter.
  97.         dialog.progress.setAttribute( "value", percent );
  98.  
  99.         // Update percentage label on progress meter.
  100.         var percentPrint = getString( "progressText" );
  101.         percentPrint = replaceInsert( percentPrint, 1, percent );
  102.         dialog.progressText.setAttribute("value", percentPrint);
  103.       } 
  104.       else 
  105.       {
  106.         // Progress meter should be barber-pole in this case.
  107.         dialog.progress.setAttribute( "mode", "undetermined" );
  108.         // Update percentage label on progress meter.
  109.         dialog.progressText.setAttribute("value", "");
  110.       }
  111.     },
  112.  
  113.       onLocationChange: function(aWebProgress, aRequest, aLocation)
  114.     {
  115.       // we can ignore this notification
  116.     },
  117.  
  118.     onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage)
  119.     {
  120.       if (aMessage != "")
  121.         dialog.status.setAttribute("value", aMessage);
  122.     },
  123.  
  124.     onSecurityChange: function(aWebProgress, aRequest, state)
  125.     {
  126.       // we can ignore this notification
  127.     },
  128.  
  129.     QueryInterface : function(iid)
  130.     {
  131.      if (iid.equals(Components.interfaces.nsIWebProgressListener) || iid.equals(Components.interfaces.nsISupportsWeakReference))
  132.       return this;
  133.      
  134.      throw Components.results.NS_NOINTERFACE;
  135.     }
  136. };
  137.  
  138. function getString( stringId ) {
  139.    // Check if we've fetched this string already.
  140.    if (!(stringId in dialog.strings)) {
  141.       // Try to get it.
  142.       var elem = document.getElementById( "dialog.strings."+stringId );
  143.       try {
  144.         if ( elem
  145.            &&
  146.            elem.childNodes
  147.            &&
  148.            elem.childNodes[0]
  149.            &&
  150.            elem.childNodes[0].nodeValue ) {
  151.          dialog.strings[ stringId ] = elem.childNodes[0].nodeValue;
  152.         } else {
  153.           // If unable to fetch string, use an empty string.
  154.           dialog.strings[ stringId ] = "";
  155.         }
  156.       } catch (e) { dialog.strings[ stringId ] = ""; }
  157.    }
  158.    return dialog.strings[ stringId ];
  159. }
  160.  
  161. function loadDialog() 
  162. {
  163. }
  164.  
  165. function replaceInsert( text, index, value ) {
  166.    var result = text;
  167.    var regExp = new RegExp( "#"+index );
  168.    result = result.replace( regExp, value );
  169.    return result;
  170. }
  171.  
  172. function onLoad() {
  173.     // Set global variables.
  174.     printProgress = window.arguments[0];
  175.     if (window.arguments[1])
  176.     {
  177.       progressParams = window.arguments[1].QueryInterface(Components.interfaces.nsIPrintProgressParams)
  178.       if (progressParams)
  179.       {
  180.         docTitle = progressParams.docTitle;
  181.         docURL = progressParams.docURL;
  182.       }
  183.     }
  184.  
  185.     if ( !printProgress ) {
  186.         dump( "Invalid argument to downloadProgress.xul\n" );
  187.         window.close()
  188.         return;
  189.     }
  190.  
  191.     dialog = new Object;
  192.     dialog.strings = new Array;
  193.     dialog.status       = document.getElementById("dialog.status");
  194.     dialog.progress     = document.getElementById("dialog.progress");
  195.     dialog.progressText = document.getElementById("dialog.progressText");
  196.     dialog.cancel       = document.getElementById("cancel");
  197.  
  198.     dialog.status.value = docURL;
  199.  
  200.     // Set up dialog button callbacks.
  201.     var object = this;
  202.     doSetOKCancel("", function () { return object.onCancel();});
  203.  
  204.     // Fill dialog.
  205.     loadDialog();
  206.  
  207.     // set our web progress listener on the helper app launcher
  208.     printProgress.registerListener(progressListener);
  209.     moveToAlertPosition();
  210.  
  211.     //We need to delay the set title else dom will overwrite it
  212.     window.setTimeout(SetTitle, 0, docTitle);
  213. }
  214.  
  215. function onUnload() 
  216. {
  217.   if (printProgress)
  218.   {
  219.    try 
  220.    {
  221.      printProgress.unregisterListener(progressListener);
  222.      printProgress = null;
  223.    }
  224.     
  225.    catch( exception ) {}
  226.   }
  227. }
  228.  
  229. function SetTitle(str)
  230. {
  231.   var prefix = getString("titlePrefixPrint");
  232.   window.title = prefix + " " + str;
  233. }
  234.  
  235. // If the user presses cancel, tell the app launcher and close the dialog...
  236. function onCancel () 
  237. {
  238.   // Cancel app launcher.
  239.    try 
  240.    {
  241.      printProgress.processCanceledByUser = true;
  242.    }
  243.    catch( exception ) {return true;}
  244.     
  245.   // don't Close up dialog by returning false, the backend will close the dialog when everything will be aborted.
  246.   return false;
  247. }
  248.