phonebookui/Phonebook2/Presentation/src/CPbk2ContactNameFormatter.cpp
changeset 0 e686773b3f54
child 11 2828b4d142c0
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     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      return result+1; // for seperator ","
       
   154     }
       
   155 // -----------------------------------------------------------------------------
       
   156 // CPbk2ContactNameFormatter::GetContactTitleWithCompanyNameL
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 HBufC* CPbk2ContactNameFormatter::GetContactTitleWithCompanyNameL(
       
   160         const MVPbkBaseContactFieldCollection& aContactFields,
       
   161         TUint32 aFormattingFlags )
       
   162     {
       
   163     HBufC* result = HBufC::NewL
       
   164          (MaxTitleLengthWithCompanyName(aContactFields, aFormattingFlags));
       
   165     TPtr resultPtr = result->Des();
       
   166   
       
   167     GetContactTitleWithCompanyName(aContactFields, resultPtr, aFormattingFlags);
       
   168 
       
   169     if (resultPtr.Length() == 0)
       
   170         {
       
   171         delete result;
       
   172         result = NULL;
       
   173         }
       
   174 
       
   175     if (!result)
       
   176         {
       
   177         result = UnnamedText().AllocL();
       
   178         }
       
   179     return result;
       
   180     }
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // CPbk2ContactNameFormatter::ContactNameFormatterExtension
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 TAny* CPbk2ContactNameFormatter::ContactNameFormatterExtension( 
       
   187         TUid aExtensionUid )
       
   188     {
       
   189     if ( aExtensionUid == MPbk2ContactNameFormatterExtension2Uid )
       
   190            {
       
   191            return static_cast<MPbk2ContactNameFormatter2*>( this );
       
   192            }
       
   193     return NULL;
       
   194     }
       
   195 // -----------------------------------------------------------------------------
       
   196 // CPbk2ContactNameFormatter::GetContactTitleWithCompanyName
       
   197 // Note: If changing this function, please consider also changing
       
   198 // MaxTitleLengthForFind
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 void CPbk2ContactNameFormatter::GetContactTitleWithCompanyName(
       
   202         const MVPbkBaseContactFieldCollection& aContactFields,
       
   203         TDes& aTitle, TUint32 aFormattingFlags)
       
   204     {
       
   205     aTitle.Zero();
       
   206     SetFieldMapper( aContactFields );
       
   207 
       
   208     // Try to create title using first two fields of the sort order
       
   209     // If the field type in the sort order is not defined as a title field
       
   210     // then it's not used for title creation.
       
   211     if ( !AreTitleFieldsEmpty( ENameFirstPart, ENameSecondPart ) )
       
   212         {
       
   213         DoGetContactTitle(aTitle, aFormattingFlags,
       
   214             ENameFirstPart, ENameSecondPart);
       
   215         
       
   216         //try to add company name to the end of title
       
   217         //third field is expected to be company name
       
   218         if ( IsCompanyNameField() )
       
   219             {
       
   220             aTitle.Append(KSeparator);
       
   221             DoGetContactTitle(aTitle, EPreserveLeadingSpaces,
       
   222                     ENameCompanyPart, ENameCompanyPart);
       
   223             }
       
   224         }
       
   225     else
       
   226         { 
       
   227         // If there was no data in the first two fields then
       
   228         // use the third and the rest of the fields in the sort order
       
   229         // for title creation.
       
   230         // Find the next field in the sort order that has
       
   231         // data to build the title
       
   232         const TInt count = iFieldMapper.FieldCount();
       
   233         DoGetContactTitle(aTitle, aFormattingFlags,
       
   234             ENameSecondPart+1 , count-1); // zero-based
       
   235         }
       
   236     }
       
   237        
       
   238 // -----------------------------------------------------------------------------
       
   239 // CPbk2ContactNameFormatter::GetContactTitleForFind
       
   240 // Note: If changing this function, please consider also changing
       
   241 // MaxTitleLengthForFind
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 void CPbk2ContactNameFormatter::GetContactTitleForFind
       
   245         (const MVPbkBaseContactFieldCollection& aContactFields,
       
   246         TDes& aTitle, TUint32 aFormattingFlags)
       
   247     {
       
   248     // Simply uses GetContactTitle as the defined functionality is the same
       
   249     GetContactTitle( aContactFields, aTitle, aFormattingFlags );
       
   250     }
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // CPbk2ContactNameFormatter::ActualTitleFieldsLC
       
   254 // See function CPbk2ContactNameFormatter::GetContactTitle.
       
   255 // -----------------------------------------------------------------------------
       
   256 //
       
   257 CVPbkBaseContactFieldTypeListIterator*
       
   258 CPbk2ContactNameFormatter::ActualTitleFieldsLC(
       
   259         CVPbkFieldTypeRefsList& aFieldTypeList,
       
   260         const MVPbkBaseContactFieldCollection& aContactFields)
       
   261     {
       
   262     aFieldTypeList.Reset();
       
   263 
       
   264     SetFieldMapper( aContactFields );
       
   265     
       
   266     DoAppendNonEmptyTitleFieldTypesL(
       
   267             aFieldTypeList,
       
   268             ENameFirstPart,
       
   269             ENameSecondPart);
       
   270 
       
   271     if( aFieldTypeList.FieldTypeCount() == 0 )
       
   272         {
       
   273         const TInt count = iFieldMapper.FieldCount();
       
   274         DoAppendNonEmptyTitleFieldTypesL(
       
   275                 aFieldTypeList,
       
   276                 ENameSecondPart+1,
       
   277                 count-1);
       
   278         }
       
   279 
       
   280     return CVPbkBaseContactFieldTypeListIterator::NewLC(
       
   281             aFieldTypeList,
       
   282             aContactFields);
       
   283     }
       
   284 
       
   285 // -----------------------------------------------------------------------------
       
   286 // CPbk2ContactNameFormatter::MaxTitleLength
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 TInt CPbk2ContactNameFormatter::MaxTitleLength
       
   290         (const MVPbkBaseContactFieldCollection& aContactFields,
       
   291         const TUint32 aFormattingFlags)
       
   292     {
       
   293     TInt result = 0;
       
   294     SetFieldMapper( aContactFields );
       
   295     
       
   296     if ( !AreTitleFieldsEmpty( ENameFirstPart, ENameSecondPart ) )
       
   297         {
       
   298         result = DoCalculateMaxTitleLength
       
   299             (aFormattingFlags, ENameFirstPart, ENameSecondPart);
       
   300         }
       
   301     else
       
   302         {
       
   303         // Use the rest of the fields in sort order to
       
   304         // build the title (ie. the company name)
       
   305         const TInt count = iFieldMapper.FieldCount();
       
   306         result = DoCalculateMaxTitleLength
       
   307             (aFormattingFlags, ENameSecondPart+1, count-1); // zero-based
       
   308         }
       
   309 
       
   310     return result;
       
   311     }
       
   312 
       
   313 // -----------------------------------------------------------------------------
       
   314 // CPbk2ContactNameFormatter::MaxTitleLengthForFind
       
   315 // -----------------------------------------------------------------------------
       
   316 //
       
   317 TInt CPbk2ContactNameFormatter::MaxTitleLengthForFind
       
   318         (const MVPbkBaseContactFieldCollection& aContactFields,
       
   319         const TUint32 aFormattingFlags)
       
   320     {
       
   321     // Simply uses MaxTitleLength as the defined functionality is the same
       
   322     return MaxTitleLength( aContactFields, aFormattingFlags );
       
   323     }
       
   324 
       
   325 // -----------------------------------------------------------------------------
       
   326 // CPbk2ContactNameFormatter::IsFindSeparatorChar
       
   327 // -----------------------------------------------------------------------------
       
   328 //
       
   329 TBool CPbk2ContactNameFormatter::IsFindSeparatorChar( TChar aCh )
       
   330     {
       
   331     return aCh.IsSpace();
       
   332     }
       
   333 
       
   334 // -----------------------------------------------------------------------------
       
   335 // CPbk2ContactNameFormatter::IsCompanyNameField
       
   336 // -----------------------------------------------------------------------------
       
   337 //
       
   338 TBool CPbk2ContactNameFormatter::IsCompanyNameField()
       
   339     {
       
   340     // This function uses field mapper of based classes and checks only
       
   341     // third field
       
   342     TBool ret = EFalse;
       
   343     const TInt count = iFieldMapper.FieldCount();
       
   344     if (count < ENameCompanyPart + 1)
       
   345         {
       
   346         return EFalse;
       
   347         }
       
   348     const MVPbkBaseContactField* field = iFieldMapper.FieldAt(ENameCompanyPart);
       
   349      
       
   350     if (field == NULL)
       
   351         {
       
   352         return EFalse;
       
   353         }
       
   354     const MVPbkFieldType* fieldType = field->BestMatchingFieldType();
       
   355     if ( fieldType && fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_COMPANYNAME )
       
   356         {
       
   357         const MVPbkContactFieldData& fieldData = field->FieldData();
       
   358         if (fieldData.DataType() == EVPbkFieldStorageTypeText)
       
   359             {
       
   360             const TDesC& fieldText =
       
   361                 MVPbkContactFieldTextData::Cast(fieldData).Text();
       
   362             if (fieldText.Size() > 0)
       
   363                 ret = ETrue;
       
   364             }
       
   365         }
       
   366     return ret;
       
   367     }
       
   368 
       
   369 // End of File