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