nsMessenger::SaveAs(const char* url, PRBool asFile)
nsMessenger::SaveAs(const char* url, PRBool asFile, nsIMsgIdentity* identity)
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,      // contentType                                                         -1,          // contentLength                                                         nsnull,      // inputStream                                                         nsnull,      // loadGroup                                                         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;}