uiservicetab/vimpstengine/src/cvimpstenginepresencesubservice.cpp
branchRCL_3
changeset 28 3104fc151679
parent 27 2b7283837edb
child 29 9a48e301e94b
equal deleted inserted replaced
27:2b7283837edb 28:3104fc151679
     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:  Handles the use of Precense Framework
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 /*#include <ximpclient.h>*/
       
    21 #include <ximpcontext.h>
       
    22 #include <ximpobjectfactory.h>
       
    23 #include <presenceobjectfactory.h>
       
    24 #include <ximpidentity.h> //for MXIMPIdentity
       
    25 #include <presentitygroups.h>
       
    26 #include <presencewatching.h> //for MximpPresenceWatching
       
    27 #include <ximpstatus.h> //Presence info test
       
    28 #include <presentitygroupcontentevent.h>
       
    29 #include <presentitygroupmemberinfo.h>
       
    30 #include <presenceauthorization.h>
       
    31 #include <presentitypresenceevent.h>
       
    32 #include <presencegrantrequestlistevent.h> // Grant request list
       
    33 #include <presencegrantrequestinfo.h>
       
    34 #include <ximpcontextstateevent.h>
       
    35 #include <ximprequestcompleteevent.h>
       
    36 #include <presenceblocklistevent.h>
       
    37 #include <presenceblockinfo.h>
       
    38 
       
    39 
       
    40 #include <presencefeatures.h>
       
    41 #include <presenceinfofilter.h> //info filtter
       
    42 #include <presenceinfofield.h> //MximpPresenceInfoField
       
    43 #include <presenceinfofieldcollection.h> //MximpPresenceInfoFieldCollection
       
    44 #include <presenceinfofieldvaluetext.h> //MximpPresenceInfoFieldValueText
       
    45 #include <presenceinfofieldvalueenum.h>
       
    46 #include <personpresenceinfo.h> // MximpPersonPresenceInfo
       
    47 #include <presencepublishing.h>//MximpPresencePublishing
       
    48 #include <ximperrors.hrh> //ximp errors
       
    49 #include <presenceinfofieldvaluebinary.h>
       
    50 //presence cache headers 
       
    51 #include <presencecachereader2.h> // cache reader
       
    52 
       
    53 
       
    54 #include "cvimpstenginepresencesubservice.h"
       
    55 #include "cvimpststoragemanagerfactory.h"
       
    56 #include "mvimpststoragecontact.h"
       
    57 #include "mvimpststoragecontactlist.h"
       
    58 #include "cvimpstenginerequestmapper.h"
       
    59 #include "cvimpstenginecchhandler.h"
       
    60 #include "mvimpststorageserviceview.h"
       
    61 #include "tvimpstconsts.h"
       
    62 #include "cvimpstengineservicetablefetcher.h"
       
    63 #include "cvimpstenginecchhandler.h"
       
    64 #include "cvimpstenginesessioncntxtobserver.h"
       
    65 #include "vimpstutilsnotemapper.h"
       
    66 #include "vimpstallerrors.h"
       
    67 #include "cvimpstenginerequest.h"
       
    68 #include "cvimpstblockedlistmanager.h"
       
    69 #include "mvimpstengineblockedlistfetcheventobserver.h"
       
    70 
       
    71 #include   <ximpfeatureinfo.h>
       
    72 #include <avabilitytext.h>
       
    73 
       
    74 //Presence Observer
       
    75 #include "mvimpstenginepresencesubserviceeventobserver.h"
       
    76 #include "uiservicetabtracer.h"
       
    77 #include "vimpstcustomcleanupapi.h" //For customized cleanup function
       
    78 #include "mvimpstengineserviceconnectioneventobserver.h"
       
    79 
       
    80 // CONTANTS
       
    81 const TInt KUriMaxLength = 255; 
       
    82 _LIT( KListNameAllBuddy ,"buddylist" );
       
    83 const TInt KCollationLevel = 1;
       
    84 
       
    85 // Compares alphabetically using MVIMPSTStorageContact::Identification and 
       
    86 // TDesC::CompareC
       
    87 TInt CompareAlphabetically( const TPtrC& aFirst, 
       
    88                             const TPtrC& aSecond )
       
    89     {
       
    90     return aFirst.CompareC( aSecond, KCollationLevel, NULL );
       
    91     }
       
    92 
       
    93 
       
    94 // ================= MEMBER FUNCTIONS =======================
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // CVIMPSTEnginePresenceSubService::
       
    98 //      CVIMPSTEnginePresenceSubService()
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 CVIMPSTEnginePresenceSubService::CVIMPSTEnginePresenceSubService( TUint32 aServiceId, 
       
   102         CVIMPSTEngineCchHandler& aCchHandler,
       
   103         CVIMPSTEngineServiceTableFetcher& aTableFetcher,
       
   104         CVIMPSTEngineSessionCntxtObserver& aXimpEventObserver,
       
   105         MVIMPSTEngineServiceConnectionEventObserver& aObserver ) 
       
   106 :iServiceId( aServiceId ),
       
   107 iCchHandler(aCchHandler),
       
   108 iSettingsTableFetcher(aTableFetcher),
       
   109 iXimpEventObserver(aXimpEventObserver),
       
   110     iObserver(aObserver),
       
   111     iChangeStatusSupported ( ETrue ),
       
   112     iChangeStatusMsgSupported( ETrue ),
       
   113     iAvatarSupported( EFalse ),
       
   114     iIsClearingAvatar(EFalse)
       
   115         {
       
   116 	TRACER_AUTO;
       
   117         }
       
   118 
       
   119 // ---------------------------------------------------------------------------
       
   120 // CVIMPSTEnginePresenceSubService::ConstructL()
       
   121 // ---------------------------------------------------------------------------
       
   122 //
       
   123 void CVIMPSTEnginePresenceSubService::ConstructL()
       
   124     {
       
   125 	TRACER_AUTO;
       
   126 
       
   127     iCchHandler.RegisterCchObserverL(this,ECCHPresenceSub);
       
   128 
       
   129     TCCHSubserviceState serviceState = ECCHUninitialized;    
       
   130     TInt error = iCchHandler.GetServiceState( 
       
   131             iServiceId, ECCHPresenceSub, serviceState );
       
   132 
       
   133     iServiceState = ResolveServiceStateL(serviceState, error);   
       
   134     //initialize the presence cache.
       
   135     iPresenceCacheReader = MPresenceCacheReader2::CreateReaderL();                                                        
       
   136 
       
   137     iPresenceCacheReader->SetObserverForSubscribedNotifications(this);
       
   138     iServiceName = HBufC::NewL( KVIMPSTUISPSMaxPropertyLength );
       
   139     TPtr serviceNamePtr( iServiceName->Des() );    
       
   140     iSettingsTableFetcher.GetServiceNameL(iServiceId, serviceNamePtr);      
       
   141     iBlockedListMgr = CVIMPSTBlockedListManager::NewL();
       
   142     iBlockListFetchReqPending = EFalse; //req of fetching blocked list has been completed.
       
   143     
       
   144 	iLogoutRequest = EFalse;
       
   145 	iSubscribeToAuthList = EFalse;
       
   146 	iAutoAccept = EFalse;
       
   147 
       
   148     }
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 // CVIMPSTEnginePresenceSubService::NewL()
       
   152 // ---------------------------------------------------------------------------
       
   153 //
       
   154 CVIMPSTEnginePresenceSubService* 
       
   155 CVIMPSTEnginePresenceSubService::NewL( TUint32 aServiceId, 
       
   156         CVIMPSTEngineCchHandler& aCchHandler,
       
   157         CVIMPSTEngineServiceTableFetcher& aTableFetcher,
       
   158         CVIMPSTEngineSessionCntxtObserver& aXimpEventObserver,
       
   159         MVIMPSTEngineServiceConnectionEventObserver& aObserver )
       
   160     {
       
   161 	TRACER_AUTO;
       
   162     CVIMPSTEnginePresenceSubService* self = NewLC( aServiceId,aCchHandler, aTableFetcher, 
       
   163             aXimpEventObserver,aObserver );
       
   164     CleanupStack::Pop(self);
       
   165     return self;
       
   166     }
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // CVIMPSTEnginePresenceSubService::NewLC()
       
   170 // ---------------------------------------------------------------------------
       
   171 //
       
   172 CVIMPSTEnginePresenceSubService* 
       
   173 CVIMPSTEnginePresenceSubService::NewLC( TUint32 aServiceId, 
       
   174         CVIMPSTEngineCchHandler& aCchHandler,
       
   175         CVIMPSTEngineServiceTableFetcher& aTableFetcher,
       
   176         CVIMPSTEngineSessionCntxtObserver& aXimpEventObserver,
       
   177         MVIMPSTEngineServiceConnectionEventObserver& aObserver )
       
   178     {
       
   179 	TRACER_AUTO;
       
   180     CVIMPSTEnginePresenceSubService* self =
       
   181     new (ELeave) CVIMPSTEnginePresenceSubService( aServiceId,aCchHandler, aTableFetcher,  aXimpEventObserver,aObserver);
       
   182     CleanupStack::PushL(self);
       
   183     self->ConstructL();
       
   184     return self;
       
   185     }
       
   186 
       
   187 // ---------------------------------------------------------------------------
       
   188 // CVIMPSTEnginePresenceSubService::
       
   189 //      ~CVIMPSTEnginePresenceSubService()
       
   190 // ---------------------------------------------------------------------------
       
   191 //
       
   192 CVIMPSTEnginePresenceSubService::~CVIMPSTEnginePresenceSubService()
       
   193     {
       
   194 	TRACER_AUTO;
       
   195 	
       
   196 	iCchHandler.UnRegisterCchObserver(ECCHPresenceSub);
       
   197     
       
   198     delete iServiceName;
       
   199     delete iPresenceCacheReader ; 
       
   200     
       
   201     delete iBlockedListMgr;
       
   202     iBlockedListMgr = NULL;
       
   203        
       
   204     }
       
   205 
       
   206 // ---------------------------------------------------------------------------
       
   207 // CVIMPSTEnginePresenceSubService::
       
   208 //      ServiceState()
       
   209 // ---------------------------------------------------------------------------
       
   210 //
       
   211 
       
   212 TVIMPSTEnums::TVIMPSTRegistrationState CVIMPSTEnginePresenceSubService::SubServiceState() const
       
   213 	{
       
   214 	TRACER_AUTO;
       
   215 	return iServiceState;
       
   216 	}
       
   217 
       
   218 // ---------------------------------------------------------------------------
       
   219 // CVIMPSTEnginePresenceSubService::
       
   220 //      Type()
       
   221 // ---------------------------------------------------------------------------
       
   222 //
       
   223 TVIMPSTEnums::SubServiceType CVIMPSTEnginePresenceSubService::Type() const
       
   224 	{
       
   225 	TRACER_AUTO;
       
   226 	TRACE( "CVIMPSTEnginePresenceSubService: [0x%x]", this );
       
   227 	return TVIMPSTEnums::EPresence;
       
   228 	}
       
   229 
       
   230 // ---------------------------------------------------------------------------
       
   231 // CVIMPSTEnginePresenceSubService::
       
   232 //      Enabled()
       
   233 // ---------------------------------------------------------------------------
       
   234 //
       
   235 
       
   236 TBool CVIMPSTEnginePresenceSubService::Enabled()
       
   237     {
       
   238 	TRACER_AUTO;
       
   239     TBool ret = EFalse;
       
   240     if( TVIMPSTEnums::ESVCERegistered == iServiceState)
       
   241         {
       
   242         ret = ETrue;
       
   243         }
       
   244     return ret;    
       
   245     }
       
   246 
       
   247 // ---------------------------------------------------------------------------
       
   248 // CVIMPSTEnginePresenceSubService::RetrieveSubscribedListL
       
   249 // ---------------------------------------------------------------------------
       
   250 //
       
   251 void CVIMPSTEnginePresenceSubService::RetrieveSubscribedListL()
       
   252     {
       
   253 	TRACER_AUTO;
       
   254 
       
   255     //Create group id
       
   256     TBuf<KUriMaxLength> buffer( KListNameAllBuddy );
       
   257     MXIMPIdentity* groupList = iXimpEventObserver.XimpPresenceContextL().ObjectFactory().NewIdentityLC();
       
   258      __ASSERT_ALWAYS( groupList , User::Leave( KErrNoMemory ) );		
       
   259     groupList->SetIdentityL( buffer );
       
   260     iIsFetchingContact = ETrue;
       
   261     //Subscribe buddy list
       
   262     // do get subscribe list"));
       
   263     TXIMPRequestId operationId = TXIMPRequestId::Null();
       
   264     operationId = iXimpEventObserver.XimpPresentityGroupsL().SubscribePresentityGroupContentL( 
       
   265             *groupList );
       
   266     CVIMPSTEngineRequestMapper* requestMapper =iXimpEventObserver.GetRequestMapper();
       
   267     requestMapper->CreateRequestL(operationId,EFalse,EVIMPSTXimpOperationGetSubscribedList);
       
   268     CleanupStack::PopAndDestroy(); // groupList
       
   269     // list retrieving ok. Waiting for list.;
       
   270     }
       
   271 
       
   272 // ---------------------------------------------------------------------------
       
   273 // CVIMPSTEnginePresenceSubService::SubscribePresenceOfSingleContactL
       
   274 // ---------------------------------------------------------------------------
       
   275 //
       
   276 void CVIMPSTEnginePresenceSubService::SubscribePresenceOfSingleContactL( const TDesC& aUriOfTheContact)
       
   277     {
       
   278 	TRACER_AUTO;
       
   279      __ASSERT_ALWAYS( aUriOfTheContact.Length(), User::Leave( KErrArgument ) );		
       
   280      //if anything is there with colon eg sip:user@presence1. strip the part before :
       
   281     TInt len = aUriOfTheContact.Find(_L(":"));
       
   282     TPtrC buddyId = aUriOfTheContact.Right( aUriOfTheContact.Length() - len - KColon().Length());
       
   283     HBufC* name = HBufC::NewLC( KPropertyMaxLength );
       
   284     TPtr namePtr( name->Des() );
       
   285     namePtr.Zero();
       
   286 
       
   287     // append the service name followed by user id ,seperated by colon
       
   288     namePtr.Append(*iServiceName);
       
   289     namePtr.Append(KColon);
       
   290     namePtr.Append(buddyId);
       
   291     
       
   292     TRACE("SubscribeToPresenceCacheL: %S", &namePtr );
       
   293     iPresenceCacheReader->SubscribePresenceBuddyChangeL(*name);
       
   294     CleanupStack::PopAndDestroy(name); //  name    
       
   295     }
       
   296 
       
   297 // ---------------------------------------------------------------------------
       
   298 // CVIMPSTEnginePresenceSubService::
       
   299 //      UnsubscribePrecenseOfSingleContactL
       
   300 // ---------------------------------------------------------------------------
       
   301 //
       
   302 void CVIMPSTEnginePresenceSubService::UnSubscribePresenceOfSingleContactL(
       
   303         const TDesC& aUriOfTheContact )
       
   304     {
       
   305 	TRACER_AUTO;
       
   306     HBufC* name = HBufC::NewLC( KPropertyMaxLength );
       
   307     TPtr namePtr( name->Des() );
       
   308     namePtr.Zero();
       
   309     // append the service name followed by user id ,seperated by colon
       
   310     namePtr.Append(*iServiceName);
       
   311     namePtr.Append(KColon);
       
   312     namePtr.Append(aUriOfTheContact);
       
   313     
       
   314     TRACE( "UnSubscribeToPresenceCacheL: %S", &namePtr);
       
   315     iPresenceCacheReader->UnSubscribePresenceBuddyChangeL(*name);
       
   316     CleanupStack::PopAndDestroy(name); //  name                  
       
   317     }
       
   318 
       
   319 // ---------------------------------------------------------------------------
       
   320 // CVIMPSTEnginePresenceSubService::
       
   321 //      DoHandlePresentityGroupContentEventL
       
   322 // ---------------------------------------------------------------------------
       
   323 //
       
   324 void CVIMPSTEnginePresenceSubService::
       
   325 DoHandlePresentityGroupContentEventL(
       
   326         const MXIMPContext& /*aContext*/,
       
   327         const MXIMPBase& aEvent )
       
   328     {
       
   329 	TRACER_AUTO;
       
   330 	  	      
       
   331     const MPresentityGroupContentEvent& event =
       
   332     	*TXIMPGetInterface< const MPresentityGroupContentEvent >::From( 
       
   333             aEvent,
       
   334             MXIMPBase::EPanicIfUnknown );
       
   335     //this needs to be checked if server contacts is supported only then update to 
       
   336     //store else don't.
       
   337 	TInt supportedFeatures = iXimpEventObserver.GetSupportedFeatures();
       
   338   	if(EVIMPSTFeatureFetch & supportedFeatures)
       
   339 		{   
       
   340   	TRACE(" -> storing into respective service store" );
       
   341 	    StoreToVirtualStoreL( event );
       
   342 		}
       
   343 	//this is to ensure the presence for the local sotre contacts is not lost.
       
   344   	TRACE( "new member count: %d" , event.NewMembersCount() ); 
       
   345     TRACE( " current member count: %d" , event.CurrentMembersCount() );  
       
   346     TRACE( "disappeared member count: %d" , event.DisappearedMembersCount() );   
       
   347     }
       
   348 
       
   349 // ---------------------------------------------------------------------------
       
   350 // CVIMPSTEnginePresenceSubService::
       
   351 //      DoHandlePresenceGrantRequestListEventL
       
   352 // ---------------------------------------------------------------------------
       
   353 //
       
   354 void CVIMPSTEnginePresenceSubService::
       
   355 DoHandlePresenceGrantRequestListEventL(
       
   356         const MXIMPContext& /*aContext*/,
       
   357         const MXIMPBase& aEvent )
       
   358     {
       
   359 	TRACER_AUTO;
       
   360     TVIMPSTEnums::TVIMPSTPresenceRequestStatus autoAccept = iSettingsTableFetcher.PresenceRequestStatusL(iServiceId);
       
   361     if(autoAccept == TVIMPSTEnums::ESVCEPresenceRequestStatusAutoAccept)
       
   362         {
       
   363         iAutoAccept = ETrue;
       
   364         }
       
   365     else
       
   366         {
       
   367         iAutoAccept = EFalse;
       
   368         }
       
   369     
       
   370      const MPresenceGrantRequestListEvent& event = 
       
   371      							*TXIMPGetInterface<const MPresenceGrantRequestListEvent >::From( 
       
   372 								aEvent, MXIMPBase::EPanicIfUnknown );
       
   373 
       
   374      TRACE("new watcher count: %d" , event.NewRequestsCount() );   
       
   375      TRACE("current watcher count: %d" , event.CurrentRequestsCount() ); 
       
   376     HBufC* identbuf(NULL);
       
   377     HBufC* displayName(NULL);
       
   378     TInt newcount     = event.NewRequestsCount();
       
   379     TInt currentcount = event.CurrentRequestsCount();
       
   380     if(newcount)
       
   381         {
       
   382     TRACE( " newcount =%d" , newcount );    
       
   383     TRACE( "iServiceState =%d" , iServiceState );    
       
   384 
       
   385         for(TInt i=0; i<newcount; i++)
       
   386             {
       
   387             const MPresenceGrantRequestInfo& reqInfo = event.NewRequest ( i );// its always a new request
       
   388             identbuf = reqInfo.RequestorId().Identity().AllocLC();
       
   389             displayName = reqInfo.RequestorDisplayName().AllocLC();
       
   390             TPtr identbufPtr = identbuf->Des();
       
   391             TRACE( "identity: %S" , &identbufPtr );   
       
   392             if(identbuf->Length())
       
   393                 {
       
   394             TRACE( " pass to command process" );   
       
   395                 if( iAutoAccept &&
       
   396                         TVIMPSTEnums::ESVCERegistered == iServiceState)
       
   397                     {
       
   398                     TInt error = SendPresenceGrantPresentityL( identbufPtr, ETrue );                    
       
   399                     }
       
   400                  else if( iSubServiceObserver )
       
   401                     {
       
   402                  TRACE( " informed observer." ); 
       
   403                     iSubServiceObserver->HandleAddRequestEventL( TVIMPSTEnums::EAddItem , *identbuf, *displayName);
       
   404                     }                    
       
   405                 TRACE( " pass to command process" );
       
   406                 }
       
   407             CleanupStack::PopAndDestroy( displayName );
       
   408             CleanupStack::PopAndDestroy( identbuf );
       
   409             }
       
   410         }
       
   411     else if(currentcount)
       
   412         {
       
   413         const MPresenceGrantRequestInfo& reqInfo = event.CurrentRequest(0 );// its always a new request
       
   414         identbuf = reqInfo.RequestorId().Identity().AllocLC();
       
   415         displayName  = reqInfo.RequestorDisplayName().AllocLC();
       
   416         TPtr identbufPtr = identbuf->Des();
       
   417         TRACE( "identity: %S" , &identbufPtr );  
       
   418         if(identbuf->Length())
       
   419             {
       
   420         TRACE( " pass to command process" );
       
   421             if( iAutoAccept)
       
   422                 {
       
   423                 TInt error = SendPresenceGrantPresentityL( identbufPtr, ETrue );                    
       
   424                 }
       
   425              else if( iSubServiceObserver )
       
   426                 {
       
   427              TRACE( " informed observer."); 
       
   428                 iSubServiceObserver->HandleAddRequestEventL(TVIMPSTEnums::EAddItem ,*identbuf, *displayName);
       
   429                 }      
       
   430             TRACE( "pass to command process" ); 
       
   431             }
       
   432         CleanupStack::PopAndDestroy( displayName );
       
   433         CleanupStack::PopAndDestroy ( identbuf );
       
   434         }    
       
   435 
       
   436     }
       
   437 
       
   438 // ---------------------------------------------------------------------------
       
   439 // CVIMPSTEnginePresenceSubService::ResolveServiceStateL
       
   440 // ---------------------------------------------------------------------------
       
   441 // 
       
   442 TVIMPSTEnums::TVIMPSTRegistrationState CVIMPSTEnginePresenceSubService::ResolveServiceStateL(
       
   443         TCCHSubserviceState aState, 
       
   444         TInt aServiceError )
       
   445     {
       
   446 
       
   447 	TRACER_AUTO;
       
   448 
       
   449     TVIMPSTEnums::TVIMPSTRegistrationState state = TVIMPSTEnums::ESVCENotRegistered;       
       
   450 
       
   451     
       
   452     TRACE( " iServiceId: %d, ServiceState: %d",  iServiceId, aState ); 
       
   453 
       
   454     TBool handleServiceStates = ETrue;
       
   455     if ( aServiceError && ECCHDisabled != aState )
       
   456         {    
       
   457         //Only if the Service supports ALR, the state can goto WaitingForNetwork
       
   458         //Still API from CCH is required to know whether ALR is supported or not
       
   459         //Not sure whether the below is right - have mailed to Markus for MoreInfo on this state
       
   460         if ( (KCCHErrorInvalidSettings != aServiceError) && (ECCHConnecting == aState) )        
       
   461             {   
       
   462             TRACE( " ESVCEWaitingForNetwork");
       
   463             handleServiceStates = EFalse;  
       
   464             //state = TVIMPSTEnums::ESVCEWaitingForNetwork;
       
   465             state = TVIMPSTEnums::ESVCENotRegistered; 
       
   466             }
       
   467         }
       
   468 
       
   469     if ( handleServiceStates )
       
   470         {        
       
   471         switch ( aState )
       
   472             {
       
   473             case ECCHEnabled:
       
   474                 {
       
   475                 TRACE( "ESVCERegistered" );
       
   476                 state = TVIMPSTEnums::ESVCEUpdatingContacts;   
       
   477                 break;
       
   478                 }
       
   479             case ECCHDisconnecting:      
       
   480                 {
       
   481                 TRACE( " ESVCERegistered" );   
       
   482                 state = TVIMPSTEnums::ESVCENetworkDisConnecting;
       
   483                 }
       
   484                 break;
       
   485 
       
   486             case ECCHUninitialized:
       
   487             case ECCHDisabled:  
       
   488                 {
       
   489                 TRACE( "ESVCENotRegistered");
       
   490                 state = TVIMPSTEnums::ESVCENotRegistered;                
       
   491                 }
       
   492                 break;
       
   493 
       
   494             case ECCHConnecting:               
       
   495                 {
       
   496                 TRACE( " ESVCENoNetworkConnecting");  
       
   497                 state = TVIMPSTEnums::ESVCENetworkConnecting;
       
   498                 }
       
   499                 break;
       
   500 
       
   501             default:
       
   502                 break;
       
   503             }
       
   504         }        
       
   505 
       
   506 
       
   507     return state;  
       
   508 
       
   509     }
       
   510 
       
   511 
       
   512 // ---------------------------------------------------------------------------
       
   513 // CVIMPSTEnginePresenceSubService::CchEventOccuredL
       
   514 // ---------------------------------------------------------------------------
       
   515 //
       
   516 void CVIMPSTEnginePresenceSubService::CchEventOccuredL( 
       
   517         TUint /*aServiceId*/, 
       
   518         TCCHSubserviceState aState, 
       
   519         TInt aServiceError )
       
   520     {
       
   521 
       
   522 	TRACER_AUTO;
       
   523 	
       
   524     TRACE("TCCHSubserviceState : %d, ServiceErr: %d", aState, aServiceError );
       
   525 
       
   526     if ( aServiceError && ECCHDisabled != aState )
       
   527         {
       
   528         //we might even end up in waiting for connection state for all
       
   529         //those service which are ALR enabled
       
   530         //So better check here if you get any CCH errors=        
       
   531         iServiceState = ResolveServiceStateL( aState, aServiceError );
       
   532         iObserver.HandleServceConnectionEventL();
       
   533         DoHandleCchErrorL( aServiceError );
       
   534         }
       
   535     else
       
   536         {
       
   537         TVIMPSTEnums::TVIMPSTRegistrationState currentState = 
       
   538         							ResolveServiceStateL( aState, aServiceError );
       
   539         							
       
   540 		if ( TVIMPSTEnums::ESVCERegistered == iServiceState 
       
   541 				&& TVIMPSTEnums::ESVCEUpdatingContacts == currentState )
       
   542 			{
       
   543 			//dont do anything
       
   544 			//updating contacts is a special case
       
   545 			//Once updating contacts, we fetch the groups and once this is done
       
   546 			//we end up changing the state to registered
       
   547 			//but CCH might send the same callback twice	
       
   548 			//to handle this situation we add a check here
       
   549 			//because we might have already moved from Updatin to Registered state
       
   550 			}
       
   551 		else
       
   552 			{
       
   553 			iServiceState = ResolveServiceStateL( aState, aServiceError );    
       
   554 		    iObserver.HandleServceConnectionEventL();			}
       
   555         }   
       
   556 
       
   557 
       
   558     }
       
   559 
       
   560 
       
   561 // ---------------------------------------------------------------------------
       
   562 // CVIMPSTEnginePresenceSubService::DoHandleCchErrorL()
       
   563 // ---------------------------------------------------------------------------
       
   564 // 
       
   565 void CVIMPSTEnginePresenceSubService::DoHandleCchErrorL( 
       
   566         TInt aServiceError )
       
   567     {
       
   568 
       
   569 	TRACER_AUTO;
       
   570 	   TRACE( "ServiceErr: %d",  aServiceError );
       
   571 
       
   572     if ( aServiceError )
       
   573         {
       
   574         //unsubscribe can only be done, when bind is already done
       
   575         if(TVIMPSTEnums::EVIMPSTBindDone ==iXimpEventObserver.ContextBindStatus())
       
   576             {
       
   577             TRACE( "unscribe and unbind");
       
   578             TRAP_IGNORE( UnsubscribeListsL() ); 
       
   579             iXimpEventObserver.ServerUnBindL( ETrue );
       
   580             }
       
   581         }
       
   582 
       
   583 
       
   584     }
       
   585 
       
   586 // ---------------------------------------------------------
       
   587 // CVIMPSTEnginePresenceSubService::PublishOwnPresenceL
       
   588 // 
       
   589 // ---------------------------------------------------------    
       
   590 TInt CVIMPSTEnginePresenceSubService::PublishOwnPresenceL(TVIMPSTEnums::TOnlineStatus aStatus, 
       
   591 														  const TDesC& aValue,
       
   592 						         						  const TDesC& aFilename /*= KNullDesC*/, 
       
   593 						         						  const TDesC8& aMimetype /*= KNullDesC8*/,
       
   594 						         						  TBool aIsAvatar /*= EFalse*/ )
       
   595     {
       
   596 	TRACER_AUTO;
       
   597     // Take handles to object factory and publish interface
       
   598     MPresencePublishing& publisher = iXimpEventObserver.XimpPresencePublishingL();
       
   599 
       
   600     //Fill presence doc with presence components and attributes
       
   601     MPresenceInfo* myPresence = iXimpEventObserver.PresenceObjectFactoryL().NewPresenceInfoLC();//1
       
   602     MPersonPresenceInfo *personPresence = iXimpEventObserver.PresenceObjectFactoryL().NewPersonPresenceInfoLC();//2
       
   603     MPresenceInfoFieldCollection& attributeFields = personPresence->Fields();
       
   604 	
       
   605  	MPresenceInfoField* infoField = NULL;
       
   606  	TRACE( "aIsAvatar: %d",  aIsAvatar );
       
   607 	// avatar field set || clear
       
   608 	if ( aIsAvatar )
       
   609         {
       
   610 	TRACE( " PublishOwnPresenceL adding avatar field");
       
   611         
       
   612      	infoField = iXimpEventObserver.PresenceObjectFactoryL().NewInfoFieldLC();
       
   613         MPresenceInfoFieldValueBinary* avatarField = iXimpEventObserver.PresenceObjectFactoryL().NewBinaryInfoFieldLC(); 
       
   614         TRACE( " PublishOwnPresenceL processing image data");
       
   615         CVIMPSTEngineImageHandler* imageHandler = CVIMPSTEngineImageHandler::NewL();
       
   616         CleanupStack::PushL(imageHandler);
       
   617         // get the avatar content from the image processor
       
   618         // returns image content if the  processing succesful 
       
   619         
       
   620         HBufC8* avatarContent = imageHandler->ProcessImageFromFileL( aFilename , aMimetype);
       
   621         TRACE( " PublishOwnPresenceL processing image data completed ");
       
   622         if ( avatarContent )
       
   623 			{
       
   624         TRACE( "PublishOwnPresenceL valid image data found ");
       
   625 			CleanupStack::PushL(avatarContent);
       
   626 			// set a new avatar
       
   627 			avatarField->SetBinaryValueL(*avatarContent);
       
   628  			avatarField->SetMimeTypeL(aMimetype);
       
   629 			CleanupStack::PopAndDestroy(); // avatarContent
       
   630 			}
       
   631 		else
       
   632 			{
       
   633 		TRACE("PublishOwnPresenceL NULL image data found ");
       
   634 			//  clear the avatar 
       
   635 			avatarField->SetBinaryValueL(KNullDesC8);
       
   636 			iIsClearingAvatar = ETrue;  //set iIsClearingAvatar to ETrue
       
   637 			}
       
   638         CleanupStack::PopAndDestroy(imageHandler); // imageHandler
       
   639         
       
   640  	    //Fill presence doc with presence components and attributes
       
   641         infoField->SetFieldTypeL( NPresenceInfo::NFieldType::KAvatar ); 
       
   642         // for clear avatar mimetye is KNUllDesc so set for all the cases
       
   643        	infoField->SetFieldValue( avatarField );  // avatarField ownership transfered
       
   644         CleanupStack::Pop(); // avatarField
       
   645 		attributeFields.AddOrReplaceFieldL(infoField ); // infofield ownership transfered
       
   646         CleanupStack::Pop(); // infoField
       
   647 		infoField = NULL;
       
   648 		TRACE( "adding avatar field completed ");
       
   649         }
       
   650 
       
   651     // availabilty field
       
   652     infoField = iXimpEventObserver.PresenceObjectFactoryL().NewInfoFieldLC();//3
       
   653     //based on the state conver it to  ximpfw status.
       
   654     NPresenceInfo::TAvailabilityValues availablity = ConvertPresenceStatus( aStatus );
       
   655     // "availability" attribute
       
   656     MPresenceInfoFieldValueEnum* availabilityField = iXimpEventObserver.PresenceObjectFactoryL().NewEnumInfoFieldLC();//4
       
   657     availabilityField->SetValueL( availablity );
       
   658     infoField->SetFieldTypeL( NPresenceInfo::NFieldType::KAvailabilityEnum ); 
       
   659     infoField->SetFieldValue( availabilityField );
       
   660     CleanupStack::Pop(); // availabilityField
       
   661     attributeFields.AddOrReplaceFieldL(infoField );
       
   662     CleanupStack::Pop(); // infoField
       
   663 
       
   664     //status text field
       
   665     infoField = iXimpEventObserver.PresenceObjectFactoryL().NewInfoFieldLC();//7
       
   666     MPresenceInfoFieldValueText* statustextField = iXimpEventObserver.PresenceObjectFactoryL().NewTextInfoFieldLC();//8
       
   667     //ownership is transfered to statustextField
       
   668     HBufC* statusText = aValue.AllocLC();
       
   669     //ownership is not transferred
       
   670     statustextField->SetTextValueL( *statusText); // some status text
       
   671     CleanupStack::PopAndDestroy();//statusText
       
   672     infoField->SetFieldTypeL( NPresenceInfo::NFieldType::KStatusMessage );
       
   673     infoField->SetFieldValue( statustextField );
       
   674     CleanupStack::Pop(); // statustextField
       
   675     attributeFields.AddOrReplaceFieldL( infoField );
       
   676     CleanupStack::Pop(); // infoField
       
   677 	
       
   678 
       
   679 	myPresence->SetPersonPresenceL(personPresence);
       
   680     CleanupStack::Pop(); // personPresence
       
   681   
       
   682     TXIMPRequestId reqId;
       
   683     reqId = publisher.PublishOwnPresenceL( *myPresence );
       
   684     // wait completion
       
   685     // not own
       
   686     CVIMPSTEngineRequestMapper* mapper = iXimpEventObserver.GetRequestMapper();
       
   687     mapper->CreateRequestL(reqId, ETrue,EVIMPSTXimpOperationPublisOwnPresence);// waite here
       
   688     TInt error = iXimpEventObserver.GetCompletedReqResult(); // get the result error
       
   689     CleanupStack::PopAndDestroy(1); // myPresence
       
   690     
       
   691     return error;
       
   692     }
       
   693 
       
   694 // ---------------------------------------------------------
       
   695 // CVIMPSTEnginePresenceSubService::UpdateStatusToServerL
       
   696 // 
       
   697 // ---------------------------------------------------------    
       
   698 NPresenceInfo::TAvailabilityValues CVIMPSTEnginePresenceSubService::ConvertPresenceStatus(TVIMPSTEnums::TOnlineStatus aStatus)
       
   699     {
       
   700 	TRACER_AUTO;
       
   701     NPresenceInfo::TAvailabilityValues availablity;
       
   702     switch(aStatus)
       
   703         {
       
   704         case TVIMPSTEnums::EOffline:
       
   705             {
       
   706             availablity = NPresenceInfo::ENotAvailable;
       
   707             break;
       
   708             }
       
   709         case TVIMPSTEnums::EOnline:
       
   710             {
       
   711             availablity = NPresenceInfo::EAvailable;
       
   712             break;
       
   713             }
       
   714         case TVIMPSTEnums::EInvisible:
       
   715             {
       
   716             availablity = NPresenceInfo::EHidden;
       
   717             break;
       
   718             }
       
   719         case TVIMPSTEnums::EAway:
       
   720             {
       
   721             availablity = NPresenceInfo::EAway;
       
   722             break;
       
   723             }
       
   724         case TVIMPSTEnums::EBusy:
       
   725             {
       
   726             availablity = NPresenceInfo::EBusy;
       
   727             break;
       
   728             }
       
   729         case TVIMPSTEnums::EOnPhone:
       
   730             {
       
   731             availablity = NPresenceInfo::EOnPhone;
       
   732             break;
       
   733             }
       
   734         case TVIMPSTEnums::EDoNotDisturb:
       
   735             {
       
   736             availablity = NPresenceInfo::EDoNotDisturb;
       
   737             break;
       
   738             }
       
   739         default: 
       
   740             {
       
   741             availablity = NPresenceInfo::ENotAvailable;
       
   742             break;
       
   743             }
       
   744         }
       
   745     return availablity;
       
   746     }
       
   747 /// ---------------------------------------------------------
       
   748 // CVIMPSTEnginePresenceSubService::FetchPresenceFromCache
       
   749 // 
       
   750 // ---------------------------------------------------------    
       
   751 TInt  CVIMPSTEnginePresenceSubService::FetchPresenceFromCache()
       
   752     {
       
   753     //TODO::Figure out how to get the service name.
       
   754     // passed the service id to see the member count
       
   755     TInt error( KErrArgument );
       
   756     TRACER_AUTO;
       
   757 	if( iServiceName )
       
   758 		{
       
   759 		TPtr serviceNamePtr = iServiceName->Des();
       
   760 		TRACE( "CVIMPSTEnginePresenceSubService::FetchPresenceFormCache() - %S", &serviceNamePtr );
       
   761 		// passed the service to register for notification
       
   762 		error = iPresenceCacheReader->AllBuddiesPresenceInService(*iServiceName, this );		
       
   763 		}
       
   764 	return error;
       
   765     }
       
   766 
       
   767 // ---------------------------------------------------------------------------
       
   768 // CVIMPSTEnginePresenceSubService::HandlePresenceReadL
       
   769 // ---------------------------------------------------------------------------
       
   770 //  
       
   771 void CVIMPSTEnginePresenceSubService::HandlePresenceReadL(TInt /*aErrorCode*/,
       
   772         RPointerArray<MPresenceBuddyInfo2>& aPresenceBuddyInfoList)
       
   773     {
       
   774 	TRACER_AUTO;
       
   775     // we have the ownership of  aPresenceBuddyInfoList : Push it to customize cleanupstack
       
   776     // aPresenceBuddyInfoList is collection of owned object and each object need to be deleted
       
   777     CustomCleanupResetAndDestroyPushL(aPresenceBuddyInfoList);
       
   778     MVIMPSTStorageServiceView* storage = 
       
   779     				CVIMPSTStorageManagerFactory::ContactListInterfaceL(iServiceId);
       
   780     TInt buddyCount = aPresenceBuddyInfoList.Count();
       
   781     TRACE( "count: %d" , buddyCount );
       
   782     for ( TInt i =0 ; i < buddyCount ; ++i)
       
   783 		{
       
   784     TRACE( " buddy index: %d" , i );
       
   785 		MPresenceBuddyInfo2* buddyinfo = aPresenceBuddyInfoList[i];
       
   786 		// read the buddyID : returns in XSP format
       
   787 		TPtrC buddyXSPId = buddyinfo->BuddyId();
       
   788 		TRACE( "Status Message: %s" , &buddyXSPId );
       
   789 		TPtrC buddyId = buddyXSPId.Right( buddyXSPId.Length() - iServiceName->Length() - KColon().Length());
       
   790 		// read the availability /presence state enum value 
       
   791 		MPresenceBuddyInfo2::TAvailabilityValues availabilityEnum = buddyinfo->Availability();
       
   792 		TRACE( "Availability ENUM value: %d" , availabilityEnum );
       
   793 		TPtrC avablityText = buddyinfo->AvailabilityText();
       
   794 		// convert the presence cache enum value to service tab enum 
       
   795 		TVIMPSTEnums::TOnlineStatus status = ConvertPresenceCacheEnums( availabilityEnum , avablityText);
       
   796 		//  get the GetAnyFiled(which has only pending and blocked states.)
       
   797 		if(TVIMPSTEnums::EUnKnown == status)
       
   798 			{
       
   799 			GetKeyFieldsAndValuesL(*buddyinfo,status);
       
   800 			}
       
   801 		// read the  status message 
       
   802 		TPtrC statusMsg = buddyinfo->StatusMessage();
       
   803 		TRACE("Status Message: %s" , &statusMsg );
       
   804 		TPtrC8 avatarContent = buddyinfo->Avatar();
       
   805 		HBufC8* avatarScaledData = NULL;
       
   806 		if ( avatarContent.Length() )
       
   807 			{
       
   808 			TRACE( "avatarContent Content available"  );
       
   809 			CVIMPSTEngineImageHandler* imageHandler = CVIMPSTEngineImageHandler::NewL();
       
   810 			CleanupStack::PushL(imageHandler);
       
   811 			TRACE( "imageHandler created "  );
       
   812 		    avatarScaledData = imageHandler->ProcessImageFromDataL( avatarContent , KNullDesC8() );
       
   813 		    TRACE( "ProcessImageFromDataL returned " );
       
   814 			CleanupStack::PopAndDestroy();//imageHandler
       
   815 			}
       
   816 		if( avatarScaledData && avatarScaledData->Length() )
       
   817 			{
       
   818 			CleanupStack::PushL(avatarScaledData);
       
   819 			storage->UpdatePresenceL(buddyId,status,statusMsg, *avatarScaledData );	
       
   820 			CleanupStack::PopAndDestroy();//avatarScaledData
       
   821 			}
       
   822 		else  
       
   823 			{
       
   824 			storage->UpdatePresenceL(buddyId, status, statusMsg, KNullDesC8 );		
       
   825 			}
       
   826 		}
       
   827     aPresenceBuddyInfoList.ResetAndDestroy();
       
   828  
       
   829     CleanupStack::PopAndDestroy();  //  aPresenceBuddyInfoList
       
   830     }
       
   831 
       
   832 // ---------------------------------------------------------------------------
       
   833 // CVIMPSTEnginePresenceSubService::HandlePresenceNotificationL
       
   834 // ---------------------------------------------------------------------------
       
   835 //  
       
   836 void CVIMPSTEnginePresenceSubService::HandlePresenceNotificationL(TInt /*aErrorCode*/,
       
   837         MPresenceBuddyInfo2* aPresenceBuddyInfo)
       
   838     {
       
   839 	TRACER_AUTO;
       
   840        
       
   841     if ( aPresenceBuddyInfo )
       
   842         {
       
   843         CleanupDeletePushL( aPresenceBuddyInfo );
       
   844         DoHandlePresenceNotificationL(*aPresenceBuddyInfo);
       
   845         CleanupStack::PopAndDestroy(1); // aPresenceBuddyInfo
       
   846         
       
   847         }
       
   848     }
       
   849 
       
   850 
       
   851 // ---------------------------------------------------------
       
   852 // CVIMPSTEnginePresenceSubService::SubscribeForAuthorizationL
       
   853 //  
       
   854 // ---------------------------------------------------------    
       
   855 void  CVIMPSTEnginePresenceSubService::SubscribeForAuthorizationL()
       
   856     {
       
   857 	TRACER_AUTO;
       
   858     TXIMPRequestId req;
       
   859     MPresenceAuthorization& authorization = iXimpEventObserver.XimpAuthorizationL();
       
   860     req = authorization.SubscribePresenceGrantRequestListL();  
       
   861     // mapper is not own
       
   862     CVIMPSTEngineRequestMapper* mapper = iXimpEventObserver.GetRequestMapper(); 
       
   863     mapper->CreateRequestL(req, EFalse,EVIMPSTXimpOperationSubcribeGrantRequestList);
       
   864     
       
   865     iSubscribeToAuthList = ETrue;
       
   866     }
       
   867 // ---------------------------------------------------------
       
   868 // CVIMPSTEnginePresenceSubService::SendPresenceGrantPresentityResponseL
       
   869 //  
       
   870 // ---------------------------------------------------------    
       
   871 TInt CVIMPSTEnginePresenceSubService::SendPresenceGrantPresentityL( const TDesC& aContactId , TBool aResponse )
       
   872     {
       
   873 	TRACER_AUTO;
       
   874     __ASSERT_ALWAYS( aContactId.Length(), User::Leave( KErrArgument ) );
       
   875       
       
   876     // return the response to the server.    
       
   877     MPresenceAuthorization& authorization = iXimpEventObserver.XimpAuthorizationL();
       
   878     // mapper is not own
       
   879     CVIMPSTEngineRequestMapper* mapper = iXimpEventObserver.GetRequestMapper();
       
   880 
       
   881     MXIMPIdentity* contactIdentity = iXimpEventObserver.XimpPresenceContextL().ObjectFactory().NewIdentityLC();
       
   882      __ASSERT_ALWAYS( contactIdentity , User::Leave( KErrNoMemory ) );		
       
   883     contactIdentity->SetIdentityL( aContactId ) ;    
       
   884     TXIMPRequestId req;
       
   885     //accepted the request.
       
   886     if(aResponse)
       
   887         {
       
   888         MPresenceInfoFilter* infoFilt = iXimpEventObserver.PresenceObjectFactoryL().NewPresenceInfoFilterLC();
       
   889         if( infoFilt )
       
   890             {
       
   891             infoFilt->AcceptPersonFilterL( NPresenceInfo::NFieldType::KAcceptAll );
       
   892             req = authorization.GrantPresenceForPresentityL(*contactIdentity ,*infoFilt );
       
   893             CleanupStack::PopAndDestroy(); //infoFilt   	
       
   894             }
       
   895         }
       
   896     else// rejected the request.
       
   897         {        
       
   898         req = authorization.WithdrawPresenceGrantFromPresentityL(*contactIdentity);         
       
   899         }
       
   900     if( iSubServiceObserver && !iAutoAccept )
       
   901         {
       
   902         iSubServiceObserver->HandleAddRequestEventL(TVIMPSTEnums::ERemoveItem ,aContactId, KNullDesC() );
       
   903         }
       
   904     mapper->CreateRequestL(req, !iAutoAccept, EVIMPSTXimpOperationGrantPresenceForPresentity );
       
   905 
       
   906     TInt error = iXimpEventObserver.GetCompletedReqResult(); // get the result error    
       
   907     if( ( ( error == KPREQUESTERRSUCCESSFUL) || ( error == KErrNone )  || iAutoAccept)  )
       
   908         {        
       
   909         MVIMPSTStorageServiceView* storage = CVIMPSTStorageManagerFactory::ContactListInterfaceL(iServiceId) ;
       
   910         if(storage && ( !storage->IsLocalStore() || iAutoAccept  ) && aResponse )
       
   911             {   
       
   912         TRACE( " server store" );
       
   913             storage->CreateNewContactL( aContactId,KNullDesC, ETrue, iAutoAccept ); // ETrue is for invitation item 
       
   914             } 
       
   915         }
       
   916     CleanupStack::PopAndDestroy(); //contactIdentity
       
   917     return error;
       
   918     }  
       
   919 // ---------------------------------------------------------
       
   920 // CVIMPSTEnginePresenceSubService::HandleSessionContextEventL
       
   921 // ---------------------------------------------------------
       
   922 void CVIMPSTEnginePresenceSubService::HandleSessionContextEventL(const MXIMPContext& aContext,
       
   923         const MXIMPBase& aEvent,
       
   924         TXimpOperation aXimpOperation /*= EVIMPSTXimpOperationNoOperation*/ )
       
   925     {
       
   926 	TRACER_AUTO;
       
   927     TInt32 eventId = aEvent.GetInterfaceId();
       
   928 
       
   929     switch( aEvent.GetInterfaceId() )
       
   930         {
       
   931         case MXIMPRequestCompleteEvent::KInterfaceId:
       
   932             {
       
   933             TRACE( "MXIMPRequestCompleteEvent");
       
   934             //temp fix TBD
       
   935             //Only use the operations that u r intertest in
       
   936             if ( aXimpOperation <= EVIMPSTXimpOperationUnsubscribe )  
       
   937                 {
       
   938 
       
   939                 const MXIMPRequestCompleteEvent* event =
       
   940                 TXIMPGetInterface< const MXIMPRequestCompleteEvent >::From( 
       
   941                         aEvent, MXIMPBase::EPanicIfUnknown );
       
   942                 TRAP_IGNORE( HandleXimpRequestCompleteL( 
       
   943                         aXimpOperation,
       
   944                         event->CompletionResult().ResultCode(),
       
   945                         aEvent ) );
       
   946                 }
       
   947             TRACE( "HandlePresenceContextEvent");  
       
   948 
       
   949             break;
       
   950             }
       
   951         case MXIMPContextStateEvent::KInterfaceId:
       
   952             {
       
   953             TRACE( "InsideCallbackswitch::MXIMPContextStateEvent");
       
   954             TRACE("InsideCallback::MXIMPContextStateEvent");
       
   955             break;
       
   956             }
       
   957         case MPresentityGroupContentEvent::KInterfaceId:
       
   958             {
       
   959             TRACE( "MPresentityGroupContentEvent");
       
   960             DoHandlePresentityGroupContentEventL( aContext, aEvent );
       
   961             TRACE( "MPresentityGroupContentEvent");
       
   962             break;  
       
   963             }
       
   964         case MPresenceGrantRequestListEvent::KInterfaceId:
       
   965             {
       
   966             TRACE( "MPresenceGrantRequestListEvent");
       
   967             DoHandlePresenceGrantRequestListEventL( aContext, aEvent );
       
   968             TRACE( "MPresenceGrantRequestListEvent");
       
   969             break;
       
   970             }
       
   971         case MPresenceBlockListEvent::KInterfaceId:
       
   972            {
       
   973            TRACE( "MPresenceBlockListEvent");
       
   974            DoHandlePresenceBlockListEventL( aContext, aEvent );
       
   975            TRACE( "MPresenceBlockListEvent");
       
   976 
       
   977            break;
       
   978            }
       
   979            
       
   980         default:
       
   981             {
       
   982             break;
       
   983             }
       
   984         }
       
   985     }    
       
   986 
       
   987 // ---------------------------------------------------------------------------
       
   988 // CVIMPSTEnginePresenceSubService::HandleListEventCompleteL
       
   989 // ---------------------------------------------------------------------------
       
   990 //
       
   991 void CVIMPSTEnginePresenceSubService::HandleListEventCompleteL(TXimpOperation aType,
       
   992         TInt aCompleteCode,
       
   993         const MXIMPBase& /*aEvent*/)
       
   994     { 
       
   995 	TRACER_AUTO;
       
   996 
       
   997     switch ( aType )
       
   998         {
       
   999         case EVIMPSTXimpOperationBind:
       
  1000             {
       
  1001             if(iLogoutRequest)
       
  1002                 {
       
  1003                 //since logout is requested, no need to Subscribe and Retrieve list
       
  1004                 break;
       
  1005                 }
       
  1006             if ( KErrNone == aCompleteCode )
       
  1007                 {         
       
  1008 			    SubscribeForAuthorizationL();
       
  1009                 // get the list.
       
  1010                 RetrieveSubscribedListL();
       
  1011                 
       
  1012                 }
       
  1013             break;
       
  1014 
       
  1015             }
       
  1016         case EVIMPSTXimpOperationGetSubscribedList:
       
  1017             {
       
  1018             if(iLogoutRequest)
       
  1019                 {
       
  1020                 //Since logout is requested need not to retrive block list
       
  1021                 break;
       
  1022                 }
       
  1023             // check if blocking is supported then get the blocked list and 
       
  1024             // subscribe those to persence cache.
       
  1025             TInt supportedFeatures = iXimpEventObserver.GetSupportedFeatures();
       
  1026             if ( (EVIMPSTFeatureBlock & supportedFeatures) && (EVIMPSTFeatureUnBlock & supportedFeatures) )
       
  1027                {
       
  1028                RetrieveBlockListL();
       
  1029                }
       
  1030             break;
       
  1031             }
       
  1032         case EVIMPSTXimpOperationUnBind:
       
  1033             {
       
  1034             //Logout request completed
       
  1035             iLogoutRequest = EFalse;
       
  1036             break;
       
  1037             }            
       
  1038         default:
       
  1039             break;   
       
  1040         }
       
  1041     }
       
  1042 
       
  1043 // ---------------------------------------------------------------------------
       
  1044 // CVIMPSTEnginePresenceSubService::HandleXimpRequestCompleteL
       
  1045 // ---------------------------------------------------------------------------
       
  1046 //
       
  1047 void CVIMPSTEnginePresenceSubService::HandleXimpRequestCompleteL(TXimpOperation aType,
       
  1048         TInt aCompleteCode,const MXIMPBase& aEvent )
       
  1049     {
       
  1050 	TRACER_AUTO;
       
  1051     switch ( aType )
       
  1052         {
       
  1053         case EVIMPSTXimpOperationBind:
       
  1054         case EVIMPSTXimpOperationGetSubscribedList:
       
  1055         case EVIMPSTXimpOperationGetBlockList:
       
  1056             //case TVIMPSTEnums::ESVCEXimpOperationGetWatcherList:
       
  1057         case EVIMPSTXimpOperationUnBind:        
       
  1058             {
       
  1059             HandleListEventCompleteL( aType, aCompleteCode,aEvent );
       
  1060             break;
       
  1061             }
       
  1062         case EVIMPSTXimpOperationWithdrawPresenceGrant:
       
  1063             {
       
  1064             break;
       
  1065             }
       
  1066         case EVIMPSTXimpOperationBlockPresenceForPresentity:
       
  1067         case EVIMPSTXimpOperationCancelPresenceBlockFromPresentity:
       
  1068         default:
       
  1069             {
       
  1070             break;
       
  1071             }
       
  1072         }
       
  1073 
       
  1074 
       
  1075     }
       
  1076 // -----------------------------------------------------------------------------
       
  1077 // CVIMPSTEnginePresenceSubService::StoreToVirtualStoreL
       
  1078 // -----------------------------------------------------------------------------
       
  1079 //  
       
  1080 void CVIMPSTEnginePresenceSubService::StoreToVirtualStoreL(
       
  1081                             const MPresentityGroupContentEvent& aListEvent )
       
  1082     {
       
  1083 	TRACER_AUTO;
       
  1084 	TRACE( " count = %d",aListEvent.CurrentMembersCount() );
       
  1085     TRACE("NewMembersCount count = %d",aListEvent.NewMembersCount() );
       
  1086     TRACE( "UpdatedMembersCount count = %d",aListEvent.UpdatedMembersCount() );
       
  1087     MVIMPSTStorageServiceView* storage = 
       
  1088     CVIMPSTStorageManagerFactory::ContactListInterfaceL(iServiceId) ;	
       
  1089     TLinearOrder< TPtrC > linearOrder (*CompareAlphabetically );
       
  1090     if (storage)
       
  1091         {		
       
  1092         //currMembrCount will be 0 when you login to the service
       
  1093         if( iIsFetchingContact)  
       
  1094             {
       
  1095             // there are contacts process each
       
  1096             TPtrC listName = aListEvent.GroupId().Identity() ;
       
  1097             RArray <TPtrC> firstNameList;
       
  1098             CleanupClosePushL( firstNameList );
       
  1099             RArray <TPtrC> serviceField;
       
  1100             CleanupClosePushL( serviceField );
       
  1101             
       
  1102             firstNameList.Reset();
       
  1103             serviceField.Reset();
       
  1104             // number of contact in this list
       
  1105            TInt currentMembrcount = aListEvent.CurrentMembersCount();
       
  1106            TRACE( "currentMembrcount count = %d",currentMembrcount );
       
  1107             // Handle first current items
       
  1108             for(TInt j = 0; j < currentMembrcount ;j++ )
       
  1109                 {
       
  1110                 const MPresentityGroupMemberInfo& memberInfo = 
       
  1111                     aListEvent.CurrentMember( j ) ;
       
  1112                 const MXIMPIdentity& memberIdentity = memberInfo.GroupMemberId() ;
       
  1113                 TPtrC userId = memberIdentity.Identity();
       
  1114                 TPtrC displayeName = memberInfo.GroupMemberDisplayName();
       
  1115                 serviceField.AppendL(userId );    
       
  1116                 firstNameList.AppendL(displayeName );
       
  1117                 }
       
  1118 
       
  1119             // number of contact in this list
       
  1120             TInt newMembrcount = aListEvent.NewMembersCount() ;
       
  1121             TRACE("newMembrcount count = %d",newMembrcount );
       
  1122             for(TInt i = 0; i < newMembrcount ;i++ )
       
  1123                 {
       
  1124                 const MPresentityGroupMemberInfo& memberInfo = 
       
  1125                 aListEvent.NewMember( i ) ;
       
  1126                 const MXIMPIdentity& memberIdentity = memberInfo.GroupMemberId() ;
       
  1127                 TPtrC userId = memberIdentity.Identity();
       
  1128                 TPtrC displayeName = memberInfo.GroupMemberDisplayName();
       
  1129                 TInt error = serviceField.InsertInOrder(userId,linearOrder);
       
  1130                 if(KErrAlreadyExists != error)
       
  1131                     {
       
  1132                     firstNameList.Append(displayeName);
       
  1133                     }
       
  1134                 }
       
  1135             TRACE( " calling CreateNewFetchContactsL" );
       
  1136             TRACE( " serviceField count %d",serviceField.Count());
       
  1137             TRACE( " firstNameList count %d",firstNameList.Count());
       
  1138 
       
  1139             // If count in both arrays does not match, storage side can panic
       
  1140             __ASSERT_ALWAYS( firstNameList.Count() == serviceField.Count(), User::Leave( KErrCorrupt));
       
  1141             storage->CreateNewFetchContactsL(firstNameList, serviceField);
       
  1142             iIsFetchingContact = EFalse;
       
  1143             
       
  1144             CleanupStack::PopAndDestroy( &serviceField );
       
  1145             CleanupStack::PopAndDestroy( &firstNameList );
       
  1146             }
       
  1147         //in case of delayed fetch contact and add contact 
       
  1148 	 	else 
       
  1149 			{
       
  1150 			// number of contact in this list
       
  1151 			TInt currentMembrcount = aListEvent.CurrentMembersCount() ;
       
  1152 			for(TInt i = 0; i < currentMembrcount ; i++ )
       
  1153                 {
       
  1154                 const MPresentityGroupMemberInfo& memberInfo = 
       
  1155                                                 aListEvent.CurrentMember( i ) ;
       
  1156                 const MXIMPIdentity& memberIdentity = memberInfo.GroupMemberId() ;
       
  1157                 RDebug::Print( _L("CVIMPSTEnginePresenceSubService: newMember %S"), &memberIdentity.Identity() );   
       
  1158                 if ( !storage->FindContactByUserId( memberIdentity.Identity() ) )
       
  1159                     {
       
  1160                     storage->CreateNewContactL( memberIdentity.Identity(), 
       
  1161                              memberInfo.GroupMemberDisplayName(), ETrue, ETrue );     
       
  1162                     }
       
  1163                 }
       
  1164 		    // number of contact in this list
       
  1165 		    TInt newMembrcount = aListEvent.NewMembersCount() ;
       
  1166 		    // there are contacts process each
       
  1167 		    TPtrC listName = aListEvent.GroupId().Identity() ;
       
  1168 		    for(TInt i = 0; i < newMembrcount ;i++ )
       
  1169 		        {
       
  1170 		        const MPresentityGroupMemberInfo& memberInfo = 
       
  1171 		                                        aListEvent.NewMember( i ) ;
       
  1172 		        const MXIMPIdentity& memberIdentity = memberInfo.GroupMemberId() ;
       
  1173 		        TPtrC userId = memberIdentity.Identity();
       
  1174 		        TPtrC displayeName = memberInfo.GroupMemberDisplayName();
       
  1175 		        TRACE( " newMember %S", &userId );
       
  1176 		        storage->CreateNewContactL(userId,displayeName,ETrue,ETrue); 	
       
  1177 		        }
       
  1178 		   TInt removedMembrcount = aListEvent.DisappearedMembersCount() ;    
       
  1179 		   for(TInt i = 0; i < removedMembrcount ;i++ )
       
  1180 		        {
       
  1181 		        const MPresentityGroupMemberInfo& memberInfo = 
       
  1182 		                                        aListEvent.DisappearedMember( i ) ;
       
  1183 		        const MXIMPIdentity& memberIdentity = memberInfo.GroupMemberId() ;
       
  1184 		        TPtrC userId = memberIdentity.Identity();
       
  1185 		        TRACE( "deleteMember %S", &userId );
       
  1186 		    	MVIMPSTStorageContact* contactExist = storage->FindContactByUserId(userId);
       
  1187 		    	if(contactExist)
       
  1188 		    	    {
       
  1189 		    	    storage->RemoveContactL(contactExist);	
       
  1190 		    	    }
       
  1191 		        }
       
  1192 			}
       
  1193          
       
  1194         }
       
  1195     }
       
  1196 
       
  1197 // ---------------------------------------------------------
       
  1198 // CVIMPSTEnginePresenceSubService::IsChangeOwnStatusSupported
       
  1199 // 
       
  1200 // ---------------------------------------------------------        
       
  1201 
       
  1202 TBool CVIMPSTEnginePresenceSubService::IsChangeOwnStatusSupported() 
       
  1203     {
       
  1204     //TODO:: get the feature supported from ximp and return
       
  1205     return iChangeStatusSupported;
       
  1206     }
       
  1207 
       
  1208 // ---------------------------------------------------------
       
  1209 // CVIMPSTEnginePresenceSubService::IsStatusMsgSupported
       
  1210 // 
       
  1211 // ---------------------------------------------------------        
       
  1212 TBool CVIMPSTEnginePresenceSubService::IsStatusMsgSupported()
       
  1213     {
       
  1214     //TODO:: get the feature supported from ximp and return
       
  1215     return iChangeStatusMsgSupported;
       
  1216     }
       
  1217 
       
  1218 // ---------------------------------------------------------
       
  1219 // CVIMPSTEnginePresenceSubService::IsAvatarSupported
       
  1220 // 
       
  1221 // ---------------------------------------------------------        
       
  1222 TBool CVIMPSTEnginePresenceSubService::IsAvatarSupported()
       
  1223     {
       
  1224     return iAvatarSupported;
       
  1225     }
       
  1226  
       
  1227  // ---------------------------------------------------------
       
  1228 // CVIMPSTEnginePresenceSubService::SetAvatarSupported
       
  1229 // 
       
  1230 // ---------------------------------------------------------        
       
  1231 void CVIMPSTEnginePresenceSubService::SetAvatarSupported(TBool aSupported )
       
  1232     {
       
  1233     iAvatarSupported = aSupported;
       
  1234     }
       
  1235 // ---------------------------------------------------------------------------
       
  1236 // CVIMPSTEnginePresenceSubService::ConvertXimpToClientPresenceStatus
       
  1237 // ---------------------------------------------------------------------------
       
  1238 //  
       
  1239 TVIMPSTEnums::TOnlineStatus CVIMPSTEnginePresenceSubService::ConvertPresenceCacheEnums(MPresenceBuddyInfo2::TAvailabilityValues aAvailabilityEnum,TPtrC aAvabilityText)
       
  1240     {
       
  1241 	TRACER_AUTO;
       
  1242     // convert the presence cache enums to UI enumvalues
       
  1243     // by default if the enum doesnot match then its  TVIMPSTEnums::UnKnown
       
  1244     TVIMPSTEnums::TOnlineStatus status;
       
  1245     switch( aAvailabilityEnum )
       
  1246         {
       
  1247         case MPresenceBuddyInfo2::EBusy:
       
  1248 			{
       
  1249 			status  = TVIMPSTEnums::EBusy;
       
  1250 			if(0==aAvabilityText.Compare(KAwayState))
       
  1251 			    {
       
  1252 			    status  = TVIMPSTEnums::EAway;
       
  1253 			    }
       
  1254 			if(0==aAvabilityText.Compare(KOnPhoneState))
       
  1255 			    {
       
  1256 			    status  = TVIMPSTEnums::EOnPhone;
       
  1257 			    }
       
  1258 			if(0==aAvabilityText.Compare(KDndState))
       
  1259 			    {
       
  1260 			    status  = TVIMPSTEnums::EDoNotDisturb;
       
  1261 			    }
       
  1262             break;
       
  1263 			}
       
  1264         case MPresenceBuddyInfo2::EAvailable:
       
  1265             {
       
  1266             status  = TVIMPSTEnums::EOnline;
       
  1267             break;
       
  1268             }
       
  1269         case MPresenceBuddyInfo2::ENotAvailable:
       
  1270             {
       
  1271             status  = TVIMPSTEnums::EOffline;
       
  1272             break;
       
  1273             }
       
  1274         case MPresenceBuddyInfo2::EUnknownAvailability:
       
  1275             {
       
  1276             status  = TVIMPSTEnums::EUnknown;
       
  1277 			if(0==aAvabilityText.Compare(KInvisibleState))
       
  1278 			    {
       
  1279 			    status  = TVIMPSTEnums::EInvisible;
       
  1280 			    }
       
  1281 			
       
  1282             break;
       
  1283             }   
       
  1284         default:
       
  1285 	        {
       
  1286             status  = TVIMPSTEnums::EUnknown;
       
  1287             break;
       
  1288 	        }
       
  1289         }
       
  1290     return status;
       
  1291     }
       
  1292 
       
  1293 
       
  1294 // ---------------------------------------------------------
       
  1295 // CVIMPSTEnginePresenceSubService::RegisterPresenceEventObserver
       
  1296 // 
       
  1297 // ---------------------------------------------------------	
       
  1298 //TODO::Should be named as RegisterPresenceEventObserverL
       
  1299 void CVIMPSTEnginePresenceSubService::RegisterPresenceEventObserverL(
       
  1300         MVIMPSTEnginePresenceSubServiceEventObserver* aObserver)
       
  1301     {
       
  1302 	TRACER_AUTO;
       
  1303     __ASSERT_ALWAYS( aObserver, User::Leave( KErrArgument ));		
       
  1304     iSubServiceObserver = aObserver;
       
  1305     }
       
  1306 
       
  1307 // ---------------------------------------------------------
       
  1308 // CVIMPSTEnginePresenceSubService::UnRegisterPresenceEventObserver
       
  1309 // 
       
  1310 // ---------------------------------------------------------	
       
  1311 
       
  1312 void CVIMPSTEnginePresenceSubService::UnRegisterPresenceEventObserver(
       
  1313         MVIMPSTEnginePresenceSubServiceEventObserver* /*aObserver*/)
       
  1314     {
       
  1315 	TRACER_AUTO;
       
  1316     iSubServiceObserver = NULL;
       
  1317     }   
       
  1318 // ---------------------------------------------------------------------------
       
  1319 // CVIMPSTEnginePresenceSubService::RetrieveBlockListL
       
  1320 // ---------------------------------------------------------------------------
       
  1321 //
       
  1322 void CVIMPSTEnginePresenceSubService::RetrieveBlockListL()
       
  1323     {
       
  1324 	TRACER_AUTO;
       
  1325 
       
  1326     //Subscribe block list
       
  1327     // do get block list"));
       
  1328     TXIMPRequestId operationId = TXIMPRequestId::Null();
       
  1329     operationId = iXimpEventObserver.XimpAuthorizationL().SubscribePresenceBlockListL();
       
  1330     CVIMPSTEngineRequestMapper* requestMapper =iXimpEventObserver.GetRequestMapper();
       
  1331     requestMapper->CreateRequestL(operationId,EFalse,EVIMPSTXimpOperationGetBlockList);
       
  1332     // list retrieving ok. Waiting for list.;
       
  1333     }
       
  1334 // ---------------------------------------------------------------------------
       
  1335 // CVIMPSTEnginePresenceSubService::DoHandlePresenceNotificationL
       
  1336 // ---------------------------------------------------------------------------
       
  1337 //
       
  1338 void CVIMPSTEnginePresenceSubService::DoHandlePresenceNotificationL(MPresenceBuddyInfo2& aPresenceBuddyInfo)
       
  1339     {
       
  1340 	TRACER_AUTO;
       
  1341     MVIMPSTStorageServiceView* storage = 
       
  1342     						CVIMPSTStorageManagerFactory::ContactListInterfaceL(iServiceId);
       
  1343     TPtrC ownUserId = storage->OwnContactL().UserId();
       
  1344     // read the buddyID : returns in XSP format
       
  1345     TPtrC buddyXSPId = aPresenceBuddyInfo.BuddyId();
       
  1346     TPtrC buddyId = buddyXSPId.Right( buddyXSPId.Length() - iServiceName->Length() - KColon().Length());
       
  1347     TRACE( " buddyId : %s" , &buddyId );
       
  1348     // read the availability /presence state enum value 
       
  1349     MPresenceBuddyInfo2::TAvailabilityValues availabilityEnum = aPresenceBuddyInfo.Availability();
       
  1350     TRACE( "Availability ENUM value: %d" , availabilityEnum );
       
  1351     TPtrC avablityText = aPresenceBuddyInfo.AvailabilityText();
       
  1352     // convert the presence cache enum value to service tab enum 
       
  1353     TVIMPSTEnums::TOnlineStatus status = ConvertPresenceCacheEnums( availabilityEnum, avablityText);
       
  1354     //TRACE( T_LIT("CVIMPSTEnginePresenceSubService::DoHandlePresenceNotificationL - status: %d" ), status );
       
  1355     //check if its pending or blocked contact.
       
  1356     if(TVIMPSTEnums::EUnKnown == status)
       
  1357         {
       
  1358         GetKeyFieldsAndValuesL(aPresenceBuddyInfo,status);
       
  1359         //TRACE( T_LIT("DoHandlePresenceNotificationL after GetKeyFieldsAndValuesL- status: %d" ), status );
       
  1360         }
       
  1361     TRACE( " status: %d" , status );
       
  1362     // Read  the  status message 
       
  1363     TPtrC statusMsg = aPresenceBuddyInfo.StatusMessage();
       
  1364     TRACE("Status Message: %s" , &statusMsg );
       
  1365     TPtrC8 avatarContent = aPresenceBuddyInfo.Avatar();
       
  1366     
       
  1367     ////////////////////////////////////////////////////////////////
       
  1368     HBufC8* avatarScaledData = NULL;
       
  1369 	if ( avatarContent.Length() )
       
  1370 		{
       
  1371 	TRACE( "avatarContent Content available" );
       
  1372 		CVIMPSTEngineImageHandler* imageHandler = CVIMPSTEngineImageHandler::NewL();
       
  1373 		CleanupStack::PushL(imageHandler);
       
  1374 		TRACE( "imageHandler created " );
       
  1375 		avatarScaledData = imageHandler->ProcessImageFromDataL( avatarContent , KNullDesC8() );
       
  1376 		TRACE( " ProcessImageFromDataL returned " );
       
  1377 		CleanupStack::PopAndDestroy();//imageHandler
       
  1378 		}
       
  1379     if( avatarScaledData && avatarScaledData->Length())
       
  1380         {
       
  1381         CleanupStack::PushL(avatarScaledData);
       
  1382         storage->UpdatePresenceL(buddyId,status,statusMsg, *avatarScaledData ); 
       
  1383         CleanupStack::PopAndDestroy();//avatarScaledData
       
  1384         }
       
  1385     else if( iIsClearingAvatar ) //clear own avatar
       
  1386         {
       
  1387         storage->UpdatePresenceL(buddyId, status, statusMsg, KNullDesC8, ETrue );
       
  1388         iIsClearingAvatar = EFalse;  
       
  1389         }
       
  1390     else
       
  1391         {
       
  1392         storage->UpdatePresenceL(buddyId, status, statusMsg, KNullDesC8 );      
       
  1393         }
       
  1394     }
       
  1395 // ---------------	------------------------------------------------------------
       
  1396 // CVIMPSTEnginePresenceSubService::
       
  1397 //      DoHandlePresentityGroupContentEventL
       
  1398 // ---------------------------------------------------------------------------
       
  1399 //
       
  1400 void CVIMPSTEnginePresenceSubService::DoHandlePresenceBlockListEventL(
       
  1401                                                         const MXIMPContext& /*aContext*/,
       
  1402                                                         const MXIMPBase& aEvent )
       
  1403     {
       
  1404 	TRACER_AUTO;
       
  1405     const MPresenceBlockListEvent& event  =
       
  1406         *TXIMPGetInterface< const MPresenceBlockListEvent >::From( 
       
  1407             aEvent,
       
  1408             MXIMPBase::EPanicIfUnknown );
       
  1409     // if the feature is supported then we need to add this contact into virtual store.
       
  1410     // that logic  needs to be implemented.
       
  1411     // inform ui about the state change from updatingcontacts to registered.
       
  1412     if(TVIMPSTEnums::ESVCEUpdatingContacts == iServiceState )
       
  1413         {
       
  1414     TRACE( " -> DoHandlePresentityGroupContentEventL:state is ESVCEUpdatingContacts" );
       
  1415         }
       
  1416         
       
  1417     TRACE( " new member count: %d" , event.NewBlocksCount());  
       
  1418 
       
  1419     TInt subscriptionCount  =  event.NewBlocksCount();
       
  1420 
       
  1421     TRACE(" subscriptionCount: %d", subscriptionCount );
       
  1422 
       
  1423         TRACE( "  handling buddy list"  ); 
       
  1424         HBufC* subsbuf(NULL);        
       
  1425         for( TInt i =0; i < subscriptionCount; i++ )
       
  1426             {
       
  1427             const MPresenceBlockInfo& blockedEntitys = event.NewBlock(i);
       
  1428             const MXIMPIdentity& ident = blockedEntitys.BlockedEntityId();
       
  1429             subsbuf = ident.Identity().AllocLC();
       
  1430             TPtr subsbufPtr = subsbuf->Des();
       
  1431             TRACE( " -> identity: %S", &subsbufPtr );   
       
  1432 
       
  1433             TRACE(" -> subscribe to cache"  );    
       
  1434             SubscribePresenceOfSingleContactL(*subsbuf);
       
  1435             iBlockedListMgr->AddToBlockedListL(*subsbuf);
       
  1436             CleanupStack::PopAndDestroy( subsbuf );
       
  1437             }
       
  1438         TInt disappearedCount = event.DisappearedBlocksCount();  
       
  1439                 
       
  1440             for( TInt j =0; j < disappearedCount; j++ )
       
  1441                 {
       
  1442                 const MPresenceBlockInfo& blockedEntitys = event.DisappearedBlock( j );
       
  1443                 const MXIMPIdentity& ident = blockedEntitys.BlockedEntityId();
       
  1444                 subsbuf = ident.Identity().AllocLC();
       
  1445                 TPtr subsbufPtr = subsbuf->Des();
       
  1446                 TRACE( " identity: %S", &subsbufPtr );
       
  1447               
       
  1448                 iBlockedListMgr->RemoveFromBlockListL( *subsbuf );    
       
  1449                 
       
  1450             CleanupStack::PopAndDestroy( subsbuf );
       
  1451             }
       
  1452         
       
  1453         if(iBlockedListObserver)
       
  1454 	        {
       
  1455 	        iBlockedListObserver->HandleBlockedListFetchCompleteL();
       
  1456 	        iBlockListFetchReqPending = EFalse;
       
  1457 	        }
       
  1458 
       
  1459     }
       
  1460 
       
  1461 // ---------------------------------------------------------------------------
       
  1462 // CVIMPSTEnginePresenceSubService::AddToBlockListL
       
  1463 // ---------------------------------------------------------------------------
       
  1464 //
       
  1465 TInt CVIMPSTEnginePresenceSubService::AddToBlockListL( const TDesC& aContactId )
       
  1466     {
       
  1467 	TRACER_AUTO;
       
  1468     if(TVIMPSTEnums::ESVCERegistered != iServiceState)
       
  1469        return KErrNotSupported;
       
  1470     //if aContactId is zero.
       
  1471     if( 0 == aContactId.Length())
       
  1472        return KErrArgument;
       
  1473     
       
  1474     TRACE( " aContactId: %s" , &aContactId);
       
  1475     TRACE( " perform block operation" ); 
       
  1476     
       
  1477     MXIMPIdentity* identity = iXimpEventObserver.XimpPresenceContextL().ObjectFactory().NewIdentityLC();
       
  1478     
       
  1479     identity->SetIdentityL( aContactId ); 
       
  1480 
       
  1481     TXIMPRequestId reqId = iXimpEventObserver.XimpAuthorizationL().BlockPresenceForPresentityL(*identity );
       
  1482     CVIMPSTEngineRequestMapper* mapper = iXimpEventObserver.GetRequestMapper();
       
  1483     mapper->CreateRequestL(reqId, ETrue,EVIMPSTXimpOperationBlockPresenceForPresentity);// waite here
       
  1484     TInt error = iXimpEventObserver.GetCompletedReqResult(); // get the result error
       
  1485     														 //Contact will be added to blocked list manager,
       
  1486     //Delete avatar of contact
       
  1487     if(KErrNone == error && IsAvatarSupported())
       
  1488 	    {
       
  1489    		MVIMPSTStorageServiceView* storage = 
       
  1490     		CVIMPSTStorageManagerFactory::ContactListInterfaceL(iServiceId);
       
  1491 		storage->UpdateAvatarL(aContactId,KNullDesC8);
       
  1492 	    }
       
  1493     //when pres. cache call will come.
       
  1494     CleanupStack::PopAndDestroy(); // identity    
       
  1495     return error;
       
  1496     }
       
  1497 
       
  1498 // ---------------------------------------------------------------------------
       
  1499 // CVIMPSTEnginePresenceSubService::RemoveFromBlockListL
       
  1500 // ---------------------------------------------------------------------------
       
  1501 //
       
  1502 TInt CVIMPSTEnginePresenceSubService::RemoveFromBlockListL( const TDesC& aUriOfTheContact )
       
  1503     {
       
  1504 	TRACER_AUTO;
       
  1505     if(TVIMPSTEnums::ESVCERegistered != iServiceState)
       
  1506        return KErrNotSupported;
       
  1507     //if aUriOfTheCOntact is zero.
       
  1508     if( 0 == aUriOfTheContact.Length())
       
  1509        return KErrNotFound;
       
  1510     
       
  1511     TRACE(" -> aUriOfTheContact: %s" , &aUriOfTheContact);
       
  1512     TRACE( " -> perform unblock operation"  );  
       
  1513     
       
  1514     MXIMPIdentity* identity = iXimpEventObserver.XimpPresenceContextL().ObjectFactory().NewIdentityLC();
       
  1515     identity->SetIdentityL( aUriOfTheContact ); 
       
  1516    
       
  1517     TXIMPRequestId reqId = iXimpEventObserver.XimpAuthorizationL().CancelPresenceBlockFromPresentityL(*identity );
       
  1518     CVIMPSTEngineRequestMapper* mapper = iXimpEventObserver.GetRequestMapper();
       
  1519     mapper->CreateRequestL(reqId, ETrue,EVIMPSTXimpOperationCancelPresenceBlockFromPresentity);// waite here
       
  1520     TInt error = iXimpEventObserver.GetCompletedReqResult(); // get the result error
       
  1521     //if blocked contact is unblocked, then remove it from
       
  1522     //locally maintained blocked list..
       
  1523     if(KErrNone == error)
       
  1524 	    {
       
  1525 	    iBlockedListMgr->RemoveFromBlockListL(aUriOfTheContact);
       
  1526 	    }
       
  1527     CleanupStack::PopAndDestroy( 1 ); // identity    
       
  1528     
       
  1529     return error;
       
  1530     }
       
  1531     
       
  1532 // ---------------------------------------------------------------------------
       
  1533 // CVIMPSTEnginePresenceSubService::GetBlockedList
       
  1534 // ---------------------------------------------------------------------------
       
  1535 //
       
  1536 RPointerArray<HBufC>* CVIMPSTEnginePresenceSubService::GetBlockedList()
       
  1537 	{
       
  1538 	if(iBlockedListMgr)
       
  1539 		{
       
  1540 		return iBlockedListMgr->BlockedList();
       
  1541 		}
       
  1542 	return NULL;	
       
  1543 	}
       
  1544 
       
  1545 // ---------------------------------------------------------------------------
       
  1546 // CVIMPSTEnginePresenceSubService::GetBlockedList
       
  1547 // ---------------------------------------------------------------------------
       
  1548 //
       
  1549 void CVIMPSTEnginePresenceSubService::ResetBlockedListManagerL()
       
  1550 	{
       
  1551 	iBlockedListMgr->ResetL();
       
  1552 	}
       
  1553 	
       
  1554 // ---------------------------------------------------------------------------
       
  1555 // CVIMPSTEnginePresenceSubService::FetchBlockedListFromServer
       
  1556 // ---------------------------------------------------------------------------
       
  1557 //
       
  1558 void CVIMPSTEnginePresenceSubService::FetchBlockedListFromServerL(MVIMPSTEngineBlockedListFetchEventObserver* aOb)
       
  1559 	{
       
  1560 	TRACER_AUTO;
       
  1561 	if(EFalse == iBlockListFetchReqPending)
       
  1562 		{
       
  1563 		//set the observer to give call back; Fetch from server is completed.
       
  1564 		iBlockedListObserver = aOb;
       
  1565 	
       
  1566 		//unsubscribe blocked list.
       
  1567 	    TXIMPRequestId operationId = TXIMPRequestId::Null();
       
  1568 	    operationId = iXimpEventObserver.XimpAuthorizationL().UnsubscribePresenceBlockListL();
       
  1569 	    CVIMPSTEngineRequestMapper* requestMapper =iXimpEventObserver.GetRequestMapper();
       
  1570 	    requestMapper->CreateRequestL(operationId,ETrue,EVIMPSTXimpOperationGetBlockList);
       
  1571 		
       
  1572 		iBlockListFetchReqPending = ETrue; 
       
  1573 		//subscribe again to get fresh blocked list from server.
       
  1574 		RetrieveBlockListL();
       
  1575 		}
       
  1576 	}
       
  1577 	
       
  1578 	
       
  1579 // ---------------------------------------------------------------------------
       
  1580 // CVIMPSTEnginePresenceSubService::GetKeyFieldsAndValuesL
       
  1581 // ---------------------------------------------------------------------------
       
  1582 //
       
  1583 void CVIMPSTEnginePresenceSubService::GetKeyFieldsAndValuesL(MPresenceBuddyInfo2& aPresenceBuddyInfo,TVIMPSTEnums::TOnlineStatus &aStatus)
       
  1584     {
       
  1585 	TRACER_AUTO;
       
  1586     TPtrC8 value = aPresenceBuddyInfo.GetAnyField( KExtensionKey());
       
  1587     // At any point of time fro remote and blocked contact only one of the keys
       
  1588     // KPendingRequestExtensionValue/KBlockedExtensionValue will be assigned, and not both the keys.
       
  1589     if(value.Compare( KPendingRequestExtensionValue ) == 0)
       
  1590         {
       
  1591         aStatus = TVIMPSTEnums::EPending;
       
  1592         }
       
  1593     else if(value.CompareF( KBlockedExtensionValue ) == 0)
       
  1594         {
       
  1595         aStatus = TVIMPSTEnums::EBlocked;
       
  1596         }
       
  1597     else if(value.CompareF( KServiceExtensionValue ) == 0)
       
  1598         {
       
  1599         aStatus = TVIMPSTEnums::EServiceOut;
       
  1600         }
       
  1601     else if(value.CompareF( KCallForwardExtensionValue ) == 0)
       
  1602         {
       
  1603         aStatus = TVIMPSTEnums::ECallForward;
       
  1604         }
       
  1605     else
       
  1606 		{
       
  1607 		aStatus  = TVIMPSTEnums::EOffline;
       
  1608 		}
       
  1609     }
       
  1610 	
       
  1611 // ---------------------------------------------------------------------------
       
  1612 // CVIMPSTEnginePresenceSubService::UpdatePresenceStateL
       
  1613 // ---------------------------------------------------------------------------
       
  1614 //
       
  1615 void CVIMPSTEnginePresenceSubService::UpdatePresenceStateL()
       
  1616     {
       
  1617 	TRACER_AUTO;
       
  1618     //inform ui about the state change from updatingcontacts to registered.
       
  1619     iServiceState = TVIMPSTEnums::ESVCERegistered;    
       
  1620     iObserver.HandleServceConnectionEventL();
       
  1621     
       
  1622     TRACE( " -> HandleContactFetchedL:state is ESVCERegistered" ); 
       
  1623     }
       
  1624 
       
  1625 // ---------------------------------------------------------------------------
       
  1626 // CVIMPSTEnginePresenceSubService::FetchPresenceOfSingleContactL
       
  1627 // ---------------------------------------------------------------------------
       
  1628 //
       
  1629 void CVIMPSTEnginePresenceSubService::FetchPresenceOfSingleContactL(const TDesC& aContact)
       
  1630     {
       
  1631 	TRACER_AUTO;
       
  1632     if(aContact.Length() && iServiceName->Length())
       
  1633         {
       
  1634         HBufC* name = HBufC::NewLC( iServiceName->Length() + KColon().Length() + aContact.Length()  ); // 1. on to cleanup stack
       
  1635         TPtr namePtr( name->Des() );
       
  1636         namePtr.Zero();
       
  1637         // append the service name followed by user id ,seperated by colon
       
  1638         namePtr.Append(*iServiceName);
       
  1639         namePtr.Append(KColon);
       
  1640         namePtr.Append(aContact);
       
  1641         TRACE( " namePtr = %S", &namePtr);  
       
  1642         MPresenceBuddyInfo2* presenceBuddyInfo = iPresenceCacheReader->PresenceInfoLC(namePtr); // 2. on to cleanupstack
       
  1643         if ( presenceBuddyInfo )
       
  1644             {
       
  1645         TRACE("presenceBuddyInfo");            
       
  1646             DoHandlePresenceNotificationL(*presenceBuddyInfo);
       
  1647             CleanupStack::PopAndDestroy();  // presenceBuddyInfo
       
  1648             TRACE("presenceBuddyInfo end");  
       
  1649             }
       
  1650         CleanupStack::PopAndDestroy(name);  // name
       
  1651         }
       
  1652     }
       
  1653 
       
  1654 // ---------------------------------------------------------------------------
       
  1655 // CVIMPSTEnginePresenceSubService::UnsubscribeListsL
       
  1656 // ---------------------------------------------------------------------------
       
  1657 //
       
  1658 void CVIMPSTEnginePresenceSubService::UnsubscribeListsL()
       
  1659     {
       
  1660     TRACER_AUTO;
       
  1661 
       
  1662     // It is assumed here that buddy list and authorization list has been always
       
  1663     // subscribed if bind has been done. Caller of this function must check
       
  1664     // that session has been bound before calling this.
       
  1665     TBuf<KUriMaxLength> buddyGroupBuffer( KFriendList );
       
  1666     MXIMPIdentity* buddyGroupList = iXimpEventObserver.XimpPresenceContextL().ObjectFactory().NewIdentityLC();
       
  1667     __ASSERT_ALWAYS( buddyGroupList , User::Leave( KErrNoMemory ) );        
       
  1668     buddyGroupList->SetIdentityL( buddyGroupBuffer );        
       
  1669     iXimpEventObserver.XimpPresentityGroupsL().UnsubscribePresentityGroupContentL(
       
  1670                    *buddyGroupList );
       
  1671     CleanupStack::PopAndDestroy(); // buddyGroupList
       
  1672            
       
  1673    // Auth list
       
  1674    if(iSubscribeToAuthList)
       
  1675        {
       
  1676        TRACE("unsubscribe auth list");
       
  1677        iSubscribeToAuthList = EFalse;
       
  1678       iXimpEventObserver.XimpAuthorizationL().UnsubscribePresenceGrantRequestListL();  
       
  1679        }
       
  1680  
       
  1681    // block list
       
  1682    TRACE( "check if block is supported");
       
  1683    TInt supportedFeatures = iXimpEventObserver.GetSupportedFeatures();
       
  1684    if ( (EVIMPSTFeatureBlock & supportedFeatures) && (EVIMPSTFeatureUnBlock & supportedFeatures) )
       
  1685        {
       
  1686        TRACE( "unsubscribe block list");
       
  1687        iXimpEventObserver.XimpAuthorizationL().UnsubscribePresenceBlockListL();        
       
  1688        }
       
  1689     
       
  1690     }
       
  1691 
       
  1692 // End of file