|
1 /* |
|
2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * Implements character converter for application. |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include <bldvariant.hrh> |
|
22 #include <eikenv.h> |
|
23 #include <miutconv.h> |
|
24 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS |
|
25 #include <cimconvertcharconv.h> |
|
26 #endif |
|
27 #include "MsgMailViewerCharConv.h" |
|
28 |
|
29 // CONSTANTS |
|
30 const TInt KBlockSize = 64; |
|
31 |
|
32 |
|
33 // ================= MEMBER FUNCTIONS ======================= |
|
34 |
|
35 // Default constructor |
|
36 CMsgMailViewerCharConv::CMsgMailViewerCharConv() |
|
37 : iState( CCnvCharacterSetConverter::KStateDefault ) |
|
38 { |
|
39 } |
|
40 |
|
41 // destructor |
|
42 CMsgMailViewerCharConv::~CMsgMailViewerCharConv() |
|
43 { |
|
44 delete iCharConv; |
|
45 delete iImCharConv; |
|
46 delete iTarget8; |
|
47 delete iTarget16; |
|
48 } |
|
49 |
|
50 // Symbian OS default constructor can leave. |
|
51 void CMsgMailViewerCharConv::ConstructL( TUint aCharSet ) |
|
52 { |
|
53 if (aCharSet > 0) |
|
54 { |
|
55 iCharConv = CCnvCharacterSetConverter::NewL(); |
|
56 iImCharConv = CImConvertCharconv::NewL( |
|
57 *iCharConv, CEikonEnv::Static()->FsSession()); // CSI: 27 # Must be used because of iEikEnv |
|
58 // is not accessible. |
|
59 TBool found = iImCharConv->PrepareToConvertToFromOurCharsetL(aCharSet); |
|
60 if (!found) |
|
61 { |
|
62 User::Leave( KErrNotFound ); |
|
63 } |
|
64 } |
|
65 } |
|
66 |
|
67 // Two-phased constructor. |
|
68 CMsgMailViewerCharConv* CMsgMailViewerCharConv::NewL( |
|
69 TUint aCharSet ) // CMsgMailViewerApp reference |
|
70 { |
|
71 CMsgMailViewerCharConv* self = new (ELeave) CMsgMailViewerCharConv(); |
|
72 CleanupStack::PushL( self ); |
|
73 self->ConstructL( aCharSet ); |
|
74 CleanupStack::Pop(); |
|
75 |
|
76 return self; |
|
77 } |
|
78 |
|
79 void CMsgMailViewerCharConv::PrepareToConvertFromUnicodeL( const TDesC16& aSource ) |
|
80 { |
|
81 iSource16.Set( aSource ); |
|
82 |
|
83 delete iTarget8; |
|
84 iTarget8 = NULL; |
|
85 iTarget8 = HBufC8::NewL( iSource16.Length()*2 ); // CSI: 47 # For conversion. |
|
86 |
|
87 iFromUnicode = ETrue; |
|
88 iFailCount = 0; |
|
89 } |
|
90 |
|
91 void CMsgMailViewerCharConv::PrepareToConvertToUnicodeL( const TDesC8& aSource ) |
|
92 { |
|
93 iSource8.Set( aSource ); |
|
94 |
|
95 delete iTarget16; |
|
96 iTarget16 = NULL; |
|
97 iTarget16 = HBufC16::NewL( iSource8.Length() ); |
|
98 |
|
99 iFromUnicode = EFalse; |
|
100 } |
|
101 |
|
102 HBufC16* CMsgMailViewerCharConv::GetUnicodeText() |
|
103 { |
|
104 HBufC16* result = iTarget16; |
|
105 iTarget16 = NULL; |
|
106 |
|
107 return result; |
|
108 } |
|
109 |
|
110 HBufC8* CMsgMailViewerCharConv::GetForeignText() |
|
111 { |
|
112 HBufC8* result = iTarget8; |
|
113 iTarget8 = NULL; |
|
114 |
|
115 return result; |
|
116 } |
|
117 |
|
118 TInt CMsgMailViewerCharConv::GetFailedCount() const |
|
119 { |
|
120 return iFailCount; |
|
121 } |
|
122 |
|
123 void CMsgMailViewerCharConv::AppendTextL( const TDesC16& aData ) |
|
124 { |
|
125 TPtr target( iTarget16->Des() ); |
|
126 TInt length = target.Length() + aData.Length(); |
|
127 if( length > target.MaxLength() ) |
|
128 { |
|
129 iTarget16 = iTarget16->ReAllocL( length ); |
|
130 target.Set( iTarget16->Des() ); |
|
131 } |
|
132 |
|
133 target.Append( aData ); |
|
134 } |
|
135 |
|
136 void CMsgMailViewerCharConv::AppendTextL( const TDesC8& aData ) |
|
137 { |
|
138 TPtr8 target( iTarget8->Des() ); |
|
139 TInt length = target.Length() + aData.Length(); |
|
140 if( length > target.MaxLength() ) |
|
141 { |
|
142 iTarget8 = iTarget8->ReAllocL( length ); |
|
143 target.Set( iTarget8->Des() ); |
|
144 } |
|
145 |
|
146 target.Append( aData ); |
|
147 } |
|
148 |
|
149 void CMsgMailViewerCharConv::StepL() |
|
150 { |
|
151 if( iFromUnicode ) |
|
152 { |
|
153 StepFromUnicodeL(); |
|
154 } |
|
155 else |
|
156 { |
|
157 StepToUnicodeL(); |
|
158 } |
|
159 } |
|
160 |
|
161 void CMsgMailViewerCharConv::StepFromUnicodeL() |
|
162 { |
|
163 if (iImCharConv) |
|
164 { |
|
165 TBuf8<KBlockSize> buf; |
|
166 TInt firstUnconvertdChar; |
|
167 TInt numUnconvertedChars; |
|
168 |
|
169 TInt length (iImCharConv->ConvertFromOurCharsetL( iSource16, buf, |
|
170 numUnconvertedChars, |
|
171 firstUnconvertdChar)); |
|
172 |
|
173 iFailCount += numUnconvertedChars; |
|
174 User::LeaveIfError( length ); |
|
175 |
|
176 iSource16.Set( iSource16.Right( length ) ); |
|
177 |
|
178 AppendTextL( buf ); |
|
179 } |
|
180 else |
|
181 { |
|
182 // No converter, just copy source to target |
|
183 iTarget8->Des().Copy(iSource16); |
|
184 iSource16.Set( KNullDesC() ); |
|
185 } |
|
186 } |
|
187 |
|
188 void CMsgMailViewerCharConv::StepToUnicodeL() |
|
189 { |
|
190 if (iImCharConv) |
|
191 { |
|
192 TBuf16<KBlockSize> buf; |
|
193 TInt firstUnconvertdChar; |
|
194 TInt numUnconvertedChars; |
|
195 |
|
196 TInt length (iImCharConv->ConvertToOurCharsetL( iSource8, buf, |
|
197 numUnconvertedChars, |
|
198 firstUnconvertdChar)); |
|
199 |
|
200 User::LeaveIfError( length ); |
|
201 |
|
202 iSource8.Set( iSource8.Right( length ) ); |
|
203 |
|
204 AppendTextL( buf ); |
|
205 } |
|
206 else |
|
207 { |
|
208 // just copy source to target |
|
209 iTarget16->Des().Copy(iSource8); |
|
210 iSource8.Set( KNullDesC8() ); |
|
211 } |
|
212 } |
|
213 |
|
214 |
|
215 TBool CMsgMailViewerCharConv::IsProcessDone() const |
|
216 { |
|
217 TBool ret( EFalse ); |
|
218 |
|
219 if( iFromUnicode ) |
|
220 { |
|
221 ret = iSource16.Length() == 0; |
|
222 } |
|
223 else |
|
224 { |
|
225 ret = iSource8.Length() == 0; |
|
226 } |
|
227 |
|
228 return ret; |
|
229 } |
|
230 |
|
231 // End of File |