simpledatamodeladapter/src/presencepluginpublisher.cpp
changeset 0 c8caa15ef882
child 7 895b8e06c769
child 12 e6a66db4e9d0
equal deleted inserted replaced
-1:000000000000 0:c8caa15ef882
       
     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 
       
    19 #include <e32std.h>
       
    20 #include <utf.h>
       
    21 #include <ximpdatasubscriptionstate.h>	  
       
    22 #include <protocolpresencepublishingdatahost.h>
       
    23 #include <protocolpresencedatahost.h>
       
    24 #include <ximpobjectfactory.h>
       
    25 #include <presenceobjectfactory.h>
       
    26 #include <presenceinfo.h>
       
    27 #include <personpresenceinfo.h>
       
    28 #include <ximpprotocolconnectionhost.h>
       
    29 #include <ximpstatus.h>
       
    30 #include <ximperrors.hrh>
       
    31 #include <ximpidentity.h>
       
    32 #include <ximpobjectcollection.h>
       
    33 #include <presencewatcherinfo.h>
       
    34 #include <simpleutils.h>   // For KSimplePDM
       
    35 #include <simplefactory.h>
       
    36 #include <msimpledocument.h>
       
    37 #include <msimplepublisher.h>
       
    38 #include <msimplewatcher.h>
       
    39 #include <msimplewinfo.h>
       
    40 #include <msimpleelement.h>
       
    41 #include <msimpleconnection.h>
       
    42 #include <simpleerrors.h>
       
    43 #include <avabilitytext.h>
       
    44 
       
    45 #include "presenceplugincommon.h"
       
    46 #include "presencepluginpublisher.h"
       
    47 #include "presencepluginwinfo.h"
       
    48 #include "presenceplugindata.h"
       
    49 #include "presencepluginxdmutils.h"
       
    50 #include "presencepluginwatcherinfo.h"
       
    51 #include "presenceplugincommon.h"
       
    52 
       
    53 // ======== MEMBER FUNCTIONS ========
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // CPresencePluginPublisher::CPresencePluginPublisher()
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 CPresencePluginPublisher::CPresencePluginPublisher(
       
    60     MPresencePluginConnectionObs& aObs,
       
    61     MSimpleConnection& aConn )
       
    62     : CActive( CActive::EPriorityStandard ),
       
    63     iConnObs(aObs), iConnection(aConn),
       
    64     iPublisher( NULL ), iWatcher( NULL ),
       
    65     iSubscribed(EFalse), iSubscribedOwn(EFalse), 
       
    66     iPublished(EFalse), iRePublish(EFalse),
       
    67     iWatcherList( CPresencePluginWatcherInfo::LinkOffset() ) 
       
    68     {
       
    69     DP_SDA("CPresencePluginPublisher::CPresencePluginPublisher ");
       
    70     CActiveScheduler::Add(this);        
       
    71     }
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 // CPresencePluginPublisher::ConstructL()
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 void CPresencePluginPublisher::ConstructL( CPresencePluginData* aPresenceData )
       
    78     {
       
    79     DP_SDA("CPresencePluginPublisher::ConstructL ");
       
    80     iWatcher = TSimpleFactory::NewWatcherL( iConnection, *this );
       
    81     iPublisher = TSimpleFactory::NewPublisherL( iConnection, *this );       
       
    82     iPresenceData = aPresenceData;
       
    83     }
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // CPresencePluginPublisher::NewL()
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 CPresencePluginPublisher* CPresencePluginPublisher::NewL(
       
    90     MPresencePluginConnectionObs& aObs,
       
    91     MSimpleConnection& aConn,
       
    92     CPresencePluginData* aPresenceData )
       
    93     {
       
    94     CPresencePluginPublisher* self =
       
    95         new( ELeave ) CPresencePluginPublisher( aObs, aConn );
       
    96     CleanupStack::PushL( self );
       
    97     self->ConstructL( aPresenceData );
       
    98     CleanupStack::Pop( self );
       
    99     return self;
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // CPresencePluginPublisher::~CPresencePluginPublisher()
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 CPresencePluginPublisher::~CPresencePluginPublisher()
       
   107     {
       
   108     DP_SDA("CPresencePluginPublisher::~CPresencePluginPublisher");
       
   109     
       
   110     if ( iDocument )
       
   111         {
       
   112         iDocument->Close();
       
   113         }
       
   114 
       
   115     if ( iPublisher )
       
   116         {
       
   117         iPublisher->Close();
       
   118         }
       
   119         
       
   120     if ( iWatcher )
       
   121         {
       
   122         iWatcher->Close();
       
   123         }
       
   124          
       
   125     DeleteWatchers();
       
   126     }
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // CPresencePluginPublisher::DeleteWatchers
       
   130 // ---------------------------------------------------------------------------
       
   131 //
       
   132 void CPresencePluginPublisher::DeleteWatchers()
       
   133     {
       
   134     DP_SDA("CPresencePluginPublisher::DeleteWatchers");
       
   135     // Delete all buffered transaction requests
       
   136     TDblQueIter<CPresencePluginWatcherInfo> rIter( iWatcherList );
       
   137     rIter.SetToFirst();
       
   138 
       
   139     while ( rIter )
       
   140         {
       
   141         DP_SDA("CPresencePluginPublisher::DeleteWatchers while"); 
       
   142         CPresencePluginWatcherInfo* w = rIter;
       
   143         rIter++;
       
   144         // delete wathcer info
       
   145         w->Destroy();
       
   146         } 
       
   147     DP_SDA("CPresencePluginPublisher::DeleteWatchers end"); 
       
   148     } 
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 // CPresencePluginPublisher::AddWatcherIfNotExistsL
       
   152 // ---------------------------------------------------------------------------
       
   153 //
       
   154 void CPresencePluginPublisher::AddWatcherIfNotExistsL( 
       
   155     const TDesC8& aId,
       
   156     const TDesC& aSipId )
       
   157     {
       
   158     DP_SDA("CPresencePluginPublisher::AddWatcherIfNotExistsL");
       
   159     // Delete all buffered transaction requests
       
   160     TDblQueIter<CPresencePluginWatcherInfo> rIter( iWatcherList );
       
   161     rIter.SetToFirst();
       
   162     
       
   163     TBool found(EFalse);
       
   164 
       
   165     while ( rIter )
       
   166         {
       
   167         CPresencePluginWatcherInfo* w = rIter;
       
   168         rIter++;
       
   169         found = w->Match( aId, aSipId );
       
   170         if ( found )
       
   171             {
       
   172             DP_SDA("CPresencePluginPublisher::AddWatcherIfNotExistsL found");
       
   173             break;
       
   174             }
       
   175         }
       
   176     if ( !found )
       
   177         {
       
   178         DP_SDA("CPresencePluginPublisher::AddWatcherIfNotExistsL !found");       
       
   179         CPresencePluginWatcherInfo* w =
       
   180             CPresencePluginWatcherInfo::NewL( aId, aSipId );
       
   181         iWatcherList.AddLast( *w );
       
   182         } 
       
   183     }
       
   184 
       
   185 // ---------------------------------------------------------------------------
       
   186 // CPresencePluginPublisher::RemoveWatcherIfExistsL
       
   187 // ---------------------------------------------------------------------------
       
   188 //
       
   189 void CPresencePluginPublisher::RemoveWatcherIfExists( 
       
   190     const TDesC8& aId, const TDesC& aSipId )
       
   191     {
       
   192     DP_SDA("CPresencePluginPublisher::RemoveWatcherIfExistsL");
       
   193     // Delete all buffered transaction requests
       
   194     TDblQueIter<CPresencePluginWatcherInfo> rIter( iWatcherList );
       
   195     rIter.SetToFirst();
       
   196     
       
   197     TBool found(EFalse);
       
   198 
       
   199     while ( rIter )
       
   200         {
       
   201         CPresencePluginWatcherInfo* w = rIter;
       
   202         rIter++;
       
   203         // delete wathcer info
       
   204         found = w->Match( aId, aSipId );
       
   205         if ( found )
       
   206             {         
       
   207             w->Destroy();
       
   208             break;
       
   209             }
       
   210         }    
       
   211     }  
       
   212     
       
   213 // ---------------------------------------------------------------------------
       
   214 // CPresencePluginPublisher::DoPublishOwnPresenceL()
       
   215 // ---------------------------------------------------------------------------
       
   216 //
       
   217 void CPresencePluginPublisher::DoPublishOwnPresenceL(
       
   218     const MPresenceInfo& aOwnPresence,
       
   219     TXIMPRequestId aReqId )
       
   220     {
       
   221     DP_SDA("CPresencePluginPublisher::DoPublishOwnPresenceL");
       
   222     const MPersonPresenceInfo* persInfo = aOwnPresence.PersonPresence();
       
   223     if ( !persInfo )
       
   224         {
       
   225         // Notice: error codes
       
   226         CompleteXIMPReq( KErrArgument );
       
   227         return;
       
   228         }
       
   229     InitializeSimpleDocumentL( );    
       
   230     AddSimpleDocumentPersL( persInfo ); 
       
   231     
       
   232     StartXdmOperationL( aReqId );
       
   233     iOperation = EPublishOwn;                 
       
   234     DP_SDA("CPresencePluginPublisher::DoPublishOwnPresenceL end");
       
   235     }
       
   236 
       
   237 // ---------------------------------------------------------------------------
       
   238 // CPresencePluginPublisher::DoSubscribeOwnPresenceL()
       
   239 // ---------------------------------------------------------------------------
       
   240 //
       
   241 void CPresencePluginPublisher::DoSubscribeOwnPresenceL(
       
   242     const MPresenceInfoFilter& /*aPif*/,   // notice: later
       
   243     TXIMPRequestId aReqId )
       
   244     {
       
   245     DP_SDA("CPresencePluginPublisher::DoSubscribeOwnPresenceL");         
       
   246     StartXdmOperationL( aReqId );    
       
   247     iOperation = ESubscribeOwn; 
       
   248     }
       
   249 
       
   250 // ---------------------------------------------------------------------------
       
   251 // CPresencePluginPublisher::DoUpdateOwnPresenceSubscriptionPifL()
       
   252 // ---------------------------------------------------------------------------
       
   253 //
       
   254 void CPresencePluginPublisher::DoUpdateOwnPresenceSubscriptionPifL(
       
   255     const MPresenceInfoFilter& /*aPif*/,
       
   256     TXIMPRequestId /*aReqId*/ )
       
   257     {
       
   258     // Notice: later
       
   259     User::Leave( KErrNotSupported );
       
   260     }
       
   261 
       
   262 // ---------------------------------------------------------------------------
       
   263 // CPresencePluginPublisher::DoUnsubscribeOwnPresenceL()
       
   264 // ---------------------------------------------------------------------------
       
   265 //
       
   266 void CPresencePluginPublisher::DoUnsubscribeOwnPresenceL(
       
   267     TXIMPRequestId aReqId )
       
   268     {     
       
   269     DP_SDA("CPresencePluginPublisher::DoUnsubscribeOwnPresenceL");
       
   270     iSubscribedOwn = EFalse;
       
   271     iSimpleId = iWatcher->UnsubscribeL();
       
   272     iXIMPId = aReqId;
       
   273     iOperation = EUnsubscribeOwn;    
       
   274     }    
       
   275 
       
   276 // ---------------------------------------------------------------------------
       
   277 // CPresencePluginPublisher::DoSubscribePresenceWatcherListL()
       
   278 // ---------------------------------------------------------------------------
       
   279 //
       
   280 void CPresencePluginPublisher::DoSubscribePresenceWatcherListL(
       
   281     TXIMPRequestId aReqId )
       
   282     {
       
   283     DP_SDA("CPresencePluginPublisher::DoSubscribePresenceWatcherListL");
       
   284     if ( !iSubscribed )
       
   285         {    
       
   286         DP_SDA("DoSubscribePresenceWatcherListL !Subscribed");    
       
   287         iConnObs.WinfoHandlerL()->SubscribeWinfoListL( aReqId );
       
   288         iSubscribed = ETrue;
       
   289         iXIMPId = aReqId;
       
   290         iOperation = ESubscribeWinfo;
       
   291         }
       
   292     else
       
   293         {
       
   294         DP_SDA("DoSubscribePresenceWatcherListL else");
       
   295         iXIMPId = aReqId;         
       
   296         iOperation = ESubscribeWinfo;        
       
   297         CompleteXIMPReq( KErrNone );
       
   298         } 
       
   299     DP_SDA("CPresencePluginPublisher::DoSubscribePresenceWatcherListL end"); 
       
   300     }
       
   301 
       
   302 // ---------------------------------------------------------------------------
       
   303 // CPresencePluginPublisher::DoUnsubscribePresenceWatcherListL()
       
   304 // ---------------------------------------------------------------------------
       
   305 //
       
   306 void CPresencePluginPublisher::DoUnsubscribePresenceWatcherListL(
       
   307     TXIMPRequestId aReqId )
       
   308     {
       
   309     DP_SDA("CPresencePluginPublisher::DoUnsubscribePresenceWatcherListL");
       
   310     if ( iSubscribed )
       
   311         {
       
   312         DP_SDA("DoUnsubscribePresenceWatcherListL: if ( iSubscribed ) - begin");
       
   313         iConnObs.WinfoHandlerL()->UnsubscribeWinfoListL( aReqId );
       
   314         iSubscribed = EFalse;
       
   315         iXIMPId = aReqId;
       
   316         iOperation = EUnsubscribeWinfo;        
       
   317         DP_SDA("DoUnsubscribePresenceWatcherListL:  if ( iSubscribed ) - end");
       
   318         
       
   319         }
       
   320     else
       
   321         {
       
   322         DP_SDA("DoUnsubscribePresenceWatcherListL: iSubscribed ) is false");        
       
   323         iXIMPId = aReqId;
       
   324         iOperation = EUnsubscribeWinfo; //hameed
       
   325         CompleteXIMPReq( KErrNone );
       
   326         }
       
   327     }
       
   328   
       
   329       
       
   330 // ---------------------------------------------------------------------------
       
   331 // CPresencePluginPublisher::PublishReqCompleteL()
       
   332 // ---------------------------------------------------------------------------
       
   333 //
       
   334 void CPresencePluginPublisher::PublishReqCompleteL( 
       
   335     TInt /*aOpid*/,
       
   336     TInt aStatus )
       
   337     {
       
   338     DP_SDA("CPresencePluginPublisher::PublishReqCompleteL");
       
   339     // Notice: make 415 error code conversion here and other 
       
   340     //SIP code -> XIMP conversions
       
   341     DP_SDA2("PublishReqCompleteL STATUS %d",aStatus );
       
   342     
       
   343     if( iConnObs.GetSessionStatus() )
       
   344         {
       
   345         DP_SDA2("PublishReqComp stopState %d",iConnObs.GetStopPublishState());
       
   346         if ( !aStatus && !iConnObs.GetStopPublishState() )
       
   347             {
       
   348             DP_SDA("CPresencePluginPublisher::PublishReqCompleteL ETrue");
       
   349             iPublished = ETrue;
       
   350             iConnObs.SetStopPublishState( ETrue );
       
   351             }
       
   352         // Save E-Tag
       
   353         if( !aStatus )
       
   354             {
       
   355             DP_SDA("CPresencePluginPublisher::PublishReqCompleteL Save ETAG");
       
   356             TBuf8<KBufSize255> buf;
       
   357             buf.Copy( iPublisher->SIPETag() );
       
   358             iConnObs.SetETag( buf );
       
   359             }
       
   360     	//Do not complete if error republish is true. XIMPFW is not knowing we 
       
   361     	//are sending republish.
       
   362         if( EFalse == iRePublish && !aStatus )
       
   363             {
       
   364             //Complete Publish request
       
   365             DP_SDA("CPresencePluginPublisher::PublishReqCompleteL complete");
       
   366             if( !(iConnObs.IsStopPublishCalled()) )
       
   367                 {
       
   368                 DP_SDA("CPresencePluginPublisher::PublishReqCompleteL complete normal");
       
   369                 // successfull Online / others: store status to cache
       
   370                 StoreOwnStatusToCacheL( );
       
   371                 CompleteXIMPReq( aStatus );
       
   372                 }
       
   373             //Complete stopPublish and close session if stopPublish is called
       
   374             if( iConnObs.IsStopPublishCalled() )
       
   375                 {
       
   376                 DP_SDA("PublishReqCompleteL complete closeSession"); 
       
   377                 iPresenceData->DeletePresenceVariablesL( iConnObs.ServiceId() );
       
   378                 // successfull Online / others: store status to cache
       
   379                 StoreOwnStatusToCacheL( );
       
   380                 //Complete close session
       
   381                 CompleteClientReq( aStatus );
       
   382                 }
       
   383             }
       
   384         if( EFalse == iRePublish && ( KSimpleErrTemporary == aStatus  ) )
       
   385            {
       
   386            DP_SDA("PublishReqCompleteL KSimpleErrTemporary"); 
       
   387            // Make new publish request without ETag
       
   388            CreatePublisherL();        
       
   389            iSimpleId = iPublisher->StartPublishL( *iDocument, ETrue );
       
   390            }
       
   391         else if( EFalse == iRePublish && ( KErrTimedOut == aStatus  ) )
       
   392             {
       
   393             DP_SDA("PublishReqCompleteL KErrTimedOut"); 
       
   394             iPublisher->StopPublishL();
       
   395             CompleteXIMPReq( KXIMPErrServicRequestTimeouted );
       
   396             }
       
   397         else
       
   398         	{
       
   399         	//Set rePublish flag back to false.
       
   400         	iRePublish = EFalse;
       
   401         	}
       
   402         }
       
   403     else
       
   404         {
       
   405         //Run this case if connection is not good any more
       
   406         DP_SDA("PublishReqCompleteL No connect CANCEL");
       
   407         Cancel();
       
   408         }
       
   409     DP_SDA("CPresencePluginPublisher::PublishReqCompleteL end");
       
   410     }
       
   411 
       
   412 // ---------------------------------------------------------------------------
       
   413 // CPresencePluginPublisher::PublishTerminatedL()
       
   414 // ---------------------------------------------------------------------------
       
   415 //
       
   416 void CPresencePluginPublisher::PublishTerminatedL( TInt aOpid )
       
   417     {
       
   418     DP_SDA("CPresencePluginPublisher::PublishTerminatedL");
       
   419     DP_SDA2("CPresencePluginPublisher::PublishTerminatedL opID %d",aOpid );
       
   420     DP_SDA2("PublishTerminatedL iSimpleId %d",iSimpleId );
       
   421     DP_SDA2("PublishTerminatedL iStatus %d",iStatus.Int() );
       
   422     
       
   423     if ( iSimpleId == aOpid && iConnObs.GetSessionStatus() )
       
   424         {
       
   425         DP_SDA("CPresencePluginPublisher::PublishTerminatedL NewPublish");
       
   426         CreatePublisherL();       
       
   427         TRAPD( error , iSimpleId = iPublisher->StartPublishL( 
       
   428         		*iDocument, ETrue ) );
       
   429         if ( KErrNone == error )
       
   430             {
       
   431             DP_SDA("PublishTerminatedL NewPublish KErrNone");
       
   432             iRePublish = ETrue;
       
   433             iConnObs.SetStopPublishState( EFalse );
       
   434             }        
       
   435         }
       
   436     else
       
   437         {
       
   438         DP_SDA("CPresencePluginPublisher::PublishTerminatedL else");
       
   439         iPublished = EFalse;
       
   440         if( iPublisher )
       
   441             {
       
   442             iPublisher->Close();
       
   443             iPublisher = NULL; 
       
   444             }    
       
   445         }  
       
   446     }   
       
   447     
       
   448 // ---------------------------------------------------------------------------
       
   449 // CPresencePluginPublisher::WatcherReqCompleteL()
       
   450 // ---------------------------------------------------------------------------
       
   451 //
       
   452 void CPresencePluginPublisher::WatcherReqCompleteL( TInt /*aOpid*/,
       
   453     TInt aStatus )
       
   454     {
       
   455     DP_SDA("CPresencePluginPublisher::WatcherReqCompleteL");
       
   456     if ( !aStatus )
       
   457         {      
       
   458         iSubscribedOwn = ETrue;   
       
   459         }
       
   460     
       
   461     CompleteXIMPReq( aStatus );       
       
   462     }
       
   463 
       
   464 // ---------------------------------------------------------------------------
       
   465 // CPresencePluginPublisher::WatcherNotificationL()
       
   466 // ---------------------------------------------------------------------------
       
   467 //
       
   468 void CPresencePluginPublisher::WatcherNotificationL( 
       
   469     MSimpleDocument& aDocument )
       
   470     {       
       
   471     // Notice: Do not need to check the expiration here since
       
   472     // WatcherTerminatedL is called then too.
       
   473     DP_SDA("CPresencePluginPublisher::WatcherNotificationL");
       
   474     
       
   475     MProtocolPresencePublishingDataHost& publishHost =
       
   476         iConnObs.ProtocolPresenceHost().PublishingDataHost();
       
   477             
       
   478     MPresenceInfo* prInfo =
       
   479         iConnObs.PresenceObjectFactoryOwn().NewPresenceInfoLC();// << prInfo
       
   480         
       
   481     iPresenceData->NotifyToPrInfoL(  
       
   482         iConnObs.ObjectFactory(),
       
   483         iConnObs.PresenceObjectFactoryOwn(), 
       
   484         aDocument,
       
   485         *prInfo );
       
   486 
       
   487     // XIMP Host API callbacks
       
   488   
       
   489     publishHost.HandleSubscribedOwnPresenceL( prInfo );
       
   490     CleanupStack::Pop();  // >> prInfo            
       
   491     }
       
   492 
       
   493 // ---------------------------------------------------------------------------
       
   494 // CPresencePluginPublisher::WatcherListNotificationL()
       
   495 // ---------------------------------------------------------------------------
       
   496 //
       
   497 void CPresencePluginPublisher::WatcherListNotificationL( 
       
   498     MSimplePresenceList& /*aList*/ )
       
   499     {
       
   500     //Do nothing now
       
   501     DP_SDA("CPresencePluginPublisher::WatcherListNotificationL EMPTY");     
       
   502     }
       
   503 
       
   504 // ---------------------------------------------------------------------------
       
   505 // CPresencePluginPublisher::WatcherTerminatedL()
       
   506 // ---------------------------------------------------------------------------
       
   507 //
       
   508 void CPresencePluginPublisher::WatcherTerminatedL(
       
   509     TInt /*aOpId*/, TInt aReason )
       
   510     {
       
   511     DP_SDA("CPresencePluginPublisher::WatcherTerminatedL");
       
   512     
       
   513     iSubscribedOwn = EFalse;    
       
   514 
       
   515     MXIMPObjectFactory& myFactory = iConnObs.ObjectFactory();    
       
   516     MProtocolPresencePublishingDataHost& publishHost =
       
   517         iConnObs.ProtocolPresenceHost().PublishingDataHost();
       
   518         
       
   519     MXIMPDataSubscriptionState *state =
       
   520         myFactory.NewDataSubscriptionStateLC();
       
   521     MXIMPStatus* status = myFactory.NewStatusLC();
       
   522     state->SetSubscriptionStateL( 
       
   523         MXIMPDataSubscriptionState::ESubscriptionInactive );
       
   524         
       
   525     status->SetResultCode( aReason );
       
   526     publishHost.SetOwnPresenceDataSubscriptionStateL( state, status );
       
   527     CleanupStack::Pop( 2 ); // status, state    
       
   528     
       
   529     DP_SDA("CPresencePluginPublisher::WatcherTerminatedL - end");
       
   530     }
       
   531     
       
   532 // ---------------------------------------------------------------------------
       
   533 // CPresencePluginPublisher::CompleteXIMPReq()
       
   534 // ---------------------------------------------------------------------------
       
   535 //
       
   536 void CPresencePluginPublisher::CompleteXIMPReq( TInt aStatus )
       
   537     {
       
   538     DP_SDA("CPresencePluginPublisher::CompleteXIMPReq");
       
   539     DP_SDA2("CompleteXIMPReq:  iOperation = %d",(TInt) iOperation );
       
   540     // Notice: should this yield active scheduler???
       
   541     if ( iOperation != ENoOperation )
       
   542         {
       
   543         DP_SDA("CPresencePluginPublisher::CompleteXIMPReq complete");                
       
   544         iOperation = ENoOperation;
       
   545         iConnObs.CompleteReq( iXIMPId, aStatus );
       
   546         iXIMPId = TXIMPRequestId();
       
   547         }
       
   548     DP_SDA("CPresencePluginPublisher::CompleteXIMPReq end"); 
       
   549     }  
       
   550 
       
   551 // ---------------------------------------------------------------------------
       
   552 // CPresencePluginPublisher::CompleteClientReq()
       
   553 // ---------------------------------------------------------------------------
       
   554 //               
       
   555 void CPresencePluginPublisher::CompleteClientReq( TInt aStatus )
       
   556     {
       
   557     DP_SDA2("CPresencePluginPublisher::CompleteClientReq status %d ", aStatus);
       
   558     iOperation = ENoOperation;
       
   559     TRequestStatus* s = iClientStatus;
       
   560     User::RequestComplete( s, aStatus );
       
   561     DP_SDA("CPresencePluginPublisher::CompleteClientReq end");
       
   562     }
       
   563 
       
   564 // ---------------------------------------------------------------------------
       
   565 // CPresencePluginPublisher::DoCancel()
       
   566 // ---------------------------------------------------------------------------
       
   567 //
       
   568 void CPresencePluginPublisher::DoCancel(  )
       
   569     {
       
   570     DP_SDA("CPresencePluginPublisher::DoCancel CANCEL");
       
   571     iXdmUtils->Cancel();
       
   572     iPublisher->Close();
       
   573     }
       
   574 
       
   575 // ---------------------------------------------------------------------------
       
   576 // CPresencePluginPublisher::RunL()
       
   577 // ---------------------------------------------------------------------------
       
   578 //
       
   579 void CPresencePluginPublisher::RunL(  )
       
   580     {
       
   581     DP_SDA("CPresencePluginPublisher::RunL");
       
   582     TInt status = iStatus.Int();
       
   583     DP_SDA2("CPresencePluginPublisher::RunL status %d", status );
       
   584     if ( !status )
       
   585         {
       
   586         
       
   587         if ( iOperation == ESubscribeOwn )
       
   588             {
       
   589             DP_SDA("CPresencePluginPublisher::RunL Subscribe Own");
       
   590             iSimpleId = iWatcher->SubscribeL(
       
   591                 iConnObs.CurrentSipPresentity8(),
       
   592                 NULL,  // aFilter <-> aPif
       
   593                 ETrue, EFalse );          
       
   594             }
       
   595         else if( iOperation == EPublishOwn )
       
   596             {
       
   597             DP_SDA("CPresencePluginPublisher::RunL MakePublishReqL");
       
   598             MakePublishReqL();
       
   599             }
       
   600         }
       
   601     else
       
   602         {
       
   603         DP_SDA("CPresencePluginPublisher::RunL complete");
       
   604         CompleteXIMPReq( status );
       
   605         }           
       
   606     }
       
   607 
       
   608 // ---------------------------------------------------------------------------
       
   609 // CPresencePluginPublisher::RunError
       
   610 // ---------------------------------------------------------------------------
       
   611 //
       
   612 TInt CPresencePluginPublisher::RunError( TInt aError )
       
   613     {
       
   614     DP_SDA2("CPresencePluginPublisher::RunError %d",aError );
       
   615     
       
   616     CompleteXIMPReq( aError );  
       
   617     return KErrNone;
       
   618     }
       
   619     
       
   620 // ---------------------------------------------------------------------------
       
   621 // CPresencePluginPublisher::GetInterface()
       
   622 // ---------------------------------------------------------------------------
       
   623 //
       
   624 TAny* CPresencePluginPublisher::GetInterface(
       
   625     TInt32 aInterfaceId,
       
   626     TIfGetOps aOptions )
       
   627     {
       
   628     DP_SDA("CPresencePluginPublisher::GetInterface");
       
   629     
       
   630     if ( aInterfaceId == GetInterfaceId() )
       
   631         {
       
   632         // caller wants this interface
       
   633         MProtocolPresencePublishing* myIf = this;
       
   634         return myIf;
       
   635         }
       
   636     if ( aOptions == MXIMPBase::EPanicIfUnknown )
       
   637         {
       
   638         User::Panic( KPluginName, KErrExtensionNotSupported );
       
   639         }  
       
   640     return NULL;
       
   641     }
       
   642 
       
   643 // ---------------------------------------------------------------------------
       
   644 // CPresencePluginPublisher::GetInterface()
       
   645 // ---------------------------------------------------------------------------
       
   646 //
       
   647 const TAny* CPresencePluginPublisher::GetInterface(
       
   648     TInt32 aInterfaceId,
       
   649     TIfGetOps aOptions ) const
       
   650     {
       
   651     DP_SDA("CPresencePluginPublisher::GetInterface 2 ");
       
   652     
       
   653     if ( aInterfaceId == GetInterfaceId() )
       
   654         {
       
   655         // caller wants this interface
       
   656         const MProtocolPresencePublishing* myIf = this;
       
   657         return myIf;
       
   658         }
       
   659     if ( aOptions == MXIMPBase::EPanicIfUnknown )
       
   660         {
       
   661         User::Panic( KPluginName, KErrExtensionNotSupported );
       
   662         }  
       
   663     return NULL;
       
   664     }
       
   665 
       
   666 // ---------------------------------------------------------------------------
       
   667 // CPresencePluginPublisher::GetInterfaceId()
       
   668 // ---------------------------------------------------------------------------
       
   669 //
       
   670 TInt32 CPresencePluginPublisher::GetInterfaceId() const
       
   671     {
       
   672     DP_SDA("CPresencePluginPublisher::GetInterfaceId");
       
   673     
       
   674     return MProtocolPresencePublishing::KInterfaceId;
       
   675     }
       
   676        
       
   677 // ---------------------------------------------------------------------------
       
   678 // CPresencePluginPublisher::InitializeSimpleDocumentL()
       
   679 // ---------------------------------------------------------------------------
       
   680 //
       
   681 void CPresencePluginPublisher::InitializeSimpleDocumentL( )
       
   682     {
       
   683     _LIT8 ( KCipidNS, "urn:ietf:params:xml:ns:pidf:cipid" );
       
   684     _LIT8 ( KCipidCP, "c" );
       
   685     if ( iDocument )
       
   686         {
       
   687         DP_SDA("CPresencePluginPublisher:: deletedocument");
       
   688         iDocument->Close();
       
   689         iDocument = NULL;
       
   690         iDocument = TSimpleFactory::NewDocumentL();
       
   691         }
       
   692     else
       
   693         {
       
   694         DP_SDA("CPresencePluginPublisher:: create document");
       
   695         iDocument = TSimpleFactory::NewDocumentL();
       
   696         }
       
   697     iDocument->AddNamespaceL( KSimplePDM, KSimpleNsPDM );
       
   698     iDocument->AddNamespaceL( KSimpleRPID, KSimpleNsRPID );
       
   699     iDocument->AddNamespaceL( KCipidCP, KCipidNS );
       
   700     }      
       
   701     
       
   702 // ---------------------------------------------------------------------------
       
   703 // CPresencePluginPublisher::AddSimpleDocumentPersL()
       
   704 // ---------------------------------------------------------------------------
       
   705 //
       
   706 void CPresencePluginPublisher::AddSimpleDocumentPersL(
       
   707     const MPersonPresenceInfo* aInfo )
       
   708     {
       
   709     TInt tupleId = iConnObs.GetTupleId();
       
   710     
       
   711     DP_SDA2("AddSimpleDocumentPersL tuple id get: %d", tupleId);
       
   712     if ( tupleId == 0 )
       
   713         {
       
   714         DP_SDA("AddSimpleDocumentPersL tuple generate");
       
   715         tupleId = iPresenceData->GenerateTupleId();
       
   716         iConnObs.SetTupleId(tupleId);
       
   717         }
       
   718    
       
   719     iPresenceData->AddPrPersToSimpleDocumentL(
       
   720         aInfo, *iDocument, iConnObs.CurrentSipPresentity8(), tupleId ); 
       
   721     }
       
   722           
       
   723 // ---------------------------------------------------------------------------
       
   724 // CPresencePluginPublisher::StopPublishL()
       
   725 // ---------------------------------------------------------------------------
       
   726 //
       
   727 void CPresencePluginPublisher::StopPublishL( TRequestStatus& aStatus )
       
   728     {
       
   729     DP_SDA2("StopPublish - begin : iPublished= %d ",(TInt) iPublished);
       
   730     
       
   731     aStatus = KRequestPending;
       
   732     iClientStatus = &aStatus;
       
   733     
       
   734     TInt err1( 0 );
       
   735     TInt err2( 0 );
       
   736     
       
   737     // get stored document id if available
       
   738     HBufC8* oldDocumentId = HBufC8::NewLC( KBufSize255 );
       
   739     TPtr8 oldDocumentIdPtr( oldDocumentId->Des() );
       
   740     TRAP( err1, iPresenceData->ReadDocumentIdL( 
       
   741             iConnObs.ServiceId(), oldDocumentIdPtr ) );
       
   742     
       
   743     if ( iConnObs.GetStopPublishState() && iPublished )
       
   744         {
       
   745         DP_SDA("CPresencePluginPublisher::StopPublish really Stop");
       
   746         if ( !err1 )
       
   747             {
       
   748             TRAP( err2, iPublisher->StopPublishL( oldDocumentIdPtr ) );
       
   749             }
       
   750         else 
       
   751             {
       
   752             // if stored document id is not available try with this
       
   753             TRAP( err2, iPublisher->StopPublishL( iConnObs.GetETag() ) );
       
   754             }
       
   755         DP_SDA2("StopPublish iPublisher->StopPublishL : error = %d ", err2 );
       
   756         if ( KErrNone != err2 )
       
   757             {            
       
   758             if( iPublisher )
       
   759                 {
       
   760                 DP_SDA("StopPublish delete and recreate publisher");
       
   761                 iPublisher->Close();
       
   762                 iPublisher = NULL;
       
   763                 DP_SDA("StopPublish really Stop try again 1 ");
       
   764                 iPublisher =
       
   765                 	TSimpleFactory::NewPublisherL( iConnection, *this ); 
       
   766                 DP_SDA("StopPublish really Stop try again 2 ");
       
   767                 err2 = KErrGeneral;
       
   768                 TRAP( err2, iPublisher->StopPublishL( iConnObs.GetETag() ););
       
   769                 DP_SDA2("StopPublishL 2nd try : error = %d ", err2 );
       
   770                 if ( KErrNone != err2 )
       
   771                     {
       
   772                     DP_SDA("StopPublish TWO TIME Error, last try without tag ");
       
   773                     TRAP( err2, iPublisher->StopPublishL(); );
       
   774                     DP_SDA2("StopPublishL 3rd try : error = %d ", err2 );
       
   775                     }
       
   776                 }
       
   777             }
       
   778         iConnObs.SetStopPublishState( ETrue );
       
   779         iPublished = EFalse;
       
   780         }
       
   781     CleanupStack::PopAndDestroy( oldDocumentId );
       
   782     
       
   783     DP_SDA("CPresencePluginPublisher::StopPublish- end");        
       
   784     }
       
   785     
       
   786 // ---------------------------------------------------------------------------
       
   787 // CPresencePluginPublisher::Published()
       
   788 // ---------------------------------------------------------------------------
       
   789 //
       
   790 TBool CPresencePluginPublisher::Published()
       
   791     {
       
   792     return iPublished;
       
   793     }
       
   794 
       
   795 // ---------------------------------------------------------------------------
       
   796 // CPresencePluginPublisher::MakePublishReqL()
       
   797 // ---------------------------------------------------------------------------
       
   798 //
       
   799 void CPresencePluginPublisher::MakePublishReqL( )
       
   800     {
       
   801     DP_SDA("CPresencePluginPublisher::MakePublishReqL");
       
   802     
       
   803         DP_SDA(" -> MakePublishReqL, check for old doc id");       
       
   804         HBufC8* oldDocumentId = HBufC8::NewLC( KBufSize255 );
       
   805         TPtr8 oldDocumentIdPtr( oldDocumentId->Des() );
       
   806         TRAPD( error, iPresenceData->ReadDocumentIdL( 
       
   807             iConnObs.ServiceId(), oldDocumentIdPtr ) );
       
   808     if ( !iPublished )
       
   809         {
       
   810         DP_SDA("CPresencePluginPublisher::MakePublishReqL 1");       
       
   811         CreatePublisherL();
       
   812         
       
   813         DP_SDA2(" -> MakePublishReqL, doc id error: %d", error );
       
   814         if ( !error )
       
   815             {
       
   816             DP_SDA(" -> MakePublishReqL, publish with old id");       
       
   817             iSimpleId = iPublisher->ContinuePublishL( 
       
   818                 *iDocument, ETrue, oldDocumentIdPtr );
       
   819             }
       
   820         else
       
   821             {
       
   822             DP_SDA(" -> MakePublishReqL, publish with new id");       
       
   823             iSimpleId = iPublisher->StartPublishL( *iDocument, ETrue );    
       
   824             }
       
   825         }
       
   826     else
       
   827         {
       
   828         DP_SDA("CPresencePluginPublisher::MakePublishReqL 2");
       
   829         CreatePublisherL();
       
   830         DP_SDA("CPresencePluginPublisher::MakePublishReqL Continue Publish");
       
   831         if ( !error )
       
   832             {
       
   833             DP_SDA(" -> MakePublishReqL, publish with stored id");
       
   834             iSimpleId = iPublisher->ContinuePublishL( 
       
   835                 *iDocument ,ETrue, oldDocumentIdPtr );
       
   836             }
       
   837         else
       
   838             {
       
   839             DP_SDA(" -> MakePublishReqL, stored id not found" );
       
   840             DP_SDA(", publish with old id");
       
   841             iSimpleId = iPublisher->ContinuePublishL( 
       
   842                             *iDocument ,ETrue, iConnObs.GetETag() );
       
   843             }
       
   844 		DP_SDA("CPresencePluginPublisher::MakePublishReqL modifyed");
       
   845         }
       
   846     CleanupStack::PopAndDestroy( oldDocumentId );
       
   847     DP_SDA("CPresencePluginPublisher::MakePublishReqL end");     
       
   848     } 
       
   849 
       
   850 // ---------------------------------------------------------------------------
       
   851 // CPresencePluginPublisher::StartXdmOperationL
       
   852 // ---------------------------------------------------------------------------
       
   853 //
       
   854 void CPresencePluginPublisher::StartXdmOperationL(
       
   855     TXIMPRequestId aReqId )
       
   856     {
       
   857     DP_SDA("CPresencePluginPublisher::StartXdmOperationL");
       
   858     __ASSERT_DEBUG( !IsActive(), User::Leave( KErrNotReady ) );
       
   859     iXIMPId = aReqId;
       
   860     if ( !iXdmUtils )
       
   861         {
       
   862         DP_SDA("StartXdmOperationL Initialize XDMUtils");
       
   863         iXdmUtils = iConnObs.XdmUtilsL();
       
   864         }
       
   865 
       
   866     // Allways check those XDM files are ok / 
       
   867     //publish seems to be jam if xdm not initialized ? 
       
   868     iXdmUtils->InitializeXdmL( iStatus ); 
       
   869     SetActive();
       
   870     DP_SDA("CPresencePluginPublisher::StartXdmOperationL end");     
       
   871     }
       
   872                  
       
   873 // ---------------------------------------------------------------------------
       
   874 // CPresencePluginPublisher::WinfoNotificationL()
       
   875 // ---------------------------------------------------------------------------
       
   876 //
       
   877 void CPresencePluginPublisher::WinfoNotificationL(
       
   878     MSimpleWinfo& aWinfo )
       
   879     {
       
   880     DP_SDA("CPresencePluginPublisher::WinfoNotificationL");
       
   881    
       
   882     RPointerArray<MSimpleElement> elems;
       
   883     CleanupClosePushL( elems );         // << elems
       
   884     TInt err = aWinfo.SimpleElementsL( elems );
       
   885     User::LeaveIfError( err );
       
   886     TInt count = elems.Count();
       
   887     
       
   888     DP_SDA2("WinfoNotificationL elems count%d", count);
       
   889 
       
   890     __ASSERT_ALWAYS( count == 1, User::Leave( KErrCorrupt ) );
       
   891 
       
   892     using namespace NPresencePlugin::NPresence;
       
   893 
       
   894     const TDesC8* stateVal = aWinfo.AttrValue( KPresenceState8 );
       
   895     
       
   896     if ( stateVal && !stateVal->CompareF( KPresenceFull8 )) //state full
       
   897         {
       
   898         // full winfo-list is received  (New empty list )
       
   899         DP_SDA("CPresencePluginPublisher::WinfoNotificationL full list found");
       
   900         DeleteWatchers();
       
   901         }
       
   902 	else
       
   903         {
       
   904 		DP_SDA("PluginPublisher: state != full");
       
   905 		// Test if state is partitial
       
   906 		if ( stateVal && !stateVal->CompareF( KPresencePartial8 ))
       
   907 			{
       
   908 			DP_SDA("PluginPublisher: state = partitial");	
       
   909 			}
       
   910 		}
       
   911 
       
   912     MSimpleElement* elem = elems[0];
       
   913     TPtrC8 p8 = elem->LocalName();
       
   914     err = p8.CompareF( KPresenceWatcherList8 );
       
   915     DP_SDA2("WinfoNotificationL KPresenceWatcherList8 err %d", err);
       
   916     User::LeaveIfError( err );
       
   917     DP_SDA("WinfoNotificationL KPresenceWatcherList8 continue");
       
   918     
       
   919     //Get elemets from document
       
   920     err = elem->SimpleElementsL( elems );
       
   921     User::LeaveIfError( err );
       
   922     DP_SDA2("WinfoNotificationL get elems err %d", err);
       
   923     //Make collect from active watchers
       
   924     CollectWatchersL( elems );
       
   925     
       
   926     CDesCArrayFlat* watchers = MakeCurrentWatcherListLC();// << watchers
       
   927     MXIMPObjectCollection *actives =
       
   928         iConnObs.ObjectFactory().NewObjectCollectionLC();// << actives       
       
   929 
       
   930     // Create MXIMPPresenceWatcherInfo entities for
       
   931     // all active watchers and add to actives.
       
   932     TInt wCount = watchers->MdcaCount();
       
   933     DP_SDA2("CPresencePluginPublisher::WinfoNotificationL wCount %d", wCount);
       
   934     for ( TInt j = 0; j < wCount; j++ )
       
   935         {       
       
   936         // create MXIMPPresenceWatcherInfo object
       
   937         MPresenceWatcherInfo* wInfo =
       
   938             iConnObs.PresenceObjectFactoryOwn().
       
   939                 NewPresenceWatcherInfoLC();// << wInfo
       
   940 
       
   941         MXIMPIdentity* identity =
       
   942         	iConnObs.ObjectFactory().NewIdentityLC();// << identity
       
   943          
       
   944         identity->SetIdentityL( watchers->MdcaPoint( j ) );                
       
   945         wInfo->SetWatcherIdL( identity );
       
   946         CleanupStack::Pop( );// >> identity
       
   947 
       
   948         wInfo->SetWatcherDisplayNameL( watchers->MdcaPoint( j ) );
       
   949         wInfo->SetWatcherTypeL( MPresenceWatcherInfo::EPresenceSubscriber );
       
   950 
       
   951         actives->AddObjectL( wInfo );
       
   952         CleanupStack::Pop( );                           // >> wInfo
       
   953         }
       
   954 
       
   955     MProtocolPresencePublishingDataHost& publishHost =
       
   956          iConnObs.Host()->ProtocolPresenceDataHost().PublishingDataHost();
       
   957 
       
   958     publishHost.HandlePresenceWatcherListL( actives );
       
   959    
       
   960     CleanupStack::Pop();                  // >> actives      
       
   961     CleanupStack::PopAndDestroy( watchers );        // >> watchers
       
   962    
       
   963     CleanupStack::PopAndDestroy( &elems );          // >> elems
       
   964     DP_SDA("CPresencePluginPublisher::WinfoNotificationL end");      
       
   965     }
       
   966         
       
   967 // ---------------------------------------------------------------------------
       
   968 // CPresencePluginPublisher::WinfoTerminatedL()
       
   969 // ---------------------------------------------------------------------------
       
   970 //
       
   971 void CPresencePluginPublisher::WinfoTerminatedL( TInt aReason )
       
   972     {
       
   973     DP_SDA("CPresencePluginPublisher::WinfoTerminatedL - begin");      
       
   974     
       
   975     // call SetPresenceWatcherListDataSubscriptionStateL  
       
   976     iSubscribed = EFalse;    
       
   977 
       
   978     MXIMPObjectFactory& myFactory = iConnObs.ObjectFactory();    
       
   979     MProtocolPresencePublishingDataHost& publishHost =
       
   980         iConnObs.Host()->ProtocolPresenceDataHost().PublishingDataHost();
       
   981     MXIMPDataSubscriptionState *state =
       
   982         myFactory.NewDataSubscriptionStateLC();
       
   983         
       
   984     MXIMPStatus* status = myFactory.NewStatusLC();
       
   985     state->SetSubscriptionStateL( 
       
   986         MXIMPDataSubscriptionState::ESubscriptionInactive );
       
   987         
       
   988     state->SetDataStateL( MXIMPDataSubscriptionState::EDataUnavailable );
       
   989     status->SetResultCode( aReason );
       
   990     publishHost.SetPresenceWatcherListDataSubscriptionStateL( state, status );
       
   991     CleanupStack::Pop( 2 ); // status, state  
       
   992     DP_SDA("CPresencePluginPublisher::WinfoTerminatedL - end");
       
   993     }  
       
   994 
       
   995 // ---------------------------------------------------------------------------
       
   996 // CPresencePluginPublisher::NewETagL()
       
   997 // ---------------------------------------------------------------------------
       
   998 //
       
   999 void CPresencePluginPublisher::NewETagL( const TDesC8& aVal )
       
  1000     {
       
  1001     DP_SDA("CPresencePluginPublisher::NewETagL");        
       
  1002     // Set ETag value 
       
  1003     DP_SDA("CPresencePluginPublisher::NewETagL Set ETag");
       
  1004     TBuf8<KBufSize255> buf;
       
  1005     buf.Copy( aVal );
       
  1006     iConnObs.SetETag( buf );
       
  1007     buf.Zero(); 
       
  1008     }
       
  1009     
       
  1010 // ---------------------------------------------------------------------------
       
  1011 // CPresencePluginPublisher::MakeCurrentWatcherListLC
       
  1012 // ---------------------------------------------------------------------------
       
  1013 //
       
  1014 CDesCArrayFlat* CPresencePluginPublisher::MakeCurrentWatcherListLC()
       
  1015     {
       
  1016     DP_SDA("CPresencePluginPublisher::MakeCurrentWatcherListLC - begin");        
       
  1017     // No one should be added more than once
       
  1018     const TInt KMyGran = 10;    
       
  1019     CDesCArrayFlat* watchers = new (ELeave) CDesCArrayFlat( KMyGran );
       
  1020     CleanupStack::PushL( watchers );      // << watchers            
       
  1021        
       
  1022     // add user only once here.
       
  1023     TDblQueIter<CPresencePluginWatcherInfo> rIter( iWatcherList );
       
  1024     rIter.SetToFirst();
       
  1025     
       
  1026     while ( rIter )
       
  1027         {
       
  1028         CPresencePluginWatcherInfo* w = rIter;
       
  1029         rIter++;
       
  1030         
       
  1031         TInt dummy = 0;
       
  1032         // Zero -> Found
       
  1033         if ( watchers->Find( w->SipId(), dummy ))
       
  1034             {            
       
  1035             watchers->AppendL( w->SipId() );
       
  1036             }                    
       
  1037         }
       
  1038     
       
  1039     DP_SDA("CPresencePluginPublisher::MakeCurrentWatcherListLC - end");        
       
  1040     return watchers;    
       
  1041     }
       
  1042 
       
  1043 // ---------------------------------------------------------------------------
       
  1044 // CPresencePluginPublisher::CollectWatchers
       
  1045 // --------------------------------------------------------------------------- 
       
  1046 //
       
  1047 void CPresencePluginPublisher::CollectWatchersL(  
       
  1048 	RPointerArray<MSimpleElement>& aElems )
       
  1049 	{
       
  1050 	using namespace NPresencePlugin::NPresence;
       
  1051 	HBufC* nodeContent = NULL;
       
  1052 	
       
  1053 	TInt count = aElems.Count();
       
  1054 	DP_SDA2("CPresencePluginPublisher::CollectWatchers elem count %d",count);
       
  1055 		
       
  1056 	for ( TInt i = 0; i < count; i++ )
       
  1057 		{
       
  1058 		MSimpleElement* elem = aElems[i];
       
  1059 		TPtrC8 p8( elem->LocalName());
       
  1060 
       
  1061 		if (!( p8.CompareF( KPresenceWatcher8 )))
       
  1062 			{
       
  1063 			DP_SDA("CPresencePluginPublisher::CollectWatchers watchers found");
       
  1064 			const TDesC8* pp8 = elem->AttrValue( KPresenceStatus8 );
       
  1065 			// Active wathers here
       
  1066 			if ( pp8 && !pp8->CompareF( KPresenceActive8 ))
       
  1067     			{
       
  1068     			DP_SDA("CollectWatchers: ACTIVE found");	
       
  1069 				// save id since there may be multiple subscriptions 
       
  1070 				// from a single watcher SIP identity.
       
  1071 				const TDesC8* pId8 = elem->AttrValue( KPresenceId8 );
       
  1072                                              
       
  1073 				// Build collection of grant requests
       
  1074 				// Find the child node containing the SIP entity
       
  1075 				nodeContent = elem->ContentUnicodeL();
       
  1076 				CleanupStack::PushL( nodeContent );// << nodeContent
       
  1077                     
       
  1078 				AddWatcherIfNotExistsL( 
       
  1079 				    pId8 ? *pId8 : KNullDesC8, nodeContent->Des() );
       
  1080 				
       
  1081 				CleanupStack::PopAndDestroy( nodeContent ); //>> nodeContent
       
  1082 				}
       
  1083 			// Terminated wathers here
       
  1084 			else if ( pp8 && !pp8->CompareF( KPresenceTerminated8 ))
       
  1085     			{
       
  1086 			    DP_SDA("CollectWatchers: Terminated");
       
  1087 				const TDesC8* pId8 = elem->AttrValue( KPresenceId8 );
       
  1088 				                
       
  1089 				// Remove terminated from iWatcherList
       
  1090 				nodeContent = elem->ContentUnicodeL();
       
  1091 				CleanupStack::PushL( nodeContent );// << nodeContent                
       
  1092 	    
       
  1093 				RemoveWatcherIfExists( 
       
  1094     				pId8 ? *pId8 : KNullDesC8, nodeContent->Des() );                     
       
  1095     
       
  1096 				CleanupStack::PopAndDestroy( nodeContent );// >> nodeContent
       
  1097         		}
       
  1098     		}
       
  1099 		}
       
  1100 	} 
       
  1101 
       
  1102 // ---------------------------------------------------------------------------
       
  1103 // CPresencePluginPublisher::CreatePublisherL()
       
  1104 // ---------------------------------------------------------------------------
       
  1105 //
       
  1106 void CPresencePluginPublisher::CreatePublisherL()
       
  1107     {
       
  1108     DP_SDA("CPresencePluginPublisher::CreatePublisherL");
       
  1109     if( iPublisher )
       
  1110         {
       
  1111         DP_SDA("CPresencePluginPublisher::CreatePublisherL create");
       
  1112         iPublisher->Close();
       
  1113         iPublisher = NULL; 
       
  1114         iPublisher = TSimpleFactory::NewPublisherL( iConnection, *this );
       
  1115         }
       
  1116     DP_SDA("CPresencePluginPublisher::CreatePublisherL end");
       
  1117     }
       
  1118 	
       
  1119 // ---------------------------------------------------------------------------
       
  1120 // CPresencePluginPublisher::StoreOwnStatusToCacheL
       
  1121 // --------------------------------------------------------------------------- 
       
  1122 //
       
  1123 void CPresencePluginPublisher::StoreOwnStatusToCacheL()
       
  1124 	{	
       
  1125 	DP_SDA("CPresencePluginPublisher::StoreOwnStatusToCacheL" );
       
  1126 	
       
  1127 	using namespace NPresencePlugin::NPresence;
       
  1128     using namespace NPresencePlugin::NPresenceStates;
       
  1129     using namespace NPresenceInfo::NFieldType;
       
  1130 	
       
  1131     DP_SDA(" CPresencePluginPublisher -> convert uri" );
       
  1132     HBufC* entityUri = 
       
  1133         CnvUtfConverter::ConvertToUnicodeFromUtf8L( 
       
  1134             *iDocument->EntityURI() );
       
  1135     CleanupStack::PushL( entityUri );
       
  1136 
       
  1137     DP_SDA(" CPresencePluginPublisher -> strip prefix" );
       
  1138     HBufC16* prefixStripped = iPresenceData->RemovePrefixLC( 
       
  1139         *entityUri );
       
  1140              
       
  1141     DP_SDA(" CPresencePluginPublisher -> fetch simple elements" );
       
  1142     RPointerArray<MSimpleElement> simpleElements;
       
  1143     CleanupClosePushL( simpleElements );
       
  1144     DP_SDA(" CPresencePluginPublisher -> do fetch simple elements" );
       
  1145     TInt error = iDocument->SimpleElementsL( simpleElements );    
       
  1146     DP_SDA2(" CPresencePluginPublisher -> fetch error: %d", error );
       
  1147     User::LeaveIfError( error );
       
  1148     TBool handled = EFalse;
       
  1149     DP_SDA2(" CPresencePluginPublisher -> element count: %d", 
       
  1150         simpleElements.Count() );
       
  1151     
       
  1152     MSimpleElement* basicSimpleElem(NULL);
       
  1153     MSimpleElement* noteSimpleElem(NULL);
       
  1154     
       
  1155     TBuf<KBufSize100> activitiesContentBuf;
       
  1156     TBool activitiesHandled( EFalse );
       
  1157     
       
  1158     for( TInt index = 0 ; index < simpleElements.Count() ; index++ )
       
  1159         {
       
  1160         DP_SDA(" CPresencePluginPublisher -> check for person element" );
       
  1161         // Check for person element
       
  1162         if (!( simpleElements[index]->LocalName().CompareF( KPresencePerson8 )) ||
       
  1163             !( simpleElements[index]->LocalName().CompareF( KPresenceTuple8 )) )
       
  1164             {
       
  1165             DP_SDA(" CPresencePluginPublisher -> person/tuple element found" );
       
  1166             RPointerArray<MSimpleElement> personSubElems;
       
  1167             CleanupClosePushL( personSubElems );
       
  1168             simpleElements[index]->SimpleElementsL( personSubElems );
       
  1169             DP_SDA2(" CPresencePluginPublisher -> person/tuple sub elemen count: %d", 
       
  1170                 personSubElems.Count() );
       
  1171 
       
  1172             for ( TInt j = 0; j < personSubElems.Count(); j++ )
       
  1173                 {
       
  1174                 DP_SDA(" CPresencePluginPublisher -> check for status/activities element");
       
  1175                 if ( !personSubElems[j]->LocalName().CompareF( KPresenceStatus8 ) ||
       
  1176                      !personSubElems[j]->LocalName().CompareF( KPresenceActivities8 ))
       
  1177                     {
       
  1178          	        DP_SDA(" CPresencePluginPublisher -> status/activities element found");
       
  1179          	        //Get childs
       
  1180          	        RPointerArray<MSimpleElement> basicElems;
       
  1181          	        CleanupClosePushL( basicElems );
       
  1182          	        
       
  1183 		            personSubElems[j]->SimpleElementsL( basicElems );
       
  1184                     DP_SDA2(" CPresencePluginPublisher -> child elem count: %d", 
       
  1185                         basicElems.Count() );
       
  1186 		            for ( TInt k = 0; k < basicElems.Count(); k++ )
       
  1187 		                {
       
  1188 		                DP_SDA(" CPresencePluginPublisher -> check child elems");
       
  1189 		                if ( !basicElems[k]->LocalName().CompareF( 
       
  1190 		                		KPresenceBasic8 ))
       
  1191 		                    {
       
  1192 		                    DP_SDA(" -> basic element found");
       
  1193 		                    basicSimpleElem = basicElems[k];
       
  1194 		                    }
       
  1195                  	    else if ( basicSimpleElem && 
       
  1196                  	        !basicElems[k]->LocalName().CompareF( KPresenceActivities8 ) )
       
  1197                  	        {
       
  1198                  	        DP_SDA(" -> activities found from basic elem");
       
  1199                  	        RPointerArray<MSimpleElement> activitiesElems;
       
  1200                  	        CleanupClosePushL( activitiesElems );
       
  1201     			            
       
  1202     			            DP_SDA(" -> get activities");
       
  1203     			            basicElems[k]->SimpleElementsL( activitiesElems );
       
  1204     			            DP_SDA2(" -> activities count: %d", 
       
  1205     			                activitiesElems.Count() );
       
  1206                             if ( activitiesElems.Count() )
       
  1207                                 {
       
  1208                                 DP_SDA(" -> get activities, multiple, use last");
       
  1209         			            TInt lastActiviesIndex = 
       
  1210         			                ( activitiesElems.Count() - 1 );
       
  1211     			                activitiesContentBuf.Copy( 
       
  1212     			                    activitiesElems[lastActiviesIndex]->LocalName() );
       
  1213     			                activitiesHandled = ETrue;
       
  1214     			                DP_SDA(" -> activity elem ok");
       
  1215                                 }
       
  1216     			            CleanupStack::PopAndDestroy( &activitiesElems );    
       
  1217                  	        }
       
  1218                  	    else if ( basicSimpleElem && 
       
  1219                  	            !personSubElems[j]->LocalName().CompareF( KPresenceActivities8 ) )
       
  1220                  	        {
       
  1221                  	        DP_SDA(" -> activities found from person elem");    
       
  1222                  	        RPointerArray<MSimpleElement> activitiesElems;
       
  1223                  	        CleanupClosePushL( activitiesElems );
       
  1224     			            
       
  1225     			            DP_SDA(" -> get activities");
       
  1226     			            personSubElems[j]->SimpleElementsL( activitiesElems );
       
  1227     			            DP_SDA2(" -> activities count: %d", 
       
  1228     			                activitiesElems.Count() );
       
  1229                             if ( activitiesElems.Count() )
       
  1230                                 {
       
  1231                                 DP_SDA(" -> get activities, multiple, use last");
       
  1232         			            TInt lastActiviesIndex = 
       
  1233         			                ( activitiesElems.Count() - 1 );
       
  1234     			                activitiesContentBuf.Copy( 
       
  1235     			                    activitiesElems[lastActiviesIndex]->LocalName() );
       
  1236     			                activitiesHandled = ETrue;
       
  1237     			                DP_SDA2(" -> activities len: %d", 
       
  1238     			                    activitiesContentBuf.Length() );
       
  1239     			                DP_SDA(" -> activity elem ok");
       
  1240                                 }
       
  1241     			            CleanupStack::PopAndDestroy( &activitiesElems );                     	                     	     
       
  1242                  	        }
       
  1243 		                } 
       
  1244 		            CleanupStack::PopAndDestroy( &basicElems );
       
  1245          	        }
       
  1246                 }
       
  1247             CleanupStack::PopAndDestroy( &personSubElems );
       
  1248             }
       
  1249  	    else if ( !simpleElements[index]->LocalName().CompareF( KPresenceNote8 ))
       
  1250  	        {
       
  1251  	        DP_SDA(" CPresencePluginPublisher -> note element found" );
       
  1252             noteSimpleElem = simpleElements[index];    
       
  1253  	        }
       
  1254         }
       
  1255     DP_SDA(" CPresencePluginPublisher -> elements handled");
       
  1256     __ASSERT_ALWAYS( basicSimpleElem, User::Leave( KErrCorrupt ) );
       
  1257     
       
  1258     if ( !activitiesHandled )
       
  1259         {
       
  1260         DP_SDA(" -> activities not handled, set unknown");
       
  1261         activitiesContentBuf.Copy( KPresenceUnknow );
       
  1262         }
       
  1263     
       
  1264     HBufC* nodeContent = basicSimpleElem->ContentUnicodeL();
       
  1265     CleanupStack::PushL( nodeContent );
       
  1266     
       
  1267     HBufC* noteContent = NULL;
       
  1268     if ( noteSimpleElem )
       
  1269         {
       
  1270         noteContent = noteSimpleElem->ContentUnicodeL();
       
  1271         CleanupStack::PushL( noteContent );
       
  1272         }
       
  1273     else
       
  1274         {
       
  1275         noteContent = KNullDesC().AllocLC();
       
  1276         }
       
  1277     
       
  1278     MPresenceBuddyInfo2::TAvailabilityValues cacheAvailability =
       
  1279         MPresenceBuddyInfo2::ENotAvailable;
       
  1280     NPresenceInfo::TAvailabilityValues ximpAvailability =
       
  1281         NPresenceInfo::ENotAvailable;
       
  1282     
       
  1283     HBufC* extendedCacheAvailability = HBufC::NewLC( KBufSize255 );
       
  1284     TPtr extendedCacheAvailabilityPtr( extendedCacheAvailability->Des() );
       
  1285     if ( nodeContent )
       
  1286         {
       
  1287         handled = ResolveOwnCacheValues( *nodeContent, 
       
  1288             activitiesContentBuf, cacheAvailability, 
       
  1289             extendedCacheAvailabilityPtr, ximpAvailability );
       
  1290         }
       
  1291 
       
  1292     __ASSERT_ALWAYS( handled, User::Leave( KErrCorrupt ));
       
  1293 
       
  1294     DP_SDA(" CPresencePluginPublisher -> write to cache" );
       
  1295     
       
  1296     DP_SDA2(" CPresencePluginPublisher -> availability: %d", 
       
  1297         cacheAvailability );
       
  1298     DP_SDA2(" CPresencePluginPublisher -> ext availability len: %d", 
       
  1299         extendedCacheAvailabilityPtr.Length() );        
       
  1300 
       
  1301     iPresenceData->WriteStatusToCacheL( 
       
  1302         *prefixStripped, cacheAvailability, 
       
  1303         extendedCacheAvailabilityPtr, *noteContent );           
       
  1304              
       
  1305     // Store publish variables for republish
       
  1306     iPresenceData->StorePresenceOwnPresenceL(
       
  1307         iConnObs.ServiceId(), ximpAvailability, *noteContent );
       
  1308 
       
  1309     CleanupStack::PopAndDestroy( extendedCacheAvailability );    
       
  1310     CleanupStack::PopAndDestroy( noteContent );        
       
  1311     CleanupStack::PopAndDestroy( nodeContent );        
       
  1312 
       
  1313     DP_SDA(" CPresencePluginPublisher -> attributes handled" );    
       
  1314     CleanupStack::PopAndDestroy( &simpleElements );
       
  1315         
       
  1316     DP_SDA(" CPresencePluginPublisher -> write to cache ok, cleanup" );
       
  1317     
       
  1318     // prefixStripped, entityUri
       
  1319     CleanupStack::PopAndDestroy( 2 );	
       
  1320     DP_SDA("CPresencePluginPublisher::StoreOwnStatusToCacheL out" );
       
  1321 	}
       
  1322 
       
  1323 // ---------------------------------------------------------------------------
       
  1324 // CPresencePluginPublisher::ResolveOwnCacheValues
       
  1325 // --------------------------------------------------------------------------- 
       
  1326 //
       
  1327 TBool CPresencePluginPublisher::ResolveOwnCacheValues( 
       
  1328     const TDesC& aUnicodeContent,
       
  1329     const TDesC& aActivities, 
       
  1330     MPresenceBuddyInfo2::TAvailabilityValues& aCacheAvailability,
       
  1331     TPtr& aExtendedCacheAvailability,
       
  1332     NPresenceInfo::TAvailabilityValues& aXimpApiAvailability )
       
  1333     {
       
  1334     DP_SDA("CPresencePluginPublisher::ResolveOwnCacheValuesL" );
       
  1335     using namespace NPresencePlugin::NPresence;
       
  1336     using namespace NPresencePlugin::NPresenceStates;
       
  1337     using namespace NPresenceInfo::NFieldType;
       
  1338     
       
  1339     TBool handled = EFalse;
       
  1340     if ( !aUnicodeContent.CompareF( KPresenceOpen ) &&
       
  1341         !aActivities.CompareF ( KPresenceBusy ) )
       
  1342         {
       
  1343         DP_SDA(" CPresencePluginPublisher -> busy");
       
  1344         aCacheAvailability = MPresenceBuddyInfo2::EBusy;
       
  1345         aExtendedCacheAvailability.Copy( KDndState() );
       
  1346         aXimpApiAvailability = NPresenceInfo::EBusy;
       
  1347         handled = ETrue;
       
  1348         }
       
  1349     // On-The-Phone case
       
  1350     else if ( !aUnicodeContent.CompareF( KPresenceOpen ) &&
       
  1351         !aActivities.CompareF ( KPresenceOnThePhone ) )
       
  1352         {
       
  1353         DP_SDA(" CPresencePluginPublisher ->  on-the-phone");
       
  1354         aCacheAvailability = MPresenceBuddyInfo2::EBusy;
       
  1355         aExtendedCacheAvailability.Copy( KOnPhoneState() );
       
  1356         aXimpApiAvailability = NPresenceInfo::EOnPhone;
       
  1357         handled = ETrue;
       
  1358         }
       
  1359     //Away case  
       
  1360     else if ( !aUnicodeContent.CompareF( KPresenceOpen ) &&
       
  1361         !aActivities.CompareF ( KPresenceAway ) )
       
  1362         {
       
  1363         DP_SDA(" CPresencePluginPublisher -> away");
       
  1364         aCacheAvailability = MPresenceBuddyInfo2::EBusy;
       
  1365         aExtendedCacheAvailability.Copy( KAwayState() );
       
  1366         aXimpApiAvailability = NPresenceInfo::EAway;
       
  1367         handled = ETrue;
       
  1368         }
       
  1369     //Dnd case  
       
  1370     else if ( !aUnicodeContent.CompareF( KPresenceOpen ) &&
       
  1371         !aActivities.CompareF ( KPresenceDoNotDisturb ) )
       
  1372         {
       
  1373         DP_SDA(" CPresencePluginPublisher -> dnd");
       
  1374         aCacheAvailability = MPresenceBuddyInfo2::EBusy;
       
  1375         aExtendedCacheAvailability.Copy( KDndState() );
       
  1376         aXimpApiAvailability = NPresenceInfo::EDoNotDisturb;
       
  1377         handled = ETrue;
       
  1378         }        
       
  1379     // Unknown open
       
  1380     else if ( !aUnicodeContent.CompareF( KPresenceOpen ) &&
       
  1381         !aActivities.CompareF ( KPresenceUnknow ) )
       
  1382         {
       
  1383         DP_SDA(" CPresencePluginPublisher -> open");
       
  1384         aCacheAvailability = MPresenceBuddyInfo2::EAvailable;
       
  1385         aExtendedCacheAvailability.Copy( KDefaultAvailableStatus() );
       
  1386         aXimpApiAvailability = NPresenceInfo::EAvailable;
       
  1387         handled = ETrue;
       
  1388         }
       
  1389     // available open
       
  1390     else if ( !aUnicodeContent.CompareF( KPresenceOpen ) &&
       
  1391         !aActivities.CompareF ( KPresenceAvailable ) )
       
  1392         {
       
  1393         DP_SDA(" CPresencePluginPublisher -> open");
       
  1394         aCacheAvailability = MPresenceBuddyInfo2::EAvailable;
       
  1395         aExtendedCacheAvailability.Copy( KDefaultAvailableStatus() );
       
  1396         aXimpApiAvailability = NPresenceInfo::EAvailable;
       
  1397         handled = ETrue;
       
  1398         } 
       
  1399     // available open
       
  1400     else if ( !aUnicodeContent.CompareF( KPresenceOpen ) &&
       
  1401         !aActivities.CompareF ( KPresenceOpen ) )
       
  1402         {
       
  1403         DP_SDA(" CPresencePluginPublisher -> open/open");
       
  1404         aCacheAvailability = MPresenceBuddyInfo2::EAvailable;
       
  1405         aExtendedCacheAvailability.Copy( KDefaultAvailableStatus() );
       
  1406         aXimpApiAvailability = NPresenceInfo::EAvailable;
       
  1407         handled = ETrue;
       
  1408         }                
       
  1409     //Unknown closed
       
  1410     else if ( !aUnicodeContent.CompareF( KPresenceClosed ) &&
       
  1411         !aActivities.CompareF ( KPresenceUnknow ) )
       
  1412         {
       
  1413         DP_SDA(" CPresencePluginPublisher -> closed");
       
  1414         aCacheAvailability = MPresenceBuddyInfo2::ENotAvailable;
       
  1415         aExtendedCacheAvailability.Copy( KInvisibleState() );
       
  1416         aXimpApiAvailability = NPresenceInfo::ENotAvailable;
       
  1417         handled = ETrue;
       
  1418         }
       
  1419     //All other states are closed
       
  1420     else
       
  1421         {
       
  1422         DP_SDA(" CPresencePluginPublisher -> else closed");
       
  1423         aCacheAvailability = MPresenceBuddyInfo2::ENotAvailable;
       
  1424         aExtendedCacheAvailability.Copy( KInvisibleState() );
       
  1425         aXimpApiAvailability = NPresenceInfo::ENotAvailable;
       
  1426         handled = ETrue;
       
  1427         }
       
  1428     DP_SDA2("CPresencePluginPublisher::ResolveOwnCacheValuesL out, ret: %d", 
       
  1429         handled );    
       
  1430     return handled;
       
  1431     }
       
  1432 
       
  1433 // End of file