|
1 // Copyright (c) 1998-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 // MIUTCONV.CPP |
|
15 // |
|
16 |
|
17 #include "MIUTCONV.H" |
|
18 #include "IMCMMAIN.H" |
|
19 #include "MIUT_ERR.H" |
|
20 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS |
|
21 #include "cimconvertcharconv.h" |
|
22 #include "miut_errconsts.h" |
|
23 #include "timrfc822datefield.h" |
|
24 #endif |
|
25 #include <msvstore.h> // CMsvStore |
|
26 |
|
27 #include <imcm.rsg> |
|
28 #include <barsc.h> |
|
29 #include <barsread.h> |
|
30 #include <centralrepository.h> |
|
31 |
|
32 const TInt KMaxMIMECharSetLength = 64; // Should be long enough for character sets |
|
33 |
|
34 |
|
35 //**************************************************************************************** |
|
36 // Class TImEmailTransformingInfo functions |
|
37 //**************************************************************************************** |
|
38 |
|
39 /** Overloaded assignment operator. |
|
40 |
|
41 @param aInfo Object from which to copy settings |
|
42 @return Reference to 'this' object with the copied settings |
|
43 */ |
|
44 EXPORT_C TImEmailTransformingInfo& TImEmailTransformingInfo::operator=(const TImEmailTransformingInfo& aInfo) |
|
45 { |
|
46 SetSendMethod(aInfo.SendMethod()); |
|
47 SetHeaderEncoding(aInfo.HeaderEncoding()); |
|
48 SetBodyTextEncoding(aInfo.BodyTextEncoding()); |
|
49 SetHTMLEncoding(aInfo.HTMLEncoding()); |
|
50 SetAttachmentEncoding(aInfo.AttachmentEncoding()); |
|
51 SetHeaderCharset(aInfo.HeaderCharset()); |
|
52 SetBodyTextCharset(aInfo.BodyTextCharset()); |
|
53 SetHTMLCharset(aInfo.HTMLCharset()); |
|
54 |
|
55 return *this; |
|
56 } |
|
57 |
|
58 /** Overloaded equality operator. |
|
59 |
|
60 @param aInfo Object with which to compare this object |
|
61 @return ETrue if the objects have the same settings, otherwise EFalse |
|
62 */ |
|
63 EXPORT_C TBool TImEmailTransformingInfo::operator==(const TImEmailTransformingInfo& aInfo) |
|
64 { |
|
65 return (SendMethod()==aInfo.SendMethod() && |
|
66 HeaderEncoding()==aInfo.HeaderEncoding() && |
|
67 BodyTextEncoding()==aInfo.BodyTextEncoding() && |
|
68 HTMLEncoding()==aInfo.HTMLEncoding() && |
|
69 AttachmentEncoding()==aInfo.AttachmentEncoding() && |
|
70 HeaderCharset()==aInfo.HeaderCharset()&& |
|
71 BodyTextCharset()==aInfo.BodyTextCharset() && |
|
72 HTMLCharset()==aInfo.HTMLCharset()); |
|
73 } |
|
74 |
|
75 |
|
76 /** Writes the object to the specified message store. |
|
77 |
|
78 The function can leave with the standard stream leave codes. |
|
79 |
|
80 @param aStore Store to which to write |
|
81 */ |
|
82 EXPORT_C void TImEmailTransformingInfo::StoreL( CMsvStore& aStore ) const |
|
83 { |
|
84 RMsvWriteStream out; |
|
85 out.AssignLC( aStore, KUidMsgFileTransformingInfo ); // pushes 'out' to the stack |
|
86 ExternalizeL(out); |
|
87 out.CommitL(); |
|
88 CleanupStack::PopAndDestroy(); |
|
89 } |
|
90 |
|
91 /** Restores the object to the specified message store. |
|
92 |
|
93 The function can leave with the standard stream leave codes. |
|
94 |
|
95 @param aStore Store to which to write |
|
96 */ |
|
97 EXPORT_C void TImEmailTransformingInfo::RestoreL( CMsvStore& aStore ) |
|
98 { |
|
99 RMsvReadStream in; |
|
100 in.OpenLC( aStore, KUidMsgFileTransformingInfo ); // pushes 'in' to the stack |
|
101 InternalizeL(in); |
|
102 in.Close();// make sure we close the stream |
|
103 CleanupStack::PopAndDestroy(); |
|
104 } |
|
105 |
|
106 |
|
107 EXPORT_C void TImEmailTransformingInfo::ExternalizeL( RWriteStream& aWriteStream ) const |
|
108 { |
|
109 aWriteStream.WriteUint8L( SendMethod() ); |
|
110 |
|
111 aWriteStream.WriteUint8L( HeaderEncoding() ); |
|
112 aWriteStream.WriteUint8L( BodyTextEncoding()); |
|
113 aWriteStream.WriteUint8L( HTMLEncoding() ); |
|
114 aWriteStream.WriteUint8L( AttachmentEncoding() ); |
|
115 |
|
116 aWriteStream.WriteUint32L( HeaderCharset() ); |
|
117 aWriteStream.WriteUint32L( BodyTextCharset() ); |
|
118 aWriteStream.WriteUint32L( HTMLCharset() ); |
|
119 } |
|
120 |
|
121 |
|
122 EXPORT_C void TImEmailTransformingInfo::InternalizeL( RReadStream& aReadStream ) |
|
123 { |
|
124 SetSendMethod( static_cast<TImSendMethod> (aReadStream.ReadUint8L()) ); |
|
125 |
|
126 SetHeaderEncoding( static_cast<TImEncodingType> (aReadStream.ReadUint8L())); |
|
127 iBodyEncoding= static_cast<TImEncodingType> (aReadStream.ReadUint8L()); |
|
128 SetHTMLEncoding( static_cast<TImEncodingType> (aReadStream.ReadUint8L()) ); |
|
129 SetAttachmentEncoding( static_cast<TImEncodingType> (aReadStream.ReadUint8L()) ); |
|
130 |
|
131 SetHeaderCharset( aReadStream.ReadUint32L()); |
|
132 SetBodyTextCharset( aReadStream.ReadUint32L()); |
|
133 SetHTMLCharset( aReadStream.ReadUint32L()); |
|
134 } |
|
135 |
|
136 |
|
137 EXPORT_C void TImEmailTransformingInfo::SetSendMethod(const TImSendMethod aMethod) |
|
138 /** Sets the method by which email is sent. |
|
139 |
|
140 This is either ESendAsSimpleEmail or ESendAsMimeEmail. |
|
141 |
|
142 @param aMethod The method by which email is sent. */ |
|
143 { |
|
144 iSendMethod=aMethod; |
|
145 } |
|
146 |
|
147 EXPORT_C void TImEmailTransformingInfo::SetToDefault(TImSendMethod aMethod) |
|
148 /** Sets the member data to their default values, for the send method specified. |
|
149 |
|
150 @param aMethod The send method. */ |
|
151 { |
|
152 SetSendMethod(aMethod); |
|
153 |
|
154 // Set to default values. |
|
155 if (iSendMethod==ESendAsSimpleEmail) |
|
156 { |
|
157 SetHeaderEncoding(KDefaultPlainHeaderEncoding); |
|
158 SetBodyTextEncoding(KDefaultPlainBodyTextEncoding); |
|
159 SetAttachmentEncoding(KDefaultPlainAttachmentEncoding); |
|
160 } |
|
161 else |
|
162 { |
|
163 SetHeaderEncoding(KDefaultMIMEHeaderEncoding); |
|
164 SetBodyTextEncoding(KDefaultMIMEBodyTextEncoding); |
|
165 SetHTMLEncoding(KDefaultMIMEHTMLEncoding); |
|
166 SetAttachmentEncoding(KDefaultMIMEAttachmentEncoding); |
|
167 } |
|
168 |
|
169 SetHeaderCharset(0); |
|
170 SetBodyTextCharset(0); |
|
171 SetHTMLCharset(0); |
|
172 } |
|
173 |
|
174 EXPORT_C TImSendMethod TImEmailTransformingInfo::SendMethod() const |
|
175 /** Gets the current send method. |
|
176 |
|
177 @return The current send method. */ |
|
178 { |
|
179 return iSendMethod; |
|
180 } |
|
181 |
|
182 |
|
183 EXPORT_C void TImEmailTransformingInfo::SetHeaderEncodingQ() |
|
184 /** Sets the header encoding to type Q. */ |
|
185 { |
|
186 iHeaderEncoding=EEncodingTypeQP; |
|
187 } |
|
188 |
|
189 EXPORT_C void TImEmailTransformingInfo::SetHeaderEncodingB() |
|
190 /** Sets the header encoding to type B. */ |
|
191 { |
|
192 iHeaderEncoding=EEncodingTypeBASE64; |
|
193 } |
|
194 |
|
195 void TImEmailTransformingInfo::SetHeaderEncoding(TImEncodingType aType) |
|
196 { |
|
197 iHeaderEncoding=aType; |
|
198 } |
|
199 |
|
200 EXPORT_C void TImEmailTransformingInfo::SetBodyTextEncoding(TImEncodingType aType) |
|
201 /** Sets the body text encoding. |
|
202 |
|
203 @param aType The body text encoding. This can be one of four possible values: |
|
204 EEncodingTypeNone, EEncodingTypeQP, EEncodingTypeBASE64, or EEncodingTypeUU. */ |
|
205 { |
|
206 iBodyEncoding=aType; |
|
207 } |
|
208 |
|
209 |
|
210 EXPORT_C void TImEmailTransformingInfo::SetHTMLEncoding(TImEncodingType aType) |
|
211 /** Sets the encoding for HTML email. |
|
212 |
|
213 @param aType The HTML email encoding. This can be one of four possible values: |
|
214 EEncodingTypeNone, EEncodingTypeQP, EEncodingTypeBASE64, or EEncodingTypeUU. */ |
|
215 { |
|
216 iHTMLEncoding=aType; |
|
217 } |
|
218 |
|
219 EXPORT_C void TImEmailTransformingInfo::SetAttachmentEncoding(TImEncodingType aType) |
|
220 /** Sets the encoding for email attachments. |
|
221 |
|
222 This can be one of four possible values : EEncodingTypeNone, EEncodingTypeQP, |
|
223 EEncodingTypeBASE64, or EEncodingTypeUU. |
|
224 |
|
225 @param aType The email attachment encoding. */ |
|
226 { |
|
227 iAttachmentEncoding=aType; |
|
228 } |
|
229 |
|
230 EXPORT_C void TImEmailTransformingInfo::SetHeaderAndBodyCharset(TUint aCharset) |
|
231 /** Sets the character set used for both the header and body text. |
|
232 |
|
233 @param aCharset The character set. */ |
|
234 { |
|
235 iHeaderCharset=iBodyCharset=aCharset; |
|
236 } |
|
237 |
|
238 |
|
239 EXPORT_C void TImEmailTransformingInfo::SetHeaderCharset(TUint aCharset) |
|
240 /** Sets the character set used for the header. |
|
241 |
|
242 @param aCharset The character set. */ |
|
243 { |
|
244 iHeaderCharset=aCharset; |
|
245 } |
|
246 |
|
247 EXPORT_C void TImEmailTransformingInfo::SetBodyTextCharset(TUint aCharset) |
|
248 /** Sets the character set for body text. |
|
249 |
|
250 @param aCharset The character set. */ |
|
251 { |
|
252 iBodyCharset=aCharset; |
|
253 } |
|
254 |
|
255 EXPORT_C void TImEmailTransformingInfo::SetHTMLCharset(TUint aCharset) |
|
256 /** Sets the HTML character set. |
|
257 |
|
258 @param aCharset The character set. */ |
|
259 { |
|
260 iHTMLCharset=aCharset; |
|
261 } |
|
262 |
|
263 |
|
264 EXPORT_C TImEncodingType TImEmailTransformingInfo::HeaderEncoding() const |
|
265 /** Gets the header encoding. |
|
266 |
|
267 @return The header encoding. */ |
|
268 { |
|
269 return iHeaderEncoding; |
|
270 } |
|
271 |
|
272 |
|
273 EXPORT_C TImEncodingType TImEmailTransformingInfo::BodyTextEncoding() const |
|
274 /** Gets the body text encoding. |
|
275 |
|
276 @return The body text encoding. */ |
|
277 { |
|
278 return iBodyEncoding; |
|
279 } |
|
280 |
|
281 EXPORT_C TImEncodingType TImEmailTransformingInfo::HTMLEncoding() const |
|
282 /** Gets the HTML encoding. |
|
283 |
|
284 @return The HTML encoding. */ |
|
285 { |
|
286 return iHTMLEncoding; |
|
287 } |
|
288 |
|
289 EXPORT_C TImEncodingType TImEmailTransformingInfo::AttachmentEncoding() const |
|
290 /** Gets the attachment encoding. |
|
291 |
|
292 @return The attachment encoding. */ |
|
293 { |
|
294 return iAttachmentEncoding; |
|
295 } |
|
296 |
|
297 EXPORT_C TUint TImEmailTransformingInfo::HeaderCharset() const |
|
298 /** Gets the character set for the header. |
|
299 |
|
300 @return The character set for the header. */ |
|
301 { |
|
302 return iHeaderCharset; |
|
303 } |
|
304 |
|
305 EXPORT_C TUint TImEmailTransformingInfo::BodyTextCharset() const |
|
306 /** Gets the character set for the body text. |
|
307 |
|
308 @return The character set for the body text. */ |
|
309 { |
|
310 return iBodyCharset; |
|
311 } |
|
312 |
|
313 EXPORT_C TUint TImEmailTransformingInfo::HTMLCharset() const |
|
314 /** Gets the HTML character set. |
|
315 |
|
316 @return The HTML character set. */ |
|
317 { |
|
318 return iHTMLCharset; |
|
319 } |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 //**************************************************************************************** |
|
325 // Class CImConvertCharconv Functions |
|
326 //**************************************************************************************** |
|
327 |
|
328 EXPORT_C CImConvertCharconv* CImConvertCharconv::NewL(CCnvCharacterSetConverter& aConverter, |
|
329 RFs& anFs) |
|
330 { |
|
331 CImConvertCharconv* self = new (ELeave) CImConvertCharconv (aConverter, anFs); |
|
332 CleanupStack::PushL(self); |
|
333 self->ConstructL(); |
|
334 CleanupStack::Pop(self); |
|
335 return self; |
|
336 } |
|
337 |
|
338 CImConvertCharconv::CImConvertCharconv(CCnvCharacterSetConverter& aConverter, RFs& anFs) : |
|
339 iConverter (aConverter), iFs(anFs) |
|
340 { |
|
341 } |
|
342 |
|
343 CImConvertCharconv::~CImConvertCharconv() |
|
344 { |
|
345 delete iCharsetsAvailable; |
|
346 } |
|
347 |
|
348 void CImConvertCharconv::ConstructL() |
|
349 { |
|
350 iCharsetsAvailable = iConverter.CreateArrayOfCharacterSetsAvailableL(iFs); |
|
351 |
|
352 // Open .ini file to get the system default |
|
353 SetSystemDefaultCharsetL(); |
|
354 iCharconvState = CCnvCharacterSetConverter::KStateDefault; |
|
355 } |
|
356 |
|
357 TInt CImConvertCharconv::StraightCopy( const TDesC8& aBufIn, TDes& rBufOut) |
|
358 { |
|
359 TInt inLen=aBufIn.Length(); |
|
360 TInt outMaxLen=rBufOut.MaxLength(); |
|
361 |
|
362 if (inLen >= outMaxLen) |
|
363 { |
|
364 TPtrC8 in = aBufIn.Left(outMaxLen); |
|
365 rBufOut.Copy(in); |
|
366 return (inLen-outMaxLen); |
|
367 } |
|
368 else |
|
369 rBufOut.Copy(aBufIn); |
|
370 return 0; |
|
371 } |
|
372 |
|
373 TInt CImConvertCharconv::StraightCopy( const TDesC& aBufIn, TDes8& rBufOut) |
|
374 { |
|
375 TInt inLen=aBufIn.Length(); |
|
376 TInt outMaxLen=rBufOut.MaxLength(); |
|
377 |
|
378 if (inLen >= outMaxLen) |
|
379 { |
|
380 TPtrC16 in = aBufIn.Left(outMaxLen); |
|
381 rBufOut.Copy(in); |
|
382 return (inLen-outMaxLen); |
|
383 } |
|
384 else |
|
385 rBufOut.Copy(aBufIn); |
|
386 return 0; |
|
387 } |
|
388 |
|
389 EXPORT_C TInt CImConvertCharconv::PrepareToConvertToFromOurCharsetL(const TUint aUid) |
|
390 { |
|
391 if (!aUid) |
|
392 { |
|
393 iCharsetUid=DefaultCharset(); |
|
394 return ETrue; |
|
395 } |
|
396 else if (CharsetAvailable(aUid)) |
|
397 { |
|
398 iCharconvState = CCnvCharacterSetConverter::KStateDefault; |
|
399 iConverter.PrepareToConvertToOrFromL(aUid, *iCharsetsAvailable, iFs); |
|
400 iConverter.SetReplacementForUnconvertibleUnicodeCharactersL(KMiutUndisplayableChar); |
|
401 iCharsetUid=aUid; |
|
402 return ETrue; |
|
403 } |
|
404 |
|
405 iCharsetUid=KUidMsvCharsetNone; |
|
406 return EFalse; |
|
407 } |
|
408 |
|
409 TBool CImConvertCharconv::CharsetAvailable(const TUint aUid) |
|
410 { |
|
411 iCharsetUid=aUid ? aUid : DefaultCharset(); |
|
412 |
|
413 if (!iCharsetUid) |
|
414 return EFalse; // No charset |
|
415 |
|
416 TInt i=iCharsetsAvailable->Count(); |
|
417 while (i--) |
|
418 { |
|
419 if (iCharsetsAvailable->At(i).Identifier()==iCharsetUid) |
|
420 return ETrue; // Charset conversion available. |
|
421 } |
|
422 |
|
423 return EFalse; // Charset id exists, but conversion unavaliable. |
|
424 } |
|
425 |
|
426 |
|
427 // Convert decoded buffer to Unicode |
|
428 // |
|
429 EXPORT_C TInt CImConvertCharconv::ConvertToOurCharsetL(const TDesC8& aBufIn, TDes& rBufOut, |
|
430 TInt& rNumUnconvertedChars, |
|
431 TInt& rIndexOfFirstUnconvertedChar) |
|
432 { |
|
433 // Conversion is not needed if iCharsetUid is any of UTF-16(UCS2), UTF-16LE(LittleEndian), UTF-16BE |
|
434 // just do a straight copy. |
|
435 if (!iCharsetUid || iCharsetUid==KUidMsvCharsetNone || iCharsetUid == KCharacterSetIdentifierUcs2 |
|
436 || iCharsetUid == KCharacterSetIdentifierUnicodeLittle || iCharsetUid == KCharacterSetIdentifierUnicodeBig) |
|
437 { |
|
438 return StraightCopy(aBufIn, rBufOut); |
|
439 } |
|
440 else |
|
441 { |
|
442 rNumUnconvertedChars=0; |
|
443 rIndexOfFirstUnconvertedChar=-1; |
|
444 TInt err = iConverter.ConvertToUnicode(rBufOut, aBufIn, iCharconvState, |
|
445 rNumUnconvertedChars, rIndexOfFirstUnconvertedChar); |
|
446 if (err<0 && rBufOut.Length()>0) |
|
447 rBufOut.Zero(); |
|
448 return err; |
|
449 } |
|
450 } |
|
451 |
|
452 // Convert decoded buffer to Unicode |
|
453 EXPORT_C TInt CImConvertCharconv::ConvertFromOurCharsetL(const TDesC& aBufIn, TDes8& rBufOut, |
|
454 TInt& rNumUnconvertedChars, |
|
455 TInt& rIndexOfFirstUnconvertedChar) |
|
456 { |
|
457 // Conversion is not needed if iCharsetUid is any of UTF-16(UCS2), UTF-16LE(LittleEndian), UTF-16BE |
|
458 // just do a straight copy. |
|
459 if (iCharsetUid && iCharsetUid != KUidMsvCharsetNone && iCharsetUid != KCharacterSetIdentifierUcs2 |
|
460 && iCharsetUid != KCharacterSetIdentifierUnicodeLittle && iCharsetUid != KCharacterSetIdentifierUnicodeBig) |
|
461 { |
|
462 return iConverter.ConvertFromUnicode(rBufOut, aBufIn, rNumUnconvertedChars, rIndexOfFirstUnconvertedChar); |
|
463 } |
|
464 else |
|
465 { |
|
466 rNumUnconvertedChars=0; |
|
467 rIndexOfFirstUnconvertedChar=-1; |
|
468 return StraightCopy(aBufIn, rBufOut); |
|
469 } |
|
470 } |
|
471 |
|
472 |
|
473 EXPORT_C TUint CImConvertCharconv::GetMimeCharsetUidL(const TDesC8& aBufIn) const |
|
474 { |
|
475 TUint id=0; |
|
476 id=iConverter.ConvertStandardNameOfCharacterSetToIdentifierL(aBufIn, iFs); |
|
477 if (!id) |
|
478 id = KUidMsvCharsetNone; |
|
479 return id; |
|
480 } |
|
481 |
|
482 EXPORT_C HBufC8* CImConvertCharconv::GetMimeCharsetTextStringL(const TUint& aUid) const |
|
483 { |
|
484 return iConverter.ConvertCharacterSetIdentifierToStandardNameL(aUid, iFs); |
|
485 } |
|
486 |
|
487 EXPORT_C TUint CImConvertCharconv::DefaultCharset() const |
|
488 { |
|
489 return iSystemDefaultCharset? iSystemDefaultCharset : KDefaultPlainCharset; |
|
490 } |
|
491 |
|
492 EXPORT_C TUint CImConvertCharconv::SystemDefaultCharset() const |
|
493 { |
|
494 return iSystemDefaultCharset; |
|
495 } |
|
496 |
|
497 void CImConvertCharconv::SetSystemDefaultCharsetL() |
|
498 { |
|
499 TBuf8<KMaxMIMECharSetLength> charsetName; |
|
500 TUint charset = KDefaultPlainCharset; |
|
501 |
|
502 // Try to get the character set from the Central Repository |
|
503 TInt err = GetSystemCharsetFromCenRepL(charsetName); |
|
504 if (KErrNone != err) |
|
505 { |
|
506 // That failed, fallback to reading from the resource file |
|
507 // Check the resource file.. |
|
508 RResourceFile resFile; |
|
509 OpenResourceFileL(resFile, iFs); // NB leaves if file not found |
|
510 |
|
511 // make sure the resource file will be closed if anything goes wrong |
|
512 TCleanupItem close( CloseResourceFile, &resFile ); |
|
513 CleanupStack::PushL( close ); |
|
514 |
|
515 HBufC8* buf = resFile.AllocReadLC( DEFAULT_SYSTEM_CHARSET ); |
|
516 |
|
517 TResourceReader reader; |
|
518 reader.SetBuffer(buf); |
|
519 charsetName.Copy(reader.ReadTPtrC8()); |
|
520 |
|
521 CleanupStack::PopAndDestroy(2,&resFile); // buf, resFile |
|
522 } |
|
523 |
|
524 charset = GetMimeCharsetUidL(charsetName); |
|
525 |
|
526 // If the character set in the resource file is not recognised. Leave.. |
|
527 if (charset==KUidMsvCharsetNone || !CharsetAvailable(charset)) |
|
528 User::Panic(_L("IMCM"),EImcmSystemDefaultCharsetNotSupported); |
|
529 else |
|
530 iSystemDefaultCharset=charset; |
|
531 } |
|
532 // Default charset is wrong when language is English on a China phone |
|
533 TInt CImConvertCharconv::GetSystemCharsetFromCenRepL(TDes8& aMimeCharSet) |
|
534 { |
|
535 // Open the repository |
|
536 CRepository* repository = CRepository::NewL(KUidMsgEmailGeneralCenRep); |
|
537 // Read the value |
|
538 TInt err = repository->Get(EEmailGeneralCharSetId, aMimeCharSet); |
|
539 delete repository; |
|
540 return err; |
|
541 } |
|
542 |