|
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include "cimapcharconv.h" |
|
17 #include <miutconv.h> |
|
18 #include <imcvcodc.h> |
|
19 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS |
|
20 #include "cimconvertcharconv.h" |
|
21 #include "cimconvertheader.h" |
|
22 #endif |
|
23 const TInt KBufSize = 100; |
|
24 |
|
25 CImapCharconv* CImapCharconv::NewL(RFs& aFs) |
|
26 { |
|
27 CImapCharconv* self = new (ELeave) CImapCharconv(); |
|
28 CleanupStack::PushL(self); |
|
29 self->ConstructL(aFs); |
|
30 CleanupStack::Pop(); |
|
31 return self; |
|
32 } |
|
33 |
|
34 CImapCharconv::CImapCharconv() |
|
35 { |
|
36 } |
|
37 |
|
38 void CImapCharconv::ConstructL(RFs& aFs) |
|
39 { |
|
40 iCharacterConverter = CCnvCharacterSetConverter::NewL(); |
|
41 iCharConv = CImConvertCharconv::NewL(*iCharacterConverter, aFs); |
|
42 iHeaderConverter = CImConvertHeader::NewL(*iCharConv); |
|
43 } |
|
44 |
|
45 CImapCharconv::~CImapCharconv() |
|
46 { |
|
47 delete iHeaderConverter; |
|
48 delete iCharConv; |
|
49 delete iCharacterConverter; |
|
50 } |
|
51 |
|
52 /** |
|
53 Converts the supplied Imap-Utf7 encoded string into Unicode, allocating sufficient buffer space for the Unicode string. |
|
54 @return a heap buffer containing the Unicode string |
|
55 */ |
|
56 EXPORT_C HBufC16* CImapCharconv::ConvertToUnicodeFromImapUtf7L(const TDesC8& aImapUtf7) |
|
57 // This code is based on CnvUtfConverter::ConvertToUnicodeFromUtf7L() |
|
58 { |
|
59 // If aImapUtf7 is an empty string return |
|
60 if (aImapUtf7.Length()==0) |
|
61 { |
|
62 HBufC16* hBuf = HBufC16::NewL(1); |
|
63 return hBuf; |
|
64 } |
|
65 |
|
66 // else convert aImapUtf7 to Unicode storing the result in a buffer, reallocating |
|
67 // it when needed. |
|
68 TInt length = aImapUtf7.Length(); |
|
69 |
|
70 iCharConv->PrepareToConvertToFromOurCharsetL(KCharacterSetIdentifierImapUtf7); |
|
71 |
|
72 TPtrC8 utf7 (aImapUtf7); |
|
73 TBuf<KBufSize> buf; |
|
74 HBufC16* hBuf = HBufC16::NewLC(length); |
|
75 TPtr unicode = hBuf->Des(); |
|
76 |
|
77 // Dummy parameters for the call to ConvertToOurCharsetL() |
|
78 // Note that they don't actually get updated by this method. |
|
79 // In particular, numUnconvertedChars does not get updated. |
|
80 // So we use the return value (assigned to "unconverted") instead. |
|
81 TInt numUnconvertedChars=0; |
|
82 TInt indexFirstUnconvertedChar=0; |
|
83 |
|
84 FOREVER |
|
85 { |
|
86 TInt unconverted = iCharConv->ConvertToOurCharsetL(utf7, buf, numUnconvertedChars, indexFirstUnconvertedChar); |
|
87 if(unconverted == CnvUtfConverter::EErrorIllFormedInput || unconverted < 0) |
|
88 { |
|
89 User::Leave(KErrCorrupt); |
|
90 } |
|
91 |
|
92 if (unicode.Length() + buf.Length() > unicode.MaxLength()) |
|
93 { |
|
94 // Reallocate hBuf |
|
95 hBuf = hBuf->ReAllocL(unicode.Length() + buf.Length()); |
|
96 CleanupStack::Pop(); |
|
97 CleanupStack::PushL(hBuf); |
|
98 unicode.Set(hBuf->Des()); |
|
99 } |
|
100 |
|
101 unicode.Append(buf); |
|
102 if (unconverted == 0) |
|
103 { |
|
104 break; |
|
105 } |
|
106 |
|
107 utf7.Set(utf7.Right(unconverted)); |
|
108 } |
|
109 |
|
110 CleanupStack::Pop(hBuf); |
|
111 return hBuf; |
|
112 } |
|
113 |
|
114 /** |
|
115 Converts the supplied Unicode string into a Imap-Utf7 encoded string, allocating sufficient buffer space for the Imap-Utf7 string. |
|
116 @return a heap buffer containing the Imap-Utf7 encoded string |
|
117 */ |
|
118 EXPORT_C HBufC8* CImapCharconv::ConvertFromUnicodeToImapUtf7L(const TDesC16& aUnicode) |
|
119 // This code is based on CnvUtfConverter::ConvertFromUnicodeToImapUtf7L() |
|
120 { |
|
121 // If aUnicode is Null string, return an empty HBufC |
|
122 if (aUnicode.Length() == 0) |
|
123 { |
|
124 HBufC8* hBuf8 = HBufC8::NewL(1); |
|
125 return hBuf8; |
|
126 } |
|
127 |
|
128 // Otherwise, convert and store result in a buffer, reallocating that buffer if needed. |
|
129 TInt length = aUnicode.Length(); |
|
130 |
|
131 iCharConv->PrepareToConvertToFromOurCharsetL(KCharacterSetIdentifierImapUtf7); |
|
132 |
|
133 TPtrC16 unicode (aUnicode); |
|
134 TBuf8<KBufSize> buf; |
|
135 HBufC8* hBuf8 = HBufC8::NewLC(length); |
|
136 TPtr8 utf7 = hBuf8->Des(); |
|
137 |
|
138 // Dummy parameters for the call to ConvertFromOurCharsetL() |
|
139 // Note that they don't actually get updated by this method. |
|
140 // In particular, numUnconvertedChars does not get updated. |
|
141 // So we use the return value (assigned to "unconverted") instead. |
|
142 TInt numUnconvertedChars=0; |
|
143 TInt indexFirstUnconvertedChar=0; |
|
144 |
|
145 FOREVER |
|
146 { |
|
147 TInt unconverted = iCharConv->ConvertFromOurCharsetL(unicode, buf, numUnconvertedChars, indexFirstUnconvertedChar); |
|
148 if(unconverted == CnvUtfConverter::EErrorIllFormedInput || unconverted < 0) |
|
149 { |
|
150 User::Leave(KErrCorrupt); |
|
151 } |
|
152 |
|
153 if (utf7.Length() + buf.Length() > utf7.MaxLength()) |
|
154 { |
|
155 // Reallocate the hBuf8 |
|
156 hBuf8 = hBuf8->ReAllocL(utf7.Length() + buf.Length()); |
|
157 CleanupStack::Pop(); |
|
158 CleanupStack::PushL(hBuf8); |
|
159 utf7.Set(hBuf8->Des()); |
|
160 } |
|
161 utf7.Append(buf); |
|
162 if (unconverted == 0) |
|
163 { |
|
164 break; |
|
165 } |
|
166 unicode.Set(unicode.Right(unconverted)); |
|
167 } |
|
168 CleanupStack::Pop(); |
|
169 return hBuf8; |
|
170 } |
|
171 |
|
172 |
|
173 |
|
174 /** |
|
175 Gets the header converter |
|
176 |
|
177 @return Header converter |
|
178 */ |
|
179 EXPORT_C CImConvertHeader& CImapCharconv::HeaderConverter() |
|
180 { |
|
181 return *iHeaderConverter; |
|
182 } |
|
183 |