Bug 479844 Consolidate checking for messages to send later into one function. r=Neil,sr=bienvenu
authorMark Banner <bugzilla@standard8.plus.com>
Tue, 10 Mar 2009 09:20:54 +0000
changeset 2169 b2161ed4236af25bbeb62f0f4df3ac6c9869d74f
parent 2168 79f7c64d3252bb48749aa466c42f5b8f1d0eb0d2
child 2170 c55d44c0a936ff3c39a32ce39e8af375961ec1f6
push idunknown
push userunknown
push dateunknown
reviewersNeil, bienvenu
bugs479844
Bug 479844 Consolidate checking for messages to send later into one function. r=Neil,sr=bienvenu
mail/base/content/mail-offline.js
mail/base/content/mail3PaneWindowCommands.js
mailnews/base/resources/content/mail-offline.js
mailnews/base/resources/content/mail3PaneWindowCommands.js
mailnews/compose/public/nsIMsgSendLater.idl
mailnews/compose/src/nsMsgSendLater.cpp
mailnews/compose/src/nsMsgSendLater.h
mailnews/compose/test/unit/test_sendMessageLater.js
mailnews/compose/test/unit/test_sendMessageLater2.js
mailnews/compose/test/unit/test_sendMessageLater3.js
--- a/mail/base/content/mail-offline.js
+++ b/mail/base/content/mail-offline.js
@@ -1,9 +1,9 @@
-# -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+# -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 # ***** 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/
 #
@@ -112,45 +112,19 @@ var MailOfflineMgr = {
       this.mailOfflineStateChanged(aState == "offline");
   },
 
   /**
    * @return true if there are unsent messages
    */
   haveUnsentMessages: function()
   {
-    try
-    {
-      var am = Components.classes["@mozilla.org/messenger/account-manager;1"]
-                 .getService(Components.interfaces.nsIMsgAccountManager);
-      var msgSendlater = Components.classes["@mozilla.org/messengercompose/sendlater;1"]
-                           .getService(Components.interfaces.nsIMsgSendLater);
-      var identitiesCount, allIdentities, currentIdentity, numMessages, msgFolder;
-
-      if (am) 
-      { 
-        allIdentities = am.allIdentities;
-        identitiesCount = allIdentities.Count();
-        for (var i = 0; i < identitiesCount; i++) 
-        {
-          currentIdentity = allIdentities.QueryElementAt(i, Components.interfaces.nsIMsgIdentity);
-          msgFolder = msgSendlater.getUnsentMessagesFolder(currentIdentity);
-          if(msgFolder) 
-          {
-            // if true, descends into all subfolders 
-            numMessages = msgFolder.getTotalMessages(false);
-            if(numMessages > 0) 
-              return true;
-          }
-        } 
-      }
-    }
-    catch(ex) {
-    }
-    return false; 
+    return Components.classes["@mozilla.org/messengercompose/sendlater;1"]
+                     .getService(Components.interfaces.nsIMsgSendLater)
+                     .hasUnsentMessages();
   },
 
   /**
    * open the offline panel in the account manager for the currently loaded
    * account.
    */
   openOfflineAccountSettings: function()
   {
--- a/mail/base/content/mail3PaneWindowCommands.js
+++ b/mail/base/content/mail3PaneWindowCommands.js
@@ -1,9 +1,9 @@
-# -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+# -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 # ***** 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/
 #
@@ -852,61 +852,47 @@ function SetupCommandUpdateHandlers()
   top.controllers.insertControllerAt(0, DefaultController);
 }
 
 function UnloadCommandUpdateHandlers()
 {
   top.controllers.removeController(DefaultController);
 }
 
