From d2c2d0f385dd34aa1e2794063023608441607a63 Mon Sep 17 00:00:00 2001 From: Mark Banner Date: Tue, 10 Mar 2009 09:20:54 +0000 Subject: [PATCH] Bug 479844 Consolidate checking for messages to send later into one function. r=Neil,sr=bienvenu --- mail/base/content/mail-offline.js | 34 ++--------- mail/base/content/mail3PaneWindowCommands.js | 56 +++++++------------ .../base/resources/content/mail-offline.js | 30 ++-------- .../content/mail3PaneWindowCommands.js | 53 +++++++----------- mailnews/compose/public/nsIMsgSendLater.idl | 14 ++++- mailnews/compose/src/nsMsgSendLater.cpp | 21 ++++++- mailnews/compose/src/nsMsgSendLater.h | 2 - .../test/unit/test_sendMessageLater.js | 6 ++ .../test/unit/test_sendMessageLater2.js | 10 ++++ .../test/unit/test_sendMessageLater3.js | 8 +++ 10 files changed, 105 insertions(+), 129 deletions(-) diff --git a/mail/base/content/mail-offline.js b/mail/base/content/mail-offline.js index e17125e880..8508cd7c89 100644 --- a/mail/base/content/mail-offline.js +++ b/mail/base/content/mail-offline.js @@ -1,4 +1,4 @@ -# -*- 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 # @@ -117,35 +117,9 @@ var MailOfflineMgr = { */ 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(); }, /** diff --git a/mail/base/content/mail3PaneWindowCommands.js b/mail/base/content/mail3PaneWindowCommands.js index c59c0b679a..61f860310a 100644 --- a/mail/base/content/mail3PaneWindowCommands.js +++ b/mail/base/content/mail3PaneWindowCommands.js @@ -1,4 +1,4 @@ -# -*- 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 # @@ -857,10 +857,8 @@ 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); @@ -868,40 +866,28 @@ function IsSendUnsentMsgsEnabled(folderResource) // 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() diff --git a/mailnews/base/resources/content/mail-offline.js b/mailnews/base/resources/content/mail-offline.js index 3900b52662..2f1430bb7e 100644 --- a/mailnews/base/resources/content/mail-offline.js +++ b/mailnews/base/resources/content/mail-offline.js @@ -1,4 +1,4 @@ -/* -*- 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 * @@ -76,31 +76,9 @@ function GetMailOfflinePrefs() // 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. diff --git a/mailnews/base/resources/content/mail3PaneWindowCommands.js b/mailnews/base/resources/content/mail3PaneWindowCommands.js index 24ebf541a3..70909809c0 100644 --- a/mailnews/base/resources/content/mail3PaneWindowCommands.js +++ b/mailnews/base/resources/content/mail3PaneWindowCommands.js @@ -818,8 +818,6 @@ function UnloadCommandUpdateHandlers() function IsSendUnsentMsgsEnabled(folderResource) { - var identity; - var msgSendlater = Components.classes["@mozilla.org/messengercompose/sendlater;1"] .getService(Components.interfaces.nsIMsgSendLater); @@ -827,40 +825,29 @@ function IsSendUnsentMsgsEnabled(folderResource) // 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() diff --git a/mailnews/compose/public/nsIMsgSendLater.idl b/mailnews/compose/public/nsIMsgSendLater.idl index d7f92a12d2..23b4bef0fa 100644 --- a/mailnews/compose/public/nsIMsgSendLater.idl +++ b/mailnews/compose/public/nsIMsgSendLater.idl @@ -48,8 +48,12 @@ 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. @@ -83,6 +87,14 @@ interface nsIMsgSendLater : nsIStreamListener */ 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; }; diff --git a/mailnews/compose/src/nsMsgSendLater.cpp b/mailnews/compose/src/nsMsgSendLater.cpp index 4ec1150ad9..eaebd5b3a6 100644 --- a/mailnews/compose/src/nsMsgSendLater.cpp +++ b/mailnews/compose/src/nsMsgSendLater.cpp @@ -622,8 +622,25 @@ nsMsgSendLater::GetUnsentMessagesFolder(nsIMsgIdentity *aIdentity, nsIMsgFolder { 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 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; } // diff --git a/mailnews/compose/src/nsMsgSendLater.h b/mailnews/compose/src/nsMsgSendLater.h index d0d69828d1..e64b50d0fb 100644 --- a/mailnews/compose/src/nsMsgSendLater.h +++ b/mailnews/compose/src/nsMsgSendLater.h @@ -38,8 +38,6 @@ #ifndef _nsMsgSendLater_H_ #define _nsMsgSendLater_H_ -#include "nsIMsgSendLater.h" - #include "nsCOMArray.h" #include "nsIMsgFolder.h" #include "nsIMsgSendListener.h" diff --git a/mailnews/compose/test/unit/test_sendMessageLater.js b/mailnews/compose/test/unit/test_sendMessageLater.js index 16e3ab026a..fadc179b67 100644 --- a/mailnews/compose/test/unit/test_sendMessageLater.js +++ b/mailnews/compose/test/unit/test_sendMessageLater.js @@ -89,6 +89,9 @@ function OnStopCopy(aStatus) { // 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); @@ -170,6 +173,9 @@ function run_test() { // 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(); diff --git a/mailnews/compose/test/unit/test_sendMessageLater2.js b/mailnews/compose/test/unit/test_sendMessageLater2.js index 6ff369b329..8380750e92 100644 --- a/mailnews/compose/test/unit/test_sendMessageLater2.js +++ b/mailnews/compose/test/unit/test_sendMessageLater2.js @@ -92,6 +92,10 @@ msll.prototype = { 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(); @@ -133,6 +137,9 @@ function OnStopCopy(aStatus) // 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); @@ -245,6 +252,9 @@ function run_test() { // 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(); diff --git a/mailnews/compose/test/unit/test_sendMessageLater3.js b/mailnews/compose/test/unit/test_sendMessageLater3.js index a3bafc2c9a..5689b96a82 100644 --- a/mailnews/compose/test/unit/test_sendMessageLater3.js +++ b/mailnews/compose/test/unit/test_sendMessageLater3.js @@ -115,6 +115,8 @@ msll.prototype = { 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(); } @@ -128,6 +130,9 @@ function OnStopCopy(aStatus) { 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); @@ -166,6 +171,9 @@ function run_test() { // 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();