1 /* |
|
2 * Copyright (c) 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: Utility class to central repository database. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <centralrepository.h> |
|
20 |
|
21 #include "cenrepdatabaseutil.h" |
|
22 //#include "//CENREPDATABASELOGger.h" |
|
23 |
|
24 TUint count( 0 ); |
|
25 const TInt KNoEntryId = 0; // Not allowed ID for entries. Deleted rows |
|
26 // are marked with KNoEntryId |
|
27 |
|
28 // ======== MEMBER FUNCTIONS ======== |
|
29 |
|
30 // --------------------------------------------------------------------------- |
|
31 // Constructor |
|
32 // --------------------------------------------------------------------------- |
|
33 // |
|
34 CCenRepDatabaseUtil::CCenRepDatabaseUtil( TUint32 aStartKey, |
|
35 TUint32 aColIncrement, |
|
36 TUint32 aColMask, |
|
37 TUint32 aIdCounterKey, |
|
38 TInt aColCount ) |
|
39 : iStartKey( aStartKey ), |
|
40 iColIncrement( aColIncrement ), |
|
41 iColMask( aColMask ), |
|
42 iIdCounterKey( aIdCounterKey ), |
|
43 iColCount( aColCount ) |
|
44 { |
|
45 } |
|
46 |
|
47 // --------------------------------------------------------------------------- |
|
48 // 2nd phase constructor |
|
49 // --------------------------------------------------------------------------- |
|
50 // |
|
51 void CCenRepDatabaseUtil::ConstructL( TUid aUid ) |
|
52 { |
|
53 if( iColIncrement - 1 + iColMask != KMaxTUint ) |
|
54 { |
|
55 User::Leave( KErrArgument ); |
|
56 } |
|
57 |
|
58 iRepository = CRepository::NewL( aUid ); |
|
59 |
|
60 // Create semaphore name from repository uid |
|
61 TUidName uidName = aUid.Name(); |
|
62 TInt err = iSemaphore.OpenGlobal( uidName ); |
|
63 if ( err != KErrNone ) |
|
64 { |
|
65 User::LeaveIfError( iSemaphore.CreateGlobal( uidName, 1 ) ); |
|
66 } |
|
67 |
|
68 } |
|
69 |
|
70 // --------------------------------------------------------------------------- |
|
71 // Constructor |
|
72 // --------------------------------------------------------------------------- |
|
73 // |
|
74 EXPORT_C CCenRepDatabaseUtil* CCenRepDatabaseUtil::NewL( TUid aUid, |
|
75 TUint32 aStartKey, |
|
76 TUint32 aColIncrement, |
|
77 TUint32 aColMask, |
|
78 TUint32 aIdCounterKey, |
|
79 TInt aColCount ) |
|
80 { |
|
81 ////CENREPDATABASELOG( "CCenRepDatabaseUtil::NewL - IN" ); |
|
82 |
|
83 CCenRepDatabaseUtil* self = CCenRepDatabaseUtil::NewLC( |
|
84 aUid, |
|
85 aStartKey, |
|
86 aColIncrement, |
|
87 aColMask, |
|
88 aIdCounterKey, |
|
89 aColCount ); |
|
90 CleanupStack::Pop( self ); |
|
91 |
|
92 ////CENREPDATABASELOG( "CCenRepDatabaseUtil::NewL - OUT" ); |
|
93 |
|
94 return self; |
|
95 } |
|
96 |
|
97 // --------------------------------------------------------------------------- |
|
98 // Constructor |
|
99 // --------------------------------------------------------------------------- |
|
100 // |
|
101 EXPORT_C CCenRepDatabaseUtil* CCenRepDatabaseUtil::NewLC( TUid aUid, |
|
102 TUint32 aStartKey, |
|
103 TUint32 aColIncrement, |
|
104 TUint32 aColMask, |
|
105 TUint32 aIdCounterKey, |
|
106 TInt aColCount ) |
|
107 { |
|
108 //CENREPDATABASELOG( "CCenRepDatabaseUtil::NewLC - IN" ); |
|
109 |
|
110 CCenRepDatabaseUtil* self = new( ELeave ) CCenRepDatabaseUtil( |
|
111 aStartKey, |
|
112 aColIncrement, |
|
113 aColMask, |
|
114 aIdCounterKey, |
|
115 aColCount ); |
|
116 CleanupStack::PushL( self ); |
|
117 self->ConstructL( aUid ); |
|
118 |
|
119 //CENREPDATABASELOG( "CCenRepDatabaseUtil::NewLC - OUT" ); |
|
120 |
|
121 return self; |
|
122 } |
|
123 |
|
124 // --------------------------------------------------------------------------- |
|
125 // Destructor |
|
126 // --------------------------------------------------------------------------- |
|
127 // |
|
128 EXPORT_C CCenRepDatabaseUtil::~CCenRepDatabaseUtil() |
|
129 { |
|
130 //CENREPDATABASELOG( "CCenRepDatabaseUtil::~CCenRepDatabaseUtil - IN" ); |
|
131 |
|
132 iSemaphore.Close(); |
|
133 delete iRepository; |
|
134 |
|
135 //CENREPDATABASELOG( "CCenRepDatabaseUtil::~CCenRepDatabaseUtil - OUT" ); |
|
136 } |
|
137 |
|
138 // --------------------------------------------------------------------------- |
|
139 // Begins transaction in repository. |
|
140 // --------------------------------------------------------------------------- |
|
141 // |
|
142 EXPORT_C void CCenRepDatabaseUtil::BeginTransactionL() |
|
143 { |
|
144 //CENREPDATABASELOG( "CCenRepDatabaseUtil::BeginTransactionL - IN" ); |
|
145 |
|
146 /* iSemaphore.Wait(); |
|
147 TCleanupItem cleanup( CCenRepDatabaseUtil::ReleaseSemaphore, this ); |
|
148 CleanupStack::PushL( cleanup ); |
|
149 |
|
150 User::LeaveIfError( |
|
151 iRepository->StartTransaction( CRepository::EReadWriteTransaction ) ); |
|
152 |
|
153 iRepository->CleanupRollbackTransactionPushL(); // if leave happens, |
|
154 // only roll back, no delete |
|
155 */ |
|
156 //CENREPDATABASELOG( "CCenRepDatabaseUtil::BeginTransactionL - OUT" ); |
|
157 } |
|
158 |
|
159 // --------------------------------------------------------------------------- |
|
160 // Commits changes in repository. |
|
161 // --------------------------------------------------------------------------- |
|
162 // |
|
163 EXPORT_C TInt CCenRepDatabaseUtil::CommitTransaction() |
|
164 { |
|
165 //CENREPDATABASELOG( "CCenRepDatabaseUtil::CommitTransaction - IN" ); |
|
166 |
|
167 /* TUint32 temp; |
|
168 |
|
169 TInt retval = iRepository->CommitTransaction( temp ); |
|
170 |
|
171 iSemaphore.Signal(); |
|
172 |
|
173 CleanupStack::Pop( 2 ); // semaphore and repository*/ |
|
174 |
|
175 //CENREPDATABASELOG( "CCenRepDatabaseUtil::CommitTransaction - OUT" ); |
|
176 |
|
177 //return retval; |
|
178 return 0; |
|
179 } |
|
180 |
|
181 // --------------------------------------------------------------------------- |
|
182 // Rollback changes in repository. |
|
183 // --------------------------------------------------------------------------- |
|
184 // |
|
185 EXPORT_C void CCenRepDatabaseUtil::RollbackTransaction() |
|
186 { |
|
187 //CENREPDATABASELOG( "CCenRepDatabaseUtil::RollbackTransaction - IN" ); |
|
188 |
|
189 /* CleanupStack::Pop( 2 ); // rollback transaction item and semaphore |
|
190 |
|
191 iRepository->RollbackTransaction(); |
|
192 iSemaphore.Signal();*/ |
|
193 |
|
194 //CENREPDATABASELOG( "CCenRepDatabaseUtil::RollbackTransaction - OUT" ); |
|
195 } |
|
196 |
|
197 // --------------------------------------------------------------------------- |
|
198 // Add new entry to cenrep. |
|
199 // --------------------------------------------------------------------------- |
|
200 // |
|
201 EXPORT_C void CCenRepDatabaseUtil::AddEntryL( |
|
202 TInt& aEntryId, |
|
203 const RIpAppPropArray& aArray ) |
|
204 { |
|
205 //CENREPDATABASELOG( "CCenRepDatabaseUtil::AddEntryL - IN" ); |
|
206 |
|
207 CreateEntryIdL( aEntryId ); |
|
208 TUint32 idKey = GetNewIdKeyL(); |
|
209 |
|
210 // Add entry Id |
|
211 SetOrCreateKeyL( idKey, aEntryId ); |
|
212 |
|
213 UpdatePropertiesL( idKey, aArray ); |
|
214 |
|
215 //CENREPDATABASELOG( "CCenRepDatabaseUtil::AddEntryL - OUT" ); |
|
216 } |
|
217 |
|
218 // --------------------------------------------------------------------------- |
|
219 // Find entry by ID. |
|
220 // --------------------------------------------------------------------------- |
|
221 // |
|
222 EXPORT_C TInt CCenRepDatabaseUtil::FindEntryL( |
|
223 TInt aEntryId, |
|
224 RIpAppPropArray& aArray ) |
|
225 { |
|
226 //CENREPDATABASELOG( "CCenRepDatabaseUtil::FindEntryL - IN" ); |
|
227 |
|
228 RKeyArray keys; |
|
229 CleanupClosePushL( keys ); |
|
230 |
|
231 // Find all rows where ID is aServiceId (entry row + each property row) |
|
232 TInt err = iRepository->FindEqL( iStartKey, iColMask, aEntryId, keys ); |
|
233 |
|
234 TInt count = keys.Count(); |
|
235 |
|
236 if ( KErrNone == err && count > 0 ) |
|
237 { |
|
238 TUint32 idKey = keys[ 0 ]; |
|
239 |
|
240 for ( TInt i = 1; i <= iColCount; i++ ) |
|
241 { |
|
242 RBuf value; |
|
243 value.CreateL( KCenRepMaxDesLength ); |
|
244 value.CleanupClosePushL(); |
|
245 TUint32 propertyKey = idKey + i * iColIncrement; |
|
246 TInt errProperty = iRepository->Get( propertyKey, value ); |
|
247 if( errProperty == KErrNone && value.Compare( KNullDesC ) != 0 ) |
|
248 { |
|
249 CCenRepDatabaseProperty* property = CCenRepDatabaseProperty::NewLC(); |
|
250 property->SetName( propertyKey - idKey + iStartKey ); |
|
251 User::LeaveIfError( property->SetValue( value ) ); |
|
252 User::LeaveIfError( aArray.Append( property ) ); |
|
253 CleanupStack::Pop( property ); |
|
254 } |
|
255 |
|
256 CleanupStack::PopAndDestroy( &value ); |
|
257 } |
|
258 } |
|
259 |
|
260 CleanupStack::PopAndDestroy( &keys ); |
|
261 |
|
262 //CENREPDATABASELOG( "CCenRepDatabaseUtil::FindEntryL - OUT" ); |
|
263 |
|
264 return err; |
|
265 |
|
266 } |
|
267 |
|
268 // --------------------------------------------------------------------------- |
|
269 // Update entry in cenrep. |
|
270 // --------------------------------------------------------------------------- |
|
271 // |
|
272 EXPORT_C TInt CCenRepDatabaseUtil::UpdateEntryL( |
|
273 TInt aEntryId, |
|
274 const RIpAppPropArray& aArray ) |
|
275 { |
|
276 //CENREPDATABASELOG( "CCenRepDatabaseUtil::UpdateEntryL - IN" ); |
|
277 |
|
278 RKeyArray keys; |
|
279 CleanupClosePushL( keys ); |
|
280 |
|
281 // Find rows where ID is located |
|
282 TInt err = iRepository->FindEqL( iStartKey, iColMask, aEntryId, keys ); |
|
283 |
|
284 TInt count = keys.Count(); |
|
285 |
|
286 if ( KErrNone == err && count > 0 ) |
|
287 { |
|
288 TUint32 idKey = keys[ 0 ]; |
|
289 UpdatePropertiesL( idKey, aArray ); |
|
290 } |
|
291 |
|
292 CleanupStack::PopAndDestroy( &keys ); |
|
293 |
|
294 //CENREPDATABASELOG( "CCenRepDatabaseUtil::UpdateEntryL - OUT" ); |
|
295 |
|
296 return err; |
|
297 } |
|
298 |
|
299 // --------------------------------------------------------------------------- |
|
300 // Delete entry from cenrep. |
|
301 // --------------------------------------------------------------------------- |
|
302 // |
|
303 EXPORT_C TInt CCenRepDatabaseUtil::DeleteEntryL( TInt aEntryId ) |
|
304 { |
|
305 //CENREPDATABASELOG( "CCenRepDatabaseUtil::DeleteEntryL - IN" ); |
|
306 |
|
307 RKeyArray keys; |
|
308 CleanupClosePushL( keys ); |
|
309 |
|
310 // Find all rows where ID is aServiceId (entry row + each property row) |
|
311 TInt err = iRepository->FindEqL( iStartKey, iColMask, aEntryId, keys ); |
|
312 |
|
313 TInt count = keys.Count(); |
|
314 |
|
315 if ( KErrNone == err && count > 0 ) |
|
316 { |
|
317 TUint32 idKey = keys[ 0 ]; |
|
318 SetOrCreateKeyL( idKey, KNoEntryId ); |
|
319 |
|
320 for ( TInt i = 1; i <= iColCount; i++ ) |
|
321 { |
|
322 TUint32 propertyKey = idKey + i * iColIncrement; |
|
323 SetOrCreateKeyL( propertyKey, KNullDesC ); |
|
324 } |
|
325 } |
|
326 |
|
327 CleanupStack::PopAndDestroy( &keys ); |
|
328 |
|
329 //CENREPDATABASELOG( "CCenRepDatabaseUtil::DeleteEntryL - OUT" ); |
|
330 |
|
331 return err; |
|
332 } |
|
333 |
|
334 // --------------------------------------------------------------------------- |
|
335 // Find property and its value by ID. |
|
336 // --------------------------------------------------------------------------- |
|
337 // |
|
338 EXPORT_C TInt CCenRepDatabaseUtil::FindPropertyL( TInt aEntryId, |
|
339 TUint32 aPropertyName, |
|
340 CCenRepDatabaseProperty& aProperty ) |
|
341 { |
|
342 //CENREPDATABASELOG( "CCenRepDatabaseUtil::FindPropertyL - IN" ); |
|
343 |
|
344 RKeyArray keys; |
|
345 CleanupClosePushL( keys ); |
|
346 TBool found = EFalse; |
|
347 |
|
348 // Find all rows where ID is aServiceId (entry row + each property row) |
|
349 TInt err = iRepository->FindEqL( iStartKey, iColMask, aEntryId, keys ); |
|
350 |
|
351 TInt count = keys.Count(); |
|
352 |
|
353 if ( KErrNone == err && count > 0 ) |
|
354 { |
|
355 TUint32 idKey = keys[ 0 ]; |
|
356 |
|
357 for ( TInt i = 1; i <= iColCount; i++ ) |
|
358 { |
|
359 TUint32 propertyKey = idKey + i * iColIncrement; |
|
360 TUint32 propertyName = propertyKey - idKey + iStartKey; |
|
361 if( propertyName == aPropertyName ) |
|
362 { |
|
363 RBuf value; |
|
364 value.CreateL( KCenRepMaxDesLength ); |
|
365 value.CleanupClosePushL(); |
|
366 |
|
367 err = iRepository->Get( propertyKey, value ); |
|
368 if( err == KErrNone ) |
|
369 { |
|
370 if( value.Compare( KNullDesC ) != 0 ) |
|
371 { |
|
372 aProperty.SetName( propertyName ); |
|
373 User::LeaveIfError( aProperty.SetValue( value ) ); |
|
374 found = ETrue; |
|
375 } |
|
376 } |
|
377 |
|
378 CleanupStack::PopAndDestroy( &value ); |
|
379 break; |
|
380 } |
|
381 } |
|
382 } |
|
383 |
|
384 if( found ) |
|
385 { |
|
386 err = KErrNone; |
|
387 } |
|
388 else |
|
389 { |
|
390 if( err == KErrNone ) |
|
391 { |
|
392 err = KErrNotFound; |
|
393 } |
|
394 } |
|
395 CleanupStack::PopAndDestroy( &keys ); |
|
396 |
|
397 //CENREPDATABASELOG( "CCenRepDatabaseUtil::FindPropertyL - OUT" ); |
|
398 |
|
399 return err; |
|
400 } |
|
401 |
|
402 // --------------------------------------------------------------------------- |
|
403 // Adds or updates properties to/in cenrep. |
|
404 // --------------------------------------------------------------------------- |
|
405 // |
|
406 EXPORT_C TInt CCenRepDatabaseUtil::AddOrUpdatePropertiesL( |
|
407 TInt aEntryId, |
|
408 const RIpAppPropArray& aArray ) |
|
409 { |
|
410 //CENREPDATABASELOG( "CCenRepDatabaseUtil::AddOrUpdatePropertiesL - IN" ); |
|
411 |
|
412 RKeyArray keys; |
|
413 CleanupClosePushL( keys ); |
|
414 |
|
415 // Find row maching to given ID |
|
416 TInt err = iRepository->FindEqL( iStartKey, iColMask, aEntryId, keys ); |
|
417 |
|
418 if ( KErrNone == err && keys.Count() > 0 ) |
|
419 { |
|
420 TUint32 idKey = keys[ 0 ]; |
|
421 |
|
422 TInt count = aArray.Count(); |
|
423 |
|
424 // Add properties one by one |
|
425 for( TInt i = 0; i < count; i++ ) |
|
426 { |
|
427 CCenRepDatabaseProperty* property = aArray[i]; |
|
428 TUint32 propertyName = property->GetName(); |
|
429 TUint32 key = idKey - iStartKey + propertyName; |
|
430 SetOrCreateKeyL( key, property->GetDesValue() ); |
|
431 } |
|
432 } |
|
433 |
|
434 CleanupStack::PopAndDestroy( &keys ); |
|
435 |
|
436 //CENREPDATABASELOG( "CCenRepDatabaseUtil::AddOrUpdatePropertiesL - OUT" ); |
|
437 |
|
438 return err; |
|
439 } |
|
440 |
|
441 // --------------------------------------------------------------------------- |
|
442 // Return count of entries in cenrep. |
|
443 // --------------------------------------------------------------------------- |
|
444 // |
|
445 EXPORT_C TInt CCenRepDatabaseUtil::EntryCountL( TInt& aCount ) |
|
446 { |
|
447 //CENREPDATABASELOG( "CCenRepDatabaseUtil::EntryCountL - IN" ); |
|
448 |
|
449 TInt err( KErrNone ); |
|
450 |
|
451 /* RKeyArray keys; |
|
452 CleanupClosePushL( keys ); |
|
453 |
|
454 // Find all rows where ID not equals to KNoEntryId |
|
455 TInt err = iRepository->FindNeqL( iStartKey, iColMask, KNoEntryId, keys ); |
|
456 |
|
457 TInt count = keys.Count(); |
|
458 |
|
459 if ( KErrNone == err && count > 0 ) |
|
460 { |
|
461 aCount = keys.Count(); |
|
462 } |
|
463 else |
|
464 { |
|
465 aCount = 0; |
|
466 } |
|
467 |
|
468 CleanupStack::PopAndDestroy( &keys );*/ |
|
469 |
|
470 if ( 2 > count ) |
|
471 { |
|
472 if ( !count ) |
|
473 { |
|
474 err = KErrNotFound; |
|
475 } |
|
476 aCount = count; |
|
477 count++; |
|
478 } |
|
479 else |
|
480 { |
|
481 aCount = count; |
|
482 count = 0; |
|
483 } |
|
484 |
|
485 //CENREPDATABASELOG( "CCenRepDatabaseUtil::EntryCountL - OUT" ); |
|
486 |
|
487 return err; |
|
488 } |
|
489 |
|
490 // --------------------------------------------------------------------------- |
|
491 // Find all ids of entries in cenrep. |
|
492 // --------------------------------------------------------------------------- |
|
493 // |
|
494 EXPORT_C TInt CCenRepDatabaseUtil::FindEntryIdsL( RArray<TInt>& aEntryIds ) |
|
495 { |
|
496 //CENREPDATABASELOG( "CCenRepDatabaseUtil::FindEntryIdsL - IN" ); |
|
497 |
|
498 aEntryIds.Reset(); |
|
499 |
|
500 RKeyArray keys; |
|
501 CleanupClosePushL( keys ); |
|
502 |
|
503 // Find all entry rows from ID column. Deleted rows are not returned. |
|
504 TInt err = iRepository->FindNeqL( iStartKey, iColMask, KNoEntryId, keys ); |
|
505 |
|
506 TInt count = keys.Count(); |
|
507 |
|
508 if ( KErrNone == err && count > 0 ) |
|
509 { |
|
510 for( TInt i = 0; i < count; i++ ) |
|
511 { |
|
512 TUint32 key = keys[i]; |
|
513 TInt entryId = 0; |
|
514 User::LeaveIfError( iRepository->Get( key, entryId ) ); |
|
515 User::LeaveIfError( aEntryIds.Append( entryId ) ); |
|
516 } |
|
517 } |
|
518 |
|
519 CleanupStack::PopAndDestroy( &keys ); |
|
520 |
|
521 //CENREPDATABASELOG( "CCenRepDatabaseUtil::FindEntryIdsL - OUT" ); |
|
522 |
|
523 return err; |
|
524 } |
|
525 |
|
526 // --------------------------------------------------------------------------- |
|
527 // Delete given properties from entry. |
|
528 // --------------------------------------------------------------------------- |
|
529 // |
|
530 EXPORT_C TInt CCenRepDatabaseUtil::DeletePropertiesL( TInt aEntryId, |
|
531 const RArray<TUint32>& aNameArray ) |
|
532 { |
|
533 //CENREPDATABASELOG( "CCenRepDatabaseUtil::DeletePropertiesL - IN" ); |
|
534 |
|
535 RKeyArray keys; |
|
536 CleanupClosePushL( keys ); |
|
537 |
|
538 // Find row that matches to aEntryID |
|
539 TInt err = iRepository->FindEqL( iStartKey, iColMask, aEntryId, keys ); |
|
540 |
|
541 if ( KErrNone == err && keys.Count() > 0 ) |
|
542 { |
|
543 TUint32 idKey = keys[ 0 ]; |
|
544 |
|
545 TInt count = aNameArray.Count(); |
|
546 |
|
547 // delete properties one by one |
|
548 for( TInt i = 0; i < count; i++ ) |
|
549 { |
|
550 TUint32 name = aNameArray[i]; |
|
551 TUint32 key = idKey - iStartKey + name; |
|
552 SetOrCreateKeyL( key, KNullDesC ); |
|
553 } |
|
554 } |
|
555 |
|
556 CleanupStack::PopAndDestroy( &keys ); |
|
557 |
|
558 //CENREPDATABASELOG( "CCenRepDatabaseUtil::DeletePropertiesL - OUT" ); |
|
559 |
|
560 return err; |
|
561 } |
|
562 |
|
563 // --------------------------------------------------------------------------- |
|
564 // Find entry ids of given properties. |
|
565 // --------------------------------------------------------------------------- |
|
566 // |
|
567 EXPORT_C TInt CCenRepDatabaseUtil::FindEntryIdsFromPropertiesL( |
|
568 const RIpAppPropArray& aPropertyArray, |
|
569 RArray<TInt>& aEntryIds ) |
|
570 { |
|
571 //CENREPDATABASELOG( "CCenRepDatabaseUtil::FindEntryIdsFromPropertiesL - IN" ); |
|
572 |
|
573 TInt err = FindEntryIdsL( aEntryIds ); |
|
574 if( err == KErrNone ) |
|
575 { |
|
576 TInt count = aEntryIds.Count(); |
|
577 TInt removed = 0; |
|
578 |
|
579 for( TInt i = 0; i < count; i++ ) |
|
580 { |
|
581 TBool ok = EFalse; |
|
582 TUint32 entryId = aEntryIds[ i - removed ]; |
|
583 CheckEntryHasPropertiesL( entryId, aPropertyArray, ok ); |
|
584 |
|
585 if( !ok ) |
|
586 { |
|
587 aEntryIds.Remove( i - removed ); |
|
588 removed++; |
|
589 } |
|
590 } |
|
591 } |
|
592 |
|
593 //CENREPDATABASELOG( "CCenRepDatabaseUtil::FindEntryIdsFromPropertiesL - OUT" ); |
|
594 |
|
595 return err; |
|
596 } |
|
597 |
|
598 // --------------------------------------------------------------------------- |
|
599 // Release semaphore, this function is used in custom cleanup |
|
600 // --------------------------------------------------------------------------- |
|
601 // |
|
602 void CCenRepDatabaseUtil::ReleaseSemaphore( TAny* aPtr ) |
|
603 { |
|
604 //CENREPDATABASELOG( "CCenRepDatabaseUtil::ReleaseSemaphore - IN" ); |
|
605 |
|
606 ASSERT ( aPtr ); |
|
607 CCenRepDatabaseUtil* utils = static_cast<CCenRepDatabaseUtil*>( aPtr ); |
|
608 utils->DoReleaseSemaphore(); |
|
609 |
|
610 //CENREPDATABASELOG( "CCenRepDatabaseUtil::ReleaseSemaphore - OUT" ); |
|
611 } |
|
612 |
|
613 // --------------------------------------------------------------------------- |
|
614 // Release semaphore |
|
615 // --------------------------------------------------------------------------- |
|
616 // |
|
617 void CCenRepDatabaseUtil::DoReleaseSemaphore() |
|
618 { |
|
619 //CENREPDATABASELOG( "CCenRepDatabaseUtil::DoReleaseSemaphore - IN" ); |
|
620 |
|
621 iSemaphore.Signal(); |
|
622 |
|
623 //CENREPDATABASELOG( "CCenRepDatabaseUtil::DoReleaseSemaphore - OUT" ); |
|
624 } |
|
625 |
|
626 // --------------------------------------------------------------------------- |
|
627 // Set value of key or create a new one. |
|
628 // --------------------------------------------------------------------------- |
|
629 // |
|
630 template<class T> void CCenRepDatabaseUtil::SetOrCreateKeyL( TUint32 aKey, |
|
631 const T& aValue ) |
|
632 { |
|
633 TInt err = iRepository->Set( aKey, aValue ); |
|
634 |
|
635 if ( KErrNotFound == err ) |
|
636 { |
|
637 err = iRepository->Create( aKey, aValue ); |
|
638 } |
|
639 |
|
640 User::LeaveIfError( err ); |
|
641 } |
|
642 |
|
643 // --------------------------------------------------------------------------- |
|
644 // Create new ID. |
|
645 // --------------------------------------------------------------------------- |
|
646 // |
|
647 void CCenRepDatabaseUtil::CreateEntryIdL( TInt& aNewId ) |
|
648 { |
|
649 TInt newId( KNoEntryId ); |
|
650 |
|
651 TInt err = iRepository->Get( iIdCounterKey, newId ); |
|
652 |
|
653 if ( KErrNone == err ) |
|
654 { |
|
655 if ( 0 < newId ) |
|
656 { |
|
657 // Store next available ID |
|
658 err = iRepository->Set( iIdCounterKey, newId + 1 ); |
|
659 } |
|
660 else |
|
661 { |
|
662 // Range of ID exceeded |
|
663 err = KErrOverflow; |
|
664 } |
|
665 } |
|
666 else if ( KErrNotFound == err ) |
|
667 { |
|
668 // Create new key (should always be found) |
|
669 SetOrCreateKeyL( iIdCounterKey, KNoEntryId + 1 ); |
|
670 } |
|
671 |
|
672 aNewId = newId; |
|
673 |
|
674 User::LeaveIfError( err ); |
|
675 } |
|
676 |
|
677 // --------------------------------------------------------------------------- |
|
678 // Get next available key for ID. |
|
679 // --------------------------------------------------------------------------- |
|
680 // |
|
681 TUint32 CCenRepDatabaseUtil::GetNewIdKeyL() |
|
682 { |
|
683 RKeyArray keys; |
|
684 CleanupClosePushL( keys ); |
|
685 TUint32 idKey( 0 ); |
|
686 |
|
687 // Reuse keys which value is KNoEntryId |
|
688 TInt err = iRepository->FindEqL( iStartKey, iColMask, KNoEntryId, keys ); |
|
689 |
|
690 if ( KErrNone == err && 0 < keys.Count() ) |
|
691 { |
|
692 // return id column key from first deleted row |
|
693 idKey = keys[0]; |
|
694 } |
|
695 else if ( KErrNotFound == err ) |
|
696 { |
|
697 keys.Close(); |
|
698 TInt err1 = iRepository->FindL( iStartKey, iColMask, keys ); |
|
699 |
|
700 if( KErrNone == err1 ) |
|
701 { |
|
702 // return new |
|
703 TInt count( keys.Count() ); |
|
704 idKey = keys[count-1] + 1; |
|
705 } |
|
706 else if ( KErrNotFound == err1 ) |
|
707 { |
|
708 idKey = iStartKey; |
|
709 } |
|
710 else |
|
711 { |
|
712 User::Leave( err1 ); |
|
713 } |
|
714 } |
|
715 else |
|
716 { |
|
717 User::Leave( err ); |
|
718 } |
|
719 |
|
720 CleanupStack::PopAndDestroy( &keys ); |
|
721 |
|
722 return idKey; |
|
723 } |
|
724 |
|
725 // --------------------------------------------------------------------------- |
|
726 // Update given properties to cenrep. |
|
727 // --------------------------------------------------------------------------- |
|
728 // |
|
729 void CCenRepDatabaseUtil::UpdatePropertiesL( |
|
730 TUint32 aIdKey, |
|
731 const RIpAppPropArray& aArray ) |
|
732 { |
|
733 TInt count = aArray.Count(); |
|
734 |
|
735 // Create all keys for this entry |
|
736 RKeyArray propertyKeys; |
|
737 CleanupClosePushL( propertyKeys ); |
|
738 for( TInt k = 1; k <= iColCount; k++ ) |
|
739 { |
|
740 User::LeaveIfError( propertyKeys.Append( aIdKey + k * iColIncrement ) ); |
|
741 } |
|
742 |
|
743 // Add properties one by one |
|
744 for( TInt i = 0; i < count; i++ ) |
|
745 { |
|
746 CCenRepDatabaseProperty* property = aArray[i]; |
|
747 TUint32 propertyName = property->GetName(); |
|
748 TUint32 key = aIdKey - iStartKey + propertyName; |
|
749 SetOrCreateKeyL( key, property->GetDesValue() ); |
|
750 |
|
751 // Get only non-set keys |
|
752 TInt index = propertyKeys.Find( key ); |
|
753 if( index != KErrNotFound ) |
|
754 { |
|
755 propertyKeys.Remove( index ); |
|
756 } |
|
757 } |
|
758 |
|
759 // set non-set properties one by one |
|
760 count = propertyKeys.Count(); |
|
761 for( TInt j = 0; j < count; j++ ) |
|
762 { |
|
763 TUint32 key = propertyKeys[j]; |
|
764 SetOrCreateKeyL( key, KNullDesC() ); |
|
765 } |
|
766 |
|
767 CleanupStack::PopAndDestroy( &propertyKeys ); |
|
768 } |
|
769 |
|
770 // --------------------------------------------------------------------------- |
|
771 // Check if entry has these properties. |
|
772 // --------------------------------------------------------------------------- |
|
773 // |
|
774 void CCenRepDatabaseUtil::CheckEntryHasPropertiesL( |
|
775 TInt aEntryId, |
|
776 const RIpAppPropArray& aPropertyArray, |
|
777 TBool& aOk ) |
|
778 { |
|
779 RIpAppPropArray allArray; |
|
780 TCleanupItem cleanup( CCenRepDatabaseUtil::CleanupPointerArray, &allArray ); |
|
781 CleanupStack::PushL( cleanup ); |
|
782 |
|
783 TInt err = FindEntryL( aEntryId, allArray ); |
|
784 if( err == KErrNone ) |
|
785 { |
|
786 aOk = ETrue; |
|
787 TInt count = aPropertyArray.Count(); |
|
788 TInt allCount = allArray.Count(); |
|
789 |
|
790 for( TInt i = 0; i < count; i++ ) |
|
791 { |
|
792 CCenRepDatabaseProperty* property = aPropertyArray[i]; |
|
793 TUint32 name = property->GetName(); |
|
794 const TDesC& value = property->GetDesValue(); |
|
795 TBool found = EFalse; |
|
796 for( TInt j = 0; j < allCount; j++ ) |
|
797 { |
|
798 CCenRepDatabaseProperty* allProperty = allArray[j]; |
|
799 if( name == allProperty->GetName() && |
|
800 value.Compare( allProperty->GetDesValue() ) == 0 ) |
|
801 { |
|
802 found = ETrue; |
|
803 break; |
|
804 } |
|
805 } |
|
806 if( !found ) |
|
807 { |
|
808 aOk = EFalse; |
|
809 break; |
|
810 } |
|
811 } |
|
812 } |
|
813 else |
|
814 { |
|
815 aOk = EFalse; |
|
816 } |
|
817 |
|
818 CleanupStack::PopAndDestroy( &allArray ); |
|
819 } |
|
820 |
|
821 // --------------------------------------------------------------------------- |
|
822 // Cleanup array of properties. |
|
823 // --------------------------------------------------------------------------- |
|
824 // |
|
825 void CCenRepDatabaseUtil::CleanupPointerArray( TAny* aPointer ) |
|
826 { |
|
827 RIpAppPropArray* array = static_cast<RIpAppPropArray*>( aPointer ); |
|
828 array->ResetAndDestroy(); |
|
829 } |
|
830 |
|
831 |
|