calendarui/regionalplugins/calenregionalutil/src/calenextrarowformatter.cpp
changeset 45 b6db4fd4947b
parent 23 fd30d51f876b
child 55 2c54b51f39c4
equal deleted inserted replaced
23:fd30d51f876b 45:b6db4fd4947b
    13 *
    13 *
    14 * Description:   Calendar Lunar Plugin 
    14 * Description:   Calendar Lunar Plugin 
    15  *
    15  *
    16 */
    16 */
    17 
    17 
    18 
    18 #include <qstringlist.h>
    19 
    19 #include <hbglobal.h>
    20 #include <calenregionalutil.rsg>
    20 
    21 #include <badesca.h> 
    21 #include <badesca.h> 
    22 #include <eikenv.h>
    22 #include <eikenv.h>
    23 #include <StringLoader.h>
       
    24 
    23 
    25 #include "calendarui_debug.h"
    24 #include "calendarui_debug.h"
    26 #include "CalenExtraRowFormatter.h"
    25 #include "CalenExtraRowFormatter.h"
    27 
    26 
       
    27 // Constants
       
    28 const TInt KZero( 0 );
       
    29 
    28 // -----------------------------------------------------------------------------
    30 // -----------------------------------------------------------------------------
    29 // CollapseDuplicatesL
    31 // CollapseDuplicatesL
    30 // -----------------------------------------------------------------------------
    32 // -----------------------------------------------------------------------------
    31 //
    33 //
    32 void CollapseDuplicatesL( TDes& aStr, TInt aPos, const TDesC& aSub )
    34 void CollapseDuplicatesL( TDes& aStr, TInt aPos, const TDesC& aSub )
    33     {
    35     {
    34     TRACE_ENTRY_POINT;
    36     TRACE_ENTRY_POINT;
    35     
    37     
    36     const TInt sublen = aSub.Length();
    38     const TInt sublen = aSub.Length();
    37     if (aStr.Length() == 0 || sublen == 0)
    39     if (aStr.Length() == KZero || sublen == KZero || aPos < KZero || (aPos >aStr.Length()))
    38         {
    40         {
    39         return;
    41         return;
    40         }
    42         }
    41 
    43 
    42     TPtrC remaining = aStr.Mid( aPos );
    44     TPtrC remaining = aStr.Mid( aPos );
    43     TInt fstInRemaining = remaining.Find( aSub );
    45     TInt fstInRemaining = remaining.Find( aSub );
    44     
    46     
    45     if ( fstInRemaining >= 0 )
    47     if ( fstInRemaining >= KZero )
    46         {
    48         {
    47         TInt restPos = fstInRemaining + sublen;
    49         TInt restPos = fstInRemaining + sublen;
    48         TPtrC rest = remaining.Mid( restPos );
    50         TPtrC rest = remaining.Mid( restPos );
    49         TInt sndInRest = rest.Find( aSub );
    51         TInt sndInRest = rest.Find( aSub );
    50 
    52 
    51         // 1) two substrings found in sequence 
    53         // 1) two substrings found in sequence 
    52         if (sndInRest == 0)
    54         if (sndInRest == KZero)
    53             { 
    55             { 
    54             // replace second substring with empty string
    56             // replace second substring with empty string
    55             TInt fst = aPos + fstInRemaining;
    57             TInt fst = aPos + fstInRemaining;
    56             TInt snd = aPos + restPos + sndInRest;
    58             TInt snd = aPos + restPos + sndInRest;
    57             aStr.Replace( snd, sublen, KNullDesC);
    59             aStr.Replace( snd, sublen, KNullDesC);
    58             // continue collapsing from first 
    60             // continue collapsing from first 
    59             CollapseDuplicatesL( aStr, fst, aSub );
    61             CollapseDuplicatesL( aStr, fst, aSub );
    60             }
    62             }
    61         // 2) substring found later in string 
    63         // 2) substring found later in string 
    62         else if (sndInRest > 0)
    64         else if (sndInRest > KZero)
    63             {         
    65             {         
    64             // continue collapsing from this second substring
    66             // continue collapsing from this second substring
    65             TInt snd = aPos + restPos + sndInRest;
    67             TInt snd = aPos + restPos + sndInRest;
    66             CollapseDuplicatesL( aStr, snd, aSub );
    68             CollapseDuplicatesL( aStr, snd, aSub );
    67             }
    69             }
    88     {
    90     {
    89     TRACE_ENTRY_POINT;
    91     TRACE_ENTRY_POINT;
    90     
    92     
    91     // Trailing
    93     // Trailing
    92     const TInt sublen = aSub.Length();
    94     const TInt sublen = aSub.Length();
    93     if ( aStr.Right( sublen ).Find( aSub ) == 0 )
    95     if ( aStr.Right( sublen ).Find( aSub ) == KZero )
    94         {
    96         {
    95         aStr.Replace( aStr.Length() - sublen, sublen, KNullDesC );
    97         aStr.Replace( aStr.Length() - sublen, sublen, KNullDesC );
    96         }
    98         }
    97 
    99 
    98     // Leading 
   100     // Leading 
    99     if ( aStr.Left( sublen ).Find( aSub ) == 0 )
   101     if ( aStr.Left( sublen ).Find( aSub ) == KZero )
   100         {
   102         {
   101         aStr.Replace( 0, sublen, KNullDesC );
   103         aStr.Replace( KZero, sublen, KNullDesC );
   102         }
   104         }
   103         
   105         
   104     TRACE_EXIT_POINT;
   106     TRACE_EXIT_POINT;
   105     }
   107     }
   106 
   108 
   157 // -----------------------------------------------------------------------------
   159 // -----------------------------------------------------------------------------
   158 //
   160 //
   159 EXPORT_C TPtrC CCalenExtraRowFormatter::FormatExtraRowInformationL( 
   161 EXPORT_C TPtrC CCalenExtraRowFormatter::FormatExtraRowInformationL( 
   160     CCalenLunarLocalizedInfo& aLocInfo, 
   162     CCalenLunarLocalizedInfo& aLocInfo, 
   161     RArray<CCalenLunarLocalizedInfo::TField>& aPrioritizedFields )
   163     RArray<CCalenLunarLocalizedInfo::TField>& aPrioritizedFields )
   162     {
   164 {
   163     TRACE_ENTRY_POINT;
   165 	TRACE_ENTRY_POINT;
   164     
   166 
   165     
   167 
   166     if ( aPrioritizedFields.Count() == 0)
   168 	if ( aPrioritizedFields.Count() == KZero)
   167         {
   169 	{
   168         iText = KNullDesC;
   170 		iText = KNullDesC;
   169         
   171 
   170         TRACE_EXIT_POINT;
   172 		TRACE_EXIT_POINT;
   171         return iText;
   173 		return iText;
   172         }
   174 	}
   173 
   175 
   174     // Initialize substring labels
   176 	// Initialize substring labels
   175     RArray<CCalenLunarLocalizedInfo::TField> subLabels;
   177 	RArray<CCalenLunarLocalizedInfo::TField> subLabels;
   176     CleanupClosePushL( subLabels );
   178 	CleanupClosePushL( subLabels );
   177     subLabels.AppendL( CCalenLunarLocalizedInfo::EAnimalYear );
   179 	subLabels.AppendL( CCalenLunarLocalizedInfo::EAnimalYear );
   178     subLabels.AppendL( CCalenLunarLocalizedInfo::ELunarYear );
   180 	subLabels.AppendL( CCalenLunarLocalizedInfo::ELunarYear );
   179     subLabels.AppendL( CCalenLunarLocalizedInfo::ELunarMonthAndDay );
   181 	subLabels.AppendL( CCalenLunarLocalizedInfo::ELunarMonthAndDay );
   180     subLabels.AppendL( CCalenLunarLocalizedInfo::EFestival );
   182 	subLabels.AppendL( CCalenLunarLocalizedInfo::EFestival );
   181     subLabels.AppendL( CCalenLunarLocalizedInfo::ESolarTerm );
   183 	subLabels.AppendL( CCalenLunarLocalizedInfo::ESolarTerm );
   182     
   184 
   183     // ASSERT that all prioritized fields can be found from subLabels
   185 	// ASSERT that all prioritized fields can be found from subLabels
   184     for ( TInt i=0; i < aPrioritizedFields.Count(); i++)
   186 	for ( TInt i=0; i < aPrioritizedFields.Count(); i++)
   185         {
   187 	{
   186         ASSERT( subLabels.Find( aPrioritizedFields[i] ) >= 0 ); 
   188 		ASSERT( subLabels.Find( aPrioritizedFields[i] ) >= KZero );
   187         }
   189 	}
   188 
   190 
   189 	// Initialize substring array 
   191 	// Initialize substring array 
   190 	CPtrCArray* subs = new (ELeave) CPtrCArray(10);
   192 	CPtrCArray* subs = new (ELeave) CPtrCArray(10);
   191 	CleanupStack::PushL( subs );
   193 	CleanupStack::PushL( subs );
   192 	for ( TInt i = 0; i < subLabels.Count(); i++) 
   194 	for ( TInt i = 0; i < subLabels.Count(); i++) 
   193 		{
   195 	{
   194 		subs->AppendL( TPtrC( KNullDesC ) );
   196 		subs->AppendL( TPtrC( KNullDesC ) );
   195 		}
   197 	}
   196 	// subs->InsertL( 0, TPtrC( KNullDesC ), 5 );
   198 
   197 	
       
   198 	// Set wanted fields to substring array
   199 	// Set wanted fields to substring array
   199 	for ( TInt i = 0; i < aPrioritizedFields.Count(); i++)
   200 	for ( TInt i = 0; i < aPrioritizedFields.Count(); i++)
   200 		{
   201 	{
   201 		CCalenLunarLocalizedInfo::TField field = aPrioritizedFields[i];
   202 		CCalenLunarLocalizedInfo::TField field = aPrioritizedFields[i];
   202 		TInt subIx = subLabels.Find( field );
   203 		TInt subIx = subLabels.Find( field );
   203 		// Replace 
   204 		// Replace 
   204 		subs->Delete(subIx);
   205 		subs->Delete(subIx);
   205 		RDebug::Print( _L("A sub count  %d"), subs->Count() );
   206 		RDebug::Print( _L("A sub count  %d"), subs->Count() );
   206 		subs->InsertL(subIx, TPtrC( aLocInfo.GetField( field ) ) );
   207 		subs->InsertL(subIx, TPtrC( aLocInfo.GetField( field ) ) );
   207 		RDebug::Print( _L("B sub count %d"), subs->Count() );
   208 		RDebug::Print( _L("B sub count %d"), subs->Count() );
   208 		RDebug::Print( _L("B field %S"), &(subs->At(subIx)) );
   209 		RDebug::Print( _L("B field %S"), &(subs->At(subIx)) );
   209 		
   210 
   210 		}
   211 	}
   211 	
   212 	TBuf<100> textBuf;
   212 	// Format all fields to extra row     
   213 	QStringList textDataStringList;
   213 	HBufC* extraRowFmt = StringLoader::LoadLC( R_CALE_EXTRA_ROW_LUNAR );
   214 	for (TInt i=0; i < subLabels.Count(); i++) {
   214 	
   215 		textBuf = subs->At( i );
   215 	RDebug::RawPrint( *extraRowFmt );
   216 		textDataStringList.append(
   216 	
   217 							QString((QChar*)textBuf.Ptr(),textBuf.Length()));
   217 	TBuf<1000> fmt = *extraRowFmt;
   218 	}
   218 	for (TInt i=0; i < subLabels.Count(); i++)
   219 	// Get the locale specific separator
   219 		{
   220 	QString separator = hbTrId("txt_calendar_preview_title_cale_separator");
   220 		RDebug::Print( _L("Before Format") );
   221 	
   221 		RDebug::RawPrint( fmt );
   222 	// Format all fields to single row 
   222 		StringLoader::Format( iText, 
   223 	QString textDataString;
   223 							  fmt,
   224 	textDataString = hbTrId(
   224 							  i + 1, // %0U is a separator 
   225 						"txt_calendar_preview_title_123242526").arg(
   225 							  subs->At( i ) );
   226 							textDataStringList.at(0)).arg(
   226 		fmt = iText;
   227 								separator).arg(
   227 		RDebug::Print( _L("After Format") );
   228 									textDataStringList.at(1)).arg(
   228 		RDebug::RawPrint( fmt );
   229 										textDataStringList.at(2)).arg(
   229 		}
   230 											textDataStringList.at(3)).arg(
   230 	
   231 												textDataStringList.at(4));
   231 	// Now we have something like "Year of Dog%0U%0U6/11%0U%0U" 
   232 
   232 	// First We need to remove multiple occurences of %0U
   233 	iText = static_cast<const TUint16*> (
   233 	_LIT(KSeparatorFmt, "%0U");
   234 							textDataString.utf16()), textDataString.length();
   234 	
   235 
   235 	CollapseDuplicatesL( iText, 0, KSeparatorFmt );
   236 	// Now we have something like "Year of Dog%2GengYin%2%2"
   236 	RDebug::Print( _L("After collapse") );
   237 	// where %2 is the separator txt_calendar_preview_title_cale_separator
   237 	RDebug::RawPrint( iText );
   238 	// First We need to remove multiple occurences of separator
   238 
   239 	textBuf = static_cast<const TUint16*> (
   239 	// Remove leading and trailing %0U
   240 										separator.utf16()), separator.length();
   240 	// By now, we are sure that there is max 1 %0U in the beginning
   241 	CollapseDuplicatesL( iText, 0, textBuf);
   241 	// and in the end of string.
   242 	
   242 	RemoveLeadingAndTrailingL( iText, KSeparatorFmt );
   243 	// Remove leading and trailing separators
   243 	RDebug::Print( _L("After leading and trailing removal") );
   244 	// Leading separator won't be there but trailing one is there for sure
   244 	RDebug::RawPrint( iText );
   245 	RemoveLeadingAndTrailingL( iText, textBuf );
   245 	
   246 	
   246 
   247 	// CleanUp
   247 	// If there are now separators anymore, then do not fill them
       
   248 	TBool hasSeparators = iText.Find( KSeparatorFmt ) >= 0;
       
   249 	
       
   250 	if ( hasSeparators ) 
       
   251 		{
       
   252 	
       
   253 		// fill in separators
       
   254 		HBufC* separator = StringLoader::LoadLC( R_CALE_LUNAR_SEPARATOR );
       
   255 		fmt = iText;
       
   256 		StringLoader::Format( iText, 
       
   257 							  fmt,
       
   258 							  0, // %0U is a separator 
       
   259 							  *separator );
       
   260 	
       
   261 		RDebug::Print( _L("After separator insert") );
       
   262 		RDebug::RawPrint( iText );
       
   263 		CleanupStack::PopAndDestroy( separator );
       
   264 		}
       
   265 
       
   266 
       
   267 	CleanupStack::PopAndDestroy( extraRowFmt );
       
   268 	CleanupStack::PopAndDestroy( subs );
   248 	CleanupStack::PopAndDestroy( subs );
   269     CleanupStack::PopAndDestroy( &subLabels );
   249 	CleanupStack::PopAndDestroy( &subLabels );
   270     
   250 	TRACE_EXIT_POINT;
   271     TRACE_EXIT_POINT;
   251 	
   272     return iText;
   252 	return iText;
   273     }
   253 }
   274 
   254 
   275 //EOF
   255 //EOF
   276 
   256