phonebookui/Phonebook2/UIControls/src/CPbk2FieldFormatter.cpp
changeset 0 e686773b3f54
child 21 9da50d567e3c
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:  Phonebook 2 field formatter.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <CPbk2FieldFormatter.h>
       
    19 
       
    20 // Phonebook 2
       
    21 #include <Pbk2PhoneNumberFormatterFactory.h>
       
    22 #include <MPbk2PhoneNumberFormatter.h>
       
    23 #include <Pbk2PresentationUtils.h>
       
    24 #include <Pbk2UIControls.rsg>
       
    25 
       
    26 // Virtual Phonebook
       
    27 #include <CVPbkContactManager.h>
       
    28 #include <MVPbkBaseContactField.h>
       
    29 #include <MVPbkContactFieldData.h>
       
    30 #include <MPbk2FieldProperty.h>
       
    31 #include <MPbk2FieldPropertyArray.h>
       
    32 #include <VPbkFieldType.hrh>
       
    33 #include <MVPbkFieldType.h>
       
    34 #include <TVPbkFieldVersitProperty.h>
       
    35 #include <MVPbkContactFieldTextData.h>
       
    36 #include <MVPbkContactFieldDateTimeData.h>
       
    37 #include <MVPbkContactFieldUriData.h>
       
    38 #include <CVPbkFieldTypeSelector.h>
       
    39 #include <VPbkSyncConstants.h>
       
    40 
       
    41 
       
    42 // System includes
       
    43 #include <AknUtils.h>
       
    44 #include <barsread.h>
       
    45 
       
    46 
       
    47 /// Unnamed namespace for local definitions
       
    48 namespace {
       
    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, "CPbk2FieldFormatter");
       
    62     User::Panic(KPanicText, aReason);
       
    63     }
       
    64 
       
    65 #endif // _DEBUG
       
    66 
       
    67 } /// namespace
       
    68 
       
    69 
       
    70 // --------------------------------------------------------------------------
       
    71 // CPbk2FieldFormatter::CPbk2FieldFormatter
       
    72 // --------------------------------------------------------------------------
       
    73 //
       
    74 inline CPbk2FieldFormatter::CPbk2FieldFormatter
       
    75         ( const MPbk2FieldPropertyArray& aFieldProperties,
       
    76           const MVPbkFieldTypeList& aFieldTypeList ):
       
    77             iFieldProperties( aFieldProperties ),
       
    78             iFieldTypeList( aFieldTypeList )
       
    79     {
       
    80     }
       
    81 
       
    82 // --------------------------------------------------------------------------
       
    83 // CPbk2FieldFormatter::~CPbk2FieldFormatter
       
    84 // --------------------------------------------------------------------------
       
    85 //
       
    86 CPbk2FieldFormatter::~CPbk2FieldFormatter()
       
    87     {
       
    88     delete iTimeFormat;
       
    89     delete iNumberFormatter;
       
    90     delete iBuffer;
       
    91     }
       
    92 
       
    93 // --------------------------------------------------------------------------
       
    94 // CPbk2FieldFormatter::ConstructL
       
    95 // --------------------------------------------------------------------------
       
    96 //
       
    97 inline void CPbk2FieldFormatter::ConstructL()
       
    98     {
       
    99     iBuffer = HBufC::NewL( KBufferLength );
       
   100     iNumberFormatter =
       
   101         Pbk2PhoneNumberFormatterFactory::CreatePhoneNumberFormatterL
       
   102             ( KBufferLength );
       
   103     }
       
   104 
       
   105 // --------------------------------------------------------------------------
       
   106 // CPbk2FieldFormatter::NewL
       
   107 // --------------------------------------------------------------------------
       
   108 //
       
   109 EXPORT_C CPbk2FieldFormatter* CPbk2FieldFormatter::NewL
       
   110         ( const MPbk2FieldPropertyArray& aFieldProperties,
       
   111           const MVPbkFieldTypeList& aFieldTypeList )
       
   112     {
       
   113     CPbk2FieldFormatter* self =
       
   114         new ( ELeave ) CPbk2FieldFormatter
       
   115             ( aFieldProperties, aFieldTypeList );
       
   116     CleanupStack::PushL( self );
       
   117     self->ConstructL();
       
   118     CleanupStack::Pop( self );
       
   119     return self;
       
   120     }
       
   121 
       
   122 // --------------------------------------------------------------------------
       
   123 // CPbk2FieldFormatter::FormatFieldContentL
       
   124 // --------------------------------------------------------------------------
       
   125 //
       
   126 EXPORT_C TPtrC CPbk2FieldFormatter::FormatFieldContentL
       
   127         ( const MVPbkBaseContactField& aField,
       
   128           const MVPbkFieldType& aFieldType )
       
   129     {
       
   130     TPtr text( iBuffer->Des() );
       
   131 
       
   132     switch ( aField.FieldData().DataType() )
       
   133         {
       
   134         case EVPbkFieldStorageTypeText:
       
   135             {
       
   136             const MVPbkContactFieldTextData* fieldData =
       
   137                     &MVPbkContactFieldTextData::Cast( aField.FieldData() );
       
   138             TPtrC fieldText( fieldData->Text() );
       
   139 
       
   140             if( MatchesFieldTypeL
       
   141                     ( aFieldType, R_PHONEBOOK2_RINGTONE_SELECTOR ) ||
       
   142                 MatchesFieldTypeL
       
   143                     ( aFieldType, R_PHONEBOOK2_IMAGE_SELECTOR ) )
       
   144                 {
       
   145                 FormatFileNameContentL( fieldText,text );
       
   146                 }
       
   147             else if ( MatchesFieldTypeL
       
   148                     ( aFieldType, R_PHONEBOOK2_DTMF_SELECTOR ) )
       
   149                 {
       
   150                 FormatDtmfContentL( fieldText,text );
       
   151                 }
       
   152             else if ( MatchesFieldTypeL
       
   153                     ( aFieldType, R_PHONEBOOK2_PHONENUMBER_SELECTOR ) )
       
   154                 {
       
   155                 FormatPhoneNumberContentL( fieldText, text );
       
   156                 }
       
   157             else if ( MatchesFieldTypeL
       
   158                     ( aFieldType, R_PHONEBOOK2_SYNCRONIZATION_SELECTOR ) )
       
   159                 {
       
   160                 FormatSyncContentL( fieldText, text );
       
   161                 }
       
   162             else if ( MatchesFieldTypeL
       
   163                     ( aFieldType, R_PHONEBOOK2_ADDRESS_SELECTOR ) )
       
   164                 {
       
   165                 FormatAddressContentL( fieldText, text );
       
   166                 }
       
   167             else
       
   168                 {
       
   169                 // Default formatting
       
   170                 FormatTextContentL( fieldText, text, aFieldType );
       
   171                 }
       
   172             break;
       
   173             }
       
   174 
       
   175         case EVPbkFieldStorageTypeDateTime:
       
   176             {
       
   177             const MVPbkContactFieldDateTimeData* fieldData =
       
   178                     &MVPbkContactFieldDateTimeData::Cast
       
   179                         ( aField.FieldData() );
       
   180             FormatDateTimeContentL( *fieldData, text );
       
   181             break;
       
   182             }
       
   183         case EVPbkFieldStorageTypeUri:
       
   184             {
       
   185             const MVPbkContactFieldUriData* fieldData =
       
   186                     &MVPbkContactFieldUriData::Cast
       
   187                         ( aField.FieldData() );
       
   188             TPtrC fieldText( fieldData->Text() );
       
   189             FormatTextContentL( fieldText, text, aFieldType );
       
   190             break;
       
   191             }
       
   192         default:
       
   193             {
       
   194             text.Zero();
       
   195             break;
       
   196             }
       
   197         }
       
   198 
       
   199     return text;
       
   200     }
       
   201 
       
   202 // --------------------------------------------------------------------------
       
   203 // CPbk2FieldFormatter::SetTimeFormatL
       
   204 // --------------------------------------------------------------------------
       
   205 //
       
   206 EXPORT_C void CPbk2FieldFormatter::SetTimeFormatL( const TDesC& aTimeFormat )
       
   207     {
       
   208     HBufC* timeFormat = aTimeFormat.AllocL();
       
   209     delete iTimeFormat;
       
   210     iTimeFormat = timeFormat;
       
   211     }
       
   212 
       
   213 // --------------------------------------------------------------------------
       
   214 // CPbk2FieldFormatter::ReallocBufferL
       
   215 // --------------------------------------------------------------------------
       
   216 //
       
   217 TPtr CPbk2FieldFormatter::ReallocBufferL( TInt aNewSize )
       
   218     {
       
   219     const TInt currMaxLength = iBuffer->Des().MaxLength();
       
   220     if (aNewSize > currMaxLength)
       
   221         {
       
   222         iBuffer = iBuffer->ReAllocL(aNewSize);
       
   223         }
       
   224 
       
   225     // PostCond:
       
   226     __ASSERT_DEBUG(iBuffer->Des().MaxLength() >= aNewSize,
       
   227         Panic(EPanicPostCond_ReallocBufferL));
       
   228 
       
   229     return iBuffer->Des();
       
   230     }
       
   231 
       
   232 // --------------------------------------------------------------------------
       
   233 // CPbk2FieldFormatter::MatchesFieldTypeL
       
   234 // --------------------------------------------------------------------------
       
   235 //
       
   236 TBool CPbk2FieldFormatter::MatchesFieldTypeL
       
   237         ( const MVPbkFieldType& aFieldType, TInt aResourceId ) const
       
   238     {
       
   239     TResourceReader reader;
       
   240     CCoeEnv::Static()->CreateResourceReaderLC( reader, aResourceId );
       
   241 
       
   242     CVPbkFieldTypeSelector* selector = CVPbkFieldTypeSelector::NewL
       
   243         ( reader, iFieldTypeList );
       
   244 
       
   245     // Check if the field type is the one needed
       
   246     TBool ret = selector->IsFieldTypeIncluded(aFieldType);
       
   247     CleanupStack::PopAndDestroy(); // resource buffer
       
   248     delete selector;
       
   249     return ret;
       
   250     }
       
   251 
       
   252 // --------------------------------------------------------------------------
       
   253 // CPbk2FieldFormatter::FormatPhoneNumberContentL
       
   254 // --------------------------------------------------------------------------
       
   255 //
       
   256 void CPbk2FieldFormatter::FormatPhoneNumberContentL
       
   257         ( TPtrC& aRawContent, TPtr& aFormattedContent )
       
   258     {
       
   259     iNumberFormatter->SetMaxBufferLengthL( aRawContent.Length() );
       
   260 
       
   261     TPtrC formattedPhoneNumber
       
   262         ( iNumberFormatter->FormatPhoneNumberForDisplay( aRawContent ) );
       
   263     aFormattedContent.Set( ReallocBufferL( formattedPhoneNumber.Length() ) );
       
   264     aFormattedContent.Copy( formattedPhoneNumber );
       
   265     }
       
   266 
       
   267 // --------------------------------------------------------------------------
       
   268 // CPbk2FieldFormatter::FormatDtmfContentL
       
   269 // --------------------------------------------------------------------------
       
   270 //
       
   271 void CPbk2FieldFormatter::FormatDtmfContentL
       
   272         ( TPtrC& aRawContent, TPtr& aFormattedContent )
       
   273     {
       
   274     HBufC* numberBuffer = HBufC::NewLC( aRawContent.Length() );
       
   275 
       
   276     TPtr number = numberBuffer->Des();
       
   277     number.Copy( aRawContent );
       
   278     AknTextUtils::DisplayTextLanguageSpecificNumberConversion( number );
       
   279 
       
   280     aFormattedContent.Set( ReallocBufferL( number.Length() ) );
       
   281     aFormattedContent.Copy( number );
       
   282 
       
   283     CleanupStack::PopAndDestroy( numberBuffer );
       
   284     }
       
   285 
       
   286 // --------------------------------------------------------------------------
       
   287 // CPbk2FieldFormatter::FormatSyncContentL
       
   288 // --------------------------------------------------------------------------
       
   289 //
       
   290 void CPbk2FieldFormatter::FormatSyncContentL
       
   291         ( TPtrC& aRawContent, TPtr& aFormattedContent )
       
   292     {
       
   293     if ( !aRawContent.CompareF( KVPbkContactSyncPublic ) )
       
   294         {
       
   295         HBufC* textBuffer = CCoeEnv::Static()->AllocReadResourceLC
       
   296             ( R_QTN_CALE_CONFIDENT_PUBLIC );
       
   297 
       
   298         TPtr localizedSyncText = textBuffer->Des();
       
   299         aFormattedContent.Set( ReallocBufferL
       
   300             ( localizedSyncText.Length() ) );
       
   301         aFormattedContent.Copy( localizedSyncText );
       
   302 
       
   303         CleanupStack::PopAndDestroy( textBuffer );
       
   304         }
       
   305     else if ( !aRawContent.CompareF( KVPbkContactSyncNoSync ) )
       
   306         {
       
   307         HBufC* textBuffer = CCoeEnv::Static()->AllocReadResourceLC
       
   308             ( R_QTN_CALE_CONFIDENT_NONE );
       
   309 
       
   310         TPtr localizedSyncText = textBuffer->Des();
       
   311         aFormattedContent.Set( ReallocBufferL
       
   312             ( localizedSyncText.Length() ) );
       
   313         aFormattedContent.Copy( localizedSyncText );
       
   314 
       
   315         CleanupStack::PopAndDestroy( textBuffer );
       
   316         }
       
   317     else
       
   318         {
       
   319         // Otherwise sync setting is the default value - private
       
   320         HBufC* textBuffer = CCoeEnv::Static()->AllocReadResourceLC
       
   321             ( R_QTN_CALE_CONFIDENT_PRIVATE );
       
   322 
       
   323         TPtr localizedSyncText = textBuffer->Des();
       
   324         aFormattedContent.Set( ReallocBufferL
       
   325             ( localizedSyncText.Length() ) );
       
   326         aFormattedContent.Copy( localizedSyncText );
       
   327 
       
   328         CleanupStack::PopAndDestroy( textBuffer );
       
   329         }
       
   330     }
       
   331 
       
   332 // --------------------------------------------------------------------------
       
   333 // CPbk2FieldFormatter::FormatFileNameContentL
       
   334 // --------------------------------------------------------------------------
       
   335 //
       
   336 void CPbk2FieldFormatter::FormatFileNameContentL
       
   337         ( TPtrC& aRawContent, TPtr& aFormattedContent )
       
   338     {
       
   339     aFormattedContent.Set( ReallocBufferL ( KMaxFileName ) );
       
   340     aFormattedContent.Copy( aRawContent );
       
   341     TParsePtr fileNameParser( aFormattedContent );
       
   342     TPtrC baseName( fileNameParser.Name() );
       
   343     aFormattedContent.Copy( baseName );
       
   344     }
       
   345 
       
   346 // --------------------------------------------------------------------------
       
   347 // CPbk2FieldFormatter::FormatTextContentL
       
   348 // --------------------------------------------------------------------------
       
   349 //
       
   350 void CPbk2FieldFormatter::FormatTextContentL
       
   351         ( TPtrC& aRawContent, TPtr& aFormattedContent,
       
   352           const MVPbkFieldType& aFieldType )
       
   353     {
       
   354     aFormattedContent.Set( ReallocBufferL( aRawContent.Length() ) );
       
   355     aFormattedContent.Zero();
       
   356     Pbk2PresentationUtils::AppendWithNewlineTranslationL
       
   357         ( aFormattedContent, aRawContent );
       
   358 
       
   359     // Check is there need to display the digits in the text with
       
   360     // foreign characters. If the field is of numeric type,
       
   361     // language specific conversion is needed.
       
   362     const MPbk2FieldProperty* property =
       
   363         iFieldProperties.FindProperty( aFieldType );
       
   364 
       
   365     if ( property &&
       
   366          property->EditMode() == EPbk2FieldEditModeNumeric )
       
   367         {
       
   368         AknTextUtils::DisplayTextLanguageSpecificNumberConversion
       
   369             ( aFormattedContent );
       
   370         }
       
   371     }
       
   372 
       
   373 // --------------------------------------------------------------------------
       
   374 // CPbk2FieldFormatter::FormatAddressContentL
       
   375 // --------------------------------------------------------------------------
       
   376 //
       
   377 void CPbk2FieldFormatter::FormatAddressContentL
       
   378         ( TPtrC& aRawContent, TPtr& aFormattedContent )
       
   379     {
       
   380     aFormattedContent.Set( ReallocBufferL( aRawContent.Length() ) );
       
   381     aFormattedContent.Zero();
       
   382     Pbk2PresentationUtils::AppendWithNewlineTranslationL
       
   383         ( aFormattedContent, aRawContent );
       
   384     }
       
   385 
       
   386 // --------------------------------------------------------------------------
       
   387 // CPbk2FieldFormatter::FormatDateTimeContentL
       
   388 // --------------------------------------------------------------------------
       
   389 //
       
   390 void CPbk2FieldFormatter::FormatDateTimeContentL
       
   391         ( const MVPbkContactFieldDateTimeData& aRawContent,
       
   392           TPtr& aFormattedContent )
       
   393     {
       
   394     TInt error = KErrNone;
       
   395     do
       
   396         {
       
   397         TRAPD( error, aRawContent.DateTime().FormatL
       
   398             ( aFormattedContent, *iTimeFormat ) );
       
   399         AknTextUtils::DisplayTextLanguageSpecificNumberConversion
       
   400             ( aFormattedContent );
       
   401 
       
   402         if ( error == KErrOverflow )
       
   403             {
       
   404             // Allocate bigger buffer for formatting text
       
   405             aFormattedContent.Set( ReallocBufferL
       
   406                 ( 2 * aFormattedContent.MaxLength() ) );
       
   407             }
       
   408         else
       
   409             {
       
   410             // Rethrow other errors
       
   411             User::LeaveIfError( error );
       
   412             }
       
   413         } while ( error == KErrOverflow );
       
   414     }
       
   415 
       
   416 // End of File