ncdengine/provider/client/src/ncdpurchaseoptionproxy.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     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 <e32base.h>
       
    20 
       
    21 #include "ncdpurchaseoptionproxy.h"
       
    22 #include "ncdnodemetadataproxy.h"
       
    23 #include "catalogsinterfaceidentifier.h"
       
    24 #include "ncdclientsubscription.h"
       
    25 #include "ncdclientpartofsubscription.h"
       
    26 #include "ncdclientupgrade.h"
       
    27 #include "catalogsclientserver.h"
       
    28 #include "ncdnodefunctionids.h"
       
    29 #include "catalogsutils.h"
       
    30 #include "catalogsdebug.h"
       
    31 #include "ncdsubscriptionmanagerproxy.h"
       
    32 #include "ncdnodeidentifier.h"
       
    33 #include "ncdnodemanagerproxy.h"
       
    34 #include "ncdnodepurchaseproxy.h"
       
    35 #include "ncdnodeproxy.h"
       
    36 #include "ncdsubscriptionproxy.h"
       
    37 #include "ncderrors.h"
       
    38 
       
    39 
       
    40 
       
    41 // ======== MEMBER FUNCTIONS ========
       
    42 
       
    43 
       
    44 CNcdPurchaseOptionProxy::CNcdPurchaseOptionProxy(
       
    45     MCatalogsClientServer& aSession, 
       
    46     TInt aHandle,
       
    47     CNcdNodePurchaseProxy& aParentNodePurchase ) :
       
    48     CNcdInterfaceBaseProxy( aSession, aHandle, &aParentNodePurchase ),
       
    49     iParentNodePurchase( &aParentNodePurchase ),
       
    50     iIsFree( EFalse ),
       
    51     iObsolete( EFalse )
       
    52     {
       
    53     }
       
    54 
       
    55 void CNcdPurchaseOptionProxy::ConstructL()
       
    56     {
       
    57     DLTRACEIN(("this-ptr: %x", this));
       
    58     // Purchaseoption interfaces are not registered
       
    59     // to the parent as there can be several purchaseoptions
       
    60     }
       
    61 
       
    62 
       
    63 CNcdPurchaseOptionProxy* CNcdPurchaseOptionProxy::NewL( 
       
    64     MCatalogsClientServer& aSession,
       
    65     TInt aHandle,
       
    66     CNcdNodePurchaseProxy& aParentNodePurchase )
       
    67     {
       
    68     CNcdPurchaseOptionProxy* self = 
       
    69         CNcdPurchaseOptionProxy::NewLC( aSession,
       
    70                                         aHandle,
       
    71                                         aParentNodePurchase );
       
    72     CleanupStack::Pop( self );
       
    73     return self;
       
    74     }
       
    75 
       
    76 
       
    77 CNcdPurchaseOptionProxy* CNcdPurchaseOptionProxy::NewLC( 
       
    78     MCatalogsClientServer& aSession,
       
    79     TInt aHandle,
       
    80     CNcdNodePurchaseProxy& aParentNodePurchase )
       
    81     {
       
    82     CNcdPurchaseOptionProxy* self =
       
    83         new( ELeave ) CNcdPurchaseOptionProxy( aSession,
       
    84                                                aHandle,
       
    85                                                aParentNodePurchase );
       
    86     // Using PushL because the object does not have any references yet
       
    87     CleanupStack::PushL( self );
       
    88     self->ConstructL();
       
    89     return self;
       
    90     }
       
    91 
       
    92 
       
    93 CNcdPurchaseOptionProxy::~CNcdPurchaseOptionProxy()
       
    94     {
       
    95     DLTRACEIN(("this-ptr: %x", this));
       
    96     delete iSubscriptionComponent;
       
    97     delete iPartOfSubscriptionComponent;
       
    98     delete iUpgradeComponent;
       
    99     
       
   100     delete iName;
       
   101     delete iPriceText;
       
   102     
       
   103     delete iPriceCurrency;
       
   104     
       
   105     delete iPurchaseOptionId;
       
   106     DLTRACEOUT(("this-ptr: %x", this));
       
   107     }
       
   108     
       
   109 void CNcdPurchaseOptionProxy::InternalizeL()
       
   110     {
       
   111     DLTRACEIN(("this-ptr: %x", this));
       
   112 
       
   113     HBufC8* data( NULL );
       
   114         
       
   115     // Because we do not know the exact size of the data id, use
       
   116     // the alloc method, which creates the buffer of the right size
       
   117     // and sets the pointer to point to the created buffer.
       
   118     User::LeaveIfError(
       
   119             ClientServerSession().
       
   120                 SendSyncAlloc( NcdNodeFunctionIds::ENcdInternalize,
       
   121                                KNullDesC8,
       
   122                                data,
       
   123                                Handle(),
       
   124                                0 ) );
       
   125 
       
   126     if ( data == NULL )
       
   127         {
       
   128         User::Leave( KErrNotFound );
       
   129         }
       
   130 
       
   131     CleanupStack::PushL( data );
       
   132 
       
   133 
       
   134     // Read the data from the stream
       
   135     RDesReadStream stream( *data );
       
   136     CleanupClosePushL( stream );
       
   137     
       
   138     InternalizeDataL( stream );
       
   139 
       
   140     // Closes the stream
       
   141     CleanupStack::PopAndDestroy( &stream ); 
       
   142     CleanupStack::PopAndDestroy( data );
       
   143     
       
   144     DLTRACEOUT((""));    
       
   145     }
       
   146 
       
   147 const TDesC& CNcdPurchaseOptionProxy::Id() const
       
   148     {
       
   149     return *iPurchaseOptionId;
       
   150     }
       
   151 
       
   152 CNcdNodePurchaseProxy* CNcdPurchaseOptionProxy::ParentNodePurchase() const
       
   153     {
       
   154     return iParentNodePurchase;
       
   155     }
       
   156 
       
   157 const CNcdClientPartOfSubscription*
       
   158     CNcdPurchaseOptionProxy::PartOfSubscription() const
       
   159     {
       
   160     return iPartOfSubscriptionComponent;
       
   161     }
       
   162 
       
   163 void CNcdPurchaseOptionProxy::SetObsolete()
       
   164     {
       
   165     DLTRACEIN((""));
       
   166     iObsolete = ETrue;
       
   167     iParentNodePurchase = NULL;
       
   168     DLTRACEOUT(( _L("PO name: %S now set as obsolete"),iName ));   
       
   169     }
       
   170 
       
   171 const TDesC& CNcdPurchaseOptionProxy::Name() const
       
   172     {
       
   173     return *iName;
       
   174     }
       
   175 
       
   176 const TDesC& CNcdPurchaseOptionProxy::Price() const
       
   177     {
       
   178     return *iPriceText;
       
   179     }
       
   180 
       
   181 TBool CNcdPurchaseOptionProxy::IsFree() const
       
   182     {
       
   183     return iIsFree;
       
   184     }
       
   185 
       
   186 MNcdPurchaseOption::TType CNcdPurchaseOptionProxy::PurchaseOptionType() const
       
   187     {
       
   188     return iType;
       
   189     }
       
   190 
       
   191 TBool CNcdPurchaseOptionProxy::IsObsolete() const
       
   192     {
       
   193     DLTRACEIN((""));
       
   194     DLTRACEOUT(( _L("PO name: %S IsObsolete: %d"),iName, iObsolete ));
       
   195     return iObsolete;
       
   196     }
       
   197 
       
   198 TBool CNcdPurchaseOptionProxy::IsUsableL() const
       
   199     {
       
   200     DLTRACEIN((""));
       
   201     
       
   202     // If purchase option is not valid anymore it should not be used.
       
   203     // This is because we cannot be sure that the parent node for example
       
   204     // still exists.
       
   205     if ( IsObsolete() )
       
   206         {
       
   207         User::Leave( KNcdErrorObsolete );
       
   208         }
       
   209     
       
   210     if ( iType == MNcdPurchaseOption::ESubscriptionPurchase )
       
   211         {
       
   212         // If parent subscription is not bought, return false.
       
   213         // (Parent subscription cannot be used.)
       
   214 
       
   215         DASSERT( iPartOfSubscriptionComponent != NULL );
       
   216                 
       
   217         const TDesC& parentSubscriptionEntityId =
       
   218             iPartOfSubscriptionComponent->ParentEntityId();
       
   219         const TDesC& parentSubscriptionPoId =
       
   220             iPartOfSubscriptionComponent->ParentPurchaseOptionId();
       
   221         
       
   222         // NOTICE: Namespace is assumed to be the same as of
       
   223         //         the metadata that owns this purchase option.
       
   224             
       
   225         TBool isBought = 
       
   226             ParentNodePurchase()->Metadata().Node().NodeManager().
       
   227             SubscriptionManager().
       
   228             ActiveSubscriptionExists( 
       
   229                 parentSubscriptionEntityId,
       
   230                 ParentNodePurchase()->Metadata().Namespace(),
       
   231                 parentSubscriptionPoId );
       
   232          
       
   233         if ( !isBought )
       
   234             {
       
   235             DLTRACEOUT(("False"));
       
   236             return EFalse;
       
   237             }
       
   238         }
       
   239     else if ( iType == MNcdPurchaseOption::ESubscriptionUpgrade )
       
   240         {
       
   241         // If target subscription is not bought, return false
       
   242         // If the subscription is not bought, it cannot be upgraded.)
       
   243         
       
   244         DASSERT( iUpgradeComponent != NULL );
       
   245         
       
   246         const TDesC& targetPurchaseOptionId =
       
   247             iUpgradeComponent->DependencyId();
       
   248 
       
   249         // Subscription that DependencyId references is from the
       
   250         // same dataentity as this purchaseoption so use other
       
   251         // info than purchaseoption id from the metadata that
       
   252         // owns this purchaseoption
       
   253         TBool isBought = 
       
   254         ParentNodePurchase()->Metadata().Node().NodeManager().
       
   255         SubscriptionManager().ActiveSubscriptionExists(
       
   256                  ParentNodePurchase()->Metadata().Id(),
       
   257                  ParentNodePurchase()->Metadata().Namespace(),
       
   258                  targetPurchaseOptionId );        
       
   259         
       
   260         if ( !isBought )
       
   261             {
       
   262             DLTRACEOUT(("False"));
       
   263             return EFalse;
       
   264             }
       
   265         }
       
   266     DLTRACEOUT(("True"));
       
   267     return ETrue;
       
   268     }
       
   269 
       
   270 MNcdSubscription* CNcdPurchaseOptionProxy::ParentSubscriptionL() const
       
   271     {
       
   272     DLTRACEIN((""));
       
   273 
       
   274     // If purchase option is not valid anymore it should not be used.
       
   275     // This is because we cannot be sure that the parent node for example
       
   276     // still exists.
       
   277     if ( IsObsolete() )
       
   278         {
       
   279         User::Leave( KNcdErrorObsolete );
       
   280         }
       
   281     // Also if we are not handling a purchase option of type
       
   282     // MNcdPurchaseOption::ESubscriptionPurchase this function should
       
   283     // not be used.
       
   284     if ( iType != MNcdPurchaseOption::ESubscriptionPurchase ||
       
   285          iPartOfSubscriptionComponent == NULL )
       
   286         {
       
   287         // Should change to leave with own error code
       
   288         DLERROR(("PO not part of subscription or part of subscription component not found."));
       
   289         User::Leave( KNcdErrorPurchaseOptionNotValidSubscriptionPurchase );
       
   290         }
       
   291 
       
   292 
       
   293     DASSERT( iPartOfSubscriptionComponent != NULL );
       
   294             
       
   295     const TDesC& parentSubscriptionEntityId =
       
   296         iPartOfSubscriptionComponent->ParentEntityId();
       
   297     const TDesC& parentSubscriptionPoId =
       
   298         iPartOfSubscriptionComponent->ParentPurchaseOptionId();
       
   299     
       
   300     // NOTICE: Namespace is assumed to be the same as of
       
   301     //         the metadata that owns this purchase option.
       
   302 
       
   303     CNcdSubscriptionProxy* searchedSubscription( NULL );        
       
   304     searchedSubscription =
       
   305         ParentNodePurchase()->Metadata().Node().NodeManager().
       
   306         SubscriptionManager().
       
   307         Subscription( parentSubscriptionEntityId,
       
   308                       ParentNodePurchase()->Metadata().Namespace(),
       
   309                       parentSubscriptionPoId );
       
   310 
       
   311     // Increase also the reference counter by one here.
       
   312     // So, the root ref count is at least one when the user
       
   313     // gets it.
       
   314     if ( searchedSubscription != NULL )
       
   315         {
       
   316         searchedSubscription->AddRef();
       
   317         }
       
   318         
       
   319     DLTRACEOUT((""));
       
   320     return searchedSubscription;
       
   321     }
       
   322 
       
   323 MNcdNode* CNcdPurchaseOptionProxy::ParentSubscriptionNodeL() const
       
   324     {
       
   325     DLTRACEIN((""));
       
   326 
       
   327     // If purchase option is not valid anymore it should not be used.
       
   328     // This is because we cannot be sure that the parent node for example
       
   329     // still exists.
       
   330     if ( IsObsolete() )
       
   331         {
       
   332         DLERROR(("Using obsolete purchaseoption"));
       
   333         User::Leave( KNcdErrorObsolete );
       
   334         }
       
   335     
       
   336     if ( iPartOfSubscriptionComponent == NULL )
       
   337         {
       
   338         DLERROR(("Asking parent subscription node from non part of subscription"));
       
   339         DLTRACEOUT((""));
       
   340         User::Leave( KNcdErrorPurchaseOptionNotValidSubscriptionPurchase );
       
   341         }
       
   342 
       
   343 
       
   344     const TDesC& parentSubscriptionEntityId =
       
   345         iPartOfSubscriptionComponent->ParentEntityId();
       
   346 
       
   347     CNcdNodeIdentifier& subscriptionOriginIdentifier =
       
   348         ParentNodePurchase()->Metadata().Node().NodeIdentifier();
       
   349 
       
   350 
       
   351     // Dataentity of the parent subscription is assumed to
       
   352     // be in the same namespace as the node that owns this
       
   353     // purchaseoption
       
   354  
       
   355     CNcdNodeIdentifier* subscriptionNodeIdentifier =
       
   356         CNcdNodeIdentifier::NewLC(
       
   357             subscriptionOriginIdentifier.NodeNameSpace(),
       
   358             parentSubscriptionEntityId,
       
   359             subscriptionOriginIdentifier.ServerUri(),
       
   360             subscriptionOriginIdentifier.ClientUid() );
       
   361 
       
   362 
       
   363     // Node manager will handle the creation of the node.
       
   364     CNcdNodeManagerProxy& nodeManager(
       
   365         ParentNodePurchase()->Metadata().Node().NodeManager() );
       
   366     
       
   367     // Notice that this function takes the metadataidentifier as a parameter.    
       
   368     MNcdNode* subscriptionNode = 
       
   369         &nodeManager.CreateTemporaryOrSupplierNodeL( *subscriptionNodeIdentifier );
       
   370 
       
   371     CleanupStack::PopAndDestroy( subscriptionNodeIdentifier );
       
   372     
       
   373     // Increase also the reference counter by one here.
       
   374     // So, the root ref count is at least one when the user
       
   375     // gets it.
       
   376     if ( subscriptionNode != NULL )
       
   377         {
       
   378         subscriptionNode->AddRef();
       
   379         }
       
   380     
       
   381     DLTRACEOUT((""));
       
   382     
       
   383     return subscriptionNode;
       
   384     }
       
   385 
       
   386      
       
   387 const TDesC&
       
   388     CNcdPurchaseOptionProxy::ParentSubscriptionPurchaseOptionIdL() const
       
   389     {
       
   390     DLTRACEIN((""));
       
   391     
       
   392     if ( iPartOfSubscriptionComponent == NULL )
       
   393         {
       
   394         User::Leave( KNcdErrorPurchaseOptionNotValidSubscriptionPurchase );
       
   395         }
       
   396 
       
   397     DLTRACEOUT((""));
       
   398     return iPartOfSubscriptionComponent->ParentPurchaseOptionId();
       
   399     }
       
   400 
       
   401  
       
   402 void CNcdPurchaseOptionProxy::InternalizeDataL( RReadStream& aStream )
       
   403     {
       
   404     DLTRACEIN((""));
       
   405 
       
   406     // First read the class id. Because, it is the first thing in the stream.
       
   407     // Actually we do not need that information here.
       
   408     aStream.ReadInt32L();
       
   409 
       
   410     InternalizeDesL( iName, aStream );
       
   411     
       
   412     DLTRACE(( _L("Purchaseoption-proxy, name: %S"), iName ));
       
   413     
       
   414     InternalizeDesL( iPriceText, aStream );
       
   415     
       
   416     DLTRACE(( _L("Purchaseoption-proxy, price: %S"), iPriceText ));
       
   417 
       
   418     iPrice = aStream.ReadReal32L();
       
   419     InternalizeDesL( iPriceCurrency, aStream );
       
   420     
       
   421     iIsFree = aStream.ReadInt32L();
       
   422     
       
   423     DLTRACE(( "Purchaseoption-proxy, isFree: %d", iIsFree ));
       
   424     
       
   425     InternalizeDesL( iPurchaseOptionId, aStream );
       
   426     
       
   427     DLTRACE(( _L("Purchaseoption-proxy, po-id: %S"), iPurchaseOptionId ));
       
   428 
       
   429     iType = static_cast<MNcdPurchaseOption::TType>( aStream.ReadInt32L() );
       
   430     
       
   431     DLTRACE(( _L("Purchaseoption-proxy, type: %d"), iType ));
       
   432     
       
   433     iRequirePurchaseProcess = aStream.ReadInt32L();
       
   434     
       
   435     DLTRACE(( "Purchaseoption-proxy, requirePurchaseProcess: %d",
       
   436               iRequirePurchaseProcess ));
       
   437     
       
   438  
       
   439     DLTRACE(( "Other data not listed yet." ));
       
   440  
       
   441 
       
   442     // Let's internalize aggregate classes
       
   443 
       
   444     TBool subscriptionExists( aStream.ReadInt32L() );
       
   445     if ( subscriptionExists )
       
   446         {
       
   447         delete iSubscriptionComponent;
       
   448         iSubscriptionComponent = NULL;
       
   449         iSubscriptionComponent = CNcdClientSubscription::NewL();
       
   450         
       
   451         iSubscriptionComponent->SetValidityDelta( aStream.ReadInt32L() );
       
   452         iSubscriptionComponent->SetValidityAutoUpdate( aStream.ReadInt32L() );
       
   453         
       
   454         iSubscriptionComponent->SetAmountOfCredits( aStream.ReadReal32L() );
       
   455         
       
   456         HBufC* tmpAmountOfCreditsCurrency( NULL );
       
   457         InternalizeDesL( tmpAmountOfCreditsCurrency, aStream );
       
   458         iSubscriptionComponent->SetAmountOfCreditsCurrency(
       
   459             tmpAmountOfCreditsCurrency );
       
   460                 
       
   461         iSubscriptionComponent->SetNumberOfDownloads( aStream.ReadInt32L() );
       
   462 
       
   463         }
       
   464     
       
   465     TBool partOfSubscriptionExists( aStream.ReadInt32L() );
       
   466     if ( partOfSubscriptionExists )
       
   467         {
       
   468 
       
   469         delete iPartOfSubscriptionComponent;
       
   470         iPartOfSubscriptionComponent = 0;
       
   471         iPartOfSubscriptionComponent = CNcdClientPartOfSubscription::NewL();
       
   472         
       
   473         HBufC* tmpParentEntityId( NULL );
       
   474         InternalizeDesL( tmpParentEntityId, aStream );
       
   475         iPartOfSubscriptionComponent->SetParentEntityId( tmpParentEntityId );
       
   476         
       
   477         HBufC* tmpParentPurchaseOptionId( NULL );
       
   478         InternalizeDesL( tmpParentPurchaseOptionId, aStream );
       
   479         iPartOfSubscriptionComponent->SetParentPurchaseOptionId(
       
   480             tmpParentPurchaseOptionId );
       
   481             
       
   482         iPartOfSubscriptionComponent->SetCreditPrice(
       
   483             aStream.ReadReal32L() );
       
   484 
       
   485         }
       
   486         
       
   487     TBool upgradeExists( aStream.ReadInt32L() );
       
   488     if ( upgradeExists )
       
   489         {
       
   490 
       
   491         delete iUpgradeComponent;
       
   492         iUpgradeComponent = NULL;
       
   493         iUpgradeComponent = CNcdClientUpgrade::NewL();
       
   494 
       
   495         HBufC* tmpDependencyId( NULL );
       
   496         InternalizeDesL( tmpDependencyId, aStream );
       
   497         iUpgradeComponent->SetDependencyId( tmpDependencyId );
       
   498 
       
   499         iUpgradeComponent->SetValidityDelta( aStream.ReadInt32L() );
       
   500         iUpgradeComponent->SetAmountOfCredits( aStream.ReadReal32L() );
       
   501         iUpgradeComponent->SetNumberOfDownloads( aStream.ReadInt32L() );
       
   502 
       
   503         }
       
   504 
       
   505     DLTRACEOUT((""));    
       
   506     }