PECengine/AttributeLibrary2/SrcTransactions/CPEngPresenceNotificationHandler.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2004 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:  Presence notification handler.
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include "CPEngPresenceNotificationHandler.h"
       
    20 #include "MPEngXMLParser.h"
       
    21 
       
    22 #include "MPEngPresenceAdvancedAttrModel2.h"
       
    23 #include "MPEngPresenceAttrManager.h"
       
    24 #include "MPEngPresenceAttrTransactionEnv.h"
       
    25 #include "PEngAddressUtils.h"
       
    26 #include "PEngAttrModelHelper.h"
       
    27 #include "PresenceDebugPrint.h"
       
    28 #include "PEngWVPresenceAttributes2.h"
       
    29 
       
    30 #include <E32Std.h>
       
    31 
       
    32 
       
    33 // ============================ MEMBER FUNCTIONS ===============================
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CPEngPresenceNotificationHandler::NewL()
       
    37 // Two-phased constructor.
       
    38 // -----------------------------------------------------------------------------
       
    39 //
       
    40 CPEngPresenceNotificationHandler* CPEngPresenceNotificationHandler::NewL(
       
    41     const TDesC& aDomain,
       
    42     TPEngWVCspVersion aCspVersion,
       
    43     MPEngPresenceAttrManager& aAttrManager,
       
    44     MPEngPresenceAttrTransactionEnv& aTransEnv )
       
    45     {
       
    46     CPEngPresenceNotificationHandler* self = new ( ELeave ) CPEngPresenceNotificationHandler(
       
    47         aAttrManager,
       
    48         aTransEnv,
       
    49         aCspVersion );
       
    50 
       
    51     CleanupStack::PushL( self );
       
    52     self->ConstructL( aDomain );
       
    53     CleanupStack::Pop(); //self
       
    54 
       
    55     return self;
       
    56     }
       
    57 
       
    58 
       
    59 
       
    60 // Destructor
       
    61 CPEngPresenceNotificationHandler::~CPEngPresenceNotificationHandler()
       
    62     {
       
    63     iPresenceDataBlocks.Reset();
       
    64     iModels.ResetAndDestroy();
       
    65     delete iXmlParser;
       
    66     delete iDomain;
       
    67     }
       
    68 
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CPEngPresenceNotificationHandler::CPEngPresenceNotificationHandler
       
    72 // C++ default constructor can NOT contain any code, that
       
    73 // might leave.
       
    74 //
       
    75 // Useually only one or two users presence is updated at time ==> 2 blocks
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 CPEngPresenceNotificationHandler::CPEngPresenceNotificationHandler(
       
    79     MPEngPresenceAttrManager& aAttrManager,
       
    80     MPEngPresenceAttrTransactionEnv& aTransEnv,
       
    81     TPEngWVCspVersion aCspVersion )
       
    82         : iAttributeManager( aAttrManager ),
       
    83         iTransEnv( aTransEnv ),
       
    84         iCspVersion( aCspVersion ),
       
    85         iPresenceDataBlocks( 2 )
       
    86     {
       
    87     }
       
    88 
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CPEngPresenceNotificationHandler::ConstructL()
       
    92 // Symbian 2nd phase constructor can leave.
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 void CPEngPresenceNotificationHandler::ConstructL( const TDesC& aDomain )
       
    96     {
       
    97     iXmlParser = CreateXMLParserL();
       
    98     iDomain = aDomain.AllocL();
       
    99     }
       
   100 
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // CPEngPresenceNotificationHandler::ProcessRequestL()
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 void CPEngPresenceNotificationHandler::ProcessRequestL( const TDesC8& aRequest,
       
   107                                                         TRequestStatus& /*aStatus*/ )
       
   108     {
       
   109     PENG_DP_TXT( "CPEngPresenceNotificationHandler::ProcessRequestL()" );
       
   110 
       
   111 
       
   112     if ( !iXmlParser->DecodeL( aRequest,
       
   113                                KPresenceNotificationRequest,
       
   114                                ETrue ) )
       
   115         {
       
   116         return;
       
   117         }
       
   118 
       
   119 
       
   120     if ( iXmlParser->DecodeL( iXmlParser->ResultL(),
       
   121                               KPresenceXMLTag,
       
   122                               ETrue ) )
       
   123         {
       
   124         //Place found presence data blocks to an array
       
   125         const TInt foundPresencesCount = iXmlParser->Count();
       
   126         TInt presenceIx = 0;
       
   127         for ( presenceIx = 0; presenceIx < foundPresencesCount; presenceIx++ )
       
   128             {
       
   129             iPresenceDataBlocks.AppendL( iXmlParser->ResultL( presenceIx ) );
       
   130             }
       
   131 
       
   132 
       
   133         //And process each presence notify block one by one
       
   134         for ( presenceIx = 0; presenceIx < foundPresencesCount; presenceIx++ )
       
   135             {
       
   136             TPtrC8 notifyBlock = iPresenceDataBlocks[ presenceIx ];
       
   137             TBool matchedToSubscription = ProcessNotifyBlockL( notifyBlock,
       
   138                                                                *iDomain,
       
   139                                                                iTransEnv.SubscribedPresenceIDs(),
       
   140                                                                iAttributeManager,
       
   141                                                                *iXmlParser,
       
   142                                                                iModels );
       
   143             if ( !matchedToSubscription )
       
   144                 {
       
   145                 //notification wasn't matched to any subscription
       
   146                 //que the orphan for later processing
       
   147                 iTransEnv.QueOrphanNotificationL( notifyBlock );
       
   148                 }
       
   149             }
       
   150 
       
   151         iAttributeManager.ForceStoreBatchL( iModels );
       
   152         iModels.ResetAndDestroy();
       
   153         }
       
   154     }
       
   155 
       
   156 
       
   157 
       
   158 // -----------------------------------------------------------------------------
       
   159 // CPEngPresenceNotificationHandler::CancelProcessing()
       
   160 // -----------------------------------------------------------------------------
       
   161 //
       
   162 void CPEngPresenceNotificationHandler::CancelProcessing()
       
   163     {
       
   164     PENG_DP_TXT( "CPEngPresenceNotificationHandler::ProcessRequestL()" );
       
   165     }
       
   166 
       
   167 
       
   168 
       
   169 
       
   170 // -----------------------------------------------------------------------------
       
   171 // CPEngPresenceNotificationHandler::CanHandleL()
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 TBool CPEngPresenceNotificationHandler::CanHandleL( const TDesC8& aIncomingRequest )
       
   175     {
       
   176     PENG_DP_TXT( "CPEngPresenceNotificationHandler::CanHandleL()" );
       
   177 
       
   178     TBool retStatus( EFalse );
       
   179     MPEngXMLParser* xmlParser = CreateXMLParserLC();
       
   180     if ( xmlParser->DecodeL( aIncomingRequest,
       
   181                              KPresenceNotificationRequest,
       
   182                              EFalse ) )
       
   183         {
       
   184         //This is a presence notification,
       
   185         retStatus = ETrue;
       
   186         }
       
   187 
       
   188     CleanupStack::PopAndDestroy(); //xmlParser
       
   189     PENG_DP( D_PENG_LIT( "CPEngPresenceNotificationHandler::CanHandleL() - %d" ), retStatus );
       
   190 
       
   191     return retStatus;
       
   192     }
       
   193 
       
   194 
       
   195 
       
   196 
       
   197 // -----------------------------------------------------------------------------
       
   198 // CPEngPresenceNotificationHandler::ProcessNotifyBlockL()
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 TBool CPEngPresenceNotificationHandler::ProcessNotifyBlockL(
       
   202     const TDesC8& aPresenceBlock,
       
   203     const TDesC& aDomain,
       
   204     const MDesCArray& aSubscribedPresences,
       
   205     MPEngPresenceAttrManager& aAttributeManager,
       
   206     MPEngXMLParser& aParser,
       
   207     RPointerArray< MPEngPresenceAttrModel2 >& aModels )
       
   208     {
       
   209     if ( aParser.DecodeL( aPresenceBlock, KUserIDXMLTag, EFalse ) )
       
   210         {
       
   211         //There is a presence id => unescape the id
       
   212         HBufC* nwPresenceID = aParser.ResultAsWVAddressL();
       
   213         CleanupStack::PushL( nwPresenceID );
       
   214 
       
   215         PENG_DP( D_PENG_LIT( "CPEngPresenceNotificationHandler - Presence notification for NW ID [%S]" ),
       
   216                  nwPresenceID );
       
   217 
       
   218         //Try get the presence id as it is subscribed
       
   219         TInt subscribedIx = PEngAddressUtils::FindWVAddressF( *nwPresenceID,
       
   220                                                               aSubscribedPresences,
       
   221                                                               aDomain,
       
   222                                                               PEngAddressUtils::EMatchToUniqueId );
       
   223         CleanupStack::PopAndDestroy( nwPresenceID );
       
   224 
       
   225         if ( subscribedIx == KErrNotFound )
       
   226             {
       
   227             //Matching subscription ID for notified presence ID not found
       
   228             PENG_DP( D_PENG_LIT( "CPEngPresenceNotificationHandler - NW ID not matched" ) );
       
   229             return EFalse;
       
   230             }
       
   231 
       
   232 
       
   233         const TDesC& subscribedPresenceID = aSubscribedPresences.MdcaPoint( subscribedIx );
       
   234         PENG_DP( D_PENG_LIT( "CPEngPresenceNotificationHandler - NW ID matches to local [%S]" ),
       
   235                  &subscribedPresenceID );
       
   236 
       
   237 
       
   238         //go through all known network attributes
       
   239         const TArray<TUint32> types = aAttributeManager.KnownAttributeTypes();
       
   240         const TInt typeCount = types.Count();
       
   241 
       
   242         MPEngPresenceAttrModel2* model = NULL;
       
   243         // load and store online status to notify change
       
   244         aAttributeManager.LoadAttributeLC( model,
       
   245                                            subscribedPresenceID,
       
   246                                            KUidPrAttrOnlineStatus,
       
   247                                            EPEngStorableModel );
       
   248         PEngAttrModelHelper::AddOrReplaceModelL( aModels,  *model );
       
   249 
       
   250         CleanupStack::Pop(); //model
       
   251 
       
   252         for ( TInt ii = 0; ii < typeCount; ii++ )
       
   253             {
       
   254             const TUint32 attributeType = types[ ii ];
       
   255             TPtrC8 attributeName;
       
   256             TPtrC8 attributeNameSpace;
       
   257             TInt err = aAttributeManager.GetAttributeXmlNameAndNameSpace( attributeType,
       
   258                                                                           attributeName,
       
   259                                                                           attributeNameSpace );
       
   260             if ( err == KErrNone )
       
   261                 {
       
   262                 if ( aParser.DecodeL( aPresenceBlock, attributeName, ETrue ) )
       
   263                     {
       
   264                     //attribute type was found from presence data block
       
   265                     //==> Update the attribute
       
   266 
       
   267                     PENG_DP( D_PENG_LIT( "CPEngPresenceNotificationHandler - Updating attribute [%08x]" ),
       
   268                              attributeType );
       
   269 
       
   270                     model = NULL;
       
   271                     aAttributeManager.LoadAttributeLC( model,
       
   272                                                        subscribedPresenceID,
       
   273                                                        attributeType,
       
   274                                                        EPEngStorableModel );
       
   275 
       
   276                     model->Advanced()->DecodeFromXmlL( aParser, aParser.ResultL() );
       
   277 
       
   278                     //Array might have already a same attribute for user
       
   279                     //==> replace existing or add the new
       
   280                     PEngAttrModelHelper::AddOrReplaceModelL( aModels, *model );
       
   281                     CleanupStack::Pop(); //model
       
   282                     }
       
   283                 else
       
   284                     {
       
   285                     if ( attributeType == KUidPrAttrStatusText )
       
   286                         {
       
   287 
       
   288                         model = NULL;
       
   289                         aAttributeManager.LoadAttributeLC( model,
       
   290                                                            subscribedPresenceID,
       
   291                                                            KUidPrAttrStatusText,
       
   292                                                            EPEngStorableModel );
       
   293 
       
   294                         // model->Advanced()->DecodeFromXmlL( aParser, aParser.ResultL() );
       
   295 
       
   296                         //Array might have already a same attribute for user
       
   297                         //==> replace existing or add the new
       
   298 
       
   299                         model->SetDataDesC16L( KNullDesC, EPEngStatusText );
       
   300 
       
   301                         PEngAttrModelHelper::AddOrReplaceModelL( aModels, *model );
       
   302                         CleanupStack::Pop(); //model
       
   303 
       
   304                         }
       
   305 
       
   306                     }
       
   307                 }
       
   308 
       
   309 
       
   310             }
       
   311 
       
   312         }
       
   313 
       
   314     //Not a notification at all or matching subscription
       
   315     //for notified presence ID was found
       
   316     //==> XML block succesfully processed
       
   317     return ETrue;
       
   318     }
       
   319 
       
   320 
       
   321 
       
   322 //  End of File
       
   323