brandingserver/bsclient/cbselement.cpp
changeset 0 e6b17d312c8b
child 21 cfd5c2994f10
equal deleted inserted replaced
-1:000000000000 0:e6b17d312c8b
       
     1 /*
       
     2 * Copyright (c) 2009 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: cbselement.cpp
       
    15 *
       
    16 */
       
    17 //  INCLUDE FILES
       
    18 
       
    19 #include <utf.h>
       
    20 #include "e32base.h"
       
    21 #include "s32strm.h"
       
    22 #include "cbselement.h"
       
    23 #include "cbsbitmap.h"
       
    24 
       
    25 
       
    26 CBSElement* CBSElement::NewL(
       
    27 						const TDesC8& aElementId,
       
    28 						TInt aData )
       
    29     {
       
    30 
       
    31 	CBSElement* self = new ( ELeave ) CBSElement() ;
       
    32     CleanupStack::PushL( self );
       
    33     self->ConstructL( EBSInt,
       
    34     				  aElementId,
       
    35     				  aData,
       
    36     				  ETrue,
       
    37     				  NULL,
       
    38     				  KNullDesC,
       
    39     				  KNullDesC8,
       
    40     				  NULL );
       
    41     CleanupStack::Pop( self );  //self
       
    42     return self;
       
    43     }
       
    44 
       
    45 
       
    46 CBSElement* CBSElement::NewL(
       
    47 						const TDesC8& aElementId,
       
    48 						const TDesC& aData,
       
    49 						TBSElementType aType /*= EText*/ )
       
    50     {
       
    51     if( aType != EBSFile && aType != EBSText )
       
    52         {
       
    53         User::Leave( KErrArgument );
       
    54         }
       
    55 
       
    56     CBSElement* self = new ( ELeave ) CBSElement() ;
       
    57     CleanupStack::PushL( self );
       
    58     self->ConstructL( aType,
       
    59     				  aElementId,
       
    60     				  0,
       
    61     				  EFalse,
       
    62     				  NULL,
       
    63     				  aData,
       
    64     				  KNullDesC8,
       
    65     				  NULL);
       
    66     CleanupStack::Pop( self );  //self
       
    67     return self;
       
    68     }
       
    69 
       
    70 
       
    71 CBSElement* CBSElement::NewL(
       
    72 						const TDesC8& aElementId,
       
    73 						const TDesC8& aData )
       
    74     {
       
    75     CBSElement* self = new ( ELeave ) CBSElement() ;
       
    76     CleanupStack::PushL( self );
       
    77     self->ConstructL( EBSBuffer,
       
    78     				  aElementId,
       
    79     				  0,
       
    80     				  EFalse,
       
    81     				  NULL,
       
    82     				  KNullDesC,
       
    83     				  aData,
       
    84     				  NULL);
       
    85     CleanupStack::Pop( self );  //self
       
    86     return self;
       
    87     }
       
    88 
       
    89 CBSElement* CBSElement::NewL(
       
    90 						const TDesC8& aElementId,
       
    91 						RBSObjOwningPtrArray<MBSElement>& aStructure )
       
    92     {
       
    93     CBSElement* self = new ( ELeave ) CBSElement();
       
    94     CleanupStack::PushL( self );
       
    95     self->ConstructL( EBSList,
       
    96     				  aElementId,
       
    97     				  0,
       
    98     				  EFalse,
       
    99     				  NULL,
       
   100     				  KNullDesC,
       
   101     				  KNullDesC8,
       
   102     				  &aStructure );
       
   103     CleanupStack::Pop( self );  //self
       
   104     return self;
       
   105     }
       
   106 
       
   107 CBSElement* CBSElement::NewL(
       
   108     					const TDesC8& aElementId,
       
   109     					CBSBitmap* aBitmap )
       
   110     {
       
   111     CBSElement* self = new ( ELeave ) CBSElement() ;
       
   112     CleanupStack::PushL( self );
       
   113     self->ConstructL( EBSBitmap,
       
   114     				  aElementId,
       
   115     				  0,
       
   116     				  EFalse,
       
   117     				  aBitmap,
       
   118     				  KNullDesC,
       
   119     				  KNullDesC8,
       
   120     				  NULL );
       
   121 
       
   122 	if( !aBitmap )
       
   123 		{
       
   124 		User::Leave( KErrArgument );
       
   125 		}
       
   126 
       
   127     CleanupStack::Pop( self );  //self
       
   128     return self;
       
   129     }
       
   130 
       
   131 void CBSElement::ConstructL(
       
   132 						 TBSElementType aElementType,
       
   133 					 	 const TDesC8& aElementId,
       
   134 					 	 TInt aIntData,
       
   135 					 	 TBool aIntDataInitialized,
       
   136 					 	 CBSBitmap* aBitmap,
       
   137 					 	 const TDesC& aTextData,
       
   138 					 	 const TDesC8& aBufferData,
       
   139 					 	 RBSObjOwningPtrArray<MBSElement>* aStructure )
       
   140     {
       
   141 	iElementType = aElementType;
       
   142 
       
   143 	iElementId = aElementId.AllocL();
       
   144 
       
   145 	iIntData = aIntData;
       
   146 	iIntDataInitialized = aIntDataInitialized;
       
   147 
       
   148 	iBitmapData = aBitmap;
       
   149 
       
   150 	if( 0 != aTextData.Compare( KNullDesC() ) )
       
   151 		{
       
   152 		iTextData = aTextData.AllocL();
       
   153 		}
       
   154 	if( 0 != aBufferData.Compare( KNullDesC8() ) )
       
   155 		{
       
   156 		iBufferData = aBufferData.AllocL();
       
   157 		}
       
   158 	if( aStructure )
       
   159 		{
       
   160 		iArrayInitialized = ETrue;
       
   161 		iArray = *aStructure;
       
   162 		}
       
   163     }
       
   164 
       
   165 
       
   166 CBSElement::~CBSElement()
       
   167     {
       
   168 	delete iTextData;
       
   169 	delete iBufferData;
       
   170 	delete iElementId;
       
   171 
       
   172 	iArray.Close();
       
   173 	delete iBitmapData;
       
   174     }
       
   175 
       
   176 
       
   177 CBSElement::CBSElement()
       
   178 	{
       
   179 	}
       
   180 
       
   181 void CBSElement::Close()
       
   182 	{
       
   183 	delete this;
       
   184 	}
       
   185 	
       
   186 	
       
   187 TBSElementType CBSElement::ElementType()
       
   188 	{
       
   189 	return iElementType;
       
   190 	}
       
   191 
       
   192 const TDesC8& CBSElement::ElementId()
       
   193 	{
       
   194 	return *iElementId;
       
   195 	}
       
   196 
       
   197 TInt CBSElement::IntDataL()
       
   198 	{
       
   199 	if( !iIntDataInitialized )
       
   200 		{
       
   201 		User::Leave( KErrArgument );
       
   202 		}
       
   203 	return iIntData;
       
   204 	}
       
   205 
       
   206 const TDesC& CBSElement::TextDataL()
       
   207 	{
       
   208 	if( !iTextData )
       
   209 		{
       
   210 		User::Leave( KErrArgument );
       
   211 		}
       
   212 	return *iTextData;
       
   213 	}
       
   214 
       
   215 const TDesC8& CBSElement::BufferDataL()
       
   216 	{
       
   217 	if( !iBufferData )
       
   218 		{
       
   219 		User::Leave( KErrArgument );
       
   220 		}
       
   221 	return *iBufferData;
       
   222 	}
       
   223 
       
   224 const CBSBitmap& CBSElement::BitmapDataL()
       
   225     {
       
   226 	if( !iBitmapData )
       
   227 		{
       
   228 		User::Leave( KErrArgument );
       
   229 		}
       
   230     return *iBitmapData;
       
   231     }
       
   232 
       
   233 TArray<MBSElement*> CBSElement::GetStructureL()
       
   234 	{
       
   235 	if( !iArrayInitialized )
       
   236 		{
       
   237 		User::Leave( KErrArgument );
       
   238 		}
       
   239 
       
   240 	return iArray.Array();
       
   241 	}
       
   242 
       
   243 const MBSElement& CBSElement::FindSubEntryL( const TDesC& aElementId )
       
   244 	{
       
   245     if ( !iArray.Count() )
       
   246         {
       
   247         User::Leave( KErrNotFound );
       
   248         }
       
   249     
       
   250 	MBSElement* element = NULL;
       
   251 	HBufC8* id = CnvUtfConverter::ConvertFromUnicodeToUtf8L( aElementId );	
       
   252 	User::LeaveIfNull( id );
       
   253 	CleanupStack::PushL( id );
       
   254 	for ( TInt i( 0 ) ; i < iArray.Count() ; i++ )
       
   255 	    {
       
   256 	    if ( iArray[ i ] )
       
   257 	        {
       
   258     	    if ( !iArray[ i ]->ElementId().CompareC( *id ) )
       
   259     	        {
       
   260     	        // Match found
       
   261     	        element = iArray[ i ];
       
   262     	        i = iArray.Count();// Exit loop.
       
   263     	        }
       
   264 	        }
       
   265 	    }
       
   266 	CleanupStack::PopAndDestroy( id );
       
   267 	  
       
   268 	if ( !element )
       
   269 	    {
       
   270 	    User::Leave( KErrNotFound );
       
   271 	    }
       
   272 	      
       
   273 	return *element;
       
   274 	}
       
   275 
       
   276 void CBSElement::ExternalizeL( RWriteStream& aWriteStream )
       
   277     {
       
   278     // Write common header for all elements
       
   279     aWriteStream.WriteInt16L( iElementType );
       
   280     aWriteStream.WriteInt16L( iElementId->Length() );
       
   281     if( iElementId->Length() > 0 )
       
   282         {
       
   283         // Write ID only if it's defined
       
   284         aWriteStream.WriteL( *iElementId );
       
   285         }
       
   286 
       
   287     // Write element type specific data
       
   288     switch( iElementType )
       
   289         {
       
   290         case EBSInt:
       
   291             {
       
   292             aWriteStream.WriteInt16L( iIntData );
       
   293             break;
       
   294             }
       
   295         case EBSText:     // flowthrough
       
   296         case EBSFile:
       
   297             {
       
   298             aWriteStream.WriteInt16L( iTextData->Length() );
       
   299             aWriteStream.WriteL( *iTextData );
       
   300             break;
       
   301             }
       
   302         case EBSList:
       
   303             {
       
   304 			TInt count = iArray.Count();
       
   305             aWriteStream.WriteInt16L( count );
       
   306             // Write all subitems to stream
       
   307             for( TInt i = 0; i < count; i++ )
       
   308                 {
       
   309 				MBSElement* element = iArray[i];
       
   310                 element->ExternalizeL( aWriteStream );
       
   311                 }
       
   312             break;
       
   313             }
       
   314         case EBSBitmap:
       
   315             {
       
   316 			TPtrC8 ptr = iBitmapData->BitmapFileId();
       
   317 			aWriteStream.WriteInt16L( ptr.Length() );
       
   318             aWriteStream.WriteL( ptr );
       
   319             
       
   320             aWriteStream.WriteInt16L( iBitmapData->BitmapId() );
       
   321             aWriteStream.WriteInt16L( iBitmapData->BitmapMaskId() );
       
   322             aWriteStream.WriteInt16L( iBitmapData->SkinIdMajor() );
       
   323             aWriteStream.WriteInt16L( iBitmapData->SkinIdMinor() );
       
   324 
       
   325             break;
       
   326             }
       
   327         case EBSBuffer:
       
   328         	{
       
   329             aWriteStream.WriteInt16L( iBufferData->Length() );
       
   330             aWriteStream.WriteL( *iBufferData );
       
   331         	break;
       
   332         	}
       
   333         default:
       
   334             {
       
   335             // unknown type!
       
   336             User::Leave( KErrCorrupt );
       
   337             break;
       
   338             }
       
   339         }
       
   340     }
       
   341 
       
   342 
       
   343 //  END OF FILE