presencecache/presencecacheclient2/src/presencecacheclientnotification.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Implementation for presence cache client.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32std.h>
       
    19 
       
    20 #include <mpresencebuddyinfo2.h>
       
    21 #include <presencecachereadhandler2.h>
       
    22 
       
    23 #include "presencecacheclientnotification.h"
       
    24 #include "presencecachedefs2.h"
       
    25 #include "cacheobjecthelpers.h"
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS =============================
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // CPresenceCacheClientNotification::NewL
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 CPresenceCacheClientNotification* CPresenceCacheClientNotification::NewL(
       
    34     CPresenceCacheClient& aClient)
       
    35     {
       
    36     CPresenceCacheClientNotification* self = new(ELeave)CPresenceCacheClientNotification(aClient);
       
    37     CleanupStack::PushL( self );
       
    38     self->ConstructL();
       
    39     CleanupStack::Pop();
       
    40     return self;    
       
    41     }
       
    42     
       
    43 // ---------------------------------------------------------------------------
       
    44 // CPresenceCacheClientNotification::~CPresenceCacheClientNotification
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 CPresenceCacheClientNotification::~CPresenceCacheClientNotification()
       
    48     {
       
    49     Cancel();
       
    50     }
       
    51     
       
    52 // ---------------------------------------------------------------------------
       
    53 // CPresenceCacheClientNotification::CPresenceCacheClientNotification
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 CPresenceCacheClientNotification::CPresenceCacheClientNotification(CPresenceCacheClient& aClient)
       
    57 : CActive( EPriorityStandard ), iClient( aClient ), iNotificationSubscribed( EFalse )
       
    58     {        
       
    59     }
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // CPresenceCacheClientNotification::ConstructL
       
    63 // ---------------------------------------------------------------------------
       
    64 //
       
    65 void CPresenceCacheClientNotification::ConstructL()
       
    66     {
       
    67     CActiveScheduler::Add( this );   
       
    68     }
       
    69     
       
    70 // ---------------------------------------------------------------------------
       
    71 // CPresenceCacheClientNotification::RunL
       
    72 // ---------------------------------------------------------------------------
       
    73 //
       
    74 void CPresenceCacheClientNotification::RunL()
       
    75     {    
       
    76     TInt origStatus = iStatus.Int();
       
    77     
       
    78     MPresenceCacheReadHandler2* client = ClientNotifyHandler();    
       
    79     
       
    80     if ( origStatus )
       
    81         {
       
    82         iNotificationSubscribed = EFalse;        
       
    83         TRAP_IGNORE( client->HandlePresenceNotificationL( origStatus, NULL ));
       
    84         }
       
    85     else
       
    86         {
       
    87         StartNotifySubscription();        
       
    88         PreseneceFromServerToClientL( *client, iSizePckg() );
       
    89         }              
       
    90     }
       
    91 // ----------------------------------------------------------
       
    92 // CPresenceCacheClientNotification::RunError
       
    93 // ----------------------------------------------------------
       
    94 //
       
    95 TInt CPresenceCacheClientNotification::RunError( TInt aError )
       
    96     {         
       
    97     if ( KErrServerTerminated == aError )
       
    98         {
       
    99         // When server is terminated we notify the clienrt about that
       
   100         iNotificationSubscribed = EFalse;         
       
   101         MPresenceCacheReadHandler2* client = ClientNotifyHandler();
       
   102         TRAP_IGNORE( client->HandlePresenceNotificationL( aError, NULL ));        
       
   103         }
       
   104     else
       
   105         {
       
   106         // StartNotifySubscription is already called.
       
   107         }
       
   108     return KErrNone;    
       
   109     } 
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // CPresenceCacheClientNotification::DoCancel
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 void CPresenceCacheClientNotification::DoCancel()
       
   116     {
       
   117     iClient.SendReceive( NRequest::ECancelWaitingForNotification );
       
   118     }
       
   119 
       
   120 // ---------------------------------------------------------------------------
       
   121 // CPresenceCacheClientNotification::SubscribePresenceBuddyChangeL
       
   122 // ---------------------------------------------------------------------------
       
   123 //    
       
   124 TInt CPresenceCacheClientNotification::SubscribePresenceBuddyChangeL(
       
   125 	const TDesC& aIdentity)
       
   126     {       
       
   127     if ( !iNotificationSubscribed )
       
   128         {
       
   129         StartNotifySubscription();
       
   130         }
       
   131                    
       
   132     // Package message arguments before sending to the server
       
   133     TIpcArgs args;
       
   134     args.Set(1, &aIdentity);  
       
   135         
       
   136     TInt err = iClient.SendReceive( NRequest::ESubscribeBuddyPresenceChange, args );    
       
   137         
       
   138     return err;
       
   139     }
       
   140 
       
   141 // ---------------------------------------------------------------------------
       
   142 // CPresenceCacheClientNotification::UnsubscribePresenceBuddyChangeL
       
   143 // ---------------------------------------------------------------------------
       
   144 //    
       
   145 void CPresenceCacheClientNotification::UnsubscribePresenceBuddyChangeL(
       
   146 	const TDesC& aIdentity)
       
   147     {
       
   148     // Package message arguments before sending to the server
       
   149     TIpcArgs args;
       
   150     args.Set(1, &aIdentity);  
       
   151         
       
   152     TInt err = iClient.SendReceive( NRequest::EUnSubscribeBuddyPresenceChange, args );
       
   153     User::LeaveIfError( err );
       
   154     }
       
   155  
       
   156 // ---------------------------------------------------------------------------
       
   157 // CPresenceCacheClientNotification::StartNotifySubscriptionL
       
   158 // ---------------------------------------------------------------------------
       
   159 //    
       
   160 void CPresenceCacheClientNotification::StartNotifySubscription()
       
   161     {                                    
       
   162     // Package message arguments before sending to the server
       
   163     SetActive();
       
   164     TIpcArgs args(&iSizePckg);            
       
   165     iClient.SendReceive( NRequest::EWaitingForNotification, args, iStatus );
       
   166     iNotificationSubscribed = ETrue;
       
   167     }
       
   168 
       
   169 // ---------------------------------------------------------------------------
       
   170 // CPresenceCacheClientNotification::ClientNotifyHandler
       
   171 // ---------------------------------------------------------------------------
       
   172 //    
       
   173 MPresenceCacheReadHandler2* CPresenceCacheClientNotification::ClientNotifyHandler()
       
   174     {
       
   175 	return iClient.iNotifyClient;
       
   176     }
       
   177 
       
   178 // ---------------------------------------------------------------------------
       
   179 // CPresenceCacheClientNotification::PreseneceFromServerToClientL
       
   180 // ---------------------------------------------------------------------------
       
   181 //    
       
   182 void CPresenceCacheClientNotification::PreseneceFromServerToClientL( 
       
   183     MPresenceCacheReadHandler2& aClient, TInt aSize )
       
   184     {     
       
   185     MPresenceBuddyInfo2* buddyInfo = MPresenceBuddyInfo2::NewLC();     
       
   186     HBufC8* presInfoDes = HBufC8::NewLC( aSize );
       
   187     TPtr8 ptrBuf( presInfoDes->Des() );
       
   188     TInt err = iClient.SendReceive( NRequest::EGetLastNotifiedtPacket, TIpcArgs( &ptrBuf ));    
       
   189     if ( !err )
       
   190         {
       
   191         TCacheObjectPacker< MPresenceBuddyInfo2 >::UnPackL( *buddyInfo, *presInfoDes );        
       
   192         CleanupStack::PopAndDestroy( presInfoDes );                          
       
   193         CleanupStack::Pop( ); //  buddyInfo
       
   194         aClient.HandlePresenceNotificationL( KErrNone, buddyInfo );
       
   195         }
       
   196     else
       
   197         {
       
   198         CleanupStack::PopAndDestroy( presInfoDes );         
       
   199         CleanupStack::PopAndDestroy( buddyInfo );          
       
   200         }
       
   201     }
       
   202 
       
   203 // end of file