home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / mail.xpi / bin / chrome / messenger.jar / content / messenger / messengercompose / sendProgress.js < prev    next >
Text File  |  2001-08-21  |  8KB  |  239 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.  */
  26.  
  27. var msgCompDeliverMode = Components.interfaces.nsIMsgCompDeliverMode;
  28.  
  29. // dialog is just an array we'll use to store various properties from the dialog document...
  30. var dialog;
  31.  
  32. // the msgProgress is a nsIMsgProgress object
  33. var msgProgress = null; 
  34.  
  35. // random global variables...
  36. var targetFile;
  37. var itsASaveOperation = false;
  38.  
  39. // all progress notifications are done through the nsIWebProgressListener implementation...
  40. var progressListener = {
  41.     onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus)
  42.     {
  43.       if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_START)
  44.       {
  45.         // Put progress meter in undetermined mode.
  46.         // dialog.progress.setAttribute( "value", 0 );
  47.         dialog.progress.setAttribute( "mode", "undetermined" );
  48.       }
  49.       
  50.       if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP)
  51.       {
  52.         // we are done sending/saving the message...
  53.         // Indicate completion in status area.
  54.         var msg;
  55.         if (itsASaveOperation)
  56.           msg = getString( "messageSaved" );
  57.         else
  58.           msg = getString( "messageSent" );
  59.         dialog.status.setAttribute("value", msg);
  60.  
  61.         // Put progress meter at 100%.
  62.         dialog.progress.setAttribute( "value", 100 );
  63.         dialog.progress.setAttribute( "mode", "normal" );
  64.         var percentMsg = getString( "progressText" );
  65.         percentMsg = replaceInsert( percentMsg, 1, 100 );
  66.         dialog.progressText.setAttribute("value", percentMsg);
  67.  
  68.         window.close();
  69.       }
  70.     },
  71.     
  72.     onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
  73.     {
  74.       // Calculate percentage.
  75.       var percent;
  76.       if ( aMaxTotalProgress > 0 ) 
  77.       {
  78.         percent = parseInt( (aCurTotalProgress*100)/aMaxTotalProgress + .5 );
  79.         if ( percent > 100 )
  80.           percent = 100;
  81.         
  82.         dialog.progress.removeAttribute( "mode");
  83.         
  84.         // Advance progress meter.
  85.         dialog.progress.setAttribute( "value", percent );
  86.  
  87.         // Update percentage label on progress meter.
  88.         var percentMsg = getString( "progressText" );
  89.         percentMsg = replaceInsert( percentMsg, 1, percent );
  90.         dialog.progressText.setAttribute("value", percentMsg);
  91.       } 
  92.       else 
  93.       {
  94.         // Progress meter should be barber-pole in this case.
  95.         dialog.progress.setAttribute( "mode", "undetermined" );
  96.         // Update percentage label on progress meter.
  97.         dialog.progressText.setAttribute("value", "");
  98.       }
  99.     },
  100.  
  101.       onLocationChange: function(aWebProgress, aRequest, aLocation)
  102.     {
  103.       // we can ignore this notification
  104.     },
  105.  
  106.     onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage)
  107.     {
  108.       if (aMessage != "")
  109.         dialog.status.setAttribute("value", aMessage);
  110.     },
  111.  
  112.     onSecurityChange: function(aWebProgress, aRequest, state)
  113.     {
  114.       // we can ignore this notification
  115.     },
  116.  
  117.     QueryInterface : function(iid)
  118.     {
  119.      if (iid.equals(Components.interfaces.nsIWebProgressListener) || iid.equals(Components.interfaces.nsISupportsWeakReference))
  120.       return this;
  121.      
  122.      throw Components.results.NS_NOINTERFACE;
  123.     }
  124. };
  125.  
  126. function getString( stringId ) {
  127.    // Check if we've fetched this string already.
  128.    if ( !dialog.strings[ stringId ] ) {
  129.       // Try to get it.
  130.       var elem = document.getElementById( "dialog.strings."+stringId );
  131.       try {
  132.         if ( elem
  133.            &&
  134.            elem.childNodes
  135.            &&
  136.            elem.childNodes[0]
  137.            &&
  138.            elem.childNodes[0].nodeValue ) {
  139.          dialog.strings[ stringId ] = elem.childNodes[0].nodeValue;
  140.         } else {
  141.           // If unable to fetch string, use an empty string.
  142.           dialog.strings[ stringId ] = "";
  143.         }
  144.       } catch (e) { dialog.strings[ stringId ] = ""; }
  145.    }
  146.    return dialog.strings[ stringId ];
  147. }
  148.  
  149. function loadDialog() 
  150. {
  151. }
  152.  
  153. function replaceInsert( text, index, value ) {
  154.    var result = text;
  155.    var regExp = new RegExp( "#"+index );
  156.    result = result.replace( regExp, value );
  157.    return result;
  158. }
  159.  
  160. function onLoad() {
  161.     // Set global variables.
  162.     var subject = "";
  163.     msgProgress = window.arguments[0];
  164.     if (window.arguments[1])
  165.     {
  166.       var progressParams = window.arguments[1].QueryInterface(Components.interfaces.nsIMsgComposeProgressParams)
  167.       if (progressParams)
  168.       {
  169.         itsASaveOperation = (progressParams.deliveryMode != msgCompDeliverMode.Now);
  170.         subject = progressParams.subject;
  171.       }
  172.     }
  173.  
  174.     if ( !msgProgress ) {
  175.         dump( "Invalid argument to downloadProgress.xul\n" );
  176.         window.close()
  177.         return;
  178.     }
  179.  
  180.     dialog = new Object;
  181.     dialog.strings = new Array;
  182.     dialog.status      = document.getElementById("dialog.status");
  183.     dialog.progress    = document.getElementById("dialog.progress");
  184.     dialog.progressText = document.getElementById("dialog.progressText");
  185.     dialog.cancel      = document.getElementById("cancel");
  186.  
  187.     // Set up dialog button callbacks.
  188.     var object = this;
  189.     doSetOKCancel("", function () { return object.onCancel();});
  190.  
  191.     // Fill dialog.
  192.     loadDialog();
  193.  
  194.     // set our web progress listener on the helper app launcher
  195.     msgProgress.registerListener(progressListener);
  196.     moveToAlertPosition();
  197.  
  198.     //We need to delay the set title else dom will overwrite it
  199.     return window.setTimeout(SetTitle, 0, subject);
  200. }
  201.  
  202. function onUnload() 
  203. {
  204.   if (msgProgress)
  205.   {
  206.    try 
  207.    {
  208.      msgProgress.unregisterListener(progressListener);
  209.      msgProgress = null;
  210.    }
  211.     
  212.    catch( exception ) {}
  213.   }
  214. }
  215.  
  216. function SetTitle(subject)
  217. {
  218.   var prefix;
  219.   if (itsASaveOperation)
  220.     prefix = getString("titlePrefixSave");
  221.   else
  222.     prefix = getString("titlePrefixSend");
  223.   window.title = prefix + " " + subject;
  224. }
  225.  
  226. // If the user presses cancel, tell the app launcher and close the dialog...
  227. function onCancel () 
  228. {
  229.   // Cancel app launcher.
  230.    try 
  231.    {
  232.      msgProgress.processCanceledByUser = true;
  233.    }
  234.    catch( exception ) {return true;}
  235.     
  236.   // don't Close up dialog by returning false, the backend will close the dialog when everything will be aborted.
  237.   return false;
  238. }
  239.