1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #include "ImportTranslate.h"
7
8 int ImportTranslate::m_useTranslator = -1;
9
ConvertString(const nsCString & inStr,nsCString & outStr,bool mimeHeader)10 bool ImportTranslate::ConvertString(const nsCString& inStr, nsCString& outStr,
11 bool mimeHeader) {
12 if (inStr.IsEmpty()) {
13 outStr = inStr;
14 return true;
15 }
16
17 nsImportTranslator* pTrans = GetTranslator();
18 // int maxLen = (int) pTrans->GetMaxBufferSize(inStr.Length());
19 // int hLen = 0;
20 nsCString set;
21 nsCString lang;
22
23 if (mimeHeader) {
24 // add the charset and language
25 pTrans->GetCharset(set);
26 pTrans->GetLanguage(lang);
27 }
28
29 // Unfortunately, we didn't implement ConvertBuffer for all translators,
30 // just ConvertToFile. This means that this data will not always
31 // be converted to the charset of pTrans. In that case...
32 // We don't always have the data in the same charset as the current
33 // translator...
34 // It is safer to leave the charset and language field blank
35 set.Truncate();
36 lang.Truncate();
37
38 uint8_t* pBuf;
39 /*
40 pBuf = (P_U8) outStr.GetBuffer(maxLen);
41 if (!pBuf) {
42 delete pTrans;
43 return FALSE;
44 }
45 pTrans->ConvertBuffer((PC_U8)(PC_S8)inStr, inStr.GetLength(), pBuf);
46 outStr.ReleaseBuffer();
47 */
48 outStr = inStr;
49 delete pTrans;
50
51 // Now I need to run the string through the mime-header special char
52 // encoder.
53
54 pTrans = new CMHTranslator;
55 pBuf = new uint8_t[pTrans->GetMaxBufferSize(outStr.Length())];
56 pTrans->ConvertBuffer((const uint8_t*)(outStr.get()), outStr.Length(), pBuf);
57 delete pTrans;
58 outStr.Truncate();
59 if (mimeHeader) {
60 outStr = set;
61 outStr += "'";
62 outStr += lang;
63 outStr += "'";
64 }
65 outStr += (const char*)pBuf;
66 delete[] pBuf;
67
68 return true;
69 }
70
GetTranslator(void)71 nsImportTranslator* ImportTranslate::GetTranslator(void) {
72 if (m_useTranslator == -1) {
73 // get the translator to use...
74 // CString trans;
75 // trans.LoadString(IDS_LANGUAGE_TRANSLATION);
76 m_useTranslator = 0;
77 // if (!trans.CompareNoCase("iso-2022-jp"))
78 // gWizData.m_useTranslator = 1;
79 }
80
81 switch (m_useTranslator) {
82 case 0:
83 return new nsImportTranslator;
84 // case 1:
85 // return new CSJis2JisTranslator;
86 default:
87 return new nsImportTranslator;
88 }
89 }
90
GetMatchingTranslator(const char * pCharSet)91 nsImportTranslator* ImportTranslate::GetMatchingTranslator(
92 const char* pCharSet) {
93 /*
94 CString jp = "iso-2022-jp";
95 if (!jp.CompareNoCase(pCharSet))
96 return new CSJis2JisTranslator;
97 */
98
99 return nullptr;
100 }
101