summaryrefslogtreecommitdiffstats
path: root/mailnews/mime/src/comi18n.cpp
diff options
context:
space:
mode:
authorMatt A. Tobin <email@mattatobin.com>2019-11-03 00:17:46 -0400
committerMatt A. Tobin <email@mattatobin.com>2019-11-03 00:17:46 -0400
commit302bf1b523012e11b60425d6eee1221ebc2724eb (patch)
treeb191a895f8716efcbe42f454f37597a545a6f421 /mailnews/mime/src/comi18n.cpp
parent21b3f6247403c06f85e1f45d219f87549862198f (diff)
downloadUXP-302bf1b523012e11b60425d6eee1221ebc2724eb.tar
UXP-302bf1b523012e11b60425d6eee1221ebc2724eb.tar.gz
UXP-302bf1b523012e11b60425d6eee1221ebc2724eb.tar.lz
UXP-302bf1b523012e11b60425d6eee1221ebc2724eb.tar.xz
UXP-302bf1b523012e11b60425d6eee1221ebc2724eb.zip
Issue #1258 - Part 1: Import mailnews, ldap, and mork from comm-esr52.9.1
Diffstat (limited to 'mailnews/mime/src/comi18n.cpp')
-rw-r--r--mailnews/mime/src/comi18n.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/mailnews/mime/src/comi18n.cpp b/mailnews/mime/src/comi18n.cpp
new file mode 100644
index 000000000..7425e32ff
--- /dev/null
+++ b/mailnews/mime/src/comi18n.cpp
@@ -0,0 +1,108 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * 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 "comi18n.h"
+#include "nsIStringCharsetDetector.h"
+#include "nsMsgUtils.h"
+#include "nsICharsetConverterManager.h"
+#include "nsIMIMEHeaderParam.h"
+#include "nsServiceManagerUtils.h"
+#include "nsComponentManagerUtils.h"
+#include "nsMsgMimeCID.h"
+#include "nsIMimeConverter.h"
+
+
+////////////////////////////////////////////////////////////////////////////////
+// BEGIN PUBLIC INTERFACE
+extern "C" {
+
+
+void MIME_DecodeMimeHeader(const char *header, const char *default_charset,
+ bool override_charset, bool eatContinuations,
+ nsACString &result)
+{
+ nsresult rv;
+ nsCOMPtr <nsIMimeConverter> mimeConverter =
+ do_GetService(NS_MIME_CONVERTER_CONTRACTID, &rv);
+ if (NS_FAILED(rv)) {
+ result.Truncate();
+ return;
+ }
+ mimeConverter->DecodeMimeHeaderToUTF8(nsDependentCString(header),
+ default_charset, override_charset,
+ eatContinuations, result);
+}
+
+// UTF-8 utility functions.
+//detect charset soly based on aBuf. return in aCharset
+nsresult
+MIME_detect_charset(const char *aBuf, int32_t aLength, const char** aCharset)
+{
+ nsresult res = NS_ERROR_UNEXPECTED;
+ nsString detector_name;
+ *aCharset = nullptr;
+
+ NS_GetLocalizedUnicharPreferenceWithDefault(nullptr, "intl.charset.detector", EmptyString(), detector_name);
+
+ if (!detector_name.IsEmpty()) {
+ nsAutoCString detector_contractid;
+ detector_contractid.AssignLiteral(NS_STRCDETECTOR_CONTRACTID_BASE);
+ detector_contractid.Append(NS_ConvertUTF16toUTF8(detector_name));
+ nsCOMPtr<nsIStringCharsetDetector> detector = do_CreateInstance(detector_contractid.get(), &res);
+ if (NS_SUCCEEDED(res)) {
+ nsDetectionConfident oConfident;
+ res = detector->DoIt(aBuf, aLength, aCharset, oConfident);
+ if (NS_SUCCEEDED(res) && (eBestAnswer == oConfident || eSureAnswer == oConfident)) {
+ return NS_OK;
+ }
+ }
+ }
+ return res;
+}
+
+//Get unicode decoder(from inputcharset to unicode) for aInputCharset
+nsresult
+MIME_get_unicode_decoder(const char* aInputCharset, nsIUnicodeDecoder **aDecoder)
+{
+ nsresult res;
+
+ // get charset converters.
+ nsCOMPtr<nsICharsetConverterManager> ccm =
+ do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res);
+ if (NS_SUCCEEDED(res)) {
+
+ // create a decoder (conv to unicode), ok if failed if we do auto detection
+ if (!*aInputCharset || !PL_strcasecmp("us-ascii", aInputCharset))
+ res = ccm->GetUnicodeDecoderRaw("ISO-8859-1", aDecoder);
+ else
+ // GetUnicodeDecoderInternal in order to support UTF-7 messages
+ //
+ // XXX this means that even HTML messages in UTF-7 will be decoded
+ res = ccm->GetUnicodeDecoderInternal(aInputCharset, aDecoder);
+ }
+
+ return res;
+}
+
+//Get unicode encoder(from unicode to inputcharset) for aOutputCharset
+nsresult
+MIME_get_unicode_encoder(const char* aOutputCharset, nsIUnicodeEncoder **aEncoder)
+{
+ nsresult res;
+
+ // get charset converters.
+ nsCOMPtr<nsICharsetConverterManager> ccm =
+ do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res);
+ if (NS_SUCCEEDED(res) && *aOutputCharset) {
+ // create a encoder (conv from unicode)
+ res = ccm->GetUnicodeEncoder(aOutputCharset, aEncoder);
+ }
+
+ return res;
+}
+
+} /* end of extern "C" */
+// END PUBLIC INTERFACE
+