ncdengine/provider/client/src/ncdsubscriptiongroupproxy.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:   Contains CNcdSubscriptionGroupProxy class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "ncdsubscriptiongroupproxy.h"
       
    20 
       
    21 #include "ncdsubscriptionproxy.h"
       
    22 #include "catalogsclientserver.h"
       
    23 #include "ncdnodeidentifier.h"
       
    24 #include "ncdnodefunctionids.h"
       
    25 #include "ncdnodeclassids.h"
       
    26 #include "catalogsutils.h"
       
    27 #include "catalogsconstants.h"
       
    28 #include "ncdnodeidentifier.h"
       
    29 
       
    30 #include "catalogsdebug.h"
       
    31 
       
    32 // ======== PUBLIC MEMBER FUNCTIONS ========
       
    33 
       
    34 CNcdSubscriptionGroupProxy::CNcdSubscriptionGroupProxy(
       
    35     MCatalogsClientServer& aSession,
       
    36     TInt aHandle,
       
    37     CNcdOperationManagerProxy& aOperationManager,
       
    38     CNcdNodeManagerProxy& aNodeManager )
       
    39     : CNcdBaseProxy( aSession, aHandle ),
       
    40       iOperationManager( aOperationManager ),
       
    41       iNodeManager( aNodeManager )
       
    42     {
       
    43     }
       
    44 
       
    45 
       
    46 void CNcdSubscriptionGroupProxy::ConstructL()
       
    47     {
       
    48     // Is it ok if internalization fails here?
       
    49     // Earlier comment: Do not let the internalization leave here.
       
    50     //                  This object may be reinternalized later.
       
    51     InternalizeL();
       
    52     }
       
    53 
       
    54 
       
    55 CNcdSubscriptionGroupProxy* CNcdSubscriptionGroupProxy::NewL(
       
    56     MCatalogsClientServer& aSession,
       
    57     TInt aHandle,
       
    58     CNcdOperationManagerProxy& aOperationManager,
       
    59     CNcdNodeManagerProxy& aNodeManager )
       
    60     {
       
    61     CNcdSubscriptionGroupProxy* self = 
       
    62         CNcdSubscriptionGroupProxy::NewLC( aSession,
       
    63                                            aHandle,
       
    64                                            aOperationManager,
       
    65                                            aNodeManager );
       
    66     CleanupStack::Pop( self );
       
    67     return self;   
       
    68     }
       
    69 
       
    70 CNcdSubscriptionGroupProxy* CNcdSubscriptionGroupProxy::NewLC(
       
    71     MCatalogsClientServer& aSession,
       
    72     TInt aHandle,
       
    73     CNcdOperationManagerProxy& aOperationManager,
       
    74     CNcdNodeManagerProxy& aNodeManager )
       
    75     {
       
    76     CNcdSubscriptionGroupProxy* self = 
       
    77         new( ELeave ) CNcdSubscriptionGroupProxy( aSession,
       
    78                                                   aHandle,
       
    79                                                   aOperationManager,
       
    80                                                   aNodeManager );
       
    81     CleanupStack::PushL( self );
       
    82     self->ConstructL();
       
    83     return self;  
       
    84     }
       
    85 
       
    86 
       
    87 CNcdSubscriptionGroupProxy::~CNcdSubscriptionGroupProxy()
       
    88     {
       
    89     DeleteSubscriptions();
       
    90     
       
    91     delete iOriginNodeIdentifier;
       
    92     }
       
    93 
       
    94 
       
    95 void CNcdSubscriptionGroupProxy::InternalizeL()
       
    96     {
       
    97     DLTRACEIN((""));
       
    98     
       
    99     // Request all the purchase option ids from server side.
       
   100     CDesCArray* poIds = RequestPurchaseOptionIdsL();
       
   101     CleanupStack::PushL( poIds );
       
   102     
       
   103     // Delete the subscriptions that don't exist in the server side.
       
   104     DeleteMissingSubscriptions( *poIds );
       
   105     
       
   106     TRAPD( err, 
       
   107         {
       
   108         // Reinternalize the existing subscriptions.
       
   109         for ( TInt i = 0; i < iSubscriptions.Count(); i++ ) 
       
   110             {
       
   111             iSubscriptions[i]->InternalizeL();
       
   112             }
       
   113     
       
   114         // Remove the existing subscriptions from the array.
       
   115         for ( TInt i = poIds->Count() - 1 ; i >= 0; i-- ) 
       
   116             {
       
   117             if ( Subscription( (*poIds)[i] ) != NULL ) 
       
   118                 {
       
   119                 poIds->Delete( i );
       
   120                 }
       
   121             }
       
   122         
       
   123         // Internalize the group with the new subscriptions.
       
   124         InternalizeL( *poIds );
       
   125         });
       
   126         
       
   127     CleanupStack::PopAndDestroy( poIds );
       
   128 
       
   129     if ( err != KErrNone ) 
       
   130         {
       
   131         DLINFO((( "Internalize error: %d" ), err ));
       
   132         DeleteSubscriptions();
       
   133         User::Leave( err );
       
   134         }
       
   135     
       
   136     DLTRACEOUT((""));    
       
   137     }
       
   138 
       
   139 const RPointerArray<CNcdSubscriptionProxy>&
       
   140     CNcdSubscriptionGroupProxy::Subscriptions()
       
   141     {
       
   142     return iSubscriptions;
       
   143     }
       
   144 
       
   145 CNcdSubscriptionProxy* CNcdSubscriptionGroupProxy::Subscription(
       
   146     const TDesC& aPurchaseOptionId )
       
   147     {
       
   148     DLTRACEIN((""));
       
   149     // Search for the subscriptions
       
   150 
       
   151     TInt subscriptionCount( iSubscriptions.Count() );
       
   152     TInt subscriptionIndexer( 0 );
       
   153     while ( subscriptionIndexer < subscriptionCount )
       
   154         {
       
   155         
       
   156         TDesC& subscriptionPurchaseOptionId = 
       
   157             iSubscriptions[subscriptionIndexer]->PurchaseOptionId();
       
   158             
       
   159         if (  aPurchaseOptionId == subscriptionPurchaseOptionId )
       
   160             {
       
   161             DLTRACEOUT(("Found."));
       
   162             return iSubscriptions[subscriptionIndexer];
       
   163             }
       
   164         
       
   165         ++subscriptionIndexer;
       
   166         }
       
   167 
       
   168     DLTRACEOUT(("Did not find."));
       
   169     return NULL;    
       
   170     }
       
   171 
       
   172 const CNcdNodeIdentifier& CNcdSubscriptionGroupProxy::Identifier() const
       
   173     {
       
   174     return *iOriginNodeIdentifier;
       
   175     }
       
   176 
       
   177 const TDesC& CNcdSubscriptionGroupProxy::EntityId() const
       
   178     {
       
   179     return iOriginNodeIdentifier->NodeId();
       
   180     }
       
   181 
       
   182 const TDesC& CNcdSubscriptionGroupProxy::Namespace() const
       
   183     {
       
   184     return iOriginNodeIdentifier->NodeNameSpace();
       
   185     }
       
   186 
       
   187 const TDesC& CNcdSubscriptionGroupProxy::ServerUri() const
       
   188     {
       
   189     return iOriginNodeIdentifier->ServerUri();
       
   190     }
       
   191 
       
   192 // ---------------------------------------------------------------------------
       
   193 // Icon data is not stored on the proxies. It is retrieved from the server
       
   194 // side every time the data is requested.
       
   195 // ---------------------------------------------------------------------------
       
   196 //
       
   197 HBufC8* CNcdSubscriptionGroupProxy::IconL() const
       
   198     {
       
   199     HBufC8* icon( RequestIconDataL() );
       
   200     return icon;
       
   201     }
       
   202 
       
   203 CNcdOperationManagerProxy& 
       
   204     CNcdSubscriptionGroupProxy::OperationManager() const
       
   205     {
       
   206     return iOperationManager;
       
   207     }
       
   208 
       
   209 
       
   210 // Other functions
       
   211 
       
   212 
       
   213 void CNcdSubscriptionGroupProxy::InternalizeDataL( RReadStream& aStream )
       
   214     {
       
   215     DLTRACEIN((""));
       
   216 
       
   217     if ( !iOriginNodeIdentifier )
       
   218         {
       
   219         iOriginNodeIdentifier = CNcdNodeIdentifier::NewL();
       
   220         }        
       
   221     iOriginNodeIdentifier->InternalizeL( aStream );
       
   222 
       
   223     DLTRACE(( _L(" Internalizing subscriptiongroup info in proxy, EntityId: %S, Namespace: %S, server uri: %S"),
       
   224               &iOriginNodeIdentifier->NodeId(),
       
   225               &iOriginNodeIdentifier->NodeNameSpace(), 
       
   226               &iOriginNodeIdentifier->ServerUri() ));
       
   227 
       
   228     TInt handleAmount( 0 );
       
   229     handleAmount = aStream.ReadInt32L();
       
   230     
       
   231     DLTRACE(( "Amount of subscription handles received: %d",
       
   232               handleAmount ));
       
   233 
       
   234     TInt tmpProxyHandle( -1 ); // handle of a proxy read from stream
       
   235     
       
   236     // temporary pointer to subscription that is going to be
       
   237     // added to subscriptions-array
       
   238     CNcdSubscriptionProxy* tmpSubscription( NULL );
       
   239 
       
   240     // In error handling, objects with received handles
       
   241     // should be released from the server side session
       
   242     // if proxies for them cannot be created.
       
   243     
       
   244     TInt handleIndex( 0 );
       
   245     while ( handleIndex < handleAmount )
       
   246         {
       
   247         tmpProxyHandle = aStream.ReadInt32L();
       
   248         
       
   249         DLTRACE(( "Received subscription handle: %i",
       
   250                   tmpProxyHandle ));
       
   251                       
       
   252         tmpSubscription = CNcdSubscriptionProxy::NewL( 
       
   253                               ClientServerSession(),
       
   254                               tmpProxyHandle,
       
   255                               OperationManager(),
       
   256                               iNodeManager,
       
   257                               *this );                
       
   258 
       
   259 
       
   260         TRAPD( addError, iSubscriptions.AppendL( tmpSubscription ) );
       
   261         if ( addError != KErrNone )
       
   262             {
       
   263             delete tmpSubscription;
       
   264             User::Leave( addError );
       
   265             }
       
   266         
       
   267         tmpSubscription->InternalAddRef();
       
   268 
       
   269         ++handleIndex;
       
   270         }
       
   271 
       
   272     DLTRACEOUT((""));
       
   273     }
       
   274 
       
   275 void CNcdSubscriptionGroupProxy::DeleteSubscriptions()
       
   276     {
       
   277     
       
   278     // If references to subscriptions are left, set it obsolete.
       
   279 
       
   280     TInt subscriptionsCount( iSubscriptions.Count() );
       
   281     TInt subscriptionsIndexer( 0 );
       
   282     while ( subscriptionsIndexer < subscriptionsCount )
       
   283         {
       
   284         DeleteSubscription( iSubscriptions[subscriptionsIndexer] );
       
   285         ++subscriptionsIndexer;
       
   286         }
       
   287 
       
   288     iSubscriptions.Reset();
       
   289     }
       
   290     
       
   291 CDesCArray* CNcdSubscriptionGroupProxy::RequestPurchaseOptionIdsL() const 
       
   292     {
       
   293     DLTRACEIN((""));
       
   294     
       
   295     HBufC8* data( NULL );
       
   296     
       
   297     // Because we do not know the exact size of the data, use
       
   298     // the alloc method, which creates the buffer of the right size
       
   299     // and sets the pointer to point to the created buffer.
       
   300     User::LeaveIfError(
       
   301         ClientServerSession().
       
   302         SendSyncAlloc( NcdNodeFunctionIds::ENcdPurchaseOptionIds,
       
   303                        KNullDesC8,
       
   304                        data,
       
   305                        Handle(),
       
   306                        0 ) );
       
   307 
       
   308     if ( data == NULL )
       
   309         {
       
   310         DLERROR((""));
       
   311         User::Leave(  KErrNotFound );
       
   312         }
       
   313 
       
   314      CleanupStack::PushL( data );
       
   315 
       
   316      // Read the data from the stream and create the array
       
   317      RDesReadStream stream( *data );
       
   318      CleanupClosePushL( stream );
       
   319      
       
   320      CDesCArray* poIds = new( ELeave ) CDesCArrayFlat( 5 );
       
   321      CleanupStack::PushL( poIds );
       
   322      
       
   323      TInt idCount = stream.ReadInt32L();
       
   324      for ( TInt i = 0; i < idCount; i++ ) 
       
   325         {
       
   326         HBufC* id( NULL );
       
   327         InternalizeDesL( id, stream );
       
   328         CleanupStack::PushL( id );
       
   329         poIds->AppendL( *id );
       
   330         CleanupStack::PopAndDestroy( id );
       
   331         }
       
   332         
       
   333     CleanupStack::Pop( poIds );
       
   334     CleanupStack::PopAndDestroy( &stream );
       
   335     CleanupStack::PopAndDestroy( data );
       
   336     
       
   337     return poIds;
       
   338     }
       
   339     
       
   340 void CNcdSubscriptionGroupProxy::DeleteMissingSubscriptions(
       
   341     const CDesCArray& aPurchaseOptionIds ) 
       
   342     {
       
   343     DLTRACEIN((""));
       
   344     
       
   345     for ( TInt i = iSubscriptions.Count() - 1; i >= 0; i-- ) 
       
   346         {
       
   347         TInt pos( 0 );
       
   348         if ( aPurchaseOptionIds.Find( iSubscriptions[i]->PurchaseOptionId(), pos )
       
   349              != 0 ) 
       
   350             {
       
   351             DeleteSubscription( iSubscriptions[i] );
       
   352             iSubscriptions.Remove( i );
       
   353             }
       
   354         }
       
   355     }
       
   356     
       
   357 void CNcdSubscriptionGroupProxy::DeleteSubscription(
       
   358     CNcdSubscriptionProxy* aSubscription ) 
       
   359     {
       
   360     if ( aSubscription->TotalRefCount() > 1 )
       
   361         {
       
   362         aSubscription->SetObsolete();
       
   363         }
       
   364     aSubscription->InternalRelease();
       
   365     }
       
   366 
       
   367 void CNcdSubscriptionGroupProxy::InternalizeL(
       
   368     const CDesCArray& aPurchaseOptionIds ) 
       
   369     {
       
   370     DLTRACEIN((""));
       
   371     
       
   372     CBufBase* buf = CBufFlat::NewL( KBufExpandSize );
       
   373     CleanupStack::PushL( buf );
       
   374         
       
   375     RBufWriteStream writeStream( *buf );
       
   376     CleanupClosePushL( writeStream );
       
   377     
       
   378     writeStream.WriteInt32L( aPurchaseOptionIds.Count() );
       
   379     for ( TInt i = 0; i < aPurchaseOptionIds.Count(); i++ ) 
       
   380         {
       
   381         ExternalizeDesL( aPurchaseOptionIds[i], writeStream );
       
   382         }
       
   383         
       
   384     HBufC8* outputData = HBufC8::NewL( buf->Size() );
       
   385     outputData->Des().Copy( buf->Ptr( 0 ) );
       
   386     
       
   387     CleanupStack::PopAndDestroy( &writeStream );
       
   388     CleanupStack::PopAndDestroy( buf );
       
   389     
       
   390     CleanupStack::PushL( outputData );
       
   391     
       
   392     HBufC8* inputData( NULL );
       
   393 
       
   394     // Because we do not know the exact size of the data, use
       
   395     // the alloc method, which creates the buffer of the right size
       
   396     // and sets the pointer to point to the created buffer.
       
   397     User::LeaveIfError(
       
   398         ClientServerSession().
       
   399         SendSyncAlloc( NcdNodeFunctionIds::ENcdInternalize,
       
   400                        *outputData,
       
   401                        inputData,
       
   402                        Handle(),
       
   403                        0 ) );
       
   404 
       
   405     if ( inputData == NULL )
       
   406         {
       
   407         DLERROR((""));
       
   408         User::Leave(  KErrNotFound );
       
   409         }
       
   410         
       
   411     CleanupStack::PopAndDestroy( outputData );
       
   412     CleanupStack::PushL( inputData );
       
   413     
       
   414     // Read the data from the stream and create the array
       
   415     RDesReadStream stream( *inputData );
       
   416     CleanupClosePushL( stream );
       
   417     
       
   418     InternalizeDataL( stream );
       
   419     
       
   420     CleanupStack::PopAndDestroy( &stream );
       
   421     CleanupStack::PopAndDestroy( inputData );
       
   422     }
       
   423 
       
   424 
       
   425 HBufC8* CNcdSubscriptionGroupProxy::RequestIconDataL() const
       
   426     {
       
   427     DLTRACEIN((""));
       
   428 
       
   429     HBufC8* data( NULL );
       
   430         
       
   431     // Because we do not know the exact size of the data, use
       
   432     // the alloc method, which creates the buffer of the right size
       
   433     // and sets the pointer to point to the created buffer.
       
   434     // Get the icon data from the subscription group implementation
       
   435     // on the server side.
       
   436     User::LeaveIfError(
       
   437         ClientServerSession().
       
   438         SendSyncAlloc( NcdNodeFunctionIds::ENcdSubscriptionIconData,
       
   439                        KNullDesC8,
       
   440                        data,
       
   441                        Handle(),
       
   442                        0 ) );
       
   443 
       
   444     if ( data == NULL )
       
   445         {
       
   446         DLERROR((""));
       
   447         User::Leave( KErrNotFound );
       
   448         }
       
   449 
       
   450     CleanupStack::PushL( data );
       
   451 
       
   452     // Read the data from the stream and insert it to variables
       
   453     RDesReadStream stream( *data );
       
   454     CleanupClosePushL( stream );
       
   455 
       
   456     HBufC8* iconData( NULL );
       
   457     InternalizeDesL( iconData, stream );
       
   458 
       
   459     // Closes the stream
       
   460     CleanupStack::PopAndDestroy( &stream ); 
       
   461     CleanupStack::PopAndDestroy( data );
       
   462     
       
   463     DLTRACEOUT((""));
       
   464     return iconData;
       
   465     }