ximpfw/presence/srcpresencedatamodel/presencedatacacheimp.cpp
changeset 0 e6b17d312c8b
equal deleted inserted replaced
-1:000000000000 0:e6b17d312c8b
       
     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:  Presence Service Connection data cache implementation.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <badesca.h>
       
    20 #include <e32cmn.h>
       
    21 
       
    22 #include "presencedatacacheimp.h"
       
    23 #include "ximpidentityimp.h"
       
    24 #include "presenceinfoimp.h"
       
    25 #include "presencewatcherinfoimp.h"
       
    26 #include "watcherlistsubscriptionitem.h"
       
    27 #include "ximpobjecthelpers.h"
       
    28 #include "ownpresenceeventimp.h"
       
    29 #include "ownpresencesubscriptionitem.h"
       
    30 #include "grouplistsubscriptionitem.h"
       
    31 #include "groupcontentsubscriptionitem.h"
       
    32 #include "presentitypresencesubscriptionitem.h"
       
    33 #include "presentitygroupmemberspresencesubscriptionitem.h"
       
    34 #include "presentitygroupinfoimp.h"
       
    35 #include "presentitygroupmemberinfoimp.h"
       
    36 #include "presencegrantrequestinfoimp.h"
       
    37 #include "grantrequestlistsubscriptionitem.h"
       
    38 #include "ximphost.h"
       
    39 #include "ximppsccontext.h"
       
    40 #include "ximpoperationdefs.h"
       
    41 #include "presenceconfigurationitem.h"
       
    42 #include "presencetoeveryoneconfigurationitem.h"
       
    43 #include "groupmemberspresenceconfigurationitem.h"
       
    44 #include "blocklistsubscriptionitem.h"
       
    45 #include "presenceblockinfoimp.h"
       
    46 #include "presencefilterstore.h"
       
    47 //#include "presencecacheclient.h"
       
    48 
       
    49 #include "ximpapieventbase.h"
       
    50 #include <presencecachewriter.h>
       
    51 
       
    52 #include "ximptrace.h"
       
    53 
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // CPresenceDataCache::CPresenceDataCache()
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 CPresenceDataCache::CPresenceDataCache( MXIMPHost& aHost )
       
    60 :iHost( aHost )
       
    61     {
       
    62     }
       
    63 
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // CPresenceDataCache::ConstructL()
       
    67 // ---------------------------------------------------------------------------
       
    68 //
       
    69 void CPresenceDataCache::ConstructL()
       
    70     {
       
    71     iEmptySubscriptionState = CXIMPDataSubscriptionStateImp::NewL();    
       
    72     iExtPresenceCache = MPresenceCacheWriter::NewL();
       
    73     }
       
    74 
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // CPresenceDataCache::NewL()
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 EXPORT_C CPresenceDataCache* CPresenceDataCache::NewL( MXIMPHost& aHost )
       
    81     {
       
    82     CPresenceDataCache* self = new( ELeave ) CPresenceDataCache( aHost );
       
    83     CleanupStack::PushL( self );
       
    84     self->ConstructL();
       
    85     CleanupStack::Pop( self );
       
    86     return self;
       
    87     }
       
    88 
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // CPresenceDataCache::~CPresenceDataCache()
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 CPresenceDataCache::~CPresenceDataCache()
       
    95     {
       
    96 
       
    97     TInt count = iExpiringSubscriptionItems.Count();
       
    98     for( TInt a = 0; a < count; ++a )
       
    99         {
       
   100         iExpiringSubscriptionItems[ a ]->ForceClose();
       
   101         }
       
   102 
       
   103     if( iOwnSubscriptionItem )
       
   104         {
       
   105         iOwnSubscriptionItem->ForceClose();
       
   106         }
       
   107     if( iGroupListSubscriptionItem )
       
   108         {
       
   109         iGroupListSubscriptionItem->ForceClose();
       
   110         }
       
   111 
       
   112     count = iGroupContentSubscriptionItems.Count();
       
   113     for( TInt a = 0; a < count; ++a )
       
   114         {
       
   115         iGroupContentSubscriptionItems[ a ]->ForceClose();
       
   116         }
       
   117 
       
   118     count = iPresentityPresenceSubscriptionItems.Count();
       
   119     for( TInt a = 0; a < count; ++a )
       
   120         {
       
   121         iPresentityPresenceSubscriptionItems[ a ]->ForceClose();
       
   122         }
       
   123 
       
   124     if ( iWatcherListSubscriptionItem )
       
   125         {
       
   126         iWatcherListSubscriptionItem->ForceClose();
       
   127         }
       
   128     if ( iGrantRequestListSubscriptionItem )
       
   129         {
       
   130         iGrantRequestListSubscriptionItem->ForceClose();
       
   131         }
       
   132     if ( iBlockListSubscriptionItem )
       
   133         {
       
   134         iBlockListSubscriptionItem->ForceClose();
       
   135         }
       
   136     if( iPresenceToEveryoneConfigurationItem )
       
   137         {
       
   138         iPresenceToEveryoneConfigurationItem->ForceClose();
       
   139         }
       
   140 
       
   141     count = iGroupMembersPresenceSubscriptionItems.Count();
       
   142     for( TInt a = 0; a < count; ++a )
       
   143         {
       
   144         iGroupMembersPresenceSubscriptionItems[ a ]->ForceClose();
       
   145         }
       
   146     count = iPresenceConfigurationItems.Count();
       
   147     for( TInt a = 0; a < count; ++a )
       
   148         {
       
   149         iPresenceConfigurationItems[ a ]->ForceClose();
       
   150         }
       
   151     count = iGroupMembersPresenceConfigurationItems.Count();
       
   152     for( TInt a = 0; a < count; ++a )
       
   153         {
       
   154         iGroupMembersPresenceConfigurationItems[ a ]->ForceClose();
       
   155         }
       
   156     
       
   157     iGroupContentSubscriptionItems.Close();
       
   158     iPresentityPresenceSubscriptionItems.Close();
       
   159     iExpiringSubscriptionItems.Close();
       
   160     iGroupMembersPresenceSubscriptionItems.Close();
       
   161     iPresenceConfigurationItems.Close();
       
   162     iGroupMembersPresenceConfigurationItems.Close();
       
   163     
       
   164     delete iEmptySubscriptionState;
       
   165     delete iExtPresenceCache;
       
   166     }
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // CPresenceDataCache::RemoveMe
       
   170 // ---------------------------------------------------------------------------
       
   171 //
       
   172 template< class Type >
       
   173 EXPORT_C void CPresenceDataCache::RemoveMe( Type*& aItem, Type* aChild )
       
   174     {
       
   175     if( aChild == aItem )
       
   176         {
       
   177         aItem = NULL;
       
   178         }
       
   179     }
       
   180 
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // CPresenceDataCache::RemoveMeFromArray
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 template< class Type >
       
   187 void CPresenceDataCache::RemoveMeFromArray( RPointerArray< Type >& aItemArray, Type* aChild )
       
   188     {
       
   189     TInt itemCount( aItemArray.Count() );
       
   190     for ( TInt i( 0 ); i < itemCount; i++ )
       
   191         {
       
   192         if ( aChild == aItemArray[ i ] )
       
   193             {
       
   194             aItemArray.Remove( i );
       
   195             break;
       
   196             }
       
   197         }    
       
   198     }
       
   199 
       
   200 
       
   201 
       
   202 // ---------------------------------------------------------------------------
       
   203 // CPresenceDataCache::RemoveMe
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 void CPresenceDataCache::RemoveMe( COwnPresenceSubscriptionItem* aChild )
       
   207     {
       
   208     RemoveMe< COwnPresenceSubscriptionItem >( iOwnSubscriptionItem, aChild );
       
   209     }
       
   210 
       
   211 // ---------------------------------------------------------------------------
       
   212 // CPresenceDataCache::RemoveMe
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 void CPresenceDataCache::RemoveMe( CGroupListSubscriptionItem* aChild )
       
   216     {
       
   217     RemoveMe< CGroupListSubscriptionItem >( iGroupListSubscriptionItem, aChild );
       
   218     }
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // CPresenceDataCache::RemoveMe
       
   222 // ---------------------------------------------------------------------------
       
   223 //
       
   224 void CPresenceDataCache::RemoveMe( CGroupContentSubscriptionItem* aChild )
       
   225     {
       
   226     RemoveMeFromArray< CGroupContentSubscriptionItem >( 
       
   227                                     iGroupContentSubscriptionItems, aChild );
       
   228     }
       
   229 
       
   230 // ---------------------------------------------------------------------------
       
   231 // CPresenceDataCache::RemoveMe
       
   232 // ---------------------------------------------------------------------------
       
   233 //
       
   234 void CPresenceDataCache::RemoveMe( CPresentityPresenceSubscriptionItem* aChild )
       
   235     {
       
   236     RemoveMeFromArray< CPresentityPresenceSubscriptionItem >( 
       
   237                                     iPresentityPresenceSubscriptionItems, aChild );
       
   238     }
       
   239 
       
   240 
       
   241 // ---------------------------------------------------------------------------
       
   242 // CPresenceDataCache::RemoveMe
       
   243 // ---------------------------------------------------------------------------
       
   244 //
       
   245 void CPresenceDataCache::RemoveMe( CWatcherListSubscriptionItem* aChild )
       
   246     {
       
   247     RemoveMe< CWatcherListSubscriptionItem >( iWatcherListSubscriptionItem, aChild );
       
   248     }
       
   249 
       
   250 // ---------------------------------------------------------------------------
       
   251 // CPresenceDataCache::RemoveMe
       
   252 // ---------------------------------------------------------------------------
       
   253 //
       
   254 void CPresenceDataCache::RemoveMe( CPresenceConfigurationItem* aChild )
       
   255     {
       
   256     RemoveMeFromArray< CPresenceConfigurationItem >( 
       
   257                                     iPresenceConfigurationItems, aChild );
       
   258     }
       
   259 
       
   260 // ---------------------------------------------------------------------------
       
   261 // CPresenceDataCache::RemoveMe
       
   262 // ---------------------------------------------------------------------------
       
   263 //
       
   264 void CPresenceDataCache::RemoveMe( CPresenceToEveryoneConfigurationItem* aChild )
       
   265     {
       
   266     RemoveMe< CPresenceToEveryoneConfigurationItem >( iPresenceToEveryoneConfigurationItem, aChild );
       
   267     }
       
   268 
       
   269 
       
   270 // ---------------------------------------------------------------------------
       
   271 // CPresenceDataCache::RemoveMe
       
   272 // ---------------------------------------------------------------------------
       
   273 //
       
   274 void CPresenceDataCache::RemoveMe( CGroupMembersPresenceConfigurationItem* aChild )
       
   275     {
       
   276     RemoveMeFromArray< CGroupMembersPresenceConfigurationItem >( 
       
   277                                     iGroupMembersPresenceConfigurationItems, aChild );
       
   278     }
       
   279 
       
   280 
       
   281 // ---------------------------------------------------------------------------
       
   282 // CPresenceDataCache::RemoveMe
       
   283 // ---------------------------------------------------------------------------
       
   284 //
       
   285 void CPresenceDataCache::RemoveMe( CPresentityGroupMembersPresenceSubscriptionItem* aChild )
       
   286     {
       
   287     RemoveMeFromArray< CPresentityGroupMembersPresenceSubscriptionItem >( 
       
   288                                     iGroupMembersPresenceSubscriptionItems, aChild );
       
   289     }
       
   290 
       
   291 // ---------------------------------------------------------------------------
       
   292 // CPresenceDataCache::RemoveMe
       
   293 // ---------------------------------------------------------------------------
       
   294 //
       
   295 void CPresenceDataCache::RemoveMe( CGrantRequestListSubscriptionItem* aChild )
       
   296     {
       
   297     RemoveMe< CGrantRequestListSubscriptionItem >( iGrantRequestListSubscriptionItem, aChild );
       
   298     }
       
   299 
       
   300 // ---------------------------------------------------------------------------
       
   301 // CPresenceDataCache::RemoveMe
       
   302 // ---------------------------------------------------------------------------
       
   303 //
       
   304 void CPresenceDataCache::RemoveMe( CBlockListSubscriptionItem* aChild )
       
   305     {
       
   306     RemoveMe< CBlockListSubscriptionItem >( iBlockListSubscriptionItem, aChild );
       
   307     }
       
   308 
       
   309 
       
   310 // ---------------------------------------------------------------------------
       
   311 // CPresenceDataCache::AddEventL
       
   312 // ---------------------------------------------------------------------------
       
   313 //
       
   314 void CPresenceDataCache::AddEventL( CXIMPApiEventBase& aEvent, MXIMPPscContext* aContext )
       
   315     {
       
   316     iHost.AddEventL( aEvent, aContext );
       
   317     }
       
   318 
       
   319 // ---------------------------------------------------------------------------
       
   320 // CPresenceDataCache::RegisterExpiringItemL
       
   321 // ---------------------------------------------------------------------------
       
   322 //
       
   323 void CPresenceDataCache::RegisterExpiringItemL( CXIMPSubscriptionItemBase* aExpireItem )
       
   324     {
       
   325     TInt index = iExpiringSubscriptionItems.Find( aExpireItem );
       
   326     if( index == KErrNotFound )
       
   327         {
       
   328         iExpiringSubscriptionItems.AppendL( aExpireItem );
       
   329         User::LeaveIfError( aExpireItem->Open() );
       
   330         }
       
   331     }
       
   332 
       
   333 // ---------------------------------------------------------------------------
       
   334 // CPresenceDataCache::UnregisterExpiringItemL
       
   335 // ---------------------------------------------------------------------------
       
   336 //
       
   337 void CPresenceDataCache::UnregisterExpiringItem( CXIMPSubscriptionItemBase* aExpireItem )
       
   338     {
       
   339     TInt index = iExpiringSubscriptionItems.Find( aExpireItem );
       
   340     if( index != KErrNotFound )
       
   341         {
       
   342         iExpiringSubscriptionItems[ index ]->Close();
       
   343         iExpiringSubscriptionItems.Remove( index );
       
   344         }
       
   345     }
       
   346 
       
   347 // ---------------------------------------------------------------------------
       
   348 // CPresenceDataCache::AddPresentityPresenceSubscriberL
       
   349 // ---------------------------------------------------------------------------
       
   350 //
       
   351 void CPresenceDataCache::AddPresentityPresenceSubscriberL( const CXIMPIdentityImp& aIdentity,
       
   352                                                           CXIMPSubscriptionItemBase* aItem,
       
   353                                                           CXIMPIdentityImp* aGroup )
       
   354     {
       
   355     TRACE(_L("CPresenceDataCache::AddPresentityPresenceSubscriberL() - begin" ) );
       
   356     
       
   357     CPresentityPresenceSubscriptionItem& presentityItem =
       
   358                                         PresentityPresenceSubscriptionItemLC( aIdentity );
       
   359                                        
       
   360     presentityItem.SetGroupIdentity( aGroup );
       
   361     
       
   362     TInt error( KErrNone );
       
   363     TRAP( error, presentityItem.AddSubscriberL( aItem ); );
       
   364     TRACE_1( _L("CPresenceDataCache::AddPresentityPresenceSubscriberL() - presentityItem.AddSubscriberL( aItem )  = %d"), error );
       
   365     
       
   366     CleanupStack::PopAndDestroy(); // presentityItem
       
   367     
       
   368     TRACE(_L("CPresenceDataCache::AddPresentityPresenceSubscriberL() - end" ) ); 
       
   369     }
       
   370 
       
   371 // ---------------------------------------------------------------------------
       
   372 // CPresenceDataCache::RemovePresentityPresenceSubscriberL
       
   373 // ---------------------------------------------------------------------------
       
   374 //
       
   375 void CPresenceDataCache::RemovePresentityPresenceSubscriber( const CXIMPIdentityImp& aIdentity,
       
   376                                                              CXIMPSubscriptionItemBase* aItem )
       
   377     {
       
   378     // PresentityPresenceSubscriptionItemLC does not leave if item already exists. It just
       
   379     // finds it. If item exists it does not matter if alloc fails or not because there will not
       
   380     // be any subscribers in that case. This one is safe to ignore.
       
   381     TRAP_IGNORE(
       
   382         CPresentityPresenceSubscriptionItem& presentityItem =
       
   383                                             PresentityPresenceSubscriptionItemLC( aIdentity );
       
   384         presentityItem.RemoveSubscriber( aItem );
       
   385         CleanupStack::PopAndDestroy(); // presentityItem
       
   386         )
       
   387     }
       
   388 
       
   389 // ---------------------------------------------------------------------------
       
   390 // CPresenceDataCache::AddPresentityPresenceConfiguratorL
       
   391 // ---------------------------------------------------------------------------
       
   392 //
       
   393 void CPresenceDataCache::AddPresenceConfiguratorL( const CXIMPIdentityImp& aIdentity,
       
   394                                                           CXIMPConfigurationItemBase* aItem )
       
   395     {
       
   396     CPresenceConfigurationItem& configItem = PresenceConfigurationItemLC( aIdentity );
       
   397     configItem.AddConfiguratorL( aItem );
       
   398     CleanupStack::PopAndDestroy(); // configItem
       
   399     }
       
   400 
       
   401 // ---------------------------------------------------------------------------
       
   402 // CPresenceDataCache::RemovePresentityPresenceConfigurator
       
   403 // ---------------------------------------------------------------------------
       
   404 //
       
   405 void CPresenceDataCache::RemovePresenceConfigurator( const CXIMPIdentityImp& aIdentity,
       
   406                                                              CXIMPConfigurationItemBase* aItem )
       
   407     {
       
   408     // PresentityPresenceSubscriptionItemLC does not leave if item already exists. It just
       
   409     // finds it. If item exists it does not matter if alloc fails or not because there will not
       
   410     // be any subscribers in that case. This one is safe to ignore.
       
   411     TRAP_IGNORE(
       
   412         CPresenceConfigurationItem& configItem = PresenceConfigurationItemLC( aIdentity );
       
   413         configItem.RemoveSubscriber( aItem );
       
   414         CleanupStack::PopAndDestroy(); // configItem
       
   415         )
       
   416     }
       
   417 
       
   418 
       
   419 // ---------------------------------------------------------------------------
       
   420 // CPresenceDataCache::ActivatePresentityPresenceSubscriberL
       
   421 // ---------------------------------------------------------------------------
       
   422 //
       
   423 void CPresenceDataCache::ActivatePresentityPresenceSubscriberL( const CXIMPIdentityImp& aIdentity,
       
   424                                                                CXIMPSubscriptionItemBase* aItem )
       
   425     {
       
   426     CPresentityPresenceSubscriptionItem& presentityItem =
       
   427                                         PresentityPresenceSubscriptionItemLC( aIdentity );
       
   428     presentityItem.ActivateSubscriberL( aItem );
       
   429     CleanupStack::PopAndDestroy(); // presentityItem
       
   430     }
       
   431 
       
   432 // ---------------------------------------------------------------------------
       
   433 // CPresenceDataCache::SynthesisePresentityPresenceSubscriptionEventToL
       
   434 // ---------------------------------------------------------------------------
       
   435 //
       
   436 void CPresenceDataCache::SynthesisePresentityPresenceSubscriptionEventToL(
       
   437                                             const CXIMPIdentityImp& aIdentity,
       
   438                                             MXIMPPscContext* aContext,
       
   439                                             TBool aForceEvent )
       
   440     {
       
   441     CPresentityPresenceSubscriptionItem& presentityItem =
       
   442                                         PresentityPresenceSubscriptionItemLC( aIdentity );
       
   443     presentityItem.SynthesiseSubscriptionEventToL( aContext, aForceEvent );
       
   444     CleanupStack::PopAndDestroy(); // presentityItem
       
   445     }
       
   446 
       
   447 // ---------------------------------------------------------------------------
       
   448 // CPresenceDataCache::ItemLC
       
   449 // ---------------------------------------------------------------------------
       
   450 //
       
   451 template< class Type >
       
   452 Type& CPresenceDataCache::ItemLC( Type*& aItem )
       
   453     {
       
   454     if( !aItem )
       
   455         {
       
   456         aItem = Type::NewLC( *this );
       
   457         }
       
   458     else
       
   459         {
       
   460         User::LeaveIfError( aItem->Open() );
       
   461         CleanupClosePushL( *aItem );
       
   462         }
       
   463     return *aItem;
       
   464     }
       
   465 
       
   466 // ---------------------------------------------------------------------------
       
   467 // CPresenceDataCache::ArrayItemLC
       
   468 // ---------------------------------------------------------------------------
       
   469 //
       
   470 template< class Type >
       
   471 Type& CPresenceDataCache::ArrayItemLC( RPointerArray< Type >& aItemArray, const CXIMPIdentityImp& aIdentity )
       
   472     {
       
   473     // Search for the subscription
       
   474     TInt pos = aItemArray.FindInOrder( aIdentity, Type::IdentityOrder );
       
   475     if( pos == KErrNotFound )
       
   476         {
       
   477         // Create a new subscription item.
       
   478         // Shared ownership of item. Item will inform this instance when it is deleted.
       
   479         Type* newItem = Type::NewLC( *this, aIdentity );
       
   480         
       
   481         TLinearOrder< Type > linearOrder( Type::Order );
       
   482         aItemArray.InsertInOrder( newItem, linearOrder );
       
   483         return *newItem;
       
   484         }
       
   485     else // Found
       
   486         {
       
   487         // Get the existing found item
       
   488         Type* item = aItemArray[ pos ];
       
   489         // Increase reference count for the existing
       
   490         User::LeaveIfError( item->Open() );
       
   491         CleanupClosePushL( *item );
       
   492         return *item;
       
   493         }    
       
   494     }
       
   495 
       
   496 // ---------------------------------------------------------------------------
       
   497 // CPresenceDataCache::ArrayItemLC
       
   498 // ---------------------------------------------------------------------------
       
   499 //
       
   500 template< class Type, class ContentType >
       
   501 Type& CPresenceDataCache::ArrayItemLC( RPointerArray< Type >& aItemArray, 
       
   502                                       RPointerArray< ContentType>& aContentArray,
       
   503                                       const CXIMPIdentityImp& aIdentity )
       
   504     {
       
   505     // Search for the subscription
       
   506     TInt pos = aItemArray.FindInOrder( aIdentity, Type::IdentityOrder );
       
   507     if( pos == KErrNotFound )
       
   508         {
       
   509         ContentType& contentItem = ArrayItemLC< ContentType >( aContentArray, aIdentity );
       
   510 
       
   511         // Create a new subscription item.
       
   512         // Shared ownership of item. Item will inform this instance when it is deleted.
       
   513         Type* newItem = Type::NewLC( *this, contentItem );
       
   514         CleanupStack::Pop(); // newItem
       
   515         CleanupStack::PopAndDestroy(); // contentItem NewItem has taken reference to ownership of contentItem
       
   516         CleanupClosePushL( *newItem ); // back to stack
       
   517         CXIMPIdentityImp* identity = TXIMPObjectCloner< CXIMPIdentityImp >::CloneL( aIdentity );
       
   518         // Set the identity
       
   519         newItem->SetIdentity( identity ); // Ownership is transferred to callee.
       
   520         
       
   521         TLinearOrder< Type > linearOrder( Type::Order );
       
   522         aItemArray.InsertInOrderL( newItem, linearOrder );
       
   523         return *newItem;
       
   524         }
       
   525     else // Found
       
   526         {
       
   527         // Get the existing found item
       
   528         Type* item = aItemArray[ pos ];
       
   529         // Increase reference count for the existing
       
   530         User::LeaveIfError( item->Open() );
       
   531         CleanupClosePushL( *item );
       
   532         return *item;
       
   533         }    
       
   534     }
       
   535 
       
   536     
       
   537 // ---------------------------------------------------------------------------
       
   538 // CPresenceDataCache::OwnPresenceSubscriptionItemLC
       
   539 // ---------------------------------------------------------------------------
       
   540 //
       
   541 EXPORT_C COwnPresenceSubscriptionItem& CPresenceDataCache::OwnPresenceSubscriptionItemLC()
       
   542     {
       
   543     return ItemLC< COwnPresenceSubscriptionItem >( iOwnSubscriptionItem );
       
   544     }
       
   545 
       
   546 // ---------------------------------------------------------------------------
       
   547 // CPresenceDataCache::GroupListSubscriptionItemLC
       
   548 // ---------------------------------------------------------------------------
       
   549 //
       
   550 EXPORT_C CGroupListSubscriptionItem& CPresenceDataCache::GroupListSubscriptionItemLC()
       
   551     {
       
   552     return ItemLC< CGroupListSubscriptionItem >( iGroupListSubscriptionItem );
       
   553     }
       
   554 
       
   555 // ---------------------------------------------------------------------------
       
   556 // CPresenceDataCache::WatcherListSubscriptionItemLC()
       
   557 // ---------------------------------------------------------------------------
       
   558 //
       
   559 EXPORT_C CWatcherListSubscriptionItem& CPresenceDataCache::WatcherListSubscriptionItemLC()
       
   560     {
       
   561     return ItemLC< CWatcherListSubscriptionItem >( iWatcherListSubscriptionItem );
       
   562     }
       
   563 
       
   564 
       
   565 // ---------------------------------------------------------------------------
       
   566 // CPresenceDataCache::GrantRequestListSubscriptionItemLC()
       
   567 // ---------------------------------------------------------------------------
       
   568 //
       
   569 EXPORT_C CGrantRequestListSubscriptionItem&
       
   570                 CPresenceDataCache::GrantRequestListSubscriptionItemLC()
       
   571     {
       
   572     return ItemLC< CGrantRequestListSubscriptionItem >( iGrantRequestListSubscriptionItem );
       
   573     }
       
   574 
       
   575 // ---------------------------------------------------------------------------
       
   576 // CPresenceDataCache::BlockListSubscriptionItemLC()
       
   577 // ---------------------------------------------------------------------------
       
   578 //
       
   579 EXPORT_C CBlockListSubscriptionItem&
       
   580                 CPresenceDataCache::BlockListSubscriptionItemLC()
       
   581     {
       
   582     return ItemLC< CBlockListSubscriptionItem >( iBlockListSubscriptionItem );
       
   583     }
       
   584 
       
   585 // ---------------------------------------------------------------------------
       
   586 // CPresenceDataCache::GroupContentSubscriptionItemLC
       
   587 // ---------------------------------------------------------------------------
       
   588 //
       
   589 EXPORT_C CGroupContentSubscriptionItem& CPresenceDataCache::GroupContentSubscriptionItemLC(
       
   590                                                                     const CXIMPIdentityImp& aGroupId )
       
   591     {
       
   592     return ArrayItemLC< CGroupContentSubscriptionItem >( iGroupContentSubscriptionItems, aGroupId );
       
   593     }
       
   594 
       
   595 // ---------------------------------------------------------------------------
       
   596 // CPresenceDataCache::GroupContentSubscriptionItemExistsL
       
   597 // ---------------------------------------------------------------------------
       
   598 //
       
   599 EXPORT_C TBool CPresenceDataCache::GroupContentSubscriptionItemExists(
       
   600                                             const CXIMPIdentityImp& aGroupId )
       
   601     {
       
   602     // Search for the subscription
       
   603     return iGroupContentSubscriptionItems.FindInOrder( 
       
   604             aGroupId, CGroupContentSubscriptionItem::IdentityOrder ) != KErrNotFound;
       
   605     }
       
   606 
       
   607 // ---------------------------------------------------------------------------
       
   608 // CPresenceDataCache::PresentityPresenceSubscriptionItemLC
       
   609 // ---------------------------------------------------------------------------
       
   610 //
       
   611 EXPORT_C CPresentityPresenceSubscriptionItem&
       
   612             CPresenceDataCache::PresentityPresenceSubscriptionItemLC(
       
   613                                             const CXIMPIdentityImp& aIdentity )
       
   614     {
       
   615     return ArrayItemLC<CPresentityPresenceSubscriptionItem>( iPresentityPresenceSubscriptionItems, 
       
   616                                                                     aIdentity );
       
   617     }
       
   618     
       
   619 // ---------------------------------------------------------------------------
       
   620 // CPresenceDataCache::PresentityGroupMembersPresenceSubscriptionItemLC
       
   621 // ---------------------------------------------------------------------------
       
   622 //
       
   623 EXPORT_C CPresentityGroupMembersPresenceSubscriptionItem&
       
   624             CPresenceDataCache::PresentityGroupMembersPresenceSubscriptionItemLC(
       
   625                                             const CXIMPIdentityImp& aIdentity )
       
   626     {
       
   627     return ArrayItemLC< CPresentityGroupMembersPresenceSubscriptionItem,
       
   628                         CGroupContentSubscriptionItem >( iGroupMembersPresenceSubscriptionItems,
       
   629                                                                 iGroupContentSubscriptionItems,
       
   630                                                                 aIdentity );
       
   631     }
       
   632 
       
   633 // ---------------------------------------------------------------------------
       
   634 // CPresenceDataCache::PresenceConfigurationItemLC
       
   635 // ---------------------------------------------------------------------------
       
   636 //
       
   637 EXPORT_C CPresenceConfigurationItem& CPresenceDataCache::PresenceConfigurationItemLC(
       
   638                                                              const CXIMPIdentityImp& aIdentity )
       
   639     {
       
   640     return ArrayItemLC< CPresenceConfigurationItem >( iPresenceConfigurationItems, aIdentity );
       
   641     }
       
   642 
       
   643 // ---------------------------------------------------------------------------
       
   644 // CPresenceDataCache::PresenceToEveryoneConfigurationItemLC
       
   645 // ---------------------------------------------------------------------------
       
   646 //
       
   647 EXPORT_C CPresenceToEveryoneConfigurationItem& 
       
   648                     CPresenceDataCache::PresenceToEveryoneConfigurationItemLC()
       
   649     {
       
   650     return ItemLC< CPresenceToEveryoneConfigurationItem >( iPresenceToEveryoneConfigurationItem );
       
   651     }
       
   652 
       
   653 // ---------------------------------------------------------------------------
       
   654 // CPresenceDataCache::PresenceConfigurationItemLC
       
   655 // ---------------------------------------------------------------------------
       
   656 //
       
   657 EXPORT_C CGroupMembersPresenceConfigurationItem&
       
   658      CPresenceDataCache::GroupMembersPresenceConfigurationItemLC( const CXIMPIdentityImp& aGroupId )
       
   659     {
       
   660     return ArrayItemLC< CGroupMembersPresenceConfigurationItem,
       
   661                         CGroupContentSubscriptionItem >( iGroupMembersPresenceConfigurationItems,
       
   662                                                                 iGroupContentSubscriptionItems,
       
   663                                                                 aGroupId );
       
   664     }
       
   665 
       
   666 // ---------------------------------------------------------------------------
       
   667 // CPresenceDataCache::CleanResources
       
   668 // ---------------------------------------------------------------------------
       
   669 //
       
   670 EXPORT_C void CPresenceDataCache::CleanResources( MXIMPPscContext& aContext )
       
   671     {
       
   672     TRACE(_L("CPresenceDataCache::CleanResources()" ) ); 
       
   673     if( iOwnSubscriptionItem )
       
   674         {
       
   675         iOwnSubscriptionItem->RemoveSubscriber( &aContext );
       
   676         }
       
   677     if( iGroupListSubscriptionItem )
       
   678         {
       
   679         iGroupListSubscriptionItem->RemoveSubscriber( &aContext );
       
   680         }
       
   681 
       
   682     // Groupmember items have to be cleaned before contentitems.
       
   683     TInt count = iGroupMembersPresenceSubscriptionItems.Count();
       
   684     for( TInt a = 0; a < count; ++a )
       
   685         {
       
   686         iGroupMembersPresenceSubscriptionItems[ 0 ]->RemoveSubscriber( &aContext );
       
   687         }
       
   688     count = iGroupMembersPresenceConfigurationItems.Count();
       
   689     for( TInt a = 0; a < count; ++a )
       
   690         {
       
   691         iGroupMembersPresenceConfigurationItems[ 0 ]->RemoveConfigurator( &aContext );
       
   692         }    
       
   693 
       
   694     count = iPresentityPresenceSubscriptionItems.Count();
       
   695     for( TInt a = 0; a < count; ++a )
       
   696         {
       
   697         iPresentityPresenceSubscriptionItems[ 0 ]->RemoveSubscriber( &aContext );
       
   698         }
       
   699 
       
   700     count = iGroupContentSubscriptionItems.Count();
       
   701     for( TInt a = 0; a < count; ++a )
       
   702         {
       
   703         iGroupContentSubscriptionItems[ 0 ]->RemoveSubscriber( &aContext );
       
   704         }
       
   705         
       
   706     if( iWatcherListSubscriptionItem )
       
   707         {
       
   708         iWatcherListSubscriptionItem->RemoveSubscriber( &aContext );
       
   709         }
       
   710 
       
   711     if( iGrantRequestListSubscriptionItem )
       
   712         {
       
   713         iGrantRequestListSubscriptionItem->RemoveSubscriber( &aContext );
       
   714         }
       
   715         
       
   716     count = iGroupMembersPresenceSubscriptionItems.Count();
       
   717     for( TInt a = 0; a < count; ++a )
       
   718         {
       
   719         iGroupMembersPresenceSubscriptionItems[ 0 ]->RemoveSubscriber( &aContext );
       
   720         }
       
   721 
       
   722     if( iBlockListSubscriptionItem )
       
   723         {
       
   724         iBlockListSubscriptionItem->RemoveSubscriber( &aContext );
       
   725         }
       
   726 
       
   727     count = iExpiringSubscriptionItems.Count();
       
   728     for( TInt a = 0; a < count; ++a )
       
   729         {
       
   730         iExpiringSubscriptionItems[ 0 ]->RemoveSubscriber( &aContext );
       
   731         }
       
   732 
       
   733     count = iPresenceConfigurationItems.Count();
       
   734     for( TInt a = 0; a < count; ++a )
       
   735         {
       
   736         iPresenceConfigurationItems[ 0 ]->RemoveConfigurator( &aContext );
       
   737         }
       
   738 
       
   739     if( iPresenceToEveryoneConfigurationItem )
       
   740         {
       
   741         iPresenceToEveryoneConfigurationItem->RemoveConfigurator( &aContext );
       
   742         }
       
   743     TRACE(_L("CPresenceDataCache::CleanResources done()" ) );
       
   744     }
       
   745 
       
   746 // ---------------------------------------------------------------------------
       
   747 // CPresenceDataCache::CleanExpired
       
   748 // ---------------------------------------------------------------------------
       
   749 //
       
   750 EXPORT_C void CPresenceDataCache::CleanExpired()
       
   751     {
       
   752     TRACE(_L("CPresenceDataCache::CleanExpired()" ) );
       
   753     // Clean those which were expired.
       
   754     TInt count = iExpiringSubscriptionItems.Count();
       
   755     for( TInt a = count - 1; a >= 0; --a )
       
   756         {
       
   757         if( iExpiringSubscriptionItems[ a ]->CleanIfExpired() )
       
   758             {
       
   759             iExpiringSubscriptionItems[ a ]->Close();
       
   760             iExpiringSubscriptionItems.Remove( a );
       
   761             }
       
   762         }
       
   763     }
       
   764 
       
   765 // ---------------------------------------------------------------------------
       
   766 // CPresenceDataCache::PackGroupIdLC
       
   767 // ---------------------------------------------------------------------------
       
   768 //
       
   769 HBufC8* CPresenceDataCache::PackGroupIdLC( const MXIMPIdentity& aGroupId )
       
   770     {
       
   771     const CXIMPIdentityImp* groupId =
       
   772         TXIMPGetImpClassOrPanic< const CXIMPIdentityImp >::From( aGroupId );
       
   773     
       
   774     HBufC8* packedGroupId = TXIMPObjectPacker< const CXIMPIdentityImp >::PackL( *groupId );
       
   775     CleanupStack::PushL( packedGroupId );
       
   776     return packedGroupId;
       
   777     }
       
   778 
       
   779 // ---------------------------------------------------------------------------
       
   780 // CPresenceDataCache::DataSubscriptionState
       
   781 // ---------------------------------------------------------------------------
       
   782 //
       
   783 template< class Type >
       
   784 const MXIMPDataSubscriptionState& CPresenceDataCache::DataSubscriptionState( Type* aItem ) const
       
   785     {
       
   786     if( aItem )
       
   787         {
       
   788         return aItem->DataSubscriptionState();
       
   789         }
       
   790     return *iEmptySubscriptionState;
       
   791     }
       
   792 
       
   793 // ---------------------------------------------------------------------------
       
   794 // CPresenceDataCache::DataSubscriptionState
       
   795 // ---------------------------------------------------------------------------
       
   796 //
       
   797 template< class Type >
       
   798 const MXIMPDataSubscriptionState& CPresenceDataCache::DataSubscriptionState( 
       
   799                 const RPointerArray< Type >& aItemArray, const MXIMPIdentity& aIdentity ) const
       
   800     {    
       
   801     const CXIMPIdentityImp* identity =
       
   802         TXIMPGetImpClassOrPanic< const CXIMPIdentityImp >::From( aIdentity );
       
   803     TInt index = aItemArray.FindInOrder( *identity, Type::IdentityOrder );
       
   804     if( index >=0 )
       
   805         {
       
   806         return aItemArray[ index ]->DataSubscriptionState();
       
   807         }
       
   808     return *iEmptySubscriptionState;    
       
   809     }
       
   810 
       
   811 
       
   812 // ---------------------------------------------------------------------------
       
   813 // CPresenceDataCache::OwnPresenceDataSubscriptionState
       
   814 // ---------------------------------------------------------------------------
       
   815 //
       
   816 EXPORT_C const MXIMPDataSubscriptionState& CPresenceDataCache::OwnPresenceDataSubscriptionState() const
       
   817     {
       
   818     return DataSubscriptionState< COwnPresenceSubscriptionItem >( iOwnSubscriptionItem );
       
   819     }
       
   820     
       
   821 // ---------------------------------------------------------------------------
       
   822 // CPresenceDataCache::PresentityGroupDataSubscriptionState
       
   823 // ---------------------------------------------------------------------------
       
   824 //
       
   825 EXPORT_C const MXIMPDataSubscriptionState& CPresenceDataCache::PresentityGroupListDataSubscriptionState() const
       
   826     {
       
   827     return DataSubscriptionState< CGroupListSubscriptionItem >( iGroupListSubscriptionItem );
       
   828     }
       
   829     
       
   830 // ---------------------------------------------------------------------------
       
   831 // CPresenceDataCache::PresentityGroupMemberDataSubscriptionState
       
   832 // ---------------------------------------------------------------------------
       
   833 //
       
   834 EXPORT_C const MXIMPDataSubscriptionState& CPresenceDataCache::PresentityGroupContentDataSubscriptionState( 
       
   835             const MXIMPIdentity& aGroupId ) const
       
   836     {
       
   837     return DataSubscriptionState< CGroupContentSubscriptionItem >( iGroupContentSubscriptionItems, 
       
   838                                                                           aGroupId );
       
   839     }
       
   840     
       
   841 // ---------------------------------------------------------------------------
       
   842 // CPresenceDataCache::PresentityPresenceDataSubscriptionState
       
   843 // ---------------------------------------------------------------------------
       
   844 //
       
   845 EXPORT_C const MXIMPDataSubscriptionState& CPresenceDataCache::PresentityPresenceDataSubscriptionState( 
       
   846             const MXIMPIdentity& aPresentityId ) const
       
   847     {
       
   848     return DataSubscriptionState< CPresentityPresenceSubscriptionItem >( iPresentityPresenceSubscriptionItems, 
       
   849                                                                                 aPresentityId );
       
   850     }
       
   851     
       
   852 // ---------------------------------------------------------------------------
       
   853 // CPresenceDataCache::PresenceWatcherListDataSubscriptionState
       
   854 // ---------------------------------------------------------------------------
       
   855 //
       
   856 EXPORT_C const MXIMPDataSubscriptionState& CPresenceDataCache::PresenceWatcherListDataSubscriptionState() const
       
   857     {    
       
   858     return DataSubscriptionState< CWatcherListSubscriptionItem >( iWatcherListSubscriptionItem );
       
   859     }
       
   860     
       
   861 // ---------------------------------------------------------------------------
       
   862 // CPresenceDataCache::PresenceBlockDataSubscriptionState
       
   863 // ---------------------------------------------------------------------------
       
   864 //
       
   865 EXPORT_C const MXIMPDataSubscriptionState& CPresenceDataCache::PresenceBlockDataSubscriptionState() const
       
   866     {
       
   867     return DataSubscriptionState< CBlockListSubscriptionItem >( iBlockListSubscriptionItem );
       
   868     }
       
   869     
       
   870 // ---------------------------------------------------------------------------
       
   871 // CPresenceDataCache::PresenceGrantRequestDataSubscriptionState
       
   872 // ---------------------------------------------------------------------------
       
   873 //
       
   874 EXPORT_C const MXIMPDataSubscriptionState& CPresenceDataCache::PresenceGrantRequestDataSubscriptionState() const
       
   875     {    
       
   876     return DataSubscriptionState< CGrantRequestListSubscriptionItem >( iGrantRequestListSubscriptionItem );
       
   877     }
       
   878 
       
   879 // ---------------------------------------------------------------------------
       
   880 // CPresenceDataCache::ProtocolID
       
   881 // ---------------------------------------------------------------------------
       
   882 //
       
   883 TInt32 CPresenceDataCache::ProtocolUID()
       
   884 	{
       
   885 	return iHost.ProtocolUID();
       
   886 	}
       
   887 
       
   888 // ---------------------------------------------------------------------------
       
   889 // CPresenceDataCache::ProtocolID
       
   890 // ---------------------------------------------------------------------------
       
   891 //
       
   892 MPresenceCacheWriter* CPresenceDataCache::ExternalCache()
       
   893 	{
       
   894 	return iExtPresenceCache;
       
   895 	}
       
   896 
       
   897 TAny* CPresenceDataCache::GetInterface(TInt aInterfaceId)  
       
   898 	{
       
   899 		if(aInterfaceId != PRESENCE_ITEM_PARENT)
       
   900 			{
       
   901 			User::Panic( _L("CXIMPPresenceCache"), KErrExtensionNotSupported );
       
   902 			}
       
   903 
       
   904 		return static_cast<MXIMPItemParent*>(this) ;	
       
   905 
       
   906 	}
       
   907 
       
   908 // End of file