convergedcallengine/cenrepdatabase/src/cenrepdatabaseutil.cpp
changeset 0 ff3b6d0fd310
child 19 7d48bed6ce0c
equal deleted inserted replaced
-1:000000000000 0:ff3b6d0fd310
       
     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 
       
    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     }
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 // Rollback changes in repository.
       
   182 // ---------------------------------------------------------------------------
       
   183 //
       
   184 EXPORT_C void CCenRepDatabaseUtil::RollbackTransaction()
       
   185     {
       
   186     CENREPDATABASELOG( "CCenRepDatabaseUtil::RollbackTransaction - IN" );
       
   187     
       
   188 	CleanupStack::Pop( 2 ); // rollback transaction item and semaphore
       
   189 
       
   190 	iRepository->RollbackTransaction();
       
   191 	iSemaphore.Signal();
       
   192 	
       
   193 	CENREPDATABASELOG( "CCenRepDatabaseUtil::RollbackTransaction - OUT" );
       
   194     }
       
   195     
       
   196 // ---------------------------------------------------------------------------
       
   197 // Add new entry to cenrep.
       
   198 // ---------------------------------------------------------------------------
       
   199 //
       
   200 EXPORT_C void CCenRepDatabaseUtil::AddEntryL( 
       
   201     TInt& aEntryId,
       
   202     const RIpAppPropArray& aArray )
       
   203     {
       
   204     CENREPDATABASELOG( "CCenRepDatabaseUtil::AddEntryL - IN" );
       
   205     
       
   206     CreateEntryIdL( aEntryId );
       
   207     TUint32 idKey = GetNewIdKeyL();
       
   208     
       
   209     // Add entry Id
       
   210     SetOrCreateKeyL( idKey, aEntryId );
       
   211     
       
   212     UpdatePropertiesL( idKey, aArray );
       
   213     
       
   214     CENREPDATABASELOG( "CCenRepDatabaseUtil::AddEntryL - OUT" );
       
   215     }
       
   216 
       
   217 // ---------------------------------------------------------------------------
       
   218 // Find entry by ID.
       
   219 // ---------------------------------------------------------------------------
       
   220 //
       
   221 EXPORT_C TInt CCenRepDatabaseUtil::FindEntryL( 
       
   222     TInt aEntryId,
       
   223     RIpAppPropArray& aArray )
       
   224     {
       
   225     CENREPDATABASELOG( "CCenRepDatabaseUtil::FindEntryL - IN" );
       
   226     
       
   227     RKeyArray keys;
       
   228     CleanupClosePushL( keys );
       
   229 
       
   230     // Find all rows where ID is aServiceId (entry row + each property row)
       
   231     TInt err = iRepository->FindEqL( iStartKey, iColMask, aEntryId, keys );
       
   232 
       
   233     TInt count = keys.Count();
       
   234 
       
   235     if ( KErrNone == err && count > 0 )
       
   236         {
       
   237         TUint32 idKey = keys[ 0 ];
       
   238 
       
   239         for ( TInt i = 1; i <= iColCount; i++ )
       
   240             {
       
   241             RBuf value;
       
   242             value.CreateL( KCenRepMaxDesLength );
       
   243             value.CleanupClosePushL();
       
   244             TUint32 propertyKey = idKey + i * iColIncrement;
       
   245             TInt errProperty = iRepository->Get( propertyKey, value );
       
   246             if( errProperty == KErrNone && value.Compare( KNullDesC ) != 0 )
       
   247                 {
       
   248                 CCenRepDatabaseProperty* property = CCenRepDatabaseProperty::NewLC();
       
   249                 property->SetName( propertyKey - idKey + iStartKey );
       
   250                 User::LeaveIfError( property->SetValue( value ) );
       
   251                 User::LeaveIfError( aArray.Append( property ) );
       
   252                 CleanupStack::Pop( property );
       
   253                 }
       
   254                 
       
   255             CleanupStack::PopAndDestroy( &value );
       
   256             }
       
   257         }
       
   258 
       
   259     CleanupStack::PopAndDestroy( &keys );
       
   260     
       
   261     CENREPDATABASELOG( "CCenRepDatabaseUtil::FindEntryL - OUT" );
       
   262     
       
   263     return err;
       
   264 
       
   265     }
       
   266 
       
   267 // ---------------------------------------------------------------------------
       
   268 // Update entry in cenrep.
       
   269 // ---------------------------------------------------------------------------
       
   270 //
       
   271 EXPORT_C TInt CCenRepDatabaseUtil::UpdateEntryL( 
       
   272     TInt aEntryId, 
       
   273     const RIpAppPropArray& aArray )
       
   274     {
       
   275     CENREPDATABASELOG( "CCenRepDatabaseUtil::UpdateEntryL - IN" );
       
   276     
       
   277     RKeyArray keys;
       
   278     CleanupClosePushL( keys );
       
   279 
       
   280     // Find rows where ID is located
       
   281     TInt err = iRepository->FindEqL( iStartKey, iColMask, aEntryId, keys );
       
   282 
       
   283     TInt count = keys.Count();
       
   284 
       
   285     if ( KErrNone == err && count > 0 )
       
   286         {
       
   287         TUint32 idKey = keys[ 0 ];
       
   288         UpdatePropertiesL( idKey, aArray );
       
   289         }
       
   290 
       
   291     CleanupStack::PopAndDestroy( &keys );
       
   292     
       
   293     CENREPDATABASELOG( "CCenRepDatabaseUtil::UpdateEntryL - OUT" );
       
   294     
       
   295     return err;
       
   296     }
       
   297 
       
   298 // ---------------------------------------------------------------------------
       
   299 // Delete entry from cenrep.
       
   300 // ---------------------------------------------------------------------------
       
   301 //
       
   302 EXPORT_C TInt CCenRepDatabaseUtil::DeleteEntryL( TInt aEntryId )
       
   303     {
       
   304     CENREPDATABASELOG( "CCenRepDatabaseUtil::DeleteEntryL - IN" );
       
   305     
       
   306     RKeyArray keys;
       
   307     CleanupClosePushL( keys );
       
   308 
       
   309     // Find all rows where ID is aServiceId (entry row + each property row)
       
   310     TInt err = iRepository->FindEqL( iStartKey, iColMask, aEntryId, keys );
       
   311 
       
   312     TInt count = keys.Count();
       
   313 
       
   314     if ( KErrNone == err && count > 0 )
       
   315         {
       
   316         TUint32 idKey = keys[ 0 ];
       
   317         SetOrCreateKeyL( idKey, KNoEntryId );
       
   318 
       
   319         for ( TInt i = 1; i <= iColCount; i++ )
       
   320             {
       
   321             TUint32 propertyKey = idKey + i * iColIncrement;
       
   322             SetOrCreateKeyL( propertyKey, KNullDesC );
       
   323             }
       
   324         }
       
   325 
       
   326     CleanupStack::PopAndDestroy( &keys );
       
   327     
       
   328     CENREPDATABASELOG( "CCenRepDatabaseUtil::DeleteEntryL - OUT" );
       
   329     
       
   330     return err;
       
   331     }
       
   332 
       
   333 // ---------------------------------------------------------------------------
       
   334 // Find property and its value by ID.
       
   335 // ---------------------------------------------------------------------------
       
   336 //
       
   337 EXPORT_C TInt CCenRepDatabaseUtil::FindPropertyL( TInt aEntryId, 
       
   338                     TUint32 aPropertyName,
       
   339                     CCenRepDatabaseProperty& aProperty )
       
   340     {
       
   341     CENREPDATABASELOG( "CCenRepDatabaseUtil::FindPropertyL - IN" );
       
   342     
       
   343     RKeyArray keys;
       
   344     CleanupClosePushL( keys );
       
   345     TBool found = EFalse;
       
   346 
       
   347     // Find all rows where ID is aServiceId (entry row + each property row)
       
   348     TInt err = iRepository->FindEqL( iStartKey, iColMask, aEntryId, keys );
       
   349 
       
   350     TInt count = keys.Count();
       
   351 
       
   352     if ( KErrNone == err && count > 0 )
       
   353         {
       
   354         TUint32 idKey = keys[ 0 ];
       
   355 
       
   356         for ( TInt i = 1; i <= iColCount; i++ )
       
   357             {
       
   358             TUint32 propertyKey = idKey + i * iColIncrement;
       
   359             TUint32 propertyName = propertyKey - idKey + iStartKey;
       
   360             if( propertyName == aPropertyName )
       
   361                 {
       
   362                 RBuf value;
       
   363                 value.CreateL( KCenRepMaxDesLength );
       
   364                 value.CleanupClosePushL();
       
   365                 
       
   366                 err = iRepository->Get( propertyKey, value );
       
   367                 if( err == KErrNone )
       
   368                     {
       
   369                     if( value.Compare( KNullDesC ) != 0 )
       
   370                         {
       
   371                         aProperty.SetName( propertyName );
       
   372                         User::LeaveIfError( aProperty.SetValue( value ) );
       
   373                         found = ETrue;
       
   374                         }
       
   375                     }
       
   376                 
       
   377                 CleanupStack::PopAndDestroy( &value );
       
   378                 break;
       
   379                 }
       
   380             }
       
   381         }
       
   382 
       
   383     if( found )
       
   384         {
       
   385         err = KErrNone;
       
   386         }
       
   387     else
       
   388         {
       
   389         if( err == KErrNone )
       
   390             {
       
   391             err = KErrNotFound;
       
   392             }
       
   393         }
       
   394     CleanupStack::PopAndDestroy( &keys );
       
   395     
       
   396     CENREPDATABASELOG( "CCenRepDatabaseUtil::FindPropertyL - OUT" );
       
   397     
       
   398     return err;
       
   399     }
       
   400 
       
   401 // ---------------------------------------------------------------------------
       
   402 // Adds or updates properties to/in cenrep.
       
   403 // ---------------------------------------------------------------------------
       
   404 //
       
   405 EXPORT_C TInt CCenRepDatabaseUtil::AddOrUpdatePropertiesL( 
       
   406     TInt aEntryId, 
       
   407     const RIpAppPropArray& aArray )
       
   408     {
       
   409     CENREPDATABASELOG( "CCenRepDatabaseUtil::AddOrUpdatePropertiesL - IN" );
       
   410     TBool updatedOrCreated = EFalse;
       
   411     RKeyArray keys;
       
   412     CleanupClosePushL( keys );
       
   413 
       
   414     // Find row maching to given ID
       
   415     TInt err = iRepository->FindEqL( iStartKey, iColMask, aEntryId, keys );
       
   416 
       
   417     if ( KErrNone == err && keys.Count() > 0 )
       
   418         {
       
   419         TUint32 idKey = keys[ 0 ];
       
   420 
       
   421         TInt count = aArray.Count();
       
   422 
       
   423         HBufC* data = HBufC::NewLC( KCenRepMaxDesLength );
       
   424         TPtr ptr = data->Des();
       
   425         
       
   426         // Add properties one by one
       
   427         for( TInt i = 0; i < count; i++ )
       
   428             {
       
   429             CCenRepDatabaseProperty* property = aArray[i];
       
   430             TUint32 propertyName = property->GetName();
       
   431             TUint32 key = idKey - iStartKey + propertyName;
       
   432             
       
   433 
       
   434             TInt errProperty = iRepository->Get( key, ptr );
       
   435             if( errProperty || ( ptr != property->GetDesValue() ) )
       
   436                 {
       
   437                 updatedOrCreated = ETrue;
       
   438                 SetOrCreateKeyL( key, property->GetDesValue() );
       
   439                 }
       
   440             
       
   441             }
       
   442         
       
   443         CleanupStack::PopAndDestroy( data );
       
   444         }
       
   445 
       
   446     CleanupStack::PopAndDestroy( &keys );
       
   447     
       
   448     if( !err && !updatedOrCreated )
       
   449         {
       
   450         CENREPDATABASELOG( "CCenRepDatabaseUtil::AddOrUpdatePropertiesL - Not updated or created" );
       
   451         err = KErrCancel;
       
   452         }
       
   453     
       
   454     CENREPDATABASELOG( "CCenRepDatabaseUtil::AddOrUpdatePropertiesL - OUT" );
       
   455     return err;
       
   456     }
       
   457 
       
   458 // ---------------------------------------------------------------------------
       
   459 // Return count of entries in cenrep.
       
   460 // ---------------------------------------------------------------------------
       
   461 //
       
   462 EXPORT_C TInt CCenRepDatabaseUtil::EntryCountL( TInt& aCount )
       
   463     {
       
   464     CENREPDATABASELOG( "CCenRepDatabaseUtil::EntryCountL - IN" );
       
   465     
       
   466     RKeyArray keys;
       
   467     CleanupClosePushL( keys );
       
   468 
       
   469     // Find all rows where ID not equals to KNoEntryId
       
   470     TInt err = iRepository->FindNeqL( iStartKey, iColMask, KNoEntryId, keys );
       
   471 
       
   472     TInt count = keys.Count();
       
   473 
       
   474     if ( KErrNone == err && count > 0 )
       
   475         {
       
   476         aCount = keys.Count();
       
   477         }
       
   478     else
       
   479         {
       
   480         aCount = 0;    
       
   481         }
       
   482 
       
   483     CleanupStack::PopAndDestroy( &keys );
       
   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 
       
   832