brandingserver/BSServer/cbsstorage.cpp
changeset 31 9dbc70490d9a
equal deleted inserted replaced
30:1fa9b890f29c 31:9dbc70490d9a
       
     1 /*
       
     2 * Copyright (c) 2006-2006 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 the License "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:   Stores element data and writes it to stream
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "cbsstorage.h"
       
    21 #include "bsimportconstants.h"
       
    22 #include "DebugTrace.h"
       
    23 
       
    24 
       
    25 #include <s32strm.h>
       
    26 #include <utf.h>
       
    27 #include <mbselement.h>
       
    28 
       
    29 //#include "mbsimportlogger.h"
       
    30 //#include "importlogwriter.h"
       
    31 
       
    32 // ======== MEMBER FUNCTIONS ========
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // CBSStorage::NewL
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 CBSStorage* CBSStorage::NewL()
       
    39     {
       
    40     CBSStorage* self = NewLC();
       
    41     CleanupStack::Pop();
       
    42     return self;
       
    43     }
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // CBSStorage::NewLC
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 CBSStorage* CBSStorage::NewLC()
       
    50     {
       
    51     CBSStorage* self = new (ELeave) CBSStorage();
       
    52     CleanupStack::PushL( self );
       
    53     self->ConstructL();
       
    54     return self;
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // CBSStorage::ConstructL
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 void CBSStorage::ConstructL()
       
    62     {
       
    63     }
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // CBSStorage::CBSStorage
       
    67 // ---------------------------------------------------------------------------
       
    68 //
       
    69 CBSStorage::CBSStorage()
       
    70     {
       
    71     }
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 // CBSStorage::~CBSStorage
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 CBSStorage::~CBSStorage()
       
    78     {
       
    79     iElements.Close();
       
    80     delete iAppId;
       
    81     delete iStorageId;
       
    82     delete iLanguage;
       
    83     delete iFilename;
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // CBSStorage::AppendElement
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90 void CBSStorage::AppendElementsL( MBSElement* aElement )
       
    91     {
       
    92     if( !aElement )
       
    93         {
       
    94 		// Ignore empty
       
    95 	    return;
       
    96         }
       
    97 
       
    98 	TPtrC8 id = aElement->ElementId();
       
    99 	
       
   100 	// check if the id is unique
       
   101 	TInt count = iElements.Count();
       
   102 	
       
   103 	for( TInt i = 0; i < count; i++ )
       
   104 		{
       
   105 		if( 0 == iElements[i]->ElementId().Compare( id ) )
       
   106 			{
       
   107 			// duplicate found
       
   108 			User::Leave( KErrAlreadyExists );
       
   109 			}
       
   110 		}
       
   111 
       
   112 	iElements.AppendL( aElement );
       
   113     }
       
   114 
       
   115 
       
   116 // ---------------------------------------------------------------------------
       
   117 // CBSStorage::ReplacesElementL
       
   118 // ---------------------------------------------------------------------------
       
   119 //
       
   120 void CBSStorage::ReplaceElementL( MBSElement* aElement )
       
   121 	{
       
   122     if( !aElement )
       
   123         {
       
   124 		// Ignore empty
       
   125 	    return;
       
   126         }
       
   127 
       
   128 	TPtrC8 id = aElement->ElementId();
       
   129 	
       
   130 	// check if the id is unique
       
   131 	TInt count = iElements.Count();
       
   132 	
       
   133 	for( TInt i = 0; i < count; i++ )
       
   134 		{
       
   135 		if( 0 == iElements[i]->ElementId().Compare( id ) )
       
   136 			{
       
   137 			// item found
       
   138 			MBSElement* element = iElements[i];
       
   139 			iElements.Remove( i );
       
   140 			delete element;
       
   141 			// put the new element
       
   142 			iElements.AppendL( aElement );
       
   143 			return;
       
   144 			}
       
   145 		}
       
   146 	// the element was not found from this storage
       
   147 	User::Leave( KErrNotFound );
       
   148 	}
       
   149 
       
   150 
       
   151 // ---------------------------------------------------------------------------
       
   152 // CBSStorage::ElementCount
       
   153 // ---------------------------------------------------------------------------
       
   154 //
       
   155 TInt CBSStorage::ElementCount()
       
   156     {
       
   157     return iElements.Count();
       
   158     }
       
   159 
       
   160 // ---------------------------------------------------------------------------
       
   161 // CBSStorage::ExternalizeL
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 void CBSStorage::ExternalizeL( RWriteStream& aWriteStream )
       
   165     {
       
   166     if( !VerifyHeader() || !VerifyDataL( iElements ) )
       
   167         {
       
   168         // Header not complete. Will not write to stream
       
   169         User::Leave( KErrCorrupt );
       
   170         }
       
   171     // Write header
       
   172     aWriteStream.WriteInt16L( iVersion );
       
   173 
       
   174     // Write elements 
       
   175     TInt count = iElements.Count();
       
   176     aWriteStream.WriteInt16L( count );
       
   177     for( TInt i = 0; i < count; i++ )
       
   178         {
       
   179         iElements[i]->ExternalizeL( aWriteStream );
       
   180         }
       
   181     }
       
   182 
       
   183 // ---------------------------------------------------------------------------
       
   184 // CBSStorage::SetVersion
       
   185 // ---------------------------------------------------------------------------
       
   186 //
       
   187 void CBSStorage::SetVersion( TInt aVersion )
       
   188     {
       
   189     iVersion = aVersion;
       
   190     }
       
   191 
       
   192 // ---------------------------------------------------------------------------
       
   193 // CBSStorage::SetApplicationIdL
       
   194 // ---------------------------------------------------------------------------
       
   195 //
       
   196 void CBSStorage::SetApplicationIdL( const TDesC& aAppId )
       
   197     {
       
   198     HBufC* tmp = aAppId.AllocL();
       
   199     
       
   200     delete iAppId;
       
   201     iAppId = tmp;
       
   202     }
       
   203 
       
   204 // ---------------------------------------------------------------------------
       
   205 // CBSStorage::SetStorageIdL
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 void CBSStorage::SetStorageIdL( const TDesC& aStorageId )
       
   209     {
       
   210     HBufC* tmp = aStorageId.AllocL();
       
   211     delete iStorageId;
       
   212     iStorageId = tmp;
       
   213     }
       
   214 
       
   215 // ---------------------------------------------------------------------------
       
   216 // CBSStorage::SetLanguageL
       
   217 // ---------------------------------------------------------------------------
       
   218 //
       
   219 void CBSStorage::SetLanguageL( TLanguage aLanguage )
       
   220     {
       
   221     TBuf<KLangBufLength> buffer;
       
   222     buffer.AppendNum( aLanguage );
       
   223     
       
   224     HBufC* tmp = buffer.AllocL();
       
   225     delete iLanguage;
       
   226     iLanguage = tmp;
       
   227     }
       
   228 
       
   229 // ---------------------------------------------------------------------------
       
   230 // CBSStorage::VerifyHeader
       
   231 // ---------------------------------------------------------------------------
       
   232 //
       
   233 TBool CBSStorage::VerifyHeader()
       
   234     {
       
   235     // not checking version as it is been consider not to use. - Pankaj - 31 may 07
       
   236     //return ( iAppId && iStorageId && iVersion > 0 && iLanguage );
       
   237     return ( iAppId && iStorageId && iLanguage );
       
   238     }
       
   239 
       
   240 
       
   241 // ---------------------------------------------------------------------------
       
   242 // CBSStorage::VerifyDataL
       
   243 // ---------------------------------------------------------------------------
       
   244 //
       
   245 TBool CBSStorage::VerifyDataL( TArray<MBSElement*> aArray,
       
   246                                     TBool aIgnoreEmptyID /* = EFalse */ )
       
   247     {
       
   248     // Check that elements have unique ID's in their own namespace
       
   249     TInt count = aArray.Count();
       
   250     for( TInt i = 0; i < count; i++ )
       
   251         {
       
   252         // Verify inside list elements
       
   253         if( aArray[i]->ElementType() == EBSList )
       
   254             {
       
   255             TArray<MBSElement*> listArray = aArray[i]->GetStructureL();
       
   256             if( !VerifyDataL( listArray, ETrue ) )
       
   257                 {
       
   258                 return EFalse;
       
   259                 }
       
   260             }
       
   261 
       
   262         if( aIgnoreEmptyID &&
       
   263             aArray[i]->ElementId().CompareC( KNullDesC8() ) == 0 )
       
   264             {
       
   265             // ignore empty id
       
   266             continue;
       
   267             }
       
   268 
       
   269         // Check that sibling elements have unique ID
       
   270         for( TInt j = i + 1; j < count; j++ )
       
   271             {
       
   272             if( aArray[i]->ElementId().CompareC( aArray[j]->ElementId() ) == 0 )
       
   273                 {
       
   274                 // convert elementID to 16-bit for event logger
       
   275                 TPtrC8 id( aArray[i]->ElementId() );
       
   276                 HBufC* conv = HBufC::NewLC( id.Length() );
       
   277                 TPtr uniptr( conv->Des() );
       
   278                 CnvUtfConverter::ConvertToUnicodeFromUtf8( uniptr, id );
       
   279                 TRACE( T_LIT("ERROR: Found duplicate element ID (%S)!"), &uniptr );
       
   280                 CleanupStack::PopAndDestroy();
       
   281                 return EFalse;
       
   282                 }
       
   283             }
       
   284         }
       
   285 
       
   286     // All elements ok
       
   287     return ETrue;
       
   288     }
       
   289 
       
   290 
       
   291 // ---------------------------------------------------------------------------
       
   292 // CBSStorage::VerifyDataL
       
   293 // ---------------------------------------------------------------------------
       
   294 //
       
   295 TBool CBSStorage::VerifyDataL( RBSObjOwningPtrArray<MBSElement>& aArray,
       
   296                                     TBool aIgnoreEmptyID /* = EFalse */ )
       
   297     {
       
   298     return VerifyDataL( aArray.Array(), aIgnoreEmptyID );
       
   299     }
       
   300 
       
   301 
       
   302 // ---------------------------------------------------------------------------
       
   303 // CBSStorage::ProposeFileNameL
       
   304 // ---------------------------------------------------------------------------
       
   305 //
       
   306 TPtrC CBSStorage::ProposeFileNameL()
       
   307     {
       
   308     if( !iFilename )
       
   309         {
       
   310         User::Leave( KErrNotReady );
       
   311         }
       
   312     return iFilename->Des();
       
   313     }
       
   314 
       
   315 // ---------------------------------------------------------------------------
       
   316 // CBSStorage::ProposeFileNameL
       
   317 // ---------------------------------------------------------------------------
       
   318 //
       
   319 TPtrC CBSStorage::ProposedDirL()
       
   320     {
       
   321     if( !iFilename )
       
   322         {
       
   323         User::Leave( KErrNotReady );
       
   324         }
       
   325     return iDir;
       
   326     }
       
   327 
       
   328 
       
   329 // ---------------------------------------------------------------------------
       
   330 // CBSStorage::GetListOfFiles()
       
   331 // ---------------------------------------------------------------------------
       
   332 //
       
   333 void CBSStorage::GetListOfFilesL( RArray<TPtrC>& aFileList )
       
   334     {
       
   335     TInt count = iElements.Count();
       
   336     for( TInt i = 0; i < count; i++ )
       
   337         {
       
   338         AppendFilesL( iElements[i], aFileList );
       
   339         }
       
   340     }
       
   341 
       
   342 // ---------------------------------------------------------------------------
       
   343 // CBSStorage::AppendFiles()
       
   344 // ---------------------------------------------------------------------------
       
   345 //
       
   346 void CBSStorage::AppendFilesL( MBSElement* aElement, RArray<TPtrC>& aFileList )
       
   347     {
       
   348     if( !aElement )
       
   349         {
       
   350         // invalid element
       
   351         return;
       
   352         }
       
   353 
       
   354     TBSElementType type = aElement->ElementType();
       
   355     if( EBSList == type )
       
   356         {
       
   357         TArray<MBSElement*> list = aElement->GetStructureL();
       
   358 
       
   359         TInt count = list.Count();
       
   360         for( TInt i = 0; i < count; i++ )
       
   361             {
       
   362             AppendFilesL( list[i], aFileList );
       
   363             }
       
   364         }
       
   365     else if( EBSFile == type )
       
   366         {
       
   367         TPtrC ptr( aElement->TextDataL() );
       
   368         aFileList.Append( aElement->TextDataL() );
       
   369         }
       
   370     }
       
   371 
       
   372 // End of file