phonebookui/Phonebook2/ServerApplication/src/CPbk2ServerAppStoreManager.cpp
changeset 0 e686773b3f54
child 3 04ab22b956c2
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2005-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:  Phonebook 2 server application contact store manager.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2ServerAppStoreManager.h"
       
    20 
       
    21 // Phonebook 2
       
    22 #include "mpbk2serverappstartupobserver.h"
       
    23 #include "CPbk2ServerAppMonitor.h"
       
    24 #include "CPbk2ServerAppAppUi.h"
       
    25 #include <CPbk2StoreProperty.h>
       
    26 #include <CPbk2StorePropertyArray.h>
       
    27 #include <CPbk2StoreManager.h>
       
    28 #include <CPbk2ContactViewBuilder.h>
       
    29 #include <CPbk2StoreConfiguration.h>
       
    30 #include <CPbk2SortOrderManager.h>
       
    31 #include <Pbk2StoreProperty.hrh>
       
    32 
       
    33 // Virtual Phonebook
       
    34 #include <CVPbkContactManager.h>
       
    35 #include <CVPbkContactStoreUriArray.h>
       
    36 #include <MVPbkContactStore.h>
       
    37 #include <MVPbkStoreContact.h>
       
    38 #include <MVPbkContactStoreProperties.h>
       
    39 #include <VPbkContactStoreUris.h>
       
    40 #include <MVPbkContactViewBase.h>
       
    41 #include <MVPbkContactLink.h>
       
    42 #include <MVPbkContactLinkArray.h>
       
    43 #include <CVPbkSortOrderAcquirer.h>
       
    44 #include <VPbkSortOrderAcquirerUid.h>
       
    45 
       
    46 // Debugging headers
       
    47 #include <Pbk2Debug.h>
       
    48 
       
    49 /// Unnamed namespace for local definitions
       
    50 namespace {
       
    51 
       
    52 _LIT( KGroupSortOrderDisplayName, "AllGroups" );
       
    53 
       
    54 /**
       
    55  * Custom cleanup function.
       
    56  *
       
    57  * @param aObj  Object to clean.
       
    58  */
       
    59 void CleanupResetAndDestroy( TAny* aObj )
       
    60     {
       
    61     if ( aObj )
       
    62         {
       
    63         static_cast<RImplInfoPtrArray*>( aObj )->ResetAndDestroy();
       
    64         }
       
    65     }
       
    66 
       
    67 } /// namespace
       
    68 
       
    69 // --------------------------------------------------------------------------
       
    70 // CPbk2ServerAppStoreManager::CPbk2ServerAppStoreManager
       
    71 // --------------------------------------------------------------------------
       
    72 //
       
    73 CPbk2ServerAppStoreManager::CPbk2ServerAppStoreManager
       
    74         ( CPbk2StoreManager& aStoreManager,
       
    75           CVPbkContactManager& aContactManager ) :
       
    76             iStoreManager( aStoreManager ),
       
    77             iContactManager( aContactManager )
       
    78     {
       
    79     }
       
    80 
       
    81 // --------------------------------------------------------------------------
       
    82 // CPbk2ServerAppStoreManager::~CPbk2ServerAppStoreManager
       
    83 // --------------------------------------------------------------------------
       
    84 //
       
    85 CPbk2ServerAppStoreManager::~CPbk2ServerAppStoreManager()
       
    86     {
       
    87     iStoreManager.DeregisterStoreEvents( *this );
       
    88     iOpenedStores.Reset();
       
    89     iOpenedStores.Close();
       
    90     delete iSortOrderAcquirer;
       
    91     delete iMonitor;
       
    92     delete iFetchView;
       
    93     }
       
    94 
       
    95 // --------------------------------------------------------------------------
       
    96 // CPbk2ServerAppStoreManager::ConstructL
       
    97 // --------------------------------------------------------------------------
       
    98 //
       
    99 void CPbk2ServerAppStoreManager::ConstructL()
       
   100     {
       
   101     iStoreManager.RegisterStoreEventsL( *this );
       
   102     }
       
   103 
       
   104 // --------------------------------------------------------------------------
       
   105 // CPbk2ServerAppStoreManager::NewL
       
   106 // --------------------------------------------------------------------------
       
   107 //
       
   108 CPbk2ServerAppStoreManager* CPbk2ServerAppStoreManager::NewL
       
   109         ( CPbk2StoreManager& aStoreManager,
       
   110           CVPbkContactManager& aContactManager )
       
   111     {
       
   112     CPbk2ServerAppStoreManager* self =
       
   113         new ( ELeave ) CPbk2ServerAppStoreManager
       
   114             ( aStoreManager, aContactManager );
       
   115     CleanupStack::PushL( self );
       
   116     self->ConstructL();
       
   117     CleanupStack::Pop( self );
       
   118     return self;
       
   119     }
       
   120 
       
   121 // --------------------------------------------------------------------------
       
   122 // CPbk2ServerAppStoreManager::LoadAndOpenContactStoresL
       
   123 // --------------------------------------------------------------------------
       
   124 //
       
   125 void CPbk2ServerAppStoreManager::LoadAndOpenContactStoresL
       
   126         ( CVPbkContactStoreUriArray& aStoreUris,
       
   127           MPbk2ServerAppStartupObserver& aObserver,
       
   128           TBool aLoadSavingMemoriesAlways )
       
   129     {
       
   130     if ( aLoadSavingMemoriesAlways )
       
   131         {
       
   132         TVPbkContactStoreUriPtr ptr
       
   133             ( VPbkContactStoreUris::DefaultCntDbUri() );
       
   134         if ( !aStoreUris.IsIncluded( ptr ) )
       
   135             {
       
   136             iContactManager.LoadContactStoreL( ptr );
       
   137             }
       
   138         iStoreManager.EnsureDefaultSavingStoreIncludedL();
       
   139         }
       
   140 
       
   141     const TInt count( aStoreUris.Count() );
       
   142     for ( TInt i = 0; i < count; ++i )
       
   143         {
       
   144         iContactManager.LoadContactStoreL( aStoreUris[i] );
       
   145         }
       
   146 
       
   147     iMonitor = CPbk2ServerAppMonitor::NewL
       
   148         ( iContactManager.ContactStoresL(), aObserver );
       
   149     iMonitor->RegisterEventsL( aObserver );
       
   150     iMonitor->StartupBeginsL();
       
   151 
       
   152     iStoreManager.OpenStoresL();
       
   153     }
       
   154 
       
   155 // --------------------------------------------------------------------------
       
   156 // CPbk2ServerAppStoreManager::BuildFetchViewL
       
   157 // --------------------------------------------------------------------------
       
   158 //
       
   159 MVPbkContactViewBase* CPbk2ServerAppStoreManager::BuildFetchViewL
       
   160         ( TVPbkContactViewType aViewType,
       
   161           const CVPbkContactStoreUriArray& aStoreUris,
       
   162           CVPbkFieldTypeSelector* aViewFilter,
       
   163           CPbk2StorePropertyArray& aStoreProperties,
       
   164           const CPbk2SortOrderManager& aSortOrderManager,
       
   165           const TUint32 aFlags )
       
   166     {
       
   167     MVPbkContactViewBase* result = NULL;
       
   168      
       
   169     CPbk2ContactViewBuilder* viewBuilder = CPbk2ContactViewBuilder::NewLC
       
   170         ( iContactManager, aStoreProperties );
       
   171 
       
   172     if ( aViewType == EVPbkContactsView )
       
   173         {
       
   174         result = viewBuilder->CreateContactViewForStoresLC
       
   175             ( aStoreUris, *this, aSortOrderManager.SortOrder(),
       
   176               aViewFilter, aFlags );
       
   177         CleanupStack::Pop(); // result
       
   178         }
       
   179     else if ( aViewType == EVPbkGroupsView )
       
   180         {
       
   181         const MVPbkFieldTypeList* sortOrder = NULL;
       
   182 
       
   183         // Acquire group sort order
       
   184         RImplInfoPtrArray implementations;
       
   185         REComSession::ListImplementationsL
       
   186             ( TUid::Uid( KVPbkSortOrderAcquirerInterfaceUID ), implementations );
       
   187         CleanupStack::PushL( TCleanupItem
       
   188                 ( CleanupResetAndDestroy, &implementations ) );
       
   189 
       
   190         TBool found = EFalse;
       
   191         const TInt count = implementations.Count();
       
   192         for ( TInt i = count - 1; i >= 0 && !found ; --i )
       
   193             {
       
   194             CImplementationInformation* implInfo = implementations[i];
       
   195 
       
   196             if ( implInfo->DisplayName().
       
   197                     CompareC( KGroupSortOrderDisplayName ) == 0 )
       
   198                 {
       
   199                 TUid implUid = implInfo->ImplementationUid();
       
   200 
       
   201                 CVPbkSortOrderAcquirer::TSortOrderAcquirerParam param
       
   202                     ( iContactManager.FieldTypes() );
       
   203 
       
   204                 delete iSortOrderAcquirer;
       
   205                 iSortOrderAcquirer = NULL;
       
   206                 iSortOrderAcquirer = CVPbkSortOrderAcquirer::NewL
       
   207                     ( implUid, param );
       
   208                 sortOrder = &iSortOrderAcquirer->SortOrder();
       
   209                 found = ETrue;
       
   210                 }
       
   211 
       
   212             }
       
   213         CleanupStack::PopAndDestroy(); // implementations
       
   214         
       
   215         result = viewBuilder->CreateGroupViewForStoresLC
       
   216             ( aStoreUris, *this, *sortOrder,
       
   217               aViewFilter, aFlags );
       
   218         CleanupStack::Pop(); // result
       
   219         }
       
   220 
       
   221     CleanupStack::PopAndDestroy(); // viewBuilder
       
   222 
       
   223     return result;
       
   224     }
       
   225 
       
   226 // --------------------------------------------------------------------------
       
   227 // CPbk2ServerAppStoreManager::RemoveReadOnlyStoreUris
       
   228 // --------------------------------------------------------------------------
       
   229 //
       
   230 void CPbk2ServerAppStoreManager::RemoveReadOnlyStoreUris
       
   231         ( CVPbkContactStoreUriArray& aStoreUriArray,
       
   232           CPbk2StorePropertyArray& aStoreProperties ) const
       
   233     {
       
   234     // Browse the list backwards
       
   235     for ( TInt i( aStoreUriArray.Count()-1 ); i >= 0; --i )
       
   236         {
       
   237         const CPbk2StoreProperty* storeProperty =
       
   238             aStoreProperties.FindProperty( aStoreUriArray[i] );
       
   239 
       
   240         if ( storeProperty && !( storeProperty->Flags() & KPbk2DefaultSavingStorageItem ) )
       
   241             {
       
   242             // Remove read only URI from the array
       
   243             aStoreUriArray.Remove( aStoreUriArray[i] );
       
   244             }
       
   245         }
       
   246     }
       
   247 
       
   248 // --------------------------------------------------------------------------
       
   249 // CPbk2ServerAppStoreManager::IsContactIncludedInCurrentConfigurationL
       
   250 // --------------------------------------------------------------------------
       
   251 //
       
   252 TBool CPbk2ServerAppStoreManager::IsContactIncludedInCurrentConfigurationL
       
   253         ( const MVPbkStoreContact& aContact,
       
   254           const CPbk2StoreConfiguration& aStoreConfiguration ) const
       
   255     {
       
   256     TBool result = EFalse;
       
   257 
       
   258     TVPbkContactStoreUriPtr contactUri =
       
   259         aContact.ParentStore().StoreProperties().Uri();
       
   260 
       
   261     CVPbkContactStoreUriArray* currentConfig =
       
   262         aStoreConfiguration.CurrentConfigurationL();
       
   263     CleanupStack::PushL( currentConfig );
       
   264 
       
   265     if ( currentConfig->IsIncluded( contactUri ) )
       
   266         {
       
   267         result = ETrue;
       
   268         }
       
   269 
       
   270     CleanupStack::PopAndDestroy( currentConfig );
       
   271 
       
   272     return result;
       
   273     }
       
   274 
       
   275 // --------------------------------------------------------------------------
       
   276 // CPbk2ServerAppStoreManager::ContactsAvailableL
       
   277 // --------------------------------------------------------------------------
       
   278 //
       
   279 TBool CPbk2ServerAppStoreManager::ContactsAvailableL
       
   280         ( const MVPbkContactLinkArray& aContactLinks ) const
       
   281     {
       
   282     TBool ret = ETrue;
       
   283 
       
   284     CVPbkContactStoreUriArray* validStores = CurrentlyValidStoresL();
       
   285 
       
   286     for ( TInt i = 0; i < aContactLinks.Count(); ++ i )
       
   287         {
       
   288         const MVPbkContactLink& link = aContactLinks.At( i );
       
   289         if ( !validStores->IsIncluded
       
   290                 ( link.ContactStore().StoreProperties().Uri() ) )
       
   291             {
       
   292             ret = EFalse;
       
   293             break;
       
   294             }
       
   295         }
       
   296 
       
   297     delete validStores;
       
   298     return ret;
       
   299     }
       
   300 
       
   301 // --------------------------------------------------------------------------
       
   302 // CPbk2ServerAppStoreManager::CurrentlyValidStoresL
       
   303 // --------------------------------------------------------------------------
       
   304 //
       
   305 CVPbkContactStoreUriArray*
       
   306         CPbk2ServerAppStoreManager::CurrentlyValidStoresL() const
       
   307     {
       
   308     CVPbkContactStoreUriArray* uris = CVPbkContactStoreUriArray::NewLC();
       
   309 
       
   310     const TInt count = iOpenedStores.Count();
       
   311     for ( TInt i = 0 ; i < count; ++i )
       
   312         {
       
   313         uris->AppendL( iOpenedStores[i]->StoreProperties().Uri() );
       
   314         }
       
   315     
       
   316     CleanupStack::Pop( uris );
       
   317     return uris;
       
   318     }
       
   319 
       
   320 // --------------------------------------------------------------------------
       
   321 // CPbk2ServerAppStoreManager::ContactViewReady
       
   322 // --------------------------------------------------------------------------
       
   323 //
       
   324 void CPbk2ServerAppStoreManager::ContactViewReady
       
   325         ( MVPbkContactViewBase& /*aView*/ )
       
   326     {
       
   327     // Do nothing
       
   328     }
       
   329 
       
   330 // --------------------------------------------------------------------------
       
   331 // CPbk2ServerAppStoreManager::ContactViewUnavailable
       
   332 // --------------------------------------------------------------------------
       
   333 //
       
   334 void CPbk2ServerAppStoreManager::ContactViewUnavailable
       
   335         ( MVPbkContactViewBase& /*aView*/ )
       
   336     {
       
   337     // Do nothing
       
   338     }
       
   339 
       
   340 // --------------------------------------------------------------------------
       
   341 // CPbk2ServerAppStoreManager::ContactAddedToView
       
   342 // --------------------------------------------------------------------------
       
   343 //
       
   344 void CPbk2ServerAppStoreManager::ContactAddedToView
       
   345         ( MVPbkContactViewBase& /*aView*/, TInt /*aIndex*/,
       
   346           const MVPbkContactLink& /*aContactLink*/ )
       
   347     {
       
   348     // Do nothing
       
   349     }
       
   350 
       
   351 // --------------------------------------------------------------------------
       
   352 // CPbk2ServerAppStoreManager::ContactRemovedFromView
       
   353 // --------------------------------------------------------------------------
       
   354 //
       
   355 void CPbk2ServerAppStoreManager::ContactRemovedFromView
       
   356         ( MVPbkContactViewBase& /*aView*/, TInt /*aIndex*/,
       
   357           const MVPbkContactLink& /*aContactLink*/ )
       
   358     {
       
   359     // Do nothing
       
   360     }
       
   361 
       
   362 // --------------------------------------------------------------------------
       
   363 // CPbk2ServerAppStoreManager::ContactViewError
       
   364 // --------------------------------------------------------------------------
       
   365 //
       
   366 void CPbk2ServerAppStoreManager::ContactViewError
       
   367         ( MVPbkContactViewBase& /*aView*/, TInt /*aError*/,
       
   368           TBool /*aErrorNotified*/ )
       
   369     {
       
   370     // Do nothing
       
   371     }
       
   372 
       
   373 // --------------------------------------------------------------------------
       
   374 // CPbk2ServerAppStoreManager::StoreReady
       
   375 // --------------------------------------------------------------------------
       
   376 //
       
   377 void CPbk2ServerAppStoreManager::StoreReady
       
   378         ( MVPbkContactStore& aContactStore )
       
   379     {
       
   380     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   381         ("CPbk2ServerAppStoreManager::StoreReady(0x%x)"),
       
   382         &aContactStore);
       
   383 
       
   384     // Add the store to the open stores list
       
   385     if ( iOpenedStores.Find( &aContactStore ) == KErrNotFound )
       
   386         {
       
   387         iOpenedStores.Append( &aContactStore );
       
   388         }
       
   389     }
       
   390 
       
   391 // --------------------------------------------------------------------------
       
   392 // CPbk2ServerAppStoreManager::StoreUnavailable
       
   393 // --------------------------------------------------------------------------
       
   394 //
       
   395 void CPbk2ServerAppStoreManager::StoreUnavailable
       
   396         ( MVPbkContactStore& aContactStore, TInt aReason )
       
   397     {
       
   398     PBK2_DEBUG_PRINT(PBK2_DEBUG_STRING
       
   399         ("CPbk2ServerAppStoreManager::StoreUnavailable(0x%x,%d)"),
       
   400         &aContactStore, aReason);
       
   401 
       
   402     // Remove the store from the open store list if it is there
       
   403     const TInt index = iOpenedStores.Find( &aContactStore );
       
   404     if ( index != KErrNotFound )
       
   405         {
       
   406         iOpenedStores.Remove( index );
       
   407         }
       
   408     }
       
   409 
       
   410 // --------------------------------------------------------------------------
       
   411 // CPbk2ServerAppStoreManager::HandleStoreEventL
       
   412 // --------------------------------------------------------------------------
       
   413 //
       
   414 void CPbk2ServerAppStoreManager::HandleStoreEventL
       
   415         ( MVPbkContactStore& /*aContactStore*/,
       
   416           TVPbkContactStoreEvent /*aEvent*/ )
       
   417     {
       
   418     // Do nothing
       
   419     }
       
   420 
       
   421 // End of File