iaupdate/IAD/engine/controller/src/iaupdatepurchaseoperation.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2007-2008 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:   CIAUpdatePurchaseOperation 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <ncdquery.h>
       
    21 #include <ncdprogress.h>
       
    22 #include <ncdoperation.h>
       
    23 #include <ncdpurchaseoperation.h>
       
    24 #include <ncdpurchaseoption.h>
       
    25 #include <ncdnodepurchase.h>
       
    26 #include <ncdnode.h>
       
    27 #include <catalogsutils.h>
       
    28 
       
    29 #include "iaupdatepurchaseoperation.h"
       
    30 #include "iaupdatenodeimpl.h"
       
    31 #include "iaupdatedebug.h"
       
    32 
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // CIAUpdatePurchaseOperation::NewL
       
    36 // 
       
    37 // ---------------------------------------------------------------------------
       
    38 //
       
    39 CIAUpdatePurchaseOperation* CIAUpdatePurchaseOperation::NewL(
       
    40     CIAUpdateNode& aNode,
       
    41     MIAUpdateContentOperationObserver& aObserver )
       
    42     {
       
    43     CIAUpdatePurchaseOperation* self = 
       
    44         CIAUpdatePurchaseOperation::NewLC( aNode, aObserver );
       
    45     CleanupStack::Pop( self );
       
    46     return self;
       
    47     }
       
    48     
       
    49     
       
    50 // ---------------------------------------------------------------------------
       
    51 // CIAUpdatePurchaseOperation::NewLC
       
    52 // 
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 CIAUpdatePurchaseOperation* CIAUpdatePurchaseOperation::NewLC(
       
    56     CIAUpdateNode& aNode,
       
    57     MIAUpdateContentOperationObserver& aObserver )
       
    58     {
       
    59     CIAUpdatePurchaseOperation* self =
       
    60         new( ELeave ) CIAUpdatePurchaseOperation( aNode, aObserver );
       
    61     CleanupStack::PushL( self );
       
    62     self->ConstructL();
       
    63     return self;
       
    64     }
       
    65 
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // CIAUpdatePurchaseOperation::CIAUpdatePurchaseOperation
       
    69 // 
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 CIAUpdatePurchaseOperation::CIAUpdatePurchaseOperation(
       
    73     CIAUpdateNode& aNode,
       
    74     MIAUpdateContentOperationObserver& aObserver )
       
    75 : CIAUpdateNormalContentOperation( aNode, aObserver )
       
    76     {
       
    77     IAUPDATE_TRACE("[IAUPDATE] CIAUpdatePurchaseOperation::CIAUpdatePurchaseOperation");
       
    78     }
       
    79 
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // CIAUpdatePurchaseOperation::ConstructL
       
    83 // 
       
    84 // ---------------------------------------------------------------------------
       
    85 //
       
    86 void CIAUpdatePurchaseOperation::ConstructL()
       
    87     {
       
    88     IAUPDATE_TRACE("[IAUPDATE] CIAUpdatePurchaseOperation::ConstructL begin");
       
    89     
       
    90     CIAUpdateNormalContentOperation::ConstructL();
       
    91 
       
    92     IAUPDATE_TRACE("[IAUPDATE] CIAUpdatePurchaseOperation::ConstructL end");
       
    93     }
       
    94     
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // CIAUpdatePurchaseOperation::~CIAUpdatePurchaseOperation
       
    98 // 
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 CIAUpdatePurchaseOperation::~CIAUpdatePurchaseOperation()
       
   102     {
       
   103     IAUPDATE_TRACE("[IAUPDATE] CIAUpdatePurchaseOperation::~CIAUpdatePurchaseOperation begin");
       
   104 
       
   105     // This will also reset and delete all the necessary data if needed.
       
   106     CancelOperation();
       
   107     
       
   108     IAUPDATE_TRACE("[IAUPDATE] CIAUpdatePurchaseOperation::~CIAUpdatePurchaseOperation end");    
       
   109     }
       
   110 
       
   111 
       
   112 // ---------------------------------------------------------------------------
       
   113 // CIAUpdatePurchaseOperation::PurchaseProgress
       
   114 // Content purchase functions
       
   115 // ---------------------------------------------------------------------------
       
   116 //
       
   117 void CIAUpdatePurchaseOperation::PurchaseProgress( 
       
   118     MNcdPurchaseOperation& /*aOperation*/, 
       
   119     TNcdProgress /*aProgress*/ )
       
   120     {
       
   121     // Purchase progress is not observed.
       
   122     }
       
   123 
       
   124 
       
   125 // ---------------------------------------------------------------------------
       
   126 // CIAUpdatePurchaseOperation::QueryReceived
       
   127 // 
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 void CIAUpdatePurchaseOperation::QueryReceived( 
       
   131     MNcdPurchaseOperation& aOperation, 
       
   132     MNcdQuery* aQuery )
       
   133     {
       
   134     IAUPDATE_TRACE("[IAUPDATE] CIAUpdatePurchaseOperation::QueryReceived() begin");
       
   135 
       
   136     ContentQueryReceived( aOperation, aQuery );
       
   137 
       
   138     IAUPDATE_TRACE("[IAUPDATE] CIAUpdatePurchaseOperation::QueryReceived() end");
       
   139     }
       
   140 
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // CIAUpdatePurchaseOperation::OperationComplete
       
   144 // 
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 void CIAUpdatePurchaseOperation::OperationComplete( 
       
   148     MNcdPurchaseOperation& aOperation, 
       
   149     TInt aError )
       
   150     {
       
   151     IAUPDATE_TRACE_1("[IAUPDATE] CIAUpdatePurchaseOperation::OperationComplete() begin: %d",
       
   152                      aError);
       
   153 
       
   154     if ( aError != KErrNone )
       
   155         {
       
   156         IAUPDATE_TRACE("[IAUPDATE] Purchase error. Update purchase history.");
       
   157         // In a purchase operation error case, NCD Engine does not update the
       
   158         // purchase history information. To make sure that purchase history 
       
   159         // contains the error code for the operation, insert it here.
       
   160         TRAP_IGNORE( Node().SetIdleErrorToPurchaseHistoryL( aError, EFalse ) );
       
   161         }
       
   162 
       
   163     ContentOperationComplete( aOperation, aError );
       
   164     
       
   165     IAUPDATE_TRACE("[IAUPDATE] CIAUpdatePurchaseOperation::OperationComplete() end");
       
   166     }
       
   167         
       
   168 
       
   169 // ---------------------------------------------------------------------------
       
   170 // CIAUpdatePurchaseOperation::HandleContentL
       
   171 // 
       
   172 // ---------------------------------------------------------------------------
       
   173 //
       
   174 MNcdOperation* CIAUpdatePurchaseOperation::HandleContentL()
       
   175     {
       
   176     IAUPDATE_TRACE("[IAUPDATE] CIAUpdatePurchaseOperation::HandleContentL begin");
       
   177 
       
   178     MNcdOperation* operation( NULL );
       
   179 
       
   180     // If node is already installed, then do not purchase it again.
       
   181     // Notice, that casting is safe here, because NewLC function uses
       
   182     // CIAUpdateNode which is given to the constructor and for parent class. 
       
   183     // So, the node is CIAUpdateNode even if the parent operation class uses
       
   184     // pointer to node's parent. 
       
   185     CIAUpdateNode& node( static_cast< CIAUpdateNode& >( Node() ) );
       
   186     if ( !node.IsInstalled() )
       
   187         {
       
   188         IAUPDATE_TRACE("[IAUPDATE] Create purchase operation.");
       
   189         operation = PurchaseL();
       
   190         }
       
   191 
       
   192     return operation;
       
   193     }
       
   194 
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // CIAUpdatePurchaseOperation::PurchaseL
       
   198 // 
       
   199 // ---------------------------------------------------------------------------
       
   200 //
       
   201 MNcdOperation* CIAUpdatePurchaseOperation::PurchaseL()
       
   202     {
       
   203     IAUPDATE_TRACE("[IAUPDATE] CIAUpdatePurchaseOperation::PurchaseL() begin");
       
   204 
       
   205     MNcdOperation* operation( NULL );
       
   206     
       
   207     MNcdNodePurchase* purchase( 
       
   208         Node().Node().QueryInterfaceLC< MNcdNodePurchase >() );
       
   209     
       
   210     if ( purchase )
       
   211         {
       
   212         // Purchase interface exists. So, we can do purchasing.
       
   213         if ( !purchase->IsPurchased() )
       
   214             {
       
   215             IAUPDATE_TRACE("[IAUPDATE] Item was not purchased yet");
       
   216 
       
   217             // Only do purchase operation if we have not already done purchasing
       
   218             // and the purchase option is usable and not obsolete.
       
   219             RCatalogsArray< MNcdPurchaseOption > options( 
       
   220                 purchase->PurchaseOptionsL() );
       
   221             CleanupResetAndDestroyPushL( options );
       
   222             
       
   223             // Notice, IsUsableL() will leave with KNcdErrorObsolete if the option
       
   224             // is obsolete. Let it leave then, because we could not purchase then
       
   225             // anyways.
       
   226             if( options.Count() == 1 
       
   227                 && options[ 0 ]->IsFree() 
       
   228                 && options[ 0 ]->IsUsableL() )
       
   229                 {
       
   230                 IAUPDATE_TRACE("[IAUPDATE] Purchase option was correct.");
       
   231 
       
   232                 operation = purchase->PurchaseL( *options[ 0 ], *this );
       
   233                 
       
   234                 if( !operation )
       
   235                     {
       
   236                     // Could not create the operation for some reason.
       
   237                     IAUPDATE_TRACE("[IAUPDATE] ERROR Could not create the operation.");
       
   238                     User::Leave( KErrGeneral );
       
   239                     }
       
   240                 }
       
   241             else
       
   242                 {
       
   243                 // Wrong kind of purchase information gotten from the server.
       
   244                 IAUPDATE_TRACE("[IAUPDATE] ERROR Wrong purchase info from server.");
       
   245                 User::Leave( KErrGeneral );
       
   246                 }
       
   247             CleanupStack::PopAndDestroy( &options );
       
   248             }
       
   249         CleanupStack::PopAndDestroy( purchase );
       
   250         }
       
   251     else
       
   252         {
       
   253         IAUPDATE_TRACE("[IAUPDATE] Purchase interface was not found.");
       
   254         User::Leave( KErrNotFound );
       
   255         }
       
   256 
       
   257     IAUPDATE_TRACE("[IAUPDATE] CIAUpdatePurchaseOperation::PurchaseL() end");
       
   258 
       
   259     return operation;
       
   260     }
       
   261