PECengine/ListLibrary2/ContactListSrc/CPEngContactListProperty.cpp
branchRCL_3
changeset 17 a941bc465d9f
parent 0 094583676ce7
equal deleted inserted replaced
16:6ca72c0fe49a 17:a941bc465d9f
       
     1 /*
       
     2 * Copyright (c) 2005 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:  Property manager and value container
       
    15 *
       
    16 */
       
    17 
       
    18 //  INCLUDES
       
    19 #include    "CPEngContactListProperty.h"
       
    20 #include    "PEngStorageGlobals.h"
       
    21 #include    "MPEngContactListProperties.h"
       
    22 #include    "MPEngContactItem.h"
       
    23 #include    <e32base.h>
       
    24 #include    <s32strm.h>
       
    25 
       
    26 
       
    27 
       
    28 // ============================ MEMBER FUNCTIONS ===============================
       
    29 
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // PEngPropertyManager::GetProperty()
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 TInt PEngPropertyManager::GetProperty(
       
    36     const RPointerArray<CPEngContactListProperty>& aProperties,
       
    37     TUint aName,
       
    38     TUint aLevel,
       
    39     TInt& aValue )
       
    40     {
       
    41     TInt x( FindProperty( aProperties, aName, aLevel, ETInt ) );
       
    42     if ( x == KErrNotFound )
       
    43         {
       
    44         return KErrNotFound;
       
    45         }
       
    46 
       
    47     aValue = aProperties[ x ]->ValueInt();
       
    48     return KErrNone;
       
    49     }
       
    50 
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // PEngPropertyManager::GetProperty()
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 TInt PEngPropertyManager::GetProperty(
       
    57     const RPointerArray<CPEngContactListProperty>& aProperties,
       
    58     TUint aName,
       
    59     TUint aLevel,
       
    60     TPtrC8& aValue )
       
    61     {
       
    62     TInt x( FindProperty( aProperties, aName, aLevel, EDes8 ) );
       
    63     if ( x == KErrNotFound )
       
    64         {
       
    65         return KErrNotFound;
       
    66         }
       
    67 
       
    68     aValue.Set( aProperties[ x ]->ValueDes8() );
       
    69     return KErrNone;
       
    70     }
       
    71 
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // PEngPropertyManager::GetProperty()
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 TInt PEngPropertyManager::GetProperty(
       
    78     const RPointerArray<CPEngContactListProperty>& aProperties,
       
    79     TUint aName,
       
    80     TUint aLevel,
       
    81     TPtrC16& aValue )
       
    82     {
       
    83     TInt x( FindProperty( aProperties, aName, aLevel, EDes16 ) );
       
    84     if ( x == KErrNotFound )
       
    85         {
       
    86         return KErrNotFound;
       
    87         }
       
    88 
       
    89     aValue.Set( aProperties[ x ]->ValueDes16() );
       
    90     return KErrNone;
       
    91     }
       
    92 
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // PEngPropertyManager::PropertyOrZero()
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 TInt PEngPropertyManager::PropertyOrZero(
       
    99     const RPointerArray<CPEngContactListProperty>& aProperties,
       
   100     TUint aName,
       
   101     TUint aLevel )
       
   102     {
       
   103     TInt x( FindProperty( aProperties, aName, aLevel ) );
       
   104     if ( x == KErrNotFound )
       
   105         {
       
   106         return 0; // zero as default value
       
   107         }
       
   108 
       
   109     return aProperties[ x ]->ValueInt();
       
   110     }
       
   111 
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // PEngPropertyManager::SetPropertyL()
       
   115 // -----------------------------------------------------------------------------
       
   116 //
       
   117 void PEngPropertyManager::SetPropertyL(
       
   118     RPointerArray<CPEngContactListProperty>& aProperties,
       
   119     TUint aName,
       
   120     TUint aLevel,
       
   121     TInt aValue,
       
   122     TInt& aSize )
       
   123     {
       
   124     TInt x( FindProperty( aProperties, aName, aLevel ) );
       
   125     if ( x == KErrNotFound )
       
   126         {
       
   127         // does not exists yet, create it
       
   128         CPEngContactListProperty* prop = CPEngContactListProperty::NewLC( aName,
       
   129                                                                           aLevel );
       
   130         prop->SetValue( aValue );
       
   131         aProperties.AppendL( prop );
       
   132         CleanupStack::Pop( prop );
       
   133         aSize += prop->EntrySize();
       
   134         }
       
   135 
       
   136     else
       
   137         {
       
   138         aProperties[ x ]->SetValue( aValue );
       
   139         }
       
   140     }
       
   141 
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // PEngPropertyManager::SetPropertyL()
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 void PEngPropertyManager::SetPropertyL(
       
   148     RPointerArray<CPEngContactListProperty>& aProperties,
       
   149     TUint aName,
       
   150     TUint aLevel,
       
   151     const TDesC8& aValue,
       
   152     TInt& aSize )
       
   153     {
       
   154     TInt x( FindProperty( aProperties, aName, aLevel ) );
       
   155     if ( x == KErrNotFound )
       
   156         {
       
   157         // does not exists yet, create it
       
   158         CPEngContactListProperty* prop = CPEngContactListProperty::NewLC( aName,
       
   159                                                                           aLevel );
       
   160         prop->SetValueL( aValue );
       
   161         aProperties.AppendL( prop );
       
   162         CleanupStack::Pop( prop );
       
   163         aSize += prop->EntrySize();
       
   164         }
       
   165     else
       
   166         {
       
   167         TInt dif( aProperties[ x ]->EntrySize() );
       
   168         aProperties[ x ]->SetValueL( aValue );
       
   169         dif -= aProperties[ x ]->EntrySize();
       
   170         aSize += dif;
       
   171         }
       
   172     }
       
   173 
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // PEngPropertyManager::SetPropertyL()
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 void PEngPropertyManager::SetPropertyL(
       
   180     RPointerArray<CPEngContactListProperty>& aProperties,
       
   181     TUint aName,
       
   182     TUint aLevel,
       
   183     const TDesC16& aValue,
       
   184     TInt& aSize )
       
   185     {
       
   186     TInt x( FindProperty( aProperties, aName, aLevel ) );
       
   187     if ( x == KErrNotFound )
       
   188         {
       
   189         // does not exists yet, create it
       
   190         CPEngContactListProperty* prop = CPEngContactListProperty::NewLC( aName,
       
   191                                                                           aLevel );
       
   192         prop->SetValueL( aValue );
       
   193         aProperties.AppendL( prop );
       
   194         CleanupStack::Pop( prop );
       
   195         aSize += prop->EntrySize();
       
   196         }
       
   197     else
       
   198         {
       
   199         TInt dif( aProperties[ x ]->EntrySize() );
       
   200         aProperties[ x ]->SetValueL( aValue );
       
   201         dif -= aProperties[ x ]->EntrySize();
       
   202         aSize += dif;
       
   203         }
       
   204     }
       
   205 
       
   206 
       
   207 // -----------------------------------------------------------------------------
       
   208 // PEngPropertyManager::DeletePropertyL()
       
   209 // -----------------------------------------------------------------------------
       
   210 //
       
   211 TBool PEngPropertyManager::DeletePropertyL(
       
   212     RPointerArray<CPEngContactListProperty>& aProperties,
       
   213     TUint aName,
       
   214     TUint aLevel,
       
   215     TInt& aSize )
       
   216     {
       
   217     TInt x( FindProperty( aProperties, aName, aLevel ) );
       
   218     if ( x != KErrNotFound )
       
   219         {
       
   220         aSize -= aProperties[ x ]->EntrySize();
       
   221         delete aProperties[ x ];
       
   222         aProperties.Remove( x );
       
   223         return ETrue;
       
   224         }
       
   225 
       
   226     return EFalse;
       
   227     }
       
   228 
       
   229 // -----------------------------------------------------------------------------
       
   230 // PEngPropertyManager::FindProperty()
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 TInt PEngPropertyManager::FindProperty(
       
   234     const RPointerArray<CPEngContactListProperty>& aProperties,
       
   235     TUint aName,
       
   236     TUint aLevel )
       
   237     {
       
   238     TInt count( aProperties.Count() );
       
   239     for ( TInt x( 0 ) ; x < count  ; ++x )
       
   240         {
       
   241         if ( ( aProperties[ x ]->Name() == aName ) &&
       
   242              ( aProperties[ x ]->Level() == aLevel ) )
       
   243             {
       
   244             return x;
       
   245             }
       
   246         }
       
   247 
       
   248     return KErrNotFound;
       
   249     }
       
   250 
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // PEngPropertyManager::FindProperty()
       
   254 // -----------------------------------------------------------------------------
       
   255 //
       
   256 TInt PEngPropertyManager::FindProperty(
       
   257     const RPointerArray<CPEngContactListProperty>& aProperties,
       
   258     TUint aName,
       
   259     TUint aLevel,
       
   260     TPEngPropertyType aType )
       
   261     {
       
   262     TInt index( FindProperty( aProperties, aName, aLevel ) );
       
   263     if ( index != KErrNotFound )
       
   264         {
       
   265         if ( aProperties[ index ]->ValidateType( aType ) )
       
   266             {
       
   267             return index;
       
   268             }
       
   269 
       
   270         return KErrNotFound;
       
   271         }
       
   272 
       
   273     return KErrNotFound;
       
   274     }
       
   275 
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 // PEngPropertyManager::ExternalizePropertiesL()
       
   279 // -----------------------------------------------------------------------------
       
   280 //
       
   281 void PEngPropertyManager::ExternalizePropertiesL(
       
   282     const RPointerArray<CPEngContactListProperty>& aProperties,
       
   283     RWriteStream& aStream,
       
   284     TInt aStoreType )
       
   285     {
       
   286     TInt count( aProperties.Count() );
       
   287     for ( TInt i( 0 ) ; i < count  ; ++i )
       
   288         {
       
   289         aProperties[ i ]->ExternalizeL( aStream, aStoreType );
       
   290         }
       
   291 
       
   292     // mark
       
   293     aStream.WriteInt32L( KErrNotFound );
       
   294     }
       
   295 
       
   296 
       
   297 // -----------------------------------------------------------------------------
       
   298 // PEngPropertyManager::InternalizePropertiesL()
       
   299 // -----------------------------------------------------------------------------
       
   300 //
       
   301 void PEngPropertyManager::InternalizePropertiesL(
       
   302     RPointerArray<CPEngContactListProperty>& aProperties,
       
   303     RReadStream& aStream,
       
   304     TInt& aSize )
       
   305     {
       
   306     // size of one number, marker for each property
       
   307     aSize += 4;
       
   308 
       
   309     while ( KErrNotFound != aStream.ReadInt32L() )
       
   310         {
       
   311         CPEngContactListProperty* prop =
       
   312             CPEngContactListProperty::NewLC( aStream );
       
   313         aProperties.AppendL( prop );
       
   314         CleanupStack::Pop(); // prop
       
   315         // before each property is validation number, size 4
       
   316         aSize += ( prop->EntrySize() + 4 );
       
   317         }
       
   318     }
       
   319 
       
   320 
       
   321 
       
   322 // ===============CPEngContactListProperty MEMBER FUNCTIONS ====================
       
   323 
       
   324 // -----------------------------------------------------------------------------
       
   325 // CPEngContactListProperty::CPEngContactListManager()
       
   326 // -----------------------------------------------------------------------------
       
   327 //
       
   328 CPEngContactListProperty::CPEngContactListProperty()
       
   329     {
       
   330     }
       
   331 
       
   332 // -----------------------------------------------------------------------------
       
   333 // CPEngContactListProperty::NewLC()
       
   334 // -----------------------------------------------------------------------------
       
   335 //
       
   336 CPEngContactListProperty* CPEngContactListProperty::NewLC( TUint aName,
       
   337                                                            TUint aLevel )
       
   338     {
       
   339     CPEngContactListProperty* self = new( ELeave ) CPEngContactListProperty();
       
   340     CleanupStack::PushL( self );
       
   341     self->iName = aName;
       
   342     self->iLevel = aLevel;
       
   343 
       
   344     //Validate the given level
       
   345     StorageTypeForLevelL( aLevel );
       
   346 
       
   347     return self;
       
   348     }
       
   349 
       
   350 
       
   351 // -----------------------------------------------------------------------------
       
   352 // CPEngContactListProperty::NewLC()
       
   353 // -----------------------------------------------------------------------------
       
   354 //
       
   355 CPEngContactListProperty* CPEngContactListProperty::NewLC( RReadStream& aStream )
       
   356     {
       
   357     CPEngContactListProperty* self = new( ELeave ) CPEngContactListProperty;
       
   358     CleanupStack::PushL( self );
       
   359     self->InternalizeL( aStream );
       
   360     return self;
       
   361     }
       
   362 
       
   363 
       
   364 // Destructor
       
   365 CPEngContactListProperty::~CPEngContactListProperty()
       
   366     {
       
   367     delete iTDes16Value;
       
   368     delete iTDes8Value;
       
   369     }
       
   370 
       
   371 
       
   372 // -----------------------------------------------------------------------------
       
   373 // CPEngContactListProperty::Name()
       
   374 // -----------------------------------------------------------------------------
       
   375 //
       
   376 TUint CPEngContactListProperty::Name() const
       
   377     {
       
   378     return iName;
       
   379     }
       
   380 
       
   381 
       
   382 // -----------------------------------------------------------------------------
       
   383 // CPEngContactListProperty::Level()
       
   384 // -----------------------------------------------------------------------------
       
   385 //
       
   386 TUint CPEngContactListProperty::Level() const
       
   387     {
       
   388     return iLevel;
       
   389     }
       
   390 
       
   391 
       
   392 // -----------------------------------------------------------------------------
       
   393 // CPEngContactListProperty::ValueInt()
       
   394 // -----------------------------------------------------------------------------
       
   395 //
       
   396 TInt CPEngContactListProperty::ValueInt() const
       
   397     {
       
   398     return iTIntValue;
       
   399     }
       
   400 
       
   401 
       
   402 // -----------------------------------------------------------------------------
       
   403 // CPEngContactListProperty::ValueDes16()
       
   404 // -----------------------------------------------------------------------------
       
   405 //
       
   406 const TDesC16& CPEngContactListProperty::ValueDes16() const
       
   407     {
       
   408     return *iTDes16Value;
       
   409     }
       
   410 
       
   411 
       
   412 // -----------------------------------------------------------------------------
       
   413 // CPEngContactListProperty::ValueDes8()
       
   414 // -----------------------------------------------------------------------------
       
   415 //
       
   416 const TDesC8& CPEngContactListProperty::ValueDes8() const
       
   417     {
       
   418     return *iTDes8Value;
       
   419     }
       
   420 
       
   421 
       
   422 // -----------------------------------------------------------------------------
       
   423 // CPEngContactListProperty::SetValueL()
       
   424 // -----------------------------------------------------------------------------
       
   425 //
       
   426 void CPEngContactListProperty::SetValue( TUint aValue )
       
   427     {
       
   428     iTIntValue = aValue;
       
   429     iTIntValueValid = ETrue;
       
   430     }
       
   431 
       
   432 
       
   433 // -----------------------------------------------------------------------------
       
   434 // CPEngContactListProperty::SetValueL()
       
   435 // -----------------------------------------------------------------------------
       
   436 //
       
   437 void CPEngContactListProperty::SetValueL( const TDesC16& aValue )
       
   438     {
       
   439     HBufC16*    temp = aValue.AllocL();
       
   440     delete iTDes16Value;
       
   441     iTDes16Value = temp;
       
   442     }
       
   443 
       
   444 
       
   445 // -----------------------------------------------------------------------------
       
   446 // CPEngContactListProperty::SetValueL()
       
   447 // -----------------------------------------------------------------------------
       
   448 //
       
   449 void CPEngContactListProperty::SetValueL( const TDesC8& aValue )
       
   450     {
       
   451     HBufC8* temp = aValue.AllocL();
       
   452     delete iTDes8Value;
       
   453     iTDes8Value = temp;
       
   454     }
       
   455 
       
   456 
       
   457 // -----------------------------------------------------------------------------
       
   458 // CPEngContactListProperty::ValidateType()
       
   459 // -----------------------------------------------------------------------------
       
   460 //
       
   461 TBool CPEngContactListProperty::ValidateType( TPEngPropertyType aType ) const
       
   462     {
       
   463     switch ( aType )
       
   464         {
       
   465         case ETInt:
       
   466             {
       
   467             return iTIntValueValid;
       
   468             }
       
   469 
       
   470 
       
   471         case EDes8:
       
   472             {
       
   473             return reinterpret_cast< TInt > ( iTDes8Value );
       
   474             }
       
   475 
       
   476 
       
   477         case EDes16:
       
   478             {
       
   479             return reinterpret_cast< TInt > ( iTDes16Value );
       
   480             }
       
   481 
       
   482         default:
       
   483             {
       
   484             return EFalse;
       
   485             }
       
   486         }
       
   487     }
       
   488 
       
   489 
       
   490 // -----------------------------------------------------------------------------
       
   491 // CPEngContactListProperty::ExternalizeL()
       
   492 // -----------------------------------------------------------------------------
       
   493 //
       
   494 void CPEngContactListProperty::ExternalizeL( RWriteStream& aStream,
       
   495                                              TInt aType ) const
       
   496     {
       
   497     if ( StorageTypeForLevelL( iLevel ) != aType )
       
   498         {
       
   499         // nothing to store, type and level do not match
       
   500         return;
       
   501         }
       
   502 
       
   503 
       
   504     //Mark start
       
   505     aStream.WriteInt32L( KErrNone );
       
   506     aStream.WriteUint32L( iName );
       
   507     aStream.WriteUint32L( iLevel );
       
   508 
       
   509 
       
   510     //Int value
       
   511     aStream.WriteInt32L( iTIntValue );
       
   512     aStream.WriteInt32L( iTIntValueValid );
       
   513 
       
   514 
       
   515     //16 bit desc
       
   516     if ( iTDes16Value )
       
   517         {
       
   518         aStream.WriteInt32L( iTDes16Value->Length() );
       
   519         aStream << *iTDes16Value;
       
   520         }
       
   521     else
       
   522         {
       
   523         aStream.WriteInt32L( 0 ); // zero length of the buffer
       
   524         }
       
   525 
       
   526 
       
   527     //8 bit desc
       
   528     if ( iTDes8Value )
       
   529         {
       
   530         aStream.WriteInt32L( iTDes8Value->Length() );
       
   531         aStream << *iTDes8Value;
       
   532         }
       
   533     else
       
   534         {
       
   535         aStream.WriteInt32L( 0 ); // zero length of the buffer
       
   536         }
       
   537     }
       
   538 
       
   539 
       
   540 // -----------------------------------------------------------------------------
       
   541 // CPEngContactListProperty::EntrySize()
       
   542 // -----------------------------------------------------------------------------
       
   543 //
       
   544 TInt CPEngContactListProperty::EntrySize()
       
   545     {
       
   546     // 24 for storing 6 32bits numbers
       
   547     return 24 + ( iTDes16Value ? iTDes16Value->Length() : 0 )
       
   548            + ( iTDes8Value ? iTDes8Value->Length() : 0 );
       
   549     }
       
   550 
       
   551 
       
   552 // -----------------------------------------------------------------------------
       
   553 // CPEngContactListProperty::InternalizeL()
       
   554 // -----------------------------------------------------------------------------
       
   555 //
       
   556 void CPEngContactListProperty::InternalizeL( RReadStream& aStream )
       
   557     {
       
   558     // mark read outside
       
   559     iName = aStream.ReadUint32L();
       
   560     iLevel = aStream.ReadUint32L();
       
   561     iTIntValue = aStream.ReadInt32L();
       
   562     iTIntValueValid = aStream.ReadInt32L();
       
   563 
       
   564     // read buffers
       
   565     delete iTDes16Value;
       
   566     iTDes16Value = NULL;
       
   567     TInt size( aStream.ReadInt32L() );
       
   568     if ( size != 0 )
       
   569         {
       
   570         iTDes16Value = HBufC16::NewL( aStream, size );
       
   571         }
       
   572 
       
   573     delete iTDes8Value;
       
   574     iTDes8Value = NULL;
       
   575     size = aStream.ReadInt32L();
       
   576     if ( size != 0 )
       
   577         {
       
   578         iTDes8Value = HBufC8::NewL( aStream, size );
       
   579         }
       
   580     }
       
   581 
       
   582 
       
   583 // -----------------------------------------------------------------------------
       
   584 // CPEngContactListProperty::StorageTypeForLevelL()
       
   585 // -----------------------------------------------------------------------------
       
   586 //
       
   587 TInt CPEngContactListProperty::StorageTypeForLevelL( TUint aLevel )
       
   588     {
       
   589     if ( ( aLevel == KPEngCntLstPropertyNativePermanent ) ||
       
   590          ( aLevel == KPEngCntLstPropertyExternalPermanent ) ||
       
   591          ( aLevel == KPEngCntItemPropertyNativePermanent ) ||
       
   592          ( aLevel == KPEngCntItemPropertyExternalPermanent ) )
       
   593         {
       
   594         return EPEngStorageBasicPermanent;
       
   595         }
       
   596 
       
   597     if ( ( aLevel == KPEngCntLstPropertyNativeCached ) ||
       
   598          ( aLevel == KPEngCntLstPropertyExternalCached ) ||
       
   599          ( aLevel == KPEngCntItemPropertyNativeCached ) ||
       
   600          ( aLevel == KPEngCntItemPropertyExternalCached ) )
       
   601         {
       
   602         return EPEngStorageBasicCached;
       
   603         }
       
   604 
       
   605     User::Leave( KErrUnknown );
       
   606     return EPEngStorageBasicTemporary;
       
   607     }
       
   608 
       
   609 
       
   610 // end of file
       
   611