Bug 1506422 - Replace use of nsMsgI18NFileSystemCharset() with NS_CopyUnicodeToNative/NS_CopyNativeToUnicode(). r=emk
authorJorg K <jorgk@jorgk.com>
Thu, 15 Nov 2018 15:39:15 +0100
changeset 33711 4a063ea24a79686f14f7692e9645de9c1989469f
parent 33710 7e28b9f45b19f9301f26d4428bb956efbe859764
child 33712 9e43ff6048ca92b12a036afd30c3f16848595c0c
push id388
push userclokep@gmail.com
push dateMon, 28 Jan 2019 20:54:56 +0000
reviewersemk
bugs1506422
Bug 1506422 - Replace use of nsMsgI18NFileSystemCharset() with NS_CopyUnicodeToNative/NS_CopyNativeToUnicode(). r=emk
mailnews/addrbook/src/nsAbManager.cpp
mailnews/base/search/src/nsMsgFilter.cpp
mailnews/base/search/src/nsMsgFilterList.cpp
mailnews/base/src/nsMessenger.cpp
mailnews/compose/src/nsMsgSend.cpp
--- a/mailnews/addrbook/src/nsAbManager.cpp
+++ b/mailnews/addrbook/src/nsAbManager.cpp
@@ -3,17 +3,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsAbManager.h"
 #include "nsAbBaseCID.h"
 #include "nsAddrDatabase.h"
 #include "nsIOutputStream.h"
 #include "nsNetUtil.h"
-#include "nsMsgI18N.h"
+#include "nsNativeCharsetUtils.h"
 #include "nsIStringBundle.h"
 #include "nsMsgUtils.h"
 #include "nsAppDirectoryServiceDefs.h"
 #include "plstr.h"
 #include "prmem.h"
 #include "mozIDOMWindow.h"
 #include "plbase64.h"
 #include "nsIWindowWatcher.h"
