calendarui/regionalplugins/calenregionalutil/src/calenextrarowformatter.cpp
changeset 0 f979ecb2b13e
child 5 42814f902fe6
child 18 c198609911f9
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     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:   Calendar Lunar Plugin 
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "calendarui_debug.h"
       
    20 #include "calenextrarowformatter.h"
       
    21 #include <calenregionalutil.rsg>
       
    22 #include <AknBidiTextUtils.h>
       
    23 #include <badesca.h> 
       
    24 #include <eikenv.h>
       
    25 #include <StringLoader.h>
       
    26 
       
    27 
       
    28 // -----------------------------------------------------------------------------
       
    29 // CollapseDuplicatesL
       
    30 // -----------------------------------------------------------------------------
       
    31 //
       
    32 void CollapseDuplicatesL( TDes& aStr, TInt aPos, const TDesC& aSub )
       
    33     {
       
    34     TRACE_ENTRY_POINT;
       
    35     
       
    36     const TInt sublen = aSub.Length();
       
    37     if (aStr.Length() == 0 || sublen == 0)
       
    38         {
       
    39         return;
       
    40         }
       
    41 
       
    42     TPtrC remaining = aStr.Mid( aPos );
       
    43     TInt fstInRemaining = remaining.Find( aSub );
       
    44     
       
    45     if ( fstInRemaining >= 0 )
       
    46         {
       
    47         TInt restPos = fstInRemaining + sublen;
       
    48         TPtrC rest = remaining.Mid( restPos );
       
    49         TInt sndInRest = rest.Find( aSub );
       
    50 
       
    51         // 1) two substrings found in sequence 
       
    52         if (sndInRest == 0)
       
    53             { 
       
    54             // replace second substring with empty string
       
    55             TInt fst = aPos + fstInRemaining;
       
    56             TInt snd = aPos + restPos + sndInRest;
       
    57             aStr.Replace( snd, sublen, KNullDesC);
       
    58             // continue collapsing from first 
       
    59             CollapseDuplicatesL( aStr, fst, aSub );
       
    60             }
       
    61         // 2) substring found later in string 
       
    62         else if (sndInRest > 0)
       
    63             {         
       
    64             // continue collapsing from this second substring
       
    65             TInt snd = aPos + restPos + sndInRest;
       
    66             CollapseDuplicatesL( aStr, snd, aSub );
       
    67             }
       
    68         // 3) No second substring found -> nothing to collapse
       
    69         else             
       
    70             {
       
    71             TRACE_EXIT_POINT; 
       
    72             return;
       
    73             }
       
    74         }
       
    75     // No substring found 
       
    76     else
       
    77         {
       
    78         TRACE_EXIT_POINT;
       
    79         return;
       
    80         }
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // RemoveLeadingAndTrailingL
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 void RemoveLeadingAndTrailingL( TDes& aStr, const TDesC& aSub )
       
    88     {
       
    89     TRACE_ENTRY_POINT;
       
    90     
       
    91     // Trailing
       
    92     const TInt sublen = aSub.Length();
       
    93     if ( aStr.Right( sublen ).Find( aSub ) == 0 )
       
    94         {
       
    95         aStr.Replace( aStr.Length() - sublen, sublen, KNullDesC );
       
    96         }
       
    97 
       
    98     // Leading 
       
    99     if ( aStr.Left( sublen ).Find( aSub ) == 0 )
       
   100         {
       
   101         aStr.Replace( 0, sublen, KNullDesC );
       
   102         }
       
   103         
       
   104     TRACE_EXIT_POINT;
       
   105     }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // CCalenExtraRowFormatter::NewL
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 EXPORT_C CCalenExtraRowFormatter* CCalenExtraRowFormatter::NewL()
       
   112     {
       
   113     TRACE_ENTRY_POINT;
       
   114     
       
   115     CCalenExtraRowFormatter* self = new (ELeave) CCalenExtraRowFormatter;
       
   116     CleanupStack::PushL(self);
       
   117     self->ConstructL();
       
   118     CleanupStack::Pop(self);
       
   119     
       
   120     TRACE_EXIT_POINT;
       
   121     return self;
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CCalenExtraRowFormatter::~CCalenExtraRowFormatter
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 EXPORT_C CCalenExtraRowFormatter::~CCalenExtraRowFormatter()
       
   129     {
       
   130     TRACE_ENTRY_POINT;
       
   131     TRACE_EXIT_POINT;
       
   132     }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // CCalenExtraRowFormatter::CCalenExtraRowFormatter
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 CCalenExtraRowFormatter::CCalenExtraRowFormatter()
       
   139     {
       
   140     TRACE_ENTRY_POINT;
       
   141     TRACE_EXIT_POINT;
       
   142     }
       
   143 
       
   144 // -----------------------------------------------------------------------------
       
   145 // CCalenExtraRowFormatter::ConstructL
       
   146 // -----------------------------------------------------------------------------
       
   147 //
       
   148 void CCalenExtraRowFormatter::ConstructL()
       
   149     {
       
   150     TRACE_ENTRY_POINT;
       
   151     TRACE_EXIT_POINT;
       
   152     }
       
   153 
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // CCalenExtraRowFormatter::FormatExtraRowInformationL
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 EXPORT_C TPtrC CCalenExtraRowFormatter::FormatExtraRowInformationL( 
       
   160     CCalenLunarLocalizedInfo& aLocInfo, 
       
   161     RArray<CCalenLunarLocalizedInfo::TField>& aPrioritizedFields,
       
   162     TInt aMaxWidth,
       
   163     const CFont& aFont
       
   164     ,TBool aTwoLines
       
   165     )
       
   166     {
       
   167     TRACE_ENTRY_POINT;
       
   168     
       
   169     
       
   170     if ( aPrioritizedFields.Count() == 0)
       
   171         {
       
   172         iText = KNullDesC;
       
   173         
       
   174         TRACE_EXIT_POINT;
       
   175         return iText;
       
   176         }
       
   177 
       
   178     // Initialize substring labels
       
   179     RArray<CCalenLunarLocalizedInfo::TField> subLabels;
       
   180     CleanupClosePushL( subLabels );
       
   181     subLabels.AppendL( CCalenLunarLocalizedInfo::EAnimalYear );
       
   182     subLabels.AppendL( CCalenLunarLocalizedInfo::ELunarYear );
       
   183     subLabels.AppendL( CCalenLunarLocalizedInfo::ELunarMonthAndDay );
       
   184     subLabels.AppendL( CCalenLunarLocalizedInfo::EFestival );
       
   185     subLabels.AppendL( CCalenLunarLocalizedInfo::ESolarTerm );
       
   186     
       
   187     // ASSERT that all prioritized fields can be found from subLabels
       
   188     for ( TInt i=0; i < aPrioritizedFields.Count(); i++)
       
   189         {
       
   190         ASSERT( subLabels.Find( aPrioritizedFields[i] ) >= 0 ); 
       
   191         }
       
   192 
       
   193     TBool fits = EFalse;
       
   194 
       
   195     do 
       
   196         {
       
   197         // Initialize substring array 
       
   198         CPtrCArray* subs = new (ELeave) CPtrCArray(10);
       
   199         CleanupStack::PushL( subs );
       
   200         for ( TInt i = 0; i < subLabels.Count(); i++) 
       
   201             {
       
   202             subs->AppendL( TPtrC( KNullDesC ) );
       
   203             }
       
   204         // subs->InsertL( 0, TPtrC( KNullDesC ), 5 );
       
   205         
       
   206         // Set wanted fields to substring array
       
   207         for ( TInt i = 0; i < aPrioritizedFields.Count(); i++)
       
   208             {
       
   209             CCalenLunarLocalizedInfo::TField field = aPrioritizedFields[i];        
       
   210             TInt subIx = subLabels.Find( field );
       
   211             // Replace 
       
   212             subs->Delete(subIx);
       
   213             RDebug::Print( _L("A sub count  %d"), subs->Count() );                       
       
   214             subs->InsertL(subIx, TPtrC( aLocInfo.GetField( field ) ) );
       
   215             RDebug::Print( _L("B sub count %d"), subs->Count() );                       
       
   216             RDebug::Print( _L("B field %S"), &(subs->At(subIx)) );                       
       
   217             
       
   218             }
       
   219         
       
   220         // Format all fields to extra row     
       
   221         HBufC* extraRowFmt = StringLoader::LoadLC( R_CALE_EXTRA_ROW_LUNAR );
       
   222         
       
   223         RDebug::RawPrint( *extraRowFmt );
       
   224         
       
   225         TBuf<1000> fmt = *extraRowFmt;
       
   226         for (TInt i=0; i < subLabels.Count(); i++)
       
   227             {
       
   228             RDebug::Print( _L("Before Format") );
       
   229             RDebug::RawPrint( fmt );
       
   230             StringLoader::Format( iText, 
       
   231                                   fmt,
       
   232                                   i + 1, // %0U is a separator 
       
   233                                   subs->At( i ) );
       
   234             fmt = iText;
       
   235             RDebug::Print( _L("After Format") );
       
   236             RDebug::RawPrint( fmt );
       
   237             }
       
   238         
       
   239         // Now we have something like "Year of Dog%0U%0U6/11%0U%0U" 
       
   240         // First We need to remove multiple occurences of %0U
       
   241         _LIT(KSeparatorFmt, "%0U");
       
   242         
       
   243         CollapseDuplicatesL( iText, 0, KSeparatorFmt );
       
   244         RDebug::Print( _L("After collapse") );
       
   245         RDebug::RawPrint( iText );
       
   246 
       
   247         // Remove leading and trailing %0U
       
   248         // By now, we are sure that there is max 1 %0U in the beginning
       
   249         // and in the end of string.
       
   250         RemoveLeadingAndTrailingL( iText, KSeparatorFmt );
       
   251         RDebug::Print( _L("After leading and trailing removal") );
       
   252         RDebug::RawPrint( iText );
       
   253         
       
   254 
       
   255         // If there are now separators anymore, then do not fill them
       
   256         TBool hasSeparators = iText.Find( KSeparatorFmt ) >= 0;
       
   257         
       
   258         if ( hasSeparators ) 
       
   259             {
       
   260         
       
   261             // fill in separators
       
   262             HBufC* separator = StringLoader::LoadLC( R_CALE_LUNAR_SEPARATOR );
       
   263             fmt = iText;
       
   264             StringLoader::Format( iText, 
       
   265                                   fmt,
       
   266                                   0, // %0U is a separator 
       
   267                                   *separator );
       
   268         
       
   269             RDebug::Print( _L("After separator insert") );
       
   270             RDebug::RawPrint( iText );
       
   271             CleanupStack::PopAndDestroy( separator );
       
   272             }
       
   273 
       
   274 
       
   275         CleanupStack::PopAndDestroy( extraRowFmt );
       
   276         CleanupStack::PopAndDestroy( subs );
       
   277         
       
   278         fits = TryToFitL( iText, aMaxWidth, aFont
       
   279 
       
   280                           , aTwoLines
       
   281 
       
   282                           ); 
       
   283         if ( ! fits )
       
   284             {
       
   285             iText = KNullDesC;
       
   286             TInt last = aPrioritizedFields.Count() - 1;
       
   287             if ( last >= 0 )
       
   288                 {
       
   289                 aPrioritizedFields.Remove( last );
       
   290                 }
       
   291             }
       
   292 
       
   293         
       
   294         } while ( ! fits && aPrioritizedFields.Count() );
       
   295 
       
   296     CleanupStack::PopAndDestroy( &subLabels );
       
   297     
       
   298     
       
   299     
       
   300     TRACE_EXIT_POINT;
       
   301     return iText;
       
   302     }
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // CCalenExtraRowFormatter::TryToFitL
       
   306 // -----------------------------------------------------------------------------
       
   307 //
       
   308 TBool CCalenExtraRowFormatter::TryToFitL( const TDesC& aStr, TInt aMaxWidth, const CFont& aFont
       
   309                                            , TBool aTwoLines )
       
   310     {
       
   311     TRACE_ENTRY_POINT;
       
   312     
       
   313     TBool result(EFalse);
       
   314     if(aTwoLines)
       
   315         {
       
   316         CArrayFixFlat<TPtrC>* textLines = new(ELeave)CArrayFixFlat<TPtrC>( 3 );
       
   317         CleanupStack::PushL( textLines );
       
   318         
       
   319         CArrayFixFlat<TInt>* lineWidths = new( ELeave )CArrayFixFlat<TInt>( 1 );
       
   320         CleanupStack::PushL( lineWidths );
       
   321         
       
   322         lineWidths->AppendL( aMaxWidth );
       
   323         
       
   324         HBufC* visualText = AknBidiTextUtils::ConvertToVisualAndWrapToArrayWholeTextL(
       
   325             aStr,
       
   326             *lineWidths,
       
   327             aFont,
       
   328             *textLines);
       
   329 
       
   330         result = (textLines->Count() <= 2);
       
   331             
       
   332         CleanupStack::PopAndDestroy( lineWidths );
       
   333         CleanupStack::PopAndDestroy( textLines );
       
   334         delete visualText;
       
   335         }
       
   336     else
       
   337         {
       
   338         CFont::TMeasureTextInput::TFlags logicalOrder = static_cast<CFont::TMeasureTextInput::TFlags>(0);
       
   339         TInt textW = AknBidiTextUtils::MeasureTextBoundsWidth( aFont, aStr, logicalOrder );
       
   340         result = (textW <= aMaxWidth);
       
   341         }   
       
   342     
       
   343     TRACE_EXIT_POINT;
       
   344     return result;
       
   345     }
       
   346 
       
   347 //EOF
       
   348