wvuing/wvuieng/EngSrc/CCABlockingManager.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:  Blocking manager handles the blocking and granting
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 
       
    21 #include "CCABlockingManager.h"
       
    22 #include "CCARequest.h"
       
    23 #include "CCARequestMapper.h"
       
    24 #include "PrivateEngineDefinitions.h"
       
    25 #include "ChatDebugPrint.h"
       
    26 #include "CCAStorageManagerFactory.h"
       
    27 #include "MCASettings.h"
       
    28 #include "MCAStoredContact.h"
       
    29 #include "MCAStoredContacts.h"
       
    30 #include "MCAImpsFactory.h"
       
    31 
       
    32 #include "impsbuilddefinitions.h"
       
    33 
       
    34 #include "TStorageManagerGlobals.h"
       
    35 #include "CAUtils.h"
       
    36 
       
    37 #include "MCAPresence.h"
       
    38 #include "MCAContactLists.h"
       
    39 #include "CAPresenceManager.h"
       
    40 
       
    41 #include "ccasyncchecker.h"
       
    42 #include "mcacontactlist.h"
       
    43 
       
    44 #include <badesca.h>
       
    45 
       
    46 const TInt KBlockingMgrListGranularity = 10;
       
    47 
       
    48 
       
    49 // ================= MEMBER FUNCTIONS =======================
       
    50 
       
    51 // C++ default constructor can NOT contain any code, that
       
    52 // might leave.
       
    53 //
       
    54 
       
    55 CCABlockingManager::CCABlockingManager(
       
    56     MCAImpsFactory* aIMPSFactory,
       
    57     CCARequestMapper* aRequestMapper,
       
    58     MCASettings* aSettingsManager )
       
    59         :   iImpsFactory( aIMPSFactory ),
       
    60         iRequestMapper( aRequestMapper ),
       
    61         iSettingsManager( aSettingsManager ),
       
    62         iStrategy( MCASettings::EAll ),
       
    63         iMgrState( EStateNormal )
       
    64     {
       
    65     }
       
    66 
       
    67 // Destructor
       
    68 EXPORT_C CCABlockingManager::~CCABlockingManager()
       
    69     {
       
    70     if ( iBlockedList )
       
    71         {
       
    72         iBlockedList->Reset();
       
    73         delete iBlockedList;
       
    74         }
       
    75 
       
    76     if ( iGrantedList )
       
    77         {
       
    78         iGrantedList->Reset();
       
    79         delete iGrantedList;
       
    80         }
       
    81     }
       
    82 
       
    83 // Two-phased constructor.
       
    84 EXPORT_C CCABlockingManager* CCABlockingManager::NewL(
       
    85     MCAImpsFactory* aIMPSFactory,
       
    86     CCARequestMapper* aRequestMapper,
       
    87     MCASettings* aSettingsManager )
       
    88     {
       
    89     CCABlockingManager* self = new ( ELeave ) CCABlockingManager(
       
    90         aIMPSFactory,
       
    91         aRequestMapper,
       
    92         aSettingsManager );
       
    93     CleanupStack::PushL( self );
       
    94     self->ConstructL();
       
    95     CleanupStack::Pop( self );
       
    96     return self;
       
    97     }
       
    98 
       
    99 
       
   100 
       
   101 // Symbian OS default constructor can leave.
       
   102 void CCABlockingManager::ConstructL()
       
   103     {
       
   104     iStoredContacts = CCAStorageManagerFactory::ContactListInterfaceL();
       
   105 
       
   106     iBlockedList = new ( ELeave ) CDesCArrayFlat( KBlockingMgrListGranularity );
       
   107     iGrantedList = new ( ELeave ) CDesCArrayFlat( KBlockingMgrListGranularity );
       
   108 
       
   109     // after this the strategy gets updated via observing mechanism
       
   110     iStrategy = ( MCASettings::TCASettingValues )
       
   111                 iSettingsManager->Value( MCASettings::EReceiveIMessages );
       
   112     }
       
   113 
       
   114 // ---------------------------------------------------------
       
   115 // CCABlockingManager::GetBlockedListRequestL()
       
   116 // GetBlocked list
       
   117 // ---------------------------------------------------------
       
   118 //
       
   119 TInt CCABlockingManager::GetBlockedListFromServerL()
       
   120     {
       
   121     CHAT_DP_TXT( "CCABlockingManager::GetBlockedListFromServerL...starts" );
       
   122 
       
   123     iGetBlockedListOpId =  iImpsFactory->CreateImClientL()->GetBlockedListRequestL();
       
   124 
       
   125     // owned by request mapper
       
   126     CCARequest* request = iRequestMapper->CreateRequestL( iGetBlockedListOpId );    // CSI: 35 # Ownership is not transferred to caller.
       
   127 
       
   128     CHAT_DP_FUNC_DP( "GetBlockedListResponseL", "Going to HandleRequest" );
       
   129     iRequestMapper->HandleRequest( iGetBlockedListOpId, KErrNone );
       
   130 
       
   131     // set to 0 so we know that there is no request hanging.
       
   132     iGetBlockedListOpId = 0;
       
   133 
       
   134     TInt errorCode( request->ErrorCode() );
       
   135     iRequestMapper->RemoveRequest( request );
       
   136 
       
   137     if ( iMgrState == EStateNormal )
       
   138         {
       
   139         if ( iStrategy != MCASettings::EAll && iCanUseGrant )
       
   140             {
       
   141             CHAT_DP_FUNC_DP( "GetBlockedListResponseL",
       
   142                              "Copying grantlist" );
       
   143             switch ( iStrategy )
       
   144                 {
       
   145                 case MCASettings::EFriends:
       
   146                     {
       
   147 #ifdef IMPS_CONTACT_FETCH_BACKGROUND
       
   148                     if( iAppUiExit )
       
   149                     	{
       
   150                     	return errorCode;
       
   151                     	}
       
   152                     // Can't grant friends if contact list
       
   153                     // sync is not done, wait for it if needed
       
   154                     if ( !CCASyncChecker::CheckSyncStateL() )
       
   155                         {
       
   156                         User::Leave( KErrNotReady );
       
   157                         }
       
   158 #endif // IMPS_CONTACT_FETCH_BACKGROUND
       
   159                     CDesCArray* clone = CloneArrayLC( *iGrantedList );
       
   160                     GrantFriendsL();
       
   161                     // Check new ids to be granted
       
   162                     CDesCArray* diff = CreateNeutralDiffLC( *iGrantedList,
       
   163                                                             *clone );
       
   164                     // Check if clone contains more ids than we have friends
       
   165                     CDesCArray* diff2 = CreateNeutralDiffLC( *clone,
       
   166                                                              *iGrantedList );
       
   167                     iGrantedList->Reset();
       
   168                     // Remove grant from excess ids
       
   169                     RemoveL( NULL, diff2, ETrue );
       
   170                     // Add grant for new ids
       
   171                     InsertL( NULL, diff, ETrue );
       
   172                     GrantFriendsL(); // reset situation
       
   173                     CleanupStack::PopAndDestroy( 3, clone ); // clone, diff, diff2
       
   174                     break;
       
   175                     }
       
   176                 case MCASettings::ENobody:
       
   177                     {
       
   178                     CDesCArray* clone = CloneArrayLC( *iGrantedList );
       
   179                     RemoveL( NULL, clone, ETrue );
       
   180                     CleanupStack::PopAndDestroy( clone );
       
   181                     break;
       
   182                     }
       
   183                 default:
       
   184                     {
       
   185                     break;
       
   186                     }
       
   187                 }
       
   188             }
       
   189         else if ( !iCanUseGrant )
       
   190             // This is done to get rid of grant list usage.
       
   191             {
       
   192             //if iGrantedList is empty, nothing to clean
       
   193             if ( iGrantedList && iGrantedList->MdcaCount() > 0 )
       
   194                 {
       
   195                 CDesCArray* clone = CloneArrayLC( *iGrantedList );
       
   196                 RemoveL( NULL, clone, ETrue );
       
   197                 CleanupStack::PopAndDestroy( clone );
       
   198                 }
       
   199             }
       
   200         else if ( iCanUseGrant && iStrategy == MCASettings::EAll )
       
   201             {
       
   202             if( iAppUiExit )
       
   203             	{
       
   204             	return errorCode;
       
   205             	}
       
   206             InsertL( iBlockedList, NULL, ETrue );
       
   207             }
       
   208         }
       
   209 
       
   210     CHAT_DP_TXT( "CCABlockingManager::GetBlockedListFromServerL...over" );
       
   211 
       
   212     return errorCode;
       
   213     }
       
   214 
       
   215 // ---------------------------------------------------------
       
   216 // CCABlockingManager::BlockedList()
       
   217 // ---------------------------------------------------------
       
   218 //
       
   219 const CDesCArray* CCABlockingManager::BlockedList()
       
   220     {
       
   221     return iBlockedList;
       
   222     }
       
   223 
       
   224 // check if grant list in use according to the
       
   225 // currently chosen strategy
       
   226 TBool CCABlockingManager::GrantListInUse()
       
   227     {
       
   228     CHAT_DP_FUNC_ENTER( "GrantListInUse" );
       
   229 
       
   230     // grant list is always in use if we're not
       
   231     // accepting IMessages from all
       
   232     TBool retval( ETrue );
       
   233 
       
   234     if ( iStrategy == MCASettings::EAll || !iCanUseGrant )
       
   235         {
       
   236         // grant list not in use, everyone is accepted (except
       
   237         // that the blocking list overrides)
       
   238         retval = EFalse;
       
   239         CHAT_DP_TXT( "Grant list not in use!" );
       
   240         }
       
   241 
       
   242     CHAT_DP_FUNC_DONE( "GrantListInUse" );
       
   243 
       
   244     return retval;
       
   245     }
       
   246 
       
   247 // ---------------------------------------------------------
       
   248 // CCABlockingManager::InsertL()
       
   249 // Insert users to block or grant lists at server
       
   250 // ---------------------------------------------------------
       
   251 //
       
   252 TInt CCABlockingManager::InsertL(
       
   253     const CDesCArray* aBlockList,
       
   254     const CDesCArray* aGrantList,
       
   255     TBool aForceUpdate /* = EFalse */ )
       
   256     {
       
   257     if ( aGrantList )
       
   258         {
       
   259         CDesCArray* diff = CreateNeutralDiffLC( *aGrantList, *iGrantedList );
       
   260 
       
   261         TInt err( KErrNone );
       
   262         if ( NeedUpdate( aBlockList, diff, aForceUpdate ) && !iUpdateInProgress )
       
   263             {
       
   264             err = DoBlockRequestL( aBlockList, NULL, diff, NULL, aBlockList,
       
   265                                    EBlockRequest );
       
   266             if ( err == KErrNone )
       
   267                 {
       
   268                 AddToGrantedListL( *aGrantList );
       
   269                 }
       
   270             }
       
   271         CleanupStack::PopAndDestroy( diff );
       
   272 
       
   273         return err;
       
   274         }
       
   275     else if ( NeedUpdate( aBlockList, aGrantList, aForceUpdate ) )
       
   276         {
       
   277         return DoBlockRequestL( aBlockList, NULL, aGrantList, NULL, aBlockList,
       
   278                                 EBlockRequest );
       
   279         }
       
   280 
       
   281     return KErrNone;
       
   282     }
       
   283 
       
   284 // ---------------------------------------------------------
       
   285 // CCABlockingManager::RemoveL()
       
   286 // Remove users from block or grant lists at server
       
   287 // ---------------------------------------------------------
       
   288 //
       
   289 TInt CCABlockingManager::RemoveL(
       
   290     const CDesCArray* aBlockList,
       
   291     const CDesCArray* aGrantList,
       
   292     TBool aForceUpdate /* = EFalse */ )
       
   293     {
       
   294     if ( aGrantList )
       
   295         {
       
   296         CDesCArray* clone = CloneArrayLC( *aGrantList );
       
   297         RemoveFromGrantedList( *clone );
       
   298         TInt err( KErrNone );
       
   299         if ( NeedUpdate( aBlockList, clone, aForceUpdate )
       
   300              && !iUpdateInProgress )
       
   301             {
       
   302             err = DoBlockRequestL( NULL, aBlockList, NULL, clone, aBlockList,
       
   303                                    EUnBlockRequest );
       
   304             }
       
   305         CleanupStack::PopAndDestroy( clone );
       
   306         return err;
       
   307         }
       
   308 
       
   309     if ( NeedUpdate( aBlockList, aGrantList, aForceUpdate ) )
       
   310         {
       
   311         return DoBlockRequestL( NULL, aBlockList, NULL, aGrantList, aBlockList,
       
   312                                 EUnBlockRequest );
       
   313         }
       
   314 
       
   315     return KErrNone;
       
   316     }
       
   317 
       
   318 // -----------------------------------------------------------------------------
       
   319 // CCABlockingManager::NeedUpdate
       
   320 // If update is needed, return True
       
   321 // -----------------------------------------------------------------------------
       
   322 //
       
   323 TBool CCABlockingManager::NeedUpdate( const CDesCArray* aBlockList,
       
   324                                       const CDesCArray* aGrantList,
       
   325                                       TBool aForceUpdate )
       
   326     {
       
   327     if ( !aForceUpdate )
       
   328         {
       
   329         if ( aGrantList )
       
   330             {
       
   331             if ( aGrantList->MdcaCount() == 0 )
       
   332                 {
       
   333                 return EFalse;
       
   334                 }
       
   335             }
       
   336         else if ( aBlockList )
       
   337             {
       
   338             if ( aBlockList->MdcaCount() == 0 )
       
   339                 {
       
   340                 return EFalse;
       
   341                 }
       
   342             }
       
   343         }
       
   344     return ETrue;
       
   345     }
       
   346 
       
   347 // ---------------------------------------------------------
       
   348 // CCABlockingManager::GrantFriendsL()
       
   349 // Insert friends to grant lists at server
       
   350 // ---------------------------------------------------------
       
   351 //
       
   352 void CCABlockingManager::GrantFriendsL(
       
   353     CDesCArray* aDoNotIncludeList /*= NULL*/ )
       
   354     {
       
   355     if( iAppUiExit )
       
   356     	{
       
   357     	return ;
       
   358     	}
       
   359     // add all friends to Grant list
       
   360     // the blocked list overrides this list anyway
       
   361     iGrantedList->Reset();
       
   362 
       
   363     // fetch the contacts from all lists in server
       
   364     CPtrCArray* wvids = new( ELeave )CPtrCArray( KArrayGranularity );
       
   365     CleanupStack::PushL( wvids );
       
   366 
       
   367     MCAPresence* presence = CAPresenceManager::InstanceL();
       
   368     MCAContactLists* listInterface = presence->ContactLists();
       
   369     TContactListsWrapper contactListIds;
       
   370 
       
   371     if ( listInterface )
       
   372         {
       
   373         contactListIds = listInterface->FetchServerContactListsL();
       
   374         }
       
   375     TInt count( contactListIds.MdcaCount() );
       
   376     TInt i;
       
   377     for ( i = 0; i < count; ++i )
       
   378         {
       
   379         listInterface->FetchServerContactsL( contactListIds.MdcaPoint( i ),
       
   380                                              *wvids );
       
   381         }
       
   382     count = wvids->MdcaCount();
       
   383     TBool doNotIncludeFlag( EFalse );
       
   384     for ( i = 0; i < count; ++i )
       
   385         {
       
   386         TPtrC id( wvids->MdcaPoint( i ) );
       
   387         if ( aDoNotIncludeList )
       
   388             {
       
   389             TInt count2( aDoNotIncludeList->MdcaCount() );
       
   390             for ( TInt j( 0 ); j < count2 && !doNotIncludeFlag; ++j )
       
   391                 {
       
   392                 if ( CAUtils::NeutralCompare( id,
       
   393                                               aDoNotIncludeList->MdcaPoint( j ) ) == 0 )
       
   394                     {
       
   395                     doNotIncludeFlag = ETrue;
       
   396                     aDoNotIncludeList->Delete( j );
       
   397                     }
       
   398                 }
       
   399             }
       
   400         if ( !doNotIncludeFlag )
       
   401             {
       
   402             iGrantedList->AppendL( wvids->MdcaPoint( i ) );
       
   403             }
       
   404         else
       
   405             {
       
   406             doNotIncludeFlag = EFalse;
       
   407             }
       
   408         }
       
   409 
       
   410     CleanupStack::PopAndDestroy( wvids ); // wvids
       
   411     }
       
   412 
       
   413 // ---------------------------------------------------------
       
   414 // CCABlockingManager::DoBlockRequestL()
       
   415 // Execute block entity request with given arguments.
       
   416 // ---------------------------------------------------------
       
   417 //
       
   418 TInt CCABlockingManager::DoBlockRequestL( const CDesCArray* aInsertBlockList,
       
   419                                           const CDesCArray* aRemoveBlockList,
       
   420                                           const CDesCArray* aInsertGrantList,
       
   421                                           const CDesCArray* aRemoveGrantList,
       
   422                                           const CDesCArray* aAdditionalData,
       
   423                                           TOperationRequests aRequest )
       
   424     {
       
   425     // check argument validity
       
   426     if ( aRequest != EBlockRequest && aRequest != EUnBlockRequest )
       
   427         {
       
   428         User::Leave( KErrArgument );
       
   429         }
       
   430 
       
   431     TBool grantUsed = GrantListInUse();
       
   432 
       
   433     // Create the request but do not start the synchronous wait yet ( EFalse )
       
   434     CCARequest* request = iRequestMapper->CreateRequestL(
       
   435                               iImpsFactory->CreateImClientL()->BlockEntityRequestL(
       
   436                                   aInsertBlockList,
       
   437                                   aRemoveBlockList,
       
   438                                   ETrue,
       
   439                                   aInsertGrantList,
       
   440                                   aRemoveGrantList,
       
   441                                   grantUsed ),
       
   442                               EFalse );
       
   443 
       
   444     request->SetRequestType( aRequest );
       
   445 
       
   446     // empty array, so granularity of one
       
   447     CDesCArrayFlat* emptyArray = new( ELeave )CDesCArrayFlat( 1 );
       
   448     CleanupStack::PushL( emptyArray );
       
   449 
       
   450     // set correct array based on request to additional data
       
   451     const CDesCArray *tempArrayPtr = NULL;
       
   452     tempArrayPtr = emptyArray;
       
   453 
       
   454     if ( aAdditionalData )
       
   455         {
       
   456         tempArrayPtr = aAdditionalData;
       
   457         }
       
   458 
       
   459     request->SetAdditionalData( *tempArrayPtr );
       
   460 
       
   461     // Now when the request type and additional data is set we can
       
   462     // activate the synchronous wait
       
   463     request->StartWait();
       
   464 
       
   465     // Execution continues from here
       
   466     TInt errorCode( request->ErrorCode() );
       
   467 
       
   468     CHAT_DP( D_CHAT_LIT( "   Error code in DoBlockRequestL is %d" ), errorCode );
       
   469 
       
   470     iRequestMapper->RemoveRequest( request );
       
   471     CleanupStack::PopAndDestroy( emptyArray );
       
   472 
       
   473     return errorCode;
       
   474 
       
   475     }
       
   476 
       
   477 // ---------------------------------------------------------
       
   478 // CCABlockingManager::IsContactBlocked
       
   479 // ---------------------------------------------------------
       
   480 //
       
   481 TBool CCABlockingManager::IsContactBlocked(
       
   482     const TDesC& aContact,
       
   483     TBool aUseGrant /*= EFalse*/ ) const
       
   484     {
       
   485     CHAT_DP_FUNC_ENTER( "IsContactBlocked" );
       
   486     CHAT_DP( D_CHAT_LIT(
       
   487                  "CCABlockingManager::IsContactBlocked - Checking contact %S" ),
       
   488              &aContact );
       
   489 
       
   490     MCAStoredContact* contact = iStoredContacts->FindAnyContact( aContact );
       
   491 
       
   492     // default to allowing the contact if it gets through
       
   493     // the switch-case
       
   494     TBool retVal( EFalse );
       
   495 
       
   496     MCASettings::TCASettingValues strategy =
       
   497         iCanUseGrant ? iStrategy : MCASettings::EAll;
       
   498     switch ( strategy )
       
   499         {
       
   500         case MCASettings::EAll:
       
   501             CHAT_DP_FUNC_DP( "IsContactBlocked", "Strategy is Grant all" );
       
   502                 {
       
   503                 // grant list not in use, allow all
       
   504                 // but note the blocklist
       
   505                 if ( !contact )
       
   506                     {
       
   507                     CHAT_DP_FUNC_DP( "IsContactBlocked", "Contact is not a friend" );
       
   508                     retVal = InternallyBlocked( aContact );
       
   509                     }
       
   510                 else
       
   511                     {
       
   512                     CHAT_DP_FUNC_DP( "IsContactBlocked", "Contact is a friend" );
       
   513                     retVal = contact->IsBlocked();
       
   514                     }
       
   515                 break;
       
   516                 }
       
   517 
       
   518         case MCASettings::EFriends:
       
   519             {
       
   520             CHAT_DP_FUNC_DP( "IsContactBlocked", "Strategy is Grant friends" );
       
   521             // friends are granted according to block status
       
   522             if ( contact )
       
   523                 {
       
   524                 CHAT_DP_FUNC_DP( "IsContactBlocked", "Contact is a friend" );
       
   525                 retVal = contact->IsBlocked();
       
   526                 }
       
   527             else
       
   528                 {
       
   529                 if ( aUseGrant )
       
   530                     {
       
   531                     // Check grantlist, because there might be some
       
   532                     // contacts in grant list.
       
   533                     CHAT_DP_FUNC_DP( "IsContactBlocked",
       
   534                                      "Contact is not friend but might in grant list" );
       
   535                     // Other than friends could be in grant list
       
   536                     // because temporary grant.
       
   537                     retVal = !IsContactGranted( aContact );
       
   538                     }
       
   539                 else
       
   540                     {
       
   541                     retVal = InternallyBlocked( aContact );
       
   542                     }
       
   543                 }
       
   544             break;
       
   545             }
       
   546         case MCASettings::ENobody:
       
   547             {
       
   548             CHAT_DP_FUNC_DP( "IsContactBlocked", "Strategy is Grant none, but \
       
   549                          someone could be in grant list." );
       
   550             // Some one could be in grant list because temporary grant.
       
   551             if ( aUseGrant )
       
   552                 {
       
   553                 retVal = !IsContactGranted( aContact );
       
   554                 }
       
   555             else
       
   556                 {
       
   557                 retVal = InternallyBlocked( aContact );
       
   558                 }
       
   559             break;
       
   560             }
       
   561 
       
   562         default:
       
   563             {
       
   564             // bogus value!
       
   565             CHAT_DP_FUNC_DP( "IsContactBlocked",
       
   566                              "Bogus grant strategy, defaulting to allow all" );
       
   567             retVal = EFalse;
       
   568             break;
       
   569             }
       
   570         }
       
   571 
       
   572     CHAT_DP_FUNC_DONE( "IsContactBlocked" );
       
   573     return retVal;
       
   574     }
       
   575 
       
   576 // ---------------------------------------------------------
       
   577 // CCABlockingManager::ContactListChangedL
       
   578 // ---------------------------------------------------------
       
   579 //
       
   580 void CCABlockingManager::ContactListChangeL( TBool aUpdateInProgress )
       
   581     {
       
   582     iUpdateInProgress = aUpdateInProgress;
       
   583 
       
   584     if ( iUpdateInProgress )
       
   585         {
       
   586         return;
       
   587         }
       
   588 
       
   589     TInt count( iBlockedList->MdcaCount() );
       
   590     for ( TInt a( 0 ); a < count; ++a )
       
   591         {
       
   592         iStoredContacts->SetBlocked( iBlockedList->MdcaPoint( a ), ETrue );
       
   593         }
       
   594     }
       
   595 
       
   596 // ---------------------------------------------------------
       
   597 // CCABlockingManager::HandleCompleteL
       
   598 // ---------------------------------------------------------
       
   599 //
       
   600 void CCABlockingManager::HandleCompleteL(
       
   601     TInt aOperationId,
       
   602     TImpsCspIdentifier& /* aCspId */ )
       
   603     {
       
   604     TInt status( KErrNone );
       
   605 
       
   606     TRAPD( err, DoHandleCompleteL( aOperationId, status ) );
       
   607     if ( err != KErrNone )
       
   608         {
       
   609         status = err;
       
   610         }
       
   611     iRequestMapper->HandleRequest( aOperationId, status );
       
   612 
       
   613     User::LeaveIfError( err );
       
   614     }
       
   615 
       
   616 // ---------------------------------------------------------
       
   617 // CCABlockingManager::DoHandleCompleteL
       
   618 // ---------------------------------------------------------
       
   619 //
       
   620 void CCABlockingManager::DoHandleCompleteL(
       
   621     TInt aOperationId,
       
   622     TInt aStatus )
       
   623     {
       
   624     CHAT_DP_TXT( "CCABlockingManager::HandleCompleteL...starts" );
       
   625     CHAT_DP( D_CHAT_LIT( "   aStatus is %d" ), aStatus );
       
   626 
       
   627     // Find the request
       
   628     const CCARequest* request = iRequestMapper->FindRequest( aOperationId );
       
   629 
       
   630     if ( request )
       
   631         {
       
   632         // check if this was block request or unblock request
       
   633         TBool blockRequest = ETrue;
       
   634         if ( request->RequestType() == EUnBlockRequest )
       
   635             {
       
   636             blockRequest = EFalse;
       
   637             }
       
   638         // set requested contacts as blocked
       
   639         TInt itemInArray( request->AdditionalData()->MdcaCount() );
       
   640 
       
   641         for ( TInt i( 0 ); i < itemInArray; i++ )
       
   642             {
       
   643             TPtrC userId =
       
   644                 TPtrC( request->AdditionalData()->MdcaPoint( i ) );
       
   645 
       
   646             // block and save state in persistent storage
       
   647             iStoredContacts->SetBlocked( userId, blockRequest );
       
   648 
       
   649             if ( blockRequest )
       
   650                 {
       
   651                 AddToBlockListL( userId );
       
   652                 }
       
   653             else
       
   654                 {
       
   655                 RemoveFromBlockList( userId );
       
   656                 }
       
   657             }
       
   658         }
       
   659 
       
   660     CHAT_DP_TXT( "CCABlockingManager::HandleCompleteL...over" );
       
   661     }
       
   662 
       
   663 
       
   664 
       
   665 // ---------------------------------------------------------
       
   666 // CCABlockingManager::GetBlockedListResponseL
       
   667 // ---------------------------------------------------------
       
   668 //
       
   669 void CCABlockingManager::GetBlockedListResponseL(
       
   670     const MDesCArray* aBlockedList,
       
   671     TBool aBlockedListInUse,
       
   672     const MDesCArray* aGrantedList,
       
   673     TBool aGrantedListInUse,
       
   674     TImpsCspIdentifier& /* aCspId */ )
       
   675     {
       
   676     TRAPD( err, DoGetBlockedListResponseL( aBlockedList, aBlockedListInUse,
       
   677                                            aGrantedList, aGrantedListInUse ) );
       
   678 
       
   679     if ( iGetBlockedListOpId != 0 )
       
   680         {
       
   681         CHAT_DP_FUNC_DP( "GetBlockedListResponseL", "Going to HandleRequest" ) ;
       
   682         iRequestMapper->HandleRequest( iGetBlockedListOpId, err );
       
   683         iGetBlockedListOpId = 0;
       
   684         }
       
   685 
       
   686     User::LeaveIfError( err );
       
   687     }
       
   688 
       
   689 // ---------------------------------------------------------
       
   690 // CCABlockingManager::DoGetBlockedListResponseL
       
   691 // ---------------------------------------------------------
       
   692 //
       
   693 void CCABlockingManager::DoGetBlockedListResponseL(
       
   694     const MDesCArray* aBlockedList,
       
   695     TBool /* aBlockedListInUse */,
       
   696     const MDesCArray* aGrantedList,
       
   697     TBool /* aGrantedListInUse */ )
       
   698     {
       
   699     CHAT_DP_FUNC_ENTER( "GetBlockedListResponseL" );
       
   700 
       
   701     switch ( iMgrState )
       
   702         {
       
   703         case EStateNormal :
       
   704             {
       
   705             CHAT_DP_FUNC_DP( "GetBlockedListResponseL", "EStateNormal" );
       
   706 
       
   707             if ( aBlockedList )
       
   708                 {
       
   709                 // nuke the internal list
       
   710                 iBlockedList->Reset();
       
   711                 if ( aBlockedList->MdcaCount() > 0 )
       
   712                     {
       
   713                     // now block the selected contacts, if there are any
       
   714                     CHAT_DP_FUNC_DP( "GetBlockedListResponseL",
       
   715                                      "Updating blocking status" );
       
   716 
       
   717                     TInt count( aBlockedList->MdcaCount() );
       
   718                     CHAT_DP( D_CHAT_LIT(
       
   719                                  "CCABlockingManager::GetBlockedListResponseL \
       
   720                     - Blocked count %d" ), count );
       
   721 
       
   722 #ifdef IMPS_CONTACT_FETCH_BACKGROUND
       
   723                     TBool needToObserve = EFalse;
       
   724                     TBool syncReady = EFalse;
       
   725 #endif // IMPS_CONTACT_FETCH_BACKGROUND                    
       
   726                     for ( TInt i( 0 ); i < count; ++i )
       
   727                         {
       
   728                         TPtrC userId = TPtrC( aBlockedList->MdcaPoint( i ) );
       
   729 
       
   730                         CHAT_DP( D_CHAT_LIT(
       
   731                                      "CCABlockingManager::GetBlockedListResponseL \
       
   732                         - Contact %S" ), &userId );
       
   733 
       
   734                         // it's blocked, so remember it
       
   735                         AddToBlockListL( userId );
       
   736 
       
   737 #ifndef IMPS_CONTACT_FETCH_BACKGROUND
       
   738                         iStoredContacts->SetBlocked( userId, ETrue );
       
   739 #else
       
   740                         // Contact list sync state must be checked.
       
   741                         // If list sync is not finished mark blocked ids
       
   742                         // while sync progresses.
       
   743                         if ( !needToObserve )
       
   744                             {
       
   745                             MCAContactLists* listInterface =
       
   746                                 CAPresenceManager::InstanceL()->ContactLists();
       
   747                             TBool syncDone = EFalse;
       
   748                             if ( listInterface )
       
   749                                 {
       
   750                                 syncDone = listInterface->IsSyncDone();
       
   751                                 }
       
   752                             TBool ignoreFailed = ETrue;
       
   753                             if ( !iStoredContacts->IsAllSynchronised( ignoreFailed )
       
   754                                  || !syncDone )
       
   755                                 {
       
   756                                 // Sync not done, observe it through stored
       
   757                                 // contacts interface
       
   758                                 needToObserve = ETrue;
       
   759                                 }
       
   760                             else
       
   761                                 {
       
   762                                 // Sync done
       
   763                                 syncReady = ETrue;
       
   764                                 }
       
   765                             }
       
   766                         if ( syncReady )
       
   767                             {
       
   768                             // Sync done, ids can be blocked directly
       
   769                             iStoredContacts->SetBlocked( userId, ETrue );
       
   770                             }
       
   771 #endif // IMPS_CONTACT_FETCH_BACKGROUND
       
   772                         }
       
   773 
       
   774 #ifdef IMPS_CONTACT_FETCH_BACKGROUND
       
   775                     if ( needToObserve )
       
   776                         {
       
   777                         // Observe fetching.
       
   778                         // Blocked ids are marked in HandleChange method.
       
   779                         iStoredContacts->AddObserverL( this );
       
   780                         }
       
   781 #endif // IMPS_CONTACT_FETCH_BACKGROUND                    
       
   782                     }
       
   783                 }
       
   784             iGrantedList->Reset();
       
   785             if ( aGrantedList )
       
   786                 {
       
   787                 // copy the granted list
       
   788                 CHAT_DP_FUNC_DP( "GetBlockedListResponseL",
       
   789                                  "Copying grantlist" );
       
   790                 const TInt count( aGrantedList->MdcaCount() );
       
   791                 for ( TInt i = 0; i < count; i++ )
       
   792                     {
       
   793                     iGrantedList->AppendL( aGrantedList->MdcaPoint( i ) );
       
   794                     }
       
   795                 }
       
   796             break;
       
   797             }
       
   798 
       
   799         case EStateDeletingGrantList :
       
   800             {
       
   801             CHAT_DP_FUNC_DP( "GetBlockedListResponseL",
       
   802                              "EStateDeletingGrantList" );
       
   803             // copy the returned grant list into an internal structure
       
   804             iGrantedList->Reset();
       
   805             if ( aGrantedList )
       
   806                 {
       
   807                 // copy the granted list
       
   808                 CHAT_DP_FUNC_DP( "GetBlockedListResponseL",
       
   809                                  "Copying grantlist" );
       
   810                 const TInt count( aGrantedList->MdcaCount() );
       
   811                 for ( TInt i = 0; i < count; i++ )
       
   812                     {
       
   813                     iGrantedList->AppendL( aGrantedList->MdcaPoint( i ) );
       
   814                     }
       
   815                 }
       
   816             else
       
   817                 {
       
   818                 // we should have gotten it
       
   819                 CHAT_DP_FUNC_DP( "GetBlockedListResponseL",
       
   820                                  "Grantlist was NULL, assuming it's empty" );
       
   821                 }
       
   822             break;
       
   823             }
       
   824 
       
   825         default:
       
   826             {
       
   827             CHAT_DP_FUNC_DP( "GetBlockedListResponseL", "Invalid state" );
       
   828             break;
       
   829             }
       
   830         }
       
   831 
       
   832     CHAT_DP_FUNC_DONE( "GetBlockedListResponseL" );
       
   833     }
       
   834 
       
   835 // clear the grant list
       
   836 void CCABlockingManager::ClearGrantListL( TBool aForceUpdate /* = EFalse */ )
       
   837     {
       
   838     // fetch the lists
       
   839     iMgrState = EStateDeletingGrantList;
       
   840     GetBlockedListFromServerL();
       
   841 
       
   842     // ungrant all of the returned grant list people,
       
   843     // effectively clearing the grant list
       
   844     CDesCArray* clone = CloneArrayLC( *iGrantedList );
       
   845     iGrantedList->Reset();
       
   846     RemoveL( NULL, clone, aForceUpdate );
       
   847     CleanupStack::PopAndDestroy( clone );
       
   848 
       
   849     // RemoveL return value ignored, because if remove fails
       
   850     // engine gets callback HandleErrorL, if removeL is succeed
       
   851     // CCABlockingManager gets callback HandleCompleteL
       
   852 
       
   853     // that's it grant list is cleared
       
   854     iMgrState = EStateNormal;
       
   855     }
       
   856 
       
   857 // ---------------------------------------------------------
       
   858 // CCABlockingManager::HandleSettingsChangeL
       
   859 // ---------------------------------------------------------
       
   860 //
       
   861 void CCABlockingManager::HandleSettingsChangeL( TInt aChangedSettingEnum )
       
   862     {
       
   863     CHAT_DP_FUNC_ENTER( "HandleSettingsChangeL" );
       
   864 
       
   865     if ( aChangedSettingEnum != MCASettings::EReceiveIMessages )
       
   866         {
       
   867         // it's not for us
       
   868         CHAT_DP_FUNC_DP( "HandleSettingsChangeL",
       
   869                          "Got event but it was not for me" );
       
   870         return;
       
   871         }
       
   872 
       
   873     MCASettings::TCASettingValues newValue = ( MCASettings::TCASettingValues )
       
   874                                              iSettingsManager->Value( MCASettings::EReceiveIMessages );
       
   875 
       
   876     CHAT_DP( D_CHAT_LIT( "CCABlockingManager::HandleSettingsChangeL - \
       
   877                         Current %d, new %d" ), iStrategy, newValue );   // CSI: 62 # Not valid, new inside debug print.
       
   878 
       
   879     if ( newValue == iStrategy )
       
   880         {
       
   881         CHAT_DP_FUNC_DP( "HandleSettingsChangeL",
       
   882                          "Unchanged setting state for grantlist strategy" );
       
   883         return;
       
   884         }
       
   885 
       
   886     CHAT_DP( D_CHAT_LIT( "CCABlockingManager::HandleSettingsChangeL - \
       
   887                         BackGroundTaskReady %d" ), iBackgroundTaskReady );
       
   888     if ( !iBackgroundTaskReady ) // haven't runned the backgroundtask yet.
       
   889         {
       
   890         return;
       
   891         }
       
   892 
       
   893     iStrategy = newValue;
       
   894     if ( !iLoggedIn )
       
   895         {
       
   896         // not logged in -> return
       
   897         return;
       
   898         }
       
   899 
       
   900     // the changed settings effects us
       
   901     MCASettings::TCASettingValues strategy =
       
   902         iCanUseGrant ? newValue : MCASettings::EAll;
       
   903 
       
   904     switch ( strategy )
       
   905         {
       
   906         case MCASettings::EAll :
       
   907             {
       
   908             CHAT_DP_FUNC_DP( "HandleSettingsChangeL", "Blocklist only" );
       
   909             // only blocklist active. it should be up-to-date.
       
   910             InsertL( iBlockedList, NULL, ETrue );
       
   911             break;
       
   912             }
       
   913 
       
   914         case MCASettings::EFriends :
       
   915             {
       
   916             CHAT_DP_FUNC_DP( "HandleSettingsChangeL",
       
   917                              "Blocklist with grantlist" );
       
   918             // blocklist with grantlist
       
   919             CDesCArray* clone = CloneArrayLC( *iGrantedList );
       
   920             GrantFriendsL();
       
   921             // Check new ids to be granted
       
   922             CDesCArray* diff = CreateNeutralDiffLC( *iGrantedList,
       
   923                                                     *clone );
       
   924             // Check if clone contains more ids than we have friends
       
   925             CDesCArray* diff2 = CreateNeutralDiffLC( *clone,
       
   926                                                      *iGrantedList );
       
   927             iGrantedList->Reset();
       
   928             // Remove grant from excess ids
       
   929             RemoveL( NULL, diff2, ETrue );
       
   930             // Add grant for new ids
       
   931             InsertL( NULL, diff, ETrue );
       
   932             CleanupStack::PopAndDestroy( 3, clone ); // clone, diff, diff2
       
   933             break;
       
   934             }
       
   935 
       
   936         case MCASettings::ENobody :
       
   937             {
       
   938             CHAT_DP_FUNC_DP( "HandleSettingsChangeL", "Granting nobody" );
       
   939             // grantlist empty == decline all messages
       
   940 
       
   941             // empty the grant list
       
   942             ClearGrantListL( ETrue );
       
   943             break;
       
   944             }
       
   945 
       
   946         default :
       
   947             {
       
   948             // bogus
       
   949             break;
       
   950             }
       
   951         }
       
   952 
       
   953     CHAT_DP_FUNC_DONE( "HandleSettingsChangeL" );
       
   954     }
       
   955 
       
   956 // ---------------------------------------------------------
       
   957 // CCABlockingManager::AddToBlockListL
       
   958 // add to internal blocking list
       
   959 // ---------------------------------------------------------
       
   960 //
       
   961 void CCABlockingManager::AddToBlockListL( const TDesC& aId )
       
   962     {
       
   963     CHAT_DP_FUNC_ENTER( "AddToBlockListL" );
       
   964 
       
   965     TInt position( KErrNotFound );
       
   966     if ( iBlockedList->Find( aId, position, ECmpCollated ) == 0 )
       
   967         {
       
   968         // found it, don't add a duplicate
       
   969         CHAT_DP_FUNC_DP( "AddToBlockListL", "Contact already in blocked list" );
       
   970         return;
       
   971         }
       
   972 
       
   973     // didn't find it, so add it
       
   974     iBlockedList->AppendL( aId );
       
   975 
       
   976     CHAT_DP_FUNC_DONE( "AddToBlockListL" );
       
   977     }
       
   978 
       
   979 
       
   980 // ---------------------------------------------------------
       
   981 // CCABlockingManager::RemoveFromBlockList
       
   982 // remove from internal blocking list
       
   983 // ---------------------------------------------------------
       
   984 //
       
   985 void CCABlockingManager::RemoveFromBlockList( const TDesC& aId )
       
   986     {
       
   987     CHAT_DP_FUNC_ENTER( "RemoveFromBlockListL" );
       
   988 
       
   989 #ifdef _DEBUG
       
   990     TPtrC myDebug( aId );
       
   991     CHAT_DP( D_CHAT_LIT( "CCABlockingManager::RemoveFromBlockListL\
       
   992 	                      looking for %S" ), &myDebug );
       
   993 #endif
       
   994 
       
   995     TInt count( iBlockedList->MdcaCount() );
       
   996     for ( TInt a( 0 ); a < count; ++a )
       
   997         {
       
   998 #ifdef _DEBUG
       
   999         myDebug.Set( iBlockedList->MdcaPoint( a ) );
       
  1000         CHAT_DP( D_CHAT_LIT( "CCABlockingManager::RemoveFromBlockListL \
       
  1001 		                     we have %S" ), &myDebug );
       
  1002 #endif
       
  1003         if ( 0 == CAUtils::NeutralCompare( iBlockedList->MdcaPoint( a ), aId ) )
       
  1004             {
       
  1005             // found it, remove it
       
  1006             CHAT_DP_FUNC_DP( "RemoveFromBlockListL",
       
  1007                              "Found internally blocked contact" );
       
  1008             iBlockedList->Delete( a );
       
  1009             iBlockedList->Compress();
       
  1010             CHAT_DP_FUNC_DONE( "RemoveFromBlockListL" );
       
  1011             return;
       
  1012             }
       
  1013         }
       
  1014     CHAT_DP_FUNC_DONE( "RemoveFromBlockListL" );
       
  1015     }
       
  1016 
       
  1017 
       
  1018 // ---------------------------------------------------------
       
  1019 // CCABlockingManager::InternallyBlocked
       
  1020 // see if contact is internally blocked or not
       
  1021 // ---------------------------------------------------------
       
  1022 //
       
  1023 TBool CCABlockingManager::InternallyBlocked( const TDesC& aId ) const
       
  1024     {
       
  1025     CHAT_DP_FUNC_ENTER( "InternallyBlocked" );
       
  1026 
       
  1027 #ifdef _DEBUG
       
  1028     TPtrC myDebug( aId );
       
  1029     CHAT_DP( D_CHAT_LIT( "CCABlockingManager::InternallyBlocked \
       
  1030 	                      looking for %S" ), &myDebug );
       
  1031 #endif
       
  1032 
       
  1033     TInt count( iBlockedList->MdcaCount() );
       
  1034     for ( TInt a( 0 ); a < count; ++a )
       
  1035         {
       
  1036 #ifdef _DEBUG
       
  1037         myDebug.Set( iBlockedList->MdcaPoint( a ) );
       
  1038         CHAT_DP( D_CHAT_LIT( "CCABlockingManager::InternallyBlocked we have %S" ),
       
  1039                  &myDebug );
       
  1040 #endif
       
  1041         if ( 0 == CAUtils::NeutralCompare( iBlockedList->MdcaPoint( a ), aId ) )
       
  1042             {
       
  1043             CHAT_DP_FUNC_DONE( "InternallyBlocked" );
       
  1044             return ETrue;
       
  1045             }
       
  1046         }
       
  1047 
       
  1048     CHAT_DP_FUNC_DONE( "InternallyBlocked" );
       
  1049     return EFalse;
       
  1050     }
       
  1051 
       
  1052 
       
  1053 // ---------------------------------------------------------
       
  1054 // CCABlockingManager::HandleNetworkStateChange
       
  1055 // ---------------------------------------------------------
       
  1056 //
       
  1057 void CCABlockingManager::HandleNetworkStateChange( TNetworkState aState,
       
  1058                                                    TBool aCanUseGrant )
       
  1059     {
       
  1060     CHAT_DP_FUNC_ENTER( "HandleNetworkState" );
       
  1061     CHAT_DP( D_CHAT_LIT( "New state is: %d" ), aState );
       
  1062 
       
  1063     iCanUseGrant = aCanUseGrant;
       
  1064 
       
  1065     if ( aState == ELoggedIn )
       
  1066         {
       
  1067         iLoggedIn = ETrue;
       
  1068         //just make sure its not started yet
       
  1069         if ( iRequestMapper->BackgroundTaskStatus(
       
  1070                  MCABackgroundInterface::EBlockGrantListFetch ) &
       
  1071              ( MCABackgroundInterface::EUnknown |
       
  1072                MCABackgroundInterface::ECompleted |
       
  1073                MCABackgroundInterface::ECancelled |
       
  1074                MCABackgroundInterface::EFailed
       
  1075              ) )
       
  1076             {
       
  1077             CHAT_DP_TXT( "CCABlockingManager::HandleNetworkStateChange \
       
  1078 			              registering block list fetch" );
       
  1079 
       
  1080 #ifdef RD_SETTINGS_FACELIFT
       
  1081             // Make sure we have the correct settings value.
       
  1082             // With settings facelift enabled it is possible to
       
  1083             // edit settings of any server. If user edits settings
       
  1084             // for some other server than the default server, notification
       
  1085             // is not observed here -> getting value here is needed.
       
  1086             iStrategy = ( MCASettings::TCASettingValues )
       
  1087                         iSettingsManager->Value( MCASettings::EReceiveIMessages );
       
  1088 #endif
       
  1089 
       
  1090             iRequestMapper->RegisterBackgroundTask(
       
  1091                 this, MCABackgroundInterface::EBlockGrantListFetch );
       
  1092             iBackgroundTaskReady = EFalse;
       
  1093             }
       
  1094         }
       
  1095     if ( aState == ELoggedOut )
       
  1096         {
       
  1097         iLoggedIn = EFalse;
       
  1098         }
       
  1099     CHAT_DP_FUNC_DONE( "HandleNetworkState" );
       
  1100     }
       
  1101 
       
  1102 
       
  1103 // ---------------------------------------------------------
       
  1104 // CCABlockingManager::HandleBackgroundTaskL
       
  1105 // ---------------------------------------------------------
       
  1106 //
       
  1107 void CCABlockingManager::HandleBackgroundTaskL( TInt aSubTask )
       
  1108     {
       
  1109     CHAT_DP_FUNC_ENTER( "HandleBackgroundTaskL" );
       
  1110     CHAT_DP( D_CHAT_LIT( "CCABlockingManager::HandleBackgroundTaskL \
       
  1111 	                      Subtask is: %d" ), aSubTask );
       
  1112 
       
  1113 
       
  1114     TInt error = GetBlockedListFromServerL();
       
  1115 
       
  1116     iBackgroundTaskReady = ETrue;
       
  1117 
       
  1118     CHAT_DP_FUNC_DONE( "HandleBackgroundTaskL" );
       
  1119 
       
  1120     if ( error )
       
  1121         {
       
  1122         User::Leave( error );
       
  1123         }
       
  1124 
       
  1125     }
       
  1126 // ---------------------------------------------------------
       
  1127 // CCABlockingManager::HandleContactDelete
       
  1128 // ---------------------------------------------------------
       
  1129 //
       
  1130 void CCABlockingManager::HandleContactDelete( const TDesC& /*aContactId*/ )
       
  1131     {
       
  1132     // Not used
       
  1133     }
       
  1134 
       
  1135 // ---------------------------------------------------------
       
  1136 // CCABlockingManager::HandleAddition
       
  1137 // ---------------------------------------------------------
       
  1138 //
       
  1139 void CCABlockingManager::HandleAddition( MCAContactList& /*aList*/,
       
  1140                                          MCAStoredContact& /*aContact*/ )
       
  1141     {
       
  1142     // Not used
       
  1143     }
       
  1144 
       
  1145 // ---------------------------------------------------------
       
  1146 // CCABlockingManager::HandleChange
       
  1147 // ---------------------------------------------------------
       
  1148 //
       
  1149 void CCABlockingManager::HandleChange(
       
  1150     MCAContactList* /*aList*/,
       
  1151     MCAStoredContact* /*aContact*/,
       
  1152     TStorageManagerGlobals::TCAObserverEventType aEventType,
       
  1153     TBool /*aUserIdChanged*/ )
       
  1154     {
       
  1155     if ( !iLoggedIn )
       
  1156         {
       
  1157         // Logout event received, stop observing
       
  1158         iStoredContacts->RemoveObserver( this );
       
  1159         return;
       
  1160         }
       
  1161 
       
  1162     // Mark contacts blocked as they are fetched
       
  1163     if ( aEventType == TStorageManagerGlobals::EStorageEventMultipleChanges )
       
  1164         {
       
  1165         MCAContactList* currentList = NULL;
       
  1166         TRAPD( err, currentList = &iStoredContacts->ListInSyncProcessL() );
       
  1167         if ( err == KErrNone && currentList )
       
  1168             {
       
  1169             // Sync not yet ready means that contact adding is going on
       
  1170             // -> check list for blocked ids
       
  1171             if ( currentList->Synchronised() == MCAContactList::ESynchroniseNotDone )
       
  1172                 {
       
  1173                 TInt blockCount = iBlockedList->Count();
       
  1174                 for ( TInt i = 0; i < blockCount; i++ )
       
  1175                     {
       
  1176                     TPtrC userId = iBlockedList->MdcaPoint( i );
       
  1177                     TPtrC listId = currentList->ListId();
       
  1178                     iStoredContacts->SetBlocked( userId, listId, ETrue );
       
  1179                     }
       
  1180                 }
       
  1181             }
       
  1182         }
       
  1183 
       
  1184     // Check if sync has ended
       
  1185     TBool failed = EFalse;
       
  1186     TBool syncDone = EFalse;
       
  1187 
       
  1188     TRAP_IGNORE(
       
  1189         MCAContactLists* listInterface =
       
  1190             CAPresenceManager::InstanceL()->ContactLists();
       
  1191         syncDone = listInterface->IsSyncDone();
       
  1192     );  // TRAP_IGNORE
       
  1193 
       
  1194     if ( iStoredContacts->IsAllSynchronised( failed ) &&
       
  1195          syncDone )
       
  1196         {
       
  1197         if ( !failed )
       
  1198             {
       
  1199             // Sync done and no failed lists, no need to observe anymore
       
  1200             iStoredContacts->RemoveObserver( this );
       
  1201             }
       
  1202         // If there were failed lists keep on observing for
       
  1203         // the possible re-fetching of the failed lists.
       
  1204         }
       
  1205     }
       
  1206 
       
  1207 // -----------------------------------------------------------------------------
       
  1208 // CCABlockingManager::SetTemporaryGrantL
       
  1209 // Temporary grant, permanent unblock.
       
  1210 // -----------------------------------------------------------------------------
       
  1211 //
       
  1212 TInt CCABlockingManager::SetTemporaryGrantL( const TDesC& aContact, TBool aOn )
       
  1213     {
       
  1214     //Creating list of unblocked users
       
  1215     CDesCArrayFlat* unBlockList =
       
  1216         new ( ELeave ) CDesCArrayFlat( KArrayGranularity );
       
  1217     CleanupStack::PushL( unBlockList );
       
  1218 
       
  1219     // unBlockList is those persons who are going to be unblocked
       
  1220     unBlockList->AppendL( aContact );
       
  1221 
       
  1222     TInt errorCode( KErrNone );
       
  1223     if ( aOn )
       
  1224         {
       
  1225         if ( GrantListInUse() )
       
  1226             {
       
  1227             errorCode = InsertL( NULL, unBlockList );
       
  1228             }
       
  1229         TInt temp( RemoveL( unBlockList, NULL ) );
       
  1230         if ( temp != KErrNone && errorCode == KErrNone )
       
  1231             {
       
  1232             errorCode = temp;
       
  1233             }
       
  1234         }
       
  1235     else
       
  1236         {
       
  1237         MCAStoredContact* contact = iStoredContacts->FindAnyContact( aContact );
       
  1238         if ( !( contact && iStrategy == MCASettings::EFriends ) )
       
  1239             {
       
  1240             errorCode = RemoveL( NULL, unBlockList );
       
  1241             }
       
  1242         }
       
  1243 
       
  1244     CleanupStack::PopAndDestroy( unBlockList );
       
  1245 
       
  1246     return errorCode;
       
  1247     }
       
  1248 
       
  1249 
       
  1250 // -----------------------------------------------------------------------------
       
  1251 // CCABlockingManager::IsContactGranted
       
  1252 // Check if contact is granted or not.
       
  1253 // -----------------------------------------------------------------------------
       
  1254 //
       
  1255 TBool CCABlockingManager::IsContactGranted( const TDesC& aContact ) const
       
  1256     {
       
  1257     TInt count( iGrantedList->MdcaCount() );
       
  1258     for ( TInt a( 0 ); a < count; ++a )
       
  1259         {
       
  1260         if ( 0 == CAUtils::NeutralCompare( iGrantedList->MdcaPoint( a ),
       
  1261                                            aContact ) )
       
  1262             {
       
  1263             return ETrue;
       
  1264             }
       
  1265         }
       
  1266     return EFalse;
       
  1267     }
       
  1268 
       
  1269 // -----------------------------------------------------------------------------
       
  1270 // CCABlockingManager::CloneArrayLC
       
  1271 // (other items were commented in a header).
       
  1272 // -----------------------------------------------------------------------------
       
  1273 //
       
  1274 CDesCArray* CCABlockingManager::CloneArrayLC( const MDesCArray& aSource )
       
  1275     {
       
  1276     TInt count( aSource.MdcaCount() );
       
  1277     CDesCArray* array = new( ELeave )CDesCArrayFlat( count == 0 ? 1 : count );
       
  1278     CleanupStack::PushL( array );
       
  1279     for ( TInt i( 0 ); i < count; ++i )
       
  1280         {
       
  1281         TPtrC text( aSource.MdcaPoint( i ) );
       
  1282         CHAT_DP( D_CHAT_LIT( " CloneArrayLC [%d] %S" ), i, &text );
       
  1283         array->AppendL( text );
       
  1284         }
       
  1285     CHAT_DP_TXT( "----------------------[endClone]----------------------" );
       
  1286     return array;
       
  1287     }
       
  1288 
       
  1289 // -----------------------------------------------------------------------------
       
  1290 // CCABlockingManager::CreateNeutralDiffLC
       
  1291 // (other items were commented in a header).
       
  1292 // -----------------------------------------------------------------------------
       
  1293 //
       
  1294 CDesCArray* CCABlockingManager::CreateNeutralDiffLC(
       
  1295     const MDesCArray& aArrayDiff,
       
  1296     const MDesCArray& aArrayTest )
       
  1297     {
       
  1298     CDesCArray* array = new( ELeave )CDesCArrayFlat( KArrayGranularity );
       
  1299     CleanupStack::PushL( array );
       
  1300     TInt count( aArrayDiff.MdcaCount() );
       
  1301     for ( TInt i( 0 ); i < count; ++i )
       
  1302         {
       
  1303         TPtrC id( aArrayDiff.MdcaPoint( i ) );
       
  1304         TInt count2( aArrayTest.MdcaCount() );
       
  1305         TInt flag( EFalse );
       
  1306 
       
  1307         for ( TInt j( 0 ); j < count2 && !flag; ++j )
       
  1308             {
       
  1309             if ( CAUtils::NeutralCompare( id, aArrayTest.MdcaPoint( j ) ) == 0 )
       
  1310                 {
       
  1311                 flag = ETrue;
       
  1312                 }
       
  1313             }
       
  1314         if ( !flag )
       
  1315             {
       
  1316             // item that is in aArrayDiff, but not in aArrayTest
       
  1317             // add to resulting array if it does not exist in resulting array
       
  1318             TInt count3( array->MdcaCount() );
       
  1319             for ( TInt k( 0 ); k < count3 && !flag; ++k )
       
  1320                 {
       
  1321                 if ( CAUtils::NeutralCompare( id, array->MdcaPoint( k ) ) == 0 )
       
  1322                     {
       
  1323                     flag = ETrue;
       
  1324                     }
       
  1325                 }
       
  1326 
       
  1327             if ( !flag )
       
  1328                 {
       
  1329                 CHAT_DP( D_CHAT_LIT( " CreateDiffLC [%d] %S" ), i, &id );
       
  1330                 array->AppendL( id );
       
  1331                 }
       
  1332             }
       
  1333         }
       
  1334     CHAT_DP_TXT( "-----------------------[endDiff]----------------------" );
       
  1335     return array;
       
  1336     }
       
  1337 
       
  1338 // -----------------------------------------------------------------------------
       
  1339 // CCABlockingManager::AddToGrantedListL
       
  1340 // (other items were commented in a header).
       
  1341 // -----------------------------------------------------------------------------
       
  1342 //
       
  1343 void CCABlockingManager::AddToGrantedListL( const MDesCArray& aInsertArray )
       
  1344     {
       
  1345     TInt count( aInsertArray.MdcaCount() );
       
  1346     for ( TInt a( 0 ); a < count && !iUpdateInProgress; ++a )
       
  1347         {
       
  1348         iGrantedList->AppendL( aInsertArray.MdcaPoint( a ) );
       
  1349         }
       
  1350     }
       
  1351 
       
  1352 // -----------------------------------------------------------------------------
       
  1353 // CCABlockingManager::RemoveFromGrantedListL
       
  1354 // (other items were commented in a header).
       
  1355 // -----------------------------------------------------------------------------
       
  1356 //
       
  1357 void CCABlockingManager::RemoveFromGrantedList( CDesCArray& aRemoveArray )
       
  1358     {
       
  1359     TInt count( aRemoveArray.MdcaCount() );
       
  1360     for ( TInt a( --count ); a >= 0 && !iUpdateInProgress; --a )
       
  1361         {
       
  1362         TInt grantedCount( iGrantedList->MdcaCount() );
       
  1363         TBool foundFlag( EFalse );
       
  1364         TBool foundOther( EFalse );
       
  1365         TPtrC id( aRemoveArray.MdcaPoint( a ) );
       
  1366 
       
  1367         for ( TInt b( --grantedCount ); b >= 0 && !foundOther; --b )
       
  1368             {
       
  1369             TPtrC id2( iGrantedList->MdcaPoint( b ) );
       
  1370             if ( CAUtils::NeutralCompare( id, id2 ) == 0 )
       
  1371                 {
       
  1372                 if ( !foundFlag )
       
  1373                     {
       
  1374                     // Delete current position
       
  1375                     iGrantedList->Delete( b );
       
  1376                     foundFlag = ETrue;
       
  1377                     }
       
  1378                 else
       
  1379                     {
       
  1380                     foundOther = ETrue;
       
  1381                     }
       
  1382                 }
       
  1383             }
       
  1384         if ( foundOther )
       
  1385             {
       
  1386             aRemoveArray.Delete( a );
       
  1387             }
       
  1388         }
       
  1389     }
       
  1390 // -----------------------------------------------------------------------------
       
  1391 // CCABlockingManager::SetAppUiExitFlag
       
  1392 // (other items were commented in a header).
       
  1393 // -----------------------------------------------------------------------------
       
  1394 //
       
  1395 void CCABlockingManager::SetAppUiExitFlag( TBool aFlag )
       
  1396 	{
       
  1397 	iAppUiExit = aFlag;
       
  1398 	}
       
  1399 // end of file