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