ncdengine/provider/server/src/ncdfavoritemanagerimpl.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 CNcdFavoriteManager class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <s32mem.h>
       
    20 
       
    21 #include "ncdfavoritemanagerimpl.h"
       
    22 #include "ncdnodefunctionids.h"
       
    23 #include "catalogsbasemessage.h"
       
    24 #include "catalogsconstants.h"
       
    25 #include "catalogscontext.h"
       
    26 #include "ncdnodeidentifier.h"
       
    27 #include "ncdstorage.h"
       
    28 #include "ncdstoragemanagerimpl.h"
       
    29 #include "ncdstorageitem.h"
       
    30 #include "ncdproviderdefines.h"
       
    31 #include "ncddatabasestorage.h"
       
    32 #include "ncdnodemanager.h"
       
    33 #include "ncdnodecachecleanermanager.h"
       
    34 #include "ncdnodecachecleaner.h"
       
    35 #include "ncdnodedbmanager.h"
       
    36 #include "ncdnodedisclaimer.h"
       
    37 #include "ncdnodeimpl.h"
       
    38 #include "ncdnodemetadataimpl.h"
       
    39 #include "catalogsutils.h"
       
    40 #include "ncdgeneralmanager.h"
       
    41 
       
    42 #include "catalogsdebug.h"
       
    43 
       
    44 CNcdFavoriteManager* CNcdFavoriteManager::NewL(
       
    45     CNcdGeneralManager& aGeneralManager ) 
       
    46     {
       
    47     CNcdFavoriteManager* self = NewLC( aGeneralManager );
       
    48     CleanupStack::Pop( self );
       
    49     return self;
       
    50     }
       
    51 
       
    52 CNcdFavoriteManager* CNcdFavoriteManager::NewLC(
       
    53     CNcdGeneralManager& aGeneralManager ) 
       
    54     {
       
    55     CNcdFavoriteManager* self =
       
    56         new( ELeave ) CNcdFavoriteManager( aGeneralManager );
       
    57     CleanupClosePushL( *self );
       
    58     self->ConstructL();
       
    59     return self;
       
    60     }
       
    61     
       
    62 CNcdFavoriteManager::CNcdFavoriteManager(
       
    63     CNcdGeneralManager& aGeneralManager ) 
       
    64     : iGeneralManager( aGeneralManager ),
       
    65       iNodeManager( aGeneralManager.NodeManager() ), 
       
    66       iStorageManager( aGeneralManager.StorageManager() )
       
    67     {
       
    68     }
       
    69     
       
    70 void CNcdFavoriteManager::ConstructL() 
       
    71     {
       
    72     DLTRACEIN((""));
       
    73     }
       
    74     
       
    75 CNcdFavoriteManager::~CNcdFavoriteManager() 
       
    76     {
       
    77     iFavorites.ResetAndDestroy();
       
    78     iTempFavorites.ResetAndDestroy();
       
    79     }
       
    80     
       
    81 TInt CNcdFavoriteManager::ClientCount() const 
       
    82     {
       
    83     return iFavorites.Count();
       
    84     }
       
    85     
       
    86 TUid CNcdFavoriteManager::ClientL( TInt aClientIndex ) const 
       
    87     {
       
    88     if ( aClientIndex < 0 || aClientIndex >= iFavorites.Count() ) 
       
    89         {
       
    90         User::Leave( KErrNotFound );
       
    91         }
       
    92     return iFavorites[ aClientIndex ]->ClientUid();
       
    93     }
       
    94 
       
    95 const RPointerArray<CNcdNodeIdentifier>& CNcdFavoriteManager::FavoriteNodesL(
       
    96     const TUid& aClientUid ) 
       
    97     {
       
    98     DLTRACEIN((""));
       
    99 
       
   100     // Load client's favorites from DB, if they are not loaded.
       
   101     DbLoadFavoritesL( aClientUid );
       
   102     
       
   103     TInt index = FindClientFavorites( aClientUid );
       
   104     DASSERT( index != KErrNotFound );
       
   105     CNcdClientFavorites* clientFavorites = iFavorites[ index ];
       
   106     
       
   107     return clientFavorites->Identifiers();
       
   108     }
       
   109 
       
   110 
       
   111 TBool CNcdFavoriteManager::IsFavorite(
       
   112     const CNcdNodeIdentifier& aNodeIdentifier ) const 
       
   113     {
       
   114     DLTRACEIN((""));
       
   115     const TUid& clientUid = aNodeIdentifier.ClientUid();
       
   116     
       
   117     TInt index = FindClientFavorites( clientUid );
       
   118     if ( index == KErrNotFound ) 
       
   119         {
       
   120         return EFalse;
       
   121         }
       
   122         
       
   123     CNcdClientFavorites* clientFavorites = iFavorites[ index ];
       
   124     return clientFavorites->HasFavorite( aNodeIdentifier );
       
   125     }
       
   126     
       
   127 
       
   128 void CNcdFavoriteManager::RemoveFavoritesL( const TUid& aClientUid )
       
   129     {
       
   130     DLTRACEIN((""));
       
   131     TInt index = FindClientFavorites( aClientUid );
       
   132     if ( index != KErrNotFound ) 
       
   133         {
       
   134         CNcdClientFavorites& clientFavorites = *iFavorites[ index ];
       
   135         clientFavorites.RemoveFavorites();
       
   136         DbSaveFavoritesL( clientFavorites ); 
       
   137         }
       
   138     }
       
   139     
       
   140 void CNcdFavoriteManager::ReceiveMessage(
       
   141     MCatalogsBaseMessage* aMessage, TInt aFunctionNumber ) 
       
   142     {
       
   143     DLTRACEIN((""));
       
   144     
       
   145     TInt trapError( KErrNone );
       
   146     
       
   147     // Check which function is called by the proxy side object.
       
   148     // Function number are located in ncdnodefunctinoids.h file.
       
   149     switch( aFunctionNumber )
       
   150         {
       
   151         case NcdNodeFunctionIds::ENcdInternalize:
       
   152             TRAP( trapError, InternalizeRequestL( *aMessage ) );
       
   153             break;
       
   154             
       
   155         case NcdNodeFunctionIds::ENcdFavoriteManagerAddFavorite:
       
   156             TRAP( trapError, AddFavoriteRequestL( *aMessage ) );
       
   157             break;
       
   158             
       
   159         case NcdNodeFunctionIds::ENcdFavoriteManagerRemoveFavorite:
       
   160             TRAP( trapError, RemoveFavoriteRequestL( *aMessage ) );
       
   161             break;
       
   162             
       
   163         case NcdNodeFunctionIds::ENcdFavoriteManagerSetDisclaimer:
       
   164             TRAP( trapError, SetDisclaimerRequestL( *aMessage ) );
       
   165             break;
       
   166             
       
   167         case NcdNodeFunctionIds::ENcdFavoriteManagerDisclaimerHandle:
       
   168             TRAP( trapError, DisclaimerHandleRequestL( *aMessage ) );
       
   169             break;
       
   170             
       
   171         case NcdNodeFunctionIds::ENcdRelease:
       
   172             // The proxy does not want to use this object anymore.
       
   173             // So, release the handle from the session.
       
   174             ReleaseRequest( *aMessage );
       
   175             break;            
       
   176         
       
   177         default:
       
   178             DASSERT( EFalse );
       
   179             break;
       
   180         }
       
   181     
       
   182     if ( trapError != KErrNone )
       
   183         {
       
   184         // Because something went wrong, the complete has not been
       
   185         // yet called for the message.
       
   186         // So, inform the client about the error if the
       
   187         // message is still available.
       
   188         aMessage->CompleteAndRelease( trapError );
       
   189         }
       
   190     }
       
   191     
       
   192 
       
   193 void CNcdFavoriteManager::CounterPartLost( const MCatalogsSession& /*aSession*/ ) 
       
   194     {
       
   195     DLTRACEIN((""));
       
   196     }
       
   197     
       
   198     
       
   199 void CNcdFavoriteManager::AddFavoriteRequestL( MCatalogsBaseMessage& aMessage ) 
       
   200     {
       
   201     DLTRACEIN((""));
       
   202 
       
   203     RCatalogsMessageReader reader;
       
   204     reader.OpenLC( aMessage );    
       
   205     
       
   206     CNcdNodeIdentifier* nodeId = CNcdNodeIdentifier::NewLC( reader() );
       
   207     TBool removeOnDisconnect = reader().ReadInt8L();
       
   208 
       
   209     if ( removeOnDisconnect )
       
   210         {
       
   211         // Add the identifier with session information. Session
       
   212         // information is needed when the favorite must be removed, that is, when
       
   213         // the session is released.
       
   214         AddTemporaryFavoriteL( nodeId, aMessage.Session() );
       
   215         }
       
   216     else 
       
   217         {
       
   218         AddFavoriteL( nodeId );
       
   219         }
       
   220     
       
   221     CleanupStack::Pop( nodeId );
       
   222     CleanupStack::PopAndDestroy( &reader );
       
   223     
       
   224     aMessage.CompleteAndRelease( KErrNone );
       
   225     }
       
   226 
       
   227 
       
   228 void CNcdFavoriteManager::RemoveFavoriteRequestL( MCatalogsBaseMessage& aMessage ) 
       
   229     {
       
   230     DLTRACEIN((""));
       
   231     
       
   232     RCatalogsMessageReader reader;
       
   233     reader.OpenLC( aMessage );
       
   234     
       
   235     CNcdNodeIdentifier* nodeId = CNcdNodeIdentifier::NewLC( reader() );
       
   236     RemoveFavoriteL( nodeId );
       
   237     
       
   238     CleanupStack::Pop( nodeId );
       
   239     CleanupStack::PopAndDestroy( &reader );
       
   240     
       
   241     aMessage.CompleteAndRelease( KErrNone );
       
   242     }
       
   243     
       
   244     
       
   245 void CNcdFavoriteManager::SetDisclaimerRequestL( MCatalogsBaseMessage& aMessage )
       
   246     {
       
   247     DLTRACEIN((""));
       
   248     
       
   249     RCatalogsMessageReader reader;
       
   250     reader.OpenLC( aMessage );
       
   251     
       
   252     // Get the favorite node identifier.
       
   253     CNcdNodeIdentifier* favoriteIdentifier = CNcdNodeIdentifier::NewLC( reader() );
       
   254     DLNODEID(( *favoriteIdentifier ));
       
   255     TInt clientIndex = FindClientFavorites( favoriteIdentifier->ClientUid() );
       
   256     DASSERT( clientIndex != KErrNotFound );
       
   257     CNcdClientFavorites* favorites = iFavorites[ clientIndex ];
       
   258     
       
   259     // Read whether the stream contains the disclaimer owner.
       
   260     TBool isDisclaimer = reader().ReadInt32L();
       
   261     
       
   262     if ( isDisclaimer ) 
       
   263         {
       
   264         DLINFO(("Setting disclaimer for favorite node"));
       
   265         CNcdNodeIdentifier* disclaimerOwner = CNcdNodeIdentifier::NewLC( reader() );
       
   266         CNcdNode& node = iNodeManager.NodeL( *disclaimerOwner );
       
   267         CNcdNodeMetaData& metadata = node.NodeMetaDataL();
       
   268         const CNcdNodeDisclaimer* disclaimer = NULL;
       
   269         
       
   270         // ownership is not transferred
       
   271         // DisclaimerL leaves only with KErrNotFound
       
   272         TRAP_IGNORE( disclaimer = &metadata.DisclaimerL() );
       
   273         
       
   274         if ( !disclaimer )
       
   275             {
       
   276             DLINFO(("Getting disclaimer from favorites"));
       
   277             // ownership is not transferred
       
   278             disclaimer = favorites->Disclaimer( *disclaimerOwner );
       
   279             }
       
   280         
       
   281         if ( disclaimer )
       
   282             {
       
   283             favorites->SetDisclaimerL( *favoriteIdentifier, *disclaimer );
       
   284             }
       
   285         else
       
   286             {
       
   287             DLERROR(("No disclaimer found, leaving with KErrNotFound"));
       
   288             User::Leave( KErrNotFound );
       
   289             }
       
   290         
       
   291         CleanupStack::PopAndDestroy( disclaimerOwner );
       
   292         }
       
   293     else 
       
   294         {
       
   295         DLINFO(("Removing disclaimer from favorite node"));
       
   296         favorites->RemoveDisclaimer( *favoriteIdentifier );
       
   297         }
       
   298         
       
   299     CleanupStack::PopAndDestroy( favoriteIdentifier );
       
   300     CleanupStack::PopAndDestroy( &reader );
       
   301     
       
   302     // Update the state to db.
       
   303     DbSaveFavoritesL( *favorites );
       
   304     
       
   305     aMessage.CompleteAndRelease( KErrNone );
       
   306     }   
       
   307     
       
   308     
       
   309 void CNcdFavoriteManager::DisclaimerHandleRequestL(
       
   310     MCatalogsBaseMessage& aMessage ) 
       
   311     {
       
   312     DLTRACEIN((""));
       
   313     
       
   314     RCatalogsMessageReader reader;
       
   315     reader.OpenLC( aMessage );
       
   316     
       
   317     // Get the favorite node identifier.
       
   318     CNcdNodeIdentifier* favoriteIdentifier = CNcdNodeIdentifier::NewL( reader() );
       
   319     
       
   320     CleanupStack::PopAndDestroy( &reader );
       
   321     
       
   322     TInt clientIndex = FindClientFavorites( favoriteIdentifier->ClientUid() );
       
   323     DASSERT( clientIndex != KErrNotFound );
       
   324     CNcdClientFavorites* favorites = iFavorites[ clientIndex ];
       
   325     CNcdNodeDisclaimer* disclaimer = favorites->Disclaimer( *favoriteIdentifier );
       
   326     
       
   327     delete favoriteIdentifier;
       
   328     favoriteIdentifier = NULL;
       
   329     
       
   330     if ( disclaimer == NULL ) 
       
   331         {
       
   332         // Disclaimer was not found for the node.
       
   333         User::Leave( KErrNotFound );
       
   334         }
       
   335         
       
   336     // Get the session that will contain the handle of the disclaimer.
       
   337     MCatalogsSession& requestSession( aMessage.Session() );
       
   338 
       
   339     // Add the icon to the session and get the handle.
       
   340     // If the node already existed in the session we will still
       
   341     // get a new handle to the same object.
       
   342     TInt32 handle( requestSession.AddObjectL( disclaimer ) );
       
   343 
       
   344     DLINFO(("Disclaimer handle: %d", handle ));
       
   345         
       
   346     // Send the information to the client side
       
   347     // If this leaves, ReceiveMessage will complete the message.
       
   348     aMessage.CompleteAndReleaseL( handle, KErrNone );
       
   349     }
       
   350     
       
   351 
       
   352 void CNcdFavoriteManager::ReleaseRequest( 
       
   353     MCatalogsBaseMessage& aMessage )
       
   354     {
       
   355     DLTRACEIN((""));
       
   356 
       
   357     // Decrease the reference count for this object.
       
   358     // When the reference count reaches zero, this object will be destroyed
       
   359     // and removed from the session.
       
   360     MCatalogsSession& requestSession( aMessage.Session() );
       
   361     TInt handle( aMessage.Handle() );
       
   362     
       
   363     // Remove the temporary favorites of the session.
       
   364     TRAPD( err, RemoveTemporaryFavoritesL( requestSession ) );
       
   365     
       
   366     // Send complete information back to proxy.
       
   367     aMessage.CompleteAndRelease( err );
       
   368         
       
   369     // Remove this object from the session.
       
   370     requestSession.RemoveObject( handle );
       
   371 
       
   372     DLTRACEOUT((""));
       
   373     }
       
   374         
       
   375     
       
   376 void CNcdFavoriteManager::AddFavoriteL( CNcdNodeIdentifier* aNodeIdentifier ) 
       
   377     {
       
   378     DLTRACEIN((""));
       
   379     DASSERT( aNodeIdentifier != NULL );
       
   380     
       
   381     TInt index = FindClientFavorites( aNodeIdentifier->ClientUid() );
       
   382     CNcdClientFavorites* favorites = iFavorites[ index ];
       
   383     
       
   384     // Add the node to node cache cleaner's block list, so it will never be removed
       
   385     // from db.    
       
   386     iNodeManager.NodeCacheCleanerManager().CacheCleanerL( 
       
   387         aNodeIdentifier->ClientUid() ).AddDoNotRemoveIdentifierL( 
       
   388             *aNodeIdentifier, ETrue );
       
   389         
       
   390     favorites->AddFavoriteL( aNodeIdentifier );    
       
   391     DbSaveFavoritesL( *favorites );
       
   392     }
       
   393     
       
   394 
       
   395 void CNcdFavoriteManager::AddTemporaryFavoriteL(
       
   396     CNcdNodeIdentifier* aNodeIdentifier,
       
   397     MCatalogsSession& aSession )
       
   398     {
       
   399     DLTRACEIN((""));
       
   400     DASSERT( aNodeIdentifier != NULL );
       
   401      
       
   402     // The node is favorite already, do nothing.
       
   403     if ( IsFavorite( *aNodeIdentifier ) ) 
       
   404         {
       
   405         delete aNodeIdentifier;
       
   406         return;
       
   407         }
       
   408 
       
   409     CNcdNodeIdentifier* copy = CNcdNodeIdentifier::NewLC( *aNodeIdentifier );
       
   410 
       
   411     // Map the identifier and the session so that the node can be removed when the
       
   412     // session is released.
       
   413     CNcdTemporaryFavorites& tempFavorites = TemporaryFavoritesL( aSession );
       
   414     tempFavorites.iFavoriteIdentifiers.AppendL( copy );
       
   415     CleanupStack::Pop( copy );
       
   416     
       
   417     // Add the favorite normally.    
       
   418     AddFavoriteL( aNodeIdentifier );
       
   419     }
       
   420 
       
   421     
       
   422 void CNcdFavoriteManager::RemoveFavoriteL( CNcdNodeIdentifier* aNodeIdentifier )
       
   423     {
       
   424     DLTRACEIN((""));
       
   425     DASSERT( aNodeIdentifier != NULL );
       
   426     
       
   427     TInt index = FindClientFavorites( aNodeIdentifier->ClientUid() );
       
   428     DASSERT( index != KErrNotFound );
       
   429     CNcdClientFavorites* favorites = iFavorites[ index ];
       
   430 
       
   431     // Remove the node from node db mananager's block list, so it can be removed from
       
   432     // db.    
       
   433     iNodeManager.NodeCacheCleanerManager().CacheCleanerL( 
       
   434         aNodeIdentifier->ClientUid() ).RemoveDoNotRemoveIdentifierL( 
       
   435             *aNodeIdentifier, ETrue );
       
   436     
       
   437     favorites->RemoveFavorite( *aNodeIdentifier );    
       
   438     DbSaveFavoritesL( *favorites );
       
   439     delete aNodeIdentifier;
       
   440     }
       
   441     
       
   442     
       
   443 void CNcdFavoriteManager::RemoveTemporaryFavoritesL( MCatalogsSession& aSession ) 
       
   444     {
       
   445     DLTRACEIN((""));
       
   446     if ( HasTemporaryFavorites( aSession ) ) 
       
   447         {
       
   448         CNcdTemporaryFavorites& tempFavorites = TemporaryFavoritesL( aSession );
       
   449         for ( TInt i = tempFavorites.iFavoriteIdentifiers.Count() - 1; i >= 0; i-- ) 
       
   450             {
       
   451             // Remove the favorite, deletes also the given node identifier object.
       
   452             RemoveFavoriteL( tempFavorites.iFavoriteIdentifiers[ i ] );
       
   453             tempFavorites.iFavoriteIdentifiers.Remove( i );
       
   454             }
       
   455         }
       
   456     }
       
   457     
       
   458     
       
   459 void CNcdFavoriteManager::InternalizeRequestL( MCatalogsBaseMessage& aMessage )
       
   460     {
       
   461     DLTRACEIN((""));
       
   462     
       
   463     // We need session info in the functions for example to
       
   464     // check the client id (there can be several clients).
       
   465     MCatalogsSession& session = aMessage.Session();
       
   466     TUid clientUid = session.Context().FamilyId();
       
   467     
       
   468     // If the client favorites are not loaded from Db, do it now.
       
   469     DbLoadFavoritesL( clientUid );
       
   470 
       
   471     RCatalogsBufferWriter writer;
       
   472     writer.OpenLC();
       
   473     
       
   474     ExternalizeIdentifiersL( writer(), clientUid );
       
   475             
       
   476     // If this leaves, ReceiveMessage will complete the message.
       
   477     // NOTE: that here we expect that the buffer contains at least
       
   478     // some data. So, make sure that ExternalizeIdentifiersL inserts
       
   479     // something to the buffer.
       
   480     aMessage.CompleteAndReleaseL( writer.PtrL(), KErrNone );
       
   481     
       
   482     CleanupStack::PopAndDestroy( &writer );
       
   483     }
       
   484     
       
   485 void CNcdFavoriteManager::ExternalizeIdentifiersL(
       
   486     RWriteStream& aStream, const TUid& aClientUid ) 
       
   487     {
       
   488     DLTRACEIN((""));
       
   489     
       
   490     TInt index = FindClientFavorites( aClientUid );
       
   491     DASSERT( index != KErrNotFound );
       
   492 
       
   493     CNcdClientFavorites* favorites = iFavorites[ index ];
       
   494     favorites->ExternalizeIdentifiersL( aStream );    
       
   495     }
       
   496 
       
   497     
       
   498 TInt CNcdFavoriteManager::FindClientFavorites( const TUid& aClientUid ) const
       
   499     {
       
   500     DLTRACEIN((""));
       
   501     for ( TInt i = 0; i < iFavorites.Count(); i++ ) 
       
   502         {
       
   503         if ( iFavorites[ i ]->ClientUid() == aClientUid ) 
       
   504             {
       
   505             return i;
       
   506             }
       
   507         }
       
   508         
       
   509     return KErrNotFound;
       
   510     }
       
   511     
       
   512     
       
   513 CNcdFavoriteManager::CNcdTemporaryFavorites& CNcdFavoriteManager::TemporaryFavoritesL(
       
   514     MCatalogsSession& aSession )
       
   515     {
       
   516     DLTRACEIN((""));
       
   517 
       
   518     // Find the correct CNcdTemporaryFavorites object from the array.        
       
   519     for ( TInt i = 0; i < iTempFavorites.Count(); i++ ) 
       
   520         {
       
   521         if ( &iTempFavorites[ i ]->iSession == &aSession ) 
       
   522             {
       
   523             return *iTempFavorites[ i ];
       
   524             }
       
   525         }
       
   526         
       
   527     // There was no existing CNcdTemporaryFavorites object, create one and return it.
       
   528     CNcdTemporaryFavorites* temp = new( ELeave ) CNcdTemporaryFavorites( aSession );
       
   529     CleanupStack::PushL( temp );
       
   530     iTempFavorites.AppendL( temp );
       
   531     CleanupStack::Pop( temp );
       
   532     return *temp;
       
   533     }
       
   534     
       
   535     
       
   536 TBool CNcdFavoriteManager::HasTemporaryFavorites( MCatalogsSession& aSession ) const
       
   537     {
       
   538     DLTRACEIN((""));
       
   539     for ( TInt i = 0; i < iTempFavorites.Count(); i++ ) 
       
   540         {
       
   541         if ( &iTempFavorites[ i ]->iSession == &aSession )
       
   542             {
       
   543             return iTempFavorites[ i ]->iFavoriteIdentifiers.Count() > 0;
       
   544             }
       
   545         }
       
   546     return EFalse;
       
   547     }
       
   548     
       
   549     
       
   550 void CNcdFavoriteManager::DbLoadFavoritesL( const TUid& aClientUid ) 
       
   551     {
       
   552     DLTRACEIN((""));
       
   553     
       
   554     if ( FindClientFavorites( aClientUid ) != KErrNotFound ) 
       
   555         {
       
   556         // Client favorites already loaded, return
       
   557         return;
       
   558         }
       
   559     
       
   560     MNcdStorage& providerStorage = iStorageManager.ProviderStorageL(
       
   561         iGeneralManager.FamilyName() );
       
   562     MNcdDatabaseStorage& database =
       
   563         providerStorage.DatabaseStorageL( NcdProviderDefines::KDefaultDatabaseUid );
       
   564     
       
   565     if( !database.ItemExistsInStorageL( aClientUid.Name(), 
       
   566         NcdProviderDefines::ENcdFavoriteManager ) ) 
       
   567         {
       
   568         // Create empty list of favorites
       
   569         CNcdClientFavorites* list = CNcdClientFavorites::NewLC( aClientUid );
       
   570         iFavorites.AppendL( list );
       
   571         CleanupStack::Pop( list );
       
   572         }
       
   573     else 
       
   574         {
       
   575         // Get the storage item from which the data is loaded
       
   576         // Note: database has the ownership of the item
       
   577         MNcdStorageItem* item = database.StorageItemL(
       
   578             aClientUid.Name(), NcdProviderDefines::ENcdFavoriteManager );    
       
   579         
       
   580         // Get data from database by using CNcdClientFavorites as the target so that 
       
   581         // internalize will be called for it
       
   582         CNcdClientFavorites* data = CNcdClientFavorites::NewLC( aClientUid );
       
   583         item->SetDataItem( data );
       
   584     
       
   585         // Read data -> calls CNcdClientFavorites::InternalizeL
       
   586         item->ReadDataL();
       
   587         
       
   588         iFavorites.AppendL( data );
       
   589         CleanupStack::Pop( data );
       
   590        
       
   591         // Add the favorites to node cache cleaner's list of nodes that must never be
       
   592         // removed from db.         
       
   593         iNodeManager.NodeCacheCleanerManager().CacheCleanerL( 
       
   594             aClientUid ).AddDoNotRemoveIdentifiersL( data->Identifiers(), ETrue );
       
   595         }
       
   596     }
       
   597     
       
   598 
       
   599 void CNcdFavoriteManager::DbSaveFavoritesL( CNcdClientFavorites& aFavorites )
       
   600     {
       
   601     DLTRACEIN((""));
       
   602     
       
   603     MNcdStorage& providerStorage = iStorageManager.ProviderStorageL(
       
   604         iGeneralManager.FamilyName() );
       
   605     MNcdDatabaseStorage& database =
       
   606         providerStorage.DatabaseStorageL( NcdProviderDefines::KDefaultDatabaseUid );
       
   607         
       
   608     // Get the storage item to which the access point manager is stored
       
   609     // Note: database has the ownership of the item
       
   610     MNcdStorageItem* item = database.StorageItemL(
       
   611         aFavorites.ClientUid().Name(), NcdProviderDefines::ENcdFavoriteManager);
       
   612     item->SetDataItem( &aFavorites );
       
   613     item->OpenL();
       
   614         
       
   615     // Calls ExternalizeL for this
       
   616     item->WriteDataL();
       
   617     item->SaveL();
       
   618     }
       
   619     
       
   620     
       
   621 CNcdFavoriteManager::CNcdClientFavorites* CNcdFavoriteManager::CNcdClientFavorites::NewLC(
       
   622     const TUid& aClientUid ) 
       
   623     {
       
   624     DLTRACEIN((""));
       
   625     CNcdClientFavorites* self = new( ELeave ) CNcdClientFavorites( aClientUid );
       
   626     CleanupStack::PushL( self );
       
   627     return self;
       
   628     }
       
   629     
       
   630     
       
   631 CNcdFavoriteManager::CNcdClientFavorites::CNcdClientFavorites( const TUid& aClientUid )
       
   632     : iClientUid( aClientUid ) 
       
   633     {
       
   634     }
       
   635     
       
   636     
       
   637 CNcdFavoriteManager::CNcdClientFavorites::~CNcdClientFavorites() 
       
   638     {
       
   639     RemoveFavorites();
       
   640     }
       
   641     
       
   642 TUid CNcdFavoriteManager::CNcdClientFavorites::ClientUid() const 
       
   643     {
       
   644     return iClientUid;
       
   645     }
       
   646     
       
   647 void CNcdFavoriteManager::CNcdClientFavorites::AddFavoriteL(
       
   648     CNcdNodeIdentifier* aNodeIdentifier )  
       
   649     {
       
   650     DLTRACEIN((""));
       
   651     DASSERT( aNodeIdentifier != NULL );
       
   652     DASSERT( iIdentifiers.Count() == iDisclaimers.Count() );
       
   653     
       
   654     iIdentifiers.ReserveL( iIdentifiers.Count() + 1 );
       
   655     iDisclaimers.ReserveL( iDisclaimers.Count() + 1 );
       
   656     
       
   657     TInt err1 = iIdentifiers.Append( aNodeIdentifier );
       
   658     
       
   659     // Add NULL disclaimer for the node.
       
   660     TInt err2 = iDisclaimers.Append( NULL );
       
   661     
       
   662     DASSERT( err1 == KErrNone && err2 == KErrNone );
       
   663     }
       
   664     
       
   665 void CNcdFavoriteManager::CNcdClientFavorites::RemoveFavorite(
       
   666     const CNcdNodeIdentifier& aNodeIdentifier ) 
       
   667     {
       
   668     TInt count = iIdentifiers.Count();
       
   669     DASSERT( iDisclaimers.Count() == count );
       
   670     
       
   671     for ( TInt i = 0; i < count; i++ ) 
       
   672         {
       
   673         if ( iIdentifiers[ i ]->Equals( aNodeIdentifier ) ) 
       
   674             {
       
   675             delete iIdentifiers[ i ];
       
   676             iIdentifiers.Remove( i );
       
   677             if ( iDisclaimers[ i ] ) 
       
   678                 {
       
   679                 iDisclaimers[ i ]->Close();
       
   680                 }
       
   681             iDisclaimers.Remove( i );
       
   682             break;
       
   683             }
       
   684         }
       
   685     }
       
   686 
       
   687 
       
   688 void CNcdFavoriteManager::CNcdClientFavorites::RemoveFavorites() 
       
   689     {
       
   690     CloseDisclaimers();
       
   691     iIdentifiers.ResetAndDestroy();    
       
   692     }
       
   693     
       
   694     
       
   695 void CNcdFavoriteManager::CNcdClientFavorites::SetDisclaimerL(
       
   696     const CNcdNodeIdentifier& aNodeIdentifier,
       
   697     const CNcdNodeDisclaimer& aDisclaimer ) 
       
   698     {
       
   699     DLTRACEIN((""));
       
   700     DLNODEID( aNodeIdentifier );
       
   701     DASSERT( iIdentifiers.Count() == iDisclaimers.Count() );
       
   702     TInt count = iIdentifiers.Count();
       
   703     
       
   704     for ( TInt i = 0; i < count; i++ ) 
       
   705         {
       
   706         CNcdNodeIdentifier* identifier = iIdentifiers[ i ];
       
   707         if ( identifier->Equals( aNodeIdentifier ) ) 
       
   708             {
       
   709             // Correct index found, copy the disclaimer.
       
   710             CNcdNodeDisclaimer* copy = CNcdNodeDisclaimer::NewL( aDisclaimer );
       
   711             if ( iDisclaimers[ i ] ) 
       
   712                 {
       
   713                 iDisclaimers[ i ]->Close();
       
   714                 }
       
   715             iDisclaimers[ i ] = copy;
       
   716             break;
       
   717             }
       
   718         }
       
   719     }
       
   720     
       
   721     
       
   722 void CNcdFavoriteManager::CNcdClientFavorites::RemoveDisclaimer(
       
   723     const CNcdNodeIdentifier& aNodeIdentifier ) 
       
   724     {
       
   725     DASSERT( iIdentifiers.Count() == iDisclaimers.Count() );
       
   726     TInt count = iIdentifiers.Count();
       
   727     
       
   728     for ( TInt i = 0; i < count; i++ ) 
       
   729         {
       
   730         if ( iIdentifiers[ i ]->Equals( aNodeIdentifier ) ) 
       
   731             {
       
   732             // Correct index found, delete the disclaimer.
       
   733             if ( iDisclaimers[ i ] ) 
       
   734                 {                
       
   735                 iDisclaimers[ i ]->Close();
       
   736                 }
       
   737             iDisclaimers[ i ] = NULL;
       
   738             break;
       
   739             }
       
   740         }
       
   741     }
       
   742     
       
   743     
       
   744 CNcdNodeDisclaimer* CNcdFavoriteManager::CNcdClientFavorites::Disclaimer(
       
   745     const CNcdNodeIdentifier& aNodeIdentifier ) const 
       
   746     {
       
   747     DASSERT( iIdentifiers.Count() == iDisclaimers.Count() );
       
   748     TInt count = iIdentifiers.Count();
       
   749     for ( TInt i = 0; i < count; i++ ) 
       
   750         {
       
   751         if ( iIdentifiers[ i ]->Equals( aNodeIdentifier ) ) 
       
   752             {
       
   753             return iDisclaimers[ i ];
       
   754             }
       
   755         }
       
   756         
       
   757     return NULL;
       
   758     }
       
   759     
       
   760 
       
   761 TBool CNcdFavoriteManager::CNcdClientFavorites::HasFavorite(
       
   762     const CNcdNodeIdentifier& aNodeIdentifier ) const 
       
   763     {
       
   764     TInt count = iIdentifiers.Count();
       
   765     for ( TInt i = 0; i < count; i++ ) 
       
   766         {
       
   767         if ( iIdentifiers[ i ]->Equals( aNodeIdentifier ) )
       
   768             {
       
   769             return ETrue;
       
   770             }
       
   771         }
       
   772     return EFalse;
       
   773     }
       
   774     
       
   775     
       
   776 const RPointerArray<CNcdNodeIdentifier>& CNcdFavoriteManager::CNcdClientFavorites::Identifiers() const 
       
   777     {
       
   778     return iIdentifiers;
       
   779     }
       
   780     
       
   781     
       
   782 void CNcdFavoriteManager::CNcdClientFavorites::ExternalizeIdentifiersL(
       
   783     RWriteStream& aStream ) const
       
   784     {
       
   785     DLTRACEIN((""));
       
   786     
       
   787     TInt count = iIdentifiers.Count();
       
   788     aStream.WriteInt32L( count );
       
   789     for ( TInt i = 0; i < count; i++ ) 
       
   790         {
       
   791         iIdentifiers[ i ]->ExternalizeL( aStream );
       
   792         }
       
   793     }
       
   794     
       
   795     
       
   796 void CNcdFavoriteManager::CNcdClientFavorites::ExternalizeL( RWriteStream& aStream )
       
   797     {
       
   798     DLTRACEIN((""));
       
   799     DASSERT( iIdentifiers.Count() == iDisclaimers.Count() );
       
   800     
       
   801     TInt count = iIdentifiers.Count();
       
   802     aStream.WriteInt32L( count );
       
   803     for ( TInt i = 0; i < count; i++ ) 
       
   804         {
       
   805         iIdentifiers[ i ]->ExternalizeL( aStream );
       
   806         if ( iDisclaimers[ i ] ) 
       
   807             {
       
   808             aStream.WriteInt8L( ETrue );
       
   809             iDisclaimers[ i ]->ExternalizeL( aStream );
       
   810             }
       
   811         else 
       
   812             {
       
   813             aStream.WriteInt8L( EFalse );
       
   814             }
       
   815         }    
       
   816     }
       
   817     
       
   818 void CNcdFavoriteManager::CNcdClientFavorites::InternalizeL( RReadStream& aStream ) 
       
   819     {
       
   820     DLTRACEIN((""));
       
   821     iIdentifiers.ResetAndDestroy();
       
   822   
       
   823     // Close all the disclaimer objects.
       
   824     CloseDisclaimers();
       
   825   
       
   826     // Reserve memory for the arrays.
       
   827     TInt count = aStream.ReadInt32L();
       
   828     
       
   829     iIdentifiers.ReserveL( count );
       
   830     iDisclaimers.ReserveL( count );
       
   831     
       
   832     // Read the stream.
       
   833     for ( TInt i = 0; i < count; i++ ) 
       
   834         {
       
   835         CNcdNodeIdentifier* identifier = CNcdNodeIdentifier::NewLC( aStream );
       
   836         CNcdNodeDisclaimer* disclaimer( NULL );
       
   837         TBool isDisclaimer = aStream.ReadInt8L();
       
   838         if ( isDisclaimer ) 
       
   839             {
       
   840             disclaimer = CNcdNodeDisclaimer::NewLC();
       
   841             disclaimer->InternalizeL( aStream );
       
   842             CleanupStack::Pop( disclaimer );
       
   843             }
       
   844         
       
   845         CleanupStack::Pop( identifier);
       
   846         
       
   847         TInt err1 = iIdentifiers.Append( identifier );
       
   848         TInt err2 = iDisclaimers.Append( disclaimer );
       
   849         
       
   850         // Memory was allocated earlier, so error should never occur.
       
   851         DASSERT( err1 == KErrNone && err2 == KErrNone );
       
   852         }
       
   853     }
       
   854 
       
   855 void CNcdFavoriteManager::CNcdClientFavorites::CloseDisclaimers() 
       
   856     {
       
   857     TInt disclaimerCount = iDisclaimers.Count();
       
   858     for ( TInt i = 0; i < disclaimerCount; i++ ) 
       
   859         {
       
   860         if ( iDisclaimers[ i ] ) 
       
   861             {            
       
   862             iDisclaimers[ i ]->Close();
       
   863             }
       
   864         }
       
   865     iDisclaimers.Reset();
       
   866     }
       
   867     
       
   868 
       
   869 CNcdFavoriteManager::CNcdTemporaryFavorites::CNcdTemporaryFavorites(
       
   870     MCatalogsSession& aSession ) 
       
   871     : iSession( aSession )
       
   872     {
       
   873     }
       
   874     
       
   875     
       
   876 CNcdFavoriteManager::CNcdTemporaryFavorites::~CNcdTemporaryFavorites() 
       
   877     {
       
   878     DLTRACEIN((""));
       
   879     iFavoriteIdentifiers.ResetAndDestroy();
       
   880     }