phonebookui/Phonebook2/Presentation/src/CPbk2ContactNameFormatter.cpp
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
child 68 9da50d567e3c
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2002-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: 
       
    15 *     Default contact name formatter.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "CPbk2ContactNameFormatter.h"
       
    21 #include <CVPbkContactFieldIterator.h>
       
    22 #include <MVPbkFieldType.h>
       
    23 #include <CVPbkFieldTypeRefsList.h>
       
    24 #include <MVPbkContactFieldData.h>
       
    25 #include <MVPbkContactFieldTextData.h>
       
    26 
       
    27 #include <VPbkEng.rsg>
       
    28 
       
    29 
       
    30 
       
    31 
       
    32 namespace {
       
    33 _LIT(KSeparator, ",");
       
    34 enum TNameOrder
       
    35     {
       
    36     ETopContactOrderNumber = 0,     //TC control data, not shown
       
    37     ENameFirstPart,                 //Contact name data
       
    38     ENameSecondPart,                //Contact name data
       
    39     ENameCompanyPart                //to support Company name
       
    40     };
       
    41 } // namespace
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CPbk2ContactNameFormatter::CPbk2ContactNameFormatter
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 CPbk2ContactNameFormatter::CPbk2ContactNameFormatter(
       
    48     const MVPbkFieldTypeList& aMasterFieldTypeList,
       
    49     const CPbk2SortOrderManager& aSortOrderManager)
       
    50     :   CPbk2ContactNameFormatterBase( aMasterFieldTypeList,
       
    51             aSortOrderManager )
       
    52     {
       
    53     }
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // CPbk2ContactNameFormatter::NewL
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 CPbk2ContactNameFormatter* CPbk2ContactNameFormatter::NewL
       
    60     (
       
    61     const TDesC& aUnnamedText,
       
    62     const MVPbkFieldTypeList& aMasterFieldTypeList,
       
    63     const CPbk2SortOrderManager& aSortOrderManager,
       
    64     CVPbkFieldTypeSelector* aTitleFieldSelector
       
    65     )
       
    66     {
       
    67     CPbk2ContactNameFormatter* self = new(ELeave) CPbk2ContactNameFormatter
       
    68         (aMasterFieldTypeList, aSortOrderManager);
       
    69     CleanupStack::PushL(self);
       
    70     self->ConstructL( aUnnamedText, aTitleFieldSelector );
       
    71     CleanupStack::Pop(self);
       
    72     return self;
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CPbk2ContactNameFormatter::ConstructL
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 void CPbk2ContactNameFormatter::ConstructL
       
    80         (const TDesC& aUnnamedText,
       
    81          CVPbkFieldTypeSelector* aTitleFieldSelector )
       
    82     {
       
    83     // Base class takes ownership of aTitleFieldSelector.
       
    84     // Must not leave after BaseConstructL
       
    85     BaseConstructL( aUnnamedText, aTitleFieldSelector );
       
    86     }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CPbk2ContactNameFormatter::~CPbk2ContactNameFormatter
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 CPbk2ContactNameFormatter::~CPbk2ContactNameFormatter()
       
    93     {
       
    94     }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CPbk2ContactNameFormatter::GetContactTitle
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void CPbk2ContactNameFormatter::GetContactTitle
       
   101         (const MVPbkBaseContactFieldCollection& aContactFields,
       
   102         TDes& aTitle, TUint32 aFormattingFlags)
       
   103     {    
       
   104     aTitle.Zero();
       
   105     SetFieldMapper( aContactFields );
       
   106 
       
   107     // Try to create title using first two fields of the sort order
       
   108     // If the field type in the sort order is not defined as a title field
       
   109     // then it's not used for title creation.
       
   110     if ( !AreTitleFieldsEmpty( ENameFirstPart, ENameSecondPart ) )
       
   111         {
       
   112         DoGetContactTitle(aTitle, aFormattingFlags,
       
   113             ENameFirstPart, ENameSecondPart);
       
   114         }
       
   115     else
       
   116         { 
       
   117         // If there was no data in the first two fields then
       
   118         // use the third and the rest of the fields in the sort order
       
   119         // for title creation.
       
   120         // Find the next field in the sort order that has
       
   121         // data to build the title
       
   122         const TInt count = iFieldMapper.FieldCount();
       
   123         DoGetContactTitle(aTitle, aFormattingFlags,
       
   124             ENameSecondPart+1 , count-1); // zero-based
       
   125         }
       
   126     }
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // CPbk2ContactNameFormatter::MaxTitleLengthWithCompanyName
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 TInt CPbk2ContactNameFormatter::MaxTitleLengthWithCompanyName(
       
   133     const MVPbkBaseContactFieldCollection& aContactFields,
       
   134     const TUint32 aFormattingFlags )
       
   135     {
       
   136     TInt result = 0;
       
   137     SetFieldMapper( aContactFields );
       
   138      
       
   139      if ( !AreTitleFieldsEmpty( ENameFirstPart, ENameCompanyPart ) )
       
   140          {
       
   141          result = DoCalculateMaxTitleLength
       
   142              (aFormattingFlags, ENameFirstPart, ENameCompanyPart);
       
   143          }
       
   144      else
       
   145          {
       
   146          // Use the rest of the fields in sort order to
       
   147          // build the title (ie. the company name)
       
   148          const TInt count = iFieldMapper.FieldCount();
       
   149          result = DoCalculateMaxTitleLength
       
   150              (aFormattingFlags, ENameCompanyPart+1, count-1); // zero-based
       
   151          }
       
   152     
       
   153      if ( !result || aFormattingFlags & MPbk2ContactNameFormatter::EDisableCompanyNameSeparator  )
       
   154          {
       
   155          return result;
       
   156          }
       
   157      return result+1; // for seperator ","
       
   158     }
       
   159 // -----------------------------------------------------------------------------
       
   160 // CPbk2ContactNameFormatter::GetContactTitleWithCompanyNameL
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 HBufC* CPbk2ContactNameFormatter::GetContactTitleWithCompanyNameL(
       
   164         const MVPbkBaseContactFieldCollection& aContactFields,
       
   165         TUint32 aFormattingFlags )
       
   166     {
       
   167     HBufC* result = HBufC::NewL
       
   168          (MaxTitleLengthWithCompanyName(aContactFields, aFormattingFlags));
       
   169     TPtr resultPtr = result->Des();
       
   170   
       
   171     GetContactTitleWithCompanyName(aContactFields, resultPtr, aFormattingFlags);
       
   172 
       
   173     if (resultPtr.Length() == 0)
       
   174         {
       
   175         delete result;
       
   176         result = NULL;
       
   177         }
       
   178 
       
   179     if (!result)
       
   180         {
       
   181         result = UnnamedText().AllocL();
       
   182         }
       
   183     return result;
       
   184     }
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CPbk2ContactNameFormatter::ContactNameFormatterExtension
       
   188 // -----------------------------------------------------------------------------
       
   189 //
       
   190 TAny* CPbk2ContactNameFormatter::ContactNameFormatterExtension( 
       
   191         TUid aExtensionUid )
       
   192     {
       
   193     if ( aExtensionUid == MPbk2ContactNameFormatterExtension2Uid )
       
   194            {
       
   195            return static_cast<MPbk2ContactNameFormatter2*>( this );
       
   196            }
       
   197     if ( aExtensionUid == MPbk2ContactNameFormatterExtension3Uid )
       
   198            {
       
   199            return static_cast<MPbk2ContactNameFormatter3*>( this );
       
   200            }
       
   201     return NULL;
       
   202     }
       
   203 // -----------------------------------------------------------------------------
       
   204 // CPbk2ContactNameFormatter::GetContactTitleWithCompanyName
       
   205 // Note: If changing this function, please consider also changing
       
   206 // MaxTitleLengthForFind
       
   207 // -----------------------------------------------------------------------------
       
   208 //
       
   209 void CPbk2ContactNameFormatter::GetContactTitleWithCompanyName(
       
   210         const MVPbkBaseContactFieldCollection& aContactFields,
       
   211         TDes& aTitle, TUint32 aFormattingFlags)
       
   212     {
       
   213     aTitle.Zero();
       
   214     SetFieldMapper( aContactFields );
       
   215 
       
   216     // Try to create title using first two fields of the sort order
       
   217     // If the field type in the sort order is not defined as a title field
       
   218     // then it's not used for title creation.
       
   219     if ( !AreTitleFieldsEmpty( ENameFirstPart, ENameSecondPart ) )
       
   220         {
       
   221         DoGetContactTitle(aTitle, aFormattingFlags,
       
   222             ENameFirstPart, ENameSecondPart);
       
   223         
       
   224         //try to add company name to the end of title
       
   225         //third field is expected to be company name
       
   226         if ( IsCompanyNameField() )
       
   227             {
       
   228             if ( !( aFormattingFlags & MPbk2ContactNameFormatter::EDisableCompanyNameSeparator)  )
       
   229                 {
       
   230                 aTitle.Append(KSeparator);
       
   231                 }
       
   232             DoGetContactTitle(aTitle, EPreserveLeadingSpaces,
       
   233                     ENameCompanyPart, ENameCompanyPart);
       
   234             }
       
   235         }
       
   236     else
       
   237         { 
       
   238         // If there was no data in the first two fields then
       
   239         // use the third and the rest of the fields in the sort order
       
   240         // for title creation.
       
   241         // Find the next field in the sort order that has
       
   242         // data to build the title
       
   243         const TInt count = iFieldMapper.FieldCount();
       
   244         DoGetContactTitle(aTitle, aFormattingFlags,
       
   245             ENameSecondPart+1 , count-1); // zero-based
       
   246         }
       
   247     }
       
   248        
       
   249 // -----------------------------------------------------------------------------
       
   250 // CPbk2ContactNameFormatter::GetContactTitleForFind
       
   251 // Note: If changing this function, please consider also changing
       
   252 // MaxTitleLengthForFind
       
   253 // -----------------------------------------------------------------------------
       
   254 //
       
   255 void CPbk2ContactNameFormatter::GetContactTitleForFind
       
   256         (const MVPbkBaseContactFieldCollection& aContactFields,
       
   257         TDes& aTitle, TUint32 aFormattingFlags)
       
   258     {
       
   259     // Simply uses GetContactTitle as the defined functionality is the same
       
   260     GetContactTitle( aContactFields, aTitle, aFormattingFlags );
       
   261     }
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CPbk2ContactNameFormatter::TitleWithCompanyNameFieldsLC
       
   265 // -----------------------------------------------------------------------------
       
   266 //
       
   267 CVPbkBaseContactFieldTypeListIterator* 
       
   268 CPbk2ContactNameFormatter::TitleWithCompanyNameFieldsLC(
       
   269         CVPbkFieldTypeRefsList& aFieldTypeList,
       
   270         const MVPbkBaseContactFieldCollection& aContactFields )
       
   271     {
       
   272     
       
   273     TitleFieldsL( aFieldTypeList, aContactFields, ENameFirstPart, ENameCompanyPart);
       
   274 
       
   275     return CVPbkBaseContactFieldTypeListIterator::NewLC(
       
   276             aFieldTypeList,
       
   277             aContactFields);
       
   278     
       
   279     }
       
   280 // -----------------------------------------------------------------------------
       
   281 // CPbk2ContactNameFormatter::ActualTitleFieldsLC
       
   282 // See function CPbk2ContactNameFormatter::GetContactTitle.
       
   283 // -----------------------------------------------------------------------------
       
   284 //
       
   285 CVPbkBaseContactFieldTypeListIterator*
       
   286 CPbk2ContactNameFormatter::ActualTitleFieldsLC(
       
   287         CVPbkFieldTypeRefsList& aFieldTypeList,
       
   288         const MVPbkBaseContactFieldCollection& aContactFields)
       
   289     {
       
   290 
       
   291     TitleFieldsL( aFieldTypeList, aContactFields, ENameFirstPart, ENameSecondPart);
       
   292            
       
   293     return CVPbkBaseContactFieldTypeListIterator::NewLC(
       
   294             aFieldTypeList,
       
   295             aContactFields);
       
   296     }
       
   297 // -----------------------------------------------------------------------------
       
   298 // CPbk2ContactNameFormatter::TitleFieldsL
       
   299 // -----------------------------------------------------------------------------
       
   300 //	
       
   301 void CPbk2ContactNameFormatter::TitleFieldsL( CVPbkFieldTypeRefsList& aFieldTypeList,
       
   302         const MVPbkBaseContactFieldCollection& aContactFields, 
       
   303         const TInt aStartingPosition, const TInt aEndingPosition )
       
   304     {
       
   305     aFieldTypeList.Reset();
       
   306 
       
   307     SetFieldMapper( aContactFields );
       
   308     
       
   309     DoAppendNonEmptyTitleFieldTypesL(
       
   310             aFieldTypeList,
       
   311             aStartingPosition,
       
   312             aEndingPosition);
       
   313 
       
   314     if( aFieldTypeList.FieldTypeCount() == 0 )
       
   315         {
       
   316         const TInt count = iFieldMapper.FieldCount();
       
   317         DoAppendNonEmptyTitleFieldTypesL(
       
   318                 aFieldTypeList,
       
   319                 aEndingPosition+1,
       
   320                 count-1);
       
   321         }
       
   322     }
       
   323 // -----------------------------------------------------------------------------
       
   324 // CPbk2ContactNameFormatter::MaxTitleLength
       
   325 // -----------------------------------------------------------------------------
       
   326 //
       
   327 TInt CPbk2ContactNameFormatter::MaxTitleLength
       
   328         (const MVPbkBaseContactFieldCollection& aContactFields,
       
   329         const TUint32 aFormattingFlags)
       
   330     {
       
   331     TInt result = 0;
       
   332     SetFieldMapper( aContactFields );
       
   333     
       
   334     if ( !AreTitleFieldsEmpty( ENameFirstPart, ENameSecondPart ) )
       
   335         {
       
   336         result = DoCalculateMaxTitleLength
       
   337             (aFormattingFlags, ENameFirstPart, ENameSecondPart);
       
   338         }
       
   339     else
       
   340         {
       
   341         // Use the rest of the fields in sort order to
       
   342         // build the title (ie. the company name)
       
   343         const TInt count = iFieldMapper.FieldCount();
       
   344         result = DoCalculateMaxTitleLength
       
   345             (aFormattingFlags, ENameSecondPart+1, count-1); // zero-based
       
   346         }
       
   347 
       
   348     return result;
       
   349     }
       
   350 
       
   351 // -----------------------------------------------------------------------------
       
   352 // CPbk2ContactNameFormatter::MaxTitleLengthForFind
       
   353 // -----------------------------------------------------------------------------
       
   354 //
       
   355 TInt CPbk2ContactNameFormatter::MaxTitleLengthForFind
       
   356         (const MVPbkBaseContactFieldCollection& aContactFields,
       
   357         const TUint32 aFormattingFlags)
       
   358     {
       
   359     // Simply uses MaxTitleLength as the defined functionality is the same
       
   360     return MaxTitleLength( aContactFields, aFormattingFlags );
       
   361     }
       
   362 
       
   363 // -----------------------------------------------------------------------------
       
   364 // CPbk2ContactNameFormatter::IsFindSeparatorChar
       
   365 // -----------------------------------------------------------------------------
       
   366 //
       
   367 TBool CPbk2ContactNameFormatter::IsFindSeparatorChar( TChar aCh )
       
   368     {
       
   369     return aCh.IsSpace();
       
   370     }
       
   371 
       
   372 // -----------------------------------------------------------------------------
       
   373 // CPbk2ContactNameFormatter::IsCompanyNameField
       
   374 // -----------------------------------------------------------------------------
       
   375 //
       
   376 TBool CPbk2ContactNameFormatter::IsCompanyNameField()
       
   377     {
       
   378     // This function uses field mapper of based classes and checks only
       
   379     // third field
       
   380     TBool ret = EFalse;
       
   381     const TInt count = iFieldMapper.FieldCount();
       
   382     if (count < ENameCompanyPart + 1)
       
   383         {
       
   384         return EFalse;
       
   385         }
       
   386     const MVPbkBaseContactField* field = iFieldMapper.FieldAt(ENameCompanyPart);
       
   387      
       
   388     if (field == NULL)
       
   389         {
       
   390         return EFalse;
       
   391         }
       
   392     const MVPbkFieldType* fieldType = field->BestMatchingFieldType();
       
   393     if ( fieldType && fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_COMPANYNAME )
       
   394         {
       
   395         const MVPbkContactFieldData& fieldData = field->FieldData();
       
   396         if (fieldData.DataType() == EVPbkFieldStorageTypeText)
       
   397             {
       
   398             const TDesC& fieldText =
       
   399                 MVPbkContactFieldTextData::Cast(fieldData).Text();
       
   400             if (fieldText.Size() > 0)
       
   401                 ret = ETrue;
       
   402             }
       
   403         }
       
   404     return ret;
       
   405     }
       
   406 
       
   407 // End of File