-function IsSendUnsentMsgsEnabled(folderResource)
+function IsSendUnsentMsgsEnabled(unsentMsgsFolder)
 {
-  var identity;
-
   var msgSendlater =
     Components.classes["@mozilla.org/messengercompose/sendlater;1"]
               .getService(Components.interfaces.nsIMsgSendLater);
 
   // If we're currently sending unsent msgs, disable this cmd.
   if (msgSendlater.sendingMessages)
     return false;
-  try {
-    if (folderResource) {
-      // if folderResource is non-null, it is
-      // resource for the "Unsent Messages" folder
-      // we're here because we've done a right click on the "Unsent Messages"
-      // folder (context menu)
-      var msgFolder = folderResource.QueryInterface(Components.interfaces.nsIMsgFolder);
-      return (msgFolder.getTotalMessages(false) > 0);
-    }
-    else {
-      var folders = GetSelectedMsgFolders();
-      if (folders.length > 0) {
-        identity = getIdentityForServer(folders[0].server);
-      }
-    }
-  }
-  catch (ex) {
-    dump("ex = " + ex + "\n");
-    identity = null;
+
+  if (unsentMsgsFolder) {
+    // If unsentMsgsFolder is non-null, it is the "Unsent Messages" folder.
+    // We're here because we've done a right click on the "Unsent Messages"
+    // folder (context menu), so we can use the folder and return true/false
+    // straight away.
+    return unsentMsgsFolder.getTotalMessages(false) > 0;
   }
 
-  try {
-    if (!identity) {
-      var am = Components.classes["@mozilla.org/messenger/account-manager;1"].getService(Components.interfaces.nsIMsgAccountManager);
-      identity = am.defaultAccount.defaultIdentity;
-    }
+  // Otherwise, we don't know where we are, so use the current identity and
+  // find out if we have messages or not via that.
+  let identity;
+  let folders = GetSelectedMsgFolders();
+  if (folders.length > 0)
+    identity = getIdentityForServer(folders[0].server);
 
-    var unsentMsgsFolder = msgSendlater.getUnsentMessagesFolder(identity);
-    return (unsentMsgsFolder.getTotalMessages(false) > 0);
-  }
-  catch (ex) {
-    dump("ex = " + ex + "\n");
-  }
-  return false;
+  if (!identity)
+    identity = Components.classes["@mozilla.org/messenger/account-manager;1"]
+                         .getService(Components.interfaces.nsIMsgAccountManager)
+                         .defaultAccount.defaultIdentity;
+
+  return msgSendlater.hasUnsentMessages(identity);
 }
 
 function IsRenameFolderEnabled()
 {
   var folders = GetSelectedMsgFolders();
   return folders.length == 1 && folders[0].canRename &&
          isCommandEnabled("cmd_renameFolder");
 }
--- a/mailnews/base/resources/content/mail-offline.js
+++ b/mailnews/base/resources/content/mail-offline.js
@@ -1,9 +1,9 @@
-/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  * ***** 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/
  *
@@ -71,41 +71,19 @@ function GetMailOfflinePrefs()
   catch(ex) {
     dump("failed to get prefs service!\n");
   }
 }
 
 // Check for unsent messages
 function CheckForUnsentMessages()
 {
-  try
-  {
-    var am = Components.classes["@mozilla.org/messenger/account-manager;1"]
-                 .getService(Components.interfaces.nsIMsgAccountManager);
-    var msgSendlater = Components.classes["@mozilla.org/messengercompose/sendlater;1"]
-                 .getService(Components.interfaces.nsIMsgSendLater);
-    var identitiesCount, allIdentities, currentIdentity, numMessages, msgFolder;
-
-    if(am) { 
-      allIdentities = am.allIdentities;
-      identitiesCount = allIdentities.Count();
-      for (var i = 0; i < identitiesCount; i++) {
-        currentIdentity = allIdentities.QueryElementAt(i, Components.interfaces.nsIMsgIdentity);
-        msgFolder = msgSendlater.getUnsentMessagesFolder(currentIdentity);
-        if(msgFolder) {
-          // if true, descends into all subfolders 
-          numMessages = msgFolder.getTotalMessages(false);
-          if(numMessages > 0) return true;
-        }
-      } 
-    }
-  }
-  catch(ex) {
-  }
-  return false;
+  return Components.classes["@mozilla.org/messengercompose/sendlater;1"]
+                   .getService(Components.interfaces.nsIMsgSendLater)
+                   .hasUnsentMessages();
 }
 
 // Init nsIPromptService & strings.
 function InitPrompts()
 {
   if(!gPromptService) {
     gPromptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService();
     gPromptService = gPromptService.QueryInterface(Components.interfaces.nsIPromptService);
--- a/mailnews/base/resources/content/mail3PaneWindowCommands.js
+++ b/mailnews/base/resources/content/mail3PaneWindowCommands.js
@@ -813,59 +813,46 @@ function UnloadCommandUpdateHandlers()
 
   var widget = GetFolderTree();
   if (widget)
     widget.controllers.removeController(FolderPaneController);
 }
 
 function IsSendUnsentMsgsEnabled(folderResource)
 {
-  var identity;
-
   var msgSendlater =
     Components.classes["@mozilla.org/messengercompose/sendlater;1"]
               .getService(Components.interfaces.nsIMsgSendLater);
 
   // If we're currently sending unsent msgs, disable this cmd.
   if (msgSendlater.sendingMessages)
     return false;
-  try {
-    if (folderResource) {
-      // if folderResource is non-null, it is
-      // resource for the "Unsent Messages" folder
-      // we're here because we've done a right click on the "Unsent Messages"
-      // folder (context menu)
-      var msgFolder = folderResource.QueryInterface(Components.interfaces.nsIMsgFolder);
-      return (msgFolder.getTotalMessages(false) > 0);
-    }
-    else {
-      var folders = GetSelectedMsgFolders();
-      if (folders.length > 0) {
-        identity = getIdentityForServer(folders[0].server);
-      }
-    }
-  }
-  catch (ex) {
-    dump("ex = " + ex + "\n");
-    identity = null;
+
+  if (folderResource &&
+      folderResource instanceof Components.interfaces.nsIMsgFolder) {
+    // If unsentMsgsFolder is non-null, it is the "Unsent Messages" folder.
+    // We're here because we've done a right click on the "Unsent Messages"
+    // folder (context menu), so we can use the folder and return true/false
+    // straight away.
+    return folderResource.getTotalMessages(false) > 0;
   }
 
-  try {
-    if (!identity) {
-      var am = Components.classes["@mozilla.org/messenger/account-manager;1"].getService(Components.interfaces.nsIMsgAccountManager);
-      identity = am.defaultAccount.defaultIdentity;
-    }
+  // Otherwise, we don't know where we are, so use the current identity and
+  // find out if we have messages or not via that.
+  let identity = null;
+  let folders = GetSelectedMsgFolders();
+  if (folders.length > 0)
+    identity = getIdentityForServer(folders[0].server);
 
-    var unsentMsgsFolder = msgSendlater.getUnsentMessagesFolder(identity);
-    return (unsentMsgsFolder.getTotalMessages(false) > 0);
-  }
-  catch (ex) {
-    dump("ex = " + ex + "\n");
-  }
-  return false;
+  if (!identity)
+    identity = Components.classes["@mozilla.org/messenger/account-manager;1"]
+                         .getService(Components.interfaces.nsIMsgAccountManager)
+                         .defaultAccount.defaultIdentity;
+
+  return msgSendLater.hasUnsentMessages(identity);
 }
 
 function IsRenameFolderEnabled()
 {
     var folderTree = GetFolderTree();
     var selection = folderTree.view.selection;
     if (selection.count == 1)
     {
--- a/mailnews/compose/public/nsIMsgSendLater.idl
+++ b/mailnews/compose/public/nsIMsgSendLater.idl
@@ -43,18 +43,22 @@ interface nsIMsgStatusFeedback;
 interface nsIMsgIdentity;
 interface nsIMsgSendLaterListener;
 interface nsIMsgFolder;
 
 /**
  * nsIMsgSendLater is a service used for sending messages in the background.
  * Messages should be saved to an identity's unsent messages folder, and then
  * can be sent by calling sendUnsentMessages.
+ *
+ * Although the service supports passing identities as parameters, until bug
+ * 317803 is fixed, all identities use the same folder, and hence currently it
+ * has no effect.
  */
-[scriptable, uuid(2e2142b2-8f94-4604-8cc7-06a00d59f6b5)]
+[scriptable, uuid(fa324a4b-4b87-4e9a-a3c0-af9071a358df)]
 interface nsIMsgSendLater : nsIStreamListener 
 {
   /// Used to obtain status feedback for when messages are sent.
   attribute nsIMsgStatusFeedback statusFeedback;
 
   /**
    * Sends any unsent messages in the identity's unsent messages folder.
    *
@@ -78,11 +82,19 @@ interface nsIMsgSendLater : nsIStreamLis
    */
   void removeListener(in nsIMsgSendLaterListener aListener);
 
   /**
    * Returns the unsent messages folder for the identity.
    */
   nsIMsgFolder getUnsentMessagesFolder(in nsIMsgIdentity userIdentity);
 
+  /**
+   * Returns true if there are any unsent messages to send.
+   *
+   * @param aIdentity  The identity whose folder to check for unsent messages.
+   *                   If not specified, all unsent message folders are checked.
+   */
+  boolean hasUnsentMessages([optional] in nsIMsgIdentity aIdentity);
+
   /// Returns true if the service is currently sending messages.
   readonly attribute boolean sendingMessages;
 };
--- a/mailnews/compose/src/nsMsgSendLater.cpp
+++ b/mailnews/compose/src/nsMsgSendLater.cpp
@@ -617,18 +617,35 @@ nsMsgSendLater::StartNextMailFileSend()
   return NS_OK;
 }
 
 NS_IMETHODIMP 
 nsMsgSendLater::GetUnsentMessagesFolder(nsIMsgIdentity *aIdentity, nsIMsgFolder **folder)
 {
   nsCString uri;
   GetFolderURIFromUserPrefs(nsIMsgSend::nsMsgQueueForLater, aIdentity, uri);
-  nsresult rv = LocateMessageFolder(aIdentity, nsIMsgSend::nsMsgQueueForLater, uri.get(), folder);
-  return rv;
+  return LocateMessageFolder(aIdentity, nsIMsgSend::nsMsgQueueForLater,
+                             uri.get(), folder);
+}
+
+NS_IMETHODIMP
+nsMsgSendLater::HasUnsentMessages(nsIMsgIdentity *aIdentity, PRBool *aResult)
+{
+  NS_ENSURE_ARG_POINTER(aResult);
+
+  nsCOMPtr<nsIMsgFolder> msgFolder;
+  nsresult rv = GetUnsentMessagesFolder(aIdentity, getter_AddRefs(msgFolder));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  PRInt32 totalMessages;
+  rv = msgFolder->GetTotalMessages(PR_FALSE, &totalMessages);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  *aResult = totalMessages > 0;
+  return NS_OK;
 }
 
 //
 // To really finalize this capability, we need to have the ability to get
 // the message from the mail store in a stream for processing. The flow 
 // would be something like this:
 //
 //      foreach (message in Outbox folder)
--- a/mailnews/compose/src/nsMsgSendLater.h
+++ b/mailnews/compose/src/nsMsgSendLater.h
@@ -33,18 +33,16 @@
  * 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 ***** */
 
 #ifndef _nsMsgSendLater_H_
 #define _nsMsgSendLater_H_
 
-#include "nsIMsgSendLater.h"
-
 #include "nsCOMArray.h"
 #include "nsIMsgFolder.h"
 #include "nsIMsgSendListener.h"
 #include "nsIMsgSendLaterListener.h"
 #include "nsIMsgSendLater.h"
 #include "nsIMsgStatusFeedback.h"
 #include "nsTObserverArray.h"
 
--- a/mailnews/compose/test/unit/test_sendMessageLater.js
+++ b/mailnews/compose/test/unit/test_sendMessageLater.js
@@ -84,16 +84,19 @@ function OnStopCopy(aStatus) {
     // Check this is false before we start sending
     do_check_eq(msgSendLater.sendingMessages, false);
 
     let folder = msgSendLater.getUnsentMessagesFolder(identity);
 
     // Check we have a message in the unsent message folder
     do_check_eq(folder.getTotalMessages(false), 1);
 
+    // Check that the send later service thinks we have messages to send
+    do_check_eq(msgSendLater.hasUnsentMessages(identity), true);
+
     // Now do a comparison of what is in the sent mail folder
     var fileData = loadFileToString(folder.filePath);
 
     // Skip the headers etc that mailnews adds
     var pos = fileData.indexOf("From:");
     do_check_neq(pos, -1);
 
     fileData = fileData.substr(pos);
@@ -165,16 +168,19 @@ function sendMessageLater()
 function run_test() {
   // Test file - for bug 429891
   originalData = loadFileToString(testFile);
 
   // Ensure we have a local mail account, an normal account and appropriate
   // servers and identities.
   loadLocalMailAccount();
 
+  // Check that the send later service thinks we don't have messages to send
+  do_check_eq(msgSendLater.hasUnsentMessages(identity), false);
+
   var acctMgr = Cc["@mozilla.org/messenger/account-manager;1"]
                   .getService(Ci.nsIMsgAccountManager);
   acctMgr.setSpecialFolders();
 
   var account = acctMgr.createAccount();
   incomingServer = acctMgr.createIncomingServer("test", "localhost", "pop3");
 
   var smtpServer = getBasicSmtpServer();
--- a/mailnews/compose/test/unit/test_sendMessageLater2.js
+++ b/mailnews/compose/test/unit/test_sendMessageLater2.js
@@ -87,16 +87,20 @@ msll.prototype = {
     }
   },
   onStopSending: function (aStatus, aMsg, aTotal, aSuccessful) {
     try {
       do_check_eq(aStatus, 0);
       do_check_eq(aTotal, aSuccessful);
       do_check_eq(msgSendLater.sendingMessages, false);
 
+      // Check that the send later service now thinks we don't have messages to
+      // send.
+      do_check_eq(msgSendLater.hasUnsentMessages(identity), false);
+
       // XXX This is another send multiple messages hack
       if (!transaction) {
         server.performTest();
         transaction = server.playTransaction();
       }
 
       do_check_transaction(transaction,
                            ["EHLO test",
@@ -128,16 +132,19 @@ msll.prototype = {
 // sending of the message.
 function OnStopCopy(aStatus)
 {
   do_check_eq(aStatus, 0);
 
   // Check this is false before we start sending
   do_check_eq(msgSendLater.sendingMessages, false);
 
+  // Check that the send later service thinks we have messages to send.
+  do_check_eq(msgSendLater.hasUnsentMessages(identity), true);
+
   // Check we have a message in the unsent message folder
   do_check_eq(gSentFolder.getTotalMessages(false), gMsgOrder.length);
 
   // Start the next step after a brief time so that functions can finish
   // properly
   do_timeout(0, "doTest(++gCurTestNum);");
 }
 
@@ -240,16 +247,19 @@ function run_test() {
   for (var i = 0; i < gMsgFile.length; ++i) {
     gMsgFileData[i] = loadFileToString(gMsgFile[i]);
   }
 
   // Ensure we have a local mail account, an normal account and appropriate
   // servers and identities.
   loadLocalMailAccount();
 
+  // Check that the send later service thinks we don't have messages to send.
+  do_check_eq(msgSendLater.hasUnsentMessages(identity), false);
+
   var acctMgr = Cc["@mozilla.org/messenger/account-manager;1"]
                   .getService(Ci.nsIMsgAccountManager);
   acctMgr.setSpecialFolders();
 
   var account = acctMgr.createAccount();
   incomingServer = acctMgr.createIncomingServer("test", "localhost", "pop3");
 
   var smtpServer = getBasicSmtpServer();
--- a/mailnews/compose/test/unit/test_sendMessageLater3.js
+++ b/mailnews/compose/test/unit/test_sendMessageLater3.js
@@ -110,29 +110,34 @@ msll.prototype = {
     print("msll onStopSending\n");
 
     // NS_ERROR_SMTP_SEND_FAILED_REFUSED is 2153066798
     do_check_eq(aStatus, 2153066798);
     do_check_eq(aTotal, 1);
     do_check_eq(aSuccessful, 0);
     do_check_eq(this._initialTotal, 1);
     do_check_eq(msgSendLater.sendingMessages, false);
+    // Check that the send later service still thinks we have messages to send.
+    do_check_eq(msgSendLater.hasUnsentMessages(identity), true);
 
     do_test_finished();
   }
 };
 
 function OnStopCopy(aStatus) {
   do_check_eq(aStatus, 0);
 
   // Check this is false before we start sending
   do_check_eq(msgSendLater.sendingMessages, false);
 
   let folder = msgSendLater.getUnsentMessagesFolder(identity);
 
+  // Check that the send later service thinks we have messages to send.
+  do_check_eq(msgSendLater.hasUnsentMessages(identity), true);
+
   // Check we have a message in the unsent message folder
   do_check_eq(folder.getTotalMessages(false), 1);
 
   // Now do a comparison of what is in the unsent mail folder
   var fileData = loadFileToString(folder.filePath);
 
   // Skip the headers etc that mailnews adds
   var pos = fileData.indexOf("From:");
@@ -161,16 +166,19 @@ function sendMessageLater()
 function run_test() {
   // Test file - for bug 429891
   originalData = loadFileToString(testFile);
 
   // Ensure we have a local mail account, an normal account and appropriate
   // servers and identities.
   loadLocalMailAccount();
 
+  // Check that the send later service thinks we don't have messages to send.
+  do_check_eq(msgSendLater.hasUnsentMessages(identity), false);
+
   var acctMgr = Cc["@mozilla.org/messenger/account-manager;1"]
                   .getService(Ci.nsIMsgAccountManager);
   acctMgr.setSpecialFolders();
 
   var account = acctMgr.createAccount();
   incomingServer = acctMgr.createIncomingServer("test", "localhost", "pop3");
 
   var smtpServer = getBasicSmtpServer();