phonebookui/Phonebook2/Presentation/src/CPbk2ContactNameFormatterBase.cpp
changeset 0 e686773b3f54
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:  A base class for contact name formatters
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CPbk2ContactNameFormatterBase.h"
       
    22 
       
    23 #include <f32file.h>
       
    24 #include <barsread.h>
       
    25 #include <barsc.h>
       
    26 #include <coemain.h>
       
    27 
       
    28 #include <MVPbkFieldType.h>
       
    29 #include <MVPbkBaseContactFieldCollection.h>
       
    30 #include <MVPbkBaseContactField.h>
       
    31 #include <MVPbkContactFieldData.h>
       
    32 #include <MVPbkContactFieldTextData.h>
       
    33 #include <CVPbkFieldTypeSelector.h>
       
    34 #include <CVPbkFieldTypeRefsList.h>
       
    35 
       
    36 #include "CPbk2SortOrderManager.h"
       
    37 
       
    38 #include "Pbk2PresentationUtils.h"
       
    39 #include "Pbk2DataCaging.hrh"
       
    40 
       
    41 namespace {
       
    42 
       
    43 // CONSTANTS
       
    44 _LIT(KSpace, " ");
       
    45 const TText KReplaceChar = ' ';
       
    46 
       
    47 // ============================= LOCAL FUNCTIONS ===============================
       
    48 
       
    49 void TrimNameAppend( TUint32 aFormattingFlags, const TDesC& aName, 
       
    50     TDes& aTitle )
       
    51     {
       
    52     if ( aFormattingFlags & MPbk2ContactNameFormatter::EPreserveLeadingSpaces )
       
    53         {
       
    54         // In lists, the leading spaces are not formatted,
       
    55         // only the end of the string is
       
    56         Pbk2PresentationUtils::TrimRightAppend( aName, aTitle );
       
    57         }
       
    58     else if (aFormattingFlags & MPbk2ContactNameFormatter::EPreserveAllOriginalSpaces)
       
    59         {
       
    60          //Preserve original string with all spaces
       
    61         aTitle.Append(aName);
       
    62         }
       
    63         else
       
    64         {
       
    65         // String is trimmed from beginning and end
       
    66         Pbk2PresentationUtils::TrimAllAppend( aName, aTitle );
       
    67         }
       
    68 
       
    69     if ( aFormattingFlags & 
       
    70         MPbk2ContactNameFormatter::EReplaceNonGraphicChars )
       
    71         {
       
    72         // Non-graphical characters are replaced with space
       
    73         Pbk2PresentationUtils::ReplaceNonGraphicCharacters( aTitle, 
       
    74             KReplaceChar );
       
    75         }
       
    76     }
       
    77     
       
    78 #ifdef _DEBUG
       
    79 namespace {
       
    80 
       
    81     enum TPanic
       
    82         {
       
    83         EPanic_PreCond_UnnamedText = 1
       
    84         };
       
    85         
       
    86     void Panic( TPanic aPanicCode )
       
    87         {
       
    88         _LIT( KPanicCat, "CPbk2ContactNameFormatterBase" );
       
    89         User::Panic( KPanicCat, aPanicCode );
       
    90         }
       
    91 }
       
    92 #endif // _DEBUG    
       
    93 
       
    94 } // namespace
       
    95 
       
    96 
       
    97 // ============================ MEMBER FUNCTIONS ===============================
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // CPbk2ContactNameFormatterBase::CPbk2ContactNameFormatterBase
       
   101 // C++ default constructor can NOT contain any code, that
       
   102 // might leave.
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 CPbk2ContactNameFormatterBase::CPbk2ContactNameFormatterBase(
       
   106         const MVPbkFieldTypeList& aMasterFieldTypeList,
       
   107         const CPbk2SortOrderManager& aSortOrderManager )
       
   108         :   iMasterFieldTypeList( aMasterFieldTypeList ),
       
   109             iSortOrderManager( aSortOrderManager )
       
   110     {
       
   111     iFieldMapper.SetSortOrder( iSortOrderManager.SortOrder() );
       
   112     }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CPbk2ContactNameFormatterBase:BaseConstructL
       
   116 // Symbian 2nd phase constructor can leave.
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 void CPbk2ContactNameFormatterBase::BaseConstructL( const TDesC& aUnnamedText,
       
   120         CVPbkFieldTypeSelector* aTitleFieldSelector )
       
   121     {
       
   122     iUnnamedText = aUnnamedText.AllocL();
       
   123     iTitleFieldSelector = aTitleFieldSelector;
       
   124     }
       
   125 
       
   126 // Destructor
       
   127 CPbk2ContactNameFormatterBase::~CPbk2ContactNameFormatterBase()
       
   128     {
       
   129     delete iUnnamedText;
       
   130     delete iTitleFieldSelector;
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CPbk2ContactNameFormatterBase::GetContactTitleOrNullL
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 HBufC* CPbk2ContactNameFormatterBase::GetContactTitleOrNullL(
       
   138         const MVPbkBaseContactFieldCollection& aContactFields,
       
   139         TUint32 aFormattingFlags)
       
   140     {
       
   141     HBufC* result = HBufC::NewL
       
   142         (MaxTitleLength(aContactFields, aFormattingFlags));
       
   143     TPtr resultPtr = result->Des();
       
   144     GetContactTitle(aContactFields, resultPtr, aFormattingFlags);
       
   145 
       
   146     if (resultPtr.Length() == 0)
       
   147         {
       
   148         delete result;
       
   149         result = NULL;
       
   150         }
       
   151 
       
   152     return result;
       
   153     }
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // CPbk2ContactNameFormatterBase::GetContactTitleL
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 HBufC* CPbk2ContactNameFormatterBase::GetContactTitleL(
       
   160        const MVPbkBaseContactFieldCollection& aContactFields,
       
   161        TUint32 aFormattingFlags )
       
   162     {
       
   163     HBufC* result = GetContactTitleOrNullL(aContactFields, aFormattingFlags);
       
   164     if (!result)
       
   165         {
       
   166         result = UnnamedText().AllocL();
       
   167         }
       
   168     return result;
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CPbk2ContactNameFormatterBase::UnnamedText
       
   173 // -----------------------------------------------------------------------------
       
   174 //
       
   175 const TDesC& CPbk2ContactNameFormatterBase::UnnamedText() const
       
   176     {
       
   177     __ASSERT_DEBUG( iUnnamedText, Panic( EPanic_PreCond_UnnamedText ) );
       
   178     return *iUnnamedText;
       
   179     }
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CPbk2ContactNameFormatterBase::IsTitleField
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 TBool CPbk2ContactNameFormatterBase::IsTitleField(
       
   186         const MVPbkBaseContactField& aContactField ) const
       
   187     {
       
   188     const TInt maxMatchPriority = iMasterFieldTypeList.MaxMatchPriority();
       
   189     for (TInt matchPriority = 0; matchPriority
       
   190             <= maxMatchPriority; ++matchPriority)
       
   191         {
       
   192         const MVPbkFieldType* fieldType =
       
   193             aContactField.MatchFieldType(matchPriority);
       
   194         if (fieldType)
       
   195             {
       
   196             return IsTitleFieldType(*fieldType);
       
   197             }
       
   198         }
       
   199     return EFalse;
       
   200     }
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 // CPbk2ContactNameFormatterBase::IsTitleFieldType
       
   204 // -----------------------------------------------------------------------------
       
   205 //
       
   206 TBool CPbk2ContactNameFormatterBase::IsTitleFieldType(
       
   207         const MVPbkFieldType& aFieldType) const
       
   208     {
       
   209     return iTitleFieldSelector->IsFieldTypeIncluded( aFieldType );
       
   210     }
       
   211 
       
   212 // -----------------------------------------------------------------------------
       
   213 // CPbk2ContactNameFormatterBase::SetFieldMapper
       
   214 // -----------------------------------------------------------------------------
       
   215 //
       
   216 void CPbk2ContactNameFormatterBase::SetFieldMapper(
       
   217         const MVPbkBaseContactFieldCollection& aContactFields )
       
   218     {
       
   219     iFieldMapper.SetSortOrder( iSortOrderManager.SortOrder() );
       
   220     iFieldMapper.SetContactFields( aContactFields );
       
   221     }
       
   222 
       
   223 // -----------------------------------------------------------------------------
       
   224 // CPbk2ContactNameFormatterBase::RequiresSeparatorBetweenNames
       
   225 // -----------------------------------------------------------------------------
       
   226 //
       
   227 TBool CPbk2ContactNameFormatterBase::RequiresSeparatorBetweenNames
       
   228         (const TDesC& aLhs, const TDesC& aRhs) const
       
   229     {
       
   230     return (aLhs.Length() > 0 && aRhs.Length() > 0);
       
   231     }
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 // CPbk2ContactNameFormatterBase::DoGetContactTitle
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 void CPbk2ContactNameFormatterBase::DoGetContactTitle
       
   238             (TDes& aTitle, TUint32 aFormattingFlags,
       
   239             const TInt aStartingPosition, const TInt aEndingPosition)
       
   240     {
       
   241     // Find the fields in the sort order that have
       
   242     // data to build the title
       
   243     const TInt fieldCount = iFieldMapper.FieldCount();
       
   244     for (TInt i = aStartingPosition; i <= aEndingPosition && 
       
   245             i < fieldCount; ++i)
       
   246         {
       
   247         TakePartOfName(iFieldMapper.FieldAt(i),
       
   248             aTitle, aFormattingFlags, ETrue);
       
   249         }
       
   250     }
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // CPbk2ContactNameFormatterBase::DoCalculateMaxTitleLength
       
   254 // -----------------------------------------------------------------------------
       
   255 //
       
   256 TInt CPbk2ContactNameFormatterBase::DoCalculateMaxTitleLength
       
   257         (const TUint32 aFormattingFlags, const TInt aStartingPosition,
       
   258         const TInt aEndingPosition)
       
   259     {
       
   260     TInt result = 0;
       
   261 
       
   262     // Use all the fields in sort order to
       
   263     // build the title (ie. the company name)
       
   264     const TInt fieldCount = iFieldMapper.FieldCount();
       
   265     for (TInt i = aStartingPosition; i <= aEndingPosition &&
       
   266             i < fieldCount; ++i)
       
   267         {
       
   268         CalculatePartOfName(iFieldMapper.FieldAt(i),
       
   269             result, aFormattingFlags, ETrue);
       
   270         }
       
   271 
       
   272     return result;
       
   273     }
       
   274 
       
   275 // -----------------------------------------------------------------------------
       
   276 // CPbk2ContactNameFormatterBase::DoAppendNonEmptyTitleFieldTypesL
       
   277 // -----------------------------------------------------------------------------
       
   278 //
       
   279 void CPbk2ContactNameFormatterBase::DoAppendNonEmptyTitleFieldTypesL(
       
   280         CVPbkFieldTypeRefsList& aFieldTypeList,
       
   281         const TInt aStartingPosition,
       
   282         const TInt aEndingPosition )
       
   283     {
       
   284     // Find the fields in the sort order that have
       
   285     // data to build the title
       
   286     for ( TInt i = aStartingPosition; i <= aEndingPosition; ++i )
       
   287         {
       
   288         const MVPbkBaseContactField* field = iFieldMapper.FieldAt(i);
       
   289         if ( field && IsTitleField( *field ) && !field->FieldData().IsEmpty() )
       
   290             {
       
   291             // get the field type
       
   292             const TInt maxMatchPriority = iMasterFieldTypeList.MaxMatchPriority();
       
   293             for ( TInt matchPriority = 0; matchPriority
       
   294                     <= maxMatchPriority; ++matchPriority )
       
   295                 {
       
   296                 const MVPbkFieldType* fieldType =
       
   297                     field->MatchFieldType(matchPriority);
       
   298                 // Do not add duplicates to aFieldTypeList
       
   299                 if( fieldType && 
       
   300                     !aFieldTypeList.ContainsSame( *fieldType ) )
       
   301                     {
       
   302                     aFieldTypeList.AppendL( *fieldType );
       
   303                     }
       
   304                 }
       
   305             }
       
   306         }
       
   307     }
       
   308     
       
   309 // -----------------------------------------------------------------------------
       
   310 // CPbk2ContactNameFormatterBase::AreTitleFieldsEmpty
       
   311 // -----------------------------------------------------------------------------
       
   312 //
       
   313 TBool CPbk2ContactNameFormatterBase::AreTitleFieldsEmpty(
       
   314         const TInt aStartingPosition,
       
   315         const TInt aEndingPosition ) const
       
   316     {
       
   317     TBool result = ETrue;
       
   318     
       
   319     // Find the fields in the sort order that have
       
   320     // data to build the title
       
   321     const TInt fieldCount = iFieldMapper.FieldCount();
       
   322     for (TInt i = aStartingPosition; i <= aEndingPosition && 
       
   323             i < fieldCount; ++i)
       
   324         {
       
   325         const MVPbkBaseContactField* field = iFieldMapper.FieldAt(i);
       
   326         
       
   327         if ( field && IsTitleField(*field))
       
   328             {
       
   329             const MVPbkContactFieldData& fieldData = field->FieldData();
       
   330             if (fieldData.DataType() == EVPbkFieldStorageTypeText)
       
   331                 {
       
   332                 const TDesC& fieldText =
       
   333                     MVPbkContactFieldTextData::Cast(fieldData).Text();
       
   334                 TInt length = fieldText.Length();
       
   335                 
       
   336                 if (length > 0)
       
   337                     {
       
   338                     // Define the firstNonSpaceChar to calculate the first 
       
   339                     // none space alph index in last name and first name, if the 
       
   340                     // firstNonSpaceChar is equal to the fieldText length
       
   341                     // then there are only spaces in first name and last name.
       
   342                     TInt firstNonSpaceChar = 0;
       
   343                     while( firstNonSpaceChar < length
       
   344                             && TChar(fieldText[firstNonSpaceChar]).IsSpace() )
       
   345                         {
       
   346                         ++firstNonSpaceChar;
       
   347                         }
       
   348                     if ( firstNonSpaceChar != length )
       
   349                         {
       
   350                         result = EFalse;
       
   351                         break;
       
   352                         }
       
   353                     }                
       
   354                 }
       
   355             }
       
   356         }
       
   357     
       
   358     return result;
       
   359     }    
       
   360 
       
   361 // -----------------------------------------------------------------------------
       
   362 // CPbk2ContactNameFormatterBase::TakePartOfName
       
   363 // Inlined for performance reasons
       
   364 // -----------------------------------------------------------------------------
       
   365 //
       
   366 inline void CPbk2ContactNameFormatterBase::TakePartOfName
       
   367         (const MVPbkBaseContactField* aField,
       
   368         TDes& aTitle, const TUint32 aFormattingFlags,
       
   369         const TBool aUseSeparator) const
       
   370     {
       
   371     if (aField && IsTitleField(*aField))
       
   372         {
       
   373         const MVPbkContactFieldData& fieldData = aField->FieldData();
       
   374         if (fieldData.DataType() == EVPbkFieldStorageTypeText)
       
   375             {
       
   376             const TDesC& fieldText =
       
   377                 MVPbkContactFieldTextData::Cast(fieldData).Text();
       
   378 
       
   379             if (aUseSeparator && RequiresSeparatorBetweenNames
       
   380                     (aTitle, fieldText))
       
   381                 {
       
   382                 const TDesC& separator = iSortOrderManager.CurrentSeparator();
       
   383                 if (aFormattingFlags &
       
   384                     MPbk2ContactNameFormatter::EUseSeparator)
       
   385                     {
       
   386                     aTitle.Append(separator);
       
   387                     // If the separator is other than space, append a space to it
       
   388                     if (separator.Compare(KSpace) != 0)
       
   389                         {
       
   390                         aTitle.Append(KSpace);    
       
   391                         }
       
   392                     }
       
   393                 else
       
   394                     {
       
   395                     // If separator is not used, add space then
       
   396                     aTitle.Append(KSpace);                        
       
   397                     }
       
   398                 
       
   399                 }
       
   400             TrimNameAppend(aFormattingFlags, fieldText, aTitle);
       
   401             }
       
   402         }
       
   403     }
       
   404 
       
   405 // -----------------------------------------------------------------------------
       
   406 // CPbk2ContactNameFormatterBase::CalculatePartOfName
       
   407 // Inlined for performance reasons
       
   408 // -----------------------------------------------------------------------------
       
   409 //
       
   410 inline void CPbk2ContactNameFormatterBase::CalculatePartOfName
       
   411         (const MVPbkBaseContactField* aField,
       
   412         TInt& aLength, const TUint32 aFormattingFlags,
       
   413         const TBool aUseSeparator) const
       
   414     {
       
   415     if (aField && IsTitleField(*aField))
       
   416         {
       
   417         const MVPbkContactFieldData& fieldData = aField->FieldData();
       
   418         if (fieldData.DataType() == EVPbkFieldStorageTypeText)
       
   419             {
       
   420             const TDesC& fieldText =
       
   421                 MVPbkContactFieldTextData::Cast(fieldData).Text();
       
   422 
       
   423             const TInt length = fieldText.Length();
       
   424             aLength += length;
       
   425 
       
   426             if (aUseSeparator && length > 0)
       
   427                 {
       
   428                 if (aFormattingFlags &
       
   429                     MPbk2ContactNameFormatter::EUseSeparator)
       
   430                     {
       
   431                     aLength += iSortOrderManager.CurrentSeparator().Length();
       
   432                     }
       
   433                 // There is also a space after separator
       
   434                 aLength += KSpace().Length();
       
   435                 }
       
   436             }
       
   437         }
       
   438     }
       
   439 
       
   440 //  End of File