ncdengine/provider/src/ncdattributes.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2006-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:   Implementation of CNcdAttributes
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <s32strm.h>
       
    20 
       
    21 #include "ncdattributes.h"
       
    22 #include "catalogsutils.h"
       
    23 #include "catalogsdebug.h"
       
    24 
       
    25 // Checks that the given attribute value is in range
       
    26 #define ASSERT_ATTRIBUTE_IN_RANGE( attribute )                          \
       
    27     NCD_ASSERT_ALWAYS( ( RangeLowerLimit() <= (attribute) ) &&          \
       
    28         ( (attribute) < RangeUpperLimit() ), ENcdPanicIndexOutOfRange );
       
    29 
       
    30 // Checks that the given type can be written to the attribute
       
    31 #define ASSERT_ATTRIBUTE_WRITABLE( attribute, writetype )               \
       
    32     NCD_ASSERT_ALWAYS(                                                  \
       
    33         attribute.iType == writetype ||                                 \
       
    34         attribute.iType == EAttributeTypeUndefined,                     \
       
    35         ENcdPanicInvalidArgument );
       
    36 
       
    37 // Checks that the given type can be read from the attribute
       
    38 #define ASSERT_ATTRIBUTE_READABLE( attribute, readtype )                \
       
    39     NCD_ASSERT_ALWAYS(                                                  \
       
    40         attribute.iType == readtype,                                    \
       
    41         ENcdPanicInvalidArgument );
       
    42         
       
    43 // ---------------------------------------------------------------------------
       
    44 // 
       
    45 // ---------------------------------------------------------------------------
       
    46 //        
       
    47 EXPORT_C CNcdAttributes* CNcdAttributes::NewL( 
       
    48     TInt aUpperLimit, 
       
    49     TInt aLowerLimit )
       
    50     {
       
    51     CNcdAttributes* self = new( ELeave ) CNcdAttributes( 
       
    52         aUpperLimit, aLowerLimit );
       
    53     CleanupStack::PushL( self );
       
    54     self->ConstructL();
       
    55     CleanupStack::Pop( self );
       
    56     return self;
       
    57     }
       
    58     
       
    59 // ---------------------------------------------------------------------------
       
    60 // 
       
    61 // ---------------------------------------------------------------------------
       
    62 //        
       
    63 EXPORT_C CNcdAttributes* CNcdAttributes::NewL( 
       
    64     RReadStream& aStream,
       
    65     TInt aUpperLimit, 
       
    66     TInt aLowerLimit )
       
    67     {
       
    68     CNcdAttributes* self = new( ELeave ) CNcdAttributes( 
       
    69         aUpperLimit, aLowerLimit );
       
    70     CleanupStack::PushL( self );
       
    71     self->InternalizeL( aStream );
       
    72     CleanupStack::Pop( self );
       
    73     return self;
       
    74     }
       
    75 
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 // 
       
    79 // ---------------------------------------------------------------------------
       
    80 //        
       
    81 EXPORT_C CNcdAttributes* CNcdAttributes::NewL( 
       
    82     const CNcdAttributes& aAttributes )
       
    83     {
       
    84     CNcdAttributes* self = new( ELeave ) CNcdAttributes( 
       
    85         aAttributes.RangeUpperLimit(), 
       
    86         aAttributes.RangeLowerLimit() );
       
    87     CleanupStack::PushL( self );
       
    88     self->ConstructL( aAttributes );
       
    89     CleanupStack::Pop( self );
       
    90     return self;
       
    91     }
       
    92 
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // Destructor
       
    96 // ---------------------------------------------------------------------------
       
    97 //        
       
    98 EXPORT_C CNcdAttributes::~CNcdAttributes()
       
    99     {
       
   100     DLTRACEIN((""));
       
   101     ClearAttributes();
       
   102     }
       
   103     
       
   104 
       
   105 // ---------------------------------------------------------------------------
       
   106 // 
       
   107 // ---------------------------------------------------------------------------
       
   108 //        
       
   109 EXPORT_C TInt CNcdAttributes::RangeLowerLimit() const
       
   110     {
       
   111     return iLowerLimit;
       
   112     }
       
   113     
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // 
       
   117 // ---------------------------------------------------------------------------
       
   118 //        
       
   119 EXPORT_C TInt CNcdAttributes::RangeUpperLimit() const
       
   120     {
       
   121     return iUpperLimit;
       
   122     }
       
   123 
       
   124 
       
   125 // ---------------------------------------------------------------------------
       
   126 // TInt32 Attribute setter
       
   127 // L-method mainly in order to be consistent with the string setter and
       
   128 // also in preparation for changing the asserts to leaves
       
   129 // ---------------------------------------------------------------------------
       
   130 //        
       
   131 EXPORT_C void CNcdAttributes::SetAttributeL( TInt aAttribute, TInt32 aValue )
       
   132     {
       
   133     DLTRACEIN(("aAttribute: %d, aValue: %d", aAttribute, aValue));
       
   134     
       
   135     TAttribute& attrib = Attribute( aAttribute );
       
   136     
       
   137     ASSERT_ATTRIBUTE_WRITABLE( attrib, EAttributeTypeInt32 );
       
   138     
       
   139     attrib.iType = EAttributeTypeInt32;
       
   140     attrib.iAttribute.iInt32 = aValue;
       
   141     }
       
   142     
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // 
       
   146 // ---------------------------------------------------------------------------
       
   147 //        
       
   148 EXPORT_C void CNcdAttributes::SetAttributeL( 
       
   149     TInt aAttribute, 
       
   150     const TDesC& aValue )
       
   151     {
       
   152     DLTRACEIN(( _L("aAttribute: %d, aValue: %S"), aAttribute, &aValue ));
       
   153     
       
   154     TAttribute& attrib = Attribute( aAttribute );
       
   155     
       
   156     ASSERT_ATTRIBUTE_WRITABLE( attrib, EAttributeTypeString16 );
       
   157     
       
   158     HBufC* tempValue = aValue.AllocL();
       
   159     
       
   160     delete attrib.iAttribute.iString16;
       
   161     attrib.iAttribute.iString16 = tempValue;
       
   162     
       
   163     attrib.iType = EAttributeTypeString16;
       
   164     }
       
   165 
       
   166 
       
   167 // ---------------------------------------------------------------------------
       
   168 // 
       
   169 // ---------------------------------------------------------------------------
       
   170 //        
       
   171 EXPORT_C const TDesC& CNcdAttributes::AttributeString16( 
       
   172     TInt aAttribute ) const
       
   173     {
       
   174     DLTRACEIN(("aAttribute: %d", aAttribute));
       
   175     
       
   176     const TAttribute& attrib = Attribute( aAttribute );
       
   177     
       
   178     if ( attrib.iType == EAttributeTypeUndefined ) 
       
   179         {
       
   180         DLTRACEOUT(("Not set"));
       
   181         return KNullDesC;
       
   182         }
       
   183         
       
   184     ASSERT_ATTRIBUTE_READABLE( attrib, EAttributeTypeString16 );
       
   185     
       
   186     DASSERT( attrib.iAttribute.iString16 );
       
   187     DLTRACEOUT(( _L("value: %S"), 
       
   188         attrib.iAttribute.iString16 ));
       
   189         
       
   190     return *attrib.iAttribute.iString16;
       
   191     }
       
   192      
       
   193 
       
   194 // ---------------------------------------------------------------------------
       
   195 // 
       
   196 // ---------------------------------------------------------------------------
       
   197 //        
       
   198 EXPORT_C TInt32 CNcdAttributes::AttributeInt32( TInt aAttribute ) const
       
   199     {
       
   200     DLTRACEIN(("aAttribute: %d", aAttribute));
       
   201     
       
   202     const TAttribute& attrib = Attribute( aAttribute );
       
   203 
       
   204     if ( attrib.iType == EAttributeTypeUndefined ) 
       
   205         {
       
   206         DLTRACEOUT(("Not set"));
       
   207         return 0;
       
   208         }
       
   209     
       
   210     ASSERT_ATTRIBUTE_READABLE( attrib, EAttributeTypeInt32 );
       
   211     
       
   212     DLTRACEOUT(("value: %d", attrib.iAttribute.iInt32 ));
       
   213     return attrib.iAttribute.iInt32;    
       
   214     }
       
   215 
       
   216 
       
   217 // ---------------------------------------------------------------------------
       
   218 // 
       
   219 // ---------------------------------------------------------------------------
       
   220 //        
       
   221 EXPORT_C CNcdAttributes::TAttributeType CNcdAttributes::AttributeType( 
       
   222     TInt aAttribute ) const
       
   223     {
       
   224     DLTRACEIN(("aAttribute: %d", aAttribute));
       
   225     return Attribute( aAttribute ).iType;
       
   226     }
       
   227 
       
   228 
       
   229 // ---------------------------------------------------------------------------
       
   230 // 
       
   231 // ---------------------------------------------------------------------------
       
   232 //        
       
   233 EXPORT_C void CNcdAttributes::ExternalizeL( RWriteStream& aStream ) const
       
   234     {
       
   235     DLTRACEIN((""));
       
   236     aStream.WriteInt32L( iLowerLimit );
       
   237     aStream.WriteInt32L( iUpperLimit );
       
   238     
       
   239     DLTRACE(("Externalizing %d attributes", iAttributes.Count() ));
       
   240     DASSERT( iAttributes.Count() == ArraySize() );
       
   241     for ( TInt i = 0; i < iAttributes.Count(); ++i ) 
       
   242         {
       
   243         ExternalizeAttributeL( iAttributes[i], aStream );
       
   244         }
       
   245     }
       
   246     
       
   247     
       
   248 // ---------------------------------------------------------------------------
       
   249 // 
       
   250 // ---------------------------------------------------------------------------
       
   251 //        
       
   252 EXPORT_C void CNcdAttributes::InternalizeL( RReadStream& aStream )
       
   253     {
       
   254     DLTRACEIN((""));    
       
   255     TInt lowerLimit = aStream.ReadInt32L();
       
   256     
       
   257     // Lower limits are not allowed to change
       
   258     if ( lowerLimit != iLowerLimit ) 
       
   259         {
       
   260         DLERROR(("Lower limits don't match, leaving"));
       
   261         User::Leave( KErrArgument );
       
   262         }
       
   263     
       
   264     // Ensure that upperlimit is in the allowed range
       
   265     NCD_ASSERT_ALWAYS( iUpperLimit > 0 && iUpperLimit > iLowerLimit, 
       
   266         ENcdPanicIndexOutOfRange );
       
   267     
       
   268     // Read stored upper limit, this will be overridden by the
       
   269     // upper limit given for NewL
       
   270     TInt upperLimit = aStream.ReadInt32L();
       
   271     
       
   272     // Reset attribute array to correct amount of attributes
       
   273     ResetAttributesL();
       
   274     
       
   275     // Read either all values from the stream or only those that fit 
       
   276     // the current limits.
       
   277     TInt count = Min( upperLimit, iUpperLimit ) - iLowerLimit;
       
   278     
       
   279     DLTRACE(("Internalizing %d attributes", count));
       
   280     for ( TInt i = 0; i < count; ++i )
       
   281         {
       
   282         iAttributes[i] = InternalizeAttributeL( aStream );
       
   283         }
       
   284     
       
   285     TInt leftOvers = upperLimit - iUpperLimit;
       
   286     // Check if some attributes were not read from the stream    
       
   287     if ( leftOvers ) 
       
   288         {
       
   289         DLTRACE(("Reading and deleting %d leftover attributes", leftOvers));
       
   290         while ( leftOvers-- ) 
       
   291             {
       
   292             TAttribute attribute = InternalizeAttributeL( aStream );
       
   293             DeleteAttribute( attribute );
       
   294             }
       
   295         }
       
   296     DLTRACEOUT(("Attributes internalized successfully" ));
       
   297     }
       
   298     
       
   299 
       
   300 // ---------------------------------------------------------------------------
       
   301 // 
       
   302 // ---------------------------------------------------------------------------
       
   303 //        
       
   304 CNcdAttributes::CNcdAttributes( TInt aUpperLimit, TInt aLowerLimit ) 
       
   305     : iLowerLimit( aLowerLimit ), 
       
   306       iUpperLimit( aUpperLimit )
       
   307     {
       
   308     }
       
   309     
       
   310 
       
   311 // ---------------------------------------------------------------------------
       
   312 // ConstructL
       
   313 // ---------------------------------------------------------------------------
       
   314 //        
       
   315 void CNcdAttributes::ConstructL()
       
   316     {
       
   317     DLTRACEIN(("Upper limit: %d, lower limit: %d", 
       
   318         iUpperLimit, iLowerLimit ));
       
   319         
       
   320     NCD_ASSERT_ALWAYS( iUpperLimit > 0 && iUpperLimit > iLowerLimit, 
       
   321         ENcdPanicIndexOutOfRange );
       
   322 
       
   323     ResetAttributesL();
       
   324     }
       
   325 
       
   326 
       
   327 // ---------------------------------------------------------------------------
       
   328 // ConstructL for copying the attributes
       
   329 // ---------------------------------------------------------------------------
       
   330 //        
       
   331 void CNcdAttributes::ConstructL( const CNcdAttributes& aAttributes )
       
   332     {
       
   333     DLTRACEIN((""));
       
   334     TInt count = ArraySize();
       
   335     DASSERT( count == aAttributes.iAttributes.Count() );
       
   336     
       
   337     iAttributes.ReserveL( count );
       
   338     for ( TInt i = 0; i < count; ++i )
       
   339         {
       
   340         iAttributes.Append( CopyAttributeL( aAttributes.iAttributes[i] ) );
       
   341         }
       
   342     }
       
   343 
       
   344 
       
   345 // ---------------------------------------------------------------------------
       
   346 // Clear attribute array
       
   347 // ---------------------------------------------------------------------------
       
   348 //        
       
   349 void CNcdAttributes::ClearAttributes()
       
   350     {
       
   351     DLTRACEIN((""));
       
   352     for ( TInt i = 0; i < iAttributes.Count(); ++i ) 
       
   353         {
       
   354         DeleteAttribute( iAttributes[i] );
       
   355         }
       
   356     iAttributes.Reset();
       
   357     }
       
   358 
       
   359 
       
   360 // ---------------------------------------------------------------------------
       
   361 // Reset attribute array
       
   362 // ---------------------------------------------------------------------------
       
   363 //        
       
   364 void CNcdAttributes::ResetAttributesL()
       
   365     {
       
   366     DLTRACEIN((""));
       
   367     ClearAttributes();
       
   368     
       
   369     TInt size = ArraySize();
       
   370     DLTRACE(("Resetting the array with %d attributes", size));
       
   371     iAttributes.ReserveL( size );
       
   372     TAttribute attrib;
       
   373     
       
   374     while( size-- ) 
       
   375         {
       
   376         iAttributes.Append( attrib );
       
   377         }    
       
   378     DLTRACEOUT(("Array size: %d", iAttributes.Count() ));
       
   379     }
       
   380 
       
   381 
       
   382 // ---------------------------------------------------------------------------
       
   383 // 
       
   384 // ---------------------------------------------------------------------------
       
   385 //        
       
   386 TInt CNcdAttributes::ArraySize() const
       
   387     {
       
   388     return iUpperLimit - iLowerLimit;
       
   389     }
       
   390     
       
   391     
       
   392 // ---------------------------------------------------------------------------
       
   393 // 
       
   394 // ---------------------------------------------------------------------------
       
   395 //        
       
   396 void CNcdAttributes::ExternalizeAttributeL( 
       
   397     const TAttribute& aAttribute, RWriteStream& aStream ) const    
       
   398     {
       
   399     DLTRACEIN((""));    
       
   400     ExternalizeEnumL( aAttribute.iType, aStream );
       
   401     switch( aAttribute.iType ) 
       
   402         {
       
   403         case EAttributeTypeInt32:
       
   404             {
       
   405             DLTRACE(("Int"));
       
   406             aStream.WriteInt32L( aAttribute.iAttribute.iInt32 );
       
   407             break;
       
   408             }
       
   409 
       
   410         case EAttributeTypeString16:
       
   411             {
       
   412             DLTRACE(("String"));
       
   413             ExternalizeDesL( *aAttribute.iAttribute.iString16, aStream );
       
   414             break;
       
   415             }
       
   416         
       
   417         case EAttributeTypeUndefined:
       
   418             {
       
   419             DLTRACE(("Undefined"));
       
   420             // Nothing to do
       
   421             break;
       
   422             }
       
   423             
       
   424         default:
       
   425             NCD_ASSERT_ALWAYS( 0, ENcdPanicIndexOutOfRange );
       
   426         }        
       
   427     }
       
   428 
       
   429 
       
   430 // ---------------------------------------------------------------------------
       
   431 // 
       
   432 // ---------------------------------------------------------------------------
       
   433 //        
       
   434 CNcdAttributes::TAttribute CNcdAttributes::InternalizeAttributeL( 
       
   435     RReadStream& aStream ) const    
       
   436     {
       
   437     DLTRACEIN((""));  
       
   438     TAttribute attribute;
       
   439     InternalizeEnumL( attribute.iType, aStream );
       
   440     
       
   441     switch( attribute.iType ) 
       
   442         {
       
   443         case EAttributeTypeInt32:
       
   444             {
       
   445             attribute.iAttribute.iInt32 = aStream.ReadInt32L();
       
   446             break;
       
   447             }
       
   448 
       
   449         case EAttributeTypeString16:
       
   450             {
       
   451             InternalizeDesL( attribute.iAttribute.iString16, aStream );
       
   452             break;
       
   453             }
       
   454         
       
   455         case EAttributeTypeUndefined:
       
   456             {
       
   457             // Nothing to do
       
   458             break;
       
   459             }
       
   460             
       
   461         default:
       
   462             NCD_ASSERT_ALWAYS( 0, ENcdPanicIndexOutOfRange );
       
   463         }        
       
   464     DLTRACEOUT(("Read attribute of type: %d", attribute.iType ));
       
   465     return attribute; 
       
   466     }
       
   467 
       
   468 
       
   469 // ---------------------------------------------------------------------------
       
   470 // 
       
   471 // ---------------------------------------------------------------------------
       
   472 //        
       
   473 void CNcdAttributes::DeleteAttribute( TAttribute& aAttribute ) 
       
   474     {
       
   475     DLTRACEIN((""));
       
   476     if ( aAttribute.iType == EAttributeTypeString16 ) 
       
   477         {
       
   478         delete aAttribute.iAttribute.iString16;
       
   479         aAttribute.iAttribute.iString16 = NULL;
       
   480         }    
       
   481     }
       
   482 
       
   483 
       
   484 // ---------------------------------------------------------------------------
       
   485 // 
       
   486 // ---------------------------------------------------------------------------
       
   487 //        
       
   488 CNcdAttributes::TAttribute CNcdAttributes::CopyAttributeL( 
       
   489     const TAttribute& aAttribute ) const
       
   490     {
       
   491     DLTRACEIN((""));
       
   492     TAttribute copy;
       
   493     copy.iType = aAttribute.iType;
       
   494     
       
   495     switch( aAttribute.iType ) 
       
   496         {
       
   497         case EAttributeTypeInt32:
       
   498             {
       
   499             copy.iAttribute.iInt32 = aAttribute.iAttribute.iInt32;
       
   500             break;
       
   501             }
       
   502 
       
   503         case EAttributeTypeString16:
       
   504             {
       
   505             if ( aAttribute.iAttribute.iString16 ) 
       
   506                 {                
       
   507                 copy.iAttribute.iString16 = 
       
   508                     aAttribute.iAttribute.iString16->AllocL();
       
   509                 }
       
   510             break;
       
   511             }
       
   512         
       
   513         case EAttributeTypeUndefined:
       
   514             {
       
   515             // Nothing to do
       
   516             break;
       
   517             }
       
   518             
       
   519         default:
       
   520             NCD_ASSERT_ALWAYS( 0, ENcdPanicIndexOutOfRange );
       
   521         } 
       
   522     return copy;       
       
   523     }
       
   524 
       
   525 // ---------------------------------------------------------------------------
       
   526 // 
       
   527 // ---------------------------------------------------------------------------
       
   528 //        
       
   529 const CNcdAttributes::TAttribute& CNcdAttributes::Attribute( 
       
   530     TInt aAttribute ) const
       
   531     {
       
   532     DLTRACEIN(("aAttribute: %d", aAttribute));
       
   533     ASSERT_ATTRIBUTE_IN_RANGE( aAttribute );
       
   534     return iAttributes[ aAttribute - iLowerLimit ];
       
   535     }
       
   536 
       
   537 
       
   538 // ---------------------------------------------------------------------------
       
   539 // 
       
   540 // ---------------------------------------------------------------------------
       
   541 //        
       
   542 CNcdAttributes::TAttribute& CNcdAttributes::Attribute( TInt aAttribute )
       
   543     {
       
   544     DLTRACEIN(("aAttribute: %d", aAttribute));
       
   545     ASSERT_ATTRIBUTE_IN_RANGE( aAttribute );
       
   546     return iAttributes[ aAttribute - iLowerLimit ];
       
   547     }