|
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: Declaration of some common global functions. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include <eikdef.h> |
|
21 #include <ApEngineConsts.h> |
|
22 #include <commdb.h> |
|
23 #include <ApUtils.h> |
|
24 #include <centralrepository.h> // link against centralrepository.lib |
|
25 |
|
26 #include "ApEngineCommons.h" |
|
27 #include "ApProtHandler.h" |
|
28 #include "ApEngineLogger.h" |
|
29 #include "ApEnginePrivateCRKeys.h" |
|
30 |
|
31 // CONSTANTS |
|
32 |
|
33 |
|
34 // LOCAL CONSTANTS AND MACROS |
|
35 _LIT(KFormatPostfix, "%S(%02d)"); |
|
36 _LIT(KFormatNoPostfix, "%S"); |
|
37 _LIT(KFormatLargePostfix, "%S(%d)"); |
|
38 |
|
39 void ApCommons::Panic( TApEnginePanicCodes aPanic ) |
|
40 { |
|
41 _LIT( kApSet, "APEngine" ) ; |
|
42 User::Panic( kApSet, aPanic ) ; |
|
43 } |
|
44 |
|
45 |
|
46 // --------------------------------------------------------- |
|
47 // ApCommons::StartPushedTransactionL |
|
48 // --------------------------------------------------------- |
|
49 // |
|
50 TBool ApCommons::StartPushedTransactionLC( CCommsDatabase& aDb, TBool aRetry ) |
|
51 { |
|
52 CLOG( ( ETransaction, 0, |
|
53 _L( "-> ApCommons::StartPushedTransactionLC" ) ) ); |
|
54 |
|
55 TBool retval( EFalse ); |
|
56 if ( !aDb.InTransaction() ) |
|
57 { |
|
58 TInt retriesLeft( aRetry ? KRetryCount : 1 ); |
|
59 TInt err = aDb.BeginTransaction(); |
|
60 while ( ( ( err == KErrLocked ) || ( err == KErrAccessDenied ) ) |
|
61 && --retriesLeft ) |
|
62 { |
|
63 User::After( KRetryWait ); |
|
64 err = aDb.BeginTransaction(); |
|
65 } |
|
66 User::LeaveIfError( err ); |
|
67 CleanupStack::PushL |
|
68 ( |
|
69 TCleanupItem( ApCommons::RollbackTransactionOnLeave, &aDb ) |
|
70 ); |
|
71 retval = ETrue; |
|
72 } |
|
73 else |
|
74 { |
|
75 retval = EFalse; |
|
76 } |
|
77 CLOG( ( ETransaction, 1, |
|
78 _L( "<- ApCommons::StartPushedTransactionLC" ) ) ); |
|
79 return retval; |
|
80 } |
|
81 |
|
82 |
|
83 |
|
84 // --------------------------------------------------------- |
|
85 // ApCommons::RollbackTransactionOnLeave |
|
86 // --------------------------------------------------------- |
|
87 // |
|
88 void ApCommons::RollbackTransactionOnLeave( TAny* aDb ) |
|
89 { |
|
90 CLOG( ( ETransaction, 0, |
|
91 _L( "-> ApCommons::RollbackTransactionOnLeave" ) ) ); |
|
92 |
|
93 CCommsDatabase* db = STATIC_CAST( CCommsDatabase*, aDb ); |
|
94 db->RollbackTransaction(); |
|
95 |
|
96 CLOG( ( ETransaction, 1, |
|
97 _L( "<- ApCommons::RollbackTransactionOnLeave" ) ) ); |
|
98 } |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 // --------------------------------------------------------- |
|
104 // ApCommons::DoMakeValidNameL |
|
105 // --------------------------------------------------------- |
|
106 // |
|
107 HBufC* ApCommons::DoMakeValidNameL |
|
108 ( |
|
109 CCommsDatabase& aDb, |
|
110 TBool& aChanged, |
|
111 HBufC* aName, |
|
112 TUint32 aUid, |
|
113 TBool aIsNew |
|
114 ) |
|
115 { |
|
116 CLOG( ( ECommons, 0, _L( "-> ApCommons::DoMakeValidNameL" ) ) ); |
|
117 |
|
118 aChanged = EFalse; |
|
119 HBufC* temp = HBufC::NewLC( KApMaxConnNameLength ); |
|
120 HBufC* temp2 = HBufC::NewLC( KApMaxConnNameLength ); |
|
121 |
|
122 HBufC* corrname = ApCommons::EnsureMaxLengthLC( aName, aChanged ); |
|
123 *temp = *corrname; |
|
124 TInt postfix( 0 ); |
|
125 TInt pf( 0 ); |
|
126 TInt i( 0 ); |
|
127 TBool valid ( EFalse ); |
|
128 |
|
129 TPtrC prefix = ApCommons::GetPrefix( *corrname ); |
|
130 |
|
131 CApUtils* utils = CApUtils::NewLC( aDb ); |
|
132 |
|
133 if ( aIsNew ) |
|
134 { |
|
135 aUid = 0; |
|
136 } |
|
137 |
|
138 postfix = GetPostfix( *temp, prefix ); |
|
139 postfix = -1; |
|
140 do |
|
141 { |
|
142 valid = utils->IsValidNameL( *temp, aUid ); |
|
143 if ( !valid ) |
|
144 { |
|
145 aChanged = ETrue; |
|
146 postfix++; |
|
147 // check the length of postfix, check text length accordingly |
|
148 pf = postfix; |
|
149 for (i=1; i<10; i++) |
|
150 { |
|
151 pf /= 10; |
|
152 if ( !pf ) |
|
153 break; |
|
154 } |
|
155 TPtr sgdptr( temp->Des() ); |
|
156 TPtr sgdptr2( temp2->Des() ); |
|
157 if ( postfix ) |
|
158 { |
|
159 if ( postfix < 10 ) |
|
160 { |
|
161 sgdptr2 = prefix.Left( KApMaxConnNameLength - i - 3 ); |
|
162 } |
|
163 else |
|
164 { |
|
165 sgdptr2 = prefix.Left( KApMaxConnNameLength - i - 2 ); |
|
166 } |
|
167 } |
|
168 else |
|
169 { |
|
170 sgdptr2 = prefix.Left( KApMaxConnNameLength ); |
|
171 } |
|
172 if ( postfix ) |
|
173 { |
|
174 if ( postfix > 9 ) |
|
175 { |
|
176 sgdptr.Format( KFormatLargePostfix, &sgdptr2, |
|
177 postfix ); |
|
178 } |
|
179 else |
|
180 { |
|
181 sgdptr.Format( KFormatPostfix, &sgdptr2, |
|
182 postfix ); |
|
183 } |
|
184 } |
|
185 else |
|
186 { |
|
187 sgdptr.Format( KFormatNoPostfix, &sgdptr2 ); |
|
188 } |
|
189 } |
|
190 } while ( !valid ); |
|
191 |
|
192 CleanupStack::PopAndDestroy( utils ); |
|
193 CleanupStack::PopAndDestroy( corrname ); |
|
194 CleanupStack::PopAndDestroy( temp2 ); |
|
195 |
|
196 if ( aChanged ) |
|
197 { |
|
198 CleanupStack::Pop( temp ); |
|
199 } |
|
200 else |
|
201 { |
|
202 CleanupStack::PopAndDestroy( temp ); |
|
203 temp = NULL; |
|
204 } |
|
205 |
|
206 CLOG( ( ECommons, 1, _L( "<- ApCommons::DoMakeValidNameL" ) ) ); |
|
207 |
|
208 return temp; |
|
209 } |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 // --------------------------------------------------------- |
|
216 // ApCommons::EnsureMaxLengthLC |
|
217 // --------------------------------------------------------- |
|
218 // |
|
219 HBufC* ApCommons::EnsureMaxLengthLC( const TDesC* aName, TBool& aChanged ) |
|
220 { |
|
221 CLOG( ( ECommons, 0, _L( "-> ApCommons::EnsureMaxLengthLC" ) ) ); |
|
222 |
|
223 TInt length = aName->Length(); |
|
224 |
|
225 aChanged = EFalse; |
|
226 |
|
227 if ( !length ) |
|
228 { |
|
229 // Name is required. |
|
230 User::Leave(KErrInvalidName); |
|
231 } |
|
232 |
|
233 HBufC* corrname; |
|
234 if ( KApMaxConnNameLength < length ) |
|
235 { // name too long, truncate. |
|
236 corrname = aName->Left( KApMaxConnNameLength ).AllocLC(); |
|
237 aChanged = ETrue; |
|
238 } |
|
239 else |
|
240 { |
|
241 corrname = aName->AllocLC(); |
|
242 corrname->Des().Trim(); |
|
243 if ( corrname->Length() == 0 ) |
|
244 { |
|
245 User::Leave(KErrInvalidName); |
|
246 } |
|
247 // comes here only if name is valid |
|
248 if ( corrname->Length() != aName->Length() ) |
|
249 { |
|
250 aChanged = ETrue; |
|
251 } |
|
252 } |
|
253 CLOG( ( ECommons, 1, _L( "<- ApCommons::EnsureMaxLengthLC" ) ) ); |
|
254 |
|
255 return corrname; |
|
256 } |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 /** |
|
262 * Given aName in the format <prefix> or <prefix><brace><integer><brace>, |
|
263 * return a pointer to the leading part. |
|
264 * That is, if there is trailing <space><integer>, |
|
265 * then that is excluded; if there is no trailing part, then the original |
|
266 * decriptor is returned. |
|
267 * Examples: |
|
268 * - "Foo" returns "Foo"; |
|
269 * - "Foo 12" returns "Foo 12"; |
|
270 * - "Foo(12)" returns "Foo"; |
|
271 * - "Foo 12 (34)" returns "Foo 12 "; |
|
272 * - "Foo bar" returns "Foo bar"; |
|
273 * - "Foo " returns "Foo ". |
|
274 */ |
|
275 TPtrC ApCommons::GetPrefix( const TDesC& aName ) |
|
276 { |
|
277 TPtrC prefix = aName; |
|
278 TInt lastBrace = aName.LocateReverse('('); |
|
279 if ( lastBrace != KErrNotFound ) |
|
280 { |
|
281 // aName looks like "<prefix><brace><something>". |
|
282 // See if <something> is an integer number. |
|
283 TPtrC num = aName.Right( aName.Length() - lastBrace - 1 ); |
|
284 TInt val; |
|
285 TLex lex( num ); |
|
286 if ( lex.Val( val ) == KErrNone ) |
|
287 { |
|
288 // Yes, the trailer is an integer. |
|
289 prefix.Set( aName.Left( lastBrace ) ); |
|
290 } |
|
291 } |
|
292 return prefix; |
|
293 } |
|
294 |
|
295 |
|
296 /** |
|
297 * If aName is constructed from aPrefix with a postfix, get the numeric |
|
298 * value of the postfix, e.g: |
|
299 * - GetPostfix( "Foo (3)", "Foo" ) == 3 |
|
300 * - GetPostfix( "Foo 23 (45)", "Foo 23" ) == 45 |
|
301 * If aName is the same as aPrefix, return 0, e.g.: |
|
302 * - GetPostfix( "Foo", "Foo" ) == 0 |
|
303 * If aName is not constructed from aPrefix, return -1, e.g.: |
|
304 * - GetPostfix( "Foobar", "Foo" ) == -1 |
|
305 * - GetPostfix( "Fo 23 45", "Foo" ) == -1 |
|
306 */ |
|
307 TInt ApCommons::GetPostfix( const TDesC& aName, const TDesC& aPrefix ) |
|
308 { |
|
309 TInt postfix( KErrNotFound ); |
|
310 TInt nameLength = aName.Length(); |
|
311 TInt prefixLength = aPrefix.Length(); |
|
312 if ( nameLength >= prefixLength && aName.FindF( aPrefix ) == 0 ) |
|
313 { |
|
314 // aName is longer or equal length, and |
|
315 // aPrefix can be found in the beginning of aName. |
|
316 if ( nameLength == prefixLength ) |
|
317 { |
|
318 // They have the same length; they equal. |
|
319 postfix = 0; |
|
320 } |
|
321 else |
|
322 { |
|
323 if ( aName[ prefixLength ] == '(' ) |
|
324 { |
|
325 // (Now we know that aName is longer than aPrefix.) |
|
326 // aName looks like "aPrefix<brace><something>". |
|
327 // See if <something> is an integer number. |
|
328 TPtrC num = aName.Right( nameLength - prefixLength - 1 ); |
|
329 TInt val; |
|
330 TLex lex( num ); |
|
331 if ( lex.Val( val ) == KErrNone ) |
|
332 { |
|
333 // Yes, the trailer is an integer. |
|
334 if ( val > 0 ) |
|
335 { |
|
336 postfix = val; |
|
337 } |
|
338 else |
|
339 { |
|
340 // signal that it is invalid... |
|
341 postfix = -1; |
|
342 } |
|
343 } |
|
344 } |
|
345 } |
|
346 } |
|
347 return postfix; |
|
348 } |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 // --------------------------------------------------------- |
|
354 // ApCommons::ReadUintL |
|
355 // --------------------------------------------------------- |
|
356 // |
|
357 TInt ApCommons::ReadUintL |
|
358 ( |
|
359 CCommsDbTableView* atable, |
|
360 const TDesC& aColumn, |
|
361 TUint32& aValue |
|
362 ) |
|
363 { |
|
364 __ASSERT_DEBUG( (atable), Panic( ENullPointer ) ) ; |
|
365 TRAPD( err, atable->ReadUintL( aColumn, aValue ) ); |
|
366 if ( err != KErrNone ) |
|
367 { // do not leave if readed value is not present in table... |
|
368 if ( ( err != KErrUnknown ) && ( err != KErrNotFound ) ) |
|
369 User::Leave( err ); |
|
370 } |
|
371 return err; |
|
372 } |
|
373 |
|
374 |
|
375 // --------------------------------------------------------- |
|
376 // ApCommons::ReadBoolL |
|
377 // --------------------------------------------------------- |
|
378 // |
|
379 TInt ApCommons::ReadBoolL |
|
380 ( |
|
381 CCommsDbTableView* atable, |
|
382 const TDesC& aColumn, |
|
383 TBool& aValue |
|
384 ) |
|
385 { |
|
386 __ASSERT_DEBUG( (atable), Panic( ENullPointer ) ) ; |
|
387 TRAPD( err, atable->ReadBoolL( aColumn, aValue ) ); |
|
388 if ( err != KErrNone ) |
|
389 { // do not leave if readed value is not present in table... |
|
390 if ( err != KErrUnknown ) |
|
391 User::Leave( err ); |
|
392 } |
|
393 return err; |
|
394 } |
|
395 |
|
396 |
|
397 |
|
398 // --------------------------------------------------------- |
|
399 // ApCommons::ReadLongTextLC |
|
400 // --------------------------------------------------------- |
|
401 // |
|
402 HBufC* ApCommons::ReadLongTextLC |
|
403 ( |
|
404 CCommsDbTableView* atable, |
|
405 const TDesC& aColumn, |
|
406 TInt& aErrCode |
|
407 ) |
|
408 { |
|
409 __ASSERT_DEBUG( (atable), Panic( ENullPointer ) ) ; |
|
410 HBufC* value = NULL; |
|
411 // because of Trap, we need to call a func |
|
412 // which pops it from the stack & push it back afterwards... |
|
413 TRAP( aErrCode, value = ReadLongTextL( atable, aColumn ) ); |
|
414 CleanupStack::PushL( value ); |
|
415 if ( aErrCode != KErrNone ) |
|
416 { // do not leave if readed value is not present in table... |
|
417 if ( aErrCode != KErrUnknown ) |
|
418 User::Leave( aErrCode ); |
|
419 } |
|
420 return value; |
|
421 } |
|
422 |
|
423 |
|
424 |
|
425 // --------------------------------------------------------- |
|
426 // ApCommons::ReadLongTextL |
|
427 // --------------------------------------------------------- |
|
428 // |
|
429 HBufC* ApCommons::ReadLongTextL( CCommsDbTableView* atable, |
|
430 const TDesC& aColumn ) |
|
431 { |
|
432 __ASSERT_DEBUG( (atable), Panic( ENullPointer ) ) ; |
|
433 HBufC* value = atable->ReadLongTextLC( aColumn ); |
|
434 CleanupStack::Pop( value ); |
|
435 return value; |
|
436 } |
|
437 |
|
438 |
|
439 |
|
440 // --------------------------------------------------------- |
|
441 // ApCommons::EscapeTextLC |
|
442 // --------------------------------------------------------- |
|
443 // |
|
444 HBufC* ApCommons::EscapeTextLC( const TDesC& aLiteral ) |
|
445 { |
|
446 TInt l( aLiteral.Length() ); |
|
447 //> 2*, to ensure correct esc. even if ALL chars are quotes... |
|
448 HBufC* retbuf = HBufC::NewLC( 2*l ); |
|
449 TPtr ret = retbuf->Des(); |
|
450 TUint quote( '\'' ); // TChar gives warnings in THUMB & ARMI |
|
451 TInt i( 0 ); |
|
452 |
|
453 for( i=0; i<l; i++ ) |
|
454 { |
|
455 ret.Append( aLiteral[i] ); |
|
456 if ( aLiteral[i] == quote ) |
|
457 { |
|
458 // Duplicate quote. |
|
459 ret.Append( quote ); |
|
460 } |
|
461 } |
|
462 return retbuf; |
|
463 } |
|
464 |
|
465 |
|
466 |
|
467 // --------------------------------------------------------- |
|
468 // ApCommons::ReadText16ValueL |
|
469 // --------------------------------------------------------- |
|
470 // |
|
471 HBufC16* ApCommons::ReadText16ValueLC( CCommsDbTableView* atable, |
|
472 const TDesC& aCol ) |
|
473 { |
|
474 // Second variant, if always keeping mem usage to minimum. |
|
475 // This case code is a bit slower and more 'complicated'. |
|
476 // First version simpler, faster, less chance for errors |
|
477 // (e.g. if memory is exhausted, can not even create object |
|
478 // that may not be able to hold the values. |
|
479 __ASSERT_DEBUG( (atable), Panic( ENullPointer ) ) ; |
|
480 |
|
481 TInt aLength(0); |
|
482 atable->ReadColumnLengthL(aCol, aLength); |
|
483 |
|
484 HBufC* buf = HBufC::NewLC(aLength); |
|
485 TPtr ptr( buf->Des() ); |
|
486 atable->ReadTextL(aCol, ptr); |
|
487 return buf; |
|
488 } |
|
489 |
|
490 |
|
491 // --------------------------------------------------------- |
|
492 // ApCommons::ReadText8ValueLC |
|
493 // --------------------------------------------------------- |
|
494 // |
|
495 HBufC8* ApCommons::ReadText8ValueLC( CCommsDbTableView* atable, |
|
496 const TDesC& aCol ) |
|
497 { |
|
498 __ASSERT_DEBUG( (atable), Panic( ENullPointer ) ) ; |
|
499 TInt aLength(0); |
|
500 atable->ReadColumnLengthL(aCol, aLength); |
|
501 |
|
502 HBufC8* buf = HBufC8::NewLC(aLength); |
|
503 TPtr8 ptr( buf->Des() ); |
|
504 atable->ReadTextL(aCol, ptr); |
|
505 return buf; |
|
506 } |
|
507 |
|
508 |
|
509 // --------------------------------------------------------- |
|
510 // ApCommons::DoGetReadOnlyL |
|
511 // --------------------------------------------------------- |
|
512 // |
|
513 TBool ApCommons::DoGetReadOnlyL( CCommsDbTableView& aTable ) |
|
514 { |
|
515 return CApProtHandler::GetProtectionStateL( aTable ); |
|
516 } |
|
517 |
|
518 |
|
519 |
|
520 // --------------------------------------------------------- |
|
521 // ApCommons::CommitTransaction |
|
522 // --------------------------------------------------------- |
|
523 // |
|
524 TInt ApCommons::CommitTransaction( CCommsDatabase& aDb ) |
|
525 { |
|
526 CLOG( ( ETransaction, 0, _L( "-> ApCommons::CommitTransaction" ) ) ); |
|
527 TInt err = aDb.CommitTransaction(); |
|
528 CLOG( ( ETransaction, 1, _L( "<- ApCommons::CommitTransaction" ) ) ); |
|
529 return err; |
|
530 } |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 //---------------------------------------------------------- |
|
536 // ApCommons::GetVariantL |
|
537 //---------------------------------------------------------- |
|
538 // |
|
539 TInt ApCommons::GetVariantL() |
|
540 { |
|
541 CLOG( ( ETransaction, 0, _L( "-> ApCommons::GetVariantL" ) ) ); |
|
542 TInt variant( 0 ); |
|
543 // Connecting and initialization: |
|
544 CRepository* repository = CRepository::NewL( KCRUidApEngineLV ); |
|
545 repository->Get( KApEngineLVFlags, variant ); |
|
546 delete repository; |
|
547 |
|
548 CLOG( ( ETransaction, 1, _L( "<- ApCommons::GetVariantL" ) ) ); |
|
549 |
|
550 return variant; |
|
551 } |
|
552 |
|
553 |
|
554 |
|
555 // --------------------------------------------------------- |
|
556 // ApCommons::IsGivenSharedDataSupportL |
|
557 // --------------------------------------------------------- |
|
558 // |
|
559 TInt ApCommons::IsGivenSharedDataSupportL( const TUid aUid, |
|
560 const TUint32 aKey ) |
|
561 { |
|
562 TInt value( 0 ); |
|
563 // Connecting and initialization: |
|
564 CRepository* repository = CRepository::NewL( aUid ); |
|
565 repository->Get( aKey, value ); |
|
566 delete repository; |
|
567 return value; |
|
568 } |
|
569 |
|
570 |
|
571 // End of File |