Bug 833988 Improve nsMsgMailList and use it properly r=Standard8
authorNeil Rashbrook <neil@parkwaycc.co.uk>
Thu, 07 Feb 2013 20:45:29 +0000
changeset 14831 c52e29d0ff20f216261fe39f33e423c3f3381e89
parent 14830 af56cbb736574c1f9e2ce3234ab8193a79e9f2a0
child 14832 0701728ff277887179ee8836fe89a5bef5d0913b
push id867
push userbugzilla@standard8.plus.com
push dateMon, 01 Apr 2013 20:44:27 +0000
treeherdercomm-beta@797726b8d244 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersStandard8
bugs833988
Bug 833988 Improve nsMsgMailList and use it properly r=Standard8
mailnews/compose/src/nsMsgCompose.cpp
mailnews/compose/src/nsMsgCompose.h
--- a/mailnews/compose/src/nsMsgCompose.cpp
+++ b/mailnews/compose/src/nsMsgCompose.cpp
@@ -4630,17 +4630,17 @@ nsresult nsMsgCompose::GetABDirectories(
         }
       }
     }
   }
   return rv;
 }
 
 nsresult nsMsgCompose::BuildMailListArray(nsIAbDirectory* parentDir,
-                                          nsISupportsArray* array)
+                                          nsTArray<nsMsgMailList>& array)
 {
   nsresult rv;
 
   nsCOMPtr<nsIAbDirectory> directory;
   nsCOMPtr<nsISimpleEnumerator> subDirectories;
 
   if (NS_SUCCEEDED(parentDir->GetChildNodes(getter_AddRefs(subDirectories))) && subDirectories)
   {
@@ -4652,69 +4652,38 @@ nsresult nsMsgCompose::BuildMailListArra
       {
         directory = do_QueryInterface(item, &rv);
         if (NS_SUCCEEDED(rv))
         {
           bool bIsMailList;
 
           if (NS_SUCCEEDED(directory->GetIsMailList(&bIsMailList)) && bIsMailList)
           {
-            nsString listName;
-            nsString listDescription;
-
-            directory->GetDirName(listName);
-            directory->GetDescription(listDescription);
-
-            nsMsgMailList* mailList = new nsMsgMailList(listName,
-                  listDescription, directory);
-            if (!mailList)
-              return NS_ERROR_OUT_OF_MEMORY;
-            NS_ADDREF(mailList);
-
-            rv = array->AppendElement(mailList);
-            if (NS_FAILED(rv))
-              return rv;
-
-            NS_RELEASE(mailList);
+            array.AppendElement(directory);
           }
         }
       }
     }
   }
   return rv;
 }
 
-
-nsresult nsMsgCompose::GetMailListAddresses(nsString& name, nsISupportsArray* mailListArray, nsIMutableArray** addressesArray)
+struct nsMsgMailListComparator
 {
-  nsresult rv;
-  nsCOMPtr<nsIEnumerator> enumerator;
-
-  rv = mailListArray->Enumerate(getter_AddRefs(enumerator));
-  if (NS_SUCCEEDED(rv))
-  {
-    for (rv = enumerator->First(); NS_SUCCEEDED(rv); rv = enumerator->Next())
-    {
-      nsMsgMailList* mailList;
-      rv = enumerator->CurrentItem((nsISupports**)&mailList);
-      if (NS_SUCCEEDED(rv) && mailList)
-      {
-        if (name.Equals(mailList->mFullName, nsCaseInsensitiveStringComparator()))
-        {
-          if (!mailList->mDirectory)
-            return NS_ERROR_FAILURE;
-
-          mailList->mDirectory->GetAddressLists(addressesArray);
-          NS_RELEASE(mailList);
-          return NS_OK;
-        }
-        NS_RELEASE(mailList);
-      }
-    }
+  bool Equals(const nsMsgMailList& mailList, const nsString& name) const {
+    return mailList.mFullName.Equals(name, nsCaseInsensitiveStringComparator());
   }
+};
+
+nsresult nsMsgCompose::GetMailListAddresses(nsString& name, nsTArray<nsMsgMailList>& mailListArray, nsIMutableArray** addressesArray)
+{
+  uint32_t index = mailListArray.IndexOf(name, 0, nsMsgMailListComparator());
+  if (index != mailListArray.NoIndex &&
+      mailListArray[index].mDirectory)
+    return mailListArray[index].mDirectory->GetAddressLists(addressesArray);
 
   return NS_ERROR_FAILURE;
 }
 
 
 // 3 = To, Cc, Bcc
 #define MAX_OF_RECIPIENT_ARRAY    3
 
@@ -4754,18 +4723,17 @@ nsMsgCompose::CheckAndPopulateRecipients
   }
 
   // Then look them up in the Addressbooks
   bool stillNeedToSearch = true;
   nsCOMPtr<nsIAbDirectory> abDirectory;
   nsCOMPtr<nsIAbCard> existingCard;
   nsCOMPtr<nsIMutableArray> mailListAddresses;
   nsCOMPtr<nsIMsgHeaderParser> parser(do_GetService(NS_MAILNEWS_MIME_HEADER_PARSER_CONTRACTID));
