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