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