1 /* |
|
2 * Copyright (c) 2007-2008 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 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <coemain.h> |
|
20 #include <bautils.h> |
|
21 #include <StringLoader.h> |
|
22 |
|
23 #include "hnmdlocalization.h" |
|
24 #include "hnmdlocalizationelement.h" |
|
25 #include "hnglobals.h" |
|
26 #include "hnconvutils.h" |
|
27 #include "hnutils.h" |
|
28 |
|
29 // ======== MEMBER FUNCTIONS ======== |
|
30 |
|
31 /** Max param count */ |
|
32 static const TInt KMaxParamsCount( 10 ); |
|
33 const TInt KFileExtensionLength( 4 ); |
|
34 |
|
35 _LIT(KLocalizationOneDes, "%U"); |
|
36 _LIT(KLocalizationMoreDes, "%?U"); |
|
37 _LIT(KLocalizationOneInt, "%N"); |
|
38 _LIT(KLocalizationMoreInt, "%?N"); |
|
39 |
|
40 // --------------------------------------------------------------------------- |
|
41 // |
|
42 // --------------------------------------------------------------------------- |
|
43 // |
|
44 void CHnMdLocalization::ConstructL() |
|
45 { |
|
46 iCoeEnv = CCoeEnv::Static(); |
|
47 ASSERT( iCoeEnv ); |
|
48 } |
|
49 |
|
50 // --------------------------------------------------------------------------- |
|
51 // |
|
52 // --------------------------------------------------------------------------- |
|
53 // |
|
54 CHnMdLocalization* CHnMdLocalization::NewL() |
|
55 { |
|
56 CHnMdLocalization* self = CHnMdLocalization::NewLC(); |
|
57 CleanupStack::Pop( self ); |
|
58 return self; |
|
59 } |
|
60 |
|
61 // --------------------------------------------------------------------------- |
|
62 // |
|
63 // --------------------------------------------------------------------------- |
|
64 // |
|
65 CHnMdLocalization* CHnMdLocalization::NewLC() |
|
66 { |
|
67 CHnMdLocalization* self = new( ELeave ) CHnMdLocalization; |
|
68 CleanupStack::PushL( self ); |
|
69 self->ConstructL(); |
|
70 return self; |
|
71 } |
|
72 |
|
73 // --------------------------------------------------------------------------- |
|
74 // |
|
75 // --------------------------------------------------------------------------- |
|
76 // |
|
77 void CHnMdLocalization::AppendLocalizationsL( TXmlEngElement aElement ) |
|
78 { |
|
79 RXmlEngNodeList< TXmlEngElement > children; |
|
80 CleanupClosePushL( children ); |
|
81 aElement.GetChildElements( children ); |
|
82 |
|
83 RPointerArray<CHnMdLocalizationElement> tempArray; |
|
84 CleanupClosePushL( tempArray ); |
|
85 TInt count = children.Count(); |
|
86 for ( TInt i( 0 ); i < count; i++ ) |
|
87 { |
|
88 TXmlEngElement child = children.Next(); |
|
89 |
|
90 if ( !child.Name().Compare( KLocalizationElementName8 ) ) |
|
91 { |
|
92 RBuf8 namespac; |
|
93 CleanupClosePushL( namespac ); |
|
94 if ( child.HasAttributeL( KNameSpaceAttrName8 ) ) |
|
95 { |
|
96 HnUtils::SetAttributeL( child, KNameSpaceAttrName8, namespac ); |
|
97 if ( !IsDuplicateL( namespac ) ) |
|
98 { |
|
99 CHnMdLocalizationElement* element = |
|
100 CHnMdLocalizationElement::NewL( child ); |
|
101 |
|
102 AppendElementL( element ); // ownership transfered |
|
103 } |
|
104 } |
|
105 CleanupStack::PopAndDestroy( &namespac ); |
|
106 } |
|
107 } |
|
108 |
|
109 CleanupStack::PopAndDestroy( &tempArray ); |
|
110 CleanupStack::PopAndDestroy( &children ); |
|
111 } |
|
112 |
|
113 // --------------------------------------------------------------------------- |
|
114 // |
|
115 // --------------------------------------------------------------------------- |
|
116 // |
|
117 const CHnMdLocalizationElement* CHnMdLocalization::ElementByNamespace( |
|
118 const TDesC& aNamespace ) const |
|
119 { |
|
120 TInt count = iInternalLocalization.Count(); |
|
121 const CHnMdLocalizationElement* element = NULL; |
|
122 |
|
123 for ( TInt i = 0; i < count; i++ ) |
|
124 { |
|
125 const CHnMdLocalizationElement* tmp = iInternalLocalization[i]; |
|
126 if ( !tmp->Namespace().Compare( aNamespace) ) |
|
127 { |
|
128 element = tmp; |
|
129 break; |
|
130 } |
|
131 } |
|
132 if( !element ) |
|
133 { |
|
134 count = iDynamicLocalization.Count(); |
|
135 |
|
136 for ( TInt i = 0; i < count; i++ ) |
|
137 { |
|
138 const CHnMdLocalizationElement* tmp = iDynamicLocalization[i]; |
|
139 if ( !tmp->Namespace().Compare( aNamespace) ) |
|
140 { |
|
141 element = tmp; |
|
142 break; |
|
143 } |
|
144 } |
|
145 } |
|
146 |
|
147 return element; |
|
148 } |
|
149 |
|
150 // --------------------------------------------------------------------------- |
|
151 // |
|
152 // --------------------------------------------------------------------------- |
|
153 // |
|
154 CHnMdLocalization::CHnMdLocalization() |
|
155 { |
|
156 } |
|
157 |
|
158 // --------------------------------------------------------------------------- |
|
159 // |
|
160 // --------------------------------------------------------------------------- |
|
161 // |
|
162 CHnMdLocalization::~CHnMdLocalization() |
|
163 { |
|
164 iDynamicLocalization.ResetAndDestroy(); |
|
165 for( TInt i( 0 ); i < iDynamicOffset.Count(); i++ ) |
|
166 { |
|
167 iCoeEnv->DeleteResourceFile( iDynamicOffset[i] ); |
|
168 } |
|
169 iDynamicOffset.Close(); |
|
170 |
|
171 iInternalLocalization.ResetAndDestroy(); |
|
172 for( TInt i( 0 ); i < iInternalOffset.Count(); i++ ) |
|
173 { |
|
174 iCoeEnv->DeleteResourceFile( iInternalOffset[i] ); |
|
175 } |
|
176 iInternalOffset.Close(); |
|
177 } |
|
178 |
|
179 // --------------------------------------------------------------------------- |
|
180 // |
|
181 // --------------------------------------------------------------------------- |
|
182 // |
|
183 void CHnMdLocalization::ReloadResourceFilesL() |
|
184 { |
|
185 for( TInt i( 0 ); i < iDynamicLocalization.Count(); i++ ) |
|
186 { |
|
187 iDynamicLocalization[i]->LocateLanguageFileL(); |
|
188 if( iDynamicLocalization[i]->SourcePath() ) |
|
189 { |
|
190 TInt offset( 0 ); |
|
191 TRAPD( err, offset = iCoeEnv->AddResourceFileL( |
|
192 *(iDynamicLocalization[i]->SourcePath()) ) ); |
|
193 if ( !err ) |
|
194 { |
|
195 iDynamicOffset.AppendL( offset ); |
|
196 } |
|
197 } |
|
198 } |
|
199 } |
|
200 |
|
201 // --------------------------------------------------------------------------- |
|
202 // |
|
203 // --------------------------------------------------------------------------- |
|
204 // |
|
205 void CHnMdLocalization::ReleaseResourceFiles() |
|
206 { |
|
207 iDynamicLocalization.ResetAndDestroy(); |
|
208 for( TInt i( 0 ); i < iDynamicOffset.Count(); i++ ) |
|
209 { |
|
210 iCoeEnv->DeleteResourceFile( iDynamicOffset[i] ); |
|
211 } |
|
212 iDynamicOffset.Reset(); |
|
213 } |
|
214 |
|
215 // --------------------------------------------------------------------------- |
|
216 // |
|
217 // --------------------------------------------------------------------------- |
|
218 // |
|
219 void CHnMdLocalization::AppendElementL( CHnMdLocalizationElement* aElement ) |
|
220 { |
|
221 if( IsInternalL( aElement->Source() ) ) |
|
222 { |
|
223 iInternalLocalization.AppendL( aElement ); |
|
224 if( IsResourceFile (aElement->Source()) ) |
|
225 { |
|
226 TInt offset( 0 ); |
|
227 TRAPD( err, offset = iCoeEnv->AddResourceFileL( *(aElement->SourcePath()) ) ); |
|
228 if ( !err ) |
|
229 { |
|
230 iInternalOffset.AppendL( offset ); |
|
231 } |
|
232 } |
|
233 } |
|
234 else |
|
235 { |
|
236 iDynamicLocalization.AppendL( aElement ); |
|
237 if( IsResourceFile (aElement->Source()) ) |
|
238 { |
|
239 TInt offset( 0 ); |
|
240 TRAPD( err, offset = iCoeEnv->AddResourceFileL( *(aElement->SourcePath()) ) ); |
|
241 if ( !err ) |
|
242 { |
|
243 iDynamicOffset.AppendL( offset ); |
|
244 } |
|
245 } |
|
246 } |
|
247 } |
|
248 |
|
249 // --------------------------------------------------------------------------- |
|
250 // |
|
251 // --------------------------------------------------------------------------- |
|
252 // |
|
253 TBool CHnMdLocalization::IsDuplicateL( TDesC8& aNamespace ) |
|
254 { |
|
255 HBufC* namespac = HnConvUtils::Str8ToStrLC( aNamespace ); |
|
256 TBool isDuplicate( IsDuplicateL( *namespac ) ); |
|
257 CleanupStack::PopAndDestroy( namespac ); |
|
258 return isDuplicate; |
|
259 } |
|
260 |
|
261 // --------------------------------------------------------------------------- |
|
262 // |
|
263 // --------------------------------------------------------------------------- |
|
264 // |
|
265 TBool CHnMdLocalization::IsDuplicateL( TDesC& aNamespace ) |
|
266 { |
|
267 TBool isDuplicate( EFalse ); |
|
268 |
|
269 if ( ElementByNamespace( aNamespace ) != NULL ) |
|
270 { |
|
271 isDuplicate = ETrue; |
|
272 } |
|
273 |
|
274 return isDuplicate; |
|
275 } |
|
276 |
|
277 // --------------------------------------------------------------------------- |
|
278 // |
|
279 // --------------------------------------------------------------------------- |
|
280 // |
|
281 TBool CHnMdLocalization::IsInternalL( const TDesC& aName ) |
|
282 { |
|
283 TBool isInternal( EFalse ); |
|
284 _LIT( KDot, "." ); |
|
285 |
|
286 TInt offset( aName.Find( KDot ) ); |
|
287 if( offset != KErrNotFound ) |
|
288 { |
|
289 if( aName.Left( offset ).Compare( KFolderSuite ) == KErrNone ) |
|
290 { |
|
291 isInternal = ETrue; |
|
292 } |
|
293 } |
|
294 |
|
295 return isInternal; |
|
296 } |
|
297 |
|
298 // --------------------------------------------------------------------------- |
|
299 // |
|
300 // --------------------------------------------------------------------------- |
|
301 // |
|
302 TBool CHnMdLocalization::IsResourceFile( const TDesC& aFileName ) |
|
303 { |
|
304 TInt pos = aFileName.Find( KExtension ); |
|
305 if( aFileName.Right( KFileExtensionLength ).Compare( KExtensionRsc ) ) |
|
306 { |
|
307 TBuf<2> tail; |
|
308 tail.Copy(aFileName.Right(2)); |
|
309 for (TInt i = 0; i < tail.Length(); i++ ) |
|
310 { |
|
311 TChar c = tail[i]; |
|
312 if ( !c.IsDigit() ) |
|
313 { |
|
314 pos = KErrNotFound; |
|
315 break; |
|
316 } |
|
317 } |
|
318 } |
|
319 return pos == ( aFileName.Length() - KFileExtensionLength ); |
|
320 } |
|
321 |
|
322 // --------------------------------------------------------------------------- |
|
323 // |
|
324 // --------------------------------------------------------------------------- |
|
325 // |
|
326 HBufC* CHnMdLocalization::LoadL( const TDesC& aResourceName, |
|
327 const CDesC16Array* aDesParams, const CArrayFix<TInt>* aIntParams ) |
|
328 { |
|
329 TInt position = aResourceName.Locate(':'); |
|
330 HBufC* res = NULL; |
|
331 if( position!= KErrNotFound ) |
|
332 { |
|
333 TPtrC resourceName = aResourceName.Mid(position + 1); |
|
334 TPtrC locName = aResourceName.Left(position); |
|
335 |
|
336 const CHnMdLocalizationElement* localization = |
|
337 ElementByNamespace( locName ); |
|
338 // if there is not namaspace defined, check may it is file in format |
|
339 // myapp.rsc:id |
|
340 if (!localization && !IsDuplicateL( locName ) && IsResourceFile( locName )) |
|
341 { |
|
342 // let's try to add namespace |
|
343 CHnMdLocalizationElement* element = |
|
344 CHnMdLocalizationElement::NewL( locName, locName); |
|
345 AppendElementL(element); // ownership transfered |
|
346 localization = ElementByNamespace( locName ); |
|
347 } |
|
348 |
|
349 if( localization && IsResourceFile( localization->Source() ) && localization->SourceExists() ) |
|
350 { |
|
351 HBufC8* resourceName8 = HBufC8::NewLC(resourceName.Length()); |
|
352 resourceName8->Des().Copy(resourceName); |
|
353 TInt resourceId (KErrNotFound); |
|
354 TRAPD(err, resourceId = |
|
355 const_cast<CHnMdLocalizationElement*>(localization)->FindResourceIdL(resourceName8) ); |
|
356 if ( err != KErrNone ) // maybe it was only number |
|
357 { |
|
358 err = HnConvUtils::Str8ToInt(*resourceName8, resourceId); |
|
359 } |
|
360 User::LeaveIfError(err); |
|
361 if ( iCoeEnv->IsResourceAvailableL( resourceId ) ) |
|
362 { |
|
363 res = iCoeEnv->AllocReadResourceL( resourceId ); |
|
364 } |
|
365 |
|
366 CleanupStack::PopAndDestroy( resourceName8 ); |
|
367 } |
|
368 } |
|
369 |
|
370 if (!res) // it mean that we dont have localization item for it |
|
371 { |
|
372 res = aResourceName.AllocL(); |
|
373 } |
|
374 if ((aDesParams && aDesParams->Count()) || |
|
375 (aIntParams && aIntParams->Count())) |
|
376 { |
|
377 CleanupStack::PushL(res); |
|
378 HBufC* formatted = FormatTextL(*res, aDesParams, aIntParams); |
|
379 CleanupStack::PopAndDestroy(res); |
|
380 res = formatted; |
|
381 } |
|
382 |
|
383 return res; |
|
384 } |
|
385 |
|
386 // --------------------------------------------------------------------------- |
|
387 // |
|
388 // --------------------------------------------------------------------------- |
|
389 // |
|
390 HBufC* CHnMdLocalization::FormatTextL( const TDesC& aTextToFormat, |
|
391 const CDesC16Array* aDesParams, const CArrayFix<TInt>* aIntParams ) |
|
392 { |
|
393 HBufC* res = aTextToFormat.AllocL(); |
|
394 TInt noParams(0); |
|
395 |
|
396 if (aDesParams) |
|
397 noParams += aDesParams->Count(); |
|
398 if (aIntParams) |
|
399 noParams += aIntParams->Count(); |
|
400 |
|
401 if (noParams > KMaxParamsCount) |
|
402 User::Leave( KErrNotSupported ); |
|
403 |
|
404 // let's check if we have descriptors to format |
|
405 if( aDesParams && aDesParams->Count() ) |
|
406 { |
|
407 // if we have only one we match to %U |
|
408 if (aDesParams->Count() == 1) |
|
409 { |
|
410 if ( res->FindC(KLocalizationOneDes) != KErrNotFound ) |
|
411 { |
|
412 TPtrC16 param = aDesParams->MdcaPoint(0); |
|
413 HBufC* dest = HBufC::NewL( res->Length() + |
|
414 param.Length() * KMaxParamsCount ); |
|
415 TPtr destDes(dest->Des()); |
|
416 StringLoader::Format( destDes, *res, -1, param ); |
|
417 delete res; |
|
418 res = dest; |
|
419 } |
|
420 } |
|
421 // and we match for format %(index)U |
|
422 TBufC<3> descformat(KLocalizationMoreDes); |
|
423 TInt desIndexUsed(0); |
|
424 for (TInt i = 0; i < noParams && desIndexUsed < aDesParams->Count(); i++) |
|
425 { |
|
426 descformat.Des()[1] = '0'+ i; // replace index infromation |
|
427 if ( res->FindC( descformat ) != KErrNotFound ) |
|
428 { |
|
429 |
|
430 TPtrC16 param = aDesParams->MdcaPoint(desIndexUsed++); |
|
431 HBufC* dest = HBufC::NewL( res->Length() + |
|
432 param.Length() * KMaxParamsCount ); |
|
433 TPtr destDes(dest->Des()); |
|
434 StringLoader::Format( destDes, *res, i, param ); |
|
435 delete res; |
|
436 res = dest; |
|
437 } |
|
438 } |
|
439 } |
|
440 // let's check if we have numbers to format |
|
441 if( aIntParams && aIntParams->Count() ) |
|
442 { |
|
443 // if we have only one we match to %N |
|
444 if (aIntParams->Count() == 1) |
|
445 { |
|
446 if ( res->FindC(KLocalizationOneInt) != KErrNotFound ) |
|
447 { |
|
448 HBufC* dest = HBufC::NewL( res->Length() + |
|
449 sizeof(TInt) * KMaxParamsCount ); |
|
450 TPtr destDes(dest->Des()); |
|
451 StringLoader::Format( destDes, *res, -1, aIntParams->At(0) ); |
|
452 delete res; |
|
453 res = dest; |
|
454 } |
|
455 } |
|
456 // and we match for format %(index)N |
|
457 TBufC<3> format(KLocalizationMoreInt); |
|
458 TInt numberUsed(0); |
|
459 for (TInt i = 0; i < noParams && numberUsed < aIntParams->Count(); i++) |
|
460 { |
|
461 format.Des()[1] = '0'+ i; |
|
462 if ( res->FindC(format) != KErrNotFound ) |
|
463 { |
|
464 HBufC* dest = HBufC::NewL( res->Length() + |
|
465 sizeof(TInt)* KMaxParamsCount ); |
|
466 TPtr destDes(dest->Des()); |
|
467 StringLoader::Format( destDes, *res, i, aIntParams->At(numberUsed++) ); |
|
468 delete res; |
|
469 res = dest; |
|
470 } |
|
471 } |
|
472 } |
|
473 return res; |
|
474 } |
|
475 |
|
476 |
|