simpledatamodeladapter/src/presencepluginentitywatcher.cpp
branchRCL_3
changeset 18 fbd2e7cec7ef
parent 0 c8caa15ef882
equal deleted inserted replaced
17:2669f8761a99 18:fbd2e7cec7ef
       
     1 /*
       
     2 * Copyright (c) 2006 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:  IETF SIMPLE Protocol implementation for XIMP Framework
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32std.h>
       
    19 #include <e32svr.h>
       
    20 #include <utf.h>
       
    21 #include <ximpdatasubscriptionstate.h>
       
    22 #include <protocolpresencewatchingdatahost.h>
       
    23 #include <ximpobjectfactory.h>
       
    24 #include <presenceinfo.h>
       
    25 #include <ximpprotocolconnectionhost.h>
       
    26 #include <protocolpresencedatahost.h>
       
    27 #include <presenceobjectfactory.h>
       
    28 #include <ximpstatus.h>
       
    29 #include <ximpidentity.h>
       
    30 #include <simplefactory.h>
       
    31 #include <msimplewatcher.h>
       
    32 #include <msimpledocument.h>
       
    33 #include <msimpleconnection.h>
       
    34 #include <simpleerrors.h>
       
    35 #include <avabilitytext.h>
       
    36 
       
    37 #include "presencepluginentitywatcher.h"
       
    38 #include "presenceplugindata.h"
       
    39 #include "presencepluginwatcher.h"
       
    40 
       
    41 // ======== MEMBER FUNCTIONS ========
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // CPresencePluginEntityWatcher::CPresencePluginEntityWatcher()
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 CPresencePluginEntityWatcher::CPresencePluginEntityWatcher(
       
    48     MPresencePluginConnectionObs& aObs,
       
    49     MSimpleConnection& aConn,
       
    50     CPresencePluginWatcher& aWatcher )
       
    51     :iConnObs(aObs), iConnection(aConn),
       
    52     iPluginWatcher(aWatcher), iOperation( EPluginIdle )
       
    53     {
       
    54     }
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // CPresencePluginEntityWatcher::ConstructL()
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 void CPresencePluginEntityWatcher::ConstructL( 
       
    61     CPresencePluginData* aPresenceData )
       
    62     { 
       
    63     DP_SDA("CPresencePluginEntityWatcher::ConstructL - begin");
       
    64     iWatcher = TSimpleFactory::NewWatcherL( iConnection, *this );
       
    65     iPresenceData = aPresenceData; 
       
    66     DP_SDA("CPresencePluginEntityWatcher::ConstructL - end");
       
    67     }
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // CPresencePluginEntityWatcher::NewL()
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 CPresencePluginEntityWatcher* CPresencePluginEntityWatcher::NewL(
       
    74     MPresencePluginConnectionObs& aObs,
       
    75     MSimpleConnection& aConn,
       
    76     CPresencePluginWatcher& aWatcher,
       
    77     CPresencePluginData* aPresenceData )
       
    78     {
       
    79     DP_SDA("CPresencePluginEntityWatcher::NewL");
       
    80     CPresencePluginEntityWatcher* self =
       
    81         new( ELeave ) CPresencePluginEntityWatcher( aObs, aConn, aWatcher );
       
    82     CleanupStack::PushL( self );
       
    83     self->ConstructL( aPresenceData );
       
    84     CleanupStack::Pop( self );
       
    85     return self;
       
    86     }
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // CPresencePluginEntityWatcher::~CPresencePluginEntityWatcher()
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 CPresencePluginEntityWatcher::~CPresencePluginEntityWatcher()
       
    93     {
       
    94     DP_SDA("CPresencePluginEntityWatcher::~CPresencePluginEntityWatcher");
       
    95     if ( iWatcher )
       
    96         {
       
    97         iWatcher->Close();
       
    98         }
       
    99     delete iEntityId;
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // CPresencePluginEntityWatcher::StartSubscribeL()
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 void CPresencePluginEntityWatcher::StartSubscribeL(
       
   107     const TDesC8& aPresentityId )
       
   108     {
       
   109     DP_SDA("CPresencePluginEntityWatcher::StartSubscribeL");
       
   110     delete iEntityId; 
       
   111     iEntityId = NULL; 
       
   112     DP_SDA(" StartSubscribeL - entity id deleted");
       
   113     
       
   114     TRAPD( error, iSimpleId = iWatcher->SubscribeL( 
       
   115         aPresentityId, NULL, ETrue, EFalse ););
       
   116     DP_SDA2("StartSubscribeL subscribe error %d",error);
       
   117     
       
   118     if( KErrNone != error )
       
   119         {
       
   120         DP_SDA("CPresencePluginEntityWatcher::StartSubscribeL ERROR");
       
   121         if( KErrInUse == error )
       
   122             {
       
   123             DP_SDA(" iWatcher is in use try to close and restart");
       
   124             iWatcher->Close();
       
   125             iWatcher = TSimpleFactory::NewWatcherL( iConnection, *this );
       
   126             DP_SDA("StartSubscribeL Second try after creating iWatcher again");
       
   127             error = KErrNone;
       
   128             TRAP( error, iSimpleId = iWatcher->GetPresenceL( 
       
   129                 aPresentityId, NULL, EFalse ););
       
   130             DP_SDA2("iWatcher->GetPresenceL error2 = %d",error );
       
   131             }
       
   132         }
       
   133     
       
   134     // Save entity id after successful call 
       
   135     DP_SDA("StartSubscribeL - allocate entity id");
       
   136     iEntityId = aPresentityId.AllocL();
       
   137     DP_SDA("StartSubscribeL - new entity id stored");
       
   138     
       
   139     iOperation = EPluginStart;
       
   140     
       
   141     DP_SDA("CPresencePluginEntityWatcher::StartSubscribeL End");
       
   142     }
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // CPresencePluginEntityWatcher::StartSubscribeL()
       
   146 // ---------------------------------------------------------------------------
       
   147 //
       
   148 void CPresencePluginEntityWatcher::StartSubscribeL(
       
   149     const TDesC8& aPresentityId, TRequestStatus& aClientRequst )
       
   150     {
       
   151     DP_SDA("CPresencePluginEntityWatcher::StartSubscribeL 2");
       
   152     delete iEntityId; 
       
   153     iEntityId = NULL; 
       
   154     DP_SDA("CPresencePluginEntityWatcher::StartSubscribeL - entity id deleted");
       
   155     
       
   156     iClientStatus = &aClientRequst;
       
   157     *iClientStatus = KRequestPending; // wait for watcher complete
       
   158     iOperation = EPluginStartNotifyClient; 
       
   159     
       
   160     TRAPD( error, iSimpleId = iWatcher->SubscribeL( 
       
   161         aPresentityId, NULL, ETrue, EFalse ););
       
   162     DP_SDA2("StartSubscribeL subscribe error %d",error);
       
   163     
       
   164     if( KErrInUse == error )
       
   165         {
       
   166         DP_SDA("CPresencePluginEntityWatcher::StartSubscribeL ERROR");	
       
   167         DP_SDA("StartSubscribeL iWatcher is in use try to close and restart");
       
   168         iWatcher->Close();
       
   169         iWatcher = TSimpleFactory::NewWatcherL( iConnection, *this );
       
   170         DP_SDA("StartSubscribeL Second try after creating iWatcher again");
       
   171         error = KErrNone;
       
   172         TRAP( error, iSimpleId = iWatcher->GetPresenceL( 
       
   173             aPresentityId, NULL, EFalse ););
       
   174         DP_SDA2("iWatcher->GetPresenceL error2 = %d",error );
       
   175         }
       
   176     User::LeaveIfError( error );
       
   177     
       
   178     // Save entity id after successful call 
       
   179     DP_SDA("StartSubscribeL - allocate entity id");
       
   180     iEntityId = aPresentityId.AllocL(); 
       
   181     DP_SDA("StartSubscribeL - new entity id stored");
       
   182     
       
   183     DP_SDA("CPresencePluginEntityWatcher::StartSubscribeL End"); 
       
   184     }
       
   185 
       
   186 // ---------------------------------------------------------------------------
       
   187 // CPresencePluginEntityWatcher::StopSubscribeL()
       
   188 // ---------------------------------------------------------------------------
       
   189 //
       
   190 void CPresencePluginEntityWatcher::StopSubscribeL( 
       
   191     TRequestStatus& aClientRequst )
       
   192     {
       
   193     DP_SDA("CPresencePluginEntityWatcher::StopSubscribeL 2 (list)");
       
   194     iClientStatus = &aClientRequst;
       
   195     *iClientStatus = KRequestPending; // wait for watcher complete    
       
   196     
       
   197     iSimpleId = iWatcher->UnsubscribeL();
       
   198     iOperation = EPluginStop;
       
   199     DP_SDA("CPresencePluginEntityWatcher::StopSubscribeL 2 end"); 
       
   200     }
       
   201 
       
   202 // ---------------------------------------------------------------------------
       
   203 // CPresencePluginEntityWatcher::StopSubscribeL()
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 void CPresencePluginEntityWatcher::StopSubscribeL( )
       
   207     {
       
   208     DP_SDA("CPresencePluginEntityWatcher::StopSubscribeL");
       
   209     iSimpleId = iWatcher->UnsubscribeL();
       
   210     iOperation = EPluginStop;
       
   211     DP_SDA("CPresencePluginEntityWatcher::StopSubscribeL end"); 
       
   212     }
       
   213 
       
   214 // ---------------------------------------------------------------------------
       
   215 // CPresencePluginEntityWatcher::WatcherReqCompleteL
       
   216 // ---------------------------------------------------------------------------
       
   217 //
       
   218 void CPresencePluginEntityWatcher::WatcherReqCompleteL( 
       
   219     TInt /*aOpId*/, TInt aStatus )
       
   220     {
       
   221     DP_SDA("CPresencePluginEntityWatcher::WatcherReqCompleteL");
       
   222     DP_SDA2("WatcherReqCompleteL status %d",aStatus );
       
   223     TPluginEntityWatcherOperation orig = iOperation;
       
   224     DP_SDA2("WatcherReqCompleteL orig %d",orig );
       
   225     iOperation = EPluginIdle;
       
   226     
       
   227     if( !aStatus )
       
   228         {
       
   229         iPluginWatcher.AcceptL( this );
       
   230         }
       
   231     
       
   232     if ( EPluginStartNotifyClient == orig )
       
   233         {
       
   234         orig = EPluginStart;
       
   235         DP_SDA(" WatcherReqCompleteL complete client");
       
   236         CompleteClientReq( aStatus );
       
   237         }
       
   238     else if ( iClientStatus )
       
   239         {
       
   240         DP_SDA(" WatcherReqCompleteL complete client 2");
       
   241         CompleteClientReq( aStatus );
       
   242         }
       
   243     
       
   244     MSimpleWatcher::TSimpleSipSubscriptionState subscribeStatus =
       
   245         iWatcher->SipSubscriptionState();
       
   246     DP_SDA2("WatcherReqCompleteL subscribe status %d",subscribeStatus );
       
   247      	
       
   248     if ( aStatus && ( orig == EPluginStop || orig == EPluginStart &&
       
   249         MSimpleWatcher::ESimpleStateTerminated != subscribeStatus ) )
       
   250         {
       
   251         DP_SDA("CPresencePluginEntityWatcher::WatcherReqCompleteL if");
       
   252         // Delete this entity as useless
       
   253         DP_SDA("WatcherReqCompleteL DELETE WATCHER");
       
   254         iPluginWatcher.DeleteWatcher( iEntityId->Des() ); 
       
   255         //Do not call anything, 
       
   256         //since the method call above deletes this instance.
       
   257         }
       
   258     else if( !aStatus && ( MSimpleWatcher::ESimpleStateActive ==
       
   259         subscribeStatus  &&
       
   260         orig == EPluginStop ) )
       
   261         {
       
   262         DP_SDA(" WatcherReqCompleteL state STOP");	
       
   263         // Delete this entity as useless
       
   264         DP_SDA("WatcherReqCompleteL DELETE WATCHER 2");
       
   265         iPluginWatcher.DeleteWatcher( iEntityId->Des() );
       
   266         }
       
   267     else if ( MSimpleWatcher::ESimpleStatePending == subscribeStatus )
       
   268         {
       
   269         SetPendingToXIMPL();
       
   270         }
       
   271     DP_SDA("CPresencePluginEntityWatcher::WatcherReqCompleteL end");
       
   272     }
       
   273 
       
   274 // ---------------------------------------------------------------------------
       
   275 // CPresencePluginEntityWatcher::SetPendingToXIMPL
       
   276 // ---------------------------------------------------------------------------
       
   277 //
       
   278 void CPresencePluginEntityWatcher::SetPendingToXIMPL()
       
   279     {
       
   280     DP_SDA("CPresencePluginEntityWatcher::SetPendingToXIMPL");
       
   281     //check what is grand request list subscribe state
       
   282     if( iConnObs.GrandListState() )
       
   283         {
       
   284         DP_SDA("CPresencePluginEntityWatcher::SetPendingToXIMPL True");
       
   285         MProtocolPresenceWatchingDataHost& watcherHost =    
       
   286         iConnObs.ProtocolPresenceHost().WatchingDataHost();
       
   287         MPresenceInfo* prInfo = 
       
   288             iConnObs.PresenceObjectFactoryOwn().NewPresenceInfoLC();
       
   289         
       
   290         //Call set to pending
       
   291         iPresenceData->NotifyToPendingToXIMPL( 
       
   292             iConnObs.PresenceObjectFactoryOwn(),
       
   293             *prInfo );
       
   294             
       
   295         MXIMPIdentity* identity2 = iConnObs.ObjectFactory().NewIdentityLC();    
       
   296         HBufC* uniBuffer = 
       
   297             CnvUtfConverter::ConvertToUnicodeFromUtf8L( iEntityId->Des() );
       
   298         CleanupStack::PushL( uniBuffer );
       
   299         
       
   300         HBufC* withoutPrefix = iPresenceData->RemovePrefixLC( *uniBuffer );
       
   301         identity2->SetIdentityL( *withoutPrefix );    
       
   302         
       
   303         //Call presence cache writer too
       
   304         TBuf<20> buf;
       
   305         buf.Copy( KPendingRequestExtensionValue );
       
   306         
       
   307         iPresenceData->WriteStatusToCacheL( *withoutPrefix, 
       
   308             MPresenceBuddyInfo2::ENotAvailable,
       
   309             buf,
       
   310             KNullDesC() ); 
       
   311         
       
   312         // XIMP Host API callback 
       
   313         watcherHost.HandleSubscribedPresentityPresenceL( identity2, prInfo );
       
   314         
       
   315         CleanupStack::PopAndDestroy( withoutPrefix );
       
   316         CleanupStack::PopAndDestroy( uniBuffer );
       
   317         CleanupStack::Pop();        // >> identity2
       
   318         CleanupStack::Pop();        // >> prInfo
       
   319         }
       
   320     DP_SDA("CPresencePluginEntityWatcher::SipSubscriptionState end");
       
   321     }
       
   322 
       
   323 // ---------------------------------------------------------------------------
       
   324 // CPresencePluginEntityWatcher::SetActiveToXIMPL
       
   325 // ---------------------------------------------------------------------------
       
   326 //
       
   327 void CPresencePluginEntityWatcher::SetActiveToXIMPL( const TDesC& aIdentity )
       
   328     {
       
   329     DP_SDA("CPresencePluginEntityWatcher::SetActiveToXIMPL, identity");
       
   330     //check what is grand request list subscribe state
       
   331     if( iConnObs.GrandListState() )
       
   332         {
       
   333         DP_SDA("CPresencePluginEntityWatcher::SetPendingToXIMPL True");
       
   334         MProtocolPresenceWatchingDataHost& watcherHost =    
       
   335         iConnObs.ProtocolPresenceHost().WatchingDataHost();
       
   336         MPresenceInfo* prInfo = 
       
   337             iConnObs.PresenceObjectFactoryOwn().NewPresenceInfoLC();
       
   338         
       
   339         //Call set to pending
       
   340         DP_SDA(" SetPendingToXIMPL, notify active");
       
   341         iPresenceData->NotifyToActiveToXIMPL( 
       
   342             iConnObs.PresenceObjectFactoryOwn(),
       
   343             *prInfo );
       
   344 
       
   345         DP_SDA(" SetActiveToXIMPL, create new identity");
       
   346         MXIMPIdentity* identity2 = iConnObs.ObjectFactory().NewIdentityLC();    
       
   347 
       
   348         DP_SDA(" SetActiveToXIMPL, strip prefix");
       
   349         HBufC* withoutPrefix = iPresenceData->RemovePrefixLC( aIdentity );
       
   350         identity2->SetIdentityL( *withoutPrefix );    
       
   351 
       
   352         DP_SDA(" SetActiveToXIMPL, write to cache");
       
   353         //Call presence cache writer too
       
   354         iPresenceData->WriteStatusToCacheL( *withoutPrefix, 
       
   355             MPresenceBuddyInfo2::ENotAvailable,
       
   356             KDefaultAvailableStatus(),
       
   357             KNullDesC() );    
       
   358 
       
   359         DP_SDA(" SetActiveToXIMPL, ximp api callback");
       
   360         // XIMP Host API callback 
       
   361         watcherHost.HandleSubscribedPresentityPresenceL( identity2, prInfo );
       
   362         
       
   363         DP_SDA("CPresencePluginEntityWatcher::SetActiveToXIMPL, cleanup");
       
   364         CleanupStack::PopAndDestroy( withoutPrefix );
       
   365         CleanupStack::Pop();        // >> identity2
       
   366         CleanupStack::Pop();        // >> prInfo
       
   367         }   
       
   368     DP_SDA("CPresencePluginEntityWatcher::SetActiveToXIMPL end");
       
   369     }
       
   370     
       
   371 // ---------------------------------------------------------------------------
       
   372 // CPresencePluginEntityWatcher::SetTerminatedToXIMPL
       
   373 // ---------------------------------------------------------------------------
       
   374 //
       
   375 void CPresencePluginEntityWatcher::SetTerminatedToXIMPL( 
       
   376 	const TDesC& aIdentity )
       
   377     {
       
   378     DP_SDA("CPresencePluginEntityWatcher::SetTerminatedToXIMPL");
       
   379     DP_SDA2("CPresencePluginEntityWatcher::SetTerminatedToXIMPL, uri: %S", 
       
   380         &aIdentity );    
       
   381     //check what is grand request list subscribe state
       
   382     if( iConnObs.GrandListState() )
       
   383         {
       
   384         DP_SDA("CPresencePluginEntityWatcher::SetTerminatedToXIMPL True");
       
   385         MProtocolPresenceWatchingDataHost& watcherHost =    
       
   386         iConnObs.ProtocolPresenceHost().WatchingDataHost();
       
   387         MPresenceInfo* prInfo = 
       
   388             iConnObs.PresenceObjectFactoryOwn().NewPresenceInfoLC();
       
   389         
       
   390         //Call set to pending
       
   391         iPresenceData->NotifyTerminatedToXIMPL( 
       
   392             iConnObs.PresenceObjectFactoryOwn(),
       
   393             *prInfo );
       
   394 
       
   395         MXIMPIdentity* identity2 = iConnObs.ObjectFactory().NewIdentityLC();    
       
   396         
       
   397         HBufC* withoutPrefix = iPresenceData->RemovePrefixLC( aIdentity );
       
   398         identity2->SetIdentityL( *withoutPrefix );
       
   399         
       
   400         DP_SDA("SetTerminatedToXIMPL, write offline status to cache");
       
   401         iPresenceData->WriteStatusToCacheL( *withoutPrefix,
       
   402             MPresenceBuddyInfo2::ENotAvailable,
       
   403             KNullDesC(),
       
   404             KNullDesC() );
       
   405         
       
   406         // XIMP Host API callback 
       
   407         watcherHost.HandleSubscribedPresentityPresenceL( identity2, prInfo );
       
   408 
       
   409         CleanupStack::PopAndDestroy( withoutPrefix );
       
   410         CleanupStack::Pop();        // >> identity2
       
   411         CleanupStack::Pop( );        // >> prInfo
       
   412         }   
       
   413     DP_SDA("CPresencePluginEntityWatcher::SetTerminatedToXIMPL end");
       
   414     }
       
   415     
       
   416 // ---------------------------------------------------------------------------
       
   417 // CPresencePluginEntityWatcher::SetActiveToXIMPL
       
   418 // ---------------------------------------------------------------------------
       
   419 //
       
   420 void CPresencePluginEntityWatcher::SetActiveToXIMPL( 
       
   421 	MSimpleDocument& aDocument )
       
   422     {
       
   423     DP_SDA("CPresencePluginEntityWatcher::SetActiveToXIMPL, aDocument");
       
   424     // No need to check the expiration here since WatcherTerminatedL
       
   425     // is called then too.
       
   426     MProtocolPresenceWatchingDataHost& watcherHost =    
       
   427         iConnObs.ProtocolPresenceHost().WatchingDataHost();
       
   428     MPresenceInfo* prInfo = 
       
   429         iConnObs.PresenceObjectFactoryOwn().NewPresenceInfoLC();
       
   430 
       
   431     DP_SDA(" SetActiveToXIMPL, call NotifyToPrInfoL");
       
   432     
       
   433     iPresenceData->NotifyToPrInfoL( 
       
   434         iConnObs.ObjectFactory(),
       
   435         iConnObs.PresenceObjectFactoryOwn(),
       
   436         aDocument, *prInfo );
       
   437     DP_SDA(" SetActiveToXIMPL, NotifyToPrInfoL ok");
       
   438     
       
   439     // Get variables from presence info object for cache entry
       
   440     MPresenceBuddyInfo2::TAvailabilityValues availability = 
       
   441         MPresenceBuddyInfo2::ENotAvailable;
       
   442     
       
   443     HBufC* extendedAvailability = HBufC::NewLC( KBufSize255 );
       
   444     TPtr extendedAvailabilityPtr( extendedAvailability->Des() );
       
   445     
       
   446     HBufC* statusMessage = HBufC::NewLC( KBufSize255 );
       
   447     TPtr statusMessagePtr( statusMessage->Des() );
       
   448     
       
   449     DP_SDA(" SetActiveToXIMPL, NotifyToPrInfoL ok, get cache entries");
       
   450     iPresenceData->CacheEntriesFromPrInfo( *prInfo,
       
   451         availability, extendedAvailabilityPtr, statusMessagePtr );
       
   452     
       
   453     DP_SDA(" SetActiveToXIMPL, create identity");
       
   454     MXIMPIdentity* identity2 = iConnObs.ObjectFactory().NewIdentityLC();    
       
   455     HBufC* uniBuffer = 
       
   456         CnvUtfConverter::ConvertToUnicodeFromUtf8L( 
       
   457             *aDocument.EntityURI() );
       
   458     CleanupStack::PushL( uniBuffer );
       
   459     DP_SDA("CPresencePluginEntityWatcher::SetActiveToXIMPL, identity ok");
       
   460     
       
   461     DP_SDA("CPresencePluginEntityWatcher::SetActiveToXIMPL, strip prefix");
       
   462     HBufC* withoutPrefix = iPresenceData->RemovePrefixLC( *uniBuffer );
       
   463     identity2->SetIdentityL( *withoutPrefix );   
       
   464 
       
   465     //Call presence cache writer too
       
   466     iPresenceData->WriteStatusToCacheL( *withoutPrefix, availability,
       
   467          extendedAvailabilityPtr, statusMessagePtr );
       
   468     
       
   469     // XIMP Host API callback 
       
   470     watcherHost.HandleSubscribedPresentityPresenceL( identity2, prInfo );
       
   471 
       
   472     CleanupStack::PopAndDestroy( withoutPrefix );
       
   473     CleanupStack::PopAndDestroy( uniBuffer );
       
   474     CleanupStack::Pop();        // >> identity2
       
   475     
       
   476     CleanupStack::PopAndDestroy( statusMessage );
       
   477     CleanupStack::PopAndDestroy( extendedAvailability );
       
   478     
       
   479     CleanupStack::Pop();        // >> prInfo
       
   480     DP_SDA("CPresencePluginEntityWatcher::SetActiveToXIMPL end");   
       
   481     }
       
   482 
       
   483 // ---------------------------------------------------------------------------
       
   484 // CPresencePluginEntityWatcher::SetTerminatedToXIMPL
       
   485 // ---------------------------------------------------------------------------
       
   486 //
       
   487 void CPresencePluginEntityWatcher::SetTerminatedToXIMPL( )
       
   488     {
       
   489     DP_SDA("CPresencePluginEntityWatcher::SetTerminatedToXIMPL");
       
   490     DP_SDA("CPresencePluginEntityWatcher::SetTerminatedToXIMPL True");
       
   491     
       
   492     MProtocolPresenceWatchingDataHost& watcherHost = 
       
   493         iConnObs.ProtocolPresenceHost().WatchingDataHost();
       
   494     MPresenceInfo* prInfo = 
       
   495         iConnObs.PresenceObjectFactoryOwn().NewPresenceInfoLC();
       
   496     
       
   497     //Call set to pending
       
   498     iPresenceData->NotifyTerminatedToXIMPL( 
       
   499         iConnObs.PresenceObjectFactoryOwn(),
       
   500         *prInfo );
       
   501     
       
   502     MXIMPIdentity* identity2 = iConnObs.ObjectFactory().NewIdentityLC();
       
   503     HBufC* uniBuffer = 
       
   504        CnvUtfConverter::ConvertToUnicodeFromUtf8L( iEntityId->Des() );
       
   505     
       
   506     CleanupStack::PushL( uniBuffer );
       
   507     HBufC* withoutPrefix = iPresenceData->RemovePrefixLC( *uniBuffer );
       
   508     identity2->SetIdentityL( *withoutPrefix ); 
       
   509     
       
   510     //Call presence cache writer too
       
   511     iPresenceData->WriteStatusToCacheL( *withoutPrefix, 
       
   512         MPresenceBuddyInfo2::ENotAvailable,
       
   513         KInvisibleState(),
       
   514         KNullDesC() );
       
   515     
       
   516     // XIMP Host API callback 
       
   517     watcherHost.HandleSubscribedPresentityPresenceL( identity2, prInfo );
       
   518     
       
   519     DP_SDA2("SetTerminatedToXIMPL2: ident %S", &identity2->Identity() );
       
   520     CleanupStack::PopAndDestroy( withoutPrefix );
       
   521     CleanupStack::PopAndDestroy( uniBuffer );
       
   522     CleanupStack::Pop(); // >> identity21
       
   523     CleanupStack::Pop(); // >> prInfo
       
   524     
       
   525     DP_SDA("CPresencePluginEntityWatcher::SetTerminatedToXIMPL end");
       
   526     }
       
   527 
       
   528 // ---------------------------------------------------------------------------
       
   529 // CPresencePluginEntityWatcher::WatcherNotificationL()
       
   530 // ---------------------------------------------------------------------------
       
   531 //
       
   532 void CPresencePluginEntityWatcher::WatcherNotificationL( 
       
   533     MSimpleDocument& aDocument )
       
   534     {
       
   535     DP_SDA("CPresencePluginEntityWatcher::WatcherNotificationL");
       
   536     
       
   537     MSimpleWatcher::TSimpleSipSubscriptionState subscribeStatus =
       
   538         iWatcher->SipSubscriptionState();
       
   539     DP_SDA2("WatcherNotificationL subscribe status %d",subscribeStatus );
       
   540     
       
   541     //If subscribeStatus = pending
       
   542     if( MSimpleWatcher::ESimpleStatePending == subscribeStatus )
       
   543         {
       
   544         // Pending
       
   545         DP_SDA("CPresencePluginEntityWatcher::WatcherNotificationL PENDING");
       
   546         SetPendingToXIMPL();
       
   547         }
       
   548     else if( MSimpleWatcher::ESimpleStateTerminated == subscribeStatus )
       
   549         {
       
   550         DP_SDA("CPresencePluginEntityWatcher::WatcherNotificationL TERMINATED");
       
   551         SetTerminatedToXIMPL( );
       
   552         }
       
   553     else
       
   554         {
       
   555         //Active
       
   556         DP_SDA("WatcherNotificationL normal Case");
       
   557         SetActiveToXIMPL( aDocument );
       
   558         }
       
   559    	DP_SDA("CPresencePluginEntityWatcher::WatcherNotificationL end");
       
   560     }
       
   561 
       
   562 // ---------------------------------------------------------------------------
       
   563 // CPresencePluginEntityWatcher::WatcherListNotificationL()
       
   564 // ---------------------------------------------------------------------------
       
   565 //
       
   566 void CPresencePluginEntityWatcher::WatcherListNotificationL(
       
   567     MSimplePresenceList& aList )
       
   568     {
       
   569     DP_SDA("CPresencePluginEntityWatcher::WatcherListNotificationL");
       
   570     // call all the necessary callbacks, for new data + teminated ones.
       
   571     
       
   572     MProtocolPresenceWatchingDataHost& watcherHost = 
       
   573          iConnObs.Host()->ProtocolPresenceDataHost().WatchingDataHost();
       
   574     
       
   575     RPointerArray<MPresenceInfo> entities;
       
   576     RPointerArray<MXIMPIdentity> terminated;
       
   577     RPointerArray<MXIMPIdentity> actives;
       
   578     
       
   579     TRAPD( err, iPresenceData->NotifyListToPrInfoL(
       
   580          iConnObs.ObjectFactory(),iConnObs.PresenceObjectFactoryOwn(),
       
   581           aList, entities, actives, terminated ));
       
   582     if ( err )
       
   583         {
       
   584         entities.ResetAndDestroy();
       
   585         terminated.ResetAndDestroy();
       
   586         actives.ResetAndDestroy();
       
   587         entities.Close();
       
   588         terminated.Close();
       
   589         actives.Close();
       
   590         return;
       
   591         }
       
   592     
       
   593     // Start to collect data for HandleSubscribedPresentityPresenceL
       
   594     TInt counter = entities.Count();
       
   595     for ( TInt i = counter-1 ; i>=0; i-- )
       
   596         {
       
   597         HBufC* withoutPrefix = iPresenceData->RemovePrefixLC( 
       
   598             actives[i]->Identity() );
       
   599         actives[i]->SetIdentityL( *withoutPrefix );    
       
   600         
       
   601         watcherHost.HandleSubscribedPresentityPresenceL(
       
   602             actives[i], entities[i] );
       
   603         // Owenership is transferred
       
   604         actives.Remove(i);
       
   605         entities.Remove(i);
       
   606         CleanupStack::PopAndDestroy( withoutPrefix );
       
   607         }
       
   608     
       
   609     entities.ResetAndDestroy();
       
   610     terminated.ResetAndDestroy();
       
   611     actives.ResetAndDestroy();
       
   612     
       
   613     entities.Close();
       
   614     terminated.Close();
       
   615     actives.Close();
       
   616     }
       
   617 
       
   618 // ---------------------------------------------------------------------------
       
   619 // CPresencePluginEntityWatcher::WatcherTerminatedL()
       
   620 // ---------------------------------------------------------------------------
       
   621 //
       
   622 void CPresencePluginEntityWatcher::WatcherTerminatedL(
       
   623     TInt /*aOpId*/, TInt aReason )
       
   624     {
       
   625     DP_SDA2("CPresencePluginEntityWatcher::WatcherTerminatedL -reason: %d", aReason );
       
   626     
       
   627     MProtocolPresenceWatchingDataHost& watcherHost = 
       
   628         iConnObs.Host()->ProtocolPresenceDataHost().WatchingDataHost();
       
   629     
       
   630     MXIMPDataSubscriptionState *state =
       
   631         iConnObs.ObjectFactory().NewDataSubscriptionStateLC();
       
   632     MXIMPStatus* status = iConnObs.ObjectFactory().NewStatusLC();
       
   633     state->SetSubscriptionStateL( 
       
   634         MXIMPDataSubscriptionState::ESubscriptionInactive );
       
   635     status->SetResultCode( aReason );
       
   636     MXIMPIdentity* identity = iConnObs.ObjectFactory().NewIdentityLC();
       
   637     
       
   638     HBufC16* buf16 = HBufC16::NewLC( PresentityId().Length());
       
   639     buf16->Des().Copy( PresentityId() );
       
   640     identity->SetIdentityL( buf16->Des() );
       
   641     
       
   642     //Visualize closed state OpenSer
       
   643     SetTerminatedToXIMPL( buf16->Des() );
       
   644     
       
   645     CleanupStack::PopAndDestroy( buf16 );
       
   646     watcherHost.SetPresentityPresenceDataSubscriptionStateL( 
       
   647         identity, state, status );
       
   648     DP_SDA("CPresencePluginEntityWatcher::WatcherTerminatedL pop");
       
   649     CleanupStack::Pop( 3 );
       
   650     
       
   651     iPluginWatcher.DeleteWatcher( iEntityId->Des() ); 
       
   652     
       
   653     DP_SDA("CPresencePluginEntityWatcher::WatcherTerminatedL end");
       
   654     }
       
   655 
       
   656 // ---------------------------------------------------------------------------
       
   657 // CPresencePluginWatcher::CompleteClientReq()
       
   658 // ---------------------------------------------------------------------------
       
   659 //
       
   660 void CPresencePluginEntityWatcher::CompleteClientReq( TInt aStatus )
       
   661     {
       
   662     DP_SDA("CPresencePluginEntityWatcher::CompleteClientReq"); 
       
   663     iOperation = EPluginIdle;    
       
   664     TRequestStatus* s = iClientStatus;
       
   665     User::RequestComplete( s, aStatus );
       
   666     iClientStatus = NULL;
       
   667     DP_SDA("CPresencePluginEntityWatcher::CompleteClientReq out"); 
       
   668     }
       
   669 
       
   670 // ---------------------------------------------------------------------------
       
   671 // CPresencePluginEntityWatcher::PresentityId
       
   672 // ---------------------------------------------------------------------------
       
   673 //
       
   674 TPtrC8 CPresencePluginEntityWatcher::PresentityId( )
       
   675     {
       
   676     DP_SDA("CPresencePluginEntityWatcher::PresentityId");
       
   677     return iEntityId ? iEntityId->Des() : TPtrC8();
       
   678     }
       
   679 
       
   680 // ---------------------------------------------------------------------------
       
   681 // CPresencePluginEntityWatcher::XIMPRequestId
       
   682 // ---------------------------------------------------------------------------
       
   683 //
       
   684 TXIMPRequestId CPresencePluginEntityWatcher::XIMPRequestId( )
       
   685     {
       
   686     return iXIMPId;  
       
   687     }
       
   688 
       
   689 // End of file