landmarks/locationlandmarks/clientlib/src/epos_poslandmarkserialization.cpp
changeset 0 667063e416a2
child 8 6fcbaa43369c
equal deleted inserted replaced
-1:000000000000 0:667063e416a2
       
     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:This class contains helper methods for landmark serialization.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <lbsposition.h>
       
    20 
       
    21 #include "EPos_CPosLandmark.h"
       
    22 #include "epos_cposreadbufstorage.h"
       
    23 #include "epos_cposwritebufstorage.h"
       
    24 #include "epos_poslandmarkserialization.h"
       
    25 
       
    26 const TInt KVersion = 0x0100; // 1.0
       
    27 
       
    28 // Keeps all landmark static-size info
       
    29 struct TLandmarkBufferHeader
       
    30     {
       
    31     TInt            iSize;
       
    32     TInt            iVersion;
       
    33 
       
    34     TPosLmItemId    iId;
       
    35     TBool           iIsPartial;
       
    36 
       
    37     TBool           iIsIconSet;
       
    38     TInt            iIconIndex;
       
    39     TInt            iIconMaskIndex;
       
    40 
       
    41     TBool           iIsRadiusSet;
       
    42     TReal32         iCoverageRadius;
       
    43 
       
    44     TBool           iIsPositionSet;
       
    45     TLocality       iPosition;
       
    46 
       
    47     TInt            iCategoriesNum;
       
    48     TInt            iPositionFieldsNum;
       
    49     };
       
    50 
       
    51 // ======== MEMBER FUNCTIONS ========
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 EXPORT_C HBufC8* PosLandmarkSerialization::PackL( const CPosLandmark& aLandmark )
       
    57     {
       
    58     TLandmarkBufferHeader header;
       
    59     memclr( &header, sizeof header );
       
    60     TPckg<TLandmarkBufferHeader> headerPack( header );
       
    61     TPtrC name, desc, icon;
       
    62 
       
    63     aLandmark.GetLandmarkName( name );
       
    64     aLandmark.GetLandmarkDescription( desc );
       
    65     header.iIsIconSet = ( KErrNone == 
       
    66         aLandmark.GetIcon( icon, header.iIconIndex, header.iIconMaskIndex ) );
       
    67 
       
    68     // calculate required memory
       
    69 
       
    70     TInt size = CPosWriteBufStorage::PackedSize( headerPack ) + 
       
    71                 CPosWriteBufStorage::PackedSize( name ) + 
       
    72                 CPosWriteBufStorage::PackedSize( desc ) + 
       
    73                 CPosWriteBufStorage::PackedSize( icon );
       
    74 
       
    75     RArray<TPosLmItemId> categories;
       
    76     CleanupClosePushL( categories );
       
    77     aLandmark.GetCategoriesL( categories );
       
    78 
       
    79     size += categories.Count() * sizeof( TPosLmItemId );
       
    80 
       
    81     TPositionFieldId field = aLandmark.FirstPositionFieldId();
       
    82     while ( field != EPositionFieldNone )
       
    83         {
       
    84         TPtrC fieldData;
       
    85         aLandmark.GetPositionField( field, fieldData );
       
    86         field = aLandmark.NextPositionFieldId( field );
       
    87         
       
    88         size += sizeof( TPositionFieldId ) + CPosWriteBufStorage::PackedSize( fieldData );
       
    89         }
       
    90 
       
    91     // collect static-size data
       
    92     header.iSize = size;
       
    93     header.iVersion = KVersion;
       
    94     header.iId = aLandmark.LandmarkId();
       
    95     header.iIsPartial = aLandmark.IsPartial();
       
    96 
       
    97     header.iIsRadiusSet = ( KErrNone == aLandmark.GetCoverageRadius( header.iCoverageRadius ) );
       
    98     header.iIsPositionSet = ( KErrNone == aLandmark.GetPosition( header.iPosition ) );
       
    99 
       
   100     header.iCategoriesNum = categories.Count();
       
   101     header.iPositionFieldsNum = aLandmark.NumOfAvailablePositionFields();
       
   102 
       
   103     // Put data
       
   104     CPosWriteBufStorage* buf = CPosWriteBufStorage::NewLC( size );
       
   105 
       
   106     buf->Put( headerPack );
       
   107     buf->Put( name );
       
   108     buf->Put( desc );
       
   109     buf->Put( icon );
       
   110 
       
   111     // categories
       
   112     for ( TInt i = 0; i < categories.Count(); i++ )
       
   113         {
       
   114         TPosLmItemId catId = categories[i];
       
   115         buf->Put( catId );
       
   116         }
       
   117 
       
   118     // position fields
       
   119     field = aLandmark.FirstPositionFieldId();
       
   120     while ( field != EPositionFieldNone )
       
   121         {
       
   122         TPtrC fieldData;
       
   123         aLandmark.GetPositionField( field, fieldData );
       
   124 
       
   125         buf->Put( field );        
       
   126         buf->Put( fieldData );
       
   127 
       
   128         field = aLandmark.NextPositionFieldId( field );
       
   129         }
       
   130 
       
   131     HBufC8* buffer = buf->FinalizeL();
       
   132 
       
   133     // Cleanup
       
   134     CleanupStack::PopAndDestroy( buf );
       
   135     CleanupStack::PopAndDestroy( &categories );
       
   136     return buffer;
       
   137     }
       
   138 
       
   139 // ---------------------------------------------------------------------------
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 EXPORT_C CPosLandmark* PosLandmarkSerialization::UnpackL( const TDesC8& aBuffer )
       
   143     {
       
   144     TPckgBuf<TLandmarkBufferHeader> header;
       
   145 
       
   146     CPosLandmark* lm = CPosLandmark::NewLC();
       
   147     CPosReadBufStorage* buf = CPosReadBufStorage::NewLC( aBuffer );
       
   148     
       
   149     buf->GetL( header );
       
   150 
       
   151     if ( header().iSize != aBuffer.Size() ||
       
   152          header().iVersion != KVersion ||
       
   153          header().iCategoriesNum < 0 ||
       
   154          header().iPositionFieldsNum < 0 )
       
   155         {
       
   156         User::Leave( KErrCorrupt );
       
   157         }
       
   158 
       
   159     lm->SetLandmarkIdL( header().iId );
       
   160     lm->SetPartialL( header().iIsPartial );
       
   161 
       
   162     if ( header().iIsRadiusSet )
       
   163         {
       
   164         lm->SetCoverageRadius( header().iCoverageRadius );
       
   165         }
       
   166     
       
   167     if ( header().iIsPositionSet )
       
   168         {
       
   169         lm->SetPositionL( header().iPosition );
       
   170         }
       
   171 
       
   172     // strings
       
   173 
       
   174     TPtrC name, desc, icon;
       
   175     buf->GetL( name );
       
   176     buf->GetL( desc );
       
   177     buf->GetL( icon );
       
   178 
       
   179     lm->SetLandmarkNameL( name );
       
   180     lm->SetLandmarkDescriptionL( desc );
       
   181 
       
   182     if ( header().iIsIconSet )
       
   183         {
       
   184         lm->SetIconL( icon, header().iIconIndex, header().iIconMaskIndex );
       
   185         }
       
   186 
       
   187     // categories
       
   188     TPosLmItemId id = KPosLmNullItemId;
       
   189     for ( TInt i = 0; i < header().iCategoriesNum; i++ )
       
   190         {
       
   191         buf->GetL( id );
       
   192         lm->AddCategoryL( id );
       
   193         }
       
   194 
       
   195     // position fields
       
   196     TPtrC field;
       
   197     TPositionFieldId fieldId = EPositionFieldNone;
       
   198     for ( TInt i = 0; i < header().iPositionFieldsNum; i++ )
       
   199         {
       
   200         buf->GetL( fieldId );
       
   201         buf->GetL( field );
       
   202         lm->SetPositionFieldL( fieldId, field );
       
   203         }
       
   204         
       
   205     // cleanup
       
   206     CleanupStack::PopAndDestroy( buf ); 
       
   207     CleanupStack::Pop( lm ); 
       
   208     return lm;
       
   209     }