rv = GetMessageServiceFromURI(url, &messageService);  if (NS_SUCCEEDED(rv) && messageService)
filePicker->SetDisplayDirectory(lastSaveDir); }  rv = filePicker->Show(&dialogResult); if (NS_FAILED(rv) || dialogResult == nsIFilePicker::returnCancel) goto done;  nsCOMPtr<nsILocalFile> localFile; rv = filePicker->GetFile(getter_AddRefs(localFile)); if (NS_FAILED(rv)) goto done;  if (dialogResult == nsIFilePicker::returnReplace) {  PRBool isFile; rv = localFile->IsFile(&isFile); if (NS_SUCCEEDED(rv) && isFile) { rv = localFile->Remove(PR_FALSE ); NS_ENSURE_SUCCESS(rv, rv); } }  rv = SetLastSaveDirectory(localFile); if (NS_FAILED(rv)) goto done;  rv = filePicker->GetFilterIndex(&saveAsFileType); if (NS_FAILED(rv)) goto done;  nsAutoString fileName; rv = localFile->GetLeafName(fileName); NS_ENSURE_SUCCESS(rv, rv);  switch ( saveAsFileType ) {  case HTML_FILE_TYPE: if ( (fileName.RFind(HTML_FILE_EXTENSION, PR_TRUE, -1, sizeof(HTML_FILE_EXTENSION)-1) == kNotFound) && (fileName.RFind(HTML_FILE_EXTENSION2, PR_TRUE, -1, sizeof(HTML_FILE_EXTENSION2)-1) == kNotFound) ) { fileName.Append(NS_LITERAL_STRING(HTML_FILE_EXTENSION2)); localFile->SetLeafName(fileName); } break; case TEXT_FILE_TYPE: if (fileName.RFind(TEXT_FILE_EXTENSION, PR_TRUE, -1, sizeof(TEXT_FILE_EXTENSION)-1) == kNotFound) { fileName.Append(NS_LITERAL_STRING(TEXT_FILE_EXTENSION)); localFile->SetLeafName(fileName); } break; case EML_FILE_TYPE: if (fileName.RFind(EML_FILE_EXTENSION, PR_TRUE, -1, sizeof(EML_FILE_EXTENSION)-1) == kNotFound) { fileName.Append(NS_LITERAL_STRING(EML_FILE_EXTENSION)); localFile->SetLeafName(fileName); } break; case ANY_FILE_TYPE: default:   PRBool noExtensionFound = PR_FALSE; if (fileName.RFind(".", 1) != kNotFound) { if ( (fileName.RFind(HTML_FILE_EXTENSION, PR_TRUE, -1, sizeof(HTML_FILE_EXTENSION)-1) != kNotFound) || (fileName.RFind(HTML_FILE_EXTENSION2, PR_TRUE, -1, sizeof(HTML_FILE_EXTENSION2)-1) != kNotFound) ) saveAsFileType = HTML_FILE_TYPE; else if (fileName.RFind(TEXT_FILE_EXTENSION, PR_TRUE, -1, sizeof(TEXT_FILE_EXTENSION)-1) != kNotFound) saveAsFileType = TEXT_FILE_TYPE; else if (fileName.RFind(EML_FILE_EXTENSION, PR_TRUE, -1, sizeof(EML_FILE_EXTENSION)-1) != kNotFound) saveAsFileType = EML_FILE_TYPE; else noExtensionFound = PR_TRUE; } else noExtensionFound = PR_TRUE;  if (noExtensionFound) { saveAsFileType = EML_FILE_TYPE; fileName.Append(NS_LITERAL_STRING(EML_FILE_EXTENSION)); localFile->SetLeafName(fileName); } break; }     nsCOMPtr<nsIFileSpec> fileSpec; rv = NS_NewFileSpecFromIFile(localFile, 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);  rv = saveListener->QueryInterface(NS_GET_IID(nsIUrlListener), getter_AddRefs(urlListener)); if (NS_FAILED(rv)) goto done;  if (saveAsFileType == EML_FILE_TYPE) { rv = messageService->SaveMessageToDisk(aURI, fileSpec, PR_FALSE, urlListener, nsnull, PR_FALSE, mMsgWindow); } else { nsCAutoString urlString(aURI);         if (saveAsFileType == TEXT_FILE_TYPE)
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;}