Bug 456860 - Improve msgCompSMIMEOverlay.js and msgReadSMIMEOverlay.js; (Dv1b-TB) msgCompSMIMEOverlay.js cleanup.
authorSerge Gautherie <sgautherie.bz@free.fr>
Mon, 01 Feb 2010 00:39:38 +0100
changeset 4790 07feda0b00436da837b14ed062d3afb2566a3cf2
parent 4789 2bbbd23b2b6254dab838dfb75d773f636ba08893
child 4791 4769df74717012b3a6b3c60ed83db6a1bdf6473b
push idunknown
push userunknown
push dateunknown
bugs456860
Bug 456860 - Improve msgCompSMIMEOverlay.js and msgReadSMIMEOverlay.js; (Dv1b-TB) msgCompSMIMEOverlay.js cleanup. r=mkmelin.
mail/extensions/smime/content/msgCompSMIMEOverlay.js
mail/extensions/smime/content/msgHdrViewSMIMEOverlay.js
mail/extensions/smime/content/msgHdrViewSMIMEOverlay.xul
--- a/mail/extensions/smime/content/msgCompSMIMEOverlay.js
+++ b/mail/extensions/smime/content/msgCompSMIMEOverlay.js
@@ -33,21 +33,28 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
+// Account encryption policy values:
+// const kEncryptionPolicy_Never = 0;
+// 'IfPossible' was used by ns4.
+// const kEncryptionPolicy_IfPossible = 1;
+const kEncryptionPolicy_Always = 2;
+
+var gEncryptedURIService =
+        Components.classes["@mozilla.org/messenger-smime/smime-encrypted-uris-service;1"]
+                  .getService(Components.interfaces.nsIEncryptedSMIMEURIsService);
+
 var gNextSecurityButtonCommand = "";
-var gBundle;
-var gBrandBundle;
-var gSMFields;
-var gEncryptedURIService = null;
+var gSMFields = null;
 
 var gEncryptOptionChanged;
 var gSignOptionChanged;
 
 function onComposerClose()
 {
   gSMFields = null;
   setNoEncryptionUI();
@@ -64,60 +71,51 @@ function onComposerReOpen()
   // Are we already set up ? Or are the required fields missing ?
   if (gSMFields || !gMsgCompose || !gMsgCompose.compFields)
     return;
 
   gMsgCompose.compFields.securityInfo = null;
 
   gSMFields = Components.classes["@mozilla.org/messenger-smime/composefields;1"]
                         .createInstance(Components.interfaces.nsIMsgSMIMECompFields);
-  if (gSMFields)
-  {
-    gMsgCompose.compFields.securityInfo = gSMFields;
-    // set up the intial security state....
-    var encryptionPolicy = gCurrentIdentity.getIntAttribute("encryptionpolicy");
-    // 0 == never, 1 == if possible, 2 == always Encrypt.
-    gSMFields.requireEncryptMessage = encryptionPolicy == 2;
+  if (!gSMFields)
+    return;
 
-    gSMFields.signMessage = gCurrentIdentity.getBoolAttribute("sign_mail");
+  gMsgCompose.compFields.securityInfo = gSMFields;
 
-    if (gEncryptedURIService && !gSMFields.requireEncryptMessage)
-    {
-      if (gEncryptedURIService.isEncrypted(gMsgCompose.originalMsgURI))
-      {
-        // Override encryption setting if original is known as encrypted.
-        gSMFields.requireEncryptMessage = true;
-      }
-    }
+  // Set up the intial security state.
+  gSMFields.requireEncryptMessage =
+    gCurrentIdentity.getIntAttribute("encryptionpolicy") == kEncryptionPolicy_Always;
+  if (!gSMFields.requireEncryptMessage &&
+      gEncryptedURIService &&
+      gEncryptedURIService.isEncrypted(gMsgCompose.originalMsgURI))
+  {
+    // Override encryption setting if original is known as encrypted.
+    gSMFields.requireEncryptMessage = true;
+  }
+  if (gSMFields.requireEncryptMessage)
+    setEncryptionUI();
+  else
+    setNoEncryptionUI();
 
-    if (gSMFields.requireEncryptMessage)
-      setEncryptionUI();
-    else
-      setNoEncryptionUI();
-
-    if (gSMFields.signMessage)
-      setSignatureUI();
-    else
-      setNoSignatureUI();
-  }
+  gSMFields.signMessage = gCurrentIdentity.getBoolAttribute("sign_mail");
+  if (gSMFields.signMessage)
+    setSignatureUI();
+  else
+    setNoSignatureUI();
 }
 
 addEventListener("load", smimeComposeOnLoad, false);
 
 // this function gets called multiple times,
 // but only on first open, not on composer recycling
 function smimeComposeOnLoad()
 {
   removeEventListener("load", smimeComposeOnLoad, false);
 
-  if (!gEncryptedURIService)
-    gEncryptedURIService = 
-      Components.classes["@mozilla.org/messenger-smime/smime-encrypted-uris-service;1"]
-      .getService(Components.interfaces.nsIEncryptedSMIMEURIsService);
-
   onComposerReOpen();
 
   top.controllers.appendController(SecurityController);
 
   addEventListener("compose-from-changed", onComposerFromChanged, true);
   addEventListener("compose-send-message", onComposerSendMessage, true);
   addEventListener("compose-window-close", onComposerClose, true);
   addEventListener("compose-window-reopen", onComposerReOpen, true);
@@ -132,77 +130,59 @@ function smimeComposeOnUnload()
   removeEventListener("compose-from-changed", onComposerFromChanged, true);
   removeEventListener("compose-send-message", onComposerSendMessage, true);
   removeEventListener("compose-window-close", onComposerClose, true);
   removeEventListener("compose-window-reopen", onComposerReOpen, true);
 
   top.controllers.removeController(SecurityController);
 }
 
