ncdengine/provider/server/src/ncdsubscriptionimpl.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:   Implements CNcdSubscription class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "ncdsubscriptionimpl.h"
       
    20 
       
    21 #include "ncdpurchaseoptionimpl.h"
       
    22 #include "ncdserversubscription.h"
       
    23 #include "ncdserverpartofsubscription.h"
       
    24 #include "ncdserverupgrade.h"
       
    25 #include "ncd_pp_subscriptiondetails.h"
       
    26 #include "catalogssession.h"
       
    27 #include "catalogsbasemessage.h"
       
    28 #include "ncdnodefunctionids.h"
       
    29 #include "ncdnodeclassids.h"
       
    30 #include "catalogsconstants.h"
       
    31 #include "ncd_pp_dataentity.h"
       
    32 #include "ncd_cp_query.h"
       
    33 #include "ncd_pp_subscription.h"
       
    34 #include "catalogsutils.h"
       
    35 #include "ncdserversubscribablecontent.h"
       
    36 #include "ncdsubscriptiongroup.h"
       
    37 #include "ncdprotocolutils.h"
       
    38 
       
    39 #include "catalogsdebug.h"
       
    40 
       
    41 
       
    42 CNcdSubscription::CNcdSubscription( CNcdSubscriptionGroup& aParentGroup )
       
    43     : CCatalogsCommunicable(),
       
    44       iParentGroup( aParentGroup )
       
    45     {
       
    46     }
       
    47 
       
    48 void CNcdSubscription::ConstructL()
       
    49     {
       
    50     }
       
    51 
       
    52 
       
    53 CNcdSubscription* CNcdSubscription::NewL(
       
    54     CNcdSubscriptionGroup& aParentGroup )
       
    55     {
       
    56     CNcdSubscription* self =   
       
    57         CNcdSubscription::NewLC( aParentGroup );
       
    58     CleanupStack::Pop( self );
       
    59     return self;        
       
    60     }
       
    61 
       
    62 CNcdSubscription* CNcdSubscription::NewLC(
       
    63     CNcdSubscriptionGroup& aParentGroup )
       
    64     {
       
    65     CNcdSubscription* self = 
       
    66         new( ELeave ) CNcdSubscription( aParentGroup );
       
    67     CleanupClosePushL( *self );
       
    68     self->ConstructL();
       
    69     return self;        
       
    70     }
       
    71 
       
    72 CNcdSubscription::~CNcdSubscription()
       
    73     {
       
    74     DLTRACEIN((""));
       
    75 
       
    76     ResetMemberVariables();
       
    77 
       
    78     DLTRACEOUT((""));
       
    79     }        
       
    80 
       
    81 const TDesC& CNcdSubscription::SubscriptionName() const
       
    82     {
       
    83     DLTRACEIN((""));
       
    84     if ( iName == NULL )
       
    85         {
       
    86         DLTRACEOUT((""));
       
    87         return KNullDesC;
       
    88         }
       
    89     else
       
    90         {
       
    91         DLTRACEOUT((""));
       
    92         return *iName;
       
    93         }      
       
    94     }
       
    95 
       
    96 void CNcdSubscription::SetSubscriptionNameL( const TDesC& aNewName )
       
    97     {
       
    98     delete iName;
       
    99     iName = NULL;
       
   100     iName = aNewName.AllocL();
       
   101     }
       
   102 
       
   103 const TDesC& CNcdSubscription::PurchaseOptionId() const
       
   104     {
       
   105     DLTRACEIN((""));
       
   106     if ( iPurchaseOptionId == NULL )
       
   107         {
       
   108         DLTRACEOUT((""));
       
   109         return KNullDesC;
       
   110         }
       
   111     else
       
   112         {
       
   113         DLTRACEOUT((""));
       
   114         return *iPurchaseOptionId;
       
   115         }    
       
   116     }
       
   117 
       
   118     
       
   119 CNcdSubscriptionGroup& CNcdSubscription::ParentGroup() const 
       
   120     {
       
   121     DLTRACEIN((""));
       
   122     return iParentGroup;
       
   123     }
       
   124     
       
   125 MNcdSubscription::TType CNcdSubscription::SubscriptionType() const 
       
   126     {
       
   127     DLTRACEIN((""));
       
   128     return iSubscriptionType;
       
   129     }
       
   130 
       
   131 void CNcdSubscription::InternalizeL( const CNcdPurchaseOptionImpl& aData )
       
   132     {
       
   133     DLTRACEIN(("Subscriptionimpl internalizing from purchase option."));
       
   134     
       
   135     // Current time saved in the beginning for later use.
       
   136     // Done as soon as possible so it won't be too long
       
   137     // after message is received from the server and delta
       
   138     // values are hopefully still valid.
       
   139     TTime now;
       
   140     now.HomeTime();
       
   141 
       
   142     
       
   143     ResetMemberVariables();
       
   144     
       
   145     iName = aData.PurchaseOptionName().AllocL();
       
   146     DLTRACE(( _L("Name: %S."), iName ));
       
   147 
       
   148     // Cannot get expired on info from the purchaseoption and
       
   149     // if this subscription is just bought as it should be, it
       
   150     // has not expired.
       
   151     // Expired on is left into its initial value.
       
   152     
       
   153     // The same thing is with iCancelled. It is assumed that as the
       
   154     // subscription is just bought, it is not unsubscribed yet.
       
   155 
       
   156 
       
   157     // Get subscription type from the subscribable content
       
   158     const CNcdServerSubscribableContent* subscribableContent =
       
   159         aData.ParentSubscribableContent();
       
   160     
       
   161     // Protocol makes it possible that although we have a node that has
       
   162     // a purchaseoption that can be used to buy a subscription the node
       
   163     // does not need to have a subscribable content field.    
       
   164     if ( subscribableContent != NULL )
       
   165         {
       
   166         iSubscriptionType = subscribableContent->SubscriptionType();
       
   167         DLINFO(( "Subscriptionimpl internalizing, type: %d",
       
   168                  iSubscriptionType ));        
       
   169         }
       
   170     else
       
   171         {
       
   172         // We use the default type of subscription
       
   173         DLINFO(( "Subscribable content not found, have to use default type: %d",
       
   174                  iSubscriptionType ));
       
   175         }
       
   176 
       
   177 
       
   178     // Next we set total values if they are set in the subscription
       
   179 
       
   180     const CNcdServerSubscription* subscriptionInfo =
       
   181         aData.SubscriptionInfo();
       
   182     if ( subscriptionInfo == NULL )
       
   183         {
       
   184         User::Leave( KErrArgument );
       
   185         }
       
   186 
       
   187  
       
   188     iTotalValidityDelta = subscriptionInfo->ValidityDelta();
       
   189     DLINFO(( "Total validitydelta: %d.", iTotalValidityDelta ));
       
   190     if ( iTotalValidityDelta >= 0 )
       
   191         {
       
   192         iValidityTimeSet = ETrue;
       
   193         }
       
   194     DLINFO(( "ValidityTimeSet: %d.", iValidityTimeSet ));
       
   195     
       
   196     iTotalCredits = subscriptionInfo->AmountOfCredits();
       
   197     DLINFO(( "Total credits: %f.", iTotalCredits ));
       
   198     // Simple >0 is decided to be enough because no complicated operations
       
   199     // are done with TReal numbers
       
   200     if ( iTotalCredits >= 0 )
       
   201         {
       
   202         iCreditLimitSet = ETrue;
       
   203         }
       
   204     
       
   205     iTotalDownloads = subscriptionInfo->NumberOfDownloads();
       
   206     DLINFO(( "Total downloads: %d.", iTotalDownloads ));
       
   207     if ( iTotalDownloads >= 0 )
       
   208         {
       
   209         iDownloadLimitSet = ETrue;
       
   210         }
       
   211     
       
   212     
       
   213     // Because this function should be used just after purchase
       
   214     // of the subscription, we set maximum values to the
       
   215     // "left" variables
       
   216 
       
   217     // validityDelta is in minutes so we use TTimeIntervalMinutes
       
   218     // to reprsent the interval
       
   219     TTimeIntervalMinutes validityInterval( iTotalValidityDelta );
       
   220     iValidUntil = now + validityInterval;
       
   221  
       
   222     iCreditsLeft = iTotalCredits;
       
   223     iDownloadsLeft = iTotalDownloads;
       
   224 
       
   225     iPurchaseOptionId = aData.Id().AllocL();
       
   226     
       
   227     DLTRACEOUT((""));
       
   228     }
       
   229 
       
   230 
       
   231 
       
   232 
       
   233 void CNcdSubscription::InternalizeL(
       
   234     MNcdPreminetProtocolSubscription& aData )
       
   235     {
       
   236     DLTRACEIN(("Subscriptionimpl internalizing from protocol."));
       
   237     
       
   238     // Current time saved in the beginning for later use.
       
   239     // Done as soon as possible so it won't be too long
       
   240     // after message is received from the server and delta
       
   241     // values are hopefully still valid.
       
   242     TTime now;
       
   243     now.HomeTime();
       
   244 
       
   245 
       
   246     // ResetMemberVariables function is not called here
       
   247     // because it deletes all member variables and
       
   248     // we cannot recreate all of them from the given protocol object.
       
   249     
       
   250     // Instead ResetProtocolMemberVariables is called
       
   251     ResetProtocolMemberVariables();
       
   252     
       
   253     
       
   254     // Nothing is done to existing name and icon
       
   255     // info because hopefully they are received some other way
       
   256     // (from purchase option)
       
   257 
       
   258     
       
   259     iExpiredOn = aData.ExpiredOn().AllocL();
       
   260     
       
   261     iCancelled = aData.Cancelled();
       
   262     
       
   263     TNcdSubscriptionType tmpType = aData.Type();
       
   264     DASSERT( tmpType != ENotSubscribable );
       
   265     
       
   266     switch( tmpType )
       
   267         {
       
   268         case EPeriodic:
       
   269             iSubscriptionType = MNcdSubscription::EPeriodic;
       
   270             break;
       
   271 
       
   272         case EAutomaticContinous:
       
   273             iSubscriptionType = MNcdSubscription::EAutomaticContinous;
       
   274             break;
       
   275 
       
   276         default:
       
   277             DASSERT( false ); // Should not end up here
       
   278             break;
       
   279         }
       
   280 
       
   281     
       
   282     // If we have a expired subscription TotalUsageRights is
       
   283     // assumed to return usagerights of the expired subscription
       
   284     const MNcdPreminetProtocolSubscriptionDetails* totalRights =
       
   285         aData.TotalUsageRights();
       
   286    
       
   287     DASSERT( totalRights != NULL );
       
   288         
       
   289     if ( totalRights != NULL )
       
   290         {
       
   291         // We assume here that if any of the original limits or
       
   292         // validity negative then that particular limit or validity
       
   293         // is not in use.
       
   294         
       
   295         iTotalValidityDelta = totalRights->ValidityDelta();
       
   296         if ( iTotalValidityDelta >= 0 )
       
   297             {
       
   298             iValidityTimeSet = ETrue;
       
   299             }
       
   300                     
       
   301 
       
   302         iTotalCredits = totalRights->AmountOfCredits();
       
   303         // Also here >0 is deemed to be enough because no complicated
       
   304         // operations are done on the TReal numbers
       
   305         if ( iTotalCredits >= 0 )
       
   306             {
       
   307             iCreditLimitSet = ETrue;
       
   308             }
       
   309 
       
   310         iTotalDownloads = totalRights->NumberOfDownloads();
       
   311         if ( iTotalDownloads >= 0 )
       
   312             {
       
   313             iDownloadLimitSet = ETrue;
       
   314             }
       
   315         }
       
   316 
       
   317 
       
   318     const MNcdPreminetProtocolSubscriptionDetails* remainingRights =
       
   319         aData.RemainingUsageRights();
       
   320 
       
   321     TInt validityDelta( -1 );
       
   322     if ( remainingRights != NULL )
       
   323         {
       
   324         validityDelta = remainingRights->ValidityDelta();
       
   325             
       
   326         // validityDelta is in minutes so we use TTimeIntervalMinutes
       
   327         // to reprsent the interval
       
   328         TTimeIntervalMinutes validityInterval( validityDelta );        
       
   329         iValidUntil = now + validityInterval;
       
   330 
       
   331         iCreditsLeft = remainingRights->AmountOfCredits();
       
   332         iDownloadsLeft = remainingRights->NumberOfDownloads();
       
   333         }
       
   334 
       
   335 
       
   336     // expiredOn is set to iValidUntil so that client can receive
       
   337     // some kind of validuntil (from proxyside interface)
       
   338     // even if the subscription has already expired.
       
   339     // This way iValidUntil is not necessiraly the original validUntil.
       
   340     // For example in situation where other constraint has run out first
       
   341     // (for example credits), iValidUntil would be this expiration
       
   342     // point. Anyway it is close enough.
       
   343     // NOTICE: iValidUntil of expired subscription cannot be greater
       
   344     //         than "now" because server does not return
       
   345     //         remaining usagerights of expired subscriptions
       
   346     if ( iValidityTimeSet && validityDelta == -1 && iExpiredOn != NULL &&
       
   347          *iExpiredOn != KNullDesC )
       
   348         {
       
   349         iValidUntil = NcdProtocolUtils::DesToTimeL( *iExpiredOn ); 
       
   350         }
       
   351 
       
   352     iPurchaseOptionId = aData.PurchaseOptionId().AllocL();
       
   353         
       
   354     DLTRACEOUT((""));
       
   355     }
       
   356 
       
   357 
       
   358 void CNcdSubscription::SetRecentlyUpdated( TBool aNewState )
       
   359     {
       
   360     DLTRACEIN((""));
       
   361     iRecentlyUpdated = aNewState;
       
   362     DLTRACEOUT((""));
       
   363     }
       
   364    
       
   365 TBool CNcdSubscription::RecentlyUpdated() const
       
   366     {
       
   367     DLTRACEIN((""));
       
   368     DLTRACEOUT((""));
       
   369     return iRecentlyUpdated;
       
   370     }
       
   371 
       
   372 
       
   373 
       
   374 // Internalization from and externalization to the database
       
   375     
       
   376 void CNcdSubscription::ExternalizeL( RWriteStream& aStream )
       
   377     {
       
   378     DLTRACEIN((""));
       
   379 
       
   380     ExternalizeOwnDataL( aStream );
       
   381 
       
   382     DLTRACEOUT((""));
       
   383     }
       
   384 
       
   385 
       
   386 void CNcdSubscription::InternalizeL( RReadStream& aStream )
       
   387     {
       
   388     DLTRACEIN((""));
       
   389 
       
   390     ResetMemberVariables();
       
   391 
       
   392     InternalizeDesL( iName, aStream );
       
   393     InternalizeDesL( iExpiredOn, aStream );
       
   394     
       
   395     iCancelled = aStream.ReadInt32L();
       
   396 
       
   397     iSubscriptionType = 
       
   398         static_cast<MNcdSubscription::TType>(aStream.ReadInt32L());
       
   399 
       
   400     iValidityTimeSet = aStream.ReadInt32L();
       
   401     TInt64 intValidUntil( 0 );
       
   402     aStream >> intValidUntil;
       
   403     iValidUntil = intValidUntil;
       
   404     iTotalValidityDelta = aStream.ReadInt32L();
       
   405     
       
   406     iCreditLimitSet = aStream.ReadInt32L();
       
   407     iCreditsLeft = aStream.ReadReal32L();
       
   408     iTotalCredits = aStream.ReadReal32L();
       
   409 
       
   410     iDownloadLimitSet = aStream.ReadInt32L();
       
   411     iDownloadsLeft = aStream.ReadInt32L();
       
   412     iTotalDownloads = aStream.ReadInt32L();
       
   413 
       
   414     InternalizeDesL( iPurchaseOptionId, aStream );
       
   415     
       
   416     DLTRACE(( _L(" Internalized subscription, purchaseoptionid: %S"),
       
   417               iPurchaseOptionId ));
       
   418 
       
   419     DLTRACEOUT((""));
       
   420     }
       
   421 
       
   422 
       
   423 void CNcdSubscription::ReceiveMessage( MCatalogsBaseMessage* aMessage,
       
   424                                   TInt aFunctionNumber )
       
   425     {
       
   426     DLTRACEIN((""));    
       
   427 
       
   428     DASSERT( aMessage );
       
   429     
       
   430     // Now, we can be sure that rest of the time iMessage exists.
       
   431     // This member variable is set for the CounterPartLost function.
       
   432     iMessage = aMessage;
       
   433     
       
   434     TInt trapError( KErrNone );
       
   435     
       
   436     // Check which function is called by the proxy side object.
       
   437     // Function number are located in ncdnodefunctinoids.h file.
       
   438     switch( aFunctionNumber )
       
   439         {
       
   440         case NcdNodeFunctionIds::ENcdInternalize:
       
   441             // Internalize the proxy side according to the data
       
   442             // of this object.
       
   443             TRAP( trapError, InternalizeRequestL( *aMessage ) );
       
   444             break;
       
   445 
       
   446         case NcdNodeFunctionIds::ENcdRelease:
       
   447             // The proxy does not want to use this object anymore.
       
   448             // So, release the handle from the session.
       
   449             ReleaseRequest( *aMessage );
       
   450             break;
       
   451                     
       
   452         default:
       
   453             break;
       
   454         }
       
   455 
       
   456     if ( trapError != KErrNone )
       
   457         {
       
   458         // Because something went wrong, the complete has not been
       
   459         // yet called for the message.
       
   460         // So, inform the client about the error if the
       
   461         // message is still available.
       
   462         aMessage->CompleteAndRelease( trapError );
       
   463         }
       
   464 
       
   465     // Because the message should not be used after this, set it NULL.
       
   466     // So, CounterPartLost function will know that no messages are
       
   467     // waiting the response at the moment.
       
   468     iMessage = NULL;        
       
   469     
       
   470     DLTRACEOUT((""));
       
   471     }
       
   472 
       
   473 void CNcdSubscription::CounterPartLost( const MCatalogsSession& aSession )
       
   474     {
       
   475     // This function may be called whenever -- when the message is waiting
       
   476     // response or when the message does not exist.
       
   477     // iMessage may be NULL here, because in the end of the
       
   478     // ReceiveMessage it is set to NULL. The life time of the message
       
   479     // ends shortly after CompleteAndRelease is called.
       
   480     if ( iMessage != NULL )
       
   481         {
       
   482         iMessage->CounterPartLost( aSession );
       
   483         }
       
   484     }
       
   485                 
       
   486 
       
   487 void CNcdSubscription::InternalizeRequestL( MCatalogsBaseMessage& aMessage )
       
   488     {
       
   489     DLTRACEIN((""));
       
   490     
       
   491     CBufBase* buf = CBufFlat::NewL( KBufExpandSize );
       
   492     CleanupStack::PushL( buf );
       
   493     
       
   494     RBufWriteStream stream( *buf );
       
   495     CleanupClosePushL( stream );
       
   496 
       
   497 
       
   498     // Include all the necessary node data to the stream
       
   499     ExternalizeDataForRequestL( stream );     
       
   500     
       
   501     
       
   502     // Commits data to the stream when closing.
       
   503     CleanupStack::PopAndDestroy( &stream );
       
   504 
       
   505 
       
   506     // If this leaves, ReceiveMessage will complete the message.
       
   507     // NOTE: that here we expect that the buffer contains at least
       
   508     // some data. So, make sure that ExternalizeDataForRequestL inserts
       
   509     // something to the buffer.
       
   510     aMessage.CompleteAndReleaseL( buf->Ptr( 0 ), KErrNone );        
       
   511         
       
   512     
       
   513     DLTRACE(("Deleting the buf"));
       
   514     CleanupStack::PopAndDestroy( buf );
       
   515         
       
   516     DLTRACEOUT((""));
       
   517     }
       
   518     
       
   519 
       
   520 void CNcdSubscription::ExternalizeDataForRequestL( RWriteStream& aStream )
       
   521     {
       
   522     DLTRACEIN((""));
       
   523 
       
   524     ExternalizeOwnDataL( aStream );
       
   525         
       
   526     DLTRACEOUT((""));
       
   527     }
       
   528 
       
   529 void CNcdSubscription::ReleaseRequest( MCatalogsBaseMessage& aMessage ) const
       
   530     {
       
   531     DLTRACEIN((""));
       
   532 
       
   533     // Decrease the reference count for this object.
       
   534     // When the reference count reaches zero, this object will be destroyed
       
   535     // and removed from the session.
       
   536     MCatalogsSession& requestSession( aMessage.Session() );
       
   537     TInt handle( aMessage.Handle() );
       
   538 
       
   539     // Send complete information back to proxy.
       
   540     aMessage.CompleteAndRelease( KErrNone );
       
   541         
       
   542     // Remove this object from the session.
       
   543     requestSession.RemoveObject( handle );
       
   544         
       
   545     DLTRACEOUT((""));
       
   546     }
       
   547 
       
   548 
       
   549 
       
   550 
       
   551 void CNcdSubscription::UseL( const CNcdPurchaseOptionImpl& aData )
       
   552     {
       
   553     DLTRACEIN((""));
       
   554     // If subscription is used, then the purchaseoption
       
   555     // has to have a partOfSubscription component.
       
   556     
       
   557     const CNcdServerPartOfSubscription* partOfSubscriptionInfo =
       
   558         aData.PartOfSubscriptionInfo();
       
   559         
       
   560     if ( partOfSubscriptionInfo == NULL )
       
   561         {
       
   562         User::Leave( KErrArgument );
       
   563         }    
       
   564 
       
   565     if ( aData.IsFree() )
       
   566         {
       
   567         // Free purchase from subscription
       
   568         // Do nothing
       
   569         return;
       
   570         }
       
   571 
       
   572     TReal32 creditPrice( partOfSubscriptionInfo->CreditPrice() );
       
   573     // Only simple floating point operations are done so simple
       
   574     // checking (>0) is also decided to be enough.
       
   575     if ( creditPrice >= 0 )
       
   576         {
       
   577         //DASSERT( iCreditLimitSet );        
       
   578         iCreditsLeft = iCreditsLeft - creditPrice;
       
   579         }
       
   580 
       
   581     if ( iDownloadLimitSet )
       
   582         {
       
   583         --iDownloadsLeft;        
       
   584         }
       
   585 
       
   586     if ( !(creditPrice >= 0) && !iDownloadLimitSet )
       
   587         {
       
   588         // This would be quite weird situation where purchase is not
       
   589         // free but no cost information is provided
       
   590         DLINFO(("Purchase with subscription is not free but it contains no cost information!"));
       
   591         }
       
   592 
       
   593     DLTRACEOUT((""));
       
   594     }
       
   595 
       
   596 
       
   597 
       
   598 void CNcdSubscription::UpgradeL( const CNcdPurchaseOptionImpl& aData )
       
   599     {
       
   600     // If subscription is upgraded, then the purchaseoption should
       
   601     // have upgrade component.
       
   602 
       
   603     const CNcdServerUpgrade* upgradeInfo = aData.UpgradeInfo(); 
       
   604     if ( upgradeInfo == NULL )
       
   605         {
       
   606         User::Leave( KErrArgument );
       
   607         }
       
   608 
       
   609     // All values received in upgrade are just added to the
       
   610     // current values. They have to be added also to total values
       
   611     // so that the info can be used for example when renewing.
       
   612 
       
   613 
       
   614     TInt validityDelta = upgradeInfo->ValidityDelta();
       
   615     // validityDelta is in minutes so we use TTimeIntervalMinutes
       
   616     // to represent the interval
       
   617     if ( validityDelta >= 0 )
       
   618         {
       
   619         TTimeIntervalMinutes validityInterval( validityDelta );        
       
   620         iValidUntil = iValidUntil + validityInterval;
       
   621         iTotalValidityDelta = iTotalValidityDelta + validityDelta;        
       
   622         }
       
   623     else
       
   624         {
       
   625         // This constraint is not in use anymore
       
   626         iValidityTimeSet = EFalse;
       
   627         iValidUntil = -1;
       
   628         iTotalValidityDelta = -1;
       
   629         }
       
   630 
       
   631 
       
   632     TInt creditsLeft( upgradeInfo->AmountOfCredits() );
       
   633     if ( iCreditsLeft >= 0 )
       
   634         {
       
   635         iCreditsLeft = iCreditsLeft + creditsLeft;
       
   636         iTotalCredits = iTotalCredits + creditsLeft;        
       
   637         }
       
   638     else
       
   639         {
       
   640         // This constraint is not in use anymore
       
   641         iCreditLimitSet = EFalse;
       
   642         iCreditsLeft = -1;
       
   643         iTotalCredits = -1;
       
   644         }
       
   645 
       
   646     TInt downloadsLeft( upgradeInfo->NumberOfDownloads() );
       
   647     if ( downloadsLeft >= 0 )
       
   648         {
       
   649         iDownloadsLeft = iDownloadsLeft + downloadsLeft;
       
   650         iTotalDownloads = iTotalDownloads + downloadsLeft;        
       
   651         }
       
   652     else
       
   653         {
       
   654         // This constraint is not in use anymore
       
   655         iDownloadLimitSet = EFalse;
       
   656         iDownloadsLeft = -1;
       
   657         iTotalDownloads = -1;
       
   658         }
       
   659     }
       
   660 
       
   661 void CNcdSubscription::ExternalizeOwnDataL( RWriteStream& aStream )
       
   662     {
       
   663     DLTRACEIN((""));
       
   664 
       
   665     if ( iName != NULL )
       
   666         {
       
   667         ExternalizeDesL( *iName, aStream );
       
   668         DLTRACE(( _L("Externalizing subscription info, name: %S"),
       
   669                   iName ));
       
   670         }
       
   671     else
       
   672         {
       
   673         ExternalizeDesL( KNullDesC, aStream );
       
   674         DLINFO(( "Externalizing subscription info, no name found." ));
       
   675         }
       
   676 
       
   677     if ( iExpiredOn != NULL )
       
   678         {
       
   679         ExternalizeDesL( *iExpiredOn, aStream );
       
   680         DLTRACE(( _L("Externalizing subscription info, expired on: %S"),
       
   681                   iExpiredOn ));
       
   682         }
       
   683     else
       
   684         {
       
   685         ExternalizeDesL( KNullDesC, aStream );
       
   686         DLINFO(( "Externalizing subscription info, no expired on info found." ));
       
   687         }
       
   688 
       
   689     aStream.WriteInt32L( iCancelled );
       
   690 
       
   691     // NOTICE: This is a little bit dangerous. If the
       
   692     //         subscription type is not found, it is the default
       
   693     //         value and that is externalized.
       
   694     aStream.WriteInt32L( iSubscriptionType );
       
   695 
       
   696     aStream.WriteInt32L( iValidityTimeSet );
       
   697     const TInt64& intValidUntil = iValidUntil.Int64();
       
   698     aStream << intValidUntil;
       
   699     aStream.WriteInt32L( iTotalValidityDelta );
       
   700     
       
   701     aStream.WriteInt32L( iCreditLimitSet );
       
   702     aStream.WriteReal32L( iCreditsLeft );
       
   703     aStream.WriteReal32L( iTotalCredits );
       
   704 
       
   705     aStream.WriteInt32L( iDownloadLimitSet );
       
   706     aStream.WriteInt32L( iDownloadsLeft );
       
   707     aStream.WriteInt32L( iTotalDownloads );
       
   708 
       
   709     DASSERT( iPurchaseOptionId != NULL );
       
   710     ExternalizeDesL( *iPurchaseOptionId, aStream );
       
   711     
       
   712     DLTRACE(( _L(" Externalized subscription, purchaseoptionid: %S"),
       
   713               iPurchaseOptionId ));
       
   714 
       
   715     DLTRACEOUT((""));    
       
   716     }
       
   717 
       
   718 
       
   719 void CNcdSubscription::ResetProtocolMemberVariables()
       
   720     {
       
   721     // In all cases this state variable is in the beginning false
       
   722     // This has to be set especially by the user.
       
   723     iRecentlyUpdated = EFalse;
       
   724     
       
   725     delete iPurchaseOptionId;
       
   726     iPurchaseOptionId = NULL;
       
   727 
       
   728     // Name 
       
   729     // cannot be deleted as they are not received
       
   730     // from the subscription protocol element.
       
   731    
       
   732     delete iExpiredOn;
       
   733     iExpiredOn = NULL;
       
   734     
       
   735     iCancelled = EFalse;
       
   736     
       
   737     iSubscriptionType = MNcdSubscription::EPeriodic;
       
   738     
       
   739     iValidityTimeSet = EFalse;
       
   740     iValidUntil = -1;
       
   741     iTotalValidityDelta = -1;
       
   742 
       
   743     iCreditLimitSet = EFalse;
       
   744     iCreditsLeft = -1;
       
   745     iTotalCredits = -1;
       
   746     
       
   747     iDownloadLimitSet = EFalse;
       
   748     iDownloadsLeft = -1;
       
   749     iTotalDownloads = -1;    
       
   750     }
       
   751 
       
   752 void CNcdSubscription::ResetMemberVariables()
       
   753     {
       
   754     delete iName;
       
   755     iName = NULL;
       
   756     
       
   757     ResetProtocolMemberVariables();
       
   758     }