}
else { saveListener->m_outputFormat = NS_LITERAL_STRING(TEXT_HTML); saveListener->m_doCharsetConversion = PR_FALSE; urlString += NS_LITERAL_CSTRING("?header=saveas"); }  rv = CreateStartupUrl(urlString.get(), getter_AddRefs(url)); NS_ASSERTION(NS_SUCCEEDED(rv), "CreateStartupUrl failed"); if (NS_FAILED(rv)) goto done;  saveListener->m_channel = nsnull; rv = NS_NewInputStreamChannel(getter_AddRefs(saveListener->m_channel), url, nsnull, NS_LITERAL_CSTRING(""), NS_LITERAL_CSTRING("")); NS_ASSERTION(NS_SUCCEEDED(rv), "NS_NewInputStreamChannel failed"); if (NS_FAILED(rv)) goto done;  nsCOMPtr<nsIStreamConverterService> streamConverterService = do_GetService("@mozilla.org/streamConverters;1"); nsCOMPtr<nsISupports> channelSupport = do_QueryInterface(saveListener->m_channel);     rv = streamConverterService->AsyncConvertData(NS_LITERAL_STRING(MESSAGE_RFC822).get(), NS_LITERAL_STRING(TEXT_HTML).get(), saveListener, channelSupport, getter_AddRefs(convertedListener)); NS_ASSERTION(NS_SUCCEEDED(rv), "AsyncConvertData failed"); if (NS_FAILED(rv)) goto done;  rv = messageService->DisplayMessage(urlString.get(), convertedListener, mMsgWindow, nsnull, nsnull, nsnull); } } else {  nsCOMPtr<nsIFileSpec> fileSpec; nsFileSpec tmpFileSpec("nsmail.tmp"); rv = NS_NewFileSpecWithSpec(tmpFileSpec, getter_AddRefs(fileSpec)); if (NS_FAILED(rv)) goto done;    saveListener = new nsSaveMsgListener(fileSpec, this); if (!saveListener) { rv = NS_ERROR_OUT_OF_MEMORY; goto done; } NS_ADDREF(saveListener);  if (aIdentity) rv = aIdentity->GetStationeryFolder(getter_Copies(saveListener->m_templateUri)); if (NS_FAILED(rv)) goto done;  PRBool needDummyHeader = PL_strcasestr(saveListener->m_templateUri, "mailbox: != nsnull; PRBool canonicalLineEnding = PL_strcasestr(saveListener->m_templateUri, "imap: != nsnull;  rv = saveListener->QueryInterface( NS_GET_IID(nsIUrlListener), getter_AddRefs(urlListener)); if (NS_FAILED(rv)) goto done;  rv = messageService->SaveMessageToDisk(aURI, fileSpec, needDummyHeader, urlListener, nsnull, canonicalLineEnding, mMsgWindow); }
nsMessenger::SaveAs(const char* url, PRBool asFile){	nsresult rv = NS_OK;    nsIMsgMessageService* messageService = nsnull;        if (url)    {      rv = GetMessageServiceFromURI(url, &messageService);      if (NS_SUCCEEDED(rv) && messageService)      {        nsCOMPtr<nsIFileSpec> aSpec;        PRUint32 saveAsFileType = 0; // 0 - raw, 1 = html, 2 = text;        if (asFile)        {          nsCOMPtr<nsIFileSpecWithUI>            fileSpec(getter_AddRefs(NS_CreateFileSpecWithUI()));           if (!fileSpec) {            rv = NS_ERROR_FAILURE;            goto done;          }          rv = fileSpec->ChooseOutputFile("Save Message", "",                                nsIFileSpecWithUI::eAllMailOutputFilters);          if (NS_FAILED(rv)) goto done;          char *fileName = nsnull;          fileSpec->GetLeafName(&fileName);          nsCString tmpFilenameString = fileName;                nsCRT::free(fileName);          if (tmpFilenameString.RFind(".htm", PR_TRUE) != -1)            saveAsFileType = 1;          else if (tmpFilenameString.RFind(".txt", PR_TRUE) != -1)            saveAsFileType = 2;                    aSpec = do_QueryInterface(fileSpec, &rv);        }        else        {          nsFileSpec tmpFileSpec("msgTemp.eml");          rv = NS_NewFileSpecWithSpec(tmpFileSpec, getter_AddRefs(aSpec));        }        if (NS_FAILED(rv)) goto done;        nsCOMPtr<nsIUrlListener> urlListener;        if (aSpec)        {          if (asFile)          {            switch (saveAsFileType)             {              case 0:              default:                messageService->SaveMessageToDisk(url, aSpec, PR_TRUE,                                                  urlListener, nsnull);                break;              case 1:              case 2:              {                nsSaveAsListener *aListener = new nsSaveAsListener(aSpec);                if (aListener)                {                  NS_ADDREF(aListener);                  nsCOMPtr<nsIURI> aURL;                  nsString urlString = url;                  urlString += "?header=none";                  char *urlCString = urlString.ToNewCString();                  rv = CreateStartupUrl(urlCString, getter_AddRefs(aURL));                  nsCRT::free(urlCString);                  if (NS_FAILED(rv))                   {                    NS_RELEASE(aListener);                    goto done;                  }                  aListener->m_channel = null_nsCOMPtr();                  NS_WITH_SERVICE(nsIIOService, netService, kIOServiceCID,                                  &rv);                  rv = netService->NewInputStreamChannel(aURL, nsnull,                                                         nsnull, nsnull,                                     getter_AddRefs(aListener->m_channel));                  nsAutoString from, to;                  from = "message/rfc822";                  to = saveAsFileType == 1 ? "text/html" : "text/plain";                  NS_WITH_SERVICE(nsIStreamConverterService,                                  streamConverterService,                                    kIStreamConverterServiceCID, &rv);                  nsCOMPtr<nsISupports> channelSupport =                    do_QueryInterface(aListener->m_channel);                  nsCOMPtr<nsIStreamListener> convertedListener;                  rv = streamConverterService->AsyncConvertData(                    from.GetUnicode(), to.GetUnicode(), aListener,                    channelSupport, getter_AddRefs(convertedListener));                  if (NS_SUCCEEDED(rv))                    messageService->DisplayMessage(url, convertedListener,                                                   nsnull, nsnull);                }              }            }          }          else          {             // ** save as Template            nsSaveAsListener *aListener = new nsSaveAsListener(aSpec);            if (aListener)            {              rv = aListener->QueryInterface(                nsCOMTypeInfo<nsIUrlListener>::GetIID(),                getter_AddRefs(urlListener));              if (NS_FAILED(rv))              {                delete aListener;                return rv;              }              NS_ADDREF(aListener); // nsUrlListenerManager uses nsVoidArray              // to keep trach of all listeners we have              // to manually add refs ourself              messageService->SaveMessageToDisk(url, aSpec, PR_TRUE,                                                urlListener, nsnull);            }          }        }      }    }done:    if (messageService)        ReleaseMessageServiceFromURI(url, messageService);	return rv;}