nsIMsgMessageService* messageService = nsnull; rv = GetMessageServiceFromURI(url, &messageService); if (NS_SUCCEEDED(rv) && messageService)
filePicker->SetDisplayDirectory(lastSaveDir); }  nsCOMPtr<nsILocalFile> localFile; nsAutoString fileName; rv = filePicker->Show(&dialogResult); if (NS_FAILED(rv) || dialogResult == nsIFilePicker::returnCancel) goto done;  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;  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;        if (url)    {      nsIMsgMessageService* messageService = nsnull;      rv = GetMessageServiceFromURI(url, &messageService);      if (NS_SUCCEEDED(rv) && messageService)      {        nsCOMPtr<nsIFileSpec> aSpec;        if (asFile)        {          nsCOMPtr<nsIFileSpecWithUI>            fileSpec(getter_AddRefs(NS_CreateFileSpecWithUI()));          if (!fileSpec) return NS_ERROR_FAILURE;          rv = fileSpec->ChooseOutputFile("Save Message", "",                                nsIFileSpecWithUI::eAllMailOutputFilters);          aSpec = do_QueryInterface(fileSpec, &rv);        }        else        {          nsFileSpec tmpFileSpec("msgTemp.eml");          rv = NS_NewFileSpecWithSpec(tmpFileSpec, getter_AddRefs(aSpec));        }        if (NS_SUCCEEDED(rv))        {          nsCOMPtr<nsIUrlListener> urlListener;          if (!asFile && aSpec)          {            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            }          }          if (NS_SUCCEEDED(rv))            messageService->SaveMessageToDisk(url, aSpec, PR_TRUE,                                              urlListener, nsnull);        }      }    }	return rv;}