ncdengine/provider/client/src/ncdpurchaseoperationproxy.cpp
changeset 4 32704c33136d
equal deleted inserted replaced
-1:000000000000 4:32704c33136d
       
     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:   ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <badesca.h>
       
    20 #include <s32strm.h>
       
    21 
       
    22 #include "ncdpurchaseoperationproxy.h"
       
    23 #include "ncdpurchaseoperationobserver.h"
       
    24 #include "ncdoperationfunctionids.h"
       
    25 #include "catalogsdebug.h"
       
    26 #include "catalogsclientserver.h"
       
    27 #include "ncdpurchaseoptionproxy.h"
       
    28 #include "ncdoperationproxyremovehandler.h"
       
    29 #include "catalogsconstants.h"
       
    30 #include "catalogsutils.h"
       
    31 #include "ncdqueryimpl.h"
       
    32 #include "ncdqueryselectionitemimpl.h"
       
    33 #include "ncdsubscriptionmanagerproxy.h"
       
    34 #include "ncdnodeproxy.h"
       
    35 #include "ncdnodemanagerproxy.h"
       
    36 
       
    37 // ======== MEMBER FUNCTIONS ========
       
    38 
       
    39 // ---------------------------------------------------------------------------
       
    40 // ?description_if_needed
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43 CNcdPurchaseOperationProxy* CNcdPurchaseOperationProxy::NewL( 
       
    44     MCatalogsClientServer& aSession,
       
    45     TInt aHandle,
       
    46     MNcdOperationProxyRemoveHandler* aRemoveHandler,
       
    47     CNcdNodeProxy* aNode,
       
    48     CNcdPurchaseOptionProxy* aSelectedPurchaseOption,
       
    49     MNcdPurchaseOperationObserver* aObserver,
       
    50     CNcdSubscriptionManagerProxy* aSubscriptionManager,
       
    51     CNcdNodeManagerProxy* aNodeManager,
       
    52     MNcdClientLocalizer* aLocalizer )
       
    53     {
       
    54     CNcdPurchaseOperationProxy* self = CNcdPurchaseOperationProxy::NewLC( 
       
    55         aSession,
       
    56         aHandle,
       
    57         aRemoveHandler,
       
    58         aNode,
       
    59         aSelectedPurchaseOption,
       
    60         aObserver,
       
    61         aSubscriptionManager,
       
    62         aNodeManager,
       
    63         aLocalizer );
       
    64     CleanupStack::Pop( self );
       
    65     return self;
       
    66     }
       
    67 
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // ?description_if_needed
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 CNcdPurchaseOperationProxy* CNcdPurchaseOperationProxy::NewLC( 
       
    74     MCatalogsClientServer& aSession,
       
    75     TInt aHandle,
       
    76     MNcdOperationProxyRemoveHandler* aRemoveHandler,
       
    77     CNcdNodeProxy* aNode,
       
    78     CNcdPurchaseOptionProxy* aSelectedPurchaseOption,
       
    79     MNcdPurchaseOperationObserver* aObserver,
       
    80     CNcdSubscriptionManagerProxy* aSubscriptionManager,
       
    81     CNcdNodeManagerProxy* aNodeManager,
       
    82     MNcdClientLocalizer* aLocalizer )
       
    83     {
       
    84     CNcdPurchaseOperationProxy* self =
       
    85         new( ELeave ) CNcdPurchaseOperationProxy( aLocalizer );
       
    86     
       
    87     self->AddRef();
       
    88     CleanupReleasePushL( *self );    
       
    89     self->ConstructL( 
       
    90         aSession, 
       
    91         aHandle, 
       
    92         aRemoveHandler, 
       
    93         aNode, 
       
    94         aSelectedPurchaseOption,
       
    95         aObserver,
       
    96         aSubscriptionManager,
       
    97         aNodeManager );
       
    98 
       
    99     return self;
       
   100     }
       
   101 
       
   102 
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 // From MNcdPurchaseOperation
       
   106 // ---------------------------------------------------------------------------
       
   107 //
       
   108 const MNcdPurchaseOption& CNcdPurchaseOperationProxy::PurchaseOption()
       
   109     {
       
   110     DASSERT( iPurchaseOption );
       
   111     return *iPurchaseOption;
       
   112     }
       
   113 	
       
   114 
       
   115 
       
   116 // ---------------------------------------------------------------------------
       
   117 // From MNcdOperation
       
   118 // ?implementation_description
       
   119 // ---------------------------------------------------------------------------
       
   120 //
       
   121 TNcdInterfaceId CNcdPurchaseOperationProxy::OperationType() const
       
   122     {
       
   123     return static_cast<TNcdInterfaceId>(MNcdPurchaseOperation::KInterfaceUid);
       
   124     }
       
   125 
       
   126 
       
   127 
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // ?description_if_needed
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 CNcdPurchaseOperationProxy::CNcdPurchaseOperationProxy(
       
   134     MNcdClientLocalizer* aLocalizer ) : 
       
   135     CNcdOperation< MNcdPurchaseOperation >( aLocalizer )
       
   136     {
       
   137     }
       
   138 
       
   139 // ---------------------------------------------------------------------------
       
   140 // ?description_if_needed
       
   141 // ---------------------------------------------------------------------------
       
   142 //
       
   143 CNcdPurchaseOperationProxy::~CNcdPurchaseOperationProxy()
       
   144     {    
       
   145     DLTRACEIN( ( "" ) );
       
   146     
       
   147     if ( iPurchaseOption != NULL )
       
   148         {
       
   149         iPurchaseOption->InternalRelease();
       
   150         }
       
   151     
       
   152     DASSERT( iRemoveHandler );
       
   153     if ( iRemoveHandler )
       
   154         {        
       
   155         DLTRACE(("Removing proxy from remove handler"));
       
   156         iRemoveHandler->RemoveOperationProxy( *this );
       
   157         }    
       
   158     DLTRACEOUT( ( "" ) );    
       
   159     }
       
   160 
       
   161 // ---------------------------------------------------------------------------
       
   162 // ?description_if_needed
       
   163 // ---------------------------------------------------------------------------
       
   164 //
       
   165 void CNcdPurchaseOperationProxy::ConstructL( MCatalogsClientServer& aSession,
       
   166     TInt aHandle,
       
   167     MNcdOperationProxyRemoveHandler* aRemoveHandler,
       
   168     CNcdNodeProxy* aNode,
       
   169     CNcdPurchaseOptionProxy* aSelectedPurchaseOption,
       
   170     MNcdPurchaseOperationObserver* aObserver,
       
   171     CNcdSubscriptionManagerProxy* aSubscriptionManager,
       
   172     CNcdNodeManagerProxy* aNodeManager )
       
   173     {
       
   174     DLTRACEIN( ( "" ) );
       
   175     CNcdBaseOperationProxy::ConstructL( aSession, 
       
   176                                         aHandle, 
       
   177                                         aRemoveHandler,
       
   178                                         aNode,
       
   179                                         aNodeManager );
       
   180     
       
   181     iObserver = aObserver;
       
   182     iSubscriptionManager = aSubscriptionManager;
       
   183     
       
   184     // We increment the reference count of the used purchaseoption
       
   185     // to ensure that it exists as long as the operation.
       
   186     iPurchaseOption = aSelectedPurchaseOption;
       
   187     if ( iPurchaseOption != NULL )
       
   188         {
       
   189         iPurchaseOption->InternalAddRef();
       
   190         }
       
   191     
       
   192     
       
   193     // Initialize the operation
       
   194 //    InitializeOperationL();
       
   195     DLTRACEOUT( ( "" ) );
       
   196     }
       
   197 
       
   198 
       
   199 
       
   200 // ---------------------------------------------------------------------------
       
   201 // HandleCompletedMessage
       
   202 // ---------------------------------------------------------------------------
       
   203 //
       
   204 
       
   205 void CNcdPurchaseOperationProxy::HandleCompletedMessage(
       
   206     TNcdOperationMessageCompletionId aCompletionId,
       
   207     RReadStream& aReadStream,
       
   208     TInt aDataLength )
       
   209     {
       
   210     DLTRACEIN((_L("aCompletionId =%d, aDataLength =%d"), aCompletionId,
       
   211         aDataLength));
       
   212     switch ( aCompletionId )
       
   213         {
       
   214         case ENCDOperationMessageCompletionProgress:
       
   215             {
       
   216             TRAPD( err, SendContinueMessageL() );
       
   217             if ( err != KErrNone ) 
       
   218                 {
       
   219                 CompleteCallback( err );
       
   220                 }
       
   221             break;
       
   222             }
       
   223         case ENCDOperationMessageCompletionComplete:
       
   224             {
       
   225             DLTRACE( ( "ENCDOperationMessageCompletionComplete" ) );
       
   226             // Should get errorcode from the received message
       
   227             CompleteCallback( KErrNone );
       
   228             break;
       
   229             }
       
   230                                 
       
   231         default:
       
   232             {
       
   233             // Call base class
       
   234             CNcdBaseOperationProxy::HandleCompletedMessage( aCompletionId,
       
   235                 aReadStream,
       
   236                 aDataLength );
       
   237             break;
       
   238             }
       
   239         }
       
   240     }
       
   241 
       
   242     
       
   243 // ---------------------------------------------------------------------------
       
   244 // Handle progress callback
       
   245 // ---------------------------------------------------------------------------
       
   246 //
       
   247 void CNcdPurchaseOperationProxy::ProgressCallback()
       
   248     {
       
   249     DLTRACEIN( ( "" ) );
       
   250     DASSERT( 0 );
       
   251     DLTRACEOUT( ( "" ) );
       
   252     }
       
   253     
       
   254     
       
   255 // ---------------------------------------------------------------------------
       
   256 // Handle query received callback
       
   257 // ---------------------------------------------------------------------------
       
   258 //
       
   259 void CNcdPurchaseOperationProxy::QueryReceivedCallback( CNcdQuery* aQuery )
       
   260     {
       
   261     DLTRACEIN((""));
       
   262     aQuery->AddRef();
       
   263     
       
   264     // Following special case concerns only one query received during
       
   265     // the purchase process.
       
   266     // The query is received in the first phase of purchase process.
       
   267     // It is not received from the server but it is constructed on
       
   268     // the server side when the information response is received
       
   269     // from the server. The information response contains
       
   270     // updated price for the purchase. The updated price is set
       
   271     // on the server side into the used purchase option. This is
       
   272     // why the purchase option has to be reinternalized.
       
   273     if ( aQuery->Id() == KQueryIdPaymentMethod() )
       
   274         {
       
   275         // If this fails, then possibly updated price
       
   276         // cannot be shown to user and purchase process should
       
   277         // not be continued
       
   278         TRAPD( priceError, iPurchaseOption->InternalizeL() );
       
   279         if ( priceError )
       
   280             {
       
   281             // This error handling is copied and altered from the base
       
   282             // operation's DoCancelOperation because call for
       
   283             // DoCancelOperation would complete the operation with
       
   284             // KErrCancel
       
   285             
       
   286             
       
   287             DLTRACE(("Price update failed. Canceling operation."));
       
   288             Cancel();
       
   289             
       
   290             // Send cancel message to server-side        
       
   291             TInt tmpNum( 0 );
       
   292             ClientServerSession().
       
   293                 SendSync( ENCDOperationFunctionCancel,
       
   294                           KNullDesC8(),
       
   295                           tmpNum,
       
   296                           Handle() );     
       
   297             SetState( MNcdOperation::EStateCancelled );
       
   298             
       
   299             DLTRACE(( "Calling complete callback with error: %d",
       
   300                       priceError ));
       
   301             CompleteCallback( priceError );
       
   302             return;
       
   303             }
       
   304         }    
       
   305     
       
   306     iObserver->QueryReceived( *this, aQuery );
       
   307     DLTRACEOUT((""));
       
   308     }
       
   309     
       
   310     
       
   311 // ---------------------------------------------------------------------------
       
   312 // Handle operation complete
       
   313 // ---------------------------------------------------------------------------
       
   314 //
       
   315 void CNcdPurchaseOperationProxy::CompleteCallback( TInt aError )
       
   316     {
       
   317     DLTRACEIN( ( "Error: %d", aError ) );
       
   318     TInt error = aError;
       
   319 
       
   320 	AddRef();
       
   321 
       
   322     DASSERT( iPurchaseOption );
       
   323     MNcdPurchaseOption::TType purchaseOptionType =
       
   324         iPurchaseOption->PurchaseOptionType();
       
   325 
       
   326     DLINFO(("Update purchase history"));
       
   327     TRAP_IGNORE( UpdateOperationInfoToPurchaseHistoryL( aError ) );        
       
   328 
       
   329     // Subscription manager on server side handles the informing
       
   330     // of all relevant proxies so no internalization for
       
   331     // subscription manager proxy here is needed.
       
   332 
       
   333     // Actually node internalization is probably not needed
       
   334     // in case we buy subscription, or subscription upgrade.
       
   335     // Then again, a updated price can be received in the
       
   336     // purchase operation. Should because of this internalize
       
   337     // be done always to update purchase options? Or can the
       
   338     // prices change?
       
   339 
       
   340     TRAPD( err,
       
   341            NodeManager()->InternalizeRelatedNodesL( *NodeProxy() ) );	
       
   342     if ( err != KErrNone ) 
       
   343 	    {
       
   344 	    error = err;
       
   345 	    }
       
   346 	
       
   347     iObserver->OperationComplete( *this, error );
       
   348     Release();
       
   349     DLTRACEOUT( ( "" ) );
       
   350     }