ximpfw/presence/srcpresenceoperations/presencewatching/operationsubscribepresentitypresence.cpp
changeset 51 61fad867f68e
equal deleted inserted replaced
-1:000000000000 51:61fad867f68e
       
     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: implementation of COperationSubscribePresentityPresence
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <ximpprotocolconnection.h>
       
    20 #include <protocolpresencewatching.h>
       
    21 #include <protocolpresencefeatures.h>
       
    22 
       
    23 #include "operationsubscribepresentitypresence.h"
       
    24 #include "presenceinfofilterimp.h"
       
    25 #include "ximpidentityimp.h"
       
    26 #include "ximpobjecthelpers.h"
       
    27 #include "documentutils.h"
       
    28 #include "presencedatacacheimp.h"
       
    29 #include "ximppanics.h"
       
    30 #include "operationsynthesisesubscriptionevent.h"
       
    31 #include "ximprestrictedobjectcollectionimp.h"
       
    32 #include "ximphost.h"
       
    33 #include "ximpstatusimp.h"
       
    34 #include "ximppsccontextimp.h"
       
    35 #include "protocolpresencedatahostimp.h"
       
    36 
       
    37 #include "ximptrace.h"
       
    38 
       
    39 // ============================ MEMBER FUNCTIONS =============================
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // COperationSubscribePresentityPresence::COperationSubscribePresentityPresence()
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 EXPORT_C COperationSubscribePresentityPresence::COperationSubscribePresentityPresence()
       
    46     {
       
    47     }
       
    48 
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // COperationSubscribePresentityPresence::~COperationSubscribePresentityPresence()
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 COperationSubscribePresentityPresence::~COperationSubscribePresentityPresence()
       
    55     {
       
    56     delete iPif;
       
    57     delete iMergedPif;
       
    58     delete iIdentity;
       
    59     if( iSubItem )
       
    60         {
       
    61         iSubItem->Close();
       
    62         }
       
    63     }
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // COperationSubscribePresentityPresence::ConstructL()
       
    67 // ---------------------------------------------------------------------------
       
    68 //
       
    69 void COperationSubscribePresentityPresence::ConstructL( const TDesC8& aParamPck )
       
    70     {
       
    71     RXIMPObjOwningPtrArray< HBufC8 > packArray;
       
    72     CleanupClosePushL( packArray );
       
    73     TXIMPHBuf8Packer::UnPackArrayL( packArray, aParamPck );
       
    74 
       
    75     iIdentity = CXIMPIdentityImp::NewLC();
       
    76     CleanupStack::Pop( iIdentity );
       
    77     TXIMPObjectPacker< CXIMPIdentityImp >::UnPackL( *iIdentity, *( packArray[ 0 ] ) );
       
    78 
       
    79     iPif = CPresenceInfoFilterImp::NewLC();
       
    80     CleanupStack::Pop( iPif );
       
    81     TXIMPObjectPacker< CPresenceInfoFilterImp >::UnPackL( *iPif, *( packArray[ 1 ] ) );
       
    82 
       
    83     CleanupStack::PopAndDestroy(); // packArray
       
    84     }
       
    85 
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // COperationSubscribePresentityPresence::ProcessL()
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 void COperationSubscribePresentityPresence::ProcessL()
       
    92     {
       
    93     TRACE(_L("COperationSubscribePresentityPresence::ProcessL()" ) );
       
    94     CXIMPOperationBase::ProcessL();
       
    95 
       
    96     // MatchAddressTo( subscriptions, iIdentity )
       
    97     CPresentityPresenceSubscriptionItem& subItem =
       
    98                 iMyHost->PresenceDataAccess().PresenceDataCache().PresentityPresenceSubscriptionItemLC( *iIdentity );
       
    99     CleanupStack::Pop(); // subItem
       
   100     iSubItem = &subItem;
       
   101 
       
   102     // Get the subscription status
       
   103     // Notice that if there is more than one client and clients are using identity
       
   104     // which will alternate for request complete, this will affect wrong logic mapping
       
   105     // for subscription operation. 
       
   106     
       
   107     // For example: Client 1 is subscription "name" and server alters it to name@domain.com
       
   108     // Client 2 subscribes with "name" and it will get subscription status ENotSubscribedAtAll
       
   109     // when it should get ESubscribedForOtherCtxOnly.
       
   110     iSubscriptionStatus = iSubItem->SubscriptionStatus( iContext );
       
   111 
       
   112     MProtocolPresenceWatching& watching = iMyHost->GetConnection().ProtocolPresenceFeatures().PresenceWatching();
       
   113 
       
   114     switch( iSubscriptionStatus )
       
   115         {
       
   116         case CPresentityPresenceSubscriptionItem::ENotSubscribedAtAll:
       
   117             {
       
   118             TRACE(_L("COperationSubscribePresentityPresence::ProcessL() not subscribed" ) );
       
   119             iMergedPif = TXIMPObjectCloner< CPresenceInfoFilterImp >::CloneL( *iPif );
       
   120             // If this leaves, subscription is not created
       
   121             watching.DoSubscribePresentityPresenceL( iSubItem->Identity(),
       
   122                     *iMergedPif,
       
   123                     iReqId );
       
   124             break;
       
   125             }
       
   126         case CPresentityPresenceSubscriptionItem::ESubscribedForOtherCtxOnly:
       
   127             {
       
   128             TRACE(_L("COperationSubscribePresentityPresence::ProcessL() subscribed for other ctx" ) );
       
   129             // Aggregate subscription and context pifs
       
   130             // Check aggregate and if needed.. update subscription
       
   131             iMergedPif = DocumentUtils::InfoFilterUnionL( iSubItem->SubscriptionPif(), *iPif );
       
   132             if( iMergedPif->Contains( iSubItem->SubscriptionPif() ) )
       
   133                 {
       
   134                 TRACE(_L("COperationSubscribePresentityPresence::ProcessL() DoUpdatePresentityPresenceSubscriptionPifL 1" ) );
       
   135                 // If this leaves, subscription is not created
       
   136                 watching.DoUpdatePresentityPresenceSubscriptionPifL( *iIdentity, *iMergedPif, iReqId );
       
   137                 }
       
   138             else
       
   139                 {
       
   140                 TRACE(_L("COperationSubscribePresentityPresence::ProcessL() FakeCompleteRequest" ) );
       
   141                 iMyHost->FakeCompleteRequest( iReqId, KErrNone );
       
   142                 }
       
   143             break;
       
   144             }
       
   145         case CPresentityPresenceSubscriptionItem::ESubscribedForCtxOnly:
       
   146         case CPresentityPresenceSubscriptionItem::ESubscribedForCtxAndOthers:
       
   147             {
       
   148             TRACE(_L("COperationSubscribePresentityPresence::ProcessL() subscribed" ) );
       
   149             CPresenceInfoFilterImp* oldPif = 
       
   150                     iContext->PresenceInfoFilter( MXIMPPscContext::EPresentityPresenceFilter, iIdentity );
       
   151             CPresenceInfoFilterImp& oldMergedPif = iSubItem->SubscriptionPif();
       
   152             TBool fake( ETrue );
       
   153             TBool makeUnion( EFalse );
       
   154             if( !oldPif )
       
   155                 {
       
   156                 TRACE(_L("COperationSubscribePresentityPresence::ProcessL() no old pif" ) );
       
   157                 makeUnion = ETrue;
       
   158                 }
       
   159             else if ( *oldPif != *iPif ) 
       
   160                 {
       
   161                 makeUnion = ETrue;
       
   162                 }
       
   163             if( makeUnion )
       
   164                 {
       
   165                 iDoNotForce = ETrue;
       
   166                 CPresenceInfoFilterImp* withoutPif = 
       
   167                             iSubItem->CollectSubscriptionPifWithoutCtxL( iContext );
       
   168                 iMergedPif = DocumentUtils::InfoFilterUnionL( *withoutPif, *iPif );
       
   169                 if( iMergedPif->Contains( oldMergedPif ) )
       
   170                     {
       
   171                     fake = EFalse;
       
   172                     TRACE(_L("COperationSubscribePresentityPresence::ProcessL() DoUpdatePresentityPresenceSubscriptionPifL 2" ) );
       
   173                     watching.DoUpdatePresentityPresenceSubscriptionPifL( *iIdentity, *iMergedPif, iReqId );
       
   174                     }
       
   175                 }
       
   176             else
       
   177                 {
       
   178                 iMergedPif = TXIMPObjectCloner< CPresenceInfoFilterImp >::CloneL( oldMergedPif );
       
   179                 }
       
   180 
       
   181             if( fake )
       
   182                 {
       
   183                 TRACE(_L("COperationSubscribePresentityPresence::ProcessL() FakeCompleteRequest" ) );
       
   184                 iMyHost->FakeCompleteRequest( iReqId, KErrNone );
       
   185                 }
       
   186             break;
       
   187             }
       
   188 
       
   189         XIMP_DEFAULT_CASE_UNSUPPORTED( NXIMPPrivPanic::EInvalidSubscriptionStatus );
       
   190         }
       
   191     }
       
   192 
       
   193 // ---------------------------------------------------------------------------
       
   194 // COperationSubscribePresentityPresence::RequestCompletedL()
       
   195 // ---------------------------------------------------------------------------
       
   196 //
       
   197 void COperationSubscribePresentityPresence::RequestCompletedL()
       
   198     {
       
   199     TRACE(_L("COperationSubscribePresentityPresence::RequestCompletedL()" ) );
       
   200     CXIMPOperationBase::RequestCompletedL();
       
   201 
       
   202     if( iStatusObj->ResultCode() == KErrNone )
       
   203         {
       
   204         if( iSubscriptionStatus == CPresentityPresenceSubscriptionItem::ENotSubscribedAtAll )
       
   205             {
       
   206             TRACE(_L("COperationSubscribePresentityPresence::RequestCompletedL() ENotSubscribedAtAll" ) );
       
   207             MXIMPBase* object = NULL; 
       
   208             iObjCollection->GetByType( object, MXIMPIdentity::KInterfaceId );
       
   209             if( object )
       
   210                 {
       
   211                 TRACE(_L("COperationSubscribePresentityPresence::RequestCompletedL() identity change" ) );
       
   212                 delete iIdentity;
       
   213                 iIdentity = TXIMPGetImpClassOrPanic< CXIMPIdentityImp >::From( *object );
       
   214 
       
   215                 // Got altered identity. If it differs from original
       
   216                 // we'll create a new subscription item and close the old.
       
   217                 CPresentityPresenceSubscriptionItem& altSubItem = 
       
   218                     iMyHost->PresenceDataAccess().PresenceDataCache().PresentityPresenceSubscriptionItemLC( *iIdentity );
       
   219 
       
   220                 iSubItem->Close();
       
   221                 iSubItem = &altSubItem;
       
   222 
       
   223                 CleanupStack::Pop(); // altSubItem                    
       
   224                 }            
       
   225             }
       
   226             
       
   227         if( iSubscriptionStatus == CPresentityPresenceSubscriptionItem::ENotSubscribedAtAll ||
       
   228             iSubscriptionStatus == CPresentityPresenceSubscriptionItem::ESubscribedForOtherCtxOnly )
       
   229             {
       
   230             TRACE(_L("COperationSubscribePresentityPresence::RequestCompletedL() calling iSubItem->AddSubscriberL" ) );
       
   231             TInt error ( KErrNone );
       
   232             TRAP( error, iSubItem->AddSubscriberL( iContext ) );
       
   233             TRACE_1( _L("COperationSubscribePresentityPresence::RequestCompletedL():  iSubItem->AddSubscriberL : error =%d"), error );
       
   234             }
       
   235             
       
   236         iContext->SetPresenceInfoFilterL( MXIMPPscContext::EPresentityPresenceFilter,
       
   237                 iPif, iIdentity );
       
   238         iPif = NULL;
       
   239         iSubItem->SetSubscriptionPif( iMergedPif );
       
   240         iMergedPif = NULL;
       
   241 
       
   242         SynthesiseEventL();
       
   243         }
       
   244     }
       
   245 
       
   246 
       
   247 // ---------------------------------------------------------------------------
       
   248 // COperationSubscribePresentityPresence::Type()
       
   249 // ---------------------------------------------------------------------------
       
   250 //
       
   251 TInt
       
   252     COperationSubscribePresentityPresence::Type() const
       
   253     {
       
   254     return NPresenceOps::EPrSubscribePresentityPresence;
       
   255     }
       
   256 
       
   257 // ---------------------------------------------------------------------------
       
   258 // COperationSubscribePresentityPresence::SynthesiseEventL()
       
   259 // ---------------------------------------------------------------------------
       
   260 //
       
   261 void COperationSubscribePresentityPresence::SynthesiseEventL()
       
   262     {
       
   263     TBool force =
       
   264     ( iSubscriptionStatus == CPresentityPresenceSubscriptionItem::ESubscribedForCtxOnly ||
       
   265       iSubscriptionStatus == CPresentityPresenceSubscriptionItem::ESubscribedForCtxAndOthers ) ? ETrue : EFalse;
       
   266 
       
   267     COperationSynthesiseSubscriptionEvent* synthOp =
       
   268             new ( ELeave ) COperationSynthesiseSubscriptionEvent(
       
   269                 COperationSynthesiseSubscriptionEvent::EPresentityPresence, force && !iDoNotForce  );
       
   270     CleanupStack::PushL( synthOp );
       
   271     synthOp->BaseConstructL( TXIMPRequestId(), iContext );
       
   272     synthOp->ConstructL( *iIdentity );
       
   273     iMyHost->AddNewOperationL( *synthOp );
       
   274     CleanupStack::Pop( synthOp );
       
   275     }
       
   276 
       
   277 
       
   278 
       
   279 // End of file