imservices/searchfeatureplugin/srcsearchdatamodel/searcheventcodec.cpp
changeset 0 e6b17d312c8b
equal deleted inserted replaced
-1:000000000000 0:e6b17d312c8b
       
     1 /*
       
     2 * Copyright (c) 2007 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:  Base class for API implementations.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <s32strm.h>
       
    19 #include <s32mem.h>
       
    20 #include "searcheventcodec.h"
       
    21 #include "searcheventimp.h"
       
    22 #include "ximpapieventbase.h"
       
    23 #include "searchkeyseventimp.h"
       
    24 
       
    25 /**
       
    26  * Type definition for exact API event constructor signature.
       
    27  */
       
    28 typedef CXIMPApiEventBase* (*TApiEventConstructor)( RReadStream& );
       
    29 
       
    30 /**
       
    31  * Structure for mapping event interface IDs to
       
    32  * to corect API event constructors.
       
    33  */
       
    34 struct SApiEventConstructorMap
       
    35     {
       
    36     //Interface id
       
    37     TInt32    iInterfaceId;
       
    38 
       
    39     //Function pointer to event interface implementation
       
    40     TApiEventConstructor iConstructorPtr;
       
    41     } ;
       
    42 
       
    43 /**
       
    44  * Helper macro to initialise KApiEventConstructorTable members.
       
    45  *
       
    46  * Macro forces that each event implementation class to have static
       
    47  * NewFromStreamLC() memeber function to instantiate the object.
       
    48  *
       
    49  * See TApiEventConstructor type definition for exact constructor
       
    50  * signature.
       
    51  */
       
    52 #define CONSTRUCTOR_ENTRY( TheImplementedIf, TheClass ) \
       
    53     { TheImplementedIf::KInterfaceId, TheClass::NewFromStreamLC } \
       
    54 
       
    55 
       
    56 /**
       
    57  * Constructor function mapping for event interface implementations.
       
    58  *
       
    59  * When new event types are implemented, add them here.
       
    60  */
       
    61 
       
    62 const SApiEventConstructorMap KApiEventConstructorTable[] =
       
    63     {
       
    64 		CONSTRUCTOR_ENTRY( MSearchEvent, CSearchEventImp ),
       
    65 		CONSTRUCTOR_ENTRY( MSearchKeysEvent, CSearchKeysEventImp )
       
    66     };
       
    67 
       
    68 
       
    69 /**
       
    70  * Count of constructor mappings.
       
    71  */
       
    72 const TInt KApiEventConstructorCount = sizeof( KApiEventConstructorTable )
       
    73                                         / sizeof( SApiEventConstructorMap );
       
    74 
       
    75 
       
    76 
       
    77 // ============================ HELPER FUNCTIONS =============================
       
    78 
       
    79 
       
    80 /**
       
    81  * Helper function to locate constructor function for
       
    82  * event interface ID.
       
    83  *
       
    84  * @param aEventInterfaceId The event interface ID.
       
    85  * @return Event object constructor function.
       
    86  */
       
    87 TApiEventConstructor ConstructorForInterface( TInt32 aEventInterfaceId )
       
    88     {
       
    89     //Locate correct constructor
       
    90     for( TInt ix = 0; ix < KApiEventConstructorCount; ix++ )
       
    91         {
       
    92      const SApiEventConstructorMap map = KApiEventConstructorTable[ ix ];
       
    93         if( map.iInterfaceId == aEventInterfaceId )
       
    94             {
       
    95             return map.iConstructorPtr;
       
    96             }  
       
    97         }
       
    98     return NULL;
       
    99     }
       
   100 
       
   101 
       
   102 /**
       
   103  * Helper function to instantiate new event object
       
   104  * of requested type and construct it from the stream.
       
   105  *
       
   106  * @param aEventInterfaceId
       
   107  * @return Event object constructor function.
       
   108  */
       
   109 CXIMPApiEventBase* NewEventObjectFromStreamLC( TInt32 aEventInterfaceId,
       
   110                                                RReadStream& aStream )
       
   111     {
       
   112     TApiEventConstructor newFromStreamLC = NULL;
       
   113 
       
   114     //Locate correct constructor for interface ID
       
   115     newFromStreamLC = ConstructorForInterface( aEventInterfaceId );
       
   116 
       
   117     if( ! newFromStreamLC )
       
   118 	    {
       
   119 	    return 	NULL;
       
   120 	    }
       
   121     //Instantiate the object
       
   122     CXIMPApiEventBase* eventObject = newFromStreamLC( aStream );
       
   123 
       
   124     return eventObject;    
       
   125     }
       
   126 
       
   127 
       
   128 // ============================ MEMBER FUNCTIONS =============================
       
   129 
       
   130 
       
   131 // ---------------------------------------------------------------------------
       
   132 // CSearchEventCodec::NewL()
       
   133 // ---------------------------------------------------------------------------
       
   134 //
       
   135 CSearchEventCodec* CSearchEventCodec::NewL()
       
   136     {
       
   137     CSearchEventCodec* self = new( ELeave ) CSearchEventCodec() ;
       
   138     CleanupStack::PushL( self );
       
   139     self->ConstructL();
       
   140     CleanupStack::Pop( self );
       
   141     return self;
       
   142     }
       
   143 
       
   144 
       
   145 // ---------------------------------------------------------------------------
       
   146 // CSearchEventCodec::ConstructL()
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 void CSearchEventCodec::ConstructL()
       
   150     {
       
   151     }
       
   152 // ---------------------------------------------------------------------------
       
   153 // CSearchEventCodec::CSearchEventCodec()
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 CSearchEventCodec::CSearchEventCodec()
       
   157     {
       
   158     }
       
   159 
       
   160 // ---------------------------------------------------------------------------
       
   161 // CSearchEventCodec::~CSearchEventCodec()
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 CSearchEventCodec::~CSearchEventCodec()
       
   165     {
       
   166     }
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // CSearchEventCodec::PackL()
       
   170 // ---------------------------------------------------------------------------
       
   171 //
       
   172 HBufC8* CSearchEventCodec::PackL( CXIMPApiEventBase& aEventObj,
       
   173                                         TInt32& aTypeOfEvent )
       
   174     {
       
   175     CBufFlat* buffer = CBufFlat::NewL( 10 ); // initial granularity to 10
       
   176     CleanupStack::PushL( buffer );
       
   177 
       
   178     RBufWriteStream ws;
       
   179     CleanupClosePushL( ws );
       
   180     ws.Open( *buffer ); // CSI: 65 #
       
   181 
       
   182     //Ask real event type through the event base interface
       
   183     TInt32 eventIfId = aEventObj.Base().GetInterfaceId();
       
   184 
       
   185     //And write both event type and data
       
   186     ws.WriteInt32L( eventIfId );
       
   187     aEventObj.ExternalizeL( ws );
       
   188     ws.CommitL();
       
   189     CleanupStack::PopAndDestroy();  //ws
       
   190 
       
   191     HBufC8* heapBuf = buffer->Ptr( 0 ).AllocL();
       
   192 
       
   193     CleanupStack::PopAndDestroy( buffer );
       
   194 
       
   195 
       
   196     aTypeOfEvent = eventIfId;
       
   197     return heapBuf;
       
   198     }
       
   199 
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // CSearchEventCodec::PackL()
       
   203 // ---------------------------------------------------------------------------
       
   204 //
       
   205 void CSearchEventCodec::PackL( CXIMPApiEventBase& aEventObj,
       
   206                                      TInt32& aTypeOfEvent,
       
   207                                      CBufFlat* aBuffer )
       
   208     {
       
   209     RBufWriteStream ws;
       
   210     CleanupClosePushL( ws );
       
   211     ws.Open( *aBuffer ); // CSI: 65 #
       
   212 
       
   213     //Ask real event type through the event base interface
       
   214     TInt32 eventIfId = aEventObj.Base().GetInterfaceId();
       
   215 
       
   216     //And write both event type and data
       
   217     ws.WriteInt32L( eventIfId );
       
   218     aEventObj.ExternalizeL( ws );
       
   219     ws.CommitL();
       
   220     CleanupStack::PopAndDestroy( );  //ws
       
   221     
       
   222     aTypeOfEvent = eventIfId;
       
   223     }
       
   224 
       
   225 
       
   226 // ---------------------------------------------------------------------------
       
   227 // CSearchEventCodec::UnPackL()
       
   228 // ---------------------------------------------------------------------------
       
   229 //
       
   230 CXIMPApiEventBase* CSearchEventCodec::UnPackL( const TDesC8& aEventData,
       
   231                                                      TInt32& aTypeOfEvent )
       
   232     {
       
   233     RDesReadStream rs;
       
   234     rs.Open( aEventData ); // CSI: 65 #
       
   235     CleanupClosePushL( rs );
       
   236 
       
   237     TInt32 eventIfId = rs.ReadInt32L();
       
   238     CXIMPApiEventBase* eventObject = NewEventObjectFromStreamLC( eventIfId, rs );
       
   239     if( eventObject )
       
   240 	    {
       
   241 	    CleanupStack::Pop( eventObject ); //eventObject	
       
   242 	    }
       
   243     
       
   244     CleanupStack::PopAndDestroy(  ); // rs
       
   245 
       
   246 
       
   247     aTypeOfEvent = eventIfId;
       
   248     return eventObject;
       
   249     }
       
   250 
       
   251 
       
   252 // End of file
       
   253