-function setupBundles()
-{
-  if (gBundle)
-    return;
-
-  gBundle = document.getElementById("bundle_comp_smime");
-  gBrandBundle = document.getElementById("bundle_brand");
-}
-
 // stub routine to make our call to MsgAccountManager work correctly
 function GetSelectedFolderURI()
 {
   return;
 }
 
 function GetServer(uri)
 {
   var servers = gAccountManager.GetServersForIdentity(gCurrentIdentity);
   return servers.QueryElementAt(0, Components.interfaces.nsIMsgIncomingServer);
 }
 
 function showNeedSetupInfo()
 {
+  let compSmimeBundle = document.getElementById("bundle_comp_smime");
+  let brandBundle = document.getElementById("bundle_brand");
+  if (!compSmimeBundle || !brandBundle)
+    return;
+
   var ifps = Components.interfaces.nsIPromptService;
-
   let promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
                                 .getService(ifps);
-  setupBundles();
 
-  if (promptService && gBundle && gBrandBundle) {
-    var dummy = new Object;
-    var buttonPressed =
+  let buttonPressed =
     promptService.confirmEx(window,
-      gBrandBundle.getString("brandShortName"),
-      gBundle.getString("NeedSetup"), 
-      ifps.STD_YES_NO_BUTTONS,
-      0,
-      0,
-      0,
-      null,
-      dummy);
-    
-    if (0 == buttonPressed) {
-      MsgAccountManager('am-smime.xul');
-    }
-  }
+                            brandBundle.getString("brandShortName"),
+                            compSmimeBundle.getString("NeedSetup"),
+                            ifps.STD_YES_NO_BUTTONS, 0, 0, 0, null, {});
+  if (buttonPressed == 0)
+    MsgAccountManager("am-smime.xul");
 }
 
 function toggleEncryptMessage()
 {
   if (!gSMFields)
     return;
 
-  // toggle
   gSMFields.requireEncryptMessage = !gSMFields.requireEncryptMessage;
 
   if (gSMFields.requireEncryptMessage)
   {
     // Make sure we have a cert.
-    var encryptionCertName = gCurrentIdentity.getUnicharAttribute("encryption_cert_name");
-    if (!encryptionCertName)
+    if (!gCurrentIdentity.getUnicharAttribute("encryption_cert_name"))
     {
       gSMFields.requireEncryptMessage = false;
       showNeedSetupInfo();
       return;
     }
 
     setEncryptionUI();
   }
@@ -214,23 +194,21 @@ function toggleEncryptMessage()
   gEncryptOptionChanged = true;
 }
 
 function toggleSignMessage()
 {
   if (!gSMFields)
     return;
 
-  // toggle
   gSMFields.signMessage = !gSMFields.signMessage;
 
   if (gSMFields.signMessage) // make sure we have a cert name...
   {
-    var signingCertName = gCurrentIdentity.getUnicharAttribute("signing_cert_name");
-    if (!signingCertName)
+    if (!gCurrentIdentity.getUnicharAttribute("signing_cert_name"))
     {
       gSMFields.signMessage = false;
       showNeedSetupInfo();
       return;
     }
 
     setSignatureUI();
   }
@@ -302,92 +280,93 @@ function setEncryptionUI()
   top.document.getElementById("securityStatus").setAttribute("crypto", "ok");
   top.document.getElementById("encryption-status").collapsed = false;
 }
 
 function showMessageComposeSecurityStatus()
 {
   Recipients2CompFields(gMsgCompose.compFields);
 
-  var encryptionCertName = gCurrentIdentity.getUnicharAttribute("encryption_cert_name");
-  var signingCertName = gCurrentIdentity.getUnicharAttribute("signing_cert_name");
-
-  window.openDialog("chrome://messenger-smime/content/msgCompSecurityInfo.xul",
+  window.openDialog(
+    "chrome://messenger-smime/content/msgCompSecurityInfo.xul",
     "",
     "chrome,modal,resizable,centerscreen",
     {
       compFields : gMsgCompose.compFields,
       subject : GetMsgSubjectElement().value,
       smFields : gSMFields,
-      isSigningCertAvailable : (signingCertName.length > 0),
-      isEncryptionCertAvailable : (encryptionCertName.length > 0),
+      isSigningCertAvailable :
+        gCurrentIdentity.getUnicharAttribute("signing_cert_name") != "",
+      isEncryptionCertAvailable :
+        gCurrentIdentity.getUnicharAttribute("encryption_cert_name") != "",
       currentIdentity : gCurrentIdentity
     }
   );
 }
 
 var SecurityController =
 {
   supportsCommand: function(command)
   {
-    switch ( command )
+    switch (command)
     {
       case "cmd_viewSecurityStatus":
         return true;
 
       default:
         return false;
     }
   },
 
   isCommandEnabled: function(command)
   {
-    switch ( command )
+    switch (command)
     {
       case "cmd_viewSecurityStatus":
         return true;
 
       default:
         return false;
     }
   }
 };
 
 function onComposerSendMessage()
 {
   let missingCount = new Object();
   let emailAddresses = new Object();
 
-  try {
-    if (!gMsgCompose.compFields.securityInfo.requireEncryptMessage) {
+  try
+  {
+    if (!gMsgCompose.compFields.securityInfo.requireEncryptMessage)
       return;
-    }
 
-    var helper = Components.classes["@mozilla.org/messenger-smime/smimejshelper;1"]
-                           .createInstance(Components.interfaces.nsISMimeJSHelper);
-    helper.getNoCertAddresses(
-      gMsgCompose.compFields,
-      missingCount,
-      emailAddresses);
+    Components.classes["@mozilla.org/messenger-smime/smimejshelper;1"]
+              .createInstance(Components.interfaces.nsISMimeJSHelper)
+              .getNoCertAddresses(gMsgCompose.compFields,
+                                  missingCount,
+                                  emailAddresses);
   }
   catch (e)
   {
     return;
   }
 
   if (missingCount.value > 0)
   {
     // The rules here: If the current identity has a directoryServer set, then
     // use that, otherwise, try the global preference instead.
 
-    var autocompleteDirectory = null;
+    let autocompleteDirectory;
 
     // Does the current identity override the global preference?
     if (gCurrentIdentity.overrideGlobalPref)
+    {
       autocompleteDirectory = gCurrentIdentity.directoryServer;
+    }
     else
     {
       // Try the global one
       var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                             .getService(Components.interfaces.nsIPrefBranch);
       if (prefs.getBoolPref("ldap_2.autoComplete.useDirectory"))
         autocompleteDirectory =
           prefs.getCharPref("ldap_2.autoComplete.directoryServer");
@@ -409,26 +388,25 @@ function onComposerFromChanged()
 
   var encryptionPolicy = gCurrentIdentity.getIntAttribute("encryptionpolicy");
   var useEncryption = false;
   if (!gEncryptOptionChanged)
   {
     // Encryption wasn't manually checked.
     // Set up the encryption policy from the setting of the new identity.
 
-    // 0 == never, 1 == if possible (ns4), 2 == always Encrypt.
-    useEncryption = (encryptionPolicy == 2);
+    useEncryption = (encryptionPolicy == kEncryptionPolicy_Always);
   }
   else
   {
     // The encryption policy was manually checked. That means we can get into
     // the situation that the new identity doesn't have a cert to encrypt with.
     // If it doesn't, don't encrypt.
 
-    if (encryptionPolicy != 2) // Encrypted (policy unencrypted, manually changed).
+    if (encryptionPolicy != kEncryptionPolicy_Always) // Encrypted (policy unencrypted, manually changed).
     {
       // Make sure we have a cert for encryption.
       var encryptionCertName = gCurrentIdentity.getUnicharAttribute("encryption_cert_name");
       useEncryption = encryptionCertName;
     }
   }
   gSMFields.requireEncryptMessage = useEncryption;
   if (useEncryption)
--- a/mail/extensions/smime/content/msgHdrViewSMIMEOverlay.js
+++ b/mail/extensions/smime/content/msgHdrViewSMIMEOverlay.js
@@ -15,17 +15,17 @@
 # The Original Code is mozilla.org Code.
 #
 # The Initial Developer of the Original Code is
 # Netscape Communications Corporation.
 # Portions created by the Initial Developer are Copyright (C) 1998-2001
 # the Initial Developer. All Rights Reserved.
 #
 # Contributor(s):
-#   Scott MacGreogr <mscott@netscape.com>
+#   Scott MacGregor <mscott@netscape.com>
 #
 # Alternatively, the contents of this file may be used under the terms of
 # either the GNU General Public License Version 2 or later (the "GPL"), or
 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 # in which case the provisions of the GPL or the LGPL are applicable instead
 # of those above. If you wish to allow use of your version of this file only
 # under the terms of either the GPL or the LGPL, and not to allow others to
 # use your version of this file under the terms of the MPL, indicate your
--- a/mail/extensions/smime/content/msgHdrViewSMIMEOverlay.xul
+++ b/mail/extensions/smime/content/msgHdrViewSMIMEOverlay.xul
@@ -1,9 +1,9 @@
-<?xml version="1.0"?> 
+<?xml version="1.0"?>
 <!-- ***** BEGIN LICENSE BLOCK *****
    - Version: MPL 1.1/GPL 2.0/LGPL 2.1
    -
    - The contents of this file are subject to the Mozilla Public License Version
    - 1.1 (the "License"); you may not use this file except in compliance with
    - the License. You may obtain a copy of the License at
    - http://www.mozilla.org/MPL/
    -
@@ -16,33 +16,33 @@
    - March 31, 1998.
    -
    - The Initial Developer of the Original Code is
    - Netscape Communications Corporation.
    - Portions created by the Initial Developer are Copyright (C) 1999
    - the Initial Developer. All Rights Reserved.
    -
    - Contributor(s):
-   -   Scott MacGregor <mscott@netscape.com
+   -   Scott MacGregor <mscott@netscape.com>
    -
    - Alternatively, the contents of this file may be used under the terms of
    - either of the GNU General Public License Version 2 or later (the "GPL"),
    - or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    - in which case the provisions of the GPL or the LGPL are applicable instead
    - of those above. If you wish to allow use of your version of this file only
    - under the terms of either the GPL or the LGPL, and not to allow others to
    - use your version of this file under the terms of the MPL, indicate your
    - decision by deleting the provisions above and replace them with the notice
    - and other provisions required by the GPL or the LGPL. If you do not delete
    - the provisions above, a recipient may use your version of this file under
    - the terms of any one of the MPL, the GPL or the LGPL.
    -
    - ***** END LICENSE BLOCK ***** -->
 
-<?xml-stylesheet href="chrome://messenger/skin/smime/msgHdrViewSMIMEOverlay.css" type="text/css"?> 
+<?xml-stylesheet href="chrome://messenger/skin/smime/msgHdrViewSMIMEOverlay.css" type="text/css"?>
 
 <overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
 
   <script type="application/javascript" src="chrome://messenger-smime/content/msgHdrViewSMIMEOverlay.js"/>
 <!-- These stringbundles are already defined in msgReadSMIMEOverlay.xul!
   <stringbundleset id="stringbundleset">
     <stringbundle id="bundle_read_smime" src="chrome://messenger-smime/locale/msgReadSMIMEOverlay.properties"/>
     <stringbundle id="bundle_brand" src="chrome://branding/locale/brand.properties"/>