@@ -725,21 +725,20 @@ nsAbManager::ExportDirectoryToDelimitedT
   for (i = 0; i < ArrayLength(EXPORT_ATTRIBUTES_TABLE); i++) {
     if (EXPORT_ATTRIBUTES_TABLE[i].plainTextStringID != 0) {
 
       // We don't need to truncate the string here as getter_Copies will
       // do that for us.
       if (NS_FAILED(bundle->GetStringFromID(EXPORT_ATTRIBUTES_TABLE[i].plainTextStringID, columnName)))
         columnName.AppendInt(EXPORT_ATTRIBUTES_TABLE[i].plainTextStringID);
 
-      rv = nsMsgI18NConvertFromUnicode(useUTF8 ? NS_LITERAL_CSTRING("UTF-8") :
-                                                 nsMsgI18NFileSystemCharset(),
-                                       columnName,
-                                       revisedName);
-      NS_ENSURE_SUCCESS(rv,rv);
+      if (useUTF8)
+        CopyUTF16toUTF8(columnName, revisedName);
+      else
+        NS_CopyUnicodeToNative(columnName, revisedName);
 
       rv = outputStream->Write(revisedName.get(),
                                revisedName.Length(),
                                &writeCount);
       NS_ENSURE_SUCCESS(rv,rv);
 
       if (revisedName.Length() != writeCount)
         return NS_ERROR_FAILURE;
@@ -818,26 +817,20 @@ nsAbManager::ExportDirectoryToDelimitedT
                   needsQuotes = true;
 
               if (needsQuotes)
               {
                 newValue.InsertLiteral(u"\"", 0);
                 newValue.Append(u'"');
               }
 
-              rv = nsMsgI18NConvertFromUnicode(useUTF8 ? NS_LITERAL_CSTRING("UTF-8") :
-                                                         nsMsgI18NFileSystemCharset(),
-                                               newValue,
-                                               valueCStr);
-              NS_ENSURE_SUCCESS(rv,rv);
-
-              if (NS_FAILED(rv)) {
-                NS_ERROR("failed to convert string to system charset.  use LDIF");
-                valueCStr = "?";
-              }
+              if (useUTF8)
+                CopyUTF16toUTF8(newValue, valueCStr);
+              else
+                NS_CopyUnicodeToNative(newValue, valueCStr);
 
               length = valueCStr.Length();
               if (length) {
                 rv = outputStream->Write(valueCStr.get(), length, &writeCount);
                 NS_ENSURE_SUCCESS(rv,rv);
                 if (length != writeCount)
                   return NS_ERROR_FAILURE;
               }
--- a/mailnews/base/search/src/nsMsgFilter.cpp
+++ b/mailnews/base/search/src/nsMsgFilter.cpp
@@ -14,16 +14,17 @@
 #include "nsMsgFilter.h"
 #include "nsMsgUtils.h"
 #include "nsMsgLocalSearch.h"
 #include "nsMsgSearchTerm.h"
 #include "nsIMsgAccountManager.h"
 #include "nsIMsgIncomingServer.h"
 #include "nsMsgSearchValue.h"
 #include "nsMsgI18N.h"
+#include "nsNativeCharsetUtils.h"
 #include "nsIMutableArray.h"
 #include "nsIOutputStream.h"
 #include "nsIStringBundle.h"
 #include "nsComponentManagerUtils.h"
 #include "nsServiceManagerUtils.h"
 #include "nsIMsgFilterService.h"
 #include "prmem.h"
 #include "mozilla/ArrayUtils.h"
@@ -742,22 +743,19 @@ nsresult nsMsgFilter::ConvertMoveOrCopyT
     // if relative path starts with kImap, this is a move to folder on the same server
     if (moveValue.Find(kImapPrefix) == 0)
     {
       int32_t prefixLen = PL_strlen(kImapPrefix);
       nsAutoCString originalServerPath(Substring(moveValue, prefixLen));
       if (filterVersion == k45Version)
       {
         nsAutoString unicodeStr;
-        nsresult rv = nsMsgI18NConvertToUnicode(nsMsgI18NFileSystemCharset(),
-                                                originalServerPath,
-                                                unicodeStr);
-        NS_ENSURE_SUCCESS(rv, rv);
+        NS_CopyNativeToUnicode(originalServerPath, unicodeStr);
 
-        rv = CopyUTF16toMUTF7(unicodeStr, originalServerPath);
+        nsresult rv = CopyUTF16toMUTF7(unicodeStr, originalServerPath);
         NS_ENSURE_SUCCESS(rv, rv);
       }
 
       nsCOMPtr <nsIMsgFolder> destIFolder;
       if (rootFolder)
       {
         rootFolder->FindSubFolder(originalServerPath, getter_AddRefs(destIFolder));
         if (destIFolder)
@@ -805,19 +803,17 @@ nsresult nsMsgFilter::ConvertMoveOrCopyT
         nsCString unescapedMoveValue;
         MsgUnescapeString(moveValue, 0, unescapedMoveValue);
         moveValue = unescapedMoveValue;
 #endif
         destFolderUri.Append('/');
         if (filterVersion == k45Version)
         {
           nsAutoString unicodeStr;
-          rv = nsMsgI18NConvertToUnicode(nsMsgI18NFileSystemCharset(),
-                                         moveValue, unicodeStr);
-          NS_ENSURE_SUCCESS(rv, rv);
+          NS_CopyNativeToUnicode(moveValue, unicodeStr);
           rv = NS_MsgEscapeEncodeURLPath(unicodeStr, moveValue);
         }
         destFolderUri.Append(moveValue);
         localMailRoot->GetChildWithURI(destFolderUri, true, false /*caseInsensitive*/, getter_AddRefs(destIMsgFolder));
 
         if (destIMsgFolder)
         {
           destIMsgFolder->GetURI(folderUri);
--- a/mailnews/base/search/src/nsMsgFilterList.cpp
+++ b/mailnews/base/search/src/nsMsgFilterList.cpp
@@ -14,29 +14,22 @@
 #include "nsMsgUtils.h"
 #include "nsMsgSearchTerm.h"
 #include "nsString.h"
 #include "nsMsgBaseCID.h"
 #include "nsIMsgFilterService.h"
 #include "nsMsgSearchScopeTerm.h"
 #include "nsNetUtil.h"
 #include "nsIInputStream.h"
-#include "nsMsgI18N.h"
+#include "nsNativeCharsetUtils.h"
 #include "nsMemory.h"
 #include "prmem.h"
 #include "mozilla/ArrayUtils.h"
 #include <ctype.h>
 
-// unicode "%s" format string
-static const char16_t unicodeFormatter[] = {
-    (char16_t)'%',
-    (char16_t)'s',
-    (char16_t)0,
-};
-
 // Marker for EOF or failure during read
 #define EOF_CHAR -1
 
 nsMsgFilterList::nsMsgFilterList() :
     m_fileVersion(0)
 {
   m_loggingEnabled = false;
   m_startWritingToBuffer = false;
@@ -555,33 +548,26 @@ nsresult nsMsgFilterList::LoadTextFilter
         }
         nsMsgFilter *filter = new nsMsgFilter;
         if (filter == nullptr)
         {
           err = NS_ERROR_OUT_OF_MEMORY;
           break;
         }
         filter->SetFilterList(static_cast<nsIMsgFilterList*>(this));
+        nsAutoString unicodeStr;
         if (m_fileVersion == k45Version)
         {
-          nsAutoString unicodeStr;
-          err = nsMsgI18NConvertToUnicode(nsMsgI18NFileSystemCharset(),
-                                          value, unicodeStr);
-          if (NS_FAILED(err))
-              break;
-
+          NS_CopyNativeToUnicode(value, unicodeStr);
           filter->SetFilterName(unicodeStr);
         }
         else
         {
-          // ### fix me - this is silly.
-          nsString unicodeString;
-          nsTextFormatter::ssprintf(unicodeString, unicodeFormatter,
-                                    value.get());
-          filter->SetFilterName(unicodeString);
+          CopyUTF8toUTF16(value, unicodeStr);
+          filter->SetFilterName(unicodeStr);
         }
         m_curFilter = filter;
         m_filters.AppendElement(filter);
       }
       break;
     case nsIMsgFilterList::attribEnabled:
       if (m_curFilter)
         m_curFilter->SetEnabled(StrToBool(value));
@@ -667,24 +653,18 @@ nsresult nsMsgFilterList::LoadTextFilter
       }
       break;
     case nsIMsgFilterList::attribCondition:
       if (m_curFilter)
       {
         if (m_fileVersion == k45Version)
         {
           nsAutoString unicodeStr;
-          err = nsMsgI18NConvertToUnicode(nsMsgI18NFileSystemCharset(),
-                                          value, unicodeStr);
-          if (NS_FAILED(err))
-              break;
-
-          char *utf8 = ToNewUTF8String(unicodeStr);
-          value.Assign(utf8);
-          free(utf8);
+          NS_CopyNativeToUnicode(value, unicodeStr);
+          CopyUTF16toUTF8(unicodeStr, value);
         }
         err = ParseCondition(m_curFilter, value.get());
         if (err == NS_ERROR_INVALID_ARG)
           err = m_curFilter->SetUnparseable(true);
         NS_ENSURE_SUCCESS(err, err);
       }
       break;
     case nsIMsgFilterList::attribCustomId:
--- a/mailnews/base/src/nsMessenger.cpp
+++ b/mailnews/base/src/nsMessenger.cpp
@@ -64,17 +64,16 @@
 
 #include "nsIMsgMessageService.h"
 #include "nsMsgRDFUtils.h"
 
 #include "nsIMsgHdr.h"
 #include "nsIMimeMiscStatus.h"
 // compose
 #include "nsMsgCompCID.h"
-#include "nsMsgI18N.h"
 #include "nsNativeCharsetUtils.h"
 
 // draft/folders/sendlater/etc
 #include "nsIMsgCopyService.h"
 #include "nsIMsgCopyServiceListener.h"
 #include "nsIUrlListener.h"
 
 // undo
@@ -1847,30 +1846,23 @@ nsSaveMsgListener::OnStopRequest(nsIRequ
   {
     // For HTML, code is emitted immediately in OnDataAvailable.
     MOZ_ASSERT(m_outputFormat == ePlainText,
       "For HTML, m_doCharsetConversion shouldn't be set");
     NS_ConvertUTF8toUTF16 utf16Buffer(m_msgBuffer);
     ConvertBufToPlainText(utf16Buffer, false, false, false, false);
 
     nsCString outCString;
-    rv = nsMsgI18NConvertFromUnicode(nsMsgI18NFileSystemCharset(),
-      utf16Buffer, outCString, true);
-    if (rv == NS_ERROR_UENC_NOMAPPING) {
-      // If we can't encode with the preferred charset, use UTF-8.
-      CopyUTF16toUTF8(utf16Buffer, outCString);
-      rv = NS_OK;
-    }
-    if (NS_SUCCEEDED(rv))
-    {
-      uint32_t writeCount;
-      rv = m_outputStream->Write(outCString.get(), outCString.Length(), &writeCount);
-      if (outCString.Length() != writeCount)
-        rv = NS_ERROR_FAILURE;
-    }
+    // NS_CopyUnicodeToNative() doesn't return an error, so we have no choice but to
+    // always use UTF-8.
+    CopyUTF16toUTF8(utf16Buffer, outCString);
+    uint32_t writeCount;
+    rv = m_outputStream->Write(outCString.get(), outCString.Length(), &writeCount);
+    if (outCString.Length() != writeCount)
+      rv = NS_ERROR_FAILURE;
   }
 
   if (m_outputStream)
   {
     m_outputStream->Close();
     m_outputStream = nullptr;
   }
 
--- a/mailnews/compose/src/nsMsgSend.cpp
+++ b/mailnews/compose/src/nsMsgSend.cpp
@@ -2481,37 +2481,26 @@ nsMsgComposeAndSend::HackAttachments(nsI
          we need to avoid to call it ourself.
       */
       needToCallGatherMimeAttachments = false;
 
       nsresult status = m_attachments[i]->SnarfAttachment(mCompFields);
       if (NS_FAILED(status))
       {
         nsString errorMsg;
-        nsresult rv = nsMsgI18NConvertToUnicode(nsMsgI18NFileSystemCharset(),
-                                                m_attachments[i]->m_realName,
-                                                attachmentFileName);
-        if (attachmentFileName.IsEmpty() && m_attachments[i]->mURL) {
-          nsCString asciiSpec;
-          m_attachments[i]->mURL->GetAsciiSpec(asciiSpec);
-          attachmentFileName.AssignASCII(asciiSpec.get());
-          rv = NS_OK;
-        }
-        if (NS_SUCCEEDED(rv))
-        {
-          nsCOMPtr<nsIStringBundle> bundle;
-          const char16_t *params[] = { attachmentFileName.get() };
-          mComposeBundle->FormatStringFromName("errorAttachingFile",
-                                               params, 1,
-                                               errorMsg);
-          mSendReport->SetMessage(nsIMsgSendReport::process_Current, errorMsg.get(), false);
-          mSendReport->SetError(nsIMsgSendReport::process_Current,
-                                NS_MSG_ERROR_ATTACHING_FILE,
-                                false);
-        }
+        NS_CopyNativeToUnicode(m_attachments[i]->m_realName, attachmentFileName);
+        nsCOMPtr<nsIStringBundle> bundle;
+        const char16_t *params[] = { attachmentFileName.get() };
+        mComposeBundle->FormatStringFromName("errorAttachingFile",
+                                             params, 1,
+                                             errorMsg);
+        mSendReport->SetMessage(nsIMsgSendReport::process_Current, errorMsg.get(), false);
+        mSendReport->SetError(nsIMsgSendReport::process_Current,
+                              NS_MSG_ERROR_ATTACHING_FILE,
+                              false);
         return NS_MSG_ERROR_ATTACHING_FILE;
       }
       if (m_be_synchronous_p)
         break;
     }
   }
 
   // If no attachments - finish now (this will call the done_callback).