phonebookui/Phonebook2/remotecontactlookup/engine/src/cpbk2remotecontactlookupaccounts.cpp
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2007 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:  Implementation of the class Pbk2RemoteContactLookupFactory.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <cpbk2remotecontactlookupaccounts.h>
       
    20 
       
    21 #include "pbkxrclutils.h"
       
    22 #include "cpbkxrclprotocolenvimpl.h"
       
    23 #include "cpbkxremotecontactlookupprotocoladapter.h"
       
    24 
       
    25 void CleanupResetAndDestroyInfoArray(TAny* aObj)
       
    26     {
       
    27     if (aObj)
       
    28         {
       
    29         static_cast<RImplInfoPtrArray*>(aObj)->ResetAndDestroy();
       
    30         }
       
    31     }
       
    32 
       
    33 void CleanupResetAndDestroyAccArray(TAny* aObj)
       
    34     {
       
    35     if (aObj)
       
    36         {
       
    37         static_cast<RPointerArray<CPbkxRemoteContactLookupProtocolAccount>*>(aObj)->ResetAndDestroy();
       
    38         }
       
    39     }
       
    40 
       
    41 
       
    42 // ======== MEMBER FUNCTIONS ========
       
    43 
       
    44 
       
    45 EXPORT_C CPbk2RemoteContactLookupAccounts* CPbk2RemoteContactLookupAccounts::NewL()
       
    46     {
       
    47     CPbk2RemoteContactLookupAccounts* self = CPbk2RemoteContactLookupAccounts::NewLC();
       
    48     CleanupStack::Pop( self );
       
    49     return self;
       
    50     }
       
    51 
       
    52 
       
    53 
       
    54 EXPORT_C CPbk2RemoteContactLookupAccounts* CPbk2RemoteContactLookupAccounts::NewLC()
       
    55     {
       
    56     CPbk2RemoteContactLookupAccounts* self = new (ELeave) CPbk2RemoteContactLookupAccounts();
       
    57     CleanupStack::PushL( self );
       
    58     return self;
       
    59     }
       
    60 
       
    61 
       
    62 
       
    63 CPbk2RemoteContactLookupAccounts::~CPbk2RemoteContactLookupAccounts()
       
    64     {
       
    65     iAdapters.ResetAndDestroy();
       
    66     delete iProtocolEnv;
       
    67     }
       
    68 
       
    69 
       
    70 
       
    71 EXPORT_C TPbkxRemoteContactLookupProtocolAccountId CPbk2RemoteContactLookupAccounts::DefaultAccountIdL()
       
    72     {
       
    73     return PbkxRclUtils::DefaultProtocolAccountIdL();
       
    74     }
       
    75 
       
    76 
       
    77 
       
    78 EXPORT_C CPbkxRemoteContactLookupProtocolAccount* CPbk2RemoteContactLookupAccounts::GetAccountL(
       
    79     const TPbkxRemoteContactLookupProtocolAccountId& aProtocolAccountId )
       
    80     {
       
    81     CPbkxRclProtocolEnvImpl* protocolEnv = CPbkxRclProtocolEnvImpl::NewLC();
       
    82 
       
    83     CPbkxRemoteContactLookupProtocolAccount* account = NULL;
       
    84 
       
    85     // Need load the plugin    
       
    86     CPbkxRemoteContactLookupProtocolAdapter* adapter = NULL;
       
    87     TRAP_IGNORE( adapter = CPbkxRemoteContactLookupProtocolAdapter::NewL( 
       
    88             aProtocolAccountId.iProtocolUid, 
       
    89             *protocolEnv ) );
       
    90     if ( adapter )
       
    91         {
       
    92         CleanupStack::PushL( adapter );
       
    93 
       
    94         // Delegate to the plugin
       
    95         account = adapter->NewProtocolAccountL( aProtocolAccountId );
       
    96         CleanupStack::PopAndDestroy( adapter );
       
    97         }
       
    98     CleanupStack::PopAndDestroy(); // protocolEnv
       
    99     return account;
       
   100     }
       
   101 
       
   102 
       
   103 
       
   104 EXPORT_C void CPbk2RemoteContactLookupAccounts::LoadProtocolPluginsL()
       
   105     {
       
   106     iAdapters.ResetAndDestroy();
       
   107     if ( !iProtocolEnv )
       
   108         {
       
   109         iProtocolEnv = CPbkxRclProtocolEnvImpl::NewL();
       
   110         }
       
   111     
       
   112     RImplInfoPtrArray implArray;
       
   113     TCleanupItem cleanupItem(CleanupResetAndDestroyInfoArray, &implArray);
       
   114     CleanupStack::PushL( cleanupItem );
       
   115     REComSession::ListImplementationsL( TUid::Uid(KPbkxRemoteContactLookupAdapterIfUid), implArray );
       
   116 
       
   117     //TODO Need to also keep track of the plugin protocol UIDs. ???
       
   118     // Also impl. UIDS to avoid unnecessary loading?
       
   119     // The plugin interface doesn't have a method for asking the protocol UID!!
       
   120 
       
   121     // Now create all the protocol adapters.
       
   122     // The actual ECom loading is implemented in CPbkxRemoteContactLookupProtocolAdapter.
       
   123     for ( TInt i = 0; i < implArray.Count(); i++ )
       
   124         {
       
   125         CPbkxRemoteContactLookupProtocolAdapter* volatile adapter = NULL;
       
   126         
       
   127         // Trap the error code of loading one specific protocal adapter, if the error is not 
       
   128         // extremly harmful like "No memory", then go on loading remaining possibly working 
       
   129         // adapters.
       
   130         TRAPD(err,
       
   131         		{
       
   132                 adapter = CPbkxRemoteContactLookupProtocolAdapter::NewL(
       
   133                 implArray[i]->ImplementationUid(), *iProtocolEnv );
       
   134         		}
       
   135         );
       
   136         
       
   137         if( err == KErrNoMemory )
       
   138             {
       
   139             User::Leave( KErrNoMemory );
       
   140             }
       
   141         
       
   142         if ( adapter )
       
   143         	{
       
   144             CleanupStack::PushL( adapter );
       
   145             User::LeaveIfError( iAdapters.Append( adapter ) );
       
   146             CleanupStack::Pop( adapter );
       
   147         	}
       
   148         }
       
   149     CleanupStack::PopAndDestroy(); // cleanupItem 
       
   150     }
       
   151 
       
   152 
       
   153 
       
   154 EXPORT_C void CPbk2RemoteContactLookupAccounts::GetAllAccountsL(
       
   155     RPointerArray<CPbkxRemoteContactLookupProtocolAccount>& aAccounts )
       
   156     {
       
   157     CleanupClosePushL( aAccounts );
       
   158     if ( iAdapters.Count() == 0 )
       
   159         {
       
   160         // No plugins were loaded.
       
   161         LoadProtocolPluginsL();
       
   162         }
       
   163     
       
   164     // Loop through all the plugins and collect all the accounts
       
   165     const TInt protocolAdapterCount = iAdapters.Count();
       
   166     for ( TInt n = 0; n < protocolAdapterCount; n++ )
       
   167         {
       
   168         CPbkxRemoteContactLookupProtocolAdapter& adapter = *iAdapters[n];
       
   169         // Using a temp array, just to avoid an adapter resetting the main array
       
   170         RPointerArray<CPbkxRemoteContactLookupProtocolAccount> protocolAccountsTmp;
       
   171         TCleanupItem cleanupItemAcc(CleanupResetAndDestroyAccArray, &protocolAccountsTmp);
       
   172         CleanupStack::PushL(cleanupItemAcc);
       
   173         
       
   174         TRAPD( err,
       
   175         		{
       
   176                 adapter.GetAllProtocolAccountsL( protocolAccountsTmp );
       
   177         		}
       
   178         );
       
   179         
       
   180         if( err == KErrNoMemory )
       
   181             {
       
   182             User::Leave( KErrNoMemory );
       
   183             }
       
   184         
       
   185         // Move accounts from temp array to main array
       
   186         
       
   187         // First expand the main array so that we can move ownership in one go.
       
   188         // TODO OOPS, METHOD NOT AVAILABLE iAdapters.SetReserveL( iAdapters.Count() + protocolAccountsTmp.Count() );
       
   189 
       
   190         const TInt count = protocolAccountsTmp.Count();
       
   191         for ( TInt x = 0; x < count; x++ )
       
   192             {
       
   193             // Ownership is passed
       
   194             User::LeaveIfError( aAccounts.Append( protocolAccountsTmp[ 0 ] ) );
       
   195             protocolAccountsTmp.Remove( 0 );
       
   196             }
       
   197         
       
   198         CleanupStack::PopAndDestroy( &protocolAccountsTmp );
       
   199         }
       
   200     CleanupStack::Pop();
       
   201     }
       
   202 
       
   203 
       
   204 
       
   205 EXPORT_C void CPbk2RemoteContactLookupAccounts::GetAccountsL(
       
   206     RPointerArray<CPbkxRemoteContactLookupProtocolAccount>& aAccounts,
       
   207     const TUid& aProtocol )
       
   208     {
       
   209     //TODO Should optimise this.
       
   210     CleanupClosePushL( aAccounts );
       
   211     
       
   212     RPointerArray<CPbkxRemoteContactLookupProtocolAccount> protocolAccounts;
       
   213     TCleanupItem cleanupItemAcc(CleanupResetAndDestroyAccArray, &protocolAccounts);
       
   214     CleanupStack::PushL(cleanupItemAcc);
       
   215 
       
   216     GetAllAccountsL( protocolAccounts );
       
   217 
       
   218     // Collect the accounts which have correct protocol
       
   219     for ( TInt j = protocolAccounts.Count() - 1; j >= 0; j-- )
       
   220         {
       
   221         CPbkxRemoteContactLookupProtocolAccount* account = protocolAccounts[j]; 
       
   222         if( account->Id().iProtocolUid == aProtocol )
       
   223             {
       
   224             // Transfer ownership
       
   225             User::LeaveIfError( aAccounts.Append( account ) );
       
   226             }
       
   227         else
       
   228             {
       
   229             // Wrong account. Don't need it.
       
   230             delete account;
       
   231             }
       
   232         protocolAccounts.Remove( j );
       
   233         }
       
   234     CleanupStack::Pop(); // cleanupItemAcc
       
   235     CleanupStack::Pop();
       
   236     }
       
   237 
       
   238 CPbk2RemoteContactLookupAccounts::CPbk2RemoteContactLookupAccounts()
       
   239     {
       
   240     }
       
   241 
       
   242 EXPORT_C void CPbk2RemoteContactLookupAccounts::SetDefaultAccountIdL(
       
   243         const TPbkxRemoteContactLookupProtocolAccountId& aAccountId )
       
   244     {
       
   245     PbkxRclUtils::StoreDefaultProtocolAccountIdL( aAccountId );    
       
   246     }
       
   247 
       
   248 // end of file