-  nsCOMPtr<nsISupportsArray> mailListArray(do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID, &rv));
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsTArray<nsMsgMailList> mailListArray;
 
   nsCOMArray<nsIAbDirectory> addrbookDirArray;
   rv = GetABDirectories(NS_LITERAL_CSTRING(kAllDirectoryRoot),
                         addrbookDirArray);
   if (NS_SUCCEEDED(rv))
   {
     nsString dirPath;
     uint32_t nbrAddressbook = addrbookDirArray.Count();
@@ -4782,17 +4750,17 @@ nsMsgCompose::CheckAndPopulateRecipients
       abDirectory = addrbookDirArray[k];
 
       bool supportsMailingLists;
       rv = abDirectory->GetSupportsMailingLists(&supportsMailingLists);
       if (NS_FAILED(rv) || !supportsMailingLists)
         continue;
 
       // Ensure the existing list is empty before filling it
-      mailListArray->Clear();
+      mailListArray.Clear();
 
       // Collect all mailing lists defined in this address book
       rv = BuildMailListArray(abDirectory, mailListArray);
       if (NS_FAILED(rv))
         return rv;
 
       stillNeedToSearch = false;
       for (i = 0; i < MAX_OF_RECIPIENT_ARRAY; i ++)
@@ -5525,33 +5493,25 @@ NS_IMETHODIMP nsMsgCompose::CheckCharset
       *_retval = nsMsgI18Ncheck_data_in_charset_range(charset, identityStrings.get(),
                                                       fallbackCharset);
     }
   }
 
   return NS_OK;
 }
 
-NS_IMPL_ADDREF(nsMsgMailList)
-NS_IMPL_RELEASE(nsMsgMailList)
-
-NS_INTERFACE_MAP_BEGIN(nsMsgMailList)
-   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsISupports)
-NS_INTERFACE_MAP_END
-
-
-nsMsgMailList::nsMsgMailList()
-{
-}
-
-nsMsgMailList::nsMsgMailList(nsString listName, nsString listDescription, nsIAbDirectory* directory) :
+nsMsgMailList::nsMsgMailList(nsIAbDirectory* directory) :
   mDirectory(directory)
 {
   nsCOMPtr<nsIMsgHeaderParser> parser (do_GetService(NS_MAILNEWS_MIME_HEADER_PARSER_CONTRACTID));
 
+  nsString listName, listDescription;
+  mDirectory->GetDirName(listName);
+  mDirectory->GetDescription(listDescription);
+
   if (parser)
     parser->MakeFullAddress(listName,
                             listDescription.IsEmpty() ? listName : listDescription,
                             mFullName);
 
   if (mFullName.IsEmpty())
   {
       //oops, parser problem! I will try to do my best...
@@ -5561,12 +5521,8 @@ nsMsgMailList::nsMsgMailList(nsString li
         mFullName += listName;
       else
         mFullName += listDescription;
       mFullName.Append(PRUnichar('>'));
   }
 
   mDirectory = directory;
 }
-
-nsMsgMailList::~nsMsgMailList()
-{
-}
--- a/mailnews/compose/src/nsMsgCompose.h
+++ b/mailnews/compose/src/nsMsgCompose.h
@@ -23,16 +23,17 @@
 #include "nsIDOMNode.h"
 
 // Forward declares
 class QuotingOutputStreamListener;
 class nsMsgComposeSendListener;
 class nsIEditorMailSupport;
 class nsIRDFService;
 class nsIArray;
+struct nsMsgMailList;
 
 class nsMsgCompose : public nsIMsgCompose, public nsSupportsWeakReference
 {
  public: 
 
 	nsMsgCompose();
 	virtual ~nsMsgCompose();
 
@@ -77,18 +78,20 @@ private:
 
  private:
   nsresult _SendMsg(MSG_DeliverMode deliverMode, nsIMsgIdentity *identity, const char *accountKey, bool entityConversionDone);
   nsresult CreateMessage(const char * originalMsgURI, MSG_ComposeType type, nsIMsgCompFields* compFields);
   void CleanUpRecipients(nsString& recipients);
   nsresult GetABDirectories(const nsACString& aDirUri,
                             nsCOMArray<nsIAbDirectory> &aDirArray);
   nsresult BuildMailListArray(nsIAbDirectory* parentDir,
-                              nsISupportsArray* array);
-  nsresult GetMailListAddresses(nsString& name, nsISupportsArray* mailListArray, nsIMutableArray** addresses);
+                              nsTArray<nsMsgMailList>& array);
+  nsresult GetMailListAddresses(nsString& name,
+                                nsTArray<nsMsgMailList>& mailListArray,
+                                nsIMutableArray** addresses);
   nsresult TagConvertible(nsIDOMNode *node,  int32_t *_retval);
   nsresult _BodyConvertible(nsIDOMNode *node, int32_t *_retval);
 
   bool IsLastWindow();
  
        // Helper function. Parameters are not checked.
   bool                                      mConvertStructs;    // for TagConvertible
   
@@ -207,23 +210,17 @@ public:
 private:
   nsWeakPtr               mWeakComposeObj;
 	MSG_DeliverMode         mDeliverMode;
 };
 
 /******************************************************************************
  * nsMsgMailList
  ******************************************************************************/
-class nsMsgMailList : public nsISupports
+struct nsMsgMailList
 {
-public:
-  nsMsgMailList();
-  nsMsgMailList(nsString listName, nsString listDescription, nsIAbDirectory* directory);
-	virtual ~nsMsgMailList();
+  explicit nsMsgMailList(nsIAbDirectory* directory);
 
-  NS_DECL_ISUPPORTS
-  
-public:
   nsString mFullName;  /* full email address (name + email) */
   nsCOMPtr<nsIAbDirectory> mDirectory;
 };
 
 #endif /* _nsMsgCompose_H_ */