wvuing/wvuistorage/src/CCAStorageManager.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2002-2005 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:  Storage manager implementation
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "CCAStorageManager.h"
       
    21 #include "CCAStorage.h"
       
    22 #include "CCARefreshTimer.h"
       
    23 #include "CCAContactList.h"
       
    24 #include "CCAGroup.h"
       
    25 #include "CCAContact.h"
       
    26 #include "CCAContactSorter.h"
       
    27 
       
    28 #include "CAUtils.h"
       
    29 
       
    30 #include "ChatDebugPrint.h"
       
    31 #include "chatdebugassert.h"
       
    32 
       
    33 #include <PEngPresenceEngineConsts2.h> // for KPEngUserOwnPresenceId
       
    34 
       
    35 // CONSTANTS
       
    36 // Own device type, default to mobile
       
    37 const TStorageManagerGlobals::TClientType KOwnDeviceType =
       
    38     TStorageManagerGlobals::EMobile;
       
    39 
       
    40 // ============================ MEMBER FUNCTIONS ===============================
       
    41 
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CCAStorageManager::InstanceL
       
    45 // Factory method to return the singleton instance of this class,
       
    46 // used by CCAStorageManagerFactory
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CCAStorageManager* CCAStorageManager::InstanceL()
       
    50     {
       
    51     CCAStorageManager *sm = NULL;
       
    52     sm = static_cast<CCAStorageManager*>( Dll::Tls() );
       
    53     if ( ! sm )
       
    54         {
       
    55         // no existing instance, create a new one
       
    56         CCAStorageManager *manager = CCAStorageManager::NewL();
       
    57         sm = manager;
       
    58         CleanupStack::PushL( manager );
       
    59         User::LeaveIfError( Dll::SetTls( static_cast<TAny*>( sm ) ) );
       
    60         CleanupStack::Pop( manager );
       
    61         }
       
    62 
       
    63     return sm;
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CCAStorageManager::ReleaseL
       
    68 // Factory method to release the singleton instance of this class,
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 TInt CCAStorageManager::ReleaseL()
       
    72     {
       
    73     // about to unload, so kill the singleton instance
       
    74     if ( Dll::Tls() )
       
    75         {
       
    76         CCAStorageManager *storage = NULL;
       
    77         storage = static_cast<CCAStorageManager*>( Dll::Tls() );
       
    78         if ( storage )
       
    79             {
       
    80             delete storage;
       
    81             storage = NULL;
       
    82             }
       
    83         }
       
    84     // Ignore error
       
    85     Dll::SetTls( NULL );
       
    86     return KErrNone;
       
    87     }
       
    88 
       
    89 // panic handler
       
    90 GLDEF_C void Panic( TCAStoragePanics aPanic )
       
    91     {
       
    92     User::Panic( KChatStoragePanicString, aPanic );
       
    93     }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // CCAStorageManager::CCAStorageManager
       
    97 // C++ default constructor can NOT contain any code, that
       
    98 // might leave.
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 CCAStorageManager::CCAStorageManager() :
       
   102         iNotifyUponSave( ETrue ),
       
   103         iRefreshStartCounter( 0 ),
       
   104         iShowNickname( ETrue ),
       
   105         iGroupDeleted( EFalse )
       
   106     {
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CCAStorageManager::ConstructL
       
   111 // Symbian 2nd phase constructor can leave.
       
   112 // The constructor also does synchronizing with Phone Book by removing the
       
   113 // deleted elements from the persistent storage
       
   114 // -----------------------------------------------------------------------------
       
   115 //
       
   116 void CCAStorageManager::ConstructL()
       
   117     {
       
   118     CHAT_DP_TXT( "CCAStorageManager::ConstructL" );
       
   119     CHAT_DP_TXT( "CCAStorageManager::Creating Storage" );
       
   120     iStorage = CCAStorage::NewL();
       
   121 
       
   122     iRefreshTimer = CCARefreshTimer::NewL( this );
       
   123 
       
   124     // create sorter for contacts
       
   125     iContactSorter = CCAContactSorter::NewL();
       
   126 
       
   127     // handle Groups
       
   128     CHAT_DP_TXT( "CCAStorageManager::ConstructL - Synching group data with \
       
   129                   persistent storage" );
       
   130     SynchronizeGroupsL();
       
   131 
       
   132     iOwnStatus = CCAContact::NewL( this, this );
       
   133     iOwnStatus->SetClientType( KOwnDeviceType );
       
   134 
       
   135     CHAT_DP_TXT( "CCAStorageManager::ConstructL - Done" );
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // CCAStorageManager::SynchronizeGroups
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 void CCAStorageManager::SynchronizeGroupsL()
       
   143     {
       
   144     // read the groups, if any
       
   145     TBool fetching( ETrue );
       
   146     for ( TInt i = 0; fetching ; i++ )
       
   147         {
       
   148         CCAGroup* group = CCAGroup::NewL( iStorage, this );
       
   149         CleanupStack::PushL( group );
       
   150 
       
   151         TInt storStatus( KErrNone );
       
   152         TInt err( KErrNone );
       
   153 
       
   154         if ( 0 == i )
       
   155             {
       
   156             // first iteration
       
   157             CHAT_DP_TXT( "CCAStorageManager::SynchronizeGroupsL - Loading \
       
   158                           first group" );
       
   159             // Load Leaves if storage has no more groups
       
   160             TRAP( err, storStatus = iStorage->LoadFirstL( group ) );
       
   161             }
       
   162         else
       
   163             {
       
   164             // subsequent iterations
       
   165             CHAT_DP_TXT( "CCAStorageManager::SynchronizeGroupsL - Loading next \
       
   166                           group" );
       
   167             // Load Leaves if storage has no more groups
       
   168             TRAP( err, storStatus = iStorage->LoadNextL( group ) );
       
   169             }
       
   170 
       
   171         if ( ( KErrNotFound == storStatus ) || ( KErrNone != err ) )
       
   172             {
       
   173             // no more groups, that's all
       
   174             CHAT_DP_TXT( "CCAStorageManager::SynchronizeGroupsL - No groups in \
       
   175                           persistent storage" );
       
   176             fetching = EFalse;
       
   177             }
       
   178         else
       
   179             {
       
   180             // found some group, add to our list
       
   181             CHAT_DP_TXT( "CCAStorageManager::SynchronizeGroupsL - Found group, \
       
   182                           adding to list" );
       
   183 
       
   184             // valid group, add to our list
       
   185             group->SetStorageType( TStorageManagerGlobals::EStoragePersistent );
       
   186             TInt status = iGroupList.Append( group );
       
   187             User::LeaveIfError( status );
       
   188             }
       
   189 
       
   190         if ( fetching == EFalse )
       
   191             {
       
   192             // all done
       
   193             CleanupStack::PopAndDestroy( group ); // group
       
   194             }
       
   195         else
       
   196             {
       
   197             // going to next iteration
       
   198             CleanupStack::Pop( group ); // group
       
   199             }
       
   200         }
       
   201 
       
   202     CHAT_DP_TXT( "CCAStorageManager::SynchronizeGroupsL - Done" );
       
   203     }
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // CCAStorageManager::NewL
       
   207 // Two-phased constructor.
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 EXPORT_C CCAStorageManager* CCAStorageManager::NewL()
       
   211     {
       
   212     CCAStorageManager* self = new( ELeave ) CCAStorageManager;
       
   213     CleanupStack::PushL( self );
       
   214     self->ConstructL();
       
   215     CleanupStack::Pop( self );
       
   216     return self;
       
   217     }
       
   218 
       
   219 // Destructor
       
   220 CCAStorageManager::~CCAStorageManager()
       
   221     {
       
   222     CHAT_DP_TXT( "CCAStorageManager::~CCAStorageManager" );
       
   223 
       
   224     delete iRefreshTimer;
       
   225     delete iLatestContactToRefresh;
       
   226 
       
   227     iContactObservers.Reset();
       
   228     iContactObservers.Close();
       
   229 
       
   230     iGroupObservers.Reset();
       
   231     iGroupObservers.Close();
       
   232 
       
   233     iContactList.ResetAndDestroy();
       
   234     iGroupList.ResetAndDestroy();
       
   235 
       
   236     delete iStorage;
       
   237 
       
   238     delete iContactSorter;
       
   239 
       
   240     delete iOwnStatus;
       
   241 
       
   242     CHAT_DP_TXT( "CCAStorageManager::~CCAStorageManager - I am dead" );
       
   243     }
       
   244 
       
   245 // -----------------------------------------------------------------------------
       
   246 // CCAStorageManager::SendObserverEventL
       
   247 // If the changed/deleted contact is in our list, the observers are notified.
       
   248 // Otherwise the observers are not notified.
       
   249 // -----------------------------------------------------------------------------
       
   250 void CCAStorageManager::SendObserverEvent(
       
   251     TStorageManagerGlobals::TCAObserverEventType aType,
       
   252     MCAStoredContact* aContact,
       
   253     const TDesC& aContactId,
       
   254     MCAContactList* aList /*= NULL*/,
       
   255     TBool aUserIdChanged /*= EFalse*/ )
       
   256     {
       
   257     CHAT_DP_TXT( "CCAStorageManager::SendObserverEventL (contact)" );
       
   258     CHAT_DP_TXT( "CCAStorageManager::SendObserverEventL - Notifying \
       
   259                   MCAStoredContactsObserver observers" );
       
   260 
       
   261     if ( !iContactListLock )
       
   262         {
       
   263         switch ( aType )
       
   264             {
       
   265             case TStorageManagerGlobals::EStorageEventPostChange: // flowtrough
       
   266             case TStorageManagerGlobals::EStorageEventOwnStatusChange:
       
   267                 {
       
   268                 NotifyAllObserversWithDelay( aType, aContact, aContactId, aList,
       
   269                                              aUserIdChanged );
       
   270                 break;
       
   271                 }
       
   272             default:
       
   273                 {
       
   274                 NotifyAllObservers( aType, aContact, aContactId, aList, aUserIdChanged );
       
   275                 break;
       
   276                 }
       
   277             }
       
   278         }
       
   279     }
       
   280 
       
   281 // -----------------------------------------------------------------------------
       
   282 // CCAStorageManager::NotifyAllObservers
       
   283 // -----------------------------------------------------------------------------
       
   284 //
       
   285 void CCAStorageManager::NotifyAllObservers( TStorageManagerGlobals::TCAObserverEventType aType,
       
   286                                             MCAStoredContact* aContact, const TDesC& aContactId,
       
   287                                             MCAContactList* aList,
       
   288                                             TBool aUserIdChanged )
       
   289     {
       
   290     CHAT_DP_FUNC_ENTER( "NotifyAllObservers" );
       
   291 
       
   292     // tell all of the observers about the event.
       
   293     // the amount of observers might change during the loop,
       
   294     // so the count MUST be taken in every round
       
   295     for ( TInt i = 0; i < iContactObservers.Count(); i++ )
       
   296         {
       
   297         MCAStoredContactsObserver* obs = iContactObservers[i];
       
   298         switch ( aType )
       
   299             {
       
   300             case TStorageManagerGlobals::EStorageEventPreChange:
       
   301             case TStorageManagerGlobals::EStorageEventPostChange:
       
   302                 {
       
   303                 obs->HandleChange( aList, aContact, aType, aUserIdChanged );
       
   304                 break;
       
   305                 }
       
   306             case TStorageManagerGlobals::EStorageEventContactDelete:
       
   307                 {
       
   308                 obs->HandleContactDelete( aContactId );
       
   309                 break;
       
   310                 }
       
   311             case TStorageManagerGlobals::EStorageEventContactAddition:
       
   312                 {
       
   313                 if ( aList )
       
   314                     {
       
   315                     obs->HandleAddition( *aList, *aContact );
       
   316                     }
       
   317                 break;
       
   318                 }
       
   319             case TStorageManagerGlobals::EStorageEventOwnStatusChange:
       
   320             case TStorageManagerGlobals::EStorageEventMultipleChanges:
       
   321             case TStorageManagerGlobals::EStorageEventListAddition:
       
   322             case TStorageManagerGlobals::EStorageEventListDelete:
       
   323             case TStorageManagerGlobals::EStorageEventListChanged:
       
   324                 {
       
   325                 obs->HandleChange( aList, NULL, aType, EFalse );
       
   326                 break;
       
   327                 }
       
   328             default:
       
   329                 {
       
   330                 break;
       
   331                 }
       
   332             }
       
   333         }
       
   334 
       
   335     CHAT_DP_FUNC_DONE( "NotifyAllObservers" );
       
   336     }
       
   337 
       
   338 // -----------------------------------------------------------------------------
       
   339 // CCAStorageManager::NotifyAllObserversWithDelay
       
   340 // -----------------------------------------------------------------------------
       
   341 //
       
   342 void CCAStorageManager::NotifyAllObserversWithDelay(
       
   343     TStorageManagerGlobals::TCAObserverEventType aType,
       
   344     MCAStoredContact* aContact, const TDesC& aContactId,
       
   345     MCAContactList* aList,
       
   346     TBool aUserIdChanged )
       
   347     {
       
   348     TRAPD( err, DoNotifyAllObserversWithDelayL( aType, aContact, aContactId,
       
   349                                                 aList, aUserIdChanged ) );
       
   350     if ( err )
       
   351         {
       
   352         CActiveScheduler::Current()->Error( err );
       
   353         }
       
   354     }
       
   355 
       
   356 // -----------------------------------------------------------------------------
       
   357 // CCAStorageManager::DoNotifyAllObserversWithDelayL
       
   358 // -----------------------------------------------------------------------------
       
   359 //
       
   360 void CCAStorageManager::DoNotifyAllObserversWithDelayL(
       
   361     TStorageManagerGlobals::TCAObserverEventType aType,
       
   362     MCAStoredContact* /*aContact*/, const TDesC& aContactId,
       
   363     MCAContactList* aList, TBool aUserIdChanged )
       
   364     {
       
   365     HBufC* tmpID = aContactId.AllocL();
       
   366     delete iLatestContactToRefresh;
       
   367     iLatestContactToRefresh = tmpID;
       
   368     iLatestContactList = aList;
       
   369     tmpID = NULL;
       
   370     iRefreshEventType = aType;
       
   371     iRefreshUserIdChanged = aUserIdChanged;
       
   372     ++iRefreshStartCounter;
       
   373     if ( iRefreshTimer->IsActive() )
       
   374         {
       
   375         iRefreshTimer->Cancel();
       
   376         }
       
   377     iRefreshTimer->Start();
       
   378     }
       
   379 
       
   380 // -----------------------------------------------------------------------------
       
   381 // CCAStorageManager::SendObserverEventL (overridden for group)
       
   382 // -----------------------------------------------------------------------------
       
   383 //
       
   384 void CCAStorageManager::SendObserverEvent(
       
   385     TStorageManagerGlobals::TCAObserverEventType aType,
       
   386     const TDesC& aParameter )
       
   387     {
       
   388     CHAT_DP_TXT( "CCAStorageManager::SendObserverEventL (group)" );
       
   389 
       
   390     TInt pos( FindGroupIndexByGroupId( aParameter ) );
       
   391     if ( KErrNotFound == pos )
       
   392         {
       
   393         return;     // not found in the list
       
   394         }
       
   395 
       
   396     CHAT_DP_TXT( "CCAStorageManager::SendObserverEventL - Notifying \
       
   397                   MCAStoredGroupsObserver observers" );
       
   398 
       
   399     // tell all of the observers about the event
       
   400     const TInt count( iGroupObservers.Count() );
       
   401     for ( TInt i = 0; i < count; i++ )
       
   402         {
       
   403         MCAStoredGroupsObserver* obs = iGroupObservers[i];
       
   404 
       
   405         switch ( aType )
       
   406             {
       
   407             case TStorageManagerGlobals::EStorageEventGroupChange:
       
   408                 {
       
   409                 obs->HandleChange( aParameter );
       
   410                 break;
       
   411                 }
       
   412 
       
   413             case TStorageManagerGlobals::EStorageEventContactDelete:
       
   414                 {
       
   415                 obs->HandleDelete( aParameter );
       
   416                 break;
       
   417                 }
       
   418 
       
   419             case TStorageManagerGlobals::EStorageEventContactAddition:
       
   420                 {
       
   421                 obs->HandleAddition( aParameter );
       
   422                 break;
       
   423                 }
       
   424             default:
       
   425                 {
       
   426                 break;
       
   427                 }
       
   428             }
       
   429         }
       
   430     }
       
   431 
       
   432 // -----------------------------------------------------------------------------
       
   433 // CCAStorageManager::AddObserverL
       
   434 // From MCAStoredContacts
       
   435 // -----------------------------------------------------------------------------
       
   436 //
       
   437 void CCAStorageManager::AddObserverL( MCAStoredContactsObserver*
       
   438                                       aObserver )
       
   439     {
       
   440     __ASSERT_ALWAYS( aObserver, Panic( EObserverIsNull ) );
       
   441 
       
   442     TInt status( iContactObservers.Append( aObserver ) );
       
   443     User::LeaveIfError( status );
       
   444     }
       
   445 
       
   446 // -----------------------------------------------------------------------------
       
   447 // CCAStorageManager::RemoveObserverL
       
   448 // From MCAStoredContacts
       
   449 // -----------------------------------------------------------------------------
       
   450 void CCAStorageManager::RemoveObserver( MCAStoredContactsObserver*
       
   451                                         aObserver )
       
   452     {
       
   453     __ASSERT_ALWAYS( aObserver, Panic( EObserverIsNull ) );
       
   454 
       
   455     const TInt status( iContactObservers.Find( aObserver ) );
       
   456     if ( status != KErrNotFound )
       
   457         {
       
   458         iContactObservers.Remove( status );
       
   459         iContactObservers.Compress();
       
   460         }
       
   461     }
       
   462 
       
   463 // -----------------------------------------------------------------------------
       
   464 // CCAStorageManager::AddObserverL
       
   465 // From MCAStoredGroups
       
   466 // -----------------------------------------------------------------------------
       
   467 //
       
   468 void CCAStorageManager::AddObserverL( MCAStoredGroupsObserver*
       
   469                                       aObserver )
       
   470     {
       
   471     __ASSERT_ALWAYS( aObserver, Panic( EObserverIsNull ) );
       
   472 
       
   473     TInt status( iGroupObservers.Append( aObserver ) );
       
   474     User::LeaveIfError( status );
       
   475     }
       
   476 
       
   477 // -----------------------------------------------------------------------------
       
   478 // CCAStorageManager::RemoveObserverL
       
   479 // From MCAStoredGroups
       
   480 // -----------------------------------------------------------------------------
       
   481 //
       
   482 void CCAStorageManager::RemoveObserver( MCAStoredGroupsObserver*
       
   483                                         aObserver )
       
   484     {
       
   485     __ASSERT_ALWAYS( aObserver, Panic( EObserverIsNull ) );
       
   486 
       
   487     const TInt status( iGroupObservers.Find( aObserver ) );
       
   488     if ( status != KErrNotFound )
       
   489         {
       
   490         iGroupObservers.Remove( status );
       
   491         iGroupObservers.Compress();
       
   492         }
       
   493     }
       
   494 
       
   495 // -----------------------------------------------------------------------------
       
   496 // CCAStorageManager::AddContactListIdL
       
   497 // -----------------------------------------------------------------------------
       
   498 //
       
   499 MCAContactList* CCAStorageManager::AddContactListIdL(
       
   500     const TDesC& aContactListId,
       
   501     const TDesC& aDisplayName,
       
   502     TBool aNotify )
       
   503     {
       
   504     CCAContactList* contactList = CCAContactList::NewLC( *this,
       
   505                                                          *iContactSorter,
       
   506                                                          *this,
       
   507                                                          aContactListId,
       
   508                                                          aDisplayName );
       
   509     TIdentityRelation< CCAContactList > findBy(
       
   510         CCAStorageManager::ContactListFindByContactId );
       
   511     TInt indexOfList( iContactList.Find( contactList, findBy ) );
       
   512     if ( indexOfList == KErrNotFound )
       
   513         {
       
   514         TLinearOrder< CCAContactList > order(
       
   515             CCAStorageManager::ContactListOrderByDisplayName );
       
   516         iContactList.InsertInOrderAllowRepeats( contactList, order );
       
   517         CleanupStack::Pop( contactList );
       
   518         }
       
   519     else
       
   520         {
       
   521         CleanupStack::PopAndDestroy( contactList );
       
   522         contactList = iContactList[ indexOfList ];
       
   523         }
       
   524     // Inform observers
       
   525     if ( aNotify )
       
   526         {
       
   527         SendObserverEvent( TStorageManagerGlobals::EStorageEventListAddition,
       
   528                            NULL,
       
   529                            KNullDesC, contactList, EFalse );
       
   530         }
       
   531     return contactList;
       
   532     }
       
   533 
       
   534 // -----------------------------------------------------------------------------
       
   535 // CCAStorageManager::SetContactListCollapsed
       
   536 // -----------------------------------------------------------------------------
       
   537 //
       
   538 void CCAStorageManager::SetContactListCollapsed( const TDesC& aContactListId,
       
   539                                                  TBool aCollapsed )
       
   540     {
       
   541     TInt pos( FindContactListById( aContactListId ) );
       
   542     if ( pos >= 0 )
       
   543         {
       
   544         MCAContactList& list = *iContactList[ pos ];
       
   545         list.SetCollapsed( aCollapsed );
       
   546         }
       
   547     }
       
   548 
       
   549 // -----------------------------------------------------------------------------
       
   550 // CCAStorageManager::RemoveContactList
       
   551 // -----------------------------------------------------------------------------
       
   552 //
       
   553 void CCAStorageManager::RemoveContactList( const TDesC& aContactListId )
       
   554     {
       
   555     TInt pos( FindContactListById( aContactListId ) );
       
   556     if ( pos >= 0 )
       
   557         {
       
   558         delete iContactList[ pos ];
       
   559         iContactList.Remove( pos );
       
   560         }
       
   561     }
       
   562 
       
   563 // -----------------------------------------------------------------------------
       
   564 // CCAStorageManager::RemoveContact
       
   565 // From MCAStoredContacts.
       
   566 // -----------------------------------------------------------------------------
       
   567 void CCAStorageManager::RemoveContact( const TDesC& aListId,
       
   568                                        const TDesC& aContactId )
       
   569     {
       
   570     CCAContactList* list = FindContactListInternal( aListId );
       
   571     if ( list )
       
   572         {
       
   573         if ( KErrNone == list->RemoveContact( aContactId ) )
       
   574             {
       
   575             SendObserverEvent( TStorageManagerGlobals::EStorageEventContactDelete,
       
   576                                NULL,
       
   577                                aContactId );
       
   578             }
       
   579         }
       
   580     }
       
   581 
       
   582 // -----------------------------------------------------------------------------
       
   583 // CCAStorageManager::FindContact
       
   584 // From MCAStoredContacts.
       
   585 // Try to load with given ID, return NULL if not found.
       
   586 // -----------------------------------------------------------------------------
       
   587 MCAStoredContact* CCAStorageManager::FindContact( const TDesC& aContactListId,
       
   588                                                   const TDesC& aId )
       
   589     {
       
   590     CCAContactList* list = FindContactListInternal( aContactListId );
       
   591     if ( list )
       
   592         {
       
   593         return list->FindContact( aId );
       
   594         }
       
   595     CHAT_DP_FUNC_DONE( "CCAStorageManager::FindContact" );
       
   596     return NULL;
       
   597     }
       
   598 
       
   599 MCAStoredContact* CCAStorageManager::FindContact( const TDesC& aContactListId,
       
   600                                                   const TDesC& aId,
       
   601                                                   MCAContactList*& aContactList )
       
   602     {
       
   603     CCAContactList* list = FindContactListInternal( aContactListId );
       
   604     aContactList = list;
       
   605     if ( list )
       
   606         {
       
   607         return list->FindContact( aId );
       
   608         }
       
   609     CHAT_DP_FUNC_DONE( "CCAStorageManager::FindContact" );
       
   610     return NULL;
       
   611     }
       
   612 
       
   613 // -----------------------------------------------------------------------------
       
   614 // CCAStorageManager::ContactInAnyList
       
   615 // From MCAStoredContacts.
       
   616 // -----------------------------------------------------------------------------
       
   617 TBool CCAStorageManager::ContactInAllLists( const TDesC& aContactId ) const
       
   618     {
       
   619     TInt listCount( iContactList.Count() );
       
   620     for ( TInt a( 0 ); a < listCount; ++a )
       
   621         {
       
   622         if ( !iContactList[ a ]->FindContact( aContactId ) )
       
   623             {
       
   624             return EFalse;
       
   625             }
       
   626         }
       
   627     return ETrue;
       
   628     }
       
   629 
       
   630 // -----------------------------------------------------------------------------
       
   631 // CCAStorageManager::FindContact
       
   632 // From MCAStoredContacts.
       
   633 // Try to load with given ID, return NULL if not found.
       
   634 // -----------------------------------------------------------------------------
       
   635 MCAStoredContact* CCAStorageManager::FindAnyContact( const TDesC& aContactId )
       
   636     {
       
   637     if ( aContactId.Compare( KPEngUserOwnPresenceId ) == 0 )
       
   638         {
       
   639         return iOwnStatus;
       
   640         }
       
   641 
       
   642     TInt count( iContactList.Count() );
       
   643     for ( TInt a( 0 ); a < count; ++a )
       
   644         {
       
   645         MCAStoredContact* contact = iContactList[ a ]->FindContact( aContactId );
       
   646         if ( contact )
       
   647             {
       
   648             return contact;
       
   649             }
       
   650         }
       
   651     return NULL;
       
   652     }
       
   653 
       
   654 // -----------------------------------------------------------------------------
       
   655 // CCAStorageManager::FindAnyContactByNick
       
   656 // From MCAStoredContacts.
       
   657 // Try to load with given nick, return NULL if not found.
       
   658 // -----------------------------------------------------------------------------
       
   659 MCAStoredContact* CCAStorageManager::FindAnyContactByNick( const TDesC& aNick )
       
   660     {
       
   661     TInt count( iContactList.Count() );
       
   662     for ( TInt a( 0 ); a < count; ++a )
       
   663         {
       
   664         MCAStoredContact* contact =
       
   665             iContactList[ a ]->FindContactByNick( aNick );
       
   666         if ( contact )
       
   667             {
       
   668             return contact;
       
   669             }
       
   670         }
       
   671     return NULL;
       
   672     }
       
   673 
       
   674 // -----------------------------------------------------------------------------
       
   675 // CCAStorageManager::ListCount
       
   676 // Count of contactlists
       
   677 // -----------------------------------------------------------------------------
       
   678 //
       
   679 TInt CCAStorageManager::ListCount() const
       
   680     {
       
   681     return iContactList.Count();
       
   682     }
       
   683 
       
   684 
       
   685 // -----------------------------------------------------------------------------
       
   686 // CCAStorageManager::ListAt
       
   687 // Reference to contact list
       
   688 // -----------------------------------------------------------------------------
       
   689 //
       
   690 MCAContactList& CCAStorageManager::ListAt( TInt aIndex ) const
       
   691     {
       
   692 
       
   693     return *iContactList[ aIndex ];
       
   694     }
       
   695 
       
   696 // -----------------------------------------------------------------------------
       
   697 // CCAStorageManager::ListAt
       
   698 // Reference to contact list
       
   699 // -----------------------------------------------------------------------------
       
   700 //
       
   701 void CCAStorageManager::FindContacts( RPointerArray< MCAStoredContact >& aArray,
       
   702                                       const TDesC& aContactId ) const
       
   703     {
       
   704     TInt count( iContactList.Count() );
       
   705     for ( TInt a( 0 ); a < count; ++a )
       
   706         {
       
   707         MCAStoredContact* contact = iContactList[a]->FindContact( aContactId );
       
   708         if ( contact )
       
   709             {
       
   710             TInt err( aArray.Append( contact ) );
       
   711             if ( err != KErrNone )
       
   712                 {
       
   713                 CActiveScheduler::Current()->Error( err );
       
   714                 return;
       
   715                 }
       
   716             }
       
   717         }
       
   718     }
       
   719 
       
   720 // -----------------------------------------------------------------------------
       
   721 // CCAStorageManager::ContactCount
       
   722 // Count of contacts
       
   723 // -----------------------------------------------------------------------------
       
   724 //
       
   725 TInt CCAStorageManager::ContactCount( TBool aSkipOfflineContacts ) const
       
   726     {
       
   727     TInt contactCount( 0 );
       
   728     TInt count( iContactList.Count() );
       
   729     for ( TInt a( 0 ); a < count; ++a )
       
   730         {
       
   731         contactCount += iContactList[ a ]->ContactCount( aSkipOfflineContacts,
       
   732                                                          EFalse );
       
   733         }
       
   734     return contactCount;
       
   735     }
       
   736 
       
   737 // -----------------------------------------------------------------------------
       
   738 // CCAStorageManager::SetWVHiding
       
   739 // From MCAStoredContacts
       
   740 // -----------------------------------------------------------------------------
       
   741 //
       
   742 void CCAStorageManager::SetWVHiding( TBool aHiding )
       
   743     {
       
   744     iWVHiding = aHiding;
       
   745     }
       
   746 
       
   747 // -----------------------------------------------------------------------------
       
   748 // CCAStorageManager::WVHiding
       
   749 // From MCAStoredContacts
       
   750 // -----------------------------------------------------------------------------
       
   751 //
       
   752 TBool CCAStorageManager::WVHiding()
       
   753     {
       
   754     return iWVHiding;
       
   755     }
       
   756 // -----------------------------------------------------------------------------
       
   757 // CCAStorageManager::SetWVHidingPrefixOnly  UI CR : 101-39728
       
   758 // From MCAStoredContacts
       
   759 // -----------------------------------------------------------------------------
       
   760 //
       
   761 void CCAStorageManager::SetWVHidingPrefixOnly( TBool aHidingPrefixOnly )
       
   762     {
       
   763     iWVHidingPrefixOnly = aHidingPrefixOnly;
       
   764     }
       
   765 // -----------------------------------------------------------------------------
       
   766 // -----------------------------------------------------------------------------
       
   767 // CCAStorageManager::CapitalizingEnabled  UI CR : 101-39727
       
   768 // From MCAStoredContacts
       
   769 // -----------------------------------------------------------------------------
       
   770 //
       
   771 void CCAStorageManager::SetCapitalizingEnabled( TBool aCapital )
       
   772     {
       
   773     iCapital = aCapital;
       
   774     }
       
   775 // CCAStorageManager::WVHidingPrefixOnly    UI CR : 101-39728
       
   776 // From MCAStoredContacts
       
   777 // -----------------------------------------------------------------------------
       
   778 //
       
   779 TBool CCAStorageManager::WVHidingPrefixOnly()
       
   780     {
       
   781     return iWVHidingPrefixOnly;
       
   782     }
       
   783 
       
   784 // -----------------------------------------------------------------------------
       
   785 // CCAStorageManager::CapitalizingEnabled   UI CR : 101-39727
       
   786 // From MCAStoredContacts
       
   787 // -----------------------------------------------------------------------------
       
   788 //
       
   789 TBool CCAStorageManager::CapitalizingEnabled()
       
   790     {
       
   791     return iCapital;
       
   792     }
       
   793 // -----------------------------------------------------------------------------
       
   794 // CCAStorageManager::CreateGroupL
       
   795 // From MCAStoredGroups
       
   796 // -----------------------------------------------------------------------------
       
   797 //
       
   798 MCAExtendedStoredGroup* CCAStorageManager::CreateGroupL()
       
   799     {
       
   800     CHAT_DP_FUNC_ENTER( "CreateGroupL" );
       
   801 
       
   802     if ( ! iStorage )
       
   803         {
       
   804         CHAT_DP_FUNC_DP( "CreateGroupL", "No Storage Manager" );
       
   805         User::Leave( KErrNotReady );
       
   806         }
       
   807 
       
   808     CCAGroup* group = CCAGroup::NewL( iStorage, this );
       
   809 
       
   810     // add to our list
       
   811     TInt status( iGroupList.Append( group ) ); // takes ownership (RPointerArray)
       
   812     if ( status != KErrNone )
       
   813         {
       
   814         delete group;
       
   815         User::Leave( status );
       
   816         }
       
   817 
       
   818     CHAT_DP_FUNC_DONE( "CreateGroupL" );
       
   819     return group;
       
   820     }
       
   821 
       
   822 // -----------------------------------------------------------------------------
       
   823 // CCAStorageManager::DeleteGroup
       
   824 // From MCAStoredGroups
       
   825 // -----------------------------------------------------------------------------
       
   826 //
       
   827 void CCAStorageManager::DeleteGroupL( MCAStoredGroup* aGroup )
       
   828     {
       
   829     CHAT_DP_FUNC_ENTER( "DeleteGroupL" );
       
   830 
       
   831     if ( ! iStorage )
       
   832         {
       
   833         CHAT_DP_FUNC_DP( "DeleteGroupL", "No Storage Manager" );
       
   834         User::Leave( KErrNotReady );
       
   835         }
       
   836 
       
   837     const TInt pos( FindGroupIndexByGroupId( aGroup->GroupId() ) );
       
   838 
       
   839     if ( KErrNotFound == pos )
       
   840         {
       
   841         // not in the list
       
   842         CHAT_DP_FUNC_DP( "DeleteGroupL", "Group not in the list, not deleting" );
       
   843         return;
       
   844         }
       
   845 
       
   846     // notify observers BEFORE deleting group
       
   847     SendObserverEvent( TStorageManagerGlobals::EStorageEventContactDelete,
       
   848                        aGroup->GroupId() );
       
   849 
       
   850     if ( aGroup->StorageType() == TStorageManagerGlobals::EStoragePersistent )
       
   851         {
       
   852         // is saved to persistent storage, so we can delete from
       
   853         // persistent storage
       
   854         CHAT_DP_FUNC_DP( "DeleteGroupL", "Deleting from persistent storage" );
       
   855         iStorage->DeleteL( ( MCAExtendedStoredGroup* ) aGroup );
       
   856         }
       
   857     // otherwise it's just in the internal list,
       
   858     // so delete from internal list
       
   859     CHAT_DP_FUNC_DP( "DeleteGroupL", "Deleting from internal list" );
       
   860 
       
   861     delete iGroupList[ pos ];
       
   862     iGroupList.Remove( pos );
       
   863     iGroupList.Compress();
       
   864 
       
   865     CHAT_DP_FUNC_DONE( "DeleteGroupL" );
       
   866     }
       
   867 
       
   868 // -----------------------------------------------------------------------------
       
   869 // CCAStorageManager::FindGroup
       
   870 // From MCAStoredGroups
       
   871 // -----------------------------------------------------------------------------
       
   872 //
       
   873 MCAStoredGroup* CCAStorageManager::FindGroup( const TDesC& aId )
       
   874     {
       
   875     const TInt pos( FindGroupIndexByGroupId( aId ) );
       
   876     return KErrNotFound == pos ? NULL : iGroupList[ pos ];
       
   877     }
       
   878 
       
   879 // -----------------------------------------------------------------------------
       
   880 // CCAStorageManager::PopulateGroupsList
       
   881 // From MCAStoredGroups
       
   882 // -----------------------------------------------------------------------------
       
   883 //
       
   884 void CCAStorageManager::PopulateGroupsListL(
       
   885     RPointerArray<MCAStoredGroup>& aList,
       
   886     TBool aReturnOnlyVisible )
       
   887     {
       
   888     aList.Reset();
       
   889 
       
   890     if ( ! iStorage )
       
   891         {
       
   892         return;
       
   893         }
       
   894 
       
   895     const TInt count( iGroupList.Count() );
       
   896     for ( int i = 0; i < count; i++ )
       
   897         {
       
   898         if ( aReturnOnlyVisible )
       
   899             {
       
   900             if ( iGroupList[ i ]->IsVisible() )
       
   901                 {
       
   902                 // only add visible groups
       
   903                 TInt status( aList.Append( iGroupList[ i ] ) );
       
   904                 User::LeaveIfError( status );
       
   905                 }
       
   906             }
       
   907         else
       
   908             {
       
   909             // add all groups
       
   910             TInt status( aList.Append( iGroupList[ i ] ) );
       
   911             User::LeaveIfError( status );
       
   912             }
       
   913         }
       
   914     }
       
   915 
       
   916 // -----------------------------------------------------------------------------
       
   917 // CCAStorageManager::SignalGroupChangedL
       
   918 // From MCAStoredGroups
       
   919 // -----------------------------------------------------------------------------
       
   920 //
       
   921 void CCAStorageManager::SignalGroupChangedL( const TDesC& aGroupId )
       
   922     {
       
   923     SendObserverEvent( TStorageManagerGlobals::EStorageEventGroupChange, aGroupId );
       
   924     }
       
   925 
       
   926 // -----------------------------------------------------------------------------
       
   927 // CCAStorageManager::SignalGroupAddedL
       
   928 // From MCAStoredGroups
       
   929 // -----------------------------------------------------------------------------
       
   930 //
       
   931 void CCAStorageManager::SignalGroupAddedL( const TDesC& aGroupId )
       
   932     {
       
   933     SendObserverEvent( TStorageManagerGlobals::EStorageEventContactAddition, aGroupId );
       
   934     }
       
   935 
       
   936 // -----------------------------------------------------------------------------
       
   937 // CCAStorageManager::SaveGroupL
       
   938 // From MCAStoredGroups
       
   939 // -----------------------------------------------------------------------------
       
   940 //
       
   941 void CCAStorageManager::SaveGroupL( const TDesC& aGroupId )
       
   942     {
       
   943     const TInt pos( FindGroupIndexByGroupId( aGroupId ) );
       
   944 
       
   945     if ( pos == KErrNotFound )
       
   946         {
       
   947         // no such group
       
   948         User::Leave( KErrNotFound );
       
   949         }
       
   950 
       
   951     iGroupList[ pos ]->SaveChangesL();
       
   952     SignalGroupAddedL( aGroupId );
       
   953     }
       
   954 
       
   955 // -----------------------------------------------------------------------------
       
   956 // CCAStorageManager::HideAllGroupsL
       
   957 // From MCAStoredGroups
       
   958 // -----------------------------------------------------------------------------
       
   959 //
       
   960 void CCAStorageManager::HideAllGroups( TBool aAllGroups /*= EFalse*/ )
       
   961     {
       
   962     CHAT_DP_FUNC_ENTER( "HideAllGroups" );
       
   963 
       
   964     const TInt count( iGroupList.Count() );
       
   965     for ( TInt i = 0; i < count; i++ )
       
   966         {
       
   967         CCAGroup * group = iGroupList[i];
       
   968         if ( aAllGroups || group->IsOwnGroup() )
       
   969             {
       
   970             // only OWN groups are set to invisible if aAllGroups is EFalse..
       
   971             group->SetVisible( EFalse );
       
   972             }
       
   973         }
       
   974 
       
   975     CHAT_DP_FUNC_DONE( "HideAllGroups" );
       
   976     }
       
   977 
       
   978 // -----------------------------------------------------------------------------
       
   979 // CCAStorageManager::ShowAllGroups
       
   980 // From MCAStoredGroups
       
   981 // -----------------------------------------------------------------------------
       
   982 //
       
   983 void CCAStorageManager::ShowAllGroups()
       
   984     {
       
   985     CHAT_DP_FUNC_ENTER( "ShowAllGroups" );
       
   986 
       
   987     const TInt count( iGroupList.Count() );
       
   988     for ( TInt i = 0; i < count; i++ )
       
   989         {
       
   990         CCAGroup * group = iGroupList[i];
       
   991         // ALL groups are set to visible...
       
   992         group->SetVisible( ETrue );
       
   993         }
       
   994 
       
   995     CHAT_DP_FUNC_DONE( "ShowAllGroups" );
       
   996     }
       
   997 
       
   998 // -----------------------------------------------------------------------------
       
   999 // CCAStorageManager::GroupCount
       
  1000 // From MCAStoredGroups
       
  1001 // -----------------------------------------------------------------------------
       
  1002 //
       
  1003 TInt CCAStorageManager::GroupCount( TBool aOnlyVisible ) const
       
  1004     {
       
  1005     if ( ! aOnlyVisible )
       
  1006         {
       
  1007         return iGroupList.Count();
       
  1008         }
       
  1009 
       
  1010     // find out number of visible groups
       
  1011     TInt count = iGroupList.Count();
       
  1012     TInt grCount = 0;
       
  1013     for ( TInt i = 0; i < count; i++ )
       
  1014         {
       
  1015         grCount += iGroupList[ i ]->IsVisible() ? 1 : 0;
       
  1016         }
       
  1017 
       
  1018     return grCount;
       
  1019     }
       
  1020 
       
  1021 // -----------------------------------------------------------------------------
       
  1022 // CCAStorageManager::CheckFreespaceLevelL
       
  1023 // From MCAStoredGroups
       
  1024 // -----------------------------------------------------------------------------
       
  1025 //
       
  1026 void CCAStorageManager::CheckFreespaceLevelL( MCAExtendedStoredGroup* aGroup )
       
  1027     {
       
  1028     iStorage->CheckFreespaceLevelL( aGroup );
       
  1029     }
       
  1030 
       
  1031 // -----------------------------------------------------------------------------
       
  1032 // CCAStorageManager::SetNotifyOnSave
       
  1033 // From MCAStoredContacts
       
  1034 // -----------------------------------------------------------------------------
       
  1035 //
       
  1036 void CCAStorageManager::SetNotifyOnSave( TBool aStatus )
       
  1037     {
       
  1038     iNotifyUponSave = aStatus;
       
  1039     }
       
  1040 
       
  1041 // -----------------------------------------------------------------------------
       
  1042 // CCAStorageManager::HandleDelete
       
  1043 // From MCAStoredContactsObserver
       
  1044 // We know when the contact is deleted, because it happens
       
  1045 // via Storage Manager, so this method unused.
       
  1046 // -----------------------------------------------------------------------------
       
  1047 //
       
  1048 void CCAStorageManager::HandleContactDelete( const TDesC& /* aContact */ )
       
  1049     {
       
  1050     // unused
       
  1051     }
       
  1052 
       
  1053 // -----------------------------------------------------------------------------
       
  1054 // CCAStorageManager::HandleAddition
       
  1055 // From MCAStoredContactsObserver
       
  1056 // We know when the contact is added, because it happens
       
  1057 // via Storage Manager, so this method unused.
       
  1058 // -----------------------------------------------------------------------------
       
  1059 //
       
  1060 void CCAStorageManager::HandleAddition( MCAContactList& /*aList*/,
       
  1061                                         MCAStoredContact& /* aContact */ )
       
  1062     {
       
  1063     // unused
       
  1064     }
       
  1065 
       
  1066 // -----------------------------------------------------------------------------
       
  1067 // CCAStorageManager::HandleChange
       
  1068 // From MCAStoredContactsObserver
       
  1069 // The contact might be saved via CCAContact::SaveChangesL call,
       
  1070 // in which case we wouldn't know about it. But this observer
       
  1071 // call is invoked if the contact was SaveChangesL'd AND there was
       
  1072 // changed information.
       
  1073 // -----------------------------------------------------------------------------
       
  1074 //
       
  1075 void CCAStorageManager::HandleChange( MCAContactList* aList,
       
  1076                                       MCAStoredContact* aContact,
       
  1077                                       TStorageManagerGlobals::TCAObserverEventType aEventType,
       
  1078                                       TBool aUserIdChanged )
       
  1079     {
       
  1080     // contact was changed, possibly the contact WVID has changed (which is the
       
  1081     // key).  keep our lists sorted. we do it by removing the old from the
       
  1082     // list, then adding it again. it's faster (2*O(logN)) than sorting
       
  1083     // (O(NlogN)).
       
  1084     if ( !aContact && !aList )
       
  1085         {
       
  1086         return;
       
  1087         }
       
  1088 
       
  1089     if ( iNotifyUponSave )
       
  1090         {
       
  1091         // ignore the event type.
       
  1092         // this is a post-change as it comes directly from the CCAContact
       
  1093         if ( aContact ) // contact
       
  1094             {
       
  1095             NotifyAllObserversWithDelay(
       
  1096                 TStorageManagerGlobals::EStorageEventPostChange,
       
  1097                 aContact, aContact->UserId(),
       
  1098                 NULL, aUserIdChanged );
       
  1099             }
       
  1100         else if ( aList )
       
  1101             {
       
  1102             NotifyAllObserversWithDelay(
       
  1103                 aEventType,
       
  1104                 NULL, KNullDesC,
       
  1105                 aList, aUserIdChanged );
       
  1106             }
       
  1107         else //own status
       
  1108             {
       
  1109             NotifyAllObserversWithDelay(
       
  1110                 TStorageManagerGlobals::EStorageEventOwnStatusChange,
       
  1111                 NULL, KPEngUserOwnPresenceId,
       
  1112                 NULL, aUserIdChanged );
       
  1113             }
       
  1114         }
       
  1115     }
       
  1116 
       
  1117 // -----------------------------------------------------------------------------
       
  1118 // CCAStorageManager::HandleBackupRestoreEvent
       
  1119 // From MCAStoredContactsObserver, MCAStoredGroupsObserver
       
  1120 // -----------------------------------------------------------------------------
       
  1121 //
       
  1122 void CCAStorageManager::HandleBackupRestoreEvent(
       
  1123     const TStorageManagerGlobals::TCAObserverEventType /* aEventType */ )
       
  1124     {
       
  1125     // unused
       
  1126     }
       
  1127 
       
  1128 
       
  1129 // -----------------------------------------------------------------------------
       
  1130 // CCAStorageManager::HandleDelete
       
  1131 // From MCAStoredGroupsObserver
       
  1132 // All deletions happen via Storage Manager.
       
  1133 // But group hiding is handled as deletion from UI point of view.
       
  1134 // -----------------------------------------------------------------------------
       
  1135 //
       
  1136 void CCAStorageManager::HandleDelete( const TDesC& aId )
       
  1137     {
       
  1138     SendObserverEvent( TStorageManagerGlobals::EStorageEventContactDelete, aId );
       
  1139     }
       
  1140 
       
  1141 // -----------------------------------------------------------------------------
       
  1142 // CCAStorageManager::HandleAddition
       
  1143 // From MCAStoredGroupsObserver
       
  1144 // Unused: all additions happen via Storage Manager
       
  1145 // -----------------------------------------------------------------------------
       
  1146 //
       
  1147 void CCAStorageManager::HandleAddition( const TDesC& /* aId */ )
       
  1148     {
       
  1149     // unused
       
  1150     }
       
  1151 
       
  1152 // -----------------------------------------------------------------------------
       
  1153 // CCAStorageManager::HandleChange
       
  1154 // From MCAStoredGroupsObserver
       
  1155 // The group might be saved via CCAContact::SaveChangesL call,
       
  1156 // in which case we wouldn't know about it. But this observer
       
  1157 // call is invoked if the group was SaveChangesL'd AND there was
       
  1158 // changed information.
       
  1159 // -----------------------------------------------------------------------------
       
  1160 //
       
  1161 void CCAStorageManager::HandleChange( const TDesC& aId )
       
  1162     {
       
  1163     if ( iNotifyUponSave )
       
  1164         {
       
  1165         // ignoring error because can't do anything
       
  1166         // if signal group change fails
       
  1167         TInt ignore;
       
  1168         TRAP( ignore, SignalGroupChangedL( aId ) );
       
  1169         }
       
  1170     }
       
  1171 
       
  1172 // -----------------------------------------------------------------------------
       
  1173 // CCAStorageManager::HandleTimeWaited
       
  1174 // From MCARefreshTimerObserver
       
  1175 // -----------------------------------------------------------------------------
       
  1176 //
       
  1177 void CCAStorageManager::HandleTimeWaited( const TInt /* aError */ )
       
  1178     {
       
  1179     if ( !iContactListLock )
       
  1180         {
       
  1181         if ( iRefreshStartCounter == 1 )
       
  1182             {
       
  1183             // checking that contact still exists
       
  1184             MCAStoredContact* contact = FindAnyContact( *iLatestContactToRefresh );
       
  1185             MCAContactList* contactList = NULL;
       
  1186             for ( TInt a( 0 ); a < iContactList.Count() && !contactList; ++a )
       
  1187                 {
       
  1188                 if ( iContactList[ a ] == iLatestContactList )
       
  1189                     {
       
  1190                     contactList = iLatestContactList;
       
  1191                     }
       
  1192                 }
       
  1193             if ( contact )
       
  1194                 {
       
  1195                 switch ( iRefreshEventType )
       
  1196                     {
       
  1197                     case TStorageManagerGlobals::EStorageEventPostChange:
       
  1198                     case TStorageManagerGlobals::EStorageEventOwnStatusChange:
       
  1199                         {
       
  1200                         NotifyAllObservers( iRefreshEventType,
       
  1201                                             contact, contact->UserId(),
       
  1202                                             NULL, iRefreshUserIdChanged );
       
  1203                         break;
       
  1204                         }
       
  1205                     default:
       
  1206                         {
       
  1207                         break;
       
  1208                         }
       
  1209                     }
       
  1210                 }
       
  1211             else if ( contactList )
       
  1212                 {
       
  1213                 NotifyAllObservers( iRefreshEventType, NULL, KNullDesC, contactList, EFalse );
       
  1214                 }
       
  1215             }
       
  1216         else
       
  1217             {
       
  1218             for ( TInt i = 0; i < iContactObservers.Count(); i++ )
       
  1219                 {
       
  1220                 MCAStoredContactsObserver* obs = iContactObservers[i];
       
  1221                 obs->HandleChange(
       
  1222                     NULL, NULL, /* multiple changes, contact id does not matter */
       
  1223                     TStorageManagerGlobals::EStorageEventMultipleChanges,
       
  1224                     iRefreshUserIdChanged );
       
  1225                 }
       
  1226             }
       
  1227         iRefreshStartCounter = 0;
       
  1228         }
       
  1229     }
       
  1230 
       
  1231 
       
  1232 // -----------------------------------------------------------------------------
       
  1233 // CCAStorageManager::CollapseLocked
       
  1234 // From MCAStorageInfo
       
  1235 // -----------------------------------------------------------------------------
       
  1236 //
       
  1237 TBool CCAStorageManager::CollapseLocked()
       
  1238     {
       
  1239     return iContactListLock;
       
  1240     }
       
  1241 
       
  1242 // -----------------------------------------------------------------------------
       
  1243 // CCAStorageManager::ShowNickname
       
  1244 // From MCAStorageInfo
       
  1245 // -----------------------------------------------------------------------------
       
  1246 //
       
  1247 TBool CCAStorageManager::ShowNickname()
       
  1248     {
       
  1249     return iShowNickname;
       
  1250     }
       
  1251 
       
  1252 // -----------------------------------------------------------------------------
       
  1253 // CCAStorageManager::CreateContactL
       
  1254 // From MCAExtendedStoredContact
       
  1255 // -----------------------------------------------------------------------------
       
  1256 //
       
  1257 MCAStoredContact* CCAStorageManager::CreateContactL(
       
  1258     const TDesC& aContactList,
       
  1259     const TDesC& aNickname,
       
  1260     const TDesC& aWVID )
       
  1261     {
       
  1262     CHAT_DP_FUNC_ENTER( "CreateContactL" );
       
  1263 
       
  1264     CCAContactList* contactList = FindContactListInternal( aContactList );
       
  1265     if ( !contactList )
       
  1266         {
       
  1267         User::LeaveIfError( iOperationError );
       
  1268         User::Leave( KErrNotFound );
       
  1269         }
       
  1270 
       
  1271     CCAContact* contact = CCAContact::NewL( this, this );
       
  1272     CleanupStack::PushL( contact );
       
  1273 
       
  1274     contact->SetUserIdL( aWVID );
       
  1275     contact->SetNicknameL( aNickname );
       
  1276 
       
  1277     MCAStoredContact* addedContact = contactList->AddContactL( contact );
       
  1278 
       
  1279     TBool notify( EFalse );
       
  1280     if ( addedContact == contact )
       
  1281         {
       
  1282         // contact added
       
  1283         CleanupStack::Pop( contact );
       
  1284         notify = ETrue;
       
  1285         }
       
  1286     else
       
  1287         {
       
  1288         // contact existed
       
  1289         CleanupStack::PopAndDestroy( contact );
       
  1290         // was something updated?
       
  1291         if ( KErrNone != addedContact->Nickname().Compare( aNickname ) )
       
  1292             {
       
  1293             addedContact->SetNicknameL( aNickname );
       
  1294             notify = ETrue;
       
  1295             }
       
  1296         if ( KErrNone != addedContact->UserId().Compare( aWVID ) )
       
  1297             {
       
  1298             addedContact->SetUserIdL( aWVID );
       
  1299             notify = ETrue;
       
  1300             }
       
  1301         // something was updated, we need to resort the contact
       
  1302         if ( notify )
       
  1303             {
       
  1304             contactList->ResortContact( addedContact );
       
  1305             }
       
  1306         }
       
  1307     if ( notify )
       
  1308         {
       
  1309         SendObserverEvent( TStorageManagerGlobals::EStorageEventContactAddition,
       
  1310                            addedContact,
       
  1311                            addedContact->UserId(), contactList, EFalse );
       
  1312         }
       
  1313     return addedContact;
       
  1314     }
       
  1315 
       
  1316 // -----------------------------------------------------------------------------
       
  1317 // CCAStorageManager::CreateContactL
       
  1318 // -----------------------------------------------------------------------------
       
  1319 //
       
  1320 MCAStoredContact* CCAStorageManager::CreateContactL(
       
  1321     MCAContactList& aContactList,
       
  1322     const TDesC& aNickname,
       
  1323     const TDesC& aWVID,
       
  1324     TStorageManagerGlobals::TPresenceStatus aStatus,
       
  1325     TStorageManagerGlobals::TClientType aType,
       
  1326     const TDesC& aAlias,
       
  1327     const TDesC& aStatusText )
       
  1328     {
       
  1329     CCAContactList& contactList = ( CCAContactList& )aContactList;
       
  1330 
       
  1331     CCAContact* contact = CCAContact::NewL( this, this );
       
  1332     CleanupStack::PushL( contact );
       
  1333 
       
  1334     contact->SetUserIdL( aWVID );
       
  1335     contact->SetNicknameL( aNickname );
       
  1336 
       
  1337     // update attributes
       
  1338     contact->SetOnlineStatus( aStatus );
       
  1339     contact->SetAliasL( aAlias );
       
  1340     contact->SetStatusTextL( aStatusText );
       
  1341     contact->SetClientType( aType );
       
  1342 
       
  1343     MCAStoredContact* addedContact = contactList.AddContactL( contact );
       
  1344     // was contact inserted to array, or did it exists?
       
  1345     if ( addedContact == contact )
       
  1346         {
       
  1347         CleanupStack::Pop( contact );
       
  1348         }
       
  1349     else
       
  1350         {
       
  1351         CleanupStack::PopAndDestroy( contact );
       
  1352         // contact was already therem, update attributes and resort that contact
       
  1353         // update attributes
       
  1354         addedContact->SetOnlineStatus( aStatus );
       
  1355         addedContact->SetAliasL( aAlias );
       
  1356         addedContact->SetStatusTextL( aStatusText );
       
  1357         addedContact->SetClientType( aType );
       
  1358         contactList.ResortContact( addedContact );
       
  1359         }
       
  1360 
       
  1361     // this is used at fetching, so do not notify any observer, will be done for whole list
       
  1362     return addedContact;
       
  1363     }
       
  1364 
       
  1365 // -----------------------------------------------------------------------------
       
  1366 // CCAStorageManager::SendContactListUpdatedEvent
       
  1367 // -----------------------------------------------------------------------------
       
  1368 //
       
  1369 void CCAStorageManager::SendContactListUpdatedEvent( MCAContactList& aList )
       
  1370     {
       
  1371     SendObserverEvent( TStorageManagerGlobals::EStorageEventListChanged,
       
  1372                        NULL,
       
  1373                        KNullDesC, &aList, EFalse );
       
  1374     }
       
  1375 
       
  1376 // -----------------------------------------------------------------------------
       
  1377 // CCAStorageManager::RemoveAllContactsL
       
  1378 // -----------------------------------------------------------------------------
       
  1379 //
       
  1380 void CCAStorageManager::RemoveAllContactsL()
       
  1381     {
       
  1382     CHAT_DP_FUNC_ENTER( "RemoveAllContactsL" );
       
  1383 
       
  1384     // work backwards because the removal removes from
       
  1385     // the array, in which case the length changes!
       
  1386     iContactList.ResetAndDestroy();
       
  1387 
       
  1388     // Notify observers
       
  1389     NotifyAllObservers( TStorageManagerGlobals::EStorageEventMultipleChanges,
       
  1390                         NULL, KNullDesC(), NULL, EFalse );
       
  1391 
       
  1392     CHAT_DP_FUNC_DONE( "RemoveAllContactsL" );
       
  1393     }
       
  1394 
       
  1395 // -----------------------------------------------------------------------------
       
  1396 // CCAStorageManager::SetShowNickname
       
  1397 // -----------------------------------------------------------------------------
       
  1398 //
       
  1399 void CCAStorageManager::SetShowNickname( TBool aShowNickname )
       
  1400     {
       
  1401     iShowNickname = aShowNickname;
       
  1402     }
       
  1403 
       
  1404 // -----------------------------------------------------------------------------
       
  1405 // CCAStorageManager::ClearSelected
       
  1406 // -----------------------------------------------------------------------------
       
  1407 //
       
  1408 void CCAStorageManager::ClearSelected()
       
  1409     {
       
  1410     TInt listCount( iContactList.Count() );
       
  1411     for ( TInt listI( 0 ); listI < listCount; ++listI )
       
  1412         {
       
  1413         MCAContactList& list = *iContactList[listI];
       
  1414         list.SetSelected( EFalse );
       
  1415 
       
  1416         TInt count( list.Count() );
       
  1417         for ( TInt i( 0 ); i < count; ++i )
       
  1418             {
       
  1419             MCAStoredContact& contact = list[i];
       
  1420             contact.SetSelected( EFalse );
       
  1421             }
       
  1422         }
       
  1423     }
       
  1424 
       
  1425 // -----------------------------------------------------------------------------
       
  1426 // CCAStorageManager::GetSelectedL
       
  1427 // -----------------------------------------------------------------------------
       
  1428 //
       
  1429 void CCAStorageManager::GetSelectedL( RPointerArray<MCAStoredContact>& aSelected,
       
  1430                                       TStorageManagerGlobals::TFilterType aFilter )
       
  1431     {
       
  1432     TInt listCount( iContactList.Count() );
       
  1433 
       
  1434     for ( TInt listI( 0 ); listI < listCount; ++listI )
       
  1435         {
       
  1436         MCAContactList& list = *iContactList[listI];
       
  1437         TBool listSelected( list.Selected() );
       
  1438 
       
  1439         TInt count( list.Count() );
       
  1440         for ( TInt i( 0 ); i < count; ++i )
       
  1441             {
       
  1442             MCAStoredContact& contact = list[i];
       
  1443 
       
  1444             if ( aFilter != TStorageManagerGlobals::EFilterAll )
       
  1445                 {
       
  1446                 // Check with the filter
       
  1447                 if ( ( listSelected || contact.Selected() )
       
  1448                      && list.FilterAllowsContact( &contact, aFilter ) )
       
  1449                     {
       
  1450                     aSelected.AppendL( &contact );
       
  1451                     }
       
  1452                 }
       
  1453             else
       
  1454                 {
       
  1455                 // Do not use filter
       
  1456                 if ( listSelected || contact.Selected() )
       
  1457                     {
       
  1458                     aSelected.AppendL( &contact );
       
  1459                     }
       
  1460                 }
       
  1461             }
       
  1462         }
       
  1463     }
       
  1464 
       
  1465 
       
  1466 // -----------------------------------------------------------------------------
       
  1467 // CCAStorageManager::FindGroupIndexByGroupId
       
  1468 // -----------------------------------------------------------------------------
       
  1469 //
       
  1470 TInt CCAStorageManager::FindGroupIndexByGroupId( const TDesC& aId )
       
  1471     {
       
  1472     const TInt count( iGroupList.Count() );
       
  1473     for ( TInt i = 0; i < count; i++ )
       
  1474         {
       
  1475         CCAGroup* group = iGroupList[i];
       
  1476 
       
  1477         if ( 0 == CAUtils::NeutralCompare( group->GroupId(), aId ) )
       
  1478             {
       
  1479             CHAT_DP_FUNC_DP( "FindGroupIndexByGroupId", "Found matching group" );
       
  1480             // found it
       
  1481             return i;
       
  1482             }
       
  1483         }
       
  1484 
       
  1485     CHAT_DP_FUNC_DP( "FindGroupIndexByGroupId", "No such group" );
       
  1486 
       
  1487     return KErrNotFound;
       
  1488     }
       
  1489 
       
  1490 // TLinearOrder
       
  1491 // -----------------------------------------------------------------------------
       
  1492 // CCAStorageManager::ContactListOrderByDisplayName
       
  1493 // -----------------------------------------------------------------------------
       
  1494 //
       
  1495 TInt CCAStorageManager::ContactListOrderByDisplayName(
       
  1496     const CCAContactList& aContactListA,
       
  1497     const CCAContactList& aContactListB )
       
  1498     {
       
  1499 #ifdef IMPS_CONTACT_FETCH_BACKGROUND
       
  1500 
       
  1501     // Check fetch status
       
  1502     TBool listANotFetched =
       
  1503         aContactListA.Synchronised() == MCAContactList::ESynchroniseNotDone;
       
  1504     TBool listBNotFetched =
       
  1505         aContactListB.Synchronised() == MCAContactList::ESynchroniseNotDone;
       
  1506 
       
  1507     if ( listANotFetched && !listBNotFetched )
       
  1508         {
       
  1509         // list A not fetched, B fetched/failed => order B first
       
  1510         return 1;
       
  1511         }
       
  1512     else if ( !listANotFetched && listBNotFetched )
       
  1513         {
       
  1514         // list A fetched/failed, B not fetched => order A first
       
  1515         return -1;
       
  1516         }
       
  1517     else
       
  1518         {
       
  1519         // Both fetched/failed or both not fetched => order by display name
       
  1520         return aContactListA.DisplayName().CompareC( aContactListB.DisplayName() );
       
  1521         }
       
  1522 
       
  1523 #else
       
  1524 
       
  1525     return aContactListA.DisplayName().CompareC( aContactListB.DisplayName() );
       
  1526 
       
  1527 #endif // IMPS_CONTACT_FETCH_BACKGROUND    
       
  1528     }
       
  1529 
       
  1530 // TIdentityRelation
       
  1531 // -----------------------------------------------------------------------------
       
  1532 // CCAStorageManager::ContactListFindByContactId
       
  1533 // -----------------------------------------------------------------------------
       
  1534 //
       
  1535 TBool CCAStorageManager::ContactListFindByContactId(
       
  1536     const CCAContactList& aContactListA,
       
  1537     const CCAContactList& aContactListB )
       
  1538     {
       
  1539     const MCAContactList& listA = aContactListA;
       
  1540     const MCAContactList& listB = aContactListB;
       
  1541     return ( CAUtils::NeutralCompare( listA.ListId(),
       
  1542                                       listB.ListId(), EFalse ) == 0 );
       
  1543     }
       
  1544 
       
  1545 // -----------------------------------------------------------------------------
       
  1546 // CCAStorageManager::SetWatched
       
  1547 // -----------------------------------------------------------------------------
       
  1548 //
       
  1549 void CCAStorageManager::SetWatched( const TDesC& aContactId, TBool aStatus )
       
  1550     {
       
  1551     TInt count( iContactList.Count() );
       
  1552     for ( TInt a( 0 ); a < count; ++a )
       
  1553         {
       
  1554         MCAStoredContact* contact =
       
  1555             iContactList[ a ]->FindContact( aContactId );
       
  1556         if ( contact )
       
  1557             {
       
  1558             contact->SetWatched( aStatus );
       
  1559             contact->SignalChanges();
       
  1560             }
       
  1561         }
       
  1562     }
       
  1563 
       
  1564 // -----------------------------------------------------------------------------
       
  1565 // CCAStorageManager::SetWatched
       
  1566 // -----------------------------------------------------------------------------
       
  1567 //
       
  1568 void CCAStorageManager::SetPendingMessages( const TDesC& aContactId, TInt aAmount )
       
  1569     {
       
  1570     TInt count( iContactList.Count() );
       
  1571     for ( TInt a( 0 ); a < count; ++a )
       
  1572         {
       
  1573         MCAStoredContact* contact =
       
  1574             iContactList[ a ]->FindContact( aContactId );
       
  1575         if ( contact )
       
  1576             {
       
  1577             contact->SetPendingMessages( aAmount );
       
  1578             contact->SignalChanges();
       
  1579             }
       
  1580         }
       
  1581     }
       
  1582 // -----------------------------------------------------------------------------
       
  1583 // CCAStorageManager::SetBlocked
       
  1584 // -----------------------------------------------------------------------------
       
  1585 //
       
  1586 void CCAStorageManager::SetBlocked( const TDesC& aContactId, TBool aStatus )
       
  1587     {
       
  1588     TInt count( iContactList.Count() );
       
  1589     for ( TInt a( 0 ); a < count; ++a )
       
  1590         {
       
  1591         MCAStoredContact* contact =
       
  1592             iContactList[ a ]->FindContact( aContactId );
       
  1593         if ( contact )
       
  1594             {
       
  1595             contact->SetBlocked( aStatus );
       
  1596             contact->SignalChanges();
       
  1597             }
       
  1598         }
       
  1599     }
       
  1600 
       
  1601 // -----------------------------------------------------------------------------
       
  1602 // CCAStorageManager::SetBlocked
       
  1603 // -----------------------------------------------------------------------------
       
  1604 //
       
  1605 void CCAStorageManager::SetBlocked( const TDesC& aContactId,
       
  1606                                     const TDesC& aListId,
       
  1607                                     TBool aStatus )
       
  1608     {
       
  1609     CCAContactList* list = FindContactListInternal( aListId );
       
  1610     if ( list )
       
  1611         {
       
  1612         MCAStoredContact* contact = list->FindContact( aContactId );
       
  1613         if ( contact )
       
  1614             {
       
  1615             contact->SetBlocked( aStatus );
       
  1616             contact->SignalChanges();
       
  1617             }
       
  1618         }
       
  1619     }
       
  1620 
       
  1621 // -----------------------------------------------------------------------------
       
  1622 // CCAStorageManager::Sort
       
  1623 /// KNullDesC -> sort all
       
  1624 // -----------------------------------------------------------------------------
       
  1625 //
       
  1626 void CCAStorageManager::Sort( const TDesC& aContactListId /* = KNullDesC */ )
       
  1627     {
       
  1628     if ( aContactListId.Length() != 0 )
       
  1629         {
       
  1630         MCAContactList* list = FindContactList( aContactListId );
       
  1631         if ( list )
       
  1632             {
       
  1633             list->Sort();
       
  1634             }
       
  1635         }
       
  1636     else
       
  1637         {
       
  1638         TInt count( iContactList.Count() );
       
  1639         for ( TInt a( 0 ); a < count; ++a )
       
  1640             {
       
  1641             MCAContactList& list = *iContactList[ a ];
       
  1642             list.Sort();
       
  1643             }
       
  1644         }
       
  1645     }
       
  1646 
       
  1647 // -----------------------------------------------------------------------------
       
  1648 // CCAStorageManager::Sort
       
  1649 /// KNullDesC -> sort all
       
  1650 // -----------------------------------------------------------------------------
       
  1651 //
       
  1652 void CCAStorageManager::ResortContactInAll( MCAStoredContact* aContact )
       
  1653     {
       
  1654     TInt count( iContactList.Count() );
       
  1655     for ( TInt x( 0 ) ; x < count ; ++x )
       
  1656         {
       
  1657         iContactList[ x ]->ResortUnKnownContact( aContact );
       
  1658         }
       
  1659     }
       
  1660 
       
  1661 
       
  1662 // -----------------------------------------------------------------------------
       
  1663 // CCAStorageManager::SetSortAlgorithm
       
  1664 // -----------------------------------------------------------------------------
       
  1665 //
       
  1666 void CCAStorageManager::SetSortAlgorithm(
       
  1667     MCAStoredContact::TSortAlgorithm aAlgorithm )
       
  1668     {
       
  1669     iContactSorter->SetSortAlgorithm( aAlgorithm );
       
  1670     }
       
  1671 
       
  1672 // -----------------------------------------------------------------------------
       
  1673 // CCAStorageManager::OnlineStatus
       
  1674 // -----------------------------------------------------------------------------
       
  1675 //
       
  1676 TStorageManagerGlobals::TPresenceStatus CCAStorageManager::OnlineStatus(
       
  1677     const TDesC& aContactId )
       
  1678     {
       
  1679     TInt count( iContactList.Count() );
       
  1680     for ( TInt a( 0 ); a < count; ++a )
       
  1681         {
       
  1682         MCAStoredContact* contact = iContactList[ a ]->FindContact( aContactId );
       
  1683         if ( contact )
       
  1684             {
       
  1685             return contact->OnlineStatus();
       
  1686             }
       
  1687         }
       
  1688     return TStorageManagerGlobals::EUnknown;
       
  1689     }
       
  1690 
       
  1691 // -----------------------------------------------------------------------------
       
  1692 // CCAStorageManager::UpdatePresenceL
       
  1693 // -----------------------------------------------------------------------------
       
  1694 //
       
  1695 MCAStoredContact* CCAStorageManager::UpdatePresenceL(
       
  1696     const TDesC& aContactId,
       
  1697     TStorageManagerGlobals::TPresenceStatus aStatus,
       
  1698     TStorageManagerGlobals::TClientType aType,
       
  1699     const TDesC& aAlias,
       
  1700     const TDesC& aStatusText,
       
  1701     TStorageManagerGlobals::TPresenceStatus& aOldOnlineStatus )
       
  1702     {
       
  1703     RPointerArray< MCAStoredContact > tempArray;
       
  1704     CleanupClosePushL( tempArray );
       
  1705     RPointerArray< MCAContactList > listArray;
       
  1706     CleanupClosePushL( listArray );
       
  1707     PopulateContactArrayL( listArray, tempArray, aContactId );
       
  1708     MCAStoredContact* contact = NULL;
       
  1709     MCAStoredContact* watchedContact = NULL;
       
  1710     TInt count( tempArray.Count() );
       
  1711     for ( TInt a( 0 ); a < count; ++a )
       
  1712         {
       
  1713         contact = tempArray[ a ];
       
  1714         TBool updated( EFalse );
       
  1715         aOldOnlineStatus = contact->OnlineStatus();
       
  1716         if ( aOldOnlineStatus != aStatus )
       
  1717             {
       
  1718             contact->SetOnlineStatus( aStatus );
       
  1719             updated = ETrue;
       
  1720             }
       
  1721         if ( KErrNone != aAlias.Compare( contact->Alias() ) )
       
  1722             {
       
  1723             contact->SetAliasL( aAlias );
       
  1724             updated = ETrue;
       
  1725             }
       
  1726         contact->SetStatusTextL( aStatusText );
       
  1727         contact->SetClientType( aType );
       
  1728         if ( updated )
       
  1729             {
       
  1730             // contacts online state was updated, resort the contact
       
  1731             listArray[ a ]->ResortContact( contact );
       
  1732             contact->SignalChanges();
       
  1733             watchedContact = contact;
       
  1734             }
       
  1735         }
       
  1736     CleanupStack::PopAndDestroy( 2 ); // listArray, tempArray
       
  1737     return watchedContact;
       
  1738     }
       
  1739 
       
  1740 // -----------------------------------------------------------------------------
       
  1741 // CCAStorageManager::PopulateContactArray
       
  1742 // -----------------------------------------------------------------------------
       
  1743 //
       
  1744 void CCAStorageManager::PopulateContactArrayL(
       
  1745     RPointerArray< MCAContactList >& aListArray,
       
  1746     RPointerArray< MCAStoredContact >& aContactArray, const TDesC& aContactId )
       
  1747     {
       
  1748     TInt count( iContactList.Count() );
       
  1749     for ( TInt a( 0 ); a < count; ++a )
       
  1750         {
       
  1751         MCAStoredContact* contact =
       
  1752             iContactList[ a ]->FindContact( aContactId );
       
  1753         if ( contact )
       
  1754             {
       
  1755             // also populate lists of lists
       
  1756             aListArray.AppendL( iContactList[ a ] );
       
  1757             aContactArray.AppendL( contact );
       
  1758             }
       
  1759         }
       
  1760     }
       
  1761 
       
  1762 // -----------------------------------------------------------------------------
       
  1763 // CCAStorageManager::Identification
       
  1764 // -----------------------------------------------------------------------------
       
  1765 //
       
  1766 const TPtrC CCAStorageManager::Identification( const TDesC& aContactId )
       
  1767     {
       
  1768     if ( aContactId.Length() == 0 )
       
  1769         {
       
  1770         TPtrC retVal( KNullDesC );
       
  1771         return retVal;
       
  1772         }
       
  1773 
       
  1774     // search from contact lists
       
  1775     MCAStoredContact* contact = FindAnyContact( aContactId );
       
  1776     if ( contact )
       
  1777         {
       
  1778         return contact->Identification();
       
  1779         }
       
  1780     else
       
  1781         {
       
  1782         // could not find contact from contact lists,
       
  1783         // check if it's my own id
       
  1784         if ( 0 == CAUtils::NeutralCompare( aContactId, iOwnStatus->UserId() ) )
       
  1785             {
       
  1786             return iOwnStatus->Identification();
       
  1787             }
       
  1788         }
       
  1789     return CAUtils::DisplayId( aContactId );
       
  1790     }
       
  1791 
       
  1792 // -----------------------------------------------------------------------------
       
  1793 // CCAStorageManager::OwnStatus
       
  1794 // -----------------------------------------------------------------------------
       
  1795 //
       
  1796 MCAStoredContact& CCAStorageManager::OwnStatus()
       
  1797     {
       
  1798     return *iOwnStatus;
       
  1799     }
       
  1800 
       
  1801 // -----------------------------------------------------------------------------
       
  1802 // CCAStorageManager::SetContactListLock
       
  1803 // -----------------------------------------------------------------------------
       
  1804 //
       
  1805 void CCAStorageManager::SetContactListLock( TBool aLocked )
       
  1806     {
       
  1807     TBool notify( !aLocked && iContactListLock );
       
  1808     iContactListLock = aLocked;
       
  1809     if ( notify )
       
  1810         {
       
  1811         SendObserverEvent( TStorageManagerGlobals::EStorageEventMultipleChanges,
       
  1812                            NULL,
       
  1813                            KNullDesC );
       
  1814         }
       
  1815     }
       
  1816 
       
  1817 // -----------------------------------------------------------------------------
       
  1818 // CCAStorageManager::FindContactListInterna
       
  1819 // -----------------------------------------------------------------------------
       
  1820 //
       
  1821 void CCAStorageManager::ResetContactLists()
       
  1822     {
       
  1823     TInt count( ListCount() );
       
  1824     TBool collapsed( count == 1 ? EFalse : ETrue );
       
  1825 
       
  1826     for ( TInt a( 0 ); a < count; ++a )
       
  1827         {
       
  1828         ListAt( a ).SetCollapsed( collapsed );
       
  1829         }
       
  1830     if ( count )
       
  1831         {
       
  1832         SendObserverEvent( TStorageManagerGlobals::EStorageEventMultipleChanges,
       
  1833                            NULL, KNullDesC );
       
  1834         }
       
  1835 
       
  1836     }
       
  1837 
       
  1838 // -----------------------------------------------------------------------------
       
  1839 // CCAStorageManager::FindContactListInternal
       
  1840 // -----------------------------------------------------------------------------
       
  1841 //
       
  1842 void CCAStorageManager::SetContactListProperty( TContactListProperty aProperty )
       
  1843     {
       
  1844     TBool primaryInUse( aProperty == EPrimaryCollapseInUse );
       
  1845 
       
  1846     TInt count( iContactList.Count() );
       
  1847     switch ( aProperty )
       
  1848         {
       
  1849         case EPrimaryCollapseInUse:
       
  1850         case ESecondaryCollapseInUse:
       
  1851             {
       
  1852             for ( TInt a( 0 ); a < count; ++a )
       
  1853                 {
       
  1854                 iContactList[ a ]->SetPrimaryInuse( primaryInUse );
       
  1855                 }
       
  1856             break;
       
  1857             }
       
  1858         default:
       
  1859             {
       
  1860             break;
       
  1861             }
       
  1862         }
       
  1863     }
       
  1864 
       
  1865 // -----------------------------------------------------------------------------
       
  1866 // CCAStorageManager::InitSynchroniseProcess
       
  1867 // -----------------------------------------------------------------------------
       
  1868 //
       
  1869 TInt CCAStorageManager::InitSynchroniseProcess()
       
  1870     {
       
  1871     // Set all failed to NotDone
       
  1872     TInt count = iContactList.Count();
       
  1873     for ( TInt a = count - 1; a >= 0; --a )
       
  1874         {
       
  1875         if ( iContactList[ a ]->Synchronised() == MCAContactList::ESynchroniseFailed )
       
  1876             {
       
  1877             iContactList[ a ]->SetSynchronised( MCAContactList::ESynchroniseNotDone );
       
  1878             }
       
  1879         }
       
  1880     return iCurrentListSync = FindNextForSynch( 0 );
       
  1881     }
       
  1882 
       
  1883 // -----------------------------------------------------------------------------
       
  1884 // CCAStorageManager::InitSynchroniseProcess
       
  1885 // -----------------------------------------------------------------------------
       
  1886 //
       
  1887 TInt CCAStorageManager::SetNextForSynchroniseProcess()
       
  1888     {
       
  1889     return iCurrentListSync = FindNextForSynch( iCurrentListSync );
       
  1890     }
       
  1891 
       
  1892 // -----------------------------------------------------------------------------
       
  1893 // CCAStorageManager::IsAllSynchronised
       
  1894 // -----------------------------------------------------------------------------
       
  1895 //
       
  1896 TBool CCAStorageManager::IsAllSynchronised( TBool &aIsFailed ) const
       
  1897     {
       
  1898     TInt count = iContactList.Count();
       
  1899     aIsFailed = EFalse;
       
  1900     TBool isSync = ETrue;
       
  1901     for ( TInt a = count - 1; a >= 0; --a )
       
  1902         {
       
  1903         MCAContactList::TSynchroniseState syncState = iContactList[ a ]->Synchronised();
       
  1904         if ( syncState == MCAContactList::ESynchroniseNotDone )
       
  1905             {
       
  1906             isSync = EFalse;
       
  1907             }
       
  1908         else if ( syncState == MCAContactList::ESynchroniseFailed )
       
  1909             {
       
  1910             aIsFailed = ETrue;
       
  1911             }
       
  1912         }
       
  1913     return isSync;
       
  1914     }
       
  1915 
       
  1916 // -----------------------------------------------------------------------------
       
  1917 // CCAStorageManager::ListInSyncProcessL
       
  1918 // -----------------------------------------------------------------------------
       
  1919 //
       
  1920 MCAContactList& CCAStorageManager::ListInSyncProcessL()
       
  1921     {
       
  1922     __CHAT_ASSERT_DEBUG( iCurrentListSync < iContactList.Count() );
       
  1923     User::LeaveIfError( iCurrentListSync );
       
  1924     return *iContactList[ iCurrentListSync ];
       
  1925     }
       
  1926 
       
  1927 // -----------------------------------------------------------------------------
       
  1928 // CCAStorageManager::FindContactListInternal
       
  1929 // -----------------------------------------------------------------------------
       
  1930 //
       
  1931 CCAContactList* CCAStorageManager::FindContactListInternal(
       
  1932     const TDesC& aListId )
       
  1933     {
       
  1934     CHAT_DP_FUNC_ENTER( "CCAStorageManager::FindContact" );
       
  1935     TInt contactListIndex( FindContactListById( aListId ) );
       
  1936     return ( contactListIndex >= 0 ? iContactList[ contactListIndex ] : NULL );
       
  1937     }
       
  1938 
       
  1939 // -----------------------------------------------------------------------------
       
  1940 // CCAStorageManager::FindContactList
       
  1941 // -----------------------------------------------------------------------------
       
  1942 //
       
  1943 MCAContactList* CCAStorageManager::FindContactList( const TDesC& aListId )
       
  1944     {
       
  1945     return FindContactListInternal( aListId );
       
  1946     }
       
  1947 
       
  1948 // -----------------------------------------------------------------------------
       
  1949 // CCAStorageManager::FindContactListById
       
  1950 // -----------------------------------------------------------------------------
       
  1951 //
       
  1952 TInt CCAStorageManager::FindContactListById(
       
  1953     const TDesC& aListId )
       
  1954     {
       
  1955     TInt count( iContactList.Count() );
       
  1956     for ( TInt x ( 0 ) ; x < count ; ++x )
       
  1957         {
       
  1958         if ( KErrNone == CAUtils::NeutralCompare(
       
  1959                  aListId,
       
  1960                  iContactList[ x ]->ListId(), EFalse )
       
  1961            )
       
  1962             {
       
  1963             return x;
       
  1964             }
       
  1965         }
       
  1966     return KErrNotFound;
       
  1967     }
       
  1968 
       
  1969 
       
  1970 // -----------------------------------------------------------------------------
       
  1971 // CCAStorageManager::FindContactListById
       
  1972 // -----------------------------------------------------------------------------
       
  1973 //
       
  1974 TInt CCAStorageManager::FindNextForSynch( TInt aOffset )
       
  1975     {
       
  1976     TInt offset( aOffset == KErrNotFound ? 0 : aOffset );
       
  1977     TInt count = iContactList.Count();
       
  1978     TInt currentListSync = KErrNotFound;
       
  1979     while ( currentListSync == KErrNotFound && offset < count )
       
  1980         {
       
  1981         if ( iContactList[ offset ]->Synchronised() == MCAContactList::ESynchroniseNotDone )
       
  1982             {
       
  1983             currentListSync = offset;
       
  1984             }
       
  1985         ++offset;
       
  1986         }
       
  1987     return currentListSync;
       
  1988     }
       
  1989 
       
  1990 // -----------------------------------------------------------------------------
       
  1991 // CCAStorageManager::IsValidContact
       
  1992 // -----------------------------------------------------------------------------
       
  1993 //
       
  1994 TBool CCAStorageManager::IsContactValid( const MCAStoredContact* aContact ) const
       
  1995     {
       
  1996     // own status
       
  1997     if ( aContact == iOwnStatus )
       
  1998         {
       
  1999         // valid
       
  2000         return ETrue;
       
  2001         }
       
  2002 
       
  2003     // search lists
       
  2004     TInt count = iContactList.Count();
       
  2005     for ( TInt i = 0; i < count; ++i )
       
  2006         {
       
  2007         if ( iContactList[ i ]->FindIndexOfContact( aContact ) != KErrNotFound )
       
  2008             {
       
  2009             // contact found -> valid
       
  2010             return ETrue;
       
  2011             }
       
  2012         }
       
  2013 
       
  2014     // contact not found
       
  2015     return EFalse;
       
  2016     }
       
  2017 
       
  2018 // -----------------------------------------------------------------------------
       
  2019 // CCAStorageManager::SortContactLists
       
  2020 // -----------------------------------------------------------------------------
       
  2021 //
       
  2022 void CCAStorageManager::SortContactLists()
       
  2023     {
       
  2024     SendObserverEvent( TStorageManagerGlobals::EStorageEventPreChange,
       
  2025                        NULL, KNullDesC );
       
  2026 
       
  2027     TLinearOrder< CCAContactList > order(
       
  2028         CCAStorageManager::ContactListOrderByDisplayName );
       
  2029 
       
  2030     iContactList.Sort( order );
       
  2031 
       
  2032     SendObserverEvent( TStorageManagerGlobals::EStorageEventMultipleChanges,
       
  2033                        NULL, KNullDesC );
       
  2034     }
       
  2035 // -----------------------------------------------------------------------------
       
  2036 // CCAStorageManager::IsGroupDeleted
       
  2037 // -----------------------------------------------------------------------------
       
  2038 //
       
  2039 
       
  2040 TBool CCAStorageManager::IsGroupDeleted()
       
  2041     {
       
  2042     return 	iGroupDeleted;
       
  2043     }
       
  2044 // -----------------------------------------------------------------------------
       
  2045 // CCAStorageManager::SetGroupDeleted
       
  2046 // -----------------------------------------------------------------------------
       
  2047 //
       
  2048 void CCAStorageManager::SetGroupDeleted( TBool aValue )
       
  2049     {
       
  2050     iGroupDeleted = aValue;
       
  2051     }
       
  2052 //  End of File