rv = CreateStartupUrl(urlCString, getter_AddRefs(aURL)); nsCRT::free(urlCString);  if (NS_SUCCEEDED(rv)) { rv = GetMessageServiceFromURI(messageUri, &messageService); if (NS_SUCCEEDED(rv)) { fetchService = do_QueryInterface(messageService);  if (fetchService) { PRInt32 sectionPos = urlString.Find("?section"); nsString mimePart;  urlString.Right(mimePart, urlString.Length() - sectionPos); fullMessageUri.AppendWithConversion(mimePart);  messageUri = fullMessageUri.get(); }  nsCOMPtr<nsIStreamListener> convertedListener; aListener->QueryInterface(NS_GET_IID(nsIStreamListener), getter_AddRefs(convertedListener)); #ifndef XP_MAC   if (contentType && !nsCRT::strcasecmp(APPLICATION_BINHEX, contentType)) { nsCOMPtr<nsIStreamListener> listener (do_QueryInterface(convertedListener)); nsCOMPtr<nsIStreamConverterService> streamConverterService = do_GetService(kIStreamConverterServiceCID, &rv); nsCOMPtr<nsISupports> channelSupport = do_QueryInterface(aListener->m_channel);  rv = streamConverterService->AsyncConvertData(NS_ConvertASCIItoUCS2(APPLICATION_BINHEX).get(), NS_LITERAL_STRING("*/*").get(), listener, channelSupport, getter_AddRefs(convertedListener)); } #endif if (fetchService) rv = fetchService->FetchMimePart(aURL, messageUri, convertedListener, mMsgWindow, nsnull,nsnull); else rv = messageService->DisplayMessage(messageUri, convertedListener, mMsgWindow,nsnull, nsnull, nsnull); } }  if (NS_FAILED(rv)) { NS_IF_RELEASE(aListener); Alert("saveAttachmentFailed"); } return rv;
done: PR_FREEIF(unescapedUrl); return rv;
nsMessenger::SaveAttachment(nsIFileSpec * fileSpec,                            const char * unescapedUrl,                            const char * messageUri,                            const char * contentType,                            void *closure){  nsIMsgMessageService * messageService = nsnull;  nsSaveMsgListener *aListener = nsnull;  nsSaveAllAttachmentsState *saveState= (nsSaveAllAttachmentsState*) closure;  nsCOMPtr<nsISupports> channelSupport;  nsCOMPtr<nsIMsgMessageFetchPartService> fetchService;  nsAutoString urlString;  char *urlCString = nsnull;  nsCOMPtr<nsIURI> aURL;  nsCAutoString fullMessageUri(messageUri);  nsresult rv = NS_OK;  // XXX whacky ref counting here...what's the deal? when does aListener get released? it's not clear.  aListener = new nsSaveMsgListener(fileSpec, this);  if (!aListener)  {    return NS_ERROR_OUT_OF_MEMORY;  }  NS_ADDREF(aListener);  aListener->m_contentType = contentType;  if (saveState)      aListener->m_saveAllAttachmentsState = saveState;  urlString.AssignWithConversion(unescapedUrl);  urlString.ReplaceSubstring(NS_LITERAL_STRING("/;section").get(), NS_LITERAL_STRING("?section").get());  urlCString = ToNewCString(urlString);  rv = CreateStartupUrl(urlCString, getter_AddRefs(aURL));  nsCRT::free(urlCString);  if (NS_SUCCEEDED(rv))  {    rv = GetMessageServiceFromURI(messageUri, &messageService);    if (NS_SUCCEEDED(rv))    {      fetchService = do_QueryInterface(messageService);      // if the message service has a fetch part service then we know we can fetch mime parts...      if (fetchService)      {        PRInt32 sectionPos = urlString.Find("?section");        nsString mimePart;        urlString.Right(mimePart, urlString.Length() - sectionPos);        fullMessageUri.AppendWithConversion(mimePart);           messageUri = fullMessageUri.get();      }      nsCOMPtr<nsIStreamListener> convertedListener;      aListener->QueryInterface(NS_GET_IID(nsIStreamListener),                                 getter_AddRefs(convertedListener));#ifndef XP_MAC      // if the content type is bin hex we are going to do a hokey hack and make sure we decode the bin hex       // when saving an attachment to disk..      if (contentType && !nsCRT::strcasecmp(APPLICATION_BINHEX, contentType))      {        nsCOMPtr<nsIStreamListener> listener (do_QueryInterface(convertedListener));        nsCOMPtr<nsIStreamConverterService> streamConverterService = do_GetService(kIStreamConverterServiceCID, &rv);        nsCOMPtr<nsISupports> channelSupport = do_QueryInterface(aListener->m_channel);                  rv = streamConverterService->AsyncConvertData(NS_ConvertASCIItoUCS2(APPLICATION_BINHEX).get(),                                                      NS_LITERAL_STRING("*/*").get(),                                                       listener,                                                      channelSupport,                                                      getter_AddRefs(convertedListener));      }#endif      if (fetchService)        rv = fetchService->FetchMimePart(aURL, messageUri, convertedListener, mMsgWindow, nsnull,nsnull);      else        rv = messageService->DisplayMessage(messageUri, convertedListener, mMsgWindow,nsnull, nsnull, nsnull);     } // if we got a message service  } // if we created a url  if (NS_FAILED(rv))  {      NS_IF_RELEASE(aListener);      Alert("saveAttachmentFailed");  }	return rv;}