emailcontacts/contactactionservice/src/CFscFieldListBoxModel.cpp
changeset 0 8466d47a6819
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     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:  Implementation of field list box model.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "emailtrace.h"
       
    21 #include "CFscFieldListBoxModel.h"
       
    22 
       
    23 // Phonebook 2
       
    24 #include "MFscClipListBoxText.h"
       
    25 #include "MFscFieldAnalyzer.h"
       
    26 #include "CFscFieldFormatter.h"
       
    27 #include "MFscContactFieldDynamicProperties.h"
       
    28 #include "CFscContactFieldDynamicProperties.h"
       
    29 #include <CPbk2IconArray.h>
       
    30 #include "FscPresentationUtils.h"
       
    31 #include "CFscPresentationContactField.h"
       
    32 #include "CFscPresentationContactFieldCollection.h"
       
    33 #include <MPbk2FieldPropertyArray.h>
       
    34 #include <MPbk2FieldProperty.h>
       
    35 #include "TFscStoreContactAnalyzer.h"
       
    36 #include <FscActionUtils.rsg>
       
    37 
       
    38 // Virtual Phonebook
       
    39 #include <MVPbkFieldType.h>
       
    40 #include <TVPbkFieldVersitProperty.h>
       
    41 #include <MVPbkContactFieldData.h>
       
    42 #include <MVPbkContactStore.h>
       
    43 #include <CVPbkContactManager.h>
       
    44 #include <MVPbkContactFieldData.h>
       
    45 #include <VPbkUtils.h>
       
    46 
       
    47 /// Unnamed namespace for local definitions
       
    48 namespace
       
    49     {
       
    50 
       
    51     _LIT( KCharsToReplace, "\t" );
       
    52     _LIT( KReplacementChars, " " );
       
    53     const TInt KMaxFormattedTIntLength( 15);
       
    54     const TInt KInitialBufferLength( 128);
       
    55     const TInt KGranularity( 8);
       
    56 
       
    57     } /// namespace
       
    58 
       
    59 
       
    60 // --------------------------------------------------------------------------
       
    61 // CFscFieldListBoxRow::CFscFieldListBoxRow
       
    62 // --------------------------------------------------------------------------
       
    63 //
       
    64 inline CFscFieldListBoxRow::CFscFieldListBoxRow()
       
    65     {
       
    66     FUNC_LOG;
       
    67     }
       
    68 
       
    69 // --------------------------------------------------------------------------
       
    70 // CFscFieldListBoxRow::~CFscFieldListBoxRow
       
    71 // --------------------------------------------------------------------------
       
    72 //
       
    73 CFscFieldListBoxRow::~CFscFieldListBoxRow()
       
    74     {
       
    75     FUNC_LOG;
       
    76     iColumns.ResetAndDestroy();
       
    77     }
       
    78 
       
    79 // --------------------------------------------------------------------------
       
    80 // CFscFieldListBoxRow::NewL
       
    81 // --------------------------------------------------------------------------
       
    82 //
       
    83 CFscFieldListBoxRow* CFscFieldListBoxRow::NewL()
       
    84     {
       
    85     FUNC_LOG;
       
    86     return new( ELeave ) CFscFieldListBoxRow;
       
    87     }
       
    88 
       
    89 // --------------------------------------------------------------------------
       
    90 // CFscFieldListBoxRow::ColumnCount
       
    91 // --------------------------------------------------------------------------
       
    92 //
       
    93 TInt CFscFieldListBoxRow::ColumnCount() const
       
    94     {
       
    95     FUNC_LOG;
       
    96     return iColumns.Count();
       
    97     }
       
    98 
       
    99 // --------------------------------------------------------------------------
       
   100 // CFscFieldListBoxRow::At
       
   101 // --------------------------------------------------------------------------
       
   102 //
       
   103 TPtrC CFscFieldListBoxRow::At(TInt aColumnIndex) const
       
   104     {
       
   105     FUNC_LOG;
       
   106     return *iColumns[aColumnIndex];
       
   107     }
       
   108 
       
   109 // --------------------------------------------------------------------------
       
   110 // CFscFieldListBoxRow::AppendColumnL
       
   111 // --------------------------------------------------------------------------
       
   112 //
       
   113 void CFscFieldListBoxRow::AppendColumnL(const TDesC& aColumnText)
       
   114     {
       
   115     FUNC_LOG;
       
   116     HBufC* buf = aColumnText.AllocLC();
       
   117     User::LeaveIfError(iColumns.Append(buf) );
       
   118     CleanupStack::Pop(buf);
       
   119     }
       
   120 
       
   121 // --------------------------------------------------------------------------
       
   122 // CFscFieldListBoxRow::TotalLength
       
   123 // --------------------------------------------------------------------------
       
   124 //
       
   125 TInt CFscFieldListBoxRow::TotalLength() const
       
   126     {
       
   127     FUNC_LOG;
       
   128     TInt result = 0;
       
   129     const TInt count = iColumns.Count();
       
   130     for (TInt i = 0; i < count; ++i)
       
   131         {
       
   132         result += At(i).Length();
       
   133         }
       
   134     return result;
       
   135     }
       
   136 
       
   137 // --------------------------------------------------------------------------
       
   138 // CFscFieldListBoxRow::MaxColumnLength
       
   139 // --------------------------------------------------------------------------
       
   140 //
       
   141 TInt CFscFieldListBoxRow::MaxColumnLength() const
       
   142     {
       
   143     FUNC_LOG;
       
   144     TInt result = 0;
       
   145     const TInt count = iColumns.Count();
       
   146     for (TInt i = 0; i < count; ++i)
       
   147         {
       
   148         result = Max(result, At(i).Length());
       
   149         }
       
   150     return result;
       
   151     }
       
   152 
       
   153 // --------------------------------------------------------------------------
       
   154 // CFscFieldListBoxModel::CFscFieldListBoxModel
       
   155 // --------------------------------------------------------------------------
       
   156 //
       
   157 inline CFscFieldListBoxModel::CFscFieldListBoxModel(TParams& aParams) :
       
   158     iRows(KGranularity), iParams(aParams)
       
   159     {
       
   160     FUNC_LOG;
       
   161     }
       
   162 
       
   163 // --------------------------------------------------------------------------
       
   164 // CFscFieldListBoxModel::~CFscFieldListBoxModel
       
   165 // --------------------------------------------------------------------------
       
   166 //
       
   167 CFscFieldListBoxModel::~CFscFieldListBoxModel()
       
   168     {
       
   169     FUNC_LOG;
       
   170     iRows.ResetAndDestroy();
       
   171     delete iColumnBuf;
       
   172     delete iLineBuf;
       
   173     delete iFieldFormatter;
       
   174     delete iDynamicProperties;
       
   175     }
       
   176 
       
   177 // --------------------------------------------------------------------------
       
   178 // CFscFieldListBoxModel::NewL
       
   179 // --------------------------------------------------------------------------
       
   180 //
       
   181 CFscFieldListBoxModel* CFscFieldListBoxModel::NewL(TParams& aParams)
       
   182     {
       
   183     FUNC_LOG;
       
   184     CFscFieldListBoxModel* self = new ( ELeave ) CFscFieldListBoxModel( aParams );
       
   185     CleanupStack::PushL(self);
       
   186     self->ConstructL(aParams);
       
   187     CleanupStack::Pop(self);
       
   188     return self;
       
   189     }
       
   190 
       
   191 // --------------------------------------------------------------------------
       
   192 // CFscFieldListBoxModel::ConstructL
       
   193 // --------------------------------------------------------------------------
       
   194 //
       
   195 void CFscFieldListBoxModel::ConstructL(TParams& aParams)
       
   196     {
       
   197     FUNC_LOG;
       
   198     iFieldFormatter = CFscFieldFormatter::NewL(aParams.iFieldProperties,
       
   199             aParams.iContactManager.FieldTypes() );
       
   200     iFieldFormatter->SetTimeFormatL(aParams.iTimeFormat);
       
   201     iLineBuf = HBufC::NewL(KInitialBufferLength);
       
   202     iColumnBuf = HBufC::NewL(KInitialBufferLength);
       
   203     iDynamicProperties = aParams.iDynamicProperties;
       
   204     }
       
   205 
       
   206 // --------------------------------------------------------------------------
       
   207 // CFscFieldListBoxModel::AppendRowL
       
   208 // --------------------------------------------------------------------------
       
   209 //
       
   210 void CFscFieldListBoxModel::AppendRowL(CFscFieldListBoxRow* aLine)
       
   211     {
       
   212     FUNC_LOG;
       
   213     ExpandBuffersL( *aLine);
       
   214     iRows.AppendL(aLine);
       
   215     }
       
   216 
       
   217 // --------------------------------------------------------------------------
       
   218 // CFscFieldListBoxModel::SetClipper
       
   219 // --------------------------------------------------------------------------
       
   220 //
       
   221 void CFscFieldListBoxModel::SetClipper(MFscClipListBoxText& aTextClipper)
       
   222     {
       
   223     FUNC_LOG;
       
   224     this->iTextClipper = &aTextClipper;
       
   225     }
       
   226 
       
   227 // --------------------------------------------------------------------------
       
   228 // CFscFieldListBoxModel::FormatFieldsL
       
   229 // --------------------------------------------------------------------------
       
   230 //
       
   231 void CFscFieldListBoxModel::FormatFieldsL()
       
   232     {
       
   233     FUNC_LOG;
       
   234     // Format all the fields and add lines to iTextArray
       
   235     const TInt fieldCount = iParams.iFields.FieldCount();
       
   236     for (TInt i=0; i < fieldCount; ++i)
       
   237         {
       
   238         FormatFieldL(iParams.iFields.At(i), iParams);
       
   239         }
       
   240     }
       
   241 
       
   242 // --------------------------------------------------------------------------
       
   243 // CFscFieldListBoxModel::MdcaCount
       
   244 // --------------------------------------------------------------------------
       
   245 //
       
   246 TInt CFscFieldListBoxModel::MdcaCount() const
       
   247     {
       
   248     FUNC_LOG;
       
   249     return iRows.Count();
       
   250     }
       
   251 
       
   252 // --------------------------------------------------------------------------
       
   253 // CFscFieldListBoxModel::MdcaPoint
       
   254 // --------------------------------------------------------------------------
       
   255 //
       
   256 TPtrC16 CFscFieldListBoxModel::MdcaPoint(TInt aIndex) const
       
   257     {
       
   258     FUNC_LOG;
       
   259     const TText KSeparator = '\t';
       
   260     TPtr rowText(iLineBuf->Des() );
       
   261     rowText.Zero();
       
   262 
       
   263     const CFscFieldListBoxRow& row = *iRows[aIndex];
       
   264     const TInt columnCount = row.ColumnCount();
       
   265     for (TInt columnIndex = 0; columnIndex < columnCount; ++columnIndex)
       
   266         {
       
   267         TPtr columnText(iColumnBuf->Des() );
       
   268         columnText.Copy(row.At(columnIndex) );
       
   269 
       
   270         // Clip the column if required
       
   271         if (iTextClipper && columnIndex == EContentColumn && aIndex
       
   272                 < iParams.iFields.FieldCount() )
       
   273             {
       
   274             TRAP_IGNORE( ClipTextL(iParams.iFields.At(aIndex), aIndex, columnText,
       
   275                     columnIndex) );
       
   276             }
       
   277 
       
   278         // Append the column and separator to the formatted row
       
   279         rowText.Append(columnText);
       
   280         rowText.Append(KSeparator);
       
   281         }
       
   282 
       
   283     return rowText;
       
   284     }
       
   285 
       
   286 // --------------------------------------------------------------------------
       
   287 // CFscFieldListBoxModel::FormatFieldL
       
   288 // --------------------------------------------------------------------------
       
   289 //
       
   290 void CFscFieldListBoxModel::FormatFieldL(
       
   291         const CFscPresentationContactField& aField, TParams& aParams)
       
   292     {
       
   293     FUNC_LOG;
       
   294     // Figure out whether the field should be shown
       
   295     const MFscFieldAnalyzer* fieldAnalyzer = aParams.iFieldAnalyzer;
       
   296     const MFscContactFieldDynamicProperties* dynamicProps =
       
   297             iDynamicProperties;
       
   298 
       
   299     TBool showField = ETrue;
       
   300     if (fieldAnalyzer && fieldAnalyzer->IsHiddenField(aField) || dynamicProps
       
   301             && dynamicProps->IsHiddenField(aField) )
       
   302         {
       
   303         showField = EFalse;
       
   304         }
       
   305 
       
   306     if (showField)
       
   307         {
       
   308         CFscFieldListBoxRow* row = CFscFieldListBoxRow::NewL();
       
   309         CleanupStack::PushL(row);
       
   310 
       
   311         // Get master field type list and match field's type against it
       
   312         const MVPbkFieldTypeList* masterFieldTypeList =
       
   313                 &iParams.iContactManager.FieldTypes();
       
   314 
       
   315         const MVPbkFieldType* fieldType = VPbkUtils::MatchFieldType(
       
   316                 *masterFieldTypeList, aField);
       
   317 
       
   318         TPtr columnBuf(iColumnBuf->Des());
       
   319         AppendIconsToBeginningL(aField, *fieldType, *row, aParams);
       
   320         AppendFieldLabelL(aField, *row, columnBuf);
       
   321         AppendFieldContentL(aField, *fieldType, *row, columnBuf);
       
   322 
       
   323         // Expand row formatting buffer if required
       
   324         ExpandBuffersL(*row);
       
   325 
       
   326         // Add the row
       
   327         iRows.AppendL(row);
       
   328         CleanupStack::Pop(row);
       
   329         }
       
   330     }
       
   331 
       
   332 // --------------------------------------------------------------------------
       
   333 // CFscFieldListBoxModel::ExpandBuffersL
       
   334 // --------------------------------------------------------------------------
       
   335 //
       
   336 void CFscFieldListBoxModel::ExpandBuffersL(const CFscFieldListBoxRow& aRow)
       
   337     {
       
   338     FUNC_LOG;
       
   339     // Row formatting buffer
       
   340     const TInt rowLength = aRow.TotalLength() + aRow.ColumnCount(); // for separator characters
       
   341 
       
   342     if (rowLength > iLineBuf->Des().MaxLength())
       
   343         {
       
   344         iLineBuf = iLineBuf->ReAllocL(rowLength);
       
   345         }
       
   346 
       
   347     ExpandColumnBufferL(aRow.MaxColumnLength() );
       
   348     }
       
   349 
       
   350 // --------------------------------------------------------------------------
       
   351 // CFscFieldListBoxModel::ExpandColumnBufferL
       
   352 // --------------------------------------------------------------------------
       
   353 //
       
   354 TPtr CFscFieldListBoxModel::ExpandColumnBufferL(TInt aRequiredLength)
       
   355     {
       
   356     FUNC_LOG;
       
   357     if (aRequiredLength > iColumnBuf->Des().MaxLength() )
       
   358         {
       
   359         iColumnBuf = iColumnBuf->ReAllocL(aRequiredLength);
       
   360         }
       
   361     return (iColumnBuf->Des() );
       
   362     }
       
   363 
       
   364 // --------------------------------------------------------------------------
       
   365 // CFscFieldListBoxModel::ClipText
       
   366 // Clip text from beginning if field is numeric field or e-mail field.
       
   367 // --------------------------------------------------------------------------
       
   368 //
       
   369 inline void CFscFieldListBoxModel::ClipTextL(
       
   370         const CFscPresentationContactField& aField, TInt aIndex,
       
   371         TPtr& aColumnText, TInt aColumnIndex) const
       
   372     {
       
   373     FUNC_LOG;
       
   374     TFscStoreContactAnalyzer analyzer(iParams.iContactManager, NULL);
       
   375     TBool clip = analyzer.IsFieldTypeIncludedL(aField,
       
   376             R_FSC_PHONENUMBER_SELECTOR);
       
   377 
       
   378     // Clip if required
       
   379     if (clip)
       
   380         {
       
   381         iTextClipper->ClipFromBeginning(aColumnText, aIndex, aColumnIndex);
       
   382         }
       
   383     }
       
   384 
       
   385 // --------------------------------------------------------------------------
       
   386 // CFscFieldListBoxModel::AppendFieldLabelL
       
   387 // Appends field label. Removes any listbox separator characters.
       
   388 // --------------------------------------------------------------------------
       
   389 //
       
   390 inline void CFscFieldListBoxModel::AppendFieldLabelL(
       
   391         const CFscPresentationContactField& aField,
       
   392         CFscFieldListBoxRow& aRow, TPtr& aColumnBuf)
       
   393     {
       
   394     FUNC_LOG;
       
   395     aColumnBuf.Set(ExpandColumnBufferL(aField.FieldLabel().Length() ) );
       
   396     aColumnBuf.Zero();
       
   397     FscPresentationUtils::AppendAndReplaceChars(aColumnBuf,
       
   398             aField.FieldLabel(), KCharsToReplace, KReplacementChars);
       
   399     // Replace characters that can not be displayed correctly
       
   400     FscPresentationUtils::ReplaceNonGraphicCharacters(aColumnBuf, ' ');
       
   401     aRow.AppendColumnL(aColumnBuf);
       
   402     }
       
   403 
       
   404 // --------------------------------------------------------------------------
       
   405 // CFscFieldListBoxModel::AppendFieldContentL
       
   406 // Appends field content.
       
   407 // --------------------------------------------------------------------------
       
   408 //
       
   409 inline void CFscFieldListBoxModel::AppendFieldContentL(
       
   410         const CFscPresentationContactField& aField,
       
   411         const MVPbkFieldType& aFieldType, CFscFieldListBoxRow& aRow,
       
   412         TPtr& aColumnBuf)
       
   413     {
       
   414     FUNC_LOG;
       
   415     TPtrC fieldText(iFieldFormatter->FormatFieldContentL(aField, aFieldType) );
       
   416     aColumnBuf.Set(ExpandColumnBufferL(fieldText.Length() ) );
       
   417     aColumnBuf.Zero();
       
   418     FscPresentationUtils::AppendAndReplaceChars(aColumnBuf, fieldText,
       
   419             KCharsToReplace, KReplacementChars);
       
   420     // Replace characters that can not be displayed correctly
       
   421     FscPresentationUtils::ReplaceNonGraphicCharacters(aColumnBuf, ' ');
       
   422     aRow.AppendColumnL(aColumnBuf);
       
   423     }
       
   424 
       
   425 // --------------------------------------------------------------------------
       
   426 // CFscFieldListBoxModel::AppendIconsToBeginningL
       
   427 // Adds icons to the beginning.
       
   428 // --------------------------------------------------------------------------
       
   429 //
       
   430 inline void CFscFieldListBoxModel::AppendIconsToBeginningL(
       
   431         const CFscPresentationContactField& /*aField*/,
       
   432         const MVPbkFieldType& aFieldType, CFscFieldListBoxRow& aRow,
       
   433         TParams& aParams)
       
   434     {
       
   435     FUNC_LOG;
       
   436     TBuf<KMaxFormattedTIntLength> iconText;
       
   437 
       
   438     // Format icon index
       
   439     TInt iconIndex = aParams.iIconArray.FindIcon(iParams.iDefaultIconId);
       
   440     const MPbk2FieldProperty* property =
       
   441             aParams.iFieldProperties.FindProperty(aFieldType);
       
   442     if (property)
       
   443         {
       
   444         iconIndex = aParams.iIconArray.FindIcon(property->IconId() );
       
   445         }
       
   446 
       
   447     iconText.Num(iconIndex);
       
   448     aRow.AppendColumnL(iconText);
       
   449     }
       
   450 
       
   451 // --------------------------------------------------------------------------
       
   452 // CFscFieldListBoxModel::AppendIconsToEndL
       
   453 // Adds additional icons to the end.
       
   454 // --------------------------------------------------------------------------
       
   455 //
       
   456 inline void CFscFieldListBoxModel::AppendIconsToEndL(
       
   457         const CFscPresentationContactField& aField,
       
   458         CFscFieldListBoxRow& aRow, TParams& aParams)
       
   459     {
       
   460     FUNC_LOG;
       
   461     if (aParams.iFieldAnalyzer)
       
   462         {
       
   463         // Check and add speed dial icon
       
   464         if (aParams.iFieldAnalyzer->HasSpeedDialL(aField.StoreField() ) )
       
   465             {
       
   466             TBuf<KMaxFormattedTIntLength> iconText;
       
   467             iconText.Num(aParams.iIconArray.FindIcon(TFscAppIconId(EPbk2qgn_indi_qdial_add) ) );
       
   468             aRow.AppendColumnL(iconText);
       
   469             }
       
   470         }
       
   471     }
       
   472 
       
   473 //  End of File
       
   474