phonebookui/Phonebook2/Presentation/src/CPbk2SortOrderManagerImpl.cpp
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
child 68 9da50d567e3c
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     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 sort order manager implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2SortOrderManagerImpl.h"
       
    20 
       
    21 // Phonebook 2
       
    22 #include "Pbk2PresentationUtils.h"
       
    23 #include "Pbk2DataCaging.hrh"
       
    24 #include <RPbk2LocalizedResourceFile.h>
       
    25 #include <Phonebook2PrivateCRKeys.h>
       
    26 #include <Pbk2Presentation.rsg>
       
    27 #include <Pbk2DataCaging.hrh>
       
    28 
       
    29 // Virtual Phonebook
       
    30 #include <MVPbkContactView.h>
       
    31 #include <MVPbkFieldType.h>
       
    32 #include <CVPbkSortOrder.h>
       
    33 
       
    34 // System includes
       
    35 #include <barsread.h>
       
    36 #include <centralrepository.h>
       
    37 #include <CPsRequestHandler.h>
       
    38 #include <featmgr.h>
       
    39 
       
    40 
       
    41 /// Unnamed namespace for local definitions
       
    42 namespace {
       
    43 
       
    44 /// Theoretical maximum of field types in sort order.
       
    45 /// NOTE: Some of the underlying stores may not support
       
    46 /// this many fields in sort order.
       
    47 const TInt KPbk2MaxNumberOfFieldsInSortOrder = 10;
       
    48 /// Maximum separator length
       
    49 /// Defines the string length, which is used to read separator character 
       
    50 /// between last and first name from central repository.
       
    51 /// @see qtn_phob_name_separator_char 
       
    52 const TInt KMaxSeparatorLength = 4;
       
    53 /// White space
       
    54 _LIT(KSpace, " ");
       
    55 
       
    56 /// These enumerations must have same values as defined in loc-file
       
    57 /// qtn_phob_name_separator_used
       
    58 enum TPbk2NameSeparatorUsage
       
    59     {
       
    60     EPbk2NameSepartorNotUsed,
       
    61     EPbk2NameSepartorUsed
       
    62     };
       
    63 
       
    64 /// These enumerations must have same values as in the specification
       
    65 /// and in the qtn_phob_name_order. These are different compared to
       
    66 /// TPbk2NameOrderInCenRep because TPbk2NameOrderInCenRep values
       
    67 /// must be same as in old PbkEng TPbkNameOrder to keep data compatibility
       
    68 /// of the CenRep.
       
    69 enum TPbk2NameOrderInUiSpecification
       
    70     {
       
    71     EPbk2UiSpecFirstNameLastName,
       
    72     EPbk2UiSpecLastNameFirstName
       
    73     };
       
    74 
       
    75 #ifdef _DEBUG
       
    76 
       
    77 enum TPanicCode
       
    78     {
       
    79     EPanicInvalidNameConfiguration
       
    80     };
       
    81 
       
    82 void Panic(TPanicCode aReason)
       
    83     {
       
    84     _LIT(KPanicText, "CPbk2SortOrderManagerImpl");
       
    85     User::Panic(KPanicText,aReason);
       
    86     }
       
    87 
       
    88 #endif // _DEBUG
       
    89 
       
    90 
       
    91 /**
       
    92  * Gets a digit from a descriptor.
       
    93  *
       
    94  * @param aReaderToBuf  Resource reader pointed to a descriptor resource.
       
    95  * @return  The digit.
       
    96  */
       
    97 TInt GetDigitFromDescriptorL( TResourceReader& aReaderToBuf )
       
    98     {
       
    99     HBufC* orderBuf = aReaderToBuf.ReadHBufCL();
       
   100     CleanupStack::PushL( orderBuf );
       
   101 
       
   102     // The setting should be 1 digit
       
   103     __ASSERT_DEBUG( orderBuf->Length() == 1,
       
   104         Panic( EPanicInvalidNameConfiguration ) );
       
   105 
       
   106     TInt result = KErrNotFound;
       
   107     TPtrC ptr( orderBuf->Des() );
       
   108     if ( ptr.Length() > 0 && TChar(ptr[0]).IsDigit() )
       
   109         {
       
   110         result = TChar(ptr[0]).GetNumericValue();
       
   111         }
       
   112 
       
   113     CleanupStack::PopAndDestroy( orderBuf );
       
   114     return result;
       
   115     }
       
   116 
       
   117 } /// namespace
       
   118 
       
   119 
       
   120 // --------------------------------------------------------------------------
       
   121 // CPbk2SortOrderManagerImpl::CPbk2SortOrderManagerImpl
       
   122 // --------------------------------------------------------------------------
       
   123 //
       
   124 CPbk2SortOrderManagerImpl::CPbk2SortOrderManagerImpl
       
   125         (const MVPbkFieldTypeList& aMasterFieldTypeList) :
       
   126             iMasterFieldTypeList(aMasterFieldTypeList)
       
   127     {
       
   128     }
       
   129 
       
   130 // --------------------------------------------------------------------------
       
   131 // CPbk2SortOrderManagerImpl::~CPbk2SortOrderManagerImpl
       
   132 // --------------------------------------------------------------------------
       
   133 //
       
   134 CPbk2SortOrderManagerImpl::~CPbk2SortOrderManagerImpl()
       
   135     {
       
   136     if (iContactView)
       
   137         {
       
   138         iContactView->RemoveObserver(*this);
       
   139         }
       
   140     iObservers.Reset();
       
   141     delete iSortOrderMonitor;
       
   142     delete iSeparatorMonitor;
       
   143     delete iSortOrder;
       
   144     delete iSeparator;
       
   145     delete iDefaultSeparator;
       
   146     delete iSortOrderSettings;
       
   147      if(iFeatureManagerInitilized)
       
   148         {
       
   149         FeatureManager::UnInitializeLib();
       
   150         }
       
   151     }
       
   152 
       
   153 // --------------------------------------------------------------------------
       
   154 // CPbk2SortOrderManagerImpl::NewL
       
   155 // --------------------------------------------------------------------------
       
   156 //
       
   157 CPbk2SortOrderManagerImpl* CPbk2SortOrderManagerImpl::NewL(
       
   158         const MVPbkFieldTypeList& aMasterFieldTypeList, RFs* aFs )
       
   159     {
       
   160     CPbk2SortOrderManagerImpl* self =
       
   161         new (ELeave) CPbk2SortOrderManagerImpl(aMasterFieldTypeList);
       
   162     CleanupStack::PushL(self);
       
   163     self->ConstructL( aFs );
       
   164     CleanupStack::Pop(self);
       
   165     return self;
       
   166     }
       
   167 
       
   168 // --------------------------------------------------------------------------
       
   169 // CPbk2SortOrderManagerImpl::ConstructL
       
   170 // --------------------------------------------------------------------------
       
   171 //
       
   172 inline void CPbk2SortOrderManagerImpl::ConstructL( RFs* aFs )
       
   173     {
       
   174     FeatureManager::InitializeLibL();
       
   175     iFeatureManagerInitilized = ETrue;
       
   176     iSortOrderSettings = CRepository::NewL(TUid::Uid(KCRUidPhonebook));
       
   177 
       
   178     // Open resource file for reading language specific default values
       
   179     RPbk2LocalizedResourceFile resFile( aFs );
       
   180     resFile.OpenLC( KPbk2RomFileDrive,
       
   181         KDC_RESOURCE_FILES_DIR, 
       
   182         Pbk2PresentationUtils::PresentationResourceFile() );
       
   183 
       
   184     // Check and set separator usage
       
   185 
       
   186     // 1) Read language specific default separator
       
   187     iDefaultSeparator = DefaultSeparatorConfigurationL( resFile );
       
   188 
       
   189     // 2) Read separator from Central Repository
       
   190     HBufC* separator = PersistentSeparatorL();
       
   191 
       
   192     // 3) Check that is it defined
       
   193     if ( separator->Length() == 0 )
       
   194         {
       
   195         // Not defined
       
   196         delete separator;
       
   197         // 4) Check if language specific configuration is defined
       
   198         // and use that if it is. Otherwise use space as separator
       
   199         if ( IsDefaultSeparatorConfiguredL( resFile ) )
       
   200             {
       
   201             separator = iDefaultSeparator->AllocL();
       
   202             }
       
   203         else
       
   204             {
       
   205             separator = KSpace().AllocL();
       
   206             }
       
   207         }
       
   208     iSeparator = separator;
       
   209 
       
   210     // Get language specific default name order
       
   211     iDefaultNameOrder = DefaultNameDisplayOrderConfigurationL( resFile );
       
   212     // Get current name order
       
   213     TPbk2NameOrderInCenRep cenRepOrder = PersistentNameDisplayOrder();
       
   214     if ( cenRepOrder == EPbk2CenRepNameOrderUndefined )
       
   215         {
       
   216         // Order was undefined in Central Repository -> use language
       
   217         // specfic default configuration
       
   218         cenRepOrder = iDefaultNameOrder;
       
   219         }
       
   220 
       
   221     // Convert ordering from cenrep format to external format and
       
   222     // create sort order
       
   223     iSortOrder = CreateSortOrderL( ConvertNameDisplayOrder( cenRepOrder ),
       
   224          &resFile );    
       
   225     CleanupStack::PopAndDestroy(); // resFile
       
   226 
       
   227     // Start listen to CenRep changes because settings can be changed e.g
       
   228     // using Phonebook1 engine API
       
   229     iSortOrderMonitor = new ( ELeave ) CSortOrderMonitor
       
   230         ( *iSortOrderSettings, *this );
       
   231     iSeparatorMonitor = new ( ELeave ) CSeparatorMonitor
       
   232         ( *iSortOrderSettings, *this );
       
   233     iSortOrderMonitor->ActivateL();
       
   234     iSeparatorMonitor->ActivateL();
       
   235     }
       
   236 
       
   237 // --------------------------------------------------------------------------
       
   238 // CPbk2SortOrderManagerImpl::SetContactViewL
       
   239 // --------------------------------------------------------------------------
       
   240 //
       
   241 void CPbk2SortOrderManagerImpl::SetContactViewL
       
   242         (MVPbkContactViewBase& aContactView)
       
   243     {
       
   244     if (iContactView)
       
   245         {
       
   246         iContactView->RemoveObserver(*this);
       
   247         }
       
   248     iContactView = &aContactView;
       
   249     iContactView->AddObserverL(*this);
       
   250 
       
   251     delete iSortOrder;
       
   252     iSortOrder = NULL;
       
   253     iSortOrder = CVPbkSortOrder::NewL(iContactView->SortOrder());
       
   254     iSortOrder->SetReserveL(KPbk2MaxNumberOfFieldsInSortOrder);
       
   255     }
       
   256 
       
   257 // --------------------------------------------------------------------------
       
   258 // CPbk2SortOrderManagerImpl::AddObserverL
       
   259 // --------------------------------------------------------------------------
       
   260 //
       
   261 void CPbk2SortOrderManagerImpl::AddObserverL
       
   262         (MPbk2SortOrderObserver& aObserver)
       
   263     {
       
   264     User::LeaveIfError(iObservers.Append(&aObserver));
       
   265     }
       
   266 
       
   267 // --------------------------------------------------------------------------
       
   268 // CPbk2SortOrderManagerImpl::RemoveObserver
       
   269 // --------------------------------------------------------------------------
       
   270 //
       
   271 void CPbk2SortOrderManagerImpl::RemoveObserver
       
   272         (MPbk2SortOrderObserver& aObserver)
       
   273     {
       
   274     const TInt index = FindObserver(aObserver);
       
   275     if (index != KErrNotFound)
       
   276         {
       
   277         iObservers.Remove(index);
       
   278         }
       
   279     }
       
   280 
       
   281 // --------------------------------------------------------------------------
       
   282 // CPbk2SortOrderManagerImpl::SetNameDisplayOrderL
       
   283 // --------------------------------------------------------------------------
       
   284 //
       
   285 void CPbk2SortOrderManagerImpl::SetNameDisplayOrderL
       
   286         ( CPbk2SortOrderManager::TPbk2NameDisplayOrder aNameDisplayOrder,
       
   287           const TDesC& aSeparator )
       
   288     {
       
   289     // Create new sort order object
       
   290     CVPbkSortOrder* newSortOrder =
       
   291         CreateSortOrderLC( aNameDisplayOrder, NULL );
       
   292     newSortOrder->SetReserveL( KPbk2MaxNumberOfFieldsInSortOrder );
       
   293 
       
   294     TBool sortOrderChanged = 
       
   295             !VPbkFieldTypeList::IsSame( *iSortOrder, *newSortOrder );
       
   296     if ( sortOrderChanged )
       
   297         {
       
   298         // Set new sort order to cenrep
       
   299         TInt res = SetPersistentNameDisplayOrder(
       
   300             ConvertNameDisplayOrder( aNameDisplayOrder) );
       
   301         if ( res == KErrNone )
       
   302             {
       
   303             delete iSortOrder;
       
   304             iSortOrder = newSortOrder;
       
   305             CleanupStack::Pop( newSortOrder );
       
   306             newSortOrder = NULL;
       
   307             
       
   308             // If view is set then try change its sort order
       
   309             if ( iContactView )
       
   310                 {
       
   311                 iContactView->ChangeSortOrderL( *iSortOrder );
       
   312                 }
       
   313             if(FeatureManager::FeatureSupported(KFeatureIdFfContactsPredictiveSearch))
       
   314                 {
       
   315                 NotifyPsEngineAboutSortOrderChangeL();
       
   316                 }
       
   317             }
       
   318         else
       
   319             {
       
   320             User::Leave( res );
       
   321             }
       
   322         }
       
   323             
       
   324     if ( newSortOrder )
       
   325         {
       
   326         CleanupStack::PopAndDestroy( newSortOrder );
       
   327         }
       
   328     
       
   329     // Set separator
       
   330     SetSeparatorL( aSeparator );
       
   331     }
       
   332 
       
   333 // --------------------------------------------------------------------------
       
   334 // CPbk2SortOrderManagerImpl::NameDisplayOrder
       
   335 // --------------------------------------------------------------------------
       
   336 //
       
   337 CPbk2SortOrderManager::TPbk2NameDisplayOrder
       
   338         CPbk2SortOrderManagerImpl::NameDisplayOrder() const
       
   339     {
       
   340     return ConvertNameDisplayOrder( PersistentNameDisplayOrder() );
       
   341     }
       
   342 
       
   343 // --------------------------------------------------------------------------
       
   344 // CPbk2SortOrderManagerImpl::SortOrder
       
   345 // --------------------------------------------------------------------------
       
   346 //
       
   347 const MVPbkFieldTypeList& CPbk2SortOrderManagerImpl::SortOrder() const
       
   348     {
       
   349     return *iSortOrder;
       
   350     }
       
   351 
       
   352 // --------------------------------------------------------------------------
       
   353 // CPbk2SortOrderManagerImpl::DefaultSeparator
       
   354 // --------------------------------------------------------------------------
       
   355 //
       
   356 const TDesC& CPbk2SortOrderManagerImpl::DefaultSeparator() const
       
   357     {
       
   358     return *iDefaultSeparator;
       
   359     }
       
   360 
       
   361 // --------------------------------------------------------------------------
       
   362 // CPbk2SortOrderManagerImpl::CurrentSeparator
       
   363 // --------------------------------------------------------------------------
       
   364 //
       
   365 const TDesC& CPbk2SortOrderManagerImpl::CurrentSeparator() const
       
   366     {
       
   367     return *iSeparator;
       
   368     }
       
   369 
       
   370 // --------------------------------------------------------------------------
       
   371 // CPbk2SortOrderManagerImpl::ContactViewReady
       
   372 // --------------------------------------------------------------------------
       
   373 //
       
   374 void CPbk2SortOrderManagerImpl::ContactViewReady(MVPbkContactViewBase& aView)
       
   375     {
       
   376     const MVPbkFieldTypeList& sortOrder = aView.SortOrder();
       
   377     if ( !VPbkFieldTypeList::IsSame( *iSortOrder, sortOrder ) )
       
   378         {
       
   379         // Set contact view's sort order if sort orders don't match, for
       
   380         // example due to a language change reboot
       
   381         iContactView->ChangeSortOrderL( *iSortOrder );
       
   382         SendEventToObservers( &MPbk2SortOrderObserver::SortOrderChanged );
       
   383         }
       
   384     }
       
   385 
       
   386 // --------------------------------------------------------------------------
       
   387 // CPbk2SortOrderManagerImpl::ContactViewUnavailable
       
   388 // We are not interested in other events than the ContactViewReady
       
   389 // --------------------------------------------------------------------------
       
   390 //
       
   391 void CPbk2SortOrderManagerImpl::ContactViewUnavailable
       
   392         (MVPbkContactViewBase& /* aView */)
       
   393     {
       
   394     }
       
   395 
       
   396 // --------------------------------------------------------------------------
       
   397 // CPbk2SortOrderManagerImpl::ContactAddedToView
       
   398 // We are not interested in other events than the ContactViewReady
       
   399 // --------------------------------------------------------------------------
       
   400 //
       
   401 void CPbk2SortOrderManagerImpl::ContactAddedToView
       
   402         (MVPbkContactViewBase& /* aView */, TInt /* aIndex */,
       
   403         const MVPbkContactLink& /* aContactLink */)
       
   404     {
       
   405     }
       
   406 
       
   407 // --------------------------------------------------------------------------
       
   408 // CPbk2SortOrderManagerImpl::ContactRemovedFromView
       
   409 // We are not interested in other events than the ContactViewReady
       
   410 // --------------------------------------------------------------------------
       
   411 //
       
   412 void CPbk2SortOrderManagerImpl::ContactRemovedFromView
       
   413         (MVPbkContactViewBase& /* aView */, TInt /* aIndex */,
       
   414         const MVPbkContactLink& /* aContactLink */)
       
   415     {
       
   416     }
       
   417 
       
   418 // --------------------------------------------------------------------------
       
   419 // CPbk2SortOrderManagerImpl::ContactViewError
       
   420 // We are not interested in other events than the ContactViewReady
       
   421 // --------------------------------------------------------------------------
       
   422 //
       
   423 void CPbk2SortOrderManagerImpl::ContactViewError
       
   424         (MVPbkContactViewBase& /* aView */, TInt /* aError */,
       
   425         TBool /* aErrorNotified */)
       
   426     {
       
   427     }
       
   428 
       
   429 // --------------------------------------------------------------------------
       
   430 // CPbk2SortOrderManagerImpl::CenRepSortOrderChangedL
       
   431 // --------------------------------------------------------------------------
       
   432 //
       
   433 void CPbk2SortOrderManagerImpl::CenRepSortOrderChangedL()
       
   434     {
       
   435     // Sort order was changed in CenRep either by call to
       
   436     // SetNameDisplayOrderL or by call to Phonebook1 API.
       
   437     CVPbkSortOrder* newSortOrder =
       
   438         CreateSortOrderLC( NameDisplayOrder(), NULL );
       
   439     newSortOrder->SetReserveL( KPbk2MaxNumberOfFieldsInSortOrder );
       
   440 
       
   441     if ( iContactView )
       
   442         {
       
   443         // If order was changed e.g using Phonebook1 APIs
       
   444         // Phonebook2 must update all subviews for new sort order.
       
   445         iContactView->ChangeSortOrderL( *newSortOrder );
       
   446         }
       
   447 
       
   448     delete iSortOrder;
       
   449     iSortOrder = newSortOrder;
       
   450     CleanupStack::Pop( newSortOrder );
       
   451     SendEventToObservers( &MPbk2SortOrderObserver::SortOrderChanged );
       
   452     
       
   453     if(FeatureManager::FeatureSupported(KFeatureIdFfContactsPredictiveSearch))    
       
   454         {
       
   455         NotifyPsEngineAboutSortOrderChangeL();
       
   456         }
       
   457     }
       
   458 
       
   459 // --------------------------------------------------------------------------
       
   460 // CPbk2SortOrderManagerImpl::CenRepSortOrderChangeError
       
   461 // --------------------------------------------------------------------------
       
   462 //
       
   463 void CPbk2SortOrderManagerImpl::CenRepSortOrderChangeError( TInt /*aError*/ )
       
   464     {
       
   465     // This is called if CenRepSortOrderChangedL leaves. Don't send message
       
   466     // because iSortOrder was not changed.
       
   467     }
       
   468 
       
   469 // --------------------------------------------------------------------------
       
   470 // CPbk2SortOrderManagerImpl::CenRepSeparatorChangedL
       
   471 // --------------------------------------------------------------------------
       
   472 //
       
   473 void CPbk2SortOrderManagerImpl::CenRepSeparatorChangedL()
       
   474     {
       
   475     // Separator was changed in CenRep either by call to
       
   476     // SetNameDisplayOrderL or  Phonebook1 API.
       
   477 
       
   478     // Get separator from CenRep
       
   479     HBufC* cenRepSeparator = PersistentSeparatorL();
       
   480     CleanupStack::PushL( cenRepSeparator );
       
   481     SetSeparatorL( *cenRepSeparator );
       
   482     CleanupStack::PopAndDestroy( cenRepSeparator );
       
   483     SendEventToObservers(
       
   484         &MPbk2SortOrderObserver::SortOrderChanged );
       
   485     }
       
   486 
       
   487 // --------------------------------------------------------------------------
       
   488 // CPbk2SortOrderManagerImpl::CenRepSeparatorChangeError
       
   489 // --------------------------------------------------------------------------
       
   490 //
       
   491 void CPbk2SortOrderManagerImpl::CenRepSeparatorChangeError( TInt /*aError*/ )
       
   492     {
       
   493     // This is called when CenRepSeparatorChangedL leaves. The error
       
   494     // is ignored because iSeparator is not changed if SetSeparatorL
       
   495     // leaves.
       
   496     }
       
   497 
       
   498 // --------------------------------------------------------------------------
       
   499 // CPbk2SortOrderManagerImpl::SendEventToObservers
       
   500 // --------------------------------------------------------------------------
       
   501 //
       
   502 void CPbk2SortOrderManagerImpl::SendEventToObservers
       
   503         (Pbk2SortOrderObserverEvent aEvent)
       
   504     {
       
   505     const TInt count = iObservers.Count();
       
   506     for (TInt i = 0; i < count; ++i)
       
   507         {
       
   508         (iObservers[i]->*aEvent)();
       
   509         }
       
   510     }
       
   511 
       
   512 // --------------------------------------------------------------------------
       
   513 // CPbk2SortOrderManagerImpl::FindObserver
       
   514 // --------------------------------------------------------------------------
       
   515 //
       
   516 TInt CPbk2SortOrderManagerImpl::FindObserver
       
   517         (MPbk2SortOrderObserver& aObserver)
       
   518     {
       
   519     TInt result = KErrNotFound;
       
   520 
       
   521     const TInt count = iObservers.Count();
       
   522     for (TInt i = 0; i < count; ++i)
       
   523         {
       
   524         if (iObservers[i] == &aObserver)
       
   525             {
       
   526             result = i;
       
   527             break;
       
   528             }
       
   529         }
       
   530     return result;
       
   531     }
       
   532 
       
   533 // --------------------------------------------------------------------------
       
   534 // CPbk2SortOrderManagerImpl::CreateSortOrderL
       
   535 // --------------------------------------------------------------------------
       
   536 //
       
   537 CVPbkSortOrder* CPbk2SortOrderManagerImpl::CreateSortOrderL(
       
   538         CPbk2SortOrderManager::TPbk2NameDisplayOrder aNameDisplayOrder,
       
   539         RResourceFile* aResFile )
       
   540     {
       
   541     TInt resId = R_DEFAULT_NAME_DISPLAY_ORDER_LASTNAME_FIRSTNAME;
       
   542 
       
   543 	if ( aNameDisplayOrder ==
       
   544 		 CPbk2SortOrderManager::EPbk2NameDisplayOrderLastNameFirstName ||
       
   545 		 aNameDisplayOrder ==
       
   546 		 CPbk2SortOrderManager::EPbk2NameDisplayOrderLastNameSeparatorFirstName )
       
   547 		{
       
   548 		resId = R_DEFAULT_NAME_DISPLAY_ORDER_LASTNAME_FIRSTNAME;
       
   549 		}
       
   550 	else
       
   551 		{
       
   552 		resId = R_DEFAULT_NAME_DISPLAY_ORDER_FIRSTNAME_LASTNAME;
       
   553 		}
       
   554 
       
   555     TResourceReader reader;
       
   556     if ( !aResFile )
       
   557         {
       
   558         RPbk2LocalizedResourceFile resFile;
       
   559         resFile.OpenLC( KPbk2RomFileDrive, KDC_RESOURCE_FILES_DIR,
       
   560             Pbk2PresentationUtils::PresentationResourceFile() );
       
   561         HBufC8* buffer = resFile.AllocReadL( resId );
       
   562         CleanupStack::PopAndDestroy(); // resFile
       
   563         CleanupStack::PushL( buffer );
       
   564         reader.SetBuffer( buffer );
       
   565         }
       
   566     else
       
   567         {
       
   568         reader.SetBuffer( aResFile->AllocReadLC( resId ) );
       
   569         }
       
   570 
       
   571     CVPbkSortOrder* result = CVPbkSortOrder::NewL( reader,
       
   572         iMasterFieldTypeList );
       
   573 
       
   574     CleanupStack::PopAndDestroy(); // AllocReadLC
       
   575     return result;
       
   576     }
       
   577 
       
   578 // --------------------------------------------------------------------------
       
   579 // CPbk2SortOrderManagerImpl::CreateSortOrderLC
       
   580 // --------------------------------------------------------------------------
       
   581 //
       
   582 CVPbkSortOrder* CPbk2SortOrderManagerImpl::CreateSortOrderLC(
       
   583         CPbk2SortOrderManager::TPbk2NameDisplayOrder aNameDisplayOrder,
       
   584         RResourceFile* aResFile )
       
   585     {
       
   586     CVPbkSortOrder* result = CreateSortOrderL( aNameDisplayOrder, aResFile );
       
   587     CleanupStack::PushL( result );
       
   588     return result;
       
   589     }
       
   590 
       
   591 // --------------------------------------------------------------------------
       
   592 // CPbk2SortOrderManagerImpl::PersistentNameDisplayOrder
       
   593 // --------------------------------------------------------------------------
       
   594 //
       
   595 TPbk2NameOrderInCenRep
       
   596         CPbk2SortOrderManagerImpl::PersistentNameDisplayOrder() const
       
   597     {
       
   598     TInt result = EPbk2CenRepNameOrderUndefined;
       
   599     // Ignore error and return a default order in that case
       
   600     /*TInt err = */iSortOrderSettings->Get(KPhonebookNameOrdering, result);
       
   601     return TPbk2NameOrderInCenRep( result );
       
   602     }
       
   603 
       
   604 // --------------------------------------------------------------------------
       
   605 // CPbk2SortOrderManagerImpl::SetPersistentNameDisplayOrder
       
   606 // --------------------------------------------------------------------------
       
   607 //
       
   608 TInt CPbk2SortOrderManagerImpl::SetPersistentNameDisplayOrder(
       
   609         TPbk2NameOrderInCenRep aNameDisplayOrder)
       
   610     {
       
   611     return iSortOrderSettings->Set( KPhonebookNameOrdering,
       
   612         TInt( aNameDisplayOrder ) );
       
   613     }
       
   614 
       
   615 // --------------------------------------------------------------------------
       
   616 // CPbk2SortOrderManagerImpl::DefaultNameDisplayOrderConfigurationL
       
   617 // --------------------------------------------------------------------------
       
   618 //
       
   619 TPbk2NameOrderInCenRep
       
   620         CPbk2SortOrderManagerImpl::DefaultNameDisplayOrderConfigurationL(
       
   621         RResourceFile& aResFile ) const
       
   622     {
       
   623     TResourceReader reader;
       
   624     reader.SetBuffer( aResFile.AllocReadLC( R_QTN_PHOB_NAME_ORDER ) );
       
   625     TInt order = GetDigitFromDescriptorL( reader );
       
   626     CleanupStack::PopAndDestroy(); // reader
       
   627 
       
   628     /// This function is responsibe of converting from
       
   629     /// TPbk2NameOrderInUiSpecification to TPbk2NameOrderInCenRep
       
   630     TPbk2NameOrderInCenRep result = EPbk2CenRepLastNameFirstName;
       
   631     if ( order != KErrNotFound && order == EPbk2UiSpecFirstNameLastName )
       
   632         {
       
   633         result = EPbk2CenRepFirstNameLastName;
       
   634         }
       
   635     return result;
       
   636     }
       
   637 
       
   638 // --------------------------------------------------------------------------
       
   639 // CPbk2SortOrderManagerImpl::ConvertNameDisplayOrder
       
   640 // --------------------------------------------------------------------------
       
   641 //
       
   642 CPbk2SortOrderManager::TPbk2NameDisplayOrder
       
   643         CPbk2SortOrderManagerImpl::ConvertNameDisplayOrder(
       
   644             TPbk2NameOrderInCenRep aCenRepOrder ) const
       
   645     {
       
   646     TPbk2NameOrderInCenRep order = aCenRepOrder;
       
   647     if ( order == EPbk2CenRepNameOrderUndefined )
       
   648         {
       
   649         // If not defined in CenRep then use the loc-file specified setting
       
   650         order = iDefaultNameOrder;
       
   651         }
       
   652         
       
   653     CPbk2SortOrderManager::TPbk2NameDisplayOrder result =
       
   654         CPbk2SortOrderManager::EPbk2NameDisplayOrderFirstNameLastName;
       
   655     if ( order == EPbk2CenRepLastNameFirstName )
       
   656         {
       
   657         if ( iSeparator->Compare( KSpace ) == 0 )
       
   658             {
       
   659             result =
       
   660                 CPbk2SortOrderManager::EPbk2NameDisplayOrderLastNameFirstName;
       
   661             }
       
   662         else
       
   663             {
       
   664             result =
       
   665                 CPbk2SortOrderManager::EPbk2NameDisplayOrderLastNameSeparatorFirstName;
       
   666             }
       
   667         }
       
   668     
       
   669     return result;
       
   670     }
       
   671 
       
   672 // --------------------------------------------------------------------------
       
   673 // CPbk2SortOrderManagerImpl::ConvertNameDisplayOrder
       
   674 // --------------------------------------------------------------------------
       
   675 //
       
   676 TPbk2NameOrderInCenRep
       
   677         CPbk2SortOrderManagerImpl::ConvertNameDisplayOrder(
       
   678             CPbk2SortOrderManager::TPbk2NameDisplayOrder aDisplayOrder ) const
       
   679     {
       
   680     TPbk2NameOrderInCenRep result = EPbk2CenRepLastNameFirstName;
       
   681     if ( aDisplayOrder ==
       
   682          CPbk2SortOrderManager::EPbk2NameDisplayOrderFirstNameLastName )
       
   683         {
       
   684         result = EPbk2CenRepFirstNameLastName;
       
   685         }
       
   686     return result;
       
   687     }
       
   688 
       
   689 // --------------------------------------------------------------------------
       
   690 // CPbk2SortOrderManagerImpl::PersistentSeparatorL
       
   691 // --------------------------------------------------------------------------
       
   692 //
       
   693 HBufC* CPbk2SortOrderManagerImpl::PersistentSeparatorL() const
       
   694     {
       
   695     HBufC* separator = HBufC::NewL(KMaxSeparatorLength);
       
   696     TPtr separatorPtr = separator->Des();
       
   697     TInt err = iSortOrderSettings->Get
       
   698         (KPhonebookFormattingSeparator, separatorPtr);
       
   699     if (err != KErrNone)
       
   700         {
       
   701         // Return zero length descriptor if separator not defined in
       
   702         // Central Repository
       
   703         delete separator;
       
   704         separator = KNullDesC().AllocL();
       
   705         }
       
   706 
       
   707     return separator;
       
   708     }
       
   709 
       
   710 // --------------------------------------------------------------------------
       
   711 // CPbk2SortOrderManagerImpl::IsDefaultSeparatorConfiguredL
       
   712 // --------------------------------------------------------------------------
       
   713 //
       
   714 TBool CPbk2SortOrderManagerImpl::IsDefaultSeparatorConfiguredL(
       
   715         RResourceFile& aResFile )
       
   716     {
       
   717     TResourceReader reader;
       
   718     reader.SetBuffer( aResFile.AllocReadLC(
       
   719         R_QTN_PHOB_NAME_SEPARATOR_USED ) );
       
   720     TInt used = GetDigitFromDescriptorL( reader );
       
   721     CleanupStack::PopAndDestroy(); // reader
       
   722 
       
   723     if ( used == EPbk2NameSepartorUsed )
       
   724         {
       
   725         return ETrue;
       
   726         }
       
   727     return EFalse;
       
   728     }
       
   729 
       
   730 // --------------------------------------------------------------------------
       
   731 // CPbk2SortOrderManagerImpl::DefaultSeparatorConfigurationL
       
   732 // --------------------------------------------------------------------------
       
   733 //
       
   734 HBufC* CPbk2SortOrderManagerImpl::DefaultSeparatorConfigurationL(
       
   735         RResourceFile& aResFile ) const
       
   736     {
       
   737     TResourceReader reader;
       
   738     reader.SetBuffer( aResFile.AllocReadLC(
       
   739         R_QTN_PHOB_NAME_SEPARATOR_CHAR ) );
       
   740     HBufC* separator = reader.ReadHBufCL();
       
   741     CleanupStack::PopAndDestroy(); // AllocReadLC
       
   742     return separator;
       
   743     }
       
   744 
       
   745 // --------------------------------------------------------------------------
       
   746 // CPbk2SortOrderManagerImpl::SetSeparatorL
       
   747 // --------------------------------------------------------------------------
       
   748 //
       
   749 TBool CPbk2SortOrderManagerImpl::SetSeparatorL
       
   750         ( const TDesC& aSeparator )
       
   751     {
       
   752     TBool separatorChanged = EFalse;
       
   753 
       
   754     HBufC* separator = NULL;
       
   755     if ( aSeparator.Length() == 0 )
       
   756         {
       
   757         separator = KSpace().AllocLC();
       
   758         }
       
   759     else
       
   760         {
       
   761         separator = aSeparator.AllocLC();
       
   762         }
       
   763 
       
   764     User::LeaveIfError( SetPersistentSeparator( *separator ) );
       
   765     CleanupStack::Pop(); //separator
       
   766 
       
   767     if ( iSeparator && iSeparator->CompareC( *separator ) != 0 )
       
   768         {
       
   769         separatorChanged = ETrue;
       
   770         }
       
   771     else if ( !iSeparator && separator )
       
   772         {
       
   773         separatorChanged = ETrue;
       
   774         }
       
   775 
       
   776     delete iSeparator;
       
   777     iSeparator = separator;
       
   778 
       
   779     return separatorChanged;
       
   780     }
       
   781 
       
   782 // --------------------------------------------------------------------------
       
   783 // CPbk2SortOrderManagerImpl::SetPersistentSeparator
       
   784 // --------------------------------------------------------------------------
       
   785 //
       
   786 TInt CPbk2SortOrderManagerImpl::SetPersistentSeparator
       
   787         ( const TDesC& aSeparator ) const
       
   788     {
       
   789     return iSortOrderSettings->Set
       
   790         ( KPhonebookFormattingSeparator, aSeparator );
       
   791     }
       
   792 
       
   793 // --------------------------------------------------------------------------
       
   794 // CPbk2SortOrderManagerImpl::NotifyPsEngineAboutSortOrderChangeL
       
   795 // --------------------------------------------------------------------------
       
   796 //
       
   797 void CPbk2SortOrderManagerImpl::NotifyPsEngineAboutSortOrderChangeL() const
       
   798     {
       
   799     CPSRequestHandler* psHandler = CPSRequestHandler::NewLC();
       
   800     RArray<TInt> psSortOrder;
       
   801     CleanupClosePushL( psSortOrder );
       
   802     TInt fieldsCount = iSortOrder->FieldTypeCount();
       
   803     for ( TInt i = 0; i < fieldsCount; i++ )
       
   804         {
       
   805     	psSortOrder.AppendL( iSortOrder->FieldTypeAt(i).FieldTypeResId() );
       
   806         }
       
   807     psHandler->ChangeSortOrderL( KVPbkDefaultCntDbURI, psSortOrder );
       
   808     CleanupStack::PopAndDestroy( 2 ); //psHandler, psSortOrder 
       
   809     }
       
   810 
       
   811 // --------------------------------------------------------------------------
       
   812 // CPbk2SortOrderManagerImpl::CSortOrderMonitor::CSortOrderMonitor
       
   813 // --------------------------------------------------------------------------
       
   814 //
       
   815 CPbk2SortOrderManagerImpl::CSortOrderMonitor::CSortOrderMonitor(
       
   816         CRepository& aRepository, MPbk2CenRepSortOrderObserver& aObserver )
       
   817         :   CActive( EPriorityStandard ),
       
   818             iRepository( aRepository ),
       
   819             iObserver( aObserver )
       
   820     {
       
   821     CActiveScheduler::Add( this );
       
   822     }
       
   823 
       
   824 // --------------------------------------------------------------------------
       
   825 // CPbk2SortOrderManagerImpl::CSortOrderMonitor::~CSortOrderMonitor
       
   826 // --------------------------------------------------------------------------
       
   827 //
       
   828 CPbk2SortOrderManagerImpl::CSortOrderMonitor::~CSortOrderMonitor()
       
   829     {
       
   830     Cancel();
       
   831     }
       
   832 
       
   833 // --------------------------------------------------------------------------
       
   834 // CPbk2SortOrderManagerImpl::CSortOrderMonitor::ActivateL
       
   835 // --------------------------------------------------------------------------
       
   836 //
       
   837 void CPbk2SortOrderManagerImpl::CSortOrderMonitor::ActivateL()
       
   838     {
       
   839     User::LeaveIfError( iRepository.NotifyRequest( KPhonebookNameOrdering,
       
   840         iStatus ) );
       
   841     SetActive();
       
   842     }
       
   843 
       
   844 // --------------------------------------------------------------------------
       
   845 // CPbk2SortOrderManagerImpl::CSortOrderMonitor::RunL
       
   846 // --------------------------------------------------------------------------
       
   847 //
       
   848 void CPbk2SortOrderManagerImpl::CSortOrderMonitor::RunL()
       
   849     {
       
   850     iObserver.CenRepSortOrderChangedL();
       
   851     ActivateL();
       
   852     }
       
   853 
       
   854 // --------------------------------------------------------------------------
       
   855 // CPbk2SortOrderManagerImpl::CSortOrderMonitor::DoCancel
       
   856 // --------------------------------------------------------------------------
       
   857 //
       
   858 void CPbk2SortOrderManagerImpl::CSortOrderMonitor::DoCancel()
       
   859     {
       
   860     iRepository.NotifyCancel( KPhonebookNameOrdering );
       
   861     }
       
   862 
       
   863 // --------------------------------------------------------------------------
       
   864 // CPbk2SortOrderManagerImpl::CSortOrderMonitor::RunError
       
   865 // --------------------------------------------------------------------------
       
   866 //
       
   867 TInt CPbk2SortOrderManagerImpl::CSortOrderMonitor::RunError( TInt aError )
       
   868     {
       
   869     iObserver.CenRepSortOrderChangeError( aError );
       
   870     return KErrNone;
       
   871     }
       
   872 
       
   873 // --------------------------------------------------------------------------
       
   874 // CPbk2SortOrderManagerImpl::CSeparatorMonitor::CSeparatorMonitor
       
   875 // --------------------------------------------------------------------------
       
   876 //
       
   877 CPbk2SortOrderManagerImpl::CSeparatorMonitor::CSeparatorMonitor(
       
   878         CRepository& aRepository, MPbk2CenRepSeparatorObserver& aObserver )
       
   879         :   CActive( EPriorityStandard ),
       
   880             iRepository( aRepository ),
       
   881             iObserver( aObserver )
       
   882     {
       
   883     CActiveScheduler::Add( this );
       
   884     }
       
   885 
       
   886 // --------------------------------------------------------------------------
       
   887 // CPbk2SortOrderManagerImpl::CSeparatorMonitor::~CSeparatorMonitor
       
   888 // --------------------------------------------------------------------------
       
   889 //
       
   890 CPbk2SortOrderManagerImpl::CSeparatorMonitor::~CSeparatorMonitor()
       
   891     {
       
   892     Cancel();
       
   893     }
       
   894 
       
   895 // --------------------------------------------------------------------------
       
   896 // CPbk2SortOrderManagerImpl::CSeparatorMonitor::ActivateL
       
   897 // --------------------------------------------------------------------------
       
   898 //
       
   899 void CPbk2SortOrderManagerImpl::CSeparatorMonitor::ActivateL()
       
   900     {
       
   901     User::LeaveIfError( iRepository.NotifyRequest(
       
   902         KPhonebookFormattingSeparator, iStatus ) );
       
   903     SetActive();
       
   904     }
       
   905 
       
   906 // --------------------------------------------------------------------------
       
   907 // CPbk2SortOrderManagerImpl::CSeparatorMonitor::RunL
       
   908 // --------------------------------------------------------------------------
       
   909 //
       
   910 void CPbk2SortOrderManagerImpl::CSeparatorMonitor::RunL()
       
   911     {
       
   912     iObserver.CenRepSeparatorChangedL();
       
   913     ActivateL();
       
   914     }
       
   915 
       
   916 // --------------------------------------------------------------------------
       
   917 // CPbk2SortOrderManagerImpl::CSeparatorMonitor::DoCancel
       
   918 // --------------------------------------------------------------------------
       
   919 //
       
   920 void CPbk2SortOrderManagerImpl::CSeparatorMonitor::DoCancel()
       
   921     {
       
   922     iRepository.NotifyCancel( KPhonebookFormattingSeparator );
       
   923     }
       
   924 
       
   925 // --------------------------------------------------------------------------
       
   926 // CPbk2SortOrderManagerImpl::CSeparatorMonitor::RunError
       
   927 // --------------------------------------------------------------------------
       
   928 //
       
   929 TInt CPbk2SortOrderManagerImpl::CSeparatorMonitor::RunError( TInt aError )
       
   930     {
       
   931     iObserver.CenRepSeparatorChangeError( aError );
       
   932     return KErrNone;
       
   933     }
       
   934 
       
   935 // End of File