|
1 /* |
|
2 * Copyright (c) 2009 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: Implementation of SysLangUtil class. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <s32file.h> |
|
20 #include <centralrepository.h> |
|
21 |
|
22 #include "syslangutil.h" |
|
23 #include "syslangutiltrace.h" |
|
24 #include "ssmmapperutilitystatic.h" |
|
25 #include "syslangutilprivatecrkeys.h" |
|
26 |
|
27 const TInt KReadBufSize = 10; |
|
28 const TInt KLangArraySize = 20; |
|
29 |
|
30 // R&D support: Use language file in Starters internal directory for module |
|
31 // testing purposes. |
|
32 #ifdef __STARTER_MODULE_TEST_SUPPORT__ |
|
33 _LIT( KLanguagesIni, "C:\\private\\100059C9\\languages.txt" ); |
|
34 #else // __STARTER_MODULE_TEST_SUPPORT__ |
|
35 _LIT( KLanguagesIni, "z:\\resource\\bootdata\\languages.txt" ); |
|
36 #endif // __STARTER_MODULE_TEST_SUPPORT__ |
|
37 |
|
38 // ============================ MEMBER FUNCTIONS =============================== |
|
39 |
|
40 // ----------------------------------------------------------------------------- |
|
41 // SysLangUtil::IsValidLanguage |
|
42 // |
|
43 // ----------------------------------------------------------------------------- |
|
44 // |
|
45 EXPORT_C TBool SysLangUtil::IsValidLanguage( |
|
46 const TInt& aLanguage, |
|
47 RFs* aFileServerSession ) |
|
48 { |
|
49 API_FUNC_ENTRY_TRACE; |
|
50 |
|
51 TBool hadFS; |
|
52 TInt err; |
|
53 aFileServerSession = CheckFS( aFileServerSession, hadFS, err ); |
|
54 |
|
55 if ( err ) |
|
56 { |
|
57 return EFalse; |
|
58 } |
|
59 |
|
60 RFile file; |
|
61 err = file.Open( |
|
62 *aFileServerSession, |
|
63 KLanguagesIni, |
|
64 EFileStream | EFileRead | EFileShareReadersOnly ); |
|
65 |
|
66 if ( !err ) |
|
67 { |
|
68 // Prepare Reader |
|
69 TFileText reader; |
|
70 reader.Set(file); |
|
71 if ( reader.Seek(ESeekStart) ) |
|
72 { |
|
73 file.Close(); |
|
74 if ( !hadFS ) |
|
75 { |
|
76 aFileServerSession->Close(); |
|
77 delete aFileServerSession; |
|
78 aFileServerSession = NULL; |
|
79 } |
|
80 return EFalse; |
|
81 } |
|
82 |
|
83 TBuf<KReadBufSize> readBuf; |
|
84 err = EFalse; // This solution is not very elegant but saves ROM :) |
|
85 |
|
86 while ( !reader.Read( readBuf ) && readBuf.Length() ) |
|
87 { |
|
88 if ( readBuf.Length() > 1 ) |
|
89 { |
|
90 TLex lex( readBuf ); |
|
91 lex.SkipSpace(); |
|
92 TInt language; |
|
93 err = lex.Val( language ); |
|
94 |
|
95 if ( language == aLanguage ) |
|
96 { |
|
97 err = ETrue; |
|
98 break; |
|
99 } |
|
100 } |
|
101 } |
|
102 file.Close(); |
|
103 } |
|
104 else |
|
105 { |
|
106 err = EFalse; |
|
107 ERROR_TRACE_1( "ERROR: file '%S' is missing", &KLanguagesIni ); |
|
108 } |
|
109 |
|
110 if ( !hadFS ) |
|
111 { |
|
112 aFileServerSession->Close(); |
|
113 delete aFileServerSession; |
|
114 aFileServerSession = NULL; |
|
115 } |
|
116 |
|
117 FUNC_EXIT_RET_TRACE( err ); |
|
118 return err; |
|
119 } |
|
120 |
|
121 |
|
122 // ----------------------------------------------------------------------------- |
|
123 // SysLangUtil::GetDefaultLanguage |
|
124 // |
|
125 // ----------------------------------------------------------------------------- |
|
126 // |
|
127 EXPORT_C TInt SysLangUtil::GetDefaultLanguage( |
|
128 TInt& aLanguage, |
|
129 RFs* aFileServerSession ) |
|
130 { |
|
131 API_FUNC_ENTRY_TRACE; |
|
132 |
|
133 TBool hadFS; |
|
134 TInt err; |
|
135 aFileServerSession = CheckFS( aFileServerSession, hadFS, err ); |
|
136 |
|
137 if ( err ) |
|
138 { |
|
139 return err; |
|
140 } |
|
141 |
|
142 RFile file; |
|
143 err = file.Open( |
|
144 *aFileServerSession, |
|
145 KLanguagesIni, |
|
146 EFileStream | EFileRead | EFileShareReadersOnly ); |
|
147 |
|
148 if ( !err ) |
|
149 { |
|
150 // Prepare Reader |
|
151 TFileText reader; |
|
152 reader.Set(file); |
|
153 err = reader.Seek( ESeekStart ); |
|
154 if ( !err ) |
|
155 { |
|
156 TBuf<KReadBufSize> readBuf; |
|
157 |
|
158 while ( !reader.Read( readBuf ) && readBuf.Length() ) |
|
159 { |
|
160 if ( readBuf.Locate('d') != KErrNotFound ) |
|
161 { |
|
162 TLex lex( readBuf ); |
|
163 lex.SkipSpace(); |
|
164 err = lex.Val( aLanguage ); |
|
165 break; |
|
166 } |
|
167 |
|
168 readBuf.Zero(); |
|
169 } |
|
170 |
|
171 if ( !readBuf.Length() ) |
|
172 { |
|
173 ERROR_TRACE_1( "ERROR: file '%S' is missing", &KLanguagesIni ); |
|
174 file.Close(); |
|
175 return KErrNotFound; |
|
176 } |
|
177 } |
|
178 file.Close(); |
|
179 } |
|
180 |
|
181 if ( !hadFS ) |
|
182 { |
|
183 aFileServerSession->Close(); |
|
184 delete aFileServerSession; |
|
185 aFileServerSession = NULL; |
|
186 } |
|
187 |
|
188 FUNC_EXIT_RET_TRACE( err ); |
|
189 return err; |
|
190 } |
|
191 |
|
192 |
|
193 // ----------------------------------------------------------------------------- |
|
194 // SysLangUtil::GetInstalledLanguages |
|
195 // |
|
196 // ----------------------------------------------------------------------------- |
|
197 // |
|
198 EXPORT_C TInt SysLangUtil::GetInstalledLanguages( |
|
199 CArrayFixFlat<TInt>*& aLanguages, |
|
200 RFs* aFileServerSession ) |
|
201 { |
|
202 API_FUNC_ENTRY_TRACE; |
|
203 |
|
204 if ( aLanguages ) |
|
205 { |
|
206 delete aLanguages; |
|
207 } |
|
208 |
|
209 aLanguages = new CArrayFixFlat<TInt>( KLangArraySize ); |
|
210 |
|
211 if ( !aLanguages ) |
|
212 { |
|
213 return KErrNoMemory; |
|
214 } |
|
215 |
|
216 |
|
217 TBool hadFS; |
|
218 TInt err; |
|
219 aFileServerSession = CheckFS( aFileServerSession, hadFS, err ); |
|
220 |
|
221 if ( err ) |
|
222 { |
|
223 return err; |
|
224 } |
|
225 |
|
226 RFile file; |
|
227 err = file.Open( |
|
228 *aFileServerSession, |
|
229 KLanguagesIni, |
|
230 EFileStream | EFileRead | EFileShareReadersOnly ); |
|
231 if ( !err ) |
|
232 { |
|
233 // Prepare Reader |
|
234 TFileText reader; |
|
235 reader.Set( file ); |
|
236 err = reader.Seek( ESeekStart ); |
|
237 if ( !err ) |
|
238 { |
|
239 TBuf<KReadBufSize> readBuf; |
|
240 |
|
241 for ( TInt i = 0; !reader.Read( readBuf ) && readBuf.Length(); i++ ) |
|
242 { |
|
243 if ( readBuf.Length() > 0 ) |
|
244 { |
|
245 TLex lex( readBuf ); |
|
246 lex.SkipSpace(); |
|
247 TInt language; |
|
248 err = lex.Val( language ); |
|
249 if ( err ) break; |
|
250 // Will not leave if array size (KLangArraySize) not exceeded |
|
251 TRAP( err, aLanguages->AppendL( language ) ); |
|
252 err = KErrNone; // Ignore. |
|
253 } |
|
254 readBuf.Zero(); |
|
255 } |
|
256 } |
|
257 file.Close(); |
|
258 } |
|
259 else |
|
260 { |
|
261 ERROR_TRACE_1( "ERROR: file '%S' is missing", &KLanguagesIni ); |
|
262 } |
|
263 |
|
264 if ( !hadFS ) |
|
265 { |
|
266 aFileServerSession->Close(); |
|
267 delete aFileServerSession; |
|
268 aFileServerSession = NULL; |
|
269 } |
|
270 |
|
271 FUNC_EXIT_RET_TRACE( err ); |
|
272 return err; |
|
273 } |
|
274 |
|
275 |
|
276 // ----------------------------------------------------------------------------- |
|
277 // SysLangUtil::RestoreSIMLanguage |
|
278 // |
|
279 // ----------------------------------------------------------------------------- |
|
280 // |
|
281 EXPORT_C TInt SysLangUtil::RestoreSIMLanguage( |
|
282 TInt &aLanguage, |
|
283 RFs* /*aFileServerSession*/ ) |
|
284 { |
|
285 API_FUNC_ENTRY_TRACE; |
|
286 |
|
287 TUid mappedUid = SsmMapperUtility::CrUid( KCRUidSysLangUtil ); |
|
288 |
|
289 CRepository* repository = NULL; |
|
290 TRAPD( errorCode, repository = CRepository::NewL( mappedUid ) ); |
|
291 |
|
292 if ( errorCode == KErrNone ) |
|
293 { |
|
294 errorCode = repository->Get( KSysLangUtilSimLanguage, aLanguage ); |
|
295 |
|
296 if ( errorCode != KErrNone ) |
|
297 { |
|
298 ERROR_TRACE_1( "Failed to get value from CentRep, error code %d.", errorCode ); |
|
299 } |
|
300 } |
|
301 else |
|
302 { |
|
303 ERROR_TRACE_1( "Failed to open repository, error code %d.", errorCode ); |
|
304 } |
|
305 |
|
306 delete repository; |
|
307 |
|
308 FUNC_EXIT_RET_TRACE( errorCode ); |
|
309 return errorCode; |
|
310 } |
|
311 |
|
312 |
|
313 // ----------------------------------------------------------------------------- |
|
314 // SysLangUtil::CheckFS |
|
315 // |
|
316 // ----------------------------------------------------------------------------- |
|
317 // |
|
318 RFs* SysLangUtil::CheckFS( RFs* aRFs, TBool& aExist, TInt& aErr ) |
|
319 { |
|
320 FUNC_ENTRY_TRACE; |
|
321 |
|
322 if ( !aRFs ) |
|
323 { |
|
324 aExist = EFalse; |
|
325 aRFs = new RFs; |
|
326 if ( aRFs ) |
|
327 { |
|
328 aErr = aRFs->Connect(); |
|
329 if ( aErr != KErrNone) |
|
330 { |
|
331 delete aRFs; |
|
332 return NULL; |
|
333 } |
|
334 } |
|
335 else |
|
336 { |
|
337 aErr = KErrNoMemory; |
|
338 return NULL; |
|
339 } |
|
340 } |
|
341 else |
|
342 { |
|
343 // File server session given as argument MUST be already connected |
|
344 ASSERT_TRACE( aRFs->Handle() ); |
|
345 |
|
346 aExist = ETrue; |
|
347 } |
|
348 |
|
349 aErr = KErrNone; |
|
350 |
|
351 FUNC_EXIT_TRACE; |
|
352 return aRFs; |
|
353 } |