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