From 36bfe5b679f77b0b2d0b6cdcb26bdbaeb22aed35 Mon Sep 17 00:00:00 2001 From: "nhotta%netscape.com" Date: Thu, 14 Sep 2000 19:54:18 +0000 Subject: [PATCH] Changed to convert to unicode to avoid confuse ScanTXT when saving a message as a html file, bug 50413, r=rhp. --- mailnews/mime/src/mimetpfl.cpp | 55 ++++++++++++++++++++++++++++++---- mailnews/mime/src/mimetpla.cpp | 49 ++++++++++++++++++++++++++++-- 2 files changed, 95 insertions(+), 9 deletions(-) diff --git a/mailnews/mime/src/mimetpfl.cpp b/mailnews/mime/src/mimetpfl.cpp index 65423fe69dfe..8105499245af 100644 --- a/mailnews/mime/src/mimetpfl.cpp +++ b/mailnews/mime/src/mimetpfl.cpp @@ -33,6 +33,7 @@ #include "nsIServiceManager.h" #include "mimemoz2.h" #include "prprf.h" +#include "nsMsgI18N.h" static NS_DEFINE_CID(kPrefCID, NS_PREF_CID); @@ -335,13 +336,55 @@ MimeInlineTextPlainFlowed_parse_line (char *line, PRInt32 length, MimeObject *ob might not be able to display the glyphs. */ } - /* This is the main TXT to HTML conversion: - escaping (very important), eventually recognizing etc. */ - rv = conv->ScanTXT(lineSource.GetUnicode(), whattodo, &wresult); - if (NS_FAILED(rv)) return -1; + // Get a mail charset of this message. + MimeInlineText *inlinetext = (MimeInlineText *) obj; + char *mailCharset = NULL; + if (inlinetext->charset && *(inlinetext->charset)) + mailCharset = inlinetext->charset; - lineResult = wresult; - Recycle(wresult); + if (obj->options->format_out != nsMimeOutput::nsMimeMessageSaveAs || + !mailCharset || !nsMsgI18Nstateful_charset(mailCharset)) + { + /* This is the main TXT to HTML conversion: + escaping (very important), eventually recognizing etc. */ + rv = conv->ScanTXT(lineSource.GetUnicode(), whattodo, &wresult); + if (NS_FAILED(rv)) return -1; + lineResult = wresult; + Recycle(wresult); + } + else + { + // If nsMimeMessageSaveAs, the string is in mail charset (and stateful, e.g. ISO-2022-JP). + // convert to unicode so it won't confuse ScanTXT. + char *newcstr; + + newcstr = lineSource.ToNewCString(); // lineSource uses nsString but the string is NOT unicode + if (!newcstr) return -1; + + nsAutoString ustr; + nsCAutoString cstr; + + cstr.Assign(newcstr); + Recycle(newcstr); + + rv = nsMsgI18NConvertToUnicode((nsCAutoString) mailCharset, cstr, ustr); + if (NS_SUCCEEDED(rv)) + { + PRUnichar *u; + rv = conv->ScanTXT(ustr.GetUnicode(), whattodo, &u); + if (NS_SUCCEEDED(rv)) + { + ustr.Assign(u); + Recycle(u); + rv = nsMsgI18NConvertFromUnicode((nsCAutoString) mailCharset, ustr, cstr); + if (NS_SUCCEEDED(rv)) + lineResult.AssignWithConversion(cstr); // create nsString which contains NON unicode + // as the following code expecting it + } + } + if (NS_FAILED(rv)) + return -1; + } } else { diff --git a/mailnews/mime/src/mimetpla.cpp b/mailnews/mime/src/mimetpla.cpp index d870ce2cc0fc..ccc6343a5c22 100644 --- a/mailnews/mime/src/mimetpla.cpp +++ b/mailnews/mime/src/mimetpla.cpp @@ -35,6 +35,7 @@ #include "nsIServiceManager.h" #include "nsIPref.h" #include "prprf.h" +#include "nsMsgI18N.h" static NS_DEFINE_CID(kTXTToHTMLConvCID, MOZITXTTOHTMLCONV_CID); static NS_DEFINE_CID(kCPrefServiceCID, NS_PREF_CID); @@ -423,12 +424,54 @@ MimeInlineTextPlain_parse_line (char *line, PRInt32 length, MimeObject *obj) prefaceResultStr += "
"; } + // Get a mail charset of this message. + MimeInlineText *inlinetext = (MimeInlineText *) obj; + char *mailCharset = NULL; + if (inlinetext->charset && *(inlinetext->charset)) + mailCharset = inlinetext->charset; + /* This is the main TXT to HTML conversion: escaping (very important), eventually recognizing etc. */ PRUnichar* lineResultUnichar = nsnull; - rv = conv->ScanTXT(lineSourceStr.GetUnicode() + logicalLineStart, - whattodo, &lineResultUnichar); - if (NS_FAILED(rv)) return -1; + + if (obj->options->format_out != nsMimeOutput::nsMimeMessageSaveAs || + !mailCharset || !nsMsgI18Nstateful_charset(mailCharset)) + { + rv = conv->ScanTXT(lineSourceStr.GetUnicode() + logicalLineStart, + whattodo, &lineResultUnichar); + if (NS_FAILED(rv)) return -1; + } + else + { + // If nsMimeMessageSaveAs, the string is in mail charset (and stateful, e.g. ISO-2022-JP). + // convert to unicode so it won't confuse ScanTXT. + nsAutoString ustr; + nsCAutoString cstr(line, length); + + rv = nsMsgI18NConvertToUnicode((nsCAutoString) mailCharset, cstr, ustr); + if (NS_SUCCEEDED(rv)) + { + PRUnichar *u; + rv = conv->ScanTXT(ustr.GetUnicode() + logicalLineStart, whattodo, &u); + if (NS_SUCCEEDED(rv)) + { + ustr.Assign(u); + Recycle(u); + rv = nsMsgI18NConvertFromUnicode((nsCAutoString) mailCharset, ustr, cstr); + if (NS_SUCCEEDED(rv)) + { + // create PRUnichar* which contains NON unicode + // as the following code expecting it + ustr.AssignWithConversion(cstr); + lineResultUnichar = ustr.ToNewUnicode(); + if (!lineResultUnichar) return -1; + } + } + } + if (NS_FAILED(rv)) + return -1; + } + // avoid an extra string copy by using nsSubsumeStr, this transfers // ownership of wresult to strresult so don't try to free wresult later.