summaryrefslogtreecommitdiffstats
path: root/mailnews/mime/src/comi18n.cpp
diff options
context:
space:
mode:
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
+