phonebookui/Phonebook2/remotecontactlookup/contactactionservice/src/CFscFieldFormatter.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:  Conacts field formatter.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "emailtrace.h"
       
    20 #include "CFscFieldFormatter.h"
       
    21 
       
    22 // Phonebook 2
       
    23 #include "FscPhoneNumberFormatterFactory.h"
       
    24 #include "MFscPhoneNumberFormatter.h"
       
    25 #include "FscPresentationUtils.h"
       
    26 #include <pbk2rclactionutils.rsg>
       
    27 
       
    28 // Virtual Phonebook
       
    29 #include <CVPbkContactManager.h>
       
    30 #include <MVPbkBaseContactField.h>
       
    31 #include <MVPbkContactFieldData.h>
       
    32 #include <MPbk2FieldProperty.h>
       
    33 #include <MPbk2FieldPropertyArray.h>
       
    34 #include <VPbkFieldType.hrh>
       
    35 #include <MVPbkFieldType.h>
       
    36 #include <TVPbkFieldVersitProperty.h>
       
    37 #include <MVPbkContactFieldTextData.h>
       
    38 #include <MVPbkContactFieldDateTimeData.h>
       
    39 #include <CVPbkFieldTypeSelector.h>
       
    40 #include <VPbkSyncConstants.h>
       
    41 
       
    42 // System includes
       
    43 #include <AknUtils.h>
       
    44 #include <barsread.h>
       
    45 
       
    46 /// Unnamed namespace for local definitions
       
    47 namespace
       
    48     {
       
    49 
       
    50     const TInt KBufferLength = 128;
       
    51 
       
    52 #ifdef _DEBUG
       
    53 
       
    54     enum TPanicCode
       
    55         {
       
    56         EPanicPostCond_ReallocBufferL = 1
       
    57         };
       
    58 
       
    59     void Panic(TPanicCode aReason)
       
    60         {
       
    61         _LIT(KPanicText, "CFscFieldFormatter");
       
    62         User::Panic(KPanicText, aReason);
       
    63         }
       
    64 
       
    65 #endif // _DEBUG
       
    66     } /// namespace
       
    67 
       
    68 
       
    69 // --------------------------------------------------------------------------
       
    70 // CFscFieldFormatter::CFscFieldFormatter
       
    71 // --------------------------------------------------------------------------
       
    72 //
       
    73 inline CFscFieldFormatter::CFscFieldFormatter(
       
    74         const MPbk2FieldPropertyArray& aFieldProperties,
       
    75         const MVPbkFieldTypeList& aFieldTypeList) :
       
    76     iFieldProperties(aFieldProperties), iFieldTypeList(aFieldTypeList)
       
    77     {
       
    78     FUNC_LOG;
       
    79     }
       
    80 
       
    81 // --------------------------------------------------------------------------
       
    82 // CFscFieldFormatter::~CFscFieldFormatter
       
    83 // --------------------------------------------------------------------------
       
    84 //
       
    85 CFscFieldFormatter::~CFscFieldFormatter()
       
    86     {
       
    87     FUNC_LOG;
       
    88     delete iTimeFormat;
       
    89     delete iNumberFormatter;
       
    90     delete iBuffer;
       
    91     }
       
    92 
       
    93 // --------------------------------------------------------------------------
       
    94 // CFscFieldFormatter::ConstructL
       
    95 // --------------------------------------------------------------------------
       
    96 //
       
    97 inline void CFscFieldFormatter::ConstructL()
       
    98     {
       
    99     FUNC_LOG;
       
   100     iBuffer = HBufC::NewL(KBufferLength);
       
   101     iNumberFormatter
       
   102             = FscPhoneNumberFormatterFactory::CreatePhoneNumberFormatterL(KBufferLength);
       
   103     }
       
   104 
       
   105 // --------------------------------------------------------------------------
       
   106 // CFscFieldFormatter::NewL
       
   107 // --------------------------------------------------------------------------
       
   108 //
       
   109  CFscFieldFormatter* CFscFieldFormatter::NewL
       
   110 ( const MPbk2FieldPropertyArray& aFieldProperties,
       
   111         const MVPbkFieldTypeList& aFieldTypeList )
       
   112     {
       
   113     FUNC_LOG;
       
   114     CFscFieldFormatter* self =
       
   115     new ( ELeave ) CFscFieldFormatter
       
   116     ( aFieldProperties, aFieldTypeList );
       
   117     CleanupStack::PushL( self );
       
   118     self->ConstructL();
       
   119     CleanupStack::Pop( self );
       
   120     return self;
       
   121     }
       
   122 
       
   123 // --------------------------------------------------------------------------
       
   124 // CFscFieldFormatter::FormatFieldContentL
       
   125 // --------------------------------------------------------------------------
       
   126 //
       
   127  TPtrC CFscFieldFormatter::FormatFieldContentL
       
   128 ( const MVPbkBaseContactField& aField,
       
   129         const MVPbkFieldType& aFieldType )
       
   130     {
       
   131     FUNC_LOG;
       
   132     TPtr text( iBuffer->Des() );
       
   133 
       
   134     switch ( aField.FieldData().DataType() )
       
   135         {
       
   136         case EVPbkFieldStorageTypeText:
       
   137             {
       
   138             const MVPbkContactFieldTextData* fieldData =
       
   139             &MVPbkContactFieldTextData::Cast( aField.FieldData() );
       
   140             TPtrC fieldText( fieldData->Text() );
       
   141 
       
   142             if ( MatchesFieldTypeL
       
   143                     ( aFieldType, R_FSC_PHONENUMBER_SELECTOR ) )
       
   144                 {
       
   145                 FormatPhoneNumberContentL( fieldText, text );
       
   146                 }
       
   147             else
       
   148                 {
       
   149                 // Default formatting
       
   150                 FormatTextContentL( fieldText, text, aFieldType );
       
   151                 }
       
   152             break;
       
   153             }
       
   154 
       
   155         default:
       
   156             {
       
   157             text.Zero();
       
   158             break;
       
   159             }
       
   160         }
       
   161 
       
   162     return text;
       
   163     }
       
   164 
       
   165 // --------------------------------------------------------------------------
       
   166 // CFscFieldFormatter::SetTimeFormatL
       
   167 // --------------------------------------------------------------------------
       
   168 //
       
   169  void CFscFieldFormatter::SetTimeFormatL(const TDesC& aTimeFormat)
       
   170     {
       
   171     FUNC_LOG;
       
   172     HBufC* timeFormat = aTimeFormat.AllocL();
       
   173     delete iTimeFormat;
       
   174     iTimeFormat = timeFormat;
       
   175     }
       
   176 
       
   177 // --------------------------------------------------------------------------
       
   178 // CFscFieldFormatter::ReallocBufferL
       
   179 // --------------------------------------------------------------------------
       
   180 //
       
   181 TPtr CFscFieldFormatter::ReallocBufferL(TInt aNewSize)
       
   182     {
       
   183     FUNC_LOG;
       
   184     const TInt currMaxLength = iBuffer->Des().MaxLength();
       
   185     if (aNewSize > currMaxLength)
       
   186         {
       
   187         iBuffer = iBuffer->ReAllocL(aNewSize);
       
   188         }
       
   189 
       
   190     // PostCond:
       
   191     __ASSERT_DEBUG(iBuffer->Des().MaxLength() >= aNewSize,
       
   192             Panic(EPanicPostCond_ReallocBufferL));
       
   193 
       
   194     return iBuffer->Des();
       
   195     }
       
   196 
       
   197 // --------------------------------------------------------------------------
       
   198 // CFscFieldFormatter::MatchesFieldTypeL
       
   199 // --------------------------------------------------------------------------
       
   200 //
       
   201 TBool CFscFieldFormatter::MatchesFieldTypeL(const MVPbkFieldType& aFieldType,
       
   202         TInt aResourceId) const
       
   203     {
       
   204     FUNC_LOG;
       
   205     TResourceReader reader;
       
   206     CCoeEnv::Static()->CreateResourceReaderLC(reader, aResourceId);
       
   207 
       
   208     CVPbkFieldTypeSelector* selector = CVPbkFieldTypeSelector::NewL(reader,
       
   209             iFieldTypeList);
       
   210 
       
   211     // Check if the field type is the one needed
       
   212     TBool ret = selector->IsFieldTypeIncluded(aFieldType);
       
   213     CleanupStack::PopAndDestroy(); // resource buffer
       
   214     delete selector;
       
   215     return ret;
       
   216     }
       
   217 
       
   218 // --------------------------------------------------------------------------
       
   219 // CFscFieldFormatter::FormatPhoneNumberContentL
       
   220 // --------------------------------------------------------------------------
       
   221 //
       
   222 void CFscFieldFormatter::FormatPhoneNumberContentL(TPtrC& aRawContent,
       
   223         TPtr& aFormattedContent)
       
   224     {
       
   225     FUNC_LOG;
       
   226     iNumberFormatter->SetMaxBufferLengthL(aRawContent.Length() );
       
   227 
       
   228     TPtrC
       
   229             formattedPhoneNumber(iNumberFormatter->FormatPhoneNumberForDisplay(aRawContent) );
       
   230     aFormattedContent.Set(ReallocBufferL(formattedPhoneNumber.Length() ) );
       
   231     aFormattedContent.Copy(formattedPhoneNumber);
       
   232     }
       
   233 
       
   234 // --------------------------------------------------------------------------
       
   235 // CFscFieldFormatter::FormatDtmfContentL
       
   236 // --------------------------------------------------------------------------
       
   237 //
       
   238 void CFscFieldFormatter::FormatDtmfContentL(TPtrC& aRawContent,
       
   239         TPtr& aFormattedContent)
       
   240     {
       
   241     FUNC_LOG;
       
   242     HBufC* numberBuffer = HBufC::NewLC(aRawContent.Length() );
       
   243 
       
   244     TPtr number = numberBuffer->Des();
       
   245     number.Copy(aRawContent);
       
   246     AknTextUtils::DisplayTextLanguageSpecificNumberConversion(number);
       
   247 
       
   248     aFormattedContent.Set(ReallocBufferL(number.Length() ) );
       
   249     aFormattedContent.Copy(number);
       
   250 
       
   251     CleanupStack::PopAndDestroy(numberBuffer);
       
   252     }
       
   253 
       
   254 // --------------------------------------------------------------------------
       
   255 // CFscFieldFormatter::FormatFileNameContentL
       
   256 // --------------------------------------------------------------------------
       
   257 //
       
   258 void CFscFieldFormatter::FormatFileNameContentL(TPtrC& aRawContent,
       
   259         TPtr& aFormattedContent)
       
   260     {
       
   261     FUNC_LOG;
       
   262     aFormattedContent.Set(ReallocBufferL(KMaxFileName) );
       
   263     aFormattedContent.Copy(aRawContent);
       
   264     TParsePtr fileNameParser(aFormattedContent);
       
   265     TPtrC baseName(fileNameParser.Name() );
       
   266     aFormattedContent.Copy(baseName);
       
   267     }
       
   268 
       
   269 // --------------------------------------------------------------------------
       
   270 // CFscFieldFormatter::FormatTextContentL
       
   271 // --------------------------------------------------------------------------
       
   272 //
       
   273 void CFscFieldFormatter::FormatTextContentL(TPtrC& aRawContent,
       
   274         TPtr& aFormattedContent, const MVPbkFieldType& aFieldType)
       
   275     {
       
   276     FUNC_LOG;
       
   277     aFormattedContent.Set(ReallocBufferL(aRawContent.Length() ) );
       
   278     aFormattedContent.Zero();
       
   279     FscPresentationUtils::AppendWithNewlineTranslationL(aFormattedContent,
       
   280             aRawContent);
       
   281 
       
   282     // Check is there need to display the digits in the text with
       
   283     // foreign characters. If the field is of numeric type,
       
   284     // language specific conversion is needed.
       
   285     const MPbk2FieldProperty* property =
       
   286             iFieldProperties.FindProperty(aFieldType);
       
   287 
       
   288     if (property && property->EditMode() == EPbk2FieldEditModeNumeric)
       
   289         {
       
   290         AknTextUtils::DisplayTextLanguageSpecificNumberConversion(aFormattedContent);
       
   291         }
       
   292     }
       
   293 
       
   294 // --------------------------------------------------------------------------
       
   295 // CFscFieldFormatter::FormatDateTimeContentL
       
   296 // --------------------------------------------------------------------------
       
   297 //
       
   298 void CFscFieldFormatter::FormatDateTimeContentL(
       
   299         const MVPbkContactFieldDateTimeData& aRawContent,
       
   300         TPtr& aFormattedContent)
       
   301     {
       
   302     FUNC_LOG;
       
   303     TInt error = KErrNone;
       
   304     do
       
   305         {
       
   306         TRAPD( error, aRawContent.DateTime().FormatL
       
   307                 ( aFormattedContent, *iTimeFormat ) )
       
   308         ;
       
   309         AknTextUtils::DisplayTextLanguageSpecificNumberConversion(aFormattedContent);
       
   310 
       
   311         if (error == KErrOverflow)
       
   312             {
       
   313             // Allocate bigger buffer for formatting text
       
   314             aFormattedContent.Set(ReallocBufferL( 2
       
   315                     * aFormattedContent.MaxLength() ) );
       
   316             }
       
   317         else
       
   318             {
       
   319             // Rethrow other errors
       
   320             User::LeaveIfError(error);
       
   321             }
       
   322         }
       
   323     while (error == KErrOverflow);
       
   324     }
       
   325 
       
   326 // End of File
       
   327